fixup_task.c 17 KB

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