123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- /** BEGIN COPYRIGHT BLOCK
- * This Program is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation; version 2 of the License.
- *
- * This Program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA.
- *
- * In addition, as a special exception, Red Hat, Inc. gives You the additional
- * right to link the code of this Program with code not covered under the GNU
- * General Public License ("Non-GPL Code") and to distribute linked combinations
- * including the two, subject to the limitations in this paragraph. Non-GPL Code
- * permitted under this exception must only link to the code of this Program
- * through those well defined interfaces identified in the file named EXCEPTION
- * found in the source code files (the "Approved Interfaces"). The files of
- * Non-GPL Code may instantiate templates or use macros or inline functions from
- * the Approved Interfaces without causing the resulting work to be covered by
- * the GNU General Public License. Only Red Hat, Inc. may make changes or
- * additions to the list of Approved Interfaces. You must obey the GNU General
- * Public License in all respects for all of the Program code and other code used
- * in conjunction with the Program except the Non-GPL Code covered by this
- * exception. If you modify this file, you may extend this exception to your
- * version of the file, but you are not obligated to do so. If you do not wish to
- * provide this exception without modification, you must delete this exception
- * statement from your version and license this file solely under the GPL without
- * exception.
- *
- *
- * Copyright (C) 2009 Red Hat, Inc.
- * All rights reserved.
- * END COPYRIGHT BLOCK **/
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- /* fixup_task.c - linked attributes fixup task */
- #include "linked_attrs.h"
- /*
- * Function Prototypes
- */
- static void linked_attrs_fixup_task_destructor(Slapi_Task *task);
- static void linked_attrs_fixup_task_thread(void *arg);
- static void linked_attrs_fixup_links(struct configEntry *config, void *txn);
- static int linked_attrs_remove_backlinks_callback(Slapi_Entry *e, void *callback_data);
- static int linked_attrs_add_backlinks_callback(Slapi_Entry *e, void *callback_data);
- static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
- const char *default_val);
- /*
- * Function Implementations
- */
- int
- linked_attrs_fixup_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
- Slapi_Entry *eAfter, int *returncode,
- char *returntext, void *arg)
- {
- PRThread *thread = NULL;
- int rv = SLAPI_DSE_CALLBACK_OK;
- task_data *mytaskdata = NULL;
- Slapi_Task *task = NULL;
- const char *linkdn = NULL;
- *returncode = LDAP_SUCCESS;
- /* make sure the plugin is not closed */
- if(!linked_attrs_is_started()){
- *returncode = LDAP_OPERATIONS_ERROR;
- rv = SLAPI_DSE_CALLBACK_ERROR;
- goto out;
- }
- /* get arg(s) */
- linkdn = fetch_attr(e, "linkdn", 0);
- /* setup our task data */
- mytaskdata = (task_data*)slapi_ch_calloc(1, sizeof(task_data));
- if (mytaskdata == NULL) {
- *returncode = LDAP_OPERATIONS_ERROR;
- rv = SLAPI_DSE_CALLBACK_ERROR;
- goto out;
- }
- if (linkdn) {
- mytaskdata->linkdn = slapi_dn_normalize(slapi_ch_strdup(linkdn));
- }
- /* allocate new task now */
- task = slapi_new_task(slapi_entry_get_ndn(e));
- /* register our destructor for cleaning up our private data */
- slapi_task_set_destructor_fn(task, linked_attrs_fixup_task_destructor);
- /* Stash a pointer to our data in the task */
- slapi_task_set_data(task, mytaskdata);
- /* start the sample task as a separate thread */
- thread = PR_CreateThread(PR_USER_THREAD, linked_attrs_fixup_task_thread,
- (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
- PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
- if (thread == NULL) {
- slapi_log_error( SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
- "unable to create task thread!\n");
- *returncode = LDAP_OPERATIONS_ERROR;
- rv = SLAPI_DSE_CALLBACK_ERROR;
- slapi_task_finish(task, *returncode);
- } else {
- rv = SLAPI_DSE_CALLBACK_OK;
- }
- out:
- return rv;
- }
- static void
- linked_attrs_fixup_task_destructor(Slapi_Task *task)
- {
- if (task) {
- task_data *mydata = (task_data *)slapi_task_get_data(task);
- if (mydata) {
- slapi_ch_free_string(&mydata->linkdn);
- /* Need to cast to avoid a compiler warning */
- slapi_ch_free((void **)&mydata);
- }
- }
- }
- static void
- linked_attrs_fixup_task_thread(void *arg)
- {
- int rc = 0;
- Slapi_Task *task = (Slapi_Task *)arg;
- task_data *td = NULL;
- PRCList *main_config = NULL;
- int found_config = 0;
- /* Fetch our task data from the task */
- td = (task_data *)slapi_task_get_data(task);
- /* Log started message. */
- slapi_task_begin(task, 1);
- slapi_task_log_notice(task, "Linked attributes fixup task starting (link dn: \"%s\") ...\n",
- td->linkdn ? td->linkdn : "");
- slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
- "Syntax validate task starting (link dn: \"%s\") ...\n",
- td->linkdn ? td->linkdn : "");
- linked_attrs_read_lock();
- main_config = linked_attrs_get_config();
- if (!PR_CLIST_IS_EMPTY(main_config)) {
- struct configEntry *config_entry = NULL;
- PRCList *list = PR_LIST_HEAD(main_config);
- while (list != main_config) {
- config_entry = (struct configEntry *) list;
- /* See if this is the requested config and fix up if so. */
- if (td->linkdn) {
- if (strcasecmp(td->linkdn, config_entry->dn) == 0) {
- found_config = 1;
- slapi_task_log_notice(task, "Fixing up linked attribute pair (%s)\n",
- config_entry->dn);
- slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
- "Fixing up linked attribute pair (%s)\n", config_entry->dn);
- linked_attrs_fixup_links(config_entry, NULL);
- break;
- }
- } else {
- /* No config DN was supplied, so fix up all configured links. */
- slapi_task_log_notice(task, "Fixing up linked attribute pair (%s)\n",
- config_entry->dn);
- slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
- "Fixing up linked attribute pair (%s)\n", config_entry->dn);
- linked_attrs_fixup_links(config_entry, NULL);
- }
- list = PR_NEXT_LINK(list);
- }
- }
- /* Log a message if we didn't find the requested attribute pair. */
- if (td->linkdn && !found_config) {
- slapi_task_log_notice(task, "Requested link config DN not found (%s)\n",
- td->linkdn);
- slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM,
- "Requested link config DN not found (%s)\n", td->linkdn);
- }
- linked_attrs_unlock();
- /* Log finished message. */
- slapi_task_log_notice(task, "Linked attributes fixup task complete.\n");
- slapi_task_log_status(task, "Linked attributes fixup task complete.\n");
- slapi_log_error(SLAPI_LOG_FATAL, LINK_PLUGIN_SUBSYSTEM, "Linked attributes fixup task complete.\n");
- slapi_task_inc_progress(task);
- /* this will queue the destruction of the task */
- slapi_task_finish(task, rc);
- }
- struct fixup_cb_data {
- char *attrtype;
- void *txn;
- struct configEntry *config;
- };
- static void
- linked_attrs_fixup_links(struct configEntry *config, void *txn)
- {
- Slapi_PBlock *pb = slapi_pblock_new();
- char *del_filter = NULL;
- char *add_filter = NULL;
- struct fixup_cb_data cb_data = {NULL, NULL, NULL};
- del_filter = slapi_ch_smprintf("%s=*", config->managedtype);
- add_filter = slapi_ch_smprintf("%s=*", config->linktype);
- /* Lock the attribute pair. */
- slapi_lock_mutex(config->lock);
- if (config->scope) {
- /* Find all entries with the managed type present
- * within the scope and remove the managed type. */
- slapi_search_internal_set_pb(pb, config->scope, LDAP_SCOPE_SUBTREE,
- del_filter, 0, 0, 0, 0, linked_attrs_get_plugin_id(), 0);
- slapi_pblock_set(pb, SLAPI_TXN, txn);
- cb_data.attrtype = config->managedtype;
- cb_data.txn = txn;
- slapi_search_internal_callback_pb(pb, &cb_data, 0,
- linked_attrs_remove_backlinks_callback, 0);
- /* Clean out pblock for reuse. */
- slapi_pblock_init(pb);
- /* Find all entries with the link type present within the
- * scope and add backlinks to the entries they point to. */
- slapi_search_internal_set_pb(pb, config->scope, LDAP_SCOPE_SUBTREE,
- add_filter, 0, 0, 0, 0, linked_attrs_get_plugin_id(), 0);
- slapi_pblock_set(pb, SLAPI_TXN, txn);
- cb_data.attrtype = NULL;
- cb_data.txn = txn;
- cb_data.config = config;
- slapi_search_internal_callback_pb(pb, &cb_data, 0,
- linked_attrs_add_backlinks_callback, 0);
- } else {
- /* Loop through all non-private backend suffixes and
- * remove the managed type from any entry that has it.
- * We then find any entry with the linktype present and
- * generate the proper backlinks. */
- void *node = NULL;
- Slapi_DN * suffix = slapi_get_first_suffix (&node, 0);
- while (suffix) {
- slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(suffix),
- LDAP_SCOPE_SUBTREE, del_filter,
- 0, 0, 0, 0,
- linked_attrs_get_plugin_id(), 0);
- slapi_pblock_set(pb, SLAPI_TXN, txn);
- cb_data.attrtype = config->managedtype;
- cb_data.txn = txn;
- slapi_search_internal_callback_pb(pb, config->managedtype, 0,
- linked_attrs_remove_backlinks_callback, 0);
- /* Clean out pblock for reuse. */
- slapi_pblock_init(pb);
- slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(suffix),
- LDAP_SCOPE_SUBTREE, add_filter,
- 0, 0, 0, 0,
- linked_attrs_get_plugin_id(), 0);
- slapi_pblock_set(pb, SLAPI_TXN, txn);
- cb_data.attrtype = NULL;
- cb_data.txn = txn;
- cb_data.config = config;
- slapi_search_internal_callback_pb(pb, &cb_data, 0,
- linked_attrs_add_backlinks_callback, 0);
- /* Clean out pblock for reuse. */
- slapi_pblock_init(pb);
- suffix = slapi_get_next_suffix (&node, 0);
- }
- }
- /* Unlock the attribute pair. */
- slapi_unlock_mutex(config->lock);
- slapi_ch_free_string(&del_filter);
- slapi_ch_free_string(&add_filter);
- slapi_pblock_destroy(pb);
- }
- static int
- linked_attrs_remove_backlinks_callback(Slapi_Entry *e, void *callback_data)
- {
- int rc = 0;
- Slapi_DN *sdn = slapi_entry_get_sdn(e);
- struct fixup_cb_data *cb_data = (struct fixup_cb_data *)callback_data;
- char *type = cb_data->attrtype;
- Slapi_PBlock *pb = slapi_pblock_new();
- char *val[1];
- LDAPMod mod;
- LDAPMod *mods[2];
- /* Remove all values of the passed in type. */
- val[0] = 0;
- mod.mod_op = LDAP_MOD_DELETE;
- mod.mod_type = type;
- mod.mod_values = val;
- mods[0] = &mod;
- mods[1] = 0;
- slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
- "Removing backpointer attribute (%s) from entry (%s)\n",
- type, slapi_sdn_get_dn(sdn));
- /* Perform the operation. */
- slapi_modify_internal_set_pb_ext(pb, sdn, mods, 0, 0,
- linked_attrs_get_plugin_id(), 0);
- slapi_pblock_set(pb, SLAPI_TXN, cb_data->txn);
- slapi_modify_internal_pb(pb);
- slapi_pblock_destroy(pb);
- return rc;
- }
- static int
- linked_attrs_add_backlinks_callback(Slapi_Entry *e, void *callback_data)
- {
- int rc = 0;
- char *linkdn = slapi_entry_get_dn(e);
- struct fixup_cb_data *cb_data = (struct fixup_cb_data *)callback_data;
- struct configEntry *config = cb_data->config;
- Slapi_PBlock *pb = slapi_pblock_new();
- int i = 0;
- char **targets = NULL;
- char *val[2];
- LDAPMod mod;
- LDAPMod *mods[2];
- /* Setup the modify operation. Only the target will
- * change, so we only need to do this once. */
- val[0] = linkdn;
- val[1] = 0;
- mod.mod_op = LDAP_MOD_ADD;
- mod.mod_type = config->managedtype;
- mod.mod_values = val;
- mods[0] = &mod;
- mods[1] = 0;
- targets = slapi_entry_attr_get_charray(e, config->linktype);
- for (i = 0; targets && targets[i]; ++i) {
- char *targetdn = (char *)targets[i];
- int perform_update = 0;
- Slapi_DN *targetsdn = NULL;
- if (g_get_shutdown()) {
- rc = -1;
- goto done;
- }
- targetsdn = slapi_sdn_new_normdn_byref(targetdn);
- if (config->scope) {
- /* Check if the target is within the scope. */
- perform_update = slapi_dn_issuffix(targetdn, config->scope);
- } else {
- /* Find out the root suffix that the linkdn is in
- * and see if the target is in the same backend. */
- Slapi_Backend *be = NULL;
- Slapi_DN *linksdn = slapi_sdn_new_normdn_byref(linkdn);
- if ((be = slapi_be_select(linksdn))) {
- perform_update = slapi_sdn_issuffix(targetsdn, slapi_be_getsuffix(be, 0));
- }
- slapi_sdn_free(&linksdn);
- }
- if (perform_update) {
- slapi_log_error(SLAPI_LOG_PLUGIN, LINK_PLUGIN_SUBSYSTEM,
- "Adding backpointer (%s) in entry (%s)\n",
- linkdn, targetdn);
- /* Perform the modify operation. */
- slapi_modify_internal_set_pb_ext(pb, targetsdn, mods, 0, 0,
- linked_attrs_get_plugin_id(), 0);
- slapi_pblock_set(pb, SLAPI_TXN, cb_data->txn);
- slapi_modify_internal_pb(pb);
- /* Initialize the pblock so we can reuse it. */
- slapi_pblock_init(pb);
- }
- slapi_sdn_free(&targetsdn);
- }
- done:
- slapi_ch_array_free(targets);
- slapi_pblock_destroy(pb);
- return rc;
- }
- /* extract a single value from the entry (as a string) -- if it's not in the
- * entry, the default will be returned (which can be NULL).
- * you do not need to free anything returned by this.
- */
- static const char *
- fetch_attr(Slapi_Entry *e, const char *attrname,
- const char *default_val)
- {
- Slapi_Attr *attr;
- Slapi_Value *val = NULL;
- if (slapi_entry_attr_find(e, attrname, &attr) != 0) {
- return default_val;
- }
- slapi_attr_first_value(attr, &val);
- return slapi_value_get_string(val);
- }
|