Merge branch 'v4-0-trivial' into v4-0-gmake3
[mat/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 #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 ops_list_entry {
124         const struct ldb_module_ops *ops;
125         struct ops_list_entry *next;    
126 } *registered_modules = NULL;
127
128 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
129 {
130         struct ops_list_entry *e;
131  
132         for (e = registered_modules; e; e = e->next) {
133                 if (strcmp(e->ops->name, name) == 0) 
134                         return e->ops;
135         }
136
137         return NULL;
138 }
139
140 #ifndef STATIC_LIBLDB_MODULES
141
142 #ifdef HAVE_LDB_LDAP
143 #define LDAP_INIT ldb_ldap_init,
144 #else
145 #define LDAP_INIT
146 #endif
147
148 #ifdef HAVE_LDB_SQLITE3
149 #define SQLITE3_INIT ldb_sqlite3_init,
150 #else
151 #define SQLITE3_INIT
152 #endif
153
154 #define STATIC_LIBLDB_MODULES \
155         LDAP_INIT \
156         SQLITE3_INIT \
157         ldb_tdb_init,   \
158         ldb_operational_init,   \
159         ldb_rdn_name_init,      \
160         ldb_paged_results_init, \
161         ldb_sort_init,          \
162         ldb_asq_init, \
163         NULL
164 #endif
165
166 int ldb_global_init(void)
167 {
168         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 void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
204                             const char *symbol)
205 {
206         char *path;
207         void *handle;
208         void *sym;
209
210         if (ldb->modules_dir == NULL)
211                 return NULL;
212
213         path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name, 
214                                SHLIBEXT);
215
216         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
217
218         handle = dlopen(path, RTLD_NOW);
219         if (handle == NULL) {
220                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
221                 return NULL;
222         }
223
224         sym = (int (*)(void))dlsym(handle, symbol);
225
226         if (sym == NULL) {
227                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s\n", symbol, path, dlerror());
228                 return NULL;
229         }
230
231         talloc_free(path);
232
233         return sym;
234 }
235
236 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
237 {
238         struct ldb_module *module;
239         int i;
240         
241         module = backend;
242
243         for (i = 0; module_list[i] != NULL; i++) {
244                 struct ldb_module *current;
245                 const struct ldb_module_ops *ops;
246                 
247                 ops = ldb_find_module_ops(module_list[i]);
248                 if (ops == NULL) {
249                         int (*init_fn) (void);
250
251                         init_fn = ldb_dso_load_symbol(ldb, module_list[i], 
252                                                       "init_module");
253                         if (init_fn != NULL && init_fn() == 0) {
254                                 ops = ldb_find_module_ops(module_list[i]);
255                         }
256                 }
257
258                 if (ops == NULL) {
259                         ops = ldb_dso_load_symbol(ldb, module_list[i], 
260                                                       "ldb_module_ops");
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         /* init is different in that it is not an error if modules
290          * do not require initialization */
291
292         if (module) {
293                 int ret = module->ops->init_context(module);
294                 if (ret != LDB_SUCCESS) {
295                         ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed\n", module->ops->name);
296                         return ret;
297                 }
298         }
299
300         return LDB_SUCCESS;
301 }
302
303 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
304 {
305         const char **modules = NULL;
306         int i;
307         int ret;
308         TALLOC_CTX *mem_ctx = talloc_new(ldb);
309         if (!mem_ctx) {
310                 return LDB_ERR_OPERATIONS_ERROR;
311         }
312
313         /* find out which modules we are requested to activate */
314
315         /* check if we have a custom module list passd as ldb option */
316         if (options) {
317                 for (i = 0; options[i] != NULL; i++) {
318                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
319                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
320                         }
321                 }
322         }
323
324         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
325         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
326                 const char * const attrs[] = { "@LIST" , NULL};
327                 struct ldb_result *res = NULL;
328                 struct ldb_dn *mods_dn;
329
330                 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
331                 if (mods_dn == NULL) {
332                         talloc_free(mem_ctx);
333                         return -1;
334                 }
335
336                 ret = ldb_search_exp_fmt(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
337                 
338                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
339                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
340                 } else if (ret != LDB_SUCCESS) {
341                         ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
342                         talloc_free(mem_ctx);
343                         return ret;
344                 } else {
345                         const char *module_list;
346                         if (res->count == 0) {
347                                 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
348                         } else if (res->count > 1) {
349                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
350                                 talloc_free(mem_ctx);
351                                 return -1;
352                         } else {
353                                 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
354                                 if (!module_list) {
355                                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
356                                 }
357                                 modules = ldb_modules_list_from_string(ldb, mem_ctx,
358                                                                module_list);
359                         }
360                 }
361
362                 talloc_free(mods_dn);
363         }
364
365         if (modules != NULL) {
366                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
367                 if (ret != LDB_SUCCESS) {
368                         talloc_free(mem_ctx);
369                         return ret;
370                 }
371         } else {
372                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
373         }
374
375         ret = ldb_init_module_chain(ldb, ldb->modules);
376         talloc_free(mem_ctx);
377         return ret;
378 }
379
380 /*
381   by using this we allow ldb modules to only implement the functions they care about,
382   which makes writing a module simpler, and makes it more likely to keep working
383   when ldb is extended
384 */
385 #define FIND_OP(module, op) do { \
386         struct ldb_context *ldb = module->ldb; \
387         module = module->next; \
388         while (module && module->ops->op == NULL) module = module->next; \
389         if (module == NULL) { \
390                 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
391                 return LDB_ERR_OPERATIONS_ERROR;        \
392         }                                               \
393 } while (0)
394
395
396 /*
397    helper functions to call the next module in chain
398 */
399
400 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
401 {
402         int ret;
403         switch (request->operation) {
404         case LDB_SEARCH:
405                 FIND_OP(module, search);
406                 ret = module->ops->search(module, request);
407                 break;
408         case LDB_ADD:
409                 FIND_OP(module, add);
410                 ret = module->ops->add(module, request);
411                 break;
412         case LDB_MODIFY:
413                 FIND_OP(module, modify);
414                 ret = module->ops->modify(module, request);
415                 break;
416         case LDB_DELETE:
417                 FIND_OP(module, del);
418                 ret = module->ops->del(module, request);
419                 break;
420         case LDB_RENAME:
421                 FIND_OP(module, rename);
422                 ret = module->ops->rename(module, request);
423                 break;
424         case LDB_EXTENDED:
425                 FIND_OP(module, extended);
426                 ret = module->ops->extended(module, request);
427                 break;
428         case LDB_SEQUENCE_NUMBER:
429                 FIND_OP(module, sequence_number);
430                 ret = module->ops->sequence_number(module, request);
431                 break;
432         default:
433                 FIND_OP(module, request);
434                 ret = module->ops->request(module, request);
435                 break;
436         }
437         if (ret == LDB_SUCCESS) {
438                 return ret;
439         }
440         if (!ldb_errstring(module->ldb)) {
441                 /* Set a default error string, to place the blame somewhere */
442                 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
443         }
444         return ret;
445 }
446
447 int ldb_next_init(struct ldb_module *module)
448 {
449         module = module->next;
450
451         return ldb_init_module_chain(module->ldb, module);
452 }
453
454 int ldb_next_start_trans(struct ldb_module *module)
455 {
456         FIND_OP(module, start_transaction);
457         return module->ops->start_transaction(module);
458 }
459
460 int ldb_next_end_trans(struct ldb_module *module)
461 {
462         FIND_OP(module, end_transaction);
463         return module->ops->end_transaction(module);
464 }
465
466 int ldb_next_del_trans(struct ldb_module *module)
467 {
468         FIND_OP(module, del_transaction);
469         return module->ops->del_transaction(module);
470 }