r25856: If the search fails, it is not valid to steal 'res'.
[ira/wip.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 #include "build.h"
39 #endif
40
41 #define LDB_MODULE_PREFIX       "modules:"
42 #define LDB_MODULE_PREFIX_LEN   8
43
44 void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
45 {
46         talloc_free(ldb->modules_dir);
47         ldb->modules_dir = talloc_strdup(ldb, path);
48 }
49
50 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
51 {
52         int i, len;
53         char *trimmed;
54
55         trimmed = talloc_strdup(mem_ctx, string);
56         if (!trimmed) {
57                 return NULL;
58         }
59
60         len = strlen(trimmed);
61         for (i = 0; trimmed[i] != '\0'; i++) {
62                 switch (trimmed[i]) {
63                 case ' ':
64                 case '\t':
65                 case '\n':
66                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
67                         break;
68                 }
69         }
70
71         return trimmed;
72 }
73
74
75 /* modules are called in inverse order on the stack.
76    Lets place them as an admin would think the right order is.
77    Modules order is important */
78 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
79 {
80         char **modules = NULL;
81         const char **m;
82         char *modstr, *p;
83         int i;
84
85         /* spaces not admitted */
86         modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
87         if ( ! modstr) {
88                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()\n");
89                 return NULL;
90         }
91
92         modules = talloc_realloc(mem_ctx, modules, char *, 2);
93         if ( ! modules ) {
94                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
95                 talloc_free(modstr);
96                 return NULL;
97         }
98         talloc_steal(modules, modstr);
99
100         i = 0;
101         /* The str*r*chr walks backwards:  This is how we get the inverse order mentioned above */
102         while ((p = strrchr(modstr, ',')) != NULL) {
103                 *p = '\0';
104                 p++;
105                 modules[i] = p;
106
107                 i++;
108                 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
109                 if ( ! modules ) {
110                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
111                         return NULL;
112                 }
113
114         }
115         modules[i] = modstr;
116
117         modules[i + 1] = NULL;
118
119         m = (const char **)modules;
120
121         return m;
122 }
123
124 static struct ops_list_entry {
125         const struct ldb_module_ops *ops;
126         struct ops_list_entry *next;    
127 } *registered_modules = NULL;
128
129 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
130 {
131         struct ops_list_entry *e;
132  
133         for (e = registered_modules; e; e = e->next) {
134                 if (strcmp(e->ops->name, name) == 0) 
135                         return e->ops;
136         }
137
138         return NULL;
139 }
140
141 #ifndef STATIC_LIBLDB_MODULES
142
143 #ifdef HAVE_LDB_LDAP
144 #define LDAP_INIT ldb_ldap_init,
145 #else
146 #define LDAP_INIT
147 #endif
148
149 #ifdef HAVE_LDB_SQLITE3
150 #define SQLITE3_INIT ldb_sqlite3_init,
151 #else
152 #define SQLITE3_INIT
153 #endif
154
155 #define STATIC_LIBLDB_MODULES \
156         {       \
157                 LDAP_INIT \
158                 SQLITE3_INIT \
159                 ldb_tdb_init,   \
160                 ldb_operational_init,   \
161                 ldb_rdn_name_init,      \
162                 ldb_paged_results_init, \
163                 ldb_sort_init,          \
164                 ldb_asq_init,           \
165                 NULL                    \
166         }
167 #endif
168
169 int ldb_global_init(void)
170 {
171         static int (*static_init_fns[])(void) = STATIC_LIBLDB_MODULES;
172
173         static int initialized = 0;
174         int ret = 0, i;
175
176         if (initialized) 
177                 return 0;
178
179         initialized = 1;
180         
181         for (i = 0; static_init_fns[i]; i++) {
182                 if (static_init_fns[i]() == -1)
183                         ret = -1;
184         }
185
186         return ret;
187 }
188
189 int ldb_register_module(const struct ldb_module_ops *ops)
190 {
191         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
192
193         if (ldb_find_module_ops(ops->name) != NULL)
194                 return -1;
195
196         if (entry == NULL)
197                 return -1;
198
199         entry->ops = ops;
200         entry->next = registered_modules;
201         registered_modules = entry;
202
203         return 0;
204 }
205
206 int ldb_try_load_dso(struct ldb_context *ldb, const char *name)
207 {
208         char *path;
209         void *handle;
210         int (*init_fn) (void);
211
212         if (ldb->modules_dir == NULL)
213                 return -1;
214
215         path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name, 
216                                SHLIBEXT);
217
218         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s\n", name, path);
219
220         handle = dlopen(path, RTLD_NOW);
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 }
237
238 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
239 {
240         struct ldb_module *module;
241         int i;
242         
243         module = backend;
244
245         for (i = 0; module_list[i] != NULL; i++) {
246                 struct ldb_module *current;
247                 const struct ldb_module_ops *ops;
248                 
249                 ops = ldb_find_module_ops(module_list[i]);
250                 if (ops == NULL) {
251                         if (ldb_try_load_dso(ldb, module_list[i]) == 0) {
252                                 ops = ldb_find_module_ops(module_list[i]);
253                         }
254                 }
255                 
256                 if (ops == NULL) {
257                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
258                                   module_list[i]);
259                         continue;
260                 }
261                 
262                 current = talloc_zero(ldb, struct ldb_module);
263                 if (current == NULL) {
264                         return LDB_ERR_OPERATIONS_ERROR;
265                 }
266                 talloc_set_name(current, "ldb_module: %s", module_list[i]);
267                 
268                 current->ldb = ldb;
269                 current->ops = ops;
270                 
271                 DLIST_ADD(module, current);
272         }
273         *out = module;
274         return LDB_SUCCESS;
275 }
276
277 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module) 
278 {
279         while (module && module->ops->init_context == NULL) 
280                 module = module->next;
281
282         if (module && module->ops->init_context &&
283                 module->ops->init_context(module) != LDB_SUCCESS) {
284                 ldb_debug(ldb, LDB_DEBUG_FATAL, "module initialization failed\n");
285                 return LDB_ERR_OPERATIONS_ERROR;
286         }
287
288         return LDB_SUCCESS;
289 }
290
291 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
292 {
293         const char **modules = NULL;
294         int i;
295         int ret;
296         TALLOC_CTX *mem_ctx = talloc_new(ldb);
297         if (!mem_ctx) {
298                 return LDB_ERR_OPERATIONS_ERROR;
299         }
300
301         /* find out which modules we are requested to activate */
302
303         /* check if we have a custom module list passd as ldb option */
304         if (options) {
305                 for (i = 0; options[i] != NULL; i++) {
306                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
307                                 modules = ldb_modules_list_from_string(ldb, mem_ctx, &options[i][LDB_MODULE_PREFIX_LEN]);
308                         }
309                 }
310         }
311
312         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
313         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
314                 const char * const attrs[] = { "@LIST" , NULL};
315                 struct ldb_result *res = NULL;
316                 struct ldb_dn *mods_dn;
317
318                 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
319                 if (mods_dn == NULL) {
320                         talloc_free(mem_ctx);
321                         return -1;
322                 }
323
324                 ret = ldb_search(ldb, mods_dn, LDB_SCOPE_BASE, "", attrs, &res);
325                 
326                 if (ret != LDB_SUCCESS) {
327                         ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
328                         talloc_free(mem_ctx);
329                         return -1;
330                 }
331                 talloc_steal(mods_dn, res);
332                 if (res->count == 0 || res->msgs[0]->num_elements == 0) {
333                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
334                 } else {
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                 if (ret != LDB_SUCCESS) {
352                         talloc_free(mem_ctx);
353                         return ret;
354                 }
355         } else {
356                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
357         }
358
359         ret = ldb_init_module_chain(ldb, ldb->modules);
360         talloc_free(mem_ctx);
361         return ret;
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         int ret;
387         switch (request->operation) {
388         case LDB_SEARCH:
389                 FIND_OP(module, search);
390                 ret = module->ops->search(module, request);
391                 break;
392         case LDB_ADD:
393                 FIND_OP(module, add);
394                 ret = module->ops->add(module, request);
395                 break;
396         case LDB_MODIFY:
397                 FIND_OP(module, modify);
398                 ret = module->ops->modify(module, request);
399                 break;
400         case LDB_DELETE:
401                 FIND_OP(module, del);
402                 ret = module->ops->del(module, request);
403                 break;
404         case LDB_RENAME:
405                 FIND_OP(module, rename);
406                 ret = module->ops->rename(module, request);
407                 break;
408         case LDB_EXTENDED:
409                 FIND_OP(module, extended);
410                 ret = module->ops->extended(module, request);
411                 break;
412         case LDB_SEQUENCE_NUMBER:
413                 FIND_OP(module, sequence_number);
414                 ret = module->ops->sequence_number(module, request);
415                 break;
416         default:
417                 FIND_OP(module, request);
418                 ret = module->ops->request(module, request);
419                 break;
420         }
421         if (ret == LDB_SUCCESS) {
422                 return ret;
423         }
424         if (!ldb_errstring(module->ldb)) {
425                 /* Set a default error string, to place the blame somewhere */
426                 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
427         }
428         return ret;
429 }
430
431 int ldb_next_init(struct ldb_module *module)
432 {
433         /* init is different in that it is not an error if modules
434          * do not require initialization */
435
436         module = module->next;
437
438         while (module && module->ops->init_context == NULL) 
439                 module = module->next;
440
441         if (module == NULL) 
442                 return LDB_SUCCESS;
443
444         return module->ops->init_context(module);
445 }
446
447 int ldb_next_start_trans(struct ldb_module *module)
448 {
449         FIND_OP(module, start_transaction);
450         return module->ops->start_transaction(module);
451 }
452
453 int ldb_next_end_trans(struct ldb_module *module)
454 {
455         FIND_OP(module, end_transaction);
456         return module->ops->end_transaction(module);
457 }
458
459 int ldb_next_del_trans(struct ldb_module *module)
460 {
461         FIND_OP(module, del_transaction);
462         return module->ops->del_transaction(module);
463 }