r24261: Fix the standalone ldb build after I moved the objectclass module out.
[jelmer/samba4-debian.git] / source / lib / ldb / common / ldb_modules.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb modules core
28  *
29  *  Description: core modules routines
30  *
31  *  Author: Simo Sorce
32  */
33
34 #include "ldb_includes.h"
35
36 #if (_SAMBA_BUILD_ >= 4)
37 #include "includes.h"
38 #include "build.h"
39 #include "dynconfig.h"
40 #endif
41
42 #define LDB_MODULE_PREFIX       "modules:"
43 #define LDB_MODULE_PREFIX_LEN   8
44
45 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
46 {
47         int i, len;
48         char *trimmed;
49
50         trimmed = talloc_strdup(mem_ctx, string);
51         if (!trimmed) {
52                 return NULL;
53         }
54
55         len = strlen(trimmed);
56         for (i = 0; trimmed[i] != '\0'; i++) {
57                 switch (trimmed[i]) {
58                 case ' ':
59                 case '\t':
60                 case '\n':
61                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
62                         break;
63                 }
64         }
65
66         return trimmed;
67 }
68
69
70 /* modules are called in inverse order on the stack.
71    Lets place them as an admin would think the right order is.
72    Modules order is important */
73 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
74 {
75         char **modules = NULL;
76         const char **m;
77         char *modstr, *p;
78         int i;
79
80         /* spaces not admitted */
81         modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
82         if ( ! modstr) {
83                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
84                 return NULL;
85         }
86
87         modules = talloc_realloc(mem_ctx, modules, char *, 2);
88         if ( ! modules ) {
89                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
90                 talloc_free(modstr);
91                 return NULL;
92         }
93         talloc_steal(modules, modstr);
94
95         i = 0;
96         /* The str*r*chr walks backwards:  This is how we get the inverse order mentioned above */
97         while ((p = strrchr(modstr, ',')) != NULL) {
98                 *p = '\0';
99                 p++;
100                 modules[i] = p;
101
102                 i++;
103                 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
104                 if ( ! modules ) {
105                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
106                         return NULL;
107                 }
108
109         }
110         modules[i] = modstr;
111
112         modules[i + 1] = NULL;
113
114         m = (const char **)modules;
115
116         return m;
117 }
118
119 static struct ops_list_entry {
120         const struct ldb_module_ops *ops;
121         struct ops_list_entry *next;    
122 } *registered_modules = NULL;
123
124 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
125 {
126         struct ops_list_entry *e;
127  
128         for (e = registered_modules; e; e = e->next) {
129                 if (strcmp(e->ops->name, name) == 0) 
130                         return e->ops;
131         }
132
133         return NULL;
134 }
135
136 #ifndef STATIC_LIBLDB_MODULES
137
138 #ifdef HAVE_LDB_LDAP
139 #define LDAP_INIT ldb_ldap_init,
140 #else
141 #define LDAP_INIT
142 #endif
143
144 #ifdef HAVE_LDB_SQLITE3
145 #define SQLITE3_INIT ldb_sqlite3_init,
146 #else
147 #define SQLITE3_INIT
148 #endif
149
150 #define STATIC_LIBLDB_MODULES \
151         {       \
152                 LDAP_INIT \
153                 SQLITE3_INIT \
154                 ldb_tdb_init,   \
155                 ldb_operational_init,   \
156                 ldb_rdn_name_init,      \
157                 ldb_paged_results_init, \
158                 ldb_sort_init,          \
159                 ldb_asq_init,           \
160                 NULL                    \
161         }
162 #endif
163
164 int ldb_global_init(void)
165 {
166         static int (*static_init_fns[])(void) = STATIC_LIBLDB_MODULES;
167
168         static int initialized = 0;
169         int ret = 0, i;
170
171         if (initialized) 
172                 return 0;
173
174         initialized = 1;
175         
176         for (i = 0; static_init_fns[i]; i++) {
177                 if (static_init_fns[i]() == -1)
178                         ret = -1;
179         }
180
181         return ret;
182 }
183
184 int ldb_register_module(const struct ldb_module_ops *ops)
185 {
186         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
187
188         if (ldb_find_module_ops(ops->name) != NULL)
189                 return -1;
190
191         if (entry == NULL)
192                 return -1;
193
194         entry->ops = ops;
195         entry->next = registered_modules;
196         registered_modules = entry;
197
198         return 0;
199 }
200
201 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
202 {
203         char *path;
204         void *handle;
205         int (*init_fn) (void);
206         char *modulesdir;
207
208         if (getenv("LD_LDB_MODULE_PATH") != NULL) {
209                 modulesdir = talloc_strdup(ldb, getenv("LD_LDB_MODULE_PATH"));
210         } else {
211 #ifdef _SAMBA_BUILD_
212                 modulesdir = talloc_asprintf(ldb, "%s/ldb", dyn_MODULESDIR);
213 #else
214                 modulesdir = talloc_strdup(ldb, MODULESDIR);
215 #endif
216         }
217
218         path = talloc_asprintf(ldb, "%s/%s.%s", modulesdir, name, SHLIBEXT);
219
220         talloc_free(modulesdir);
221
222         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
223
224         handle = dlopen(path, RTLD_NOW);
225         if (handle == NULL) {
226                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
227                 return -1;
228         }
229
230         init_fn = (int (*)(void))dlsym(handle, "init_module");
231
232         if (init_fn == NULL) {
233                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
234                 return -1;
235         }
236
237         talloc_free(path);
238
239         return init_fn();
240 }
241
242 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
243 {
244         struct ldb_module *module;
245         int i;
246         
247         module = backend;
248
249         for (i = 0; module_list[i] != NULL; i++) {
250                 struct ldb_module *current;
251                 const struct ldb_module_ops *ops;
252                 
253                 ops = ldb_find_module_ops(module_list[i]);
254                 if (ops == NULL) {
255                         if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
256                                 ops = ldb_find_module_ops(module_list[i]);
257                         }
258                 }
259                 
260                 if (ops == NULL) {
261                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
262                                   module_list[i]);
263                         continue;
264                 }
265                 
266                 current = talloc_zero(ldb, struct ldb_module);
267                 if (current == NULL) {
268                         return LDB_ERR_OPERATIONS_ERROR;
269                 }
270                 talloc_set_name(current, "ldb_module: %s", module_list[i]);
271                 
272                 current->ldb = ldb;
273                 current->ops = ops;
274                 
275                 DLIST_ADD(module, current);
276         }
277         *out = module;
278         return LDB_SUCCESS;
279 }
280
281 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module) 
282 {
283         while (module && module->ops->init_context == NULL) 
284                 module = module->next;
285
286         if (module && module->ops->init_context &&
287                 module->ops->init_context(module) != LDB_SUCCESS) {
288                 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
289                 return LDB_ERR_OPERATIONS_ERROR;
290         }
291
292         return LDB_SUCCESS;
293 }
294
295 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
296 {
297         const char **modules = NULL;
298         int i;
299         int ret;
300         TALLOC_CTX *mem_ctx = talloc_new(ldb);
301         if (!mem_ctx) {
302                 return LDB_ERR_OPERATIONS_ERROR;
303         }
304
305         /* find out which modules we are requested to activate */
306
307         /* check if we have a custom module list passd as ldb option */
308         if (options) {
309                 for (i = 0; options[i] != NULL; i++) {
310                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
311                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
312                         }
313                 }
314         }
315
316         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
317         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
318                 const char * const attrs[] = { "@LIST" , NULL};
319                 struct ldb_result *res = NULL;
320                 struct ldb_dn *mods_dn;
321
322                 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
323                 if (mods_dn == NULL) {
324                         talloc_free(mem_ctx);
325                         return -1;
326                 }
327
328                 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
329                 talloc_steal(mods_dn, res);
330                 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
331                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
332                 } else {
333                         if (ret != LDB_SUCCESS) {
334                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
335                                 talloc_free(mem_ctx);
336                                 return -1;
337                         }
338                         if (res->count > 1) {
339                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
340                                 talloc_free(mem_ctx);
341                                 return -1;
342                         }
343
344                         modules = ldb_modules_list_from_string(ldb, mem_ctx,
345                                                                (const char *)res->msgs[0]->elements[0].values[0].data);
346
347                 }
348
349                 talloc_free(mods_dn);
350         }
351
352         if (modules != NULL) {
353                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
354                 talloc_free(modules);
355                 if (ret != LDB_SUCCESS) {
356                         return ret;
357                 }
358         } else {
359                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
360         }
361
362         return ldb_init_module_chain(ldb, ldb->modules);
363 }
364
365 /*
366   by using this we allow ldb modules to only implement the functions they care about,
367   which makes writing a module simpler, and makes it more likely to keep working
368   when ldb is extended
369 */
370 #define FIND_OP(module, op) do { \
371         struct ldb_context *ldb = module->ldb; \
372         module = module->next; \
373         while (module && module->ops->op == NULL) module = module->next; \
374         if (module == NULL) { \
375                 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
376                 return LDB_ERR_OPERATIONS_ERROR;        \
377         }                                               \
378 } while (0)
379
380
381 /*
382    helper functions to call the next module in chain
383 */
384
385 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
386 {
387         int ret;
388         switch (request->operation) {
389         case LDB_SEARCH:
390                 FIND_OP(module, search);
391                 ret = module->ops->search(module, request);
392                 break;
393         case LDB_ADD:
394                 FIND_OP(module, add);
395                 ret = module->ops->add(module, request);
396                 break;
397         case LDB_MODIFY:
398                 FIND_OP(module, modify);
399                 ret = module->ops->modify(module, request);
400                 break;
401         case LDB_DELETE:
402                 FIND_OP(module, del);
403                 ret = module->ops->del(module, request);
404                 break;
405         case LDB_RENAME:
406                 FIND_OP(module, rename);
407                 ret = module->ops->rename(module, request);
408                 break;
409         case LDB_EXTENDED:
410                 FIND_OP(module, extended);
411                 ret = module->ops->extended(module, request);
412                 break;
413         case LDB_SEQUENCE_NUMBER:
414                 FIND_OP(module, sequence_number);
415                 ret = module->ops->sequence_number(module, request);
416                 break;
417         default:
418                 FIND_OP(module, request);
419                 ret = module->ops->request(module, request);
420                 break;
421         }
422         if (ret == LDB_SUCCESS) {
423                 return ret;
424         }
425         if (!ldb_errstring(module->ldb)) {
426                 /* Set a default error string, to place the blame somewhere */
427                 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
428         }
429         return ret;
430 }
431
432 int ldb_next_init(struct ldb_module *module)
433 {
434         /* init is different in that it is not an error if modules
435          * do not require initialization */
436
437         module = module->next;
438
439         while (module && module->ops->init_context == NULL) 
440                 module = module->next;
441
442         if (module == NULL) 
443                 return LDB_SUCCESS;
444
445         return module->ops->init_context(module);
446 }
447
448 int ldb_next_start_trans(struct ldb_module *module)
449 {
450         FIND_OP(module, start_transaction);
451         return module->ops->start_transaction(module);
452 }
453
454 int ldb_next_end_trans(struct ldb_module *module)
455 {
456         FIND_OP(module, end_transaction);
457         return module->ops->end_transaction(module);
458 }
459
460 int ldb_next_del_trans(struct ldb_module *module)
461 {
462         FIND_OP(module, del_transaction);
463         return module->ops->del_transaction(module);
464 }