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