linked_attrs.c 69 KB

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