96605f23ebcc0214405f87aaa0da45e011871eab
[ira/wip.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
29 #include "ldb_map.h"
30 #include "ldb_map_private.h"
31
32
33 /* Mapping message elements
34  * ======================== */
35
36 /* Map a message element into the remote partition. */
37 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)
38 {
39         struct ldb_message_element *el;
40         int i;
41
42         el = talloc_zero(mem_ctx, struct ldb_message_element);
43         if (el == NULL) {
44                 map_oom(module);
45                 return NULL;
46         }
47
48         el->num_values = old->num_values;
49         el->values = talloc_array(el, struct ldb_val, el->num_values);
50         if (el->values == NULL) {
51                 talloc_free(el);
52                 map_oom(module);
53                 return NULL;
54         }
55
56         el->name = map_attr_map_local(el, map, old->name);
57
58         for (i = 0; i < el->num_values; i++) {
59                 el->values[i] = ldb_val_map_local(module, el->values, map, &old->values[i]);
60         }
61
62         return el;
63 }
64
65 /* Add a message element either to a local or to a remote message,
66  * depending on whether it goes into the local or remote partition. */
67 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)
68 {
69         const struct ldb_map_context *data = map_get_context(module);
70         const struct ldb_map_attribute *map = map_attr_find_local(data, attr_name);
71         struct ldb_message_element *el=NULL;
72
73         /* Unknown attribute: ignore */
74         if (map == NULL) {
75                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
76                           "Not mapping attribute '%s': no mapping found\n",
77                           old->name);
78                 goto local;
79         }
80
81         switch (map->type) {
82         case MAP_IGNORE:
83                 goto local;
84
85         case MAP_CONVERT:
86                 if (map->u.convert.convert_local == NULL) {
87                         ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
88                                   "Not mapping attribute '%s': "
89                                   "'convert_local' not set\n",
90                                   map->local_name);
91                         goto local;
92                 }
93                 /* fall through */
94         case MAP_KEEP:
95         case MAP_RENAME:
96                 el = ldb_msg_el_map_local(module, remote, map, old);
97                 break;
98
99         case MAP_GENERATE:
100                 if (map->u.generate.generate_remote == NULL) {
101                         ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
102                                   "Not mapping attribute '%s': "
103                                   "'generate_remote' not set\n",
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         int i, ret;
162
163         for (i = 0; i < msg->num_elements; i++) {
164                 /* Skip 'IS_MAPPED' */
165                 if (ldb_attr_cmp(msg->elements[i].name, IS_MAPPED) == 0) {
166                         ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
167                                   "Skipping attribute '%s'\n",
168                                   msg->elements[i].name);
169                         continue;
170                 }
171
172                 ret = ldb_msg_el_partition(module, local, remote, msg, msg->elements[i].name, &msg->elements[i]);
173                 if (ret) {
174                         return ret;
175                 }
176         }
177
178         return 0;
179 }
180
181
182 static int map_add_do_local(struct map_context *ac);
183 static int map_modify_do_local(struct map_context *ac);
184 static int map_delete_do_local(struct map_context *ac);
185 static int map_rename_do_local(struct map_context *ac);
186 static int map_rename_do_fixup(struct map_context *ac);
187 static int map_rename_local_callback(struct ldb_request *req,
188                                      struct ldb_reply *ares);
189
190
191 /*****************************************************************************
192  * COMMON INBOUND functions
193 *****************************************************************************/
194
195 /* Store the DN of a single search result in context. */
196 static int map_search_self_callback(struct ldb_request *req, struct ldb_reply *ares)
197 {
198         struct map_context *ac;
199         int ret;
200
201         ac = talloc_get_type(req->context, struct map_context);
202
203         if (!ares) {
204                 return ldb_module_done(ac->req, NULL, NULL,
205                                         LDB_ERR_OPERATIONS_ERROR);
206         }
207         if (ares->error != LDB_SUCCESS) {
208                 return ldb_module_done(ac->req, ares->controls,
209                                         ares->response, ares->error);
210         }
211
212         /* We are interested only in the single reply */
213         switch(ares->type) {
214         case LDB_REPLY_ENTRY:
215                 /* We have already found a remote DN */
216                 if (ac->local_dn) {
217                         ldb_set_errstring(ac->module->ldb,
218                                           "Too many results!");
219                         return ldb_module_done(ac->req, NULL, NULL,
220                                                 LDB_ERR_OPERATIONS_ERROR);
221                 }
222
223                 /* Store local DN */
224                 ac->local_dn = talloc_steal(ac, ares->message->dn);
225                 break;
226
227         case LDB_REPLY_DONE:
228
229                 switch (ac->req->operation) {
230                 case LDB_MODIFY:
231                         ret = map_modify_do_local(ac);
232                         break;
233                 case LDB_DELETE:
234                         ret = map_delete_do_local(ac);
235                         break;
236                 case LDB_RENAME:
237                         ret = map_rename_do_local(ac);
238                         break;
239                 default:
240                         /* if we get here we have definitely a problem */
241                         ret = LDB_ERR_OPERATIONS_ERROR;
242                 }
243                 if (ret != LDB_SUCCESS) {
244                         return ldb_module_done(ac->req, NULL, NULL,
245                                                 LDB_ERR_OPERATIONS_ERROR);
246                 }
247
248         default:
249                 /* ignore referrals */
250                 break;
251         }
252
253         talloc_free(ares);
254         return LDB_SUCCESS;
255 }
256
257 /* Build a request to search the local record by its DN. */
258 static int map_search_self_req(struct ldb_request **req,
259                                 struct map_context *ac,
260                                 struct ldb_dn *dn)
261 {
262         /* attrs[] is returned from this function in
263          * ac->search_req->op.search.attrs, so it must be static, as
264          * otherwise the compiler can put it on the stack */
265         static const char * const attrs[] = { IS_MAPPED, NULL };
266         struct ldb_parse_tree *tree;
267
268         /* Limit search to records with 'IS_MAPPED' present */
269         tree = ldb_parse_tree(ac, "(" IS_MAPPED "=*)");
270         if (tree == NULL) {
271                 map_oom(ac->module);
272                 return LDB_ERR_OPERATIONS_ERROR;
273         }
274
275         *req = map_search_base_req(ac, dn, attrs, tree,
276                                    ac, map_search_self_callback);
277         if (*req == NULL) {
278                 return LDB_ERR_OPERATIONS_ERROR;
279         }
280
281         return LDB_SUCCESS;
282 }
283
284 static int map_op_local_callback(struct ldb_request *req,
285                                  struct ldb_reply *ares)
286 {
287         struct map_context *ac;
288         int ret;
289
290         ac = talloc_get_type(req->context, struct map_context);
291
292         if (!ares) {
293                 return ldb_module_done(ac->req, NULL, NULL,
294                                         LDB_ERR_OPERATIONS_ERROR);
295         }
296         if (ares->error != LDB_SUCCESS) {
297                 return ldb_module_done(ac->req, ares->controls,
298                                         ares->response, ares->error);
299         }
300
301         if (ares->type != LDB_REPLY_DONE) {
302                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
303                 return ldb_module_done(ac->req, NULL, NULL,
304                                         LDB_ERR_OPERATIONS_ERROR);
305         }
306
307         /* Do the remote request. */
308         ret = ldb_next_remote_request(ac->module, ac->remote_req);
309         if (ret != LDB_SUCCESS) {
310                 return ldb_module_done(ac->req, NULL, NULL,
311                                         LDB_ERR_OPERATIONS_ERROR);
312         }
313
314         return LDB_SUCCESS;
315 }
316
317 static int map_op_remote_callback(struct ldb_request *req,
318                                   struct ldb_reply *ares)
319 {
320         struct map_context *ac;
321
322         ac = talloc_get_type(req->context, struct map_context);
323
324         if (!ares) {
325                 return ldb_module_done(ac->req, NULL, NULL,
326                                         LDB_ERR_OPERATIONS_ERROR);
327         }
328         if (ares->error != LDB_SUCCESS) {
329                 return ldb_module_done(ac->req, ares->controls,
330                                         ares->response, ares->error);
331         }
332
333         if (ares->type != LDB_REPLY_DONE) {
334                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
335                 return ldb_module_done(ac->req, NULL, NULL,
336                                         LDB_ERR_OPERATIONS_ERROR);
337         }
338
339         return ldb_module_done(ac->req, ares->controls,
340                                         ares->response, ares->error);
341 }
342
343
344 /*****************************************************************************
345  * ADD operations
346 *****************************************************************************/
347
348
349 /* Add a record. */
350 int map_add(struct ldb_module *module, struct ldb_request *req)
351 {
352         const struct ldb_message *msg = req->op.add.message;
353         struct map_context *ac;
354         struct ldb_message *remote_msg;
355         const char *dn;
356         int ret;
357
358         /* Do not manipulate our control entries */
359         if (ldb_dn_is_special(msg->dn)) {
360                 return ldb_next_request(module, req);
361         }
362
363         /* No mapping requested (perhaps no DN mapping specified), skip to next module */
364         if (!ldb_dn_check_local(module, msg->dn)) {
365                 return ldb_next_request(module, req);
366         }
367
368         /* No mapping needed, fail */
369         if (!ldb_msg_check_remote(module, msg)) {
370                 return LDB_ERR_OPERATIONS_ERROR;
371         }
372
373         /* Prepare context and handle */
374         ac = map_init_context(module, req);
375         if (ac == NULL) {
376                 return LDB_ERR_OPERATIONS_ERROR;
377         }
378
379
380         /* Prepare the local message */
381         ac->local_msg = ldb_msg_new(ac);
382         if (ac->local_msg == NULL) {
383                 map_oom(module);
384                 return LDB_ERR_OPERATIONS_ERROR;
385         }
386         ac->local_msg->dn = msg->dn;
387
388         /* Prepare the remote message */
389         remote_msg = ldb_msg_new(ac);
390         if (remote_msg == NULL) {
391                 map_oom(module);
392                 return LDB_ERR_OPERATIONS_ERROR;
393         }
394         remote_msg->dn = ldb_dn_map_local(ac->module, remote_msg, msg->dn);
395
396         /* Split local from remote message */
397         ldb_msg_partition(module, ac->local_msg, remote_msg, msg);
398
399         /* Prepare the remote operation */
400         ret = ldb_build_add_req(&ac->remote_req, module->ldb,
401                                 ac, remote_msg,
402                                 req->controls,
403                                 ac, map_op_remote_callback,
404                                 req);
405         if (ret != LDB_SUCCESS) {
406                 return LDB_ERR_OPERATIONS_ERROR;
407         }
408
409         if ((ac->local_msg->num_elements == 0) ||
410             ( ! map_check_local_db(ac->module))) {
411                 /* No local data or db, just run the remote request */
412                 return ldb_next_remote_request(ac->module, ac->remote_req);
413         }
414
415         /* Store remote DN in 'IS_MAPPED' */
416         /* TODO: use GUIDs here instead */
417         dn = ldb_dn_alloc_linearized(ac->local_msg, remote_msg->dn);
418         if (ldb_msg_add_string(ac->local_msg, IS_MAPPED, dn) != 0) {
419                 return LDB_ERR_OPERATIONS_ERROR;
420         }
421
422         return map_add_do_local(ac);
423 }
424
425 /* Add the local record. */
426 static int map_add_do_local(struct map_context *ac)
427 {
428         struct ldb_request *local_req;
429         int ret;
430
431         /* Prepare the local operation */
432         ret = ldb_build_add_req(&local_req, ac->module->ldb, ac,
433                                 ac->local_msg,
434                                 ac->req->controls,
435                                 ac,
436                                 map_op_local_callback,
437                                 ac->req);
438         if (ret != LDB_SUCCESS) {
439                 return LDB_ERR_OPERATIONS_ERROR;
440         }
441         return ldb_next_request(ac->module, local_req);
442 }
443
444 /*****************************************************************************
445  * MODIFY operations
446 *****************************************************************************/
447
448 /* Modify a record. */
449 int map_modify(struct ldb_module *module, struct ldb_request *req)
450 {
451         const struct ldb_message *msg = req->op.mod.message;
452         struct ldb_request *search_req;
453         struct ldb_message *remote_msg;
454         struct map_context *ac;
455         int ret;
456
457         /* Do not manipulate our control entries */
458         if (ldb_dn_is_special(msg->dn)) {
459                 return ldb_next_request(module, req);
460         }
461
462         /* No mapping requested (perhaps no DN mapping specified), skip to next module */
463         if (!ldb_dn_check_local(module, msg->dn)) {
464                 return ldb_next_request(module, req);
465         }
466
467         /* No mapping needed, skip to next module */
468         /* TODO: What if the remote part exists, the local doesn't,
469          *       and this request wants to modify local data and thus
470          *       add the local record? */
471         if (!ldb_msg_check_remote(module, msg)) {
472                 return LDB_ERR_OPERATIONS_ERROR;
473         }
474
475         /* Prepare context and handle */
476         ac = map_init_context(module, req);
477         if (ac == NULL) {
478                 return LDB_ERR_OPERATIONS_ERROR;
479         }
480
481         /* Prepare the local message */
482         ac->local_msg = ldb_msg_new(ac);
483         if (ac->local_msg == NULL) {
484                 map_oom(module);
485                 return LDB_ERR_OPERATIONS_ERROR;
486         }
487         ac->local_msg->dn = msg->dn;
488
489         /* Prepare the remote message */
490         remote_msg = ldb_msg_new(ac->remote_req);
491         if (remote_msg == NULL) {
492                 map_oom(module);
493                 return LDB_ERR_OPERATIONS_ERROR;
494         }
495         remote_msg->dn = ldb_dn_map_local(ac->module, remote_msg, msg->dn);
496
497         /* Split local from remote message */
498         ldb_msg_partition(module, ac->local_msg, remote_msg, msg);
499
500         /* Prepare the remote operation */
501         ret = ldb_build_mod_req(&ac->remote_req, module->ldb,
502                                 ac, remote_msg,
503                                 req->controls,
504                                 ac, map_op_remote_callback,
505                                 req);
506         if (ret != LDB_SUCCESS) {
507                 return LDB_ERR_OPERATIONS_ERROR;
508         }
509
510         if ((ac->local_msg->num_elements == 0) ||
511             ( ! map_check_local_db(ac->module))) {
512                 /* No local data or db, just run the remote request */
513                 return ldb_next_remote_request(ac->module, ac->remote_req);
514         }
515
516         /* prepare the search operation */
517         ret = map_search_self_req(&search_req, ac, msg->dn);
518         if (ret != LDB_SUCCESS) {
519                 return LDB_ERR_OPERATIONS_ERROR;
520         }
521
522         return ldb_next_request(module, search_req);
523 }
524
525 /* Modify the local record. */
526 static int map_modify_do_local(struct map_context *ac)
527 {
528         struct ldb_request *local_req;
529         char *dn;
530         int ret;
531
532         if (ac->local_dn == NULL) {
533                 /* No local record present, add it instead */
534                 /* Add local 'IS_MAPPED' */
535                 /* TODO: use GUIDs here instead */
536                 if (ldb_msg_add_empty(ac->local_msg, IS_MAPPED,
537                                         LDB_FLAG_MOD_ADD, NULL) != 0) {
538                         return LDB_ERR_OPERATIONS_ERROR;
539                 }
540                 dn = ldb_dn_alloc_linearized(ac->local_msg,
541                                         ac->remote_req->op.mod.message->dn);
542                 if (ldb_msg_add_string(ac->local_msg, IS_MAPPED, dn) != 0) {
543                         return LDB_ERR_OPERATIONS_ERROR;
544                 }
545
546                 /* Prepare the local operation */
547                 ret = ldb_build_add_req(&local_req, ac->module->ldb, ac,
548                                         ac->local_msg,
549                                         ac->req->controls,
550                                         ac,
551                                         map_op_local_callback,
552                                         ac->req);
553                 if (ret != LDB_SUCCESS) {
554                         return LDB_ERR_OPERATIONS_ERROR;
555                 }
556         } else {
557                 /* Prepare the local operation */
558                 ret = ldb_build_mod_req(&local_req, ac->module->ldb, ac,
559                                         ac->local_msg,
560                                         ac->req->controls,
561                                         ac,
562                                         map_op_local_callback,
563                                         ac->req);
564                 if (ret != LDB_SUCCESS) {
565                         return LDB_ERR_OPERATIONS_ERROR;
566                 }
567         }
568
569         return ldb_next_request(ac->module, local_req);
570 }
571
572 /*****************************************************************************
573  * DELETE operations
574 *****************************************************************************/
575
576 /* Delete a record. */
577 int map_delete(struct ldb_module *module, struct ldb_request *req)
578 {
579         struct ldb_request *search_req;
580         struct map_context *ac;
581         int ret;
582
583         /* Do not manipulate our control entries */
584         if (ldb_dn_is_special(req->op.del.dn)) {
585                 return ldb_next_request(module, req);
586         }
587
588         /* No mapping requested (perhaps no DN mapping specified).
589          * Skip to next module */
590         if (!ldb_dn_check_local(module, req->op.del.dn)) {
591                 return ldb_next_request(module, req);
592         }
593
594         /* Prepare context and handle */
595         ac = map_init_context(module, req);
596         if (ac == NULL) {
597                 return LDB_ERR_OPERATIONS_ERROR;
598         }
599
600         /* Prepare the remote operation */
601         ret = ldb_build_del_req(&ac->remote_req, module->ldb, ac,
602                                    ldb_dn_map_local(module, ac, req->op.del.dn),
603                                    req->controls,
604                                    ac,
605                                    map_op_remote_callback,
606                                    req);
607         if (ret != LDB_SUCCESS) {
608                 return LDB_ERR_OPERATIONS_ERROR;
609         }
610
611         /* No local db, just run the remote request */
612         if (!map_check_local_db(ac->module)) {
613                 /* Do the remote request. */
614                 return ldb_next_remote_request(ac->module, ac->remote_req);
615         }
616
617         /* Prepare the search operation */
618         ret = map_search_self_req(&search_req, ac, req->op.del.dn);
619         if (ret != LDB_SUCCESS) {
620                 map_oom(module);
621                 return LDB_ERR_OPERATIONS_ERROR;
622         }
623
624         return ldb_next_request(module, search_req);
625 }
626
627 /* Delete the local record. */
628 static int map_delete_do_local(struct map_context *ac)
629 {
630         struct ldb_request *local_req;
631         int ret;
632
633         /* No local record, continue remotely */
634         if (ac->local_dn == NULL) {
635                 /* Do the remote request. */
636                 return ldb_next_remote_request(ac->module, ac->remote_req);
637         }
638
639         /* Prepare the local operation */
640         ret = ldb_build_del_req(&local_req, ac->module->ldb, ac,
641                                    ac->req->op.del.dn,
642                                    ac->req->controls,
643                                    ac,
644                                    map_op_local_callback,
645                                    ac->req);
646         if (ret != LDB_SUCCESS) {
647                 return LDB_ERR_OPERATIONS_ERROR;
648         }
649         return ldb_next_request(ac->module, local_req);
650 }
651
652 /*****************************************************************************
653  * RENAME operations
654 *****************************************************************************/
655
656 /* Rename a record. */
657 int map_rename(struct ldb_module *module, struct ldb_request *req)
658 {
659         struct ldb_request *search_req;
660         struct map_context *ac;
661         int ret;
662
663         /* Do not manipulate our control entries */
664         if (ldb_dn_is_special(req->op.rename.olddn)) {
665                 return ldb_next_request(module, req);
666         }
667
668         /* No mapping requested (perhaps no DN mapping specified).
669          * Skip to next module */
670         if ((!ldb_dn_check_local(module, req->op.rename.olddn)) &&
671             (!ldb_dn_check_local(module, req->op.rename.newdn))) {
672                 return ldb_next_request(module, req);
673         }
674
675         /* Rename into/out of the mapped partition requested, bail out */
676         if (!ldb_dn_check_local(module, req->op.rename.olddn) ||
677             !ldb_dn_check_local(module, req->op.rename.newdn)) {
678                 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
679         }
680
681         /* Prepare context and handle */
682         ac = map_init_context(module, req);
683         if (ac == NULL) {
684                 return LDB_ERR_OPERATIONS_ERROR;
685         }
686
687         /* Prepare the remote operation */
688         ret = ldb_build_rename_req(&ac->remote_req, module->ldb, ac,
689                                    ldb_dn_map_local(module, ac, req->op.rename.olddn),
690                                    ldb_dn_map_local(module, ac, req->op.rename.newdn),
691                                    req->controls,
692                                    ac, map_op_remote_callback,
693                                    req);
694         if (ret != LDB_SUCCESS) {
695                 return LDB_ERR_OPERATIONS_ERROR;
696         }
697
698         /* No local db, just run the remote request */
699         if (!map_check_local_db(ac->module)) {
700                 /* Do the remote request. */
701                 return ldb_next_remote_request(ac->module, ac->remote_req);
702         }
703
704         /* Prepare the search operation */
705         ret = map_search_self_req(&search_req, ac, req->op.rename.olddn);
706         if (ret != LDB_SUCCESS) {
707                 map_oom(module);
708                 return LDB_ERR_OPERATIONS_ERROR;
709         }
710
711         return ldb_next_request(module, search_req);
712 }
713
714 /* Rename the local record. */
715 static int map_rename_do_local(struct map_context *ac)
716 {
717         struct ldb_request *local_req;
718         int ret;
719
720         /* No local record, continue remotely */
721         if (ac->local_dn == NULL) {
722                 /* Do the remote request. */
723                 return ldb_next_remote_request(ac->module, ac->remote_req);
724         }
725
726         /* Prepare the local operation */
727         ret = ldb_build_rename_req(&local_req, ac->module->ldb, ac,
728                                    ac->req->op.rename.olddn,
729                                    ac->req->op.rename.newdn,
730                                    ac->req->controls,
731                                    ac,
732                                    map_rename_local_callback,
733                                    ac->req);
734         if (ret != LDB_SUCCESS) {
735                 return LDB_ERR_OPERATIONS_ERROR;
736         }
737
738         return ldb_next_request(ac->module, local_req);
739 }
740
741 static int map_rename_local_callback(struct ldb_request *req,
742                                      struct ldb_reply *ares)
743 {
744         struct map_context *ac;
745         int ret;
746
747         ac = talloc_get_type(req->context, struct map_context);
748
749         if (!ares) {
750                 return ldb_module_done(ac->req, NULL, NULL,
751                                         LDB_ERR_OPERATIONS_ERROR);
752         }
753         if (ares->error != LDB_SUCCESS) {
754                 return ldb_module_done(ac->req, ares->controls,
755                                         ares->response, ares->error);
756         }
757
758         if (ares->type != LDB_REPLY_DONE) {
759                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
760                 return ldb_module_done(ac->req, NULL, NULL,
761                                         LDB_ERR_OPERATIONS_ERROR);
762         }
763
764         /* proceed with next step */
765         ret = map_rename_do_fixup(ac);
766         if (ret != LDB_SUCCESS) {
767                 return ldb_module_done(ac->req, NULL, NULL,
768                                         LDB_ERR_OPERATIONS_ERROR);
769         }
770
771         return LDB_SUCCESS;
772 }
773
774 /* Update the local 'IS_MAPPED' attribute. */
775 static int map_rename_do_fixup(struct map_context *ac)
776 {
777         struct ldb_request *local_req;
778
779         /* Prepare the fixup operation */
780         /* TODO: use GUIDs here instead -- or skip it when GUIDs are used. */
781         local_req = map_build_fixup_req(ac,
782                                         ac->req->op.rename.newdn,
783                                         ac->remote_req->op.rename.newdn,
784                                         ac,
785                                         map_op_local_callback);
786         if (local_req == NULL) {
787                 return LDB_ERR_OPERATIONS_ERROR;
788         }
789
790         return ldb_next_request(ac->module, local_req);
791 }