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