linked_attrs.c 69 KB

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