a54a7d64726b26c677b374b86a40869bfd9d10b6
[kai/samba.git] / source4 / dsdb / samdb / ldb_modules / linked_attributes.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
5    Copyright (C) Simo Sorce <idra@samba.org> 2008
6
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.
11    
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.
16    
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/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb linked_attributes module
25  *
26  *  Description: Module to ensure linked attribute pairs remain in sync
27  *
28  *  Author: Andrew Bartlett
29  */
30
31 #include "includes.h"
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"
36
37 struct la_op_store {
38         struct la_op_store *next;
39         enum la_op {LA_OP_ADD, LA_OP_DEL} op;
40         struct ldb_dn *dn;
41         char *name;
42         char *value;
43 };
44
45 struct replace_context {
46         struct la_context *ac;
47         unsigned int num_elements;
48         struct ldb_message_element *el;
49 };
50
51 struct la_context {
52         const struct dsdb_schema *schema;
53         struct ldb_module *module;
54         struct ldb_request *req;
55
56         struct replace_context *rc;
57         struct la_op_store *ops;
58         struct la_op_store *cur;
59 };
60
61 static struct la_context *linked_attributes_init(struct ldb_module *module,
62                                                  struct ldb_request *req)
63 {
64         struct la_context *ac;
65
66         ac = talloc_zero(req, struct la_context);
67         if (ac == NULL) {
68                 ldb_set_errstring(module->ldb, "Out of Memory");
69                 return NULL;
70         }
71
72         ac->schema = dsdb_get_schema(module->ldb);
73         ac->module = module;
74         ac->req = req;
75
76         return ac;
77 }
78
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)
84 {
85         struct la_op_store *os, *tmp;
86         struct ldb_dn *op_dn;
87
88         op_dn = ldb_dn_from_ldb_val(ac, ac->module->ldb, dn);
89         if (!op_dn) {
90                 return LDB_ERR_OPERATIONS_ERROR;
91         }
92
93         /* optimize out del - add operations that would end up
94          * with no changes */
95         if (ac->ops && op == LA_OP_DEL) {
96                 /* do a linear search to find out if there is
97                  * an equivalent add */
98                 os = ac->ops;
99                 while (os->next) {
100
101                         tmp = os->next;
102                         if (tmp->op == LA_OP_ADD) {
103
104                                 if ((strcmp(name, tmp->name) == 0) &&
105                                     (strcmp(value, tmp->value) == 0) &&
106                                     (ldb_dn_compare(op_dn, tmp->dn) == 0)) {
107
108                                         break;
109                                 }
110                         }
111                         os = os->next;
112                 }
113                 if (os->next) {
114                         /* pair found, remove it and return */
115                         os->next = tmp->next;
116                         talloc_free(tmp);
117                         talloc_free(op_dn);
118                         return LDB_SUCCESS;
119                 }
120         }
121
122         os = talloc_zero(ac, struct la_op_store);
123         if (!os) {
124                 return LDB_ERR_OPERATIONS_ERROR;
125         }
126
127         os->op = op;
128
129         os->dn = talloc_steal(os, op_dn);
130         if (!os->dn) {
131                 return LDB_ERR_OPERATIONS_ERROR;
132         }
133
134         os->name = talloc_strdup(os, name);
135         if (!os->name) {
136                 return LDB_ERR_OPERATIONS_ERROR;
137         }
138
139         if ((op != LA_OP_DEL) && (value == NULL)) {
140                 return LDB_ERR_OPERATIONS_ERROR;
141         }
142         if (value) {
143                 os->value = talloc_strdup(os, value);
144                 if (!os->value) {
145                         return LDB_ERR_OPERATIONS_ERROR;
146                 }
147         }
148
149         if (ac->ops) {
150                 ac->cur->next = os;
151         } else {
152                 ac->ops = os;
153         }
154         ac->cur = os;
155
156         return LDB_SUCCESS;
157 }
158
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);
167
168
169
170 /* add */
171 static int linked_attributes_add(struct ldb_module *module, struct ldb_request *req)
172 {
173         const struct dsdb_attribute *target_attr;
174         struct la_context *ac;
175         const char *attr_name;
176         const char *attr_val;
177         int ret;
178         int i, j;
179
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);
183         }
184
185         ac = linked_attributes_init(module, req);
186         if (!ac) {
187                 return LDB_ERR_OPERATIONS_ERROR;
188         }
189
190         if (!ac->schema) {
191                 /* without schema, this doesn't make any sense */
192                 talloc_free(ac);
193                 return ldb_next_request(module, req);
194         }
195
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);
201                 if (!schema_attr) {
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;                  
205                 }
206                 /* We have a valid attribute, now find out if it is linked */
207                 if (schema_attr->linkID == 0) {
208                         continue;
209                 }
210                 
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;
216                 }
217                 
218                 /* Even link IDs are for the originating attribute */
219                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
220                 if (!target_attr) {
221                         /*
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
226                          * attribute
227                          */
228                         continue;
229                 }
230
231                 attr_name = target_attr->lDAPDisplayName;
232                 attr_val = ldb_dn_get_linearized(ac->req->op.add.message->dn);
233
234                 for (j = 0; j < el->num_values; j++) {
235                         ret = la_store_op(ac, LA_OP_ADD,
236                                           &el->values[j],
237                                           attr_name, attr_val);
238                         if (ret != LDB_SUCCESS) {
239                                 return ret;
240                         }
241                 }
242         }
243
244         /* if no linked attributes are present continue */
245         if (ac->ops == NULL) {
246                 talloc_free(ac);
247                 return ldb_next_request(module, req);
248         }
249
250         /* start with the first one */
251         return la_do_mod_request(ac);
252 }
253
254 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
255 {
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;
262         const char *dn;
263         int i, j;
264         int ret = LDB_SUCCESS;
265
266         ac = talloc_get_type(req->context, struct la_context);
267         rc = ac->rc;
268
269         if (!ares) {
270                 return ldb_module_done(ac->req, NULL, NULL,
271                                         LDB_ERR_OPERATIONS_ERROR);
272         }
273         if (ares->error != LDB_SUCCESS) {
274                 return ldb_module_done(ac->req, ares->controls,
275                                         ares->response, ares->error);
276         }
277
278         /* Only entries are interesting, and we only want the olddn */
279         switch (ares->type) {
280         case LDB_REPLY_ENTRY:
281
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);
285                         talloc_free(ares);
286                         return ldb_module_done(ac->req, NULL, NULL,
287                                                 LDB_ERR_OPERATIONS_ERROR);
288                 }
289
290                 dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
291
292                 for (i = 0; i < rc->num_elements; i++) {
293
294                         schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
295                         if (!schema_attr) {
296                                 ldb_asprintf_errstring(ac->module->ldb,
297                                         "attribute %s is not a valid attribute in schema",
298                                         rc->el[i].name);
299                                 talloc_free(ares);
300                                 return ldb_module_done(ac->req, NULL, NULL,
301                                                 LDB_ERR_OBJECT_CLASS_VIOLATION);
302                         }
303
304                         search_el = ldb_msg_find_element(ares->message,
305                                                          rc->el[i].name);
306
307                         /* See if this element already exists */
308                         /* otherwise just ignore as
309                          * the add has already been scheduled */
310                         if ( ! search_el) {
311                                 continue;
312                         }
313
314                         target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
315                         if (!target_attr) {
316                                 /*
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
321                                  * attribute
322                                  */
323                                 continue;
324                         }
325                         attr_name = target_attr->lDAPDisplayName;
326
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],
331                                                   attr_name, dn);
332                                 if (ret != LDB_SUCCESS) {
333                                         talloc_free(ares);
334                                         return ldb_module_done(ac->req,
335                                                                NULL, NULL, ret);
336                                 }
337                         }
338                 }
339
340                 break;
341
342         case LDB_REPLY_REFERRAL:
343                 /* ignore */
344                 break;
345
346         case LDB_REPLY_DONE:
347
348                 talloc_free(ares);
349
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);
354                 }
355                 return LDB_SUCCESS;
356         }
357
358         talloc_free(ares);
359         return ret;
360 }
361
362
363 /* modify */
364 static int linked_attributes_modify(struct ldb_module *module, struct ldb_request *req)
365 {
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 */
370
371         int i, j;
372         struct la_context *ac;
373         struct ldb_request *search_req;
374         int ret;
375
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);
379         }
380
381         ac = linked_attributes_init(module, req);
382         if (!ac) {
383                 return LDB_ERR_OPERATIONS_ERROR;
384         }
385
386         if (!ac->schema) {
387                 /* without schema, this doesn't make any sense */
388                 return ldb_next_request(module, req);
389         }
390
391         ac->rc = NULL;
392
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);
401                 if (!schema_attr) {
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;                  
405                 }
406                 /* We have a valid attribute, now find out if it is linked */
407                 if (schema_attr->linkID == 0) {
408                         continue;
409                 }
410                 
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;
416                 }
417                 
418                 /* Even link IDs are for the originating attribute */
419                 
420                 /* Now find the target attribute */
421                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
422                 if (!target_attr) {
423                         /*
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
428                          * attribute
429                          */
430                         continue;
431                 }
432
433                 attr_name = target_attr->lDAPDisplayName;
434                 attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
435
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 */
439                         store_el = true;
440
441                         /* break intentionally missing */
442
443                 case LDB_FLAG_MOD_ADD:
444
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                                                   &el->values[j],
449                                                   attr_name, attr_val);
450                                 if (ret != LDB_SUCCESS) {
451                                         return ret;
452                                 }
453                         }
454                         break;
455
456                 case LDB_FLAG_MOD_DELETE:
457
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                                                           &el->values[j],
463                                                           attr_name, attr_val);
464                                         if (ret != LDB_SUCCESS) {
465                                                 return ret;
466                                         }
467                                 }
468                         } else {
469                                 /* Flag that there was a DELETE
470                                  * without a value specified, so we
471                                  * need to look for the old value */
472                                 store_el = true;
473                         }
474
475                         break;
476                 }
477
478                 if (store_el) {
479                         struct ldb_message_element *search_el;
480
481                         /* Fill out ac->rc only if we have to find the old values */
482                         if (!ac->rc) {
483                                 ac->rc = talloc_zero(ac, struct replace_context);
484                                 if (!ac->rc) {
485                                         ldb_oom(module->ldb);
486                                         return LDB_ERR_OPERATIONS_ERROR;
487                                 }
488                         }
489
490                         search_el = talloc_realloc(ac->rc, ac->rc->el,
491                                                    struct ldb_message_element,
492                                                    ac->rc->num_elements +1);
493                         if (!search_el) {
494                                 ldb_oom(module->ldb);
495                                 return LDB_ERR_OPERATIONS_ERROR;
496                         }
497                         ac->rc->el = search_el;
498
499                         ac->rc->el[ac->rc->num_elements] = *el;
500                         ac->rc->num_elements++;
501                 }
502         }
503
504         /* both replace and delete without values are handled in the callback
505          * after the search on the entry to be modified is performed */
506
507         /* Only bother doing a search of this entry (to find old
508          * values) if replace or delete operations are attempted */
509         if (ac->rc) {
510                 const char **attrs;
511
512                 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
513                 if (!attrs) {
514                         ldb_oom(module->ldb);
515                         return LDB_ERR_OPERATIONS_ERROR;
516                 }
517                 for (i = 0; i < ac->rc->num_elements; i++) {
518                         attrs[i] = ac->rc->el[i].name;
519                 }
520                 attrs[i] = NULL;
521
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,
525                                            LDB_SCOPE_BASE,
526                                            "(objectClass=*)", attrs,
527                                            NULL,
528                                            ac, la_mod_search_callback,
529                                            req);
530
531                 if (ret == LDB_SUCCESS) {
532                         talloc_steal(search_req, attrs);
533
534                         ret = ldb_next_request(module, search_req);
535                 }
536
537                 
538         } else {
539                 if (ac->ops) {
540                         /* Jump directly to handling the modifies */
541                         ret = la_do_mod_request(ac);
542                 } else {
543                         /* nothing to do for this module, proceed */
544                         talloc_free(ac);
545                         ret = ldb_next_request(module, req);
546                 }
547         }
548
549         return ret;
550 }
551
552 /* delete, rename */
553 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
554 {
555         struct ldb_request *search_req;
556         struct ldb_dn *base_dn;
557         struct la_context *ac;
558         const char **attrs;
559         WERROR werr;
560         int ret;
561
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
568            - Regain our sainity
569         */
570
571         switch (req->operation) {
572         case LDB_RENAME:
573                 base_dn = req->op.rename.olddn;
574                 break;
575         case LDB_DELETE:
576                 base_dn = req->op.del.dn;
577                 break;
578         default:
579                 return LDB_ERR_OPERATIONS_ERROR;
580         }
581
582         ac = linked_attributes_init(module, req);
583         if (!ac) {
584                 return LDB_ERR_OPERATIONS_ERROR;
585         }
586
587         if (!ac->schema) {
588                 /* without schema, this doesn't make any sense */
589                 return ldb_next_request(module, req);
590         }
591
592         werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
593         if (!W_ERROR_IS_OK(werr)) {
594                 return LDB_ERR_OPERATIONS_ERROR;
595         }
596
597         ret = ldb_build_search_req(&search_req, module->ldb, req,
598                                    base_dn, LDB_SCOPE_BASE,
599                                    "(objectClass=*)", attrs,
600                                    NULL,
601                                    ac, la_op_search_callback,
602                                    req);
603
604         if (ret != LDB_SUCCESS) {
605                 return ret;
606         }
607
608         talloc_steal(search_req, attrs);
609
610         return ldb_next_request(module, search_req);
611 }
612
613 static int la_op_search_callback(struct ldb_request *req,
614                                  struct ldb_reply *ares)
615 {
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;
621         const char *deldn;
622         const char *adddn;
623         int i, j;
624         int ret;
625
626         ac = talloc_get_type(req->context, struct la_context);
627
628         if (!ares) {
629                 return ldb_module_done(ac->req, NULL, NULL,
630                                         LDB_ERR_OPERATIONS_ERROR);
631         }
632         if (ares->error != LDB_SUCCESS) {
633                 return ldb_module_done(ac->req, ares->controls,
634                                         ares->response, ares->error);
635         }
636
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);
641                 if (ret != 0) {
642                         /* Guh?  We only asked for this DN */
643                         talloc_free(ares);
644                         return ldb_module_done(ac->req, NULL, NULL,
645                                                 LDB_ERR_OPERATIONS_ERROR);
646                 }
647                 if (ares->message->num_elements == 0) {
648                         /* only bother at all if there were some
649                          * linked attributes found */
650                         talloc_free(ares);
651                         return LDB_SUCCESS;
652                 }
653
654                 switch (ac->req->operation) {
655                 case LDB_DELETE:
656                         deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
657                         adddn = NULL;
658                         break;
659                 case LDB_RENAME:
660                         deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
661                         adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
662                         break;
663                 default:
664                         talloc_free(ares);
665                         return ldb_module_done(ac->req, NULL, NULL,
666                                                 LDB_ERR_OPERATIONS_ERROR);
667                 }
668
669                 for (i = 0; i < ares->message->num_elements; i++) {
670                         el = &ares->message->elements[i];
671
672                         schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
673                         if (!schema_attr) {
674                                 ldb_asprintf_errstring(ac->module->ldb,
675                                         "attribute %s is not a valid attribute"
676                                         " in schema", el->name);
677                                 talloc_free(ares);
678                                 return ldb_module_done(ac->req, NULL, NULL,
679                                                 LDB_ERR_OBJECT_CLASS_VIOLATION);
680                         }
681
682                         /* Valid attribute, now find out if it is linked */
683                         if (schema_attr->linkID == 0) {
684                                 /* Not a linked attribute, skip */
685                                 continue;
686                         }
687
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);
691                                 if (!target_attr) {
692                                         continue;
693                                 }
694                                 attr_name = target_attr->lDAPDisplayName;
695                         } else {
696                                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
697                                 if (!target_attr) {
698                                         continue;
699                                 }
700                                 attr_name = target_attr->lDAPDisplayName;
701                         }
702                         for (j = 0; j < el->num_values; j++) {
703                                 ret = la_store_op(ac, LA_OP_DEL,
704                                                   &el->values[j],
705                                                   attr_name, deldn);
706                                 if (ret != LDB_SUCCESS) {
707                                         talloc_free(ares);
708                                         return ldb_module_done(ac->req,
709                                                                NULL, NULL, ret);
710                                 }
711                                 if (!adddn) continue;
712                                 ret = la_store_op(ac, LA_OP_ADD,
713                                                   &el->values[j],
714                                                   attr_name, adddn);
715                                 if (ret != LDB_SUCCESS) {
716                                         talloc_free(ares);
717                                         return ldb_module_done(ac->req,
718                                                                NULL, NULL, ret);
719                                 }
720                         }
721                 }
722
723                 break;
724
725         case LDB_REPLY_REFERRAL:
726                 /* ignore */
727                 break;
728
729         case LDB_REPLY_DONE:
730
731                 talloc_free(ares);
732
733                 if (ac->ops) {
734                         /* start the mod requests chain */
735                         ret = la_do_mod_request(ac);
736                 } else {
737                         ret = la_down_req(ac);
738                 }
739                 if (ret != LDB_SUCCESS) {
740                         return ldb_module_done(ac->req, NULL, NULL, ret);
741                 }
742                 return LDB_SUCCESS;
743         }
744
745         talloc_free(ares);
746         return LDB_SUCCESS;
747 }
748
749 /* do a linked attributes modify request */
750 static int la_do_mod_request(struct la_context *ac)
751 {
752         struct ldb_message_element *ret_el;
753         struct ldb_request *mod_req;
754         struct ldb_message *new_msg;
755         struct ldb_context *ldb;
756         int ret;
757
758         ldb = ac->module->ldb;
759
760         /* Create the modify request */
761         new_msg = ldb_msg_new(ac);
762         if (!new_msg) {
763                 ldb_oom(ldb);
764                 return LDB_ERR_OPERATIONS_ERROR;
765         }
766         new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
767         if (!new_msg->dn) {
768                 return LDB_ERR_OPERATIONS_ERROR;
769         }
770
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);
774         } else {
775                 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
776                                         LDB_FLAG_MOD_DELETE, &ret_el);
777         }
778         if (ret != LDB_SUCCESS) {
779                 return ret;
780         }
781         ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
782         if (!ret_el->values) {
783                 ldb_oom(ldb);
784                 return LDB_ERR_OPERATIONS_ERROR;
785         }
786         ret_el->values[0] = data_blob_string_const(ac->ops->value);
787         ret_el->num_values = 1;
788
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,
792                                 new_msg,
793                                 NULL,
794                                 ac, la_mod_callback,
795                                 ac->req);
796         if (ret != LDB_SUCCESS) {
797                 return ret;
798         }
799         talloc_steal(mod_req, new_msg);
800
801         /* Run the new request */
802         return ldb_next_request(ac->module, mod_req);
803 }
804
805 static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
806 {
807         struct la_context *ac;
808         struct la_op_store *os;
809         int ret;
810
811         ac = talloc_get_type(req->context, struct la_context);
812
813         if (!ares) {
814                 return ldb_module_done(ac->req, NULL, NULL,
815                                         LDB_ERR_OPERATIONS_ERROR);
816         }
817         if (ares->error != LDB_SUCCESS) {
818                 return ldb_module_done(ac->req, ares->controls,
819                                         ares->response, ares->error);
820         }
821
822         if (ares->type != LDB_REPLY_DONE) {
823                 ldb_set_errstring(ac->module->ldb,
824                                   "invalid ldb_reply_type in callback");
825                 talloc_free(ares);
826                 return ldb_module_done(ac->req, NULL, NULL,
827                                         LDB_ERR_OPERATIONS_ERROR);
828         }
829
830         talloc_free(ares);
831
832         if (ac->ops) {
833                 os = ac->ops;
834                 ac->ops = os->next;
835
836                 /* this frees the request too
837                  * DO NOT access 'req' after this point */
838                 talloc_free(os);
839         }
840
841         /* as last op run the original request */
842         if (ac->ops) {
843                 ret = la_do_mod_request(ac);
844         } else {
845                 ret = la_down_req(ac);
846         }
847
848         if (ret != LDB_SUCCESS) {
849                 return ldb_module_done(ac->req, NULL, NULL, ret);
850         }
851         return LDB_SUCCESS;
852 }
853
854 static int la_down_req(struct la_context *ac)
855 {
856         struct ldb_request *down_req;
857         int ret;
858
859         switch (ac->req->operation) {
860         case LDB_ADD:
861                 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
862                                         ac->req->op.add.message,
863                                         ac->req->controls,
864                                         ac, la_down_callback,
865                                         ac->req);
866                 break;
867         case LDB_MODIFY:
868                 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
869                                         ac->req->op.mod.message,
870                                         ac->req->controls,
871                                         ac, la_down_callback,
872                                         ac->req);
873                 break;
874         case LDB_DELETE:
875                 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
876                                         ac->req->op.del.dn,
877                                         ac->req->controls,
878                                         ac, la_down_callback,
879                                         ac->req);
880                 break;
881         case LDB_RENAME:
882                 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
883                                            ac->req->op.rename.olddn,
884                                            ac->req->op.rename.newdn,
885                                            ac->req->controls,
886                                            ac, la_down_callback,
887                                            ac->req);
888                 break;
889         default:
890                 ret = LDB_ERR_OPERATIONS_ERROR;
891         }
892         if (ret != LDB_SUCCESS) {
893                 return ret;
894         }
895
896         return ldb_next_request(ac->module, down_req);
897 }
898
899 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
900 {
901         struct la_context *ac;
902
903         ac = talloc_get_type(req->context, struct la_context);
904
905         if (!ares) {
906                 return ldb_module_done(ac->req, NULL, NULL,
907                                         LDB_ERR_OPERATIONS_ERROR);
908         }
909         if (ares->error != LDB_SUCCESS) {
910                 return ldb_module_done(ac->req, ares->controls,
911                                         ares->response, ares->error);
912         }
913
914         if (ares->type != LDB_REPLY_DONE) {
915                 ldb_set_errstring(ac->module->ldb,
916                                   "invalid ldb_reply_type in callback");
917                 talloc_free(ares);
918                 return ldb_module_done(ac->req, NULL, NULL,
919                                         LDB_ERR_OPERATIONS_ERROR);
920         }
921
922         return ldb_module_done(ac->req, ares->controls,
923                                 ares->response, ares->error);
924 }
925
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,
932 };