linked_attrs.c 70 KB

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