4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
6 Copyright (C) Simo Sorce 2005
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Component: ldb header
32 * Description: defines for base ldb API
34 * Author: Andrew Tridgell
35 * Author: Stefan Metzmacher
42 major restrictions as compared to normal LDAP:
45 - each record must have a unique key field
46 - the key must be representable as a NULL terminated C string and may not
47 contain a comma or braces
49 major restrictions as compared to tdb:
51 - no explicit locking calls
52 UPDATE: we have transactions now, better than locking --SSS.
57 an individual lump of data in a result comes in this format. The
58 pointer will usually be to a UTF-8 string if the application is
59 sensible, but it can be to anything you like, including binary data
60 blobs of arbitrary size.
69 /* internal ldb exploded dn structures */
70 struct ldb_dn_component {
76 struct ldb_dn_component *components;
79 /* these flags are used in ldd_message_element.flags fields. The
80 LDA_FLAGS_MOD_* flags are used in ldap_modify() calls to specify
81 whether attributes are being added, deleted or modified */
82 #define LDB_FLAG_MOD_MASK 0x3
83 #define LDB_FLAG_MOD_ADD 1
84 #define LDB_FLAG_MOD_REPLACE 2
85 #define LDB_FLAG_MOD_DELETE 3
91 #define LDB_OID_COMPARATOR_AND "1.2.840.113556.1.4.803"
92 #define LDB_OID_COMPARATOR_OR "1.2.840.113556.1.4.804"
95 results are given back as arrays of ldb_message_element
97 struct ldb_message_element {
100 unsigned int num_values;
101 struct ldb_val *values;
106 a ldb_message represents all or part of a record. It can contain an arbitrary
111 unsigned int num_elements;
112 struct ldb_message_element *elements;
113 void *private_data; /* private to the backend */
116 enum ldb_changetype {
117 LDB_CHANGETYPE_NONE=0,
119 LDB_CHANGETYPE_DELETE,
120 LDB_CHANGETYPE_MODIFY
124 a ldif record - from ldif_read
127 enum ldb_changetype changetype;
128 struct ldb_message *msg;
131 enum ldb_scope {LDB_SCOPE_DEFAULT=-1,
133 LDB_SCOPE_ONELEVEL=1,
134 LDB_SCOPE_SUBTREE=2};
139 the fuction type for the callback used in traversing the database
141 typedef int (*ldb_traverse_fn)(struct ldb_context *, const struct ldb_message *);
146 /* debugging uses one of the following levels */
147 enum ldb_debug_level {LDB_DEBUG_FATAL, LDB_DEBUG_ERROR,
148 LDB_DEBUG_WARNING, LDB_DEBUG_TRACE};
151 the user can optionally supply a debug function. The function
152 is based on the vfprintf() style of interface, but with the addition
155 struct ldb_debug_ops {
156 void (*debug)(void *context, enum ldb_debug_level level,
157 const char *fmt, va_list ap);
161 #define LDB_FLG_RDONLY 1
162 #define LDB_FLG_NOSYNC 2
164 #ifndef PRINTF_ATTRIBUTE
165 #define PRINTF_ATTRIBUTE(a,b)
168 /* structures for ldb_parse_tree handling code */
169 enum ldb_parse_op { LDB_OP_AND=1, LDB_OP_OR=2, LDB_OP_NOT=3,
170 LDB_OP_EQUALITY=4, LDB_OP_SUBSTRING=5,
171 LDB_OP_GREATER=6, LDB_OP_LESS=7, LDB_OP_PRESENT=8,
172 LDB_OP_APPROX=9, LDB_OP_EXTENDED=10 };
174 struct ldb_parse_tree {
175 enum ldb_parse_op operation;
178 struct ldb_parse_tree *child;
182 struct ldb_val value;
186 int start_with_wildcard;
187 int end_with_wildcard;
188 struct ldb_val **chunks;
195 struct ldb_val value;
201 struct ldb_val value;
204 unsigned int num_elements;
205 struct ldb_parse_tree **elements;
210 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s);
211 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree);
212 char *ldb_binary_encode(void *ctx, struct ldb_val val);
216 functions for controlling attribute handling
218 typedef int (*ldb_attr_handler_t)(struct ldb_context *, void *mem_ctx, const struct ldb_val *, struct ldb_val *);
219 typedef int (*ldb_attr_comparison_t)(struct ldb_context *, void *mem_ctx, const struct ldb_val *, const struct ldb_val *);
221 struct ldb_attrib_handler {
224 /* LDB_ATTR_FLAG_* */
227 /* convert from ldif to binary format */
228 ldb_attr_handler_t ldif_read_fn;
230 /* convert from binary to ldif format */
231 ldb_attr_handler_t ldif_write_fn;
233 /* canonicalise a value, for use by indexing and dn construction */
234 ldb_attr_handler_t canonicalise_fn;
236 /* compare two values */
237 ldb_attr_comparison_t comparison_fn;
240 #define LDB_ATTR_FLAG_HIDDEN (1<<0)
242 /* well-known ldap attribute syntaxes - see rfc2252 section 4.3.2 */
243 #define LDB_SYNTAX_DN "1.3.6.1.4.1.1466.115.121.1.12"
244 #define LDB_SYNTAX_DIRECTORY_STRING "1.3.6.1.4.1.1466.115.121.1.15"
245 #define LDB_SYNTAX_INTEGER "1.3.6.1.4.1.1466.115.121.1.27"
246 #define LDB_SYNTAX_OCTET_STRING "1.3.6.1.4.1.1466.115.121.1.40"
247 #define LDB_SYNTAX_OBJECTCLASS "LDB_SYNTAX_OBJECTCLASS"
250 initialise a ldb context
252 struct ldb_context *ldb_init(void *mem_ctx);
255 connect to a database. The URL can either be one of the following forms
259 flags is made up of LDB_FLG_*
261 the options are passed uninterpreted to the backend, and are
264 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]);
267 search the database given a LDAP-like search expression
269 return the number of records found, or -1 on error
271 use talloc_free to free the ldb_message returned
273 int ldb_search(struct ldb_context *ldb,
274 const struct ldb_dn *base,
275 enum ldb_scope scope,
276 const char *expression,
277 const char * const *attrs, struct ldb_message ***res);
280 like ldb_search() but takes a parse tree
282 int ldb_search_bytree(struct ldb_context *ldb,
283 const struct ldb_dn *base,
284 enum ldb_scope scope,
285 struct ldb_parse_tree *tree,
286 const char * const *attrs, struct ldb_message ***res);
289 add a record to the database. Will fail if a record with the given class and key
292 int ldb_add(struct ldb_context *ldb,
293 const struct ldb_message *message);
296 modify the specified attributes of a record
298 int ldb_modify(struct ldb_context *ldb,
299 const struct ldb_message *message);
302 rename a record in the database
304 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn);
307 delete a record from the database
309 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn);
313 return extended error information from the last call
315 const char *ldb_errstring(struct ldb_context *ldb);
318 casefold a string (should be UTF8, but at the moment it isn't)
320 char *ldb_casefold(void *mem_ctx, const char *s);
321 int ldb_caseless_cmp(const char *s1, const char *s2);
324 ldif manipulation functions
326 int ldb_ldif_write(struct ldb_context *ldb,
327 int (*fprintf_fn)(void *, const char *, ...),
329 const struct ldb_ldif *ldif);
330 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *);
331 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
332 int (*fgetc_fn)(void *), void *private_data);
333 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f);
334 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s);
335 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg);
336 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len);
337 int ldb_base64_decode(char *s);
338 int ldb_attrib_add_handlers(struct ldb_context *ldb,
339 const struct ldb_attrib_handler *handlers,
340 unsigned num_handlers);
342 /* The following definitions come from lib/ldb/common/ldb_dn.c */
344 int ldb_dn_is_special(const struct ldb_dn *dn);
345 int ldb_dn_check_special(const struct ldb_dn *dn, const char *check);
346 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value);
347 struct ldb_dn *ldb_dn_new(void *mem_ctx);
348 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn);
349 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn);
350 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, const struct ldb_dn *edn);
351 int ldb_dn_compare_base(struct ldb_context *ldb, const struct ldb_dn *base, const struct ldb_dn *dn);
352 int ldb_dn_compare(struct ldb_context *ldb, const struct ldb_dn *edn0, const struct ldb_dn *edn1);
353 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, const struct ldb_dn *edn);
354 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, const char *dn);
355 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el);
356 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn);
357 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn);
358 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
360 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
362 const struct ldb_dn *base);
363 struct ldb_dn *ldb_dn_make_child(void *mem_ctx,
364 const struct ldb_dn_component *component,
365 const struct ldb_dn *base);
366 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2);
367 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...);
368 struct ldb_dn_component *ldb_dn_get_rdn(void *mem_ctx, const struct ldb_dn *dn);
370 /* useful functions for ldb_message structure manipulation */
371 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn1, const char *dn2);
372 int ldb_attr_cmp(const char *dn1, const char *dn2);
373 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value);
375 /* create an empty message */
376 struct ldb_message *ldb_msg_new(void *mem_ctx);
378 /* find an element within an message */
379 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
380 const char *attr_name);
382 /* compare two ldb_val values - return 0 on match */
383 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2);
385 /* find a value within an ldb_message_element */
386 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
387 struct ldb_val *val);
389 /* add a new empty element to a ldb_message */
390 int ldb_msg_add_empty(struct ldb_context *ldb,
391 struct ldb_message *msg, const char *attr_name, int flags);
393 /* add a element to a ldb_message */
394 int ldb_msg_add(struct ldb_context *ldb,
395 struct ldb_message *msg,
396 const struct ldb_message_element *el,
398 int ldb_msg_add_value(struct ldb_context *ldb,
399 struct ldb_message *msg,
400 const char *attr_name,
401 const struct ldb_val *val);
402 int ldb_msg_add_string(struct ldb_context *ldb, struct ldb_message *msg,
403 const char *attr_name, const char *str);
404 int ldb_msg_add_fmt(struct ldb_context *ldb, struct ldb_message *msg,
405 const char *attr_name, const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
407 /* compare two message elements - return 0 on match */
408 int ldb_msg_element_compare(struct ldb_message_element *el1,
409 struct ldb_message_element *el2);
411 /* find elements in a message and convert to a specific type, with
412 a give default value if not found. Assumes that elements are
414 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name);
415 int ldb_msg_find_int(const struct ldb_message *msg,
416 const char *attr_name,
418 unsigned int ldb_msg_find_uint(const struct ldb_message *msg,
419 const char *attr_name,
420 unsigned int default_value);
421 int64_t ldb_msg_find_int64(const struct ldb_message *msg,
422 const char *attr_name,
423 int64_t default_value);
424 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg,
425 const char *attr_name,
426 uint64_t default_value);
427 double ldb_msg_find_double(const struct ldb_message *msg,
428 const char *attr_name,
429 double default_value);
430 const char *ldb_msg_find_string(const struct ldb_message *msg,
431 const char *attr_name,
432 const char *default_value);
434 void ldb_msg_sort_elements(struct ldb_message *msg);
436 struct ldb_message *ldb_msg_copy(void *mem_ctx,
437 const struct ldb_message *msg);
439 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
440 const struct ldb_message *msg);
443 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
444 struct ldb_message *msg1,
445 struct ldb_message *msg2);
447 int ldb_msg_sanity_check(const struct ldb_message *msg);
449 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v);
452 this allows the user to set a debug function for error reporting
454 int ldb_set_debug(struct ldb_context *ldb,
455 void (*debug)(void *context, enum ldb_debug_level level,
456 const char *fmt, va_list ap),
459 /* this sets up debug to print messages on stderr */
460 int ldb_set_debug_stderr(struct ldb_context *ldb);
462 /* control backend specific opaque values */
463 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value);
464 void *ldb_get_opaque(struct ldb_context *ldb, const char *name);
466 const struct ldb_attrib_handler *ldb_attrib_handler(struct ldb_context *ldb,