r17730: cast dlsym result to try to avoid a compiler crash on hpux
[abartlet/samba.git/.git] / source4 / lib / ldb / common / ldb_modules.c
1
2 /* 
3    ldb database library
4
5    Copyright (C) Simo Sorce  2004
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with this library; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb modules core
30  *
31  *  Description: core modules routines
32  *
33  *  Author: Simo Sorce
34  */
35
36 #include "includes.h"
37 #include "ldb/include/includes.h"
38
39 #ifdef _SAMBA_BUILD_
40 #include "build.h"
41 #include "dynconfig.h"
42 #endif
43
44 #define LDB_MODULE_PREFIX       "modules:"
45 #define LDB_MODULE_PREFIX_LEN   8
46
47 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
48 {
49         int i, len;
50         char *trimmed;
51
52         trimmed = talloc_strdup(ldb, string);
53         if (!trimmed) {
54                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
55                 return NULL;
56         }
57
58         len = strlen(trimmed);
59         for (i = 0; trimmed[i] != '\0'; i++) {
60                 switch (trimmed[i]) {
61                 case ' ':
62                 case '\t':
63                 case '\n':
64                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
65                         break;
66                 }
67         }
68
69         return trimmed;
70 }
71
72
73 /* modules are called in inverse order on the stack.
74    Lets place them as an admin would think the right order is.
75    Modules order is important */
76 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
77 {
78         char **modules = NULL;
79         const char **m;
80         char *modstr, *p;
81         int i;
82
83         /* spaces not admitted */
84         modstr = talloc_strdup_no_spaces(mem_ctx, string);
85         if ( ! modstr) {
86                 return NULL;
87         }
88
89         modules = talloc_realloc(mem_ctx, modules, char *, 2);
90         if ( ! modules ) {
91                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
92                 talloc_free(modstr);
93                 return NULL;
94         }
95         talloc_steal(modules, modstr);
96
97         i = 0;
98         /* The str*r*chr walks backwards:  This is how we get the inverse order mentioned above */
99         while ((p = strrchr(modstr, ',')) != NULL) {
100                 *p = '\0';
101                 p++;
102                 modules[i] = p;
103
104                 i++;
105                 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
106                 if ( ! modules ) {
107                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
108                         return NULL;
109                 }
110
111         }
112         modules[i] = modstr;
113
114         modules[i + 1] = NULL;
115
116         m = (const char **)modules;
117
118         return m;
119 }
120
121 static struct ops_list_entry {
122         const struct ldb_module_ops *ops;
123         struct ops_list_entry *next;    
124 } *registered_modules = NULL;
125
126 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
127 {
128         struct ops_list_entry *e;
129  
130         for (e = registered_modules; e; e = e->next) {
131                 if (strcmp(e->ops->name, name) == 0) 
132                         return e->ops;
133         }
134
135         return NULL;
136 }
137
138 #ifdef HAVE_LDAP
139 #define LDAP_INIT ldb_ldap_init,
140 #else
141 #define LDAP_INIT
142 #endif
143
144 #ifdef HAVE_SQLITE3
145 #define SQLITE3_INIT ldb_sqlite3_init,
146 #else
147 #define SQLITE3_INIT
148 #endif
149
150 #ifndef STATIC_ldb_MODULES
151 #define STATIC_ldb_MODULES \
152         {       \
153                 LDAP_INIT \
154                 SQLITE3_INIT \
155                 ldb_tdb_init,   \
156                 ldb_schema_init,        \
157                 ldb_operational_init,   \
158                 ldb_rdn_name_init,      \
159                 ldb_objectclass_init,   \
160                 ldb_paged_results_init, \
161                 ldb_sort_init,          \
162                 NULL                    \
163         }
164 #endif
165
166 int ldb_global_init(void)
167 {
168         static int (*static_init_fns[])(void) = STATIC_ldb_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 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
204 {
205         char *path;
206         void *handle;
207         int (*init_fn) (void);
208
209 #ifdef HAVE_DLOPEN
210 #ifdef _SAMBA_BUILD_
211         path = talloc_asprintf(ldb, "%s/ldb/%s.%s", dyn_MODULESDIR, name, dyn_SHLIBEXT);
212 #else
213         path = talloc_asprintf(ldb, "%s/%s.%s", MODULESDIR, name, SHLIBEXT);
214 #endif
215
216         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
217
218         handle = dlopen(path, 0);
219         if (handle == NULL) {
220                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
221                 return -1;
222         }
223
224         init_fn = (int (*)(void))dlsym(handle, "init_module");
225
226         if (init_fn == NULL) {
227                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
228                 return -1;
229         }
230
231         talloc_free(path);
232
233         return init_fn();
234 #else
235         ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
236         return -1;
237 #endif
238 }
239
240 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
241 {
242         struct ldb_module *module;
243         int i;
244         
245         module = backend;
246
247         for (i = 0; module_list[i] != NULL; i++) {
248                 struct ldb_module *current;
249                 const struct ldb_module_ops *ops;
250                 
251                 ops = ldb_find_module_ops(module_list[i]);
252                 if (ops == NULL) {
253                         if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
254                                 ops = ldb_find_module_ops(module_list[i]);
255                         }
256                 }
257                 
258                 if (ops == NULL) {
259                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
260                                   module_list[i]);
261                         continue;
262                 }
263                 
264                 current = talloc_zero(ldb, struct ldb_module);
265                 if (current == NULL) {
266                         return LDB_ERR_OPERATIONS_ERROR;
267                 }
268                 
269                 current->ldb = ldb;
270                 current->ops = ops;
271                 
272                 DLIST_ADD(module, current);
273         }
274         *out = module;
275         return LDB_SUCCESS;
276 }
277
278 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module) 
279 {
280         while (module && module->ops->init_context == NULL) 
281                 module = module->next;
282
283         if (module && module->ops->init_context &&
284                 module->ops->init_context(module) != LDB_SUCCESS) {
285                 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
286                 return LDB_ERR_OPERATIONS_ERROR;
287         }
288
289         return LDB_SUCCESS;
290 }
291
292 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
293 {
294         const char **modules = NULL;
295         int i;
296         int ret;
297         TALLOC_CTX *mem_ctx = talloc_new(ldb);
298         if (!mem_ctx) {
299                 return LDB_ERR_OPERATIONS_ERROR;
300         }
301
302         /* find out which modules we are requested to activate */
303
304         /* check if we have a custom module list passd as ldb option */
305         if (options) {
306                 for (i = 0; options[i] != NULL; i++) {
307                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
308                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
309                         }
310                 }
311         }
312
313         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
314         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
315                 const char * const attrs[] = { "@LIST" , NULL};
316                 struct ldb_result *res = NULL;
317                 struct ldb_dn *mods_dn;
318
319                 mods_dn = ldb_dn_explode(mem_ctx, "@MODULES");
320                 if (mods_dn == NULL) {
321                         talloc_free(mem_ctx);
322                         return -1;
323                 }
324
325                 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
326                 if (res) talloc_steal(mods_dn, res);
327                 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
328                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
329                 } else {
330                         if (ret != LDB_SUCCESS) {
331                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
332                                 talloc_free(mem_ctx);
333                                 return -1;
334                         }
335                         if (res->count > 1) {
336                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
337                                 talloc_free(mem_ctx);
338                                 return -1;
339                         }
340
341                         modules = ldb_modules_list_from_string(ldb, mem_ctx,
342                                                                (const char *)res->msgs[0]->elements[0].values[0].data);
343
344                 }
345
346                 talloc_free(mods_dn);
347         }
348
349         if (modules != NULL) {
350                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
351                 talloc_free(modules);
352                 if (ret != LDB_SUCCESS) {
353                         return ret;
354                 }
355         } else {
356                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
357         }
358
359         return ldb_init_module_chain(ldb, ldb->modules);
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         switch (request->operation) {
385         case LDB_SEARCH:
386                 FIND_OP(module, search);
387                 return module->ops->search(module, request);
388         case LDB_ADD:
389                 FIND_OP(module, add);
390                 return module->ops->add(module, request);
391         case LDB_MODIFY:
392                 FIND_OP(module, modify);
393                 return module->ops->modify(module, request);
394         case LDB_DELETE:
395                 FIND_OP(module, del);
396                 return module->ops->del(module, request);
397         case LDB_RENAME:
398                 FIND_OP(module, rename);
399                 return module->ops->rename(module, request);
400         case LDB_SEQUENCE_NUMBER:
401                 FIND_OP(module, sequence_number);
402                 return module->ops->sequence_number(module, request);
403         default:
404                 FIND_OP(module, request);
405                 return module->ops->request(module, request);
406         }
407 }
408
409 int ldb_next_init(struct ldb_module *module)
410 {
411         /* init is different in that it is not an error if modules
412          * do not require initialization */
413
414         module = module->next;
415
416         while (module && module->ops->init_context == NULL) 
417                 module = module->next;
418
419         if (module == NULL) 
420                 return LDB_SUCCESS;
421
422         return module->ops->init_context(module);
423 }
424
425 int ldb_next_start_trans(struct ldb_module *module)
426 {
427         FIND_OP(module, start_transaction);
428         return module->ops->start_transaction(module);
429 }
430
431 int ldb_next_end_trans(struct ldb_module *module)
432 {
433         FIND_OP(module, end_transaction);
434         return module->ops->end_transaction(module);
435 }
436
437 int ldb_next_del_trans(struct ldb_module *module)
438 {
439         FIND_OP(module, del_transaction);
440         return module->ops->del_transaction(module);
441 }