r15944: rename LDB_ASYNC_ADD -> LDB_ADD, LDB_ASYNC_MODIFY -> LDB_MODIFY, etc...
[kai/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-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         return LDB_SUCCESS;
144 }
145
146 void ldb_set_errstring(struct ldb_context *ldb, char *err_string)
147 {
148         if (ldb->err_string) {
149                 talloc_free(ldb->err_string);
150         }
151         ldb->err_string = talloc_steal(ldb, err_string);
152 }
153
154 void ldb_reset_err_string(struct ldb_context *ldb)
155 {
156         if (ldb->err_string) {
157                 talloc_free(ldb->err_string);
158                 ldb->err_string = NULL;
159         }
160 }
161
162 #define FIRST_OP(ldb, op) do { \
163         module = ldb->modules; \
164         while (module && module->ops->op == NULL) module = module->next; \
165         if (module == NULL) return LDB_ERR_OPERATIONS_ERROR; \
166 } while (0)
167
168 /*
169   start a transaction
170 */
171 int ldb_transaction_start(struct ldb_context *ldb)
172 {
173         struct ldb_module *module;
174         int status;
175         FIRST_OP(ldb, start_transaction);
176         
177         ldb->transaction_active++;
178
179         ldb_reset_err_string(ldb);
180
181         status = module->ops->start_transaction(module);
182         if (status != LDB_SUCCESS) {
183                 if (ldb->err_string == NULL) {
184                         /* no error string was setup by the backend */
185                         ldb_set_errstring(ldb, 
186                                           talloc_asprintf(ldb, "ldb transaction start error %d", status));
187                 }
188         }
189         return status;
190 }
191
192 /*
193   commit a transaction
194 */
195 int ldb_transaction_commit(struct ldb_context *ldb)
196 {
197         struct ldb_module *module;
198         int status;
199         FIRST_OP(ldb, end_transaction);
200
201         if (ldb->transaction_active > 0) {
202                 ldb->transaction_active--;
203         } else {
204                 return LDB_ERR_OPERATIONS_ERROR;
205         }
206
207         ldb_reset_err_string(ldb);
208
209         status = module->ops->end_transaction(module);
210         if (status != LDB_SUCCESS) {
211                 if (ldb->err_string == NULL) {
212                         /* no error string was setup by the backend */
213                         ldb_set_errstring(ldb, 
214                                           talloc_asprintf(ldb, "ldb transaction commit error %d", status));
215                 }
216         }
217         return status;
218 }
219
220 /*
221   cancel a transaction
222 */
223 int ldb_transaction_cancel(struct ldb_context *ldb)
224 {
225         struct ldb_module *module;
226         int status;
227         FIRST_OP(ldb, del_transaction);
228
229         if (ldb->transaction_active > 0) {
230                 ldb->transaction_active--;
231         } else {
232                 return LDB_ERR_OPERATIONS_ERROR;
233         }
234
235         status = module->ops->del_transaction(module);
236         if (status != LDB_SUCCESS) {
237                 if (ldb->err_string == NULL) {
238                         /* no error string was setup by the backend */
239                         ldb_set_errstring(ldb, 
240                                           talloc_asprintf(ldb, "ldb transaction cancel error %d", status));
241                 }
242         }
243         return status;
244 }
245
246 int ldb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
247 {
248         if (!handle) {
249                 return LDB_SUCCESS;
250         }
251
252         return handle->module->ops->async_wait(handle, type);
253 }
254
255
256 /*
257   check for an error return from an op 
258   if an op fails, but has not setup an error string, then setup one now
259 */
260 static int ldb_op_finish(struct ldb_context *ldb, int status)
261 {
262         if (status == LDB_SUCCESS) {
263                 return ldb_transaction_commit(ldb);
264         }
265         if (ldb->err_string == NULL) {
266                 /* no error string was setup by the backend */
267                 ldb_set_errstring(ldb, 
268                                   talloc_asprintf(ldb, "ldb error %d", status));
269         }
270         ldb_transaction_cancel(ldb);
271         return status;
272 }
273
274 /*
275   start an ldb request
276   NOTE: the request must be a talloc context.
277   returns LDB_ERR_* on errors.
278 */
279 int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
280 {
281         struct ldb_module *module;
282         int ret;
283
284         ldb_reset_err_string(ldb);
285
286         /* call the first module in the chain */
287         switch (req->operation) {
288         case LDB_SEARCH:
289                 FIRST_OP(ldb, search);
290                 ret = module->ops->search(module, req);
291                 break;
292         case LDB_ADD:
293                 FIRST_OP(ldb, add);
294                 ret = module->ops->add(module, req);
295                 break;
296         case LDB_MODIFY:
297                 FIRST_OP(ldb, modify);
298                 ret = module->ops->modify(module, req);
299                 break;
300         case LDB_DELETE:
301                 FIRST_OP(ldb, del);
302                 ret = module->ops->del(module, req);
303                 break;
304         case LDB_RENAME:
305                 FIRST_OP(ldb, rename);
306                 ret = module->ops->rename(module, req);
307                 break;
308         default:
309                 FIRST_OP(ldb, request);
310                 ret = module->ops->request(module, req);
311                 break;
312         }
313
314         return ret;
315 }
316
317 /*
318   search the database given a LDAP-like search expression
319
320   returns an LDB error code
321
322   Use talloc_free to free the ldb_message returned in 'res', if successful
323
324 */
325 static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
326 {
327         struct ldb_result *res;
328         int n;
329         
330         if (!context) {
331                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context in callback"));
332                 return LDB_ERR_OPERATIONS_ERROR;
333         }       
334
335         res = *((struct ldb_result **)context);
336
337         if (!res || !ares) {
338                 goto error;
339         }
340
341         if (ares->type == LDB_REPLY_ENTRY) {
342                 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
343                 if (! res->msgs) {
344                         goto error;
345                 }
346
347                 res->msgs[res->count + 1] = NULL;
348
349                 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
350                 if (! res->msgs[res->count]) {
351                         goto error;
352                 }
353
354                 res->count++;
355         }
356
357         if (ares->type == LDB_REPLY_REFERRAL) {
358                 if (res->refs) {
359                         for (n = 0; res->refs[n]; n++) /*noop*/ ;
360                 } else {
361                         n = 0;
362                 }
363
364                 res->refs = talloc_realloc(res, res->refs, char *, n + 2);
365                 if (! res->refs) {
366                         goto error;
367                 }
368
369                 res->refs[n] = talloc_steal(res->refs, ares->referral);
370                 res->refs[n + 1] = NULL;
371         }
372
373         if (ares->controls) {
374                 res->controls = talloc_steal(res, ares->controls);
375                 if (! res->controls) {
376                         goto error;
377                 }
378         }
379
380         talloc_free(ares);
381         return LDB_SUCCESS;
382
383 error:
384         talloc_free(ares);
385         talloc_free(res);
386         *((struct ldb_result **)context) = NULL;
387         return LDB_ERR_OPERATIONS_ERROR;
388 }
389
390 int ldb_search(struct ldb_context *ldb, 
391                const struct ldb_dn *base,
392                enum ldb_scope scope,
393                const char *expression,
394                const char * const *attrs, 
395                struct ldb_result **res)
396 {
397         struct ldb_request *req;
398         int ret;
399
400         *res = talloc_zero(ldb, struct ldb_result);
401         if (! *res) {
402                 return LDB_ERR_OPERATIONS_ERROR;
403         }
404
405         req = talloc(ldb, struct ldb_request);
406         if (req == NULL) {
407                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
408                 return LDB_ERR_OPERATIONS_ERROR;
409         }
410
411         req->operation = LDB_SEARCH;
412         req->op.search.base = base;
413         req->op.search.scope = scope;
414
415         req->op.search.tree = ldb_parse_tree(req, expression);
416         if (req->op.search.tree == NULL) {
417                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Unable to parse search expression"));
418                 talloc_free(req);
419                 return LDB_ERR_OPERATIONS_ERROR;
420         }
421
422         req->op.search.attrs = attrs;
423         req->controls = NULL;
424         req->async.context = res;
425         req->async.callback = ldb_search_callback;
426         req->async.timeout = 600; /* 10 minutes */
427
428         ret = ldb_request(ldb, req);
429         
430         if (ret == LDB_SUCCESS) {
431                 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
432         }
433         
434         if (ret != LDB_SUCCESS) {
435                 talloc_free(*res);
436                 *res = NULL;
437         }
438
439         talloc_free(req);
440         return ret;
441 }
442
443 /* autostarts a transacion if none active */
444 static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req)
445 {
446         int ret, close_transaction;
447
448         close_transaction = 0;
449         if (!ldb->transaction_active) {
450                 ret = ldb_transaction_start(ldb);
451                 if (ret != LDB_SUCCESS) {
452                         return ret;
453                 }
454                 close_transaction = 1;
455         }
456
457         ret = ldb_request(ldb, req);
458         if (ret == LDB_SUCCESS) {
459                 ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL);
460         }
461
462         if (close_transaction) {
463                 return ldb_op_finish(ldb, ret);
464         }
465
466         return ret;
467 }
468
469
470 /*
471   add a record to the database. Will fail if a record with the given class and key
472   already exists
473 */
474 int ldb_add(struct ldb_context *ldb, 
475             const struct ldb_message *message)
476 {
477         struct ldb_request *req;
478         int ret;
479
480         ret = ldb_msg_sanity_check(message);
481         if (ret != LDB_SUCCESS) return ret;
482
483         req = talloc(ldb, struct ldb_request);
484         if (req == NULL) {
485                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
486                 return LDB_ERR_OPERATIONS_ERROR;
487         }
488
489         req->operation = LDB_ADD;
490         req->op.add.message = message;
491         req->controls = NULL;
492         req->async.context = NULL;
493         req->async.callback = NULL;
494         req->async.timeout = 600; /* 10 minutes */
495
496         /* do request and autostart a transaction */
497         ret = ldb_autotransaction_request(ldb, req);
498
499         talloc_free(req);
500         return ret;
501 }
502
503 /*
504   modify the specified attributes of a record
505 */
506 int ldb_modify(struct ldb_context *ldb, 
507                const struct ldb_message *message)
508 {
509         struct ldb_request *req;
510         int ret;
511
512         ret = ldb_msg_sanity_check(message);
513         if (ret != LDB_SUCCESS) return ret;
514
515         req = talloc(ldb, struct ldb_request);
516         if (req == NULL) {
517                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
518                 return LDB_ERR_OPERATIONS_ERROR;
519         }
520
521         req->operation = LDB_MODIFY;
522         req->op.add.message = message;
523         req->controls = NULL;
524         req->async.context = NULL;
525         req->async.callback = NULL;
526         req->async.timeout = 600; /* 10 minutes */
527
528         /* do request and autostart a transaction */
529         ret = ldb_autotransaction_request(ldb, req);
530
531         talloc_free(req);
532         return ret;
533 }
534
535
536 /*
537   delete a record from the database
538 */
539 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
540 {
541         struct ldb_request *req;
542         int ret;
543
544         req = talloc(ldb, struct ldb_request);
545         if (req == NULL) {
546                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
547                 return LDB_ERR_OPERATIONS_ERROR;
548         }
549
550         req->operation = LDB_DELETE;
551         req->op.del.dn = dn;
552         req->controls = NULL;
553         req->async.context = NULL;
554         req->async.callback = NULL;
555         req->async.timeout = 600; /* 10 minutes */
556
557         /* do request and autostart a transaction */
558         ret = ldb_autotransaction_request(ldb, req);
559
560         talloc_free(req);
561         return ret;
562 }
563
564 /*
565   rename a record in the database
566 */
567 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
568 {
569         struct ldb_request *req;
570         int ret;
571
572         req = talloc(ldb, struct ldb_request);
573         if (req == NULL) {
574                 ldb_set_errstring(ldb, talloc_strdup(ldb, "Out of memory!"));
575                 return LDB_ERR_OPERATIONS_ERROR;
576         }
577
578         req->operation = LDB_RENAME;
579         req->op.rename.olddn = olddn;
580         req->op.rename.newdn = newdn;
581         req->controls = NULL;
582         req->async.context = NULL;
583         req->async.callback = NULL;
584         req->async.timeout = 600; /* 10 minutes */
585
586         /* do request and autostart a transaction */
587         ret = ldb_autotransaction_request(ldb, req);
588
589         talloc_free(req);
590         return ret;
591 }
592
593
594
595 /*
596   return extended error information 
597 */
598 const char *ldb_errstring(struct ldb_context *ldb)
599 {
600         if (ldb->err_string) {
601                 return ldb->err_string;
602         }
603
604         return NULL;
605 }
606
607 /*
608   return a string explaining what a ldb error constant meancs
609 */
610 const char *ldb_strerror(int ldb_err)
611 {
612         switch (ldb_err) {
613         case LDB_SUCCESS:
614                 return "Success";
615         case LDB_ERR_OPERATIONS_ERROR:
616                 return "Operations error";
617         case LDB_ERR_PROTOCOL_ERROR:
618                 return "Protocol error";
619         case LDB_ERR_TIME_LIMIT_EXCEEDED:
620                 return "Time limit exceeded";
621         case LDB_ERR_SIZE_LIMIT_EXCEEDED:
622                 return "Size limit exceeded";
623         case LDB_ERR_COMPARE_FALSE:
624                 return "Compare false";
625         case LDB_ERR_COMPARE_TRUE:
626                 return "Compare true";
627         case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
628                 return "Auth method not supported";
629         case LDB_ERR_STRONG_AUTH_REQUIRED:
630                 return "Strong auth required";
631 /* 9 RESERVED */
632         case LDB_ERR_REFERRAL:
633                 return "Referral error";
634         case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
635                 return "Admin limit exceeded";
636         case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
637                 return "Unsupported critical extension";
638         case LDB_ERR_CONFIDENTIALITY_REQUIRED:
639                 return "Confidentiality required";
640         case LDB_ERR_SASL_BIND_IN_PROGRESS:
641                 return "SASL bind in progress";
642         case LDB_ERR_NO_SUCH_ATTRIBUTE:
643                 return "No such attribute";
644         case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
645                 return "Undefined attribute type";
646         case LDB_ERR_INAPPROPRIATE_MATCHING:
647                 return "Inappropriate matching";
648         case LDB_ERR_CONSTRAINT_VIOLATION:
649                 return "Constraint violation";
650         case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
651                 return "Attribute or value exists";
652         case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
653                 return "Invalid attribute syntax";
654 /* 22-31 unused */
655         case LDB_ERR_NO_SUCH_OBJECT:
656                 return "No such object";
657         case LDB_ERR_ALIAS_PROBLEM:
658                 return "Alias problem";
659         case LDB_ERR_INVALID_DN_SYNTAX:
660                 return "Invalid DN syntax";
661 /* 35 RESERVED */
662         case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
663                 return "Alias dereferencing problem";
664 /* 37-47 unused */
665         case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
666                 return "Inappropriate authentication";
667         case LDB_ERR_INVALID_CREDENTIALS:
668                 return "Invalid credentials";
669         case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
670                 return "insufficient access rights";
671         case LDB_ERR_BUSY:
672                 return "Busy";
673         case LDB_ERR_UNAVAILABLE:
674                 return "Unavailable";
675         case LDB_ERR_UNWILLING_TO_PERFORM:
676                 return "Unwilling to perform";
677         case LDB_ERR_LOOP_DETECT:
678                 return "Loop detect";
679 /* 55-63 unused */
680         case LDB_ERR_NAMING_VIOLATION:
681                 return "Naming violation";
682         case LDB_ERR_OBJECT_CLASS_VIOLATION:
683                 return "Object class violation";
684         case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
685                 return "Not allowed on non-leaf";
686         case LDB_ERR_NOT_ALLOWED_ON_RDN:
687                 return "Not allowed on RDN";
688         case LDB_ERR_ENTRY_ALREADY_EXISTS:
689                 return "Entry already exists";
690         case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
691                 return "Object class mods prohibited";
692 /* 70 RESERVED FOR CLDAP */
693         case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
694                 return "Affects multiple DSAs";
695 /* 72-79 unused */
696         case LDB_ERR_OTHER:
697                 return "Other";
698         }
699
700         return "Unknown error";
701 }
702
703 /*
704   set backend specific opaque parameters
705 */
706 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
707 {
708         struct ldb_opaque *o;
709
710         /* allow updating an existing value */
711         for (o=ldb->opaque;o;o=o->next) {
712                 if (strcmp(o->name, name) == 0) {
713                         o->value = value;
714                         return LDB_SUCCESS;
715                 }
716         }
717
718         o = talloc(ldb, struct ldb_opaque);
719         if (o == NULL) {
720                 ldb_oom(ldb);
721                 return LDB_ERR_OTHER;
722         }
723         o->next = ldb->opaque;
724         o->name = name;
725         o->value = value;
726         ldb->opaque = o;
727         return LDB_SUCCESS;
728 }
729
730 /*
731   get a previously set opaque value
732 */
733 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
734 {
735         struct ldb_opaque *o;
736         for (o=ldb->opaque;o;o=o->next) {
737                 if (strcmp(o->name, name) == 0) {
738                         return o->value;
739                 }
740         }
741         return NULL;
742 }