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;
126 /* find out which modules we are requested to activate */
128 /* check if we have a custom module list passd as ldb option */
130 for (i = 0; options[i] != NULL; i++) {
131 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
132 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
137 /* if not overloaded by options and the backend is not ldap try to load the modules list form ldb */
138 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
140 const char * const attrs[] = { "@LIST" , NULL};
141 struct ldb_message **msg = NULL;
144 mods = ldb_dn_explode(ldb, "@MODULES");
149 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &msg);
151 if (ret == 0 || (ret == 1 && msg[0]->num_elements == 0)) {
152 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
155 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
159 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
164 modules = ldb_modules_list_from_string(ldb, msg[0]->elements[0].values[0].data);
171 if (modules == NULL) {
172 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
176 for (i = 0; modules[i] != NULL; i++) {
177 struct ldb_module *current;
179 if (strcmp(modules[i], "schema") == 0) {
180 current = schema_module_init(ldb, options);
182 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
185 DLIST_ADD(ldb->modules, current);
189 if (strcmp(modules[i], "timestamps") == 0) {
190 current = timestamps_module_init(ldb, options);
192 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
195 DLIST_ADD(ldb->modules, current);
199 if (strcmp(modules[i], "rdn_name") == 0) {
200 current = rdn_name_module_init(ldb, options);
202 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
205 DLIST_ADD(ldb->modules, current);
210 if (strcmp(modules[i], "objectguid") == 0) {
211 current = objectguid_module_init(ldb, options);
213 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
216 DLIST_ADD(ldb->modules, current);
220 if (strcmp(modules[i], "samldb") == 0) {
221 current = samldb_module_init(ldb, options);
223 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
226 DLIST_ADD(ldb->modules, current);
231 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", modules[i]);
234 talloc_free(modules);
239 helper functions to call the next module in chain
242 int ldb_next_search(struct ldb_module *module,
243 const struct ldb_dn *base,
244 enum ldb_scope scope,
245 const char *expression,
246 const char * const *attrs, struct ldb_message ***res)
251 return module->next->ops->search(module->next, base, scope, expression, attrs, res);
254 int ldb_next_search_bytree(struct ldb_module *module,
255 const struct ldb_dn *base,
256 enum ldb_scope scope,
257 struct ldb_parse_tree *tree,
258 const char * const *attrs, struct ldb_message ***res)
263 return module->next->ops->search_bytree(module->next, base, scope, tree, attrs, res);
266 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
271 return module->next->ops->add_record(module->next, message);
274 int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message)
279 return module->next->ops->modify_record(module->next, message);
282 int ldb_next_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
287 return module->next->ops->delete_record(module->next, dn);
290 int ldb_next_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
295 return module->next->ops->rename_record(module->next, olddn, newdn);
298 int ldb_next_named_lock(struct ldb_module *module, const char *lockname)
303 return module->next->ops->named_lock(module->next, lockname);
306 int ldb_next_named_unlock(struct ldb_module *module, const char *lockname)
311 return module->next->ops->named_unlock(module->next, lockname);
314 const char *ldb_next_errstring(struct ldb_module *module)
319 return module->next->ops->errstring(module->next);