r16036: Add a couple of new functions to corretly deal with timeouts.
[kamenim/samba.git] / source4 / lib / ldb / include / ldb.h
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5    Copyright (C) Stefan Metzmacher  2004
6    Copyright (C) Simo Sorce  2005
7
8      ** NOTE! The following LGPL license applies to the ldb
9      ** library. This does NOT imply that all of Samba is released
10      ** under the LGPL
11    
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.
16
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.
21
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
25 */
26
27 /*
28  *  Name: ldb
29  *
30  *  Component: ldb header
31  *
32  *  Description: defines for base ldb API
33  *
34  *  Author: Andrew Tridgell
35  *  Author: Stefan Metzmacher
36  */
37
38 /**
39    \file ldb.h Samba's ldb database
40
41    This header file provides the main API for ldb.
42 */
43
44 #ifndef _LDB_H_
45
46 /*! \cond DOXYGEN_IGNORE */
47 #define _LDB_H_ 1
48 /*! \endcond */
49
50 /*
51   major restrictions as compared to normal LDAP:
52
53      - no async calls.
54      - each record must have a unique key field
55      - the key must be representable as a NULL terminated C string and may not 
56        contain a comma or braces
57
58   major restrictions as compared to tdb:
59
60      - no explicit locking calls
61      UPDATE: we have transactions now, better than locking --SSS.
62
63 */
64
65 #ifndef ldb_val
66 /**
67    Result value
68
69    An individual lump of data in a result comes in this format. The
70    pointer will usually be to a UTF-8 string if the application is
71    sensible, but it can be to anything you like, including binary data
72    blobs of arbitrary size.
73
74    \note the data is null (0x00) terminated, but the length does not
75    include the terminator. 
76 */
77 struct ldb_val {
78         uint8_t *data; /*!< result data */
79         size_t length; /*!< length of data */
80 };
81 #endif
82
83 /**
84    internal ldb exploded dn structures
85 */
86 struct ldb_dn_component {
87         char *name;  
88         struct ldb_val value;
89 };
90
91 struct ldb_dn {
92         int comp_num;
93         struct ldb_dn_component *components;
94 };
95
96 /**
97  There are a number of flags that are used with ldap_modify() in
98  ldb_message_element.flags fields. The LDA_FLAGS_MOD_ADD,
99  LDA_FLAGS_MOD_DELETE and LDA_FLAGS_MOD_REPLACE flags are used in
100  ldap_modify() calls to specify whether attributes are being added,
101  deleted or modified respectively.
102 */
103 #define LDB_FLAG_MOD_MASK  0x3
104
105 /**
106    Flag value used in ldap_modify() to indicate that attributes are
107    being added.
108
109    \sa LDB_FLAG_MOD_MASK
110 */
111 #define LDB_FLAG_MOD_ADD     1
112
113 /**
114    Flag value used in ldap_modify() to indicate that attributes are
115    being replaced.
116
117    \sa LDB_FLAG_MOD_MASK
118 */
119 #define LDB_FLAG_MOD_REPLACE 2
120
121 /**
122    Flag value used in ldap_modify() to indicate that attributes are
123    being deleted.
124
125    \sa LDB_FLAG_MOD_MASK
126 */
127 #define LDB_FLAG_MOD_DELETE  3
128
129 /**
130   OID for logic AND comaprison.
131
132   This is the well known object ID for a logical AND comparitor.
133 */
134 #define LDB_OID_COMPARATOR_AND  "1.2.840.113556.1.4.803"
135
136 /**
137   OID for logic OR comparison.
138
139   This is the well known object ID for a logical OR comparitor.
140 */
141 #define LDB_OID_COMPARATOR_OR   "1.2.840.113556.1.4.804"
142
143 /**
144   results are given back as arrays of ldb_message_element
145 */
146 struct ldb_message_element {
147         unsigned int flags;
148         const char *name;
149         unsigned int num_values;
150         struct ldb_val *values;
151 };
152
153
154 /**
155   a ldb_message represents all or part of a record. It can contain an arbitrary
156   number of elements. 
157 */
158 struct ldb_message {
159         struct ldb_dn *dn;
160         unsigned int num_elements;
161         struct ldb_message_element *elements;
162         void *private_data; /* private to the backend */
163 };
164
165 enum ldb_changetype {
166         LDB_CHANGETYPE_NONE=0,
167         LDB_CHANGETYPE_ADD,
168         LDB_CHANGETYPE_DELETE,
169         LDB_CHANGETYPE_MODIFY
170 };
171
172 /**
173   LDIF record
174
175   This structure contains a LDIF record, as returned from ldif_read()
176   and equivalent functions.
177 */
178 struct ldb_ldif {
179         enum ldb_changetype changetype; /*!< The type of change */
180         struct ldb_message *msg;  /*!< The changes */
181 };
182
183 enum ldb_scope {LDB_SCOPE_DEFAULT=-1, 
184                 LDB_SCOPE_BASE=0, 
185                 LDB_SCOPE_ONELEVEL=1,
186                 LDB_SCOPE_SUBTREE=2};
187
188 struct ldb_context;
189
190 /*
191   the fuction type for the callback used in traversing the database
192 */
193 typedef int (*ldb_traverse_fn)(struct ldb_context *, const struct ldb_message *);
194
195
196 /* debugging uses one of the following levels */
197 enum ldb_debug_level {LDB_DEBUG_FATAL, LDB_DEBUG_ERROR, 
198                       LDB_DEBUG_WARNING, LDB_DEBUG_TRACE};
199
200 /**
201   the user can optionally supply a debug function. The function
202   is based on the vfprintf() style of interface, but with the addition
203   of a severity level
204 */
205 struct ldb_debug_ops {
206         void (*debug)(void *context, enum ldb_debug_level level, 
207                       const char *fmt, va_list ap);
208         void *context;
209 };
210
211 /**
212   The user can optionally supply a custom utf8 functions,
213   to handle comparisons and casefolding.
214 */
215 struct ldb_utf8_fns {
216         void *context;
217         char *(*casefold)(void *context, void *mem_ctx, const char *s);
218 };
219
220 /**
221    Flag value for database connection mode.
222
223    If LDB_FLG_RDONLY is used in ldb_connect, then the database will be
224    opened read-only, if possible.
225 */
226 #define LDB_FLG_RDONLY 1
227
228 /**
229    Flag value for database connection mode.
230
231    If LDB_FLG_NOSYNC is used in ldb_connect, then the database will be
232    opened without synchronous operations, if possible.
233 */
234 #define LDB_FLG_NOSYNC 2
235
236 /**
237    Flag value to specify autoreconnect mode.
238
239    If LDB_FLG_RECONNECT is used in ldb_connect, then the backend will
240    be opened in a way that makes it try to auto reconnect if the
241    connection is dropped (actually make sense only with ldap).
242 */
243 #define LDB_FLG_RECONNECT 4
244
245 /*! \cond DOXYGEN_IGNORE */
246 #ifndef PRINTF_ATTRIBUTE
247 #define PRINTF_ATTRIBUTE(a,b)
248 #endif
249 /*! \endcond */
250
251 /*
252    structures for ldb_parse_tree handling code
253 */
254 enum ldb_parse_op { LDB_OP_AND=1, LDB_OP_OR=2, LDB_OP_NOT=3,
255                     LDB_OP_EQUALITY=4, LDB_OP_SUBSTRING=5,
256                     LDB_OP_GREATER=6, LDB_OP_LESS=7, LDB_OP_PRESENT=8,
257                     LDB_OP_APPROX=9, LDB_OP_EXTENDED=10 };
258
259 struct ldb_parse_tree {
260         enum ldb_parse_op operation;
261         union {
262                 struct {
263                         struct ldb_parse_tree *child;
264                 } isnot;
265                 struct {
266                         const char *attr;
267                         struct ldb_val value;
268                 } equality;
269                 struct {
270                         const char *attr;
271                         int start_with_wildcard;
272                         int end_with_wildcard;
273                         struct ldb_val **chunks;
274                 } substring;
275                 struct {
276                         const char *attr;
277                 } present;
278                 struct {
279                         const char *attr;
280                         struct ldb_val value;
281                 } comparison;
282                 struct {
283                         const char *attr;
284                         int dnAttributes;
285                         char *rule_id;
286                         struct ldb_val value;
287                 } extended;
288                 struct {
289                         unsigned int num_elements;
290                         struct ldb_parse_tree **elements;
291                 } list;
292         } u;
293 };
294
295 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s);
296 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree);
297
298 /**
299    Encode a binary blob
300
301    This function encodes a binary blob using the encoding rules in RFC
302    2254 (Section 4). This function also escapes any non-printable
303    characters.
304
305    \param ctx the memory context to allocate the return string in.
306    \param val the (potentially) binary data to be encoded
307
308    \return the encoded data as a null terminated string
309
310    \sa <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>.
311 */
312 char *ldb_binary_encode(void *ctx, struct ldb_val val);
313
314 /**
315    Encode a string
316
317    This function encodes a string using the encoding rules in RFC 2254
318    (Section 4). This function also escapes any non-printable
319    characters.
320
321    \param mem_ctx the memory context to allocate the return string in.
322    \param string the string to be encoded
323
324    \return the encoded data as a null terminated string
325
326    \sa <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>.
327 */
328 char *ldb_binary_encode_string(void *mem_ctx, const char *string);
329
330 /*
331   functions for controlling attribute handling
332 */
333 typedef int (*ldb_attr_handler_t)(struct ldb_context *, void *mem_ctx, const struct ldb_val *, struct ldb_val *);
334 typedef int (*ldb_attr_comparison_t)(struct ldb_context *, void *mem_ctx, const struct ldb_val *, const struct ldb_val *);
335
336 struct ldb_attrib_handler {
337         const char *attr;
338
339         /* LDB_ATTR_FLAG_* */
340         unsigned flags;
341
342         /* convert from ldif to binary format */
343         ldb_attr_handler_t ldif_read_fn;
344
345         /* convert from binary to ldif format */
346         ldb_attr_handler_t ldif_write_fn;
347         
348         /* canonicalise a value, for use by indexing and dn construction */
349         ldb_attr_handler_t canonicalise_fn;
350
351         /* compare two values */
352         ldb_attr_comparison_t comparison_fn;
353 };
354
355 /**
356    The attribute is not returned by default
357 */
358 #define LDB_ATTR_FLAG_HIDDEN       (1<<0) 
359
360 /**
361    The attribute is constructed from other attributes
362 */
363 #define LDB_ATTR_FLAG_CONSTRUCTED  (1<<1) 
364
365 /**
366   LDAP attribute syntax for a DN
367
368   This is the well-known LDAP attribute syntax for a DN.
369
370   See <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>, Section 4.3.2 
371 */
372 #define LDB_SYNTAX_DN                   "1.3.6.1.4.1.1466.115.121.1.12"
373
374 /**
375   LDAP attribute syntax for a Directory String
376
377   This is the well-known LDAP attribute syntax for a Directory String.
378
379   \sa <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>, Section 4.3.2 
380 */
381 #define LDB_SYNTAX_DIRECTORY_STRING     "1.3.6.1.4.1.1466.115.121.1.15"
382
383 /**
384   LDAP attribute syntax for an integer
385
386   This is the well-known LDAP attribute syntax for an integer.
387
388   See <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>, Section 4.3.2 
389 */
390 #define LDB_SYNTAX_INTEGER              "1.3.6.1.4.1.1466.115.121.1.27"
391
392 /**
393   LDAP attribute syntax for an octet string
394
395   This is the well-known LDAP attribute syntax for an octet string.
396
397   See <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>, Section 4.3.2 
398 */
399 #define LDB_SYNTAX_OCTET_STRING         "1.3.6.1.4.1.1466.115.121.1.40"
400
401 /**
402   LDAP attribute syntax for UTC time.
403
404   This is the well-known LDAP attribute syntax for a UTC time.
405
406   See <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>, Section 4.3.2 
407 */
408 #define LDB_SYNTAX_UTC_TIME             "1.3.6.1.4.1.1466.115.121.1.53"
409
410 #define LDB_SYNTAX_OBJECTCLASS          "LDB_SYNTAX_OBJECTCLASS"
411
412 /* sorting helpers */
413 typedef int (*ldb_qsort_cmp_fn_t) (void *v1, void *v2, void *opaque);
414
415 /**
416    OID for the paged results control. This control is included in the
417    searchRequest and searchResultDone messages as part of the controls
418    field of the LDAPMessage, as defined in Section 4.1.12 of
419    LDAP v3. 
420
421    \sa <a href="http://www.ietf.org/rfc/rfc2696.txt">RFC 2696</a>.
422 */
423 #define LDB_CONTROL_PAGED_RESULTS_OID   "1.2.840.113556.1.4.319"
424
425 /**
426    OID for notification
427
428    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_notification_oid.asp">Microsoft documentation of this OID</a>
429 */
430 #define LDB_CONTROL_NOTIFICATION_OID    "1.2.840.113556.1.4.528"
431
432 /**
433    OID for extended DN
434
435    \sa <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/ldap_server_extended_dn_oid.asp">Microsoft documentation of this OID</a>
436 */
437 #define LDB_CONTROL_EXTENDED_DN_OID     "1.2.840.113556.1.4.529"
438
439 /**
440    OID for LDAP server sort result extension.
441
442    This control is included in the searchRequest message as part of
443    the controls field of the LDAPMessage, as defined in Section 4.1.12
444    of LDAP v3. The controlType is set to
445    "1.2.840.113556.1.4.473". The criticality MAY be either TRUE or
446    FALSE (where absent is also equivalent to FALSE) at the client's
447    option.
448
449    \sa <a href="http://www.ietf.org/rfc/rfc2891.txt">RFC 2891</a>.
450 */
451 #define LDB_CONTROL_SERVER_SORT_OID     "1.2.840.113556.1.4.473"
452
453 /**
454    OID for LDAP server sort result response extension.
455
456    This control is included in the searchResultDone message as part of
457    the controls field of the LDAPMessage, as defined in Section 4.1.12 of
458    LDAP v3.
459
460    \sa <a href="http://www.ietf.org/rfc/rfc2891.txt">RFC 2891</a>.
461 */
462 #define LDB_CONTROL_SORT_RESP_OID       "1.2.840.113556.1.4.474"
463
464 /**
465    OID for LDAP Attribute Scoped Query extension.
466
467    This control is included in SearchRequest or SearchResponse
468    messages as part of the controls field of the LDAPMessage.
469 */
470 #define LDB_CONTROL_ASQ_OID             "1.2.840.113556.1.4.1504"
471
472 /**
473    OID for LDAP Directory Sync extension. 
474
475    This control is included in SearchRequest or SearchResponse
476    messages as part of the controls field of the LDAPMessage.
477 */
478 #define LDB_CONTROL_DIRSYNC_OID         "1.2.840.113556.1.4.841"
479
480
481 /**
482    OID for LDAP Virtual List View Request extension.
483
484    This control is included in SearchRequest messages
485    as part of the controls field of the LDAPMessage.
486 */
487 #define LDB_CONTROL_VLV_REQ_OID         "2.16.840.1.113730.3.4.9"
488
489 /**
490    OID for LDAP Virtual List View Response extension.
491
492    This control is included in SearchResponse messages
493    as part of the controls field of the LDAPMessage.
494 */
495 #define LDB_CONTROL_VLV_RESP_OID        "2.16.840.1.113730.3.4.10"
496
497 struct ldb_paged_control {
498         int size;
499         int cookie_len;
500         char *cookie;
501 };
502
503 struct ldb_extended_dn_control {
504         int type;
505 };
506
507 struct ldb_server_sort_control {
508         char *attributeName;
509         char *orderingRule;
510         int reverse;
511 };
512
513 struct ldb_sort_resp_control {
514         int result;
515         char *attr_desc;
516 };
517
518 struct ldb_asq_control {
519         int request;
520         char *source_attribute;
521         int src_attr_len;
522         int result;
523 };
524
525 struct ldb_dirsync_control {
526         int flags;
527         int max_attributes;
528         int cookie_len;
529         char *cookie;
530 };
531
532 struct ldb_vlv_req_control {
533         int beforeCount;
534         int afterCount;
535         int type;
536         union {
537                 struct {
538                         int offset;
539                         int contentCount;
540                 } byOffset;
541                 struct {
542                         int value_len;
543                         char *value;
544                 } gtOrEq;
545         } match;
546         int ctxid_len;
547         char *contextId;
548 };
549
550 struct ldb_vlv_resp_control {
551         int targetPosition;
552         int contentCount;
553         int vlv_result;
554         int ctxid_len;
555         char *contextId;
556 };
557
558 struct ldb_control {
559         const char *oid;
560         int critical;
561         void *data;
562 };
563
564 enum ldb_request_type {
565         LDB_SEARCH,
566         LDB_ADD,
567         LDB_MODIFY,
568         LDB_DELETE,
569         LDB_RENAME,
570
571         LDB_REQ_REGISTER
572 };
573
574 enum ldb_reply_type {
575         LDB_REPLY_ENTRY,
576         LDB_REPLY_REFERRAL,
577         LDB_REPLY_DONE
578 };
579
580 enum ldb_async_wait_type {
581         LDB_WAIT_ALL,
582         LDB_WAIT_NONE
583 };
584
585 enum ldb_async_state {
586         LDB_ASYNC_INIT,
587         LDB_ASYNC_PENDING,
588         LDB_ASYNC_DONE
589 };
590
591 struct ldb_result {
592         unsigned int count;
593         struct ldb_message **msgs;
594         char **refs;
595         struct ldb_control **controls;
596 };
597
598 struct ldb_async_result {
599         enum ldb_reply_type type;
600         struct ldb_message *message;
601         char *referral;
602         struct ldb_control **controls;
603 };
604
605 struct ldb_async_handle {
606         int status;
607         enum ldb_async_state state;
608         void *private_data;
609         struct ldb_module *module;
610 };
611
612 struct ldb_search {
613         const struct ldb_dn *base;
614         enum ldb_scope scope;
615         const struct ldb_parse_tree *tree;
616         const char * const *attrs;
617         struct ldb_result *res;
618 };
619
620 struct ldb_add {
621         const struct ldb_message *message;
622 };
623
624 struct  ldb_modify {
625         const struct ldb_message *message;
626 };
627
628 struct ldb_delete {
629         const struct ldb_dn *dn;
630 };
631
632 struct ldb_rename {
633         const struct ldb_dn *olddn;
634         const struct ldb_dn *newdn;
635 };
636
637 struct ldb_register_control {
638         const char *oid;
639 };
640
641 struct ldb_request {
642
643         enum ldb_request_type operation;
644
645         union {
646                 struct ldb_search search;
647                 struct ldb_add    add;
648                 struct ldb_modify mod;
649                 struct ldb_delete del;
650                 struct ldb_rename rename;
651                 struct ldb_register_control reg;
652         } op;
653
654         struct ldb_control **controls;
655
656         struct {
657                 void *context;
658                 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
659
660                 int timeout;
661                 time_t starttime;
662                 struct ldb_async_handle *handle;
663         } async;
664 };
665
666 int ldb_request(struct ldb_context *ldb, struct ldb_request *request);
667
668 int ldb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type);
669
670 int ldb_set_timeout(struct ldb_context *ldb, struct ldb_request *req, int timeout);
671 int ldb_set_timeout_from_prev_req(struct ldb_context *ldb, struct ldb_request *oldreq, struct ldb_request *newreq);
672
673 /**
674   Initialise ldbs' global information
675
676   This is required before any other LDB call
677
678   \return 0 if initialisation succeeded, -1 otherwise
679 */
680 int ldb_global_init(void);
681
682 /**
683   Initialise an ldb context
684
685   This is required before any other LDB call.
686
687   \param mem_ctx pointer to a talloc memory context. Pass NULL if there is
688   no suitable context available.
689
690   \return pointer to ldb_context that should be free'd (using talloc_free())
691   at the end of the program.
692 */
693 struct ldb_context *ldb_init(void *mem_ctx);
694
695 /**
696    Connect to a database.
697
698    This is typically called soon after ldb_init(), and is required prior to
699    any search or database modification operations.
700
701    The URL can be one of the following forms:
702     - tdb://path
703     - ldapi://path
704     - ldap://host
705     - sqlite://path
706
707    \param ldb the context associated with the database (from ldb_init())
708    \param url the URL of the database to connect to, as noted above
709    \param flags a combination of LDB_FLG_* to modify the connection behaviour
710    \param options backend specific options - passed uninterpreted to the backend
711
712    \return result code (LDB_SUCCESS on success, or a failure code)
713
714    \note It is an error to connect to a database that does not exist in readonly mode
715    (that is, with LDB_FLG_RDONLY). However in read-write mode, the database will be
716    created if it does not exist.
717 */
718 int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]);
719
720 /**
721   Search the database
722
723   This function searches the database, and returns 
724   records that match an LDAP-like search expression
725
726   \param ldb the context associated with the database (from ldb_init())
727   \param base the Base Distinguished Name for the query (pass NULL for root DN)
728   \param scope the search scope for the query
729   \param expression the search expression to use for this query
730   \param attrs the search attributes for the query (pass NULL if none required)
731   \param res the return result
732
733   \return result code (LDB_SUCCESS on success, or a failure code)
734
735   \note use talloc_free() to free the ldb_result returned
736 */
737 int ldb_search(struct ldb_context *ldb, 
738                const struct ldb_dn *base,
739                enum ldb_scope scope,
740                const char *expression,
741                const char * const *attrs, struct ldb_result **res);
742
743 /*
744   like ldb_search() but takes a parse tree
745 */
746 int ldb_search_bytree(struct ldb_context *ldb, 
747                       const struct ldb_dn *base,
748                       enum ldb_scope scope,
749                       struct ldb_parse_tree *tree,
750                       const char * const *attrs, struct ldb_result **res);
751
752 /**
753   Add a record to the database.
754
755   This function adds a record to the database. This function will fail
756   if a record with the specified class and key already exists in the
757   database. 
758
759   \param ldb the context associated with the database (from
760   ldb_init())
761   \param message the message containing the record to add.
762
763   \return result code (LDB_SUCCESS if the record was added, otherwise
764   a failure code)
765 */
766 int ldb_add(struct ldb_context *ldb, 
767             const struct ldb_message *message);
768
769 /**
770   Modify the specified attributes of a record
771
772   This function modifies a record that is in the database.
773
774   \param ldb the context associated with the database (from
775   ldb_init())
776   \param message the message containing the changes required.
777
778   \return result code (LDB_SUCCESS if the record was modified as
779   requested, otherwise a failure code)
780 */
781 int ldb_modify(struct ldb_context *ldb, 
782                const struct ldb_message *message);
783
784 /**
785   Rename a record in the database
786
787   This function renames a record in the database.
788
789   \param ldb the context associated with the database (from
790   ldb_init())
791   \param olddn the DN for the record to be renamed.
792   \param newdn the new DN 
793
794   \return result code (LDB_SUCCESS if the record was renamed as
795   requested, otherwise a failure code)
796 */
797 int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn);
798
799 /**
800   Delete a record from the database
801
802   This function deletes a record from the database.
803
804   \param ldb the context associated with the database (from
805   ldb_init())
806   \param dn the DN for the record to be deleted.
807
808   \return result code (LDB_SUCCESS if the record was deleted,
809   otherwise a failure code)
810 */
811 int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn);
812
813 /**
814   start a transaction
815 */
816 int ldb_transaction_start(struct ldb_context *ldb);
817
818 /**
819   commit a transaction
820 */
821 int ldb_transaction_commit(struct ldb_context *ldb);
822
823 /**
824   cancel a transaction
825 */
826 int ldb_transaction_cancel(struct ldb_context *ldb);
827
828
829 /**
830   return extended error information from the last call
831 */
832 const char *ldb_errstring(struct ldb_context *ldb);
833
834 /**
835   return a string explaining what a ldb error constant meancs
836 */
837 const char *ldb_strerror(int ldb_err);
838
839 /**
840   setup the default utf8 functions
841   FIXME: these functions do not yet handle utf8
842 */
843 void ldb_set_utf8_default(struct ldb_context *ldb);
844
845 /**
846    Casefold a string
847
848    \param ldb the ldb context
849    \param mem_ctx the memory context to allocate the result string
850    memory from. 
851    \param s the string that is to be folded
852    \return a copy of the string, converted to upper case
853
854    \note The default function is not yet UTF8 aware. Provide your own
855          set of functions through ldb_set_utf8_fns()
856 */
857 char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s);
858
859 /**
860    Check the attribute name is valid according to rfc2251
861    \param s tthe string to check
862
863    \return 1 if the name is ok
864 */
865 int ldb_valid_attr_name(const char *s);
866
867 /*
868   ldif manipulation functions
869 */
870 /**
871    Write an LDIF message
872
873    This function writes an LDIF message using a caller supplied  write
874    function.
875
876    \param ldb the ldb context (from ldb_init())
877    \param fprintf_fn a function pointer for the write function. This must take
878    a private data pointer, followed by a format string, and then a variable argument
879    list. 
880    \param private_data pointer that will be provided back to the write
881    function. This is useful for maintaining state or context.
882    \param ldif the message to write out
883
884    \return the total number of bytes written, or an error code as returned
885    from the write function.
886
887    \sa ldb_ldif_write_file for a more convenient way to write to a
888    file stream.
889
890    \sa ldb_ldif_read for the reader equivalent to this function.
891 */
892 int ldb_ldif_write(struct ldb_context *ldb,
893                    int (*fprintf_fn)(void *, const char *, ...), 
894                    void *private_data,
895                    const struct ldb_ldif *ldif);
896
897 /**
898    Clean up an LDIF message
899
900    This function cleans up a LDIF message read using ldb_ldif_read()
901    or related functions (such as ldb_ldif_read_string() and
902    ldb_ldif_read_file().
903
904    \param ldb the ldb context (from ldb_init())
905    \param msg the message to clean up and free
906
907 */
908 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *msg);
909
910 /**
911    Read an LDIF message
912
913    This function creates an LDIF message using a caller supplied read
914    function. 
915
916    \param ldb the ldb context (from ldb_init())
917    \param fgetc_fn a function pointer for the read function. This must
918    take a private data pointer, and must return a pointer to an
919    integer corresponding to the next byte read (or EOF if there is no
920    more data to be read).
921    \param private_data pointer that will be provided back to the read
922    function. This is udeful for maintaining state or context.
923
924    \return the LDIF message that has been read in
925
926    \note You must free the LDIF message when no longer required, using
927    ldb_ldif_read_free().
928
929    \sa ldb_ldif_read_file for a more convenient way to read from a
930    file stream.
931
932    \sa ldb_ldif_read_string for a more convenient way to read from a
933    string (char array).
934
935    \sa ldb_ldif_write for the writer equivalent to this function.
936 */
937 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb, 
938                                int (*fgetc_fn)(void *), void *private_data);
939
940 /**
941    Read an LDIF message from a file
942
943    This function reads the next LDIF message from the contents of a
944    file stream. If you want to get all of the LDIF messages, you will
945    need to repeatedly call this function, until it returns NULL.
946
947    \param ldb the ldb context (from ldb_init())
948    \param f the file stream to read from (typically from fdopen())
949
950    \sa ldb_ldif_read_string for an equivalent function that will read
951    from a string (char array).
952
953    \sa ldb_ldif_write_file for the writer equivalent to this function.
954
955 */
956 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f);
957
958 /**
959    Read an LDIF message from a string
960
961    This function reads the next LDIF message from the contents of a char
962    array. If you want to get all of the LDIF messages, you will need
963    to repeatedly call this function, until it returns NULL.
964
965    \param ldb the ldb context (from ldb_init())
966    \param s pointer to the char array to read from
967
968    \sa ldb_ldif_read_file for an equivalent function that will read
969    from a file stream.
970
971    \sa ldb_ldif_write for a more general (arbitrary read function)
972    version of this function.
973 */
974 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s);
975
976 /**
977    Write an LDIF message to a file
978
979    \param ldb the ldb context (from ldb_init())
980    \param f the file stream to write to (typically from fdopen())
981    \param msg the message to write out
982
983    \return the total number of bytes written, or a negative error code
984
985    \sa ldb_ldif_read_file for the reader equivalent to this function.
986 */
987 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg);
988
989 /**
990    Base64 encode a buffer
991
992    \param mem_ctx the memory context that the result is allocated
993    from. 
994    \param buf pointer to the array that is to be encoded
995    \param len the number of elements in the array to be encoded
996
997    \return pointer to an array containing the encoded data
998
999    \note The caller is responsible for freeing the result
1000 */
1001 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len);
1002
1003 /**
1004    Base64 decode a buffer
1005
1006    This function decodes a base64 encoded string in place.
1007
1008    \param s the string to decode.
1009
1010    \return the length of the returned (decoded) string.
1011
1012    \note the string is null terminated, but the null terminator is not
1013    included in the length.
1014 */
1015 int ldb_base64_decode(char *s);
1016
1017 int ldb_attrib_add_handlers(struct ldb_context *ldb, 
1018                             const struct ldb_attrib_handler *handlers, 
1019                             unsigned num_handlers);
1020
1021 /* The following definitions come from lib/ldb/common/ldb_dn.c  */
1022
1023 int ldb_dn_is_special(const struct ldb_dn *dn);
1024 int ldb_dn_check_special(const struct ldb_dn *dn, const char *check);
1025 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value);
1026 struct ldb_dn *ldb_dn_new(void *mem_ctx);
1027 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn);
1028 struct ldb_dn *ldb_dn_explode_or_special(void *mem_ctx, const char *dn);
1029 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn);
1030 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, const struct ldb_dn *edn);
1031 int ldb_dn_compare_base(struct ldb_context *ldb, const struct ldb_dn *base, const struct ldb_dn *dn);
1032 int ldb_dn_compare(struct ldb_context *ldb, const struct ldb_dn *edn0, const struct ldb_dn *edn1);
1033 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, const struct ldb_dn *edn);
1034 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, const char *dn);
1035 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el);
1036 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn);
1037 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn);
1038 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
1039                                                                const char *val);
1040 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
1041                                                  const char * value,
1042                                                  const struct ldb_dn *base);
1043 struct ldb_dn *ldb_dn_make_child(void *mem_ctx,
1044                                  const struct ldb_dn_component *component,
1045                                  const struct ldb_dn *base);
1046 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2);
1047 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...) PRINTF_ATTRIBUTE(3,4);
1048 struct ldb_dn_component *ldb_dn_get_rdn(void *mem_ctx, const struct ldb_dn *dn);
1049
1050 /* useful functions for ldb_message structure manipulation */
1051 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn1, const char *dn2);
1052
1053 /**
1054    Compare two attributes
1055
1056    This function compares to attribute names. Note that this is a
1057    case-insensitive comparison.
1058
1059    \param attr1 the first attribute name to compare
1060    \param attr2 the second attribute name to compare
1061
1062    \return 0 if the attribute names are the same, or only differ in
1063    case; non-zero if there are any differences
1064 */
1065 int ldb_attr_cmp(const char *attr1, const char *attr2);
1066 char *ldb_attr_casefold(void *mem_ctx, const char *s);
1067 int ldb_attr_dn(const char *attr);
1068
1069 /**
1070    Create an empty message
1071
1072    \param mem_ctx the memory context to create in. You can pass NULL
1073    to get the top level context, however the ldb context (from
1074    ldb_init()) may be a better choice
1075 */
1076 struct ldb_message *ldb_msg_new(void *mem_ctx);
1077
1078 /**
1079    Find an element within an message
1080 */
1081 struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg, 
1082                                                  const char *attr_name);
1083
1084 /**
1085    Compare two ldb_val values
1086
1087    \param v1 first ldb_val structure to be tested
1088    \param v2 second ldb_val structure to be tested
1089
1090    \return 1 for a match, 0 if there is any difference
1091 */
1092 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2);
1093
1094 /**
1095    find a value within an ldb_message_element
1096
1097    \param el the element to search
1098    \param val the value to search for
1099
1100    \note This search is case sensitive
1101 */
1102 struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el, 
1103                                  struct ldb_val *val);
1104
1105 /**
1106    add a new empty element to a ldb_message
1107 */
1108 int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags);
1109
1110 /**
1111    add a element to a ldb_message
1112 */
1113 int ldb_msg_add(struct ldb_message *msg, 
1114                 const struct ldb_message_element *el, 
1115                 int flags);
1116 int ldb_msg_add_value(struct ldb_message *msg, 
1117                       const char *attr_name,
1118                       const struct ldb_val *val);
1119 int ldb_msg_add_steal_value(struct ldb_message *msg, 
1120                       const char *attr_name,
1121                       struct ldb_val *val);
1122 int ldb_msg_add_steal_string(struct ldb_message *msg, 
1123                              const char *attr_name, char *str);
1124 int ldb_msg_add_string(struct ldb_message *msg, 
1125                        const char *attr_name, const char *str);
1126 int ldb_msg_add_fmt(struct ldb_message *msg, 
1127                     const char *attr_name, const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
1128
1129 /**
1130    compare two message elements - return 0 on match
1131 */
1132 int ldb_msg_element_compare(struct ldb_message_element *el1, 
1133                             struct ldb_message_element *el2);
1134
1135 /**
1136    Find elements in a message.
1137
1138    This function finds elements and converts to a specific type, with
1139    a give default value if not found. Assumes that elements are
1140    single valued.
1141 */
1142 const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name);
1143 int ldb_msg_find_int(const struct ldb_message *msg, 
1144                      const char *attr_name,
1145                      int default_value);
1146 unsigned int ldb_msg_find_uint(const struct ldb_message *msg, 
1147                                const char *attr_name,
1148                                unsigned int default_value);
1149 int64_t ldb_msg_find_int64(const struct ldb_message *msg, 
1150                            const char *attr_name,
1151                            int64_t default_value);
1152 uint64_t ldb_msg_find_uint64(const struct ldb_message *msg, 
1153                              const char *attr_name,
1154                              uint64_t default_value);
1155 double ldb_msg_find_double(const struct ldb_message *msg, 
1156                            const char *attr_name,
1157                            double default_value);
1158 const char *ldb_msg_find_string(const struct ldb_message *msg, 
1159                                 const char *attr_name,
1160                                 const char *default_value);
1161
1162 void ldb_msg_sort_elements(struct ldb_message *msg);
1163
1164 struct ldb_message *ldb_msg_copy_shallow(void *mem_ctx, 
1165                                          const struct ldb_message *msg);
1166 struct ldb_message *ldb_msg_copy(void *mem_ctx, 
1167                                  const struct ldb_message *msg);
1168
1169 struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 
1170                                          const struct ldb_message *msg);
1171
1172
1173 struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, 
1174                                  struct ldb_message *msg1,
1175                                  struct ldb_message *msg2);
1176
1177 int ldb_msg_check_string_attribute(const struct ldb_message *msg,
1178                                    const char *name,
1179                                    const char *value);
1180
1181 /**
1182    Integrity check an ldb_message
1183
1184    This function performs basic sanity / integrity checks on an
1185    ldb_message.
1186
1187    \param msg the message to check
1188
1189    \return LDB_SUCCESS if the message is OK, or a non-zero error code
1190    (one of LDB_ERR_INVALID_DN_SYNTAX, LDB_ERR_ENTRY_ALREADY_EXISTS or
1191    LDB_ERR_INVALID_ATTRIBUTE_SYNTAX) if there is a problem with a
1192    message.
1193 */
1194 int ldb_msg_sanity_check(const struct ldb_message *msg);
1195
1196 /**
1197    Duplicate an ldb_val structure
1198
1199    This function copies an ldb value structure. 
1200
1201    \param mem_ctx the memory context that the duplicated value will be
1202    allocated from
1203    \param v the ldb_val to be duplicated.
1204
1205    \return the duplicated ldb_val structure.
1206 */
1207 struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v);
1208
1209 /**
1210   this allows the user to set a debug function for error reporting
1211 */
1212 int ldb_set_debug(struct ldb_context *ldb,
1213                   void (*debug)(void *context, enum ldb_debug_level level, 
1214                                 const char *fmt, va_list ap),
1215                   void *context);
1216
1217 /**
1218   this allows the user to set custom utf8 function for error reporting
1219 */
1220 void ldb_set_utf8_fns(struct ldb_context *ldb,
1221                         void *context,
1222                         char *(*casefold)(void *, void *, const char *));
1223
1224 /**
1225    this sets up debug to print messages on stderr
1226 */
1227 int ldb_set_debug_stderr(struct ldb_context *ldb);
1228
1229 /* control backend specific opaque values */
1230 int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value);
1231 void *ldb_get_opaque(struct ldb_context *ldb, const char *name);
1232
1233 const struct ldb_attrib_handler *ldb_attrib_handler(struct ldb_context *ldb,
1234                                                     const char *attrib);
1235
1236
1237 const char **ldb_attr_list_copy(void *mem_ctx, const char * const *attrs);
1238 int ldb_attr_in_list(const char * const *attrs, const char *attr);
1239
1240
1241 void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree, 
1242                                  const char *attr, 
1243                                  const char *replace);
1244
1245 int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace);
1246 int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace);
1247 void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr);
1248
1249 /**
1250    Convert a time structure to a string
1251
1252    This function converts a time_t structure to an LDAP formatted time
1253    string.
1254
1255    \param mem_ctx the memory context to allocate the return string in
1256    \param t the time structure to convert
1257
1258    \return the formatted string, or NULL if the time structure could
1259    not be converted
1260 */
1261 char *ldb_timestring(void *mem_ctx, time_t t);
1262
1263 /**
1264    Convert a string to a time structure
1265
1266    This function converts an LDAP formatted time string to a time_t
1267    structure.
1268
1269    \param s the string to convert
1270
1271    \return the time structure, or 0 if the string cannot be converted
1272 */
1273 time_t ldb_string_to_time(const char *s);
1274
1275 char *ldb_dn_canonical_string(void *mem_ctx, const struct ldb_dn *dn);
1276 char *ldb_dn_canonical_ex_string(void *mem_ctx, const struct ldb_dn *dn);
1277
1278
1279 void ldb_qsort (void *const pbase, size_t total_elems, size_t size, void *opaque, ldb_qsort_cmp_fn_t cmp);
1280 #endif