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