mep.c 98 KB

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