14fd107d81d504f347f39bfde334121fb317ffee
[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, char *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_new(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.mod.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                                           (char *)el->values[j].data,
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                                           (char *)search_el->values[j].data,
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                                           (char *)el->values[j].data,
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                                                   (char *)el->values[j].data,
463                                                           attr_name, attr_val);
464                                         if (ret != LDB_SUCCESS) {
465                                                 return ret;
466                                         }
467                                 }
468                         } else {
469                                 store_el = true;
470                         }
471
472                         break;
473                 }
474
475                 if (store_el) {
476                         struct ldb_message_element *search_el;
477
478                         if (!ac->rc) {
479                                 ac->rc = talloc_zero(ac, struct replace_context);
480                                 if (!ac->rc) {
481                                         ldb_oom(module->ldb);
482                                         return LDB_ERR_OPERATIONS_ERROR;
483                                 }
484                         }
485
486                         search_el = talloc_realloc(ac->rc, ac->rc->el,
487                                                    struct ldb_message_element,
488                                                    ac->rc->num_elements +1);
489                         if (!search_el) {
490                                 ldb_oom(module->ldb);
491                                 return LDB_ERR_OPERATIONS_ERROR;
492                         }
493                         ac->rc->el = search_el;
494
495                         ac->rc->el[ac->rc->num_elements] = *el;
496                         ac->rc->num_elements++;
497                 }
498         }
499
500         /* both replace and delete without values are handled in the callback
501          * after the search on the entry to be modified is performed */
502         if (ac->rc) {
503                 const char **attrs;
504
505                 attrs = talloc_array(ac->rc, const char *, ac->rc->num_elements +1);
506                 if (!attrs) {
507                         ldb_oom(module->ldb);
508                         return LDB_ERR_OPERATIONS_ERROR;
509                 }
510                 for (i = 0; i < ac->rc->num_elements; i++) {
511                         attrs[i] = ac->rc->el[i].name;
512                 }
513                 attrs[i] = NULL;
514
515                 /* The callback does all the hard work here */
516                 ret = ldb_build_search_req(&search_req, module->ldb, ac,
517                                            req->op.mod.message->dn,
518                                            LDB_SCOPE_BASE,
519                                            "(objectClass=*)", attrs,
520                                            NULL,
521                                            ac, la_mod_search_callback,
522                                            req);
523
524                 if (ret == LDB_SUCCESS) {
525                         talloc_steal(search_req, attrs);
526
527                         ret = ldb_next_request(module, search_req);
528                 }
529
530         } else {
531                 if (ac->ops) {
532                         /* start the mod requests chain */
533                         ret = la_do_mod_request(ac);
534                 } else {
535                         /* nothing to do for this module, proceed */
536                         talloc_free(ac);
537                         ret = ldb_next_request(module, req);
538                 }
539         }
540
541         return ret;
542 }
543
544 /* delete, rename */
545 static int linked_attributes_op(struct ldb_module *module, struct ldb_request *req)
546 {
547         struct ldb_request *search_req;
548         struct ldb_dn *base_dn;
549         struct la_context *ac;
550         const char **attrs;
551         WERROR werr;
552         int ret;
553
554         /* This gets complex:  We need to:
555            - Do a search for the entry
556            - Wait for these result to appear
557            - In the callback for the result, issue a modify
558                 request based on the linked attributes found
559            - Wait for each modify result
560            - Regain our sainity
561         */
562
563         switch (req->operation) {
564         case LDB_RENAME:
565                 base_dn = req->op.rename.olddn;
566                 break;
567         case LDB_DELETE:
568                 base_dn = req->op.del.dn;
569                 break;
570         default:
571                 return LDB_ERR_OPERATIONS_ERROR;
572         }
573
574         ac = linked_attributes_init(module, req);
575         if (!ac) {
576                 return LDB_ERR_OPERATIONS_ERROR;
577         }
578
579         if (!ac->schema) {
580                 /* without schema, this doesn't make any sense */
581                 return ldb_next_request(module, req);
582         }
583
584         werr = dsdb_linked_attribute_lDAPDisplayName_list(ac->schema, ac, &attrs);
585         if (!W_ERROR_IS_OK(werr)) {
586                 return LDB_ERR_OPERATIONS_ERROR;
587         }
588
589         ret = ldb_build_search_req(&search_req, module->ldb, req,
590                                    base_dn, LDB_SCOPE_BASE,
591                                    "(objectClass=*)", attrs,
592                                    NULL,
593                                    ac, la_op_search_callback,
594                                    req);
595
596         if (ret != LDB_SUCCESS) {
597                 return ret;
598         }
599
600         talloc_steal(search_req, attrs);
601
602         return ldb_next_request(module, search_req);
603 }
604
605 static int la_op_search_callback(struct ldb_request *req,
606                                  struct ldb_reply *ares)
607 {
608         struct la_context *ac;
609         const struct dsdb_attribute *schema_attr;
610         const struct dsdb_attribute *target_attr;
611         const struct ldb_message_element *el;
612         const char *attr_name;
613         const char *deldn;
614         const char *adddn;
615         int i, j;
616         int ret;
617
618         ac = talloc_get_type(req->context, struct la_context);
619
620         if (!ares) {
621                 return ldb_module_done(ac->req, NULL, NULL,
622                                         LDB_ERR_OPERATIONS_ERROR);
623         }
624         if (ares->error != LDB_SUCCESS) {
625                 return ldb_module_done(ac->req, ares->controls,
626                                         ares->response, ares->error);
627         }
628
629         /* Only entries are interesting, and we only want the olddn */
630         switch (ares->type) {
631         case LDB_REPLY_ENTRY:
632                 ret = ldb_dn_compare(ares->message->dn, req->op.search.base);
633                 if (ret != 0) {
634                         /* Guh?  We only asked for this DN */
635                         talloc_free(ares);
636                         return ldb_module_done(ac->req, NULL, NULL,
637                                                 LDB_ERR_OPERATIONS_ERROR);
638                 }
639                 if (ares->message->num_elements == 0) {
640                         /* only bother at all if there were some
641                          * linked attributes found */
642                         talloc_free(ares);
643                         return LDB_SUCCESS;
644                 }
645
646                 switch (ac->req->operation) {
647                 case LDB_DELETE:
648                         deldn = ldb_dn_get_linearized(ac->req->op.del.dn);
649                         adddn = NULL;
650                         break;
651                 case LDB_RENAME:
652                         deldn = ldb_dn_get_linearized(ac->req->op.rename.olddn);
653                         adddn = ldb_dn_get_linearized(ac->req->op.rename.newdn);
654                         break;
655                 default:
656                         talloc_free(ares);
657                         return ldb_module_done(ac->req, NULL, NULL,
658                                                 LDB_ERR_OPERATIONS_ERROR);
659                 }
660
661                 for (i = 0; i < ares->message->num_elements; i++) {
662                         el = &ares->message->elements[i];
663
664                         schema_attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, el->name);
665                         if (!schema_attr) {
666                                 ldb_asprintf_errstring(ac->module->ldb,
667                                         "attribute %s is not a valid attribute"
668                                         " in schema", el->name);
669                                 talloc_free(ares);
670                                 return ldb_module_done(ac->req, NULL, NULL,
671                                                 LDB_ERR_OBJECT_CLASS_VIOLATION);
672                         }
673
674                         /* Valid attribute, now find out if it is linked */
675                         if (schema_attr->linkID == 0) {
676                                 /* Not a linked attribute, skip */
677                                 continue;
678                         }
679
680                         if ((schema_attr->linkID & 1) == 0) {
681                                 /* Odd is for the target. */
682                                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID + 1);
683                                 if (!target_attr) {
684                                         continue;
685                                 }
686                                 attr_name = target_attr->lDAPDisplayName;
687                         } else {
688                                 target_attr = dsdb_attribute_by_linkID(ac->schema, schema_attr->linkID - 1);
689                                 if (!target_attr) {
690                                         continue;
691                                 }
692                                 attr_name = target_attr->lDAPDisplayName;
693                         }
694                         for (j = 0; j < el->num_values; j++) {
695                                 ret = la_store_op(ac, LA_OP_DEL,
696                                           (char *)el->values[j].data,
697                                                   attr_name, deldn);
698                                 if (ret != LDB_SUCCESS) {
699                                         talloc_free(ares);
700                                         return ldb_module_done(ac->req,
701                                                                NULL, NULL, ret);
702                                 }
703                                 if (!adddn) continue;
704                                 ret = la_store_op(ac, LA_OP_ADD,
705                                           (char *)el->values[j].data,
706                                                   attr_name, adddn);
707                                 if (ret != LDB_SUCCESS) {
708                                         talloc_free(ares);
709                                         return ldb_module_done(ac->req,
710                                                                NULL, NULL, ret);
711                                 }
712                         }
713                 }
714
715                 break;
716
717         case LDB_REPLY_REFERRAL:
718                 /* ignore */
719                 break;
720
721         case LDB_REPLY_DONE:
722
723                 talloc_free(ares);
724
725                 if (ac->ops) {
726                         /* start the mod requests chain */
727                         ret = la_do_mod_request(ac);
728                 } else {
729                         ret = la_down_req(ac);
730                 }
731                 if (ret != LDB_SUCCESS) {
732                         return ldb_module_done(ac->req, NULL, NULL, ret);
733                 }
734                 return LDB_SUCCESS;
735         }
736
737         talloc_free(ares);
738         return LDB_SUCCESS;
739 }
740
741 /* do a linked attributes modify request */
742 static int la_do_mod_request(struct la_context *ac)
743 {
744         struct ldb_message_element *ret_el;
745         struct ldb_request *mod_req;
746         struct ldb_message *new_msg;
747         struct ldb_context *ldb;
748         int ret;
749
750         ldb = ac->module->ldb;
751
752         /* Create the modify request */
753         new_msg = ldb_msg_new(ac);
754         if (!new_msg) {
755                 ldb_oom(ldb);
756                 return LDB_ERR_OPERATIONS_ERROR;
757         }
758         new_msg->dn = ldb_dn_copy(new_msg, ac->ops->dn);
759         if (!new_msg->dn) {
760                 return LDB_ERR_OPERATIONS_ERROR;
761         }
762
763         if (ac->ops->op == LA_OP_ADD) {
764                 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
765                                         LDB_FLAG_MOD_ADD, &ret_el);
766         } else {
767                 ret = ldb_msg_add_empty(new_msg, ac->ops->name,
768                                         LDB_FLAG_MOD_DELETE, &ret_el);
769         }
770         if (ret != LDB_SUCCESS) {
771                 return ret;
772         }
773         ret_el->values = talloc_array(new_msg, struct ldb_val, 1);
774         if (!ret_el->values) {
775                 ldb_oom(ldb);
776                 return LDB_ERR_OPERATIONS_ERROR;
777         }
778         ret_el->values[0] = data_blob_string_const(ac->ops->value);
779         ret_el->num_values = 1;
780
781         /* use ac->ops as the mem_ctx so that the request will be freed
782          * in the callback as soon as completed */
783         ret = ldb_build_mod_req(&mod_req, ldb, ac->ops,
784                                 new_msg,
785                                 NULL,
786                                 ac, la_mod_callback,
787                                 ac->req);
788         if (ret != LDB_SUCCESS) {
789                 return ret;
790         }
791         talloc_steal(mod_req, new_msg);
792
793         /* Run the new request */
794         return ldb_next_request(ac->module, mod_req);
795 }
796
797 static int la_mod_callback(struct ldb_request *req, struct ldb_reply *ares)
798 {
799         struct la_context *ac;
800         struct la_op_store *os;
801         int ret;
802
803         ac = talloc_get_type(req->context, struct la_context);
804
805         if (!ares) {
806                 return ldb_module_done(ac->req, NULL, NULL,
807                                         LDB_ERR_OPERATIONS_ERROR);
808         }
809         if (ares->error != LDB_SUCCESS) {
810                 return ldb_module_done(ac->req, ares->controls,
811                                         ares->response, ares->error);
812         }
813
814         if (ares->type != LDB_REPLY_DONE) {
815                 ldb_set_errstring(ac->module->ldb,
816                                   "invalid ldb_reply_type in callback");
817                 talloc_free(ares);
818                 return ldb_module_done(ac->req, NULL, NULL,
819                                         LDB_ERR_OPERATIONS_ERROR);
820         }
821
822         talloc_free(ares);
823
824         if (ac->ops) {
825                 os = ac->ops;
826                 ac->ops = os->next;
827
828                 /* this frees the request too
829                  * DO NOT access 'req' after this point */
830                 talloc_free(os);
831         }
832
833         /* as last op run the original request */
834         if (ac->ops) {
835                 ret = la_do_mod_request(ac);
836         } else {
837                 ret = la_down_req(ac);
838         }
839
840         if (ret != LDB_SUCCESS) {
841                 return ldb_module_done(ac->req, NULL, NULL, ret);
842         }
843         return LDB_SUCCESS;
844 }
845
846 static int la_down_req(struct la_context *ac)
847 {
848         struct ldb_request *down_req;
849         int ret;
850
851         switch (ac->req->operation) {
852         case LDB_ADD:
853                 ret = ldb_build_add_req(&down_req, ac->module->ldb, ac,
854                                         ac->req->op.add.message,
855                                         ac->req->controls,
856                                         ac, la_down_callback,
857                                         ac->req);
858                 break;
859         case LDB_MODIFY:
860                 ret = ldb_build_mod_req(&down_req, ac->module->ldb, ac,
861                                         ac->req->op.mod.message,
862                                         ac->req->controls,
863                                         ac, la_down_callback,
864                                         ac->req);
865                 break;
866         case LDB_DELETE:
867                 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
868                                         ac->req->op.del.dn,
869                                         ac->req->controls,
870                                         ac, la_down_callback,
871                                         ac->req);
872                 break;
873         case LDB_RENAME:
874                 ret = ldb_build_rename_req(&down_req, ac->module->ldb, ac,
875                                            ac->req->op.rename.olddn,
876                                            ac->req->op.rename.newdn,
877                                            ac->req->controls,
878                                            ac, la_down_callback,
879                                            ac->req);
880                 break;
881         default:
882                 ret = LDB_ERR_OPERATIONS_ERROR;
883         }
884         if (ret != LDB_SUCCESS) {
885                 return ret;
886         }
887
888         return ldb_next_request(ac->module, down_req);
889 }
890
891 static int la_down_callback(struct ldb_request *req, struct ldb_reply *ares)
892 {
893         struct la_context *ac;
894
895         ac = talloc_get_type(req->context, struct la_context);
896
897         if (!ares) {
898                 return ldb_module_done(ac->req, NULL, NULL,
899                                         LDB_ERR_OPERATIONS_ERROR);
900         }
901         if (ares->error != LDB_SUCCESS) {
902                 return ldb_module_done(ac->req, ares->controls,
903                                         ares->response, ares->error);
904         }
905
906         if (ares->type != LDB_REPLY_DONE) {
907                 ldb_set_errstring(ac->module->ldb,
908                                   "invalid ldb_reply_type in callback");
909                 talloc_free(ares);
910                 return ldb_module_done(ac->req, NULL, NULL,
911                                         LDB_ERR_OPERATIONS_ERROR);
912         }
913
914         return ldb_module_done(ac->req, ares->controls,
915                                 ares->response, ares->error);
916 }
917
918 _PUBLIC_ const struct ldb_module_ops ldb_linked_attributes_module_ops = {
919         .name              = "linked_attributes",
920         .add               = linked_attributes_add,
921         .modify            = linked_attributes_modify,
922         .del               = linked_attributes_op,
923         .rename            = linked_attributes_op,
924 };