mep.c 81 KB

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