r574: - another attempt at const cleanliness in ldb
[samba.git] / source / lib / ldb / include / ldb.h
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 header
29  *
30  *  Description: defines for base ldb API
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #ifndef _LDB_H_
36 #define _LDB_H_ 1
37
38 /*
39   major restrictions as compared to normal LDAP:
40
41      - no async calls.
42      - each record must have a unique key field
43      - the key must be representable as a NULL terminated C string and may not 
44        contain a comma or braces
45
46   major restrictions as compared to tdb:
47
48      - no explicit locking calls
49
50 */
51
52 /*
53   an individual lump of data in a result comes in this format. The
54   pointer will usually be to a UTF-8 string if the application is
55   sensible, but it can be to anything you like, including binary data
56   blobs of arbitrary size.
57 */
58 struct ldb_val {
59         unsigned int length;
60         void *data;
61 };
62
63 #include "ldb_parse.h"
64
65
66 /* these flags are used in ldd_message_element.flags fields. The
67    LDA_FLAGS_MOD_* flags are used in ldap_modify() calls to specify
68    whether attributes are being added, deleted or modified */
69 #define LDB_FLAG_MOD_MASK  0x3
70 #define LDB_FLAG_MOD_ADD     1
71 #define LDB_FLAG_MOD_REPLACE 2
72 #define LDB_FLAG_MOD_DELETE  3
73
74
75 /*
76   results are given back as arrays of ldb_message_element
77 */
78 struct ldb_message_element {
79         unsigned int flags;
80         char *name;
81         unsigned int num_values;
82         struct ldb_val *values;
83 };
84
85
86 /*
87   a ldb_message represents all or part of a record. It can contain an arbitrary
88   number of elements. 
89 */
90 struct ldb_message {
91         char *dn;
92         unsigned int num_elements;
93         struct ldb_message_element *elements;
94         void *private_data; /* private to the backend */
95 };
96
97 enum ldb_changetype {
98         LDB_CHANGETYPE_NONE=0,
99         LDB_CHANGETYPE_ADD,
100         LDB_CHANGETYPE_DELETE,
101         LDB_CHANGETYPE_MODIFY
102 };
103
104 /*
105   a ldif record - from ldif_read
106 */
107 struct ldb_ldif {
108         enum ldb_changetype changetype;
109         struct ldb_message msg;
110 };
111
112 enum ldb_scope {LDB_SCOPE_DEFAULT=-1, 
113                 LDB_SCOPE_BASE=0, 
114                 LDB_SCOPE_ONELEVEL=1,
115                 LDB_SCOPE_SUBTREE=2};
116
117 struct ldb_context;
118
119 /*
120   the fuction type for the callback used in traversing the database
121 */
122 typedef int (*ldb_traverse_fn)(struct ldb_context *, const struct ldb_message *);
123
124
125 /* 
126    these function pointers define the operations that a ldb backend must perform
127    they correspond exactly to the ldb_*() interface 
128 */
129 struct ldb_backend_ops {
130         int (*close)(struct ldb_context *);
131         int (*search)(struct ldb_context *, const char *, enum ldb_scope,
132                       const char *, const char * const [], struct ldb_message ***);
133         int (*search_free)(struct ldb_context *, struct ldb_message **);
134         int (*add_record)(struct ldb_context *, const struct ldb_message *);
135         int (*modify_record)(struct ldb_context *, const struct ldb_message *);
136         int (*delete_record)(struct ldb_context *, const char *);
137         const char * (*errstring)(struct ldb_context *);
138
139         /* this is called when the alloc ops changes to ensure we 
140            don't have any old allocated data in the context */
141         void (*cache_free)(struct ldb_context *);
142 };
143
144
145 /*
146   the user can optionally supply a allocator function. It is presumed
147   it will act like a modern realloc(), with a context ptr to allow
148   for pool allocators
149 */
150 struct ldb_alloc_ops {
151         void *(*alloc)(void *context, void *ptr, size_t size);
152         void *context;
153 };
154
155 /* debugging uses one of the following levels */
156 enum ldb_debug_level {LDB_DEBUG_FATAL, LDB_DEBUG_ERROR, 
157                       LDB_DEBUG_WARNING, LDB_DEBUG_TRACE};
158
159 /*
160   the user can optionally supply a debug function. The function
161   is based on the vfprintf() style of interface, but with the addition
162   of a severity level
163 */
164 struct ldb_debug_ops {
165         void (*debug)(void *context, enum ldb_debug_level level, 
166                       const char *fmt, va_list ap);
167         void *context;
168 };
169
170
171 /*
172   every ldb connection is started by establishing a ldb_context
173 */
174 struct ldb_context {
175         /* a private pointer for the backend to use */
176         void *private_data;
177
178         /* the operations provided by the backend */
179         const struct ldb_backend_ops *ops;
180
181         /* memory allocation info */
182         struct ldb_alloc_ops alloc_ops;
183
184         /* memory allocation info */
185         struct ldb_debug_ops debug_ops;
186 };
187
188
189 #define LDB_FLG_RDONLY 1
190
191 /* 
192  connect to a database. The URL can either be one of the following forms
193    ldb://path
194    ldapi://path
195
196    flags is made up of LDB_FLG_*
197
198    the options are passed uninterpreted to the backend, and are
199    backend specific
200 */
201 struct ldb_context *ldb_connect(const char *url, unsigned int flags,
202                                 const char *options[]);
203
204 /*
205   close the connection to the database
206 */
207 int ldb_close(struct ldb_context *ldb);
208
209
210 /*
211   search the database given a LDAP-like search expression
212
213   return the number of records found, or -1 on error
214 */
215 int ldb_search(struct ldb_context *ldb, 
216                const char *base,
217                enum ldb_scope scope,
218                const char *expression,
219                const char * const *attrs, struct ldb_message ***res);
220
221 /* 
222    free a set of messages returned by ldb_search
223 */
224 int ldb_search_free(struct ldb_context *ldb, struct ldb_message **msgs);
225
226
227 /*
228   add a record to the database. Will fail if a record with the given class and key
229   already exists
230 */
231 int ldb_add(struct ldb_context *ldb, 
232             const struct ldb_message *message);
233
234 /*
235   modify the specified attributes of a record
236 */
237 int ldb_modify(struct ldb_context *ldb, 
238                const struct ldb_message *message);
239
240 /*
241   delete a record from the database
242 */
243 int ldb_delete(struct ldb_context *ldb, const char *dn);
244
245
246 /*
247   return extended error information from the last call
248 */
249 const char *ldb_errstring(struct ldb_context *ldb);
250
251 /*
252   casefold a string (should be UTF8, but at the moment it isn't)
253 */
254 char *ldb_casefold(struct ldb_context *ldb, const char *s);
255
256 /*
257   ldif manipulation functions
258 */
259 int ldif_write(struct ldb_context *ldb,
260                int (*fprintf_fn)(void *, const char *, ...), 
261                void *private_data,
262                const struct ldb_ldif *ldif);
263 void ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *);
264 struct ldb_ldif *ldif_read(struct ldb_context *ldb, 
265                            int (*fgetc_fn)(void *), void *private_data);
266 struct ldb_ldif *ldif_read_file(struct ldb_context *ldb, FILE *f);
267 struct ldb_ldif *ldif_read_string(struct ldb_context *ldb, const char *s);
268 int ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg);
269
270
271 /* useful functions for ldb_message structure manipulation */
272
273 /* find an element within an message */
274 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, 
275                                                  const char *attr_name);
276
277 /* compare two ldb_val values - return 0 on match */
278 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2);
279
280 /* find a value within an ldb_message_element */
281 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, 
282                                  struct ldb_val *val);
283
284 /* add a new empty element to a ldb_message */
285 int ldb_msg_add_empty(struct ldb_context *ldb,
286                       struct ldb_message *msg, const char *attr_name, int flags);
287
288 /* add a element to a ldb_message */
289 int ldb_msg_add(struct ldb_context *ldb, 
290                 struct ldb_message *msg, 
291                 const struct ldb_message_element *el, 
292                 int flags);
293
294 /* compare two message elements - return 0 on match */
295 int ldb_msg_element_compare(struct ldb_message_element *el1, 
296                             struct ldb_message_element *el2);
297
298 /* find elements in a message and convert to a specific type, with
299    a give default value if not found. Assumes that elements are
300    single valued */
301 int ldb_msg_find_int(const struct ldb_message *msg, 
302                      const char *attr_name,
303                      int default_value);
304 unsigned int ldb_msg_find_uint(const struct ldb_message *msg, 
305                                const char *attr_name,
306                                int default_value);
307 double ldb_msg_find_double(const struct ldb_message *msg, 
308                            const char *attr_name,
309                            double default_value);
310 const char *ldb_msg_find_string(const struct ldb_message *msg, 
311                                 const char *attr_name,
312                                 const char *default_value);
313
314
315 /*
316   this allows the user to choose their own allocation function
317   the allocation function should behave like a modern realloc() 
318   function, which means that:
319      malloc(size)       == alloc(context, NULL, size)
320      free(ptr)          == alloc(context, ptr, 0)
321      realloc(ptr, size) == alloc(context, ptr, size)
322   The context argument is provided to allow for pool based allocators,
323   which often take a context argument
324 */
325 int ldb_set_alloc(struct ldb_context *ldb,
326                   void *(*alloc)(void *context, void *ptr, size_t size),
327                   void *context);
328
329 /*
330   this allows the user to set a debug function for error reporting
331 */
332 int ldb_set_debug(struct ldb_context *ldb,
333                   void (*debug)(void *context, enum ldb_debug_level level, 
334                                 const char *fmt, va_list ap),
335                   void *context);
336
337 /* this sets up debug to print messages on stderr */
338 int ldb_set_debug_stderr(struct ldb_context *ldb);
339
340
341 /* these are used as type safe versions of the ldb allocation functions */
342 #define ldb_malloc_p(ldb, type) (type *)ldb_malloc(ldb, sizeof(type))
343 #define ldb_malloc_array_p(ldb, type, count) (type *)ldb_realloc_array(ldb, NULL, sizeof(type), count)
344 #define ldb_realloc_p(ldb, p, type, count) (type *)ldb_realloc_array(ldb, p, sizeof(type), count)
345
346 #endif