r19507: Merge my DSO fixes branch. Building Samba's libraries as shared libraries
[Samba/aatanasov.git] / source / lib / ldb / common / ldb_modules.c
blobf1138afd81f85a14f286b3f238ce2ab97ae37d3d
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 #if (_SAMBA_BUILD_ >= 4)
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 *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
49 int i, len;
50 char *trimmed;
52 trimmed = talloc_strdup(mem_ctx, string);
53 if (!trimmed) {
54 return NULL;
57 len = strlen(trimmed);
58 for (i = 0; trimmed[i] != '\0'; i++) {
59 switch (trimmed[i]) {
60 case ' ':
61 case '\t':
62 case '\n':
63 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
64 break;
68 return trimmed;
72 /* modules are called in inverse order on the stack.
73 Lets place them as an admin would think the right order is.
74 Modules order is important */
75 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
77 char **modules = NULL;
78 const char **m;
79 char *modstr, *p;
80 int i;
82 /* spaces not admitted */
83 modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
84 if ( ! modstr) {
85 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
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 /* The str*r*chr walks backwards: This is how we get the inverse order mentioned above */
99 while ((p = strrchr(modstr, ',')) != NULL) {
100 *p = '\0';
101 p++;
102 modules[i] = p;
104 i++;
105 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
106 if ( ! modules ) {
107 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
108 return NULL;
112 modules[i] = modstr;
114 modules[i + 1] = NULL;
116 m = (const char **)modules;
118 return m;
121 static struct ops_list_entry {
122 const struct ldb_module_ops *ops;
123 struct ops_list_entry *next;
124 } *registered_modules = NULL;
126 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
128 struct ops_list_entry *e;
130 for (e = registered_modules; e; e = e->next) {
131 if (strcmp(e->ops->name, name) == 0)
132 return e->ops;
135 return NULL;
138 #ifndef STATIC_ldb_MODULES
140 #ifdef HAVE_LDB_LDAP
141 #define LDAP_INIT ldb_ldap_init,
142 #else
143 #define LDAP_INIT
144 #endif
146 #ifdef HAVE_LDB_SQLITE3
147 #define SQLITE3_INIT ldb_sqlite3_init,
148 #else
149 #define SQLITE3_INIT
150 #endif
152 #define STATIC_ldb_MODULES \
154 LDAP_INIT \
155 SQLITE3_INIT \
156 ldb_tdb_init, \
157 ldb_schema_init, \
158 ldb_operational_init, \
159 ldb_rdn_name_init, \
160 ldb_objectclass_init, \
161 ldb_paged_results_init, \
162 ldb_sort_init, \
163 ldb_asq_init, \
164 NULL \
166 #endif
168 int ldb_global_init(void)
170 static int (*static_init_fns[])(void) = STATIC_ldb_MODULES;
172 static int initialized = 0;
173 int ret = 0, i;
175 if (initialized)
176 return 0;
178 initialized = 1;
180 for (i = 0; static_init_fns[i]; i++) {
181 if (static_init_fns[i]() == -1)
182 ret = -1;
185 return ret;
188 int ldb_register_module(const struct ldb_module_ops *ops)
190 struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
192 if (ldb_find_module_ops(ops->name) != NULL)
193 return -1;
195 if (entry == NULL)
196 return -1;
198 entry->ops = ops;
199 entry->next = registered_modules;
200 registered_modules = entry;
202 return 0;
205 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
207 char *path;
208 void *handle;
209 int (*init_fn) (void);
210 char *modulesdir;
212 #ifdef HAVE_DLOPEN
213 if (getenv("LD_LDB_MODULE_PATH") != NULL) {
214 modulesdir = talloc_strdup(ldb, getenv("LD_LDB_MODULE_PATH"));
215 } else {
216 #ifdef _SAMBA_BUILD_
217 modulesdir = talloc_asprintf(ldb, "%s/ldb", dyn_MODULESDIR);
218 #else
219 modulesdir = talloc_strdup(ldb, MODULESDIR);
220 #endif
223 path = talloc_asprintf(ldb, "%s/%s.%s", modulesdir, name, SHLIBEXT);
225 talloc_free(modulesdir);
227 ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
229 handle = dlopen(path, RTLD_NOW);
230 if (handle == NULL) {
231 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
232 return -1;
235 init_fn = (int (*)(void))dlsym(handle, "init_module");
237 if (init_fn == NULL) {
238 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
239 return -1;
242 talloc_free(path);
244 return init_fn();
245 #else
246 ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
247 return -1;
248 #endif
251 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
253 struct ldb_module *module;
254 int i;
256 module = backend;
258 for (i = 0; module_list[i] != NULL; i++) {
259 struct ldb_module *current;
260 const struct ldb_module_ops *ops;
262 ops = ldb_find_module_ops(module_list[i]);
263 if (ops == NULL) {
264 if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
265 ops = ldb_find_module_ops(module_list[i]);
269 if (ops == NULL) {
270 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
271 module_list[i]);
272 continue;
275 current = talloc_zero(ldb, struct ldb_module);
276 if (current == NULL) {
277 return LDB_ERR_OPERATIONS_ERROR;
279 talloc_set_name(current, "ldb_module: %s", module_list[i]);
281 current->ldb = ldb;
282 current->ops = ops;
284 DLIST_ADD(module, current);
286 *out = module;
287 return LDB_SUCCESS;
290 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
292 while (module && module->ops->init_context == NULL)
293 module = module->next;
295 if (module && module->ops->init_context &&
296 module->ops->init_context(module) != LDB_SUCCESS) {
297 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
298 return LDB_ERR_OPERATIONS_ERROR;
301 return LDB_SUCCESS;
304 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
306 const char **modules = NULL;
307 int i;
308 int ret;
309 TALLOC_CTX *mem_ctx = talloc_new(ldb);
310 if (!mem_ctx) {
311 return LDB_ERR_OPERATIONS_ERROR;
314 /* find out which modules we are requested to activate */
316 /* check if we have a custom module list passd as ldb option */
317 if (options) {
318 for (i = 0; options[i] != NULL; i++) {
319 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
320 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
325 /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
326 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
327 const char * const attrs[] = { "@LIST" , NULL};
328 struct ldb_result *res = NULL;
329 struct ldb_dn *mods_dn;
331 mods_dn = ldb_dn_explode(mem_ctx, "@MODULES");
332 if (mods_dn == NULL) {
333 talloc_free(mem_ctx);
334 return -1;
337 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
338 talloc_steal(mods_dn, res);
339 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
340 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
341 } else {
342 if (ret != LDB_SUCCESS) {
343 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
344 talloc_free(mem_ctx);
345 return -1;
347 if (res->count > 1) {
348 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
349 talloc_free(mem_ctx);
350 return -1;
353 modules = ldb_modules_list_from_string(ldb, mem_ctx,
354 (const char *)res->msgs[0]->elements[0].values[0].data);
358 talloc_free(mods_dn);
361 if (modules != NULL) {
362 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
363 talloc_free(modules);
364 if (ret != LDB_SUCCESS) {
365 return ret;
367 } else {
368 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
371 return ldb_init_module_chain(ldb, ldb->modules);
375 by using this we allow ldb modules to only implement the functions they care about,
376 which makes writing a module simpler, and makes it more likely to keep working
377 when ldb is extended
379 #define FIND_OP(module, op) do { \
380 struct ldb_context *ldb = module->ldb; \
381 module = module->next; \
382 while (module && module->ops->op == NULL) module = module->next; \
383 if (module == NULL) { \
384 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
385 return LDB_ERR_OPERATIONS_ERROR; \
387 } while (0)
391 helper functions to call the next module in chain
394 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
396 switch (request->operation) {
397 case LDB_SEARCH:
398 FIND_OP(module, search);
399 return module->ops->search(module, request);
400 case LDB_ADD:
401 FIND_OP(module, add);
402 return module->ops->add(module, request);
403 case LDB_MODIFY:
404 FIND_OP(module, modify);
405 return module->ops->modify(module, request);
406 case LDB_DELETE:
407 FIND_OP(module, del);
408 return module->ops->del(module, request);
409 case LDB_RENAME:
410 FIND_OP(module, rename);
411 return module->ops->rename(module, request);
412 case LDB_SEQUENCE_NUMBER:
413 FIND_OP(module, sequence_number);
414 return module->ops->sequence_number(module, request);
415 default:
416 FIND_OP(module, request);
417 return module->ops->request(module, request);
421 int ldb_next_init(struct ldb_module *module)
423 /* init is different in that it is not an error if modules
424 * do not require initialization */
426 module = module->next;
428 while (module && module->ops->init_context == NULL)
429 module = module->next;
431 if (module == NULL)
432 return LDB_SUCCESS;
434 return module->ops->init_context(module);
437 int ldb_next_start_trans(struct ldb_module *module)
439 FIND_OP(module, start_transaction);
440 return module->ops->start_transaction(module);
443 int ldb_next_end_trans(struct ldb_module *module)
445 FIND_OP(module, end_transaction);
446 return module->ops->end_transaction(module);
449 int ldb_next_del_trans(struct ldb_module *module)
451 FIND_OP(module, del_transaction);
452 return module->ops->del_transaction(module);