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