4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Simo Sorce 2005-2008
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 3 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, see <http://www.gnu.org/licenses/>.
28 * Component: ldb core API
30 * Description: core API routines interfacing to ldb backends
32 * Author: Andrew Tridgell
35 #include "ldb_includes.h"
38 initialise a ldb context
39 The mem_ctx is required
40 The event_ctx is required
42 struct ldb_context
*ldb_init(TALLOC_CTX
*mem_ctx
, struct event_context
*ev_ctx
)
44 struct ldb_context
*ldb
;
47 ldb
= talloc_zero(mem_ctx
, struct ldb_context
);
48 /* FIXME: Hack a new event context so that CMD line utilities work
49 * until we have them all converted */
51 ev_ctx
= event_context_init(talloc_autofree_context());
54 ret
= ldb_setup_wellknown_attributes(ldb
);
60 ldb_set_utf8_default(ldb
);
61 ldb_set_create_perms(ldb
, 0666);
62 ldb_set_modules_dir(ldb
, LDB_MODULESDIR
);
63 ldb_set_event_context(ldb
, ev_ctx
);
65 /* TODO: get timeout from options if available there */
66 ldb
->default_timeout
= 300; /* set default to 5 minutes */
71 static struct backends_list_entry
{
72 struct ldb_backend_ops
*ops
;
73 struct backends_list_entry
*prev
, *next
;
74 } *ldb_backends
= NULL
;
76 #ifndef STATIC_LIBLDB_BACKENDS
79 #define LDAP_INIT &ldb_ldap_backend_ops, \
80 &ldb_ldapi_backend_ops, \
81 &ldb_ldaps_backend_ops,
86 #ifdef HAVE_LDB_SQLITE3
87 #define SQLITE3_INIT &ldb_sqlite3_backend_ops,
92 #define STATIC_LIBLDB_BACKENDS \
95 &ldb_tdb_backend_ops, \
99 const static struct ldb_backend_ops
*builtin_backends
[] = {
100 STATIC_LIBLDB_BACKENDS
103 static ldb_connect_fn
ldb_find_backend(const char *url
)
105 struct backends_list_entry
*backend
;
108 for (i
= 0; builtin_backends
[i
]; i
++) {
109 if (strncmp(builtin_backends
[i
]->name
, url
,
110 strlen(builtin_backends
[i
]->name
)) == 0)
111 return builtin_backends
[i
]->connect_fn
;
114 for (backend
= ldb_backends
; backend
; backend
= backend
->next
) {
115 if (strncmp(backend
->ops
->name
, url
,
116 strlen(backend
->ops
->name
)) == 0) {
117 return backend
->ops
->connect_fn
;
125 register a new ldb backend
127 int ldb_register_backend(const char *url_prefix
, ldb_connect_fn connectfn
)
129 struct ldb_backend_ops
*backend
;
130 struct backends_list_entry
*entry
;
132 backend
= talloc(talloc_autofree_context(), struct ldb_backend_ops
);
133 if (!backend
) return LDB_ERR_OPERATIONS_ERROR
;
135 entry
= talloc(talloc_autofree_context(), struct backends_list_entry
);
137 talloc_free(backend
);
138 return LDB_ERR_OPERATIONS_ERROR
;
141 if (ldb_find_backend(url_prefix
)) {
145 /* Maybe check for duplicity here later on? */
147 backend
->name
= talloc_strdup(backend
, url_prefix
);
148 backend
->connect_fn
= connectfn
;
149 entry
->ops
= backend
;
150 DLIST_ADD(ldb_backends
, entry
);
156 Return the ldb module form of a database.
157 The URL can either be one of the following forms
161 flags is made up of LDB_FLG_*
163 the options are passed uninterpreted to the backend, and are
166 This allows modules to get at only the backend module, for example where a
167 module may wish to direct certain requests at a particular backend.
169 int ldb_connect_backend(struct ldb_context
*ldb
,
171 const char *options
[],
172 struct ldb_module
**backend_module
)
178 if (strchr(url
, ':') != NULL
) {
179 backend
= talloc_strndup(ldb
, url
, strchr(url
, ':')-url
);
182 backend
= talloc_strdup(ldb
, "tdb");
185 fn
= ldb_find_backend(backend
);
188 int (*init_fn
) (void);
190 init_fn
= ldb_dso_load_symbol(ldb
, backend
,
192 if (init_fn
!= NULL
&& init_fn() == 0) {
193 fn
= ldb_find_backend(backend
);
198 struct ldb_backend_ops
*ops
;
199 char *symbol_name
= talloc_asprintf(ldb
, "ldb_%s_backend_ops", backend
);
200 if (symbol_name
== NULL
) {
201 return LDB_ERR_OPERATIONS_ERROR
;
203 ops
= ldb_dso_load_symbol(ldb
, backend
, symbol_name
);
205 fn
= ops
->connect_fn
;
207 talloc_free(symbol_name
);
210 talloc_free(backend
);
213 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
214 "Unable to find backend for '%s'\n", url
);
215 return LDB_ERR_OTHER
;
218 ret
= fn(ldb
, url
, ldb
->flags
, options
, backend_module
);
220 if (ret
!= LDB_SUCCESS
) {
221 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
222 "Failed to connect to '%s'\n", url
);
229 try to autodetect a basedn if none specified. This fixes one of my
230 pet hates about ldapsearch, which is that you have to get a long,
231 complex basedn right to make any use of it.
233 void ldb_set_default_dns(struct ldb_context
*ldb
)
237 struct ldb_result
*res
;
238 struct ldb_dn
*tmp_dn
=NULL
;
239 static const char *attrs
[] = {
240 "rootDomainNamingContext",
241 "configurationNamingContext",
242 "schemaNamingContext",
243 "defaultNamingContext",
247 tmp_ctx
= talloc_new(ldb
);
248 ret
= ldb_search(ldb
, ldb_dn_new(tmp_ctx
, ldb
, NULL
), LDB_SCOPE_BASE
,
249 "(objectClass=*)", attrs
, &res
);
250 if (ret
!= LDB_SUCCESS
) {
251 talloc_free(tmp_ctx
);
255 if (res
->count
!= 1) {
260 if (!ldb_get_opaque(ldb
, "rootDomainNamingContext")) {
261 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
262 "rootDomainNamingContext");
263 ldb_set_opaque(ldb
, "rootDomainNamingContext", tmp_dn
);
266 if (!ldb_get_opaque(ldb
, "configurationNamingContext")) {
267 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
268 "configurationNamingContext");
269 ldb_set_opaque(ldb
, "configurationNamingContext", tmp_dn
);
272 if (!ldb_get_opaque(ldb
, "schemaNamingContext")) {
273 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
274 "schemaNamingContext");
275 ldb_set_opaque(ldb
, "schemaNamingContext", tmp_dn
);
278 if (!ldb_get_opaque(ldb
, "defaultNamingContext")) {
279 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
280 "defaultNamingContext");
281 ldb_set_opaque(ldb
, "defaultNamingContext", tmp_dn
);
285 talloc_free(tmp_ctx
);
288 struct ldb_dn
*ldb_get_root_basedn(struct ldb_context
*ldb
)
290 void *opaque
= ldb_get_opaque(ldb
, "rootDomainNamingContext");
291 return talloc_get_type(opaque
, struct ldb_dn
);
294 struct ldb_dn
*ldb_get_config_basedn(struct ldb_context
*ldb
)
296 void *opaque
= ldb_get_opaque(ldb
, "configurationNamingContext");
297 return talloc_get_type(opaque
, struct ldb_dn
);
300 struct ldb_dn
*ldb_get_schema_basedn(struct ldb_context
*ldb
)
302 void *opaque
= ldb_get_opaque(ldb
, "schemaNamingContext");
303 return talloc_get_type(opaque
, struct ldb_dn
);
306 struct ldb_dn
*ldb_get_default_basedn(struct ldb_context
*ldb
)
308 void *opaque
= ldb_get_opaque(ldb
, "defaultNamingContext");
309 return talloc_get_type(opaque
, struct ldb_dn
);
313 connect to a database. The URL can either be one of the following forms
317 flags is made up of LDB_FLG_*
319 the options are passed uninterpreted to the backend, and are
322 int ldb_connect(struct ldb_context
*ldb
, const char *url
,
323 unsigned int flags
, const char *options
[])
327 /* We seem to need to do this here, or else some utilities don't
328 * get ldb backends */
332 url2
= talloc_strdup(ldb
, url
);
335 return LDB_ERR_OPERATIONS_ERROR
;
337 ret
= ldb_set_opaque(ldb
, "ldb_url", talloc_strdup(ldb
, url2
));
338 if (ret
!= LDB_SUCCESS
) {
342 ret
= ldb_connect_backend(ldb
, url
, options
, &ldb
->modules
);
343 if (ret
!= LDB_SUCCESS
) {
347 if (ldb_load_modules(ldb
, options
) != LDB_SUCCESS
) {
348 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
349 "Unable to load modules for %s: %s\n",
350 url
, ldb_errstring(ldb
));
351 return LDB_ERR_OTHER
;
354 /* TODO: get timeout from options if available there */
355 ldb
->default_timeout
= 300; /* set default to 5 minutes */
357 /* set the default base dn */
358 ldb_set_default_dns(ldb
);
363 void ldb_set_errstring(struct ldb_context
*ldb
, const char *err_string
)
365 if (ldb
->err_string
) {
366 talloc_free(ldb
->err_string
);
368 ldb
->err_string
= talloc_strdup(ldb
, err_string
);
371 void ldb_asprintf_errstring(struct ldb_context
*ldb
, const char *format
, ...)
374 char *old_string
= NULL
;
376 if (ldb
->err_string
) {
377 old_string
= ldb
->err_string
;
380 va_start(ap
, format
);
381 ldb
->err_string
= talloc_vasprintf(ldb
, format
, ap
);
383 talloc_free(old_string
);
386 void ldb_reset_err_string(struct ldb_context
*ldb
)
388 if (ldb
->err_string
) {
389 talloc_free(ldb
->err_string
);
390 ldb
->err_string
= NULL
;
394 #define FIRST_OP(ldb, op) do { \
395 module = ldb->modules; \
396 while (module && module->ops->op == NULL) module = module->next; \
397 if (module == NULL) { \
398 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
399 return LDB_ERR_OPERATIONS_ERROR; \
406 static int ldb_transaction_start_internal(struct ldb_context
*ldb
)
408 struct ldb_module
*module
;
410 FIRST_OP(ldb
, start_transaction
);
412 ldb_reset_err_string(ldb
);
414 status
= module
->ops
->start_transaction(module
);
415 if (status
!= LDB_SUCCESS
) {
416 if (ldb
->err_string
== NULL
) {
417 /* no error string was setup by the backend */
418 ldb_asprintf_errstring(ldb
,
419 "ldb transaction start: %s (%d)",
420 ldb_strerror(status
),
430 static int ldb_transaction_commit_internal(struct ldb_context
*ldb
)
432 struct ldb_module
*module
;
434 FIRST_OP(ldb
, end_transaction
);
436 ldb_reset_err_string(ldb
);
438 status
= module
->ops
->end_transaction(module
);
439 if (status
!= LDB_SUCCESS
) {
440 if (ldb
->err_string
== NULL
) {
441 /* no error string was setup by the backend */
442 ldb_asprintf_errstring(ldb
,
443 "ldb transaction commit: %s (%d)",
444 ldb_strerror(status
),
454 static int ldb_transaction_cancel_internal(struct ldb_context
*ldb
)
456 struct ldb_module
*module
;
458 FIRST_OP(ldb
, del_transaction
);
460 status
= module
->ops
->del_transaction(module
);
461 if (status
!= LDB_SUCCESS
) {
462 if (ldb
->err_string
== NULL
) {
463 /* no error string was setup by the backend */
464 ldb_asprintf_errstring(ldb
,
465 "ldb transaction cancel: %s (%d)",
466 ldb_strerror(status
),
473 int ldb_transaction_start(struct ldb_context
*ldb
)
475 /* disable autotransactions */
476 ldb
->transaction_active
++;
478 return ldb_transaction_start_internal(ldb
);
481 int ldb_transaction_commit(struct ldb_context
*ldb
)
483 /* renable autotransactions (when we reach 0) */
484 if (ldb
->transaction_active
> 0)
485 ldb
->transaction_active
--;
487 return ldb_transaction_commit_internal(ldb
);
490 int ldb_transaction_cancel(struct ldb_context
*ldb
)
492 /* renable autotransactions (when we reach 0) */
493 if (ldb
->transaction_active
> 0)
494 ldb
->transaction_active
--;
496 return ldb_transaction_cancel_internal(ldb
);
499 static int ldb_autotransaction_start(struct ldb_context
*ldb
)
501 /* explicit transaction active, ignore autotransaction request */
502 if (ldb
->transaction_active
)
505 return ldb_transaction_start_internal(ldb
);
508 static int ldb_autotransaction_commit(struct ldb_context
*ldb
)
510 /* explicit transaction active, ignore autotransaction request */
511 if (ldb
->transaction_active
)
514 return ldb_transaction_commit_internal(ldb
);
517 static int ldb_autotransaction_cancel(struct ldb_context
*ldb
)
519 /* explicit transaction active, ignore autotransaction request */
520 if (ldb
->transaction_active
)
523 return ldb_transaction_cancel_internal(ldb
);
526 /* autostarts a transacion if none active */
527 static int ldb_autotransaction_request(struct ldb_context
*ldb
,
528 struct ldb_request
*req
)
532 ret
= ldb_autotransaction_start(ldb
);
533 if (ret
!= LDB_SUCCESS
) {
537 ret
= ldb_request(ldb
, req
);
538 if (ret
== LDB_SUCCESS
) {
539 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
542 if (ret
== LDB_SUCCESS
) {
543 return ldb_autotransaction_commit(ldb
);
545 ldb_autotransaction_cancel(ldb
);
547 if (ldb
->err_string
== NULL
) {
548 /* no error string was setup by the backend */
549 ldb_asprintf_errstring(ldb
, "%s (%d)", ldb_strerror(ret
), ret
);
555 int ldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
562 ret
= handle
->module
->ops
->wait(handle
, type
);
563 if (!ldb_errstring(handle
->module
->ldb
)) {
564 /* Set a default error string, to place the blame somewhere */
565 ldb_asprintf_errstring(handle
->module
->ldb
,
566 "error waiting on module %s: %s (%d)",
567 handle
->module
->ops
->name
,
568 ldb_strerror(ret
), ret
);
573 /* set the specified timeout or, if timeout is 0 set the default timeout */
574 /* timeout == -1 means no timeout */
575 int ldb_set_timeout(struct ldb_context
*ldb
,
576 struct ldb_request
*req
,
579 if (req
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
582 req
->timeout
= timeout
;
584 req
->timeout
= ldb
->default_timeout
;
586 req
->starttime
= time(NULL
);
591 /* calculates the new timeout based on the previous starttime and timeout */
592 int ldb_set_timeout_from_prev_req(struct ldb_context
*ldb
,
593 struct ldb_request
*oldreq
,
594 struct ldb_request
*newreq
)
598 if (newreq
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
603 return ldb_set_timeout(ldb
, newreq
, 0);
605 if ((now
- oldreq
->starttime
) > oldreq
->timeout
) {
606 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
608 newreq
->starttime
= oldreq
->starttime
;
609 newreq
->timeout
= oldreq
->timeout
- (now
- oldreq
->starttime
);
616 set the permissions for new files to be passed to open() in
617 backends that use local files
619 void ldb_set_create_perms(struct ldb_context
*ldb
, unsigned int perms
)
621 ldb
->create_perms
= perms
;
624 void ldb_set_event_context(struct ldb_context
*ldb
, struct event_context
*ev
)
629 struct event_context
* ldb_get_event_context(struct ldb_context
*ldb
)
636 NOTE: the request must be a talloc context.
637 returns LDB_ERR_* on errors.
639 int ldb_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
641 struct ldb_module
*module
;
644 ldb_reset_err_string(ldb
);
646 /* call the first module in the chain */
647 switch (req
->operation
) {
649 FIRST_OP(ldb
, search
);
650 ret
= module
->ops
->search(module
, req
);
654 ret
= module
->ops
->add(module
, req
);
657 FIRST_OP(ldb
, modify
);
658 ret
= module
->ops
->modify(module
, req
);
662 ret
= module
->ops
->del(module
, req
);
665 FIRST_OP(ldb
, rename
);
666 ret
= module
->ops
->rename(module
, req
);
669 FIRST_OP(ldb
, extended
);
670 ret
= module
->ops
->extended(module
, req
);
672 case LDB_SEQUENCE_NUMBER
:
673 FIRST_OP(ldb
, sequence_number
);
674 ret
= module
->ops
->sequence_number(module
, req
);
677 FIRST_OP(ldb
, request
);
678 ret
= module
->ops
->request(module
, req
);
686 search the database given a LDAP-like search expression
688 returns an LDB error code
690 Use talloc_free to free the ldb_message returned in 'res', if successful
693 int ldb_search_default_callback(struct ldb_context
*ldb
,
695 struct ldb_reply
*ares
)
697 struct ldb_result
*res
;
701 ldb_set_errstring(ldb
, "NULL Context in callback");
702 return LDB_ERR_OPERATIONS_ERROR
;
705 res
= talloc_get_type(context
, struct ldb_result
);
708 ldb_set_errstring(ldb
, "NULL res or ares in callback");
712 switch (ares
->type
) {
713 case LDB_REPLY_ENTRY
:
714 res
->msgs
= talloc_realloc(res
, res
->msgs
,
715 struct ldb_message
*,
721 res
->msgs
[res
->count
+ 1] = NULL
;
723 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
726 case LDB_REPLY_REFERRAL
:
728 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
733 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
738 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
739 res
->refs
[n
+ 1] = NULL
;
741 case LDB_REPLY_EXTENDED
:
743 /* TODO: we should really support controls on entries
744 * and referrals too! */
745 res
->controls
= talloc_move(res
, &ares
->controls
);
753 return LDB_ERR_OPERATIONS_ERROR
;
756 int ldb_build_search_req(struct ldb_request
**ret_req
,
757 struct ldb_context
*ldb
,
760 enum ldb_scope scope
,
761 const char *expression
,
762 const char * const *attrs
,
763 struct ldb_control
**controls
,
765 ldb_request_callback_t callback
)
767 struct ldb_request
*req
;
771 req
= talloc(mem_ctx
, struct ldb_request
);
773 ldb_set_errstring(ldb
, "Out of Memory");
774 return LDB_ERR_OPERATIONS_ERROR
;
777 req
->operation
= LDB_SEARCH
;
779 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
781 req
->op
.search
.base
= base
;
783 req
->op
.search
.scope
= scope
;
785 req
->op
.search
.tree
= ldb_parse_tree(req
, expression
);
786 if (req
->op
.search
.tree
== NULL
) {
787 ldb_set_errstring(ldb
, "Unable to parse search expression");
789 return LDB_ERR_OPERATIONS_ERROR
;
792 req
->op
.search
.attrs
= attrs
;
793 req
->controls
= controls
;
794 req
->context
= context
;
795 req
->callback
= callback
;
801 int ldb_build_add_req(struct ldb_request
**ret_req
,
802 struct ldb_context
*ldb
,
804 const struct ldb_message
*message
,
805 struct ldb_control
**controls
,
807 ldb_request_callback_t callback
)
809 struct ldb_request
*req
;
813 req
= talloc(mem_ctx
, struct ldb_request
);
815 ldb_set_errstring(ldb
, "Out of Memory");
816 return LDB_ERR_OPERATIONS_ERROR
;
819 req
->operation
= LDB_ADD
;
820 req
->op
.add
.message
= message
;
821 req
->controls
= controls
;
822 req
->context
= context
;
823 req
->callback
= callback
;
830 int ldb_build_mod_req(struct ldb_request
**ret_req
,
831 struct ldb_context
*ldb
,
833 const struct ldb_message
*message
,
834 struct ldb_control
**controls
,
836 ldb_request_callback_t callback
)
838 struct ldb_request
*req
;
842 req
= talloc(mem_ctx
, struct ldb_request
);
844 ldb_set_errstring(ldb
, "Out of Memory");
845 return LDB_ERR_OPERATIONS_ERROR
;
848 req
->operation
= LDB_MODIFY
;
849 req
->op
.mod
.message
= message
;
850 req
->controls
= controls
;
851 req
->context
= context
;
852 req
->callback
= callback
;
859 int ldb_build_del_req(struct ldb_request
**ret_req
,
860 struct ldb_context
*ldb
,
863 struct ldb_control
**controls
,
865 ldb_request_callback_t callback
)
867 struct ldb_request
*req
;
871 req
= talloc(mem_ctx
, struct ldb_request
);
873 ldb_set_errstring(ldb
, "Out of Memory");
874 return LDB_ERR_OPERATIONS_ERROR
;
877 req
->operation
= LDB_DELETE
;
879 req
->controls
= controls
;
880 req
->context
= context
;
881 req
->callback
= callback
;
888 int ldb_build_rename_req(struct ldb_request
**ret_req
,
889 struct ldb_context
*ldb
,
891 struct ldb_dn
*olddn
,
892 struct ldb_dn
*newdn
,
893 struct ldb_control
**controls
,
895 ldb_request_callback_t callback
)
897 struct ldb_request
*req
;
901 req
= talloc(mem_ctx
, struct ldb_request
);
903 ldb_set_errstring(ldb
, "Out of Memory");
904 return LDB_ERR_OPERATIONS_ERROR
;
907 req
->operation
= LDB_RENAME
;
908 req
->op
.rename
.olddn
= olddn
;
909 req
->op
.rename
.newdn
= newdn
;
910 req
->controls
= controls
;
911 req
->context
= context
;
912 req
->callback
= callback
;
919 int ldb_extended_default_callback(struct ldb_context
*ldb
,
921 struct ldb_reply
*ares
)
923 struct ldb_result
*res
;
926 ldb_set_errstring(ldb
, "NULL Context in callback");
927 return LDB_ERR_OPERATIONS_ERROR
;
930 res
= talloc_get_type(context
, struct ldb_result
);
932 ldb_set_errstring(ldb
, "NULL res or ares in callback");
936 switch (ares
->type
) {
937 case LDB_REPLY_ENTRY
:
938 case LDB_REPLY_REFERRAL
:
940 ldb_set_errstring(ldb
, "invalid ares type in callback");
942 case LDB_REPLY_EXTENDED
:
943 /* TODO: we should really support controls on entries and
945 res
->extended
= talloc_move(res
, &ares
->response
);
946 res
->controls
= talloc_move(res
, &ares
->controls
);
954 return LDB_ERR_OPERATIONS_ERROR
;
957 int ldb_build_extended_req(struct ldb_request
**ret_req
,
958 struct ldb_context
*ldb
,
962 struct ldb_control
**controls
,
964 ldb_request_callback_t callback
)
966 struct ldb_request
*req
;
970 req
= talloc(mem_ctx
, struct ldb_request
);
972 ldb_set_errstring(ldb
, "Out of Memory");
973 return LDB_ERR_OPERATIONS_ERROR
;
976 req
->operation
= LDB_EXTENDED
;
977 req
->op
.extended
.oid
= oid
;
978 req
->op
.extended
.data
= data
;
979 req
->controls
= controls
;
980 req
->context
= context
;
981 req
->callback
= callback
;
988 int ldb_extended(struct ldb_context
*ldb
,
991 struct ldb_result
**_res
)
993 struct ldb_request
*req
;
995 struct ldb_result
*res
;
999 res
= talloc_zero(ldb
, struct ldb_result
);
1001 return LDB_ERR_OPERATIONS_ERROR
;
1004 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
1006 res
, ldb_extended_default_callback
);
1007 if (ret
!= LDB_SUCCESS
) goto done
;
1009 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1011 ret
= ldb_request(ldb
, req
);
1013 if (ret
== LDB_SUCCESS
) {
1014 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1020 if (ret
!= LDB_SUCCESS
) {
1029 note that ldb_search() will automatically replace a NULL 'base' value
1030 with the defaultNamingContext from the rootDSE if available.
1032 int ldb_search(struct ldb_context
*ldb
,
1033 struct ldb_dn
*base
,
1034 enum ldb_scope scope
,
1035 const char *expression
,
1036 const char * const *attrs
,
1037 struct ldb_result
**_res
)
1039 struct ldb_request
*req
;
1041 struct ldb_result
*res
;
1045 res
= talloc_zero(ldb
, struct ldb_result
);
1047 return LDB_ERR_OPERATIONS_ERROR
;
1050 ret
= ldb_build_search_req(&req
, ldb
, ldb
,
1051 base
?base
:ldb_get_default_basedn(ldb
),
1057 ldb_search_default_callback
);
1059 if (ret
!= LDB_SUCCESS
) goto done
;
1061 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1063 ret
= ldb_request(ldb
, req
);
1065 if (ret
== LDB_SUCCESS
) {
1066 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1072 if (ret
!= LDB_SUCCESS
) {
1081 a useful search function where you can easily define the expression and that
1082 takes a memory context where results are allocated
1085 int ldb_search_exp_fmt(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1086 struct ldb_result
**result
, struct ldb_dn
*base
,
1087 enum ldb_scope scope
, const char * const *attrs
,
1088 const char *exp_fmt
, ...)
1090 struct ldb_result
*res
;
1098 va_start(ap
, exp_fmt
);
1099 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
1102 if ( ! expression
) {
1103 return LDB_ERR_OPERATIONS_ERROR
;
1106 ret
= ldb_search(ldb
, base
, scope
, expression
, attrs
, &res
);
1108 if (ret
== LDB_SUCCESS
) {
1109 talloc_steal(mem_ctx
, res
);
1113 talloc_free(expression
);
1119 add a record to the database. Will fail if a record with the
1120 given class and key already exists
1122 int ldb_add(struct ldb_context
*ldb
,
1123 const struct ldb_message
*message
)
1125 struct ldb_request
*req
;
1128 ret
= ldb_msg_sanity_check(ldb
, message
);
1129 if (ret
!= LDB_SUCCESS
) {
1133 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
1139 if (ret
!= LDB_SUCCESS
) return ret
;
1141 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1143 /* do request and autostart a transaction */
1144 ret
= ldb_autotransaction_request(ldb
, req
);
1151 modify the specified attributes of a record
1153 int ldb_modify(struct ldb_context
*ldb
,
1154 const struct ldb_message
*message
)
1156 struct ldb_request
*req
;
1159 ret
= ldb_msg_sanity_check(ldb
, message
);
1160 if (ret
!= LDB_SUCCESS
) {
1164 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
1170 if (ret
!= LDB_SUCCESS
) return ret
;
1172 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1174 /* do request and autostart a transaction */
1175 ret
= ldb_autotransaction_request(ldb
, req
);
1183 delete a record from the database
1185 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1187 struct ldb_request
*req
;
1190 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
1196 if (ret
!= LDB_SUCCESS
) return ret
;
1198 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1200 /* do request and autostart a transaction */
1201 ret
= ldb_autotransaction_request(ldb
, req
);
1208 rename a record in the database
1210 int ldb_rename(struct ldb_context
*ldb
,
1211 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
1213 struct ldb_request
*req
;
1216 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
1223 if (ret
!= LDB_SUCCESS
) return ret
;
1225 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1227 /* do request and autostart a transaction */
1228 ret
= ldb_autotransaction_request(ldb
, req
);
1236 return the global sequence number
1238 int ldb_sequence_number(struct ldb_context
*ldb
,
1239 enum ldb_sequence_type type
,
1242 struct ldb_request
*req
;
1245 req
= talloc(ldb
, struct ldb_request
);
1247 ldb_set_errstring(ldb
, "Out of Memory");
1248 return LDB_ERR_OPERATIONS_ERROR
;
1251 req
->operation
= LDB_SEQUENCE_NUMBER
;
1252 req
->controls
= NULL
;
1253 req
->context
= NULL
;
1254 req
->callback
= NULL
;
1255 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1257 req
->op
.seq_num
.type
= type
;
1258 /* do request and autostart a transaction */
1259 ret
= ldb_request(ldb
, req
);
1261 if (ret
== LDB_SUCCESS
) {
1262 *seq_num
= req
->op
.seq_num
.seq_num
;
1272 return extended error information
1274 const char *ldb_errstring(struct ldb_context
*ldb
)
1276 if (ldb
->err_string
) {
1277 return ldb
->err_string
;
1284 return a string explaining what a ldb error constant meancs
1286 const char *ldb_strerror(int ldb_err
)
1291 case LDB_ERR_OPERATIONS_ERROR
:
1292 return "Operations error";
1293 case LDB_ERR_PROTOCOL_ERROR
:
1294 return "Protocol error";
1295 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
1296 return "Time limit exceeded";
1297 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
1298 return "Size limit exceeded";
1299 case LDB_ERR_COMPARE_FALSE
:
1300 return "Compare false";
1301 case LDB_ERR_COMPARE_TRUE
:
1302 return "Compare true";
1303 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
1304 return "Auth method not supported";
1305 case LDB_ERR_STRONG_AUTH_REQUIRED
:
1306 return "Strong auth required";
1308 case LDB_ERR_REFERRAL
:
1309 return "Referral error";
1310 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
1311 return "Admin limit exceeded";
1312 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
1313 return "Unsupported critical extension";
1314 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
1315 return "Confidentiality required";
1316 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
1317 return "SASL bind in progress";
1318 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
1319 return "No such attribute";
1320 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
1321 return "Undefined attribute type";
1322 case LDB_ERR_INAPPROPRIATE_MATCHING
:
1323 return "Inappropriate matching";
1324 case LDB_ERR_CONSTRAINT_VIOLATION
:
1325 return "Constraint violation";
1326 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
1327 return "Attribute or value exists";
1328 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
1329 return "Invalid attribute syntax";
1331 case LDB_ERR_NO_SUCH_OBJECT
:
1332 return "No such object";
1333 case LDB_ERR_ALIAS_PROBLEM
:
1334 return "Alias problem";
1335 case LDB_ERR_INVALID_DN_SYNTAX
:
1336 return "Invalid DN syntax";
1338 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
1339 return "Alias dereferencing problem";
1341 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
1342 return "Inappropriate authentication";
1343 case LDB_ERR_INVALID_CREDENTIALS
:
1344 return "Invalid credentials";
1345 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
1346 return "insufficient access rights";
1349 case LDB_ERR_UNAVAILABLE
:
1350 return "Unavailable";
1351 case LDB_ERR_UNWILLING_TO_PERFORM
:
1352 return "Unwilling to perform";
1353 case LDB_ERR_LOOP_DETECT
:
1354 return "Loop detect";
1356 case LDB_ERR_NAMING_VIOLATION
:
1357 return "Naming violation";
1358 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
1359 return "Object class violation";
1360 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
1361 return "Not allowed on non-leaf";
1362 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
1363 return "Not allowed on RDN";
1364 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
1365 return "Entry already exists";
1366 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
1367 return "Object class mods prohibited";
1368 /* 70 RESERVED FOR CLDAP */
1369 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
1370 return "Affects multiple DSAs";
1376 return "Unknown error";
1380 set backend specific opaque parameters
1382 int ldb_set_opaque(struct ldb_context
*ldb
, const char *name
, void *value
)
1384 struct ldb_opaque
*o
;
1386 /* allow updating an existing value */
1387 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1388 if (strcmp(o
->name
, name
) == 0) {
1394 o
= talloc(ldb
, struct ldb_opaque
);
1397 return LDB_ERR_OTHER
;
1399 o
->next
= ldb
->opaque
;
1407 get a previously set opaque value
1409 void *ldb_get_opaque(struct ldb_context
*ldb
, const char *name
)
1411 struct ldb_opaque
*o
;
1412 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1413 if (strcmp(o
->name
, name
) == 0) {