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 struct ldb_map_mappings *mappings;
34 static struct ldb_dn *ldb_map_dn(struct ldb_module *module, const struct ldb_dn *dn)
40 static char *ldb_map_expression(struct ldb_module *module, const char *expr)
46 static const char **ldb_map_attrs(struct ldb_module *module, const char *const attrs[])
52 static struct ldb_message *ldb_map_message_incoming(struct ldb_module *module, const struct ldb_message *mi)
58 static struct ldb_message *ldb_map_message_outgoing(struct ldb_module *module, const struct ldb_message *mi)
67 static int map_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
69 struct ldb_dn *n_olddn, *n_newdn;
72 n_olddn = ldb_map_dn(module, olddn);
73 n_newdn = ldb_map_dn(module, newdn);
75 ret = ldb_next_rename_record(module, n_olddn, n_newdn);
86 static int map_delete(struct ldb_module *module, const struct ldb_dn *dn)
91 newdn = ldb_map_dn(module, dn);
93 ret = ldb_next_delete_record(module, newdn);
102 search for matching records
104 static int map_search(struct ldb_module *module, const struct ldb_dn *base,
105 enum ldb_scope scope, const char *expression,
106 const char * const *attrs, struct ldb_message ***res)
110 const char **newattrs;
111 struct ldb_dn *new_base;
112 struct ldb_message **newres;
115 newexpr = ldb_map_expression(module, expression);
116 newattrs = ldb_map_attrs(module, attrs);
117 new_base = ldb_map_dn(module, base);
119 ret = ldb_next_search(module, new_base, scope, newexpr, newattrs, &newres);
121 talloc_free(new_base);
122 talloc_free(newexpr);
123 talloc_free(newattrs);
125 for (i = 0; i < ret; i++) {
126 *res[i] = ldb_map_message_incoming(module, newres[i]);
127 talloc_free(newres[i]);
136 static int map_add(struct ldb_module *module, const struct ldb_message *msg)
138 struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
141 ret = ldb_next_add_record(module, nmsg);
150 search for matching records using a ldb_parse_tree
152 static int map_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
153 enum ldb_scope scope, struct ldb_parse_tree *tree,
154 const char * const *attrs, struct ldb_message ***res)
156 struct map_private *privdat = module->private_data;
160 expression = ldb_filter_from_tree(privdat, tree);
161 if (expression == NULL) {
164 ret = map_search(module, base, scope, expression, attrs, res);
165 talloc_free(expression);
172 static int map_modify(struct ldb_module *module, const struct ldb_message *msg)
174 struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
177 ret = ldb_next_modify_record(module, nmsg);
184 static int map_lock(struct ldb_module *module, const char *lockname)
186 return ldb_next_named_lock(module, lockname);
189 static int map_unlock(struct ldb_module *module, const char *lockname)
191 return ldb_next_named_unlock(module, lockname);
195 return extended error information
197 static const char *map_errstring(struct ldb_module *module)
199 return ldb_next_errstring(module);
202 static const struct ldb_module_ops map_ops = {
204 .search = map_search,
205 .search_bytree = map_search_bytree,
206 .add_record = map_add,
207 .modify_record = map_modify,
208 .delete_record = map_delete,
209 .rename_record = map_rename,
210 .named_lock = map_lock,
211 .named_unlock = map_unlock,
212 .errstring = map_errstring
215 /* the init function */
216 struct ldb_module *ldb_map_init(struct ldb_context *ldb, struct ldb_map_mappings *mappings, const char *options[])
218 struct ldb_module *ctx;
219 struct map_private *data;
221 ctx = talloc(ldb, struct ldb_module);
225 data = talloc(ctx, struct map_private);
231 data->mappings = mappings;
232 ctx->private_data = data;
234 ctx->prev = ctx->next = NULL;