046d36edf184d6418f46f1ead449a1a667a27c05
[ira/wip.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 #endif
39
40 #define LDB_MODULE_PREFIX       "modules:"
41 #define LDB_MODULE_PREFIX_LEN   8
42
43 void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
44 {
45         talloc_free(ldb->modules_dir);
46         ldb->modules_dir = talloc_strdup(ldb, path);
47 }
48
49 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
50 {
51         int i, len;
52         char *trimmed;
53
54         trimmed = talloc_strdup(mem_ctx, string);
55         if (!trimmed) {
56                 return NULL;
57         }
58
59         len = strlen(trimmed);
60         for (i = 0; trimmed[i] != '\0'; i++) {
61                 switch (trimmed[i]) {
62                 case ' ':
63                 case '\t':
64                 case '\n':
65                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
66                         break;
67                 }
68         }
69
70         return trimmed;
71 }
72
73
74 /* modules are called in inverse order on the stack.
75    Lets place them as an admin would think the right order is.
76    Modules order is important */
77 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
78 {
79         char **modules = NULL;
80         const char **m;
81         char *modstr, *p;
82         int i;
83
84         /* spaces not admitted */
85         modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
86         if ( ! modstr) {
87                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
88                 return NULL;
89         }
90
91         modules = talloc_realloc(mem_ctx, modules, char *, 2);
92         if ( ! modules ) {
93                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
94                 talloc_free(modstr);
95                 return NULL;
96         }
97         talloc_steal(modules, modstr);
98
99         i = 0;
100         /* The str*r*chr walks backwards:  This is how we get the inverse order mentioned above */
101         while ((p = strrchr(modstr, ',')) != NULL) {
102                 *p = '\0';
103                 p++;
104                 modules[i] = p;
105
106                 i++;
107                 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
108                 if ( ! modules ) {
109                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
110                         return NULL;
111                 }
112
113         }
114         modules[i] = modstr;
115
116         modules[i + 1] = NULL;
117
118         m = (const char **)modules;
119
120         return m;
121 }
122
123 static struct backends_list_entry {
124         struct ldb_backend_ops *ops;
125         struct backends_list_entry *prev, *next;
126 } *ldb_backends = NULL;
127
128 static struct ops_list_entry {
129         const struct ldb_module_ops *ops;
130         struct ops_list_entry *next;    
131 } *registered_modules = NULL;
132
133 static const struct ldb_builtins {
134         const struct ldb_backend_ops *backend_ops;
135         const struct ldb_module_ops *module_ops;
136 } builtins[];
137
138 static ldb_connect_fn ldb_find_backend(const char *url)
139 {
140         struct backends_list_entry *backend;
141         int i;
142
143         for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
144                 if (builtins[i].backend_ops == NULL) continue;
145
146                 if (strncmp(builtins[i].backend_ops->name, url,
147                             strlen(builtins[i].backend_ops->name)) == 0) {
148                         return builtins[i].backend_ops->connect_fn;
149                 }
150         }
151
152         for (backend = ldb_backends; backend; backend = backend->next) {
153                 if (strncmp(backend->ops->name, url,
154                             strlen(backend->ops->name)) == 0) {
155                         return backend->ops->connect_fn;
156                 }
157         }
158
159         return NULL;
160 }
161
162 /*
163  register a new ldb backend
164 */
165 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
166 {
167         struct ldb_backend_ops *backend;
168         struct backends_list_entry *entry;
169
170         backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
171         if (!backend) return LDB_ERR_OPERATIONS_ERROR;
172
173         entry = talloc(talloc_autofree_context(), struct backends_list_entry);
174         if (!entry) {
175                 talloc_free(backend);
176                 return LDB_ERR_OPERATIONS_ERROR;
177         }
178
179         if (ldb_find_backend(url_prefix)) {
180                 return LDB_SUCCESS;
181         }
182
183         /* Maybe check for duplicity here later on? */
184
185         backend->name = talloc_strdup(backend, url_prefix);
186         backend->connect_fn = connectfn;
187         entry->ops = backend;
188         DLIST_ADD(ldb_backends, entry);
189
190         return LDB_SUCCESS;
191 }
192
193 /*
194    Return the ldb module form of a database.
195    The URL can either be one of the following forms
196    ldb://path
197    ldapi://path
198
199    flags is made up of LDB_FLG_*
200
201    the options are passed uninterpreted to the backend, and are
202    backend specific.
203
204    This allows modules to get at only the backend module, for example where a
205    module may wish to direct certain requests at a particular backend.
206 */
207 int ldb_connect_backend(struct ldb_context *ldb,
208                         const char *url,
209                         const char *options[],
210                         struct ldb_module **backend_module)
211 {
212         int ret;
213         char *backend;
214         ldb_connect_fn fn;
215
216         if (strchr(url, ':') != NULL) {
217                 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
218         } else {
219                 /* Default to tdb */
220                 backend = talloc_strdup(ldb, "tdb");
221         }
222
223         fn = ldb_find_backend(backend);
224
225         if (fn == NULL) {
226                 struct ldb_backend_ops *ops;
227                 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
228                 if (symbol_name == NULL) {
229                         return LDB_ERR_OPERATIONS_ERROR;
230                 }
231                 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
232                 if (ops != NULL) {
233                         fn = ops->connect_fn;
234                 }
235                 talloc_free(symbol_name);
236         }
237
238         talloc_free(backend);
239
240         if (fn == NULL) {
241                 ldb_debug(ldb, LDB_DEBUG_FATAL,
242                           "Unable to find backend for '%s'\n", url);
243                 return LDB_ERR_OTHER;
244         }
245
246         ret = fn(ldb, url, ldb->flags, options, backend_module);
247
248         if (ret != LDB_SUCCESS) {
249                 ldb_debug(ldb, LDB_DEBUG_ERROR,
250                           "Failed to connect to '%s'\n", url);
251                 return ret;
252         }
253         return ret;
254 }
255
256 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
257 {
258         struct ops_list_entry *e;
259         int i;
260
261         for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
262                 if (builtins[i].module_ops == NULL) continue;
263
264                 if (strcmp(builtins[i].module_ops->name, name) == 0)
265                         return builtins[i].module_ops;
266         }
267  
268         for (e = registered_modules; e; e = e->next) {
269                 if (strcmp(e->ops->name, name) == 0) 
270                         return e->ops;
271         }
272
273         return NULL;
274 }
275
276
277 int ldb_register_module(const struct ldb_module_ops *ops)
278 {
279         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
280
281         if (ldb_find_module_ops(ops->name) != NULL)
282                 return LDB_ERR_OPERATIONS_ERROR;
283
284         if (entry == NULL)
285                 return LDB_ERR_OPERATIONS_ERROR;
286
287         entry->ops = ops;
288         entry->next = registered_modules;
289         registered_modules = entry;
290
291         return LDB_SUCCESS;
292 }
293
294 void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
295                             const char *symbol)
296 {
297         char *path;
298         void *handle;
299         void *sym;
300
301         if (ldb->modules_dir == NULL)
302                 return NULL;
303
304         path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name, 
305                                SHLIBEXT);
306
307         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
308
309         handle = dlopen(path, RTLD_NOW);
310         if (handle == NULL) {
311                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
312                 return NULL;
313         }
314
315         sym = (int (*)(void))dlsym(handle, symbol);
316
317         if (sym == NULL) {
318                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s\n", symbol, path, dlerror());
319                 return NULL;
320         }
321
322         talloc_free(path);
323
324         return sym;
325 }
326
327 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
328 {
329         struct ldb_module *module;
330         int i;
331         
332         module = backend;
333
334         for (i = 0; module_list[i] != NULL; i++) {
335                 struct ldb_module *current;
336                 const struct ldb_module_ops *ops;
337                 
338                 ops = ldb_find_module_ops(module_list[i]);
339                 if (ops == NULL) {
340                         char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops", 
341                                                                                                 module_list[i]);
342                         if (symbol_name == NULL) {
343                                 return LDB_ERR_OPERATIONS_ERROR;
344                         }
345                         ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
346                         talloc_free(symbol_name);
347                 }
348                 
349                 if (ops == NULL) {
350                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
351                                   module_list[i]);
352                         continue;
353                 }
354                 
355                 current = talloc_zero(ldb, struct ldb_module);
356                 if (current == NULL) {
357                         return LDB_ERR_OPERATIONS_ERROR;
358                 }
359                 talloc_set_name(current, "ldb_module: %s", module_list[i]);
360                 
361                 current->ldb = ldb;
362                 current->ops = ops;
363                 
364                 DLIST_ADD(module, current);
365         }
366         *out = module;
367         return LDB_SUCCESS;
368 }
369
370 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module) 
371 {
372         while (module && module->ops->init_context == NULL) 
373                 module = module->next;
374
375         /* init is different in that it is not an error if modules
376          * do not require initialization */
377
378         if (module) {
379                 int ret = module->ops->init_context(module);
380                 if (ret != LDB_SUCCESS) {
381                         ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed\n", module->ops->name);
382                         return ret;
383                 }
384         }
385
386         return LDB_SUCCESS;
387 }
388
389 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
390 {
391         const char **modules = NULL;
392         int i;
393         int ret;
394         TALLOC_CTX *mem_ctx = talloc_new(ldb);
395         if (!mem_ctx) {
396                 return LDB_ERR_OPERATIONS_ERROR;
397         }
398
399         /* find out which modules we are requested to activate */
400
401         /* check if we have a custom module list passd as ldb option */
402         if (options) {
403                 for (i = 0; options[i] != NULL; i++) {
404                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
405                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
406                         }
407                 }
408         }
409
410         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
411         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
412                 const char * const attrs[] = { "@LIST" , NULL};
413                 struct ldb_result *res = NULL;
414                 struct ldb_dn *mods_dn;
415
416                 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
417                 if (mods_dn == NULL) {
418                         talloc_free(mem_ctx);
419                         return -1;
420                 }
421
422                 ret = ldb_search(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
423                 
424                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
425                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
426                 } else if (ret != LDB_SUCCESS) {
427                         ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
428                         talloc_free(mem_ctx);
429                         return ret;
430                 } else {
431                         const char *module_list;
432                         if (res->count == 0) {
433                                 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
434                         } else if (res->count > 1) {
435                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
436                                 talloc_free(mem_ctx);
437                                 return -1;
438                         } else {
439                                 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
440                                 if (!module_list) {
441                                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
442                                 }
443                                 modules = ldb_modules_list_from_string(ldb, mem_ctx,
444                                                                module_list);
445                         }
446                 }
447
448                 talloc_free(mods_dn);
449         }
450
451         if (modules != NULL) {
452                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
453                 if (ret != LDB_SUCCESS) {
454                         talloc_free(mem_ctx);
455                         return ret;
456                 }
457         } else {
458                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
459         }
460
461         ret = ldb_init_module_chain(ldb, ldb->modules);
462         talloc_free(mem_ctx);
463         return ret;
464 }
465
466 /*
467   by using this we allow ldb modules to only implement the functions they care about,
468   which makes writing a module simpler, and makes it more likely to keep working
469   when ldb is extended
470 */
471 #define FIND_OP(module, op) do { \
472         struct ldb_context *ldb = module->ldb; \
473         module = module->next; \
474         while (module && module->ops->op == NULL) module = module->next; \
475         if (module == NULL) { \
476                 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
477                 return LDB_ERR_OPERATIONS_ERROR;        \
478         }                                               \
479 } while (0)
480
481
482 /*
483    helper functions to call the next module in chain
484 */
485
486 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
487 {
488         int ret;
489         switch (request->operation) {
490         case LDB_SEARCH:
491                 FIND_OP(module, search);
492                 ret = module->ops->search(module, request);
493                 break;
494         case LDB_ADD:
495                 FIND_OP(module, add);
496                 ret = module->ops->add(module, request);
497                 break;
498         case LDB_MODIFY:
499                 FIND_OP(module, modify);
500                 ret = module->ops->modify(module, request);
501                 break;
502         case LDB_DELETE:
503                 FIND_OP(module, del);
504                 ret = module->ops->del(module, request);
505                 break;
506         case LDB_RENAME:
507                 FIND_OP(module, rename);
508                 ret = module->ops->rename(module, request);
509                 break;
510         case LDB_EXTENDED:
511                 FIND_OP(module, extended);
512                 ret = module->ops->extended(module, request);
513                 break;
514         case LDB_SEQUENCE_NUMBER:
515                 FIND_OP(module, sequence_number);
516                 ret = module->ops->sequence_number(module, request);
517                 break;
518         default:
519                 FIND_OP(module, request);
520                 ret = module->ops->request(module, request);
521                 break;
522         }
523         if (ret == LDB_SUCCESS) {
524                 return ret;
525         }
526         if (!ldb_errstring(module->ldb)) {
527                 /* Set a default error string, to place the blame somewhere */
528                 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
529         }
530         return ret;
531 }
532
533 int ldb_next_init(struct ldb_module *module)
534 {
535         module = module->next;
536
537         return ldb_init_module_chain(module->ldb, module);
538 }
539
540 int ldb_next_start_trans(struct ldb_module *module)
541 {
542         FIND_OP(module, start_transaction);
543         return module->ops->start_transaction(module);
544 }
545
546 int ldb_next_end_trans(struct ldb_module *module)
547 {
548         FIND_OP(module, end_transaction);
549         return module->ops->end_transaction(module);
550 }
551
552 int ldb_next_del_trans(struct ldb_module *module)
553 {
554         FIND_OP(module, del_transaction);
555         return module->ops->del_transaction(module);
556 }
557
558 #ifndef STATIC_LIBLDB_MODULES
559
560 #ifdef HAVE_LDB_LDAP
561 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
562 #else
563 #define LDAP_BACKEND
564 #endif
565
566 #ifdef HAVE_LDB_SQLITE3
567 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
568 #else
569 #define SQLITE3_BACKEND
570 #endif
571
572 #define STATIC_LIBLDB_MODULES \
573         LDB_BACKEND(tdb),       \
574         LDAP_BACKEND    \
575         SQLITE3_BACKEND \
576         LDB_MODULE(operational),        \
577         LDB_MODULE(rdn_name),   \
578         LDB_MODULE(paged_results),      \
579         LDB_MODULE(server_sort),                \
580         LDB_MODULE(asq), \
581         NULL
582 #endif
583
584 /*
585  * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
586  * between the elements and we want to autogenerate the
587  * extern struct declarations, so we do some hacks and let the
588  * ',' appear in an unused function prototype.
589  */
590 #undef NULL
591 #define NULL LDB_MODULE(NULL),
592
593 #define LDB_BACKEND(name) \
594         int); \
595         extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
596         extern void ldb_noop ## name (int
597 #define LDB_MODULE(name) \
598         int); \
599         extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
600         extern void ldb_noop ## name (int
601
602 extern void ldb_start_noop(int,
603 STATIC_LIBLDB_MODULES
604 int);
605
606 #undef NULL
607 #define NULL { \
608         .backend_ops = (void *)0, \
609         .module_ops = (void *)0 \
610 }
611
612 #undef LDB_BACKEND
613 #define LDB_BACKEND(name) { \
614         .backend_ops = &ldb_ ## name ## _backend_ops, \
615         .module_ops = (void *)0 \
616 }
617 #undef LDB_MODULE
618 #define LDB_MODULE(name) { \
619         .backend_ops = (void *)0, \
620         .module_ops = &ldb_ ## name ## _module_ops \
621 }
622
623 static const struct ldb_builtins builtins[] = {
624         STATIC_LIBLDB_MODULES
625 };