r9686: Another round of bugfixes. Works somewhat now, when running under valgrind ;-)
[mimir/samba.git] / source4 / lib / ldb / ldb_map / 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 "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "lib/ldb/ldb_map/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  */
35
36 struct map_private {
37         const struct ldb_map_attribute *attribute_maps;
38         const struct ldb_map_objectclass *objectclass_maps;
39         const char *last_err_string;
40 };
41
42 /* find an attribute by the local name */
43 static const struct ldb_map_attribute *map_find_attr_local(struct ldb_module *module, const char *attr)
44 {
45         struct map_private *privdat = module->private_data;
46         int i;
47         for (i = 0; privdat->attribute_maps[i].local_name; i++) {
48                 if (!strcmp(privdat->attribute_maps[i].local_name, attr)) 
49                         return &privdat->attribute_maps[i];
50         }
51
52         return NULL;
53 }
54
55 /* find an attribute by the remote name */
56 static const struct ldb_map_attribute *map_find_attr_remote(struct ldb_module *module, const char *attr)
57 {
58         struct map_private *privdat = module->private_data;
59         int i;
60         for (i = 0; privdat->attribute_maps[i].local_name; i++) {
61                 if (privdat->attribute_maps[i].type != MAP_RENAME &&
62                         privdat->attribute_maps[i].type != MAP_CONVERT) 
63                         continue;
64
65                 if (!strcmp(privdat->attribute_maps[i].u.rename.remote_name, attr)) 
66                         return &privdat->attribute_maps[i];
67         }
68
69         return NULL;
70 }
71
72 static struct ldb_parse_tree *ldb_map_parse_tree(struct ldb_module *module, const struct ldb_parse_tree *tree)
73 {
74         int i;
75         const struct ldb_map_attribute *attr;
76         struct ldb_parse_tree *new_tree;
77
78         if (tree == NULL)
79                 return NULL;
80         
81         new_tree = talloc_memdup(module, tree, sizeof(*tree));
82
83         /* Find attr in question and:
84          *  - if it has a convert_operator function, run that
85          *  - otherwise, replace attr name with required[0] */
86
87         if (tree->operation == LDB_OP_AND || 
88                 tree->operation == LDB_OP_OR) {
89                 for (i = 0; i < tree->u.list.num_elements; i++) {
90                         new_tree->u.list.elements[i] = ldb_map_parse_tree(module, tree->u.list.elements[i]);
91                 }
92
93                 return new_tree;
94         }
95                 
96         if (tree->operation == LDB_OP_NOT) {
97                 new_tree->u.isnot.child = ldb_map_parse_tree(module, tree->u.isnot.child);
98                 return new_tree;
99         }
100
101         /* tree->operation is LDB_OP_EQUALITY, LDB_OP_SUBSTRING, LDB_OP_GREATER,
102          * LDB_OP_LESS, LDB_OP_APPROX, LDB_OP_PRESENT or LDB_OP_EXTENDED
103          *
104          * (all have attr as the first element)
105          */
106
107         attr = map_find_attr_local(module, tree->u.equality.attr);
108
109         if (!attr) {
110                 DEBUG(0, ("Unable to find local attribute '%s', leaving as is\n", tree->u.equality.attr));
111                 return new_tree;
112         }
113
114         if (attr->type == MAP_IGNORE)
115                 return NULL;
116
117         if (attr->convert_operator) {
118                 /* Run convert_operator */
119                 talloc_free(new_tree);
120                 new_tree = attr->convert_operator(module, tree);
121         } else {
122                 new_tree->u.equality.attr = talloc_strdup(new_tree, attr->u.rename.remote_name);
123         }
124
125         return new_tree;
126 }
127
128 /* Remote DN -> Local DN */
129 static struct ldb_dn *map_remote_dn(struct ldb_module *module, const struct ldb_dn *dn)
130 {
131         struct ldb_dn *newdn;
132         int i;
133
134         if (dn == NULL)
135                 return NULL;
136
137         newdn = talloc_memdup(module, dn, sizeof(*dn));
138         if (!newdn) 
139                 return NULL;
140
141         newdn->components = talloc_memdup(newdn, dn->components, sizeof(struct ldb_dn_component) * newdn->comp_num); 
142
143         if (!newdn->components)
144                 return NULL;
145
146         /* For each rdn, map the attribute name and possibly the 
147          * complete rdn */
148         
149         for (i = 0; i < dn->comp_num; i++) {
150                 const struct ldb_map_attribute *attr = map_find_attr_remote(module, dn->components[i].name);
151
152                 /* Unknown attribute - leave this dn as is and hope the best... */
153                 if (!attr)
154                         continue;
155
156                 if (attr->type == MAP_IGNORE) {
157                         DEBUG(0, ("Local MAP_IGNORE attribute '%s' used in DN!", dn->components[i].name));
158                         talloc_free(newdn);
159                         return NULL;
160                 }
161
162                 if (attr->type == MAP_GENERATE) {
163                         DEBUG(0, ("Local MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
164                         talloc_free(newdn);
165
166                         return NULL;
167                 }
168
169                 if (attr->type == MAP_CONVERT) {
170                         struct ldb_message_element elm, *newelm;
171                         struct ldb_val vals[1] = { dn->components[i].value };
172                         elm.flags = 0;
173                         elm.name = attr->u.convert.remote_name;
174                         elm.num_values = 1;
175                         elm.values = vals;
176
177                         newelm = attr->u.convert.convert_remote(module, attr->local_name, &elm);
178
179                         newdn->components[i].name = talloc_strdup(module, newelm->name);
180                         newdn->components[i].value = newelm->values[0];
181                 } else if (attr->type == MAP_RENAME) {
182                         newdn->components[i].name = talloc_strdup(module, attr->local_name);
183                 }
184         }
185         return newdn;
186 }
187
188 /* Local DN -> Remote DN */
189 static struct ldb_dn *map_local_dn(struct ldb_module *module, const struct ldb_dn *dn)
190 {       struct ldb_dn *newdn;
191         int i;
192         struct ldb_parse_tree eqtree, *new_eqtree;
193
194         if (dn == NULL)
195                 return NULL;
196
197         newdn = talloc_memdup(module, dn, sizeof(*dn));
198         if (!newdn) 
199                 return NULL;
200
201         newdn->components = talloc_memdup(newdn, dn->components, sizeof(struct ldb_dn_component) * newdn->comp_num); 
202
203         if (!newdn->components)
204                 return NULL;
205
206         /* For each rdn, map the attribute name and possibly the 
207          * complete rdn using an equality convert_operator call */
208         
209         for (i = 0; i < dn->comp_num; i++) {
210                 const struct ldb_map_attribute *attr = map_find_attr_local(module, dn->components[i].name);
211
212                 /* Unknown attribute - leave this dn as is and hope the best... */
213                 if (!attr)
214                         continue;
215
216                 if (attr->type == MAP_IGNORE) {
217                         DEBUG(0, ("Local MAP_IGNORE attribute '%s' used in DN!", dn->components[i].name));
218                         talloc_free(newdn);
219                         return NULL;
220                 }
221
222                 if (attr->type == MAP_GENERATE) {
223                         DEBUG(0, ("Local MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
224                         talloc_free(newdn);
225
226                         return NULL;
227                 }
228
229                 /* Simple rename/convert only */
230                 if (attr->convert_operator) {
231                         /* Fancy stuff */
232                         eqtree.operation = LDB_OP_EQUALITY;
233                         eqtree.u.equality.attr = dn->components[i].name;
234                         eqtree.u.equality.value = dn->components[i].value;
235
236                         new_eqtree = ldb_map_parse_tree(module, &eqtree);
237
238                         /* Silently continue for now */
239                         if (!new_eqtree) {
240                                 DEBUG(0, ("Unable to convert RDN for attribute %s\n", dn->components[i].name));
241                                 continue;
242                         }
243
244                         newdn->components[i].name = new_eqtree->u.equality.attr;
245                         newdn->components[i].value = new_eqtree->u.equality.value;
246                 } else if (attr->type == MAP_CONVERT) {
247                         struct ldb_message_element elm, *newelm;
248                         struct ldb_val vals[1] = { dn->components[i].value };
249                         elm.flags = 0;
250                         elm.name = attr->local_name;
251                         elm.num_values = 1;
252                         elm.values = vals;
253
254                         newelm = attr->u.convert.convert_local(module, attr->u.convert.remote_name, &elm);
255
256                         newdn->components[i].name = talloc_strdup(module, newelm->name);
257                         newdn->components[i].value = newelm->values[0];
258                 } else if (attr->type == MAP_RENAME) {
259                         newdn->components[i].name = talloc_strdup(module, attr->u.rename.remote_name);
260                 }
261         }
262
263         return newdn;
264 }
265
266 /* Loop over ldb_map_attribute array and add remote_names */
267 static const char **ldb_map_attrs(struct ldb_module *module, const char *const attrs[])
268 {
269         int i;
270         const char **ret;
271         int ar_size = 0, last_element = 0;
272
273         if (attrs == NULL) 
274                 return NULL;
275
276         /* Start with good guess of number of elements */
277         for (i = 0; attrs[i]; i++);
278
279         ret = talloc_array(module, const char *, i);
280         ar_size = i;
281
282         for (i = 0; attrs[i]; i++) {
283                 int j;
284                 const struct ldb_map_attribute *attr = map_find_attr_local(module, attrs[i]);
285
286                 if (!attr) {
287                         DEBUG(0, ("Local attribute '%s' does not have a definition!\n", attrs[i]));
288                         continue;
289                 }
290
291                 switch (attr->type)
292                 { 
293                         case MAP_IGNORE: break;
294                         case MAP_KEEP: 
295                                 if (last_element >= ar_size) {
296                                         ret = talloc_realloc(module, ret, const char *, ar_size+1);
297                                         ar_size++;
298                                 }
299                                 ret[last_element] = attr->local_name;
300                                 last_element++;
301                                 break;
302
303                         case MAP_RENAME:
304                         case MAP_CONVERT:
305                                 if (last_element >= ar_size) {
306                                         ret = talloc_realloc(module, ret, const char *, ar_size+1);
307                                         ar_size++;
308                                 }
309                                 ret[last_element] = attr->u.rename.remote_name;
310                                 last_element++;
311                                 break;
312
313                         case MAP_GENERATE:
314                                 /* Add remote_names[] for this attribute to the list of 
315                                  * attributes to request from the remote server */
316                                 for (j = 0; attr->u.generate.remote_names[j]; j++) {
317                                         if (last_element >= ar_size) {
318                                                 ret = talloc_realloc(module, ret, const char *, ar_size+1);
319                                                 ar_size++;
320                                         }
321                                         ret[last_element] = attr->u.generate.remote_names[j];                   
322                                         last_element++;
323                                 }
324                                 break;
325                 } 
326         }
327
328         return NULL;
329 }
330
331 static const char **available_local_attributes(struct ldb_module *module, const struct ldb_message *msg)
332 {
333         struct map_private *map = module->private_data;
334         int i, j;
335         int count = 0;
336         const char **ret = talloc_array(module, const char *, 1);
337
338         ret[0] = NULL;
339
340         for (i = 0; map->attribute_maps[i].local_name; i++) {
341                 BOOL avail = False;
342                 const struct ldb_map_attribute *attr = &map->attribute_maps[i];
343
344                 /* If all remote attributes for this attribute are present, add the 
345                  * local one to the list */
346                 
347                 switch (attr->type) {
348                 case MAP_IGNORE: break;
349                 case MAP_KEEP: 
350                                 avail = (ldb_msg_find_ldb_val(msg, attr->local_name) != NULL); 
351                                 break;
352                                 
353                 case MAP_RENAME:
354                 case MAP_CONVERT:
355                                 avail = (ldb_msg_find_ldb_val(msg, attr->u.rename.remote_name) != NULL);
356                                 break;
357
358                 case MAP_GENERATE:
359                                 avail = True;
360                                 for (j = 0; attr->u.generate.remote_names[j]; j++) {
361                                         avail &= (ldb_msg_find_ldb_val(msg, attr->u.generate.remote_names[j]) != NULL);
362                                 }
363                                 break;
364                 }
365
366                 if (!avail)
367                         continue;
368
369                 ret = talloc_realloc(module, ret, const char *, count+2);
370                 ret[count] = attr->local_name;
371                 ret[count+1] = NULL;
372                 count++;
373         }
374
375         return ret;
376 }
377
378 static struct ldb_message *ldb_map_message_incoming(struct ldb_module *module, const char * const*attrs, const struct ldb_message *mi)
379 {
380         int i;
381         struct ldb_message *msg = talloc_zero(module, struct ldb_message);
382         struct ldb_message_element *elm, *oldelm;
383         const char **newattrs = NULL;
384
385         msg->dn = map_remote_dn(module, mi->dn);
386
387         /* Loop over attrs, find in ldb_map_attribute array and 
388          * run generate() */
389
390         if (attrs == NULL) {
391                 /* Generate list of the local attributes that /can/ be generated
392                  * using the specific remote attributes */
393
394                 attrs = newattrs = available_local_attributes(module, mi);
395         }
396
397         for (i = 0; attrs[i]; i++) {
398                 const struct ldb_map_attribute *attr = map_find_attr_local(module, attrs[i]);
399
400                 if (!attr) {
401                         DEBUG(0, ("Unable to find local attribute '%s' when generating incoming message\n", attrs[i]));
402                         continue;
403                 }
404
405                 switch (attr->type) {
406                         case MAP_IGNORE:break;
407                         case MAP_RENAME:
408                                 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
409                                 elm = talloc_memdup(msg, oldelm, sizeof(*oldelm));
410                                 elm->name = talloc_strdup(elm, attr->local_name);
411
412                                 ldb_msg_add(module->ldb, msg, elm, 0);
413                                 break;
414                                 
415                         case MAP_CONVERT:
416                                 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
417                                 elm = attr->u.convert.convert_local(msg, attr->local_name, oldelm);
418
419                                 ldb_msg_add(module->ldb, msg, elm, 0);
420                                 break;
421
422                         case MAP_KEEP:
423                                 ldb_msg_add(module->ldb, msg, ldb_msg_find_element(mi, attr->local_name), 0);
424                                 break;
425
426                         case MAP_GENERATE:
427                                 elm = attr->u.generate.generate_local(msg, attr->local_name, mi);
428                                 ldb_msg_add(module->ldb, msg, elm, 0);
429                                 break;
430                         default: 
431                                 DEBUG(0, ("Unknown attr->type for %s", attr->local_name));
432                                 break;
433                 }
434         }
435
436         talloc_free(newattrs);
437
438         return msg;
439 }
440
441 static struct ldb_message *ldb_map_message_outgoing(struct ldb_module *module, const struct ldb_message *mo)
442 {
443         struct ldb_message *msg = talloc_zero(module, struct ldb_message);
444         struct ldb_message_element *elm;
445         int i;
446         
447         msg->private_data = mo->private_data;
448         
449         msg->dn = map_local_dn(module, mo->dn);
450
451         /* Loop over mi and call generate_remote for each attribute */
452         for (i = 0; i < mo->num_elements; i++) {
453                 const struct ldb_map_attribute *attr = map_find_attr_local(module, mo->elements[i].name);
454
455                 if (!attr) {
456                         DEBUG(0, ("Undefined local attribute '%s', ignoring\n", mo->elements[i].name));
457                         continue;
458                 }
459
460                 switch (attr->type) {
461                 case MAP_IGNORE: break;
462                 case MAP_RENAME:
463                         elm = talloc_memdup(msg, &msg->elements[i], sizeof(*elm));
464                         elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
465
466                         ldb_msg_add(module->ldb, msg, elm, 0);
467                         break;
468
469                 case MAP_CONVERT:
470                         elm = attr->u.convert.convert_remote(msg, attr->local_name, &msg->elements[i]);
471                         ldb_msg_add(module->ldb, msg, elm, 0);
472                         break;
473
474                 case MAP_KEEP:
475                         ldb_msg_add(module->ldb, msg, &msg->elements[i], 0);    
476                         break;
477
478                 case MAP_GENERATE:
479                         attr->u.generate.generate_remote(attr->local_name, mo, msg);
480                         break;
481                 } 
482         }
483
484         return msg;
485 }
486
487 /*
488   rename a record
489 */
490 static int map_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
491 {
492         struct ldb_dn *n_olddn, *n_newdn;
493         int ret;
494         
495         n_olddn = map_local_dn(module, olddn);
496         n_newdn = map_local_dn(module, newdn);
497
498         ret = ldb_next_rename_record(module, n_olddn, n_newdn);
499
500         talloc_free(n_olddn);
501         talloc_free(n_newdn);
502         
503         return ret;
504 }
505
506 /*
507   delete a record
508 */
509 static int map_delete(struct ldb_module *module, const struct ldb_dn *dn)
510 {
511         struct ldb_dn *newdn;
512         int ret;
513
514         newdn = map_local_dn(module, dn);
515
516         ret = ldb_next_delete_record(module, newdn);
517
518         talloc_free(newdn);
519
520         return ret;
521 }
522
523 /*
524   search for matching records using a ldb_parse_tree
525 */
526 static int map_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
527                               enum ldb_scope scope, struct ldb_parse_tree *tree,
528                               const char * const *attrs, struct ldb_message ***res)
529 {
530         int ret;
531         const char **newattrs;
532         struct ldb_parse_tree *new_tree;
533         struct ldb_dn *new_base;
534         struct ldb_message **newres;
535         int i;
536
537         new_tree = ldb_map_parse_tree(module, tree);
538         newattrs = ldb_map_attrs(module, attrs); 
539         new_base = map_local_dn(module, base);
540
541         ret = ldb_next_search_bytree(module, new_base, scope, new_tree, newattrs, &newres);
542
543         talloc_free(new_base);
544         talloc_free(new_tree);
545         talloc_free(newattrs);
546
547         *res = talloc_array(module, struct ldb_message *, ret);
548
549         for (i = 0; i < ret; i++) {
550                 (*res)[i] = ldb_map_message_incoming(module, attrs, newres[i]);
551                 talloc_free(newres[i]);
552         }
553
554         return ret;
555 }
556 /*
557   search for matching records
558 */
559 static int map_search(struct ldb_module *module, const struct ldb_dn *base,
560                        enum ldb_scope scope, const char *expression,
561                        const char * const *attrs, struct ldb_message ***res)
562 {
563         struct map_private *map = module->private_data;
564         struct ldb_parse_tree *tree;
565         int ret;
566
567         tree = ldb_parse_tree(NULL, expression);
568         if (tree == NULL) {
569                 map->last_err_string = "expression parse failed";
570                 return -1;
571         }
572
573         ret = map_search_bytree(module, base, scope, tree, attrs, res);
574         talloc_free(tree);
575         return ret;
576 }
577
578 /*
579   add a record
580 */
581 static int map_add(struct ldb_module *module, const struct ldb_message *msg)
582 {
583         struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
584         int ret;
585
586         ret = ldb_next_add_record(module, nmsg);
587
588         talloc_free(nmsg);
589
590         return ret;
591 }
592
593
594
595
596 /*
597   modify a record
598 */
599 static int map_modify(struct ldb_module *module, const struct ldb_message *msg)
600 {
601         struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
602         int ret;
603
604         ret = ldb_next_modify_record(module, nmsg);
605
606         talloc_free(nmsg);
607
608         return ret;
609 }
610
611 static int map_lock(struct ldb_module *module, const char *lockname)
612 {
613         return ldb_next_named_lock(module, lockname);
614 }
615
616 static int map_unlock(struct ldb_module *module, const char *lockname)
617 {
618         return ldb_next_named_unlock(module, lockname);
619 }
620
621 /*
622   return extended error information
623 */
624 static const char *map_errstring(struct ldb_module *module)
625 {
626         struct map_private *map = module->private_data;
627         
628         if (map->last_err_string)
629                 return map->last_err_string;
630
631         return ldb_next_errstring(module);
632 }
633
634 static const struct ldb_module_ops map_ops = {
635         .name          = "map",
636         .search        = map_search,
637         .search_bytree = map_search_bytree,
638         .add_record    = map_add,
639         .modify_record = map_modify,
640         .delete_record = map_delete,
641         .rename_record = map_rename,
642         .named_lock    = map_lock,
643         .named_unlock  = map_unlock,
644         .errstring     = map_errstring
645 };
646
647 /* the init function */
648 struct ldb_module *ldb_map_init(struct ldb_context *ldb, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls, const char *options[])
649 {
650         struct ldb_module *ctx;
651         struct map_private *data;
652
653         ctx = talloc(ldb, struct ldb_module);
654         if (!ctx)
655                 return NULL;
656
657         data = talloc(ctx, struct map_private);
658         if (!data) {
659                 talloc_free(ctx);
660                 return NULL;
661         }
662
663         data->attribute_maps = attrs;
664         data->objectclass_maps = ocls;
665         ctx->private_data = data;
666         ctx->ldb = ldb;
667         ctx->prev = ctx->next = NULL;
668         ctx->ops = &map_ops;
669
670         return ctx;
671 }