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