1
0

mep.c 76 KB

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