2f3a9c439c7fc039b7f358cb0e681d184e15b043
[metze/samba/wip.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    Copyright (C) Simo Sorce 2008
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 3 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, see <http://www.gnu.org/licenses/>.
24
25 */
26
27 /* 
28  *  Name: ldb
29  *
30  *  Component: ldb ldb_map module
31  *
32  *  Description: Map portions of data into a different format on a
33  *  remote partition.
34  *
35  *  Author: Jelmer Vernooij, Martin Kuehl
36  */
37
38 #include "ldb_includes.h"
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(ldb_module_get_private(module), struct map_private);
105         return data->context;
106 }
107
108 /* Create a generic request context. */
109 struct map_context *map_init_context(struct ldb_module *module,
110                                         struct ldb_request *req)
111 {
112         struct ldb_context *ldb;
113         struct map_context *ac;
114
115         ldb = ldb_module_get_ctx(module);
116
117         ac = talloc_zero(req, struct map_context);
118         if (ac == NULL) {
119                 ldb_set_errstring(ldb, "Out of Memory");
120                 return NULL;
121         }
122
123         ac->module = module;
124         ac->req = req;
125
126         return ac;
127 }
128
129 /* Dealing with DNs for different partitions
130  * ========================================= */
131
132 /* Check whether any data should be stored in the local partition. */
133 bool map_check_local_db(struct ldb_module *module)
134 {
135         const struct ldb_map_context *data = map_get_context(module);
136
137         if (!data->remote_base_dn || !data->local_base_dn) {
138                 return false;
139         }
140
141         return true;
142 }
143
144 /* Copy a DN with the base DN of the local partition. */
145 static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
146 {
147         struct ldb_dn *new_dn;
148
149         new_dn = ldb_dn_copy(mem_ctx, dn);
150         if ( ! ldb_dn_validate(new_dn)) {
151                 talloc_free(new_dn);
152                 return NULL;
153         }
154
155         /* may be we don't need to rebase at all */
156         if ( ! data->remote_base_dn || ! data->local_base_dn) {
157                 return new_dn;
158         }
159
160         if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
161                 talloc_free(new_dn);
162                 return NULL;
163         }
164
165         if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
166                 talloc_free(new_dn);
167                 return NULL;
168         }
169
170         return new_dn;
171 }
172
173 /* Copy a DN with the base DN of the remote partition. */
174 static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
175 {
176         struct ldb_dn *new_dn;
177
178         new_dn = ldb_dn_copy(mem_ctx, dn);
179         if ( ! ldb_dn_validate(new_dn)) {
180                 talloc_free(new_dn);
181                 return NULL;
182         }
183
184         /* may be we don't need to rebase at all */
185         if ( ! data->remote_base_dn || ! data->local_base_dn) {
186                 return new_dn;
187         }
188
189         if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
190                 talloc_free(new_dn);
191                 return NULL;
192         }
193
194         if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
195                 talloc_free(new_dn);
196                 return NULL;
197         }
198
199         return new_dn;
200 }
201
202 /* Run a request and make sure it targets the remote partition. */
203 /* TODO: free old DNs and messages? */
204 int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
205 {
206         const struct ldb_map_context *data = map_get_context(module);
207         struct ldb_context *ldb;
208         struct ldb_message *msg;
209
210         ldb = ldb_module_get_ctx(module);
211
212         switch (request->operation) {
213         case LDB_SEARCH:
214                 if (request->op.search.base) {
215                         request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
216                 } else {
217                         request->op.search.base = data->remote_base_dn;
218                         /* TODO: adjust scope? */
219                 }
220                 break;
221
222         case LDB_ADD:
223                 msg = ldb_msg_copy_shallow(request, request->op.add.message);
224                 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
225                 request->op.add.message = msg;
226                 break;
227
228         case LDB_MODIFY:
229                 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
230                 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
231                 request->op.mod.message = msg;
232                 break;
233
234         case LDB_DELETE:
235                 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
236                 break;
237
238         case LDB_RENAME:
239                 request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
240                 request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
241                 break;
242
243         default:
244                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
245                           "Invalid remote request!");
246                 return LDB_ERR_OPERATIONS_ERROR;
247         }
248
249         return ldb_next_request(module, request);
250 }
251
252
253 /* Finding mappings for attributes and objectClasses
254  * ================================================= */
255
256 /* Find an objectClass mapping by the local name. */
257 static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
258 {
259         unsigned int i;
260
261         for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
262                 if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
263                         return &data->objectclass_maps[i];
264                 }
265         }
266
267         return NULL;
268 }
269
270 /* Find an objectClass mapping by the remote name. */
271 static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
272 {
273         unsigned int i;
274
275         for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
276                 if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
277                         return &data->objectclass_maps[i];
278                 }
279         }
280
281         return NULL;
282 }
283
284 /* Find an attribute mapping by the local name. */
285 const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
286 {
287         unsigned int i;
288
289         for (i = 0; data->attribute_maps[i].local_name; i++) {
290                 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
291                         return &data->attribute_maps[i];
292                 }
293         }
294         for (i = 0; data->attribute_maps[i].local_name; i++) {
295                 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
296                         return &data->attribute_maps[i];
297                 }
298         }
299
300         return NULL;
301 }
302
303 /* Find an attribute mapping by the remote name. */
304 const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
305 {
306         const struct ldb_map_attribute *map;
307         const struct ldb_map_attribute *wildcard = NULL;
308         unsigned int i, j;
309
310         for (i = 0; data->attribute_maps[i].local_name; i++) {
311                 map = &data->attribute_maps[i];
312                 if (ldb_attr_cmp(map->local_name, "*") == 0) {
313                         wildcard = &data->attribute_maps[i];
314                 }
315
316                 switch (map->type) {
317                 case LDB_MAP_IGNORE:
318                         break;
319
320                 case LDB_MAP_KEEP:
321                         if (ldb_attr_cmp(map->local_name, name) == 0) {
322                                 return map;
323                         }
324                         break;
325
326                 case LDB_MAP_RENAME:
327                 case LDB_MAP_CONVERT:
328                         if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
329                                 return map;
330                         }
331                         break;
332
333                 case LDB_MAP_GENERATE:
334                         for (j = 0; map->u.generate.remote_names && map->u.generate.remote_names[j]; j++) {
335                                 if (ldb_attr_cmp(map->u.generate.remote_names[j], name) == 0) {
336                                         return map;
337                                 }
338                         }
339                         break;
340                 }
341         }
342
343         /* We didn't find it, so return the wildcard record if one was configured */
344         return wildcard;
345 }
346
347
348 /* Mapping attributes
349  * ================== */
350
351 /* Check whether an attribute will be mapped into the remote partition. */
352 bool map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
353 {
354         const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
355
356         if (map == NULL) {
357                 return false;
358         }
359         if (map->type == LDB_MAP_IGNORE) {
360                 return false;
361         }
362
363         return true;
364 }
365
366 /* Map an attribute name into the remote partition. */
367 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
368 {
369         if (map == NULL) {
370                 return talloc_strdup(mem_ctx, attr);
371         }
372
373         switch (map->type) {
374         case LDB_MAP_KEEP:
375                 return talloc_strdup(mem_ctx, attr);
376
377         case LDB_MAP_RENAME:
378         case LDB_MAP_CONVERT:
379                 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
380
381         default:
382                 return NULL;
383         }
384 }
385
386 /* Map an attribute name back into the local partition. */
387 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
388 {
389         if (map == NULL) {
390                 return talloc_strdup(mem_ctx, attr);
391         }
392
393         if (map->type == LDB_MAP_KEEP) {
394                 return talloc_strdup(mem_ctx, attr);
395         }
396
397         return talloc_strdup(mem_ctx, map->local_name);
398 }
399
400
401 /* Merge two lists of attributes into a single one. */
402 int map_attrs_merge(struct ldb_module *module, void *mem_ctx, 
403                     const char ***attrs, const char * const *more_attrs)
404 {
405         unsigned int i, j, k;
406
407         for (i = 0; *attrs && (*attrs)[i]; i++) /* noop */ ;
408         for (j = 0; more_attrs && more_attrs[j]; j++) /* noop */ ;
409         
410         *attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1);
411         if (*attrs == NULL) {
412                 map_oom(module);
413                 return -1;
414         }
415
416         for (k = 0; k < j; k++) {
417                 (*attrs)[i + k] = more_attrs[k];
418         }
419
420         (*attrs)[i+k] = NULL;
421
422         return 0;
423 }
424
425 /* Mapping ldb values
426  * ================== */
427
428 /* Map an ldb value into the remote partition. */
429 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx, 
430                                  const struct ldb_map_attribute *map, const struct ldb_val *val)
431 {
432         if (map && (map->type == LDB_MAP_CONVERT) && (map->u.convert.convert_local)) {
433                 return map->u.convert.convert_local(module, mem_ctx, val);
434         }
435
436         return ldb_val_dup(mem_ctx, val);
437 }
438
439 /* Map an ldb value back into the local partition. */
440 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx, 
441                                   const struct ldb_map_attribute *map, const struct ldb_val *val)
442 {
443         if (map && (map->type == LDB_MAP_CONVERT) && (map->u.convert.convert_remote)) {
444                 return map->u.convert.convert_remote(module, mem_ctx, val);
445         }
446
447         return ldb_val_dup(mem_ctx, val);
448 }
449
450
451 /* Mapping DNs
452  * =========== */
453
454 /* Check whether a DN is below the local baseDN. */
455 bool ldb_dn_check_local(struct ldb_module *module, struct ldb_dn *dn)
456 {
457         const struct ldb_map_context *data = map_get_context(module);
458
459         if (!data->local_base_dn) {
460                 return true;
461         }
462
463         return ldb_dn_compare_base(data->local_base_dn, dn) == 0;
464 }
465
466 /* Map a DN into the remote partition. */
467 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
468 {
469         const struct ldb_map_context *data = map_get_context(module);
470         struct ldb_context *ldb;
471         struct ldb_dn *newdn;
472         const struct ldb_map_attribute *map;
473         enum ldb_map_attr_type map_type;
474         const char *name;
475         struct ldb_val value;
476         unsigned int i;
477         int ret;
478
479         if (dn == NULL) {
480                 return NULL;
481         }
482
483         ldb = ldb_module_get_ctx(module);
484
485         newdn = ldb_dn_copy(mem_ctx, dn);
486         if (newdn == NULL) {
487                 map_oom(module);
488                 return NULL;
489         }
490
491         /* For each RDN, map the component name and possibly the value */
492         for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
493                 map = map_attr_find_local(data, ldb_dn_get_component_name(dn, i));
494
495                 /* Unknown attribute - leave this RDN as is and hope the best... */
496                 if (map == NULL) {
497                         map_type = LDB_MAP_KEEP;
498                 } else {
499                         map_type = map->type;
500                 }
501
502                 switch (map_type) {
503                 case LDB_MAP_IGNORE:
504                 case LDB_MAP_GENERATE:
505                         ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
506                                   "LDB_MAP_IGNORE/LDB_MAP_GENERATE attribute '%s' "
507                                   "used in DN!", ldb_dn_get_component_name(dn, i));
508                         goto failed;
509
510                 case LDB_MAP_CONVERT:
511                         if (map->u.convert.convert_local == NULL) {
512                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
513                                           "'convert_local' not set for attribute '%s' "
514                                           "used in DN!", ldb_dn_get_component_name(dn, i));
515                                 goto failed;
516                         }
517                         /* fall through */
518                 case LDB_MAP_KEEP:
519                 case LDB_MAP_RENAME:
520                         name = map_attr_map_local(newdn, map, ldb_dn_get_component_name(dn, i));
521                         if (name == NULL) goto failed;
522
523                         value = ldb_val_map_local(module, newdn, map, ldb_dn_get_component_val(dn, i));
524                         if (value.data == NULL) goto failed;
525
526                         ret = ldb_dn_set_component(newdn, i, name, value);
527                         if (ret != LDB_SUCCESS) {
528                                 goto failed;
529                         }
530
531                         break;
532                 }
533         }
534
535         return newdn;
536
537 failed:
538         talloc_free(newdn);
539         return NULL;
540 }
541
542 /* Map a DN into the local partition. */
543 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
544 {
545         const struct ldb_map_context *data = map_get_context(module);
546         struct ldb_context *ldb;
547         struct ldb_dn *newdn;
548         const struct ldb_map_attribute *map;
549         enum ldb_map_attr_type map_type;
550         const char *name;
551         struct ldb_val value;
552         unsigned int i;
553         int ret;
554
555         if (dn == NULL) {
556                 return NULL;
557         }
558
559         ldb = ldb_module_get_ctx(module);
560
561         newdn = ldb_dn_copy(mem_ctx, dn);
562         if (newdn == NULL) {
563                 map_oom(module);
564                 return NULL;
565         }
566
567         /* For each RDN, map the component name and possibly the value */
568         for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
569                 map = map_attr_find_remote(data, ldb_dn_get_component_name(dn, i));
570
571                 /* Unknown attribute - leave this RDN as is and hope the best... */
572                 if (map == NULL) {
573                         map_type = LDB_MAP_KEEP;
574                 } else {
575                         map_type = map->type;
576                 }
577
578                 switch (map_type) {
579                 case LDB_MAP_IGNORE:
580                 case LDB_MAP_GENERATE:
581                         ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
582                                   "LDB_MAP_IGNORE/LDB_MAP_GENERATE attribute '%s' "
583                                   "used in DN!", ldb_dn_get_component_name(dn, i));
584                         goto failed;
585
586                 case LDB_MAP_CONVERT:
587                         if (map->u.convert.convert_remote == NULL) {
588                                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
589                                           "'convert_remote' not set for attribute '%s' "
590                                           "used in DN!", ldb_dn_get_component_name(dn, i));
591                                 goto failed;
592                         }
593                         /* fall through */
594                 case LDB_MAP_KEEP:
595                 case LDB_MAP_RENAME:
596                         name = map_attr_map_remote(newdn, map, ldb_dn_get_component_name(dn, i));
597                         if (name == NULL) goto failed;
598
599                         value = ldb_val_map_remote(module, newdn, map, ldb_dn_get_component_val(dn, i));
600                         if (value.data == NULL) goto failed;
601
602                         ret = ldb_dn_set_component(newdn, i, name, value);
603                         if (ret != LDB_SUCCESS) {
604                                 goto failed;
605                         }
606
607                         break;
608                 }
609         }
610
611         return newdn;
612
613 failed:
614         talloc_free(newdn);
615         return NULL;
616 }
617
618 /* Map a DN and its base into the local partition. */
619 /* TODO: This should not be required with GUIDs. */
620 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
621 {
622         const struct ldb_map_context *data = map_get_context(module);
623         struct ldb_dn *dn1, *dn2;
624
625         dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
626         dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
627
628         talloc_free(dn1);
629         return dn2;
630 }
631
632
633 /* Converting DNs and objectClasses (as ldb values)
634  * ================================================ */
635
636 /* Map a DN contained in an ldb value into the remote partition. */
637 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
638 {
639         struct ldb_context *ldb;
640         struct ldb_dn *dn, *newdn;
641         struct ldb_val newval;
642
643         ldb = ldb_module_get_ctx(module);
644
645         dn = ldb_dn_from_ldb_val(mem_ctx, ldb, val);
646         if (! ldb_dn_validate(dn)) {
647                 newval.length = 0;
648                 newval.data = NULL;
649                 talloc_free(dn);
650                 return newval;
651         }
652         newdn = ldb_dn_map_local(module, mem_ctx, dn);
653         talloc_free(dn);
654
655         newval.length = 0;
656         newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
657         if (newval.data) {
658                 newval.length = strlen((char *)newval.data);
659         }
660         talloc_free(newdn);
661
662         return newval;
663 }
664
665 /* Map a DN contained in an ldb value into the local partition. */
666 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
667 {
668         struct ldb_context *ldb;
669         struct ldb_dn *dn, *newdn;
670         struct ldb_val newval;
671
672         ldb = ldb_module_get_ctx(module);
673
674         dn = ldb_dn_from_ldb_val(mem_ctx, ldb, val);
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_remote(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 an objectClass into the remote partition. */
695 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
696 {
697         const struct ldb_map_context *data = map_get_context(module);
698         const char *name = (char *)val->data;
699         const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
700         struct ldb_val newval;
701
702         if (map) {
703                 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
704                 newval.length = strlen((char *)newval.data);
705                 return newval;
706         }
707
708         return ldb_val_dup(mem_ctx, val);
709 }
710
711 /* Generate a remote message with a mapped objectClass. */
712 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)
713 {
714         const struct ldb_map_context *data = map_get_context(module);
715         struct ldb_context *ldb;
716         struct ldb_message_element *el, *oc;
717         struct ldb_val val;
718         bool found_extensibleObject = false;
719         unsigned int i;
720
721         ldb = ldb_module_get_ctx(module);
722
723         /* Find old local objectClass */
724         oc = ldb_msg_find_element(old, "objectClass");
725         if (oc == NULL) {
726                 return;
727         }
728
729         /* Prepare new element */
730         el = talloc_zero(remote, struct ldb_message_element);
731         if (el == NULL) {
732                 ldb_oom(ldb);
733                 return;                 /* TODO: fail? */
734         }
735
736         /* Copy local objectClass element, reverse space for an extra value */
737         el->num_values = oc->num_values + 1;
738         el->values = talloc_array(el, struct ldb_val, el->num_values);
739         if (el->values == NULL) {
740                 talloc_free(el);
741                 ldb_oom(ldb);
742                 return;                 /* TODO: fail? */
743         }
744
745         /* Copy local element name "objectClass" */
746         el->name = talloc_strdup(el, local_attr);
747
748         /* Convert all local objectClasses */
749         for (i = 0; i < el->num_values - 1; i++) {
750                 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
751                 if (ldb_attr_cmp((char *)el->values[i].data, data->add_objectclass) == 0) {
752                         found_extensibleObject = true;
753                 }
754         }
755
756         if (!found_extensibleObject) {
757                 val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
758                 val.length = strlen((char *)val.data);
759
760                 /* Append additional objectClass data->add_objectclass */
761                 el->values[i] = val;
762         } else {
763                 el->num_values--;
764         }
765
766         /* Add new objectClass to remote message */
767         ldb_msg_add(remote, el, 0);
768 }
769
770 /* Map an objectClass into the local partition. */
771 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
772 {
773         const struct ldb_map_context *data = map_get_context(module);
774         const char *name = (char *)val->data;
775         const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
776         struct ldb_val newval;
777
778         if (map) {
779                 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
780                 newval.length = strlen((char *)newval.data);
781                 return newval;
782         }
783
784         return ldb_val_dup(mem_ctx, val);
785 }
786
787 /* Generate a local message with a mapped objectClass. */
788 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)
789 {
790         const struct ldb_map_context *data = map_get_context(module);
791         struct ldb_context *ldb;
792         struct ldb_message_element *el, *oc;
793         struct ldb_val val;
794         unsigned int i;
795
796         ldb = ldb_module_get_ctx(module);
797
798         /* Find old remote objectClass */
799         oc = ldb_msg_find_element(remote, "objectClass");
800         if (oc == NULL) {
801                 return NULL;
802         }
803
804         /* Prepare new element */
805         el = talloc_zero(mem_ctx, struct ldb_message_element);
806         if (el == NULL) {
807                 ldb_oom(ldb);
808                 return NULL;
809         }
810
811         /* Copy remote objectClass element */
812         el->num_values = oc->num_values;
813         el->values = talloc_array(el, struct ldb_val, el->num_values);
814         if (el->values == NULL) {
815                 talloc_free(el);
816                 ldb_oom(ldb);
817                 return NULL;
818         }
819
820         /* Copy remote element name "objectClass" */
821         el->name = talloc_strdup(el, local_attr);
822
823         /* Convert all remote objectClasses */
824         for (i = 0; i < el->num_values; i++) {
825                 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
826         }
827
828         val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
829         val.length = strlen((char *)val.data);
830
831         /* Remove last value if it was the string in data->add_objectclass (eg samba4top, extensibleObject) */
832         if (ldb_val_equal_exact(&val, &el->values[i-1])) {
833                 el->num_values--;
834                 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
835                 if (el->values == NULL) {
836                         talloc_free(el);
837                         ldb_oom(ldb);
838                         return NULL;
839                 }
840         }
841
842         return el;
843 }
844
845 static const struct ldb_map_attribute objectclass_convert_map = {
846         .local_name = "objectClass",
847         .type = LDB_MAP_CONVERT,
848         .u = {
849                 .convert = {
850                         .remote_name = "objectClass",
851                         .convert_local = map_objectclass_convert_local,
852                         .convert_remote = map_objectclass_convert_remote,
853                 },
854         },
855 };
856
857
858 /* Mappings for searches on objectClass= assuming a one-to-one
859  * mapping.  Needed because this is a generate operator for the
860  * add/modify code */
861 static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx, 
862                                             struct ldb_parse_tree **new, const struct ldb_parse_tree *tree) 
863 {
864         
865         return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_convert_map);
866 }
867
868 /* Auxiliary request construction
869  * ============================== */
870
871 /* Build a request to search a record by its DN. */
872 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_map_callback_t callback)
873 {
874         const struct ldb_parse_tree *search_tree;
875         struct ldb_context *ldb;
876         struct ldb_request *req;
877         int ret;
878
879         ldb = ldb_module_get_ctx(ac->module);
880
881         if (tree) {
882                 search_tree = tree;
883         } else {
884                 search_tree = ldb_parse_tree(ac, NULL);
885                 if (search_tree == NULL) {
886                         return NULL;
887                 }
888         }
889
890         ret = ldb_build_search_req_ex(&req, ldb, ac,
891                                         dn, LDB_SCOPE_BASE,
892                                         search_tree, attrs,
893                                         NULL,
894                                         context, callback,
895                                         ac->req);
896         if (ret != LDB_SUCCESS) {
897                 return NULL;
898         }
899
900         return req;
901 }
902
903 /* Build a request to update the 'IS_MAPPED' attribute */
904 struct ldb_request *map_build_fixup_req(struct map_context *ac,
905                                         struct ldb_dn *olddn,
906                                         struct ldb_dn *newdn,
907                                         void *context,
908                                         ldb_map_callback_t callback)
909 {
910         struct ldb_context *ldb;
911         struct ldb_request *req;
912         struct ldb_message *msg;
913         const char *dn;
914         int ret;
915
916         ldb = ldb_module_get_ctx(ac->module);
917
918         /* Prepare message */
919         msg = ldb_msg_new(ac);
920         if (msg == NULL) {
921                 map_oom(ac->module);
922                 return NULL;
923         }
924
925         /* Update local 'IS_MAPPED' to the new remote DN */
926         msg->dn = ldb_dn_copy(msg, olddn);
927         dn = ldb_dn_alloc_linearized(msg, newdn);
928         if ( ! dn || ! ldb_dn_validate(msg->dn)) {
929                 goto failed;
930         }
931         if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
932                 goto failed;
933         }
934         if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
935                 goto failed;
936         }
937
938         /* Prepare request */
939         ret = ldb_build_mod_req(&req, ldb,
940                                 ac, msg, NULL,
941                                 context, callback,
942                                 ac->req);
943         if (ret != LDB_SUCCESS) {
944                 goto failed;
945         }
946         talloc_steal(req, msg);
947
948         return req;
949 failed:
950         talloc_free(msg);
951         return NULL;
952 }
953
954 /* Module initialization
955  * ===================== */
956
957
958 /* Builtin mappings for DNs and objectClasses */
959 static const struct ldb_map_attribute builtin_attribute_maps[] = {
960         {
961                 .local_name = "dn",
962                 .type = LDB_MAP_CONVERT,
963                 .u = {
964                         .convert = {
965                                  .remote_name = "dn",
966                                  .convert_local = ldb_dn_convert_local,
967                                  .convert_remote = ldb_dn_convert_remote,
968                          },
969                 },
970         },
971         {
972                 .local_name = NULL,
973         }
974 };
975
976 static const struct ldb_map_attribute objectclass_attribute_map = {
977         .local_name = "objectClass",
978         .type = LDB_MAP_GENERATE,
979         .convert_operator = map_objectclass_convert_operator,
980         .u = {
981                 .generate = {
982                         .remote_names = { "objectClass", NULL },
983                         .generate_local = map_objectclass_generate_local,
984                         .generate_remote = map_objectclass_generate_remote,
985                 },
986         },
987 };
988
989
990 /* Find the special 'MAP_DN_NAME' record and store local and remote
991  * base DNs in private data. */
992 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
993 {
994         static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
995         struct ldb_context *ldb;
996         struct ldb_dn *dn;
997         struct ldb_message *msg;
998         struct ldb_result *res;
999         int ret;
1000
1001         if (!name) {
1002                 data->local_base_dn = NULL;
1003                 data->remote_base_dn = NULL;
1004                 return LDB_SUCCESS;
1005         }
1006
1007         ldb = ldb_module_get_ctx(module);
1008
1009         dn = ldb_dn_new_fmt(data, ldb, "%s=%s", MAP_DN_NAME, name);
1010         if ( ! ldb_dn_validate(dn)) {
1011                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
1012                           "Failed to construct '%s' DN!", MAP_DN_NAME);
1013                 return LDB_ERR_OPERATIONS_ERROR;
1014         }
1015
1016         ret = ldb_search(ldb, data, &res, dn, LDB_SCOPE_BASE, attrs, NULL);
1017         talloc_free(dn);
1018         if (ret != LDB_SUCCESS) {
1019                 return ret;
1020         }
1021         if (res->count == 0) {
1022                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
1023                           "No results for '%s=%s'!", MAP_DN_NAME, name);
1024                 talloc_free(res);
1025                 return LDB_ERR_CONSTRAINT_VIOLATION;
1026         }
1027         if (res->count > 1) {
1028                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
1029                           "Too many results for '%s=%s'!", MAP_DN_NAME, name);
1030                 talloc_free(res);
1031                 return LDB_ERR_CONSTRAINT_VIOLATION;
1032         }
1033
1034         msg = res->msgs[0];
1035         data->local_base_dn = ldb_msg_find_attr_as_dn(ldb, data, msg, MAP_DN_FROM);
1036         data->remote_base_dn = ldb_msg_find_attr_as_dn(ldb, data, msg, MAP_DN_TO);
1037         talloc_free(res);
1038
1039         return LDB_SUCCESS;
1040 }
1041
1042 /* Store attribute maps and objectClass maps in private data. */
1043 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data, 
1044                          const struct ldb_map_attribute *attrs, 
1045                          const struct ldb_map_objectclass *ocls, 
1046                          const char * const *wildcard_attributes)
1047 {
1048         unsigned int i, j, last;
1049         last = 0;
1050
1051         /* Count specified attribute maps */
1052         for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1053         /* Count built-in attribute maps */
1054         for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1055
1056         /* Store list of attribute maps */
1057         data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+2);
1058         if (data->attribute_maps == NULL) {
1059                 map_oom(module);
1060                 return LDB_ERR_OPERATIONS_ERROR;
1061         }
1062
1063         /* Specified ones go first */
1064         for (i = 0; attrs[i].local_name; i++) {
1065                 data->attribute_maps[last] = attrs[i];
1066                 last++;
1067         }
1068
1069         /* Built-in ones go last */
1070         for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1071                 data->attribute_maps[last] = builtin_attribute_maps[i];
1072                 last++;
1073         }
1074
1075         if (data->add_objectclass) {
1076                 /* ObjectClass one is very last, if required */
1077                 data->attribute_maps[last] = objectclass_attribute_map;
1078                 last++;
1079         } else if (ocls) {
1080                 data->attribute_maps[last] = objectclass_convert_map;
1081                 last++;
1082         }
1083
1084         /* Ensure 'local_name == NULL' for the last entry */
1085         memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1086
1087         /* Store list of objectClass maps */
1088         data->objectclass_maps = ocls;
1089
1090         data->wildcard_attributes = wildcard_attributes;
1091
1092         return LDB_SUCCESS;
1093 }
1094
1095 /* Initialize global private data. */
1096 _PUBLIC_ int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs, 
1097                           const struct ldb_map_objectclass *ocls,
1098                           const char * const *wildcard_attributes,
1099                           const char *add_objectclass,
1100                           const char *name)
1101 {
1102         struct map_private *data;
1103         int ret;
1104
1105         /* Prepare private data */
1106         data = talloc_zero(module, struct map_private);
1107         if (data == NULL) {
1108                 map_oom(module);
1109                 return LDB_ERR_OPERATIONS_ERROR;
1110         }
1111
1112         ldb_module_set_private(module, data);
1113
1114         data->context = talloc_zero(data, struct ldb_map_context);
1115         if (!data->context) {
1116                 map_oom(module);
1117                 return LDB_ERR_OPERATIONS_ERROR;                
1118         }
1119
1120         /* Store local and remote baseDNs */
1121         ret = map_init_dns(module, data->context, name);
1122         if (ret != LDB_SUCCESS) {
1123                 talloc_free(data);
1124                 return ret;
1125         }
1126
1127         data->context->add_objectclass = add_objectclass;
1128
1129         /* Store list of attribute and objectClass maps */
1130         ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
1131         if (ret != LDB_SUCCESS) {
1132                 talloc_free(data);
1133                 return ret;
1134         }
1135
1136         return LDB_SUCCESS;
1137 }