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 original request */
253 return la_down_req(ac);
256 /* For a delete or rename, we need to find out what linked attributes
257 * are currently on this DN, and then deal with them. This is the
258 * callback to the base search */
260 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
262 const struct dsdb_attribute *schema_attr;
263 const struct dsdb_attribute *target_attr;
264 struct ldb_message_element *search_el;
265 struct replace_context *rc;
266 struct la_context *ac;
267 const char *attr_name;
270 int ret = LDB_SUCCESS;
272 ac = talloc_get_type(req->context, struct la_context);
276 return ldb_module_done(ac->req, NULL, NULL,
277 LDB_ERR_OPERATIONS_ERROR);
279 if (ares->error != LDB_SUCCESS) {
280 return ldb_module_done(ac->req, ares->controls,
281 ares->response, ares->error);
284 /* Only entries are interesting, and we only want the olddn */
285 switch (ares->type) {
286 case LDB_REPLY_ENTRY:
288 if (ldb_dn_compare(ares->message->dn, ac->req->op.mod.message->dn) != 0) {
289 /* Guh? We only asked for this DN */
290 ldb_oom(ac->module->ldb);
292 return ldb_module_done(ac->req, NULL, NULL,
293 LDB_ERR_OPERATIONS_ERROR);
296 dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
298 for (i = 0; i < rc->num_elements; i++) {
300 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
302 ldb_asprintf_errstring(ac->module->ldb,
303 "attribute %s is not a valid attribute in schema",
306 return ldb_module_done(ac->req, NULL, NULL,
307 LDB_ERR_OBJECT_CLASS_VIOLATION);
310 search_el = ldb_msg_find_element(ares->message,
313 /* See if this element already exists */
314 /* otherwise just ignore as
315 * the add has already been scheduled */
320 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
323 * windows 2003 has a broken schema where
324 * the definition of msDS-IsDomainFor
325 * is missing (which is supposed to be
326 * the backlink of the msDS-HasDomainNCs
331 attr_name = target_attr->lDAPDisplayName;
333 /* make sure we manage each value */
334 for (j = 0; j < search_el->num_values; j++) {
335 ret = la_store_op(ac, LA_OP_DEL,
336 &search_el->values[j],
338 if (ret != LDB_SUCCESS) {
340 return ldb_module_done(ac->req,
348 case LDB_REPLY_REFERRAL:
356 /* Start with the original request */
357 ret = la_down_req(ac);
358 if (ret != LDB_SUCCESS) {
359 return ldb_module_done(ac->req, NULL, NULL, ret);
370 static int linked_attributes_modify(struct ldb_module *module, struct ldb_request *req)
372 /* Look over list of modifications */
373 /* Find if any are for linked attributes */
374 /* Determine the effect of the modification */
375 /* Apply the modify to the linked entry */
378 struct la_context *ac;
379 struct ldb_request *search_req;
382 if (ldb_dn_is_special(req->op.mod.message->dn)) {
383 /* do not manipulate our control entries */
384 return ldb_next_request(module, req);
387 ac = linked_attributes_init(module, req);
389 return LDB_ERR_OPERATIONS_ERROR;
393 /* without schema, this doesn't make any sense */
394 return ldb_next_request(module, req);
399 for (i=0; i < req->op.mod.message->num_elements; i++) {
400 bool store_el = false;
401 const char *attr_name;
402 const char *attr_val;
403 const struct dsdb_attribute *target_attr;
404 const struct ldb_message_element *el = &req->op.mod.message->elements[i];
405 const struct dsdb_attribute *schema_attr
406 = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
408 ldb_asprintf_errstring(module->ldb,
409 "attribute %s is not a valid attribute in schema", el->name);
410 return LDB_ERR_OBJECT_CLASS_VIOLATION;
412 /* We have a valid attribute, now find out if it is linked */
413 if (schema_attr->linkID == 0) {
417 if ((schema_attr->linkID & 1) == 1) {
418 /* Odd is for the target. Illegal to modify */
419 ldb_asprintf_errstring(module->ldb,
420 "attribute %s must not be modified directly, it is a linked attribute", el->name);
421 return LDB_ERR_UNWILLING_TO_PERFORM;
424 /* Even link IDs are for the originating attribute */
426 /* Now find the target attribute */
427 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
430 * windows 2003 has a broken schema where
431 * the definition of msDS-IsDomainFor
432 * is missing (which is supposed to be
433 * the backlink of the msDS-HasDomainNCs
439 attr_name = target_attr->lDAPDisplayName;
440 attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
442 switch (el->flags & LDB_FLAG_MOD_MASK) {
443 case LDB_FLAG_MOD_REPLACE:
444 /* treat as just a normal add the delete part is handled by the callback */
447 /* break intentionally missing */
449 case LDB_FLAG_MOD_ADD:
451 /* For each value being added, we need to setup the adds */
452 for (j = 0; j < el->num_values; j++) {
453 ret = la_store_op(ac, LA_OP_ADD,
455 attr_name, attr_val);
456 if (ret != LDB_SUCCESS) {
462 case LDB_FLAG_MOD_DELETE:
464 if (el->num_values) {
465 /* For each value being deleted, we need to setup the delete */
466 for (j = 0; j < el->num_values; j++) {
467 ret = la_store_op(ac, LA_OP_DEL,
469 attr_name, attr_val);
470 if (ret != LDB_SUCCESS) {
475 /* Flag that there was a DELETE
476 * without a value specified, so we
477 * need to look for the old value */
485 struct ldb_message_element *search_el;
487 /* Fill out ac->rc only if we have to find the old values */
489 ac->rc = talloc_zero(ac, struct replace_context);
491 ldb_oom(module->ldb);
492 return LDB_ERR_OPERATIONS_ERROR;
496 search_el = talloc_realloc(ac->rc, ac->rc->el,
497 struct ldb_message_element,
498 ac->rc->num_elements +1);
500 ldb_oom(module->ldb);
501 return LDB_ERR_OPERATIONS_ERROR;
503 ac->rc->el = search_el;
505 ac->rc->el[ac->rc->num_elements] = *el;
506 ac->rc->num_elements++;
510 /* both replace and delete without values are handled in the callback
511 * after the search on the entry to be modified is performed */
513 /* Only bother doing a search of this entry (to find old
514 * values) if replace or delete operations are attempted */
518 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
520 ldb_oom(module->ldb);
521 return LDB_ERR_OPERATIONS_ERROR;
523 for (i = 0; i < ac->rc->num_elements; i++) {
524 attrs[i] = ac->rc->el[i].name;
528 /* The callback does all the hard work here */
529 ret = ldb_build_search_req(&search_req, module->ldb, ac,
530 req->op.mod.message->dn,
532 "(objectClass=*)", attrs,
534 ac, la_mod_search_callback,
537 if (ret == LDB_SUCCESS) {
538 talloc_steal(search_req, attrs);
540 ret = ldb_next_request(module, search_req);
546 /* Start with the original request */
547 ret = la_down_req(ac);
549 /* nothing to do for this module, proceed */
551 ret = ldb_next_request(module, req);
559 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
561 struct ldb_request *search_req;
562 struct ldb_dn *base_dn;
563 struct la_context *ac;
568 /* This gets complex: We need to:
569 - Do a search for the entry
570 - Wait for these result to appear
571 - In the callback for the result, issue a modify
572 request based on the linked attributes found
573 - Wait for each modify result
577 switch (req->operation) {
579 base_dn = req->op.rename.olddn;
582 base_dn = req->op.del.dn;
585 return LDB_ERR_OPERATIONS_ERROR;
588 ac = linked_attributes_init(module, req);
590 return LDB_ERR_OPERATIONS_ERROR;
594 /* without schema, this doesn't make any sense */
595 return ldb_next_request(module, req);
598 werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
599 if (!W_ERROR_IS_OK(werr)) {
600 return LDB_ERR_OPERATIONS_ERROR;
603 ret = ldb_build_search_req(&search_req, module->ldb, req,
604 base_dn, LDB_SCOPE_BASE,
605 "(objectClass=*)", attrs,
607 ac, la_op_search_callback,
610 if (ret != LDB_SUCCESS) {
614 talloc_steal(search_req, attrs);
616 return ldb_next_request(module, search_req);
619 static int la_op_search_callback(struct ldb_request *req,
620 struct ldb_reply *ares)
622 struct la_context *ac;
623 const struct dsdb_attribute *schema_attr;
624 const struct dsdb_attribute *target_attr;
625 const struct ldb_message_element *el;
626 const char *attr_name;
632 ac = talloc_get_type(req->context, struct la_context);
635 return ldb_module_done(ac->req, NULL, NULL,
636 LDB_ERR_OPERATIONS_ERROR);
638 if (ares->error != LDB_SUCCESS) {
639 return ldb_module_done(ac->req, ares->controls,
640 ares->response, ares->error);
643 /* Only entries are interesting, and we only want the olddn */
644 switch (ares->type) {
645 case LDB_REPLY_ENTRY:
646 ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
648 /* Guh? We only asked for this DN */
650 return ldb_module_done(ac->req, NULL, NULL,
651 LDB_ERR_OPERATIONS_ERROR);
653 if (ares->message->num_elements == 0) {
654 /* only bother at all if there were some
655 * linked attributes found */
660 switch (ac->req->operation) {
662 deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
666 deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
667 adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
671 return ldb_module_done(ac->req, NULL, NULL,
672 LDB_ERR_OPERATIONS_ERROR);
675 for (i = 0; i < ares->message->num_elements; i++) {
676 el = &ares->message->elements[i];
678 schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
680 ldb_asprintf_errstring(ac->module->ldb,
681 "attribute %s is not a valid attribute"
682 " in schema", el->name);
684 return ldb_module_done(ac->req, NULL, NULL,
685 LDB_ERR_OBJECT_CLASS_VIOLATION);
688 /* Valid attribute, now find out if it is linked */
689 if (schema_attr->linkID == 0) {
690 /* Not a linked attribute, skip */
694 if ((schema_attr->linkID & 1) == 0) {
695 /* Odd is for the target. */
696 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
700 attr_name = target_attr->lDAPDisplayName;
702 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
706 attr_name = target_attr->lDAPDisplayName;
708 for (j = 0; j < el->num_values; j++) {
709 ret = la_store_op(ac, LA_OP_DEL,
712 if (ret != LDB_SUCCESS) {
714 return ldb_module_done(ac->req,
717 if (!adddn) continue;
718 ret = la_store_op(ac, LA_OP_ADD,
721 if (ret != LDB_SUCCESS) {
723 return ldb_module_done(ac->req,
731 case LDB_REPLY_REFERRAL:
739 /* start the mod requests chain */
740 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 /* If we still have modifies in the queue, then run them */
845 ret = la_do_mod_request(ac);
847 /* Otherwise, we are done! */
848 ret = ldb_module_done(ac->req, ares->controls,
849 ares->response, ares->error);
852 if (ret != LDB_SUCCESS) {
853 return ldb_module_done(ac->req, NULL, NULL, ret);
858 static int la_down_req(struct la_context *ac)
860 struct ldb_request *down_req;
863 switch (ac->req->operation) {
865 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
866 ac->req->op.add.message,
868 ac, la_down_callback,
872 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
873 ac->req->op.mod.message,
875 ac, la_down_callback,
879 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
882 ac, la_down_callback,
886 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
887 ac->req->op.rename.olddn,
888 ac->req->op.rename.newdn,
890 ac, la_down_callback,
894 ret = LDB_ERR_OPERATIONS_ERROR;
896 if (ret != LDB_SUCCESS) {
900 return ldb_next_request(ac->module, down_req);
903 /* Having done the original operation, then try to fix up all the linked attributes */
904 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
906 struct la_context *ac;
908 ac = talloc_get_type(req->context, struct la_context);
911 return ldb_module_done(ac->req, NULL, NULL,
912 LDB_ERR_OPERATIONS_ERROR);
914 if (ares->error != LDB_SUCCESS) {
915 return ldb_module_done(ac->req, ares->controls,
916 ares->response, ares->error);
919 if (ares->type != LDB_REPLY_DONE) {
920 ldb_set_errstring(ac->module->ldb,
921 "invalid ldb_reply_type in callback");
923 return ldb_module_done(ac->req, NULL, NULL,
924 LDB_ERR_OPERATIONS_ERROR);
926 /* If we have modfies to make, then run them */
928 return la_do_mod_request(ac);
930 return ldb_module_done(ac->req, ares->controls,
931 ares->response, ares->error);
935 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
936 .name = "linked_attributes",
937 .add = linked_attributes_add,
938 .modify = linked_attributes_modify,
939 .del = linked_attributes_op,
940 .rename = linked_attributes_op,