fixup_task.c 18 KB

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