linked_attrs.c 71 KB

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