linked_attrs.c 70 KB

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