5 Copyright (C) Simo Sorce 2004
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb modules core
31 * Description: core modules routines
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dlinklist.h"
40 #include <sys/types.h>
44 #ifdef HAVE_DLOPEN_DISABLED
48 #define LDB_MODULE_PREFIX "modules:"
49 #define LDB_MODULE_PREFIX_LEN 8
51 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
56 trimmed = talloc_strdup(ldb, string);
58 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
62 len = strlen(trimmed);
63 for (i = 0; trimmed[i] != '\0'; i++) {
68 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
77 /* modules are called in inverse order on the stack.
78 Lets place them as an admin would think the right order is.
79 Modules order is imprtant */
80 static char **ldb_modules_list_from_string(struct ldb_context *ldb, const char *string)
82 char **modules = NULL;
86 /* spaces not admitted */
87 modstr = talloc_strdup_no_spaces(ldb, string);
92 modules = talloc_realloc(ldb, modules, char *, 2);
94 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
98 talloc_steal(modules, modstr);
101 while ((p = strrchr(modstr, ',')) != NULL) {
107 modules = talloc_realloc(ldb, modules, char *, i + 2);
109 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
116 modules[i + 1] = NULL;
121 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
123 char **modules = NULL;
127 ldb_module_init_t init;
128 } well_known_modules[] = {
129 { "schema", schema_module_init },
130 { "timestamps", timestamps_module_init },
131 { "rdn_name", rdn_name_module_init },
133 { "objectguid", objectguid_module_init },
134 { "samldb", samldb_module_init },
135 { "samba3sam", ldb_samba3sam_module_init },
136 { "proxy", proxy_module_init },
141 /* find out which modules we are requested to activate */
143 /* check if we have a custom module list passd as ldb option */
145 for (i = 0; options[i] != NULL; i++) {
146 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
147 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
152 /* if not overloaded by options and the backend is not ldap try to load the modules list form ldb */
153 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
155 const char * const attrs[] = { "@LIST" , NULL};
156 struct ldb_message **msg = NULL;
159 mods = ldb_dn_explode(ldb, "@MODULES");
164 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &msg);
166 if (ret == 0 || (ret == 1 && msg[0]->num_elements == 0)) {
167 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
170 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
174 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", ret);
179 modules = ldb_modules_list_from_string(ldb,
180 (const char *)msg[0]->elements[0].values[0].data);
187 if (modules == NULL) {
188 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
192 for (i = 0; modules[i] != NULL; i++) {
193 struct ldb_module *current;
195 for (m=0;well_known_modules[m].name;m++) {
196 if (strcmp(modules[i], well_known_modules[m].name) == 0) {
197 current = well_known_modules[m].init(ldb, options);
198 if (current == NULL) {
199 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
202 DLIST_ADD(ldb->modules, current);
206 if (well_known_modules[m].name == NULL) {
207 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
212 talloc_free(modules);
217 by using this we allow ldb modules to only implement the functions they care about,
218 which makes writing a module simpler, and makes it more likely to keep working
221 #define FIND_OP(module, op) do { \
222 module = module->next; \
223 while (module && module->ops->op == NULL) module = module->next; \
224 if (module == NULL) return -1; \
229 helper functions to call the next module in chain
231 int ldb_next_search_bytree(struct ldb_module *module,
232 const struct ldb_dn *base,
233 enum ldb_scope scope,
234 struct ldb_parse_tree *tree,
235 const char * const *attrs, struct ldb_message ***res)
237 FIND_OP(module, search_bytree);
238 return module->ops->search_bytree(module, base, scope, tree, attrs, res);
241 int ldb_next_search(struct ldb_module *module,
242 const struct ldb_dn *base,
243 enum ldb_scope scope,
244 const char *expression,
245 const char * const *attrs, struct ldb_message ***res)
247 struct ldb_parse_tree *tree;
249 FIND_OP(module, search_bytree);
250 tree = ldb_parse_tree(module, expression);
252 ldb_set_errstring(module, talloc_strdup(module, "Unable to parse search expression"));
255 ret = module->ops->search_bytree(module, base, scope, tree, attrs, res);
261 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
263 FIND_OP(module, add_record);
264 return module->ops->add_record(module, message);
267 int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message)
269 FIND_OP(module, modify_record);
270 return module->ops->modify_record(module, message);
273 int ldb_next_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
275 FIND_OP(module, delete_record);
276 return module->ops->delete_record(module, dn);
279 int ldb_next_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
281 FIND_OP(module, rename_record);
282 return module->ops->rename_record(module, olddn, newdn);
285 int ldb_next_start_trans(struct ldb_module *module)
287 FIND_OP(module, start_transaction);
288 return module->ops->start_transaction(module);
291 int ldb_next_end_trans(struct ldb_module *module)
293 FIND_OP(module, end_transaction);
294 return module->ops->end_transaction(module);
297 int ldb_next_del_trans(struct ldb_module *module)
299 FIND_OP(module, del_transaction);
300 return module->ops->del_transaction(module);
303 void ldb_set_errstring(struct ldb_module *module, char *err_string)
305 if (module->ldb->err_string) {
306 talloc_free(module->ldb->err_string);
309 module->ldb->err_string = err_string;