mep.c 100 KB

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