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 ldb_asprintf_errstring(ac->module->ldb,
91 "could not parse attribute as a DN");
92 return LDB_ERR_INVALID_DN_SYNTAX;
95 /* optimize out del - add operations that would end up
97 if (ac->ops && op == LA_OP_DEL) {
98 /* do a linear search to find out if there is
99 * an equivalent add */
104 if (tmp->op == LA_OP_ADD) {
106 if ((strcmp(name, tmp->name) == 0) &&
107 (strcmp(value, tmp->value) == 0) &&
108 (ldb_dn_compare(op_dn, tmp->dn) == 0)) {
116 /* pair found, remove it and return */
117 os->next = tmp->next;
124 os = talloc_zero(ac, struct la_op_store);
126 return LDB_ERR_OPERATIONS_ERROR;
131 os->dn = talloc_steal(os, op_dn);
133 return LDB_ERR_OPERATIONS_ERROR;
136 os->name = talloc_strdup(os, name);
138 return LDB_ERR_OPERATIONS_ERROR;
141 if ((op != LA_OP_DEL) && (value == NULL)) {
142 return LDB_ERR_OPERATIONS_ERROR;
145 os->value = talloc_strdup(os, value);
147 return LDB_ERR_OPERATIONS_ERROR;
161 static int la_op_search_callback(struct ldb_request *req,
162 struct ldb_reply *ares);
163 static int la_do_mod_request(struct la_context *ac);
164 static int la_mod_callback(struct ldb_request *req,
165 struct ldb_reply *ares);
166 static int la_down_req(struct la_context *ac);
167 static int la_down_callback(struct ldb_request *req,
168 struct ldb_reply *ares);
173 static int linked_attributes_add(struct ldb_module *module, struct ldb_request *req)
175 const struct dsdb_attribute *target_attr;
176 struct la_context *ac;
177 const char *attr_name;
178 const char *attr_val;
182 if (ldb_dn_is_special(req->op.add.message->dn)) {
183 /* do not manipulate our control entries */
184 return ldb_next_request(module, req);
187 ac = linked_attributes_init(module, req);
189 return LDB_ERR_OPERATIONS_ERROR;
193 /* without schema, this doesn't make any sense */
195 return ldb_next_request(module, req);
198 /* Need to ensure we only have forward links being specified */
199 for (i=0; i < req->op.add.message->num_elements; i++) {
200 const struct ldb_message_element *el = &req->op.add.message->elements[i];
201 const struct dsdb_attribute *schema_attr
202 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
204 ldb_asprintf_errstring(module->ldb,
205 "attribute %s is not a valid attribute in schema", el->name);
206 return LDB_ERR_OBJECT_CLASS_VIOLATION;
208 /* We have a valid attribute, now find out if it is linked */
209 if (schema_attr->linkID == 0) {
213 if ((schema_attr->linkID & 1) == 1) {
214 /* Odd is for the target. Illigal to modify */
215 ldb_asprintf_errstring(module->ldb,
216 "attribute %s must not be modified directly, it is a linked attribute", el->name);
217 return LDB_ERR_UNWILLING_TO_PERFORM;
220 /* Even link IDs are for the originating attribute */
221 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
224 * windows 2003 has a broken schema where
225 * the definition of msDS-IsDomainFor
226 * is missing (which is supposed to be
227 * the backlink of the msDS-HasDomainNCs
233 attr_name = target_attr->lDAPDisplayName;
234 attr_val = ldb_dn_get_linearized(ac->req->op.add.message->dn);
236 for (j = 0; j < el->num_values; j++) {
237 ret = la_store_op(ac, LA_OP_ADD,
239 attr_name, attr_val);
240 if (ret != LDB_SUCCESS) {
246 /* if no linked attributes are present continue */
247 if (ac->ops == NULL) {
249 return ldb_next_request(module, req);
252 /* start with the first one */
253 return la_do_mod_request(ac);
256 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
258 const struct dsdb_attribute *schema_attr;
259 const struct dsdb_attribute *target_attr;
260 struct ldb_message_element *search_el;
261 struct replace_context *rc;
262 struct la_context *ac;
263 const char *attr_name;
266 int ret = LDB_SUCCESS;
268 ac = talloc_get_type(req->context, struct la_context);
272 return ldb_module_done(ac->req, NULL, NULL,
273 LDB_ERR_OPERATIONS_ERROR);
275 if (ares->error != LDB_SUCCESS) {
276 return ldb_module_done(ac->req, ares->controls,
277 ares->response, ares->error);
280 /* Only entries are interesting, and we only want the olddn */
281 switch (ares->type) {
282 case LDB_REPLY_ENTRY:
284 if (ldb_dn_compare(ares->message->dn, ac->req->op.mod.message->dn) != 0) {
285 /* Guh? We only asked for this DN */
286 ldb_oom(ac->module->ldb);
288 return ldb_module_done(ac->req, NULL, NULL,
289 LDB_ERR_OPERATIONS_ERROR);
292 dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
294 for (i = 0; i < rc->num_elements; i++) {
296 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
298 ldb_asprintf_errstring(ac->module->ldb,
299 "attribute %s is not a valid attribute in schema",
302 return ldb_module_done(ac->req, NULL, NULL,
303 LDB_ERR_OBJECT_CLASS_VIOLATION);
306 search_el = ldb_msg_find_element(ares->message,
309 /* See if this element already exists */
310 /* otherwise just ignore as
311 * the add has already been scheduled */
316 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
319 * windows 2003 has a broken schema where
320 * the definition of msDS-IsDomainFor
321 * is missing (which is supposed to be
322 * the backlink of the msDS-HasDomainNCs
327 attr_name = target_attr->lDAPDisplayName;
329 /* make sure we manage each value */
330 for (j = 0; j < search_el->num_values; j++) {
331 ret = la_store_op(ac, LA_OP_DEL,
332 &search_el->values[j],
334 if (ret != LDB_SUCCESS) {
336 return ldb_module_done(ac->req,
344 case LDB_REPLY_REFERRAL:
352 /* All mods set up, start with the first one */
353 ret = la_do_mod_request(ac);
354 if (ret != LDB_SUCCESS) {
355 return ldb_module_done(ac->req, NULL, NULL, ret);
366 static int linked_attributes_modify(struct ldb_module *module, struct ldb_request *req)
368 /* Look over list of modifications */
369 /* Find if any are for linked attributes */
370 /* Determine the effect of the modification */
371 /* Apply the modify to the linked entry */
374 struct la_context *ac;
375 struct ldb_request *search_req;
378 if (ldb_dn_is_special(req->op.mod.message->dn)) {
379 /* do not manipulate our control entries */
380 return ldb_next_request(module, req);
383 ac = linked_attributes_init(module, req);
385 return LDB_ERR_OPERATIONS_ERROR;
389 /* without schema, this doesn't make any sense */
390 return ldb_next_request(module, req);
395 for (i=0; i < req->op.mod.message->num_elements; i++) {
396 bool store_el = false;
397 const char *attr_name;
398 const char *attr_val;
399 const struct dsdb_attribute *target_attr;
400 const struct ldb_message_element *el = &req->op.mod.message->elements[i];
401 const struct dsdb_attribute *schema_attr
402 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
404 ldb_asprintf_errstring(module->ldb,
405 "attribute %s is not a valid attribute in schema", el->name);
406 return LDB_ERR_OBJECT_CLASS_VIOLATION;
408 /* We have a valid attribute, now find out if it is linked */
409 if (schema_attr->linkID == 0) {
413 if ((schema_attr->linkID & 1) == 1) {
414 /* Odd is for the target. Illegal to modify */
415 ldb_asprintf_errstring(module->ldb,
416 "attribute %s must not be modified directly, it is a linked attribute", el->name);
417 return LDB_ERR_UNWILLING_TO_PERFORM;
420 /* Even link IDs are for the originating attribute */
422 /* Now find the target attribute */
423 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
426 * windows 2003 has a broken schema where
427 * the definition of msDS-IsDomainFor
428 * is missing (which is supposed to be
429 * the backlink of the msDS-HasDomainNCs
435 attr_name = target_attr->lDAPDisplayName;
436 attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
438 switch (el->flags & LDB_FLAG_MOD_MASK) {
439 case LDB_FLAG_MOD_REPLACE:
440 /* treat as just a normal add the delete part is handled by the callback */
443 /* break intentionally missing */
445 case LDB_FLAG_MOD_ADD:
447 /* For each value being added, we need to setup the adds */
448 for (j = 0; j < el->num_values; j++) {
449 ret = la_store_op(ac, LA_OP_ADD,
451 attr_name, attr_val);
452 if (ret != LDB_SUCCESS) {
458 case LDB_FLAG_MOD_DELETE:
460 if (el->num_values) {
461 /* For each value being deleted, we need to setup the delete */
462 for (j = 0; j < el->num_values; j++) {
463 ret = la_store_op(ac, LA_OP_DEL,
465 attr_name, attr_val);
466 if (ret != LDB_SUCCESS) {
471 /* Flag that there was a DELETE
472 * without a value specified, so we
473 * need to look for the old value */
481 struct ldb_message_element *search_el;
483 /* Fill out ac->rc only if we have to find the old values */
485 ac->rc = talloc_zero(ac, struct replace_context);
487 ldb_oom(module->ldb);
488 return LDB_ERR_OPERATIONS_ERROR;
492 search_el = talloc_realloc(ac->rc, ac->rc->el,
493 struct ldb_message_element,
494 ac->rc->num_elements +1);
496 ldb_oom(module->ldb);
497 return LDB_ERR_OPERATIONS_ERROR;
499 ac->rc->el = search_el;
501 ac->rc->el[ac->rc->num_elements] = *el;
502 ac->rc->num_elements++;
506 /* both replace and delete without values are handled in the callback
507 * after the search on the entry to be modified is performed */
509 /* Only bother doing a search of this entry (to find old
510 * values) if replace or delete operations are attempted */
514 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
516 ldb_oom(module->ldb);
517 return LDB_ERR_OPERATIONS_ERROR;
519 for (i = 0; i < ac->rc->num_elements; i++) {
520 attrs[i] = ac->rc->el[i].name;
524 /* The callback does all the hard work here */
525 ret = ldb_build_search_req(&search_req, module->ldb, ac,
526 req->op.mod.message->dn,
528 "(objectClass=*)", attrs,
530 ac, la_mod_search_callback,
533 if (ret == LDB_SUCCESS) {
534 talloc_steal(search_req, attrs);
536 ret = ldb_next_request(module, search_req);
542 /* Jump directly to handling the modifies */
543 ret = la_do_mod_request(ac);
545 /* nothing to do for this module, proceed */
547 ret = ldb_next_request(module, req);
555 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
557 struct ldb_request *search_req;
558 struct ldb_dn *base_dn;
559 struct la_context *ac;
564 /* This gets complex: We need to:
565 - Do a search for the entry
566 - Wait for these result to appear
567 - In the callback for the result, issue a modify
568 request based on the linked attributes found
569 - Wait for each modify result
573 switch (req->operation) {
575 base_dn = req->op.rename.olddn;
578 base_dn = req->op.del.dn;
581 return LDB_ERR_OPERATIONS_ERROR;
584 ac = linked_attributes_init(module, req);
586 return LDB_ERR_OPERATIONS_ERROR;
590 /* without schema, this doesn't make any sense */
591 return ldb_next_request(module, req);
594 werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
595 if (!W_ERROR_IS_OK(werr)) {
596 return LDB_ERR_OPERATIONS_ERROR;
599 ret = ldb_build_search_req(&search_req, module->ldb, req,
600 base_dn, LDB_SCOPE_BASE,
601 "(objectClass=*)", attrs,
603 ac, la_op_search_callback,
606 if (ret != LDB_SUCCESS) {
610 talloc_steal(search_req, attrs);
612 return ldb_next_request(module, search_req);
615 static int la_op_search_callback(struct ldb_request *req,
616 struct ldb_reply *ares)
618 struct la_context *ac;
619 const struct dsdb_attribute *schema_attr;
620 const struct dsdb_attribute *target_attr;
621 const struct ldb_message_element *el;
622 const char *attr_name;
628 ac = talloc_get_type(req->context, struct la_context);
631 return ldb_module_done(ac->req, NULL, NULL,
632 LDB_ERR_OPERATIONS_ERROR);
634 if (ares->error != LDB_SUCCESS) {
635 return ldb_module_done(ac->req, ares->controls,
636 ares->response, ares->error);
639 /* Only entries are interesting, and we only want the olddn */
640 switch (ares->type) {
641 case LDB_REPLY_ENTRY:
642 ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
644 /* Guh? We only asked for this DN */
646 return ldb_module_done(ac->req, NULL, NULL,
647 LDB_ERR_OPERATIONS_ERROR);
649 if (ares->message->num_elements == 0) {
650 /* only bother at all if there were some
651 * linked attributes found */
656 switch (ac->req->operation) {
658 deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
662 deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
663 adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
667 return ldb_module_done(ac->req, NULL, NULL,
668 LDB_ERR_OPERATIONS_ERROR);
671 for (i = 0; i < ares->message->num_elements; i++) {
672 el = &ares->message->elements[i];
674 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
676 ldb_asprintf_errstring(ac->module->ldb,
677 "attribute %s is not a valid attribute"
678 " in schema", el->name);
680 return ldb_module_done(ac->req, NULL, NULL,
681 LDB_ERR_OBJECT_CLASS_VIOLATION);
684 /* Valid attribute, now find out if it is linked */
685 if (schema_attr->linkID == 0) {
686 /* Not a linked attribute, skip */
690 if ((schema_attr->linkID & 1) == 0) {
691 /* Odd is for the target. */
692 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
696 attr_name = target_attr->lDAPDisplayName;
698 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
702 attr_name = target_attr->lDAPDisplayName;
704 for (j = 0; j < el->num_values; j++) {
705 ret = la_store_op(ac, LA_OP_DEL,
708 if (ret != LDB_SUCCESS) {
710 return ldb_module_done(ac->req,
713 if (!adddn) continue;
714 ret = la_store_op(ac, LA_OP_ADD,
717 if (ret != LDB_SUCCESS) {
719 return ldb_module_done(ac->req,
727 case LDB_REPLY_REFERRAL:
736 /* start the mod requests chain */
737 ret = la_do_mod_request(ac);
739 ret = la_down_req(ac);
741 if (ret != LDB_SUCCESS) {
742 return ldb_module_done(ac->req, NULL, NULL, ret);
751 /* do a linked attributes modify request */
752 static int la_do_mod_request(struct la_context *ac)
754 struct ldb_message_element *ret_el;
755 struct ldb_request *mod_req;
756 struct ldb_message *new_msg;
757 struct ldb_context *ldb;
760 ldb = ac->module->ldb;
762 /* Create the modify request */
763 new_msg = ldb_msg_new(ac);
766 return LDB_ERR_OPERATIONS_ERROR;
768 new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
770 return LDB_ERR_OPERATIONS_ERROR;
773 if (ac->ops->op == LA_OP_ADD) {
774 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
775 LDB_FLAG_MOD_ADD, &ret_el);
777 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
778 LDB_FLAG_MOD_DELETE, &ret_el);
780 if (ret != LDB_SUCCESS) {
783 ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
784 if (!ret_el->values) {
786 return LDB_ERR_OPERATIONS_ERROR;
788 ret_el->values[0] = data_blob_string_const(ac->ops->value);
789 ret_el->num_values = 1;
791 /* use ac->ops as the mem_ctx so that the request will be freed
792 * in the callback as soon as completed */
793 ret = ldb_build_mod_req(&mod_req, ldb, ac->ops,
798 if (ret != LDB_SUCCESS) {
801 talloc_steal(mod_req, new_msg);
803 /* Run the new request */
804 return ldb_next_request(ac->module, mod_req);
807 static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
809 struct la_context *ac;
810 struct la_op_store *os;
813 ac = talloc_get_type(req->context, struct la_context);
816 return ldb_module_done(ac->req, NULL, NULL,
817 LDB_ERR_OPERATIONS_ERROR);
819 if (ares->error != LDB_SUCCESS) {
820 return ldb_module_done(ac->req, ares->controls,
821 ares->response, ares->error);
824 if (ares->type != LDB_REPLY_DONE) {
825 ldb_set_errstring(ac->module->ldb,
826 "invalid ldb_reply_type in callback");
828 return ldb_module_done(ac->req, NULL, NULL,
829 LDB_ERR_OPERATIONS_ERROR);
838 /* this frees the request too
839 * DO NOT access 'req' after this point */
843 /* as last op run the original request */
845 ret = la_do_mod_request(ac);
847 ret = la_down_req(ac);
850 if (ret != LDB_SUCCESS) {
851 return ldb_module_done(ac->req, NULL, NULL, ret);
856 static int la_down_req(struct la_context *ac)
858 struct ldb_request *down_req;
861 switch (ac->req->operation) {
863 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
864 ac->req->op.add.message,
866 ac, la_down_callback,
870 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
871 ac->req->op.mod.message,
873 ac, la_down_callback,
877 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
880 ac, la_down_callback,
884 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
885 ac->req->op.rename.olddn,
886 ac->req->op.rename.newdn,
888 ac, la_down_callback,
892 ret = LDB_ERR_OPERATIONS_ERROR;
894 if (ret != LDB_SUCCESS) {
898 return ldb_next_request(ac->module, down_req);
901 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
903 struct la_context *ac;
905 ac = talloc_get_type(req->context, struct la_context);
908 return ldb_module_done(ac->req, NULL, NULL,
909 LDB_ERR_OPERATIONS_ERROR);
911 if (ares->error != LDB_SUCCESS) {
912 return ldb_module_done(ac->req, ares->controls,
913 ares->response, ares->error);
916 if (ares->type != LDB_REPLY_DONE) {
917 ldb_set_errstring(ac->module->ldb,
918 "invalid ldb_reply_type in callback");
920 return ldb_module_done(ac->req, NULL, NULL,
921 LDB_ERR_OPERATIONS_ERROR);
924 return ldb_module_done(ac->req, ares->controls,
925 ares->response, ares->error);
928 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
929 .name = "linked_attributes",
930 .add = linked_attributes_add,
931 .modify = linked_attributes_modify,
932 .del = linked_attributes_op,
933 .rename = linked_attributes_op,