Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[jelmer/samba4-debian.git] / source / 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 #ifndef STATIC_LIBLDB_MODULES
130
131 #define STATIC_LIBLDB_MODULES \
132         ldb_operational_module_ops,     \
133         ldb_rdn_name_module_ops,        \
134         ldb_paged_results_module_ops,   \
135         ldb_sort_module_ops,            \
136         ldb_asq_module_ops, \
137         NULL
138 #endif
139
140 const static struct ldb_module_ops *builtin_modules[] = {
141         STATIC_LIBLDB_MODULES
142 };
143
144 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
145 {
146         struct ops_list_entry *e;
147         int i;
148
149         for (i = 0; builtin_modules[i]; i++) {
150                 if (strcmp(builtin_modules[i]->name, name) == 0)
151                         return builtin_modules[i];
152         }
153  
154         for (e = registered_modules; e; e = e->next) {
155                 if (strcmp(e->ops->name, name) == 0) 
156                         return e->ops;
157         }
158
159         return NULL;
160 }
161
162
163 int ldb_register_module(const struct ldb_module_ops *ops)
164 {
165         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
166
167         if (ldb_find_module_ops(ops->name) != NULL)
168                 return -1;
169
170         if (entry == NULL)
171                 return -1;
172
173         entry->ops = ops;
174         entry->next = registered_modules;
175         registered_modules = entry;
176
177         return 0;
178 }
179
180 void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
181                             const char *symbol)
182 {
183         char *path;
184         void *handle;
185         void *sym;
186
187         if (ldb->modules_dir == NULL)
188                 return NULL;
189
190         path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name, 
191                                SHLIBEXT);
192
193         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
194
195         handle = dlopen(path, RTLD_NOW);
196         if (handle == NULL) {
197                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
198                 return NULL;
199         }
200
201         sym = (int (*)(void))dlsym(handle, symbol);
202
203         if (sym == NULL) {
204                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s\n", symbol, path, dlerror());
205                 return NULL;
206         }
207
208         talloc_free(path);
209
210         return sym;
211 }
212
213 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
214 {
215         struct ldb_module *module;
216         int i;
217         
218         module = backend;
219
220         for (i = 0; module_list[i] != NULL; i++) {
221                 struct ldb_module *current;
222                 const struct ldb_module_ops *ops;
223                 
224                 ops = ldb_find_module_ops(module_list[i]);
225                 if (ops == NULL) {
226                         int (*init_fn) (void);
227
228                         init_fn = ldb_dso_load_symbol(ldb, module_list[i], 
229                                                       "init_module");
230                         if (init_fn != NULL && init_fn() == 0) {
231                                 ops = ldb_find_module_ops(module_list[i]);
232                         }
233                 }
234
235                 if (ops == NULL) {
236                         char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops", 
237                                                                                                 module_list[i]);
238                         if (symbol_name == NULL) {
239                                 return LDB_ERR_OPERATIONS_ERROR;
240                         }
241                         ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
242                         talloc_free(symbol_name);
243                 }
244                 
245                 if (ops == NULL) {
246                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
247                                   module_list[i]);
248                         continue;
249                 }
250                 
251                 current = talloc_zero(ldb, struct ldb_module);
252                 if (current == NULL) {
253                         return LDB_ERR_OPERATIONS_ERROR;
254                 }
255                 talloc_set_name(current, "ldb_module: %s", module_list[i]);
256                 
257                 current->ldb = ldb;
258                 current->ops = ops;
259                 
260                 DLIST_ADD(module, current);
261         }
262         *out = module;
263         return LDB_SUCCESS;
264 }
265
266 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module) 
267 {
268         while (module && module->ops->init_context == NULL) 
269                 module = module->next;
270
271         /* init is different in that it is not an error if modules
272          * do not require initialization */
273
274         if (module) {
275                 int ret = module->ops->init_context(module);
276                 if (ret != LDB_SUCCESS) {
277                         ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed\n", module->ops->name);
278                         return ret;
279                 }
280         }
281
282         return LDB_SUCCESS;
283 }
284
285 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
286 {
287         const char **modules = NULL;
288         int i;
289         int ret;
290         TALLOC_CTX *mem_ctx = talloc_new(ldb);
291         if (!mem_ctx) {
292                 return LDB_ERR_OPERATIONS_ERROR;
293         }
294
295         /* find out which modules we are requested to activate */
296
297         /* check if we have a custom module list passd as ldb option */
298         if (options) {
299                 for (i = 0; options[i] != NULL; i++) {
300                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
301                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
302                         }
303                 }
304         }
305
306         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
307         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
308                 const char * const attrs[] = { "@LIST" , NULL};
309                 struct ldb_result *res = NULL;
310                 struct ldb_dn *mods_dn;
311
312                 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
313                 if (mods_dn == NULL) {
314                         talloc_free(mem_ctx);
315                         return -1;
316                 }
317
318                 ret = ldb_search_exp_fmt(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
319                 
320                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
321                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
322                 } else if (ret != LDB_SUCCESS) {
323                         ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
324                         talloc_free(mem_ctx);
325                         return ret;
326                 } else {
327                         const char *module_list;
328                         if (res->count == 0) {
329                                 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
330                         } else if (res->count > 1) {
331                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
332                                 talloc_free(mem_ctx);
333                                 return -1;
334                         } else {
335                                 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
336                                 if (!module_list) {
337                                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
338                                 }
339                                 modules = ldb_modules_list_from_string(ldb, mem_ctx,
340                                                                module_list);
341                         }
342                 }
343
344                 talloc_free(mods_dn);
345         }
346
347         if (modules != NULL) {
348                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
349                 if (ret != LDB_SUCCESS) {
350                         talloc_free(mem_ctx);
351                         return ret;
352                 }
353         } else {
354                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
355         }
356
357         ret = ldb_init_module_chain(ldb, ldb->modules);
358         talloc_free(mem_ctx);
359         return ret;
360 }
361
362 /*
363   by using this we allow ldb modules to only implement the functions they care about,
364   which makes writing a module simpler, and makes it more likely to keep working
365   when ldb is extended
366 */
367 #define FIND_OP(module, op) do { \
368         struct ldb_context *ldb = module->ldb; \
369         module = module->next; \
370         while (module && module->ops->op == NULL) module = module->next; \
371         if (module == NULL) { \
372                 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
373                 return LDB_ERR_OPERATIONS_ERROR;        \
374         }                                               \
375 } while (0)
376
377
378 /*
379    helper functions to call the next module in chain
380 */
381
382 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
383 {
384         int ret;
385         switch (request->operation) {
386         case LDB_SEARCH:
387                 FIND_OP(module, search);
388                 ret = module->ops->search(module, request);
389                 break;
390         case LDB_ADD:
391                 FIND_OP(module, add);
392                 ret = module->ops->add(module, request);
393                 break;
394         case LDB_MODIFY:
395                 FIND_OP(module, modify);
396                 ret = module->ops->modify(module, request);
397                 break;
398         case LDB_DELETE:
399                 FIND_OP(module, del);
400                 ret = module->ops->del(module, request);
401                 break;
402         case LDB_RENAME:
403                 FIND_OP(module, rename);
404                 ret = module->ops->rename(module, request);
405                 break;
406         case LDB_EXTENDED:
407                 FIND_OP(module, extended);
408                 ret = module->ops->extended(module, request);
409                 break;
410         case LDB_SEQUENCE_NUMBER:
411                 FIND_OP(module, sequence_number);
412                 ret = module->ops->sequence_number(module, request);
413                 break;
414         default:
415                 FIND_OP(module, request);
416                 ret = module->ops->request(module, request);
417                 break;
418         }
419         if (ret == LDB_SUCCESS) {
420                 return ret;
421         }
422         if (!ldb_errstring(module->ldb)) {
423                 /* Set a default error string, to place the blame somewhere */
424                 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
425         }
426         return ret;
427 }
428
429 int ldb_next_init(struct ldb_module *module)
430 {
431         module = module->next;
432
433         return ldb_init_module_chain(module->ldb, module);
434 }
435
436 int ldb_next_start_trans(struct ldb_module *module)
437 {
438         FIND_OP(module, start_transaction);
439         return module->ops->start_transaction(module);
440 }
441
442 int ldb_next_end_trans(struct ldb_module *module)
443 {
444         FIND_OP(module, end_transaction);
445         return module->ops->end_transaction(module);
446 }
447
448 int ldb_next_del_trans(struct ldb_module *module)
449 {
450         FIND_OP(module, del_transaction);
451         return module->ops->del_transaction(module);
452 }