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