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