linked_attrs.c 66 KB

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