fixup_task.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. #include <config.h>
  10. #endif
  11. /* fixup_task.c - linked attributes fixup task */
  12. #include "linked_attrs.h"
  13. /*
  14. * Function Prototypes
  15. */
  16. static void linked_attrs_fixup_task_destructor(Slapi_Task *task);
  17. static void linked_attrs_fixup_task_thread(void *arg);
  18. static void linked_attrs_fixup_links(struct configEntry *config);
  19. static int linked_attrs_remove_backlinks_callback(Slapi_Entry *e, void *callback_data);
  20. static int linked_attrs_add_backlinks_callback(Slapi_Entry *e, void *callback_data);
  21. /*
  22. * Function Implementations
  23. */
  24. int
  25. linked_attrs_fixup_task_add(Slapi_PBlock *pb,
  26. Slapi_Entry *e,
  27. Slapi_Entry *eAfter __attribute__((unused)),
  28. int *returncode,
  29. char *returntext __attribute__((unused)),
  30. void *arg)
  31. {
  32. PRThread *thread = NULL;
  33. int rv = SLAPI_DSE_CALLBACK_OK;
  34. task_data *mytaskdata = NULL;
  35. Slapi_Task *task = NULL;
  36. const char *linkdn = NULL;
  37. char *bind_dn;
  38. *returncode = LDAP_SUCCESS;
  39. mytaskdata = (task_data *)slapi_ch_calloc(1, sizeof(task_data));
  40. if (mytaskdata == NULL) {
  41. *returncode = LDAP_OPERATIONS_ERROR;
  42. rv = SLAPI_DSE_CALLBACK_ERROR;
  43. goto out;
  44. }
  45. /* get arg(s) and setup our task data */
  46. linkdn = fetch_attr(e, "linkdn", 0);
  47. if (linkdn) {
  48. mytaskdata->linkdn = slapi_dn_normalize(slapi_ch_strdup(linkdn));
  49. }
  50. slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
  51. mytaskdata->bind_dn = slapi_ch_strdup(bind_dn);
  52. /* allocate new task now */
  53. task = slapi_plugin_new_task(slapi_entry_get_ndn(e), arg);
  54. /* register our destructor for cleaning up our private data */
  55. slapi_task_set_destructor_fn(task, linked_attrs_fixup_task_destructor);
  56. /* Stash a pointer to our data in the task */
  57. slapi_task_set_data(task, mytaskdata);
  58. /* start the sample task as a separate thread */
  59. thread = PR_CreateThread(PR_USER_THREAD, linked_attrs_fixup_task_thread,
  60. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  61. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  62. if (thread == NULL) {
  63. slapi_log_err(SLAPI_LOG_ERR, LINK_PLUGIN_SUBSYSTEM,
  64. "linked_attrs_fixup_task_add - Unable to create task thread!\n");
  65. *returncode = LDAP_OPERATIONS_ERROR;
  66. slapi_task_finish(task, *returncode);
  67. rv = SLAPI_DSE_CALLBACK_ERROR;
  68. } else {
  69. rv = SLAPI_DSE_CALLBACK_OK;
  70. }
  71. out:
  72. return rv;
  73. }
  74. static void
  75. linked_attrs_fixup_task_destructor(Slapi_Task *task)
  76. {
  77. if (task) {
  78. task_data *mydata = (task_data *)slapi_task_get_data(task);
  79. while (slapi_task_get_refcount(task) > 0) {
  80. /* Yield to wait for the fixup task finishes. */
  81. DS_Sleep(PR_MillisecondsToInterval(100));
  82. }
  83. if (mydata) {
  84. slapi_ch_free_string(&mydata->linkdn);
  85. slapi_ch_free_string(&mydata->bind_dn);
  86. /* Need to cast to avoid a compiler warning */
  87. slapi_ch_free((void **)&mydata);
  88. }
  89. }
  90. }
  91. static void
  92. linked_attrs_fixup_task_thread(void *arg)
  93. {
  94. Slapi_Task *task = (Slapi_Task *)arg;
  95. task_data *td = NULL;
  96. PRCList *main_config = NULL;
  97. int found_config = 0;
  98. int rc = 0;
  99. if (!task) {
  100. return; /* no task */
  101. }
  102. slapi_task_inc_refcount(task);
  103. slapi_log_err(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  104. "linked_attrs_fixup_task_thread --> refcount incremented.\n");
  105. /* Fetch our task data from the task */
  106. td = (task_data *)slapi_task_get_data(task);
  107. /* init and set the bind dn in the thread data */
  108. slapi_td_set_dn(slapi_ch_strdup(td->bind_dn));
  109. /* Log started message. */
  110. slapi_task_begin(task, 1);
  111. slapi_task_log_notice(task, "Linked attributes fixup task starting (link dn: \"%s\") ...\n",
  112. td->linkdn ? td->linkdn : "");
  113. slapi_log_err(SLAPI_LOG_INFO, LINK_PLUGIN_SUBSYSTEM,
  114. "linked_attrs_fixup_task_thread - Syntax validate task starting (link dn: \"%s\") ...\n",
  115. td->linkdn ? td->linkdn : "");
  116. linked_attrs_read_lock();
  117. main_config = linked_attrs_get_config();
  118. if (!PR_CLIST_IS_EMPTY(main_config)) {
  119. struct configEntry *config_entry = NULL;
  120. PRCList *list = PR_LIST_HEAD(main_config);
  121. while (list != main_config) {
  122. config_entry = (struct configEntry *)list;
  123. /* See if this is the requested config and fix up if so. */
  124. if (td->linkdn) {
  125. if (strcasecmp(td->linkdn, config_entry->dn) == 0) {
  126. found_config = 1;
  127. slapi_task_log_notice(task, "Fixing up linked attribute pair (%s)\n",
  128. config_entry->dn);
  129. slapi_log_err(SLAPI_LOG_INFO, LINK_PLUGIN_SUBSYSTEM,
  130. "linked_attrs_fixup_task_thread - Fixing up linked attribute pair (%s)\n", config_entry->dn);
  131. linked_attrs_fixup_links(config_entry);
  132. break;
  133. }
  134. } else {
  135. /* No config DN was supplied, so fix up all configured links. */
  136. slapi_task_log_notice(task, "Fixing up linked attribute pair (%s)\n",
  137. config_entry->dn);
  138. slapi_log_err(SLAPI_LOG_INFO, LINK_PLUGIN_SUBSYSTEM,
  139. "linked_attrs_fixup_task_thread - Fixing up linked attribute pair (%s)\n", config_entry->dn);
  140. linked_attrs_fixup_links(config_entry);
  141. }
  142. list = PR_NEXT_LINK(list);
  143. }
  144. }
  145. /* Log a message if we didn't find the requested attribute pair. */
  146. if (td->linkdn && !found_config) {
  147. slapi_task_log_notice(task, "Requested link config DN not found (%s)\n",
  148. td->linkdn);
  149. slapi_log_err(SLAPI_LOG_ERR, LINK_PLUGIN_SUBSYSTEM,
  150. "linked_attrs_fixup_task_thread - Requested link config DN not found (%s)\n", td->linkdn);
  151. }
  152. linked_attrs_unlock();
  153. /* Log finished message. */
  154. slapi_task_log_notice(task, "Linked attributes fixup task complete.");
  155. slapi_task_log_status(task, "Linked attributes fixup task complete.");
  156. slapi_log_err(SLAPI_LOG_INFO, LINK_PLUGIN_SUBSYSTEM, "linked_attrs_fixup_task_thread - Linked attributes fixup task complete.\n");
  157. slapi_task_inc_progress(task);
  158. /* this will queue the destruction of the task */
  159. slapi_task_finish(task, rc);
  160. slapi_task_dec_refcount(task);
  161. slapi_log_err(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  162. "linked_attrs_fixup_task_thread <-- refcount decremented.\n");
  163. }
  164. static void
  165. linked_attrs_fixup_links(struct configEntry *config)
  166. {
  167. Slapi_PBlock *pb = slapi_pblock_new();
  168. Slapi_PBlock *fixup_pb = NULL;
  169. char *del_filter = NULL;
  170. char *add_filter = NULL;
  171. int rc = 0;
  172. del_filter = slapi_ch_smprintf("%s=*", config->managedtype);
  173. add_filter = slapi_ch_smprintf("%s=*", config->linktype);
  174. /* Lock the attribute pair. */
  175. slapi_lock_mutex(config->lock);
  176. if (config->scope) {
  177. /*
  178. * If this is a backend txn plugin, start the transaction
  179. */
  180. if (plugin_is_betxn) {
  181. Slapi_DN *fixup_dn = slapi_sdn_new_dn_byref(config->scope);
  182. Slapi_Backend *be = slapi_be_select(fixup_dn);
  183. slapi_sdn_free(&fixup_dn);
  184. if (be) {
  185. fixup_pb = slapi_pblock_new();
  186. slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
  187. if (slapi_back_transaction_begin(fixup_pb) != LDAP_SUCCESS) {
  188. slapi_log_err(SLAPI_LOG_ERR, LINK_PLUGIN_SUBSYSTEM,
  189. "linked_attrs_fixup_links - Failed to start transaction\n");
  190. }
  191. } else {
  192. slapi_log_err(SLAPI_LOG_ERR, LINK_PLUGIN_SUBSYSTEM,
  193. "linked_attrs_fixup_link - Failed to get be backend from %s\n",
  194. config->scope);
  195. }
  196. }
  197. /* Find all entries with the managed type present
  198. * within the scope and remove the managed type. */
  199. slapi_search_internal_set_pb(pb, config->scope, LDAP_SCOPE_SUBTREE,
  200. del_filter, 0, 0, 0, 0, linked_attrs_get_plugin_id(), 0);
  201. rc = slapi_search_internal_callback_pb(pb, config->managedtype, 0,
  202. linked_attrs_remove_backlinks_callback, 0);
  203. /* Clean out pblock for reuse. */
  204. slapi_pblock_init(pb);
  205. /* Find all entries with the link type present within the
  206. * scope and add backlinks to the entries they point to. */
  207. slapi_search_internal_set_pb(pb, config->scope, LDAP_SCOPE_SUBTREE,
  208. add_filter, 0, 0, 0, 0, linked_attrs_get_plugin_id(), 0);
  209. slapi_search_internal_callback_pb(pb, config, 0,
  210. linked_attrs_add_backlinks_callback, 0);
  211. /*
  212. * Finish the transaction.
  213. */
  214. if (plugin_is_betxn && fixup_pb) {
  215. if (rc == 0) {
  216. slapi_back_transaction_commit(fixup_pb);
  217. } else {
  218. slapi_back_transaction_abort(fixup_pb);
  219. }
  220. slapi_pblock_destroy(fixup_pb);
  221. }
  222. } else {
  223. /* Loop through all non-private backend suffixes and
  224. * remove the managed type from any entry that has it.
  225. * We then find any entry with the linktype present and
  226. * generate the proper backlinks. */
  227. void *node = NULL;
  228. config->suffix = slapi_get_first_suffix(&node, 0);
  229. while (config->suffix) {
  230. /*
  231. * If this is a backend txn plugin, start the transaction
  232. */
  233. if (plugin_is_betxn) {
  234. Slapi_Backend *be = slapi_be_select(config->suffix);
  235. if (be) {
  236. fixup_pb = slapi_pblock_new();
  237. slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
  238. if (slapi_back_transaction_begin(fixup_pb) != LDAP_SUCCESS) {
  239. slapi_log_err(SLAPI_LOG_ERR, LINK_PLUGIN_SUBSYSTEM,
  240. "linked_attrs_fixup_links: failed to start transaction\n");
  241. }
  242. } else {
  243. slapi_log_err(SLAPI_LOG_ERR, LINK_PLUGIN_SUBSYSTEM,
  244. "linked_attrs_fixup_links: failed to get be backend from %s\n",
  245. slapi_sdn_get_dn(config->suffix));
  246. }
  247. }
  248. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(config->suffix),
  249. LDAP_SCOPE_SUBTREE, del_filter,
  250. 0, 0, 0, 0,
  251. linked_attrs_get_plugin_id(), 0);
  252. slapi_search_internal_callback_pb(pb, config->managedtype, 0,
  253. linked_attrs_remove_backlinks_callback, 0);
  254. /* Clean out pblock for reuse. */
  255. slapi_pblock_init(pb);
  256. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(config->suffix),
  257. LDAP_SCOPE_SUBTREE, add_filter,
  258. 0, 0, 0, 0,
  259. linked_attrs_get_plugin_id(), 0);
  260. rc = slapi_search_internal_callback_pb(pb, config, 0,
  261. linked_attrs_add_backlinks_callback, 0);
  262. /* Clean out pblock for reuse. */
  263. slapi_pblock_init(pb);
  264. config->suffix = slapi_get_next_suffix(&node, 0);
  265. /*
  266. * Finish the transaction.
  267. */
  268. if (plugin_is_betxn && fixup_pb) {
  269. if (rc == 0) {
  270. slapi_back_transaction_commit(fixup_pb);
  271. } else {
  272. slapi_back_transaction_abort(fixup_pb);
  273. }
  274. slapi_pblock_destroy(fixup_pb);
  275. }
  276. }
  277. }
  278. /* Unlock the attribute pair. */
  279. slapi_unlock_mutex(config->lock);
  280. slapi_ch_free_string(&del_filter);
  281. slapi_ch_free_string(&add_filter);
  282. slapi_pblock_destroy(pb);
  283. }
  284. static int
  285. linked_attrs_remove_backlinks_callback(Slapi_Entry *e, void *callback_data)
  286. {
  287. int rc = 0;
  288. Slapi_DN *sdn = slapi_entry_get_sdn(e);
  289. char *type = (char *)callback_data;
  290. Slapi_PBlock *pb = NULL;
  291. char *val[1];
  292. LDAPMod mod;
  293. LDAPMod *mods[2];
  294. /*
  295. * If the server is ordered to shutdown, stop the fixup and return an error.
  296. */
  297. if (slapi_is_shutting_down()) {
  298. rc = -1;
  299. goto bail;
  300. }
  301. pb = slapi_pblock_new();
  302. /* Remove all values of the passed in type. */
  303. val[0] = 0;
  304. mod.mod_op = LDAP_MOD_DELETE;
  305. mod.mod_type = type;
  306. mod.mod_values = val;
  307. mods[0] = &mod;
  308. mods[1] = 0;
  309. slapi_log_err(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  310. "linked_attrs_remove_backlinks_callback - Removing backpointer attribute (%s) from entry (%s)\n",
  311. type, slapi_sdn_get_dn(sdn));
  312. /* Perform the operation. */
  313. slapi_modify_internal_set_pb_ext(pb, sdn, mods, 0, 0,
  314. linked_attrs_get_plugin_id(), 0);
  315. slapi_modify_internal_pb(pb);
  316. slapi_pblock_destroy(pb);
  317. bail:
  318. return rc;
  319. }
  320. static int
  321. linked_attrs_add_backlinks_callback(Slapi_Entry *e, void *callback_data)
  322. {
  323. int rc = 0;
  324. char *linkdn = slapi_entry_get_dn(e);
  325. struct configEntry *config = (struct configEntry *)callback_data;
  326. Slapi_PBlock *pb = slapi_pblock_new();
  327. int i = 0;
  328. char **targets = NULL;
  329. char *val[2];
  330. LDAPMod mod;
  331. LDAPMod *mods[2];
  332. /*
  333. * If the server is ordered to shutdown, stop the fixup and return an error.
  334. */
  335. if (slapi_is_shutting_down()) {
  336. rc = -1;
  337. goto done;
  338. }
  339. /* Setup the modify operation. Only the target will
  340. * change, so we only need to do this once. */
  341. val[0] = linkdn;
  342. val[1] = 0;
  343. mod.mod_op = LDAP_MOD_ADD;
  344. mod.mod_type = config->managedtype;
  345. mod.mod_values = val;
  346. mods[0] = &mod;
  347. mods[1] = 0;
  348. targets = slapi_entry_attr_get_charray(e, config->linktype);
  349. for (i = 0; targets && targets[i]; ++i) {
  350. char *targetdn = (char *)targets[i];
  351. int perform_update = 0;
  352. Slapi_DN *targetsdn = NULL;
  353. if (slapi_is_shutting_down()) {
  354. rc = -1;
  355. goto done;
  356. }
  357. targetsdn = slapi_sdn_new_normdn_byref(targetdn);
  358. if (config->scope) {
  359. /* Check if the target is within the scope. */
  360. perform_update = slapi_dn_issuffix(targetdn, config->scope);
  361. } else {
  362. /* Find out the root suffix that the linkdn is in
  363. * and see if the target is in the same backend. */
  364. Slapi_Backend *be = NULL;
  365. Slapi_DN *linksdn = slapi_sdn_new_normdn_byref(linkdn);
  366. if ((be = slapi_be_select(linksdn))) {
  367. perform_update = slapi_sdn_issuffix(targetsdn, slapi_be_getsuffix(be, 0));
  368. }
  369. slapi_sdn_free(&linksdn);
  370. }
  371. if (perform_update) {
  372. slapi_log_err(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
  373. "linked_attrs_add_backlinks_callback - Adding backpointer (%s) in entry (%s)\n",
  374. linkdn, targetdn);
  375. /* Perform the modify operation. */
  376. slapi_modify_internal_set_pb_ext(pb, targetsdn, mods, 0, 0,
  377. linked_attrs_get_plugin_id(), 0);
  378. slapi_modify_internal_pb(pb);
  379. /* Initialize the pblock so we can reuse it. */
  380. slapi_pblock_init(pb);
  381. }
  382. slapi_sdn_free(&targetsdn);
  383. }
  384. done:
  385. slapi_ch_array_free(targets);
  386. slapi_pblock_destroy(pb);
  387. return rc;
  388. }