5 Copyright (C) Simo Sorce 2004
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
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.
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.
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
29 * Component: ldb modules core
31 * Description: core modules routines
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>
45 #ifdef HAVE_DLOPEN_DISABLED
49 #define LDB_MODULE_PREFIX "modules:"
50 #define LDB_MODULE_PREFIX_LEN 8
52 static char *talloc_strdup_no_spaces(struct ldb_context *ldb, const char *string)
57 trimmed = talloc_strdup(ldb, string);
59 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in talloc_strdup_trim_spaces()\n");
63 len = strlen(trimmed);
64 for (i = 0; trimmed[i] != '\0'; i++) {
69 memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
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)
83 char **modules = NULL;
87 /* spaces not admitted */
88 modstr = talloc_strdup_no_spaces(ldb, string);
93 modules = talloc_realloc(ldb, modules, char *, 2);
95 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
99 talloc_steal(modules, modstr);
102 while ((p = strrchr(modstr, ',')) != NULL) {
108 modules = talloc_realloc(ldb, modules, char *, i + 2);
110 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()\n");
117 modules[i + 1] = NULL;
122 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
124 char **modules = NULL;
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 },
134 { "objectguid", objectguid_module_init },
135 { "samldb", samldb_module_init },
136 { "samba3sam", ldb_samba3sam_module_init },
137 { "proxy", proxy_module_init },
138 { "rootdse", rootdse_module_init },
139 { "password_hash", password_hash_module_init },
144 /* find out which modules we are requested to activate */
146 /* check if we have a custom module list passd as ldb option */
148 for (i = 0; options[i] != NULL; i++) {
149 if (strncmp(options[i], LDB_MODULE_PREFIX, LDB_MODULE_PREFIX_LEN) == 0) {
150 modules = ldb_modules_list_from_string(ldb, &options[i][LDB_MODULE_PREFIX_LEN]);
155 /* if not overloaded by options and the backend is not ldap try to load the modules list form ldb */
156 if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
158 const char * const attrs[] = { "@LIST" , NULL};
159 struct ldb_result *res = NULL;
162 mods = ldb_dn_explode(ldb, "@MODULES");
167 ret = ldb_search(ldb, mods, LDB_SCOPE_BASE, "", attrs, &res);
169 if (ret == LDB_SUCCESS && (res->count == 0 || res->msgs[0]->num_elements == 0)) {
170 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db\n");
172 if (ret != LDB_SUCCESS) {
173 ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out\n", ldb_errstring(ldb));
176 if (res->count > 1) {
177 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out\n", res->count);
182 modules = ldb_modules_list_from_string(ldb,
183 (const char *)res->msgs[0]->elements[0].values[0].data);
190 if (modules == NULL) {
191 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database\n");
195 for (i = 0; modules[i] != NULL; i++) {
196 struct ldb_module *current;
198 for (m=0;well_known_modules[m].name;m++) {
199 if (strcmp(modules[i], well_known_modules[m].name) == 0) {
200 current = well_known_modules[m].init(ldb, options);
201 if (current == NULL) {
202 ldb_debug(ldb, LDB_DEBUG_FATAL, "function 'init_module' in %s fails\n", modules[i]);
205 DLIST_ADD(ldb->modules, current);
209 if (well_known_modules[m].name == NULL) {
210 ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found\n",
215 talloc_free(modules);
220 by using this we allow ldb modules to only implement the functions they care about,
221 which makes writing a module simpler, and makes it more likely to keep working
224 #define FIND_OP(module, op) do { \
225 module = module->next; \
226 while (module && module->ops->op == NULL) module = module->next; \
227 if (module == NULL) return -1; \
232 helper functions to call the next module in chain
234 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
236 FIND_OP(module, request);
237 return module->ops->request(module, request);
240 int ldb_next_start_trans(struct ldb_module *module)
242 FIND_OP(module, start_transaction);
243 return module->ops->start_transaction(module);
246 int ldb_next_end_trans(struct ldb_module *module)
248 FIND_OP(module, end_transaction);
249 return module->ops->end_transaction(module);
252 int ldb_next_del_trans(struct ldb_module *module)
254 FIND_OP(module, del_transaction);
255 return module->ops->del_transaction(module);
258 void ldb_set_errstring(struct ldb_module *module, char *err_string)
260 if (module->ldb->err_string) {
261 talloc_free(module->ldb->err_string);
264 module->ldb->err_string = talloc_steal(module->ldb, err_string);