r17397: Add const, and use a more local memory context.
[Samba/aatanasov.git] / source / lib / ldb / common / ldb_modules.c
blob7d1a91126e3e3585c8e1bbe72a529202829ff2ac
2 /*
3 ldb database library
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
9 ** under the LGPL
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
27 * Name: ldb
29 * Component: ldb modules core
31 * Description: core modules routines
33 * Author: Simo Sorce
36 #include "includes.h"
37 #include "ldb/include/includes.h"
39 #ifdef _SAMBA_BUILD_
40 #include "build.h"
41 #include "dynconfig.h"
42 #endif
44 #define LDB_MODULE_PREFIX "modules:"
45 #define LDB_MODULE_PREFIX_LEN 8
47 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
49 int i, len;
50 char *trimmed;
52 trimmed = talloc_strdup(ldb, string);
53 if (!trimmed) {
54 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
55 return NULL;
58 len = strlen(trimmed);
59 for (i = 0; trimmed[i] != '\0'; i++) {
60 switch (trimmed[i]) {
61 case ' ':
62 case '\t':
63 case '\n':
64 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
65 break;
69 return trimmed;
73 /* modules are called in inverse order on the stack.
74 Lets place them as an admin would think the right order is.
75 Modules order is important */
76 static const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
78 char **modules = NULL;
79 const char **m;
80 char *modstr, *p;
81 int i;
83 /* spaces not admitted */
84 modstr = talloc_strdup_no_spaces(mem_ctx, string);
85 if ( ! modstr) {
86 return NULL;
89 modules = talloc_realloc(mem_ctx, modules, char *, 2);
90 if ( ! modules ) {
91 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
92 talloc_free(modstr);
93 return NULL;
95 talloc_steal(modules, modstr);
97 i = 0;
98 while ((p = strrchr(modstr, ',')) != NULL) {
99 *p = '\0';
100 p++;
101 modules[i] = p;
103 i++;
104 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
105 if ( ! modules ) {
106 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
107 return NULL;
111 modules[i] = modstr;
113 modules[i + 1] = NULL;
115 m = (const char **)modules;
117 return m;
120 static struct ops_list_entry {
121 const struct ldb_module_ops *ops;
122 struct ops_list_entry *next;
123 } *registered_modules = NULL;
125 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
127 struct ops_list_entry *e;
129 for (e = registered_modules; e; e = e->next) {
130 if (strcmp(e->ops->name, name) == 0)
131 return e->ops;
134 return NULL;
137 #ifdef HAVE_LDAP
138 #define LDAP_INIT ldb_ldap_init,
139 #else
140 #define LDAP_INIT
141 #endif
143 #ifdef HAVE_SQLITE3
144 #define SQLITE3_INIT ldb_sqlite3_init,
145 #else
146 #define SQLITE3_INIT
147 #endif
149 #ifndef STATIC_ldb_MODULES
150 #define STATIC_ldb_MODULES \
152 LDAP_INIT \
153 SQLITE3_INIT \
154 ldb_tdb_init, \
155 ldb_schema_init, \
156 ldb_operational_init, \
157 ldb_rdn_name_init, \
158 ldb_objectclass_init, \
159 ldb_paged_results_init, \
160 ldb_sort_init, \
161 NULL \
163 #endif
165 int ldb_global_init(void)
167 static int (*static_init_fns[])(void) = STATIC_ldb_MODULES;
169 static int initialized = 0;
170 int ret = 0, i;
172 if (initialized)
173 return 0;
175 initialized = 1;
177 for (i = 0; static_init_fns[i]; i++) {
178 if (static_init_fns[i]() == -1)
179 ret = -1;
182 return ret;
185 int ldb_register_module(const struct ldb_module_ops *ops)
187 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
189 if (ldb_find_module_ops(ops->name) != NULL)
190 return -1;
192 if (entry == NULL)
193 return -1;
195 entry->ops = ops;
196 entry->next = registered_modules;
197 registered_modules = entry;
199 return 0;
202 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
204 char *path;
205 void *handle;
206 int (*init_fn) (void);
208 #ifdef HAVE_DLOPEN
209 #ifdef _SAMBA_BUILD_
210 path = talloc_asprintf(ldb, "%s/ldb/%s.%s", dyn_MODULESDIR, name, dyn_SHLIBEXT);
211 #else
212 path = talloc_asprintf(ldb, "%s/%s.%s", MODULESDIR, name, SHLIBEXT);
213 #endif
215 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
217 handle = dlopen(path, 0);
218 if (handle == NULL) {
219 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
220 return -1;
223 init_fn = dlsym(handle, "init_module");
225 if (init_fn == NULL) {
226 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
227 return -1;
230 talloc_free(path);
232 return init_fn();
233 #else
234 ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
235 return -1;
236 #endif
239 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
241 const char **modules = NULL;
242 struct ldb_module *module;
243 int i;
244 TALLOC_CTX *mem_ctx = talloc_new(ldb);
245 if (!mem_ctx) {
246 return LDB_ERR_OPERATIONS_ERROR;
249 /* find out which modules we are requested to activate */
251 /* check if we have a custom module list passd as ldb option */
252 if (options) {
253 for (i = 0; options[i] != NULL; i++) {
254 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
255 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
260 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
261 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
262 int ret;
263 const char * const attrs[] = { "@LIST" , NULL};
264 struct ldb_result *res;
265 struct ldb_dn *mods;
267 mods = ldb_dn_explode(mem_ctx, "@MODULES");
268 if (mods == NULL) {
269 talloc_free(mem_ctx);
270 return -1;
273 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &res);
274 talloc_free(mods);
275 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
276 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
277 } else {
278 if (ret != LDB_SUCCESS) {
279 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
280 talloc_free(mem_ctx);
281 return -1;
283 if (res->count > 1) {
284 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
285 talloc_free(res);
286 talloc_free(mem_ctx);
287 return -1;
290 modules = ldb_modules_list_from_string(ldb, mem_ctx,
291 (const char *)res->msgs[0]->elements[0].values[0].data);
293 talloc_free(res);
297 if (modules != NULL) {
298 for (i = 0; modules[i] != NULL; i++) {
299 struct ldb_module *current;
300 const struct ldb_module_ops *ops;
302 ops = ldb_find_module_ops(modules[i]);
303 if (ops == NULL) {
304 if (ldb_try_load_dso(ldb, modules[i]) == 0) {
305 ops = ldb_find_module_ops(modules[i]);
309 if (ops == NULL) {
310 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
311 modules[i]);
312 continue;
315 current = talloc_zero(ldb, struct ldb_module);
316 if (current == NULL) {
317 return -1;
320 current->ldb = ldb;
321 current->ops = ops;
323 DLIST_ADD(ldb->modules, current);
326 talloc_free(modules);
327 } else {
328 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
331 module = ldb->modules;
333 while (module && module->ops->init_context == NULL)
334 module = module->next;
336 if (module && module->ops->init_context &&
337 module->ops->init_context(module) != LDB_SUCCESS) {
338 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
339 return -1;
342 return 0;
346 by using this we allow ldb modules to only implement the functions they care about,
347 which makes writing a module simpler, and makes it more likely to keep working
348 when ldb is extended
350 #define FIND_OP(module, op) do { \
351 struct ldb_context *ldb = module->ldb; \
352 module = module->next; \
353 while (module && module->ops->op == NULL) module = module->next; \
354 if (module == NULL) { \
355 ldb_set_errstring(ldb, talloc_strdup(ldb, "Unable to find backend operation for " #op )); \
356 return LDB_ERR_OPERATIONS_ERROR; \
358 } while (0)
362 helper functions to call the next module in chain
365 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
367 switch (request->operation) {
368 case LDB_SEARCH:
369 FIND_OP(module, search);
370 return module->ops->search(module, request);
371 case LDB_ADD:
372 FIND_OP(module, add);
373 return module->ops->add(module, request);
374 case LDB_MODIFY:
375 FIND_OP(module, modify);
376 return module->ops->modify(module, request);
377 case LDB_DELETE:
378 FIND_OP(module, del);
379 return module->ops->del(module, request);
380 case LDB_RENAME:
381 FIND_OP(module, rename);
382 return module->ops->rename(module, request);
383 case LDB_SEQUENCE_NUMBER:
384 FIND_OP(module, sequence_number);
385 return module->ops->sequence_number(module, request);
386 default:
387 FIND_OP(module, request);
388 return module->ops->request(module, request);
392 int ldb_next_init(struct ldb_module *module)
394 /* init is different in that it is not an error if modules
395 * do not require initialization */
397 module = module->next;
399 while (module && module->ops->init_context == NULL)
400 module = module->next;
402 if (module == NULL)
403 return LDB_SUCCESS;
405 return module->ops->init_context(module);
408 int ldb_next_start_trans(struct ldb_module *module)
410 FIND_OP(module, start_transaction);
411 return module->ops->start_transaction(module);
414 int ldb_next_end_trans(struct ldb_module *module)
416 FIND_OP(module, end_transaction);
417 return module->ops->end_transaction(module);
420 int ldb_next_del_trans(struct ldb_module *module)
422 FIND_OP(module, del_transaction);
423 return module->ops->del_transaction(module);