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, char *dn,
83 const char *name, const char *value)
85 struct la_op_store *os, *tmp;
88 op_dn = ldb_dn_new(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.mod.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,
236 (char *)el->values[j].data,
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 (char *)search_el->values[j].data,
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,
448 (char *)el->values[j].data,
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,
462 (char *)el->values[j].data,
463 attr_name, attr_val);
464 if (ret != LDB_SUCCESS) {
476 struct ldb_message_element *search_el;
479 ac->rc = talloc_zero(ac, struct replace_context);
481 ldb_oom(module->ldb);
482 return LDB_ERR_OPERATIONS_ERROR;
486 search_el = talloc_realloc(ac->rc, ac->rc->el,
487 struct ldb_message_element,
488 ac->rc->num_elements +1);
490 ldb_oom(module->ldb);
491 return LDB_ERR_OPERATIONS_ERROR;
493 ac->rc->el = search_el;
495 ac->rc->el[ac->rc->num_elements] = *el;
496 ac->rc->num_elements++;
500 /* both replace and delete without values are handled in the callback
501 * after the search on the entry to be modified is performed */
505 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
507 ldb_oom(module->ldb);
508 return LDB_ERR_OPERATIONS_ERROR;
510 for (i = 0; i < ac->rc->num_elements; i++) {
511 attrs[i] = ac->rc->el[i].name;
515 /* The callback does all the hard work here */
516 ret = ldb_build_search_req(&search_req, module->ldb, ac,
517 req->op.mod.message->dn,
519 "(objectClass=*)", attrs,
521 ac, la_mod_search_callback,
524 if (ret == LDB_SUCCESS) {
525 talloc_steal(search_req, attrs);
527 ret = ldb_next_request(module, search_req);
532 /* start the mod requests chain */
533 ret = la_do_mod_request(ac);
535 /* nothing to do for this module, proceed */
537 ret = ldb_next_request(module, req);
545 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
547 struct ldb_request *search_req;
548 struct ldb_dn *base_dn;
549 struct la_context *ac;
554 /* This gets complex: We need to:
555 - Do a search for the entry
556 - Wait for these result to appear
557 - In the callback for the result, issue a modify
558 request based on the linked attributes found
559 - Wait for each modify result
563 switch (req->operation) {
565 base_dn = req->op.rename.olddn;
568 base_dn = req->op.del.dn;
571 return LDB_ERR_OPERATIONS_ERROR;
574 ac = linked_attributes_init(module, req);
576 return LDB_ERR_OPERATIONS_ERROR;
580 /* without schema, this doesn't make any sense */
581 return ldb_next_request(module, req);
584 werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
585 if (!W_ERROR_IS_OK(werr)) {
586 return LDB_ERR_OPERATIONS_ERROR;
589 ret = ldb_build_search_req(&search_req, module->ldb, req,
590 base_dn, LDB_SCOPE_BASE,
591 "(objectClass=*)", attrs,
593 ac, la_op_search_callback,
596 if (ret != LDB_SUCCESS) {
600 talloc_steal(search_req, attrs);
602 return ldb_next_request(module, search_req);
605 static int la_op_search_callback(struct ldb_request *req,
606 struct ldb_reply *ares)
608 struct la_context *ac;
609 const struct dsdb_attribute *schema_attr;
610 const struct dsdb_attribute *target_attr;
611 const struct ldb_message_element *el;
612 const char *attr_name;
618 ac = talloc_get_type(req->context, struct la_context);
621 return ldb_module_done(ac->req, NULL, NULL,
622 LDB_ERR_OPERATIONS_ERROR);
624 if (ares->error != LDB_SUCCESS) {
625 return ldb_module_done(ac->req, ares->controls,
626 ares->response, ares->error);
629 /* Only entries are interesting, and we only want the olddn */
630 switch (ares->type) {
631 case LDB_REPLY_ENTRY:
632 ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
634 /* Guh? We only asked for this DN */
636 return ldb_module_done(ac->req, NULL, NULL,
637 LDB_ERR_OPERATIONS_ERROR);
639 if (ares->message->num_elements == 0) {
640 /* only bother at all if there were some
641 * linked attributes found */
646 switch (ac->req->operation) {
648 deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
652 deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
653 adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
657 return ldb_module_done(ac->req, NULL, NULL,
658 LDB_ERR_OPERATIONS_ERROR);
661 for (i = 0; i < ares->message->num_elements; i++) {
662 el = &ares->message->elements[i];
664 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
666 ldb_asprintf_errstring(ac->module->ldb,
667 "attribute %s is not a valid attribute"
668 " in schema", el->name);
670 return ldb_module_done(ac->req, NULL, NULL,
671 LDB_ERR_OBJECT_CLASS_VIOLATION);
674 /* Valid attribute, now find out if it is linked */
675 if (schema_attr->linkID == 0) {
676 /* Not a linked attribute, skip */
680 if ((schema_attr->linkID & 1) == 0) {
681 /* Odd is for the target. */
682 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
686 attr_name = target_attr->lDAPDisplayName;
688 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
692 attr_name = target_attr->lDAPDisplayName;
694 for (j = 0; j < el->num_values; j++) {
695 ret = la_store_op(ac, LA_OP_DEL,
696 (char *)el->values[j].data,
698 if (ret != LDB_SUCCESS) {
700 return ldb_module_done(ac->req,
703 if (!adddn) continue;
704 ret = la_store_op(ac, LA_OP_ADD,
705 (char *)el->values[j].data,
707 if (ret != LDB_SUCCESS) {
709 return ldb_module_done(ac->req,
717 case LDB_REPLY_REFERRAL:
726 /* start the mod requests chain */
727 ret = la_do_mod_request(ac);
729 ret = la_down_req(ac);
731 if (ret != LDB_SUCCESS) {
732 return ldb_module_done(ac->req, NULL, NULL, ret);
741 /* do a linked attributes modify request */
742 static int la_do_mod_request(struct la_context *ac)
744 struct ldb_message_element *ret_el;
745 struct ldb_request *mod_req;
746 struct ldb_message *new_msg;
747 struct ldb_context *ldb;
750 ldb = ac->module->ldb;
752 /* Create the modify request */
753 new_msg = ldb_msg_new(ac);
756 return LDB_ERR_OPERATIONS_ERROR;
758 new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
760 return LDB_ERR_OPERATIONS_ERROR;
763 if (ac->ops->op == LA_OP_ADD) {
764 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
765 LDB_FLAG_MOD_ADD, &ret_el);
767 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
768 LDB_FLAG_MOD_DELETE, &ret_el);
770 if (ret != LDB_SUCCESS) {
773 ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
774 if (!ret_el->values) {
776 return LDB_ERR_OPERATIONS_ERROR;
778 ret_el->values[0] = data_blob_string_const(ac->ops->value);
779 ret_el->num_values = 1;
781 /* use ac->ops as the mem_ctx so that the request will be freed
782 * in the callback as soon as completed */
783 ret = ldb_build_mod_req(&mod_req, ldb, ac->ops,
788 if (ret != LDB_SUCCESS) {
791 talloc_steal(mod_req, new_msg);
793 /* Run the new request */
794 return ldb_next_request(ac->module, mod_req);
797 static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
799 struct la_context *ac;
800 struct la_op_store *os;
803 ac = talloc_get_type(req->context, struct la_context);
806 return ldb_module_done(ac->req, NULL, NULL,
807 LDB_ERR_OPERATIONS_ERROR);
809 if (ares->error != LDB_SUCCESS) {
810 return ldb_module_done(ac->req, ares->controls,
811 ares->response, ares->error);
814 if (ares->type != LDB_REPLY_DONE) {
815 ldb_set_errstring(ac->module->ldb,
816 "invalid ldb_reply_type in callback");
818 return ldb_module_done(ac->req, NULL, NULL,
819 LDB_ERR_OPERATIONS_ERROR);
828 /* this frees the request too
829 * DO NOT access 'req' after this point */
833 /* as last op run the original request */
835 ret = la_do_mod_request(ac);
837 ret = la_down_req(ac);
840 if (ret != LDB_SUCCESS) {
841 return ldb_module_done(ac->req, NULL, NULL, ret);
846 static int la_down_req(struct la_context *ac)
848 struct ldb_request *down_req;
851 switch (ac->req->operation) {
853 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
854 ac->req->op.add.message,
856 ac, la_down_callback,
860 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
861 ac->req->op.mod.message,
863 ac, la_down_callback,
867 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
870 ac, la_down_callback,
874 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
875 ac->req->op.rename.olddn,
876 ac->req->op.rename.newdn,
878 ac, la_down_callback,
882 ret = LDB_ERR_OPERATIONS_ERROR;
884 if (ret != LDB_SUCCESS) {
888 return ldb_next_request(ac->module, down_req);
891 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
893 struct la_context *ac;
895 ac = talloc_get_type(req->context, struct la_context);
898 return ldb_module_done(ac->req, NULL, NULL,
899 LDB_ERR_OPERATIONS_ERROR);
901 if (ares->error != LDB_SUCCESS) {
902 return ldb_module_done(ac->req, ares->controls,
903 ares->response, ares->error);
906 if (ares->type != LDB_REPLY_DONE) {
907 ldb_set_errstring(ac->module->ldb,
908 "invalid ldb_reply_type in callback");
910 return ldb_module_done(ac->req, NULL, NULL,
911 LDB_ERR_OPERATIONS_ERROR);
914 return ldb_module_done(ac->req, ares->controls,
915 ares->response, ares->error);
918 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
919 .name = "linked_attributes",
920 .add = linked_attributes_add,
921 .modify = linked_attributes_modify,
922 .del = linked_attributes_op,
923 .rename = linked_attributes_op,