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