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