Merge branch 'master' of ssh://git.samba.org/data/git/samba
[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 original request */
253         return la_down_req(ac);
254 }
255
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 */
259
260 static int la_mod_search_callback(struct ldb_request *req, struct ldb_reply *ares)
261 {
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;
268         const char *dn;
269         int i, j;
270         int ret = LDB_SUCCESS;
271
272         ac = talloc_get_type(req->context, struct la_context);
273         rc = ac->rc;
274
275         if (!ares) {
276                 return ldb_module_done(ac->req, NULL, NULL,
277                                         LDB_ERR_OPERATIONS_ERROR);
278         }
279         if (ares->error != LDB_SUCCESS) {
280                 return ldb_module_done(ac->req, ares->controls,
281                                         ares->response, ares->error);
282         }
283
284         /* Only entries are interesting, and we only want the olddn */
285         switch (ares->type) {
286         case LDB_REPLY_ENTRY:
287
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);
291                         talloc_free(ares);
292                         return ldb_module_done(ac->req, NULL, NULL,
293                                                 LDB_ERR_OPERATIONS_ERROR);
294                 }
295
296                 dn = ldb_dn_get_linearized(ac->req->op.add.message->dn);
297
298                 for (i = 0; i < rc->num_elements; i++) {
299
300                         schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, rc->el[i].name);
301                         if (!schema_attr) {
302                                 ldb_asprintf_errstring(ac->module->ldb,
303                                         "attribute %s is not a valid attribute in schema",
304                                         rc->el[i].name);
305                                 talloc_free(ares);
306                                 return ldb_module_done(ac->req, NULL, NULL,
307                                                 LDB_ERR_OBJECT_CLASS_VIOLATION);
308                         }
309
310                         search_el = ldb_msg_find_element(ares->message,
311                                                          rc->el[i].name);
312
313                         /* See if this element already exists */
314                         /* otherwise just ignore as
315                          * the add has already been scheduled */
316                         if ( ! search_el) {
317                                 continue;
318                         }
319
320                         target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
321                         if (!target_attr) {
322                                 /*
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
327                                  * attribute
328                                  */
329                                 continue;
330                         }
331                         attr_name = target_attr->lDAPDisplayName;
332
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],
337                                                   attr_name, dn);
338                                 if (ret != LDB_SUCCESS) {
339                                         talloc_free(ares);
340                                         return ldb_module_done(ac->req,
341                                                                NULL, NULL, ret);
342                                 }
343                         }
344                 }
345
346                 break;
347
348         case LDB_REPLY_REFERRAL:
349                 /* ignore */
350                 break;
351
352         case LDB_REPLY_DONE:
353
354                 talloc_free(ares);
355
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);
360                 }
361                 return LDB_SUCCESS;
362         }
363
364         talloc_free(ares);
365         return ret;
366 }
367
368
369 /* modify */
370 static int linked_attributes_modify(struct ldb_module *module, struct ldb_request *req)
371 {
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 */
376
377         int i, j;
378         struct la_context *ac;
379         struct ldb_request *search_req;
380         int ret;
381
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);
385         }
386
387         ac = linked_attributes_init(module, req);
388         if (!ac) {
389                 return LDB_ERR_OPERATIONS_ERROR;
390         }
391
392         if (!ac->schema) {
393                 /* without schema, this doesn't make any sense */
394                 return ldb_next_request(module, req);
395         }
396
397         ac->rc = NULL;
398
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);
407                 if (!schema_attr) {
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;                  
411                 }
412                 /* We have a valid attribute, now find out if it is linked */
413                 if (schema_attr->linkID == 0) {
414                         continue;
415                 }
416                 
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;
422                 }
423                 
424                 /* Even link IDs are for the originating attribute */
425                 
426                 /* Now find the target attribute */
427                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
428                 if (!target_attr) {
429                         /*
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
434                          * attribute
435                          */
436                         continue;
437                 }
438
439                 attr_name = target_attr->lDAPDisplayName;
440                 attr_val = ldb_dn_get_linearized(ac->req->op.mod.message->dn);
441
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 */
445                         store_el = true;
446
447                         /* break intentionally missing */
448
449                 case LDB_FLAG_MOD_ADD:
450
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,
454                                                   &el->values[j],
455                                                   attr_name, attr_val);
456                                 if (ret != LDB_SUCCESS) {
457                                         return ret;
458                                 }
459                         }
460                         break;
461
462                 case LDB_FLAG_MOD_DELETE:
463
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,
468                                                           &el->values[j],
469                                                           attr_name, attr_val);
470                                         if (ret != LDB_SUCCESS) {
471                                                 return ret;
472                                         }
473                                 }
474                         } else {
475                                 /* Flag that there was a DELETE
476                                  * without a value specified, so we
477                                  * need to look for the old value */
478                                 store_el = true;
479                         }
480
481                         break;
482                 }
483
484                 if (store_el) {
485                         struct ldb_message_element *search_el;
486
487                         /* Fill out ac->rc only if we have to find the old values */
488                         if (!ac->rc) {
489                                 ac->rc = talloc_zero(ac, struct replace_context);
490                                 if (!ac->rc) {
491                                         ldb_oom(module->ldb);
492                                         return LDB_ERR_OPERATIONS_ERROR;
493                                 }
494                         }
495
496                         search_el = talloc_realloc(ac->rc, ac->rc->el,
497                                                    struct ldb_message_element,
498                                                    ac->rc->num_elements +1);
499                         if (!search_el) {
500                                 ldb_oom(module->ldb);
501                                 return LDB_ERR_OPERATIONS_ERROR;
502                         }
503                         ac->rc->el = search_el;
504
505                         ac->rc->el[ac->rc->num_elements] = *el;
506                         ac->rc->num_elements++;
507                 }
508         }
509
510         /* both replace and delete without values are handled in the callback
511          * after the search on the entry to be modified is performed */
512
513         /* Only bother doing a search of this entry (to find old
514          * values) if replace or delete operations are attempted */
515         if (ac->rc) {
516                 const char **attrs;
517
518                 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
519                 if (!attrs) {
520                         ldb_oom(module->ldb);
521                         return LDB_ERR_OPERATIONS_ERROR;
522                 }
523                 for (i = 0; i < ac->rc->num_elements; i++) {
524                         attrs[i] = ac->rc->el[i].name;
525                 }
526                 attrs[i] = NULL;
527
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,
531                                            LDB_SCOPE_BASE,
532                                            "(objectClass=*)", attrs,
533                                            NULL,
534                                            ac, la_mod_search_callback,
535                                            req);
536
537                 if (ret == LDB_SUCCESS) {
538                         talloc_steal(search_req, attrs);
539
540                         ret = ldb_next_request(module, search_req);
541                 }
542
543                 
544         } else {
545                 if (ac->ops) {
546                         /* Start with the original request */
547                         ret = la_down_req(ac);
548                 } else {
549                         /* nothing to do for this module, proceed */
550                         talloc_free(ac);
551                         ret = ldb_next_request(module, req);
552                 }
553         }
554
555         return ret;
556 }
557
558 /* delete, rename */
559 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
560 {
561         struct ldb_request *search_req;
562         struct ldb_dn *base_dn;
563         struct la_context *ac;
564         const char **attrs;
565         WERROR werr;
566         int ret;
567
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
574            - Regain our sainity
575         */
576
577         switch (req->operation) {
578         case LDB_RENAME:
579                 base_dn = req->op.rename.olddn;
580                 break;
581         case LDB_DELETE:
582                 base_dn = req->op.del.dn;
583                 break;
584         default:
585                 return LDB_ERR_OPERATIONS_ERROR;
586         }
587
588         ac = linked_attributes_init(module, req);
589         if (!ac) {
590                 return LDB_ERR_OPERATIONS_ERROR;
591         }
592
593         if (!ac->schema) {
594                 /* without schema, this doesn't make any sense */
595                 return ldb_next_request(module, req);
596         }
597
598         werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
599         if (!W_ERROR_IS_OK(werr)) {
600                 return LDB_ERR_OPERATIONS_ERROR;
601         }
602
603         ret = ldb_build_search_req(&search_req, module->ldb, req,
604                                    base_dn, LDB_SCOPE_BASE,
605                                    "(objectClass=*)", attrs,
606                                    NULL,
607                                    ac, la_op_search_callback,
608                                    req);
609
610         if (ret != LDB_SUCCESS) {
611                 return ret;
612         }
613
614         talloc_steal(search_req, attrs);
615
616         return ldb_next_request(module, search_req);
617 }
618
619 static int la_op_search_callback(struct ldb_request *req,
620                                  struct ldb_reply *ares)
621 {
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;
627         const char *deldn;
628         const char *adddn;
629         int i, j;
630         int ret;
631
632         ac = talloc_get_type(req->context, struct la_context);
633
634         if (!ares) {
635                 return ldb_module_done(ac->req, NULL, NULL,
636                                         LDB_ERR_OPERATIONS_ERROR);
637         }
638         if (ares->error != LDB_SUCCESS) {
639                 return ldb_module_done(ac->req, ares->controls,
640                                         ares->response, ares->error);
641         }
642
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);
647                 if (ret != 0) {
648                         /* Guh?  We only asked for this DN */
649                         talloc_free(ares);
650                         return ldb_module_done(ac->req, NULL, NULL,
651                                                 LDB_ERR_OPERATIONS_ERROR);
652                 }
653                 if (ares->message->num_elements == 0) {
654                         /* only bother at all if there were some
655                          * linked attributes found */
656                         talloc_free(ares);
657                         return LDB_SUCCESS;
658                 }
659
660                 switch (ac->req->operation) {
661                 case LDB_DELETE:
662                         deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
663                         adddn = NULL;
664                         break;
665                 case LDB_RENAME:
666                         deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
667                         adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
668                         break;
669                 default:
670                         talloc_free(ares);
671                         return ldb_module_done(ac->req, NULL, NULL,
672                                                 LDB_ERR_OPERATIONS_ERROR);
673                 }
674
675                 for (i = 0; i < ares->message->num_elements; i++) {
676                         el = &ares->message->elements[i];
677
678                         schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
679                         if (!schema_attr) {
680                                 ldb_asprintf_errstring(ac->module->ldb,
681                                         "attribute %s is not a valid attribute"
682                                         " in schema", el->name);
683                                 talloc_free(ares);
684                                 return ldb_module_done(ac->req, NULL, NULL,
685                                                 LDB_ERR_OBJECT_CLASS_VIOLATION);
686                         }
687
688                         /* Valid attribute, now find out if it is linked */
689                         if (schema_attr->linkID == 0) {
690                                 /* Not a linked attribute, skip */
691                                 continue;
692                         }
693
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);
697                                 if (!target_attr) {
698                                         continue;
699                                 }
700                                 attr_name = target_attr->lDAPDisplayName;
701                         } else {
702                                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
703                                 if (!target_attr) {
704                                         continue;
705                                 }
706                                 attr_name = target_attr->lDAPDisplayName;
707                         }
708                         for (j = 0; j < el->num_values; j++) {
709                                 ret = la_store_op(ac, LA_OP_DEL,
710                                                   &el->values[j],
711                                                   attr_name, deldn);
712                                 if (ret != LDB_SUCCESS) {
713                                         talloc_free(ares);
714                                         return ldb_module_done(ac->req,
715                                                                NULL, NULL, ret);
716                                 }
717                                 if (!adddn) continue;
718                                 ret = la_store_op(ac, LA_OP_ADD,
719                                                   &el->values[j],
720                                                   attr_name, adddn);
721                                 if (ret != LDB_SUCCESS) {
722                                         talloc_free(ares);
723                                         return ldb_module_done(ac->req,
724                                                                NULL, NULL, ret);
725                                 }
726                         }
727                 }
728
729                 break;
730
731         case LDB_REPLY_REFERRAL:
732                 /* ignore */
733                 break;
734
735         case LDB_REPLY_DONE:
736
737                 talloc_free(ares);
738
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);
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         /* If we still have modifies in the queue, then run them */
844         if (ac->ops) {
845                 ret = la_do_mod_request(ac);
846         } else {
847                 /* Otherwise, we are done! */
848                 ret = ldb_module_done(ac->req, ares->controls,
849                                       ares->response, ares->error);
850         }
851
852         if (ret != LDB_SUCCESS) {
853                 return ldb_module_done(ac->req, NULL, NULL, ret);
854         }
855         return LDB_SUCCESS;
856 }
857
858 static int la_down_req(struct la_context *ac)
859 {
860         struct ldb_request *down_req;
861         int ret;
862
863         switch (ac->req->operation) {
864         case LDB_ADD:
865                 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
866                                         ac->req->op.add.message,
867                                         ac->req->controls,
868                                         ac, la_down_callback,
869                                         ac->req);
870                 break;
871         case LDB_MODIFY:
872                 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
873                                         ac->req->op.mod.message,
874                                         ac->req->controls,
875                                         ac, la_down_callback,
876                                         ac->req);
877                 break;
878         case LDB_DELETE:
879                 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
880                                         ac->req->op.del.dn,
881                                         ac->req->controls,
882                                         ac, la_down_callback,
883                                         ac->req);
884                 break;
885         case LDB_RENAME:
886                 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
887                                            ac->req->op.rename.olddn,
888                                            ac->req->op.rename.newdn,
889                                            ac->req->controls,
890                                            ac, la_down_callback,
891                                            ac->req);
892                 break;
893         default:
894                 ret = LDB_ERR_OPERATIONS_ERROR;
895         }
896         if (ret != LDB_SUCCESS) {
897                 return ret;
898         }
899
900         return ldb_next_request(ac->module, down_req);
901 }
902
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)
905 {
906         struct la_context *ac;
907
908         ac = talloc_get_type(req->context, struct la_context);
909
910         if (!ares) {
911                 return ldb_module_done(ac->req, NULL, NULL,
912                                         LDB_ERR_OPERATIONS_ERROR);
913         }
914         if (ares->error != LDB_SUCCESS) {
915                 return ldb_module_done(ac->req, ares->controls,
916                                         ares->response, ares->error);
917         }
918
919         if (ares->type != LDB_REPLY_DONE) {
920                 ldb_set_errstring(ac->module->ldb,
921                                   "invalid ldb_reply_type in callback");
922                 talloc_free(ares);
923                 return ldb_module_done(ac->req, NULL, NULL,
924                                         LDB_ERR_OPERATIONS_ERROR);
925         }
926         /* If we have modfies to make, then run them */
927         if (ac->ops) {
928                 return la_do_mod_request(ac);
929         } else {
930                 return ldb_module_done(ac->req, ares->controls,
931                                       ares->response, ares->error);
932         }
933 }
934
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,
941 };