r9391: Convert all the code to use struct ldb_dn to ohandle ldap like distinguished...
[sfrench/samba-autobuild/.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 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.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         return ldb;
55 }
56
57 /* 
58  connect to a database. The URL can either be one of the following forms
59    ldb://path
60    ldapi://path
61
62    flags is made up of LDB_FLG_*
63
64    the options are passed uninterpreted to the backend, and are
65    backend specific
66 */
67 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[])
68 {
69         int ret;
70
71         if (strncmp(url, "tdb:", 4) == 0 ||
72             strchr(url, ':') == NULL) {
73                 ret = ltdb_connect(ldb, url, flags, options);
74         }
75
76 #if HAVE_ILDAP
77         else if (strncmp(url, "ldap", 4) == 0) {
78                 ret = ildb_connect(ldb, url, flags, options);
79         }
80 #elif HAVE_LDAP
81         else if (strncmp(url, "ldap", 4) == 0) {
82                 ret = lldb_connect(ldb, url, flags, options);
83         }
84 #endif
85 #if HAVE_SQLITE3
86         else if (strncmp(url, "sqlite:", 7) == 0) {
87                 ret = lsqlite3_connect(ldb, url, flags, options);
88         }
89 #endif
90         else {
91                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to find backend for '%s'\n", url);
92                 return -1;
93         }
94
95         if (ret != 0) {
96                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Failed to connect to '%s'\n", url);
97                 return ret;
98         }
99
100         if (ldb_load_modules(ldb, options) != 0) {
101                 ldb_debug(ldb, LDB_DEBUG_FATAL, "Unable to load modules for '%s'\n", url);
102                 return -1;
103         }
104
105         return 0;
106 }
107
108 /*
109   search the database given a LDAP-like search expression
110
111   return the number of records found, or -1 on error
112
113   Use talloc_free to free the ldb_message returned in 'res'
114
115 */
116 int ldb_search(struct ldb_context *ldb, 
117                const struct ldb_dn *base,
118                enum ldb_scope scope,
119                const char *expression,
120                const char * const *attrs, struct ldb_message ***res)
121 {
122         return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
123 }
124
125 /*
126   search the database given a LDAP-like search expression
127
128   return the number of records found, or -1 on error
129
130   Use talloc_free to free the ldb_message returned in 'res'
131
132 */
133 int ldb_search_bytree(struct ldb_context *ldb, 
134                       const struct ldb_dn *base,
135                       enum ldb_scope scope,
136                       struct ldb_parse_tree *tree,
137                       const char * const *attrs, struct ldb_message ***res)
138 {
139         return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
140 }
141
142 /*
143   add a record to the database. Will fail if a record with the given class and key
144   already exists
145 */
146 int ldb_add(struct ldb_context *ldb, 
147             const struct ldb_message *message)
148 {
149         return ldb->modules->ops->add_record(ldb->modules, message);
150 }
151
152 /*
153   modify the specified attributes of a record
154 */
155 int ldb_modify(struct ldb_context *ldb, 
156                const struct ldb_message *message)
157 {
158         return ldb->modules->ops->modify_record(ldb->modules, message);
159 }
160
161
162 /*
163   delete a record from the database
164 */
165 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn)
166 {
167         return ldb->modules->ops->delete_record(ldb->modules, dn);
168 }
169
170 /*
171   rename a record in the database
172 */
173 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
174 {
175         return ldb->modules->ops->rename_record(ldb->modules, olddn, newdn);
176 }
177
178 /*
179   create a named lock
180 */
181 int ldb_lock(struct ldb_context *ldb, const char *lockname)
182 {
183         return ldb->modules->ops->named_lock(ldb->modules, lockname);
184 }
185
186 /*
187   release a named lock
188 */
189 int ldb_unlock(struct ldb_context *ldb, const char *lockname)
190 {
191         return ldb->modules->ops->named_unlock(ldb->modules, lockname);
192 }
193
194 /*
195   return extended error information 
196 */
197 const char *ldb_errstring(struct ldb_context *ldb)
198 {
199         if (ldb->modules == NULL) {
200                 return "ldb not connected";
201         }
202         return ldb->modules->ops->errstring(ldb->modules);
203 }
204
205
206 /*
207   set backend specific opaque parameters
208 */
209 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value)
210 {
211         struct ldb_opaque *o = talloc(ldb, struct ldb_opaque);
212         if (o == NULL) {
213                 ldb_oom(ldb);
214                 return -1;
215         }
216         o->next = ldb->opaque;
217         o->name = name;
218         o->value = value;
219         ldb->opaque = o;
220         return 0;
221 }
222
223 /*
224   get a previously set opaque value
225 */
226 void *ldb_get_opaque(struct ldb_context *ldb, const char *name)
227 {
228         struct ldb_opaque *o;
229         for (o=ldb->opaque;o;o=o->next) {
230                 if (strcmp(o->name, name) == 0) {
231                         return o->value;
232                 }
233         }
234         return NULL;
235 }