linked_attrs.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /*
  12. * Linked attributes plug-in
  13. */
  14. #include "linked_attrs.h"
  15. /*
  16. * Plug-in globals
  17. */
  18. static PRCList *g_link_config = NULL;
  19. static PRCList *g_managed_config_index = NULL;
  20. static Slapi_RWLock *g_config_lock;
  21. static void *_PluginID = NULL;
  22. static char *_PluginDN = NULL;
  23. int plugin_is_betxn = 0;
  24. static Slapi_PluginDesc pdesc = { LINK_FEATURE_DESC,
  25. VENDOR,
  26. DS_PACKAGE_VERSION,
  27. LINK_PLUGIN_DESC };
  28. /*
  29. * Plug-in management functions
  30. */
  31. int linked_attrs_init(Slapi_PBlock * pb);
  32. static int linked_attrs_start(Slapi_PBlock * pb);
  33. static int linked_attrs_close(Slapi_PBlock * pb);
  34. static int linked_attrs_postop_init(Slapi_PBlock * pb);
  35. static int linked_attrs_internal_postop_init(Slapi_PBlock *pb);
  36. /*
  37. * Operation callbacks (where the real work is done)
  38. */
  39. static int linked_attrs_mod_post_op(Slapi_PBlock *pb);
  40. static int linked_attrs_add_post_op(Slapi_PBlock *pb);
  41. static int linked_attrs_del_post_op(Slapi_PBlock *pb);
  42. static int linked_attrs_modrdn_post_op(Slapi_PBlock *pb);
  43. static int linked_attrs_pre_op(Slapi_PBlock *pb, int modop);
  44. static int linked_attrs_mod_pre_op(Slapi_PBlock *pb);
  45. static int linked_attrs_add_pre_op(Slapi_PBlock *pb);
  46. /*
  47. * Config cache management functions
  48. */
  49. static int linked_attrs_load_config();
  50. static void linked_attrs_delete_config();
  51. static int linked_attrs_parse_config_entry(Slapi_Entry * e, int apply);
  52. static void linked_attrs_insert_config_index(struct configEntry *entry);
  53. static void linked_attrs_free_config_entry(struct configEntry ** entry);
  54. /*
  55. * helpers
  56. */
  57. static char *linked_attrs_get_dn(Slapi_PBlock * pb);
  58. static Slapi_DN *linked_attrs_get_sdn(Slapi_PBlock * pb);
  59. static int linked_attrs_dn_is_config(char *dn);
  60. static void linked_attrs_find_config(const char *dn, const char *type,
  61. struct configEntry **config);
  62. static void linked_attrs_find_config_reverse(const char *dn,
  63. const char *type, struct configEntry **config);
  64. static int linked_attrs_config_index_has_type(char *type);
  65. static int linked_attrs_config_exists(struct configEntry *entry);
  66. static int linked_attrs_config_exists_reverse(struct configEntry *entry);
  67. static int linked_attrs_oktodo(Slapi_PBlock *pb);
  68. void linked_attrs_load_array(Slapi_Value **array, Slapi_Attr *attr);
  69. int linked_attrs_compare(const void *a, const void *b);
  70. static int linked_attrs_add_backpointers(Slapi_PBlock *pb, char *linkdn, struct configEntry *config,
  71. Slapi_Mod *smod);
  72. static int linked_attrs_del_backpointers(Slapi_PBlock *pb, char *linkdn,
  73. struct configEntry *config, Slapi_Mod *smod);
  74. static int linked_attrs_replace_backpointers(Slapi_PBlock *pb, char *linkdn,
  75. struct configEntry *config, Slapi_Mod *smod);
  76. static int linked_attrs_mod_backpointers(Slapi_PBlock *pb, char *linkdn, char *type,
  77. char *scope, int modop, Slapi_ValueSet *targetvals);
  78. /*
  79. * Config cache locking functions
  80. */
  81. void
  82. linked_attrs_read_lock()
  83. {
  84. slapi_rwlock_rdlock(g_config_lock);
  85. }
  86. void
  87. linked_attrs_write_lock()
  88. {
  89. slapi_rwlock_wrlock(g_config_lock);
  90. }
  91. void
  92. linked_attrs_unlock()
  93. {
  94. slapi_rwlock_unlock(g_config_lock);
  95. }
  96. /*
  97. * Plugin identity functions
  98. */
  99. void
  100. linked_attrs_set_plugin_id(void *pluginID)
  101. {
  102. _PluginID = pluginID;
  103. }
  104. void *
  105. linked_attrs_get_plugin_id()
  106. {
  107. return _PluginID;
  108. }
  109. void
  110. linked_attrs_set_plugin_dn(const char *pluginDN)
  111. {
  112. _PluginDN = (char *)pluginDN;
  113. }
  114. char *
  115. linked_attrs_get_plugin_dn()
  116. {
  117. return _PluginDN;
  118. }
  119. /*
  120. * Plug-in initialization functions
  121. */
  122. int
  123. linked_attrs_init(Slapi_PBlock *pb)
  124. {
  125. int status = 0;
  126. char *plugin_identity = NULL;
  127. Slapi_Entry *plugin_entry = NULL;
  128. char *plugin_type = NULL;
  129. int preadd = SLAPI_PLUGIN_PRE_ADD_FN;
  130. int premod = SLAPI_PLUGIN_PRE_MODIFY_FN;
  131. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  132. "--> linked_attrs_init\n");
  133. if ((slapi_pblock_get(pb, SLAPI_PLUGIN_CONFIG_ENTRY, &plugin_entry) == 0) &&
  134. plugin_entry &&
  135. (plugin_type = slapi_entry_attr_get_charptr(plugin_entry, "nsslapd-plugintype")) &&
  136. plugin_type && strstr(plugin_type, "betxn")) {
  137. plugin_is_betxn = 1;
  138. preadd = SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN;
  139. premod = SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN;
  140. }
  141. slapi_ch_free_string(&plugin_type);
  142. /* Store the plugin identity for later use.
  143. * Used for internal operations. */
  144. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_identity);
  145. PR_ASSERT(plugin_identity);
  146. linked_attrs_set_plugin_id(plugin_identity);
  147. /* Register callbacks */
  148. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  149. SLAPI_PLUGIN_VERSION_01) != 0 ||
  150. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  151. (void *) linked_attrs_start) != 0 ||
  152. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  153. (void *) linked_attrs_close) != 0 ||
  154. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  155. (void *) &pdesc) != 0 ||
  156. slapi_pblock_set(pb, premod, (void *) linked_attrs_mod_pre_op) != 0 ||
  157. slapi_pblock_set(pb, preadd, (void *) linked_attrs_add_pre_op) != 0) {
  158. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  159. "linked_attrs_init: failed to register plugin\n");
  160. status = -1;
  161. }
  162. if (!status && !plugin_is_betxn &&
  163. slapi_register_plugin("internalpostoperation", /* op type */
  164. 1, /* Enabled */
  165. "linked_attrs_init", /* this function desc */
  166. linked_attrs_internal_postop_init, /* init func */
  167. LINK_INT_POSTOP_DESC, /* plugin desc */
  168. NULL, /* ? */
  169. plugin_identity /* access control */
  170. )) {
  171. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  172. "linked_attrs_init: failed to register internalpostoperation plugin\n");
  173. status = -1;
  174. }
  175. if (!status) {
  176. plugin_type = "postoperation";
  177. if (plugin_is_betxn) {
  178. plugin_type = "betxnpostoperation";
  179. }
  180. if (slapi_register_plugin(plugin_type, /* op type */
  181. 1, /* Enabled */
  182. "linked_attrs_init", /* this function desc */
  183. linked_attrs_postop_init, /* init func for post op */
  184. LINK_POSTOP_DESC, /* plugin desc */
  185. NULL, /* ? */
  186. plugin_identity /* access control */
  187. )) {
  188. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  189. "linked_attrs_init: failed to register postop plugin\n");
  190. status = -1;
  191. }
  192. }
  193. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  194. "<-- linked_attrs_init\n");
  195. return status;
  196. }
  197. /* not used when using plugin as a betxn plugin - betxn plugins are called for both internal and external ops */
  198. static int
  199. linked_attrs_internal_postop_init(Slapi_PBlock *pb)
  200. {
  201. int status = 0;
  202. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  203. SLAPI_PLUGIN_VERSION_01) != 0 ||
  204. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  205. (void *) &pdesc) != 0 ||
  206. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
  207. (void *) linked_attrs_add_post_op) != 0 ||
  208. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
  209. (void *) linked_attrs_del_post_op) != 0 ||
  210. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
  211. (void *) linked_attrs_mod_post_op) != 0 ||
  212. slapi_pblock_set(pb, SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
  213. (void *) linked_attrs_modrdn_post_op) != 0) {
  214. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  215. "linked_attrs_internal_postop_init: failed to register plugin\n");
  216. status = -1;
  217. }
  218. return status;
  219. }
  220. static int
  221. linked_attrs_postop_init(Slapi_PBlock *pb)
  222. {
  223. int status = 0;
  224. int addfn = SLAPI_PLUGIN_POST_ADD_FN;
  225. int delfn = SLAPI_PLUGIN_POST_DELETE_FN;
  226. int modfn = SLAPI_PLUGIN_POST_MODIFY_FN;
  227. int mdnfn = SLAPI_PLUGIN_POST_MODRDN_FN;
  228. if (plugin_is_betxn) {
  229. addfn = SLAPI_PLUGIN_BE_TXN_POST_ADD_FN;
  230. delfn = SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN;
  231. modfn = SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN;
  232. mdnfn = SLAPI_PLUGIN_BE_TXN_POST_MODRDN_FN;
  233. }
  234. if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  235. SLAPI_PLUGIN_VERSION_01) != 0 ||
  236. slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION,
  237. (void *) &pdesc) != 0 ||
  238. slapi_pblock_set(pb, addfn, (void *) linked_attrs_add_post_op) != 0 ||
  239. slapi_pblock_set(pb, delfn, (void *) linked_attrs_del_post_op) != 0 ||
  240. slapi_pblock_set(pb, modfn, (void *) linked_attrs_mod_post_op) != 0 ||
  241. slapi_pblock_set(pb, mdnfn, (void *) linked_attrs_modrdn_post_op) != 0) {
  242. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  243. "linked_attrs_postop_init: failed to register plugin\n");
  244. status = -1;
  245. }
  246. return status;
  247. }
  248. /*
  249. * linked_attrs_start()
  250. *
  251. * Creates config lock and loads config cache.
  252. */
  253. static int
  254. linked_attrs_start(Slapi_PBlock * pb)
  255. {
  256. Slapi_DN *plugindn = NULL;
  257. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  258. "--> linked_attrs_start\n");
  259. g_config_lock = slapi_new_rwlock();
  260. if (!g_config_lock) {
  261. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  262. "linked_attrs_start: lock creation failed\n");
  263. return -1;
  264. }
  265. /*
  266. * Get the plug-in target dn from the system
  267. * and store it for future use. */
  268. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &plugindn);
  269. if (NULL == plugindn || 0 == slapi_sdn_get_ndn_len(plugindn)) {
  270. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  271. "linked_attrs_start: unable to retrieve plugin dn\n");
  272. return -1;
  273. }
  274. linked_attrs_set_plugin_dn(slapi_sdn_get_dn(plugindn));
  275. /*
  276. * Load the config cache
  277. */
  278. g_link_config = (PRCList *)slapi_ch_calloc(1, sizeof(struct configEntry));
  279. PR_INIT_CLIST(g_link_config);
  280. g_managed_config_index = (PRCList *)slapi_ch_calloc(1, sizeof(struct configIndex));
  281. PR_INIT_CLIST(g_managed_config_index);
  282. if (linked_attrs_load_config() != 0) {
  283. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  284. "linked_attrs_start: unable to load plug-in configuration\n");
  285. return -1;
  286. }
  287. /*
  288. * Register our task callback
  289. */
  290. slapi_plugin_task_register_handler("fixup linked attributes", linked_attrs_fixup_task_add, pb);
  291. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  292. "linked attributes plug-in: ready for service\n");
  293. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  294. "<-- linked_attrs_start\n");
  295. return 0;
  296. }
  297. /*
  298. * linked_attrs_close()
  299. *
  300. * Cleans up the config cache.
  301. */
  302. static int
  303. linked_attrs_close(Slapi_PBlock * pb)
  304. {
  305. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  306. "--> linked_attrs_close\n");
  307. slapi_plugin_task_unregister_handler("fixup linked attributes", linked_attrs_fixup_task_add);
  308. linked_attrs_delete_config();
  309. slapi_destroy_rwlock(g_config_lock);
  310. g_config_lock = NULL;
  311. slapi_ch_free((void **)&g_link_config);
  312. slapi_ch_free((void **)&g_managed_config_index);
  313. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  314. "<-- linked_attrs_close\n");
  315. return 0;
  316. }
  317. PRCList *
  318. linked_attrs_get_config()
  319. {
  320. return g_link_config;
  321. }
  322. /*
  323. * config looks like this
  324. * - cn=myplugin
  325. * --- cn=manager link
  326. * --- cn=owner link
  327. * --- cn=etc
  328. */
  329. static int
  330. linked_attrs_load_config()
  331. {
  332. int status = 0;
  333. int result;
  334. int i;
  335. Slapi_PBlock *search_pb;
  336. Slapi_Entry **entries = NULL;
  337. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  338. "--> linked_attrs_load_config\n");
  339. /* Clear out any old config. */
  340. linked_attrs_write_lock();
  341. linked_attrs_delete_config();
  342. /* Find the config entries beneath our plugin entry. */
  343. search_pb = slapi_pblock_new();
  344. slapi_search_internal_set_pb(search_pb, linked_attrs_get_plugin_dn(),
  345. LDAP_SCOPE_SUBTREE, "objectclass=*",
  346. NULL, 0, NULL, NULL, linked_attrs_get_plugin_id(), 0);
  347. slapi_search_internal_pb(search_pb);
  348. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
  349. if (LDAP_SUCCESS != result) {
  350. status = -1;
  351. goto cleanup;
  352. }
  353. slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
  354. &entries);
  355. if (NULL == entries || NULL == entries[0]) {
  356. /* If there are no config entries, we're done. */
  357. goto cleanup;
  358. }
  359. /* Loop through all of the entries we found and parse them. */
  360. for (i = 0; (entries[i] != NULL); i++) {
  361. /* We don't care about the status here because we may have
  362. * some invalid config entries, but we just want to continue
  363. * looking for valid ones. */
  364. linked_attrs_parse_config_entry(entries[i], 1);
  365. }
  366. cleanup:
  367. slapi_free_search_results_internal(search_pb);
  368. slapi_pblock_destroy(search_pb);
  369. linked_attrs_unlock();
  370. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  371. "<-- linked_attrs_load_config\n");
  372. return status;
  373. }
  374. /*
  375. * linked_attrs_parse_config_entry()
  376. *
  377. * Parses a single config entry. If apply is non-zero, then
  378. * we will load and start using the new config. You can simply
  379. * validate config without making any changes by setting apply
  380. * to 0.
  381. *
  382. * Returns 0 if the entry is valid and -1 if it is invalid.
  383. */
  384. static int
  385. linked_attrs_parse_config_entry(Slapi_Entry * e, int apply)
  386. {
  387. char *value;
  388. struct configEntry *entry = NULL;
  389. struct configEntry *config_entry;
  390. PRCList *list;
  391. int entry_added = 0;
  392. int ret = 0;
  393. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  394. "--> linked_attrs_parse_config_entry\n");
  395. /* If this is the main plug-in
  396. * config entry, just bail. */
  397. if (strcasecmp(linked_attrs_get_plugin_dn(), slapi_entry_get_ndn(e)) == 0) {
  398. ret = -1;
  399. goto bail;
  400. }
  401. entry = (struct configEntry *)slapi_ch_calloc(1, sizeof(struct configEntry));
  402. if (NULL == entry) {
  403. ret = -1;
  404. goto bail;
  405. }
  406. value = slapi_entry_get_ndn(e);
  407. if (value) {
  408. entry->dn = slapi_ch_strdup(value);
  409. } else {
  410. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  411. "linked_attrs_parse_config_entry: Error "
  412. "reading dn from config entry\n");
  413. ret = -1;
  414. goto bail;
  415. }
  416. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  417. "----------> dn [%s]\n", entry->dn);
  418. value = slapi_entry_attr_get_charptr(e, LINK_LINK_TYPE);
  419. if (value) {
  420. int not_dn_syntax = 0;
  421. char *syntaxoid = NULL;
  422. Slapi_Attr *attr = slapi_attr_new();
  423. /* Set this first so we free it if we encounter an error */
  424. entry->linktype = value;
  425. /* Gather some information about this attribute. */
  426. slapi_attr_init(attr, value);
  427. slapi_attr_get_syntax_oid_copy(attr, &syntaxoid );
  428. not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID);
  429. slapi_ch_free_string(&syntaxoid);
  430. slapi_attr_free(&attr);
  431. /* Check if the link type's syntax is Distinguished Name.
  432. * We only treat this as a warning. */
  433. if (not_dn_syntax) {
  434. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  435. "linked_attrs_parse_config_entry: The %s config "
  436. "setting must be set to an attribute with the "
  437. "Distinguished Name syntax for linked attribute "
  438. "pair \"%s\" attribute \"%s\".\n", LINK_LINK_TYPE, entry->dn, value);
  439. }
  440. } else {
  441. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  442. "linked_attrs_parse_config_entry: The %s config "
  443. "setting is required for linked attribute pair \"%s\".\n",
  444. LINK_LINK_TYPE, entry->dn);
  445. ret = -1;
  446. goto bail;
  447. }
  448. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  449. "----------> %s [%s]\n", LINK_LINK_TYPE, entry->linktype);
  450. value = slapi_entry_attr_get_charptr(e, LINK_MANAGED_TYPE);
  451. if (value) {
  452. int single_valued = 0;
  453. int not_dn_syntax = 0;
  454. char *syntaxoid = NULL;
  455. Slapi_Attr *attr = slapi_attr_new();
  456. /* Set this first so we free it if we encounter an error */
  457. entry->managedtype = value;
  458. /* Gather some information about this attribute. */
  459. slapi_attr_init(attr, value);
  460. slapi_attr_get_syntax_oid_copy(attr, &syntaxoid );
  461. not_dn_syntax = strcmp(syntaxoid, DN_SYNTAX_OID);
  462. single_valued = slapi_attr_flag_is_set(attr, SLAPI_ATTR_FLAG_SINGLE);
  463. slapi_ch_free_string(&syntaxoid);
  464. slapi_attr_free(&attr);
  465. /* Ensure that the managed type is a multi-valued attribute. */
  466. if (single_valued) {
  467. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  468. "linked_attrs_parse_config_entry: The %s config "
  469. "setting must be set to a multi-valued attribute "
  470. "for linked attribute pair \"%s\".\n",
  471. LINK_MANAGED_TYPE, entry->dn);
  472. ret = -1;
  473. goto bail;
  474. /* Check if the link type's syntax is Distinguished Name.
  475. * We only treat this as a warning. */
  476. } else if (not_dn_syntax) {
  477. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  478. "linked_attrs_parse_config_entry: The %s config "
  479. "setting must be set to an attribute with the "
  480. "Distinguished Name syntax for linked attribute "
  481. "pair \"%s\".\n", LINK_MANAGED_TYPE, entry->dn);
  482. }
  483. } else {
  484. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  485. "linked_attrs_parse_config_entry: The %s config "
  486. "setting is required for linked attribute pair \"%s\".\n",
  487. LINK_MANAGED_TYPE, entry->dn);
  488. ret = -1;
  489. goto bail;
  490. }
  491. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  492. "----------> %s [%s]\n", LINK_MANAGED_TYPE,
  493. entry->managedtype);
  494. /* A scope is not required. No scope means it
  495. * applies to any part of the DIT. */
  496. value = slapi_entry_attr_get_charptr(e, LINK_SCOPE);
  497. if (value) {
  498. entry->scope = value;
  499. }
  500. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  501. "----------> %s [%s]\n", LINK_SCOPE,
  502. entry->scope ? entry->scope : "NULL");
  503. /* Check if config already exists for
  504. * the link type at the same scope. */
  505. if (linked_attrs_config_exists(entry)) {
  506. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  507. "linked_attrs_parse_config_entry: A config "
  508. "entry for the link attribute %s already "
  509. "exists at a scope of \"%s\".\n", entry->linktype,
  510. entry->scope);
  511. ret = -1;
  512. goto bail;
  513. }
  514. /* Check if config already exists for
  515. * the managed type at the same scope. */
  516. if (linked_attrs_config_exists_reverse(entry)) {
  517. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  518. "linked_attrs_parse_config_entry: A config "
  519. "entry for the managed attribute %s already "
  520. "exists at a scope of \"%s\".\n", entry->managedtype,
  521. entry->scope);
  522. ret = -1;
  523. goto bail;
  524. }
  525. /* If we were only called to validate config, we can
  526. * just bail out before applying the config changes */
  527. if (apply == 0) {
  528. goto bail;
  529. }
  530. /* Create a lock for this attribute pair. */
  531. entry->lock = slapi_new_mutex();
  532. if (!entry->lock) {
  533. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  534. "linked_attrs_parse_config_entry: Unable to create "
  535. "lock for linked attribute pair \"%s\".\n", entry->dn);
  536. ret = -1;
  537. goto bail;
  538. }
  539. /* Add the entry to the list. We group by link type. We
  540. * also maintain a reverse list grouped by managed type. */
  541. if (!PR_CLIST_IS_EMPTY(g_link_config)) {
  542. list = PR_LIST_HEAD(g_link_config);
  543. while (list != g_link_config) {
  544. config_entry = (struct configEntry *) list;
  545. /* See if the types match. We want to group
  546. * entries for the same link type together. */
  547. if (slapi_attr_type_cmp(config_entry->linktype, entry->linktype, 1) == 0) {
  548. PR_INSERT_BEFORE(&(entry->list), list);
  549. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  550. "store [%s] before [%s] \n", entry->dn,
  551. config_entry->dn);
  552. /* add to managed type index */
  553. linked_attrs_insert_config_index(entry);
  554. entry_added = 1;
  555. break;
  556. }
  557. list = PR_NEXT_LINK(list);
  558. if (g_link_config == list) {
  559. /* add to tail */
  560. PR_INSERT_BEFORE(&(entry->list), list);
  561. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  562. "store [%s] at tail\n", entry->dn);
  563. /* add to managed type index */
  564. linked_attrs_insert_config_index(entry);
  565. entry_added = 1;
  566. break;
  567. }
  568. }
  569. } else {
  570. /* first entry */
  571. PR_INSERT_LINK(&(entry->list), g_link_config);
  572. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  573. "store [%s] at head \n", entry->dn);
  574. /* add to managed type index */
  575. linked_attrs_insert_config_index(entry);
  576. entry_added = 1;
  577. }
  578. bail:
  579. if (0 == entry_added) {
  580. /* Don't log error if we weren't asked to apply config */
  581. if ((apply != 0) && (entry != NULL)) {
  582. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  583. "linked_attrs_parse_config_entry: Invalid config entry "
  584. "[%s] skipped\n", entry->dn);
  585. }
  586. linked_attrs_free_config_entry(&entry);
  587. } else {
  588. ret = 0;
  589. }
  590. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  591. "<-- linked_attrs_parse_config_entry\n");
  592. return ret;
  593. }
  594. /*
  595. * linked_attrs_insert_config_index()
  596. *
  597. * Adds an entry to the ordered config index. We maintain
  598. * an list of pointers to the cached config entries that is
  599. * grouped by managed type. We use this index to find the
  600. * appropriate config entry when given a backpointer. This
  601. * is useful for the case where an entry with backpointers
  602. * is renamed and we need to updated the forward link.
  603. */
  604. static void
  605. linked_attrs_insert_config_index(struct configEntry *entry)
  606. {
  607. struct configEntry *config_entry = NULL;
  608. struct configIndex *index_entry = NULL;
  609. PRCList *list = PR_LIST_HEAD(g_managed_config_index);
  610. int inserted = 0;
  611. index_entry = (struct configIndex *)slapi_ch_calloc(1, sizeof(struct configIndex));
  612. index_entry->config = entry;
  613. if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  614. while (list != g_managed_config_index) {
  615. config_entry = ((struct configIndex *)list)->config;
  616. /* See if the types match. */
  617. if (slapi_attr_type_cmp(config_entry->managedtype, entry->managedtype, 1) == 0) {
  618. PR_INSERT_BEFORE(&(index_entry->list), list);
  619. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  620. "store [%s] before [%s] \n", entry->dn,
  621. config_entry->dn);
  622. inserted = 1;
  623. break;
  624. }
  625. list = PR_NEXT_LINK(list);
  626. if (g_managed_config_index == list) {
  627. /* add to tail */
  628. PR_INSERT_BEFORE(&(index_entry->list), list);
  629. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  630. "store [%s] at tail\n", entry->dn);
  631. inserted = 1;
  632. break;
  633. }
  634. }
  635. } else {
  636. /* first entry */
  637. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  638. "store [%s] at head \n", entry->dn);
  639. PR_INSERT_LINK(&(index_entry->list), g_managed_config_index);
  640. inserted = 1;
  641. }
  642. if(!inserted){
  643. slapi_ch_free((void **)&index_entry);
  644. }
  645. }
  646. static void
  647. linked_attrs_free_config_entry(struct configEntry ** entry)
  648. {
  649. struct configEntry *e = *entry;
  650. if (e == NULL)
  651. return;
  652. if (e->dn) {
  653. slapi_log_error(SLAPI_LOG_CONFIG, LINK_PLUGIN_SUBSYSTEM,
  654. "freeing config entry [%s]\n", e->dn);
  655. slapi_ch_free_string(&e->dn);
  656. }
  657. if (e->linktype)
  658. slapi_ch_free_string(&e->linktype);
  659. if (e->managedtype)
  660. slapi_ch_free_string(&e->managedtype);
  661. if (e->scope)
  662. slapi_ch_free_string(&e->scope);
  663. if (e->lock)
  664. slapi_destroy_mutex(e->lock);
  665. slapi_ch_free((void **) entry);
  666. }
  667. static void
  668. linked_attrs_delete_configEntry(PRCList *entry)
  669. {
  670. PR_REMOVE_LINK(entry);
  671. linked_attrs_free_config_entry((struct configEntry **) &entry);
  672. }
  673. static void
  674. linked_attrs_delete_config()
  675. {
  676. PRCList *list;
  677. /* Delete the config cache. */
  678. while (!PR_CLIST_IS_EMPTY(g_link_config)) {
  679. list = PR_LIST_HEAD(g_link_config);
  680. linked_attrs_delete_configEntry(list);
  681. }
  682. /* Delete the reverse index. */
  683. while (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  684. list = PR_LIST_HEAD(g_managed_config_index);
  685. PR_REMOVE_LINK(list);
  686. slapi_ch_free((void **)&list);
  687. }
  688. return;
  689. }
  690. /*
  691. * Helper functions
  692. */
  693. static char *
  694. linked_attrs_get_dn(Slapi_PBlock * pb)
  695. {
  696. const char *dn = 0;
  697. Slapi_DN *sdn = NULL;
  698. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  699. "--> linked_attrs_get_dn\n");
  700. if (slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn)) {
  701. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  702. "linked_attrs_get_dn: failed to get dn of changed entry");
  703. goto bail;
  704. }
  705. dn = slapi_sdn_get_dn(sdn);
  706. bail:
  707. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  708. "<-- linked_attrs_get_dn\n");
  709. return (char *)dn;
  710. }
  711. static Slapi_DN *
  712. linked_attrs_get_sdn(Slapi_PBlock * pb)
  713. {
  714. Slapi_DN *sdn = 0;
  715. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  716. "--> linked_attrs_get_sdn\n");
  717. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  718. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  719. "<-- linked_attrs_get_sdn\n");
  720. return sdn;
  721. }
  722. /*
  723. * linked_attrs_dn_is_config()
  724. *
  725. * Checks if dn is a linked attribute config entry.
  726. */
  727. static int
  728. linked_attrs_dn_is_config(char *dn)
  729. {
  730. int ret = 0;
  731. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  732. "--> linked_attrs_dn_is_config\n");
  733. /* Return 1 if the passed in dn is a child of the main
  734. * plugin config entry. */
  735. if (slapi_dn_issuffix(dn, linked_attrs_get_plugin_dn()) &&
  736. strcasecmp(dn, linked_attrs_get_plugin_dn())) {
  737. ret = 1;
  738. }
  739. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  740. "<-- linked_attrs_dn_is_config\n");
  741. return ret;
  742. }
  743. /*
  744. * linked_attrs_find_config()
  745. *
  746. * Finds the appropriate config entry for a given dn and
  747. * link type. A read lock must be held on the config
  748. * before calling this function. The configEntry that is
  749. * returned is a pointer to the actual config entry in
  750. * the config cache. It should not be modified in any
  751. * way. The read lock should not be released until you
  752. * are finished with the config entry that is returned.
  753. * Returns NULL if no applicable config entry is found.
  754. */
  755. static void
  756. linked_attrs_find_config(const char *dn,
  757. const char *type, struct configEntry **config)
  758. {
  759. int found_type = 0;
  760. PRCList *list = NULL;
  761. *config = NULL;
  762. if (!PR_CLIST_IS_EMPTY(g_link_config)) {
  763. list = PR_LIST_HEAD(g_link_config);
  764. while (list != g_link_config) {
  765. if (slapi_attr_type_cmp(((struct configEntry *)list)->linktype,
  766. type, 1) == 0) {
  767. /* Set a flag indicating that we found a config entry
  768. * for this type. We use this flag so we can stop
  769. * processing early if we don't find a matching scope. */
  770. found_type = 1;
  771. /* Check if the dn is in the scope of this config
  772. * entry. If the config entry doesn't have a scope
  773. * (global), consider it a match. If we have a match,
  774. * we can stop processing the config. */
  775. if ((((struct configEntry *)list)->scope == NULL) ||
  776. (slapi_dn_issuffix(dn, ((struct configEntry *)list)->scope))) {
  777. *config = (struct configEntry *)list;
  778. break;
  779. }
  780. } else {
  781. /* If flag is set, we're done. We have configured links
  782. * for this type, but none of the scopes match. */
  783. if (found_type) {
  784. break;
  785. }
  786. }
  787. list = PR_NEXT_LINK(list);
  788. }
  789. }
  790. }
  791. /*
  792. * linked_attrs_find_config_reverse()
  793. *
  794. * Finds the appropriate config entry for a given dn and
  795. * managed type. A read lock must be held on the config
  796. * before calling this function. The configEntry that is
  797. * returned is a pointer to the actual config entry in
  798. * the config cache. It should not be modified in any
  799. * way. The read lock should not be released until you
  800. * are finished with the config entry that is returned.
  801. * Returns NULL if no applicable config entry is found.
  802. */
  803. static void
  804. linked_attrs_find_config_reverse(const char *dn,
  805. const char *type, struct configEntry **config)
  806. {
  807. int found_type = 0;
  808. PRCList *list = NULL;
  809. *config = NULL;
  810. if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  811. list = PR_LIST_HEAD(g_managed_config_index);
  812. while (list != g_managed_config_index) {
  813. if (slapi_attr_type_cmp(((struct configIndex *)list)->config->managedtype,
  814. type, 1) == 0) {
  815. /* Set a flag indicating that we found a config entry
  816. * for this type. We use this flag so we can stop
  817. * processing early if we don't find a matching scope. */
  818. found_type = 1;
  819. /* Check if the dn is in the scope of this config
  820. * entry. If the config entry doesn't have a scope
  821. * (global), consider it a match. If we have a match,
  822. * we can stop processing the config. */
  823. if ((((struct configIndex *)list)->config->scope == NULL) ||
  824. (slapi_dn_issuffix(dn, ((struct configIndex *)list)->config->scope))) {
  825. *config = ((struct configIndex *)list)->config;
  826. break;
  827. }
  828. } else {
  829. /* If flag is set, we're done. We have configured links
  830. * for this type, but none of the scopes match. */
  831. if (found_type) {
  832. break;
  833. }
  834. }
  835. list = PR_NEXT_LINK(list);
  836. }
  837. }
  838. }
  839. /*
  840. * linked_attrs_config_index_has_type()
  841. *
  842. * Returns 1 if a config entry exists with the passed
  843. * in managed type.
  844. *
  845. * A read lock on the config must be held before calling
  846. * this function.
  847. */
  848. static int
  849. linked_attrs_config_index_has_type(char *type)
  850. {
  851. int rc = 0;
  852. PRCList *list = NULL;
  853. if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  854. list = PR_LIST_HEAD(g_managed_config_index);
  855. while (list != g_managed_config_index) {
  856. if (slapi_attr_type_cmp(((struct configIndex *)list)->config->managedtype,
  857. type, 1) == 0) {
  858. rc = 1;
  859. break;
  860. }
  861. list = PR_NEXT_LINK(list);
  862. }
  863. }
  864. return rc;
  865. }
  866. /*
  867. * linked_attrs_config_exists()
  868. *
  869. * Returns 1 if a config entry exists in the cache
  870. * already for the given link type at the given scope.
  871. * This will detect if the cached config entry is really
  872. * the same one as the passed in entry by comparing the
  873. * dn of the config entry. We will still return 0 in
  874. * this case as it's one and the same config entry. We
  875. * really want to use this to prevent multiple config
  876. * entries for the same link type at the same scope.
  877. *
  878. * A read lock on the config must be held before calling
  879. * this function.
  880. */
  881. static int
  882. linked_attrs_config_exists(struct configEntry *entry)
  883. {
  884. int rc = 0;
  885. int found_type = 0;
  886. PRCList *list = NULL;
  887. if (!PR_CLIST_IS_EMPTY(g_link_config)) {
  888. list = PR_LIST_HEAD(g_link_config);
  889. while (list != g_link_config) {
  890. if (slapi_attr_type_cmp(((struct configEntry *)list)->linktype,
  891. entry->linktype, 1) == 0) {
  892. found_type = 1;
  893. /* We don't allow nested config for the same type. We
  894. * need to check for nesting in both directions here.
  895. * If no scope is set, we consider the entry global. */
  896. if ((((struct configEntry *)list)->scope == NULL) ||
  897. slapi_dn_issuffix(entry->scope, ((struct configEntry *)list)->scope) ||
  898. slapi_dn_issuffix(((struct configEntry *)list)->scope, entry->scope)) {
  899. /* Make sure that this isn't the same exact entry
  900. * in the list already. This can happen if a config
  901. * entry is being modified. Both of these were already
  902. * normalized when the config struct was filled in. */
  903. if (strcasecmp(entry->dn, ((struct configEntry *)list)->dn) != 0) {
  904. rc = 1;
  905. break;
  906. }
  907. }
  908. } else {
  909. if (found_type) {
  910. /* Since the list is sorted by link type, we
  911. * are finished if we get here since we found
  912. * the type but didn't match the scope. */
  913. break;
  914. }
  915. }
  916. list = PR_NEXT_LINK(list);
  917. }
  918. }
  919. return rc;
  920. }
  921. /*
  922. * linked_attrs_config_exists_reverse()
  923. *
  924. * Returns 1 if a config entry exists in the cache
  925. * already for the given managed type at the given scope.
  926. * This will detect if the cached config entry is really
  927. * the same one as the passed in entry by comparing the
  928. * dn of the config entry. We will still return 0 in
  929. * this case as it's one and the same config entry. We
  930. * really want to use this to prevent multiple config
  931. * entries for the same managed type at the same scope.
  932. *
  933. * A read lock on the config must be held before calling
  934. * this function.
  935. */
  936. static int
  937. linked_attrs_config_exists_reverse(struct configEntry *entry)
  938. {
  939. int rc = 0;
  940. int found_type = 0;
  941. PRCList *list = NULL;
  942. if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  943. list = PR_LIST_HEAD(g_managed_config_index);
  944. while (list != g_managed_config_index) {
  945. if (slapi_attr_type_cmp(((struct configIndex *)list)->config->managedtype,
  946. entry->managedtype, 1) == 0) {
  947. found_type = 1;
  948. /* We don't allow nested config for the same type. We
  949. * need to check for nesting in both directions here. */
  950. if ((((struct configIndex *)list)->config->scope == NULL) ||
  951. slapi_dn_issuffix(entry->scope,
  952. ((struct configIndex *)list)->config->scope) ||
  953. slapi_dn_issuffix(((struct configIndex *)list)->config->scope,
  954. entry->scope)) {
  955. /* Make sure that this isn't the same exact entry
  956. * in the list already. This can happen if a config
  957. * entry is being modified. Both of these were already
  958. * normalized when the config struct was filled in. */
  959. if (strcasecmp(entry->dn, ((struct configIndex *)list)->config->dn) != 0) {
  960. rc = 1;
  961. break;
  962. }
  963. }
  964. } else {
  965. if (found_type) {
  966. /* Since the list is sorted by link type, we
  967. * are finished if we get here since we found
  968. * the type but didn't match the scope. */
  969. break;
  970. }
  971. }
  972. list = PR_NEXT_LINK(list);
  973. }
  974. }
  975. return rc;
  976. }
  977. /*
  978. * linked_attrs_oktodo()
  979. *
  980. * Check if we want to process this operation. We need to be
  981. * sure that the operation succeeded. We also respond to replicated
  982. * ops so we don't test for that. This does require that the managed
  983. * attributes not be replicated.
  984. */
  985. static int
  986. linked_attrs_oktodo(Slapi_PBlock *pb)
  987. {
  988. int ret = 1;
  989. int oprc = 0;
  990. slapi_log_error( SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  991. "--> linked_attrs_oktodo\n" );
  992. if(slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &oprc) != 0)
  993. {
  994. slapi_log_error( SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  995. "linked_attrs_oktodo: could not get parameters\n" );
  996. ret = -1;
  997. }
  998. /* This plugin should only execute if the operation succeeded. */
  999. if(oprc != 0)
  1000. {
  1001. ret = 0;
  1002. }
  1003. slapi_log_error( SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1004. "<-- linked_attrs_oktodo\n" );
  1005. return ret;
  1006. }
  1007. /* linked_attrs_load_array()
  1008. *
  1009. * put attribute values in array structure
  1010. */
  1011. void
  1012. linked_attrs_load_array(Slapi_Value **array, Slapi_Attr *attr)
  1013. {
  1014. Slapi_Value *val = 0;
  1015. int hint = slapi_attr_first_value(attr, &val);
  1016. while(val)
  1017. {
  1018. *array = val;
  1019. array++;
  1020. hint = slapi_attr_next_value(attr, hint, &val);
  1021. }
  1022. }
  1023. /* linked_attrs_compare()
  1024. *
  1025. * Compare two attr values using the DN syntax.
  1026. */
  1027. int
  1028. linked_attrs_compare(const void *a, const void *b)
  1029. {
  1030. Slapi_Value *val1;
  1031. Slapi_Value *val2;
  1032. Slapi_Attr *linkattr;
  1033. int rc = 0;
  1034. if(a == NULL && b != NULL){
  1035. return 1;
  1036. } else if(a != NULL && b == NULL){
  1037. return -1;
  1038. } else if(a == NULL && b == NULL){
  1039. return 0;
  1040. }
  1041. val1 = *((Slapi_Value **)a);
  1042. val2 = *((Slapi_Value **)b);
  1043. linkattr = slapi_attr_new();
  1044. slapi_attr_init(linkattr, "distinguishedName");
  1045. rc = slapi_attr_value_cmp(linkattr,
  1046. slapi_value_get_berval(val1),
  1047. slapi_value_get_berval(val2));
  1048. slapi_attr_free(&linkattr);
  1049. return rc;
  1050. }
  1051. /*
  1052. * linked_attrs_add_backpointers()
  1053. *
  1054. * Adds backpointers pointing to dn to the entries referred to
  1055. * by the values in smod.
  1056. */
  1057. static int
  1058. linked_attrs_add_backpointers(Slapi_PBlock *pb, char *linkdn, struct configEntry *config,
  1059. Slapi_Mod *smod)
  1060. {
  1061. Slapi_ValueSet *vals = slapi_valueset_new();
  1062. int rc = LDAP_SUCCESS;
  1063. slapi_valueset_set_from_smod(vals, smod);
  1064. rc = linked_attrs_mod_backpointers(pb, linkdn, config->managedtype, config->scope,
  1065. LDAP_MOD_ADD, vals);
  1066. slapi_valueset_free(vals);
  1067. return rc;
  1068. }
  1069. /*
  1070. * linked_attrs_del_backpointers()
  1071. *
  1072. * Remove backpointers pointing to linkdn in the entries referred
  1073. * to by the values in smod.
  1074. */
  1075. static int
  1076. linked_attrs_del_backpointers(Slapi_PBlock *pb, char *linkdn,
  1077. struct configEntry *config, Slapi_Mod *smod)
  1078. {
  1079. Slapi_ValueSet *vals = NULL;
  1080. int rc = LDAP_SUCCESS;
  1081. /* If no values are listed in the smod, we need to get
  1082. * a list of all of the values that were deleted by
  1083. * looking at the pre-op copy of the entry. */
  1084. if (slapi_mod_get_num_values(smod) == 0) {
  1085. Slapi_Entry *pre_e = NULL;
  1086. Slapi_Attr *pre_attr = NULL;
  1087. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &pre_e );
  1088. slapi_entry_attr_find( pre_e, config->linktype, &pre_attr );
  1089. slapi_attr_get_valueset(pre_attr, &vals);
  1090. } else {
  1091. vals = slapi_valueset_new();
  1092. slapi_valueset_set_from_smod(vals, smod);
  1093. }
  1094. rc = linked_attrs_mod_backpointers(pb, linkdn, config->managedtype, config->scope,
  1095. LDAP_MOD_DELETE, vals);
  1096. slapi_valueset_free(vals);
  1097. return rc;
  1098. }
  1099. /*
  1100. * linked_attrs_replace_backpointers()
  1101. *
  1102. * Remove backpointers pointing to linkdn from the entries
  1103. * whose values were deleted in smod and add backpointers
  1104. * for any new values that were added as a part of the
  1105. * replace operation.
  1106. */
  1107. static int
  1108. linked_attrs_replace_backpointers(Slapi_PBlock *pb, char *linkdn,
  1109. struct configEntry *config, Slapi_Mod *smod)
  1110. {
  1111. Slapi_Entry *pre_e = NULL;
  1112. Slapi_Entry *post_e = NULL;
  1113. Slapi_Attr *pre_attr = 0;
  1114. Slapi_Attr *post_attr = 0;
  1115. int rc = LDAP_SUCCESS;
  1116. /* Get the pre and post copy of the entry to see
  1117. * what values have been added and removed. */
  1118. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &pre_e);
  1119. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  1120. if(pre_e && post_e) {
  1121. slapi_entry_attr_find(pre_e, config->linktype, &pre_attr);
  1122. slapi_entry_attr_find(post_e, config->linktype, &post_attr);
  1123. }
  1124. if(pre_attr || post_attr) {
  1125. int pre_total = 0;
  1126. int post_total = 0;
  1127. Slapi_Value **pre_array = 0;
  1128. Slapi_Value **post_array = 0;
  1129. int pre_index = 0;
  1130. int post_index = 0;
  1131. Slapi_ValueSet *addvals = NULL;
  1132. Slapi_ValueSet *delvals = NULL;
  1133. /* create arrays of values */
  1134. if(pre_attr) {
  1135. slapi_attr_get_numvalues(pre_attr, &pre_total);
  1136. }
  1137. if(post_attr) {
  1138. slapi_attr_get_numvalues(post_attr, &post_total);
  1139. }
  1140. if(pre_total) {
  1141. pre_array = (Slapi_Value**) slapi_ch_malloc(sizeof(Slapi_Value*)*pre_total);
  1142. linked_attrs_load_array(pre_array, pre_attr);
  1143. qsort(pre_array, pre_total, sizeof(Slapi_Value*), linked_attrs_compare);
  1144. }
  1145. if(post_total) {
  1146. post_array = (Slapi_Value**) slapi_ch_malloc(sizeof(Slapi_Value*)*post_total);
  1147. linked_attrs_load_array(post_array, post_attr);
  1148. qsort(post_array, post_total, sizeof(Slapi_Value*), linked_attrs_compare);
  1149. }
  1150. /* Work through arrays, following these rules:
  1151. * - in pre, in post, do nothing
  1152. * - in pre, not in post, delete from entry
  1153. * - not in pre, in post, add to entry
  1154. */
  1155. while(pre_index < pre_total || post_index < post_total) {
  1156. if(pre_index == pre_total) {
  1157. /* add the rest of post */
  1158. if (addvals == NULL) {
  1159. addvals = slapi_valueset_new();
  1160. }
  1161. slapi_valueset_add_value(addvals, post_array[post_index]);
  1162. post_index++;
  1163. } else if(post_index == post_total) {
  1164. /* delete the rest of pre */
  1165. if (delvals == NULL) {
  1166. delvals = slapi_valueset_new();
  1167. }
  1168. slapi_valueset_add_value(delvals, pre_array[pre_index]);
  1169. pre_index++;
  1170. } else {
  1171. /* decide what to do */
  1172. int cmp = linked_attrs_compare(&(pre_array[pre_index]),
  1173. &(post_array[post_index]));
  1174. if(cmp < 0) {
  1175. /* delete pre array */
  1176. if (delvals == NULL) {
  1177. delvals = slapi_valueset_new();
  1178. }
  1179. slapi_valueset_add_value(delvals, pre_array[pre_index]);
  1180. pre_index++;
  1181. } else if(cmp > 0) {
  1182. /* add post array */
  1183. if (addvals == NULL) {
  1184. addvals = slapi_valueset_new();
  1185. }
  1186. slapi_valueset_add_value(addvals, post_array[post_index]);
  1187. post_index++;
  1188. } else {
  1189. /* do nothing, advance */
  1190. pre_index++;
  1191. post_index++;
  1192. }
  1193. }
  1194. }
  1195. /* Perform the actual updates to the target entries. */
  1196. if (delvals) {
  1197. rc = linked_attrs_mod_backpointers(pb, linkdn, config->managedtype,
  1198. config->scope, LDAP_MOD_DELETE, delvals);
  1199. slapi_valueset_free(delvals);
  1200. }
  1201. if (rc == LDAP_SUCCESS && addvals) {
  1202. rc = linked_attrs_mod_backpointers(pb, linkdn, config->managedtype,
  1203. config->scope, LDAP_MOD_ADD, addvals);
  1204. slapi_valueset_free(addvals);
  1205. }
  1206. slapi_ch_free((void **)&pre_array);
  1207. slapi_ch_free((void **)&post_array);
  1208. }
  1209. return rc;
  1210. }
  1211. /*
  1212. * linked_attrs_mod_backpointers()
  1213. *
  1214. * Performs backpointer management.
  1215. */
  1216. static int
  1217. linked_attrs_mod_backpointers(Slapi_PBlock *pb, char *linkdn, char *type,
  1218. char *scope, int modop, Slapi_ValueSet *targetvals)
  1219. {
  1220. char *val[2];
  1221. int i = 0;
  1222. Slapi_PBlock *mod_pb = slapi_pblock_new();
  1223. LDAPMod mod;
  1224. LDAPMod *mods[2];
  1225. Slapi_Value *targetval = NULL;
  1226. int rc = LDAP_SUCCESS;
  1227. /* Setup the modify operation. Only the target will
  1228. * change, so we only need to do this once. */
  1229. val[0] = linkdn;
  1230. val[1] = 0;
  1231. mod.mod_op = modop;
  1232. mod.mod_type = type;
  1233. mod.mod_values = val;
  1234. mods[0] = &mod;
  1235. mods[1] = 0;
  1236. i = slapi_valueset_first_value(targetvals, &targetval);
  1237. while(targetval)
  1238. {
  1239. int perform_update = 0;
  1240. const char *targetdn = slapi_value_get_string(targetval);
  1241. Slapi_DN *targetsdn = slapi_sdn_new_dn_byref(targetdn);
  1242. /* If we have a scope, only update the target if it is within
  1243. * the scope. If we don't have a scope, only update the target
  1244. * if it is in the same backend as the linkdn. */
  1245. if (scope) {
  1246. perform_update = slapi_dn_issuffix(targetdn, scope);
  1247. } else {
  1248. Slapi_Backend *be = NULL;
  1249. Slapi_DN *linksdn = slapi_sdn_new_normdn_byref(linkdn);
  1250. if ((be = slapi_be_select(linksdn))) {
  1251. perform_update = slapi_sdn_issuffix(targetsdn, slapi_be_getsuffix(be, 0));
  1252. }
  1253. slapi_sdn_free(&linksdn);
  1254. }
  1255. if (perform_update) {
  1256. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1257. "%s backpointer (%s) in entry (%s)\n",
  1258. (modop == LDAP_MOD_ADD) ? "Adding" : "Removing",
  1259. linkdn, targetdn);
  1260. /* Perform the modify operation. */
  1261. slapi_modify_internal_set_pb_ext(mod_pb, targetsdn, mods, 0, 0,
  1262. linked_attrs_get_plugin_id(), 0);
  1263. slapi_modify_internal_pb(mod_pb);
  1264. slapi_pblock_get(mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1265. if (((LDAP_MOD_DELETE == modop) && (LDAP_NO_SUCH_OBJECT == rc)) ||
  1266. ((LDAP_MOD_ADD == modop) && (LDAP_TYPE_OR_VALUE_EXISTS == rc))) {
  1267. /*
  1268. * We should ignore LDAP_NO_SUCH_OBJECT and LDAP_TYPE_OR_VALUE_EXISTS
  1269. * to support the case:
  1270. * target entry was renamed and the linktype value is being adjusted.
  1271. */
  1272. rc = LDAP_SUCCESS;
  1273. } else if (rc != LDAP_SUCCESS) {
  1274. char *err_msg = NULL;
  1275. err_msg = PR_smprintf("Linked Attrs Plugin: Failed to update "
  1276. "link to target entry (%s) error %d",
  1277. targetdn, rc);
  1278. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM, "%s\n", err_msg);
  1279. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, err_msg);
  1280. PR_smprintf_free(err_msg);
  1281. slapi_sdn_free(&targetsdn);
  1282. rc = LDAP_UNWILLING_TO_PERFORM;
  1283. break;
  1284. }
  1285. /* Initialize the pblock so we can reuse it. */
  1286. slapi_pblock_init(mod_pb);
  1287. }
  1288. slapi_sdn_free(&targetsdn);
  1289. i = slapi_valueset_next_value(targetvals, i, &targetval);
  1290. }
  1291. slapi_pblock_destroy(mod_pb);
  1292. return rc;
  1293. }
  1294. /*
  1295. * Operation callback functions
  1296. */
  1297. /*
  1298. * linked_attrs_pre_op()
  1299. *
  1300. * Checks if an operation is modifying the linked
  1301. * attribute config and validates the config changes.
  1302. */
  1303. static int
  1304. linked_attrs_pre_op(Slapi_PBlock * pb, int modop)
  1305. {
  1306. char *dn = 0;
  1307. Slapi_Entry *e = 0;
  1308. LDAPMod **mods = NULL;
  1309. int free_entry = 0;
  1310. char *errstr = NULL;
  1311. int ret = SLAPI_PLUGIN_SUCCESS;
  1312. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1313. "--> linked_attrs_pre_op\n");
  1314. if (0 == (dn = linked_attrs_get_dn(pb)))
  1315. goto bail;
  1316. if (linked_attrs_dn_is_config(dn)) {
  1317. /* Validate config changes, but don't apply them.
  1318. * This allows us to reject invalid config changes
  1319. * here at the pre-op stage. Applying the config
  1320. * needs to be done at the post-op stage. */
  1321. if (LDAP_CHANGETYPE_ADD == modop) {
  1322. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1323. } else {
  1324. /* Fetch the entry being modified so we can
  1325. * create the resulting entry for validation. */
  1326. /* int free_sdn = 0; */
  1327. Slapi_DN *tmp_dn = linked_attrs_get_sdn(pb);
  1328. if (tmp_dn) {
  1329. slapi_search_internal_get_entry(tmp_dn, 0, &e, linked_attrs_get_plugin_id());
  1330. free_entry = 1;
  1331. }
  1332. /* If the entry doesn't exist, just bail and
  1333. * let the server handle it. */
  1334. if (e == NULL) {
  1335. goto bail;
  1336. }
  1337. /* Grab the mods. */
  1338. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1339. /* Apply the mods to create the resulting entry. */
  1340. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1341. /* The mods don't apply cleanly, so we just let this op go
  1342. * to let the main server handle it. */
  1343. goto bail;
  1344. }
  1345. }
  1346. if (linked_attrs_parse_config_entry(e, 0) != 0) {
  1347. /* Refuse the operation if config parsing failed. */
  1348. ret = LDAP_UNWILLING_TO_PERFORM;
  1349. if (LDAP_CHANGETYPE_ADD == modop) {
  1350. errstr = slapi_ch_smprintf("Not a valid linked attribute configuration entry.");
  1351. } else {
  1352. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1353. "linked attribute configuration.");
  1354. }
  1355. }
  1356. }
  1357. bail:
  1358. if (free_entry && e)
  1359. slapi_entry_free(e);
  1360. if (ret) {
  1361. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1362. "linked_attrs_pre_op: operation failure [%d]\n", ret);
  1363. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1364. slapi_ch_free((void **)&errstr);
  1365. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1366. ret = SLAPI_PLUGIN_FAILURE;
  1367. }
  1368. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1369. "<-- linked_attrs_pre_op\n");
  1370. return ret;
  1371. }
  1372. static int
  1373. linked_attrs_add_pre_op(Slapi_PBlock * pb)
  1374. {
  1375. return linked_attrs_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1376. }
  1377. static int
  1378. linked_attrs_mod_pre_op(Slapi_PBlock * pb)
  1379. {
  1380. return linked_attrs_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1381. }
  1382. static int
  1383. linked_attrs_mod_post_op(Slapi_PBlock *pb)
  1384. {
  1385. Slapi_Mods *smods = NULL;
  1386. Slapi_Mod *smod = NULL;
  1387. LDAPMod **mods;
  1388. Slapi_Mod *next_mod = NULL;
  1389. char *dn = NULL;
  1390. struct configEntry *config = NULL;
  1391. void *caller_id = NULL;
  1392. int rc = SLAPI_PLUGIN_SUCCESS;
  1393. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1394. "--> linked_attrs_mod_post_op\n");
  1395. /* We don't want to process internal modify
  1396. * operations that originate from this plugin.
  1397. * Doing so could cause a deadlock. */
  1398. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  1399. if (caller_id == linked_attrs_get_plugin_id()) {
  1400. /* Just return without processing */
  1401. return SLAPI_PLUGIN_SUCCESS;
  1402. }
  1403. if (linked_attrs_oktodo(pb) &&
  1404. (dn = linked_attrs_get_dn(pb))) {
  1405. /* First check if the config is being modified. */
  1406. if (linked_attrs_dn_is_config(dn)) {
  1407. linked_attrs_load_config();
  1408. }
  1409. /* get the mod set */
  1410. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1411. smods = slapi_mods_new();
  1412. slapi_mods_init_byref(smods, mods);
  1413. next_mod = slapi_mod_new();
  1414. smod = slapi_mods_get_first_smod(smods, next_mod);
  1415. while(smod) {
  1416. char *type = (char *)slapi_mod_get_type(smod);
  1417. /* See if there is an applicable link configured. */
  1418. linked_attrs_read_lock();
  1419. /* Bail out if the plug-in close function was just called. */
  1420. if (!slapi_plugin_running(pb)) {
  1421. linked_attrs_unlock();
  1422. return SLAPI_PLUGIN_SUCCESS;
  1423. }
  1424. linked_attrs_find_config(dn, type, &config);
  1425. /* If we have a matching config entry, we have
  1426. * work to do. If not, we can go to the next smod. */
  1427. if (config) {
  1428. int op = slapi_mod_get_operation(smod);
  1429. /* Prevent other threads from managing
  1430. * this specific link at the same time. */
  1431. slapi_lock_mutex(config->lock);
  1432. switch(op & ~LDAP_MOD_BVALUES) {
  1433. case LDAP_MOD_ADD:
  1434. /* Find the entries pointed to by the new
  1435. * values and add the backpointers. */
  1436. rc = linked_attrs_add_backpointers(pb, dn, config, smod);
  1437. break;
  1438. case LDAP_MOD_DELETE:
  1439. /* Find the entries pointed to by the deleted
  1440. * values and remove the backpointers. */
  1441. rc = linked_attrs_del_backpointers(pb, dn, config, smod);
  1442. break;
  1443. case LDAP_MOD_REPLACE:
  1444. /* Find the entries pointed to by the deleted
  1445. * values and remove the backpointers. If
  1446. * any new values are being added, find those
  1447. * entries and add the backpointers. */
  1448. rc = linked_attrs_replace_backpointers(pb, dn, config, smod);
  1449. break;
  1450. default:
  1451. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1452. "linked_attrs_mod_post_op: unknown mod type\n" );
  1453. rc = SLAPI_PLUGIN_FAILURE;
  1454. break;
  1455. }
  1456. slapi_unlock_mutex(config->lock);
  1457. if(rc != LDAP_SUCCESS){
  1458. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1459. "linked_attrs_mod_post_op - update failed (%d)\n",rc);
  1460. linked_attrs_unlock();
  1461. slapi_mod_done(next_mod);
  1462. break;
  1463. }
  1464. }
  1465. config = NULL;
  1466. linked_attrs_unlock();
  1467. slapi_mod_done(next_mod);
  1468. smod = slapi_mods_get_next_smod(smods, next_mod);
  1469. }
  1470. slapi_mod_free(&next_mod);
  1471. slapi_mods_free(&smods);
  1472. }
  1473. if (rc) {
  1474. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1475. rc = SLAPI_PLUGIN_FAILURE;
  1476. }
  1477. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1478. "<-- linked_attrs_mod_post_op (%d)\n", rc);
  1479. return rc;
  1480. }
  1481. static int
  1482. linked_attrs_add_post_op(Slapi_PBlock *pb)
  1483. {
  1484. Slapi_Entry *e = NULL;
  1485. char *dn = NULL;
  1486. int rc = SLAPI_PLUGIN_SUCCESS;
  1487. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1488. "--> linked_attrs_add_post_op\n");
  1489. /* Reload config if a config entry was added. */
  1490. if ((dn = linked_attrs_get_dn(pb))) {
  1491. if (linked_attrs_dn_is_config(dn))
  1492. linked_attrs_load_config();
  1493. } else {
  1494. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1495. "linked_attrs_add_post_op: Error "
  1496. "retrieving dn\n");
  1497. }
  1498. /* Get the newly added entry. */
  1499. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1500. if (e) {
  1501. Slapi_Attr *attr = NULL;
  1502. char *type = NULL;
  1503. struct configEntry *config = NULL;
  1504. slapi_entry_first_attr(e, &attr);
  1505. while (attr) {
  1506. slapi_attr_get_type(attr, &type);
  1507. /* See if there is an applicable link configured. */
  1508. linked_attrs_read_lock();
  1509. /* Bail out if the plug-in close function was just called. */
  1510. if (!slapi_plugin_running(pb)) {
  1511. linked_attrs_unlock();
  1512. return SLAPI_PLUGIN_SUCCESS;
  1513. }
  1514. linked_attrs_find_config(dn, type, &config);
  1515. /* If config was found, add the backpointers to this entry. */
  1516. if (config) {
  1517. Slapi_ValueSet *vals = NULL;
  1518. slapi_attr_get_valueset(attr, &vals);
  1519. slapi_lock_mutex(config->lock);
  1520. rc = linked_attrs_mod_backpointers(pb, dn, config->managedtype,
  1521. config->scope, LDAP_MOD_ADD, vals);
  1522. slapi_unlock_mutex(config->lock);
  1523. slapi_valueset_free(vals);
  1524. if(rc != LDAP_SUCCESS){
  1525. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1526. "linked_attrs_add_post_op: update failed (%d)\n",rc);
  1527. linked_attrs_unlock();
  1528. break;
  1529. }
  1530. }
  1531. config = NULL;
  1532. linked_attrs_unlock();
  1533. slapi_entry_next_attr(e, attr, &attr);
  1534. }
  1535. } else {
  1536. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1537. "linked_attrs_add_post_op: Error "
  1538. "retrieving post-op entry %s\n", dn);
  1539. }
  1540. if (rc) {
  1541. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1542. rc = SLAPI_PLUGIN_FAILURE;
  1543. }
  1544. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1545. "<-- linked_attrs_add_post_op\n");
  1546. return rc;
  1547. }
  1548. static int
  1549. linked_attrs_del_post_op(Slapi_PBlock *pb)
  1550. {
  1551. char *dn = NULL;
  1552. Slapi_Entry *e = NULL;
  1553. int rc = SLAPI_PLUGIN_SUCCESS;
  1554. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1555. "--> linked_attrs_del_post_op\n");
  1556. /* Just bail if we aren't ready to service requests yet. */
  1557. if (!linked_attrs_oktodo(pb)){
  1558. return rc;
  1559. }
  1560. /* Reload config if a config entry was deleted. */
  1561. if ((dn = linked_attrs_get_dn(pb))) {
  1562. if (linked_attrs_dn_is_config(dn))
  1563. linked_attrs_load_config();
  1564. } else {
  1565. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1566. "linked_attrs_del_post_op: Error "
  1567. "retrieving dn\n");
  1568. }
  1569. /* Get deleted entry, then go through types to find config. */
  1570. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );
  1571. if (e) {
  1572. Slapi_Attr *attr = NULL;
  1573. char *type = NULL;
  1574. struct configEntry *config = NULL;
  1575. slapi_entry_first_attr(e, &attr);
  1576. while (attr) {
  1577. slapi_attr_get_type(attr, &type);
  1578. /* See if there is an applicable link configured. */
  1579. linked_attrs_read_lock();
  1580. /* Bail out if the plug-in close function was just called. */
  1581. if (!slapi_plugin_running(pb)) {
  1582. linked_attrs_unlock();
  1583. return rc;
  1584. }
  1585. linked_attrs_find_config(dn, type, &config);
  1586. /* If config was found, delete the backpointers to this entry. */
  1587. if (config) {
  1588. Slapi_ValueSet *vals = NULL;
  1589. slapi_attr_get_valueset(attr, &vals);
  1590. slapi_lock_mutex(config->lock);
  1591. rc = linked_attrs_mod_backpointers(pb, dn, config->managedtype,
  1592. config->scope, LDAP_MOD_DELETE, vals);
  1593. slapi_unlock_mutex(config->lock);
  1594. slapi_valueset_free(vals);
  1595. if (rc != LDAP_SUCCESS){
  1596. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1597. "linked_attrs_del_post_op - update failed (%d)\n",rc);
  1598. linked_attrs_unlock();
  1599. break;
  1600. }
  1601. }
  1602. config = NULL;
  1603. /* See if any of the values for this attribute are managed
  1604. * backpointers. We need to remove the forward link if so. */
  1605. if (linked_attrs_config_index_has_type(type)) {
  1606. int hint = 0;
  1607. Slapi_Value *val = NULL;
  1608. /* Loop through values and see if we have matching config */
  1609. hint = slapi_attr_first_value(attr, &val);
  1610. while (val) {
  1611. linked_attrs_find_config_reverse(slapi_value_get_string(val),
  1612. type, &config);
  1613. if (config) {
  1614. Slapi_ValueSet *vals = slapi_valueset_new();
  1615. slapi_valueset_add_value(vals, val);
  1616. slapi_lock_mutex(config->lock);
  1617. /* Delete forward link value. */
  1618. rc = linked_attrs_mod_backpointers(pb, dn, config->linktype,
  1619. config->scope, LDAP_MOD_DELETE, vals);
  1620. slapi_unlock_mutex(config->lock);
  1621. slapi_valueset_free(vals);
  1622. config = NULL;
  1623. if(rc != LDAP_SUCCESS){
  1624. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1625. "linked_attrs_del_post_op: update failed (%d)\n",rc);
  1626. linked_attrs_unlock();
  1627. goto bail;
  1628. }
  1629. }
  1630. hint = slapi_attr_next_value(attr, hint, &val);
  1631. }
  1632. }
  1633. linked_attrs_unlock();
  1634. slapi_entry_next_attr(e, attr, &attr);
  1635. }
  1636. } else {
  1637. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1638. "linked_attrs_del_post_op: Error "
  1639. "retrieving pre-op entry %s\n", dn);
  1640. rc = SLAPI_PLUGIN_FAILURE;
  1641. }
  1642. bail:
  1643. if (rc) {
  1644. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1645. rc = SLAPI_PLUGIN_FAILURE;
  1646. }
  1647. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1648. "<-- linked_attrs_del_post_op\n");
  1649. return rc;
  1650. }
  1651. static int
  1652. linked_attrs_modrdn_post_op(Slapi_PBlock *pb)
  1653. {
  1654. char *old_dn = NULL;
  1655. char *new_dn = NULL;
  1656. Slapi_Entry *post_e = NULL;
  1657. Slapi_Attr *attr = NULL;
  1658. char *type = NULL;
  1659. struct configEntry *config = NULL;
  1660. int rc = SLAPI_PLUGIN_SUCCESS;
  1661. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1662. "--> linked_attrs_modrdn_post_op\n");
  1663. /* Just bail if we aren't ready to service requests yet. */
  1664. if (!linked_attrs_oktodo(pb)) {
  1665. goto done;
  1666. }
  1667. /* Reload config if an existing config entry was renamed,
  1668. * or if the new dn brings an entry into the scope of the
  1669. * config entries. */
  1670. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  1671. if (post_e) {
  1672. new_dn = slapi_entry_get_ndn(post_e);
  1673. } else {
  1674. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1675. "linked_attrs_modrdn_post_op: Error "
  1676. "retrieving post-op entry\n");
  1677. rc = LDAP_OPERATIONS_ERROR;
  1678. goto done;
  1679. }
  1680. if ((old_dn = linked_attrs_get_dn(pb))) {
  1681. if (linked_attrs_dn_is_config(old_dn) || linked_attrs_dn_is_config(new_dn))
  1682. linked_attrs_load_config();
  1683. } else {
  1684. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1685. "linked_attrs_modrdn_post_op: Error "
  1686. "retrieving dn\n");
  1687. rc = LDAP_OPERATIONS_ERROR;
  1688. goto done;
  1689. }
  1690. /* Check if this operation requires any updates to links. */
  1691. slapi_entry_first_attr(post_e, &attr);
  1692. while (attr) {
  1693. slapi_attr_get_type(attr, &type);
  1694. /* See if there is an applicable link configured. */
  1695. linked_attrs_read_lock();
  1696. /* Bail out if the plug-in close function was just called. */
  1697. if (!slapi_plugin_running(pb)) {
  1698. linked_attrs_unlock();
  1699. return SLAPI_PLUGIN_SUCCESS;
  1700. }
  1701. linked_attrs_find_config(old_dn, type, &config);
  1702. /* If config was found for the old dn, delete the backpointers
  1703. * to this entry. */
  1704. if (config) {
  1705. Slapi_ValueSet *vals = NULL;
  1706. slapi_attr_get_valueset(attr, &vals);
  1707. slapi_lock_mutex(config->lock);
  1708. /* Delete old dn value. */
  1709. rc = linked_attrs_mod_backpointers(pb, old_dn, config->managedtype,
  1710. config->scope, LDAP_MOD_DELETE, vals);
  1711. slapi_unlock_mutex(config->lock);
  1712. slapi_valueset_free(vals);
  1713. config = NULL;
  1714. if(rc != LDAP_SUCCESS){
  1715. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1716. "linked_attrs_modrdn_post_op: update failed(old type) (%d)\n",rc);
  1717. linked_attrs_unlock();
  1718. break;
  1719. }
  1720. }
  1721. linked_attrs_find_config(new_dn, type, &config);
  1722. /* If config was found for the new dn, add the backpointers
  1723. * to this entry. We do this separate check for both dn's
  1724. * to catch an entry that comes into or goes out of scope
  1725. * from the MODRDN operation. */
  1726. if (config) {
  1727. Slapi_ValueSet *vals = NULL;
  1728. slapi_attr_get_valueset(attr, &vals);
  1729. slapi_lock_mutex(config->lock);
  1730. /* Add new dn value. */
  1731. rc = linked_attrs_mod_backpointers(pb, new_dn, config->managedtype,
  1732. config->scope, LDAP_MOD_ADD, vals);
  1733. slapi_unlock_mutex(config->lock);
  1734. slapi_valueset_free(vals);
  1735. config = NULL;
  1736. if(rc != LDAP_SUCCESS){
  1737. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1738. "linked_attrs_modrdn_post_op: update failed(new type) (%d)\n",rc);
  1739. linked_attrs_unlock();
  1740. break;
  1741. }
  1742. }
  1743. /* See if any of the values for this attribute are managed
  1744. * backpointers. We need to update the forward link if so. */
  1745. if (linked_attrs_config_index_has_type(type)) {
  1746. int hint = 0;
  1747. Slapi_Value *val = NULL;
  1748. /* Loop through values and see if we have matching config */
  1749. hint = slapi_attr_first_value(attr, &val);
  1750. while (val) {
  1751. linked_attrs_find_config_reverse(slapi_value_get_string(val),
  1752. type, &config);
  1753. /* If the new DN is within scope, we should fixup the forward links. */
  1754. if (config && slapi_dn_issuffix(new_dn, (config->scope))) {
  1755. Slapi_ValueSet *vals = slapi_valueset_new();
  1756. slapi_valueset_add_value(vals, val);
  1757. slapi_lock_mutex(config->lock);
  1758. /* Delete old dn value. */
  1759. rc = linked_attrs_mod_backpointers(pb, old_dn, config->linktype,
  1760. config->scope, LDAP_MOD_DELETE, vals);
  1761. if(rc != LDAP_SUCCESS){
  1762. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1763. "linked_attrs_modrdn_post_op: update failed(old dn) (%d)\n",rc);
  1764. slapi_unlock_mutex(config->lock);
  1765. slapi_valueset_free(vals);
  1766. linked_attrs_unlock();
  1767. goto done;
  1768. }
  1769. /* Add new dn value. */
  1770. rc = linked_attrs_mod_backpointers(pb, new_dn, config->linktype,
  1771. config->scope, LDAP_MOD_ADD, vals);
  1772. slapi_unlock_mutex(config->lock);
  1773. slapi_valueset_free(vals);
  1774. config = NULL;
  1775. if(rc != LDAP_SUCCESS){
  1776. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1777. "linked_attrs_modrdn_post_op: update failed(new dn) (%d)\n",rc);
  1778. linked_attrs_unlock();
  1779. goto done;
  1780. }
  1781. }
  1782. hint = slapi_attr_next_value(attr, hint, &val);
  1783. }
  1784. }
  1785. linked_attrs_unlock();
  1786. slapi_entry_next_attr(post_e, attr, &attr);
  1787. }
  1788. done:
  1789. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1790. "<-- linked_attrs_modrdn_post_op\n");
  1791. if (rc) {
  1792. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1793. rc = SLAPI_PLUGIN_FAILURE;
  1794. }
  1795. return rc;
  1796. }
  1797. /*
  1798. * Debug functions to print config
  1799. */
  1800. void
  1801. linked_attrs_dump_config()
  1802. {
  1803. PRCList *list;
  1804. linked_attrs_read_lock();
  1805. if (!PR_CLIST_IS_EMPTY(g_link_config)) {
  1806. list = PR_LIST_HEAD(g_link_config);
  1807. while (list != g_link_config) {
  1808. linked_attrs_dump_config_entry((struct configEntry *)list);
  1809. list = PR_NEXT_LINK(list);
  1810. }
  1811. }
  1812. linked_attrs_unlock();
  1813. }
  1814. void
  1815. linked_attrs_dump_config_index()
  1816. {
  1817. PRCList *list;
  1818. linked_attrs_read_lock();
  1819. if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  1820. list = PR_LIST_HEAD(g_managed_config_index);
  1821. while (list != g_managed_config_index) {
  1822. linked_attrs_dump_config_entry(((struct configIndex *)list)->config);
  1823. list = PR_NEXT_LINK(list);
  1824. }
  1825. }
  1826. linked_attrs_unlock();
  1827. }
  1828. void
  1829. linked_attrs_dump_config_entry(struct configEntry * entry)
  1830. {
  1831. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1832. "<==== Linked Attribute Pair =====>\n");
  1833. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1834. "<---- config entry dn -----> %s\n", entry->dn);
  1835. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1836. "<---- link type -----------> %s\n", entry->linktype);
  1837. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1838. "<---- managed type --------> %s\n", entry->managedtype);
  1839. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1840. "<---- scope ---------------> %s\n", entry->scope);
  1841. }