linked_attrs.c 73 KB

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