ec71c8d04256c8b7cd89e45e2528483a78acecc2
[abartlet/samba.git/.git] / source4 / lib / ldb / common / ldb_modules.c
1 /*
2    ldb database library
3
4    Copyright (C) Simo Sorce  2004-2008
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_private.h"
35 #include "dlinklist.h"
36
37 #define LDB_MODULE_PREFIX       "modules:"
38 #define LDB_MODULE_PREFIX_LEN   8
39
40 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
41                                  const char *symbol);
42
43 void ldb_set_modules_dir(struct ldb_context *ldb, const char *path)
44 {
45         talloc_free(ldb->modules_dir);
46         ldb->modules_dir = talloc_strdup(ldb, path);
47 }
48
49 static char *ldb_modules_strdup_no_spaces(TALLOC_CTX *mem_ctx, const char *string)
50 {
51         size_t i, len;
52         char *trimmed;
53
54         trimmed = talloc_strdup(mem_ctx, string);
55         if (!trimmed) {
56                 return NULL;
57         }
58
59         len = strlen(trimmed);
60         for (i = 0; trimmed[i] != '\0'; i++) {
61                 switch (trimmed[i]) {
62                 case ' ':
63                 case '\t':
64                 case '\n':
65                         memmove(&trimmed[i], &trimmed[i + 1], len -i -1);
66                         break;
67                 }
68         }
69
70         return trimmed;
71 }
72
73
74 /* modules are called in inverse order on the stack.
75    Lets place them as an admin would think the right order is.
76    Modules order is important */
77 const char **ldb_modules_list_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *string)
78 {
79         char **modules = NULL;
80         const char **m;
81         char *modstr, *p;
82         unsigned int i;
83
84         /* spaces not admitted */
85         modstr = ldb_modules_strdup_no_spaces(mem_ctx, string);
86         if ( ! modstr) {
87                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_strdup_no_spaces()");
88                 return NULL;
89         }
90
91         modules = talloc_realloc(mem_ctx, modules, char *, 2);
92         if ( ! modules ) {
93                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
94                 talloc_free(modstr);
95                 return NULL;
96         }
97         talloc_steal(modules, modstr);
98
99         if (modstr[0] == '\0') {
100                 modules[0] = NULL;
101                 m = (const char **)modules;
102                 return m;
103         }
104
105         i = 0;
106         /* The str*r*chr walks backwards:  This is how we get the inverse order mentioned above */
107         while ((p = strrchr(modstr, ',')) != NULL) {
108                 *p = '\0';
109                 p++;
110                 modules[i] = p;
111
112                 i++;
113                 modules = talloc_realloc(mem_ctx, modules, char *, i + 2);
114                 if ( ! modules ) {
115                         ldb_debug(ldb, LDB_DEBUG_FATAL, "Out of Memory in ldb_modules_list_from_string()");
116                         return NULL;
117                 }
118
119         }
120         modules[i] = modstr;
121
122         modules[i + 1] = NULL;
123
124         m = (const char **)modules;
125
126         return m;
127 }
128
129 static struct backends_list_entry {
130         struct ldb_backend_ops *ops;
131         struct backends_list_entry *prev, *next;
132 } *ldb_backends = NULL;
133
134 static struct ops_list_entry {
135         const struct ldb_module_ops *ops;
136         struct ops_list_entry *next;
137 } *registered_modules = NULL;
138
139 static const struct ldb_builtins {
140         const struct ldb_backend_ops *backend_ops;
141         const struct ldb_module_ops *module_ops;
142 } builtins[];
143
144 static ldb_connect_fn ldb_find_backend(const char *url)
145 {
146         struct backends_list_entry *backend;
147         unsigned int i;
148
149         for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
150                 if (builtins[i].backend_ops == NULL) continue;
151
152                 if (strncmp(builtins[i].backend_ops->name, url,
153                             strlen(builtins[i].backend_ops->name)) == 0) {
154                         return builtins[i].backend_ops->connect_fn;
155                 }
156         }
157
158         for (backend = ldb_backends; backend; backend = backend->next) {
159                 if (strncmp(backend->ops->name, url,
160                             strlen(backend->ops->name)) == 0) {
161                         return backend->ops->connect_fn;
162                 }
163         }
164
165         return NULL;
166 }
167
168 /*
169  register a new ldb backend
170 */
171 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
172 {
173         struct ldb_backend_ops *backend;
174         struct backends_list_entry *entry;
175
176         backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
177         if (!backend) return LDB_ERR_OPERATIONS_ERROR;
178
179         entry = talloc(talloc_autofree_context(), struct backends_list_entry);
180         if (!entry) {
181                 talloc_free(backend);
182                 return LDB_ERR_OPERATIONS_ERROR;
183         }
184
185         if (ldb_find_backend(url_prefix)) {
186                 return LDB_SUCCESS;
187         }
188
189         /* Maybe check for duplicity here later on? */
190
191         backend->name = talloc_strdup(backend, url_prefix);
192         backend->connect_fn = connectfn;
193         entry->ops = backend;
194         DLIST_ADD(ldb_backends, entry);
195
196         return LDB_SUCCESS;
197 }
198
199 /*
200    Return the ldb module form of a database.
201    The URL can either be one of the following forms
202    ldb://path
203    ldapi://path
204
205    flags is made up of LDB_FLG_*
206
207    the options are passed uninterpreted to the backend, and are
208    backend specific.
209
210    This allows modules to get at only the backend module, for example where a
211    module may wish to direct certain requests at a particular backend.
212 */
213 int ldb_connect_backend(struct ldb_context *ldb,
214                         const char *url,
215                         const char *options[],
216                         struct ldb_module **backend_module)
217 {
218         int ret;
219         char *backend;
220         ldb_connect_fn fn;
221
222         if (strchr(url, ':') != NULL) {
223                 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
224         } else {
225                 /* Default to tdb */
226                 backend = talloc_strdup(ldb, "tdb");
227         }
228
229         fn = ldb_find_backend(backend);
230
231         if (fn == NULL) {
232                 struct ldb_backend_ops *ops;
233                 char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
234                 if (symbol_name == NULL) {
235                         return LDB_ERR_OPERATIONS_ERROR;
236                 }
237                 ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
238                 if (ops != NULL) {
239                         fn = ops->connect_fn;
240                 }
241                 talloc_free(symbol_name);
242         }
243
244         talloc_free(backend);
245
246         if (fn == NULL) {
247                 ldb_debug(ldb, LDB_DEBUG_FATAL,
248                           "Unable to find backend for '%s'", url);
249                 return LDB_ERR_OTHER;
250         }
251
252         ret = fn(ldb, url, ldb->flags, options, backend_module);
253
254         if (ret != LDB_SUCCESS) {
255                 ldb_debug(ldb, LDB_DEBUG_ERROR,
256                           "Failed to connect to '%s'", url);
257                 return ret;
258         }
259         return ret;
260 }
261
262 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
263 {
264         struct ops_list_entry *e;
265         unsigned int i;
266
267         for (i = 0; builtins[i].backend_ops || builtins[i].module_ops; i++) {
268                 if (builtins[i].module_ops == NULL) continue;
269
270                 if (strcmp(builtins[i].module_ops->name, name) == 0)
271                         return builtins[i].module_ops;
272         }
273
274         for (e = registered_modules; e; e = e->next) {
275                 if (strcmp(e->ops->name, name) == 0)
276                         return e->ops;
277         }
278
279         return NULL;
280 }
281
282
283 int ldb_register_module(const struct ldb_module_ops *ops)
284 {
285         struct ops_list_entry *entry = talloc(talloc_autofree_context(), struct ops_list_entry);
286
287         if (ldb_find_module_ops(ops->name) != NULL)
288                 return -1;
289
290         if (entry == NULL)
291                 return -1;
292
293         entry->ops = ops;
294         entry->next = registered_modules;
295         registered_modules = entry;
296
297         return 0;
298 }
299
300 static void *ldb_dso_load_symbol(struct ldb_context *ldb, const char *name,
301                                  const char *symbol)
302 {
303         char *path;
304         void *handle;
305         void *sym;
306
307         if (ldb->modules_dir == NULL)
308                 return NULL;
309
310         path = talloc_asprintf(ldb, "%s/%s.%s", ldb->modules_dir, name,
311                                SHLIBEXT);
312
313         ldb_debug(ldb, LDB_DEBUG_TRACE, "trying to load %s from %s", name, path);
314
315         handle = dlopen(path, RTLD_NOW);
316         if (handle == NULL) {
317                 ldb_debug(ldb, LDB_DEBUG_WARNING, "unable to load %s from %s: %s", name, path, dlerror());
318                 return NULL;
319         }
320
321         sym = dlsym(handle, symbol);
322         if (sym == NULL) {
323                 ldb_debug(ldb, LDB_DEBUG_ERROR, "no symbol `%s' found in %s: %s", symbol, path, dlerror());
324                 return NULL;
325         }
326
327         talloc_free(path);
328
329         return sym;
330 }
331
332 int ldb_load_modules_list(struct ldb_context *ldb, const char **module_list, struct ldb_module *backend, struct ldb_module **out)
333 {
334         struct ldb_module *module;
335         unsigned int i;
336
337         module = backend;
338
339         for (i = 0; module_list && module_list[i] != NULL; i++) {
340                 struct ldb_module *current;
341                 const struct ldb_module_ops *ops;
342
343                 if (strcmp(module_list[i], "") == 0) {
344                         continue;
345                 }
346
347                 ops = ldb_find_module_ops(module_list[i]);
348                 if (ops == NULL) {
349                         char *symbol_name = talloc_asprintf(ldb, "ldb_%s_module_ops",
350                                                                                                 module_list[i]);
351                         if (symbol_name == NULL) {
352                                 return LDB_ERR_OPERATIONS_ERROR;
353                         }
354                         ops = ldb_dso_load_symbol(ldb, module_list[i], symbol_name);
355                         talloc_free(symbol_name);
356                 }
357
358                 if (ops == NULL) {
359                         ldb_debug(ldb, LDB_DEBUG_WARNING, "WARNING: Module [%s] not found",
360                                   module_list[i]);
361                         continue;
362                 }
363
364                 current = talloc_zero(ldb, struct ldb_module);
365                 if (current == NULL) {
366                         return LDB_ERR_OPERATIONS_ERROR;
367                 }
368                 talloc_set_name(current, "ldb_module: %s", module_list[i]);
369
370                 current->ldb = ldb;
371                 current->ops = ops;
372
373                 DLIST_ADD(module, current);
374         }
375         *out = module;
376         return LDB_SUCCESS;
377 }
378
379 int ldb_init_module_chain(struct ldb_context *ldb, struct ldb_module *module)
380 {
381         while (module && module->ops->init_context == NULL)
382                 module = module->next;
383
384         /* init is different in that it is not an error if modules
385          * do not require initialization */
386
387         if (module) {
388                 int ret = module->ops->init_context(module);
389                 if (ret != LDB_SUCCESS) {
390                         ldb_debug(ldb, LDB_DEBUG_FATAL, "module %s initialization failed", module->ops->name);
391                         return ret;
392                 }
393         }
394
395         return LDB_SUCCESS;
396 }
397
398 int ldb_load_modules(struct ldb_context *ldb, const char *options[])
399 {
400         const char *modules_string;
401         const char **modules = NULL;
402         int ret;
403         TALLOC_CTX *mem_ctx = talloc_new(ldb);
404         if (!mem_ctx) {
405                 return LDB_ERR_OPERATIONS_ERROR;
406         }
407
408         /* find out which modules we are requested to activate */
409
410         /* check if we have a custom module list passd as ldb option */
411         if (options) {
412                 modules_string = ldb_options_find(ldb, options, "modules");
413                 if (modules_string) {
414                         modules = ldb_modules_list_from_string(ldb, mem_ctx, modules_string);
415                 }
416         }
417
418         /* if not overloaded by options and the backend is not ldap try to load the modules list from ldb */
419         if ((modules == NULL) && (strcmp("ldap", ldb->modules->ops->name) != 0)) {
420                 const char * const attrs[] = { "@LIST" , NULL};
421                 struct ldb_result *res = NULL;
422                 struct ldb_dn *mods_dn;
423
424                 mods_dn = ldb_dn_new(mem_ctx, ldb, "@MODULES");
425                 if (mods_dn == NULL) {
426                         talloc_free(mem_ctx);
427                         return -1;
428                 }
429
430                 ret = ldb_search(ldb, mods_dn, &res, mods_dn, LDB_SCOPE_BASE, attrs, "@LIST=*");
431
432                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
433                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
434                 } else if (ret != LDB_SUCCESS) {
435                         ldb_debug(ldb, LDB_DEBUG_FATAL, "ldb error (%s) occurred searching for modules, bailing out", ldb_errstring(ldb));
436                         talloc_free(mem_ctx);
437                         return ret;
438                 } else {
439                         const char *module_list;
440                         if (res->count == 0) {
441                                 ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
442                         } else if (res->count > 1) {
443                                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Too many records found (%d), bailing out", res->count);
444                                 talloc_free(mem_ctx);
445                                 return -1;
446                         } else {
447                                 module_list = ldb_msg_find_attr_as_string(res->msgs[0], "@LIST", NULL);
448                                 if (!module_list) {
449                                         ldb_debug(ldb, LDB_DEBUG_TRACE, "no modules required by the db");
450                                 }
451                                 modules = ldb_modules_list_from_string(ldb, mem_ctx,
452                                                                module_list);
453                         }
454                 }
455
456                 talloc_free(mods_dn);
457         }
458
459         if (modules != NULL) {
460                 ret = ldb_load_modules_list(ldb, modules, ldb->modules, &ldb->modules);
461                 if (ret != LDB_SUCCESS) {
462                         talloc_free(mem_ctx);
463                         return ret;
464                 }
465         } else {
466                 ldb_debug(ldb, LDB_DEBUG_TRACE, "No modules specified for this database");
467         }
468
469         ret = ldb_init_module_chain(ldb, ldb->modules);
470         talloc_free(mem_ctx);
471         return ret;
472 }
473
474 /*
475   by using this we allow ldb modules to only implement the functions they care about,
476   which makes writing a module simpler, and makes it more likely to keep working
477   when ldb is extended
478 */
479 #define FIND_OP_NOERR(module, op) do { \
480         module = module->next; \
481         while (module && module->ops->op == NULL) module = module->next; \
482         if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { \
483                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_trace_next_request: (%s)->" #op, \
484                           module->ops->name);                           \
485         }                                                               \
486 } while (0)
487
488 #define FIND_OP(module, op) do { \
489         struct ldb_context *ldb = module->ldb; \
490         FIND_OP_NOERR(module, op); \
491         if (module == NULL) { \
492                 ldb_asprintf_errstring(ldb, "Unable to find backend operation for " #op ); \
493                 return LDB_ERR_OPERATIONS_ERROR;        \
494         }                                               \
495 } while (0)
496
497
498 struct ldb_module *ldb_module_new(TALLOC_CTX *memctx,
499                                   struct ldb_context *ldb,
500                                   const char *module_name,
501                                   const struct ldb_module_ops *ops)
502 {
503         struct ldb_module *module;
504
505         module = talloc(memctx, struct ldb_module);
506         if (!module) {
507                 ldb_oom(ldb);
508                 return NULL;
509         }
510         talloc_set_name_const(module, module_name);
511         module->ldb = ldb;
512         module->prev = module->next = NULL;
513         module->ops = ops;
514
515         return module;
516 }
517
518 const char * ldb_module_get_name(struct ldb_module *module)
519 {
520         return module->ops->name;
521 }
522
523 struct ldb_context *ldb_module_get_ctx(struct ldb_module *module)
524 {
525         return module->ldb;
526 }
527
528 const struct ldb_module_ops *ldb_module_get_ops(struct ldb_module *module)
529 {
530         return module->ops;
531 }
532
533 void *ldb_module_get_private(struct ldb_module *module)
534 {
535         return module->private_data;
536 }
537
538 void ldb_module_set_private(struct ldb_module *module, void *private_data)
539 {
540         module->private_data = private_data;
541 }
542
543 /*
544    helper functions to call the next module in chain
545 */
546
547 int ldb_next_request(struct ldb_module *module, struct ldb_request *request)
548 {
549         int ret;
550
551         if (request->callback == NULL) {
552                 ldb_set_errstring(module->ldb, "Requests MUST define callbacks");
553                 return LDB_ERR_UNWILLING_TO_PERFORM;
554         }
555
556         request->handle->nesting++;
557
558         switch (request->operation) {
559         case LDB_SEARCH:
560                 FIND_OP(module, search);
561                 ret = module->ops->search(module, request);
562                 break;
563         case LDB_ADD:
564                 FIND_OP(module, add);
565                 ret = module->ops->add(module, request);
566                 break;
567         case LDB_MODIFY:
568                 FIND_OP(module, modify);
569                 ret = module->ops->modify(module, request);
570                 break;
571         case LDB_DELETE:
572                 FIND_OP(module, del);
573                 ret = module->ops->del(module, request);
574                 break;
575         case LDB_RENAME:
576                 FIND_OP(module, rename);
577                 ret = module->ops->rename(module, request);
578                 break;
579         case LDB_EXTENDED:
580                 FIND_OP(module, extended);
581                 ret = module->ops->extended(module, request);
582                 break;
583         default:
584                 FIND_OP(module, request);
585                 ret = module->ops->request(module, request);
586                 break;
587         }
588
589         request->handle->nesting--;
590
591         if (ret == LDB_SUCCESS) {
592                 return ret;
593         }
594         if (!ldb_errstring(module->ldb)) {
595                 /* Set a default error string, to place the blame somewhere */
596                 ldb_asprintf_errstring(module->ldb, "error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
597         }
598
599         if (!(request->handle->flags & LDB_HANDLE_FLAG_DONE_CALLED)) {
600                 /* It is _extremely_ common that a module returns a
601                  * failure without calling ldb_module_done(), but that
602                  * guarantees we will end up hanging in
603                  * ldb_wait(). This fixes it without having to rewrite
604                  * all our modules, and leaves us one less sharp
605                  * corner for module developers to cut themselves on
606                  */
607                 ret = ldb_module_done(request, NULL, NULL, ret);
608         }
609         return ret;
610 }
611
612 int ldb_next_init(struct ldb_module *module)
613 {
614         module = module->next;
615
616         return ldb_init_module_chain(module->ldb, module);
617 }
618
619 int ldb_next_start_trans(struct ldb_module *module)
620 {
621         int ret;
622         FIND_OP(module, start_transaction);
623         ret = module->ops->start_transaction(module);
624         if (ret == LDB_SUCCESS) {
625                 return ret;
626         }
627         if (!ldb_errstring(module->ldb)) {
628                 /* Set a default error string, to place the blame somewhere */
629                 ldb_asprintf_errstring(module->ldb, "start_trans error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
630         }
631         if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
632                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_start_trans error: %s", 
633                           ldb_errstring(module->ldb));                          
634         }
635         return ret;
636 }
637
638 int ldb_next_end_trans(struct ldb_module *module)
639 {
640         int ret;
641         FIND_OP(module, end_transaction);
642         ret = module->ops->end_transaction(module);
643         if (ret == LDB_SUCCESS) {
644                 return ret;
645         }
646         if (!ldb_errstring(module->ldb)) {
647                 /* Set a default error string, to place the blame somewhere */
648                 ldb_asprintf_errstring(module->ldb, "end_trans error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
649         }
650         if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
651                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_end_trans error: %s", 
652                           ldb_errstring(module->ldb));                          
653         }
654         return ret;
655 }
656
657 int ldb_next_prepare_commit(struct ldb_module *module)
658 {
659         int ret;
660         FIND_OP_NOERR(module, prepare_commit);
661         if (module == NULL) {
662                 /* we are allowed to have no prepare commit in
663                    backends */
664                 return LDB_SUCCESS;
665         }
666         ret = module->ops->prepare_commit(module);
667         if (ret == LDB_SUCCESS) {
668                 return ret;
669         }
670         if (!ldb_errstring(module->ldb)) {
671                 /* Set a default error string, to place the blame somewhere */
672                 ldb_asprintf_errstring(module->ldb, "prepare_commit error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
673         }
674         if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
675                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_prepare_commit error: %s", 
676                           ldb_errstring(module->ldb));                          
677         }
678         return ret;
679 }
680
681 int ldb_next_del_trans(struct ldb_module *module)
682 {
683         int ret;
684         FIND_OP(module, del_transaction);
685         ret = module->ops->del_transaction(module);
686         if (ret == LDB_SUCCESS) {
687                 return ret;
688         }
689         if (!ldb_errstring(module->ldb)) {
690                 /* Set a default error string, to place the blame somewhere */
691                 ldb_asprintf_errstring(module->ldb, "del_trans error in module %s: %s (%d)", module->ops->name, ldb_strerror(ret), ret);
692         }
693         if ((module && module->ldb->flags & LDB_FLG_ENABLE_TRACING)) { 
694                 ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_next_del_trans error: %s", 
695                           ldb_errstring(module->ldb));                          
696         }
697         return ret;
698 }
699
700 struct ldb_handle *ldb_handle_new(TALLOC_CTX *mem_ctx, struct ldb_context *ldb)
701 {
702         struct ldb_handle *h;
703
704         h = talloc_zero(mem_ctx, struct ldb_handle);
705         if (h == NULL) {
706                 ldb_set_errstring(ldb, "Out of Memory");
707                 return NULL;
708         }
709
710         h->status = LDB_SUCCESS;
711         h->state = LDB_ASYNC_INIT;
712         h->ldb = ldb;
713         h->flags = 0;
714         h->location = NULL;
715         h->parent = NULL;
716
717         return h;
718 }
719
720 /* calls the request callback to send an entry
721  *
722  * params:
723  *      req: the original request passed to your module
724  *      msg: reply message (must be a talloc pointer, and it will be stolen
725  *           on the ldb_reply that is sent to the callback)
726  *      ctrls: controls to send in the reply  (must be a talloc pointer, and it will be stolen
727  *           on the ldb_reply that is sent to the callback)
728  */
729
730 int ldb_module_send_entry(struct ldb_request *req,
731                           struct ldb_message *msg,
732                           struct ldb_control **ctrls)
733 {
734         struct ldb_reply *ares;
735
736         ares = talloc_zero(req, struct ldb_reply);
737         if (!ares) {
738                 ldb_oom(req->handle->ldb);
739                 req->callback(req, NULL);
740                 return LDB_ERR_OPERATIONS_ERROR;
741         }
742         ares->type = LDB_REPLY_ENTRY;
743         ares->message = talloc_steal(ares, msg);
744         ares->controls = talloc_steal(ares, ctrls);
745         ares->error = LDB_SUCCESS;
746
747         if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
748             req->handle->nesting == 0) {
749                 char *s;
750                 ldb_debug_add(req->handle->ldb, "ldb_trace_response: ENTRY\n");
751                 s = ldb_ldif_message_string(req->handle->ldb, msg, LDB_CHANGETYPE_NONE, msg);
752                 ldb_debug_add(req->handle->ldb, "%s\n", s);
753                 talloc_free(s);
754                 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
755         }
756
757         return req->callback(req, ares);
758 }
759
760 /* calls the request callback to send an referrals
761  *
762  * params:
763  *      req: the original request passed to your module
764  *      ref: referral string (must be a talloc pointeri, steal)
765  */
766
767 int ldb_module_send_referral(struct ldb_request *req,
768                                            char *ref)
769 {
770         struct ldb_reply *ares;
771
772         ares = talloc_zero(req, struct ldb_reply);
773         if (!ares) {
774                 ldb_oom(req->handle->ldb);
775                 req->callback(req, NULL);
776                 return LDB_ERR_OPERATIONS_ERROR;
777         }
778         ares->type = LDB_REPLY_REFERRAL;
779         ares->referral = talloc_steal(ares, ref);
780         ares->error = LDB_SUCCESS;
781
782         if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
783             req->handle->nesting == 0) {
784                 ldb_debug_add(req->handle->ldb, "ldb_trace_response: REFERRAL\n");
785                 ldb_debug_add(req->handle->ldb, "ref: %s\n", ref);
786                 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
787         }
788
789         return req->callback(req, ares);
790 }
791
792 /* calls the original request callback
793  *
794  * params:
795  *      req:   the original request passed to your module
796  *      ctrls: controls to send in the reply (must be a talloc pointer, steal)
797  *      response: results for extended request (steal)
798  *      error: LDB_SUCCESS for a successful return
799  *             any other ldb error otherwise
800  */
801 int ldb_module_done(struct ldb_request *req,
802                     struct ldb_control **ctrls,
803                     struct ldb_extended *response,
804                     int error)
805 {
806         struct ldb_reply *ares;
807
808         ares = talloc_zero(req, struct ldb_reply);
809         if (!ares) {
810                 ldb_oom(req->handle->ldb);
811                 req->callback(req, NULL);
812                 return LDB_ERR_OPERATIONS_ERROR;
813         }
814         ares->type = LDB_REPLY_DONE;
815         ares->controls = talloc_steal(ares, ctrls);
816         ares->response = talloc_steal(ares, response);
817         ares->error = error;
818
819         req->handle->flags |= LDB_HANDLE_FLAG_DONE_CALLED;
820
821         if ((req->handle->ldb->flags & LDB_FLG_ENABLE_TRACING) &&
822             req->handle->nesting == 0) {
823                 ldb_debug_add(req->handle->ldb, "ldb_trace_response: DONE\n");
824                 ldb_debug_add(req->handle->ldb, "error: %u\n", error);
825                 if (ldb_errstring(req->handle->ldb)) {
826                         ldb_debug_add(req->handle->ldb, "msg: %s\n",
827                                   ldb_errstring(req->handle->ldb));
828                 }
829                 ldb_debug_end(req->handle->ldb, LDB_DEBUG_TRACE);
830         }
831
832         return req->callback(req, ares);
833 }
834
835 /* to be used *only* in modules init functions.
836  * this function is synchronous and will register
837  * the requested OID in the rootdse module if present
838  * otherwise it will return an error */
839 int ldb_mod_register_control(struct ldb_module *module, const char *oid)
840 {
841         struct ldb_request *req;
842         int ret;
843
844         req = talloc_zero(module, struct ldb_request);
845         if (req == NULL) {
846                 return LDB_ERR_OPERATIONS_ERROR;
847         }
848
849         req->operation = LDB_REQ_REGISTER_CONTROL;
850         req->op.reg_control.oid = oid;
851         req->callback = ldb_op_default_callback;
852
853         ldb_set_timeout(module->ldb, req, 0);
854
855         req->handle = ldb_handle_new(req, module->ldb);
856         if (req->handle == NULL) {
857                 return LDB_ERR_OPERATIONS_ERROR;
858         }
859
860         ret = ldb_request(module->ldb, req);
861         if (ret == LDB_SUCCESS) {
862                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
863         }
864         talloc_free(req);
865
866         return ret;
867 }
868
869 #ifdef STATIC_ldb_MODULES
870 #define STATIC_LIBLDB_MODULES STATIC_ldb_MODULES
871 #endif
872
873 #ifndef STATIC_LIBLDB_MODULES
874
875 #ifdef HAVE_LDB_LDAP
876 #define LDAP_BACKEND LDB_BACKEND(ldap), LDB_BACKEND(ldapi), LDB_BACKEND(ldaps),
877 #else
878 #define LDAP_BACKEND
879 #endif
880
881 #ifdef HAVE_LDB_SQLITE3
882 #define SQLITE3_BACKEND LDB_BACKEND(sqlite3),
883 #else
884 #define SQLITE3_BACKEND
885 #endif
886
887 #define STATIC_LIBLDB_MODULES \
888         LDB_BACKEND(tdb),       \
889         LDAP_BACKEND    \
890         SQLITE3_BACKEND \
891         LDB_MODULE(rdn_name),   \
892         LDB_MODULE(paged_results),      \
893         LDB_MODULE(server_sort),                \
894         LDB_MODULE(asq), \
895         NULL
896 #endif
897
898 /*
899  * this is a bit hacked, as STATIC_LIBLDB_MODULES contains ','
900  * between the elements and we want to autogenerate the
901  * extern struct declarations, so we do some hacks and let the
902  * ',' appear in an unused function prototype.
903  */
904 #undef NULL
905 #define NULL LDB_MODULE(NULL),
906
907 #define LDB_BACKEND(name) \
908         int); \
909         extern const struct ldb_backend_ops ldb_ ## name ## _backend_ops;\
910         extern void ldb_noop ## name (int
911 #define LDB_MODULE(name) \
912         int); \
913         extern const struct ldb_module_ops ldb_ ## name ## _module_ops;\
914         extern void ldb_noop ## name (int
915
916 extern void ldb_start_noop(int,
917 STATIC_LIBLDB_MODULES
918 int);
919
920 #undef NULL
921 #define NULL { \
922         .backend_ops = (void *)0, \
923         .module_ops = (void *)0 \
924 }
925
926 #undef LDB_BACKEND
927 #define LDB_BACKEND(name) { \
928         .backend_ops = &ldb_ ## name ## _backend_ops, \
929         .module_ops = (void *)0 \
930 }
931 #undef LDB_MODULE
932 #define LDB_MODULE(name) { \
933         .backend_ops = (void *)0, \
934         .module_ops = &ldb_ ## name ## _module_ops \
935 }
936
937 static const struct ldb_builtins builtins[] = {
938         STATIC_LIBLDB_MODULES
939 };