r19188: merge from samba3:
[kamenim/samba.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 #if (_SAMBA_BUILD_ >= 4)
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((struct ldb_context *)mem_ctx,
85                                          string);
86         if ( ! modstr) {
87                 return NULL;
88         }
89
90         modules = talloc_realloc(mem_ctx, modules, char *, 2);
91         if ( ! modules ) {
92                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
93                 talloc_free(modstr);
94                 return NULL;
95         }
96         talloc_steal(modules, modstr);
97
98         i = 0;
99         /* The str*r*chr walks backwards:  This is how we get the inverse order mentioned above */
100         while ((p = strrchr(modstr, ',')) != NULL) {
101                 *p = '\0';
102                 p++;
103                 modules[i] = p;
104
105                 i++;
106                 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
107                 if ( ! modules ) {
108                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
109                         return NULL;
110                 }
111
112         }
113         modules[i] = modstr;
114
115         modules[i + 1] = NULL;
116
117         m = (const char **)modules;
118
119         return m;
120 }
121
122 static struct ops_list_entry {
123         const struct ldb_module_ops *ops;
124         struct ops_list_entry *next;    
125 } *registered_modules = NULL;
126
127 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
128 {
129         struct ops_list_entry *e;
130  
131         for (e = registered_modules; e; e = e->next) {
132                 if (strcmp(e->ops->name, name) == 0) 
133                         return e->ops;
134         }
135
136         return NULL;
137 }
138
139 #ifndef STATIC_ldb_MODULES
140
141 #ifdef HAVE_LDB_LDAP
142 #define LDAP_INIT ldb_ldap_init,
143 #else
144 #define LDAP_INIT
145 #endif
146
147 #ifdef HAVE_LDB_SQLITE3
148 #define SQLITE3_INIT ldb_sqlite3_init,
149 #else
150 #define SQLITE3_INIT
151 #endif
152
153 #define STATIC_ldb_MODULES \
154         {       \
155                 LDAP_INIT \
156                 SQLITE3_INIT \
157                 ldb_tdb_init,   \
158                 ldb_schema_init,        \
159                 ldb_operational_init,   \
160                 ldb_rdn_name_init,      \
161                 ldb_objectclass_init,   \
162                 ldb_paged_results_init, \
163                 ldb_sort_init,          \
164                 NULL                    \
165         }
166 #endif
167
168 int ldb_global_init(void)
169 {
170         static int (*static_init_fns[])(void) = STATIC_ldb_MODULES;
171
172         static int initialized = 0;
173         int ret = 0, i;
174
175         if (initialized) 
176                 return 0;
177
178         initialized = 1;
179         
180         for (i = 0; static_init_fns[i]; i++) {
181                 if (static_init_fns[i]() == -1)
182                         ret = -1;
183         }
184
185         return ret;
186 }
187
188 int ldb_register_module(const struct ldb_module_ops *ops)
189 {
190         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
191
192         if (ldb_find_module_ops(ops->name) != NULL)
193                 return -1;
194
195         if (entry == NULL)
196                 return -1;
197
198         entry->ops = ops;
199         entry->next = registered_modules;
200         registered_modules = entry;
201
202         return 0;
203 }
204
205 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
206 {
207         char *path;
208         void *handle;
209         int (*init_fn) (void);
210
211 #ifdef HAVE_DLOPEN
212 #ifdef _SAMBA_BUILD_
213         path = talloc_asprintf(ldb, "%s/ldb/%s.%s", dyn_MODULESDIR, name, dyn_SHLIBEXT);
214 #else
215         path = talloc_asprintf(ldb, "%s/%s.%s", MODULESDIR, name, SHLIBEXT);
216 #endif
217
218         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
219
220         handle = dlopen(path, 0);
221         if (handle == NULL) {
222                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s\n", name, path, dlerror());
223                 return -1;
224         }
225
226         init_fn = (int (*)(void))dlsym(handle, "init_module");
227
228         if (init_fn == NULL) {
229                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `init_module' found in %s: %s\n", path, dlerror());
230                 return -1;
231         }
232
233         talloc_free(path);
234
235         return init_fn();
236 #else
237         ldb_debug(ldb, LDB_DEBUG_TRACE, "no dlopen() - not trying to load %s module\n", name);
238         return -1;
239 #endif
240 }
241
242 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
243 {
244         struct ldb_module *module;
245         int i;
246         
247         module = backend;
248
249         for (i = 0; module_list[i] != NULL; i++) {
250                 struct ldb_module *current;
251                 const struct ldb_module_ops *ops;
252                 
253                 ops = ldb_find_module_ops(module_list[i]);
254                 if (ops == NULL) {
255                         if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
256                                 ops = ldb_find_module_ops(module_list[i]);
257                         }
258                 }
259                 
260                 if (ops == NULL) {
261                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
262                                   module_list[i]);
263                         continue;
264                 }
265                 
266                 current = talloc_zero(ldb, struct ldb_module);
267                 if (current == NULL) {
268                         return LDB_ERR_OPERATIONS_ERROR;
269                 }
270                 
271                 current->ldb = ldb;
272                 current->ops = ops;
273                 
274                 DLIST_ADD(module, current);
275         }
276         *out = module;
277         return LDB_SUCCESS;
278 }
279
280 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module) 
281 {
282         while (module && module->ops->init_context == NULL) 
283                 module = module->next;
284
285         if (module && module->ops->init_context &&
286                 module->ops->init_context(module) != LDB_SUCCESS) {
287                 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
288                 return LDB_ERR_OPERATIONS_ERROR;
289         }
290
291         return LDB_SUCCESS;
292 }
293
294 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
295 {
296         const char **modules = NULL;
297         int i;
298         int ret;
299         TALLOC_CTX *mem_ctx = talloc_new(ldb);
300         if (!mem_ctx) {
301                 return LDB_ERR_OPERATIONS_ERROR;
302         }
303
304         /* find out which modules we are requested to activate */
305
306         /* check if we have a custom module list passd as ldb option */
307         if (options) {
308                 for (i = 0; options[i] != NULL; i++) {
309                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
310                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
311                         }
312                 }
313         }
314
315         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
316         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
317                 const char * const attrs[] = { "@LIST" , NULL};
318                 struct ldb_result *res = NULL;
319                 struct ldb_dn *mods_dn;
320
321                 mods_dn = ldb_dn_explode(mem_ctx, "@MODULES");
322                 if (mods_dn == NULL) {
323                         talloc_free(mem_ctx);
324                         return -1;
325                 }
326
327                 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
328                 talloc_steal(mods_dn, res);
329                 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
330                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
331                 } else {
332                         if (ret != LDB_SUCCESS) {
333                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
334                                 talloc_free(mem_ctx);
335                                 return -1;
336                         }
337                         if (res->count > 1) {
338                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
339                                 talloc_free(mem_ctx);
340                                 return -1;
341                         }
342
343                         modules = ldb_modules_list_from_string(ldb, mem_ctx,
344                                                                (const char *)res->msgs[0]->elements[0].values[0].data);
345
346                 }
347
348                 talloc_free(mods_dn);
349         }
350
351         if (modules != NULL) {
352                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
353                 talloc_free(modules);
354                 if (ret != LDB_SUCCESS) {
355                         return ret;
356                 }
357         } else {
358                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
359         }
360
361         return ldb_init_module_chain(ldb, ldb->modules);
362 }
363
364 /*
365   by using this we allow ldb modules to only implement the functions they care about,
366   which makes writing a module simpler, and makes it more likely to keep working
367   when ldb is extended
368 */
369 #define FIND_OP(module, op) do { \
370         struct ldb_context *ldb = module->ldb; \
371         module = module->next; \
372         while (module && module->ops->op == NULL) module = module->next; \
373         if (module == NULL) { \
374                 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
375                 return LDB_ERR_OPERATIONS_ERROR;        \
376         }                                               \
377 } while (0)
378
379
380 /*
381    helper functions to call the next module in chain
382 */
383
384 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
385 {
386         switch (request->operation) {
387         case LDB_SEARCH:
388                 FIND_OP(module, search);
389                 return module->ops->search(module, request);
390         case LDB_ADD:
391                 FIND_OP(module, add);
392                 return module->ops->add(module, request);
393         case LDB_MODIFY:
394                 FIND_OP(module, modify);
395                 return module->ops->modify(module, request);
396         case LDB_DELETE:
397                 FIND_OP(module, del);
398                 return module->ops->del(module, request);
399         case LDB_RENAME:
400                 FIND_OP(module, rename);
401                 return module->ops->rename(module, request);
402         case LDB_SEQUENCE_NUMBER:
403                 FIND_OP(module, sequence_number);
404                 return module->ops->sequence_number(module, request);
405         default:
406                 FIND_OP(module, request);
407                 return module->ops->request(module, request);
408         }
409 }
410
411 int ldb_next_init(struct ldb_module *module)
412 {
413         /* init is different in that it is not an error if modules
414          * do not require initialization */
415
416         module = module->next;
417
418         while (module && module->ops->init_context == NULL) 
419                 module = module->next;
420
421         if (module == NULL) 
422                 return LDB_SUCCESS;
423
424         return module->ops->init_context(module);
425 }
426
427 int ldb_next_start_trans(struct ldb_module *module)
428 {
429         FIND_OP(module, start_transaction);
430         return module->ops->start_transaction(module);
431 }
432
433 int ldb_next_end_trans(struct ldb_module *module)
434 {
435         FIND_OP(module, end_transaction);
436         return module->ops->end_transaction(module);
437 }
438
439 int ldb_next_del_trans(struct ldb_module *module)
440 {
441         FIND_OP(module, del_transaction);
442         return module->ops->del_transaction(module);
443 }