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