4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
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.
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.
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
29 * Component: ldb header
31 * Description: defines for base ldb API
33 * Author: Andrew Tridgell
34 * Author: Stefan Metzmacher
41 major restrictions as compared to normal LDAP:
44 - each record must have a unique key field
45 - the key must be representable as a NULL terminated C string and may not
46 contain a comma or braces
48 major restrictions as compared to tdb:
50 - no explicit locking calls
55 an individual lump of data in a result comes in this format. The
56 pointer will usually be to a UTF-8 string if the application is
57 sensible, but it can be to anything you like, including binary data
58 blobs of arbitrary size.
65 /* these flags are used in ldd_message_element.flags fields. The
66 LDA_FLAGS_MOD_* flags are used in ldap_modify() calls to specify
67 whether attributes are being added, deleted or modified */
68 #define LDB_FLAG_MOD_MASK 0x3
69 #define LDB_FLAG_MOD_ADD 1
70 #define LDB_FLAG_MOD_REPLACE 2
71 #define LDB_FLAG_MOD_DELETE 3
75 results are given back as arrays of ldb_message_element
77 struct ldb_message_element {
80 unsigned int num_values;
81 struct ldb_val *values;
86 a ldb_message represents all or part of a record. It can contain an arbitrary
91 unsigned int num_elements;
92 struct ldb_message_element *elements;
93 void *private_data; /* private to the backend */
97 LDB_CHANGETYPE_NONE=0,
99 LDB_CHANGETYPE_DELETE,
100 LDB_CHANGETYPE_MODIFY
104 a ldif record - from ldif_read
107 enum ldb_changetype changetype;
108 struct ldb_message *msg;
111 enum ldb_scope {LDB_SCOPE_DEFAULT=-1,
113 LDB_SCOPE_ONELEVEL=1,
114 LDB_SCOPE_SUBTREE=2};
119 the fuction type for the callback used in traversing the database
121 typedef int (*ldb_traverse_fn)(struct ldb_context *, const struct ldb_message *);
126 /* debugging uses one of the following levels */
127 enum ldb_debug_level {LDB_DEBUG_FATAL, LDB_DEBUG_ERROR,
128 LDB_DEBUG_WARNING, LDB_DEBUG_TRACE};
131 the user can optionally supply a debug function. The function
132 is based on the vfprintf() style of interface, but with the addition
135 struct ldb_debug_ops {
136 void (*debug)(void *context, enum ldb_debug_level level,
137 const char *fmt, va_list ap);
141 #define LDB_FLG_RDONLY 1
143 #ifndef PRINTF_ATTRIBUTE
144 #define PRINTF_ATTRIBUTE(a,b)
148 /* structues for ldb_parse_tree handling code */
149 enum ldb_parse_op {LDB_OP_SIMPLE=1, LDB_OP_AND='&', LDB_OP_OR='|', LDB_OP_NOT='!'};
151 struct ldb_parse_tree {
152 enum ldb_parse_op operation;
156 struct ldb_val value;
159 unsigned int num_elements;
160 struct ldb_parse_tree **elements;
163 struct ldb_parse_tree *child;
168 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s);
169 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree);
170 char *ldb_binary_encode(void *ctx, struct ldb_val val);
174 connect to a database. The URL can either be one of the following forms
178 flags is made up of LDB_FLG_*
180 the options are passed uninterpreted to the backend, and are
183 struct ldb_context *ldb_connect(const char *url, unsigned int flags,
184 const char *options[]);
187 search the database given a LDAP-like search expression
189 return the number of records found, or -1 on error
191 use talloc_free to free the ldb_message returned
193 int ldb_search(struct ldb_context *ldb,
195 enum ldb_scope scope,
196 const char *expression,
197 const char * const *attrs, struct ldb_message ***res);
200 like ldb_search() but takes a parse tree
202 int ldb_search_bytree(struct ldb_context *ldb,
204 enum ldb_scope scope,
205 struct ldb_parse_tree *tree,
206 const char * const *attrs, struct ldb_message ***res);
209 add a record to the database. Will fail if a record with the given class and key
212 int ldb_add(struct ldb_context *ldb,
213 const struct ldb_message *message);
216 modify the specified attributes of a record
218 int ldb_modify(struct ldb_context *ldb,
219 const struct ldb_message *message);
222 rename a record in the database
224 int ldb_rename(struct ldb_context *ldb, const char *olddn, const char *newdn);
227 delete a record from the database
229 int ldb_delete(struct ldb_context *ldb, const char *dn);
233 return extended error information from the last call
235 const char *ldb_errstring(struct ldb_context *ldb);
238 casefold a string (should be UTF8, but at the moment it isn't)
240 char *ldb_casefold(void *mem_ctx, const char *s);
243 ldif manipulation functions
245 int ldb_ldif_write(struct ldb_context *ldb,
246 int (*fprintf_fn)(void *, const char *, ...),
248 const struct ldb_ldif *ldif);
249 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *);
250 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
251 int (*fgetc_fn)(void *), void *private_data);
252 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f);
253 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s);
254 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg);
257 /* useful functions for ldb_message structure manipulation */
259 int ldb_dn_cmp(const char *dn1, const char *dn2);
260 int ldb_attr_cmp(const char *dn1, const char *dn2);
263 char *ldb_dn_fold(struct ldb_module *module, const char *dn, int (*case_fold_attr_fn)(struct ldb_module * module, char * attr));
265 /* create an empty message */
266 struct ldb_message *ldb_msg_new(void *mem_ctx);
268 /* find an element within an message */
269 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
270 const char *attr_name);
272 /* compare two ldb_val values - return 0 on match */
273 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2);
275 /* find a value within an ldb_message_element */
276 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
277 struct ldb_val *val);
279 /* add a new empty element to a ldb_message */
280 int ldb_msg_add_empty(struct ldb_context *ldb,
281 struct ldb_message *msg, const char *attr_name, int flags);
283 /* add a element to a ldb_message */
284 int ldb_msg_add(struct ldb_context *ldb,
285 struct ldb_message *msg,
286 const struct ldb_message_element *el,
288 int ldb_msg_add_value(struct ldb_context *ldb,
289 struct ldb_message *msg,
290 const char *attr_name,
291 const struct ldb_val *val);
292 int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
293 const char *attr_name, const char *str);
294 int ldb_msg_add_fmt(struct ldb_context *ldb, struct ldb_message *msg,
295 const char *attr_name, const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
297 /* compare two message elements - return 0 on match */
298 int ldb_msg_element_compare(struct ldb_message_element *el1,
299 struct ldb_message_element *el2);
301 /* find elements in a message and convert to a specific type, with
302 a give default value if not found. Assumes that elements are
304 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name);
305 int ldb_msg_find_int(const struct ldb_message *msg,
306 const char *attr_name,
308 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
309 const char *attr_name,
310 unsigned int default_value);
311 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
312 const char *attr_name,
313 int64_t default_value);
314 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
315 const char *attr_name,
316 uint64_t default_value);
317 double ldb_msg_find_double(const struct ldb_message *msg,
318 const char *attr_name,
319 double default_value);
320 const char *ldb_msg_find_string(const struct ldb_message *msg,
321 const char *attr_name,
322 const char *default_value);
324 void ldb_msg_sort_elements(struct ldb_message *msg);
326 void ldb_msg_free(struct ldb_context *ldb, struct ldb_message *msg);
328 struct ldb_message *ldb_msg_copy(struct ldb_context *ldb,
329 const struct ldb_message *msg);
331 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
332 const struct ldb_message *msg);
335 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
336 struct ldb_message *msg1,
337 struct ldb_message *msg2);
339 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v);
342 this allows the user to set a debug function for error reporting
344 int ldb_set_debug(struct ldb_context *ldb,
345 void (*debug)(void *context, enum ldb_debug_level level,
346 const char *fmt, va_list ap),
349 /* this sets up debug to print messages on stderr */
350 int ldb_set_debug_stderr(struct ldb_context *ldb);