mep.c 98 KB

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