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