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