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