r17525: This is a merge from the Google Summer of Code 2006 project by Martin Kühl
[gd/samba-autobuild/.git] / source4 / lib / ldb / modules / ldb_map.c
1 /*
2    ldb database mapping module
3
4    Copyright (C) Jelmer Vernooij 2005
5    Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
6
7    * NOTICE: this module is NOT released under the GNU LGPL license as
8    * other ldb code. This module is release under the GNU GPL v2 or
9    * later license.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 /* 
27  *  Name: ldb
28  *
29  *  Component: ldb ldb_map module
30  *
31  *  Description: Map portions of data into a different format on a
32  *  remote partition.
33  *
34  *  Author: Jelmer Vernooij, Martin Kuehl
35  */
36
37 #include "includes.h"
38 #include "ldb/include/includes.h"
39
40 #include "ldb/modules/ldb_map.h"
41 #include "ldb/modules/ldb_map_private.h"
42
43 /* Description of the provided ldb requests:
44  - special attribute 'isMapped'
45
46  - search:
47      - if parse tree can be split
48          - search remote records w/ remote attrs and parse tree
49      - otherwise
50          - enumerate all remote records
51      - for each remote result
52          - map remote result to local message
53          - search local result
54          - is present
55              - merge local into remote result
56              - run callback on merged result
57          - otherwise
58              - run callback on remote result
59
60  - add:
61      - split message into local and remote part
62      - if local message is not empty
63          - add isMapped to local message
64          - add local message
65      - add remote message
66
67  - modify:
68      - split message into local and remote part
69      - if local message is not empty
70          - add isMapped to local message
71          - search for local record
72          - if present
73              - modify local record
74          - otherwise
75              - add local message
76      - modify remote record
77
78  - delete:
79      - search for local record
80      - if present
81          - delete local record
82      - delete remote record
83
84  - rename:
85      - search for local record
86      - if present
87          - rename local record
88          - modify local isMapped
89      - rename remote record
90 */
91
92
93
94 /* Private data structures
95  * ======================= */
96
97 /* Global private data */
98 /* Extract mappings from private data. */
99 const struct ldb_map_context *map_get_context(struct ldb_module *module)
100 {
101         const struct map_private *data = talloc_get_type(module->private_data, struct map_private);
102         return &data->context;
103 }
104
105 /* Create a generic request context. */
106 static struct map_context *map_init_context(struct ldb_handle *h, struct ldb_request *req)
107 {
108         struct map_context *ac;
109
110         ac = talloc_zero(h, struct map_context);
111         if (ac == NULL) {
112                 map_oom(h->module);
113                 return NULL;
114         }
115
116         ac->module = h->module;
117         ac->orig_req = req;
118
119         return ac;
120 }
121
122 /* Create a search request context. */
123 struct map_search_context *map_init_search_context(struct map_context *ac, struct ldb_reply *ares)
124 {
125         struct map_search_context *sc;
126
127         sc = talloc_zero(ac, struct map_search_context);
128         if (sc == NULL) {
129                 map_oom(ac->module);
130                 return NULL;
131         }
132
133         sc->ac = ac;
134         sc->local_res = NULL;
135         sc->remote_res = ares;
136
137         return sc;
138 }
139
140 /* Create a request context and handle. */
141 struct ldb_handle *map_init_handle(struct ldb_request *req, struct ldb_module *module)
142 {
143         struct map_context *ac;
144         struct ldb_handle *h;
145
146         h = talloc_zero(req, struct ldb_handle);
147         if (h == NULL) {
148                 map_oom(module);
149                 return NULL;
150         }
151
152         h->module = module;
153
154         ac = map_init_context(h, req);
155         if (ac == NULL) {
156                 talloc_free(h);
157                 return NULL;
158         }
159
160         h->private_data = (void *)ac;
161
162         h->state = LDB_ASYNC_INIT;
163         h->status = LDB_SUCCESS;
164
165         return h;
166 }
167
168
169 /* Dealing with DNs for different partitions
170  * ========================================= */
171
172 /* Check whether any data should be stored in the local partition. */
173 BOOL map_check_local_db(struct ldb_module *module)
174 {
175         const struct ldb_map_context *data = map_get_context(module);
176
177         if (!data->remote_base_dn || !data->local_base_dn) {
178                 return False;
179         }
180
181         return True;
182 }
183
184 /* WARK: verbatim copy from ldb_dn.c */
185 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
186 {
187         struct ldb_dn_component dst;
188
189         memset(&dst, 0, sizeof(dst));
190
191         if (src == NULL) {
192                 return dst;
193         }
194
195         dst.value = ldb_val_dup(mem_ctx, &(src->value));
196         if (dst.value.data == NULL) {
197                 return dst;
198         }
199
200         dst.name = talloc_strdup(mem_ctx, src->name);
201         if (dst.name == NULL) {
202                 talloc_free(dst.value.data);
203         }
204
205         return dst;
206 }
207
208 /* Copy a DN but replace the old with the new base DN. */
209 static struct ldb_dn *ldb_dn_rebase(void *mem_ctx, const struct ldb_dn *old, const struct ldb_dn *old_base, const struct ldb_dn *new_base)
210 {
211         struct ldb_dn *new;
212         int i, offset;
213
214         /* Perhaps we don't need to rebase at all? */
215         if (!old_base || !new_base) {
216                 return ldb_dn_copy(mem_ctx, old);
217         }
218
219         offset = old->comp_num - old_base->comp_num;
220         new = ldb_dn_copy_partial(mem_ctx, new_base, offset + new_base->comp_num);
221         for (i = 0; i < offset; i++) {
222                 new->components[i] = ldb_dn_copy_component(new->components, &(old->components[i]));
223         }
224
225         return new;
226 }
227
228 /* Copy a DN with the base DN of the local partition. */
229 static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, const struct ldb_dn *dn)
230 {
231         return ldb_dn_rebase(mem_ctx, dn, data->remote_base_dn, data->local_base_dn);
232 }
233
234 /* Copy a DN with the base DN of the remote partition. */
235 static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, const struct ldb_dn *dn)
236 {
237         return ldb_dn_rebase(mem_ctx, dn, data->local_base_dn, data->remote_base_dn);
238 }
239
240 /* Run a request and make sure it targets the remote partition. */
241 /* TODO: free old DNs and messages? */
242 int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
243 {
244         const struct ldb_map_context *data = map_get_context(module);
245         struct ldb_message *msg;
246
247         switch (request->operation) {
248         case LDB_SEARCH:
249                 if (request->op.search.base) {
250                         request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
251                 } else {
252                         request->op.search.base = data->remote_base_dn;
253                         /* TODO: adjust scope? */
254                 }
255                 break;
256
257         case LDB_ADD:
258                 msg = ldb_msg_copy_shallow(request, request->op.add.message);
259                 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
260                 request->op.add.message = msg;
261                 break;
262
263         case LDB_MODIFY:
264                 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
265                 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
266                 request->op.mod.message = msg;
267                 break;
268
269         case LDB_DELETE:
270                 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
271                 break;
272
273         case LDB_RENAME:
274                 request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
275                 request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
276                 break;
277
278         default:
279                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
280                           "Invalid remote request!\n");
281                 return LDB_ERR_OPERATIONS_ERROR;
282         }
283
284         return ldb_next_request(module, request);
285 }
286
287
288 /* Finding mappings for attributes and objectClasses
289  * ================================================= */
290
291 /* Find an objectClass mapping by the local name. */
292 static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
293 {
294         int i;
295
296         for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
297                 if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
298                         return &data->objectclass_maps[i];
299                 }
300         }
301
302         return NULL;
303 }
304
305 /* Find an objectClass mapping by the remote name. */
306 static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
307 {
308         int i;
309
310         for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
311                 if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
312                         return &data->objectclass_maps[i];
313                 }
314         }
315
316         return NULL;
317 }
318
319 /* Find an attribute mapping by the local name. */
320 const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
321 {
322         int i;
323
324         for (i = 0; data->attribute_maps[i].local_name; i++) {
325                 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
326                         return &data->attribute_maps[i];
327                 }
328         }
329         for (i = 0; data->attribute_maps[i].local_name; i++) {
330                 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
331                         return &data->attribute_maps[i];
332                 }
333         }
334
335         return NULL;
336 }
337
338 /* Find an attribute mapping by the remote name. */
339 const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
340 {
341         const struct ldb_map_attribute *wildcard = NULL;
342         int i, j;
343
344         for (i = 0; data->attribute_maps[i].local_name; i++) {
345                 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
346                         wildcard = &data->attribute_maps[i];
347                 }
348
349                 switch (data->attribute_maps[i].type) {
350                 case MAP_IGNORE:
351                         break;
352
353                 case MAP_KEEP:
354                         if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
355                                 return &data->attribute_maps[i];
356                         }
357                         break;
358
359                 case MAP_RENAME:
360                 case MAP_CONVERT:
361                         if (ldb_attr_cmp(data->attribute_maps[i].u.rename.remote_name, name) == 0) {
362                                 return &data->attribute_maps[i];
363                         }
364                         break;
365
366                 case MAP_GENERATE:
367                         for (j = 0; data->attribute_maps[i].u.generate.remote_names[j]; j++) {
368                                 if (ldb_attr_cmp(data->attribute_maps[i].u.generate.remote_names[j], name) == 0) {
369                                         return &data->attribute_maps[i];
370                                 }
371                         }
372                         break;
373                 }
374         }
375
376         /* We didn't find it, so return the wildcard record if one was configured */
377         return wildcard;
378 }
379
380
381 /* Mapping attributes
382  * ================== */
383
384 /* Check whether an attribute will be mapped into the remote partition. */
385 BOOL map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
386 {
387         const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
388
389         if (map == NULL) {
390                 return False;
391         }
392         if (map->type == MAP_IGNORE) {
393                 return False;
394         }
395
396         return True;
397 }
398
399 /* Map an attribute name into the remote partition. */
400 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
401 {
402         if (map == NULL) {
403                 return talloc_strdup(mem_ctx, attr);
404         }
405
406         switch (map->type) {
407         case MAP_KEEP:
408                 return talloc_strdup(mem_ctx, attr);
409
410         case MAP_RENAME:
411         case MAP_CONVERT:
412                 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
413
414         default:
415                 return NULL;
416         }
417 }
418
419 /* Map an attribute name back into the local partition. */
420 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
421 {
422         if (map == NULL) {
423                 return talloc_strdup(mem_ctx, attr);
424         }
425
426         if (map->type == MAP_KEEP) {
427                 return talloc_strdup(mem_ctx, attr);
428         }
429
430         return talloc_strdup(mem_ctx, map->local_name);
431 }
432
433 /* Mapping ldb values
434  * ================== */
435
436 /* Map an ldb value into the remote partition. */
437 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_val val)
438 {
439         if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
440                 return map->u.convert.convert_local(module, mem_ctx, &val);
441         }
442
443         return ldb_val_dup(mem_ctx, &val);
444 }
445
446 /* Map an ldb value back into the local partition. */
447 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_val val)
448 {
449         if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
450                 return map->u.convert.convert_remote(module, mem_ctx, &val);
451         }
452
453         return ldb_val_dup(mem_ctx, &val);
454 }
455
456
457 /* Mapping DNs
458  * =========== */
459
460 /* Check whether a DN is below the local baseDN. */
461 BOOL ldb_dn_check_local(struct ldb_module *module, const struct ldb_dn *dn)
462 {
463         const struct ldb_map_context *data = map_get_context(module);
464
465         if (!data->local_base_dn) {
466                 return True;
467         }
468
469         return ldb_dn_compare_base(module->ldb, data->local_base_dn, dn) == 0;
470 }
471
472 /* Map a DN into the remote partition. */
473 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
474 {
475         const struct ldb_map_context *data = map_get_context(module);
476         struct ldb_dn *newdn;
477         struct ldb_dn_component *old, *new;
478         const struct ldb_map_attribute *map;
479         enum ldb_map_attr_type map_type;
480         int i;
481
482         if (dn == NULL) {
483                 return NULL;
484         }
485
486         newdn = ldb_dn_copy(mem_ctx, dn);
487         if (newdn == NULL) {
488                 map_oom(module);
489                 return NULL;
490         }
491
492         /* For each RDN, map the component name and possibly the value */
493         for (i = 0; i < newdn->comp_num; i++) {
494                 old = &dn->components[i];
495                 new = &newdn->components[i];
496                 map = map_attr_find_local(data, old->name);
497
498                 /* Unknown attribute - leave this RDN as is and hope the best... */
499                 if (map == NULL) {
500                         map_type = MAP_KEEP;
501                 } else {
502                         map_type = map->type;
503                 }
504
505                 switch (map_type) {
506                 case MAP_IGNORE:
507                 case MAP_GENERATE:
508                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
509                                   "MAP_IGNORE/MAP_GENERATE attribute '%s' "
510                                   "used in DN!\n", old->name);
511                         goto failed;
512
513                 case MAP_CONVERT:
514                         if (map->u.convert.convert_local == NULL) {
515                                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
516                                           "'convert_local' not set for attribute '%s' "
517                                           "used in DN!\n", old->name);
518                                 goto failed;
519                         }
520                         /* fall through */
521                 case MAP_KEEP:
522                 case MAP_RENAME:
523                         new->name = discard_const_p(char, map_attr_map_local(newdn->components, map, old->name));
524                         new->value = ldb_val_map_local(module, newdn->components, map, old->value);
525                         break;
526                 }
527         }
528
529         return newdn;
530
531 failed:
532         talloc_free(newdn);
533         return NULL;
534 }
535
536 /* Map a DN into the local partition. */
537 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
538 {
539         const struct ldb_map_context *data = map_get_context(module);
540         struct ldb_dn *newdn;
541         struct ldb_dn_component *old, *new;
542         const struct ldb_map_attribute *map;
543         enum ldb_map_attr_type map_type;
544         int i;
545
546         if (dn == NULL) {
547                 return NULL;
548         }
549
550         newdn = ldb_dn_copy(mem_ctx, dn);
551         if (newdn == NULL) {
552                 map_oom(module);
553                 return NULL;
554         }
555
556         /* For each RDN, map the component name and possibly the value */
557         for (i = 0; i < newdn->comp_num; i++) {
558                 old = &dn->components[i];
559                 new = &newdn->components[i];
560                 map = map_attr_find_remote(data, old->name);
561
562                 /* Unknown attribute - leave this RDN as is and hope the best... */
563                 if (map == NULL) {
564                         map_type = MAP_KEEP;
565                 } else {
566                         map_type = map->type;
567                 }
568
569                 switch (map_type) {
570                 case MAP_IGNORE:
571                 case MAP_GENERATE:
572                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
573                                   "MAP_IGNORE/MAP_GENERATE attribute '%s' "
574                                   "used in DN!\n", old->name);
575                         goto failed;
576
577                 case MAP_CONVERT:
578                         if (map->u.convert.convert_remote == NULL) {
579                                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
580                                           "'convert_remote' not set for attribute '%s' "
581                                           "used in DN!\n", old->name);
582                                 goto failed;
583                         }
584                         /* fall through */
585                 case MAP_KEEP:
586                 case MAP_RENAME:
587                         new->name = discard_const_p(char, map_attr_map_remote(newdn->components, map, old->name));
588                         new->value = ldb_val_map_remote(module, newdn->components, map, old->value);
589                         break;
590                 }
591         }
592
593         return newdn;
594
595 failed:
596         talloc_free(newdn);
597         return NULL;
598 }
599
600 /* Map a DN and its base into the local partition. */
601 /* TODO: This should not be required with GUIDs. */
602 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
603 {
604         const struct ldb_map_context *data = map_get_context(module);
605         struct ldb_dn *dn1, *dn2;
606
607         dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
608         dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
609
610         talloc_free(dn1);
611         return dn2;
612 }
613
614
615 /* Converting DNs and objectClasses (as ldb values)
616  * ================================================ */
617
618 /* Map a DN contained in an ldb value into the remote partition. */
619 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
620 {
621         struct ldb_dn *dn, *newdn;
622         struct ldb_val newval;
623
624         dn = ldb_dn_explode(mem_ctx, (char *)val->data);
625         newdn = ldb_dn_map_local(module, mem_ctx, dn);
626         talloc_free(dn);
627
628         newval.length = 0;
629         newval.data = (uint8_t *)ldb_dn_linearize(mem_ctx, newdn);
630         if (newval.data) {
631                 newval.length = strlen((char *)newval.data);
632         }
633         talloc_free(newdn);
634
635         return newval;
636 }
637
638 /* Map a DN contained in an ldb value into the local partition. */
639 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
640 {
641         struct ldb_dn *dn, *newdn;
642         struct ldb_val newval;
643
644         dn = ldb_dn_explode(mem_ctx, (char *)val->data);
645         newdn = ldb_dn_map_remote(module, mem_ctx, dn);
646         talloc_free(dn);
647
648         newval.length = 0;
649         newval.data = (uint8_t *)ldb_dn_linearize(mem_ctx, newdn);
650         if (newval.data) {
651                 newval.length = strlen((char *)newval.data);
652         }
653         talloc_free(newdn);
654
655         return newval;
656 }
657
658 /* Map an objectClass into the remote partition. */
659 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
660 {
661         const struct ldb_map_context *data = map_get_context(module);
662         const char *name = (char *)val->data;
663         const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
664         struct ldb_val newval;
665
666         if (map) {
667                 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
668                 newval.length = strlen((char *)newval.data);
669                 return newval;
670         }
671
672         return ldb_val_dup(mem_ctx, val);
673 }
674
675 /* Generate a remote message with a mapped objectClass. */
676 static void map_objectclass_generate_remote(struct ldb_module *module, const char *local_attr, const struct ldb_message *old, struct ldb_message *remote, struct ldb_message *local)
677 {
678         struct ldb_message_element *el, *oc;
679         struct ldb_val val;
680         BOOL found_extensibleObject = False;
681         int i;
682
683         /* Find old local objectClass */
684         oc = ldb_msg_find_element(old, local_attr);
685         if (oc == NULL) {
686                 return;
687         }
688
689         /* Prepare new element */
690         el = talloc_zero(remote, struct ldb_message_element);
691         if (el == NULL) {
692                 ldb_oom(module->ldb);
693                 return;                 /* TODO: fail? */
694         }
695
696         /* Copy local objectClass element, reverse space for an extra value */
697         el->num_values = oc->num_values + 1;
698         el->values = talloc_array(el, struct ldb_val, el->num_values);
699         if (el->values == NULL) {
700                 talloc_free(el);
701                 ldb_oom(module->ldb);
702                 return;                 /* TODO: fail? */
703         }
704
705         /* Copy local element name "objectClass" */
706         el->name = talloc_strdup(el, local_attr);
707
708         /* Convert all local objectClasses */
709         for (i = 0; i < el->num_values - 1; i++) {
710                 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
711                 if (ldb_attr_cmp((char *)el->values[i].data, "extensibleObject") == 0) {
712                         found_extensibleObject = True;
713                 }
714         }
715
716         if (!found_extensibleObject) {
717                 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
718                 val.length = strlen((char *)val.data);
719
720                 /* Append additional objectClass "extensibleObject" */
721                 el->values[i] = val;
722         } else {
723                 el->num_values--;
724         }
725
726         /* Add new objectClass to remote message */
727         ldb_msg_add(remote, el, 0);
728 }
729
730 /* Map an objectClass into the local partition. */
731 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
732 {
733         const struct ldb_map_context *data = map_get_context(module);
734         const char *name = (char *)val->data;
735         const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
736         struct ldb_val newval;
737
738         if (map) {
739                 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
740                 newval.length = strlen((char *)newval.data);
741                 return newval;
742         }
743
744         return ldb_val_dup(mem_ctx, val);
745 }
746
747 /* Generate a local message with a mapped objectClass. */
748 static struct ldb_message_element *map_objectclass_generate_local(struct ldb_module *module, void *mem_ctx, const char *remote_attr, const struct ldb_message *remote)
749 {
750         struct ldb_message_element *el, *oc;
751         struct ldb_val val;
752         int i;
753
754         /* Find old remote objectClass */
755         oc = ldb_msg_find_element(remote, remote_attr);
756         if (oc == NULL) {
757                 return NULL;
758         }
759
760         /* Prepare new element */
761         el = talloc_zero(mem_ctx, struct ldb_message_element);
762         if (el == NULL) {
763                 ldb_oom(module->ldb);
764                 return NULL;
765         }
766
767         /* Copy remote objectClass element */
768         el->num_values = oc->num_values;
769         el->values = talloc_array(el, struct ldb_val, el->num_values);
770         if (el->values == NULL) {
771                 talloc_free(el);
772                 ldb_oom(module->ldb);
773                 return NULL;
774         }
775
776         /* Copy remote element name "objectClass" */
777         el->name = talloc_strdup(el, remote_attr);
778
779         /* Convert all remote objectClasses */
780         for (i = 0; i < el->num_values; i++) {
781                 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
782         }
783
784         val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
785         val.length = strlen((char *)val.data);
786
787         /* Remove last value if it was "extensibleObject" */
788         if (ldb_val_equal_exact(&val, &el->values[i-1])) {
789                 el->num_values--;
790                 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
791                 if (el->values == NULL) {
792                         talloc_free(el);
793                         ldb_oom(module->ldb);
794                         return NULL;
795                 }
796         }
797
798         return el;
799 }
800
801
802
803 /* Auxiliary request construction
804  * ============================== */
805
806 /* Store the DN of a single search result in context. */
807 static int map_search_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
808 {
809         struct map_context *ac;
810
811         if (context == NULL || ares == NULL) {
812                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
813                 return LDB_ERR_OPERATIONS_ERROR;
814         }
815
816         ac = talloc_get_type(context, struct map_context);
817
818         /* We are interested only in the single reply */
819         if (ares->type != LDB_REPLY_ENTRY) {
820                 talloc_free(ares);
821                 return LDB_SUCCESS;
822         }
823
824         /* We have already found a remote DN */
825         if (ac->local_dn) {
826                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "Too many results to base search"));
827                 talloc_free(ares);
828                 return LDB_ERR_OPERATIONS_ERROR;
829         }
830
831         /* Store local DN */
832         ac->local_dn = ares->message->dn;
833
834         return LDB_SUCCESS;
835 }
836
837 /* Build a request to search a record by its DN. */
838 struct ldb_request *map_search_base_req(struct map_context *ac, const struct ldb_dn *dn, const char * const *attrs, const struct ldb_parse_tree *tree, void *context, ldb_search_callback callback)
839 {
840         struct ldb_request *req;
841
842         req = talloc_zero(ac, struct ldb_request);
843         if (req == NULL) {
844                 map_oom(ac->module);
845                 return NULL;
846         }
847
848         req->operation = LDB_SEARCH;
849         req->op.search.base = dn;
850         req->op.search.scope = LDB_SCOPE_BASE;
851         req->op.search.attrs = attrs;
852
853         if (tree) {
854                 req->op.search.tree = tree;
855         } else {
856                 req->op.search.tree = ldb_parse_tree(req, NULL);
857                 if (req->op.search.tree == NULL) {
858                         talloc_free(req);
859                         return NULL;
860                 }
861         }
862
863         req->controls = NULL;
864         req->context = context;
865         req->callback = callback;
866         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, req);
867
868         return req;
869 }
870
871 /* Build a request to search the local record by its DN. */
872 struct ldb_request *map_search_self_req(struct map_context *ac, const struct ldb_dn *dn)
873 {
874         /* attrs[] is returned from this function in
875          * ac->search_req->op.search.attrs, so it must be static, as
876          * otherwise the compiler can put it on the stack */
877         static const char * const attrs[] = { IS_MAPPED, NULL };
878         struct ldb_parse_tree *tree;
879
880         /* Limit search to records with 'IS_MAPPED' present */
881         /* TODO: `tree = ldb_parse_tree(ac, IS_MAPPED);' won't do. */
882         tree = talloc_zero(ac, struct ldb_parse_tree);
883         if (tree == NULL) {
884                 map_oom(ac->module);
885                 return NULL;
886         }
887
888         tree->operation = LDB_OP_PRESENT;
889         tree->u.present.attr = talloc_strdup(tree, IS_MAPPED);
890
891         return map_search_base_req(ac, dn, attrs, tree, ac, map_search_self_callback);
892 }
893
894 /* Build a request to update the 'IS_MAPPED' attribute */
895 struct ldb_request *map_build_fixup_req(struct map_context *ac, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
896 {
897         struct ldb_request *req;
898         struct ldb_message *msg;
899         const char *dn;
900
901         /* Prepare request */
902         req = talloc_zero(ac, struct ldb_request);
903         if (req == NULL) {
904                 map_oom(ac->module);
905                 return NULL;
906         }
907
908         /* Prepare message */
909         msg = ldb_msg_new(req);
910         if (msg == NULL) {
911                 map_oom(ac->module);
912                 goto failed;
913         }
914
915         /* Update local 'IS_MAPPED' to the new remote DN */
916         msg->dn = discard_const_p(struct ldb_dn, olddn);
917         dn = ldb_dn_linearize(msg, newdn);
918         if (dn == NULL) {
919                 goto failed;
920         }
921         if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE) != 0) {
922                 goto failed;
923         }
924         if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
925                 goto failed;
926         }
927
928         req->operation = LDB_MODIFY;
929         req->op.mod.message = msg;
930         req->controls = NULL;
931         req->handle = NULL;
932         req->context = NULL;
933         req->callback = NULL;
934
935         return req;
936
937 failed:
938         talloc_free(req);
939         return NULL;
940 }
941
942
943 /* Asynchronous call structure
944  * =========================== */
945
946 /* Figure out which request is currently pending. */
947 static struct ldb_request *map_get_req(struct map_context *ac)
948 {
949         switch (ac->step) {
950         case MAP_SEARCH_SELF_MODIFY:
951         case MAP_SEARCH_SELF_DELETE:
952         case MAP_SEARCH_SELF_RENAME:
953                 return ac->search_req;
954
955         case MAP_ADD_REMOTE:
956         case MAP_MODIFY_REMOTE:
957         case MAP_DELETE_REMOTE:
958         case MAP_RENAME_REMOTE:
959                 return ac->remote_req;
960
961         case MAP_RENAME_FIXUP:
962                 return ac->down_req;
963
964         case MAP_ADD_LOCAL:
965         case MAP_MODIFY_LOCAL:
966         case MAP_DELETE_LOCAL:
967         case MAP_RENAME_LOCAL:
968                 return ac->local_req;
969
970         case MAP_SEARCH_REMOTE:
971                 /* Can't happen */
972                 break;
973         }
974
975         return NULL;            /* unreachable; silences a warning */
976 }
977
978 typedef int (*map_next_function)(struct ldb_handle *handle);
979
980 /* Figure out the next request to run. */
981 static map_next_function map_get_next(struct map_context *ac)
982 {
983         switch (ac->step) {
984         case MAP_SEARCH_REMOTE:
985                 return NULL;
986
987         case MAP_ADD_LOCAL:
988                 return map_add_do_remote;
989         case MAP_ADD_REMOTE:
990                 return NULL;
991
992         case MAP_SEARCH_SELF_MODIFY:
993                 return map_modify_do_local;
994         case MAP_MODIFY_LOCAL:
995                 return map_modify_do_remote;
996         case MAP_MODIFY_REMOTE:
997                 return NULL;
998
999         case MAP_SEARCH_SELF_DELETE:
1000                 return map_delete_do_local;
1001         case MAP_DELETE_LOCAL:
1002                 return map_delete_do_remote;
1003         case MAP_DELETE_REMOTE:
1004                 return NULL;
1005
1006         case MAP_SEARCH_SELF_RENAME:
1007                 return map_rename_do_local;
1008         case MAP_RENAME_LOCAL:
1009                 return map_rename_do_fixup;
1010         case MAP_RENAME_FIXUP:
1011                 return map_rename_do_remote;
1012         case MAP_RENAME_REMOTE:
1013                 return NULL;
1014         }
1015
1016         return NULL;            /* unreachable; silences a warning */
1017 }
1018
1019 /* Wait for the current pending request to finish and continue with the next. */
1020 static int map_wait_next(struct ldb_handle *handle)
1021 {
1022         struct map_context *ac;
1023         struct ldb_request *req;
1024         map_next_function next;
1025         int ret;
1026
1027         if (handle == NULL || handle->private_data == NULL) {
1028                 return LDB_ERR_OPERATIONS_ERROR;
1029         }
1030
1031         if (handle->state == LDB_ASYNC_DONE) {
1032                 return handle->status;
1033         }
1034
1035         handle->state = LDB_ASYNC_PENDING;
1036         handle->status = LDB_SUCCESS;
1037
1038         ac = talloc_get_type(handle->private_data, struct map_context);
1039
1040         if (ac->step == MAP_SEARCH_REMOTE) {
1041                 int i;
1042                 for (i = 0; i < ac->num_searches; i++) {
1043                         req = ac->search_reqs[i];
1044                         ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1045
1046                         if (ret != LDB_SUCCESS) {
1047                                 handle->status = ret;
1048                                 goto done;
1049                         }
1050                         if (req->handle->status != LDB_SUCCESS) {
1051                                 handle->status = req->handle->status;
1052                                 goto done;
1053                         }
1054                         if (req->handle->state != LDB_ASYNC_DONE) {
1055                                 return LDB_SUCCESS;
1056                         }
1057                 }
1058         } else {
1059
1060                 req = map_get_req(ac);
1061
1062                 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1063
1064                 if (ret != LDB_SUCCESS) {
1065                         handle->status = ret;
1066                         goto done;
1067                 }
1068                 if (req->handle->status != LDB_SUCCESS) {
1069                         handle->status = req->handle->status;
1070                         goto done;
1071                 }
1072                 if (req->handle->state != LDB_ASYNC_DONE) {
1073                         return LDB_SUCCESS;
1074                 }
1075
1076                 next = map_get_next(ac);
1077                 if (next) {
1078                         return next(handle);
1079                 }
1080         }
1081
1082         ret = LDB_SUCCESS;
1083
1084 done:
1085         handle->state = LDB_ASYNC_DONE;
1086         return ret;
1087 }
1088
1089 /* Wait for all current pending requests to finish. */
1090 static int map_wait_all(struct ldb_handle *handle)
1091 {
1092         int ret;
1093
1094         while (handle->state != LDB_ASYNC_DONE) {
1095                 ret = map_wait_next(handle);
1096                 if (ret != LDB_SUCCESS) {
1097                         return ret;
1098                 }
1099         }
1100
1101         return handle->status;
1102 }
1103
1104 /* Wait for pending requests to finish. */
1105 static int map_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1106 {
1107         if (type == LDB_WAIT_ALL) {
1108                 return map_wait_all(handle);
1109         } else {
1110                 return map_wait_next(handle);
1111         }
1112 }
1113
1114
1115 /* Module initialization
1116  * ===================== */
1117
1118 /* Provided module operations */
1119 static const struct ldb_module_ops map_ops = {
1120         .name           = "ldb_map",
1121         .add            = map_add,
1122         .modify         = map_modify,
1123         .del            = map_delete,
1124         .rename         = map_rename,
1125         .search         = map_search,
1126         .wait           = map_wait,
1127 };
1128
1129 /* Builtin mappings for DNs and objectClasses */
1130 static const struct ldb_map_attribute builtin_attribute_maps[] = {
1131         {
1132                 .local_name = "dn",
1133                 .type = MAP_CONVERT,
1134                 .u = {
1135                         .convert = {
1136                                  .remote_name = "dn",
1137                                  .convert_local = ldb_dn_convert_local,
1138                                  .convert_remote = ldb_dn_convert_remote,
1139                          },
1140                 },
1141         },
1142         {
1143                 .local_name = "objectclass",
1144                 .type = MAP_GENERATE,
1145                 .u = {
1146                         .generate = {
1147                                  .remote_names = { "objectclass", NULL },
1148                                  .generate_local = map_objectclass_generate_local,
1149                                  .generate_remote = map_objectclass_generate_remote,
1150                          },
1151                 },
1152         },
1153         {
1154                 .local_name = NULL,
1155         }
1156 };
1157
1158 /* Find the special 'MAP_DN_NAME' record and store local and remote
1159  * base DNs in private data. */
1160 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
1161 {
1162         static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
1163         struct ldb_dn *dn;
1164         struct ldb_message *msg;
1165         struct ldb_result *res;
1166         int ret;
1167
1168         if (!name) {
1169                 data->local_base_dn = NULL;
1170                 data->remote_base_dn = NULL;
1171                 return LDB_SUCCESS;
1172         }
1173
1174         dn = ldb_dn_string_compose(data, NULL, "%s=%s", MAP_DN_NAME, name);
1175         if (dn == NULL) {
1176                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1177                           "Failed to construct '%s' DN!\n", MAP_DN_NAME);
1178                 return LDB_ERR_OPERATIONS_ERROR;
1179         }
1180
1181         ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
1182         talloc_free(dn);
1183         if (ret != LDB_SUCCESS) {
1184                 return ret;
1185         }
1186         if (res->count == 0) {
1187                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1188                           "No results for '%s=%s'!\n", MAP_DN_NAME, name);
1189                 return LDB_ERR_CONSTRAINT_VIOLATION;
1190         }
1191         if (res->count > 1) {
1192                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1193                           "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
1194                 return LDB_ERR_CONSTRAINT_VIOLATION;
1195         }
1196
1197         msg = res->msgs[0];
1198         data->local_base_dn = ldb_msg_find_attr_as_dn(data, msg, MAP_DN_FROM);
1199         data->remote_base_dn = ldb_msg_find_attr_as_dn(data, msg, MAP_DN_TO);
1200         talloc_free(res);
1201
1202         return LDB_SUCCESS;
1203 }
1204
1205 /* Store attribute maps and objectClass maps in private data. */
1206 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls)
1207 {
1208         int i, j, last;
1209         last = 0;
1210
1211         /* Count specified attribute maps */
1212         for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1213         /* Count built-in attribute maps */
1214         for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1215
1216         /* Store list of attribute maps */
1217         data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+1);
1218         if (data->attribute_maps == NULL) {
1219                 map_oom(module);
1220                 return LDB_ERR_OPERATIONS_ERROR;
1221         }
1222
1223         /* Specified ones go first */
1224         for (i = 0; attrs[i].local_name; i++) {
1225                 data->attribute_maps[last] = attrs[i];
1226                 last++;
1227         }
1228
1229         /* Built-in ones go last */
1230         for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1231                 data->attribute_maps[last] = builtin_attribute_maps[i];
1232                 last++;
1233         }
1234
1235         /* Ensure 'local_name == NULL' for the last entry */
1236         memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1237
1238         /* Store list of objectClass maps */
1239         data->objectclass_maps = ocls;
1240
1241         return LDB_SUCCESS;
1242 }
1243
1244 /* Copy the list of provided module operations. */
1245 struct ldb_module_ops ldb_map_get_ops(void)
1246 {
1247         return map_ops;
1248 }
1249
1250 /* Initialize global private data. */
1251 int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls, const char *name)
1252 {
1253         struct map_private *data;
1254         int ret;
1255
1256         /* Prepare private data */
1257         data = talloc_zero(module, struct map_private);
1258         if (data == NULL) {
1259                 map_oom(module);
1260                 return LDB_ERR_OPERATIONS_ERROR;
1261         }
1262
1263         module->private_data = data;
1264
1265         /* Store local and remote baseDNs */
1266         ret = map_init_dns(module, &(data->context), name);
1267         if (ret != LDB_SUCCESS) {
1268                 talloc_free(data);
1269                 return ret;
1270         }
1271
1272         /* Store list of attribute and objectClass maps */
1273         ret = map_init_maps(module, &(data->context), attrs, ocls);
1274         if (ret != LDB_SUCCESS) {
1275                 talloc_free(data);
1276                 return ret;
1277         }
1278
1279         return LDB_SUCCESS;
1280 }
1281
1282 /* Usage note for initialization of this module:
1283  *
1284  * ldb_map is meant to be used from a different module that sets up
1285  * the mappings and gets registered in ldb.
1286  *
1287  * 'ldb_map_init' initializes the private data of this module and
1288  * stores the attribute and objectClass maps in there.  It also looks
1289  * up the '@MAP' special DN so requests can be redirected to the
1290  * remote partition.
1291  *
1292  * This function should be called from the 'init_context' op of the
1293  * module using ldb_map.
1294  *
1295  * 'ldb_map_get_ops' returns a copy of ldb_maps module operations.
1296  *
1297  * It should be called from the initialize function of the using
1298  * module, which should then override the 'init_context' op with a
1299  * function making the appropriate calls to 'ldb_map_init'.
1300  */