added new function "ldb_msg_add_dn"
[samba.git] / source4 / lib / ldb / ldb_map / ldb_map_inbound.c
1 /*
2    ldb database mapping module
3
4    Copyright (C) Jelmer Vernooij 2005
5    Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
6    Copyright (C) Simo Sorce <idra@samba.org> 2008
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24
25 */
26
27 #include "ldb_includes.h"
28 #include "ldb_map.h"
29 #include "ldb_map_private.h"
30
31
32 /* Mapping message elements
33  * ======================== */
34
35 /* Map a message element into the remote partition. */
36 static struct ldb_message_element *ldb_msg_el_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, const struct ldb_message_element *old)
37 {
38         struct ldb_message_element *el;
39         int i;
40
41         el = talloc_zero(mem_ctx, struct ldb_message_element);
42         if (el == NULL) {
43                 map_oom(module);
44                 return NULL;
45         }
46
47         el->num_values = old->num_values;
48         el->values = talloc_array(el, struct ldb_val, el->num_values);
49         if (el->values == NULL) {
50                 talloc_free(el);
51                 map_oom(module);
52                 return NULL;
53         }
54
55         el->name = map_attr_map_local(el, map, old->name);
56
57         for (i = 0; i < el->num_values; i++) {
58                 el->values[i] = ldb_val_map_local(module, el->values, map, &old->values[i]);
59         }
60
61         return el;
62 }
63
64 /* Add a message element either to a local or to a remote message,
65  * depending on whether it goes into the local or remote partition. */
66 static int ldb_msg_el_partition(struct ldb_module *module, struct ldb_message *local, struct ldb_message *remote, const struct ldb_message *msg, const char *attr_name, /* const char * const names[], */ const struct ldb_message_element *old)
67 {
68         const struct ldb_map_context *data = map_get_context(module);
69         const struct ldb_map_attribute *map = map_attr_find_local(data, attr_name);
70         struct ldb_message_element *el=NULL;
71         struct ldb_context *ldb = ldb_module_get_ctx(module);
72
73         /* Unknown attribute: ignore */
74         if (map == NULL) {
75                 ldb_debug(ldb, LDB_DEBUG_WARNING, "ldb_map: "
76                           "Not mapping attribute '%s': no mapping found",
77                           old->name);
78                 goto local;
79         }
80
81         switch (map->type) {
82         case LDB_MAP_IGNORE:
83                 goto local;
84
85         case LDB_MAP_CONVERT:
86                 if (map->u.convert.convert_local == NULL) {
87                         ldb_debug(ldb, LDB_DEBUG_WARNING, "ldb_map: "
88                                   "Not mapping attribute '%s': "
89                                   "'convert_local' not set",
90                                   map->local_name);
91                         goto local;
92                 }
93                 /* fall through */
94         case LDB_MAP_KEEP:
95         case LDB_MAP_RENAME:
96                 el = ldb_msg_el_map_local(module, remote, map, old);
97                 break;
98
99         case LDB_MAP_GENERATE:
100                 if (map->u.generate.generate_remote == NULL) {
101                         ldb_debug(ldb, LDB_DEBUG_WARNING, "ldb_map: "
102                                   "Not mapping attribute '%s': "
103                                   "'generate_remote' not set",
104                                   map->local_name);
105                         goto local;
106                 }
107
108                 /* TODO: if this attr requires context:
109                  *       make sure all context attrs are mappable (in 'names')
110                  *       make sure all context attrs have already been mapped?
111                  *       maybe postpone generation until they have been mapped?
112                  */
113
114                 map->u.generate.generate_remote(module, map->local_name, msg, remote, local);
115                 return 0;
116         }
117
118         if (el == NULL) {
119                 return -1;
120         }
121
122         return ldb_msg_add(remote, el, old->flags);
123
124 local:
125         el = talloc(local, struct ldb_message_element);
126         if (el == NULL) {
127                 map_oom(module);
128                 return -1;
129         }
130
131         *el = *old;                     /* copy the old element */
132
133         return ldb_msg_add(local, el, old->flags);
134 }
135
136 /* Mapping messages
137  * ================ */
138
139 /* Check whether a message will be (partially) mapped into the remote partition. */
140 static bool ldb_msg_check_remote(struct ldb_module *module, const struct ldb_message *msg)
141 {
142         const struct ldb_map_context *data = map_get_context(module);
143         bool ret;
144         int i;
145
146         for (i = 0; i < msg->num_elements; i++) {
147                 ret = map_attr_check_remote(data, msg->elements[i].name);
148                 if (ret) {
149                         return ret;
150                 }
151         }
152
153         return false;
154 }
155
156 /* Split message elements that stay in the local partition from those
157  * that are mapped into the remote partition. */
158 static int ldb_msg_partition(struct ldb_module *module, struct ldb_message *local, struct ldb_message *remote, const struct ldb_message *msg)
159 {
160         /* const char * const names[]; */
161         struct ldb_context *ldb;
162         int i, ret;
163
164         ldb = ldb_module_get_ctx(module);
165
166         for (i = 0; i < msg->num_elements; i++) {
167                 /* Skip 'IS_MAPPED' */
168                 if (ldb_attr_cmp(msg->elements[i].name, IS_MAPPED) == 0) {
169                         ldb_debug(ldb, LDB_DEBUG_WARNING, "ldb_map: "
170                                   "Skipping attribute '%s'",
171                                   msg->elements[i].name);
172                         continue;
173                 }
174
175                 ret = ldb_msg_el_partition(module, local, remote, msg, msg->elements[i].name, &msg->elements[i]);
176                 if (ret) {
177                         return ret;
178                 }
179         }
180
181         return 0;
182 }
183
184
185 static int map_add_do_local(struct map_context *ac);
186 static int map_modify_do_local(struct map_context *ac);
187 static int map_delete_do_local(struct map_context *ac);
188 static int map_rename_do_local(struct map_context *ac);
189 static int map_rename_do_fixup(struct map_context *ac);
190 static int map_rename_local_callback(struct ldb_request *req,
191                                      struct ldb_reply *ares);
192
193
194 /*****************************************************************************
195  * COMMON INBOUND functions
196 *****************************************************************************/
197
198 /* Store the DN of a single search result in context. */
199 static int map_search_self_callback(struct ldb_request *req, struct ldb_reply *ares)
200 {
201         struct ldb_context *ldb;
202         struct map_context *ac;
203         int ret;
204
205         ac = talloc_get_type(req->context, struct map_context);
206         ldb = ldb_module_get_ctx(ac->module);
207
208         if (!ares) {
209                 return ldb_module_done(ac->req, NULL, NULL,
210                                         LDB_ERR_OPERATIONS_ERROR);
211         }
212         if (ares->error != LDB_SUCCESS) {
213                 return ldb_module_done(ac->req, ares->controls,
214                                         ares->response, ares->error);
215         }
216
217         /* We are interested only in the single reply */
218         switch(ares->type) {
219         case LDB_REPLY_ENTRY:
220                 /* We have already found a remote DN */
221                 if (ac->local_dn) {
222                         ldb_set_errstring(ldb,
223                                           "Too many results!");
224                         return ldb_module_done(ac->req, NULL, NULL,
225                                                 LDB_ERR_OPERATIONS_ERROR);
226                 }
227
228                 /* Store local DN */
229                 ac->local_dn = talloc_steal(ac, ares->message->dn);
230                 break;
231
232         case LDB_REPLY_DONE:
233
234                 switch (ac->req->operation) {
235                 case LDB_MODIFY:
236                         ret = map_modify_do_local(ac);
237                         break;
238                 case LDB_DELETE:
239                         ret = map_delete_do_local(ac);
240                         break;
241                 case LDB_RENAME:
242                         ret = map_rename_do_local(ac);
243                         break;
244                 default:
245                         /* if we get here we have definitely a problem */
246                         ret = LDB_ERR_OPERATIONS_ERROR;
247                 }
248                 if (ret != LDB_SUCCESS) {
249                         return ldb_module_done(ac->req, NULL, NULL,
250                                                 LDB_ERR_OPERATIONS_ERROR);
251                 }
252
253         default:
254                 /* ignore referrals */
255                 break;
256         }
257
258         talloc_free(ares);
259         return LDB_SUCCESS;
260 }
261
262 /* Build a request to search the local record by its DN. */
263 static int map_search_self_req(struct ldb_request **req,
264                                 struct map_context *ac,
265                                 struct ldb_dn *dn)
266 {
267         /* attrs[] is returned from this function in
268          * ac->search_req->op.search.attrs, so it must be static, as
269          * otherwise the compiler can put it on the stack */
270         static const char * const attrs[] = { IS_MAPPED, NULL };
271         struct ldb_parse_tree *tree;
272
273         /* Limit search to records with 'IS_MAPPED' present */
274         tree = ldb_parse_tree(ac, "(" IS_MAPPED "=*)");
275         if (tree == NULL) {
276                 map_oom(ac->module);
277                 return LDB_ERR_OPERATIONS_ERROR;
278         }
279
280         *req = map_search_base_req(ac, dn, attrs, tree,
281                                    ac, map_search_self_callback);
282         if (*req == NULL) {
283                 return LDB_ERR_OPERATIONS_ERROR;
284         }
285
286         return LDB_SUCCESS;
287 }
288
289 static int map_op_local_callback(struct ldb_request *req,
290                                  struct ldb_reply *ares)
291 {
292         struct ldb_context *ldb;
293         struct map_context *ac;
294         int ret;
295
296         ac = talloc_get_type(req->context, struct map_context);
297         ldb = ldb_module_get_ctx(ac->module);
298
299         if (!ares) {
300                 return ldb_module_done(ac->req, NULL, NULL,
301                                         LDB_ERR_OPERATIONS_ERROR);
302         }
303         if (ares->error != LDB_SUCCESS) {
304                 return ldb_module_done(ac->req, ares->controls,
305                                         ares->response, ares->error);
306         }
307
308         if (ares->type != LDB_REPLY_DONE) {
309                 ldb_set_errstring(ldb, "Invalid reply type!");
310                 return ldb_module_done(ac->req, NULL, NULL,
311                                         LDB_ERR_OPERATIONS_ERROR);
312         }
313
314         /* Do the remote request. */
315         ret = ldb_next_remote_request(ac->module, ac->remote_req);
316         if (ret != LDB_SUCCESS) {
317                 return ldb_module_done(ac->req, NULL, NULL,
318                                         LDB_ERR_OPERATIONS_ERROR);
319         }
320
321         return LDB_SUCCESS;
322 }
323
324 static int map_op_remote_callback(struct ldb_request *req,
325                                   struct ldb_reply *ares)
326 {
327         struct ldb_context *ldb;
328         struct map_context *ac;
329
330         ac = talloc_get_type(req->context, struct map_context);
331         ldb = ldb_module_get_ctx(ac->module);
332
333         if (!ares) {
334                 return ldb_module_done(ac->req, NULL, NULL,
335                                         LDB_ERR_OPERATIONS_ERROR);
336         }
337         if (ares->error != LDB_SUCCESS) {
338                 return ldb_module_done(ac->req, ares->controls,
339                                         ares->response, ares->error);
340         }
341
342         if (ares->type != LDB_REPLY_DONE) {
343                 ldb_set_errstring(ldb, "Invalid reply type!");
344                 return ldb_module_done(ac->req, NULL, NULL,
345                                         LDB_ERR_OPERATIONS_ERROR);
346         }
347
348         return ldb_module_done(ac->req, ares->controls,
349                                         ares->response, ares->error);
350 }
351
352
353 /*****************************************************************************
354  * ADD operations
355 *****************************************************************************/
356
357
358 /* Add a record. */
359 int map_add(struct ldb_module *module, struct ldb_request *req)
360 {
361         const struct ldb_message *msg = req->op.add.message;
362         struct ldb_context *ldb;
363         struct map_context *ac;
364         struct ldb_message *remote_msg;
365         int ret;
366
367         ldb = ldb_module_get_ctx(module);
368
369         /* Do not manipulate our control entries */
370         if (ldb_dn_is_special(msg->dn)) {
371                 return ldb_next_request(module, req);
372         }
373
374         /* No mapping requested (perhaps no DN mapping specified), skip to next module */
375         if (!ldb_dn_check_local(module, msg->dn)) {
376                 return ldb_next_request(module, req);
377         }
378
379         /* No mapping needed, fail */
380         if (!ldb_msg_check_remote(module, msg)) {
381                 return LDB_ERR_OPERATIONS_ERROR;
382         }
383
384         /* Prepare context and handle */
385         ac = map_init_context(module, req);
386         if (ac == NULL) {
387                 return LDB_ERR_OPERATIONS_ERROR;
388         }
389
390
391         /* Prepare the local message */
392         ac->local_msg = ldb_msg_new(ac);
393         if (ac->local_msg == NULL) {
394                 map_oom(module);
395                 return LDB_ERR_OPERATIONS_ERROR;
396         }
397         ac->local_msg->dn = msg->dn;
398
399         /* Prepare the remote message */
400         remote_msg = ldb_msg_new(ac);
401         if (remote_msg == NULL) {
402                 map_oom(module);
403                 return LDB_ERR_OPERATIONS_ERROR;
404         }
405         remote_msg->dn = ldb_dn_map_local(ac->module, remote_msg, msg->dn);
406
407         /* Split local from remote message */
408         ldb_msg_partition(module, ac->local_msg, remote_msg, msg);
409
410         /* Prepare the remote operation */
411         ret = ldb_build_add_req(&ac->remote_req, ldb,
412                                 ac, remote_msg,
413                                 req->controls,
414                                 ac, map_op_remote_callback,
415                                 req);
416         if (ret != LDB_SUCCESS) {
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419
420         if ((ac->local_msg->num_elements == 0) ||
421             ( ! map_check_local_db(ac->module))) {
422                 /* No local data or db, just run the remote request */
423                 return ldb_next_remote_request(ac->module, ac->remote_req);
424         }
425
426         /* Store remote DN in 'IS_MAPPED' */
427         /* TODO: use GUIDs here instead */
428         if (ldb_msg_add_dn(ac->local_msg, IS_MAPPED, remote_msg->dn) != 0) {
429                 return LDB_ERR_OPERATIONS_ERROR;
430         }
431
432         return map_add_do_local(ac);
433 }
434
435 /* Add the local record. */
436 static int map_add_do_local(struct map_context *ac)
437 {
438         struct ldb_request *local_req;
439         struct ldb_context *ldb;
440         int ret;
441
442         ldb = ldb_module_get_ctx(ac->module);
443
444         /* Prepare the local operation */
445         ret = ldb_build_add_req(&local_req, ldb, ac,
446                                 ac->local_msg,
447                                 ac->req->controls,
448                                 ac,
449                                 map_op_local_callback,
450                                 ac->req);
451         if (ret != LDB_SUCCESS) {
452                 return LDB_ERR_OPERATIONS_ERROR;
453         }
454         return ldb_next_request(ac->module, local_req);
455 }
456
457 /*****************************************************************************
458  * MODIFY operations
459 *****************************************************************************/
460
461 /* Modify a record. */
462 int map_modify(struct ldb_module *module, struct ldb_request *req)
463 {
464         const struct ldb_message *msg = req->op.mod.message;
465         struct ldb_request *search_req;
466         struct ldb_message *remote_msg;
467         struct ldb_context *ldb;
468         struct map_context *ac;
469         int ret;
470
471         ldb = ldb_module_get_ctx(module);
472
473         /* Do not manipulate our control entries */
474         if (ldb_dn_is_special(msg->dn)) {
475                 return ldb_next_request(module, req);
476         }
477
478         /* No mapping requested (perhaps no DN mapping specified), skip to next module */
479         if (!ldb_dn_check_local(module, msg->dn)) {
480                 return ldb_next_request(module, req);
481         }
482
483         /* No mapping needed, skip to next module */
484         /* TODO: What if the remote part exists, the local doesn't,
485          *       and this request wants to modify local data and thus
486          *       add the local record? */
487         if (!ldb_msg_check_remote(module, msg)) {
488                 return LDB_ERR_OPERATIONS_ERROR;
489         }
490
491         /* Prepare context and handle */
492         ac = map_init_context(module, req);
493         if (ac == NULL) {
494                 return LDB_ERR_OPERATIONS_ERROR;
495         }
496
497         /* Prepare the local message */
498         ac->local_msg = ldb_msg_new(ac);
499         if (ac->local_msg == NULL) {
500                 map_oom(module);
501                 return LDB_ERR_OPERATIONS_ERROR;
502         }
503         ac->local_msg->dn = msg->dn;
504
505         /* Prepare the remote message */
506         remote_msg = ldb_msg_new(ac->remote_req);
507         if (remote_msg == NULL) {
508                 map_oom(module);
509                 return LDB_ERR_OPERATIONS_ERROR;
510         }
511         remote_msg->dn = ldb_dn_map_local(ac->module, remote_msg, msg->dn);
512
513         /* Split local from remote message */
514         ldb_msg_partition(module, ac->local_msg, remote_msg, msg);
515
516         /* Prepare the remote operation */
517         ret = ldb_build_mod_req(&ac->remote_req, ldb,
518                                 ac, remote_msg,
519                                 req->controls,
520                                 ac, map_op_remote_callback,
521                                 req);
522         if (ret != LDB_SUCCESS) {
523                 return LDB_ERR_OPERATIONS_ERROR;
524         }
525
526         if ((ac->local_msg->num_elements == 0) ||
527             ( ! map_check_local_db(ac->module))) {
528                 /* No local data or db, just run the remote request */
529                 return ldb_next_remote_request(ac->module, ac->remote_req);
530         }
531
532         /* prepare the search operation */
533         ret = map_search_self_req(&search_req, ac, msg->dn);
534         if (ret != LDB_SUCCESS) {
535                 return LDB_ERR_OPERATIONS_ERROR;
536         }
537
538         return ldb_next_request(module, search_req);
539 }
540
541 /* Modify the local record. */
542 static int map_modify_do_local(struct map_context *ac)
543 {
544         struct ldb_request *local_req;
545         struct ldb_context *ldb;
546         int ret;
547
548         ldb = ldb_module_get_ctx(ac->module);
549
550         if (ac->local_dn == NULL) {
551                 /* No local record present, add it instead */
552                 /* Add local 'IS_MAPPED' */
553                 /* TODO: use GUIDs here instead */
554                 if (ldb_msg_add_empty(ac->local_msg, IS_MAPPED,
555                                         LDB_FLAG_MOD_ADD, NULL) != 0) {
556                         return LDB_ERR_OPERATIONS_ERROR;
557                 }
558                 if (ldb_msg_add_dn(ac->local_msg, IS_MAPPED,
559                                    ac->remote_req->op.mod.message->dn) != 0) {
560                         return LDB_ERR_OPERATIONS_ERROR;
561                 }
562
563                 /* Prepare the local operation */
564                 ret = ldb_build_add_req(&local_req, ldb, ac,
565                                         ac->local_msg,
566                                         ac->req->controls,
567                                         ac,
568                                         map_op_local_callback,
569                                         ac->req);
570                 if (ret != LDB_SUCCESS) {
571                         return LDB_ERR_OPERATIONS_ERROR;
572                 }
573         } else {
574                 /* Prepare the local operation */
575                 ret = ldb_build_mod_req(&local_req, ldb, ac,
576                                         ac->local_msg,
577                                         ac->req->controls,
578                                         ac,
579                                         map_op_local_callback,
580                                         ac->req);
581                 if (ret != LDB_SUCCESS) {
582                         return LDB_ERR_OPERATIONS_ERROR;
583                 }
584         }
585
586         return ldb_next_request(ac->module, local_req);
587 }
588
589 /*****************************************************************************
590  * DELETE operations
591 *****************************************************************************/
592
593 /* Delete a record. */
594 int map_delete(struct ldb_module *module, struct ldb_request *req)
595 {
596         struct ldb_request *search_req;
597         struct ldb_context *ldb;
598         struct map_context *ac;
599         int ret;
600
601         ldb = ldb_module_get_ctx(module);
602
603         /* Do not manipulate our control entries */
604         if (ldb_dn_is_special(req->op.del.dn)) {
605                 return ldb_next_request(module, req);
606         }
607
608         /* No mapping requested (perhaps no DN mapping specified).
609          * Skip to next module */
610         if (!ldb_dn_check_local(module, req->op.del.dn)) {
611                 return ldb_next_request(module, req);
612         }
613
614         /* Prepare context and handle */
615         ac = map_init_context(module, req);
616         if (ac == NULL) {
617                 return LDB_ERR_OPERATIONS_ERROR;
618         }
619
620         /* Prepare the remote operation */
621         ret = ldb_build_del_req(&ac->remote_req, ldb, ac,
622                                    ldb_dn_map_local(module, ac, req->op.del.dn),
623                                    req->controls,
624                                    ac,
625                                    map_op_remote_callback,
626                                    req);
627         if (ret != LDB_SUCCESS) {
628                 return LDB_ERR_OPERATIONS_ERROR;
629         }
630
631         /* No local db, just run the remote request */
632         if (!map_check_local_db(ac->module)) {
633                 /* Do the remote request. */
634                 return ldb_next_remote_request(ac->module, ac->remote_req);
635         }
636
637         /* Prepare the search operation */
638         ret = map_search_self_req(&search_req, ac, req->op.del.dn);
639         if (ret != LDB_SUCCESS) {
640                 map_oom(module);
641                 return LDB_ERR_OPERATIONS_ERROR;
642         }
643
644         return ldb_next_request(module, search_req);
645 }
646
647 /* Delete the local record. */
648 static int map_delete_do_local(struct map_context *ac)
649 {
650         struct ldb_request *local_req;
651         struct ldb_context *ldb;
652         int ret;
653
654         ldb = ldb_module_get_ctx(ac->module);
655
656         /* No local record, continue remotely */
657         if (ac->local_dn == NULL) {
658                 /* Do the remote request. */
659                 return ldb_next_remote_request(ac->module, ac->remote_req);
660         }
661
662         /* Prepare the local operation */
663         ret = ldb_build_del_req(&local_req, ldb, ac,
664                                    ac->req->op.del.dn,
665                                    ac->req->controls,
666                                    ac,
667                                    map_op_local_callback,
668                                    ac->req);
669         if (ret != LDB_SUCCESS) {
670                 return LDB_ERR_OPERATIONS_ERROR;
671         }
672         return ldb_next_request(ac->module, local_req);
673 }
674
675 /*****************************************************************************
676  * RENAME operations
677 *****************************************************************************/
678
679 /* Rename a record. */
680 int map_rename(struct ldb_module *module, struct ldb_request *req)
681 {
682         struct ldb_request *search_req;
683         struct ldb_context *ldb;
684         struct map_context *ac;
685         int ret;
686
687         ldb = ldb_module_get_ctx(module);
688
689         /* Do not manipulate our control entries */
690         if (ldb_dn_is_special(req->op.rename.olddn)) {
691                 return ldb_next_request(module, req);
692         }
693
694         /* No mapping requested (perhaps no DN mapping specified).
695          * Skip to next module */
696         if ((!ldb_dn_check_local(module, req->op.rename.olddn)) &&
697             (!ldb_dn_check_local(module, req->op.rename.newdn))) {
698                 return ldb_next_request(module, req);
699         }
700
701         /* Rename into/out of the mapped partition requested, bail out */
702         if (!ldb_dn_check_local(module, req->op.rename.olddn) ||
703             !ldb_dn_check_local(module, req->op.rename.newdn)) {
704                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
705         }
706
707         /* Prepare context and handle */
708         ac = map_init_context(module, req);
709         if (ac == NULL) {
710                 return LDB_ERR_OPERATIONS_ERROR;
711         }
712
713         /* Prepare the remote operation */
714         ret = ldb_build_rename_req(&ac->remote_req, ldb, ac,
715                                    ldb_dn_map_local(module, ac, req->op.rename.olddn),
716                                    ldb_dn_map_local(module, ac, req->op.rename.newdn),
717                                    req->controls,
718                                    ac, map_op_remote_callback,
719                                    req);
720         if (ret != LDB_SUCCESS) {
721                 return LDB_ERR_OPERATIONS_ERROR;
722         }
723
724         /* No local db, just run the remote request */
725         if (!map_check_local_db(ac->module)) {
726                 /* Do the remote request. */
727                 return ldb_next_remote_request(ac->module, ac->remote_req);
728         }
729
730         /* Prepare the search operation */
731         ret = map_search_self_req(&search_req, ac, req->op.rename.olddn);
732         if (ret != LDB_SUCCESS) {
733                 map_oom(module);
734                 return LDB_ERR_OPERATIONS_ERROR;
735         }
736
737         return ldb_next_request(module, search_req);
738 }
739
740 /* Rename the local record. */
741 static int map_rename_do_local(struct map_context *ac)
742 {
743         struct ldb_request *local_req;
744         struct ldb_context *ldb;
745         int ret;
746
747         ldb = ldb_module_get_ctx(ac->module);
748
749         /* No local record, continue remotely */
750         if (ac->local_dn == NULL) {
751                 /* Do the remote request. */
752                 return ldb_next_remote_request(ac->module, ac->remote_req);
753         }
754
755         /* Prepare the local operation */
756         ret = ldb_build_rename_req(&local_req, ldb, ac,
757                                    ac->req->op.rename.olddn,
758                                    ac->req->op.rename.newdn,
759                                    ac->req->controls,
760                                    ac,
761                                    map_rename_local_callback,
762                                    ac->req);
763         if (ret != LDB_SUCCESS) {
764                 return LDB_ERR_OPERATIONS_ERROR;
765         }
766
767         return ldb_next_request(ac->module, local_req);
768 }
769
770 static int map_rename_local_callback(struct ldb_request *req,
771                                      struct ldb_reply *ares)
772 {
773         struct ldb_context *ldb;
774         struct map_context *ac;
775         int ret;
776
777         ac = talloc_get_type(req->context, struct map_context);
778         ldb = ldb_module_get_ctx(ac->module);
779
780         if (!ares) {
781                 return ldb_module_done(ac->req, NULL, NULL,
782                                         LDB_ERR_OPERATIONS_ERROR);
783         }
784         if (ares->error != LDB_SUCCESS) {
785                 return ldb_module_done(ac->req, ares->controls,
786                                         ares->response, ares->error);
787         }
788
789         if (ares->type != LDB_REPLY_DONE) {
790                 ldb_set_errstring(ldb, "Invalid reply type!");
791                 return ldb_module_done(ac->req, NULL, NULL,
792                                         LDB_ERR_OPERATIONS_ERROR);
793         }
794
795         /* proceed with next step */
796         ret = map_rename_do_fixup(ac);
797         if (ret != LDB_SUCCESS) {
798                 return ldb_module_done(ac->req, NULL, NULL,
799                                         LDB_ERR_OPERATIONS_ERROR);
800         }
801
802         return LDB_SUCCESS;
803 }
804
805 /* Update the local 'IS_MAPPED' attribute. */
806 static int map_rename_do_fixup(struct map_context *ac)
807 {
808         struct ldb_request *local_req;
809
810         /* Prepare the fixup operation */
811         /* TODO: use GUIDs here instead -- or skip it when GUIDs are used. */
812         local_req = map_build_fixup_req(ac,
813                                         ac->req->op.rename.newdn,
814                                         ac->remote_req->op.rename.newdn,
815                                         ac,
816                                         map_op_local_callback);
817         if (local_req == NULL) {
818                 return LDB_ERR_OPERATIONS_ERROR;
819         }
820
821         return ldb_next_request(ac->module, local_req);
822 }