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;
129 newdn = talloc_memdup(module, dn, sizeof(*dn));
133 newdn->components = talloc_memdup(newdn, dn->components, sizeof(struct ldb_dn_component) * newdn->comp_num);
135 if (!newdn->components)
138 /* For each rdn, map the attribute name and possibly the
141 for (i = 0; i < dn->comp_num; i++) {
142 const struct ldb_map_attribute *attr = map_find_attr_remote(module, dn->components[i].name);
144 /* Unknown attribute - leave this dn as is and hope the best... */
148 if (attr->type == MAP_IGNORE) {
149 DEBUG(0, ("Local MAP_IGNORE attribute '%s' used in DN!", dn->components[i].name));
154 if (attr->type == MAP_GENERATE) {
155 DEBUG(0, ("Local MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
161 if (attr->type == MAP_CONVERT) {
162 struct ldb_message_element elm, *newelm;
163 struct ldb_val vals[1] = { dn->components[i].value };
165 elm.name = attr->u.convert.remote_name;
169 newelm = attr->u.convert.convert_remote(module, attr->local_name, &elm);
171 newdn->components[i].name = talloc_strdup(module, newelm->name);
172 newdn->components[i].value = newelm->values[0];
173 } else if (attr->type == MAP_RENAME) {
174 newdn->components[i].name = talloc_strdup(module, attr->local_name);
180 /* Local DN -> Remote DN */
181 static struct ldb_dn *map_local_dn(struct ldb_module *module, const struct ldb_dn *dn)
182 { struct ldb_dn *newdn;
184 struct ldb_parse_tree eqtree, *new_eqtree;
186 newdn = talloc_memdup(module, dn, sizeof(*dn));
190 newdn->components = talloc_memdup(newdn, dn->components, sizeof(struct ldb_dn_component) * newdn->comp_num);
192 if (!newdn->components)
195 /* For each rdn, map the attribute name and possibly the
196 * complete rdn using an equality convert_operator call */
198 for (i = 0; i < dn->comp_num; i++) {
199 const struct ldb_map_attribute *attr = map_find_attr_local(module, dn->components[i].name);
201 /* Unknown attribute - leave this dn as is and hope the best... */
205 if (attr->type == MAP_IGNORE) {
206 DEBUG(0, ("Local MAP_IGNORE attribute '%s' used in DN!", dn->components[i].name));
211 if (attr->type == MAP_GENERATE) {
212 DEBUG(0, ("Local MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
218 /* Simple rename/convert only */
219 if (attr->convert_operator) {
221 eqtree.operation = LDB_OP_EQUALITY;
222 eqtree.u.equality.attr = dn->components[i].name;
223 eqtree.u.equality.value = dn->components[i].value;
225 new_eqtree = ldb_map_parse_tree(module, &eqtree);
227 /* Silently continue for now */
229 DEBUG(0, ("Unable to convert RDN for attribute %s\n", dn->components[i].name));
233 newdn->components[i].name = new_eqtree->u.equality.attr;
234 newdn->components[i].value = new_eqtree->u.equality.value;
235 } else if (attr->type == MAP_CONVERT) {
236 struct ldb_message_element elm, *newelm;
237 struct ldb_val vals[1] = { dn->components[i].value };
239 elm.name = attr->local_name;
243 newelm = attr->u.convert.convert_local(module, attr->u.convert.remote_name, &elm);
245 newdn->components[i].name = talloc_strdup(module, newelm->name);
246 newdn->components[i].value = newelm->values[0];
247 } else if (attr->type == MAP_RENAME) {
248 newdn->components[i].name = talloc_strdup(module, attr->u.rename.remote_name);
257 /* Loop over ldb_map_attribute array and add remote_names */
258 static const char **ldb_map_attrs(struct ldb_module *module, const char *const attrs[])
262 int ar_size = 0, last_element = 0;
264 /* Start with good guess of number of elements */
265 for (i = 0; attrs[i]; i++);
267 ret = talloc_array(module, const char *, i);
270 for (i = 0; attrs[i]; i++) {
272 const struct ldb_map_attribute *attr = map_find_attr_local(module, attrs[i]);
275 DEBUG(0, ("Local attribute '%s' does not have a definition!\n", attrs[i]));
281 case MAP_IGNORE: break;
283 if (last_element >= ar_size) {
284 ret = talloc_realloc(module, ret, const char *, ar_size+1);
287 ret[last_element] = attr->local_name;
293 if (last_element >= ar_size) {
294 ret = talloc_realloc(module, ret, const char *, ar_size+1);
297 ret[last_element] = attr->u.rename.remote_name;
302 /* Add remote_names[] for this attribute to the list of
303 * attributes to request from the remote server */
304 for (j = 0; attr->u.generate.remote_names[j]; j++) {
305 if (last_element >= ar_size) {
306 ret = talloc_realloc(module, ret, const char *, ar_size+1);
309 ret[last_element] = attr->u.generate.remote_names[j];
319 static struct ldb_message *ldb_map_message_incoming(struct ldb_module *module, const char * const*attrs, const struct ldb_message *mi)
322 struct ldb_message *msg = talloc_zero(module, struct ldb_message);
323 struct ldb_message_element *elm, *oldelm;
325 msg->dn = map_remote_dn(module, mi->dn);
327 /* Loop over attrs, find in ldb_map_attribute array and
330 for (i = 0; attrs[i]; i++) {
331 const struct ldb_map_attribute *attr = map_find_attr_local(module, attrs[i]);
334 DEBUG(0, ("Unable to find local attribute '%s' when generating incoming message", attrs[i]));
338 switch (attr->type) {
339 case MAP_IGNORE:break;
341 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
342 elm = talloc_memdup(msg, oldelm, sizeof(*oldelm));
343 elm->name = talloc_strdup(elm, attr->local_name);
345 ldb_msg_add(module->ldb, msg, elm, 0);
349 oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
350 elm = attr->u.convert.convert_local(msg, attr->local_name, oldelm);
352 ldb_msg_add(module->ldb, msg, elm, 0);
356 ldb_msg_add(module->ldb, msg, ldb_msg_find_element(mi, attr->local_name), 0);
360 elm = attr->u.generate.generate_local(msg, attr->local_name, mi);
361 ldb_msg_add(module->ldb, msg, elm, 0);
364 DEBUG(0, ("Unknown attr->type for %s", attr->local_name));
372 static struct ldb_message *ldb_map_message_outgoing(struct ldb_module *module, const struct ldb_message *mo)
374 struct ldb_message *msg = talloc_zero(module, struct ldb_message);
375 struct ldb_message_element *elm;
378 msg->private_data = mo->private_data;
380 msg->dn = map_local_dn(module, mo->dn);
382 /* Loop over mi and call generate_remote for each attribute */
383 for (i = 0; i < mo->num_elements; i++) {
384 const struct ldb_map_attribute *attr = map_find_attr_local(module, mo->elements[i].name);
387 DEBUG(0, ("Undefined local attribute '%s', ignoring\n", mo->elements[i].name));
391 switch (attr->type) {
392 case MAP_IGNORE: break;
394 elm = talloc_memdup(msg, &msg->elements[i], sizeof(*elm));
395 elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
397 ldb_msg_add(module->ldb, msg, elm, 0);
401 elm = attr->u.convert.convert_remote(msg, attr->local_name, &msg->elements[i]);
402 ldb_msg_add(module->ldb, msg, elm, 0);
406 ldb_msg_add(module->ldb, msg, &msg->elements[i], 0);
410 attr->u.generate.generate_remote(attr->local_name, mo, msg);
421 static int map_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
423 struct ldb_dn *n_olddn, *n_newdn;
426 n_olddn = map_local_dn(module, olddn);
427 n_newdn = map_local_dn(module, newdn);
429 ret = ldb_next_rename_record(module, n_olddn, n_newdn);
431 talloc_free(n_olddn);
432 talloc_free(n_newdn);
440 static int map_delete(struct ldb_module *module, const struct ldb_dn *dn)
442 struct ldb_dn *newdn;
445 newdn = map_local_dn(module, dn);
447 ret = ldb_next_delete_record(module, newdn);
455 search for matching records using a ldb_parse_tree
457 static int map_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
458 enum ldb_scope scope, struct ldb_parse_tree *tree,
459 const char * const *attrs, struct ldb_message ***res)
462 const char **newattrs;
463 struct ldb_parse_tree *new_tree;
464 struct ldb_dn *new_base;
465 struct ldb_message **newres;
468 new_tree = ldb_map_parse_tree(module, tree);
469 newattrs = ldb_map_attrs(module, attrs);
470 new_base = map_local_dn(module, base);
472 ret = ldb_next_search_bytree(module, new_base, scope, new_tree, newattrs, &newres);
474 talloc_free(new_base);
475 talloc_free(new_tree);
476 talloc_free(newattrs);
478 for (i = 0; i < ret; i++) {
479 *res[i] = ldb_map_message_incoming(module, attrs, newres[i]);
480 talloc_free(newres[i]);
486 search for matching records
488 static int map_search(struct ldb_module *module, const struct ldb_dn *base,
489 enum ldb_scope scope, const char *expression,
490 const char * const *attrs, struct ldb_message ***res)
492 struct map_private *map = module->private_data;
493 struct ldb_parse_tree *tree;
496 tree = ldb_parse_tree(map, expression);
498 map->last_err_string = "expression parse failed";
502 ret = map_search_bytree(module, base, scope, tree, attrs, res);
510 static int map_add(struct ldb_module *module, const struct ldb_message *msg)
512 struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
515 ret = ldb_next_add_record(module, nmsg);
528 static int map_modify(struct ldb_module *module, const struct ldb_message *msg)
530 struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
533 ret = ldb_next_modify_record(module, nmsg);
540 static int map_lock(struct ldb_module *module, const char *lockname)
542 return ldb_next_named_lock(module, lockname);
545 static int map_unlock(struct ldb_module *module, const char *lockname)
547 return ldb_next_named_unlock(module, lockname);
551 return extended error information
553 static const char *map_errstring(struct ldb_module *module)
555 struct map_private *map = module->private_data;
557 if (map->last_err_string)
558 return map->last_err_string;
560 return ldb_next_errstring(module);
563 static const struct ldb_module_ops map_ops = {
565 .search = map_search,
566 .search_bytree = map_search_bytree,
567 .add_record = map_add,
568 .modify_record = map_modify,
569 .delete_record = map_delete,
570 .rename_record = map_rename,
571 .named_lock = map_lock,
572 .named_unlock = map_unlock,
573 .errstring = map_errstring
576 /* the init function */
577 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[])
579 struct ldb_module *ctx;
580 struct map_private *data;
582 ctx = talloc(ldb, struct ldb_module);
586 data = talloc(ctx, struct map_private);
592 data->attribute_maps = attrs;
593 data->objectclass_maps = ocls;
594 ctx->private_data = data;
596 ctx->prev = ctx->next = NULL;