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