r9842: More error checks in the ldb_map modules, extend testsuite
[ira/wip.git] / source4 / lib / ldb / modules / ldb_map.c
1 /* 
2    ldb database library - map backend
3
4    Copyright (C) Jelmer Vernooij 2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "includes.h"
26 #include "ldb/include/ldb.h"
27 #include "ldb/include/ldb_private.h"
28 #include "ldb/modules/ldb_map.h"
29
30 /* TODO:
31  *  - objectclass hint in ldb_map_attribute 
32  *     for use when multiple remote attributes (independant of each other)
33  *     map to one local attribute. E.g.: (uid, gidNumber) -> unixName
34  *     (use MAP_GENERATE instead ?) 
35  */
36
37 /*
38  - special attribute 'isMapped'
39  - add/modify
40         - split up ldb_message into fallback and mapped parts if is_mappable
41  - search: 
42         - search local one for not isMapped entries
43         - remove remote attributes from ldb_parse_tree
44         - search remote one
45          - per record, search local one for additional data (by dn)
46          - test if (full expression) is now true
47  - delete
48         - delete both
49  - rename
50         - rename locally and remotely
51 */
52
53 static struct ldb_val map_convert_local_dn(struct ldb_module *map,
54                                            TALLOC_CTX *ctx,
55                                            const struct ldb_val *val);
56 static struct ldb_val map_convert_remote_dn(struct ldb_module *map,
57                                             TALLOC_CTX *ctx,
58                                             const struct ldb_val *val);
59 static struct ldb_val map_convert_local_objectclass(struct ldb_module *map,
60                                                     TALLOC_CTX *ctx,
61                                                     const struct ldb_val *val);
62 static struct ldb_val map_convert_remote_objectclass(struct ldb_module *map,
63                                                      TALLOC_CTX *ctx,
64                                                      const struct ldb_val *val);
65
66 static const struct ldb_map_attribute builtin_attribute_maps[] = {
67         {
68                 .local_name = "dn",
69                 .type = MAP_CONVERT,
70                 .u.convert.remote_name = "dn",
71                 .u.convert.convert_local = map_convert_local_dn,
72                 .u.convert.convert_remote = map_convert_remote_dn,
73         },
74         {
75                 .local_name = "objectclass",
76                 .type = MAP_CONVERT,
77                 .u.convert.remote_name = "objectclass",
78                 .u.convert.convert_local = map_convert_local_objectclass,
79                 .u.convert.convert_remote = map_convert_remote_objectclass,
80         },
81         {
82                 .local_name = NULL,
83         }
84 };
85
86 struct map_private {
87         struct ldb_map_context context;
88         const char *last_err_string;
89 };
90
91 static struct ldb_map_context *map_get_privdat(struct ldb_module *module)
92 {
93         return &((struct map_private *)module->private_data)->context;
94 }
95
96 static const struct ldb_map_objectclass *map_find_objectclass_local(struct ldb_map_context *privdat, const char *name)
97 {
98         int i;
99         for (i = 0; privdat->objectclass_maps[i].local_name; i++) {
100                 if (!ldb_attr_cmp(privdat->objectclass_maps[i].local_name, name))
101                         return &privdat->objectclass_maps[i];
102         }
103
104         return NULL;
105 }
106
107 /* Decide whether a add/modify should be pushed to the 
108  * remote LDAP server. We currently only do this if we see an objectClass we know */
109 static int map_is_mappable(struct ldb_map_context *privdat, const struct ldb_message *msg)
110 {
111         int i;
112         struct ldb_message_element *el;
113
114         if (ldb_dn_is_special(msg->dn))
115                 return 0;
116         
117         el = ldb_msg_find_element(msg, "objectClass");
118
119         /* No objectClass... */
120         if (el == NULL) {
121                 return 0;
122         }
123
124         for (i = 0; i < el->num_values; i++) {
125                 if (map_find_objectclass_local(privdat, (char *)el->values[i].data))
126                         return 1;
127         }
128
129         return 0;
130 }
131
132 /* find an attribute by the local name */
133 static const struct ldb_map_attribute *map_find_attr_local(struct ldb_map_context *privdat, const char *attr)
134 {
135         int i;
136
137         for (i = 0; privdat->attribute_maps[i].local_name; i++) {
138                 if (!ldb_attr_cmp(privdat->attribute_maps[i].local_name, attr)) 
139                         return &privdat->attribute_maps[i];
140         }
141
142         return NULL;
143 }
144
145 /* find an attribute by the remote name */
146 static const struct ldb_map_attribute *map_find_attr_remote(struct ldb_map_context *privdat, const char *attr)
147 {
148         int i;
149
150         for (i = 0; privdat->attribute_maps[i].local_name; i++) {
151                 if (privdat->attribute_maps[i].type == MAP_IGNORE)
152                         continue;
153
154                 if (privdat->attribute_maps[i].type == MAP_GENERATE)
155                         continue;
156
157                 if (privdat->attribute_maps[i].type == MAP_KEEP &&
158                         ldb_attr_cmp(privdat->attribute_maps[i].local_name, attr) == 0)
159                         return &privdat->attribute_maps[i];
160
161                 if ((privdat->attribute_maps[i].type == MAP_RENAME ||
162                         privdat->attribute_maps[i].type == MAP_CONVERT) &&
163                         ldb_attr_cmp(privdat->attribute_maps[i].u.rename.remote_name, attr) == 0) 
164                         return &privdat->attribute_maps[i];
165
166         }
167
168         return NULL;
169 }
170
171 static struct ldb_parse_tree *ldb_map_parse_tree(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_parse_tree *tree)
172 {
173         int i;
174         const struct ldb_map_attribute *attr;
175         struct ldb_parse_tree *new_tree;
176         enum ldb_map_attr_type map_type;
177         struct ldb_val value, newvalue;
178         struct ldb_map_context *privdat = map_get_privdat(module);
179
180         if (tree == NULL)
181                 return NULL;
182         
183
184         /* Find attr in question and:
185          *  - if it has a convert_operator function, run that
186          *  - otherwise, replace attr name with required[0] */
187
188         if (tree->operation == LDB_OP_AND || 
189                 tree->operation == LDB_OP_OR) {
190                 
191                 new_tree = talloc_memdup(ctx, tree, sizeof(*tree));
192                 new_tree->u.list.elements = talloc_array(new_tree, struct ldb_parse_tree *, tree->u.list.num_elements);
193                 new_tree->u.list.num_elements = 0;
194                 for (i = 0; i < tree->u.list.num_elements; i++) {
195                         struct ldb_parse_tree *child = ldb_map_parse_tree(module, new_tree, tree->u.list.elements[i]);
196                         
197                         if (child) {
198                                 new_tree->u.list.elements[i] = child;
199                                 new_tree->u.list.num_elements++;
200                         }
201                 }
202
203                 return new_tree;
204         }
205                 
206         if (tree->operation == LDB_OP_NOT) {
207                 struct ldb_parse_tree *child;
208                 
209                 new_tree = talloc_memdup(ctx, tree, sizeof(*tree));
210                 child = ldb_map_parse_tree(module, new_tree, tree->u.isnot.child);
211
212                 if (!child) {
213                         talloc_free(new_tree);
214                         return NULL;
215                 }
216
217                 new_tree->u.isnot.child = child;
218                 return new_tree;
219         }
220
221         /* tree->operation is LDB_OP_EQUALITY, LDB_OP_SUBSTRING, LDB_OP_GREATER,
222          * LDB_OP_LESS, LDB_OP_APPROX, LDB_OP_PRESENT or LDB_OP_EXTENDED
223          *
224          * (all have attr as the first element)
225          */
226
227         attr = map_find_attr_local(privdat, tree->u.equality.attr);
228
229         if (!attr) {
230                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Unable to find local attribute '%s', removing from parse tree\n", tree->u.equality.attr);
231                 map_type = MAP_IGNORE;
232         } else {
233                 map_type = attr->type;
234         }
235
236         if (attr && attr->convert_operator) {
237                 /* Run convert_operator */
238                 return attr->convert_operator(privdat, module, tree);
239         }
240
241         if (map_type == MAP_IGNORE) {
242                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Not mapping search on ignored attribute '%s'\n", tree->u.equality.attr);
243                 return NULL;
244         }
245
246         if (map_type == MAP_GENERATE) {
247                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Can't do conversion for MAP_GENERATE in map_parse_tree without convert_operator for '%s'\n", tree->u.equality.attr);
248                 return NULL;
249         }
250
251         if (tree->operation == LDB_OP_EQUALITY) {
252                 value = tree->u.equality.value;
253         } else if (tree->operation == LDB_OP_LESS || tree->operation == LDB_OP_GREATER ||
254                            tree->operation == LDB_OP_APPROX) {
255                 value = tree->u.comparison.value;
256         } else if (tree->operation == LDB_OP_EXTENDED) {
257                 value = tree->u.extended.value;
258         }
259         
260         new_tree = talloc_memdup(ctx, tree, sizeof(*tree));
261
262         if (map_type == MAP_KEEP) {
263                 new_tree->u.equality.attr = talloc_strdup(new_tree, tree->u.equality.attr);
264         } else { /* MAP_RENAME / MAP_CONVERT */
265                 new_tree->u.equality.attr = talloc_strdup(new_tree, attr->u.rename.remote_name);
266         }
267
268         if (new_tree->operation == LDB_OP_PRESENT) 
269                 return new_tree;
270                 
271         if (new_tree->operation == LDB_OP_SUBSTRING) {
272                 new_tree->u.substring.chunks = NULL; /* FIXME! */
273                 return new_tree;
274         }
275
276         if (map_type == MAP_CONVERT) {
277                 newvalue = attr->u.convert.convert_local(module, new_tree, &value);
278         } else {
279                 newvalue = ldb_val_dup(new_tree, &value);
280         }
281
282         if (new_tree->operation == LDB_OP_EQUALITY) {
283                 new_tree->u.equality.value = newvalue;
284         } else if (new_tree->operation == LDB_OP_LESS || new_tree->operation == LDB_OP_GREATER ||
285                            new_tree->operation == LDB_OP_APPROX) {
286                 new_tree->u.comparison.value = newvalue;
287         } else if (new_tree->operation == LDB_OP_EXTENDED) {
288                 new_tree->u.extended.value = newvalue;
289                 new_tree->u.extended.rule_id = talloc_strdup(new_tree, tree->u.extended.rule_id);
290         }
291         
292         return new_tree;
293 }
294
295 /* Remote DN -> Local DN */
296 static struct ldb_dn *map_remote_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_dn *dn)
297 {
298         struct ldb_dn *newdn;
299         int i;
300
301         if (dn == NULL)
302                 return NULL;
303
304         newdn = talloc_memdup(ctx, dn, sizeof(*dn));
305         if (!newdn) 
306                 return NULL;
307
308         newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num); 
309
310         if (!newdn->components)
311                 return NULL;
312
313         /* For each rdn, map the attribute name and possibly the 
314          * complete rdn */
315         
316         for (i = 0; i < dn->comp_num; i++) {
317                 const struct ldb_map_attribute *attr = map_find_attr_remote(module->private_data, dn->components[i].name);
318                 enum ldb_map_attr_type map_type;
319
320                 /* Unknown attribute - leave this dn as is and hope the best... */
321                 if (!attr) map_type = MAP_KEEP;
322                 else map_type = attr->type;
323                         
324                 switch (map_type) { 
325                 case MAP_IGNORE:
326                 case MAP_GENERATE:
327                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Local MAP_IGNORE or MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name);
328                         talloc_free(newdn);
329                         return NULL;
330
331                 case MAP_KEEP:
332                         newdn->components[i].name = talloc_strdup(newdn->components, dn->components[i].name);
333                         newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
334                         break;
335                         
336                 case MAP_CONVERT:
337                         newdn->components[i].name = talloc_strdup(newdn->components, attr->local_name);
338                         newdn->components[i].value = attr->u.convert.convert_remote(module, ctx, &dn->components[i].value);
339                         break;
340                         
341                 case MAP_RENAME:
342                         newdn->components[i].name = talloc_strdup(newdn->components, attr->local_name);
343                         newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
344                         break;
345                 }
346         }
347         return newdn;
348 }
349
350 /* Local DN -> Remote DN */
351 static struct ldb_dn *map_local_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_dn *dn)
352 {       
353         struct ldb_dn *newdn;
354         int i;
355
356         if (dn == NULL)
357                 return NULL;
358
359         newdn = talloc_memdup(ctx, dn, sizeof(*dn));
360         if (!newdn) 
361                 return NULL;
362
363         newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num); 
364
365         if (!newdn->components)
366                 return NULL;
367
368         /* For each rdn, map the attribute name and possibly the 
369          * complete rdn using an equality convert_operator call */
370         
371         for (i = 0; i < dn->comp_num; i++) {
372                 const struct ldb_map_attribute *attr = map_find_attr_local(module->private_data, dn->components[i].name);
373                 enum ldb_map_attr_type map_type;
374
375                 /* Unknown attribute - leave this dn as is and hope the best... */
376                 if (!attr) map_type = MAP_KEEP; else map_type = attr->type;
377                 
378                 switch (map_type) 
379                 {
380                         case MAP_IGNORE: 
381                         case MAP_GENERATE:
382                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Local MAP_IGNORE/MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name);
383                         talloc_free(newdn);
384                         return NULL;
385
386                         case MAP_CONVERT: 
387                                 newdn->components[i].name = talloc_strdup(newdn->components, attr->u.convert.remote_name);
388                                 newdn->components[i].value = attr->u.convert.convert_local(module, newdn->components, &dn->components[i].value);
389                         break;
390                         
391                         case MAP_RENAME:
392                                 newdn->components[i].name = talloc_strdup(newdn->components, attr->u.rename.remote_name);
393                                 newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
394                         break;
395
396                         case MAP_KEEP:
397                                 newdn->components[i].name = talloc_strdup(newdn->components, dn->components[i].name);
398                                 newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
399                         continue;
400                 }
401         }
402
403         return newdn;
404 }
405
406 /* Loop over ldb_map_attribute array and add remote_names */
407 static const char **ldb_map_attrs(struct ldb_module *module, const char *const attrs[])
408 {
409         int i;
410         const char **ret;
411         int ar_size = 0, last_element = 0;
412         struct ldb_map_context *privdat = map_get_privdat(module);
413
414         if (attrs == NULL) 
415                 return NULL;
416
417         /* Start with good guess of number of elements */
418         for (i = 0; attrs[i]; i++);
419
420         ret = talloc_array(module, const char *, i);
421         ar_size = i;
422
423         for (i = 0; attrs[i]; i++) {
424                 int j;
425                 const struct ldb_map_attribute *attr = map_find_attr_local(privdat, attrs[i]);
426                 enum ldb_map_attr_type map_type;
427
428                 if (!attr) {
429                         ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Local attribute '%s' does not have a definition!\n", attrs[i]);
430                         map_type = MAP_IGNORE;
431                 } else map_type = attr->type;
432
433                 switch (map_type)
434                 { 
435                         case MAP_IGNORE: break;
436                         case MAP_KEEP: 
437                                 if (last_element >= ar_size) {
438                                         ret = talloc_realloc(module, ret, const char *, ar_size+1);
439                                         ar_size++;
440                                 }
441                                 ret[last_element] = attr->local_name;
442                                 last_element++;
443                                 break;
444
445                         case MAP_RENAME:
446                         case MAP_CONVERT:
447                                 if (last_element >= ar_size) {
448                                         ret = talloc_realloc(module, ret, const char *, ar_size+1);
449                                         ar_size++;
450                                 }
451                                 ret[last_element] = attr->u.rename.remote_name;
452                                 last_element++;
453                                 break;
454
455                         case MAP_GENERATE:
456                                 /* Add remote_names[] for this attribute to the list of 
457                                  * attributes to request from the remote server */
458                                 for (j = 0; attr->u.generate.remote_names[j]; j++) {
459                                         if (last_element >= ar_size) {
460                                                 ret = talloc_realloc(module, ret, const char *, ar_size+1);
461                                                 ar_size++;
462                                         }
463                                         ret[last_element] = attr->u.generate.remote_names[j];                   
464                                         last_element++;
465                                 }
466                                 break;
467                 } 
468         }
469         
470         if (last_element >= ar_size) {
471                 ret = talloc_realloc(module, ret, const char *, ar_size+1);
472                 ar_size++;
473         }
474
475         ret[last_element] = NULL;
476
477         return ret;
478 }
479
480 static const char **available_local_attributes(struct ldb_module *module, const struct ldb_message *msg)
481 {
482         struct ldb_map_context *privdat = map_get_privdat(module);
483         int i, j;
484         int count = 0;
485         const char **ret = talloc_array(module, const char *, 1);
486
487         ret[0] = NULL;
488
489         for (i = 0; privdat->attribute_maps[i].local_name; i++) {
490                 int avail = 0;
491                 const struct ldb_map_attribute *attr = &privdat->attribute_maps[i];
492
493                 /* If all remote attributes for this attribute are present, add the 
494                  * local one to the list */
495                 
496                 switch (attr->type) {
497                 case MAP_IGNORE: break;
498                 case MAP_KEEP: 
499                                 avail = (ldb_msg_find_ldb_val(msg, attr->local_name) != NULL); 
500                                 break;
501                                 
502                 case MAP_RENAME:
503                 case MAP_CONVERT:
504                                 avail = (ldb_msg_find_ldb_val(msg, attr->u.rename.remote_name) != NULL);
505                                 break;
506
507                 case MAP_GENERATE:
508                                 avail = 1;
509                                 for (j = 0; attr->u.generate.remote_names[j]; j++) {
510                                         avail &= (ldb_msg_find_ldb_val(msg, attr->u.generate.remote_names[j]) != NULL);
511                                 }
512                                 break;
513                 }
514
515                 if (!avail)
516                         continue;
517
518                 ret = talloc_realloc(module, ret, const char *, count+2);
519                 ret[count] = attr->local_name;
520                 ret[count+1] = NULL;
521                 count++;
522         }
523
524         return ret;
525 }
526
527 /* Used for search */
528 static struct ldb_message *ldb_map_message_incoming(struct ldb_module *module, const char * const*attrs, const struct ldb_message *mi)
529 {
530         int i, j;
531         struct ldb_message *msg = talloc_zero(module, struct ldb_message);
532         struct ldb_message_element *elm, *oldelm;
533         struct ldb_map_context *privdat = map_get_privdat(module);
534         const char **newattrs = NULL;
535
536         msg->dn = map_remote_dn(module, module, mi->dn);
537
538         ldb_msg_add_string(module->ldb, msg, "mappedFromDn", ldb_dn_linearize(msg, mi->dn));
539
540         /* Loop over attrs, find in ldb_map_attribute array and 
541          * run generate() */
542
543         if (attrs == NULL) {
544                 /* Generate list of the local attributes that /can/ be generated
545                  * using the specific remote attributes */
546
547                 attrs = newattrs = available_local_attributes(module, mi);
548         }
549
550         for (i = 0; attrs[i]; i++) {
551                 const struct ldb_map_attribute *attr = map_find_attr_local(privdat, attrs[i]);
552                 enum ldb_map_attr_type map_type;
553
554                 if (!attr) {
555                         ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Unable to find local attribute '%s' when generating incoming message\n", attrs[i]);
556                         map_type = MAP_IGNORE;
557                 } else map_type = attr->type;
558
559                 switch (map_type) {
560                         case MAP_IGNORE:break;
561                         case MAP_RENAME:
562                                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Renaming remote attribute %s to %s", attr->u.rename.remote_name, attr->local_name);
563                                 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
564                                 if (!oldelm)
565                                         continue;
566
567                                 elm = talloc(msg, struct ldb_message_element);
568                                 elm->name = talloc_strdup(elm, attr->local_name);
569                                 elm->num_values = oldelm->num_values;
570                                 elm->values = talloc_array(elm, struct ldb_val, elm->num_values);
571                                 for (j = 0; j < oldelm->num_values; j++)
572                                         elm->values[j] = ldb_val_dup(elm, &oldelm->values[j]);
573
574                                 ldb_msg_add(module->ldb, msg, elm, oldelm->flags);
575                                 break;
576                                 
577                         case MAP_CONVERT:
578                                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Converting remote attribute %s to %s", attr->u.rename.remote_name, attr->local_name);
579                                 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
580                                 if (!oldelm) 
581                                         continue;
582
583                                 elm = talloc(msg, struct ldb_message_element);
584                                 elm->name = talloc_strdup(elm, attr->local_name);
585                                 elm->num_values = oldelm->num_values;
586                                 elm->values = talloc_array(elm, struct ldb_val, elm->num_values);
587
588                                 for (j = 0; j < oldelm->num_values; j++)
589                                         elm->values[j] = attr->u.convert.convert_remote(module, elm, &oldelm->values[j]);
590
591                                 ldb_msg_add(module->ldb, msg, elm, oldelm->flags);
592                                 break;
593
594                         case MAP_KEEP:
595                                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Keeping remote attribute %s", attr->local_name);
596                                 oldelm = ldb_msg_find_element(mi, attr->local_name);
597                                 if (!oldelm) continue;
598                                 
599                                 elm = talloc(msg, struct ldb_message_element);
600
601                                 elm->num_values = oldelm->num_values;
602                                 elm->values = talloc_array(elm, struct ldb_val, elm->num_values);
603                                 for (j = 0; j < oldelm->num_values; j++)
604                                         elm->values[j] = ldb_val_dup(elm, &oldelm->values[j]);
605
606                                 elm->name = talloc_strdup(elm, oldelm->name);
607
608                                 ldb_msg_add(module->ldb, msg, elm, oldelm->flags);
609                                 break;
610
611                         case MAP_GENERATE:
612                                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Generating local attribute %s", attr->local_name);
613                                 elm = attr->u.generate.generate_local(module, msg, attr->local_name, mi);
614                                 if (!elm) continue;
615
616                                 ldb_msg_add(module->ldb, msg, elm, elm->flags);
617                                 break;
618                         default: 
619                                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Unknown attr->type for %s", attr->local_name);
620                                 break;
621                 }
622         }
623
624         talloc_free(newattrs);
625
626         return msg;
627 }
628
629 /* Used for add, modify */
630 static int ldb_map_message_outgoing(struct ldb_module *module, const struct ldb_message *mo, struct ldb_message **fb, struct ldb_message **mp)
631 {
632         struct ldb_map_context *privdat = map_get_privdat(module);
633         struct ldb_message_element *elm;
634         int i,j;
635
636         *fb = talloc_zero(module, struct ldb_message);
637         (*fb)->dn = talloc_reference(*fb, mo->dn);
638
639         *mp = talloc_zero(module, struct ldb_message);
640         (*mp)->dn = map_local_dn(module, module, mo->dn);
641
642         /* Loop over mi and call generate_remote for each attribute */
643         for (i = 0; i < mo->num_elements; i++) {
644                 const struct ldb_map_attribute *attr = map_find_attr_local(privdat, mo->elements[i].name);
645                 enum ldb_map_attr_type map_type;
646
647                 if (!attr) {
648                         ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Undefined local attribute '%s', ignoring\n", mo->elements[i].name);
649                         map_type = MAP_IGNORE;
650                         continue;
651                 } else map_type = attr->type;
652
653                 switch (map_type) {
654                 case MAP_IGNORE: /* Add to fallback message */
655                         elm = talloc(*fb, struct ldb_message_element);
656
657                         elm->num_values = mo->elements[i].num_values;
658                         elm->values = talloc_reference(elm, mo->elements[i].values);
659                         elm->name = talloc_strdup(elm, mo->elements[i].name);
660                         
661                         ldb_msg_add(module->ldb, *fb, elm, mo->elements[i].flags);      
662                         break;
663                 case MAP_RENAME:
664                         elm = talloc(*mp, struct ldb_message_element);
665
666                         elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
667                         elm->num_values = mo->elements[i].num_values;
668                         elm->values = talloc_reference(elm, mo->elements[i].values);
669
670                         ldb_msg_add(module->ldb, *mp, elm, mo->elements[i].flags);
671                         break;
672
673                 case MAP_CONVERT:
674                         elm = talloc(*mp, struct ldb_message_element);
675
676                         elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
677                         elm->num_values = mo->elements[i].num_values;
678                         elm->values = talloc_array(elm, struct ldb_val, elm->num_values);
679                         
680                         for (j = 0; j < elm->num_values; j++) {
681                                 elm->values[j] = attr->u.convert.convert_local(module, *mp, &mo->elements[i].values[j]);
682                         }
683
684                         ldb_msg_add(module->ldb, *mp, elm, mo->elements[i].flags);
685                         break;
686
687                 case MAP_KEEP:
688                         elm = talloc(*mp, struct ldb_message_element);
689
690                         elm->num_values = mo->elements[i].num_values;
691                         elm->values = talloc_reference(elm, mo->elements[i].values);
692                         elm->name = talloc_strdup(elm, mo->elements[i].name);
693                         
694                         ldb_msg_add(module->ldb, *mp, elm, mo->elements[i].flags);      
695                         break;
696
697                 case MAP_GENERATE:
698                         attr->u.generate.generate_remote(module, attr->local_name, mo, *mp);
699                         break;
700                 } 
701         }
702
703         if ((*fb)->num_elements == 0) {
704                 /* No elements, discard.. */
705                 talloc_free(*fb);
706                 *fb = NULL;
707         } else {
708                 ldb_msg_add_string(module->ldb, *fb, "isMapped", "TRUE");
709         }
710
711         if ((*mp)->num_elements == 0) {
712                 /* No elements, discard.. */
713                 talloc_free(*mp);
714                 *mp = NULL;
715         }
716
717         return 0;
718 }
719
720
721 /*
722   rename a record
723 */
724 static int map_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
725 {
726         struct ldb_map_context *privdat = map_get_privdat(module);
727         struct ldb_dn *n_olddn, *n_newdn;
728         int ret;
729
730         ret = ldb_next_rename_record(module, olddn, newdn);
731         
732         n_olddn = map_local_dn(module, module, olddn);
733         n_newdn = map_local_dn(module, module, newdn);
734
735         ret = ldb_rename(privdat->mapped_ldb, n_olddn, n_newdn);
736
737         talloc_free(n_olddn);
738         talloc_free(n_newdn);
739         
740         return ret;
741 }
742
743 /*
744   delete a record
745 */
746 static int map_delete(struct ldb_module *module, const struct ldb_dn *dn)
747 {
748         struct ldb_map_context *privdat = map_get_privdat(module);
749         struct ldb_dn *newdn;
750         int ret;
751
752         ret = ldb_next_delete_record(module, dn);
753         
754         newdn = map_local_dn(module, module, dn);
755
756         ret = ldb_delete(privdat->mapped_ldb, newdn);
757
758         talloc_free(newdn);
759
760         return ret;
761 }
762
763 /* search fallback database */
764 static int map_search_bytree_fb(struct ldb_module *module, const struct ldb_dn *base,
765                               enum ldb_scope scope, struct ldb_parse_tree *tree,
766                               const char * const *attrs, struct ldb_message ***res)
767 {
768         int ret;
769         struct ldb_parse_tree t_and, t_not, t_present, *childs[2];
770
771         t_present.operation = LDB_OP_PRESENT;
772         t_present.u.present.attr = talloc_strdup(NULL, "isMapped");
773
774         t_not.operation = LDB_OP_NOT;
775         t_not.u.isnot.child = &t_present;
776
777         childs[0] = &t_not;
778         childs[1] = tree;
779         t_and.operation = LDB_OP_AND;
780         t_and.u.list.num_elements = 2;
781         t_and.u.list.elements = childs;
782         
783         ret = ldb_next_search_bytree(module, base, scope, &t_and, attrs, res);
784
785         talloc_free(t_present.u.present.attr);
786
787         return ret;
788 }
789
790 /* Search in the database against which we are mapping */
791 static int map_search_bytree_mp(struct ldb_module *module, const struct ldb_dn *base,
792                               enum ldb_scope scope, struct ldb_parse_tree *tree,
793                               const char * const *attrs, struct ldb_message ***res)
794 {
795         struct ldb_parse_tree *new_tree;
796         struct ldb_dn *new_base;
797         struct ldb_message **newres;
798         const char **newattrs;
799         int mpret, ret;
800         struct ldb_map_context *privdat = map_get_privdat(module);
801         int i;
802
803         /*- search mapped database */
804
805         new_tree = ldb_map_parse_tree(module, module, tree);
806         if (new_tree == NULL) {
807                 /* All attributes used in the parse tree are 
808                  * local, apparently. Fall back to enumerating the complete remote 
809                  * database... Rather a slow search then no results. */
810                 new_tree = talloc_zero(module, struct ldb_parse_tree);
811                 new_tree->operation = LDB_OP_PRESENT;
812                 new_tree->u.present.attr = talloc_strdup(new_tree, "dn");
813                 return 0;
814         }
815                 
816         newattrs = ldb_map_attrs(module, attrs); 
817         new_base = map_local_dn(module, module, base);
818
819         mpret = ldb_search_bytree(privdat->mapped_ldb, new_base, scope, new_tree, newattrs, &newres);
820
821         talloc_free(new_base);
822         talloc_free(new_tree);
823         talloc_free(newattrs);
824
825         if (mpret == -1) {
826                 struct map_private *map_private = module->private_data;
827                 map_private->last_err_string = ldb_errstring(privdat->mapped_ldb);
828                 return -1;
829         }
830
831         /*
832          - per returned record, search fallback database for additional data (by dn)
833          - test if (full expression) is now true
834         */
835
836         *res = talloc_array(module, struct ldb_message *, mpret);
837
838         ret = 0;
839
840         for (i = 0; i < mpret; i++) {
841                 struct ldb_message *merged;
842                 struct ldb_message **extrares = NULL;
843                 int extraret;
844
845                 /* Always get special DN's from the fallback database */
846                 if (ldb_dn_is_special(newres[i]->dn))
847                         continue;
848
849                 merged = ldb_map_message_incoming(module, attrs, newres[i]);
850                 
851                 /* Merge with additional data from local database */
852                 extraret = ldb_next_search(module, merged->dn, LDB_SCOPE_BASE, "", NULL, &extrares);
853
854                 if (extraret == -1) {
855                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "Error searching for extra data!\n");
856                 } else if (extraret > 1) {
857                         ldb_debug(module->ldb, LDB_DEBUG_ERROR, "More than one result for extra data!\n");
858                         talloc_free(newres);
859                         return -1;
860                 } else if (extraret == 0) {
861                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "No extra data found for remote DN: %s", ldb_dn_linearize(merged, merged->dn));
862                 }
863                 
864                 if (extraret == 1) {
865                         int j;
866                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Extra data found for remote DN: %s", ldb_dn_linearize(merged, merged->dn));
867                         for (j = 0; j < extrares[0]->num_elements; j++) {
868                                 ldb_msg_add(module->ldb, merged, &(extrares[0]->elements[j]), extrares[0]->elements[j].flags);
869                         }
870
871                         ldb_msg_add_string(module->ldb, merged, "extraMapped", "TRUE");
872                 } else {
873                         ldb_msg_add_string(module->ldb, merged, "extraMapped", "FALSE");
874                 }
875                 
876                 if (ldb_match_msg(module->ldb, merged, tree, base, scope) != 0) {
877                         (*res)[ret] = merged;
878                         ret++;
879                 } else {
880                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Discarded merged message because it did not match");
881                 }
882         }
883
884         talloc_free(newres);
885
886         return ret;
887 }
888
889
890 /*
891   search for matching records using a ldb_parse_tree
892 */
893 static int map_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
894                               enum ldb_scope scope, struct ldb_parse_tree *tree,
895                               const char * const *attrs, struct ldb_message ***res)
896 {
897         struct ldb_message **fbres, **mpres;
898         int i;
899         int ret_fb, ret_mp;
900
901         ret_fb = map_search_bytree_fb(module, base, scope, tree, attrs, &fbres);
902         if (ret_fb == -1) 
903                 return -1;
904
905         /* special dn's are never mapped.. */
906         if (ldb_dn_is_special(base)) {
907                 *res = fbres;
908                 return ret_fb;
909         }
910
911         ret_mp = map_search_bytree_mp(module, base, scope, tree, attrs, &mpres);
912         if (ret_mp == -1) {
913                 return -1;
914         }
915
916         /* Merge results */
917         *res = talloc_array(module, struct ldb_message *, ret_fb + ret_mp);
918
919         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Merging %d mapped and %d fallback messages", ret_mp, ret_fb);
920
921         for (i = 0; i < ret_fb; i++) (*res)[i] = fbres[i];
922         for (i = 0; i < ret_mp; i++) (*res)[ret_fb+i] = mpres[i];
923
924         return ret_fb + ret_mp;
925 }
926 /*
927   search for matching records
928 */
929 static int map_search(struct ldb_module *module, const struct ldb_dn *base,
930                        enum ldb_scope scope, const char *expression,
931                        const char * const *attrs, struct ldb_message ***res)
932 {
933         struct map_private *map = module->private_data;
934         struct ldb_parse_tree *tree;
935         int ret;
936
937         tree = ldb_parse_tree(NULL, expression);
938         if (tree == NULL) {
939                 map->last_err_string = "expression parse failed";
940                 return -1;
941         }
942
943         ret = map_search_bytree(module, base, scope, tree, attrs, res);
944         talloc_free(tree);
945         return ret;
946 }
947
948 /*
949   add a record
950 */
951 static int map_add(struct ldb_module *module, const struct ldb_message *msg)
952 {
953         int ret;
954         struct ldb_map_context *privdat = map_get_privdat(module);
955         struct ldb_message *fb, *mp;
956
957         if (!map_is_mappable(privdat, msg)) {
958                 return ldb_next_add_record(module, msg);
959         }
960
961         if (ldb_map_message_outgoing(module, msg, &fb, &mp) == -1)
962                 return -1;
963
964         if (fb != NULL) {
965                 ret = ldb_next_add_record(module, fb);
966                 if (ret == -1) {
967                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Adding fallback record failed");
968                         return -1;
969                 }
970         }
971
972         talloc_free(fb);
973                 
974         if (mp != NULL) {
975                 ret = ldb_add(privdat->mapped_ldb, mp);
976                 if (ret == -1) {
977                         ldb_debug(module->ldb, LDB_DEBUG_TRACE, "Adding mapped record failed");
978                         return -1;
979                 }
980         }
981
982         talloc_free(mp);
983
984         return ret;
985 }
986
987
988 /*
989   modify a record
990 */
991 static int map_modify(struct ldb_module *module, const struct ldb_message *msg)
992 {
993         struct ldb_map_context *privdat = map_get_privdat(module);
994         struct ldb_message *fb, *mp;
995         int ret;
996
997         if (!map_is_mappable(privdat, msg))
998                 return ldb_next_modify_record(module, msg);
999                 
1000
1001         if (ldb_map_message_outgoing(module, msg, &fb, &mp) == -1)
1002                 return -1;
1003
1004         if (fb != NULL) {
1005                 ret = ldb_next_modify_record(module, fb);
1006                 talloc_free(fb);
1007         }
1008
1009         if (mp != NULL) {
1010                 ret = ldb_modify(privdat->mapped_ldb, mp);
1011                 talloc_free(mp);
1012         }
1013
1014         return ret;
1015 }
1016
1017 static int map_lock(struct ldb_module *module, const char *lockname)
1018 {
1019         return ldb_next_named_lock(module, lockname);
1020 }
1021
1022 static int map_unlock(struct ldb_module *module, const char *lockname)
1023 {
1024         return ldb_next_named_unlock(module, lockname);
1025 }
1026
1027 /*
1028   return extended error information
1029 */
1030 static const char *map_errstring(struct ldb_module *module)
1031 {
1032         struct map_private *map = module->private_data;
1033         
1034         if (map->last_err_string)
1035                 return map->last_err_string;
1036
1037         return ldb_next_errstring(module);
1038 }
1039
1040 static const struct ldb_module_ops map_ops = {
1041         .name          = "map",
1042         .search        = map_search,
1043         .search_bytree = map_search_bytree,
1044         .add_record    = map_add,
1045         .modify_record = map_modify,
1046         .delete_record = map_delete,
1047         .rename_record = map_rename,
1048         .named_lock    = map_lock,
1049         .named_unlock  = map_unlock,
1050         .errstring     = map_errstring
1051 };
1052
1053 static char *map_find_url(struct ldb_context *ldb, const char *name)
1054 {
1055         const char * const attrs[] = { "@MAP_URL" , NULL};
1056         struct ldb_message **msg = NULL;
1057         struct ldb_dn *mods;
1058         char *url;
1059         int ret;
1060
1061         mods = ldb_dn_string_compose(ldb, NULL, "@MAP=%s", name);
1062         if (mods == NULL) {
1063                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Can't construct DN");
1064                 return NULL;
1065         }
1066
1067         ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &msg);
1068         talloc_free(mods);
1069         if (ret < 1) {
1070                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Not enough results found looking for @MAP");
1071                 return NULL;
1072         }
1073
1074         url = talloc_strdup(ldb, ldb_msg_find_string(msg[0], "@MAP_URL", NULL));
1075
1076         talloc_free(msg);
1077
1078         return url;
1079 }
1080
1081 /* the init function */
1082 struct ldb_module *ldb_map_init(struct ldb_context *ldb, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls, const char *name)
1083 {
1084         int i, j;
1085         struct ldb_module *ctx;
1086         struct map_private *data;
1087         char *url;
1088
1089         ctx = talloc(ldb, struct ldb_module);
1090         if (!ctx)
1091                 return NULL;
1092
1093         data = talloc(ctx, struct map_private);
1094         if (!data) {
1095                 talloc_free(ctx);
1096                 return NULL;
1097         }
1098
1099         data->context.mapped_ldb = ldb_init(data);
1100         ldb_set_debug(data->context.mapped_ldb, ldb->debug_ops.debug, ldb->debug_ops.context);
1101         url = map_find_url(ldb, name);
1102
1103         if (!url) {
1104                 ldb_debug(ldb, LDB_DEBUG_FATAL, "@MAP=%s not set!\n", name);
1105                 return NULL;
1106         }
1107
1108         if (ldb_connect(data->context.mapped_ldb, url, 0, NULL) != 0) {
1109                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to open mapped database for %s at '%s'\n", name, url);
1110                 return NULL;
1111         }
1112
1113         talloc_free(url);
1114
1115         data->last_err_string = NULL;
1116
1117         /* Get list of attribute maps */
1118         j = 0;
1119         data->context.attribute_maps = NULL;
1120
1121         for (i = 0; attrs[i].local_name; i++) {
1122                 data->context.attribute_maps = talloc_realloc(data, data->context.attribute_maps, struct ldb_map_attribute, j+1);
1123                 data->context.attribute_maps[j] = attrs[i];
1124                 j++;
1125         }
1126
1127         for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1128                 data->context.attribute_maps = talloc_realloc(data, data->context.attribute_maps, struct ldb_map_attribute, j+1);
1129                 data->context.attribute_maps[j] = builtin_attribute_maps[i];
1130                 j++;
1131         }
1132
1133         data->context.attribute_maps = talloc_realloc(data, data->context.attribute_maps, struct ldb_map_attribute, j+1);
1134         memset(&data->context.attribute_maps[j], 0, sizeof(struct ldb_map_attribute));
1135
1136         data->context.objectclass_maps = ocls;
1137         ctx->private_data = data;
1138         ctx->ldb = ldb;
1139         ctx->prev = ctx->next = NULL;
1140         ctx->ops = &map_ops;
1141
1142         return ctx;
1143 }
1144
1145 static struct ldb_val map_convert_local_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
1146 {
1147         struct ldb_dn *dn, *newdn;;
1148         struct ldb_val *newval;
1149
1150         dn = ldb_dn_explode(ctx, (char *)val->data);
1151
1152         newdn = map_local_dn(module, ctx, dn);
1153
1154         talloc_free(dn);
1155
1156         newval = talloc(ctx, struct ldb_val);
1157         newval->data = (uint8_t *)ldb_dn_linearize(ctx, newdn);
1158         newval->length = strlen((char *)newval->data);
1159
1160         talloc_free(newdn);
1161
1162         return *newval;
1163 }
1164
1165 static struct ldb_val map_convert_remote_dn(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
1166 {
1167         struct ldb_dn *dn, *newdn;;
1168         struct ldb_val *newval;
1169
1170         dn = ldb_dn_explode(ctx, (char *)val->data);
1171
1172         newdn = map_remote_dn(module, ctx, dn);
1173
1174         talloc_free(dn);
1175
1176         newval = talloc(ctx, struct ldb_val);
1177         newval->data = (uint8_t *)ldb_dn_linearize(ctx, newdn);
1178         newval->length = strlen((char *)newval->data);
1179
1180         talloc_free(newdn);
1181
1182         return *newval;
1183 }
1184
1185 static struct ldb_val map_convert_local_objectclass(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
1186 {
1187         int i;
1188         struct ldb_map_context *map = module->private_data;
1189
1190         for (i = 0; map->objectclass_maps[i].local_name; i++) {
1191                 if (!strcmp(map->objectclass_maps[i].local_name, (char *)val->data)) {
1192                         struct ldb_val newval;
1193                         newval.data = (uint8_t*)talloc_strdup(ctx, map->objectclass_maps[i].remote_name);
1194                         newval.length = strlen((char *)newval.data);
1195
1196                         return ldb_val_dup(ctx, &newval);
1197                 }
1198         }
1199
1200         return ldb_val_dup(ctx, val); 
1201 }
1202
1203 static struct ldb_val map_convert_remote_objectclass(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_val *val)
1204 {
1205         int i;
1206         struct ldb_map_context *map = module->private_data;
1207
1208         for (i = 0; map->objectclass_maps[i].remote_name; i++) {
1209                 if (!strcmp(map->objectclass_maps[i].remote_name, (char *)val->data)) {
1210                         struct ldb_val newval;
1211                         newval.data = (uint8_t*)talloc_strdup(ctx, map->objectclass_maps[i].local_name);
1212                         newval.length = strlen((char *)newval.data);
1213
1214                         return ldb_val_dup(ctx, &newval);
1215                 }
1216         }
1217
1218         return ldb_val_dup(ctx, val); 
1219 }
1220