linked_attrs.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  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. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1276. "Linked Attrs Plugin: Failed to update link to target entry (%s) error %d",
  1277. targetdn, rc);
  1278. err_msg = PR_smprintf("Linked Attrs Plugin: Failed to update "
  1279. "link to target entry (%s) error %d",
  1280. targetdn, rc);
  1281. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM, "%s\n", err_msg);
  1282. slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, err_msg);
  1283. PR_smprintf_free(err_msg);
  1284. slapi_sdn_free(&targetsdn);
  1285. if (i > 0) {
  1286. int j;
  1287. Slapi_ValueSet *undoVals = slapi_valueset_new();
  1288. /* undo 0..i-1 */
  1289. j = slapi_valueset_first_value(targetvals, &targetval);
  1290. do {
  1291. slapi_valueset_add_value(undoVals, targetval);
  1292. j = slapi_valueset_next_value(targetvals, j, &targetval);
  1293. } while (j < i);
  1294. if (LDAP_MOD_DELETE == modop) {
  1295. modop = LDAP_MOD_ADD;
  1296. } else {
  1297. modop = LDAP_MOD_DELETE;
  1298. }
  1299. rc = linked_attrs_mod_backpointers(pb, linkdn, type, scope, modop, undoVals);
  1300. slapi_valueset_free(undoVals);
  1301. }
  1302. rc = LDAP_UNWILLING_TO_PERFORM;
  1303. break;
  1304. }
  1305. /* Initialize the pblock so we can reuse it. */
  1306. slapi_pblock_init(mod_pb);
  1307. }
  1308. slapi_sdn_free(&targetsdn);
  1309. i = slapi_valueset_next_value(targetvals, i, &targetval);
  1310. }
  1311. slapi_pblock_destroy(mod_pb);
  1312. return rc;
  1313. }
  1314. /*
  1315. * Operation callback functions
  1316. */
  1317. /*
  1318. * linked_attrs_pre_op()
  1319. *
  1320. * Checks if an operation is modifying the linked
  1321. * attribute config and validates the config changes.
  1322. */
  1323. static int
  1324. linked_attrs_pre_op(Slapi_PBlock * pb, int modop)
  1325. {
  1326. char *dn = 0;
  1327. Slapi_Entry *e = 0;
  1328. LDAPMod **mods = NULL;
  1329. int free_entry = 0;
  1330. char *errstr = NULL;
  1331. int ret = SLAPI_PLUGIN_SUCCESS;
  1332. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1333. "--> linked_attrs_pre_op\n");
  1334. if (0 == (dn = linked_attrs_get_dn(pb)))
  1335. goto bail;
  1336. if (linked_attrs_dn_is_config(dn)) {
  1337. /* Validate config changes, but don't apply them.
  1338. * This allows us to reject invalid config changes
  1339. * here at the pre-op stage. Applying the config
  1340. * needs to be done at the post-op stage. */
  1341. if (LDAP_CHANGETYPE_ADD == modop) {
  1342. slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e);
  1343. } else {
  1344. /* Fetch the entry being modified so we can
  1345. * create the resulting entry for validation. */
  1346. /* int free_sdn = 0; */
  1347. Slapi_DN *tmp_dn = linked_attrs_get_sdn(pb);
  1348. if (tmp_dn) {
  1349. slapi_search_internal_get_entry(tmp_dn, 0, &e, linked_attrs_get_plugin_id());
  1350. free_entry = 1;
  1351. }
  1352. /* If the entry doesn't exist, just bail and
  1353. * let the server handle it. */
  1354. if (e == NULL) {
  1355. goto bail;
  1356. }
  1357. /* Grab the mods. */
  1358. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1359. /* Apply the mods to create the resulting entry. */
  1360. if (mods && (slapi_entry_apply_mods(e, mods) != LDAP_SUCCESS)) {
  1361. /* The mods don't apply cleanly, so we just let this op go
  1362. * to let the main server handle it. */
  1363. goto bail;
  1364. }
  1365. }
  1366. if (linked_attrs_parse_config_entry(e, 0) != 0) {
  1367. /* Refuse the operation if config parsing failed. */
  1368. ret = LDAP_UNWILLING_TO_PERFORM;
  1369. if (LDAP_CHANGETYPE_ADD == modop) {
  1370. errstr = slapi_ch_smprintf("Not a valid linked attribute configuration entry.");
  1371. } else {
  1372. errstr = slapi_ch_smprintf("Changes result in an invalid "
  1373. "linked attribute configuration.");
  1374. }
  1375. }
  1376. }
  1377. bail:
  1378. if (free_entry && e)
  1379. slapi_entry_free(e);
  1380. if (ret) {
  1381. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1382. "linked_attrs_pre_op: operation failure [%d]\n", ret);
  1383. slapi_send_ldap_result(pb, ret, NULL, errstr, 0, NULL);
  1384. slapi_ch_free((void **)&errstr);
  1385. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ret);
  1386. ret = SLAPI_PLUGIN_FAILURE;
  1387. }
  1388. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1389. "<-- linked_attrs_pre_op\n");
  1390. return ret;
  1391. }
  1392. static int
  1393. linked_attrs_add_pre_op(Slapi_PBlock * pb)
  1394. {
  1395. return linked_attrs_pre_op(pb, LDAP_CHANGETYPE_ADD);
  1396. }
  1397. static int
  1398. linked_attrs_mod_pre_op(Slapi_PBlock * pb)
  1399. {
  1400. return linked_attrs_pre_op(pb, LDAP_CHANGETYPE_MODIFY);
  1401. }
  1402. static int
  1403. linked_attrs_mod_post_op(Slapi_PBlock *pb)
  1404. {
  1405. Slapi_Mods *smods = NULL;
  1406. Slapi_Mod *smod = NULL;
  1407. LDAPMod **mods;
  1408. Slapi_Mod *next_mod = NULL;
  1409. char *dn = NULL;
  1410. struct configEntry *config = NULL;
  1411. void *caller_id = NULL;
  1412. int rc = SLAPI_PLUGIN_SUCCESS;
  1413. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1414. "--> linked_attrs_mod_post_op\n");
  1415. /* We don't want to process internal modify
  1416. * operations that originate from this plugin.
  1417. * Doing so could cause a deadlock. */
  1418. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &caller_id);
  1419. if (caller_id == linked_attrs_get_plugin_id()) {
  1420. /* Just return without processing */
  1421. return SLAPI_PLUGIN_SUCCESS;
  1422. }
  1423. if (linked_attrs_oktodo(pb) &&
  1424. (dn = linked_attrs_get_dn(pb))) {
  1425. /* First check if the config is being modified. */
  1426. if (linked_attrs_dn_is_config(dn)) {
  1427. linked_attrs_load_config();
  1428. }
  1429. /* get the mod set */
  1430. slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
  1431. smods = slapi_mods_new();
  1432. slapi_mods_init_byref(smods, mods);
  1433. next_mod = slapi_mod_new();
  1434. smod = slapi_mods_get_first_smod(smods, next_mod);
  1435. while(smod) {
  1436. char *type = (char *)slapi_mod_get_type(smod);
  1437. /* See if there is an applicable link configured. */
  1438. linked_attrs_read_lock();
  1439. /* Bail out if the plug-in close function was just called. */
  1440. if (!slapi_plugin_running(pb)) {
  1441. linked_attrs_unlock();
  1442. return SLAPI_PLUGIN_SUCCESS;
  1443. }
  1444. linked_attrs_find_config(dn, type, &config);
  1445. /* If we have a matching config entry, we have
  1446. * work to do. If not, we can go to the next smod. */
  1447. if (config) {
  1448. int op = slapi_mod_get_operation(smod);
  1449. /* Prevent other threads from managing
  1450. * this specific link at the same time. */
  1451. slapi_lock_mutex(config->lock);
  1452. switch(op & ~LDAP_MOD_BVALUES) {
  1453. case LDAP_MOD_ADD:
  1454. /* Find the entries pointed to by the new
  1455. * values and add the backpointers. */
  1456. rc = linked_attrs_add_backpointers(pb, dn, config, smod);
  1457. break;
  1458. case LDAP_MOD_DELETE:
  1459. /* Find the entries pointed to by the deleted
  1460. * values and remove the backpointers. */
  1461. rc = linked_attrs_del_backpointers(pb, dn, config, smod);
  1462. break;
  1463. case LDAP_MOD_REPLACE:
  1464. /* Find the entries pointed to by the deleted
  1465. * values and remove the backpointers. If
  1466. * any new values are being added, find those
  1467. * entries and add the backpointers. */
  1468. rc = linked_attrs_replace_backpointers(pb, dn, config, smod);
  1469. break;
  1470. default:
  1471. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1472. "linked_attrs_mod_post_op: unknown mod type\n" );
  1473. rc = SLAPI_PLUGIN_FAILURE;
  1474. break;
  1475. }
  1476. slapi_unlock_mutex(config->lock);
  1477. if(rc != LDAP_SUCCESS){
  1478. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1479. "linked_attrs_mod_post_op - update failed (%d)\n",rc);
  1480. linked_attrs_unlock();
  1481. slapi_mod_done(next_mod);
  1482. break;
  1483. }
  1484. }
  1485. config = NULL;
  1486. linked_attrs_unlock();
  1487. slapi_mod_done(next_mod);
  1488. smod = slapi_mods_get_next_smod(smods, next_mod);
  1489. }
  1490. slapi_mod_free(&next_mod);
  1491. slapi_mods_free(&smods);
  1492. }
  1493. if (rc) {
  1494. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1495. rc = SLAPI_PLUGIN_FAILURE;
  1496. }
  1497. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1498. "<-- linked_attrs_mod_post_op (%d)\n", rc);
  1499. return rc;
  1500. }
  1501. static int
  1502. linked_attrs_add_post_op(Slapi_PBlock *pb)
  1503. {
  1504. Slapi_Entry *e = NULL;
  1505. char *dn = NULL;
  1506. int rc = SLAPI_PLUGIN_SUCCESS;
  1507. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1508. "--> linked_attrs_add_post_op\n");
  1509. /* Reload config if a config entry was added. */
  1510. if ((dn = linked_attrs_get_dn(pb))) {
  1511. if (linked_attrs_dn_is_config(dn))
  1512. linked_attrs_load_config();
  1513. } else {
  1514. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1515. "linked_attrs_add_post_op: Error "
  1516. "retrieving dn\n");
  1517. }
  1518. /* Get the newly added entry. */
  1519. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &e);
  1520. if (e) {
  1521. Slapi_Attr *attr = NULL;
  1522. char *type = NULL;
  1523. struct configEntry *config = NULL;
  1524. slapi_entry_first_attr(e, &attr);
  1525. while (attr) {
  1526. slapi_attr_get_type(attr, &type);
  1527. /* See if there is an applicable link configured. */
  1528. linked_attrs_read_lock();
  1529. /* Bail out if the plug-in close function was just called. */
  1530. if (!slapi_plugin_running(pb)) {
  1531. linked_attrs_unlock();
  1532. return SLAPI_PLUGIN_SUCCESS;
  1533. }
  1534. linked_attrs_find_config(dn, type, &config);
  1535. /* If config was found, add the backpointers to this entry. */
  1536. if (config) {
  1537. Slapi_ValueSet *vals = NULL;
  1538. slapi_attr_get_valueset(attr, &vals);
  1539. slapi_lock_mutex(config->lock);
  1540. rc = linked_attrs_mod_backpointers(pb, dn, config->managedtype,
  1541. config->scope, LDAP_MOD_ADD, vals);
  1542. slapi_unlock_mutex(config->lock);
  1543. slapi_valueset_free(vals);
  1544. if(rc != LDAP_SUCCESS){
  1545. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1546. "linked_attrs_add_post_op: update failed (%d)\n",rc);
  1547. linked_attrs_unlock();
  1548. break;
  1549. }
  1550. }
  1551. config = NULL;
  1552. linked_attrs_unlock();
  1553. slapi_entry_next_attr(e, attr, &attr);
  1554. }
  1555. } else {
  1556. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1557. "linked_attrs_add_post_op: Error "
  1558. "retrieving post-op entry %s\n", dn);
  1559. }
  1560. if (rc) {
  1561. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1562. rc = SLAPI_PLUGIN_FAILURE;
  1563. }
  1564. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1565. "<-- linked_attrs_add_post_op\n");
  1566. return rc;
  1567. }
  1568. static int
  1569. linked_attrs_del_post_op(Slapi_PBlock *pb)
  1570. {
  1571. char *dn = NULL;
  1572. Slapi_Entry *e = NULL;
  1573. int rc = SLAPI_PLUGIN_SUCCESS;
  1574. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1575. "--> linked_attrs_del_post_op\n");
  1576. /* Just bail if we aren't ready to service requests yet. */
  1577. if (!linked_attrs_oktodo(pb)){
  1578. return rc;
  1579. }
  1580. /* Reload config if a config entry was deleted. */
  1581. if ((dn = linked_attrs_get_dn(pb))) {
  1582. if (linked_attrs_dn_is_config(dn))
  1583. linked_attrs_load_config();
  1584. } else {
  1585. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1586. "linked_attrs_del_post_op: Error "
  1587. "retrieving dn\n");
  1588. }
  1589. /* Get deleted entry, then go through types to find config. */
  1590. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e );
  1591. if (e) {
  1592. Slapi_Attr *attr = NULL;
  1593. char *type = NULL;
  1594. struct configEntry *config = NULL;
  1595. slapi_entry_first_attr(e, &attr);
  1596. while (attr) {
  1597. slapi_attr_get_type(attr, &type);
  1598. /* See if there is an applicable link configured. */
  1599. linked_attrs_read_lock();
  1600. /* Bail out if the plug-in close function was just called. */
  1601. if (!slapi_plugin_running(pb)) {
  1602. linked_attrs_unlock();
  1603. return rc;
  1604. }
  1605. linked_attrs_find_config(dn, type, &config);
  1606. /* If config was found, delete the backpointers to this entry. */
  1607. if (config) {
  1608. Slapi_ValueSet *vals = NULL;
  1609. slapi_attr_get_valueset(attr, &vals);
  1610. slapi_lock_mutex(config->lock);
  1611. rc = linked_attrs_mod_backpointers(pb, dn, config->managedtype,
  1612. config->scope, LDAP_MOD_DELETE, vals);
  1613. slapi_unlock_mutex(config->lock);
  1614. slapi_valueset_free(vals);
  1615. if (rc != LDAP_SUCCESS){
  1616. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1617. "linked_attrs_del_post_op - update failed (%d)\n",rc);
  1618. linked_attrs_unlock();
  1619. break;
  1620. }
  1621. }
  1622. config = NULL;
  1623. /* See if any of the values for this attribute are managed
  1624. * backpointers. We need to remove the forward link if so. */
  1625. if (linked_attrs_config_index_has_type(type)) {
  1626. int hint = 0;
  1627. Slapi_Value *val = NULL;
  1628. /* Loop through values and see if we have matching config */
  1629. hint = slapi_attr_first_value(attr, &val);
  1630. while (val) {
  1631. linked_attrs_find_config_reverse(slapi_value_get_string(val),
  1632. type, &config);
  1633. if (config) {
  1634. Slapi_ValueSet *vals = slapi_valueset_new();
  1635. slapi_valueset_add_value(vals, val);
  1636. slapi_lock_mutex(config->lock);
  1637. /* Delete forward link value. */
  1638. rc = linked_attrs_mod_backpointers(pb, dn, config->linktype,
  1639. config->scope, LDAP_MOD_DELETE, vals);
  1640. slapi_unlock_mutex(config->lock);
  1641. slapi_valueset_free(vals);
  1642. config = NULL;
  1643. if(rc != LDAP_SUCCESS){
  1644. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1645. "linked_attrs_del_post_op: update failed (%d)\n",rc);
  1646. linked_attrs_unlock();
  1647. goto bail;
  1648. }
  1649. }
  1650. hint = slapi_attr_next_value(attr, hint, &val);
  1651. }
  1652. }
  1653. linked_attrs_unlock();
  1654. slapi_entry_next_attr(e, attr, &attr);
  1655. }
  1656. } else {
  1657. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1658. "linked_attrs_del_post_op: Error "
  1659. "retrieving pre-op entry %s\n", dn);
  1660. rc = SLAPI_PLUGIN_FAILURE;
  1661. }
  1662. bail:
  1663. if (rc) {
  1664. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1665. rc = SLAPI_PLUGIN_FAILURE;
  1666. }
  1667. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1668. "<-- linked_attrs_del_post_op\n");
  1669. return rc;
  1670. }
  1671. static int
  1672. linked_attrs_modrdn_post_op(Slapi_PBlock *pb)
  1673. {
  1674. char *old_dn = NULL;
  1675. char *new_dn = NULL;
  1676. Slapi_Entry *post_e = NULL;
  1677. Slapi_Attr *attr = NULL;
  1678. char *type = NULL;
  1679. struct configEntry *config = NULL;
  1680. int rc = SLAPI_PLUGIN_SUCCESS;
  1681. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1682. "--> linked_attrs_modrdn_post_op\n");
  1683. /* Just bail if we aren't ready to service requests yet. */
  1684. if (!linked_attrs_oktodo(pb)) {
  1685. goto done;
  1686. }
  1687. /* Reload config if an existing config entry was renamed,
  1688. * or if the new dn brings an entry into the scope of the
  1689. * config entries. */
  1690. slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &post_e);
  1691. if (post_e) {
  1692. new_dn = slapi_entry_get_ndn(post_e);
  1693. } else {
  1694. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1695. "linked_attrs_modrdn_post_op: Error "
  1696. "retrieving post-op entry\n");
  1697. rc = LDAP_OPERATIONS_ERROR;
  1698. goto done;
  1699. }
  1700. if ((old_dn = linked_attrs_get_dn(pb))) {
  1701. if (linked_attrs_dn_is_config(old_dn) || linked_attrs_dn_is_config(new_dn))
  1702. linked_attrs_load_config();
  1703. } else {
  1704. slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  1705. "linked_attrs_modrdn_post_op: Error "
  1706. "retrieving dn\n");
  1707. rc = LDAP_OPERATIONS_ERROR;
  1708. goto done;
  1709. }
  1710. /* Check if this operation requires any updates to links. */
  1711. slapi_entry_first_attr(post_e, &attr);
  1712. while (attr) {
  1713. slapi_attr_get_type(attr, &type);
  1714. /* See if there is an applicable link configured. */
  1715. linked_attrs_read_lock();
  1716. /* Bail out if the plug-in close function was just called. */
  1717. if (!slapi_plugin_running(pb)) {
  1718. linked_attrs_unlock();
  1719. return SLAPI_PLUGIN_SUCCESS;
  1720. }
  1721. linked_attrs_find_config(old_dn, type, &config);
  1722. /* If config was found for the old dn, delete the backpointers
  1723. * to this entry. */
  1724. if (config) {
  1725. Slapi_ValueSet *vals = NULL;
  1726. slapi_attr_get_valueset(attr, &vals);
  1727. slapi_lock_mutex(config->lock);
  1728. /* Delete old dn value. */
  1729. rc = linked_attrs_mod_backpointers(pb, old_dn, config->managedtype,
  1730. config->scope, LDAP_MOD_DELETE, vals);
  1731. slapi_unlock_mutex(config->lock);
  1732. slapi_valueset_free(vals);
  1733. config = NULL;
  1734. if(rc != LDAP_SUCCESS){
  1735. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1736. "linked_attrs_modrdn_post_op: update failed(old type) (%d)\n",rc);
  1737. linked_attrs_unlock();
  1738. break;
  1739. }
  1740. }
  1741. linked_attrs_find_config(new_dn, type, &config);
  1742. /* If config was found for the new dn, add the backpointers
  1743. * to this entry. We do this separate check for both dn's
  1744. * to catch an entry that comes into or goes out of scope
  1745. * from the MODRDN operation. */
  1746. if (config) {
  1747. Slapi_ValueSet *vals = NULL;
  1748. slapi_attr_get_valueset(attr, &vals);
  1749. slapi_lock_mutex(config->lock);
  1750. /* Add new dn value. */
  1751. rc = linked_attrs_mod_backpointers(pb, new_dn, config->managedtype,
  1752. config->scope, LDAP_MOD_ADD, vals);
  1753. slapi_unlock_mutex(config->lock);
  1754. slapi_valueset_free(vals);
  1755. config = NULL;
  1756. if(rc != LDAP_SUCCESS){
  1757. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1758. "linked_attrs_modrdn_post_op: update failed(new type) (%d)\n",rc);
  1759. linked_attrs_unlock();
  1760. break;
  1761. }
  1762. }
  1763. /* See if any of the values for this attribute are managed
  1764. * backpointers. We need to update the forward link if so. */
  1765. if (linked_attrs_config_index_has_type(type)) {
  1766. int hint = 0;
  1767. Slapi_Value *val = NULL;
  1768. /* Loop through values and see if we have matching config */
  1769. hint = slapi_attr_first_value(attr, &val);
  1770. while (val) {
  1771. linked_attrs_find_config_reverse(slapi_value_get_string(val),
  1772. type, &config);
  1773. /* If the new DN is within scope, we should fixup the forward links. */
  1774. if (config && slapi_dn_issuffix(new_dn, (config->scope))) {
  1775. Slapi_ValueSet *vals = slapi_valueset_new();
  1776. slapi_valueset_add_value(vals, val);
  1777. slapi_lock_mutex(config->lock);
  1778. /* Delete old dn value. */
  1779. rc = linked_attrs_mod_backpointers(pb, old_dn, config->linktype,
  1780. config->scope, LDAP_MOD_DELETE, vals);
  1781. if(rc != LDAP_SUCCESS){
  1782. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1783. "linked_attrs_modrdn_post_op: update failed(old dn) (%d)\n",rc);
  1784. slapi_unlock_mutex(config->lock);
  1785. slapi_valueset_free(vals);
  1786. linked_attrs_unlock();
  1787. goto done;
  1788. }
  1789. /* Add new dn value. */
  1790. rc = linked_attrs_mod_backpointers(pb, new_dn, config->linktype,
  1791. config->scope, LDAP_MOD_ADD, vals);
  1792. slapi_unlock_mutex(config->lock);
  1793. slapi_valueset_free(vals);
  1794. config = NULL;
  1795. if(rc != LDAP_SUCCESS){
  1796. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1797. "linked_attrs_modrdn_post_op: update failed(new dn) (%d)\n",rc);
  1798. linked_attrs_unlock();
  1799. goto done;
  1800. }
  1801. }
  1802. hint = slapi_attr_next_value(attr, hint, &val);
  1803. }
  1804. }
  1805. linked_attrs_unlock();
  1806. slapi_entry_next_attr(post_e, attr, &attr);
  1807. }
  1808. done:
  1809. slapi_log_error(SLAPI_LOG_TRACE, LINK_PLUGIN_SUBSYSTEM,
  1810. "<-- linked_attrs_modrdn_post_op\n");
  1811. if (rc) {
  1812. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &rc);
  1813. rc = SLAPI_PLUGIN_FAILURE;
  1814. }
  1815. return rc;
  1816. }
  1817. /*
  1818. * Debug functions to print config
  1819. */
  1820. void
  1821. linked_attrs_dump_config()
  1822. {
  1823. PRCList *list;
  1824. linked_attrs_read_lock();
  1825. if (!PR_CLIST_IS_EMPTY(g_link_config)) {
  1826. list = PR_LIST_HEAD(g_link_config);
  1827. while (list != g_link_config) {
  1828. linked_attrs_dump_config_entry((struct configEntry *)list);
  1829. list = PR_NEXT_LINK(list);
  1830. }
  1831. }
  1832. linked_attrs_unlock();
  1833. }
  1834. void
  1835. linked_attrs_dump_config_index()
  1836. {
  1837. PRCList *list;
  1838. linked_attrs_read_lock();
  1839. if (!PR_CLIST_IS_EMPTY(g_managed_config_index)) {
  1840. list = PR_LIST_HEAD(g_managed_config_index);
  1841. while (list != g_managed_config_index) {
  1842. linked_attrs_dump_config_entry(((struct configIndex *)list)->config);
  1843. list = PR_NEXT_LINK(list);
  1844. }
  1845. }
  1846. linked_attrs_unlock();
  1847. }
  1848. void
  1849. linked_attrs_dump_config_entry(struct configEntry * entry)
  1850. {
  1851. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1852. "<==== Linked Attribute Pair =====>\n");
  1853. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1854. "<---- config entry dn -----> %s\n", entry->dn);
  1855. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1856. "<---- link type -----------> %s\n", entry->linktype);
  1857. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1858. "<---- managed type --------> %s\n", entry->managedtype);
  1859. slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
  1860. "<---- scope ---------------> %s\n", entry->scope);
  1861. }