linked_attrs.c 67 KB

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