fixup_task.c 15 KB

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