4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
5 Copyright (C) Simo Sorce <idra@samba.org> 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Component: ldb linked_attributes module
26 * Description: Module to ensure linked attribute pairs remain in sync
28 * Author: Andrew Bartlett
32 #include "ldb/include/ldb.h"
33 #include "ldb/include/ldb_errors.h"
34 #include "ldb/include/ldb_private.h"
35 #include "dsdb/samdb/samdb.h"
38 struct la_op_store *next;
39 enum la_op {LA_OP_ADD, LA_OP_DEL} op;
45 struct replace_context {
46 struct la_context *ac;
47 unsigned int num_elements;
48 struct ldb_message_element *el;
52 const struct dsdb_schema *schema;
53 struct ldb_module *module;
54 struct ldb_request *req;
56 struct replace_context *rc;
57 struct la_op_store *ops;
58 struct la_op_store *cur;
61 static struct la_context *linked_attributes_init(struct ldb_module *module,
62 struct ldb_request *req)
64 struct la_context *ac;
66 ac = talloc_zero(req, struct la_context);
68 ldb_set_errstring(module->ldb, "Out of Memory");
72 ac->schema = dsdb_get_schema(module->ldb);
79 /* Common routine to handle reading the attributes and creating a
80 * series of modify requests */
81 static int la_store_op(struct la_context *ac,
82 enum la_op op, struct ldb_val *dn,
83 const char *name, const char *value)
85 struct la_op_store *os, *tmp;
88 op_dn = ldb_dn_from_ldb_val(ac, ac->module->ldb, dn);
90 return LDB_ERR_OPERATIONS_ERROR;
93 /* optimize out del - add operations that would end up
95 if (ac->ops && op == LA_OP_DEL) {
96 /* do a linear search to find out if there is
97 * an equivalent add */
102 if (tmp->op == LA_OP_ADD) {
104 if ((strcmp(name, tmp->name) == 0) &&
105 (strcmp(value, tmp->value) == 0) &&
106 (ldb_dn_compare(op_dn, tmp->dn) == 0)) {
114 /* pair found, remove it and return */
115 os->next = tmp->next;
122 os = talloc_zero(ac, struct la_op_store);
124 return LDB_ERR_OPERATIONS_ERROR;
129 os->dn = talloc_steal(os, op_dn);
131 return LDB_ERR_OPERATIONS_ERROR;
134 os->name = talloc_strdup(os, name);
136 return LDB_ERR_OPERATIONS_ERROR;
139 if ((op != LA_OP_DEL) && (value == NULL)) {
140 return LDB_ERR_OPERATIONS_ERROR;
143 os->value = talloc_strdup(os, value);
145 return LDB_ERR_OPERATIONS_ERROR;
159 static int la_op_search_callback(struct ldb_request *req,
160 struct ldb_reply *ares);
161 static int la_do_mod_request(struct la_context *ac);
162 static int la_mod_callback(struct ldb_request *req,
163 struct ldb_reply *ares);
164 static int la_down_req(struct la_context *ac);
165 static int la_down_callback(struct ldb_request *req,
166 struct ldb_reply *ares);
171 static int linked_attributes_add(struct ldb_module *module, struct ldb_request *req)
173 const struct dsdb_attribute *target_attr;
174 struct la_context *ac;
175 const char *attr_name;
176 const char *attr_val;
180 if (ldb_dn_is_special(req->op.add.message->dn)) {
181 /* do not manipulate our control entries */
182 return ldb_next_request(module, req);
185 ac = linked_attributes_init(module, req);
187 return LDB_ERR_OPERATIONS_ERROR;
191 /* without schema, this doesn't make any sense */
193 return ldb_next_request(module, req);
196 /* Need to ensure we only have forward links being specified */
197 for (i=0; i < req->op.add.message->num_elements; i++) {
198 const struct ldb_message_element *el = &req->op.add.message->elements[i];
199 const struct dsdb_attribute *schema_attr
200 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
202 ldb_asprintf_errstring(module->ldb,
203 "attribute %s is not a valid attribute in schema", el->name);
204 return LDB_ERR_OBJECT_CLASS_VIOLATION;
206 /* We have a valid attribute, now find out if it is linked */
207 if (schema_attr->linkID == 0) {
211 if ((schema_attr->linkID & 1) == 1) {
212 /* Odd is for the target. Illigal to modify */
213 ldb_asprintf_errstring(module->ldb,
214 "attribute %s must not be modified directly, it is a linked attribute", el->name);
215 return LDB_ERR_UNWILLING_TO_PERFORM;
218 /* Even link IDs are for the originating attribute */
219 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
222 * windows 2003 has a broken schema where
223 * the definition of msDS-IsDomainFor
224 * is missing (which is supposed to be
225 * the backlink of the msDS-HasDomainNCs
231 attr_name = target_attr->lDAPDisplayName;
232 attr_val = ldb_dn_get_linearized(ac->req->op.add.message->dn);
234 for (j = 0; j < el->num_values; j++) {
235 ret = la_store_op(ac, LA_OP_ADD,
237 attr_name, attr_val);
238 if (ret != LDB_SUCCESS) {
244 /* if no linked attributes are present continue */
245 if (ac->ops == NULL) {
247 return ldb_next_request(module, req);
250 /* start with the first one */
251 return la_do_mod_request(ac);
254 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
256 const struct dsdb_attribute *schema_attr;
257 const struct dsdb_attribute *target_attr;
258 struct ldb_message_element *search_el;
259 struct replace_context *rc;
260 struct la_context *ac;
261 const char *attr_name;
264 int ret = LDB_SUCCESS;
266 ac = talloc_get_type(req->context, struct la_context);
270 return ldb_module_done(ac->req, NULL, NULL,
271 LDB_ERR_OPERATIONS_ERROR);
273 if (ares->error != LDB_SUCCESS) {
274 return ldb_module_done(ac->req, ares->controls,
275 ares->response, ares->error);
278 /* Only entries are interesting, and we only want the olddn */
279 switch (ares->type) {
280 case LDB_REPLY_ENTRY:
282 if (ldb_dn_compare(ares->message->dn, ac->req->op.mod.message->dn) != 0) {
283 /* Guh? We only asked for this DN */
284 ldb_oom(ac->module->ldb);
286 return ldb_module_done(ac->req, NULL, NULL,
287 LDB_ERR_OPERATIONS_ERROR);
290 dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
292 for (i = 0; i < rc->num_elements; i++) {
294 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
296 ldb_asprintf_errstring(ac->module->ldb,
297 "attribute %s is not a valid attribute in schema",
300 return ldb_module_done(ac->req, NULL, NULL,
301 LDB_ERR_OBJECT_CLASS_VIOLATION);
304 search_el = ldb_msg_find_element(ares->message,
307 /* See if this element already exists */
308 /* otherwise just ignore as
309 * the add has already been scheduled */
314 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
317 * windows 2003 has a broken schema where
318 * the definition of msDS-IsDomainFor
319 * is missing (which is supposed to be
320 * the backlink of the msDS-HasDomainNCs
325 attr_name = target_attr->lDAPDisplayName;
327 /* make sure we manage each value */
328 for (j = 0; j < search_el->num_values; j++) {
329 ret = la_store_op(ac, LA_OP_DEL,
330 &search_el->values[j],
332 if (ret != LDB_SUCCESS) {
334 return ldb_module_done(ac->req,
342 case LDB_REPLY_REFERRAL:
350 /* All mods set up, start with the first one */
351 ret = la_do_mod_request(ac);
352 if (ret != LDB_SUCCESS) {
353 return ldb_module_done(ac->req, NULL, NULL, ret);
364 static int linked_attributes_modify(struct ldb_module *module, struct ldb_request *req)
366 /* Look over list of modifications */
367 /* Find if any are for linked attributes */
368 /* Determine the effect of the modification */
369 /* Apply the modify to the linked entry */
372 struct la_context *ac;
373 struct ldb_request *search_req;
376 if (ldb_dn_is_special(req->op.mod.message->dn)) {
377 /* do not manipulate our control entries */
378 return ldb_next_request(module, req);
381 ac = linked_attributes_init(module, req);
383 return LDB_ERR_OPERATIONS_ERROR;
387 /* without schema, this doesn't make any sense */
388 return ldb_next_request(module, req);
393 for (i=0; i < req->op.mod.message->num_elements; i++) {
394 bool store_el = false;
395 const char *attr_name;
396 const char *attr_val;
397 const struct dsdb_attribute *target_attr;
398 const struct ldb_message_element *el = &req->op.mod.message->elements[i];
399 const struct dsdb_attribute *schema_attr
400 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
402 ldb_asprintf_errstring(module->ldb,
403 "attribute %s is not a valid attribute in schema", el->name);
404 return LDB_ERR_OBJECT_CLASS_VIOLATION;
406 /* We have a valid attribute, now find out if it is linked */
407 if (schema_attr->linkID == 0) {
411 if ((schema_attr->linkID & 1) == 1) {
412 /* Odd is for the target. Illegal to modify */
413 ldb_asprintf_errstring(module->ldb,
414 "attribute %s must not be modified directly, it is a linked attribute", el->name);
415 return LDB_ERR_UNWILLING_TO_PERFORM;
418 /* Even link IDs are for the originating attribute */
420 /* Now find the target attribute */
421 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
424 * windows 2003 has a broken schema where
425 * the definition of msDS-IsDomainFor
426 * is missing (which is supposed to be
427 * the backlink of the msDS-HasDomainNCs
433 attr_name = target_attr->lDAPDisplayName;
434 attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
436 switch (el->flags & LDB_FLAG_MOD_MASK) {
437 case LDB_FLAG_MOD_REPLACE:
438 /* treat as just a normal add the delete part is handled by the callback */
441 /* break intentionally missing */
443 case LDB_FLAG_MOD_ADD:
445 /* For each value being added, we need to setup the adds */
446 for (j = 0; j < el->num_values; j++) {
447 ret = la_store_op(ac, LA_OP_ADD,
449 attr_name, attr_val);
450 if (ret != LDB_SUCCESS) {
456 case LDB_FLAG_MOD_DELETE:
458 if (el->num_values) {
459 /* For each value being deleted, we need to setup the delete */
460 for (j = 0; j < el->num_values; j++) {
461 ret = la_store_op(ac, LA_OP_DEL,
463 attr_name, attr_val);
464 if (ret != LDB_SUCCESS) {
469 /* Flag that there was a DELETE
470 * without a value specified, so we
471 * need to look for the old value */
479 struct ldb_message_element *search_el;
481 /* Fill out ac->rc only if we have to find the old values */
483 ac->rc = talloc_zero(ac, struct replace_context);
485 ldb_oom(module->ldb);
486 return LDB_ERR_OPERATIONS_ERROR;
490 search_el = talloc_realloc(ac->rc, ac->rc->el,
491 struct ldb_message_element,
492 ac->rc->num_elements +1);
494 ldb_oom(module->ldb);
495 return LDB_ERR_OPERATIONS_ERROR;
497 ac->rc->el = search_el;
499 ac->rc->el[ac->rc->num_elements] = *el;
500 ac->rc->num_elements++;
504 /* both replace and delete without values are handled in the callback
505 * after the search on the entry to be modified is performed */
507 /* Only bother doing a search of this entry (to find old
508 * values) if replace or delete operations are attempted */
512 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
514 ldb_oom(module->ldb);
515 return LDB_ERR_OPERATIONS_ERROR;
517 for (i = 0; i < ac->rc->num_elements; i++) {
518 attrs[i] = ac->rc->el[i].name;
522 /* The callback does all the hard work here */
523 ret = ldb_build_search_req(&search_req, module->ldb, ac,
524 req->op.mod.message->dn,
526 "(objectClass=*)", attrs,
528 ac, la_mod_search_callback,
531 if (ret == LDB_SUCCESS) {
532 talloc_steal(search_req, attrs);
534 ret = ldb_next_request(module, search_req);
540 /* Jump directly to handling the modifies */
541 ret = la_do_mod_request(ac);
543 /* nothing to do for this module, proceed */
545 ret = ldb_next_request(module, req);
553 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
555 struct ldb_request *search_req;
556 struct ldb_dn *base_dn;
557 struct la_context *ac;
562 /* This gets complex: We need to:
563 - Do a search for the entry
564 - Wait for these result to appear
565 - In the callback for the result, issue a modify
566 request based on the linked attributes found
567 - Wait for each modify result
571 switch (req->operation) {
573 base_dn = req->op.rename.olddn;
576 base_dn = req->op.del.dn;
579 return LDB_ERR_OPERATIONS_ERROR;
582 ac = linked_attributes_init(module, req);
584 return LDB_ERR_OPERATIONS_ERROR;
588 /* without schema, this doesn't make any sense */
589 return ldb_next_request(module, req);
592 werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
593 if (!W_ERROR_IS_OK(werr)) {
594 return LDB_ERR_OPERATIONS_ERROR;
597 ret = ldb_build_search_req(&search_req, module->ldb, req,
598 base_dn, LDB_SCOPE_BASE,
599 "(objectClass=*)", attrs,
601 ac, la_op_search_callback,
604 if (ret != LDB_SUCCESS) {
608 talloc_steal(search_req, attrs);
610 return ldb_next_request(module, search_req);
613 static int la_op_search_callback(struct ldb_request *req,
614 struct ldb_reply *ares)
616 struct la_context *ac;
617 const struct dsdb_attribute *schema_attr;
618 const struct dsdb_attribute *target_attr;
619 const struct ldb_message_element *el;
620 const char *attr_name;
626 ac = talloc_get_type(req->context, struct la_context);
629 return ldb_module_done(ac->req, NULL, NULL,
630 LDB_ERR_OPERATIONS_ERROR);
632 if (ares->error != LDB_SUCCESS) {
633 return ldb_module_done(ac->req, ares->controls,
634 ares->response, ares->error);
637 /* Only entries are interesting, and we only want the olddn */
638 switch (ares->type) {
639 case LDB_REPLY_ENTRY:
640 ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
642 /* Guh? We only asked for this DN */
644 return ldb_module_done(ac->req, NULL, NULL,
645 LDB_ERR_OPERATIONS_ERROR);
647 if (ares->message->num_elements == 0) {
648 /* only bother at all if there were some
649 * linked attributes found */
654 switch (ac->req->operation) {
656 deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
660 deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
661 adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
665 return ldb_module_done(ac->req, NULL, NULL,
666 LDB_ERR_OPERATIONS_ERROR);
669 for (i = 0; i < ares->message->num_elements; i++) {
670 el = &ares->message->elements[i];
672 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
674 ldb_asprintf_errstring(ac->module->ldb,
675 "attribute %s is not a valid attribute"
676 " in schema", el->name);
678 return ldb_module_done(ac->req, NULL, NULL,
679 LDB_ERR_OBJECT_CLASS_VIOLATION);
682 /* Valid attribute, now find out if it is linked */
683 if (schema_attr->linkID == 0) {
684 /* Not a linked attribute, skip */
688 if ((schema_attr->linkID & 1) == 0) {
689 /* Odd is for the target. */
690 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
694 attr_name = target_attr->lDAPDisplayName;
696 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
700 attr_name = target_attr->lDAPDisplayName;
702 for (j = 0; j < el->num_values; j++) {
703 ret = la_store_op(ac, LA_OP_DEL,
706 if (ret != LDB_SUCCESS) {
708 return ldb_module_done(ac->req,
711 if (!adddn) continue;
712 ret = la_store_op(ac, LA_OP_ADD,
715 if (ret != LDB_SUCCESS) {
717 return ldb_module_done(ac->req,
725 case LDB_REPLY_REFERRAL:
734 /* start the mod requests chain */
735 ret = la_do_mod_request(ac);
737 ret = la_down_req(ac);
739 if (ret != LDB_SUCCESS) {
740 return ldb_module_done(ac->req, NULL, NULL, ret);
749 /* do a linked attributes modify request */
750 static int la_do_mod_request(struct la_context *ac)
752 struct ldb_message_element *ret_el;
753 struct ldb_request *mod_req;
754 struct ldb_message *new_msg;
755 struct ldb_context *ldb;
758 ldb = ac->module->ldb;
760 /* Create the modify request */
761 new_msg = ldb_msg_new(ac);
764 return LDB_ERR_OPERATIONS_ERROR;
766 new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
768 return LDB_ERR_OPERATIONS_ERROR;
771 if (ac->ops->op == LA_OP_ADD) {
772 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
773 LDB_FLAG_MOD_ADD, &ret_el);
775 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
776 LDB_FLAG_MOD_DELETE, &ret_el);
778 if (ret != LDB_SUCCESS) {
781 ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
782 if (!ret_el->values) {
784 return LDB_ERR_OPERATIONS_ERROR;
786 ret_el->values[0] = data_blob_string_const(ac->ops->value);
787 ret_el->num_values = 1;
789 /* use ac->ops as the mem_ctx so that the request will be freed
790 * in the callback as soon as completed */
791 ret = ldb_build_mod_req(&mod_req, ldb, ac->ops,
796 if (ret != LDB_SUCCESS) {
799 talloc_steal(mod_req, new_msg);
801 /* Run the new request */
802 return ldb_next_request(ac->module, mod_req);
805 static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
807 struct la_context *ac;
808 struct la_op_store *os;
811 ac = talloc_get_type(req->context, struct la_context);
814 return ldb_module_done(ac->req, NULL, NULL,
815 LDB_ERR_OPERATIONS_ERROR);
817 if (ares->error != LDB_SUCCESS) {
818 return ldb_module_done(ac->req, ares->controls,
819 ares->response, ares->error);
822 if (ares->type != LDB_REPLY_DONE) {
823 ldb_set_errstring(ac->module->ldb,
824 "invalid ldb_reply_type in callback");
826 return ldb_module_done(ac->req, NULL, NULL,
827 LDB_ERR_OPERATIONS_ERROR);
836 /* this frees the request too
837 * DO NOT access 'req' after this point */
841 /* as last op run the original request */
843 ret = la_do_mod_request(ac);
845 ret = la_down_req(ac);
848 if (ret != LDB_SUCCESS) {
849 return ldb_module_done(ac->req, NULL, NULL, ret);
854 static int la_down_req(struct la_context *ac)
856 struct ldb_request *down_req;
859 switch (ac->req->operation) {
861 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
862 ac->req->op.add.message,
864 ac, la_down_callback,
868 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
869 ac->req->op.mod.message,
871 ac, la_down_callback,
875 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
878 ac, la_down_callback,
882 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
883 ac->req->op.rename.olddn,
884 ac->req->op.rename.newdn,
886 ac, la_down_callback,
890 ret = LDB_ERR_OPERATIONS_ERROR;
892 if (ret != LDB_SUCCESS) {
896 return ldb_next_request(ac->module, down_req);
899 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
901 struct la_context *ac;
903 ac = talloc_get_type(req->context, struct la_context);
906 return ldb_module_done(ac->req, NULL, NULL,
907 LDB_ERR_OPERATIONS_ERROR);
909 if (ares->error != LDB_SUCCESS) {
910 return ldb_module_done(ac->req, ares->controls,
911 ares->response, ares->error);
914 if (ares->type != LDB_REPLY_DONE) {
915 ldb_set_errstring(ac->module->ldb,
916 "invalid ldb_reply_type in callback");
918 return ldb_module_done(ac->req, NULL, NULL,
919 LDB_ERR_OPERATIONS_ERROR);
922 return ldb_module_done(ac->req, ares->controls,
923 ares->response, ares->error);
926 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
927 .name = "linked_attributes",
928 .add = linked_attributes_add,
929 .modify = linked_attributes_modify,
930 .del = linked_attributes_op,
931 .rename = linked_attributes_op,