linked_attrs.c 68 KB

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