linked_attrs.c 66 KB

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