make a more recent snapshot of ldb available to interested
[metze/samba/wip.git] / source4 / lib / ldb / common / ldb.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 "includes.h"
36
37 /* 
38  connect to a database. The URL can either be one of the following forms
39    ldb://path
40    ldapi://path
41
42    flags is made up of LDB_FLG_*
43
44    the options are passed uninterpreted to the backend, and are
45    backend specific
46 */
47 struct ldb_context *ldb_connect(const char *url, unsigned int flags,
48                                 const char *options[])
49 {
50
51         if (strncmp(url, "tdb:", 4) == 0) {
52                 return ltdb_connect(url, flags, options);
53         }
54
55         if (strncmp(url, "ldap", 4) == 0) {
56                 return lldb_connect(url, flags, options);
57         }
58
59         errno = EINVAL;
60         return NULL;
61 }
62
63 /*
64   close the connection to the database
65 */
66 int ldb_close(struct ldb_context *ldb)
67 {
68         return ldb->ops->close(ldb);
69 }
70
71
72 /*
73   search the database given a LDAP-like search expression
74
75   return the number of records found, or -1 on error
76 */
77 int ldb_search(struct ldb_context *ldb, 
78                const char *base,
79                enum ldb_scope scope,
80                const char *expression,
81                const char *attrs[], struct ldb_message ***res)
82 {
83         return ldb->ops->search(ldb, base, scope, expression, attrs, res);
84 }
85
86 /* 
87    free a set of messages returned by ldb_search
88 */
89 int ldb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
90 {
91         return ldb->ops->search_free(ldb, msgs);
92 }
93
94
95 /*
96   add a record to the database. Will fail if a record with the given class and key
97   already exists
98 */
99 int ldb_add(struct ldb_context *ldb, 
100             const struct ldb_message *message)
101 {
102         return ldb->ops->add(ldb, message);
103 }
104
105 /*
106   modify the specified attributes of a record
107 */
108 int ldb_modify(struct ldb_context *ldb, 
109                const struct ldb_message *message)
110 {
111         return ldb->ops->modify(ldb, message);
112 }
113
114
115 /*
116   delete a record from the database
117 */
118 int ldb_delete(struct ldb_context *ldb, const char *dn)
119 {
120         return ldb->ops->delete(ldb, dn);
121 }
122
123 /*
124   return extended error information 
125 */
126 const char *ldb_errstring(struct ldb_context *ldb)
127 {
128         return ldb->ops->errstring(ldb);
129 }