r16071: tdb has nested transactions
[metze/samba/wip.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-2006
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 2 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, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb core API
30  *
31  *  Description: core API routines interfacing to ldb backends
32  *
33  *  Author: Andrew Tridgell
34  */
35
36 #include "includes.h"
37 #include "ldb/include/includes.h"
38
39 /* 
40    initialise a ldb context
41    The mem_ctx is optional
42 */
43 struct ldb_context *ldb_init(void *mem_ctx)
44 {
45         struct ldb_context *ldb = talloc_zero(mem_ctx, struct ldb_context);
46         int ret;
47
48         ret = ldb_setup_wellknown_attributes(ldb);
49         if (ret != 0) {
50                 talloc_free(ldb);
51                 return NULL;
52         }
53
54         ldb_set_utf8_default(ldb);
55
56         return ldb;
57 }
58
59 static struct ldb_backend {
60         const char *name;
61         ldb_connect_fn connect_fn;
62         struct ldb_backend *prev, *next;
63 } *ldb_backends = NULL;
64 /*
65  register a new ldb backend
66 */
67 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
68 {
69         struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
70
71         /* Maybe check for duplicity here later on? */
72
73         backend->name = talloc_strdup(backend, url_prefix);
74         backend->connect_fn = connectfn;
75         DLIST_ADD(ldb_backends, backend);
76
77         return LDB_SUCCESS;
78 }
79
80 static ldb_connect_fn ldb_find_backend(const char *url)
81 {
82         struct ldb_backend *backend;
83
84         for (backend = ldb_backends; backend; backend = backend->next) {
85                 if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
86                         return backend->connect_fn;
87                 }
88         }
89
90         return NULL;
91 }
92
93 /* 
94  connect to a database. The URL can either be one of the following forms
95    ldb://path
96    ldapi://path
97
98    flags is made up of LDB_FLG_*
99
100    the options are passed uninterpreted to the backend, and are
101    backend specific
102 */
103 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
104 {
105         int ret;
106         char *backend;
107         ldb_connect_fn fn;
108
109         if (strchr(url, ':') != NULL) {
110                 backend = talloc_strndup(ldb, url, strchr(url, ':')-url);
111         } else {
112                 /* Default to tdb */
113                 backend = talloc_strdup(ldb, "tdb");
114         }
115
116         fn = ldb_find_backend(backend);
117
118         if (fn == NULL) {
119                 if (ldb_try_load_dso(ldb, backend) == 0) {
120                         fn = ldb_find_backend(backend);
121                 }
122         }
123
124         talloc_free(backend);
125
126         if (fn == NULL) {
127                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
128                 return LDB_ERR_OTHER;
129         }
130
131         ret = fn(ldb, url, flags, options);
132
133         if (ret != LDB_SUCCESS) {
134                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
135                 return ret;
136         }
137
138         if (ldb_load_modules(ldb, options) != LDB_SUCCESS) {
139                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
140                 return LDB_ERR_OTHER;
141         }
142
143         /* TODO: get timeout from options if available there */
144         ldb->default_timeout = 300; /* set default to 5 minutes */
145
146         return LDB_SUCCESS;
147 }
148
149 void ldb_set_errstring(struct ldb_context *ldb, char *err_string)
150 {
151         if (ldb->err_string) {
152                 talloc_free(ldb->err_string);
153         }
154         ldb->err_string = talloc_steal(ldb, err_string);
155 }
156
157 void ldb_reset_err_string(struct ldb_context *ldb)
158 {
159         if (ldb->err_string) {
160                 talloc_free(ldb->err_string);
161                 ldb->err_string = NULL;
162         }
163 }
164
165 #define FIRST_OP(ldb, op) do { \
166         module = ldb->modules; \
167         while (module && module->ops->op == NULL) module = module->next; \
168         if (module == NULL) return LDB_ERR_OPERATIONS_ERROR; \
169 } while (0)
170
171 /*
172   start a transaction
173 */
174 static int ldb_transaction_start_internal(struct ldb_context *ldb)
175 {
176         struct ldb_module *module;
177         int status;
178         FIRST_OP(ldb, start_transaction);
179
180         ldb_reset_err_string(ldb);
181
182         status = module->ops->start_transaction(module);
183         if (status != LDB_SUCCESS) {
184                 if (ldb->err_string == NULL) {
185                         /* no error string was setup by the backend */
186                         ldb_set_errstring(ldb, 
187                                           talloc_asprintf(ldb, "ldb transaction start error %d", status));
188                 }
189         }
190         return status;
191 }
192
193 /*
194   commit a transaction
195 */
196 static int ldb_transaction_commit_internal(struct ldb_context *ldb)
197 {
198         struct ldb_module *module;
199         int status;
200         FIRST_OP(ldb, end_transaction);
201
202         ldb_reset_err_string(ldb);
203
204         status = module->ops->end_transaction(module);
205         if (status != LDB_SUCCESS) {
206                 if (ldb->err_string == NULL) {
207                         /* no error string was setup by the backend */
208                         ldb_set_errstring(ldb, 
209                                           talloc_asprintf(ldb, "ldb transaction commit error %d", status));
210                 }
211         }
212         return status;
213 }
214
215 /*
216   cancel a transaction
217 */
218 static int ldb_transaction_cancel_internal(struct ldb_context *ldb)
219 {
220         struct ldb_module *module;
221         int status;
222         FIRST_OP(ldb, del_transaction);
223
224         status = module->ops->del_transaction(module);
225         if (status != LDB_SUCCESS) {
226                 if (ldb->err_string == NULL) {
227                         /* no error string was setup by the backend */
228                         ldb_set_errstring(ldb, 
229                                           talloc_asprintf(ldb, "ldb transaction cancel error %d", status));
230                 }
231         }
232         return status;
233 }
234
235 int ldb_transaction_start(struct ldb_context *ldb)
236 {
237         /* disable autotransactions */
238         ldb->transaction_active++;
239
240         return ldb_transaction_start_internal(ldb);
241 }
242
243 int ldb_transaction_commit(struct ldb_context *ldb)
244 {
245         /* renable autotransactions (when we reach 0) */
246         if (ldb->transaction_active > 0)
247                 ldb->transaction_active--;
248
249         return ldb_transaction_commit_internal(ldb);
250 }
251
252 int ldb_transaction_cancel(struct ldb_context *ldb)
253 {
254         /* renable autotransactions (when we reach 0) */
255         if (ldb->transaction_active > 0)
256                 ldb->transaction_active--;
257
258         return ldb_transaction_cancel_internal(ldb);
259 }
260
261 int ldb_autotransaction_start(struct ldb_context *ldb)
262 {
263         /* explicit transaction active, ignore autotransaction request */
264         if (ldb->transaction_active)
265                 return LDB_SUCCESS;
266
267         return ldb_transaction_start_internal(ldb);
268 }
269
270 int ldb_autotransaction_commit(struct ldb_context *ldb)
271 {
272         /* explicit transaction active, ignore autotransaction request */
273         if (ldb->transaction_active)
274                 return LDB_SUCCESS;
275
276         return ldb_transaction_commit_internal(ldb);
277 }
278
279 int ldb_autotransaction_cancel(struct ldb_context *ldb)
280 {
281         /* explicit transaction active, ignore autotransaction request */
282         if (ldb->transaction_active)
283                 return LDB_SUCCESS;
284
285         return ldb_transaction_cancel_internal(ldb);
286 }
287
288 /* autostarts a transacion if none active */
289 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
290 {
291         int ret;
292
293         ret = ldb_autotransaction_start(ldb);
294         if (ret != LDB_SUCCESS) {
295                 return ret;
296         }
297
298         ret = ldb_request(ldb, req);
299         if (ret == LDB_SUCCESS) {
300                 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
301         }
302
303         if (ret == LDB_SUCCESS) {
304                 return ldb_autotransaction_commit(ldb);
305         }
306         ldb_autotransaction_cancel(ldb);
307
308         if (ldb->err_string == NULL) {
309                 /* no error string was setup by the backend */
310                 ldb_set_errstring(ldb, 
311                                   talloc_asprintf(ldb, "%s (%d)", 
312                                                   ldb_strerror(ret), ret));
313         }
314
315         return ret;
316 }
317
318 int ldb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
319 {
320         if (!handle) {
321                 return LDB_SUCCESS;
322         }
323
324         return handle->module->ops->async_wait(handle, type);
325 }
326
327 /* set the specified timeout or, if timeout is 0 set the default timeout */
328 /* timeout == -1 means no timeout */
329 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout)
330 {
331         if (req == NULL) return LDB_ERR_OPERATIONS_ERROR;
332         
333         if (timeout != 0) {
334                 req->async.timeout = timeout;
335         } else {
336                 req->async.timeout = ldb->default_timeout;
337         }
338         req->async.starttime = time(NULL);
339
340         return LDB_SUCCESS;
341 }
342
343 /* calculates the new timeout based on the previous starttime and timeout */
344 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq)
345 {
346         time_t now;
347
348         if (newreq == NULL) return LDB_ERR_OPERATIONS_ERROR;
349
350         now = time(NULL);
351
352         if (oldreq == NULL)
353                 return ldb_set_timeout(ldb, newreq, 0);
354
355         if ((now - oldreq->async.starttime) > oldreq->async.timeout) {
356                 return LDB_ERR_TIME_LIMIT_EXCEEDED;
357         }
358         newreq->async.starttime = oldreq->async.starttime;
359         newreq->async.timeout = oldreq->async.timeout - (now - oldreq->async.starttime);
360
361         return LDB_SUCCESS;
362 }
363
364 /*
365   start an ldb request
366   NOTE: the request must be a talloc context.
367   returns LDB_ERR_* on errors.
368 */
369 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
370 {
371         struct ldb_module *module;
372         int ret;
373
374         ldb_reset_err_string(ldb);
375
376         /* call the first module in the chain */
377         switch (req->operation) {
378         case LDB_SEARCH:
379                 FIRST_OP(ldb, search);
380                 ret = module->ops->search(module, req);
381                 break;
382         case LDB_ADD:
383                 FIRST_OP(ldb, add);
384                 ret = module->ops->add(module, req);
385                 break;
386         case LDB_MODIFY:
387                 FIRST_OP(ldb, modify);
388                 ret = module->ops->modify(module, req);
389                 break;
390         case LDB_DELETE:
391                 FIRST_OP(ldb, del);
392                 ret = module->ops->del(module, req);
393                 break;
394         case LDB_RENAME:
395                 FIRST_OP(ldb, rename);
396                 ret = module->ops->rename(module, req);
397                 break;
398         default:
399                 FIRST_OP(ldb, request);
400                 ret = module->ops->request(module, req);
401                 break;
402         }
403
404         return ret;
405 }
406
407 /*
408   search the database given a LDAP-like search expression
409
410   returns an LDB error code
411
412   Use talloc_free to free the ldb_message returned in 'res', if successful
413
414 */
415 static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
416 {
417         struct ldb_result *res;
418         int n;
419         
420         if (!context) {
421                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
422                 return LDB_ERR_OPERATIONS_ERROR;
423         }       
424
425         res = *((struct ldb_result **)context);
426
427         if (!res || !ares) {
428                 goto error;
429         }
430
431         if (ares->type == LDB_REPLY_ENTRY) {
432                 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
433                 if (! res->msgs) {
434                         goto error;
435                 }
436
437                 res->msgs[res->count + 1] = NULL;
438
439                 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
440                 if (! res->msgs[res->count]) {
441                         goto error;
442                 }
443
444                 res->count++;
445         }
446
447         if (ares->type == LDB_REPLY_REFERRAL) {
448                 if (res->refs) {
449                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
450                 } else {
451                         n = 0;
452                 }
453
454                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
455                 if (! res->refs) {
456                         goto error;
457                 }
458
459                 res->refs[n] = talloc_steal(res->refs, ares->referral);
460                 res->refs[n + 1] = NULL;
461         }
462
463         if (ares->controls) {
464                 res->controls = talloc_steal(res, ares->controls);
465                 if (! res->controls) {
466                         goto error;
467                 }
468         }
469
470         talloc_free(ares);
471         return LDB_SUCCESS;
472
473 error:
474         talloc_free(ares);
475         talloc_free(res);
476         *((struct ldb_result **)context) = NULL;
477         return LDB_ERR_OPERATIONS_ERROR;
478 }
479
480 int ldb_search(struct ldb_context *ldb, 
481                const struct ldb_dn *base,
482                enum ldb_scope scope,
483                const char *expression,
484                const char * const *attrs, 
485                struct ldb_result **res)
486 {
487         struct ldb_request *req;
488         int ret;
489
490         *res = talloc_zero(ldb, struct ldb_result);
491         if (! *res) {
492                 return LDB_ERR_OPERATIONS_ERROR;
493         }
494
495         req = talloc(ldb, struct ldb_request);
496         if (req == NULL) {
497                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
498                 return LDB_ERR_OPERATIONS_ERROR;
499         }
500
501         req->operation = LDB_SEARCH;
502         req->op.search.base = base;
503         req->op.search.scope = scope;
504
505         req->op.search.tree = ldb_parse_tree(req, expression);
506         if (req->op.search.tree == NULL) {
507                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Unable to parse search expression"));
508                 talloc_free(req);
509                 return LDB_ERR_OPERATIONS_ERROR;
510         }
511
512         req->op.search.attrs = attrs;
513         req->controls = NULL;
514         req->async.context = res;
515         req->async.callback = ldb_search_callback;
516         ldb_set_timeout(ldb, req, 0); /* use default timeout */
517
518         ret = ldb_request(ldb, req);
519         
520         if (ret == LDB_SUCCESS) {
521                 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
522         }
523         
524         if (ret != LDB_SUCCESS) {
525                 talloc_free(*res);
526                 *res = NULL;
527         }
528
529         talloc_free(req);
530         return ret;
531 }
532
533
534 /*
535   add a record to the database. Will fail if a record with the given class and key
536   already exists
537 */
538 int ldb_add(struct ldb_context *ldb, 
539             const struct ldb_message *message)
540 {
541         struct ldb_request *req;
542         int ret;
543
544         ret = ldb_msg_sanity_check(message);
545         if (ret != LDB_SUCCESS) return ret;
546
547         req = talloc(ldb, struct ldb_request);
548         if (req == NULL) {
549                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
550                 return LDB_ERR_OPERATIONS_ERROR;
551         }
552
553         req->operation = LDB_ADD;
554         req->op.add.message = message;
555         req->controls = NULL;
556         req->async.context = NULL;
557         req->async.callback = NULL;
558         ldb_set_timeout(ldb, req, 0); /* use default timeout */
559
560         /* do request and autostart a transaction */
561         ret = ldb_autotransaction_request(ldb, req);
562
563         talloc_free(req);
564         return ret;
565 }
566
567 /*
568   modify the specified attributes of a record
569 */
570 int ldb_modify(struct ldb_context *ldb, 
571                const struct ldb_message *message)
572 {
573         struct ldb_request *req;
574         int ret;
575
576         ret = ldb_msg_sanity_check(message);
577         if (ret != LDB_SUCCESS) return ret;
578
579         req = talloc(ldb, struct ldb_request);
580         if (req == NULL) {
581                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
582                 return LDB_ERR_OPERATIONS_ERROR;
583         }
584
585         req->operation = LDB_MODIFY;
586         req->op.add.message = message;
587         req->controls = NULL;
588         req->async.context = NULL;
589         req->async.callback = NULL;
590         ldb_set_timeout(ldb, req, 0); /* use default timeout */
591
592         /* do request and autostart a transaction */
593         ret = ldb_autotransaction_request(ldb, req);
594
595         talloc_free(req);
596         return ret;
597 }
598
599
600 /*
601   delete a record from the database
602 */
603 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
604 {
605         struct ldb_request *req;
606         int ret;
607
608         req = talloc(ldb, struct ldb_request);
609         if (req == NULL) {
610                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
611                 return LDB_ERR_OPERATIONS_ERROR;
612         }
613
614         req->operation = LDB_DELETE;
615         req->op.del.dn = dn;
616         req->controls = NULL;
617         req->async.context = NULL;
618         req->async.callback = NULL;
619         ldb_set_timeout(ldb, req, 0); /* use default timeout */
620
621         /* do request and autostart a transaction */
622         ret = ldb_autotransaction_request(ldb, req);
623
624         talloc_free(req);
625         return ret;
626 }
627
628 /*
629   rename a record in the database
630 */
631 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
632 {
633         struct ldb_request *req;
634         int ret;
635
636         req = talloc(ldb, struct ldb_request);
637         if (req == NULL) {
638                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
639                 return LDB_ERR_OPERATIONS_ERROR;
640         }
641
642         req->operation = LDB_RENAME;
643         req->op.rename.olddn = olddn;
644         req->op.rename.newdn = newdn;
645         req->controls = NULL;
646         req->async.context = NULL;
647         req->async.callback = NULL;
648         ldb_set_timeout(ldb, req, 0); /* use default timeout */
649
650         /* do request and autostart a transaction */
651         ret = ldb_autotransaction_request(ldb, req);
652
653         talloc_free(req);
654         return ret;
655 }
656
657
658
659 /*
660   return extended error information 
661 */
662 const char *ldb_errstring(struct ldb_context *ldb)
663 {
664         if (ldb->err_string) {
665                 return ldb->err_string;
666         }
667
668         return NULL;
669 }
670
671 /*
672   return a string explaining what a ldb error constant meancs
673 */
674 const char *ldb_strerror(int ldb_err)
675 {
676         switch (ldb_err) {
677         case LDB_SUCCESS:
678                 return "Success";
679         case LDB_ERR_OPERATIONS_ERROR:
680                 return "Operations error";
681         case LDB_ERR_PROTOCOL_ERROR:
682                 return "Protocol error";
683         case LDB_ERR_TIME_LIMIT_EXCEEDED:
684                 return "Time limit exceeded";
685         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
686                 return "Size limit exceeded";
687         case LDB_ERR_COMPARE_FALSE:
688                 return "Compare false";
689         case LDB_ERR_COMPARE_TRUE:
690                 return "Compare true";
691         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
692                 return "Auth method not supported";
693         case LDB_ERR_STRONG_AUTH_REQUIRED:
694                 return "Strong auth required";
695 /* 9 RESERVED */
696         case LDB_ERR_REFERRAL:
697                 return "Referral error";
698         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
699                 return "Admin limit exceeded";
700         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
701                 return "Unsupported critical extension";
702         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
703                 return "Confidentiality required";
704         case LDB_ERR_SASL_BIND_IN_PROGRESS:
705                 return "SASL bind in progress";
706         case LDB_ERR_NO_SUCH_ATTRIBUTE:
707                 return "No such attribute";
708         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
709                 return "Undefined attribute type";
710         case LDB_ERR_INAPPROPRIATE_MATCHING:
711                 return "Inappropriate matching";
712         case LDB_ERR_CONSTRAINT_VIOLATION:
713                 return "Constraint violation";
714         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
715                 return "Attribute or value exists";
716         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
717                 return "Invalid attribute syntax";
718 /* 22-31 unused */
719         case LDB_ERR_NO_SUCH_OBJECT:
720                 return "No such object";
721         case LDB_ERR_ALIAS_PROBLEM:
722                 return "Alias problem";
723         case LDB_ERR_INVALID_DN_SYNTAX:
724                 return "Invalid DN syntax";
725 /* 35 RESERVED */
726         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
727                 return "Alias dereferencing problem";
728 /* 37-47 unused */
729         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
730                 return "Inappropriate authentication";
731         case LDB_ERR_INVALID_CREDENTIALS:
732                 return "Invalid credentials";
733         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
734                 return "insufficient access rights";
735         case LDB_ERR_BUSY:
736                 return "Busy";
737         case LDB_ERR_UNAVAILABLE:
738                 return "Unavailable";
739         case LDB_ERR_UNWILLING_TO_PERFORM:
740                 return "Unwilling to perform";
741         case LDB_ERR_LOOP_DETECT:
742                 return "Loop detect";
743 /* 55-63 unused */
744         case LDB_ERR_NAMING_VIOLATION:
745                 return "Naming violation";
746         case LDB_ERR_OBJECT_CLASS_VIOLATION:
747                 return "Object class violation";
748         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
749                 return "Not allowed on non-leaf";
750         case LDB_ERR_NOT_ALLOWED_ON_RDN:
751                 return "Not allowed on RDN";
752         case LDB_ERR_ENTRY_ALREADY_EXISTS:
753                 return "Entry already exists";
754         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
755                 return "Object class mods prohibited";
756 /* 70 RESERVED FOR CLDAP */
757         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
758                 return "Affects multiple DSAs";
759 /* 72-79 unused */
760         case LDB_ERR_OTHER:
761                 return "Other";
762         }
763
764         return "Unknown error";
765 }
766
767 /*
768   set backend specific opaque parameters
769 */
770 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
771 {
772         struct ldb_opaque *o;
773
774         /* allow updating an existing value */
775         for (o=ldb->opaque;o;o=o->next) {
776                 if (strcmp(o->name, name) == 0) {
777                         o->value = value;
778                         return LDB_SUCCESS;
779                 }
780         }
781
782         o = talloc(ldb, struct ldb_opaque);
783         if (o == NULL) {
784                 ldb_oom(ldb);
785                 return LDB_ERR_OTHER;
786         }
787         o->next = ldb->opaque;
788         o->name = name;
789         o->value = value;
790         ldb->opaque = o;
791         return LDB_SUCCESS;
792 }
793
794 /*
795   get a previously set opaque value
796 */
797 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
798 {
799         struct ldb_opaque *o;
800         for (o=ldb->opaque;o;o=o->next) {
801                 if (strcmp(o->name, name) == 0) {
802                         return o->value;
803                 }
804         }
805         return NULL;
806 }