mep.c 100 KB

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