r12733: Merge ldap/ldb controls into main tree
[mdw/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_errors.h"
39 #include "ldb/include/ldb_private.h"
40 #include "dlinklist.h"
41 #include <sys/types.h> 
42 #include <sys/stat.h> 
43 #include <unistd.h> 
44
45 #ifdef HAVE_DLOPEN_DISABLED
46 #include <dlfcn.h>
47 #endif
48
49 #define LDB_MODULE_PREFIX       "modules:"
50 #define LDB_MODULE_PREFIX_LEN   8
51
52 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
53 {
54         int i, len;
55         char *trimmed;
56
57         trimmed = talloc_strdup(ldb, string);
58         if (!trimmed) {
59                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
60                 return NULL;
61         }
62
63         len = strlen(trimmed);
64         for (i = 0; trimmed[i] != '\0'; i++) {
65                 switch (trimmed[i]) {
66                 case ' ':
67                 case '\t':
68                 case '\n':
69                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
70                         break;
71                 }
72         }
73
74         return trimmed;
75 }
76
77
78 /* modules are called in inverse order on the stack.
79    Lets place them as an admin would think the right order is.
80    Modules order is imprtant */
81 static char **ldb_modules_list_from_string(struct ldb_context *ldb, const char *string)
82 {
83         char **modules = NULL;
84         char *modstr, *p;
85         int i;
86
87         /* spaces not admitted */
88         modstr = talloc_strdup_no_spaces(ldb, string);
89         if ( ! modstr) {
90                 return NULL;
91         }
92
93         modules = talloc_realloc(ldb, modules, char *, 2);
94         if ( ! modules ) {
95                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
96                 talloc_free(modstr);
97                 return NULL;
98         }
99         talloc_steal(modules, modstr);
100
101         i = 0;
102         while ((p = strrchr(modstr, ',')) != NULL) {
103                 *p = '\0';
104                 p++;
105                 modules[i] = p;
106
107                 i++;
108                 modules = talloc_realloc(ldb, 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         return modules;
120 }
121
122 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
123 {
124         char **modules = NULL;
125         int i;
126         struct {
127                 const char *name;
128                 ldb_module_init_t init;
129         } well_known_modules[] = {
130                 { "schema", schema_module_init },
131                 { "operational", operational_module_init },
132                 { "rdn_name", rdn_name_module_init },
133                 { "objectclass", objectclass_module_init },
134                 { "paged_results", paged_results_module_init },
135                 { "server_sort", server_sort_module_init },
136 #ifdef _SAMBA_BUILD_
137                 { "objectguid", objectguid_module_init },
138                 { "samldb", samldb_module_init },
139                 { "samba3sam", ldb_samba3sam_module_init },
140                 { "proxy", proxy_module_init },
141                 { "rootdse", rootdse_module_init },
142                 { "extended_dn", extended_dn_module_init },
143                 { "password_hash", password_hash_module_init },
144 #endif
145                 { NULL, NULL }
146         };
147
148         /* find out which modules we are requested to activate */
149
150         /* check if we have a custom module list passd as ldb option */
151         if (options) {
152                 for (i = 0; options[i] != NULL; i++) {
153                         if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
154                                 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
155                         }
156                 }
157         }
158
159         /* if not overloaded by options and the backend is not ldap try to load the modules list form ldb */
160         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) { 
161                 int ret;
162                 const char * const attrs[] = { "@LIST" , NULL};
163                 struct ldb_result *res = NULL;
164                 struct ldb_dn *mods;
165
166                 mods = ldb_dn_explode(ldb, "@MODULES");
167                 if (mods == NULL) {
168                         return -1;
169                 }
170
171                 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &res);
172                 talloc_free(mods);
173                 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
174                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
175                 } else {
176                         if (ret != LDB_SUCCESS) {
177                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
178                                 return -1;
179                         }
180                         if (res->count > 1) {
181                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
182                                 talloc_free(res);
183                                 return -1;
184                         }
185
186                         modules = ldb_modules_list_from_string(ldb, 
187                                                                (const char *)res->msgs[0]->elements[0].values[0].data);
188
189                 }
190
191                 talloc_free(res);
192         }
193
194         if (modules == NULL) {
195                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
196                 return 0;
197         }
198
199         for (i = 0; modules[i] != NULL; i++) {
200                 struct ldb_module *current;
201                 int m;
202                 for (m=0;well_known_modules[m].name;m++) {
203                         if (strcmp(modules[i], well_known_modules[m].name) == 0) {
204                                 current = well_known_modules[m].init(ldb, LDB_MODULES_INIT_STAGE_1, options);
205                                 if (current == NULL) {
206                                         ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
207                                         return -1;
208                                 }
209                                 DLIST_ADD(ldb->modules, current);
210                                 break;
211                         }
212                 }
213                 if (well_known_modules[m].name == NULL) {
214                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n", 
215                                   modules[i]);
216                 }
217         }
218
219         /* second stage init */
220         for (i = 0; modules[i] != NULL; i++) {
221                 int m;
222                 for (m = 0; well_known_modules[m].name; m++) {
223                         if (strcmp(modules[i], well_known_modules[m].name) == 0) {
224                                 well_known_modules[m].init(ldb, LDB_MODULES_INIT_STAGE_2, options);
225                                 break;
226                         }
227                 }
228         }
229
230         talloc_free(modules);
231         return 0; 
232 }
233
234 /*
235   by using this we allow ldb modules to only implement the functions they care about,
236   which makes writing a module simpler, and makes it more likely to keep working
237   when ldb is extended
238 */
239 #define FIND_OP(module, op) do { \
240         module = module->next; \
241         while (module && module->ops->op == NULL) module = module->next; \
242         if (module == NULL) return -1; \
243 } while (0)
244
245
246 /*
247    helper functions to call the next module in chain
248 */
249 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
250 {
251         FIND_OP(module, request);
252         return module->ops->request(module, request);
253 }
254
255 int ldb_next_start_trans(struct ldb_module *module)
256 {
257         FIND_OP(module, start_transaction);
258         return module->ops->start_transaction(module);
259 }
260
261 int ldb_next_end_trans(struct ldb_module *module)
262 {
263         FIND_OP(module, end_transaction);
264         return module->ops->end_transaction(module);
265 }
266
267 int ldb_next_del_trans(struct ldb_module *module)
268 {
269         FIND_OP(module, del_transaction);
270         return module->ops->del_transaction(module);
271 }
272
273 void ldb_set_errstring(struct ldb_module *module, char *err_string)
274 {
275         if (module->ldb->err_string) {
276                 talloc_free(module->ldb->err_string);
277         }
278
279         module->ldb->err_string = talloc_steal(module->ldb, err_string);
280 }
281