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
);
49 ev_ctx
= event_context_init(ldb
);
52 ret
= ldb_setup_wellknown_attributes(ldb
);
58 ldb_set_utf8_default(ldb
);
59 ldb_set_create_perms(ldb
, 0666);
60 ldb_set_modules_dir(ldb
, LDB_MODULESDIR
);
61 ldb_set_event_context(ldb
, ev_ctx
);
63 /* TODO: get timeout from options if available there */
64 ldb
->default_timeout
= 300; /* set default to 5 minutes */
69 static struct backends_list_entry
{
70 struct ldb_backend_ops
*ops
;
71 struct backends_list_entry
*prev
, *next
;
72 } *ldb_backends
= NULL
;
74 #ifndef STATIC_LIBLDB_BACKENDS
77 #define LDAP_INIT &ldb_ldap_backend_ops, \
78 &ldb_ldapi_backend_ops, \
79 &ldb_ldaps_backend_ops,
84 #ifdef HAVE_LDB_SQLITE3
85 #define SQLITE3_INIT &ldb_sqlite3_backend_ops,
90 #define STATIC_LIBLDB_BACKENDS \
93 &ldb_tdb_backend_ops, \
97 const static struct ldb_backend_ops
*builtin_backends
[] = {
98 STATIC_LIBLDB_BACKENDS
101 static ldb_connect_fn
ldb_find_backend(const char *url
)
103 struct backends_list_entry
*backend
;
106 for (i
= 0; builtin_backends
[i
]; i
++) {
107 if (strncmp(builtin_backends
[i
]->name
, url
,
108 strlen(builtin_backends
[i
]->name
)) == 0)
109 return builtin_backends
[i
]->connect_fn
;
112 for (backend
= ldb_backends
; backend
; backend
= backend
->next
) {
113 if (strncmp(backend
->ops
->name
, url
,
114 strlen(backend
->ops
->name
)) == 0) {
115 return backend
->ops
->connect_fn
;
123 register a new ldb backend
125 int ldb_register_backend(const char *url_prefix
, ldb_connect_fn connectfn
)
127 struct ldb_backend_ops
*backend
;
128 struct backends_list_entry
*entry
;
130 backend
= talloc(talloc_autofree_context(), struct ldb_backend_ops
);
131 if (!backend
) return LDB_OPERATIONS_ERROR
;
133 entry
= talloc(talloc_autofree_context(), struct backends_list_entry
);
135 talloc_free(backend
);
136 return LDB_OPERATIONS_ERROR
;
139 if (ldb_find_backend(url_prefix
)) {
143 /* Maybe check for duplicity here later on? */
145 backend
->name
= talloc_strdup(backend
, url_prefix
);
146 backend
->connect_fn
= connectfn
;
147 entry
->ops
= backend
;
148 DLIST_ADD(ldb_backends
, entry
);
154 Return the ldb module form of a database.
155 The URL can either be one of the following forms
159 flags is made up of LDB_FLG_*
161 the options are passed uninterpreted to the backend, and are
164 This allows modules to get at only the backend module, for example where a
165 module may wish to direct certain requests at a particular backend.
167 int ldb_connect_backend(struct ldb_context
*ldb
,
169 const char *options
[],
170 struct ldb_module
**backend_module
)
176 if (strchr(url
, ':') != NULL
) {
177 backend
= talloc_strndup(ldb
, url
, strchr(url
, ':')-url
);
180 backend
= talloc_strdup(ldb
, "tdb");
183 fn
= ldb_find_backend(backend
);
186 int (*init_fn
) (void);
188 init_fn
= ldb_dso_load_symbol(ldb
, backend
,
190 if (init_fn
!= NULL
&& init_fn() == 0) {
191 fn
= ldb_find_backend(backend
);
196 struct ldb_backend_ops
*ops
;
197 char *symbol_name
= talloc_asprintf(ldb
, "ldb_%s_backend_ops", backend
);
198 if (symbol_name
== NULL
) {
199 return LDB_ERR_OPERATIONS_ERROR
;
201 ops
= ldb_dso_load_symbol(ldb
, backend
, symbol_name
);
203 fn
= ops
->connect_fn
;
205 talloc_free(symbol_name
);
208 talloc_free(backend
);
211 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
212 "Unable to find backend for '%s'\n", url
);
213 return LDB_ERR_OTHER
;
216 ret
= fn(ldb
, url
, ldb
->flags
, options
, backend_module
);
218 if (ret
!= LDB_SUCCESS
) {
219 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
220 "Failed to connect to '%s'\n", url
);
227 try to autodetect a basedn if none specified. This fixes one of my
228 pet hates about ldapsearch, which is that you have to get a long,
229 complex basedn right to make any use of it.
231 void ldb_set_default_dns(struct ldb_context
*ldb
)
235 struct ldb_result
*res
;
236 struct ldb_dn
*tmp_dn
=NULL
;
237 static const char *attrs
[] = {
238 "rootDomainNamingContext",
239 "configurationNamingContext",
240 "schemaNamingContext",
241 "defaultNamingContext",
245 tmp_ctx
= talloc_new(ldb
);
246 ret
= ldb_search(ldb
, ldb_dn_new(tmp_ctx
, ldb
, NULL
), LDB_SCOPE_BASE
,
247 "(objectClass=*)", attrs
, &res
);
248 if (ret
!= LDB_SUCCESS
) {
249 talloc_free(tmp_ctx
);
253 if (res
->count
!= 1) {
258 if (!ldb_get_opaque(ldb
, "rootDomainNamingContext")) {
259 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
260 "rootDomainNamingContext");
261 ldb_set_opaque(ldb
, "rootDomainNamingContext", tmp_dn
);
264 if (!ldb_get_opaque(ldb
, "configurationNamingContext")) {
265 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
266 "configurationNamingContext");
267 ldb_set_opaque(ldb
, "configurationNamingContext", tmp_dn
);
270 if (!ldb_get_opaque(ldb
, "schemaNamingContext")) {
271 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
272 "schemaNamingContext");
273 ldb_set_opaque(ldb
, "schemaNamingContext", tmp_dn
);
276 if (!ldb_get_opaque(ldb
, "defaultNamingContext")) {
277 tmp_dn
= ldb_msg_find_attr_as_dn(ldb
, ldb
, res
->msgs
[0],
278 "defaultNamingContext");
279 ldb_set_opaque(ldb
, "defaultNamingContext", tmp_dn
);
283 talloc_free(tmp_ctx
);
286 struct ldb_dn
*ldb_get_root_basedn(struct ldb_context
*ldb
)
288 void *opaque
= ldb_get_opaque(ldb
, "rootDomainNamingContext");
289 return talloc_get_type(opaque
, struct ldb_dn
);
292 struct ldb_dn
*ldb_get_config_basedn(struct ldb_context
*ldb
)
294 void *opaque
= ldb_get_opaque(ldb
, "configurationNamingContext");
295 return talloc_get_type(opaque
, struct ldb_dn
);
298 struct ldb_dn
*ldb_get_schema_basedn(struct ldb_context
*ldb
)
300 void *opaque
= ldb_get_opaque(ldb
, "schemaNamingContext");
301 return talloc_get_type(opaque
, struct ldb_dn
);
304 struct ldb_dn
*ldb_get_default_basedn(struct ldb_context
*ldb
)
306 void *opaque
= ldb_get_opaque(ldb
, "defaultNamingContext");
307 return talloc_get_type(opaque
, struct ldb_dn
);
311 connect to a database. The URL can either be one of the following forms
315 flags is made up of LDB_FLG_*
317 the options are passed uninterpreted to the backend, and are
320 int ldb_connect(struct ldb_context
*ldb
, const char *url
,
321 unsigned int flags
, const char *options
[])
325 /* We seem to need to do this here, or else some utilities don't
326 * get ldb backends */
330 url2
= talloc_strdup(ldb
, url
);
333 return LDB_ERR_OPERATIONS_ERROR
;
335 ret
= ldb_set_opaque(ldb
, "ldb_url", talloc_strdup(ldb
, url2
));
336 if (ret
!= LDB_SUCCESS
) {
340 ret
= ldb_connect_backend(ldb
, url
, options
, &ldb
->modules
);
341 if (ret
!= LDB_SUCCESS
) {
345 if (ldb_load_modules(ldb
, options
) != LDB_SUCCESS
) {
346 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
347 "Unable to load modules for %s: %s\n",
348 url
, ldb_errstring(ldb
));
349 return LDB_ERR_OTHER
;
352 /* TODO: get timeout from options if available there */
353 ldb
->default_timeout
= 300; /* set default to 5 minutes */
355 /* set the default base dn */
356 ldb_set_default_dns(ldb
);
361 void ldb_set_errstring(struct ldb_context
*ldb
, const char *err_string
)
363 if (ldb
->err_string
) {
364 talloc_free(ldb
->err_string
);
366 ldb
->err_string
= talloc_strdup(ldb
, err_string
);
369 void ldb_asprintf_errstring(struct ldb_context
*ldb
, const char *format
, ...)
372 char *old_string
= NULL
;
374 if (ldb
->err_string
) {
375 old_string
= ldb
->err_string
;
378 va_start(ap
, format
);
379 ldb
->err_string
= talloc_vasprintf(ldb
, format
, ap
);
381 talloc_free(old_string
);
384 void ldb_reset_err_string(struct ldb_context
*ldb
)
386 if (ldb
->err_string
) {
387 talloc_free(ldb
->err_string
);
388 ldb
->err_string
= NULL
;
392 #define FIRST_OP(ldb, op) do { \
393 module = ldb->modules; \
394 while (module && module->ops->op == NULL) module = module->next; \
395 if (module == NULL) { \
396 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
397 return LDB_ERR_OPERATIONS_ERROR; \
404 static int ldb_transaction_start_internal(struct ldb_context
*ldb
)
406 struct ldb_module
*module
;
408 FIRST_OP(ldb
, start_transaction
);
410 ldb_reset_err_string(ldb
);
412 status
= module
->ops
->start_transaction(module
);
413 if (status
!= LDB_SUCCESS
) {
414 if (ldb
->err_string
== NULL
) {
415 /* no error string was setup by the backend */
416 ldb_asprintf_errstring(ldb
,
417 "ldb transaction start: %s (%d)",
418 ldb_strerror(status
),
428 static int ldb_transaction_commit_internal(struct ldb_context
*ldb
)
430 struct ldb_module
*module
;
432 FIRST_OP(ldb
, end_transaction
);
434 ldb_reset_err_string(ldb
);
436 status
= module
->ops
->end_transaction(module
);
437 if (status
!= LDB_SUCCESS
) {
438 if (ldb
->err_string
== NULL
) {
439 /* no error string was setup by the backend */
440 ldb_asprintf_errstring(ldb
,
441 "ldb transaction commit: %s (%d)",
442 ldb_strerror(status
),
452 static int ldb_transaction_cancel_internal(struct ldb_context
*ldb
)
454 struct ldb_module
*module
;
456 FIRST_OP(ldb
, del_transaction
);
458 status
= module
->ops
->del_transaction(module
);
459 if (status
!= LDB_SUCCESS
) {
460 if (ldb
->err_string
== NULL
) {
461 /* no error string was setup by the backend */
462 ldb_asprintf_errstring(ldb
,
463 "ldb transaction cancel: %s (%d)",
464 ldb_strerror(status
),
471 int ldb_transaction_start(struct ldb_context
*ldb
)
473 /* disable autotransactions */
474 ldb
->transaction_active
++;
476 return ldb_transaction_start_internal(ldb
);
479 int ldb_transaction_commit(struct ldb_context
*ldb
)
481 /* renable autotransactions (when we reach 0) */
482 if (ldb
->transaction_active
> 0)
483 ldb
->transaction_active
--;
485 return ldb_transaction_commit_internal(ldb
);
488 int ldb_transaction_cancel(struct ldb_context
*ldb
)
490 /* renable autotransactions (when we reach 0) */
491 if (ldb
->transaction_active
> 0)
492 ldb
->transaction_active
--;
494 return ldb_transaction_cancel_internal(ldb
);
497 static int ldb_autotransaction_start(struct ldb_context
*ldb
)
499 /* explicit transaction active, ignore autotransaction request */
500 if (ldb
->transaction_active
)
503 return ldb_transaction_start_internal(ldb
);
506 static int ldb_autotransaction_commit(struct ldb_context
*ldb
)
508 /* explicit transaction active, ignore autotransaction request */
509 if (ldb
->transaction_active
)
512 return ldb_transaction_commit_internal(ldb
);
515 static int ldb_autotransaction_cancel(struct ldb_context
*ldb
)
517 /* explicit transaction active, ignore autotransaction request */
518 if (ldb
->transaction_active
)
521 return ldb_transaction_cancel_internal(ldb
);
524 /* autostarts a transacion if none active */
525 static int ldb_autotransaction_request(struct ldb_context
*ldb
,
526 struct ldb_request
*req
)
530 ret
= ldb_autotransaction_start(ldb
);
531 if (ret
!= LDB_SUCCESS
) {
535 ret
= ldb_request(ldb
, req
);
536 if (ret
== LDB_SUCCESS
) {
537 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
540 if (ret
== LDB_SUCCESS
) {
541 return ldb_autotransaction_commit(ldb
);
543 ldb_autotransaction_cancel(ldb
);
545 if (ldb
->err_string
== NULL
) {
546 /* no error string was setup by the backend */
547 ldb_asprintf_errstring(ldb
, "%s (%d)", ldb_strerror(ret
), ret
);
553 int ldb_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
560 ret
= handle
->module
->ops
->wait(handle
, type
);
561 if (!ldb_errstring(handle
->module
->ldb
)) {
562 /* Set a default error string, to place the blame somewhere */
563 ldb_asprintf_errstring(handle
->module
->ldb
,
564 "error waiting on module %s: %s (%d)",
565 handle
->module
->ops
->name
,
566 ldb_strerror(ret
), ret
);
571 /* set the specified timeout or, if timeout is 0 set the default timeout */
572 /* timeout == -1 means no timeout */
573 int ldb_set_timeout(struct ldb_context
*ldb
,
574 struct ldb_request
*req
,
577 if (req
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
580 req
->timeout
= timeout
;
582 req
->timeout
= ldb
->default_timeout
;
584 req
->starttime
= time(NULL
);
589 /* calculates the new timeout based on the previous starttime and timeout */
590 int ldb_set_timeout_from_prev_req(struct ldb_context
*ldb
,
591 struct ldb_request
*oldreq
,
592 struct ldb_request
*newreq
)
596 if (newreq
== NULL
) return LDB_ERR_OPERATIONS_ERROR
;
601 return ldb_set_timeout(ldb
, newreq
, 0);
603 if ((now
- oldreq
->starttime
) > oldreq
->timeout
) {
604 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
606 newreq
->starttime
= oldreq
->starttime
;
607 newreq
->timeout
= oldreq
->timeout
- (now
- oldreq
->starttime
);
614 set the permissions for new files to be passed to open() in
615 backends that use local files
617 void ldb_set_create_perms(struct ldb_context
*ldb
, unsigned int perms
)
619 ldb
->create_perms
= perms
;
622 void ldb_set_event_context(struct ldb_context
*ldb
, struct event_context
*ev
)
627 struct event_context
* ldb_get_event_context(struct ldb_context
*ldb
)
634 NOTE: the request must be a talloc context.
635 returns LDB_ERR_* on errors.
637 int ldb_request(struct ldb_context
*ldb
, struct ldb_request
*req
)
639 struct ldb_module
*module
;
642 ldb_reset_err_string(ldb
);
644 /* call the first module in the chain */
645 switch (req
->operation
) {
647 FIRST_OP(ldb
, search
);
648 ret
= module
->ops
->search(module
, req
);
652 ret
= module
->ops
->add(module
, req
);
655 FIRST_OP(ldb
, modify
);
656 ret
= module
->ops
->modify(module
, req
);
660 ret
= module
->ops
->del(module
, req
);
663 FIRST_OP(ldb
, rename
);
664 ret
= module
->ops
->rename(module
, req
);
667 FIRST_OP(ldb
, extended
);
668 ret
= module
->ops
->extended(module
, req
);
670 case LDB_SEQUENCE_NUMBER
:
671 FIRST_OP(ldb
, sequence_number
);
672 ret
= module
->ops
->sequence_number(module
, req
);
675 FIRST_OP(ldb
, request
);
676 ret
= module
->ops
->request(module
, req
);
684 search the database given a LDAP-like search expression
686 returns an LDB error code
688 Use talloc_free to free the ldb_message returned in 'res', if successful
691 int ldb_search_default_callback(struct ldb_context
*ldb
,
693 struct ldb_reply
*ares
)
695 struct ldb_result
*res
;
699 ldb_set_errstring(ldb
, "NULL Context in callback");
700 return LDB_ERR_OPERATIONS_ERROR
;
703 res
= talloc_get_type(context
, struct ldb_result
);
706 ldb_set_errstring(ldb
, "NULL res or ares in callback");
710 switch (ares
->type
) {
711 case LDB_REPLY_ENTRY
:
712 res
->msgs
= talloc_realloc(res
, res
->msgs
,
713 struct ldb_message
*,
719 res
->msgs
[res
->count
+ 1] = NULL
;
721 res
->msgs
[res
->count
] = talloc_move(res
->msgs
, &ares
->message
);
724 case LDB_REPLY_REFERRAL
:
726 for (n
= 0; res
->refs
[n
]; n
++) /*noop*/ ;
731 res
->refs
= talloc_realloc(res
, res
->refs
, char *, n
+ 2);
736 res
->refs
[n
] = talloc_move(res
->refs
, &ares
->referral
);
737 res
->refs
[n
+ 1] = NULL
;
739 case LDB_REPLY_EXTENDED
:
741 /* TODO: we should really support controls on entries
742 * and referrals too! */
743 res
->controls
= talloc_move(res
, &ares
->controls
);
751 return LDB_ERR_OPERATIONS_ERROR
;
754 int ldb_build_search_req(struct ldb_request
**ret_req
,
755 struct ldb_context
*ldb
,
758 enum ldb_scope scope
,
759 const char *expression
,
760 const char * const *attrs
,
761 struct ldb_control
**controls
,
763 ldb_request_callback_t callback
)
765 struct ldb_request
*req
;
769 req
= talloc(mem_ctx
, struct ldb_request
);
771 ldb_set_errstring(ldb
, "Out of Memory");
772 return LDB_ERR_OPERATIONS_ERROR
;
775 req
->operation
= LDB_SEARCH
;
777 req
->op
.search
.base
= ldb_dn_new(req
, ldb
, NULL
);
779 req
->op
.search
.base
= base
;
781 req
->op
.search
.scope
= scope
;
783 req
->op
.search
.tree
= ldb_parse_tree(req
, expression
);
784 if (req
->op
.search
.tree
== NULL
) {
785 ldb_set_errstring(ldb
, "Unable to parse search expression");
787 return LDB_ERR_OPERATIONS_ERROR
;
790 req
->op
.search
.attrs
= attrs
;
791 req
->controls
= controls
;
792 req
->context
= context
;
793 req
->callback
= callback
;
799 int ldb_build_add_req(struct ldb_request
**ret_req
,
800 struct ldb_context
*ldb
,
802 const struct ldb_message
*message
,
803 struct ldb_control
**controls
,
805 ldb_request_callback_t callback
)
807 struct ldb_request
*req
;
811 req
= talloc(mem_ctx
, struct ldb_request
);
813 ldb_set_errstring(ldb
, "Out of Memory");
814 return LDB_ERR_OPERATIONS_ERROR
;
817 req
->operation
= LDB_ADD
;
818 req
->op
.add
.message
= message
;
819 req
->controls
= controls
;
820 req
->context
= context
;
821 req
->callback
= callback
;
828 int ldb_build_mod_req(struct ldb_request
**ret_req
,
829 struct ldb_context
*ldb
,
831 const struct ldb_message
*message
,
832 struct ldb_control
**controls
,
834 ldb_request_callback_t callback
)
836 struct ldb_request
*req
;
840 req
= talloc(mem_ctx
, struct ldb_request
);
842 ldb_set_errstring(ldb
, "Out of Memory");
843 return LDB_ERR_OPERATIONS_ERROR
;
846 req
->operation
= LDB_MODIFY
;
847 req
->op
.mod
.message
= message
;
848 req
->controls
= controls
;
849 req
->context
= context
;
850 req
->callback
= callback
;
857 int ldb_build_del_req(struct ldb_request
**ret_req
,
858 struct ldb_context
*ldb
,
861 struct ldb_control
**controls
,
863 ldb_request_callback_t callback
)
865 struct ldb_request
*req
;
869 req
= talloc(mem_ctx
, struct ldb_request
);
871 ldb_set_errstring(ldb
, "Out of Memory");
872 return LDB_ERR_OPERATIONS_ERROR
;
875 req
->operation
= LDB_DELETE
;
877 req
->controls
= controls
;
878 req
->context
= context
;
879 req
->callback
= callback
;
886 int ldb_build_rename_req(struct ldb_request
**ret_req
,
887 struct ldb_context
*ldb
,
889 struct ldb_dn
*olddn
,
890 struct ldb_dn
*newdn
,
891 struct ldb_control
**controls
,
893 ldb_request_callback_t callback
)
895 struct ldb_request
*req
;
899 req
= talloc(mem_ctx
, struct ldb_request
);
901 ldb_set_errstring(ldb
, "Out of Memory");
902 return LDB_ERR_OPERATIONS_ERROR
;
905 req
->operation
= LDB_RENAME
;
906 req
->op
.rename
.olddn
= olddn
;
907 req
->op
.rename
.newdn
= newdn
;
908 req
->controls
= controls
;
909 req
->context
= context
;
910 req
->callback
= callback
;
917 int ldb_extended_default_callback(struct ldb_context
*ldb
,
919 struct ldb_reply
*ares
)
921 struct ldb_result
*res
;
924 ldb_set_errstring(ldb
, "NULL Context in callback");
925 return LDB_ERR_OPERATIONS_ERROR
;
928 res
= talloc_get_type(context
, struct ldb_result
);
930 ldb_set_errstring(ldb
, "NULL res or ares in callback");
934 switch (ares
->type
) {
935 case LDB_REPLY_ENTRY
:
936 case LDB_REPLY_REFERRAL
:
938 ldb_set_errstring(ldb
, "invalid ares type in callback");
940 case LDB_REPLY_EXTENDED
:
941 /* TODO: we should really support controls on entries and
943 res
->extended
= talloc_move(res
, &ares
->response
);
944 res
->controls
= talloc_move(res
, &ares
->controls
);
952 return LDB_ERR_OPERATIONS_ERROR
;
955 int ldb_build_extended_req(struct ldb_request
**ret_req
,
956 struct ldb_context
*ldb
,
960 struct ldb_control
**controls
,
962 ldb_request_callback_t callback
)
964 struct ldb_request
*req
;
968 req
= talloc(mem_ctx
, struct ldb_request
);
970 ldb_set_errstring(ldb
, "Out of Memory");
971 return LDB_ERR_OPERATIONS_ERROR
;
974 req
->operation
= LDB_EXTENDED
;
975 req
->op
.extended
.oid
= oid
;
976 req
->op
.extended
.data
= data
;
977 req
->controls
= controls
;
978 req
->context
= context
;
979 req
->callback
= callback
;
986 int ldb_extended(struct ldb_context
*ldb
,
989 struct ldb_result
**_res
)
991 struct ldb_request
*req
;
993 struct ldb_result
*res
;
997 res
= talloc_zero(ldb
, struct ldb_result
);
999 return LDB_ERR_OPERATIONS_ERROR
;
1002 ret
= ldb_build_extended_req(&req
, ldb
, ldb
,
1004 res
, ldb_extended_default_callback
);
1005 if (ret
!= LDB_SUCCESS
) goto done
;
1007 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1009 ret
= ldb_request(ldb
, req
);
1011 if (ret
== LDB_SUCCESS
) {
1012 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1018 if (ret
!= LDB_SUCCESS
) {
1027 note that ldb_search() will automatically replace a NULL 'base' value
1028 with the defaultNamingContext from the rootDSE if available.
1030 int ldb_search(struct ldb_context
*ldb
,
1031 struct ldb_dn
*base
,
1032 enum ldb_scope scope
,
1033 const char *expression
,
1034 const char * const *attrs
,
1035 struct ldb_result
**_res
)
1037 struct ldb_request
*req
;
1039 struct ldb_result
*res
;
1043 res
= talloc_zero(ldb
, struct ldb_result
);
1045 return LDB_ERR_OPERATIONS_ERROR
;
1048 ret
= ldb_build_search_req(&req
, ldb
, ldb
,
1049 base
?base
:ldb_get_default_basedn(ldb
),
1055 ldb_search_default_callback
);
1057 if (ret
!= LDB_SUCCESS
) goto done
;
1059 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1061 ret
= ldb_request(ldb
, req
);
1063 if (ret
== LDB_SUCCESS
) {
1064 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1070 if (ret
!= LDB_SUCCESS
) {
1079 a useful search function where you can easily define the expression and that
1080 takes a memory context where results are allocated
1083 int ldb_search_exp_fmt(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1084 struct ldb_result
**result
, struct ldb_dn
*base
,
1085 enum ldb_scope scope
, const char * const *attrs
,
1086 const char *exp_fmt
, ...)
1088 struct ldb_result
*res
;
1096 va_start(ap
, exp_fmt
);
1097 expression
= talloc_vasprintf(mem_ctx
, exp_fmt
, ap
);
1100 if ( ! expression
) {
1101 return LDB_ERR_OPERATIONS_ERROR
;
1104 ret
= ldb_search(ldb
, base
, scope
, expression
, attrs
, &res
);
1106 if (ret
== LDB_SUCCESS
) {
1107 talloc_steal(mem_ctx
, res
);
1111 talloc_free(expression
);
1117 add a record to the database. Will fail if a record with the
1118 given class and key already exists
1120 int ldb_add(struct ldb_context
*ldb
,
1121 const struct ldb_message
*message
)
1123 struct ldb_request
*req
;
1126 ret
= ldb_msg_sanity_check(ldb
, message
);
1127 if (ret
!= LDB_SUCCESS
) {
1131 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
1137 if (ret
!= LDB_SUCCESS
) return ret
;
1139 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1141 /* do request and autostart a transaction */
1142 ret
= ldb_autotransaction_request(ldb
, req
);
1149 modify the specified attributes of a record
1151 int ldb_modify(struct ldb_context
*ldb
,
1152 const struct ldb_message
*message
)
1154 struct ldb_request
*req
;
1157 ret
= ldb_msg_sanity_check(ldb
, message
);
1158 if (ret
!= LDB_SUCCESS
) {
1162 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
1168 if (ret
!= LDB_SUCCESS
) return ret
;
1170 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1172 /* do request and autostart a transaction */
1173 ret
= ldb_autotransaction_request(ldb
, req
);
1181 delete a record from the database
1183 int ldb_delete(struct ldb_context
*ldb
, struct ldb_dn
*dn
)
1185 struct ldb_request
*req
;
1188 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
1194 if (ret
!= LDB_SUCCESS
) return ret
;
1196 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1198 /* do request and autostart a transaction */
1199 ret
= ldb_autotransaction_request(ldb
, req
);
1206 rename a record in the database
1208 int ldb_rename(struct ldb_context
*ldb
,
1209 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
1211 struct ldb_request
*req
;
1214 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
1221 if (ret
!= LDB_SUCCESS
) return ret
;
1223 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1225 /* do request and autostart a transaction */
1226 ret
= ldb_autotransaction_request(ldb
, req
);
1234 return the global sequence number
1236 int ldb_sequence_number(struct ldb_context
*ldb
,
1237 enum ldb_sequence_type type
,
1240 struct ldb_request
*req
;
1243 req
= talloc(ldb
, struct ldb_request
);
1245 ldb_set_errstring(ldb
, "Out of Memory");
1246 return LDB_ERR_OPERATIONS_ERROR
;
1249 req
->operation
= LDB_SEQUENCE_NUMBER
;
1250 req
->controls
= NULL
;
1251 req
->context
= NULL
;
1252 req
->callback
= NULL
;
1253 ldb_set_timeout(ldb
, req
, 0); /* use default timeout */
1255 req
->op
.seq_num
.type
= type
;
1256 /* do request and autostart a transaction */
1257 ret
= ldb_request(ldb
, req
);
1259 if (ret
== LDB_SUCCESS
) {
1260 *seq_num
= req
->op
.seq_num
.seq_num
;
1270 return extended error information
1272 const char *ldb_errstring(struct ldb_context
*ldb
)
1274 if (ldb
->err_string
) {
1275 return ldb
->err_string
;
1282 return a string explaining what a ldb error constant meancs
1284 const char *ldb_strerror(int ldb_err
)
1289 case LDB_ERR_OPERATIONS_ERROR
:
1290 return "Operations error";
1291 case LDB_ERR_PROTOCOL_ERROR
:
1292 return "Protocol error";
1293 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
1294 return "Time limit exceeded";
1295 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
1296 return "Size limit exceeded";
1297 case LDB_ERR_COMPARE_FALSE
:
1298 return "Compare false";
1299 case LDB_ERR_COMPARE_TRUE
:
1300 return "Compare true";
1301 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
1302 return "Auth method not supported";
1303 case LDB_ERR_STRONG_AUTH_REQUIRED
:
1304 return "Strong auth required";
1306 case LDB_ERR_REFERRAL
:
1307 return "Referral error";
1308 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
1309 return "Admin limit exceeded";
1310 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
1311 return "Unsupported critical extension";
1312 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
1313 return "Confidentiality required";
1314 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
1315 return "SASL bind in progress";
1316 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
1317 return "No such attribute";
1318 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
1319 return "Undefined attribute type";
1320 case LDB_ERR_INAPPROPRIATE_MATCHING
:
1321 return "Inappropriate matching";
1322 case LDB_ERR_CONSTRAINT_VIOLATION
:
1323 return "Constraint violation";
1324 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
1325 return "Attribute or value exists";
1326 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
1327 return "Invalid attribute syntax";
1329 case LDB_ERR_NO_SUCH_OBJECT
:
1330 return "No such object";
1331 case LDB_ERR_ALIAS_PROBLEM
:
1332 return "Alias problem";
1333 case LDB_ERR_INVALID_DN_SYNTAX
:
1334 return "Invalid DN syntax";
1336 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
1337 return "Alias dereferencing problem";
1339 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
1340 return "Inappropriate authentication";
1341 case LDB_ERR_INVALID_CREDENTIALS
:
1342 return "Invalid credentials";
1343 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
1344 return "insufficient access rights";
1347 case LDB_ERR_UNAVAILABLE
:
1348 return "Unavailable";
1349 case LDB_ERR_UNWILLING_TO_PERFORM
:
1350 return "Unwilling to perform";
1351 case LDB_ERR_LOOP_DETECT
:
1352 return "Loop detect";
1354 case LDB_ERR_NAMING_VIOLATION
:
1355 return "Naming violation";
1356 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
1357 return "Object class violation";
1358 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
1359 return "Not allowed on non-leaf";
1360 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
1361 return "Not allowed on RDN";
1362 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
1363 return "Entry already exists";
1364 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
1365 return "Object class mods prohibited";
1366 /* 70 RESERVED FOR CLDAP */
1367 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
1368 return "Affects multiple DSAs";
1374 return "Unknown error";
1378 set backend specific opaque parameters
1380 int ldb_set_opaque(struct ldb_context
*ldb
, const char *name
, void *value
)
1382 struct ldb_opaque
*o
;
1384 /* allow updating an existing value */
1385 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1386 if (strcmp(o
->name
, name
) == 0) {
1392 o
= talloc(ldb
, struct ldb_opaque
);
1395 return LDB_ERR_OTHER
;
1397 o
->next
= ldb
->opaque
;
1405 get a previously set opaque value
1407 void *ldb_get_opaque(struct ldb_context
*ldb
, const char *name
)
1409 struct ldb_opaque
*o
;
1410 for (o
=ldb
->opaque
;o
;o
=o
->next
) {
1411 if (strcmp(o
->name
, name
) == 0) {