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/includes.h"
39 #define LDB_MODULE_PREFIX "modules:"
40 #define LDB_MODULE_PREFIX_LEN 8
42 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
47 trimmed = talloc_strdup(ldb, string);
49 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
53 len = strlen(trimmed);
54 for (i = 0; trimmed[i] != '\0'; i++) {
59 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
68 /* modules are called in inverse order on the stack.
69 Lets place them as an admin would think the right order is.
70 Modules order is important */
71 static char **ldb_modules_list_from_string(struct ldb_context *ldb, const char *string)
73 char **modules = NULL;
77 /* spaces not admitted */
78 modstr = talloc_strdup_no_spaces(ldb, string);
83 modules = talloc_realloc(ldb, modules, char *, 2);
85 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
89 talloc_steal(modules, modstr);
92 while ((p = strrchr(modstr, ',')) != NULL) {
98 modules = talloc_realloc(ldb, modules, char *, i + 2);
100 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
107 modules[i + 1] = NULL;
112 static struct ops_list_entry {
113 const struct ldb_module_ops *ops;
114 struct ops_list_entry *next;
115 } *registered_modules = NULL;
117 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
119 struct ops_list_entry *e;
121 for (e = registered_modules; e; e = e->next) {
122 if (strcmp(e->ops->name, name) == 0)
130 #define LDAP_INIT ldb_ldap_init,
136 #define SQLITE3_INIT ldb_sqlite3_init,
141 #ifndef STATIC_LIBLDB_MODULES
142 #define STATIC_LIBLDB_MODULES \
148 ldb_operational_init, \
150 ldb_objectclass_init, \
151 ldb_paged_results_init, \
157 int ldb_global_init(void)
159 static int (*static_init_fns[])(void) = STATIC_LIBLDB_MODULES;
161 static int initialized = 0;
169 for (i = 0; static_init_fns[i]; i++) {
170 if (static_init_fns[i]() == -1)
177 int ldb_register_module(const struct ldb_module_ops *ops)
179 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
181 if (ldb_find_module_ops(ops->name) != NULL)
188 entry->next = registered_modules;
189 registered_modules = entry;
194 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
196 char **modules = NULL;
197 struct ldb_module *module;
199 /* find out which modules we are requested to activate */
201 /* check if we have a custom module list passd as ldb option */
203 for (i = 0; options[i] != NULL; i++) {
204 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
205 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
210 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
211 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
213 const char * const attrs[] = { "@LIST" , NULL};
214 struct ldb_result *res = NULL;
217 mods = ldb_dn_explode(ldb, "@MODULES");
222 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &res);
224 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
225 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
227 if (ret != LDB_SUCCESS) {
228 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
231 if (res->count > 1) {
232 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
237 modules = ldb_modules_list_from_string(ldb,
238 (const char *)res->msgs[0]->elements[0].values[0].data);
245 if (modules != NULL) {
246 for (i = 0; modules[i] != NULL; i++) {
247 struct ldb_module *current;
248 const struct ldb_module_ops *ops;
250 ops = ldb_find_module_ops(modules[i]);
252 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
257 current = talloc_zero(ldb, struct ldb_module);
258 if (current == NULL) {
265 DLIST_ADD(ldb->modules, current);
268 talloc_free(modules);
270 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
273 module = ldb->modules;
275 while (module && module->ops->init_context == NULL)
276 module = module->next;
278 if (module && module->ops->init_context &&
279 module->ops->init_context(module) != LDB_SUCCESS) {
280 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
288 by using this we allow ldb modules to only implement the functions they care about,
289 which makes writing a module simpler, and makes it more likely to keep working
292 #define FIND_OP(module, op) do { \
293 module = module->next; \
294 while (module && module->ops->op == NULL) module = module->next; \
295 if (module == NULL) return LDB_ERR_OPERATIONS_ERROR; \
300 helper functions to call the next module in chain
302 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
304 FIND_OP(module, request);
305 return module->ops->request(module, request);
308 int ldb_next_init(struct ldb_module *module)
310 /* init is different in that it is not an error if modules
311 * do not require initialization */
313 module = module->next;
315 while (module && module->ops->init_context == NULL)
316 module = module->next;
321 return module->ops->init_context(module);
324 int ldb_next_start_trans(struct ldb_module *module)
326 FIND_OP(module, start_transaction);
327 return module->ops->start_transaction(module);
330 int ldb_next_end_trans(struct ldb_module *module)
332 FIND_OP(module, end_transaction);
333 return module->ops->end_transaction(module);
336 int ldb_next_del_trans(struct ldb_module *module)
338 FIND_OP(module, del_transaction);
339 return module->ops->del_transaction(module);
342 int ldb_next_async_wait(struct ldb_module *module, struct ldb_async_handle *handle, enum ldb_async_wait_type type)
344 FIND_OP(module, async_wait);
345 return module->ops->async_wait(module, handle, type);