mep.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2010 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * Managed Entries Plug-in
  42. */
  43. #include "mep.h"
  44. /*
  45. * Plug-in globals
  46. */
  47. static PRCList *g_mep_config = NULL;
  48. static PRRWLock *g_mep_config_lock;
  49. static void *_PluginID = NULL;
  50. static char *_PluginDN = NULL;
  51. static int g_plugin_started = 0;
  52. static Slapi_PluginDesc pdesc = { MEP_FEATURE_DESC,
  53. VENDOR,
  54. DS_PACKAGE_VERSION,
  55. MEP_PLUGIN_DESC };
  56. /*
  57. * Plug-in management functions
  58. */
  59. int mep_init(Slapi_PBlock * pb);
  60. static int mep_start(Slapi_PBlock * pb);
  61. static int mep_close(Slapi_PBlock * pb);
  62. static int mep_postop_init(Slapi_PBlock * pb);
  63. static int mep_internal_postop_init(Slapi_PBlock *pb);
  64. /*
  65. * Operation callbacks (where the real work is done)
  66. */
  67. static int mep_mod_post_op(Slapi_PBlock *pb);
  68. static int mep_add_post_op(Slapi_PBlock *pb);
  69. static int mep_del_post_op(Slapi_PBlock *pb);
  70. static int mep_modrdn_post_op(Slapi_PBlock *pb);
  71. static int mep_pre_op(Slapi_PBlock *pb, int modop);
  72. static int mep_mod_pre_op(Slapi_PBlock *pb);
  73. static int mep_add_pre_op(Slapi_PBlock *pb);
  74. static int mep_del_pre_op(Slapi_PBlock *pb);
  75. static int mep_modrdn_pre_op(Slapi_PBlock *pb);
  76. /*
  77. * Config cache management functions
  78. */
  79. static int mep_load_config();
  80. static void mep_delete_config();
  81. static int mep_parse_config_entry(Slapi_Entry * e, int apply);
  82. static void mep_free_config_entry(struct configEntry ** entry);
  83. /*
  84. * helpers
  85. */
  86. static char *mep_get_dn(Slapi_PBlock * pb);
  87. static int mep_dn_is_config(char *dn);
  88. static int mep_dn_is_template(char *dn);
  89. static void mep_find_config(Slapi_Entry *e, struct configEntry **config);
  90. static void mep_find_config_by_template_dn(const char *template_dn,
  91. struct configEntry **config);
  92. static int mep_oktodo(Slapi_PBlock *pb);
  93. static int mep_isrepl(Slapi_PBlock *pb);
  94. static Slapi_Entry *mep_create_managed_entry(struct configEntry *config,
  95. Slapi_Entry *origin);
  96. static void mep_add_managed_entry(struct configEntry *config,
  97. Slapi_Entry *origin);
  98. static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
  99. Slapi_Entry *origin);
  100. static int mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
  101. char **type, char **value);
  102. static int mep_is_managed_entry(Slapi_Entry *e);
  103. static int mep_is_mapped_attr(Slapi_Entry *template, char *type);
  104. /*
  105. * Config cache locking functions
  106. */
  107. void
  108. mep_config_read_lock()
  109. {
  110. PR_RWLock_Rlock(g_mep_config_lock);
  111. }
  112. void
  113. mep_config_write_lock()
  114. {
  115. PR_RWLock_Wlock(g_mep_config_lock);
  116. }
  117. void
  118. mep_config_unlock()
  119. {
  120. PR_RWLock_Unlock(g_mep_config_lock);
  121. }
  122. /*
  123. * Plugin identity functions
  124. */
  125. void
  126. mep_set_plugin_id(void *pluginID)
  127. {
  128. _PluginID = pluginID;
  129. }
  130. void *
  131. mep_get_plugin_id()
  132. {
  133. return _PluginID;
  134. }
  135. void
  136. mep_set_plugin_dn(char *pluginDN)
  137. {
  138. _PluginDN = pluginDN;
  139. }
  140. char *
  141. mep_get_plugin_dn()
  142. {
  143. return _PluginDN;
  144. }
  145. /*
  146. * Plug-in initialization functions
  147. */
  148. int
  149. mep_init(Slapi_PBlock *pb)
  150. {
  151. int status = 0;
  152. char *plugin_identity = NULL;
  153. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  154. "--> mep_init\n");
  155. /* Store the plugin identity for later use.
  156. * Used for internal operations. */
  157. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  158. PR_ASSERT(plugin_identity);
  159. mep_set_plugin_id(plugin_identity);
  160. /* Register callbacks */
  161. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  162. SLAPI_PLUGIN_VERSION_01) != 0 ||
  163. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  164. (void *) mep_start) != 0 ||
  165. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  166. (void *) mep_close) != 0 ||
  167. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  168. (void *) &pdesc) != 0 ||
  169. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODIFY_FN,
  170. (void *) mep_mod_pre_op) != 0 ||
  171. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN,
  172. (void *) mep_add_pre_op) != 0 ||
  173. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_DELETE_FN,
  174. (void *) mep_del_pre_op) != 0 ||
  175. slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_MODRDN_FN,
  176. (void *) mep_modrdn_pre_op) != 0 ||
  177. slapi_register_plugin("internalpostoperation", /* op type */
  178. 1, /* Enabled */
  179. "mep_init", /* this function desc */
  180. mep_internal_postop_init, /* init func */
  181. MEP_INT_POSTOP_DESC, /* plugin desc */
  182. NULL, /* ? */
  183. plugin_identity /* access control */
  184. ) ||
  185. slapi_register_plugin("postoperation", /* op type */
  186. 1, /* Enabled */
  187. "mep_init", /* this function desc */
  188. mep_postop_init, /* init func for post op */
  189. MEP_POSTOP_DESC, /* plugin desc */
  190. NULL, /* ? */
  191. plugin_identity /* access control */
  192. )
  193. ) {
  194. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  195. "mep_init: failed to register plugin\n");
  196. status = -1;
  197. }
  198. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  199. "<-- mep_init\n");
  200. return status;
  201. }
  202. static int
  203. mep_internal_postop_init(Slapi_PBlock *pb)
  204. {
  205. int status = 0;
  206. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  207. SLAPI_PLUGIN_VERSION_01) != 0 ||
  208. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  209. (void *) &pdesc) != 0 ||
  210. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  211. (void *) mep_add_post_op) != 0 ||
  212. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  213. (void *) mep_del_post_op) != 0 ||
  214. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  215. (void *) mep_mod_post_op) != 0 ||
  216. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  217. (void *) mep_modrdn_post_op) != 0) {
  218. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  219. "mep_internal_postop_init: failed to register plugin\n");
  220. status = -1;
  221. }
  222. return status;
  223. }
  224. static int
  225. mep_postop_init(Slapi_PBlock *pb)
  226. {
  227. int status = 0;
  228. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  229. SLAPI_PLUGIN_VERSION_01) != 0 ||
  230. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  231. (void *) &pdesc) != 0 ||
  232. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
  233. (void *) mep_add_post_op) != 0 ||
  234. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
  235. (void *) mep_del_post_op) != 0 ||
  236. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
  237. (void *) mep_mod_post_op) != 0 ||
  238. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  239. (void *) mep_modrdn_post_op) != 0) {
  240. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  241. "mep_postop_init: failed to register plugin\n");
  242. status = -1;
  243. }
  244. return status;
  245. }
  246. /*
  247. * mep_start()
  248. *
  249. * Creates config lock and loads config cache.
  250. */
  251. static int
  252. mep_start(Slapi_PBlock * pb)
  253. {
  254. char *plugindn = NULL;
  255. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  256. "--> mep_start\n");
  257. /* Check if we're already started */
  258. if (g_plugin_started) {
  259. goto done;
  260. }
  261. g_mep_config_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "mep_config");
  262. if (!g_mep_config_lock) {
  263. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  264. "mep_start: lock creation failed\n");
  265. return -1;
  266. }
  267. /*
  268. * Get the plug-in target dn from the system
  269. * and store it for future use. */
  270. slapi_pblock_get(pb, SLAPI_TARGET_DN, &plugindn);
  271. if (NULL == plugindn || 0 == strlen(plugindn)) {
  272. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  273. "mep_start: unable to retrieve plugin dn\n");
  274. return -1;
  275. }
  276. mep_set_plugin_dn(plugindn);
  277. /*
  278. * Load the config cache
  279. */
  280. g_mep_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  281. PR_INIT_CLIST(g_mep_config);
  282. if (mep_load_config() != 0) {
  283. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  284. "mep_start: unable to load plug-in configuration\n");
  285. return -1;
  286. }
  287. g_plugin_started = 1;
  288. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  289. "managed entries plug-in: ready for service\n");
  290. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  291. "<-- mep_start\n");
  292. done:
  293. return 0;
  294. }
  295. /*
  296. * mep_close()
  297. *
  298. * Cleans up the config cache.
  299. */
  300. static int
  301. mep_close(Slapi_PBlock * pb)
  302. {
  303. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  304. "--> mep_close\n");
  305. mep_delete_config();
  306. slapi_ch_free((void **)&g_mep_config);
  307. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  308. "<-- mep_close\n");
  309. return 0;
  310. }
  311. PRCList *
  312. mep_get_config()
  313. {
  314. return g_mep_config;
  315. }
  316. /*
  317. * config looks like this
  318. * - cn=Managed Entries,cn=plugins,cn=config
  319. * --- cn=user private groups,...
  320. * --- cn=etc,...
  321. */
  322. static int
  323. mep_load_config()
  324. {
  325. int status = 0;
  326. int result;
  327. int i;
  328. Slapi_PBlock *search_pb;
  329. Slapi_Entry **entries = NULL;
  330. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  331. "--> mep_load_config\n");
  332. /* Clear out any old config. */
  333. mep_config_write_lock();
  334. mep_delete_config();
  335. /* Find the config entries beneath our plugin entry. */
  336. search_pb = slapi_pblock_new();
  337. slapi_search_internal_set_pb(search_pb, mep_get_plugin_dn(),
  338. LDAP_SCOPE_SUBTREE, "objectclass=*",
  339. NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
  340. slapi_search_internal_pb(search_pb);
  341. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  342. if (LDAP_SUCCESS != result) {
  343. status = -1;
  344. goto cleanup;
  345. }
  346. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  347. &entries);
  348. if (NULL == entries || NULL == entries[0]) {
  349. /* If there are no config entries, we're done. */
  350. goto cleanup;
  351. }
  352. /* Loop through all of the entries we found and parse them. */
  353. for (i = 0; (entries[i] != NULL); i++) {
  354. /* We don't care about the status here because we may have
  355. * some invalid config entries, but we just want to continue
  356. * looking for valid ones. */
  357. mep_parse_config_entry(entries[i], 1);
  358. }
  359. cleanup:
  360. slapi_free_search_results_internal(search_pb);
  361. slapi_pblock_destroy(search_pb);
  362. mep_config_unlock();
  363. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  364. "<-- mep_load_config\n");
  365. return status;
  366. }
  367. /*
  368. * mep_parse_config_entry()
  369. *
  370. * Parses a single config entry. If apply is non-zero, then
  371. * we will load and start using the new config. You can simply
  372. * validate config without making any changes by setting apply
  373. * to 0.
  374. *
  375. * Returns 0 if the entry is valid and -1 if it is invalid.
  376. */
  377. static int
  378. mep_parse_config_entry(Slapi_Entry * e, int apply)
  379. {
  380. char *value;
  381. struct configEntry *entry = NULL;
  382. struct configEntry *config_entry;
  383. PRCList *list;
  384. int entry_added = 0;
  385. int ret = 0;
  386. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  387. "--> mep_parse_config_entry\n");
  388. /* If this is the main plug-in
  389. * config entry, just bail. */
  390. if (strcasecmp(mep_get_plugin_dn(), slapi_entry_get_ndn(e)) == 0) {
  391. ret = -1;
  392. goto bail;
  393. }
  394. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  395. if (NULL == entry) {
  396. ret = -1;
  397. goto bail;
  398. }
  399. value = slapi_entry_get_ndn(e);
  400. if (value) {
  401. entry->dn = slapi_ch_strdup(value);
  402. } else {
  403. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  404. "mep_parse_config_entry: Error "
  405. "reading dn from config entry\n");
  406. ret = -1;
  407. goto bail;
  408. }
  409. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  410. "----------> dn [%s]\n", entry->dn);
  411. /* Load the origin scope */
  412. value = slapi_entry_attr_get_charptr(e, MEP_SCOPE_TYPE);
  413. if (value) {
  414. entry->origin_scope = value;
  415. } else {
  416. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  417. "mep_parse_config_entry: The %s config "
  418. "setting is required for config entry \"%s\".\n",
  419. MEP_SCOPE_TYPE, entry->dn);
  420. ret = -1;
  421. goto bail;
  422. }
  423. /* Load the origin filter */
  424. value = slapi_entry_attr_get_charptr(e, MEP_FILTER_TYPE);
  425. if (value) {
  426. /* Convert to a Slapi_Filter to improve performance. */
  427. if (NULL == (entry->origin_filter = slapi_str2filter(value))) {
  428. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM ,
  429. "mep_parse_config_entry: Invalid search filter in "
  430. "%s config setting for config entry \"%s\" "
  431. "(filter = \"%s\").\n", MEP_FILTER_TYPE, entry->dn, value);
  432. ret = -1;
  433. }
  434. slapi_ch_free_string(&value);
  435. if (ret != 0) {
  436. goto bail;
  437. }
  438. } else {
  439. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  440. "mep_parse_config_entry: The %s config "
  441. "setting is required for config entry \"%s\".\n",
  442. MEP_FILTER_TYPE, entry->dn);
  443. ret = -1;
  444. goto bail;
  445. }
  446. /* Load the managed base */
  447. value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BASE_TYPE);
  448. if (value) {
  449. entry->managed_base = value;
  450. } else {
  451. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  452. "mep_parse_config_entry: The %s config "
  453. "setting is required for config entry \"%s\".\n",
  454. MEP_MANAGED_BASE_TYPE, entry->dn);
  455. ret = -1;
  456. goto bail;
  457. }
  458. /* Load the managed template */
  459. value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_TEMPLATE_TYPE);
  460. if (value) {
  461. Slapi_Entry *test_entry = NULL;
  462. Slapi_DN *template_sdn = NULL;
  463. entry->template_dn = value;
  464. /* Fetch the managed entry template */
  465. template_sdn = slapi_sdn_new_dn_byref(entry->template_dn);
  466. slapi_search_internal_get_entry(template_sdn, 0,
  467. &entry->template_entry, mep_get_plugin_id());
  468. slapi_sdn_free(&template_sdn);
  469. if (entry->template_entry == NULL) {
  470. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  471. "mep_parse_config_entry: The managed entry "
  472. "template \"%s\" does not exist. Please "
  473. "add it or correct the %s config setting for "
  474. "config entry \"%s\"\n", entry->template_dn,
  475. MEP_MANAGED_TEMPLATE_TYPE, entry->dn);
  476. ret = -1;
  477. goto bail;
  478. }
  479. /* Validate the template entry by creating a test managed
  480. * entry and running a schema check on it */
  481. test_entry = mep_create_managed_entry(entry, NULL);
  482. if (test_entry == NULL) {
  483. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  484. "mep_parse_config_entry: Unable to create "
  485. "a test managed entry from managed entry "
  486. "template \"%s\". Please check the template "
  487. "entry for errors.\n", entry->template_dn);
  488. ret = -1;
  489. goto bail;
  490. }
  491. if (slapi_entry_schema_check(NULL, test_entry) != 0) {
  492. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  493. "mep_parse_config_entry: Test managed "
  494. "entry created from managed entry template "
  495. "\"%s\" violates the schema. Please check "
  496. "the template entry for schema errors.\n",
  497. entry->template_dn);
  498. slapi_entry_free(test_entry);
  499. ret = -1;
  500. goto bail;
  501. }
  502. /* Dispose of the test entry */
  503. slapi_entry_free(test_entry);
  504. } else {
  505. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  506. "mep_parse_config_entry: The %s config "
  507. "setting is required for config entry \"%s\".\n",
  508. MEP_MANAGED_TEMPLATE_TYPE, entry->dn);
  509. ret = -1;
  510. goto bail;
  511. }
  512. /* If we were only called to validate config, we can
  513. * just bail out before applying the config changes */
  514. if (apply == 0) {
  515. goto bail;
  516. }
  517. /* Add the config object to the list. We order by scope. */
  518. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  519. list = PR_LIST_HEAD(g_mep_config);
  520. while (list != g_mep_config) {
  521. config_entry = (struct configEntry *) list;
  522. /* If the config entry we are adding has a scope that is
  523. * a child of the scope of the current list item, we insert
  524. * the entry before that list item. */
  525. if (slapi_dn_issuffix(entry->origin_scope, config_entry->origin_scope)) {
  526. PR_INSERT_BEFORE(&(entry->list), list);
  527. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  528. "store [%s] before [%s] \n", entry->dn,
  529. config_entry->dn);
  530. entry_added = 1;
  531. break;
  532. }
  533. list = PR_NEXT_LINK(list);
  534. /* If we hit the end of the list, add to the tail. */
  535. if (g_mep_config == list) {
  536. PR_INSERT_BEFORE(&(entry->list), list);
  537. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  538. "store [%s] at tail\n", entry->dn);
  539. entry_added = 1;
  540. break;
  541. }
  542. }
  543. } else {
  544. /* first entry */
  545. PR_INSERT_LINK(&(entry->list), g_mep_config);
  546. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  547. "store [%s] at head \n", entry->dn);
  548. entry_added = 1;
  549. }
  550. bail:
  551. if (0 == entry_added) {
  552. /* Don't log error if we weren't asked to apply config */
  553. if ((apply != 0) && (entry != NULL)) {
  554. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  555. "mep_parse_config_entry: Invalid config entry "
  556. "[%s] skipped\n", entry->dn);
  557. }
  558. mep_free_config_entry(&entry);
  559. } else {
  560. ret = 0;
  561. }
  562. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  563. "<-- mep_parse_config_entry\n");
  564. return ret;
  565. }
  566. static void
  567. mep_free_config_entry(struct configEntry ** entry)
  568. {
  569. struct configEntry *e = *entry;
  570. if (e == NULL)
  571. return;
  572. if (e->dn) {
  573. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  574. "freeing config entry [%s]\n", e->dn);
  575. slapi_ch_free_string(&e->dn);
  576. }
  577. if (e->origin_scope) {
  578. slapi_ch_free_string(&e->origin_scope);
  579. }
  580. if (e->origin_filter) {
  581. slapi_filter_free(e->origin_filter, 1);
  582. }
  583. if (e->managed_base) {
  584. slapi_ch_free_string(&e->managed_base);
  585. }
  586. if (e->template_dn) {
  587. slapi_ch_free_string(&e->template_dn);
  588. }
  589. if (e->template_entry) {
  590. slapi_entry_free(e->template_entry);
  591. }
  592. slapi_ch_free((void **) entry);
  593. }
  594. static void
  595. mep_delete_configEntry(PRCList *entry)
  596. {
  597. PR_REMOVE_LINK(entry);
  598. mep_free_config_entry((struct configEntry **) &entry);
  599. }
  600. static void
  601. mep_delete_config()
  602. {
  603. PRCList *list;
  604. /* Delete the config cache. */
  605. while (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  606. list = PR_LIST_HEAD(g_mep_config);
  607. mep_delete_configEntry(list);
  608. }
  609. return;
  610. }
  611. /*
  612. * Helper functions
  613. */
  614. static char *
  615. mep_get_dn(Slapi_PBlock * pb)
  616. {
  617. char *dn = 0;
  618. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  619. "--> mep_get_dn\n");
  620. if (slapi_pblock_get(pb, SLAPI_TARGET_DN, &dn)) {
  621. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  622. "mep_get_dn: failed to get dn of changed entry");
  623. goto bail;
  624. }
  625. bail:
  626. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  627. "<-- mep_get_dn\n");
  628. return dn;
  629. }
  630. /*
  631. * mep_dn_is_config()
  632. *
  633. * Checks if dn is a managed entries config entry.
  634. */
  635. static int
  636. mep_dn_is_config(char *dn)
  637. {
  638. int ret = 0;
  639. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  640. "--> mep_dn_is_config\n");
  641. /* Return 1 if the passed in dn is a child of the main
  642. * plugin config entry. */
  643. if (slapi_dn_issuffix(dn, mep_get_plugin_dn()) &&
  644. strcasecmp(dn, mep_get_plugin_dn())) {
  645. ret = 1;
  646. }
  647. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  648. "<-- mep_dn_is_config\n");
  649. return ret;
  650. }
  651. /*
  652. * mep_dn_is_template()
  653. *
  654. * Checks if dn is a managed entries template.
  655. */
  656. static int
  657. mep_dn_is_template(char *dn)
  658. {
  659. int ret = 0;
  660. PRCList *list = NULL;
  661. Slapi_DN *config_sdn = NULL;
  662. Slapi_DN *sdn = slapi_sdn_new_dn_byref(dn);
  663. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  664. list = PR_LIST_HEAD(g_mep_config);
  665. while (list != g_mep_config) {
  666. config_sdn = slapi_sdn_new_dn_byref(((struct configEntry *)list)->template_dn);
  667. if (slapi_sdn_compare(config_sdn, sdn) == 0) {
  668. ret = 1;
  669. slapi_sdn_free(&config_sdn);
  670. break;
  671. } else {
  672. list = PR_NEXT_LINK(list);
  673. slapi_sdn_free(&config_sdn);
  674. }
  675. }
  676. }
  677. slapi_sdn_free(&sdn);
  678. return ret;
  679. }
  680. /*
  681. * mep_find_config()
  682. *
  683. * Finds the appropriate config entry for a given entry
  684. * by checking if the entry meets the origin scope and
  685. * filter requirements of any config entry. A read lock
  686. * must be held on the config before calling this function.
  687. * The configEntry that is returned is a pointer to the
  688. * actual config entry in the config cache. It should not
  689. * be modified in any way. The read lock should not be
  690. * released until you are finished with the config entry
  691. * that is returned.
  692. *
  693. * Returns NULL if no applicable config entry is found.
  694. */
  695. static void
  696. mep_find_config(Slapi_Entry *e, struct configEntry **config)
  697. {
  698. PRCList *list = NULL;
  699. char *dn = NULL;
  700. *config = NULL;
  701. if (e && !PR_CLIST_IS_EMPTY(g_mep_config)) {
  702. dn = slapi_entry_get_dn(e);
  703. list = PR_LIST_HEAD(g_mep_config);
  704. while (list != g_mep_config) {
  705. /* See if the dn is within the scope of this config entry
  706. * in addition to matching the origin filter. */
  707. if (slapi_dn_issuffix(dn, ((struct configEntry *)list)->origin_scope) &&
  708. (slapi_filter_test_simple(e, ((struct configEntry *)list)->origin_filter) == 0)) {
  709. *config = (struct configEntry *)list;
  710. break;
  711. }
  712. list = PR_NEXT_LINK(list);
  713. }
  714. }
  715. }
  716. /*
  717. * mep_find_config_by_template_dn()
  718. *
  719. * Finds the config entry associated with a particular
  720. * template dn. A read lock must be held on the config
  721. * before calling this function. The configEntry that
  722. * us returned is a pointer to the actual config entry
  723. * in the config cache. It should not be modified in
  724. * any way. The read lock should not be released until
  725. * you are finished with the config entry that is returned.
  726. *
  727. * Returns NULL if no applicable config entry is found.
  728. */
  729. static void
  730. mep_find_config_by_template_dn(const char *template_dn,
  731. struct configEntry **config)
  732. {
  733. PRCList *list = NULL;
  734. Slapi_DN *config_sdn = NULL;
  735. Slapi_DN *template_sdn = slapi_sdn_new_dn_byref(template_dn);
  736. *config = NULL;
  737. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  738. list = PR_LIST_HEAD(g_mep_config);
  739. while (list != g_mep_config) {
  740. config_sdn = slapi_sdn_new_dn_byref(((struct configEntry *)list)->template_dn);
  741. if (slapi_sdn_compare(config_sdn, template_sdn) == 0) {
  742. *config = (struct configEntry *)list;
  743. slapi_sdn_free(&config_sdn);
  744. break;
  745. } else {
  746. list = PR_NEXT_LINK(list);
  747. slapi_sdn_free(&config_sdn);
  748. }
  749. }
  750. }
  751. slapi_sdn_free(&template_sdn);
  752. }
  753. /*
  754. * mep_oktodo()
  755. *
  756. * Check if we want to process this operation. We need to be
  757. * sure that the operation succeeded.
  758. */
  759. static int
  760. mep_oktodo(Slapi_PBlock *pb)
  761. {
  762. int ret = 1;
  763. int oprc = 0;
  764. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  765. "--> mep_oktodo\n" );
  766. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  767. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  768. "mep_oktodo: could not get parameters\n" );
  769. ret = -1;
  770. }
  771. /* This plugin should only execute if the operation succeeded. */
  772. if(oprc != 0) {
  773. ret = 0;
  774. }
  775. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  776. "<-- mep_oktodo\n" );
  777. return ret;
  778. }
  779. /*
  780. * mep_isrepl()
  781. *
  782. * Returns 1 if the operation associated with pb
  783. * is a replicated op. Returns 0 otherwise.
  784. */
  785. static int
  786. mep_isrepl(Slapi_PBlock *pb)
  787. {
  788. int is_repl = 0;
  789. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  790. "--> mep_isrepl\n" );
  791. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  792. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  793. "<-- mep_isrepl\n" );
  794. return is_repl;
  795. }
  796. /*
  797. * mep_create_managed_entry()
  798. *
  799. * Creates a managed entry from a specified template and origin
  800. * entry. If an origin entry is not passed in, the values of
  801. * mapped attributes will not be filled in and the addition of
  802. * a backlink to the origin is not added. This is useful for
  803. * creating a test managed entry for template validation.
  804. */
  805. static Slapi_Entry *
  806. mep_create_managed_entry(struct configEntry *config, Slapi_Entry *origin)
  807. {
  808. Slapi_Entry *managed_entry = NULL;
  809. Slapi_Entry *template = NULL;
  810. char *rdn_type = NULL;
  811. char **vals = NULL;
  812. char *type = NULL;
  813. char *value = NULL;
  814. Slapi_Value *sval = NULL;
  815. int found_rdn_map = 0;
  816. int i = 0;
  817. int err = 0;
  818. /* If no template was supplied, there's nothing we can do. */
  819. if ((config == NULL) || (config->template_entry == NULL)) {
  820. return NULL;
  821. } else {
  822. template = config->template_entry;
  823. }
  824. /* Ensure that a RDN type was specified in the template. */
  825. if ((rdn_type = slapi_entry_attr_get_charptr(template, MEP_RDN_ATTR_TYPE)) == NULL) {
  826. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  827. "mep_create_managed_entry: The %s config attribute "
  828. "was not found in template \"%s\". This attribute "
  829. "is required.\n", MEP_RDN_ATTR_TYPE, config->template_dn);
  830. err = 1;
  831. goto done;
  832. }
  833. /* Create the entry to be returned. */
  834. managed_entry = slapi_entry_alloc();
  835. slapi_entry_init(managed_entry, NULL, NULL);
  836. /* Add all of the static attributes from the template to the newly
  837. * created managed entry. */
  838. vals = slapi_entry_attr_get_charray(template, MEP_STATIC_ATTR_TYPE);
  839. for (i = 0; vals && vals[i]; ++i) {
  840. struct berval bvtype = {0, NULL}, bvvalue = {0, NULL};
  841. int freeval = 0;
  842. if (slapi_ldif_parse_line(vals[i], &bvtype, &bvvalue, &freeval) != 0) {
  843. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  844. "mep_create_managed_entry: Value for %s config setting "
  845. "is not in the correct format in template \"%s\". "
  846. "(value: \"%s\")\n", MEP_STATIC_ATTR_TYPE,
  847. config->template_dn, vals[i]);
  848. err = 1;
  849. goto done;
  850. } else {
  851. /* Create a berval and add the value to the entry. */
  852. sval = slapi_value_new_berval(&bvvalue);
  853. slapi_entry_add_value(managed_entry, bvtype.bv_val, sval);
  854. slapi_value_free(&sval);
  855. /* Set type and value to NULL so they don't get
  856. * free'd by mep_parse_mapped_attr(). */
  857. if (freeval) {
  858. slapi_ch_free_string(&bvvalue.bv_val);
  859. }
  860. type = NULL;
  861. value = NULL;
  862. }
  863. }
  864. /* Clear out vals so we can use them again */
  865. slapi_ch_array_free(vals);
  866. /* Add the mapped attributes to the newly created managed entry. */
  867. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  868. for (i = 0; vals && vals[i]; ++i) {
  869. if (mep_parse_mapped_attr(vals[i], origin, &type, &value) == 0) {
  870. /* Add the attribute to the managed entry. */
  871. slapi_entry_add_string(managed_entry, type, value);
  872. /* Check if this type is the RDN type. */
  873. if (slapi_attr_type_cmp(type, rdn_type, SLAPI_TYPE_CMP_EXACT) == 0) {
  874. found_rdn_map = 1;
  875. }
  876. slapi_ch_free_string(&type);
  877. slapi_ch_free_string(&value);
  878. } else {
  879. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  880. "mep_create_managed_entry: Error parsing mapped attribute "
  881. "in template \"%s\".\n", config->template_dn);
  882. err = 1;
  883. goto done;
  884. }
  885. }
  886. /* The RDN attribute must be a mapped attribute. If we didn't find it,
  887. * we need to bail. */
  888. if (!found_rdn_map) {
  889. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  890. "mep_create_managed_entry: The RDN type \"%s\" "
  891. "was not found as a mapped attribute in template "
  892. "\"%s\". It must be a mapped attribute.\n",
  893. rdn_type, config->template_dn);
  894. err = 1;
  895. goto done;
  896. } else {
  897. /* Build the DN and set it in the entry. */
  898. char *dn = NULL;
  899. char *rdn_val = NULL;
  900. /* If an origin entry was supplied, the RDN value will be
  901. * the mapped value. If no origin entry was supplied, the
  902. * value will be the mapping rule from the template. */
  903. rdn_val = slapi_entry_attr_get_charptr(managed_entry, rdn_type);
  904. /* Create the DN using the mapped RDN value
  905. * and the base specified in the config. */
  906. dn = slapi_create_dn_string("%s=%s,%s", rdn_type, rdn_val, config->managed_base);
  907. slapi_ch_free_string(&rdn_val);
  908. if (dn != NULL) {
  909. slapi_sdn_set_dn_passin(slapi_entry_get_sdn(managed_entry), dn);
  910. } else {
  911. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  912. "mep_create_managed_entry: Error setting DN "
  913. "in managed entry based off of template entry "
  914. "\"%s\" (origin entry \"%s\").\n",
  915. config->template_dn,
  916. origin ? slapi_entry_get_dn(origin) : "NULL");
  917. err = 1;
  918. goto done;
  919. }
  920. }
  921. /* If an origin entry was supplied, set a backpointer to the
  922. * origin in the managed entry. */
  923. if (origin) {
  924. slapi_entry_add_string(managed_entry,
  925. SLAPI_ATTR_OBJECTCLASS, MEP_MANAGED_OC);
  926. slapi_entry_add_string(managed_entry, MEP_MANAGED_BY_TYPE,
  927. slapi_entry_get_dn(origin));
  928. }
  929. done:
  930. slapi_ch_array_free(vals);
  931. slapi_ch_free_string(&rdn_type);
  932. if (err != 0) {
  933. slapi_entry_free(managed_entry);
  934. managed_entry = NULL;
  935. }
  936. return managed_entry;
  937. }
  938. /*
  939. * mep_add_managed_entry()
  940. *
  941. * Creates and adds a managed entry to the database. The
  942. * origin entry will also be modified to add a link to the
  943. * newly created managed entry.
  944. */
  945. static void
  946. mep_add_managed_entry(struct configEntry *config,
  947. Slapi_Entry *origin)
  948. {
  949. Slapi_Entry *managed_entry = NULL;
  950. char *managed_dn = NULL;
  951. Slapi_PBlock *mod_pb = slapi_pblock_new();
  952. int result = LDAP_SUCCESS;
  953. /* Create the managed entry */
  954. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  955. "mep_add_managed_entry: Creating a managed "
  956. "entry from origin entry \"%s\" using "
  957. "config \"%s\".\n", slapi_entry_get_dn(origin),
  958. config->dn);
  959. managed_entry = mep_create_managed_entry(config, origin);
  960. if (managed_entry == NULL) {
  961. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  962. "mep_add_managed_entry: Unable to create a managed "
  963. "entry from origin entry \"%s\" using config "
  964. "\"%s\".\n", slapi_entry_get_dn(origin), config->dn);
  965. } else {
  966. /* Copy the managed entry DN to use when
  967. * creating the pointer attribute. */
  968. managed_dn = slapi_ch_strdup(slapi_entry_get_dn(managed_entry));
  969. /* Add managed entry to db. The entry will be consumed. */
  970. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  971. "Adding managed entry \"%s\" for origin "
  972. "entry \"%s\"\n.", managed_dn, slapi_entry_get_dn(origin));
  973. slapi_add_entry_internal_set_pb(mod_pb, managed_entry, NULL,
  974. mep_get_plugin_id(), 0);
  975. slapi_add_internal_pb(mod_pb);
  976. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  977. if (result != LDAP_SUCCESS) {
  978. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  979. "mep_add_managed_entry: Unable to add managed "
  980. "entry \"%s\" for origin entry \"%s\" (%s).\n",
  981. managed_dn, slapi_entry_get_dn(origin),
  982. ldap_err2string(result));
  983. } else {
  984. /* Add forward link to origin entry. */
  985. LDAPMod oc_mod;
  986. LDAPMod pointer_mod;
  987. LDAPMod *mods[3];
  988. char *oc_vals[2];
  989. char *pointer_vals[2];
  990. /* Clear out the pblock for reuse. */
  991. slapi_pblock_init(mod_pb);
  992. /* Add the origin entry objectclass. */
  993. oc_vals[0] = MEP_ORIGIN_OC;
  994. oc_vals[1] = 0;
  995. oc_mod.mod_op = LDAP_MOD_ADD;
  996. oc_mod.mod_type = SLAPI_ATTR_OBJECTCLASS;
  997. oc_mod.mod_values = oc_vals;
  998. /* Add a pointer to the managed entry. */
  999. pointer_vals[0] = managed_dn;
  1000. pointer_vals[1] = 0;
  1001. pointer_mod.mod_op = LDAP_MOD_ADD;
  1002. pointer_mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1003. pointer_mod.mod_values = pointer_vals;
  1004. mods[0] = &oc_mod;
  1005. mods[1] = &pointer_mod;
  1006. mods[2] = 0;
  1007. /* Perform the modify operation. */
  1008. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1009. "Adding %s pointer to \"%s\" in entry \"%s\"\n.",
  1010. MEP_MANAGED_ENTRY_TYPE, managed_dn, slapi_entry_get_dn(origin));
  1011. slapi_modify_internal_set_pb(mod_pb, slapi_entry_get_dn(origin),
  1012. mods, 0, 0, mep_get_plugin_id(), 0);
  1013. slapi_modify_internal_pb(mod_pb);
  1014. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1015. if (result != LDAP_SUCCESS) {
  1016. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1017. "mep_add_managed_entry: Unable to add pointer to "
  1018. "managed entry \"%s\" in origin entry \"%s\" "
  1019. "(%s).\n", managed_dn, slapi_entry_get_dn(origin),
  1020. ldap_err2string(result));
  1021. }
  1022. }
  1023. }
  1024. slapi_ch_free_string(&managed_dn);
  1025. slapi_pblock_destroy(mod_pb);
  1026. }
  1027. /*
  1028. * mep_get_mapped_mods()
  1029. *
  1030. * Creates the modifications needed to update the mapped values
  1031. * in a managed entry. It is up to the caller to free the
  1032. * returned mods when it is finished using them.
  1033. */
  1034. static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
  1035. Slapi_Entry *origin)
  1036. {
  1037. Slapi_Mods *smods = NULL;
  1038. Slapi_Entry *template = NULL;
  1039. Slapi_Attr *attr = NULL;
  1040. char **vals = NULL;
  1041. char *type = NULL;
  1042. char *value = NULL;
  1043. int i = 0;
  1044. /* If no template was supplied, there's nothing we can do. */
  1045. if (origin == NULL || config == NULL || config->template_entry == NULL) {
  1046. return NULL;
  1047. } else {
  1048. template = config->template_entry;
  1049. }
  1050. /* See how many mods we will have and initialize the smods. */
  1051. if (slapi_entry_attr_find(config->template_entry, MEP_MAPPED_ATTR_TYPE, &attr) == 0) {
  1052. int numvals = 0;
  1053. slapi_attr_get_numvalues(attr, &numvals);
  1054. smods = slapi_mods_new();
  1055. slapi_mods_init(smods, numvals);
  1056. }
  1057. /* Go through the template and find the mapped attrs. */
  1058. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1059. for (i = 0; vals && vals[i]; ++i) {
  1060. if (mep_parse_mapped_attr(vals[i], origin, &type, &value) == 0) {
  1061. /* Add a modify that replaces all values with the new value. */
  1062. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, type, value);
  1063. slapi_ch_free_string(&type);
  1064. slapi_ch_free_string(&value);
  1065. } else {
  1066. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1067. "mep_get_mapped_mods: Error parsing mapped attribute "
  1068. "in template \"%s\".\n", config->template_dn);
  1069. slapi_mods_free(&smods);
  1070. goto bail;
  1071. }
  1072. }
  1073. bail:
  1074. slapi_ch_array_free(vals);
  1075. return smods;
  1076. }
  1077. /*
  1078. * mep_parse_mapped_attr()
  1079. *
  1080. * Parses a mapped attribute setting from a template and
  1081. * fills in the type and value based off of the origin
  1082. * entry. If an origin entry is not supplied, the value
  1083. * is simply the mapping rule.
  1084. */
  1085. static int
  1086. mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
  1087. char **type, char **value)
  1088. {
  1089. int ret = 0;
  1090. char *p = NULL;
  1091. char *pre_str = NULL;
  1092. char *post_str = NULL;
  1093. char *end = NULL;
  1094. char *var_start = NULL;
  1095. char *map_type = NULL;
  1096. /* Clear out any existing type or value. */
  1097. slapi_ch_free_string(type);
  1098. slapi_ch_free_string(value);
  1099. /* split out the type from the value (use the first ':') */
  1100. if ((p = strchr(mapping, ':')) == NULL) {
  1101. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1102. "mep_parse_mapped_attr: Value for mapped attribute "
  1103. "is not in the correct format. (value: \"%s\").\n",
  1104. mapping);
  1105. ret = 1;
  1106. goto bail;
  1107. }
  1108. /* Ensure the type is not empty. */
  1109. if (p == mapping) {
  1110. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1111. "mep_parse_mapped_attr: Value for mapped attribute "
  1112. "is not in the correct format. The type is missing. "
  1113. "(value: \"%s\").\n",
  1114. mapping);
  1115. ret = 1;
  1116. goto bail;
  1117. }
  1118. /* Terminate the type so we can use it as a string. */
  1119. *p = '\0';
  1120. /* Duplicate the type to be returned. */
  1121. *type = slapi_ch_strdup(mapping);
  1122. /* Advance p to point to the beginning of the value. */
  1123. p++;
  1124. while (*p == ' ') {
  1125. p++;
  1126. }
  1127. pre_str = p;
  1128. /* Make end point to the last character that we want in the value. */
  1129. end = p + strlen(p) - 1;
  1130. /* Find the variable that we need to substitute. */
  1131. for (; p <= end; p++) {
  1132. if (*p == '$') {
  1133. if (p == end) {
  1134. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1135. "mep_parse_mapped_attr: Invalid mapped "
  1136. "attribute value for type \"%s\".\n", mapping);
  1137. ret = 1;
  1138. goto bail;
  1139. }
  1140. if (*(p + 1) == '$') {
  1141. /* This is an escaped $. Eliminate the escape character
  1142. * to prevent if from being a part of the value. */
  1143. p++;
  1144. memmove(p, p+1, end-(p+1)+1);
  1145. *end = '\0';
  1146. end--;
  1147. } else {
  1148. int quoted = 0;
  1149. /* We found a variable. Terminate the pre
  1150. * string and process the variable. */
  1151. *p = '\0';
  1152. p++;
  1153. /* Check if the variable name is quoted. If it is, we skip past
  1154. * the quoting brace to avoid putting it in the mapped value. */
  1155. if (*p == '{') {
  1156. quoted = 1;
  1157. if (p < end) {
  1158. p++;
  1159. } else {
  1160. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1161. "mep_parse_mapped_attr: Invalid mapped "
  1162. "attribute value for type \"%s\".\n", mapping);
  1163. ret = 1;
  1164. goto bail;
  1165. }
  1166. }
  1167. /* We should be pointing at the variable name now. */
  1168. var_start = p;
  1169. /* Move the pointer to the end of the variable name. We
  1170. * stop at the first character that is not legal for use
  1171. * in an attribute description. */
  1172. while ((p < end) && IS_ATTRDESC_CHAR(*p)) {
  1173. p++;
  1174. }
  1175. /* If the variable is quoted and this is not a closing
  1176. * brace, there is a syntax error in the mapping rule. */
  1177. if (quoted && (*p != '}')) {
  1178. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1179. "mep_parse_mapped_attr: Invalid mapped "
  1180. "attribute value for type \"%s\".\n", mapping);
  1181. ret = 1;
  1182. goto bail;
  1183. }
  1184. /* Check for a missing variable name. */
  1185. if (p == var_start) {
  1186. break;
  1187. }
  1188. if (p == end) {
  1189. /* Set the map type. In this case, p could be
  1190. * pointing at either the last character of the
  1191. * map type, or at the first character after the
  1192. * map type. If the character is valid for use
  1193. * in an attribute description, we consider it
  1194. * to be a part of the map type. */
  1195. if (IS_ATTRDESC_CHAR(*p)) {
  1196. map_type = strndup(var_start, p - var_start + 1);
  1197. /* There is no post string. */
  1198. post_str = NULL;
  1199. } else {
  1200. map_type = strndup(var_start, p - var_start);
  1201. post_str = p;
  1202. }
  1203. } else {
  1204. /* Set the map type. In this case, p is pointing
  1205. * at the first character after the map type. */
  1206. map_type = strndup(var_start, p - var_start);
  1207. /* If the variable is quoted, don't include
  1208. * the closing brace in the post string. */
  1209. if (quoted) {
  1210. post_str = p+1;
  1211. } else {
  1212. post_str = p;
  1213. }
  1214. }
  1215. /* Process the post string to remove any escapes. */
  1216. for (p = post_str; p && (p <= end); p++) {
  1217. if (*p == '$') {
  1218. if ((p == end) || (*(p+1) != '$')) {
  1219. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1220. "mep_parse_mapped_attr: Invalid mapped "
  1221. "attribute value for type \"%s\".\n", mapping);
  1222. ret = 1;
  1223. goto bail;
  1224. } else {
  1225. /* This is an escaped '$'. Remove the escape char. */
  1226. p++;
  1227. memmove(p, p+1, end-(p+1)+1);
  1228. *end = '\0';
  1229. end--;
  1230. }
  1231. }
  1232. }
  1233. /* We only support a single variable, so we're done. */
  1234. break;
  1235. }
  1236. }
  1237. }
  1238. if (map_type) {
  1239. if (origin) {
  1240. char *map_val = NULL;
  1241. int freeit = 0;
  1242. /* If the map type is dn, fetch the origin dn. */
  1243. if (slapi_attr_type_cmp(map_type, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1244. map_val = slapi_entry_get_ndn(origin);
  1245. } else {
  1246. map_val = slapi_entry_attr_get_charptr(origin, map_type);
  1247. freeit = 1;
  1248. }
  1249. if (map_val) {
  1250. /* Create the new mapped value. */
  1251. *value = slapi_ch_smprintf("%s%s%s", pre_str,
  1252. map_val, post_str ? post_str : "");
  1253. if (freeit) {
  1254. slapi_ch_free_string(&map_val);
  1255. }
  1256. } else {
  1257. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1258. "mep_parse_mapped_attr: Mapped attribute \"%s\" "
  1259. "is not present in origin entry \"%s\". Please "
  1260. "correct template to only map attributes "
  1261. "required by the schema.\n", map_type,
  1262. slapi_entry_get_dn(origin));
  1263. ret = 1;
  1264. goto bail;
  1265. }
  1266. } else {
  1267. /* Just use the mapping since we have no origin entry. */
  1268. *value = slapi_ch_smprintf("%s$%s%s", pre_str, map_type,
  1269. post_str);
  1270. }
  1271. } else {
  1272. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1273. "mep_parse_mapped_attr: No variable found in "
  1274. "mapped attribute value for type \"%s\".\n",
  1275. mapping);
  1276. ret = 1;
  1277. goto bail;
  1278. }
  1279. bail:
  1280. slapi_ch_free_string(&map_type);
  1281. if (ret != 0) {
  1282. slapi_ch_free_string(type);
  1283. slapi_ch_free_string(value);
  1284. }
  1285. return ret;
  1286. }
  1287. /*
  1288. * mep_is_managed_entry()
  1289. *
  1290. * Returns 1 if the entry is a managed entry, 0 otherwise.
  1291. * The check is performed by seeing if the managed entry
  1292. * objectclass is present.
  1293. */
  1294. static int
  1295. mep_is_managed_entry(Slapi_Entry *e)
  1296. {
  1297. int ret = 0;
  1298. Slapi_Attr *attr = NULL;
  1299. struct berval bv;
  1300. bv.bv_val = MEP_MANAGED_OC;
  1301. bv.bv_len = strlen(bv.bv_val);
  1302. if (e && (slapi_entry_attr_find(e, SLAPI_ATTR_OBJECTCLASS, &attr) == 0)) {
  1303. if (slapi_attr_value_find(attr, &bv) == 0) {
  1304. ret = 1;
  1305. }
  1306. }
  1307. return ret;
  1308. }
  1309. /*
  1310. * mep_is_mapped_attr()
  1311. *
  1312. * Checks if type is defined as a mapped attribute in template.
  1313. */
  1314. static int
  1315. mep_is_mapped_attr(Slapi_Entry *template, char *type)
  1316. {
  1317. int ret = 0;
  1318. int i = 0;
  1319. char **vals = NULL;
  1320. char *map_type = NULL;
  1321. char *value = NULL;
  1322. if (template && type) {
  1323. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1324. for (i = 0; vals && vals[i]; ++i) {
  1325. if (mep_parse_mapped_attr(vals[i], NULL, &map_type, &value) == 0) {
  1326. if (slapi_attr_type_cmp(map_type, type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1327. ret = 1;
  1328. }
  1329. slapi_ch_free_string(&map_type);
  1330. slapi_ch_free_string(&value);
  1331. /* If we found a match, we're done. */
  1332. if (ret == 1) {
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. slapi_ch_array_free(vals);
  1338. }
  1339. return ret;
  1340. }
  1341. /*
  1342. * Operation callback functions
  1343. */
  1344. /*
  1345. * mep_pre_op()
  1346. *
  1347. * Checks if an operation is modifying the managed
  1348. * entries config and validates the config changes.
  1349. */
  1350. static int
  1351. mep_pre_op(Slapi_PBlock * pb, int modop)
  1352. {
  1353. char *dn = 0;
  1354. Slapi_Entry *e = 0;
  1355. Slapi_Mods *smods = 0;
  1356. LDAPMod **mods;
  1357. int free_entry = 0;
  1358. char *errstr = NULL;
  1359. struct configEntry *config = NULL;
  1360. void *caller_id = NULL;
  1361. int ret = 0;
  1362. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1363. "--> mep_pre_op\n");
  1364. /* Just bail if we aren't ready to service requests yet. */
  1365. if (!g_plugin_started)
  1366. goto bail;
  1367. /* See if we're calling ourselves. */
  1368. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  1369. if (0 == (dn = mep_get_dn(pb)))
  1370. goto bail;
  1371. if (mep_dn_is_config(dn)) {
  1372. /* Validate config changes, but don't apply them.
  1373. * This allows us to reject invalid config changes
  1374. * here at the pre-op stage. Applying the config
  1375. * needs to be done at the post-op stage. */
  1376. if (LDAP_CHANGETYPE_ADD == modop) {
  1377. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1378. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1379. /* Fetch the entry being modified so we can
  1380. * create the resulting entry for validation. */
  1381. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  1382. if (tmp_dn) {
  1383. slapi_search_internal_get_entry(tmp_dn, 0, &e, mep_get_plugin_id());
  1384. slapi_sdn_free(&tmp_dn);
  1385. free_entry = 1;
  1386. }
  1387. /* If the entry doesn't exist, just bail and
  1388. * let the server handle it. */
  1389. if (e == NULL) {
  1390. goto bail;
  1391. }
  1392. /* Grab the mods. */
  1393. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1394. smods = slapi_mods_new();
  1395. slapi_mods_init_byref(smods, mods);
  1396. /* Apply the mods to create the resulting entry. */
  1397. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1398. /* The mods don't apply cleanly, so we just let this op go
  1399. * to let the main server handle it. */
  1400. goto bailmod;
  1401. }
  1402. } else {
  1403. /* Refuse other operations. */
  1404. ret = LDAP_UNWILLING_TO_PERFORM;
  1405. errstr = slapi_ch_smprintf("Not a valid operation.");
  1406. goto bail;
  1407. }
  1408. if (mep_parse_config_entry(e, 0) != 0) {
  1409. /* Refuse the operation if config parsing failed. */
  1410. ret = LDAP_UNWILLING_TO_PERFORM;
  1411. if (LDAP_CHANGETYPE_ADD == modop) {
  1412. errstr = slapi_ch_smprintf("Not a valid managed entries configuration entry.");
  1413. } else {
  1414. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1415. "managed entries configuration.");
  1416. }
  1417. }
  1418. } else {
  1419. /* Check if an active template entry is being updated. If so, validate it. */
  1420. mep_config_read_lock();
  1421. mep_find_config_by_template_dn(dn, &config);
  1422. if (config) {
  1423. Slapi_Entry *test_entry = NULL;
  1424. struct configEntry *config_copy = NULL;
  1425. Slapi_DN *tmp_dn = NULL;
  1426. config_copy = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  1427. /* Make a temporary copy of the config to use for validation. */
  1428. config_copy->dn = slapi_ch_strdup(config->dn);
  1429. config_copy->managed_base = slapi_ch_strdup(config->managed_base);
  1430. config_copy->template_dn = slapi_ch_strdup(config->template_dn);
  1431. /* Reject attempts to delete or rename an active template.
  1432. * Validate changes to an active template. */
  1433. switch (modop) {
  1434. case LDAP_CHANGETYPE_DELETE:
  1435. errstr = slapi_ch_smprintf("Deleting an active managed "
  1436. "entries template is not allowed. "
  1437. "Delete the associated config "
  1438. "entry first.");
  1439. ret = LDAP_UNWILLING_TO_PERFORM;
  1440. break;
  1441. case LDAP_CHANGETYPE_MODDN:
  1442. errstr = slapi_ch_smprintf("Renaming an active managed "
  1443. "entries template is not allowed. "
  1444. "Create a new template and modify "
  1445. "the associated config entry instead.");
  1446. ret = LDAP_UNWILLING_TO_PERFORM;
  1447. break;
  1448. case LDAP_CHANGETYPE_MODIFY:
  1449. tmp_dn = slapi_sdn_new_dn_byref(dn);
  1450. /* Fetch the existing template entry. */
  1451. if (tmp_dn) {
  1452. slapi_search_internal_get_entry(tmp_dn, 0, &e, mep_get_plugin_id());
  1453. slapi_sdn_free(&tmp_dn);
  1454. free_entry = 1;
  1455. }
  1456. /* If the entry doesn't exist, we just skip
  1457. * validation and let the server handle it. */
  1458. if (e) {
  1459. /* Grab the mods. */
  1460. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1461. smods = slapi_mods_new();
  1462. slapi_mods_init_byref(smods, mods);
  1463. /* Apply the mods to create the resulting entry. */
  1464. if (mods && (slapi_entry_apply_mods(e, mods) == LDAP_SUCCESS)) {
  1465. /* Set the resulting template in the config copy.
  1466. * The ownership of the resulting entry is handed
  1467. * over to the config copy. */
  1468. config_copy->template_entry = e;
  1469. e = NULL;
  1470. /* Validate the changed template. */
  1471. test_entry = mep_create_managed_entry(config_copy, NULL);
  1472. if (test_entry == NULL) {
  1473. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1474. "managed entries template.");
  1475. ret = LDAP_UNWILLING_TO_PERFORM;
  1476. } else if (slapi_entry_schema_check(NULL, test_entry) != 0) {
  1477. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1478. "managed entries template due "
  1479. "to a schema violation.");
  1480. ret = LDAP_UNWILLING_TO_PERFORM;
  1481. }
  1482. }
  1483. }
  1484. /* Dispose of the test entry */
  1485. slapi_entry_free(test_entry);
  1486. break;
  1487. }
  1488. /* Free the config copy */
  1489. mep_free_config_entry(&config_copy);
  1490. }
  1491. mep_config_unlock();
  1492. /* If replication, just bail. */
  1493. if (mep_isrepl(pb)) {
  1494. goto bailmod;
  1495. }
  1496. /* Check if a managed entry is being deleted or
  1497. * renamed and reject if it's not being done by
  1498. * this plugin. */
  1499. if (((modop == LDAP_CHANGETYPE_DELETE) || (modop == LDAP_CHANGETYPE_MODDN) ||
  1500. (modop == LDAP_CHANGETYPE_MODIFY)) && (caller_id != mep_get_plugin_id())) {
  1501. Slapi_DN *tmp_dn = slapi_sdn_new_dn_byref(dn);
  1502. Slapi_Entry *origin_e = NULL;
  1503. Slapi_Mod *smod = NULL;
  1504. Slapi_Mod *next_mod = NULL;
  1505. char *origin_dn = NULL;
  1506. Slapi_DN *origin_sdn = NULL;
  1507. /* Fetch the target entry. */
  1508. if (tmp_dn) {
  1509. /* Free any existing entry so we don't leak it. */
  1510. if (e && free_entry) {
  1511. slapi_entry_free(e);
  1512. }
  1513. slapi_search_internal_get_entry(tmp_dn, 0, &e, mep_get_plugin_id());
  1514. slapi_sdn_free(&tmp_dn);
  1515. free_entry = 1;
  1516. }
  1517. if (e && mep_is_managed_entry(e)) {
  1518. if (modop == LDAP_CHANGETYPE_MODIFY) {
  1519. /* Fetch the origin entry so we can locate the config template. */
  1520. origin_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BY_TYPE);
  1521. if (origin_dn) {
  1522. origin_sdn = slapi_sdn_new_dn_byref(origin_dn);
  1523. slapi_search_internal_get_entry(origin_sdn, 0,
  1524. &origin_e, mep_get_plugin_id());
  1525. slapi_sdn_free(&origin_sdn);
  1526. }
  1527. if (origin_e) {
  1528. /* Fetch the config. */
  1529. mep_config_read_lock();
  1530. mep_find_config(origin_e, &config);
  1531. if (config) {
  1532. /* Get the mods if we haven't already. */
  1533. if (smods == NULL) {
  1534. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1535. smods = slapi_mods_new();
  1536. slapi_mods_init_byref(smods, mods);
  1537. }
  1538. next_mod = slapi_mod_new();
  1539. smod = slapi_mods_get_first_smod(smods, next_mod);
  1540. while(smod) {
  1541. char *type = (char *)slapi_mod_get_type(smod);
  1542. /* If this is a mapped attribute, reject the op. */
  1543. if (mep_is_mapped_attr(config->template_entry, type)) {
  1544. errstr = slapi_ch_smprintf("Modifying a mapped attribute "
  1545. " in a managed entry is not allowed. "
  1546. "The \"%s\" attribute is mapped for "
  1547. "this entry.", type);
  1548. ret = LDAP_UNWILLING_TO_PERFORM;
  1549. break;
  1550. }
  1551. slapi_mod_done(next_mod);
  1552. smod = slapi_mods_get_next_smod(smods, next_mod);
  1553. }
  1554. slapi_mod_free(&next_mod);
  1555. } else {
  1556. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1557. "mep_pre_op: Unable to fetch config for "
  1558. "origin entry \"%s\".\n", origin_dn);
  1559. }
  1560. slapi_entry_free(origin_e);
  1561. mep_config_unlock();
  1562. } else {
  1563. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1564. "mep_pre_op: Unable to fetch origin entry "
  1565. "\"%s\".\n", origin_dn);
  1566. }
  1567. slapi_ch_free_string(&origin_dn);
  1568. } else {
  1569. errstr = slapi_ch_smprintf("%s a managed entry is not allowed. "
  1570. "It needs to be manually unlinked first.",
  1571. modop == LDAP_CHANGETYPE_DELETE ? "Deleting"
  1572. : "Renaming");
  1573. ret = LDAP_UNWILLING_TO_PERFORM;
  1574. }
  1575. }
  1576. }
  1577. }
  1578. bailmod:
  1579. /* Clean up smods. */
  1580. if (LDAP_CHANGETYPE_MODIFY == modop) {
  1581. slapi_mods_free(&smods);
  1582. }
  1583. bail:
  1584. if (free_entry && e)
  1585. slapi_entry_free(e);
  1586. if (ret) {
  1587. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1588. "mep_pre_op: operation failure [%d]\n", ret);
  1589. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1590. slapi_ch_free((void **)&errstr);
  1591. ret = -1;
  1592. }
  1593. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1594. "<-- mep_pre_op\n");
  1595. return ret;
  1596. }
  1597. static int
  1598. mep_add_pre_op(Slapi_PBlock * pb)
  1599. {
  1600. return mep_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1601. }
  1602. static int
  1603. mep_del_pre_op(Slapi_PBlock * pb)
  1604. {
  1605. return mep_pre_op(pb, LDAP_CHANGETYPE_DELETE);
  1606. }
  1607. static int
  1608. mep_mod_pre_op(Slapi_PBlock * pb)
  1609. {
  1610. return mep_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1611. }
  1612. static int
  1613. mep_modrdn_pre_op(Slapi_PBlock * pb)
  1614. {
  1615. return mep_pre_op(pb, LDAP_CHANGETYPE_MODDN);
  1616. }
  1617. static int
  1618. mep_mod_post_op(Slapi_PBlock *pb)
  1619. {
  1620. Slapi_Mods *smods = NULL;
  1621. Slapi_PBlock *mep_pb = NULL;
  1622. Slapi_Entry *e = NULL;
  1623. char *dn = NULL;
  1624. char *managed_dn = NULL;
  1625. struct configEntry *config = NULL;
  1626. int result = 0;
  1627. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1628. "--> mep_mod_post_op\n");
  1629. /* Just bail if we aren't ready to service requests yet. */
  1630. if (!g_plugin_started)
  1631. return 0;
  1632. if (mep_oktodo(pb) && (dn = mep_get_dn(pb))) {
  1633. /* First check if the config or a template is being modified. */
  1634. if (mep_dn_is_config(dn) || mep_dn_is_template(dn)) {
  1635. mep_load_config();
  1636. }
  1637. /* If replication, just bail. */
  1638. if (mep_isrepl(pb)) {
  1639. goto bail;
  1640. }
  1641. /* Fetch the modified entry. */
  1642. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1643. if (e == NULL) {
  1644. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1645. "mep_mod_post_op: Unable to fetch postop entry.\n");
  1646. goto bail;
  1647. }
  1648. /* Check if we're an origin entry. Update the
  1649. * mapped attrs of it's managed entry if so. */
  1650. managed_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_ENTRY_TYPE);
  1651. if (managed_dn) {
  1652. mep_config_read_lock();
  1653. mep_find_config(e, &config);
  1654. if (config) {
  1655. smods = mep_get_mapped_mods(config, e);
  1656. if (smods) {
  1657. /* Clear out the pblock for reuse. */
  1658. mep_pb = slapi_pblock_new();
  1659. /* Perform the modify operation. */
  1660. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1661. "mep_mod_post_op: Updating mapped attributes "
  1662. "in entry \"%s\"\n.", managed_dn);
  1663. slapi_modify_internal_set_pb(mep_pb, managed_dn,
  1664. slapi_mods_get_ldapmods_byref(smods), 0, 0,
  1665. mep_get_plugin_id(), 0);
  1666. slapi_modify_internal_pb(mep_pb);
  1667. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1668. if (result != LDAP_SUCCESS) {
  1669. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1670. "mep_mod_post_op: Unable to update mapped "
  1671. "attributes from origin entry \"%s\" in managed "
  1672. "entry \"%s\" (%s).\n", dn, managed_dn,
  1673. ldap_err2string(result));
  1674. }
  1675. slapi_mods_free(&smods);
  1676. slapi_pblock_destroy(mep_pb);
  1677. }
  1678. } else {
  1679. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1680. "mep_mod_post_op: Unable to find config for origin "
  1681. "entry \"%s\".\n", dn);
  1682. }
  1683. slapi_ch_free_string(&managed_dn);
  1684. mep_config_unlock();
  1685. }
  1686. }
  1687. bail:
  1688. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1689. "<-- mep_mod_post_op\n");
  1690. return 0;
  1691. }
  1692. static int
  1693. mep_add_post_op(Slapi_PBlock *pb)
  1694. {
  1695. Slapi_Entry *e = NULL;
  1696. char *dn = NULL;
  1697. struct configEntry *config = NULL;
  1698. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1699. "--> mep_add_post_op\n");
  1700. /* Just bail if we aren't ready to service requests yet. */
  1701. if (!g_plugin_started || !mep_oktodo(pb))
  1702. return 0;
  1703. /* Reload config if a config entry was added. */
  1704. if ((dn = mep_get_dn(pb))) {
  1705. if (mep_dn_is_config(dn)) {
  1706. mep_load_config();
  1707. }
  1708. } else {
  1709. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1710. "mep_add_post_op: Error "
  1711. "retrieving dn\n");
  1712. }
  1713. /* If replication, just bail. */
  1714. if (mep_isrepl(pb)) {
  1715. return 0;
  1716. }
  1717. /* Get the newly added entry. */
  1718. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1719. if (e) {
  1720. /* Check if a config entry applies
  1721. * to the entry being added. */
  1722. mep_config_read_lock();
  1723. mep_find_config(e, &config);
  1724. if (config) {
  1725. mep_add_managed_entry(config, e);
  1726. }
  1727. mep_config_unlock();
  1728. } else {
  1729. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1730. "mep_add_post_op: Error "
  1731. "retrieving post-op entry %s\n", dn);
  1732. }
  1733. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1734. "<-- mep_add_post_op\n");
  1735. return 0;
  1736. }
  1737. static int
  1738. mep_del_post_op(Slapi_PBlock *pb)
  1739. {
  1740. char *dn = NULL;
  1741. Slapi_Entry *e = NULL;
  1742. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1743. "--> mep_del_post_op\n");
  1744. /* Just bail if we aren't ready to service requests yet. */
  1745. if (!g_plugin_started || !mep_oktodo(pb)) {
  1746. return 0;
  1747. }
  1748. /* Reload config if a config entry was deleted. */
  1749. if ((dn = mep_get_dn(pb))) {
  1750. if (mep_dn_is_config(dn))
  1751. mep_load_config();
  1752. } else {
  1753. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1754. "mep_del_post_op: Error "
  1755. "retrieving dn\n");
  1756. }
  1757. /* If replication, just bail. */
  1758. if (mep_isrepl(pb)) {
  1759. return 0;
  1760. }
  1761. /* Get deleted entry, then go through types to find config. */
  1762. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );
  1763. if (e) {
  1764. char *managed_dn = NULL;
  1765. /* See if we're an origin entry . */
  1766. managed_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_ENTRY_TYPE);
  1767. if (managed_dn) {
  1768. Slapi_PBlock *mep_pb = slapi_pblock_new();
  1769. /* Delete the managed entry. */
  1770. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1771. "mep_del_post_op: Deleting managed entry "
  1772. "\"%s\" due to deletion of origin entry "
  1773. "\"%s\".\n ", managed_dn, dn);
  1774. slapi_delete_internal_set_pb(mep_pb, managed_dn, NULL,
  1775. NULL, mep_get_plugin_id(), 0);
  1776. slapi_delete_internal_pb(mep_pb);
  1777. slapi_ch_free_string(&managed_dn);
  1778. slapi_pblock_destroy(mep_pb);
  1779. }
  1780. } else {
  1781. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1782. "mep_del_post_op: Error "
  1783. "retrieving pre-op entry %s\n", dn);
  1784. }
  1785. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1786. "<-- mep_del_post_op\n");
  1787. return 0;
  1788. }
  1789. static int
  1790. mep_modrdn_post_op(Slapi_PBlock *pb)
  1791. {
  1792. char *old_dn = NULL;
  1793. char *new_dn = NULL;
  1794. Slapi_Entry *post_e = NULL;
  1795. char *managed_dn = NULL;
  1796. struct configEntry *config = NULL;
  1797. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1798. "--> mep_modrdn_post_op\n");
  1799. /* Just bail if we aren't ready to service requests yet. */
  1800. if (!g_plugin_started || !mep_oktodo(pb))
  1801. return 0;;
  1802. /* Reload config if an existing config entry was renamed,
  1803. * or if the new dn brings an entry into the scope of the
  1804. * config entries. */
  1805. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  1806. if (post_e) {
  1807. new_dn = slapi_entry_get_ndn(post_e);
  1808. } else {
  1809. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1810. "mep_modrdn_post_op: Error "
  1811. "retrieving post-op entry\n");
  1812. return 0;
  1813. }
  1814. if ((old_dn = mep_get_dn(pb))) {
  1815. if (mep_dn_is_config(old_dn) || mep_dn_is_config(new_dn))
  1816. mep_load_config();
  1817. } else {
  1818. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1819. "mep_modrdn_post_op: Error "
  1820. "retrieving dn\n");
  1821. }
  1822. /* If replication, just bail. */
  1823. if (mep_isrepl(pb)) {
  1824. return 0;
  1825. }
  1826. /* See if we're an origin entry . */
  1827. managed_dn = slapi_entry_attr_get_charptr(post_e, MEP_MANAGED_ENTRY_TYPE);
  1828. if (managed_dn) {
  1829. LDAPMod mod;
  1830. LDAPMod *mods[3];
  1831. char *vals[2];
  1832. int result = LDAP_SUCCESS;
  1833. Slapi_PBlock *mep_pb = slapi_pblock_new();
  1834. Slapi_Entry *new_managed_entry = NULL;
  1835. Slapi_DN *managed_sdn = NULL;
  1836. Slapi_Mods *smods = NULL;
  1837. mep_config_read_lock();
  1838. mep_find_config(post_e, &config);
  1839. if (!config) {
  1840. LDAPMod mod2;
  1841. char *vals2[2];
  1842. /* Delete the associated managed entry. */
  1843. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1844. "mep_modrdn_post_op: Removing managed entry \"%s\" "
  1845. "since origin entry \"%s\" was moved out of scope.\n",
  1846. managed_dn, old_dn);
  1847. slapi_delete_internal_set_pb (mep_pb, managed_dn, NULL, NULL,
  1848. mep_get_plugin_id(), 0);
  1849. slapi_delete_internal_pb(mep_pb);
  1850. /* Clear out the pblock for reuse. */
  1851. slapi_pblock_init(mep_pb);
  1852. /* Remove the pointer from the origin entry. */
  1853. vals[0] = 0;
  1854. mod.mod_op = LDAP_MOD_DELETE;
  1855. mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1856. mod.mod_values = vals;
  1857. /* Remove the origin objectclass. */
  1858. vals2[0] = MEP_ORIGIN_OC;
  1859. vals2[1] = 0;
  1860. mod2.mod_op = LDAP_MOD_DELETE;
  1861. mod2.mod_type = SLAPI_ATTR_OBJECTCLASS;
  1862. mod2.mod_values = vals2;
  1863. mods[0] = &mod;
  1864. mods[1] = &mod2;
  1865. mods[2] = 0;
  1866. /* Perform the modify operation. */
  1867. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1868. "mep_modrdn_post_op: Removing %s pointer and %s "
  1869. "objectclass from entry \"%s\".\n",
  1870. MEP_MANAGED_ENTRY_TYPE, MEP_ORIGIN_OC, new_dn);
  1871. slapi_modify_internal_set_pb(mep_pb, new_dn, mods, 0, 0,
  1872. mep_get_plugin_id(), 0);
  1873. slapi_modify_internal_pb(mep_pb);
  1874. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1875. if (result != LDAP_SUCCESS) {
  1876. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1877. "mep_modrdn_post_op: Unable to remove %s "
  1878. "pointer and %s objectclass from entry "
  1879. "\"%s\".\n", MEP_MANAGED_ENTRY_TYPE,
  1880. MEP_ORIGIN_OC, new_dn);
  1881. }
  1882. } else {
  1883. /* Update backlink to new origin DN in managed entry. */
  1884. vals[0] = new_dn;
  1885. vals[1] = 0;
  1886. mod.mod_op = LDAP_MOD_REPLACE;
  1887. mod.mod_type = MEP_MANAGED_BY_TYPE;
  1888. mod.mod_values = vals;
  1889. mods[0] = &mod;
  1890. mods[1] = 0;
  1891. /* Perform the modify operation. */
  1892. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1893. "mep_modrdn_post_op: Updating %s pointer to \"%s\" "
  1894. "in entry \"%s\".\n", MEP_MANAGED_BY_TYPE, new_dn, managed_dn);
  1895. slapi_modify_internal_set_pb(mep_pb, managed_dn, mods, 0, 0,
  1896. mep_get_plugin_id(), 0);
  1897. slapi_modify_internal_pb(mep_pb);
  1898. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1899. if (result != LDAP_SUCCESS) {
  1900. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1901. "mep_modrdn_post_op: Unable to update pointer to "
  1902. "origin entry \"%s\" in managed entry \"%s\" "
  1903. "(%s).\n", new_dn, managed_dn, ldap_err2string(result));
  1904. } else {
  1905. /* Create a new managed entry to determine what changes
  1906. * we need to make to the existing managed entry. */
  1907. new_managed_entry = mep_create_managed_entry(config, post_e);
  1908. /* See if we need to rename the managed entry. */
  1909. managed_sdn = slapi_sdn_new_dn_byref(managed_dn);
  1910. if (slapi_sdn_compare(slapi_entry_get_sdn(new_managed_entry), managed_sdn) != 0) {
  1911. Slapi_RDN *srdn = slapi_rdn_new();
  1912. /* Clear out the pblock for reuse. */
  1913. slapi_pblock_init(mep_pb);
  1914. /* Create new RDN */
  1915. slapi_rdn_set_dn(srdn, slapi_entry_get_dn(new_managed_entry));
  1916. /* Rename the managed entry. */
  1917. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1918. "mep_modrdn_post_op: Renaming managed entry "
  1919. "\"%s\" to \"%s\" due to rename of origin "
  1920. "entry \"%s\".\n ", managed_dn,
  1921. slapi_entry_get_dn(new_managed_entry), old_dn);
  1922. slapi_rename_internal_set_pb(mep_pb, managed_dn,
  1923. slapi_rdn_get_rdn(srdn),
  1924. NULL, 1, NULL, NULL, mep_get_plugin_id(), 0);
  1925. slapi_modrdn_internal_pb(mep_pb);
  1926. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1927. if (result != LDAP_SUCCESS) {
  1928. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1929. "mep_modrdn_post_op: Unable to rename managed "
  1930. "entry \"%s\" to \"%s\" (%s).\n", managed_dn,
  1931. slapi_entry_get_dn(new_managed_entry),
  1932. ldap_err2string(result));
  1933. } else {
  1934. /* Clear out the pblock for reuse. */
  1935. slapi_pblock_init(mep_pb);
  1936. /* Update the link to the managed entry in the origin
  1937. * entry. We can just reuse the mod structure. */
  1938. vals[0] = slapi_entry_get_dn(new_managed_entry);
  1939. mod.mod_op = LDAP_MOD_REPLACE;
  1940. mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1941. mod.mod_values = vals;
  1942. /* Perform the modify operation. */
  1943. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1944. "mep_modrdn_post_op: Updating %s pointer to "
  1945. "\"%s\" in entry \"%s\"\n.", MEP_MANAGED_ENTRY_TYPE,
  1946. vals[0], new_dn);
  1947. slapi_modify_internal_set_pb(mep_pb, new_dn, mods, 0, 0,
  1948. mep_get_plugin_id(), 0);
  1949. slapi_modify_internal_pb(mep_pb);
  1950. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1951. if (result != LDAP_SUCCESS) {
  1952. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1953. "mep_modrdn_post_op: Unable to rename managed "
  1954. "entry \"%s\" to \"%s\" (%s).\n", managed_dn,
  1955. slapi_entry_get_dn(new_managed_entry),
  1956. ldap_err2string(result));
  1957. slapi_rdn_free(&srdn);
  1958. goto bail;
  1959. }
  1960. }
  1961. slapi_rdn_free(&srdn);
  1962. }
  1963. /* Update all of the mapped attributes
  1964. * to be sure they are up to date. */
  1965. smods = mep_get_mapped_mods(config, post_e);
  1966. if (smods) {
  1967. /* Clear out the pblock for reuse. */
  1968. slapi_pblock_init(mep_pb);
  1969. /* Perform the modify operation. */
  1970. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1971. "mep_modrdn_post_op: Updating mapped attributes "
  1972. "in entry \"%s\"\n.", managed_dn);
  1973. slapi_modify_internal_set_pb(mep_pb, slapi_entry_get_dn(new_managed_entry),
  1974. slapi_mods_get_ldapmods_byref(smods), 0, 0,
  1975. mep_get_plugin_id(), 0);
  1976. slapi_modify_internal_pb(mep_pb);
  1977. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1978. if (result != LDAP_SUCCESS) {
  1979. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1980. "mep_modrdn_post_op: Unable to update mapped "
  1981. "attributes from origin entry \"%s\" in managed "
  1982. "entry \"%s\" (%s).\n", new_dn,
  1983. slapi_entry_get_dn(new_managed_entry),
  1984. ldap_err2string(result));
  1985. }
  1986. slapi_mods_free(&smods);
  1987. }
  1988. bail:
  1989. slapi_sdn_free(&managed_sdn);
  1990. slapi_entry_free(new_managed_entry);
  1991. }
  1992. }
  1993. slapi_pblock_destroy(mep_pb);
  1994. slapi_ch_free_string(&managed_dn);
  1995. mep_config_unlock();
  1996. } else {
  1997. /* Was this entry moved into scope of a config entry?
  1998. * If so, treat like an add and create the new managed
  1999. * entry and links. */
  2000. mep_config_read_lock();
  2001. mep_find_config(post_e, &config);
  2002. if (config) {
  2003. mep_add_managed_entry(config, post_e);
  2004. }
  2005. mep_config_unlock();
  2006. }
  2007. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2008. "<-- mep_modrdn_post_op\n");
  2009. return 0;
  2010. }