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