r13901: Backends need to be initialized even if there are no modules
[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 #define LDB_MODULE_PREFIX       "modules:"
40 #define LDB_MODULE_PREFIX_LEN   8
41
42 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
43 {
44         int i, len;
45         char *trimmed;
46
47         trimmed = talloc_strdup(ldb, string);
48         if (!trimmed) {
49                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
50                 return NULL;
51         }
52
53         len = strlen(trimmed);
54         for (i = 0; trimmed[i] != '\0'; i++) {
55                 switch (trimmed[i]) {
56                 case ' ':
57                 case '\t':
58                 case '\n':
59                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
60                         break;
61                 }
62         }
63
64         return trimmed;
65 }
66
67
68 /* modules are called in inverse order on the stack.
69    Lets place them as an admin would think the right order is.
70    Modules order is important */
71 static char **ldb_modules_list_from_string(struct ldb_context *ldb, const char *string)
72 {
73         char **modules = NULL;
74         char *modstr, *p;
75         int i;
76
77         /* spaces not admitted */
78         modstr = talloc_strdup_no_spaces(ldb, string);
79         if ( ! modstr) {
80                 return NULL;
81         }
82
83         modules = talloc_realloc(ldb, modules, char *, 2);
84         if ( ! modules ) {
85                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
86                 talloc_free(modstr);
87                 return NULL;
88         }
89         talloc_steal(modules, modstr);
90
91         i = 0;
92         while ((p = strrchr(modstr, ',')) != NULL) {
93                 *p = '\0';
94                 p++;
95                 modules[i] = p;
96
97                 i++;
98                 modules = talloc_realloc(ldb, modules, char *, i + 2);
99                 if ( ! modules ) {
100                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
101                         return NULL;
102                 }
103
104         }
105         modules[i] = modstr;
106
107         modules[i + 1] = NULL;
108
109         return modules;
110 }
111
112 static struct ops_list_entry {
113         const struct ldb_module_ops *ops;
114         struct ops_list_entry *next;    
115 } *registered_modules = NULL;
116
117 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
118 {
119         struct ops_list_entry *e;
120  
121         for (e = registered_modules; e; e = e->next) {
122                 if (strcmp(e->ops->name, name) == 0) 
123                         return e->ops;
124         }
125
126         return NULL;
127 }
128
129 #ifdef HAVE_LDAP
130 #define LDAP_INIT ldb_ldap_init,
131 #else
132 #define LDAP_INIT
133 #endif
134
135 #ifdef HAVE_SQLITE3
136 #define SQLITE3_INIT ldb_sqlite3_init,
137 #else
138 #define SQLITE3_INIT
139 #endif
140
141 #ifndef STATIC_LIBLDB_MODULES
142 #define STATIC_LIBLDB_MODULES \
143         {       \
144                 LDAP_INIT \
145                 SQLITE3_INIT \
146                 ldb_tdb_init,   \
147                 ldb_schema_init,        \
148                 ldb_operational_init,   \
149                 ldb_rdn_name_init,      \
150                 ldb_objectclass_init,   \
151                 ldb_paged_results_init, \
152                 ldb_sort_init,          \
153                 NULL                    \
154         }
155 #endif
156
157 int ldb_global_init(void)
158 {
159         static int (*static_init_fns[])(void) = STATIC_LIBLDB_MODULES;
160
161         static int initialized = 0;
162         int ret = 0, i;
163
164         if (initialized) 
165                 return 0;
166
167         initialized = 1;
168         
169         for (i = 0; static_init_fns[i]; i++) {
170                 if (static_init_fns[i]() == -1)
171                         ret = -1;
172         }
173
174         return ret;
175 }
176
177 int ldb_register_module(const struct ldb_module_ops *ops)
178 {
179         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
180
181         if (ldb_find_module_ops(ops->name) != NULL)
182                 return -1;
183
184         if (entry == NULL)
185                 return -1;
186
187         entry->ops = ops;
188         entry->next = registered_modules;
189         registered_modules = entry;
190
191         return 0;
192 }
193
194 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
195 {
196         char **modules = NULL;
197         struct ldb_module *module;
198         int i;
199         /* find out which modules we are requested to activate */
200
201         /* check if we have a custom module list passd as ldb option */
202         if (options) {
203                 for (i = 0; options[i] != NULL; i++) {
204                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
205                                 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
206                         }
207                 }
208         }
209
210         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
211         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
212                 int ret;
213                 const char * const attrs[] = { "@LIST" , NULL};
214                 struct ldb_result *res = NULL;
215                 struct ldb_dn *mods;
216
217                 mods = ldb_dn_explode(ldb, "@MODULES");
218                 if (mods == NULL) {
219                         return -1;
220                 }
221
222                 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &res);
223                 talloc_free(mods);
224                 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
225                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
226                 } else {
227                         if (ret != LDB_SUCCESS) {
228                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
229                                 return -1;
230                         }
231                         if (res->count > 1) {
232                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
233                                 talloc_free(res);
234                                 return -1;
235                         }
236
237                         modules = ldb_modules_list_from_string(ldb, 
238                                                                (const char *)res->msgs[0]->elements[0].values[0].data);
239
240                 }
241
242                 talloc_free(res);
243         }
244
245         if (modules != NULL) {
246                 for (i = 0; modules[i] != NULL; i++) {
247                         struct ldb_module *current;
248                         const struct ldb_module_ops *ops;
249                                 
250                         ops = ldb_find_module_ops(modules[i]);
251                         if (ops == NULL) {
252                                 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
253                                           modules[i]);
254                                 continue;
255                         }
256
257                         current = talloc_zero(ldb, struct ldb_module);
258                         if (current == NULL) {
259                                 return -1;
260                         }
261
262                         current->ldb = ldb;
263                         current->ops = ops;
264                 
265                         DLIST_ADD(ldb->modules, current);
266                 }
267
268                 talloc_free(modules);
269         } else {
270                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
271         }
272
273         module = ldb->modules;
274
275         while (module && module->ops->init_context == NULL) 
276                 module = module->next;
277
278         if (module && module->ops->init_context &&
279                 module->ops->init_context(module) != LDB_SUCCESS) {
280                 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
281                 return -1;
282         }
283
284         return 0; 
285 }
286
287 /*
288   by using this we allow ldb modules to only implement the functions they care about,
289   which makes writing a module simpler, and makes it more likely to keep working
290   when ldb is extended
291 */
292 #define FIND_OP(module, op) do { \
293         module = module->next; \
294         while (module && module->ops->op == NULL) module = module->next; \
295         if (module == NULL) return LDB_ERR_OPERATIONS_ERROR; \
296 } while (0)
297
298
299 /*
300    helper functions to call the next module in chain
301 */
302 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
303 {
304         FIND_OP(module, request);
305         return module->ops->request(module, request);
306 }
307
308 int ldb_next_init(struct ldb_module *module)
309 {
310         /* init is different in that it is not an error if modules
311          * do not require initialization */
312
313         module = module->next;
314
315         while (module && module->ops->init_context == NULL) 
316                 module = module->next;
317
318         if (module == NULL) 
319                 return LDB_SUCCESS;
320
321         return module->ops->init_context(module);
322 }
323
324 int ldb_next_start_trans(struct ldb_module *module)
325 {
326         FIND_OP(module, start_transaction);
327         return module->ops->start_transaction(module);
328 }
329
330 int ldb_next_end_trans(struct ldb_module *module)
331 {
332         FIND_OP(module, end_transaction);
333         return module->ops->end_transaction(module);
334 }
335
336 int ldb_next_del_trans(struct ldb_module *module)
337 {
338         FIND_OP(module, del_transaction);
339         return module->ops->del_transaction(module);
340 }
341
342 int ldb_next_async_wait(struct ldb_module *module, struct ldb_async_handle *handle, enum ldb_async_wait_type type)
343 {
344         FIND_OP(module, async_wait);
345         return module->ops->async_wait(module, handle, type);
346 }