mep.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2010 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /*
  12. * Managed Entries Plug-in
  13. */
  14. #include "mep.h"
  15. #include "slapi-private.h"
  16. /*
  17. * Plug-in globals
  18. */
  19. static PRCList *g_mep_config = NULL;
  20. static Slapi_RWLock *g_mep_config_lock;
  21. static void *_PluginID = NULL;
  22. static Slapi_DN *_PluginDN = NULL;
  23. static Slapi_DN *_ConfigAreaDN = NULL;
  24. static Slapi_PluginDesc pdesc = { MEP_FEATURE_DESC,
  25. VENDOR,
  26. DS_PACKAGE_VERSION,
  27. MEP_PLUGIN_DESC };
  28. /*
  29. * Plug-in management functions
  30. */
  31. int mep_init(Slapi_PBlock * pb);
  32. static int mep_start(Slapi_PBlock * pb);
  33. static int mep_close(Slapi_PBlock * pb);
  34. static int mep_postop_init(Slapi_PBlock * pb);
  35. static int mep_internal_postop_init(Slapi_PBlock *pb);
  36. /*
  37. * Operation callbacks (where the real work is done)
  38. */
  39. static int mep_mod_post_op(Slapi_PBlock *pb);
  40. static int mep_add_post_op(Slapi_PBlock *pb);
  41. static int mep_del_post_op(Slapi_PBlock *pb);
  42. static int mep_modrdn_post_op(Slapi_PBlock *pb);
  43. static int mep_pre_op(Slapi_PBlock *pb, int modop);
  44. static int mep_mod_pre_op(Slapi_PBlock *pb);
  45. static int mep_add_pre_op(Slapi_PBlock *pb);
  46. static int mep_del_pre_op(Slapi_PBlock *pb);
  47. static int mep_modrdn_pre_op(Slapi_PBlock *pb);
  48. /*
  49. * Config cache management functions
  50. */
  51. static int mep_load_config();
  52. static void mep_delete_config();
  53. static int mep_parse_config_entry(Slapi_Entry * e, int apply);
  54. static void mep_free_config_entry(struct configEntry ** entry);
  55. /*
  56. * helpers
  57. */
  58. static Slapi_DN *mep_get_sdn(Slapi_PBlock * pb);
  59. static Slapi_DN *mep_get_config_area();
  60. static void mep_set_config_area(Slapi_DN *sdn);
  61. static int mep_dn_is_config(Slapi_DN *sdn);
  62. static int mep_dn_is_template(Slapi_DN *dn);
  63. static void mep_find_config(Slapi_Entry *e, struct configEntry **config);
  64. static void mep_find_config_by_template_dn(Slapi_DN *template_dn,
  65. struct configEntry **config);
  66. static int mep_oktodo(Slapi_PBlock *pb);
  67. static int mep_isrepl(Slapi_PBlock *pb);
  68. static Slapi_Entry *mep_create_managed_entry(struct configEntry *config,
  69. Slapi_Entry *origin);
  70. static int mep_add_managed_entry(struct configEntry *config,
  71. Slapi_Entry *origin);
  72. static int mep_rename_managed_entry(Slapi_Entry *origin,
  73. Slapi_DN *new_dn, Slapi_DN *old_dn);
  74. static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
  75. Slapi_Entry *origin, char **mapped_dn);
  76. static int mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
  77. char **type, char **value);
  78. static int mep_is_managed_entry(Slapi_Entry *e);
  79. static int mep_is_mapped_attr(Slapi_Entry *template, char *type);
  80. static int mep_has_tombstone_value(Slapi_Entry * e);
  81. static int mep_parse_mapped_origin_attr(char *mapping, char **origin_type);
  82. static int mep_is_mapped_origin_attr(char **vals, char *type);
  83. static char** mep_extract_origin_attrs(Slapi_Entry *entry);
  84. /*
  85. * Config cache locking functions
  86. */
  87. void
  88. mep_config_read_lock()
  89. {
  90. slapi_rwlock_rdlock(g_mep_config_lock);
  91. }
  92. void
  93. mep_config_write_lock()
  94. {
  95. slapi_rwlock_wrlock(g_mep_config_lock);
  96. }
  97. void
  98. mep_config_unlock()
  99. {
  100. slapi_rwlock_unlock(g_mep_config_lock);
  101. }
  102. /*
  103. * Plugin identity functions
  104. */
  105. void
  106. mep_set_plugin_id(void *pluginID)
  107. {
  108. _PluginID = pluginID;
  109. }
  110. void *
  111. mep_get_plugin_id()
  112. {
  113. return _PluginID;
  114. }
  115. void
  116. mep_set_plugin_sdn(Slapi_DN *pluginDN)
  117. {
  118. _PluginDN = pluginDN;
  119. }
  120. Slapi_DN *
  121. mep_get_plugin_sdn()
  122. {
  123. return _PluginDN;
  124. }
  125. static int plugin_is_betxn = 0;
  126. /*
  127. * Plug-in initialization functions
  128. */
  129. int
  130. mep_init(Slapi_PBlock *pb)
  131. {
  132. int status = 0;
  133. char *plugin_identity = NULL;
  134. Slapi_Entry *plugin_entry = NULL;
  135. char *plugin_type = NULL;
  136. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  137. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  138. int predel = SLAPI_PLUGIN_PRE_DELETE_FN;
  139. int premdn = SLAPI_PLUGIN_PRE_MODRDN_FN;
  140. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  141. "--> mep_init\n");
  142. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  143. plugin_entry &&
  144. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  145. plugin_type && strstr(plugin_type, "betxn")) {
  146. plugin_is_betxn = 1;
  147. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  148. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  149. predel = SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN;
  150. premdn = SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN;
  151. }
  152. slapi_ch_free_string(&plugin_type);
  153. /* Store the plugin identity for later use.
  154. * Used for internal operations. */
  155. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  156. PR_ASSERT(plugin_identity);
  157. mep_set_plugin_id(plugin_identity);
  158. /* Register callbacks */
  159. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  160. SLAPI_PLUGIN_VERSION_01) != 0 ||
  161. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  162. (void *) mep_start) != 0 ||
  163. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  164. (void *) mep_close) != 0 ||
  165. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  166. (void *) &pdesc) != 0 ||
  167. slapi_pblock_set(pb, premod, (void *) mep_mod_pre_op) != 0 ||
  168. slapi_pblock_set(pb, preadd, (void *) mep_add_pre_op) != 0 ||
  169. slapi_pblock_set(pb, predel, (void *) mep_del_pre_op) != 0 ||
  170. slapi_pblock_set(pb, premdn, (void *) mep_modrdn_pre_op) != 0) {
  171. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  172. "mep_init: failed to register plugin\n");
  173. status = -1;
  174. }
  175. if (!status && !plugin_is_betxn &&
  176. slapi_register_plugin("internalpostoperation", /* op type */
  177. 1, /* Enabled */
  178. "mep_init", /* this function desc */
  179. mep_internal_postop_init, /* init func */
  180. MEP_INT_POSTOP_DESC, /* plugin desc */
  181. NULL, /* ? */
  182. plugin_identity /* access control */
  183. )) {
  184. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  185. "mep_init: failed to register internalpostoperation plugin\n");
  186. status = -1;
  187. }
  188. if (!status) {
  189. plugin_type = plugin_is_betxn ? "betxnpostoperation" : "postoperation";
  190. if (slapi_register_plugin(plugin_type, /* op type */
  191. 1, /* Enabled */
  192. "mep_init", /* this function desc */
  193. mep_postop_init, /* init func for post op */
  194. MEP_POSTOP_DESC, /* plugin desc */
  195. NULL, /* ? */
  196. plugin_identity /* access control */
  197. )) {
  198. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  199. "mep_init: failed to register plugin\n");
  200. status = -1;
  201. }
  202. }
  203. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  204. "<-- mep_init\n");
  205. return status;
  206. }
  207. /* not used when using plugin as a betxn plugin - betxn plugins are called for both internal and external ops */
  208. static int
  209. mep_internal_postop_init(Slapi_PBlock *pb)
  210. {
  211. int status = 0;
  212. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  213. SLAPI_PLUGIN_VERSION_01) != 0 ||
  214. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  215. (void *) &pdesc) != 0 ||
  216. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  217. (void *) mep_add_post_op) != 0 ||
  218. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  219. (void *) mep_del_post_op) != 0 ||
  220. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  221. (void *) mep_mod_post_op) != 0 ||
  222. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  223. (void *) mep_modrdn_post_op) != 0) {
  224. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  225. "mep_internal_postop_init: failed to register plugin\n");
  226. status = -1;
  227. }
  228. return status;
  229. }
  230. static int
  231. mep_postop_init(Slapi_PBlock *pb)
  232. {
  233. int status = 0;
  234. int addfn = SLAPI_PLUGIN_POST_ADD_FN;
  235. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  236. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
  237. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  238. if (plugin_is_betxn) {
  239. addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  240. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  241. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  242. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  243. }
  244. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  245. SLAPI_PLUGIN_VERSION_01) != 0 ||
  246. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  247. (void *) &pdesc) != 0 ||
  248. slapi_pblock_set(pb, addfn, (void *) mep_add_post_op) != 0 ||
  249. slapi_pblock_set(pb, delfn, (void *) mep_del_post_op) != 0 ||
  250. slapi_pblock_set(pb, modfn, (void *) mep_mod_post_op) != 0 ||
  251. slapi_pblock_set(pb, mdnfn, (void *) mep_modrdn_post_op) != 0) {
  252. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  253. "mep_postop_init: failed to register plugin\n");
  254. status = -1;
  255. }
  256. return status;
  257. }
  258. /*
  259. * mep_start()
  260. *
  261. * Creates config lock and loads config cache.
  262. */
  263. static int
  264. mep_start(Slapi_PBlock * pb)
  265. {
  266. Slapi_DN *plugindn = NULL;
  267. char *config_area = NULL;
  268. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  269. "--> mep_start\n");
  270. g_mep_config_lock = slapi_new_rwlock();
  271. if (!g_mep_config_lock) {
  272. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  273. "mep_start: lock creation failed\n");
  274. return -1;
  275. }
  276. /*
  277. * Get the plug-in target dn from the system
  278. * and store it for future use. */
  279. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
  280. if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(plugindn)) {
  281. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  282. "mep_start: unable to retrieve plugin dn\n");
  283. return -1;
  284. }
  285. mep_set_plugin_sdn(slapi_sdn_dup(plugindn));
  286. /* Set the alternate config area if one is defined. */
  287. slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_AREA, &config_area);
  288. if (config_area) {
  289. mep_set_config_area(slapi_sdn_new_normdn_byval(config_area));
  290. }
  291. /*
  292. * Load the config cache
  293. */
  294. g_mep_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  295. PR_INIT_CLIST(g_mep_config);
  296. if (mep_load_config() != 0) {
  297. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  298. "mep_start: unable to load plug-in configuration\n");
  299. return -1;
  300. }
  301. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  302. "managed entries plug-in: ready for service\n");
  303. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  304. "<-- mep_start\n");
  305. return 0;
  306. }
  307. /*
  308. * mep_close()
  309. *
  310. * Cleans up the config cache.
  311. */
  312. static int
  313. mep_close(Slapi_PBlock * pb)
  314. {
  315. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  316. "--> mep_close\n");
  317. mep_delete_config();
  318. slapi_destroy_rwlock(g_mep_config_lock);
  319. g_mep_config_lock = NULL;
  320. slapi_ch_free((void **)&g_mep_config);
  321. slapi_sdn_free(&_PluginDN);
  322. slapi_sdn_free(&_ConfigAreaDN);
  323. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  324. "<-- mep_close\n");
  325. return 0;
  326. }
  327. PRCList *
  328. mep_get_config()
  329. {
  330. return g_mep_config;
  331. }
  332. /*
  333. * config looks like this
  334. * - cn=Managed Entries,cn=plugins,cn=config
  335. * --- cn=user private groups,...
  336. * --- cn=etc,...
  337. */
  338. static int
  339. mep_load_config()
  340. {
  341. int status = 0;
  342. int result;
  343. int i;
  344. Slapi_PBlock *search_pb;
  345. Slapi_Entry **entries = NULL;
  346. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  347. "--> mep_load_config\n");
  348. /* Clear out any old config. */
  349. mep_config_write_lock();
  350. mep_delete_config();
  351. search_pb = slapi_pblock_new();
  352. /* If an alternate config area is configured, find
  353. * the config entries that are beneath it, otherwise
  354. * we load the entries beneath our top-level plug-in
  355. * config entry. */
  356. if (mep_get_config_area()) {
  357. /* Find the config entries beneath the alternate config area. */
  358. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  359. "mep_load_config: Looking for config entries "
  360. "beneath \"%s\".\n", slapi_sdn_get_ndn(mep_get_config_area()));
  361. slapi_search_internal_set_pb(search_pb, slapi_sdn_get_ndn(mep_get_config_area()),
  362. LDAP_SCOPE_SUBTREE, "objectclass=*",
  363. NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
  364. } else {
  365. /* Find the config entries beneath our plugin entry. */
  366. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  367. "mep_load_config: Looking for config entries "
  368. "beneath \"%s\".\n",
  369. slapi_sdn_get_ndn(mep_get_plugin_sdn()));
  370. slapi_search_internal_set_pb(search_pb,
  371. slapi_sdn_get_ndn(mep_get_plugin_sdn()),
  372. LDAP_SCOPE_SUBTREE, "objectclass=*",
  373. NULL, 0, NULL, NULL, mep_get_plugin_id(), 0);
  374. }
  375. slapi_search_internal_pb(search_pb);
  376. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  377. if (LDAP_SUCCESS != result) {
  378. if (mep_get_config_area() && (result == LDAP_NO_SUCH_OBJECT)) {
  379. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  380. "mep_load_config: Config container \"%s\" does "
  381. "not exist.\n", slapi_sdn_get_ndn(mep_get_config_area()));
  382. goto cleanup;
  383. } else {
  384. status = -1;
  385. goto cleanup;
  386. }
  387. }
  388. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  389. &entries);
  390. /* Loop through all of the entries we found and parse them. */
  391. for (i = 0; entries && (entries[i] != NULL); i++) {
  392. /* We don't care about the status here because we may have
  393. * some invalid config entries, but we just want to continue
  394. * looking for valid ones. */
  395. mep_parse_config_entry(entries[i], 1);
  396. }
  397. cleanup:
  398. slapi_free_search_results_internal(search_pb);
  399. slapi_pblock_destroy(search_pb);
  400. mep_config_unlock();
  401. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  402. "<-- mep_load_config\n");
  403. return status;
  404. }
  405. /*
  406. * mep_parse_config_entry()
  407. *
  408. * Parses a single config entry. If apply is non-zero, then
  409. * we will load and start using the new config. You can simply
  410. * validate config without making any changes by setting apply
  411. * to 0.
  412. *
  413. * Returns 0 if the entry is valid and -1 if it is invalid.
  414. */
  415. static int
  416. mep_parse_config_entry(Slapi_Entry * e, int apply)
  417. {
  418. char *value;
  419. struct configEntry *entry = NULL;
  420. struct configEntry *config_entry;
  421. PRCList *list;
  422. int entry_added = 0;
  423. int ret = 0;
  424. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  425. "--> mep_parse_config_entry\n");
  426. /* If this is the main plug-in
  427. * config entry, just bail. */
  428. if ((slapi_sdn_compare(mep_get_plugin_sdn(), slapi_entry_get_sdn(e)) == 0) ||
  429. (mep_get_config_area() && (slapi_sdn_compare(mep_get_config_area(),
  430. slapi_entry_get_sdn(e)) == 0))) {
  431. ret = -1;
  432. goto bail;
  433. }
  434. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  435. if (NULL == entry) {
  436. ret = -1;
  437. goto bail;
  438. }
  439. entry->sdn = slapi_sdn_dup(slapi_entry_get_sdn(e));
  440. if(entry->sdn == NULL){
  441. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  442. "mep_parse_config_entry: Error "
  443. "reading dn from config entry\n");
  444. ret = -1;
  445. goto bail;
  446. }
  447. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  448. "----------> dn [%s]\n", slapi_sdn_get_dn(entry->sdn));
  449. /* Load the origin scope */
  450. value = slapi_entry_attr_get_charptr(e, MEP_SCOPE_TYPE);
  451. if (value) {
  452. entry->origin_scope = value;
  453. } else {
  454. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  455. "mep_parse_config_entry: The %s config "
  456. "setting is required for config entry \"%s\".\n",
  457. MEP_SCOPE_TYPE, slapi_sdn_get_dn(entry->sdn));
  458. ret = -1;
  459. goto bail;
  460. }
  461. /* Load the origin filter */
  462. value = slapi_entry_attr_get_charptr(e, MEP_FILTER_TYPE);
  463. if (value) {
  464. /* Convert to a Slapi_Filter to improve performance. */
  465. if (NULL == (entry->origin_filter = slapi_str2filter(value))) {
  466. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM ,
  467. "mep_parse_config_entry: Invalid search filter in "
  468. "%s config setting for config entry \"%s\" "
  469. "(filter = \"%s\").\n", MEP_FILTER_TYPE, slapi_sdn_get_dn(entry->sdn), value);
  470. ret = -1;
  471. }
  472. slapi_ch_free_string(&value);
  473. if (ret != 0) {
  474. goto bail;
  475. }
  476. } else {
  477. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  478. "mep_parse_config_entry: The %s config "
  479. "setting is required for config entry \"%s\".\n",
  480. MEP_FILTER_TYPE, slapi_sdn_get_dn(entry->sdn));
  481. ret = -1;
  482. goto bail;
  483. }
  484. /* Load the managed base */
  485. value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BASE_TYPE);
  486. if (value) {
  487. entry->managed_base = value;
  488. } else {
  489. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  490. "mep_parse_config_entry: The %s config "
  491. "setting is required for config entry \"%s\".\n",
  492. MEP_MANAGED_BASE_TYPE, slapi_sdn_get_dn(entry->sdn));
  493. ret = -1;
  494. goto bail;
  495. }
  496. /* Load the managed template */
  497. value = slapi_entry_attr_get_charptr(e, MEP_MANAGED_TEMPLATE_TYPE);
  498. if (value) {
  499. Slapi_Entry *test_entry = NULL;
  500. entry->template_sdn = slapi_sdn_new_dn_passin(value);
  501. value = NULL; /* entry->template_sdn owns value now */
  502. /* Fetch the managed entry template */
  503. slapi_search_internal_get_entry(entry->template_sdn, 0,
  504. &entry->template_entry, mep_get_plugin_id());
  505. if (entry->template_entry == NULL) {
  506. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  507. "mep_parse_config_entry: The managed entry "
  508. "template \"%s\" does not exist. Please "
  509. "add it or correct the %s config setting for "
  510. "config entry \"%s\"\n", slapi_sdn_get_dn(entry->template_sdn),
  511. MEP_MANAGED_TEMPLATE_TYPE, slapi_sdn_get_dn(entry->sdn));
  512. ret = -1;
  513. goto bail;
  514. }
  515. /* Validate the template entry by creating a test managed
  516. * entry and running a schema check on it */
  517. test_entry = mep_create_managed_entry(entry, NULL);
  518. if (test_entry == NULL) {
  519. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  520. "mep_parse_config_entry: Unable to create "
  521. "a test managed entry from managed entry "
  522. "template \"%s\". Please check the template "
  523. "entry for errors.\n", slapi_sdn_get_dn(entry->template_sdn));
  524. ret = -1;
  525. goto bail;
  526. }
  527. /* Check the schema */
  528. if (slapi_entry_schema_check(NULL, test_entry) != 0) {
  529. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  530. "mep_parse_config_entry: Test managed "
  531. "entry created from managed entry template "
  532. "\"%s\" violates the schema. Please check "
  533. "the template entry for schema errors.\n",
  534. slapi_sdn_get_dn(entry->template_sdn));
  535. slapi_entry_free(test_entry);
  536. ret = -1;
  537. goto bail;
  538. }
  539. /*
  540. * Extract the origin attrs from the template entry
  541. */
  542. entry->origin_attrs = mep_extract_origin_attrs(entry->template_entry);
  543. /* Dispose of the test entry */
  544. slapi_entry_free(test_entry);
  545. } else {
  546. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  547. "mep_parse_config_entry: The %s config "
  548. "setting is required for config entry \"%s\".\n",
  549. MEP_MANAGED_TEMPLATE_TYPE, slapi_sdn_get_dn(entry->sdn));
  550. ret = -1;
  551. goto bail;
  552. }
  553. /* If we were only called to validate config, we can
  554. * just bail out before applying the config changes */
  555. if (apply == 0) {
  556. goto bail;
  557. }
  558. /* Add the config object to the list. We order by scope. */
  559. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  560. list = PR_LIST_HEAD(g_mep_config);
  561. while (list != g_mep_config) {
  562. config_entry = (struct configEntry *) list;
  563. /* If the config entry we are adding has a scope that is
  564. * a child of the scope of the current list item, we insert
  565. * the entry before that list item. */
  566. if (slapi_dn_issuffix(entry->origin_scope, config_entry->origin_scope)) {
  567. PR_INSERT_BEFORE(&(entry->list), list);
  568. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  569. "store [%s] before [%s] \n", slapi_sdn_get_dn(entry->sdn),
  570. slapi_sdn_get_dn(config_entry->sdn));
  571. entry_added = 1;
  572. break;
  573. }
  574. list = PR_NEXT_LINK(list);
  575. /* If we hit the end of the list, add to the tail. */
  576. if (g_mep_config == list) {
  577. PR_INSERT_BEFORE(&(entry->list), list);
  578. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  579. "store [%s] at tail\n", slapi_sdn_get_dn(entry->sdn));
  580. entry_added = 1;
  581. break;
  582. }
  583. }
  584. } else {
  585. /* first entry */
  586. PR_INSERT_LINK(&(entry->list), g_mep_config);
  587. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  588. "store [%s] at head \n", slapi_sdn_get_dn(entry->sdn));
  589. entry_added = 1;
  590. }
  591. bail:
  592. if (0 == entry_added) {
  593. /* Don't log error if we weren't asked to apply config */
  594. if ((apply != 0) && (entry != NULL)) {
  595. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  596. "mep_parse_config_entry: Invalid config entry "
  597. "[%s] skipped\n", slapi_sdn_get_dn(entry->sdn));
  598. }
  599. mep_free_config_entry(&entry);
  600. } else {
  601. ret = 0;
  602. }
  603. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  604. "<-- mep_parse_config_entry\n");
  605. return ret;
  606. }
  607. static void
  608. mep_free_config_entry(struct configEntry ** entry)
  609. {
  610. struct configEntry *e = *entry;
  611. if (e == NULL)
  612. return;
  613. if (e->sdn) {
  614. slapi_log_error(SLAPI_LOG_CONFIG, MEP_PLUGIN_SUBSYSTEM,
  615. "freeing config entry [%s]\n", slapi_sdn_get_dn(e->sdn));
  616. slapi_sdn_free(&e->sdn);
  617. }
  618. if (e->origin_scope) {
  619. slapi_ch_free_string(&e->origin_scope);
  620. }
  621. if (e->origin_filter) {
  622. slapi_filter_free(e->origin_filter, 1);
  623. }
  624. if (e->managed_base) {
  625. slapi_ch_free_string(&e->managed_base);
  626. }
  627. if (e->template_sdn) {
  628. slapi_sdn_free(&e->template_sdn);
  629. }
  630. if (e->template_entry) {
  631. slapi_entry_free(e->template_entry);
  632. }
  633. if(e->origin_attrs){
  634. slapi_ch_array_free(e->origin_attrs);
  635. }
  636. slapi_ch_free((void **) entry);
  637. }
  638. static void
  639. mep_delete_configEntry(PRCList *entry)
  640. {
  641. PR_REMOVE_LINK(entry);
  642. mep_free_config_entry((struct configEntry **) &entry);
  643. }
  644. static void
  645. mep_delete_config()
  646. {
  647. PRCList *list;
  648. /* Delete the config cache. */
  649. while (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  650. list = PR_LIST_HEAD(g_mep_config);
  651. mep_delete_configEntry(list);
  652. }
  653. return;
  654. }
  655. /*
  656. * Helper functions
  657. */
  658. /*
  659. * mep_parse_mapped_origin_attr()
  660. *
  661. * Parses a mapped attribute setting from a template and
  662. * grabs the attribute name and places it in origin_type.
  663. *
  664. * This is used to determine if a modify operation needs
  665. * to update the managed entry.
  666. */
  667. static int
  668. mep_parse_mapped_origin_attr(char *mapping, char **origin_type)
  669. {
  670. int ret = 0;
  671. char *p = NULL;
  672. char *end = NULL;
  673. char *var_start = NULL;
  674. /* reset the pointer for origin_type as this func is usually in a loop */
  675. *origin_type = NULL;
  676. /* split out the type from the value (use the first ':') */
  677. if ((p = strchr(mapping, ':')) == NULL) {
  678. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  679. "mep_parse_mapped_origin_attr: Value for mapped attribute "
  680. "is not in the correct format. (value: \"%s\").\n",
  681. mapping);
  682. ret = 1;
  683. goto bail;
  684. }
  685. /* Ensure the type is not empty. */
  686. if (p == mapping) {
  687. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  688. "mep_parse_mapped_origin_attr: Value for mapped attribute "
  689. "is not in the correct format. The type is missing. "
  690. "(value: \"%s\").\n",
  691. mapping);
  692. ret = 1;
  693. goto bail;
  694. }
  695. /* Terminate the type so we can use it as a string. */
  696. *p = '\0';
  697. /* Advance p to point to the beginning of the value. */
  698. p++;
  699. while (*p == ' ') {
  700. p++;
  701. }
  702. /* Make end point to the last character that we want in the value. */
  703. end = p + strlen(p) - 1;
  704. /* Find the variable that we need to substitute. */
  705. for (; p <= end; p++) {
  706. if (*p == '$') {
  707. if (p == end) {
  708. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  709. "mep_parse_mapped_origin_attr: Invalid mapped "
  710. "attribute value for type \"%s\".\n", mapping);
  711. ret = 1;
  712. goto bail;
  713. }
  714. if (*(p + 1) == '$') {
  715. /* This is an escaped $. Eliminate the escape character
  716. * to prevent if from being a part of the value. */
  717. p++;
  718. memmove(p, p+1, end-(p+1)+1);
  719. *end = '\0';
  720. end--;
  721. } else {
  722. int quoted = 0;
  723. /* We found a variable. Terminate the pre
  724. * string and process the variable. */
  725. *p = '\0';
  726. p++;
  727. /* Check if the variable name is quoted. If it is, we skip past
  728. * the quoting brace to avoid putting it in the mapped value. */
  729. if (*p == '{') {
  730. quoted = 1;
  731. if (p < end) {
  732. p++;
  733. } else {
  734. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  735. "mep_parse_mapped_origin_attr: Invalid mapped "
  736. "attribute value for type \"%s\".\n", mapping);
  737. ret = 1;
  738. goto bail;
  739. }
  740. }
  741. /* We should be pointing at the variable name now. */
  742. var_start = p;
  743. /* Move the pointer to the end of the variable name. We
  744. * stop at the first character that is not legal for use
  745. * in an attribute description. */
  746. while ((p < end) && IS_ATTRDESC_CHAR(*p)) {
  747. p++;
  748. }
  749. /* If the variable is quoted and this is not a closing
  750. * brace, there is a syntax error in the mapping rule. */
  751. if (quoted && (*p != '}')) {
  752. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  753. "mep_parse_mapped_origin_attr: Invalid mapped "
  754. "attribute value for type \"%s\".\n", mapping);
  755. ret = 1;
  756. goto bail;
  757. }
  758. /* Check for a missing variable name. */
  759. if (p == var_start) {
  760. break;
  761. }
  762. if (p == end) {
  763. /* Set the map type. In this case, p could be
  764. * pointing at either the last character of the
  765. * map type, or at the first character after the
  766. * map type. If the character is valid for use
  767. * in an attribute description, we consider it
  768. * to be a part of the map type. */
  769. if (IS_ATTRDESC_CHAR(*p)) {
  770. *origin_type = strndup(var_start, p - var_start + 1);
  771. /* There is no post string. */
  772. } else {
  773. *origin_type = strndup(var_start, p - var_start);
  774. }
  775. } else {
  776. /* Set the map type. In this case, p is pointing
  777. * at the first character after the map type. */
  778. *origin_type = strndup(var_start, p - var_start);
  779. }
  780. /* We only support a single variable, so we're done. */
  781. break;
  782. }
  783. }
  784. }
  785. bail:
  786. if (ret != 0) {
  787. slapi_ch_free_string(origin_type);
  788. }
  789. return ret;
  790. }
  791. /*
  792. * mep_extract_origin_attrs
  793. *
  794. * Extract the attributes from the template that reside on
  795. * the origin entry that trigger updates to the managed entry.
  796. */
  797. static char **
  798. mep_extract_origin_attrs(Slapi_Entry *template)
  799. {
  800. char **vals = NULL;
  801. char *origin_type = NULL;
  802. char **origin_attrs = NULL;
  803. int i;
  804. if (template) {
  805. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  806. for (i = 0; vals && vals[i]; ++i) {
  807. if (mep_parse_mapped_origin_attr(vals[i], &origin_type) == 0) {
  808. slapi_ch_array_add(&origin_attrs,origin_type);
  809. }
  810. }
  811. slapi_ch_array_free(vals);
  812. }
  813. return origin_attrs;
  814. }
  815. /*
  816. * mep_is_mapped_origin_attr()
  817. *
  818. * Checks if type is a mapped origin attribute.
  819. */
  820. static int
  821. mep_is_mapped_origin_attr(char **vals, char *type)
  822. {
  823. int ret = 0;
  824. int i;
  825. if (type) {
  826. for (i = 0; vals && vals[i]; ++i) {
  827. if (slapi_attr_type_cmp(vals[i], type, SLAPI_TYPE_CMP_EXACT) == 0) {
  828. /* Ok, we are modifying a attribute that affects the managed entry */
  829. ret = 1;
  830. break;
  831. }
  832. }
  833. }
  834. return ret;
  835. }
  836. static Slapi_DN *
  837. mep_get_sdn(Slapi_PBlock * pb)
  838. {
  839. Slapi_DN *sdn = 0;
  840. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  841. "--> mep_get_sdn\n");
  842. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  843. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  844. "<-- mep_get_sdn\n");
  845. return sdn;
  846. }
  847. static void
  848. mep_set_config_area(Slapi_DN *sdn)
  849. {
  850. _ConfigAreaDN = sdn;
  851. }
  852. static Slapi_DN *
  853. mep_get_config_area()
  854. {
  855. return _ConfigAreaDN;
  856. }
  857. /*
  858. * mep_dn_is_config()
  859. *
  860. * Checks if dn is a managed entries config entry.
  861. */
  862. static int
  863. mep_dn_is_config(Slapi_DN *sdn)
  864. {
  865. int ret = 0;
  866. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  867. "--> mep_dn_is_config\n");
  868. if (sdn == NULL) {
  869. goto bail;
  870. }
  871. /* If an alternate config area is configured, treat it's child
  872. * entries as config entries. If the alternate config area is
  873. * not configured, treat children of the top-level plug-in
  874. * config entry as our config entries. */
  875. if (mep_get_config_area()) {
  876. if (slapi_sdn_issuffix(sdn, mep_get_config_area()) &&
  877. slapi_sdn_compare(sdn, mep_get_config_area())) {
  878. ret = 1;
  879. }
  880. } else {
  881. if (slapi_sdn_issuffix(sdn, mep_get_plugin_sdn()) &&
  882. slapi_sdn_compare(sdn, mep_get_plugin_sdn())) {
  883. ret = 1;
  884. }
  885. }
  886. bail:
  887. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  888. "<-- mep_dn_is_config\n");
  889. return ret;
  890. }
  891. /*
  892. * mep_dn_is_template()
  893. *
  894. * Checks if dn is a managed entries template.
  895. */
  896. static int
  897. mep_dn_is_template(Slapi_DN *sdn)
  898. {
  899. int ret = 0;
  900. PRCList *list = NULL;
  901. Slapi_DN *config_sdn = NULL;
  902. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  903. list = PR_LIST_HEAD(g_mep_config);
  904. while (list != g_mep_config) {
  905. config_sdn = ((struct configEntry *)list)->template_sdn;
  906. if (slapi_sdn_compare(config_sdn, sdn) == 0) {
  907. ret = 1;
  908. break;
  909. } else {
  910. list = PR_NEXT_LINK(list);
  911. }
  912. }
  913. }
  914. return ret;
  915. }
  916. /*
  917. * mep_find_config()
  918. *
  919. * Finds the appropriate config entry for a given entry
  920. * by checking if the entry meets the origin scope and
  921. * filter requirements of any config entry. A read lock
  922. * must be held on the config before calling this function.
  923. * The configEntry that is returned is a pointer to the
  924. * actual config entry in the config cache. It should not
  925. * be modified in any way. The read lock should not be
  926. * released until you are finished with the config entry
  927. * that is returned.
  928. *
  929. * Returns NULL if no applicable config entry is found.
  930. */
  931. static void
  932. mep_find_config(Slapi_Entry *e, struct configEntry **config)
  933. {
  934. PRCList *list = NULL;
  935. char *dn = NULL;
  936. *config = NULL;
  937. if (e && !PR_CLIST_IS_EMPTY(g_mep_config)) {
  938. dn = slapi_entry_get_dn(e);
  939. list = PR_LIST_HEAD(g_mep_config);
  940. while (list != g_mep_config) {
  941. /* See if the dn is within the scope of this config entry
  942. * in addition to matching the origin filter. */
  943. if (slapi_dn_issuffix(dn, ((struct configEntry *)list)->origin_scope) &&
  944. (slapi_filter_test_simple(e, ((struct configEntry *)list)->origin_filter) == 0)) {
  945. *config = (struct configEntry *)list;
  946. break;
  947. }
  948. list = PR_NEXT_LINK(list);
  949. }
  950. }
  951. }
  952. /*
  953. * mep_find_config_by_template_dn()
  954. *
  955. * Finds the config entry associated with a particular
  956. * template dn. A read lock must be held on the config
  957. * before calling this function. The configEntry that
  958. * us returned is a pointer to the actual config entry
  959. * in the config cache. It should not be modified in
  960. * any way. The read lock should not be released until
  961. * you are finished with the config entry that is returned.
  962. *
  963. * Returns NULL if no applicable config entry is found.
  964. */
  965. static void
  966. mep_find_config_by_template_dn(Slapi_DN *template_sdn,
  967. struct configEntry **config)
  968. {
  969. PRCList *list = NULL;
  970. Slapi_DN *config_sdn = NULL;
  971. *config = NULL;
  972. if (!PR_CLIST_IS_EMPTY(g_mep_config)) {
  973. list = PR_LIST_HEAD(g_mep_config);
  974. while (list != g_mep_config) {
  975. config_sdn = ((struct configEntry *)list)->template_sdn;
  976. if (slapi_sdn_compare(config_sdn, template_sdn) == 0) {
  977. *config = (struct configEntry *)list;
  978. break;
  979. } else {
  980. list = PR_NEXT_LINK(list);
  981. }
  982. }
  983. }
  984. }
  985. /*
  986. * mep_oktodo()
  987. *
  988. * Check if we want to process this operation. We need to be
  989. * sure that the operation succeeded.
  990. */
  991. static int
  992. mep_oktodo(Slapi_PBlock *pb)
  993. {
  994. int ret = 1;
  995. int oprc = 0;
  996. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  997. "--> mep_oktodo\n" );
  998. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0) {
  999. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1000. "mep_oktodo: could not get parameters\n" );
  1001. ret = -1;
  1002. }
  1003. /* This plugin should only execute if the operation succeeded. */
  1004. if(oprc != 0) {
  1005. ret = 0;
  1006. }
  1007. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1008. "<-- mep_oktodo\n" );
  1009. return ret;
  1010. }
  1011. /*
  1012. * mep_isrepl()
  1013. *
  1014. * Returns 1 if the operation associated with pb
  1015. * is a replicated op. Returns 0 otherwise.
  1016. */
  1017. static int
  1018. mep_isrepl(Slapi_PBlock *pb)
  1019. {
  1020. int is_repl = 0;
  1021. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1022. "--> mep_isrepl\n" );
  1023. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_repl);
  1024. slapi_log_error( SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1025. "<-- mep_isrepl\n" );
  1026. return is_repl;
  1027. }
  1028. /*
  1029. * mep_create_managed_entry()
  1030. *
  1031. * Creates a managed entry from a specified template and origin
  1032. * entry. If an origin entry is not passed in, the values of
  1033. * mapped attributes will not be filled in and the addition of
  1034. * a backlink to the origin is not added. This is useful for
  1035. * creating a test managed entry for template validation.
  1036. */
  1037. static Slapi_Entry *
  1038. mep_create_managed_entry(struct configEntry *config, Slapi_Entry *origin)
  1039. {
  1040. Slapi_Entry *managed_entry = NULL;
  1041. Slapi_Entry *template = NULL;
  1042. char *rdn_type = NULL;
  1043. char **vals = NULL;
  1044. char *type = NULL;
  1045. char *value = NULL;
  1046. Slapi_Value *sval = NULL;
  1047. int found_rdn_map = 0;
  1048. int i = 0;
  1049. int err = 0;
  1050. /* If no template was supplied, there's nothing we can do. */
  1051. if ((config == NULL) || (config->template_entry == NULL)) {
  1052. return NULL;
  1053. } else {
  1054. template = config->template_entry;
  1055. }
  1056. /* Ensure that a RDN type was specified in the template. */
  1057. if ((rdn_type = slapi_entry_attr_get_charptr(template, MEP_RDN_ATTR_TYPE)) == NULL) {
  1058. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1059. "mep_create_managed_entry: The %s config attribute "
  1060. "was not found in template \"%s\". This attribute "
  1061. "is required.\n", MEP_RDN_ATTR_TYPE, slapi_sdn_get_dn(config->template_sdn));
  1062. err = 1;
  1063. goto done;
  1064. }
  1065. /* Create the entry to be returned. */
  1066. managed_entry = slapi_entry_alloc();
  1067. slapi_entry_init(managed_entry, NULL, NULL);
  1068. /* Add all of the static attributes from the template to the newly
  1069. * created managed entry. */
  1070. vals = slapi_entry_attr_get_charray(template, MEP_STATIC_ATTR_TYPE);
  1071. for (i = 0; vals && vals[i]; ++i) {
  1072. struct berval bvtype = {0, NULL}, bvvalue = {0, NULL};
  1073. int freeval = 0;
  1074. if (slapi_ldif_parse_line(vals[i], &bvtype, &bvvalue, &freeval) != 0) {
  1075. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1076. "mep_create_managed_entry: Value for %s config setting "
  1077. "is not in the correct format in template \"%s\". "
  1078. "(value: \"%s\")\n", MEP_STATIC_ATTR_TYPE,
  1079. slapi_sdn_get_dn(config->template_sdn), vals[i]);
  1080. err = 1;
  1081. goto done;
  1082. } else {
  1083. /* Create a berval and add the value to the entry. */
  1084. sval = slapi_value_new_berval(&bvvalue);
  1085. slapi_entry_add_value(managed_entry, bvtype.bv_val, sval);
  1086. slapi_value_free(&sval);
  1087. /* Set type and value to NULL so they don't get
  1088. * free'd by mep_parse_mapped_attr(). */
  1089. if (freeval) {
  1090. slapi_ch_free_string(&bvvalue.bv_val);
  1091. }
  1092. type = NULL;
  1093. value = NULL;
  1094. }
  1095. }
  1096. /* Clear out vals so we can use them again */
  1097. slapi_ch_array_free(vals);
  1098. /* Add the mapped attributes to the newly created managed entry. */
  1099. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1100. for (i = 0; vals && vals[i]; ++i) {
  1101. if (mep_parse_mapped_attr(vals[i], origin, &type, &value) == 0) {
  1102. /* Add the attribute to the managed entry. */
  1103. slapi_entry_add_string(managed_entry, type, value);
  1104. /* Check if this type is the RDN type. */
  1105. if (slapi_attr_type_cmp(type, rdn_type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1106. found_rdn_map = 1;
  1107. }
  1108. slapi_ch_free_string(&type);
  1109. slapi_ch_free_string(&value);
  1110. } else {
  1111. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1112. "mep_create_managed_entry: Error parsing mapped attribute "
  1113. "in template \"%s\".\n", slapi_sdn_get_dn(config->template_sdn));
  1114. err = 1;
  1115. goto done;
  1116. }
  1117. }
  1118. /* The RDN attribute must be a mapped attribute. If we didn't find it,
  1119. * we need to bail. */
  1120. if (!found_rdn_map) {
  1121. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1122. "mep_create_managed_entry: The RDN type \"%s\" "
  1123. "was not found as a mapped attribute in template "
  1124. "\"%s\". It must be a mapped attribute.\n",
  1125. rdn_type, slapi_sdn_get_dn(config->template_sdn));
  1126. err = 1;
  1127. goto done;
  1128. } else {
  1129. /* Build the DN and set it in the entry. */
  1130. char *dn = NULL;
  1131. char *rdn_val = NULL;
  1132. /* If an origin entry was supplied, the RDN value will be
  1133. * the mapped value. If no origin entry was supplied, the
  1134. * value will be the mapping rule from the template. */
  1135. rdn_val = slapi_entry_attr_get_charptr(managed_entry, rdn_type);
  1136. /* Create the DN using the mapped RDN value
  1137. * and the base specified in the config. */
  1138. dn = slapi_ch_smprintf("%s=%s,%s", rdn_type, rdn_val, config->managed_base);
  1139. slapi_ch_free_string(&rdn_val);
  1140. if (dn != NULL) {
  1141. slapi_sdn_set_dn_passin(slapi_entry_get_sdn(managed_entry), dn);
  1142. } else {
  1143. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1144. "mep_create_managed_entry: Error setting DN "
  1145. "in managed entry based off of template entry "
  1146. "\"%s\" (origin entry \"%s\").\n",
  1147. slapi_sdn_get_dn(config->template_sdn),
  1148. origin ? slapi_entry_get_dn(origin) : "NULL");
  1149. err = 1;
  1150. goto done;
  1151. }
  1152. }
  1153. /* If an origin entry was supplied, set a backpointer to the
  1154. * origin in the managed entry. */
  1155. if (origin) {
  1156. slapi_entry_add_string(managed_entry,
  1157. SLAPI_ATTR_OBJECTCLASS, MEP_MANAGED_OC);
  1158. slapi_entry_add_string(managed_entry, MEP_MANAGED_BY_TYPE,
  1159. slapi_entry_get_dn(origin));
  1160. }
  1161. done:
  1162. slapi_ch_array_free(vals);
  1163. slapi_ch_free_string(&rdn_type);
  1164. if (err != 0) {
  1165. slapi_entry_free(managed_entry);
  1166. managed_entry = NULL;
  1167. }
  1168. return managed_entry;
  1169. }
  1170. /*
  1171. * mep_add_managed_entry()
  1172. *
  1173. * Creates and adds a managed entry to the database. The
  1174. * origin entry will also be modified to add a link to the
  1175. * newly created managed entry.
  1176. */
  1177. static int
  1178. mep_add_managed_entry(struct configEntry *config,
  1179. Slapi_Entry *origin)
  1180. {
  1181. Slapi_Entry *managed_entry = NULL;
  1182. char *managed_dn = NULL;
  1183. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1184. int result = LDAP_SUCCESS;
  1185. /* Create the managed entry */
  1186. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1187. "mep_add_managed_entry: Creating a managed "
  1188. "entry from origin entry \"%s\" using "
  1189. "config \"%s\".\n", slapi_entry_get_dn(origin),
  1190. slapi_sdn_get_dn(config->sdn));
  1191. managed_entry = mep_create_managed_entry(config, origin);
  1192. if (managed_entry == NULL) {
  1193. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1194. "mep_add_managed_entry: Unable to create a managed "
  1195. "entry from origin entry \"%s\" using config "
  1196. "\"%s\".\n", slapi_entry_get_dn(origin), slapi_sdn_get_dn(config->sdn));
  1197. result = -1;
  1198. goto bail;
  1199. } else {
  1200. /* Copy the managed entry DN to use when
  1201. * creating the pointer attribute. */
  1202. managed_dn = slapi_ch_strdup(slapi_entry_get_dn(managed_entry));
  1203. /* Add managed entry to db. The entry will be consumed. */
  1204. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1205. "Adding managed entry \"%s\" for origin "
  1206. "entry \"%s\"\n.", managed_dn, slapi_entry_get_dn(origin));
  1207. slapi_add_entry_internal_set_pb(mod_pb, managed_entry, NULL,
  1208. mep_get_plugin_id(), 0);
  1209. slapi_add_internal_pb(mod_pb);
  1210. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1211. if (result != LDAP_SUCCESS) {
  1212. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1213. "mep_add_managed_entry: Unable to add managed "
  1214. "entry \"%s\" for origin entry \"%s\" (%s).\n",
  1215. managed_dn, slapi_entry_get_dn(origin),
  1216. ldap_err2string(result));
  1217. goto bail;
  1218. } else {
  1219. /* Add forward link to origin entry. */
  1220. LDAPMod oc_mod;
  1221. LDAPMod pointer_mod;
  1222. LDAPMod *mods[2];
  1223. char *oc_vals[2];
  1224. char *pointer_vals[2];
  1225. /* Clear out the pblock for reuse. */
  1226. slapi_pblock_init(mod_pb);
  1227. /*
  1228. * Add the origin entry objectclass. A modrdn might result in
  1229. * an err 20 (type or value exists), in which case just ignore it.
  1230. */
  1231. oc_vals[0] = MEP_ORIGIN_OC;
  1232. oc_vals[1] = 0;
  1233. oc_mod.mod_op = LDAP_MOD_ADD;
  1234. oc_mod.mod_type = SLAPI_ATTR_OBJECTCLASS;
  1235. oc_mod.mod_values = oc_vals;
  1236. mods[0] = &oc_mod;
  1237. mods[1] = NULL;
  1238. /* add the objectclass */
  1239. slapi_modify_internal_set_pb_ext(mod_pb, slapi_entry_get_sdn(origin),
  1240. mods, 0, 0, mep_get_plugin_id(), 0);
  1241. slapi_modify_internal_pb(mod_pb);
  1242. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1243. if (result != LDAP_SUCCESS && result != LDAP_TYPE_OR_VALUE_EXISTS){
  1244. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1245. "mep_add_managed_entry: Failed to add managed entry "
  1246. "objectclass in origin entry \"%s\", error (%s)\n",
  1247. slapi_entry_get_dn(origin), ldap_err2string(result));
  1248. goto bail;
  1249. }
  1250. slapi_pblock_init(mod_pb);
  1251. /*
  1252. * Now, add a pointer to the managed entry.
  1253. */
  1254. pointer_vals[0] = managed_dn;
  1255. pointer_vals[1] = 0;
  1256. pointer_mod.mod_op = LDAP_MOD_ADD;
  1257. pointer_mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1258. pointer_mod.mod_values = pointer_vals;
  1259. mods[0] = &pointer_mod;
  1260. mods[1] = NULL;
  1261. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1262. "Adding %s pointer to \"%s\" in entry \"%s\"\n.",
  1263. MEP_MANAGED_ENTRY_TYPE, managed_dn, slapi_entry_get_dn(origin));
  1264. slapi_modify_internal_set_pb_ext(mod_pb, slapi_entry_get_sdn(origin),
  1265. mods, 0, 0, mep_get_plugin_id(), 0);
  1266. slapi_modify_internal_pb(mod_pb);
  1267. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1268. if (result != LDAP_SUCCESS) {
  1269. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1270. "mep_add_managed_entry: Unable to add pointer to "
  1271. "managed entry \"%s\" in origin entry \"%s\" "
  1272. "(%s).\n", managed_dn, slapi_entry_get_dn(origin),
  1273. ldap_err2string(result));
  1274. }
  1275. }
  1276. }
  1277. bail:
  1278. slapi_ch_free_string(&managed_dn);
  1279. slapi_pblock_destroy(mod_pb);
  1280. return result;
  1281. }
  1282. /* mep_rename_managed_entry()
  1283. *
  1284. * Renames a managed entry and updates the pointer in the
  1285. * origin entry.
  1286. */
  1287. static int
  1288. mep_rename_managed_entry(Slapi_Entry *origin,
  1289. Slapi_DN *new_dn, Slapi_DN *old_dn)
  1290. {
  1291. Slapi_RDN *srdn = slapi_rdn_new();
  1292. Slapi_PBlock *mep_pb = slapi_pblock_new();
  1293. LDAPMod mod;
  1294. LDAPMod *mods[2];
  1295. char *vals[2];
  1296. int result = LDAP_SUCCESS;
  1297. /* Just bail if any of our parameters are NULL. */
  1298. if (origin == NULL || new_dn == NULL || old_dn == NULL) {
  1299. goto bail;
  1300. }
  1301. /* Create new RDN */
  1302. slapi_rdn_set_dn(srdn, slapi_sdn_get_ndn(new_dn));
  1303. /* Rename the managed entry. */
  1304. slapi_rename_internal_set_pb_ext(mep_pb, old_dn,
  1305. slapi_rdn_get_rdn(srdn),
  1306. NULL, 1, NULL, NULL, mep_get_plugin_id(), 0);
  1307. slapi_modrdn_internal_pb(mep_pb);
  1308. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1309. if (result != LDAP_SUCCESS) {
  1310. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1311. "mep_rename_managed_entry: Unable to rename managed "
  1312. "entry \"%s\" to \"%s\" (%s).\n", slapi_sdn_get_dn(old_dn),
  1313. slapi_sdn_get_dn(new_dn), ldap_err2string(result));
  1314. } else {
  1315. /* Clear out the pblock for reuse. */
  1316. slapi_pblock_init(mep_pb);
  1317. /* Update the link to the managed entry in the origin entry. */
  1318. vals[0] = (char *)slapi_sdn_get_dn(new_dn);
  1319. vals[1] = 0;
  1320. mod.mod_op = LDAP_MOD_REPLACE;
  1321. mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  1322. mod.mod_values = vals;
  1323. mods[0] = &mod;
  1324. mods[1] = 0;
  1325. /* Perform the modify operation. */
  1326. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1327. "mep_rename_managed_entry: Updating %s pointer to "
  1328. "\"%s\" in entry \"%s\"\n.", MEP_MANAGED_ENTRY_TYPE,
  1329. vals[0], slapi_entry_get_dn(origin));
  1330. slapi_modify_internal_set_pb_ext(mep_pb, slapi_entry_get_sdn(origin),
  1331. mods, 0, 0, mep_get_plugin_id(), 0);
  1332. slapi_modify_internal_pb(mep_pb);
  1333. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  1334. if (result != LDAP_SUCCESS) {
  1335. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1336. "mep_rename_managed_entry: Unable to update %s "
  1337. "pointer in entry \"%s\" (%s).\n", MEP_MANAGED_ENTRY_TYPE,
  1338. slapi_entry_get_dn(origin), ldap_err2string(result));
  1339. }
  1340. }
  1341. bail:
  1342. slapi_rdn_free(&srdn);
  1343. slapi_pblock_destroy(mep_pb);
  1344. return result;
  1345. }
  1346. /*
  1347. * mep_get_mapped_mods()
  1348. *
  1349. * Creates the modifications needed to update the mapped values
  1350. * in a managed entry. It is up to the caller to free the
  1351. * returned mods when it is finished using them.
  1352. */
  1353. static Slapi_Mods *mep_get_mapped_mods(struct configEntry *config,
  1354. Slapi_Entry *origin, char **mapped_dn)
  1355. {
  1356. Slapi_Mods *smods = NULL;
  1357. Slapi_Entry *template = NULL;
  1358. Slapi_Attr *attr = NULL;
  1359. char **vals = NULL;
  1360. char *type = NULL;
  1361. char *value = NULL;
  1362. char *rdn_type = NULL;
  1363. int i = 0;
  1364. /* If no template was supplied, there's nothing we can do. */
  1365. if (origin == NULL || config == NULL || config->template_entry == NULL) {
  1366. return NULL;
  1367. } else {
  1368. template = config->template_entry;
  1369. }
  1370. /* See how many mods we will have and initialize the smods. */
  1371. if (slapi_entry_attr_find(config->template_entry, MEP_MAPPED_ATTR_TYPE, &attr) == 0) {
  1372. int numvals = 0;
  1373. slapi_attr_get_numvalues(attr, &numvals);
  1374. smods = slapi_mods_new();
  1375. slapi_mods_init(smods, numvals);
  1376. }
  1377. /* Find the the RDN type for the managed entry. */
  1378. if ((rdn_type = slapi_entry_attr_get_charptr(template, MEP_RDN_ATTR_TYPE)) == NULL) {
  1379. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1380. "mep_get_mapped_mods: Error getting RDN type from tempate "
  1381. "\"%s\".\n", slapi_sdn_get_dn(config->template_sdn));
  1382. slapi_mods_free(&smods);
  1383. goto bail;
  1384. }
  1385. /* Go through the template and find the mapped attrs. */
  1386. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1387. for (i = 0; vals && vals[i]; ++i) {
  1388. if (mep_parse_mapped_attr(vals[i], origin, &type, &value) == 0) {
  1389. /* Don't attempt to modify the RDN type, but create
  1390. * the mapped DN if requested. It is up to the caller
  1391. * to free the returned DN. */
  1392. if (slapi_attr_type_cmp(type, rdn_type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1393. if (mapped_dn) {
  1394. *mapped_dn = slapi_create_dn_string("%s=%s,%s", rdn_type,
  1395. value, config->managed_base);
  1396. }
  1397. } else {
  1398. /* Add a modify that replaces all values with the new value. */
  1399. slapi_mods_add_string(smods, LDAP_MOD_REPLACE, type, value);
  1400. }
  1401. slapi_ch_free_string(&type);
  1402. slapi_ch_free_string(&value);
  1403. } else {
  1404. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1405. "mep_get_mapped_mods: Error parsing mapped attribute "
  1406. "in template \"%s\".\n", slapi_sdn_get_dn(config->template_sdn));
  1407. slapi_mods_free(&smods);
  1408. goto bail;
  1409. }
  1410. }
  1411. bail:
  1412. slapi_ch_free_string(&rdn_type);
  1413. slapi_ch_array_free(vals);
  1414. return smods;
  1415. }
  1416. /*
  1417. * mep_parse_mapped_attr()
  1418. *
  1419. * Parses a mapped attribute setting from a template and
  1420. * fills in the type and value based off of the origin
  1421. * entry. If an origin entry is not supplied, the value
  1422. * is simply the mapping rule.
  1423. */
  1424. static int
  1425. mep_parse_mapped_attr(char *mapping, Slapi_Entry *origin,
  1426. char **type, char **value)
  1427. {
  1428. int ret = 0;
  1429. char *p = NULL;
  1430. char *pre_str = NULL;
  1431. char *post_str = NULL;
  1432. char *end = NULL;
  1433. char *var_start = NULL;
  1434. char *map_type = NULL;
  1435. /* Clear out any existing type or value. */
  1436. slapi_ch_free_string(type);
  1437. slapi_ch_free_string(value);
  1438. /* split out the type from the value (use the first ':') */
  1439. if ((p = strchr(mapping, ':')) == NULL) {
  1440. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1441. "mep_parse_mapped_attr: Value for mapped attribute "
  1442. "is not in the correct format. (value: \"%s\").\n",
  1443. mapping);
  1444. ret = 1;
  1445. goto bail;
  1446. }
  1447. /* Ensure the type is not empty. */
  1448. if (p == mapping) {
  1449. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1450. "mep_parse_mapped_attr: Value for mapped attribute "
  1451. "is not in the correct format. The type is missing. "
  1452. "(value: \"%s\").\n",
  1453. mapping);
  1454. ret = 1;
  1455. goto bail;
  1456. }
  1457. /* Terminate the type so we can use it as a string. */
  1458. *p = '\0';
  1459. /* Duplicate the type to be returned. */
  1460. *type = slapi_ch_strdup(mapping);
  1461. /* Advance p to point to the beginning of the value. */
  1462. p++;
  1463. while (*p == ' ') {
  1464. p++;
  1465. }
  1466. pre_str = p;
  1467. /* Make end point to the last character that we want in the value. */
  1468. end = p + strlen(p) - 1;
  1469. /* Find the variable that we need to substitute. */
  1470. for (; p <= end; p++) {
  1471. if (*p == '$') {
  1472. if (p == end) {
  1473. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1474. "mep_parse_mapped_attr: Invalid mapped "
  1475. "attribute value for type \"%s\".\n", mapping);
  1476. ret = 1;
  1477. goto bail;
  1478. }
  1479. if (*(p + 1) == '$') {
  1480. /* This is an escaped $. Eliminate the escape character
  1481. * to prevent if from being a part of the value. */
  1482. p++;
  1483. memmove(p, p+1, end-(p+1)+1);
  1484. *end = '\0';
  1485. end--;
  1486. } else {
  1487. int quoted = 0;
  1488. /* We found a variable. Terminate the pre
  1489. * string and process the variable. */
  1490. *p = '\0';
  1491. p++;
  1492. /* Check if the variable name is quoted. If it is, we skip past
  1493. * the quoting brace to avoid putting it in the mapped value. */
  1494. if (*p == '{') {
  1495. quoted = 1;
  1496. if (p < end) {
  1497. p++;
  1498. } else {
  1499. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1500. "mep_parse_mapped_attr: Invalid mapped "
  1501. "attribute value for type \"%s\".\n", mapping);
  1502. ret = 1;
  1503. goto bail;
  1504. }
  1505. }
  1506. /* We should be pointing at the variable name now. */
  1507. var_start = p;
  1508. /* Move the pointer to the end of the variable name. We
  1509. * stop at the first character that is not legal for use
  1510. * in an attribute description. */
  1511. while ((p < end) && IS_ATTRDESC_CHAR(*p)) {
  1512. p++;
  1513. }
  1514. /* If the variable is quoted and this is not a closing
  1515. * brace, there is a syntax error in the mapping rule. */
  1516. if (quoted && (*p != '}')) {
  1517. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1518. "mep_parse_mapped_attr: Invalid mapped "
  1519. "attribute value for type \"%s\".\n", mapping);
  1520. ret = 1;
  1521. goto bail;
  1522. }
  1523. /* Check for a missing variable name. */
  1524. if (p == var_start) {
  1525. break;
  1526. }
  1527. if (p == end) {
  1528. /* Set the map type. In this case, p could be
  1529. * pointing at either the last character of the
  1530. * map type, or at the first character after the
  1531. * map type. If the character is valid for use
  1532. * in an attribute description, we consider it
  1533. * to be a part of the map type. */
  1534. if (IS_ATTRDESC_CHAR(*p)) {
  1535. map_type = strndup(var_start, p - var_start + 1);
  1536. /* There is no post string. */
  1537. post_str = NULL;
  1538. } else {
  1539. map_type = strndup(var_start, p - var_start);
  1540. post_str = p;
  1541. }
  1542. } else {
  1543. /* Set the map type. In this case, p is pointing
  1544. * at the first character after the map type. */
  1545. map_type = strndup(var_start, p - var_start);
  1546. /* If the variable is quoted, don't include
  1547. * the closing brace in the post string. */
  1548. if (quoted) {
  1549. post_str = p+1;
  1550. } else {
  1551. post_str = p;
  1552. }
  1553. }
  1554. /* Process the post string to remove any escapes. */
  1555. for (p = post_str; p && (p <= end); p++) {
  1556. if (*p == '$') {
  1557. if ((p == end) || (*(p+1) != '$')) {
  1558. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1559. "mep_parse_mapped_attr: Invalid mapped "
  1560. "attribute value for type \"%s\".\n", mapping);
  1561. ret = 1;
  1562. goto bail;
  1563. } else {
  1564. /* This is an escaped '$'. Remove the escape char. */
  1565. p++;
  1566. memmove(p, p+1, end-(p+1)+1);
  1567. *end = '\0';
  1568. end--;
  1569. }
  1570. }
  1571. }
  1572. /* We only support a single variable, so we're done. */
  1573. break;
  1574. }
  1575. }
  1576. }
  1577. if (map_type) {
  1578. if (origin) {
  1579. char *map_val = NULL;
  1580. int freeit = 0;
  1581. /* If the map type is dn, fetch the origin dn. */
  1582. if (slapi_attr_type_cmp(map_type, "dn", SLAPI_TYPE_CMP_EXACT) == 0) {
  1583. map_val = slapi_entry_get_ndn(origin);
  1584. } else {
  1585. map_val = slapi_entry_attr_get_charptr(origin, map_type);
  1586. freeit = 1;
  1587. }
  1588. if (map_val) {
  1589. /* Create the new mapped value. */
  1590. *value = slapi_ch_smprintf("%s%s%s", pre_str,
  1591. map_val, post_str ? post_str : "");
  1592. if (freeit) {
  1593. slapi_ch_free_string(&map_val);
  1594. }
  1595. } else {
  1596. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1597. "mep_parse_mapped_attr: Mapped attribute \"%s\" "
  1598. "is not present in origin entry \"%s\". Please "
  1599. "correct template to only map attributes "
  1600. "required by the schema.\n", map_type,
  1601. slapi_entry_get_dn(origin));
  1602. ret = 1;
  1603. goto bail;
  1604. }
  1605. } else {
  1606. /* Just use the mapping since we have no origin entry. */
  1607. *value = slapi_ch_smprintf("%s$%s%s", pre_str, map_type,
  1608. post_str);
  1609. }
  1610. } else {
  1611. slapi_log_error( SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1612. "mep_parse_mapped_attr: No variable found in "
  1613. "mapped attribute value for type \"%s\".\n",
  1614. mapping);
  1615. ret = 1;
  1616. goto bail;
  1617. }
  1618. bail:
  1619. slapi_ch_free_string(&map_type);
  1620. if (ret != 0) {
  1621. slapi_ch_free_string(type);
  1622. slapi_ch_free_string(value);
  1623. }
  1624. return ret;
  1625. }
  1626. /*
  1627. * mep_is_managed_entry()
  1628. *
  1629. * Returns 1 if the entry is a managed entry, 0 otherwise.
  1630. * The check is performed by seeing if the managed entry
  1631. * objectclass is present.
  1632. */
  1633. static int
  1634. mep_is_managed_entry(Slapi_Entry *e)
  1635. {
  1636. int ret = 0;
  1637. Slapi_Attr *attr = NULL;
  1638. struct berval bv;
  1639. bv.bv_val = MEP_MANAGED_OC;
  1640. bv.bv_len = strlen(bv.bv_val);
  1641. if (e && (slapi_entry_attr_find(e, SLAPI_ATTR_OBJECTCLASS, &attr) == 0)) {
  1642. if (slapi_attr_value_find(attr, &bv) == 0) {
  1643. ret = 1;
  1644. }
  1645. }
  1646. return ret;
  1647. }
  1648. /*
  1649. * mep_is_mapped_attr()
  1650. *
  1651. * Checks if type is defined as a mapped attribute in template.
  1652. */
  1653. static int
  1654. mep_is_mapped_attr(Slapi_Entry *template, char *type)
  1655. {
  1656. int ret = 0;
  1657. int i = 0;
  1658. char **vals = NULL;
  1659. char *map_type = NULL;
  1660. char *value = NULL;
  1661. if (template && type) {
  1662. vals = slapi_entry_attr_get_charray(template, MEP_MAPPED_ATTR_TYPE);
  1663. for (i = 0; vals && vals[i]; ++i) {
  1664. if (mep_parse_mapped_attr(vals[i], NULL, &map_type, &value) == 0) {
  1665. if (slapi_attr_type_cmp(map_type, type, SLAPI_TYPE_CMP_EXACT) == 0) {
  1666. ret = 1;
  1667. }
  1668. slapi_ch_free_string(&map_type);
  1669. slapi_ch_free_string(&value);
  1670. /* If we found a match, we're done. */
  1671. if (ret == 1) {
  1672. break;
  1673. }
  1674. }
  1675. }
  1676. slapi_ch_array_free(vals);
  1677. }
  1678. return ret;
  1679. }
  1680. /*
  1681. * Operation callback functions
  1682. */
  1683. /*
  1684. * mep_pre_op()
  1685. *
  1686. * Checks if an operation is modifying the managed
  1687. * entries config and validates the config changes.
  1688. */
  1689. static int
  1690. mep_pre_op(Slapi_PBlock * pb, int modop)
  1691. {
  1692. Slapi_DN *sdn = NULL;
  1693. Slapi_Entry *e = 0;
  1694. Slapi_Mods *smods = 0;
  1695. LDAPMod **mods;
  1696. int free_entry = 0;
  1697. char *errstr = NULL;
  1698. struct configEntry *config = NULL;
  1699. void *caller_id = NULL;
  1700. int ret = SLAPI_PLUGIN_SUCCESS;
  1701. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1702. "--> mep_pre_op\n");
  1703. /* See if we're calling ourselves. */
  1704. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  1705. if (0 == (sdn = mep_get_sdn(pb)))
  1706. goto bail;
  1707. if (mep_dn_is_config(sdn)) {
  1708. /* Validate config changes, but don't apply them.
  1709. * This allows us to reject invalid config changes
  1710. * here at the pre-op stage. Applying the config
  1711. * needs to be done at the post-op stage. */
  1712. if (LDAP_CHANGETYPE_ADD == modop) {
  1713. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1714. } else if (LDAP_CHANGETYPE_MODIFY == modop) {
  1715. /* Fetch the entry being modified so we can
  1716. * create the resulting entry for validation. */
  1717. if (sdn) {
  1718. slapi_search_internal_get_entry(sdn, 0, &e, mep_get_plugin_id());
  1719. free_entry = 1;
  1720. }
  1721. /* If the entry doesn't exist, just bail and
  1722. * let the server handle it. */
  1723. if (e == NULL) {
  1724. goto bail;
  1725. }
  1726. /* Grab the mods. */
  1727. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1728. smods = slapi_mods_new();
  1729. slapi_mods_init_byref(smods, mods);
  1730. /* Apply the mods to create the resulting entry. */
  1731. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1732. /* The mods don't apply cleanly, so we just let this op go
  1733. * to let the main server handle it. */
  1734. goto bailmod;
  1735. }
  1736. } else if (LDAP_CHANGETYPE_DELETE == modop){
  1737. /* we allow for deletes, so goto bail to we can skip the config parsing */
  1738. goto bail;
  1739. } else {
  1740. /* Refuse other operations. */
  1741. ret = LDAP_UNWILLING_TO_PERFORM;
  1742. errstr = slapi_ch_smprintf("Not a valid operation.");
  1743. goto bail;
  1744. }
  1745. if (mep_parse_config_entry(e, 0) != 0) {
  1746. /* Refuse the operation if config parsing failed. */
  1747. ret = LDAP_UNWILLING_TO_PERFORM;
  1748. if (LDAP_CHANGETYPE_ADD == modop) {
  1749. errstr = slapi_ch_smprintf("Not a valid managed entries configuration entry.");
  1750. } else {
  1751. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1752. "managed entries configuration.");
  1753. }
  1754. }
  1755. } else {
  1756. /* Check if an active template entry is being updated. If so, validate it. */
  1757. mep_config_read_lock();
  1758. /* Bail out if the plug-in close function was just called. */
  1759. if (!slapi_plugin_running(pb)) {
  1760. mep_config_unlock();
  1761. goto bail;
  1762. }
  1763. mep_find_config_by_template_dn(sdn, &config);
  1764. if (config) {
  1765. Slapi_Entry *test_entry = NULL;
  1766. struct configEntry *config_copy = NULL;
  1767. config_copy = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  1768. /* Make a temporary copy of the config to use for validation. */
  1769. config_copy->sdn = slapi_sdn_dup(config->sdn);
  1770. config_copy->managed_base = slapi_ch_strdup(config->managed_base);
  1771. config_copy->template_sdn = slapi_sdn_dup(config->template_sdn);
  1772. /* Reject attempts to delete or rename an active template.
  1773. * Validate changes to an active template. */
  1774. switch (modop) {
  1775. case LDAP_CHANGETYPE_DELETE:
  1776. errstr = slapi_ch_smprintf("Deleting an active managed "
  1777. "entries template is not allowed. "
  1778. "Delete the associated config "
  1779. "entry first.");
  1780. ret = LDAP_UNWILLING_TO_PERFORM;
  1781. break;
  1782. case LDAP_CHANGETYPE_MODDN:
  1783. errstr = slapi_ch_smprintf("Renaming an active managed "
  1784. "entries template is not allowed. "
  1785. "Create a new template and modify "
  1786. "the associated config entry instead.");
  1787. ret = LDAP_UNWILLING_TO_PERFORM;
  1788. break;
  1789. case LDAP_CHANGETYPE_MODIFY:
  1790. /* Fetch the existing template entry. */
  1791. if (sdn) {
  1792. slapi_search_internal_get_entry(sdn, 0, &e, mep_get_plugin_id());
  1793. free_entry = 1;
  1794. }
  1795. /* If the entry doesn't exist, we just skip
  1796. * validation and let the server handle it. */
  1797. if (e) {
  1798. /* Grab the mods. */
  1799. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1800. smods = slapi_mods_new();
  1801. slapi_mods_init_byref(smods, mods);
  1802. /* Apply the mods to create the resulting entry. */
  1803. if (mods && (slapi_entry_apply_mods(e, mods) == LDAP_SUCCESS)) {
  1804. /* Set the resulting template in the config copy.
  1805. * The ownership of the resulting entry is handed
  1806. * over to the config copy. */
  1807. config_copy->template_entry = e;
  1808. e = NULL;
  1809. /* Validate the changed template. */
  1810. test_entry = mep_create_managed_entry(config_copy, NULL);
  1811. if (test_entry == NULL) {
  1812. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1813. "managed entries template.");
  1814. ret = LDAP_UNWILLING_TO_PERFORM;
  1815. } else if (slapi_entry_schema_check(NULL, test_entry) != 0) {
  1816. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1817. "managed entries template due "
  1818. "to a schema violation.");
  1819. ret = LDAP_UNWILLING_TO_PERFORM;
  1820. }
  1821. }
  1822. }
  1823. /* Dispose of the test entry */
  1824. slapi_entry_free(test_entry);
  1825. break;
  1826. }
  1827. /* Free the config copy */
  1828. mep_free_config_entry(&config_copy);
  1829. }
  1830. mep_config_unlock();
  1831. /* If replication, just bail. */
  1832. if (mep_isrepl(pb)) {
  1833. goto bailmod;
  1834. }
  1835. /* Check if a managed entry is being deleted or
  1836. * renamed and reject if it's not being done by
  1837. * this plugin. */
  1838. if (((modop == LDAP_CHANGETYPE_DELETE) || (modop == LDAP_CHANGETYPE_MODDN) ||
  1839. (modop == LDAP_CHANGETYPE_MODIFY)) && (caller_id != mep_get_plugin_id())) {
  1840. Slapi_Entry *origin_e = NULL;
  1841. Slapi_Mod *smod = NULL;
  1842. Slapi_Mod *next_mod = NULL;
  1843. char *origin_dn = NULL;
  1844. Slapi_DN *origin_sdn = NULL;
  1845. /* Fetch the target entry. */
  1846. if (sdn) {
  1847. /* Free any existing entry so we don't leak it. */
  1848. if (e && free_entry) {
  1849. slapi_entry_free(e);
  1850. }
  1851. slapi_search_internal_get_entry(sdn, 0, &e, mep_get_plugin_id());
  1852. free_entry = 1;
  1853. }
  1854. if (e && mep_is_managed_entry(e)) {
  1855. if (modop == LDAP_CHANGETYPE_MODIFY) {
  1856. /* Fetch the origin entry so we can locate the config template. */
  1857. origin_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_BY_TYPE);
  1858. if (origin_dn) {
  1859. origin_sdn = slapi_sdn_new_normdn_byref(origin_dn);
  1860. slapi_search_internal_get_entry(origin_sdn, 0,
  1861. &origin_e, mep_get_plugin_id());
  1862. slapi_sdn_free(&origin_sdn);
  1863. }
  1864. if (origin_e) {
  1865. /* Fetch the config. */
  1866. mep_config_read_lock();
  1867. /* Bail out if the plug-in close function was just called. */
  1868. if (!slapi_plugin_running(pb)) {
  1869. mep_config_unlock();
  1870. goto bail;
  1871. }
  1872. mep_find_config(origin_e, &config);
  1873. if (config) {
  1874. /* Get the mods if we haven't already. */
  1875. if (smods == NULL) {
  1876. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1877. smods = slapi_mods_new();
  1878. slapi_mods_init_byref(smods, mods);
  1879. }
  1880. next_mod = slapi_mod_new();
  1881. smod = slapi_mods_get_first_smod(smods, next_mod);
  1882. while(smod) {
  1883. char *type = (char *)slapi_mod_get_type(smod);
  1884. /* If this is a mapped attribute, reject the op. */
  1885. if (mep_is_mapped_attr(config->template_entry, type)) {
  1886. errstr = slapi_ch_smprintf("Modifying a mapped attribute "
  1887. " in a managed entry is not allowed. "
  1888. "The \"%s\" attribute is mapped for "
  1889. "this entry.", type);
  1890. ret = LDAP_UNWILLING_TO_PERFORM;
  1891. break;
  1892. }
  1893. slapi_mod_done(next_mod);
  1894. smod = slapi_mods_get_next_smod(smods, next_mod);
  1895. }
  1896. slapi_mod_free(&next_mod);
  1897. } else {
  1898. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1899. "mep_pre_op: Unable to fetch config for "
  1900. "origin entry \"%s\".\n", origin_dn);
  1901. }
  1902. slapi_entry_free(origin_e);
  1903. mep_config_unlock();
  1904. } else {
  1905. Slapi_Operation *op;
  1906. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  1907. if(operation_is_flag_set(op, OP_FLAG_INTERNAL)){
  1908. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1909. "mep_pre_op: (internal operation) Unable to fetch "
  1910. "origin entry \"%s\".\n", origin_dn);
  1911. } else {
  1912. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1913. "mep_pre_op: Unable to fetch origin entry "
  1914. "\"%s\".\n", origin_dn);
  1915. }
  1916. }
  1917. slapi_ch_free_string(&origin_dn);
  1918. } else {
  1919. errstr = slapi_ch_smprintf("%s a managed entry is not allowed. "
  1920. "It needs to be manually unlinked first.",
  1921. modop == LDAP_CHANGETYPE_DELETE ? "Deleting"
  1922. : "Renaming");
  1923. ret = LDAP_UNWILLING_TO_PERFORM;
  1924. }
  1925. }
  1926. }
  1927. }
  1928. bailmod:
  1929. /* Clean up smods. */
  1930. if (LDAP_CHANGETYPE_MODIFY == modop) {
  1931. slapi_mods_free(&smods);
  1932. }
  1933. bail:
  1934. if (free_entry && e)
  1935. slapi_entry_free(e);
  1936. if (ret) {
  1937. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  1938. "mep_pre_op: operation failure [%d]\n", ret);
  1939. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1940. slapi_ch_free((void **)&errstr);
  1941. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1942. ret = SLAPI_PLUGIN_FAILURE;
  1943. }
  1944. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1945. "<-- mep_pre_op\n");
  1946. return ret;
  1947. }
  1948. static int
  1949. mep_add_pre_op(Slapi_PBlock * pb)
  1950. {
  1951. return mep_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1952. }
  1953. static int
  1954. mep_del_pre_op(Slapi_PBlock * pb)
  1955. {
  1956. return mep_pre_op(pb, LDAP_CHANGETYPE_DELETE);
  1957. }
  1958. static int
  1959. mep_mod_pre_op(Slapi_PBlock * pb)
  1960. {
  1961. return mep_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1962. }
  1963. static int
  1964. mep_modrdn_pre_op(Slapi_PBlock * pb)
  1965. {
  1966. return mep_pre_op(pb, LDAP_CHANGETYPE_MODDN);
  1967. }
  1968. static int
  1969. mep_mod_post_op(Slapi_PBlock *pb)
  1970. {
  1971. Slapi_Mods *smods = NULL;
  1972. Slapi_PBlock *mep_pb = NULL;
  1973. Slapi_Entry *e = NULL;
  1974. Slapi_DN *sdn = NULL;
  1975. char *managed_dn = NULL;
  1976. Slapi_DN *managed_sdn = NULL;
  1977. char *mapped_dn = NULL;
  1978. Slapi_DN *mapped_sdn = NULL;
  1979. struct configEntry *config = NULL;
  1980. int result = SLAPI_PLUGIN_SUCCESS;
  1981. LDAPMod **mods = NULL;
  1982. int i, abort_mod = 1;
  1983. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  1984. "--> mep_mod_post_op\n");
  1985. if (mep_oktodo(pb) && (sdn = mep_get_sdn(pb))) {
  1986. /* First check if the config or a template is being modified. */
  1987. if (mep_dn_is_config(sdn) || mep_dn_is_template(sdn)) {
  1988. mep_load_config();
  1989. }
  1990. /* If replication, just bail. */
  1991. if (mep_isrepl(pb)) {
  1992. goto bail;
  1993. }
  1994. /* Fetch the modified entry. This will not be set for a chaining
  1995. * backend, so don't treat the message as fatal. */
  1996. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1997. if (e == NULL) {
  1998. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  1999. "mep_mod_post_op: Unable to fetch postop entry.\n");
  2000. result = SLAPI_PLUGIN_FAILURE;
  2001. goto bail;
  2002. }
  2003. /* If the entry is a tombstone, just bail. */
  2004. if (mep_has_tombstone_value(e)) {
  2005. goto bail;
  2006. }
  2007. /* Check if we're an origin entry. Update the
  2008. * mapped attrs of it's managed entry if so. */
  2009. managed_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_ENTRY_TYPE);
  2010. if (managed_dn) {
  2011. mep_config_read_lock();
  2012. /* Bail out if the plug-in close function was just called. */
  2013. if (!slapi_plugin_running(pb)) {
  2014. mep_config_unlock();
  2015. goto bail;
  2016. }
  2017. mep_find_config(e, &config);
  2018. if (config) {
  2019. /*
  2020. * Check to see if the applied mods are mapped origin attributes.
  2021. * If they are not, then we don't need to modify the mapped entry
  2022. * as it has not changed.
  2023. */
  2024. slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
  2025. for(i = 0; mods && mods[i]; i++){
  2026. if(mep_is_mapped_origin_attr(config->origin_attrs,mods[i]->mod_type)){
  2027. /*
  2028. * We are modifying a managed origin attr, so we can proceed with
  2029. * modifying the managed entry. Otherwise we would modify the
  2030. * managed entry for no reason.
  2031. */
  2032. abort_mod = 0;
  2033. break;
  2034. }
  2035. }
  2036. if(abort_mod){
  2037. mep_config_unlock();
  2038. goto bail;
  2039. }
  2040. smods = mep_get_mapped_mods(config, e, &mapped_dn);
  2041. if (smods) {
  2042. /* Clear out the pblock for reuse. */
  2043. mep_pb = slapi_pblock_new();
  2044. /* Perform the modify operation. */
  2045. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2046. "mep_mod_post_op: Updating mapped attributes "
  2047. "in entry \"%s\"\n.", managed_dn);
  2048. slapi_modify_internal_set_pb(mep_pb, managed_dn,
  2049. slapi_mods_get_ldapmods_byref(smods), 0, 0,
  2050. mep_get_plugin_id(), 0);
  2051. slapi_modify_internal_pb(mep_pb);
  2052. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2053. if (result != LDAP_SUCCESS) {
  2054. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2055. "mep_mod_post_op: Unable to update mapped "
  2056. "attributes from origin entry \"%s\" in managed "
  2057. "entry \"%s\" (%s).\n", slapi_sdn_get_dn(sdn), managed_dn,
  2058. ldap_err2string(result));
  2059. }
  2060. slapi_mods_free(&smods);
  2061. slapi_pblock_destroy(mep_pb);
  2062. }
  2063. /* Check if we need to rename the managed entry. */
  2064. if (result == SLAPI_PLUGIN_SUCCESS && mapped_dn) {
  2065. mapped_sdn = slapi_sdn_new_normdn_passin(mapped_dn);
  2066. managed_sdn = slapi_sdn_new_normdn_byref(managed_dn);
  2067. if (slapi_sdn_compare(managed_sdn, mapped_sdn) != 0) {
  2068. result = mep_rename_managed_entry(e, mapped_sdn, managed_sdn);
  2069. }
  2070. slapi_sdn_free(&mapped_sdn);
  2071. slapi_sdn_free(&managed_sdn);
  2072. }
  2073. } else {
  2074. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2075. "mep_mod_post_op: Unable to find config for origin "
  2076. "entry \"%s\".\n", slapi_sdn_get_dn(sdn));
  2077. }
  2078. mep_config_unlock();
  2079. }
  2080. }
  2081. bail:
  2082. if(result){
  2083. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &result);
  2084. result = SLAPI_PLUGIN_FAILURE;
  2085. }
  2086. slapi_ch_free_string(&managed_dn);
  2087. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2088. "<-- mep_mod_post_op\n");
  2089. return result;
  2090. }
  2091. static int
  2092. mep_add_post_op(Slapi_PBlock *pb)
  2093. {
  2094. Slapi_Entry *e = NULL;
  2095. Slapi_DN *sdn = NULL;
  2096. struct configEntry *config = NULL;
  2097. int result = SLAPI_PLUGIN_SUCCESS;
  2098. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2099. "--> mep_add_post_op\n");
  2100. /* Reload config if a config entry was added. */
  2101. if ((sdn = mep_get_sdn(pb))) {
  2102. if (mep_dn_is_config(sdn)) {
  2103. mep_load_config();
  2104. }
  2105. } else {
  2106. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2107. "mep_add_post_op: Error "
  2108. "retrieving dn\n");
  2109. }
  2110. /* If replication, just bail. */
  2111. if (mep_isrepl(pb)) {
  2112. return SLAPI_PLUGIN_SUCCESS;
  2113. }
  2114. /* Get the newly added entry. */
  2115. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  2116. if (e) {
  2117. /* If the entry is a tombstone, just bail. */
  2118. if (mep_has_tombstone_value(e)) {
  2119. return SLAPI_PLUGIN_SUCCESS;
  2120. }
  2121. /* Check if a config entry applies
  2122. * to the entry being added. */
  2123. mep_config_read_lock();
  2124. /* Bail out if the plug-in close function was just called. */
  2125. if (!slapi_plugin_running(pb)) {
  2126. mep_config_unlock();
  2127. return SLAPI_PLUGIN_SUCCESS;
  2128. }
  2129. mep_find_config(e, &config);
  2130. if (config) {
  2131. if(mep_add_managed_entry(config, e)){
  2132. char errtxt[SLAPI_DSE_RETURNTEXT_SIZE];
  2133. int rc = LDAP_UNWILLING_TO_PERFORM;
  2134. PR_snprintf(errtxt, SLAPI_DSE_RETURNTEXT_SIZE,
  2135. "Managed Entry Plugin rejected add operation (see errors log).\n");
  2136. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, &errtxt);
  2137. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  2138. result = SLAPI_PLUGIN_FAILURE;
  2139. }
  2140. }
  2141. mep_config_unlock();
  2142. } else {
  2143. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2144. "mep_add_post_op: Error "
  2145. "retrieving post-op entry %s\n", slapi_sdn_get_dn(sdn));
  2146. }
  2147. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2148. "<-- mep_add_post_op\n");
  2149. return result;
  2150. }
  2151. static int
  2152. mep_del_post_op(Slapi_PBlock *pb)
  2153. {
  2154. Slapi_Entry *e = NULL;
  2155. Slapi_DN *sdn = NULL;
  2156. int result = SLAPI_PLUGIN_SUCCESS;
  2157. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2158. "--> mep_del_post_op\n");
  2159. /* Reload config if a config entry was deleted. */
  2160. if ((sdn = mep_get_sdn(pb))) {
  2161. if (mep_dn_is_config(sdn))
  2162. mep_load_config();
  2163. } else {
  2164. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2165. "mep_del_post_op: Error "
  2166. "retrieving dn\n");
  2167. }
  2168. /* If replication, just bail. */
  2169. if (mep_isrepl(pb)) {
  2170. return SLAPI_PLUGIN_SUCCESS;
  2171. }
  2172. /* Get deleted entry, then go through types to find config. */
  2173. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );
  2174. if (e) {
  2175. char *managed_dn = NULL;
  2176. /* If the entry is a tombstone, just bail. */
  2177. if (mep_has_tombstone_value(e)) {
  2178. return SLAPI_PLUGIN_SUCCESS;
  2179. }
  2180. /* See if we're an origin entry . */
  2181. managed_dn = slapi_entry_attr_get_charptr(e, MEP_MANAGED_ENTRY_TYPE);
  2182. if (managed_dn) {
  2183. Slapi_PBlock *mep_pb = slapi_pblock_new();
  2184. /* Delete the managed entry. */
  2185. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2186. "mep_del_post_op: Deleting managed entry "
  2187. "\"%s\" due to deletion of origin entry "
  2188. "\"%s\".\n ", managed_dn, slapi_sdn_get_dn(sdn));
  2189. slapi_delete_internal_set_pb(mep_pb, managed_dn, NULL,
  2190. NULL, mep_get_plugin_id(), 0);
  2191. slapi_delete_internal_pb(mep_pb);
  2192. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2193. if(result){
  2194. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2195. "mep_del_post_op: failed to delete managed entry "
  2196. "(%s) - error (%d)\n",managed_dn, result);
  2197. }
  2198. slapi_ch_free_string(&managed_dn);
  2199. slapi_pblock_destroy(mep_pb);
  2200. }
  2201. } else {
  2202. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2203. "mep_del_post_op: Error "
  2204. "retrieving pre-op entry %s\n", slapi_sdn_get_dn(sdn));
  2205. }
  2206. if(result){
  2207. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &result);
  2208. result = SLAPI_PLUGIN_FAILURE;
  2209. }
  2210. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2211. "<-- mep_del_post_op\n");
  2212. return result;
  2213. }
  2214. static int
  2215. mep_modrdn_post_op(Slapi_PBlock *pb)
  2216. {
  2217. const char *new_dn = NULL;
  2218. Slapi_DN *old_sdn = NULL;
  2219. Slapi_DN *new_sdn = NULL;
  2220. Slapi_Entry *post_e = NULL;
  2221. char *managed_dn = NULL;
  2222. struct configEntry *config = NULL;
  2223. int result = SLAPI_PLUGIN_SUCCESS;
  2224. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2225. "--> mep_modrdn_post_op\n");
  2226. /* Just bail if we aren't ready to service requests yet. */
  2227. if (!mep_oktodo(pb)){
  2228. return SLAPI_PLUGIN_SUCCESS;
  2229. }
  2230. /* Reload config if an existing config entry was renamed,
  2231. * or if the new dn brings an entry into the scope of the
  2232. * config entries. */
  2233. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  2234. if (post_e) {
  2235. new_sdn = slapi_entry_get_sdn(post_e);
  2236. new_dn = slapi_sdn_get_dn(new_sdn);
  2237. } else {
  2238. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2239. "mep_modrdn_post_op: Error "
  2240. "retrieving post-op entry\n");
  2241. return SLAPI_PLUGIN_FAILURE;
  2242. }
  2243. if ((old_sdn = mep_get_sdn(pb))) {
  2244. if (mep_dn_is_config(old_sdn) || mep_dn_is_config(new_sdn))
  2245. mep_load_config();
  2246. } else {
  2247. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2248. "mep_modrdn_post_op: Error "
  2249. "retrieving dn\n");
  2250. }
  2251. /* If replication, just bail. */
  2252. if (mep_isrepl(pb)) {
  2253. return SLAPI_PLUGIN_SUCCESS;
  2254. }
  2255. /* If the entry is a tombstone, just bail. */
  2256. if (mep_has_tombstone_value(post_e)) {
  2257. return SLAPI_PLUGIN_SUCCESS;
  2258. }
  2259. /* See if we're an origin entry . */
  2260. managed_dn = slapi_entry_attr_get_charptr(post_e, MEP_MANAGED_ENTRY_TYPE);
  2261. if (managed_dn) {
  2262. LDAPMod mod;
  2263. LDAPMod *mods[3];
  2264. char *vals[2];
  2265. int result = LDAP_SUCCESS;
  2266. Slapi_PBlock *mep_pb = slapi_pblock_new();
  2267. Slapi_Entry *new_managed_entry = NULL;
  2268. Slapi_DN *managed_sdn = NULL;
  2269. Slapi_Mods *smods = NULL;
  2270. int free_managed_dn = 1;
  2271. mep_config_read_lock();
  2272. /* Bail out if the plug-in close function was just called. */
  2273. if (!slapi_plugin_running(pb)) {
  2274. mep_config_unlock();
  2275. slapi_ch_free_string(&managed_dn);
  2276. slapi_pblock_destroy(mep_pb);
  2277. return SLAPI_PLUGIN_SUCCESS;
  2278. }
  2279. mep_find_config(post_e, &config);
  2280. if (!config) {
  2281. LDAPMod mod2;
  2282. char *vals2[2];
  2283. /* Delete the associated managed entry. */
  2284. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2285. "mep_modrdn_post_op: Removing managed entry \"%s\" "
  2286. "since origin entry \"%s\" was moved out of scope.\n",
  2287. managed_dn, slapi_sdn_get_dn(old_sdn));
  2288. slapi_delete_internal_set_pb (mep_pb, managed_dn, NULL, NULL,
  2289. mep_get_plugin_id(), 0);
  2290. slapi_delete_internal_pb(mep_pb);
  2291. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2292. if(result){
  2293. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2294. "mep_modrdn_post_op: failed to delete managed entry "
  2295. "(%s) - error (%d)\n",managed_dn, result);
  2296. goto bailmod;
  2297. }
  2298. /* Clear out the pblock for reuse. */
  2299. slapi_pblock_init(mep_pb);
  2300. /* Remove the pointer from the origin entry. */
  2301. vals[0] = 0;
  2302. mod.mod_op = LDAP_MOD_DELETE;
  2303. mod.mod_type = MEP_MANAGED_ENTRY_TYPE;
  2304. mod.mod_values = vals;
  2305. /* Remove the origin objectclass. */
  2306. vals2[0] = MEP_ORIGIN_OC;
  2307. vals2[1] = 0;
  2308. mod2.mod_op = LDAP_MOD_DELETE;
  2309. mod2.mod_type = SLAPI_ATTR_OBJECTCLASS;
  2310. mod2.mod_values = vals2;
  2311. mods[0] = &mod;
  2312. mods[1] = &mod2;
  2313. mods[2] = 0;
  2314. /* Perform the modify operation. */
  2315. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2316. "mep_modrdn_post_op: Removing %s pointer and %s "
  2317. "objectclass from entry \"%s\".\n",
  2318. MEP_MANAGED_ENTRY_TYPE, MEP_ORIGIN_OC, new_dn);
  2319. slapi_modify_internal_set_pb_ext(mep_pb, new_sdn, mods, 0, 0,
  2320. mep_get_plugin_id(), 0);
  2321. slapi_modify_internal_pb(mep_pb);
  2322. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2323. if (result != LDAP_SUCCESS) {
  2324. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2325. "mep_modrdn_post_op: Unable to remove %s "
  2326. "pointer and %s objectclass from entry "
  2327. "\"%s\".\n", MEP_MANAGED_ENTRY_TYPE,
  2328. MEP_ORIGIN_OC, new_dn);
  2329. }
  2330. } else {
  2331. /* Update backlink to new origin DN in managed entry. */
  2332. vals[0] = (char *)new_dn;
  2333. vals[1] = 0;
  2334. mod.mod_op = LDAP_MOD_REPLACE;
  2335. mod.mod_type = MEP_MANAGED_BY_TYPE;
  2336. mod.mod_values = vals;
  2337. mods[0] = &mod;
  2338. mods[1] = 0;
  2339. /* Create a new managed entry to determine what changes
  2340. * we need to make to the existing managed entry. */
  2341. new_managed_entry = mep_create_managed_entry(config, post_e);
  2342. if (new_managed_entry == NULL) {
  2343. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2344. "mep_modrdn_post_op: Unable to create in-memory "
  2345. "managed entry from origin entry \"%s\".\n", new_dn);
  2346. result = SLAPI_PLUGIN_FAILURE;
  2347. goto bailmod;
  2348. }
  2349. /* Check if the managed entry exists. It is possible that
  2350. * it has already been renamed by another plug-in. If it
  2351. * has already been renamed, we need to use the new DN to
  2352. * perform our updates. */
  2353. managed_sdn = slapi_sdn_new_normdn_byref(managed_dn);
  2354. if (slapi_search_internal_get_entry(managed_sdn, 0,
  2355. NULL, mep_get_plugin_id()) == LDAP_NO_SUCH_OBJECT) {
  2356. slapi_ch_free_string(&managed_dn);
  2357. /* This DN is not a copy, so we don't want to free it later. */
  2358. managed_dn = slapi_entry_get_dn(new_managed_entry);
  2359. slapi_sdn_set_normdn_byref(managed_sdn, managed_dn);
  2360. free_managed_dn = 0;
  2361. }
  2362. /* Perform the modify operation. */
  2363. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2364. "mep_modrdn_post_op: Updating %s pointer to \"%s\" "
  2365. "in entry \"%s\".\n", MEP_MANAGED_BY_TYPE, new_dn, managed_dn);
  2366. slapi_modify_internal_set_pb(mep_pb, managed_dn, mods, 0, 0,
  2367. mep_get_plugin_id(), 0);
  2368. slapi_modify_internal_pb(mep_pb);
  2369. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2370. if (result != LDAP_SUCCESS) {
  2371. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2372. "mep_modrdn_post_op: Unable to update pointer to "
  2373. "origin entry \"%s\" in managed entry \"%s\" "
  2374. "(%s).\n", new_dn, managed_dn, ldap_err2string(result));
  2375. } else {
  2376. /* See if we need to rename the managed entry. */
  2377. if (slapi_sdn_compare(slapi_entry_get_sdn(new_managed_entry), managed_sdn) != 0) {
  2378. /* Rename the managed entry. */
  2379. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2380. "mep_modrdn_post_op: Renaming managed entry "
  2381. "\"%s\" to \"%s\" due to rename of origin "
  2382. "entry \"%s\".\n ", managed_dn,
  2383. slapi_entry_get_dn(new_managed_entry),
  2384. slapi_sdn_get_dn(old_sdn));
  2385. if((result = mep_rename_managed_entry(post_e,
  2386. slapi_entry_get_sdn(new_managed_entry),
  2387. managed_sdn)))
  2388. {
  2389. goto bailmod;
  2390. }
  2391. }
  2392. /* Update all of the mapped attributes
  2393. * to be sure they are up to date. */
  2394. smods = mep_get_mapped_mods(config, post_e, NULL);
  2395. if (smods) {
  2396. /* Clear out the pblock for reuse. */
  2397. slapi_pblock_init(mep_pb);
  2398. /* Perform the modify operation. */
  2399. slapi_log_error(SLAPI_LOG_PLUGIN, MEP_PLUGIN_SUBSYSTEM,
  2400. "mep_modrdn_post_op: Updating mapped attributes "
  2401. "in entry \"%s\"\n.", managed_dn);
  2402. slapi_modify_internal_set_pb_ext(mep_pb,
  2403. slapi_entry_get_sdn(new_managed_entry),
  2404. slapi_mods_get_ldapmods_byref(smods), 0, 0,
  2405. mep_get_plugin_id(), 0);
  2406. slapi_modify_internal_pb(mep_pb);
  2407. slapi_pblock_get(mep_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  2408. if (result != LDAP_SUCCESS) {
  2409. slapi_log_error(SLAPI_LOG_FATAL, MEP_PLUGIN_SUBSYSTEM,
  2410. "mep_modrdn_post_op: Unable to update mapped "
  2411. "attributes from origin entry \"%s\" in managed "
  2412. "entry \"%s\" (%s).\n", new_dn,
  2413. slapi_entry_get_dn(new_managed_entry),
  2414. ldap_err2string(result));
  2415. }
  2416. slapi_mods_free(&smods);
  2417. }
  2418. }
  2419. bailmod:
  2420. slapi_entry_free(new_managed_entry);
  2421. slapi_sdn_free(&managed_sdn);
  2422. }
  2423. slapi_pblock_destroy(mep_pb);
  2424. if (free_managed_dn) {
  2425. slapi_ch_free_string(&managed_dn);
  2426. }
  2427. mep_config_unlock();
  2428. } else {
  2429. /* Was this entry moved into scope of a config entry?
  2430. * If so, treat like an add and create the new managed
  2431. * entry and links. */
  2432. mep_config_read_lock();
  2433. /* Bail out if the plug-in close function was just called. */
  2434. if (!slapi_plugin_running(pb)) {
  2435. mep_config_unlock();
  2436. return result;
  2437. }
  2438. mep_find_config(post_e, &config);
  2439. if (config) {
  2440. if(mep_add_managed_entry(config, post_e)){
  2441. char errtxt[SLAPI_DSE_RETURNTEXT_SIZE];
  2442. result = LDAP_UNWILLING_TO_PERFORM;
  2443. PR_snprintf(errtxt, SLAPI_DSE_RETURNTEXT_SIZE,
  2444. "Managed Entry Plugin rejected modrdn operation (see errors log).\n");
  2445. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, &errtxt);
  2446. }
  2447. }
  2448. mep_config_unlock();
  2449. }
  2450. if(result){
  2451. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &result);
  2452. result = SLAPI_PLUGIN_FAILURE;
  2453. }
  2454. slapi_log_error(SLAPI_LOG_TRACE, MEP_PLUGIN_SUBSYSTEM,
  2455. "<-- mep_modrdn_post_op\n");
  2456. return result;
  2457. }
  2458. static int
  2459. mep_has_tombstone_value(Slapi_Entry * e)
  2460. {
  2461. Slapi_Value *tombstone = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
  2462. int rc = slapi_entry_attr_has_syntax_value(e, SLAPI_ATTR_OBJECTCLASS,
  2463. tombstone);
  2464. slapi_value_free(&tombstone);
  2465. return rc;
  2466. }