mep.c 98 KB

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