summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/common/ldb_modules.c
blob: f8162aee8c78b939c59234c223f7ee3e949b386f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

/* 
   ldb database library

   Copyright (C) Simo Sorce  2004

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 *  Name: ldb
 *
 *  Component: ldb modules core
 *
 *  Description: core modules routines
 *
 *  Author: Simo Sorce
 */

#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
#include "dlinklist.h"
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 

#ifdef HAVE_DLOPEN_DISABLED
#include <dlfcn.h>
#endif

#define LDB_MODULE_PREFIX	"modules"
#define LDB_MODULE_PREFIX_LEN	7
#define LDB_MODULE_SEP		':'

int ldb_load_modules(struct ldb_context *ldb, const char *options[])
{
	struct ldb_module *current;
	char **modules;
	int mnum, i;

	/* find out which modules we are requested to activate */
	modules = NULL;
	mnum = 0;

	if (options) {
		char *q, *p;

		for (i = 0; options[i] != NULL; i++) {
			if (strncmp(options[i], LDB_MODULE_PREFIX, 
				    LDB_MODULE_PREFIX_LEN) == 0) {
				p = q = talloc_strdup(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
				if (*q != ':') {
					talloc_free(q);
					return -1;
				}
				do {
					*p = '\0';
					q = p + 1;
					mnum++;
					modules = talloc_realloc(ldb, modules, char *, mnum);
					if (!modules) {
						ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_load_modules()\n");
						return -1;
					}
					modules[mnum - 1] = q;
				} while ((p = strchr(q, LDB_MODULE_SEP)));
			}
		}
	}

	if (!modules && strcmp("ldap", ldb->modules->ops->name)) { 
		/* no modules in the options, look for @MODULES in the
		   db (not for ldap) */
		int ret;
		const char * const attrs[] = { "@LIST" , NULL};
		struct ldb_message **msg = NULL;
		char *modstr, *c, *p; 

		ret = ldb_search(ldb, "", LDB_SCOPE_BASE, "dn=@MODULES", attrs, &msg);
		if (ret == 0) {
			ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
		} else {
			if (ret < 0) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
				return -1;
			}
			if (ret > 1) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found, bailing out\n");
				return -1;
			}

/*
			for (j = 0; j < msg[0]->num_elements; j++) {
				for (k = 0; k < msg[0]->elements[j].num_values; k++) {
					pn++;
					modules = talloc_realloc(ldb, modules, char *, pn);
					if (!modules) {
						ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
						return -1;
					}
					modules[pn - 1] = talloc_strndup(modules, msg[0]->elements[j].values[k].data, msg[0]->elements[j].values[k].length);
					if (!modules[pn - 1]) {
						ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in register_modules()\n");
						return -1;
					}
				}
			}
*/
			modstr = msg[0]->elements[0].values[0].data;
			for (c = modstr, mnum = 0; c != NULL; mnum++) {
				c = strchr(c, ',');
				if (c != NULL) {
					c++;
					if (*c == '\0') { /* avoid failing if the modules string lasts with ',' */
						break;
					}
				}
			}
			

			modules = talloc_array(ldb, char *, mnum);
			if ( ! modules ) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_load_modules()\n");
				return -1;
			}

			for (p = c = modstr, i = 0; mnum > i; i++) {
				c = strchr(p, ',');
				if (c) {
					*c = '\0';
				}
				/* modules are seeked in inverse order. Lets place them as an admin would think the right order is */
				modules[mnum - i - 1] = talloc_strdup(modules, p);
				p = c + 1;
			}
		}
		talloc_free(msg);
	}

	if (modules) {
		for (i = 0; i < mnum; i++) {
#ifdef HAVE_DLOPEN_DISABLED
			void *handle;
			ldb_module_init_function init;
			struct stat st;
			char *filename;
			const char *errstr;
#endif

			if (strcmp(modules[i], "schema") == 0) {
				current = schema_module_init(ldb, options);
				if (!current) {
					ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
					return -1;
				}
				DLIST_ADD(ldb->modules, current);
				continue;
			}

			if (strcmp(modules[i], "timestamps") == 0) {
				current = timestamps_module_init(ldb, options);
				if (!current) {
					ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
					return -1;
				}
				DLIST_ADD(ldb->modules, current);
				continue;
			}

			if (strcmp(modules[i], "samldb") == 0) {
				current = samldb_module_init(ldb, options);
				if (!current) {
					ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
					return -1;
				}
				DLIST_ADD(ldb->modules, current);
				continue;
			}

#ifdef HAVE_DLOPEN_DISABLED
			filename = talloc_asprintf(ldb, "%s.so", modules[i]);
			if (!filename) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Talloc failed!\n");
				return -1;
			}

			if (stat(filename, &st) < 0) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
				return -1;
			}

			handle = dlopen(filename, RTLD_LAZY);

			if (!handle) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Error loading module %s [%s]\n", modules[i], dlerror());
				return -1;
			}

			init = (ldb_module_init_function)dlsym(handle, "init_module");

			errstr = dlerror();
			if (errstr) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "Error trying to resolve symbol 'init_module' in %s [%s]\n", modules[i], errstr);
				return -1;
			}

			current = init(ldb, options);
			if (!current) {
				ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
				return -1;
			}
			DLIST_ADD(ldb->modules, current);
#else
			ldb_debug(ldb, LDB_DEBUG_FATAL, "Required module [%s] not found, bailing out!\n", modules[i]);
			return -1;
#endif
		}
	}

	return 0; 
}

/*
   helper functions to call the next module in chain
*/

int ldb_next_search(struct ldb_module *module, 
	       const char *base,
	       enum ldb_scope scope,
	       const char *expression,
	       const char * const *attrs, struct ldb_message ***res)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->search(module->next, base, scope, expression, attrs, res);
}

int ldb_next_search_free(struct ldb_module *module, struct ldb_message **msg)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->search_free(module->next, msg);
}

int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->add_record(module->next, message);
}

int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->modify_record(module->next, message);
}

int ldb_next_delete_record(struct ldb_module *module, const char *dn)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->delete_record(module->next, dn);
}

int ldb_next_rename_record(struct ldb_module *module, const char *olddn, const char *newdn)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->rename_record(module->next, olddn, newdn);
}

int ldb_next_named_lock(struct ldb_module *module, const char *lockname)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->named_lock(module->next, lockname);
}

int ldb_next_named_unlock(struct ldb_module *module, const char *lockname)
{
	if (!module->next) {
		return -1;
	}
	return module->next->ops->named_unlock(module->next, lockname);
}

const char *ldb_next_errstring(struct ldb_module *module)
{
	if (!module->next) {
		return NULL;
	}
	return module->next->ops->errstring(module->next);
}