Merge branch 'master' of ssh://git.samba.org/data/git/samba into arc4
[kamenim/samba.git] / source4 / lib / ldb / common / ldb.c
1 /*
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Simo Sorce  2005-2008
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10
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.
15
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.
20
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/>.
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb core API
29  *
30  *  Description: core API routines interfacing to ldb backends
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "ldb_includes.h"
36
37 /*
38    initialise a ldb context
39    The mem_ctx is required
40    The event_ctx is required
41 */
42 struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx, struct event_context *ev_ctx)
43 {
44         struct ldb_context *ldb;
45         int ret;
46
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 */
50         if (ev_ctx == NULL) {
51                 ev_ctx = event_context_init(talloc_autofree_context());
52         }
53
54         ret = ldb_setup_wellknown_attributes(ldb);
55         if (ret != 0) {
56                 talloc_free(ldb);
57                 return NULL;
58         }
59
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);
64
65         /* TODO: get timeout from options if available there */
66         ldb->default_timeout = 300; /* set default to 5 minutes */
67
68         return ldb;
69 }
70
71 /*
72   try to autodetect a basedn if none specified. This fixes one of my
73   pet hates about ldapsearch, which is that you have to get a long,
74   complex basedn right to make any use of it.
75 */
76 void ldb_set_default_dns(struct ldb_context *ldb)
77 {
78         TALLOC_CTX *tmp_ctx;
79         int ret;
80         struct ldb_result *res;
81         struct ldb_dn *tmp_dn=NULL;
82         static const char *attrs[] = {
83                 "rootDomainNamingContext",
84                 "configurationNamingContext",
85                 "schemaNamingContext",
86                 "defaultNamingContext",
87                 NULL
88         };
89
90         tmp_ctx = talloc_new(ldb);
91         ret = ldb_search(ldb, tmp_ctx, &res, ldb_dn_new(tmp_ctx, ldb, NULL),
92                          LDB_SCOPE_BASE, attrs, "(objectClass=*)");
93         if (ret != LDB_SUCCESS) {
94                 talloc_free(tmp_ctx);
95                 return;
96         }
97
98         if (res->count != 1) {
99                 talloc_free(tmp_ctx);
100                 return;
101         }
102
103         if (!ldb_get_opaque(ldb, "rootDomainNamingContext")) {
104                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
105                                                  "rootDomainNamingContext");
106                 ldb_set_opaque(ldb, "rootDomainNamingContext", tmp_dn);
107         }
108
109         if (!ldb_get_opaque(ldb, "configurationNamingContext")) {
110                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
111                                                  "configurationNamingContext");
112                 ldb_set_opaque(ldb, "configurationNamingContext", tmp_dn);
113         }
114
115         if (!ldb_get_opaque(ldb, "schemaNamingContext")) {
116                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
117                                                  "schemaNamingContext");
118                 ldb_set_opaque(ldb, "schemaNamingContext", tmp_dn);
119         }
120
121         if (!ldb_get_opaque(ldb, "defaultNamingContext")) {
122                 tmp_dn = ldb_msg_find_attr_as_dn(ldb, ldb, res->msgs[0],
123                                                  "defaultNamingContext");
124                 ldb_set_opaque(ldb, "defaultNamingContext", tmp_dn);
125         }
126
127         talloc_free(tmp_ctx);
128 }
129
130 struct ldb_dn *ldb_get_root_basedn(struct ldb_context *ldb)
131 {
132         void *opaque = ldb_get_opaque(ldb, "rootDomainNamingContext");
133         return talloc_get_type(opaque, struct ldb_dn);
134 }
135
136 struct ldb_dn *ldb_get_config_basedn(struct ldb_context *ldb)
137 {
138         void *opaque = ldb_get_opaque(ldb, "configurationNamingContext");
139         return talloc_get_type(opaque, struct ldb_dn);
140 }
141
142 struct ldb_dn *ldb_get_schema_basedn(struct ldb_context *ldb)
143 {
144         void *opaque = ldb_get_opaque(ldb, "schemaNamingContext");
145         return talloc_get_type(opaque, struct ldb_dn);
146 }
147
148 struct ldb_dn *ldb_get_default_basedn(struct ldb_context *ldb)
149 {
150         void *opaque = ldb_get_opaque(ldb, "defaultNamingContext");
151         return talloc_get_type(opaque, struct ldb_dn);
152 }
153
154 /*
155    connect to a database. The URL can either be one of the following forms
156    ldb://path
157    ldapi://path
158
159    flags is made up of LDB_FLG_*
160
161    the options are passed uninterpreted to the backend, and are
162    backend specific
163 */
164 int ldb_connect(struct ldb_context *ldb, const char *url,
165                 unsigned int flags, const char *options[])
166 {
167         int ret;
168         const char *url2;
169         /* We seem to need to do this here, or else some utilities don't
170          * get ldb backends */
171
172         ldb->flags = flags;
173
174         url2 = talloc_strdup(ldb, url);
175         if (!url2) {
176                 ldb_oom(ldb);
177                 return LDB_ERR_OPERATIONS_ERROR;
178         }
179         ret = ldb_set_opaque(ldb, "ldb_url", talloc_strdup(ldb, url2));
180         if (ret != LDB_SUCCESS) {
181                 return ret;
182         }
183
184         ret = ldb_connect_backend(ldb, url, options, &ldb->modules);
185         if (ret != LDB_SUCCESS) {
186                 return ret;
187         }
188
189         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
190                 ldb_debug(ldb, LDB_DEBUG_FATAL,
191                           "Unable to load modules for %s: %s\n",
192                           url, ldb_errstring(ldb));
193                 return LDB_ERR_OTHER;
194         }
195
196         /* set the default base dn */
197         ldb_set_default_dns(ldb);
198
199         return LDB_SUCCESS;
200 }
201
202 void ldb_set_errstring(struct ldb_context *ldb, const char *err_string)
203 {
204         if (ldb->err_string) {
205                 talloc_free(ldb->err_string);
206         }
207         ldb->err_string = talloc_strdup(ldb, err_string);
208 }
209
210 void ldb_asprintf_errstring(struct ldb_context *ldb, const char *format, ...)
211 {
212         va_list ap;
213         char *old_string = NULL;
214
215         if (ldb->err_string) {
216                 old_string = ldb->err_string;
217         }
218
219         va_start(ap, format);
220         ldb->err_string = talloc_vasprintf(ldb, format, ap);
221         va_end(ap);
222         talloc_free(old_string);
223 }
224
225 void ldb_reset_err_string(struct ldb_context *ldb)
226 {
227         if (ldb->err_string) {
228                 talloc_free(ldb->err_string);
229                 ldb->err_string = NULL;
230         }
231 }
232
233 #define FIRST_OP(ldb, op) do { \
234         module = ldb->modules;                                  \
235         while (module && module->ops->op == NULL) module = module->next; \
236         if (module == NULL) {                                           \
237                 ldb_asprintf_errstring(ldb, "unable to find module or backend to handle operation: " #op); \
238                 return LDB_ERR_OPERATIONS_ERROR;                        \
239         } \
240 } while (0)
241
242 /*
243   start a transaction
244 */
245 static int ldb_transaction_start_internal(struct ldb_context *ldb)
246 {
247         struct ldb_module *module;
248         int status;
249         FIRST_OP(ldb, start_transaction);
250
251         ldb_reset_err_string(ldb);
252
253         status = module->ops->start_transaction(module);
254         if (status != LDB_SUCCESS) {
255                 if (ldb->err_string == NULL) {
256                         /* no error string was setup by the backend */
257                         ldb_asprintf_errstring(ldb,
258                                 "ldb transaction start: %s (%d)",
259                                 ldb_strerror(status),
260                                 status);
261                 }
262         }
263         return status;
264 }
265
266 /*
267   commit a transaction
268 */
269 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
270 {
271         struct ldb_module *module;
272         int status;
273         FIRST_OP(ldb, end_transaction);
274
275         ldb_reset_err_string(ldb);
276
277         status = module->ops->end_transaction(module);
278         if (status != LDB_SUCCESS) {
279                 if (ldb->err_string == NULL) {
280                         /* no error string was setup by the backend */
281                         ldb_asprintf_errstring(ldb,
282                                 "ldb transaction commit: %s (%d)",
283                                 ldb_strerror(status),
284                                 status);
285                 }
286         }
287         return status;
288 }
289
290 /*
291   cancel a transaction
292 */
293 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
294 {
295         struct ldb_module *module;
296         int status;
297         FIRST_OP(ldb, del_transaction);
298
299         status = module->ops->del_transaction(module);
300         if (status != LDB_SUCCESS) {
301                 if (ldb->err_string == NULL) {
302                         /* no error string was setup by the backend */
303                         ldb_asprintf_errstring(ldb,
304                                 "ldb transaction cancel: %s (%d)",
305                                 ldb_strerror(status),
306                                 status);
307                 }
308         }
309         return status;
310 }
311
312 int ldb_transaction_start(struct ldb_context *ldb)
313 {
314         /* disable autotransactions */
315         ldb->transaction_active++;
316
317         return ldb_transaction_start_internal(ldb);
318 }
319
320 int ldb_transaction_commit(struct ldb_context *ldb)
321 {
322         /* renable autotransactions (when we reach 0) */
323         if (ldb->transaction_active > 0)
324                 ldb->transaction_active--;
325
326         return ldb_transaction_commit_internal(ldb);
327 }
328
329 int ldb_transaction_cancel(struct ldb_context *ldb)
330 {
331         /* renable autotransactions (when we reach 0) */
332         if (ldb->transaction_active > 0)
333                 ldb->transaction_active--;
334
335         return ldb_transaction_cancel_internal(ldb);
336 }
337
338 static int ldb_autotransaction_start(struct ldb_context *ldb)
339 {
340         /* explicit transaction active, ignore autotransaction request */
341         if (ldb->transaction_active)
342                 return LDB_SUCCESS;
343
344         return ldb_transaction_start_internal(ldb);
345 }
346
347 static int ldb_autotransaction_commit(struct ldb_context *ldb)
348 {
349         /* explicit transaction active, ignore autotransaction request */
350         if (ldb->transaction_active)
351                 return LDB_SUCCESS;
352
353         return ldb_transaction_commit_internal(ldb);
354 }
355
356 static int ldb_autotransaction_cancel(struct ldb_context *ldb)
357 {
358         /* explicit transaction active, ignore autotransaction request */
359         if (ldb->transaction_active)
360                 return LDB_SUCCESS;
361
362         return ldb_transaction_cancel_internal(ldb);
363 }
364
365 /* autostarts a transacion if none active */
366 static int ldb_autotransaction_request(struct ldb_context *ldb,
367                                        struct ldb_request *req)
368 {
369         int ret;
370
371         ret = ldb_autotransaction_start(ldb);
372         if (ret != LDB_SUCCESS) {
373                 return ret;
374         }
375
376         ret = ldb_request(ldb, req);
377         if (ret == LDB_SUCCESS) {
378                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
379         }
380
381         if (ret == LDB_SUCCESS) {
382                 return ldb_autotransaction_commit(ldb);
383         }
384         ldb_autotransaction_cancel(ldb);
385
386         if (ldb->err_string == NULL) {
387                 /* no error string was setup by the backend */
388                 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
389         }
390
391         return ret;
392 }
393
394 int ldb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
395 {
396         struct event_context *ev;
397
398         if (!handle) {
399                 return LDB_ERR_UNAVAILABLE;
400         }
401
402         if (handle->state == LDB_ASYNC_DONE) {
403                 return handle->status;
404         }
405
406         ev = ldb_get_event_context(handle->ldb);
407         if (NULL == ev) {
408                 return LDB_ERR_OPERATIONS_ERROR;
409         }
410
411         switch (type) {
412         case LDB_WAIT_NONE:
413                 event_loop_once(ev);
414                 if (handle->state == LDB_ASYNC_DONE ||
415                     handle->status != LDB_SUCCESS) {
416                         return handle->status;
417                 }
418                 break;
419
420         case LDB_WAIT_ALL:
421                 while (handle->state != LDB_ASYNC_DONE) {
422                         event_loop_once(ev);
423                         if (handle->status != LDB_SUCCESS) {
424                                 return handle->status;
425                         }
426                 }
427                 return handle->status;
428         }
429
430         return LDB_SUCCESS;
431 }
432
433 /* set the specified timeout or, if timeout is 0 set the default timeout */
434 int ldb_set_timeout(struct ldb_context *ldb,
435                     struct ldb_request *req,
436                     int timeout)
437 {
438         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
439
440         if (timeout != 0) {
441                 req->timeout = timeout;
442         } else {
443                 req->timeout = ldb->default_timeout;
444         }
445         req->starttime = time(NULL);
446
447         return LDB_SUCCESS;
448 }
449
450 /* calculates the new timeout based on the previous starttime and timeout */
451 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb,
452                                   struct ldb_request *oldreq,
453                                   struct ldb_request *newreq)
454 {
455         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
456
457         if (oldreq == NULL) {
458                 return ldb_set_timeout(ldb, newreq, 0);
459         }
460
461         newreq->starttime = oldreq->starttime;
462         newreq->timeout = oldreq->timeout;
463
464         return LDB_SUCCESS;
465 }
466
467
468 /*
469    set the permissions for new files to be passed to open() in
470    backends that use local files
471  */
472 void ldb_set_create_perms(struct ldb_context *ldb, unsigned int perms)
473 {
474         ldb->create_perms = perms;
475 }
476
477 void ldb_set_event_context(struct ldb_context *ldb, struct event_context *ev)
478 {
479         ldb->ev_ctx = ev;
480 }
481
482 struct event_context * ldb_get_event_context(struct ldb_context *ldb)
483 {
484         return ldb->ev_ctx;
485 }
486
487 /*
488   start an ldb request
489   NOTE: the request must be a talloc context.
490   returns LDB_ERR_* on errors.
491 */
492 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
493 {
494         struct ldb_module *module;
495         int ret;
496
497         if (req->callback == NULL) {
498                 ldb_set_errstring(ldb, "Requests MUST define callbacks");
499                 return LDB_ERR_UNWILLING_TO_PERFORM;
500         }
501
502         ldb_reset_err_string(ldb);
503
504         /* call the first module in the chain */
505         switch (req->operation) {
506         case LDB_SEARCH:
507                 FIRST_OP(ldb, search);
508                 ret = module->ops->search(module, req);
509                 break;
510         case LDB_ADD:
511                 FIRST_OP(ldb, add);
512                 ret = module->ops->add(module, req);
513                 break;
514         case LDB_MODIFY:
515                 FIRST_OP(ldb, modify);
516                 ret = module->ops->modify(module, req);
517                 break;
518         case LDB_DELETE:
519                 FIRST_OP(ldb, del);
520                 ret = module->ops->del(module, req);
521                 break;
522         case LDB_RENAME:
523                 FIRST_OP(ldb, rename);
524                 ret = module->ops->rename(module, req);
525                 break;
526         case LDB_EXTENDED:
527                 FIRST_OP(ldb, extended);
528                 ret = module->ops->extended(module, req);
529                 break;
530         case LDB_SEQUENCE_NUMBER:
531                 FIRST_OP(ldb, sequence_number);
532                 ret = module->ops->sequence_number(module, req);
533                 break;
534         default:
535                 FIRST_OP(ldb, request);
536                 ret = module->ops->request(module, req);
537                 break;
538         }
539
540         return ret;
541 }
542
543 int ldb_request_done(struct ldb_request *req, int status)
544 {
545         req->handle->state = LDB_ASYNC_DONE;
546         req->handle->status = status;
547         return status;
548 }
549
550 /*
551   search the database given a LDAP-like search expression
552
553   returns an LDB error code
554
555   Use talloc_free to free the ldb_message returned in 'res', if successful
556
557 */
558 int ldb_search_default_callback(struct ldb_request *req,
559                                 struct ldb_reply *ares)
560 {
561         struct ldb_result *res;
562         int n;
563
564         res = talloc_get_type(req->context, struct ldb_result);
565
566         if (!ares) {
567                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
568         }
569         if (ares->error != LDB_SUCCESS) {
570                 return ldb_request_done(req, ares->error);
571         }
572
573         switch (ares->type) {
574         case LDB_REPLY_ENTRY:
575                 res->msgs = talloc_realloc(res, res->msgs,
576                                         struct ldb_message *, res->count + 2);
577                 if (! res->msgs) {
578                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
579                 }
580
581                 res->msgs[res->count + 1] = NULL;
582
583                 res->msgs[res->count] = talloc_move(res->msgs, &ares->message);
584                 res->count++;
585                 break;
586
587         case LDB_REPLY_REFERRAL:
588                 if (res->refs) {
589                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
590                 } else {
591                         n = 0;
592                 }
593
594                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
595                 if (! res->refs) {
596                         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
597                 }
598
599                 res->refs[n] = talloc_move(res->refs, &ares->referral);
600                 res->refs[n + 1] = NULL;
601                 break;
602
603         case LDB_REPLY_DONE:
604                 /* TODO: we should really support controls on entries
605                  * and referrals too! */
606                 res->controls = talloc_move(res, &ares->controls);
607
608                 /* this is the last message, and means the request is done */
609                 /* we have to signal and eventual ldb_wait() waiting that the
610                  * async request operation was completed */
611                 return ldb_request_done(req, LDB_SUCCESS);
612         }
613
614         talloc_free(ares);
615         return LDB_SUCCESS;
616 }
617
618 int ldb_op_default_callback(struct ldb_request *req, struct ldb_reply *ares)
619 {
620         int ret;
621
622         if (!ares) {
623                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
624         }
625
626         if (ares->error != LDB_SUCCESS) {
627                 ret = ares->error;
628                 talloc_free(ares);
629                 return ldb_request_done(req, ret);
630         }
631
632         if (ares->type != LDB_REPLY_DONE) {
633                 talloc_free(ares);
634                 ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
635                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
636         }
637
638         talloc_free(ares);
639         return ldb_request_done(req, LDB_SUCCESS);
640 }
641
642 int ldb_build_search_req_ex(struct ldb_request **ret_req,
643                         struct ldb_context *ldb,
644                         void *mem_ctx,
645                         struct ldb_dn *base,
646                         enum ldb_scope scope,
647                         struct ldb_parse_tree *tree,
648                         const char * const *attrs,
649                         struct ldb_control **controls,
650                         void *context,
651                         ldb_request_callback_t callback,
652                         struct ldb_request *parent)
653 {
654         struct ldb_request *req;
655
656         *ret_req = NULL;
657
658         req = talloc(mem_ctx, struct ldb_request);
659         if (req == NULL) {
660                 ldb_oom(ldb);
661                 return LDB_ERR_OPERATIONS_ERROR;
662         }
663
664         req->operation = LDB_SEARCH;
665         if (base == NULL) {
666                 req->op.search.base = ldb_dn_new(req, ldb, NULL);
667         } else {
668                 req->op.search.base = base;
669         }
670         req->op.search.scope = scope;
671
672         req->op.search.tree = tree;
673         if (req->op.search.tree == NULL) {
674                 ldb_set_errstring(ldb, "'tree' can't be NULL");
675                 talloc_free(req);
676                 return LDB_ERR_OPERATIONS_ERROR;
677         }
678
679         req->op.search.attrs = attrs;
680         req->controls = controls;
681         req->context = context;
682         req->callback = callback;
683
684         ldb_set_timeout_from_prev_req(ldb, parent, req);
685
686         req->handle = ldb_handle_new(req, ldb);
687         if (req->handle == NULL) {
688                 ldb_oom(ldb);
689                 return LDB_ERR_OPERATIONS_ERROR;
690         }
691
692         *ret_req = req;
693         return LDB_SUCCESS;
694 }
695
696 int ldb_build_search_req(struct ldb_request **ret_req,
697                         struct ldb_context *ldb,
698                         void *mem_ctx,
699                         struct ldb_dn *base,
700                         enum ldb_scope scope,
701                         const char *expression,
702                         const char * const *attrs,
703                         struct ldb_control **controls,
704                         void *context,
705                         ldb_request_callback_t callback,
706                         struct ldb_request *parent)
707 {
708         struct ldb_parse_tree *tree;
709         int ret;
710
711         tree = ldb_parse_tree(mem_ctx, expression);
712         if (tree == NULL) {
713                 ldb_set_errstring(ldb, "Unable to parse search expression");
714                 return LDB_ERR_OPERATIONS_ERROR;
715         }
716
717         ret = ldb_build_search_req_ex(ret_req, ldb, mem_ctx, base,
718                                       scope, tree, attrs, controls,
719                                       context, callback, parent);
720         if (ret == LDB_SUCCESS) {
721                 talloc_steal(*ret_req, tree);
722         }
723         return ret;
724 }
725
726 int ldb_build_add_req(struct ldb_request **ret_req,
727                         struct ldb_context *ldb,
728                         void *mem_ctx,
729                         const struct ldb_message *message,
730                         struct ldb_control **controls,
731                         void *context,
732                         ldb_request_callback_t callback,
733                         struct ldb_request *parent)
734 {
735         struct ldb_request *req;
736
737         *ret_req = NULL;
738
739         req = talloc(mem_ctx, struct ldb_request);
740         if (req == NULL) {
741                 ldb_set_errstring(ldb, "Out of Memory");
742                 return LDB_ERR_OPERATIONS_ERROR;
743         }
744
745         req->operation = LDB_ADD;
746         req->op.add.message = message;
747         req->controls = controls;
748         req->context = context;
749         req->callback = callback;
750
751         ldb_set_timeout_from_prev_req(ldb, parent, req);
752
753         req->handle = ldb_handle_new(req, ldb);
754         if (req->handle == NULL) {
755                 ldb_oom(ldb);
756                 return LDB_ERR_OPERATIONS_ERROR;
757         }
758
759         *ret_req = req;
760
761         return LDB_SUCCESS;
762 }
763
764 int ldb_build_mod_req(struct ldb_request **ret_req,
765                         struct ldb_context *ldb,
766                         void *mem_ctx,
767                         const struct ldb_message *message,
768                         struct ldb_control **controls,
769                         void *context,
770                         ldb_request_callback_t callback,
771                         struct ldb_request *parent)
772 {
773         struct ldb_request *req;
774
775         *ret_req = NULL;
776
777         req = talloc(mem_ctx, struct ldb_request);
778         if (req == NULL) {
779                 ldb_set_errstring(ldb, "Out of Memory");
780                 return LDB_ERR_OPERATIONS_ERROR;
781         }
782
783         req->operation = LDB_MODIFY;
784         req->op.mod.message = message;
785         req->controls = controls;
786         req->context = context;
787         req->callback = callback;
788
789         ldb_set_timeout_from_prev_req(ldb, parent, req);
790
791         req->handle = ldb_handle_new(req, ldb);
792         if (req->handle == NULL) {
793                 ldb_oom(ldb);
794                 return LDB_ERR_OPERATIONS_ERROR;
795         }
796
797         *ret_req = req;
798
799         return LDB_SUCCESS;
800 }
801
802 int ldb_build_del_req(struct ldb_request **ret_req,
803                         struct ldb_context *ldb,
804                         void *mem_ctx,
805                         struct ldb_dn *dn,
806                         struct ldb_control **controls,
807                         void *context,
808                         ldb_request_callback_t callback,
809                         struct ldb_request *parent)
810 {
811         struct ldb_request *req;
812
813         *ret_req = NULL;
814
815         req = talloc(mem_ctx, struct ldb_request);
816         if (req == NULL) {
817                 ldb_set_errstring(ldb, "Out of Memory");
818                 return LDB_ERR_OPERATIONS_ERROR;
819         }
820
821         req->operation = LDB_DELETE;
822         req->op.del.dn = dn;
823         req->controls = controls;
824         req->context = context;
825         req->callback = callback;
826
827         ldb_set_timeout_from_prev_req(ldb, parent, req);
828
829         req->handle = ldb_handle_new(req, ldb);
830         if (req->handle == NULL) {
831                 ldb_oom(ldb);
832                 return LDB_ERR_OPERATIONS_ERROR;
833         }
834
835         *ret_req = req;
836
837         return LDB_SUCCESS;
838 }
839
840 int ldb_build_rename_req(struct ldb_request **ret_req,
841                         struct ldb_context *ldb,
842                         void *mem_ctx,
843                         struct ldb_dn *olddn,
844                         struct ldb_dn *newdn,
845                         struct ldb_control **controls,
846                         void *context,
847                         ldb_request_callback_t callback,
848                         struct ldb_request *parent)
849 {
850         struct ldb_request *req;
851
852         *ret_req = NULL;
853
854         req = talloc(mem_ctx, struct ldb_request);
855         if (req == NULL) {
856                 ldb_set_errstring(ldb, "Out of Memory");
857                 return LDB_ERR_OPERATIONS_ERROR;
858         }
859
860         req->operation = LDB_RENAME;
861         req->op.rename.olddn = olddn;
862         req->op.rename.newdn = newdn;
863         req->controls = controls;
864         req->context = context;
865         req->callback = callback;
866
867         ldb_set_timeout_from_prev_req(ldb, parent, req);
868
869         req->handle = ldb_handle_new(req, ldb);
870         if (req->handle == NULL) {
871                 ldb_oom(ldb);
872                 return LDB_ERR_OPERATIONS_ERROR;
873         }
874
875         *ret_req = req;
876
877         return LDB_SUCCESS;
878 }
879
880 int ldb_extended_default_callback(struct ldb_request *req,
881                                   struct ldb_reply *ares)
882 {
883         struct ldb_result *res;
884
885         res = talloc_get_type(req->context, struct ldb_result);
886
887         if (!ares) {
888                 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
889         }
890         if (ares->error != LDB_SUCCESS) {
891                 return ldb_request_done(req, ares->error);
892         }
893
894         if (ares->type == LDB_REPLY_DONE) {
895
896                 /* TODO: we should really support controls on entries and referrals too! */
897                 res->extended = talloc_move(res, &ares->response);
898                 res->controls = talloc_move(res, &ares->controls);
899
900                 talloc_free(ares);
901                 return ldb_request_done(req, LDB_SUCCESS);
902         }
903
904         talloc_free(ares);
905         ldb_set_errstring(req->handle->ldb, "Invalid reply type!");
906         return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
907 }
908
909 int ldb_build_extended_req(struct ldb_request **ret_req,
910                            struct ldb_context *ldb,
911                            void *mem_ctx,
912                            const char *oid,
913                            void *data,
914                            struct ldb_control **controls,
915                            void *context,
916                            ldb_request_callback_t callback,
917                            struct ldb_request *parent)
918 {
919         struct ldb_request *req;
920
921         *ret_req = NULL;
922
923         req = talloc(mem_ctx, struct ldb_request);
924         if (req == NULL) {
925                 ldb_set_errstring(ldb, "Out of Memory");
926                 return LDB_ERR_OPERATIONS_ERROR;
927         }
928
929         req->operation = LDB_EXTENDED;
930         req->op.extended.oid = oid;
931         req->op.extended.data = data;
932         req->controls = controls;
933         req->context = context;
934         req->callback = callback;
935
936         ldb_set_timeout_from_prev_req(ldb, parent, req);
937
938         req->handle = ldb_handle_new(req, ldb);
939         if (req->handle == NULL) {
940                 ldb_oom(ldb);
941                 return LDB_ERR_OPERATIONS_ERROR;
942         }
943
944         *ret_req = req;
945
946         return LDB_SUCCESS;
947 }
948
949 int ldb_extended(struct ldb_context *ldb,
950                  const char *oid,
951                  void *data,
952                  struct ldb_result **_res)
953 {
954         struct ldb_request *req;
955         int ret;
956         struct ldb_result *res;
957
958         *_res = NULL;
959
960         res = talloc_zero(ldb, struct ldb_result);
961         if (!res) {
962                 return LDB_ERR_OPERATIONS_ERROR;
963         }
964
965         ret = ldb_build_extended_req(&req, ldb, ldb,
966                                      oid, data, NULL,
967                                      res, ldb_extended_default_callback,
968                                      NULL);
969         if (ret != LDB_SUCCESS) goto done;
970
971         ldb_set_timeout(ldb, req, 0); /* use default timeout */
972
973         ret = ldb_request(ldb, req);
974
975         if (ret == LDB_SUCCESS) {
976                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
977         }
978
979         talloc_free(req);
980
981 done:
982         if (ret != LDB_SUCCESS) {
983                 talloc_free(res);
984         }
985
986         *_res = res;
987         return ret;
988 }
989
990 /*
991   note that ldb_search() will automatically replace a NULL 'base' value
992   with the defaultNamingContext from the rootDSE if available.
993 */
994 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
995                 struct ldb_result **result, struct ldb_dn *base,
996                 enum ldb_scope scope, const char * const *attrs,
997                 const char *exp_fmt, ...)
998 {
999         struct ldb_request *req;
1000         struct ldb_result *res;
1001         char *expression;
1002         va_list ap;
1003         int ret;
1004
1005         expression = NULL;
1006         *result = NULL;
1007         req = NULL;
1008
1009         res = talloc_zero(mem_ctx, struct ldb_result);
1010         if (!res) {
1011                 return LDB_ERR_OPERATIONS_ERROR;
1012         }
1013
1014         if (exp_fmt) {
1015                 va_start(ap, exp_fmt);
1016                 expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
1017                 va_end(ap);
1018
1019                 if (!expression) {
1020                         talloc_free(res);
1021                         return LDB_ERR_OPERATIONS_ERROR;
1022                 }
1023         }
1024
1025         ret = ldb_build_search_req(&req, ldb, mem_ctx,
1026                                         base?base:ldb_get_default_basedn(ldb),
1027                                         scope,
1028                                         expression,
1029                                         attrs,
1030                                         NULL,
1031                                         res,
1032                                         ldb_search_default_callback,
1033                                         NULL);
1034
1035         if (ret != LDB_SUCCESS) goto done;
1036
1037         ret = ldb_request(ldb, req);
1038
1039         if (ret == LDB_SUCCESS) {
1040                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
1041         }
1042
1043 done:
1044         if (ret != LDB_SUCCESS) {
1045                 talloc_free(res);
1046                 res = NULL;
1047         }
1048
1049         talloc_free(expression);
1050         talloc_free(req);
1051
1052         *result = res;
1053         return ret;
1054 }
1055
1056 /*
1057   add a record to the database. Will fail if a record with the given class
1058   and key already exists
1059 */
1060 int ldb_add(struct ldb_context *ldb,
1061             const struct ldb_message *message)
1062 {
1063         struct ldb_request *req;
1064         int ret;
1065
1066         ret = ldb_msg_sanity_check(ldb, message);
1067         if (ret != LDB_SUCCESS) {
1068                 return ret;
1069         }
1070
1071         ret = ldb_build_add_req(&req, ldb, ldb,
1072                                         message,
1073                                         NULL,
1074                                         NULL,
1075                                         ldb_op_default_callback,
1076                                         NULL);
1077
1078         if (ret != LDB_SUCCESS) return ret;
1079
1080         /* do request and autostart a transaction */
1081         ret = ldb_autotransaction_request(ldb, req);
1082
1083         talloc_free(req);
1084         return ret;
1085 }
1086
1087 /*
1088   modify the specified attributes of a record
1089 */
1090 int ldb_modify(struct ldb_context *ldb,
1091                const struct ldb_message *message)
1092 {
1093         struct ldb_request *req;
1094         int ret;
1095
1096         ret = ldb_msg_sanity_check(ldb, message);
1097         if (ret != LDB_SUCCESS) {
1098                 return ret;
1099         }
1100
1101         ret = ldb_build_mod_req(&req, ldb, ldb,
1102                                         message,
1103                                         NULL,
1104                                         NULL,
1105                                         ldb_op_default_callback,
1106                                         NULL);
1107
1108         if (ret != LDB_SUCCESS) return ret;
1109
1110         /* do request and autostart a transaction */
1111         ret = ldb_autotransaction_request(ldb, req);
1112
1113         talloc_free(req);
1114         return ret;
1115 }
1116
1117
1118 /*
1119   delete a record from the database
1120 */
1121 int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn)
1122 {
1123         struct ldb_request *req;
1124         int ret;
1125
1126         ret = ldb_build_del_req(&req, ldb, ldb,
1127                                         dn,
1128                                         NULL,
1129                                         NULL,
1130                                         ldb_op_default_callback,
1131                                         NULL);
1132
1133         if (ret != LDB_SUCCESS) return ret;
1134
1135         /* do request and autostart a transaction */
1136         ret = ldb_autotransaction_request(ldb, req);
1137
1138         talloc_free(req);
1139         return ret;
1140 }
1141
1142 /*
1143   rename a record in the database
1144 */
1145 int ldb_rename(struct ldb_context *ldb,
1146                 struct ldb_dn *olddn, struct ldb_dn *newdn)
1147 {
1148         struct ldb_request *req;
1149         int ret;
1150
1151         ret = ldb_build_rename_req(&req, ldb, ldb,
1152                                         olddn,
1153                                         newdn,
1154                                         NULL,
1155                                         NULL,
1156                                         ldb_op_default_callback,
1157                                         NULL);
1158
1159         if (ret != LDB_SUCCESS) return ret;
1160
1161         /* do request and autostart a transaction */
1162         ret = ldb_autotransaction_request(ldb, req);
1163
1164         talloc_free(req);
1165         return ret;
1166 }
1167
1168
1169 /*
1170   return the global sequence number
1171 */
1172 int ldb_sequence_number(struct ldb_context *ldb,
1173                         enum ldb_sequence_type type, uint64_t *seq_num)
1174 {
1175         struct ldb_request *req;
1176         int ret;
1177
1178         req = talloc_zero(ldb, struct ldb_request);
1179         if (req == NULL) {
1180                 ldb_set_errstring(ldb, "Out of Memory");
1181                 return LDB_ERR_OPERATIONS_ERROR;
1182         }
1183
1184         req->operation = LDB_SEQUENCE_NUMBER;
1185         req->controls = NULL;
1186         req->context = NULL;
1187         req->callback = ldb_op_default_callback;
1188         ldb_set_timeout(ldb, req, 0); /* use default timeout */
1189
1190         req->op.seq_num.type = type;
1191         /* do request and autostart a transaction */
1192         ret = ldb_request(ldb, req);
1193
1194         if (ret == LDB_SUCCESS) {
1195                 *seq_num = req->op.seq_num.seq_num;
1196         }
1197
1198         talloc_free(req);
1199         return ret;
1200 }
1201
1202
1203
1204 /*
1205   return extended error information
1206 */
1207 const char *ldb_errstring(struct ldb_context *ldb)
1208 {
1209         if (ldb->err_string) {
1210                 return ldb->err_string;
1211         }
1212
1213         return NULL;
1214 }
1215
1216 /*
1217   return a string explaining what a ldb error constant meancs
1218 */
1219 const char *ldb_strerror(int ldb_err)
1220 {
1221         switch (ldb_err) {
1222         case LDB_SUCCESS:
1223                 return "Success";
1224         case LDB_ERR_OPERATIONS_ERROR:
1225                 return "Operations error";
1226         case LDB_ERR_PROTOCOL_ERROR:
1227                 return "Protocol error";
1228         case LDB_ERR_TIME_LIMIT_EXCEEDED:
1229                 return "Time limit exceeded";
1230         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
1231                 return "Size limit exceeded";
1232         case LDB_ERR_COMPARE_FALSE:
1233                 return "Compare false";
1234         case LDB_ERR_COMPARE_TRUE:
1235                 return "Compare true";
1236         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
1237                 return "Auth method not supported";
1238         case LDB_ERR_STRONG_AUTH_REQUIRED:
1239                 return "Strong auth required";
1240 /* 9 RESERVED */
1241         case LDB_ERR_REFERRAL:
1242                 return "Referral error";
1243         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
1244                 return "Admin limit exceeded";
1245         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
1246                 return "Unsupported critical extension";
1247         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
1248                 return "Confidentiality required";
1249         case LDB_ERR_SASL_BIND_IN_PROGRESS:
1250                 return "SASL bind in progress";
1251         case LDB_ERR_NO_SUCH_ATTRIBUTE:
1252                 return "No such attribute";
1253         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
1254                 return "Undefined attribute type";
1255         case LDB_ERR_INAPPROPRIATE_MATCHING:
1256                 return "Inappropriate matching";
1257         case LDB_ERR_CONSTRAINT_VIOLATION:
1258                 return "Constraint violation";
1259         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
1260                 return "Attribute or value exists";
1261         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
1262                 return "Invalid attribute syntax";
1263 /* 22-31 unused */
1264         case LDB_ERR_NO_SUCH_OBJECT:
1265                 return "No such object";
1266         case LDB_ERR_ALIAS_PROBLEM:
1267                 return "Alias problem";
1268         case LDB_ERR_INVALID_DN_SYNTAX:
1269                 return "Invalid DN syntax";
1270 /* 35 RESERVED */
1271         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
1272                 return "Alias dereferencing problem";
1273 /* 37-47 unused */
1274         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
1275                 return "Inappropriate authentication";
1276         case LDB_ERR_INVALID_CREDENTIALS:
1277                 return "Invalid credentials";
1278         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
1279                 return "insufficient access rights";
1280         case LDB_ERR_BUSY:
1281                 return "Busy";
1282         case LDB_ERR_UNAVAILABLE:
1283                 return "Unavailable";
1284         case LDB_ERR_UNWILLING_TO_PERFORM:
1285                 return "Unwilling to perform";
1286         case LDB_ERR_LOOP_DETECT:
1287                 return "Loop detect";
1288 /* 55-63 unused */
1289         case LDB_ERR_NAMING_VIOLATION:
1290                 return "Naming violation";
1291         case LDB_ERR_OBJECT_CLASS_VIOLATION:
1292                 return "Object class violation";
1293         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
1294                 return "Not allowed on non-leaf";
1295         case LDB_ERR_NOT_ALLOWED_ON_RDN:
1296                 return "Not allowed on RDN";
1297         case LDB_ERR_ENTRY_ALREADY_EXISTS:
1298                 return "Entry already exists";
1299         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
1300                 return "Object class mods prohibited";
1301 /* 70 RESERVED FOR CLDAP */
1302         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
1303                 return "Affects multiple DSAs";
1304 /* 72-79 unused */
1305         case LDB_ERR_OTHER:
1306                 return "Other";
1307         }
1308
1309         return "Unknown error";
1310 }
1311
1312 /*
1313   set backend specific opaque parameters
1314 */
1315 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
1316 {
1317         struct ldb_opaque *o;
1318
1319         /* allow updating an existing value */
1320         for (o=ldb->opaque;o;o=o->next) {
1321                 if (strcmp(o->name, name) == 0) {
1322                         o->value = value;
1323                         return LDB_SUCCESS;
1324                 }
1325         }
1326
1327         o = talloc(ldb, struct ldb_opaque);
1328         if (o == NULL) {
1329                 ldb_oom(ldb);
1330                 return LDB_ERR_OTHER;
1331         }
1332         o->next = ldb->opaque;
1333         o->name = name;
1334         o->value = value;
1335         ldb->opaque = o;
1336         return LDB_SUCCESS;
1337 }
1338
1339 /*
1340   get a previously set opaque value
1341 */
1342 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
1343 {
1344         struct ldb_opaque *o;
1345         for (o=ldb->opaque;o;o=o->next) {
1346                 if (strcmp(o->name, name) == 0) {
1347                         return o->value;
1348                 }
1349         }
1350         return NULL;
1351 }