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