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