r10477: expose transactions outside ldb and change the API once more
[jra/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/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "dlinklist.h"
40 #include <sys/types.h> 
41 #include <sys/stat.h> 
42 #include <unistd.h> 
43
44 #ifdef HAVE_DLOPEN_DISABLED
45 #include <dlfcn.h>
46 #endif
47
48 #define LDB_MODULE_PREFIX       "modules:"
49 #define LDB_MODULE_PREFIX_LEN   8
50
51 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
52 {
53         int i, len;
54         char *trimmed;
55
56         trimmed = talloc_strdup(ldb, string);
57         if (!trimmed) {
58                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
59                 return NULL;
60         }
61
62         len = strlen(trimmed);
63         for (i = 0; trimmed[i] != '\0'; i++) {
64                 switch (trimmed[i]) {
65                 case ' ':
66                 case '\t':
67                 case '\n':
68                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
69                         break;
70                 }
71         }
72
73         return trimmed;
74 }
75
76
77 /* modules are called in inverse order on the stack.
78    Lets place them as an admin would think the right order is.
79    Modules order is imprtant */
80 static char **ldb_modules_list_from_string(struct ldb_context *ldb, const char *string)
81 {
82         char **modules = NULL;
83         char *modstr, *p;
84         int i;
85
86         /* spaces not admitted */
87         modstr = talloc_strdup_no_spaces(ldb, string);
88         if ( ! modstr) {
89                 return NULL;
90         }
91
92         modules = talloc_realloc(ldb, 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         while ((p = strrchr(modstr, ',')) != NULL) {
102                 *p = '\0';
103                 p++;
104                 modules[i] = p;
105
106                 i++;
107                 modules = talloc_realloc(ldb, modules, char *, i + 2);
108                 if ( ! modules ) {
109                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
110                         return NULL;
111                 }
112
113         }
114         modules[i] = modstr;
115
116         modules[i + 1] = NULL;
117
118         return modules;
119 }
120
121 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
122 {
123         char **modules = NULL;
124         int i;
125
126         /* find out which modules we are requested to activate */
127
128         /* check if we have a custom module list passd as ldb option */
129         if (options) {
130                 for (i = 0; options[i] != NULL; i++) {
131                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
132                                 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
133                         }
134                 }
135         }
136
137         /* if not overloaded by options and the backend is not ldap try to load the modules list form ldb */
138         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
139                 int ret;
140                 const char * const attrs[] = { "@LIST" , NULL};
141                 struct ldb_message **msg = NULL;
142                 struct ldb_dn *mods;
143
144                 mods = ldb_dn_explode(ldb, "@MODULES");
145                 if (mods == NULL) {
146                         return -1;
147                 }
148
149                 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &msg);
150                 talloc_free(mods);
151                 if (ret == 0 || (ret == 1 && msg[0]->num_elements == 0)) {
152                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
153                 } else {
154                         if (ret < 0) {
155                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
156                                 return -1;
157                         }
158                         if (ret > 1) {
159                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
160                                 talloc_free(msg);
161                                 return -1;
162                         }
163
164                         modules = ldb_modules_list_from_string(ldb, msg[0]->elements[0].values[0].data);
165
166                 }
167
168                 talloc_free(msg);
169         }
170
171         if (modules == NULL) {
172                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
173                 return 0;
174         }
175
176         for (i = 0; modules[i] != NULL; i++) {
177                 struct ldb_module *current;
178
179                 if (strcmp(modules[i], "schema") == 0) {
180                         current = schema_module_init(ldb, options);
181                         if (!current) {
182                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
183                                 return -1;
184                         }
185                         DLIST_ADD(ldb->modules, current);
186                         continue;
187                 }
188
189                 if (strcmp(modules[i], "timestamps") == 0) {
190                         current = timestamps_module_init(ldb, options);
191                         if (!current) {
192                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
193                                 return -1;
194                         }
195                         DLIST_ADD(ldb->modules, current);
196                         continue;
197                 }
198
199                 if (strcmp(modules[i], "rdn_name") == 0) {
200                         current = rdn_name_module_init(ldb, options);
201                         if (!current) {
202                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
203                                 return -1;
204                         }
205                         DLIST_ADD(ldb->modules, current);
206                         continue;
207                 }
208
209 #ifdef _SAMBA_BUILD_
210                 if (strcmp(modules[i], "objectguid") == 0) {
211                         current = objectguid_module_init(ldb, options);
212                         if (!current) {
213                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
214                                 return -1;
215                         }
216                         DLIST_ADD(ldb->modules, current);
217                         continue;
218                 }
219
220                 if (strcmp(modules[i], "samldb") == 0) {
221                         current = samldb_module_init(ldb, options);
222                         if (!current) {
223                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
224                                 return -1;
225                         }
226                         DLIST_ADD(ldb->modules, current);
227                         continue;
228                 }
229
230                 if (strcmp(modules[i], "samba3sam") == 0) {
231                         current = ldb_samba3sam_module_init(ldb, options);
232                         if (!current) {
233                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
234                                 return -1;
235                         }
236                         DLIST_ADD(ldb->modules, current);
237                         continue;
238                 }
239
240 #endif
241
242                 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", modules[i]);
243         }
244
245         talloc_free(modules);
246         return 0; 
247 }
248
249 /*
250    helper functions to call the next module in chain
251 */
252
253 int ldb_next_search(struct ldb_module *module, 
254                     const struct ldb_dn *base,
255                     enum ldb_scope scope,
256                     const char *expression,
257                     const char * const *attrs, struct ldb_message ***res)
258 {
259         if (!module->next) {
260                 return -1;
261         }
262         return module->next->ops->search(module->next, base, scope, expression, attrs, res);
263 }
264
265 int ldb_next_search_bytree(struct ldb_module *module, 
266                            const struct ldb_dn *base,
267                            enum ldb_scope scope,
268                            struct ldb_parse_tree *tree,
269                            const char * const *attrs, struct ldb_message ***res)
270 {
271         if (!module->next) {
272                 return -1;
273         }
274         return module->next->ops->search_bytree(module->next, base, scope, tree, attrs, res);
275 }
276
277 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
278 {
279         if (!module->next) {
280                 return -1;
281         }
282         return module->next->ops->add_record(module->next, message);
283 }
284
285 int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message)
286 {
287         if (!module->next) {
288                 return -1;
289         }
290         return module->next->ops->modify_record(module->next, message);
291 }
292
293 int ldb_next_delete_record(struct ldb_module *module, const struct ldb_dn *dn)
294 {
295         if (!module->next) {
296                 return -1;
297         }
298         return module->next->ops->delete_record(module->next, dn);
299 }
300
301 int ldb_next_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
302 {
303         if (!module->next) {
304                 return -1;
305         }
306         return module->next->ops->rename_record(module->next, olddn, newdn);
307 }
308
309 int ldb_next_start_trans(struct ldb_module *module)
310 {
311         if (!module->next) {
312                 return -1;
313         }
314         return module->next->ops->start_transaction(module->next);
315 }
316
317 int ldb_next_end_trans(struct ldb_module *module)
318 {
319         if (!module->next) {
320                 return -1;
321         }
322         return module->next->ops->end_transaction(module->next);
323 }
324
325 int ldb_next_del_trans(struct ldb_module *module)
326 {
327         if (!module->next) {
328                 return -1;
329         }
330         return module->next->ops->del_transaction(module->next);
331 }
332
333 void ldb_set_errstring(struct ldb_module *module, char *err_string)
334 {
335         if (module->ldb->err_string) {
336                 talloc_free(module->ldb->err_string);
337         }
338
339         module->ldb->err_string = err_string;
340 }
341