2 ldb database library - map backend
4 Copyright (C) Jelmer Vernooij 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "lib/ldb/ldb_map/ldb_map.h"
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
37 const struct ldb_map_attribute *attribute_maps;
38 const struct ldb_map_objectclass *objectclass_maps;
39 const char *last_err_string;
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)
45 struct map_private *privdat = module->private_data;
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];
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)
58 struct map_private *privdat = module->private_data;
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)
65 if (!strcmp(privdat->attribute_maps[i].u.rename.remote_name, attr))
66 return &privdat->attribute_maps[i];
72 static struct ldb_parse_tree *ldb_map_parse_tree(struct ldb_module *module, const struct ldb_parse_tree *tree)
75 const struct ldb_map_attribute *attr;
76 struct ldb_parse_tree *new_tree = talloc_memdup(module, tree, sizeof(*tree));
78 /* Find attr in question and:
79 * - if it has a convert_operator function, run that
80 * - otherwise, replace attr name with required[0] */
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]);
91 if (tree->operation == LDB_OP_NOT) {
92 new_tree->u.isnot.child = ldb_map_parse_tree(module, tree->u.isnot.child);
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
99 * (all have attr as the first element)
102 attr = map_find_attr_local(module, tree->u.equality.attr);
105 DEBUG(0, ("Unable to find local attribute '%s', leaving as is", tree->u.equality.attr));
109 if (attr->type == MAP_IGNORE)
112 if (attr->convert_operator) {
113 /* Run convert_operator */
114 talloc_free(new_tree);
115 new_tree = attr->convert_operator(module, tree);
117 new_tree->u.equality.attr = talloc_strdup(new_tree, attr->u.rename.remote_name);
123 /* Remote DN -> Local DN */
124 static struct ldb_dn *map_remote_dn(struct ldb_module *module, const struct ldb_dn *dn)
126 struct ldb_dn *newdn;
132 newdn = talloc_memdup(module, dn, sizeof(*dn));
136 newdn->components = talloc_memdup(newdn, dn->components, sizeof(struct ldb_dn_component) * newdn->comp_num);
138 if (!newdn->components)
141 /* For each rdn, map the attribute name and possibly the
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);
147 /* Unknown attribute - leave this dn as is and hope the best... */
151 if (attr->type == MAP_IGNORE) {
152 DEBUG(0, ("Local MAP_IGNORE attribute '%s' used in DN!", dn->components[i].name));
157 if (attr->type == MAP_GENERATE) {
158 DEBUG(0, ("Local MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
164 if (attr->type == MAP_CONVERT) {
165 struct ldb_message_element elm, *newelm;
166 struct ldb_val vals[1] = { dn->components[i].value };
168 elm.name = attr->u.convert.remote_name;
172 newelm = attr->u.convert.convert_remote(module, attr->local_name, &elm);
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);
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;
187 struct ldb_parse_tree eqtree, *new_eqtree;
192 newdn = talloc_memdup(module, dn, sizeof(*dn));
196 newdn->components = talloc_memdup(newdn, dn->components, sizeof(struct ldb_dn_component) * newdn->comp_num);
198 if (!newdn->components)
201 /* For each rdn, map the attribute name and possibly the
202 * complete rdn using an equality convert_operator call */
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);
207 /* Unknown attribute - leave this dn as is and hope the best... */
211 if (attr->type == MAP_IGNORE) {
212 DEBUG(0, ("Local MAP_IGNORE attribute '%s' used in DN!", dn->components[i].name));
217 if (attr->type == MAP_GENERATE) {
218 DEBUG(0, ("Local MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
224 /* Simple rename/convert only */
225 if (attr->convert_operator) {
227 eqtree.operation = LDB_OP_EQUALITY;
228 eqtree.u.equality.attr = dn->components[i].name;
229 eqtree.u.equality.value = dn->components[i].value;
231 new_eqtree = ldb_map_parse_tree(module, &eqtree);
233 /* Silently continue for now */
235 DEBUG(0, ("Unable to convert RDN for attribute %s\n", dn->components[i].name));
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 };
245 elm.name = attr->local_name;
249 newelm = attr->u.convert.convert_local(module, attr->u.convert.remote_name, &elm);
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);
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[])
266 int ar_size = 0, last_element = 0;
271 /* Start with good guess of number of elements */
272 for (i = 0; attrs[i]; i++);
274 ret = talloc_array(module, const char *, i);
277 for (i = 0; attrs[i]; i++) {
279 const struct ldb_map_attribute *attr = map_find_attr_local(module, attrs[i]);
282 DEBUG(0, ("Local attribute '%s' does not have a definition!\n", attrs[i]));
288 case MAP_IGNORE: break;
290 if (last_element >= ar_size) {
291 ret = talloc_realloc(module, ret, const char *, ar_size+1);
294 ret[last_element] = attr->local_name;
300 if (last_element >= ar_size) {
301 ret = talloc_realloc(module, ret, const char *, ar_size+1);
304 ret[last_element] = attr->u.rename.remote_name;
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);
316 ret[last_element] = attr->u.generate.remote_names[j];
326 static struct ldb_message *ldb_map_message_incoming(struct ldb_module *module, const char * const*attrs, const struct ldb_message *mi)
329 struct ldb_message *msg = talloc_zero(module, struct ldb_message);
330 struct ldb_message_element *elm, *oldelm;
332 msg->dn = map_remote_dn(module, mi->dn);
334 /* Loop over attrs, find in ldb_map_attribute array and
337 for (i = 0; attrs[i]; i++) {
338 const struct ldb_map_attribute *attr = map_find_attr_local(module, attrs[i]);
341 DEBUG(0, ("Unable to find local attribute '%s' when generating incoming message", attrs[i]));
345 switch (attr->type) {
346 case MAP_IGNORE:break;
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);
352 ldb_msg_add(module->ldb, msg, elm, 0);
356 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
357 elm = attr->u.convert.convert_local(msg, attr->local_name, oldelm);
359 ldb_msg_add(module->ldb, msg, elm, 0);
363 ldb_msg_add(module->ldb, msg, ldb_msg_find_element(mi, attr->local_name), 0);
367 elm = attr->u.generate.generate_local(msg, attr->local_name, mi);
368 ldb_msg_add(module->ldb, msg, elm, 0);
371 DEBUG(0, ("Unknown attr->type for %s", attr->local_name));
379 static struct ldb_message *ldb_map_message_outgoing(struct ldb_module *module, const struct ldb_message *mo)
381 struct ldb_message *msg = talloc_zero(module, struct ldb_message);
382 struct ldb_message_element *elm;
385 msg->private_data = mo->private_data;
387 msg->dn = map_local_dn(module, mo->dn);
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);
394 DEBUG(0, ("Undefined local attribute '%s', ignoring\n", mo->elements[i].name));
398 switch (attr->type) {
399 case MAP_IGNORE: break;
401 elm = talloc_memdup(msg, &msg->elements[i], sizeof(*elm));
402 elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
404 ldb_msg_add(module->ldb, msg, elm, 0);
408 elm = attr->u.convert.convert_remote(msg, attr->local_name, &msg->elements[i]);
409 ldb_msg_add(module->ldb, msg, elm, 0);
413 ldb_msg_add(module->ldb, msg, &msg->elements[i], 0);
417 attr->u.generate.generate_remote(attr->local_name, mo, msg);
428 static int map_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
430 struct ldb_dn *n_olddn, *n_newdn;
433 n_olddn = map_local_dn(module, olddn);
434 n_newdn = map_local_dn(module, newdn);
436 ret = ldb_next_rename_record(module, n_olddn, n_newdn);
438 talloc_free(n_olddn);
439 talloc_free(n_newdn);
447 static int map_delete(struct ldb_module *module, const struct ldb_dn *dn)
449 struct ldb_dn *newdn;
452 newdn = map_local_dn(module, dn);
454 ret = ldb_next_delete_record(module, newdn);
462 search for matching records using a ldb_parse_tree
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)
469 const char **newattrs;
470 struct ldb_parse_tree *new_tree;
471 struct ldb_dn *new_base;
472 struct ldb_message **newres;
475 new_tree = ldb_map_parse_tree(module, tree);
476 newattrs = ldb_map_attrs(module, attrs);
477 new_base = map_local_dn(module, base);
479 ret = ldb_next_search_bytree(module, new_base, scope, new_tree, newattrs, &newres);
481 talloc_free(new_base);
482 talloc_free(new_tree);
483 talloc_free(newattrs);
485 for (i = 0; i < ret; i++) {
486 *res[i] = ldb_map_message_incoming(module, attrs, newres[i]);
487 talloc_free(newres[i]);
493 search for matching records
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)
499 struct map_private *map = module->private_data;
500 struct ldb_parse_tree *tree;
503 tree = ldb_parse_tree(map, expression);
505 map->last_err_string = "expression parse failed";
509 ret = map_search_bytree(module, base, scope, tree, attrs, res);
517 static int map_add(struct ldb_module *module, const struct ldb_message *msg)
519 struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
522 ret = ldb_next_add_record(module, nmsg);
535 static int map_modify(struct ldb_module *module, const struct ldb_message *msg)
537 struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
540 ret = ldb_next_modify_record(module, nmsg);
547 static int map_lock(struct ldb_module *module, const char *lockname)
549 return ldb_next_named_lock(module, lockname);
552 static int map_unlock(struct ldb_module *module, const char *lockname)
554 return ldb_next_named_unlock(module, lockname);
558 return extended error information
560 static const char *map_errstring(struct ldb_module *module)
562 struct map_private *map = module->private_data;
564 if (map->last_err_string)
565 return map->last_err_string;
567 return ldb_next_errstring(module);
570 static const struct ldb_module_ops map_ops = {
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
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[])
586 struct ldb_module *ctx;
587 struct map_private *data;
589 ctx = talloc(ldb, struct ldb_module);
593 data = talloc(ctx, struct map_private);
599 data->attribute_maps = attrs;
600 data->objectclass_maps = ocls;
601 ctx->private_data = data;
603 ctx->prev = ctx->next = NULL;