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