]> git.sur5r.net Git - openldap/blob - servers/slapd/slap.h
f96e38d787ea2945a8f5cfe086b120cb999832c6
[openldap] / servers / slapd / slap.h
1 /* slap.h - stand alone ldap server include file */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #ifndef _SLDAPD_H_
9 #define _SLDAPD_H_
10
11 #define SLAPD_SCHEMA_COMPAT 1
12
13 #include "ldap_defaults.h"
14
15 #include <ac/stdlib.h>
16
17 #include <sys/types.h>
18 #include <ac/syslog.h>
19 #include <ac/regex.h>
20 #include <ac/socket.h>
21 #include <ac/time.h>
22 #include <ac/param.h>
23
24 #ifdef HAVE_CYRUS_SASL
25 #include <sasl.h>
26 #endif
27
28 #include "avl.h"
29
30 #ifndef ldap_debug
31 #define ldap_debug slap_debug
32 #endif
33
34
35 #include "ldap_log.h"
36
37 #include <ldap.h>
38 #include <ldap_schema.h>
39
40 #include "ldap_pvt_thread.h"
41 #include "ldif.h"
42
43 LDAP_BEGIN_DECL
44
45 #ifdef f_next
46 #undef f_next /* name conflict between sys/file.h on SCO and struct filter */
47 #endif
48
49 #define SERVICE_NAME  OPENLDAP_PACKAGE "-slapd"
50
51 /* LDAPMod.mod_op value ===> Must be kept in sync with ldap.h!
52  *
53  * This is a value used internally by the backends. It is needed to allow
54  * adding values that already exist without getting an error as required by
55  * modrdn when the new rdn was already an attribute value itself.
56  * JCG 05/1999 (gomez@engr.sgi.com)
57  */
58 #define LDAP_MOD_SOFTADD        0x1000
59
60 #define ON      1
61 #define OFF     (-1)
62 #define UNDEFINED 0
63
64 #define MAXREMATCHES 10
65
66 /* psuedo error code to indicating abandoned operation */
67 #define SLAPD_ABANDON -1
68
69 /* XXYYZ: these macros assume 'x' is an ASCII x */
70 #define DNSEPARATOR(c)  ((c) == ',' || (c) == ';')
71 #define SEPARATOR(c)    ((c) == ',' || (c) == ';' || (c) == '+')
72 #define SPACE(c)        ((c) == ' ' || (c) == '\n')
73
74 #define ASCII_LOWER(c)  ( (c) >= 'a' && (c) <= 'z' )
75 #define ASCII_UPPER(c)  ( (c) >= 'A' && (c) <= 'Z' )
76 #define ASCII_ALPHA(c)  ( ASCII_LOWER(c) || ASCII_UPPER(c) )
77 #define ASCII_DIGIT(c)  ( (c) >= '0' && (c) <= '9' )
78 #define ASCII_ALNUM(c)  ( ASCII_ALPHA(c) || ASCII_DIGIT(c) )
79
80 #define LEADKEYCHAR(c)  ( ASCII_ALPHA(c) )
81 #define KEYCHAR(c)      ( ASCII_ALNUM(c) || (c) == '-' )
82 #define LEADOIDCHAR(c)  ( ASCII_DIGIT(c) )
83 #define OIDCHAR(c)      ( ASCII_DIGIT(c) || (c) == '.' )
84
85 #define LEADATTRCHAR(c) ( LEADKEYCHAR(c) || LEADOIDCHAR(c) )
86 #define ATTRCHAR(c)     ( KEYCHAR((c)) || (c) == '.' )
87
88 #define NEEDSESCAPE(c)  ((c) == '\\' || (c) == '"')
89
90 #define SLAPD_ACI_DEFAULT_ATTR          "aci"
91
92 LIBSLAPD_F (int) slap_debug;
93
94
95 /*
96  * represents schema information for a database
97  */
98 #define SLAP_SCHERR_OUTOFMEM            1
99 #define SLAP_SCHERR_CLASS_NOT_FOUND     2
100 #define SLAP_SCHERR_ATTR_NOT_FOUND      3
101 #define SLAP_SCHERR_DUP_CLASS           4
102 #define SLAP_SCHERR_DUP_ATTR            5
103 #define SLAP_SCHERR_DUP_SYNTAX          6
104 #define SLAP_SCHERR_DUP_RULE            7
105 #define SLAP_SCHERR_NO_NAME             8
106 #define SLAP_SCHERR_ATTR_INCOMPLETE     9
107 #define SLAP_SCHERR_MR_NOT_FOUND        10
108 #define SLAP_SCHERR_SYN_NOT_FOUND       11
109 #define SLAP_SCHERR_MR_INCOMPLETE       12
110
111 typedef struct slap_oid_macro {
112         struct slap_oid_macro *next;
113         char *name;
114         char *oid;
115         int oidlen;
116 } OidMacro;
117
118 typedef int slap_syntax_validate_func LDAP_P((
119         struct berval * in));
120
121 typedef int slap_syntax_normalize_func LDAP_P((
122         struct berval * in,
123         struct berval ** out));
124
125 typedef struct slap_syntax {
126         LDAP_SYNTAX                     ssyn_syn;
127         slap_syntax_validate_func       *ssyn_validate;
128         slap_syntax_normalize_func      *ssyn_normalize;
129         struct slap_syntax              *ssyn_next;
130 #define ssyn_oid                        ssyn_syn.syn_oid
131 #define ssyn_desc                       ssyn_syn.syn_desc
132 } Syntax;
133
134 typedef int slap_mr_match_func LDAP_P((
135         struct berval * atval,
136         struct berval * matchval));
137
138 typedef struct slap_matching_rule {
139         LDAP_MATCHING_RULE              smr_mrule;
140         slap_mr_match_func              *smr_match;
141         Syntax                          *smr_syntax;
142         struct slap_matching_rule       *smr_next;
143 #define smr_oid                         smr_mrule.mr_oid
144 #define smr_names                       smr_mrule.mr_names
145 #define smr_desc                        smr_mrule.mr_desc
146 #define smr_obsolete                    smr_mrule.mr_obsolete
147 #define smr_syntax_oid                  smr_mrule.mr_syntax_oid
148 } MatchingRule;
149
150 typedef struct slap_attribute_type {
151         LDAP_ATTRIBUTE_TYPE             sat_atype;
152         struct slap_attribute_type      *sat_sup;
153         struct slap_attribute_type      **sat_subtypes;
154         MatchingRule                    *sat_equality;
155         MatchingRule                    *sat_ordering;
156         MatchingRule                    *sat_substr;
157         Syntax                          *sat_syntax;
158         /* The next one is created to help in the transition */
159         int                             sat_syntax_compat;
160         struct slap_attribute_type      *sat_next;
161 #define sat_oid                 sat_atype.at_oid
162 #define sat_names               sat_atype.at_names
163 #define sat_desc                sat_atype.at_desc
164 #define sat_obsolete            sat_atype.at_obsolete
165 #define sat_sup_oid             sat_atype.at_sup_oid
166 #define sat_equality_oid        sat_atype.at_equality_oid
167 #define sat_ordering_oid        sat_atype.at_ordering_oid
168 #define sat_substr_oid          sat_atype.at_substr_oid
169 #define sat_syntax_oid          sat_atype.at_syntax_oid
170 #define sat_single_value        sat_atype.at_single_value
171 #define sat_collective          sat_atype.at_collective
172 #define sat_no_user_mods        sat_atype.at_no_user_mods
173 #define sat_usage               sat_atype.at_usage
174 } AttributeType;
175
176 typedef struct slap_object_class {
177         LDAP_OBJECT_CLASS               soc_oclass;
178         struct slap_object_class        **soc_sups;
179         AttributeType                   **soc_required;
180         AttributeType                   **soc_allowed;
181         struct slap_object_class        *soc_next;
182 #define soc_oid                 soc_oclass.oc_oid
183 #define soc_names               soc_oclass.oc_names
184 #define soc_desc                soc_oclass.oc_desc
185 #define soc_obsolete            soc_oclass.oc_obsolete
186 #define soc_sup_oids            soc_oclass.oc_sup_oids
187 #define soc_kind                soc_oclass.oc_kind
188 #define soc_at_oids_must        soc_oclass.oc_at_oids_must
189 #define soc_at_oids_may         soc_oclass.oc_at_oids_may
190 } ObjectClass;
191
192
193 struct slap_op;
194 struct slap_conn;
195
196 struct replog_moddn {
197         char *newrdn;
198         int     deloldrdn;
199         char *newsup;
200 };
201
202 /*
203  * represents an attribute value assertion (i.e., attr=value)
204  */
205 typedef struct slap_ava {
206         char            *ava_type;      /* attribute description */
207         struct berval   ava_value;
208 } Ava;
209
210 typedef struct slap_mra {
211         char    *mra_rule;
212         char    *mra_type;      /* attribute description */
213         char    *mra_value;
214         int             mra_dnattrs;
215 } Mra;
216
217 /*
218  * represents a search filter
219  */
220 typedef struct slap_filter {
221         ber_tag_t       f_choice;       /* values taken from ldap.h */
222
223         union f_un_u {
224                 /* present */
225                 char            *f_un_type;
226
227                 /* equality, lessorequal, greaterorequal, approx */
228                 Ava             f_un_ava;
229
230                 /* extensible */
231                 Mra             f_un_fra;       
232
233                 /* and, or, not */
234                 struct slap_filter      *f_un_complex;
235
236                 /* substrings */
237                 struct sub {
238                         char    *f_un_sub_type;
239                         char    *f_un_sub_initial;
240                         char    **f_un_sub_any;
241                         char    *f_un_sub_final;
242                 } f_un_sub;
243         } f_un;
244
245 #define f_dn            f_un.f_un_type  /* used for DN indices */
246 #define f_type          f_un.f_un_type
247 #define f_ava           f_un.f_un_ava
248 #define f_avtype        f_un.f_un_ava.ava_type
249 #define f_avvalue       f_un.f_un_ava.ava_value
250 #define f_mra           f_un.f_un_mra
251 #define f_mrtype        f_un.f_un_mra.mra_type
252 #define f_mrvalue       f_un.f_un_mra.mra_value
253 #define f_mrdnaddrs     f_un.f_un_mra.mra_dnattrs
254 #define f_and           f_un.f_un_complex
255 #define f_or            f_un.f_un_complex
256 #define f_not           f_un.f_un_complex
257 #define f_list          f_un.f_un_complex
258 #define f_sub           f_un.f_un_sub
259 #define f_sub_type      f_un.f_un_sub.f_un_sub_type
260 #define f_sub_initial   f_un.f_un_sub.f_un_sub_initial
261 #define f_sub_any       f_un.f_un_sub.f_un_sub_any
262 #define f_sub_final     f_un.f_un_sub.f_un_sub_final
263
264         struct slap_filter      *f_next;
265 } Filter;
266
267 /*
268  * represents an attribute (type + values + syntax)
269  */
270 typedef struct slap_attr {
271         char            *a_type;        /* description */
272         struct berval   **a_vals;
273 #ifdef SLAPD_SCHEMA_COMPAT
274         int             a_syntax;
275 #endif
276         struct slap_attr        *a_next;
277 } Attribute;
278
279 #ifdef SLAPD_SCHEMA_COMPAT
280 /*
281  * the attr_syntax() routine returns one of these values
282  * telling what kind of syntax an attribute supports.
283  */
284 #define SYNTAX_CIS      0x01    /* case insensitive string              */
285 #define SYNTAX_CES      0x02    /* case sensitive string                */
286 #define SYNTAX_BIN      0x04    /* binary data                          */
287 #define SYNTAX_TEL      0x08    /* telephone number string              */
288 #define SYNTAX_DN       0x10    /* dn string                            */
289 #endif
290
291 /*
292  * the id used in the indexes to refer to an entry
293  */
294 typedef unsigned long   ID;
295 #define NOID    ((ID)~0)
296
297 /*
298  * represents an entry in core
299  */
300 typedef struct slap_entry {
301         /*
302          * The ID field should only be changed before entry is
303          * inserted into a cache.  The ID value is backend
304          * specific.
305          */
306         ID              e_id;
307
308         char            *e_dn;          /* DN of this entry */
309         char            *e_ndn;         /* normalized DN of this entry */
310         Attribute       *e_attrs;       /* list of attributes + values */
311
312         /* for use by the backend for any purpose */
313         void*   e_private;
314 } Entry;
315
316 /*
317  * represents an access control list
318  */
319
320 typedef enum slap_access_e {
321         ACL_INVALID_ACCESS = -1,
322         ACL_NONE = 0,
323         ACL_AUTH,
324         ACL_COMPARE,
325         ACL_SEARCH,
326         ACL_READ,
327         ACL_WRITE
328 } slap_access_t;
329
330 typedef enum slap_control_e {
331         ACL_INVALID_CONTROL     = 0,
332         ACL_STOP,
333         ACL_CONTINUE,
334         ACL_BREAK
335 } slap_control_t;
336
337 typedef unsigned long slap_access_mask_t;
338
339 /* the "by" part */
340 typedef struct slap_access {
341         slap_control_t a_type;
342
343 #define ACL_ACCESS2PRIV(access) (0x01U << (access))
344
345 #define ACL_PRIV_NONE                   ACL_ACCESS2PRIV( ACL_NONE )
346 #define ACL_PRIV_AUTH                   ACL_ACCESS2PRIV( ACL_AUTH )
347 #define ACL_PRIV_COMPARE                ACL_ACCESS2PRIV( ACL_COMPARE )
348 #define ACL_PRIV_SEARCH                 ACL_ACCESS2PRIV( ACL_SEARCH )
349 #define ACL_PRIV_READ                   ACL_ACCESS2PRIV( ACL_READ )
350 #define ACL_PRIV_WRITE                  ACL_ACCESS2PRIV( ACL_WRITE )
351
352 #define ACL_PRIV_MASK                   0x00ffUL
353
354 /* priv flags */
355 #define ACL_PRIV_LEVEL                  0x1000UL
356 #define ACL_PRIV_ADDITIVE               0x2000UL
357 #define ACL_PRIV_SUBSTRACTIVE   0x4000UL
358
359 /* invalid privs */
360 #define ACL_PRIV_INVALID                0x0UL
361
362 #define ACL_PRIV_ISSET(m,p)             (((m) & (p)) == (p))
363 #define ACL_PRIV_ASSIGN(m,p)    do { (m)  =  (p); } while(0)
364 #define ACL_PRIV_SET(m,p)               do { (m) |=  (p); } while(0)
365 #define ACL_PRIV_CLR(m,p)               do { (m) &= ~(p); } while(0)
366
367 #define ACL_INIT(m)                             ACL_PRIV_ASSIGN(m, ACL_PRIV_NONE)
368 #define ACL_INVALIDATE(m)               ACL_PRIV_ASSIGN(m, ACL_PRIV_INVALID)
369
370 #define ACL_GRANT(m,a)                  ACL_PRIV_ISSET((m),ACL_ACCESS2PRIV(a))
371
372 #define ACL_IS_INVALID(m)               ((m) == ACL_PRIV_INVALID)
373
374 #define ACL_IS_LEVEL(m)                 ACL_PRIV_ISSET((m),ACL_PRIV_LEVEL)
375 #define ACL_IS_ADDITIVE(m)              ACL_PRIV_ISSET((m),ACL_PRIV_ADDITIVE)
376 #define ACL_IS_SUBTRACTIVE(m)   ACL_PRIV_ISSET((m),ACL_PRIV_SUBSTRACTIVE)
377
378 #define ACL_LVL_NONE                    (ACL_PRIV_NONE|ACL_PRIV_LEVEL)
379 #define ACL_LVL_AUTH                    (ACL_PRIV_AUTH|ACL_LVL_NONE)
380 #define ACL_LVL_COMPARE                 (ACL_PRIV_COMPARE|ACL_LVL_AUTH)
381 #define ACL_LVL_SEARCH                  (ACL_PRIV_SEARCH|ACL_LVL_COMPARE)
382 #define ACL_LVL_READ                    (ACL_PRIV_READ|ACL_LVL_SEARCH)
383 #define ACL_LVL_WRITE                   (ACL_PRIV_WRITE|ACL_LVL_READ)
384
385 #define ACL_LVL(m,l)                    (((m)&ACL_PRIV_MASK) == ((l)&ACL_PRIV_MASK))
386 #define ACL_LVL_IS_NONE(m)              ACL_LVL((m),ACL_LVL_NONE)
387 #define ACL_LVL_IS_AUTH(m)              ACL_LVL((m),ACL_LVL_AUTH)
388 #define ACL_LVL_IS_COMPARE(m)   ACL_LVL((m),ACL_LVL_COMPARE)
389 #define ACL_LVL_IS_SEARCH(m)    ACL_LVL((m),ACL_LVL_SEARCH)
390 #define ACL_LVL_IS_READ(m)              ACL_LVL((m),ACL_LVL_READ)
391 #define ACL_LVL_IS_WRITE(m)             ACL_LVL((m),ACL_LVL_WRITE)
392
393 #define ACL_LVL_ASSIGN_NONE(m)          ACL_PRIV_ASSIGN((m),ACL_LVL_NONE)
394 #define ACL_LVL_ASSIGN_AUTH(m)          ACL_PRIV_ASSIGN((m),ACL_LVL_AUTH)
395 #define ACL_LVL_ASSIGN_COMPARE(m)       ACL_PRIV_ASSIGN((m),ACL_LVL_COMPARE)
396 #define ACL_LVL_ASSIGN_SEARCH(m)        ACL_PRIV_ASSIGN((m),ACL_LVL_SEARCH)
397 #define ACL_LVL_ASSIGN_READ(m)          ACL_PRIV_ASSIGN((m),ACL_LVL_READ)
398 #define ACL_LVL_ASSIGN_WRITE(m)         ACL_PRIV_ASSIGN((m),ACL_LVL_WRITE)
399
400         slap_access_mask_t      a_mask;
401
402         char            *a_dn_pat;
403         char            *a_dn_at;
404         int                     a_dn_self;
405
406         char            *a_peername_pat;
407         char            *a_sockname_pat;
408
409         char            *a_domain_pat;
410         char            *a_sockurl_pat;
411
412 #ifdef SLAPD_ACI_ENABLED
413         char            *a_aci_at;
414 #endif
415
416         /* ACL Groups */
417         char            *a_group_pat;
418         char            *a_group_oc;
419         char            *a_group_at;
420
421         struct slap_access      *a_next;
422 } Access;
423
424 /* the "to" part */
425 typedef struct slap_acl {
426         /* "to" part: the entries this acl applies to */
427         Filter          *acl_filter;
428         regex_t         acl_dn_re;
429         char            *acl_dn_pat;
430         char            **acl_attrs;
431
432         /* "by" part: list of who has what access to the entries */
433         Access  *acl_access;
434
435         struct slap_acl *acl_next;
436 } AccessControl;
437
438 /*
439  * A list of LDAPMods
440  */
441 typedef struct ldapmodlist {
442         struct ldapmod ml_mod;
443         struct ldapmodlist *ml_next;
444 #define ml_op           ml_mod.mod_op
445 #define ml_type         ml_mod.mod_type
446 #define ml_values       ml_mod.mod_values
447 #define ml_bvalues      ml_mod.mod_bvalues
448 } LDAPModList;
449
450 /*
451  * Backend-info
452  * represents a backend 
453  */
454
455 typedef struct slap_backend_info BackendInfo;   /* per backend type */
456 typedef struct slap_backend_db BackendDB;               /* per backend database */
457
458 LIBSLAPD_F (int) nBackendInfo;
459 LIBSLAPD_F (int) nBackendDB;
460 LIBSLAPD_F (BackendInfo *) backendInfo;
461 LIBSLAPD_F (BackendDB *) backendDB;
462
463 LIBSLAPD_F (int) slapMode;      
464 #define SLAP_UNDEFINED_MODE     0x0000
465 #define SLAP_SERVER_MODE        0x0001
466 #define SLAP_TOOL_MODE          0x0002
467 #define SLAP_MODE                       0x0003
468
469 #define SLAP_TRUNCATE_MODE      0x0100
470 #ifdef SLAPD_BDB2
471 #define SLAP_TIMED_MODE         0x1000
472 #endif
473 #define SLAP_TOOLID_MODE    4
474
475 /* temporary aliases */
476 typedef BackendDB Backend;
477 #define nbackends nBackendDB
478 #define backends backendDB
479
480 struct slap_backend_db {
481         BackendInfo     *bd_info;       /* pointer to shared backend info */
482
483         /* BackendInfo accessors */
484 #define         be_config       bd_info->bi_db_config
485 #define         be_type         bd_info->bi_type
486
487 #define         be_bind         bd_info->bi_op_bind
488 #define         be_unbind       bd_info->bi_op_unbind
489 #define         be_add          bd_info->bi_op_add
490 #define         be_compare      bd_info->bi_op_compare
491 #define         be_delete       bd_info->bi_op_delete
492 #define         be_modify       bd_info->bi_op_modify
493 #define         be_modrdn       bd_info->bi_op_modrdn
494 #define         be_search       bd_info->bi_op_search
495
496 #define         be_extended     bd_info->bi_extended
497
498 #define         be_release      bd_info->bi_entry_release_rw
499 #define         be_group        bd_info->bi_acl_group
500
501 #define         be_controls     bd_info->bi_controls
502
503 #define         be_connection_init      bd_info->bi_connection_init
504 #define         be_connection_destroy   bd_info->bi_connection_destroy
505
506 #ifdef SLAPD_TOOLS
507 #define         be_entry_open bd_info->bi_tool_entry_open
508 #define         be_entry_close bd_info->bi_tool_entry_close
509 #define         be_entry_first bd_info->bi_tool_entry_first
510 #define         be_entry_next bd_info->bi_tool_entry_next
511 #define         be_entry_get bd_info->bi_tool_entry_get
512 #define         be_entry_put bd_info->bi_tool_entry_put
513 #define         be_index_attr bd_info->bi_tool_index_attr
514 #define         be_index_change bd_info->bi_tool_index_change
515 #define         be_sync bd_info->bi_tool_sync
516 #endif
517
518 #ifdef HAVE_CYRUS_SASL
519 #define         be_sasl_authorize bd_info->bi_sasl_authorize
520 #define         be_sasl_getsecret bd_info->bi_sasl_getsecret
521 #define         be_sasl_putsecret bd_info->bi_sasl_putsecret
522 #endif
523
524         /* these should be renamed from be_ to bd_ */
525         char    **be_suffix;    /* the DN suffixes of data in this backend */
526         char    **be_nsuffix;   /* the normalized DN suffixes in this backend */
527         char    **be_suffixAlias; /* pairs of DN suffix aliases and deref values */
528         char    *be_root_dn;    /* the magic "root" dn for this db      */
529         char    *be_root_ndn;   /* the magic "root" normalized dn for this db   */
530         struct berval be_root_pw;       /* the magic "root" password for this db        */
531         int     be_readonly;    /* 1 => db is in "read only" mode          */
532         unsigned int be_max_deref_depth;       /* limit for depth of an alias deref  */
533         int     be_sizelimit;   /* size limit for this backend             */
534         int     be_timelimit;   /* time limit for this backend             */
535         AccessControl *be_acl;  /* access control list for this backend    */
536         slap_access_t   be_dfltaccess;  /* access given if no acl matches          */
537         char    **be_replica;   /* replicas of this backend (in master)    */
538         char    *be_replogfile; /* replication log file (in master)        */
539         char    *be_update_ndn; /* allowed to make changes (in replicas) */
540         struct berval **be_update_refs; /* where to refer modifying clients to */
541         int     be_lastmod;     /* keep track of lastmodified{by,time}     */
542
543         char    *be_realm;
544
545         void    *be_private;    /* anything the backend database needs     */
546 };
547
548 typedef int (*SLAP_EXTENDED_FN) LDAP_P((
549     Backend             *be,
550     struct slap_conn            *conn,
551     struct slap_op              *op,
552         char            *reqoid,
553     struct berval * reqdata,
554         char            **rspoid,
555     struct berval ** rspdata,
556         LDAPControl *** rspctrls,
557         char ** text,
558         struct berval *** refs ));
559
560 struct slap_backend_info {
561         char    *bi_type;       /* type of backend */
562
563         /*
564          * per backend type routines:
565          * bi_init: called to allocate a backend_info structure,
566          *              called once BEFORE configuration file is read.
567          *              bi_init() initializes this structure hence is
568          *              called directly from be_initialize()
569          * bi_config: called per 'backend' specific option
570          *              all such options must before any 'database' options
571          *              bi_config() is called only from read_config()
572          * bi_open: called to open each database, called
573          *              once AFTER configuration file is read but
574          *              BEFORE any bi_db_open() calls.
575          *              bi_open() is called from backend_startup()
576          * bi_close: called to close each database, called
577          *              once during shutdown after all bi_db_close calls.
578          *              bi_close() is called from backend_shutdown()
579          * bi_destroy: called to destroy each database, called
580          *              once during shutdown after all bi_db_destroy calls.
581          *              bi_destory() is called from backend_destroy()
582          */
583         int (*bi_init)  LDAP_P((BackendInfo *bi));
584         int     (*bi_config) LDAP_P((BackendInfo *bi,
585                 const char *fname, int lineno, int argc, char **argv ));
586         int (*bi_open) LDAP_P((BackendInfo *bi));
587         int (*bi_close) LDAP_P((BackendInfo *bi));
588         int (*bi_destroy) LDAP_P((BackendInfo *bi));
589
590         /*
591          * per database routines:
592          * bi_db_init: called to initialize each database,
593          *      called upon reading 'database <type>' 
594          *      called only from backend_db_init()
595          * bi_db_config: called to configure each database,
596          *  called per database to handle per database options
597          *      called only from read_config()
598          * bi_db_open: called to open each database
599          *      called once per database immediately AFTER bi_open()
600          *      calls but before daemon startup.
601          *  called only by backend_startup()
602          * bi_db_close: called to close each database
603          *      called once per database during shutdown but BEFORE
604          *  any bi_close call.
605          *  called only by backend_shutdown()
606          * bi_db_destroy: called to destroy each database
607          *  called once per database during shutdown AFTER all
608          *  bi_close calls but before bi_destory calls.
609          *  called only by backend_destory()
610          */
611         int (*bi_db_init) LDAP_P((Backend *bd));
612         int     (*bi_db_config) LDAP_P((Backend *bd,
613                 const char *fname, int lineno, int argc, char **argv ));
614         int (*bi_db_open) LDAP_P((Backend *bd));
615         int (*bi_db_close) LDAP_P((Backend *bd));
616         int (*bi_db_destroy) LDAP_P((Backend *db));
617
618         /* LDAP Operations Handling Routines */
619         int     (*bi_op_bind)  LDAP_P(( BackendDB *bd,
620                 struct slap_conn *c, struct slap_op *o,
621                 char *dn, char *ndn, int method, char* mechanism,
622                 struct berval *cred, char** edn ));
623         int (*bi_op_unbind) LDAP_P((BackendDB *bd,
624                 struct slap_conn *c, struct slap_op *o ));
625         int     (*bi_op_search) LDAP_P((BackendDB *bd,
626                 struct slap_conn *c, struct slap_op *o,
627                 char *base, char *nbase, int scope, int deref,
628                 int slimit, int tlimit,
629                 Filter *f, char *filterstr, char **attrs,
630                 int attrsonly));
631         int     (*bi_op_compare)LDAP_P((BackendDB *bd,
632                 struct slap_conn *c, struct slap_op *o,
633                 char *dn, char *ndn, Ava *ava));
634         int     (*bi_op_modify) LDAP_P((BackendDB *bd,
635                 struct slap_conn *c, struct slap_op *o,
636                 char *dn, char *ndn, LDAPModList *m));
637         int     (*bi_op_modrdn) LDAP_P((BackendDB *bd,
638                 struct slap_conn *c, struct slap_op *o,
639                 char *dn, char *ndn, char *newrdn, int deleteoldrdn,
640                 char *newSuperior));
641         int     (*bi_op_add)    LDAP_P((BackendDB *bd,
642                 struct slap_conn *c, struct slap_op *o,
643                 Entry *e));
644         int     (*bi_op_delete) LDAP_P((BackendDB *bd,
645                 struct slap_conn *c, struct slap_op *o,
646                 char *dn, char *ndn));
647         int     (*bi_op_abandon) LDAP_P((BackendDB *bd,
648                 struct slap_conn *c, struct slap_op *o,
649                 ber_int_t msgid));
650
651         /* Extended Operations Helper */
652         SLAP_EXTENDED_FN bi_extended;
653
654         /* Auxilary Functions */
655         int     (*bi_entry_release_rw) LDAP_P((BackendDB *bd, Entry *e, int rw));
656
657         int     (*bi_acl_group)  LDAP_P((Backend *bd,
658                 Entry *e, const char *bdn, const char *edn,
659                 const char *objectclassValue, const char *groupattrName ));
660
661         int     (*bi_connection_init) LDAP_P((BackendDB *bd,
662                 struct slap_conn *c));
663         int     (*bi_connection_destroy) LDAP_P((BackendDB *bd,
664                 struct slap_conn *c));
665
666         /* hooks for slap tools */
667         int (*bi_tool_entry_open) LDAP_P(( BackendDB *be, int mode ));
668         int (*bi_tool_entry_close) LDAP_P(( BackendDB *be ));
669         ID (*bi_tool_entry_first) LDAP_P(( BackendDB *be ));
670         ID (*bi_tool_entry_next) LDAP_P(( BackendDB *be ));
671         Entry* (*bi_tool_entry_get) LDAP_P(( BackendDB *be, ID id ));
672         ID (*bi_tool_entry_put) LDAP_P(( BackendDB *be, Entry *e ));
673         int (*bi_tool_index_attr) LDAP_P(( BackendDB *be, char* type ));
674         int (*bi_tool_index_change) LDAP_P(( BackendDB *be, char* type,
675                 struct berval **bv, ID id, int op ));
676         int (*bi_tool_sync) LDAP_P(( BackendDB *be ));
677
678 #ifdef HAVE_CYRUS_SASL
679         int (*bi_sasl_authorize) LDAP_P(( BackendDB *be,
680                 const char *authnid, const char *authzid,
681                 const char **canon_authzid, const char **errstr ));
682         int (*bi_sasl_getsecret) LDAP_P(( BackendDB *be,
683                 const char *mechanism, const char *authzid,
684                 const char *realm, sasl_secret_t **secret ));
685         int (*bi_sasl_putsecret) LDAP_P(( BackendDB *be,
686                 const char *mechanism, const char *auth_identity,
687                 const char *realm, const sasl_secret_t *secret ));
688 #endif /* HAVE_CYRUS_SASL */
689
690 #define SLAP_INDEX_ADD_OP               0x0001
691 #define SLAP_INDEX_DELETE_OP    0x0002
692
693         char **bi_controls;             /* supported controls */
694
695         unsigned int bi_nDB;    /* number of databases of this type */
696         void    *bi_private;    /* anything the backend type needs */
697 };
698
699 /*
700  * represents an operation pending from an ldap client
701  */
702
703 typedef struct slap_op {
704         ber_int_t       o_opid;         /* id of this operation           */
705         ber_int_t       o_msgid;        /* msgid of the request           */
706
707         ldap_pvt_thread_t       o_tid;          /* thread handling this op        */
708
709         BerElement      *o_ber;         /* ber of the request             */
710
711         ber_tag_t       o_tag;          /* tag of the request             */
712         time_t          o_time;         /* time op was initiated          */
713
714         int             o_bind_in_progress;     /* multi-step bind in progress */
715 #ifdef SLAP_AUTHZID
716         /* should only be used for reporting purposes */
717         char    *o_authc_dn;    /* authentication DN */
718
719         /* should be used as the DN of the User */
720         char    *o_authz_dn;    /* authorization DN */
721         char    *o_authz_ndn;   /* authorizaiton NDN */
722
723 #else
724         char            *o_dn;          /* dn bound when op was initiated */
725         char            *o_ndn;         /* normalized dn bound when op was initiated */
726 #endif
727
728         ber_int_t       o_protocol;     /* version of the LDAP protocol used by client */
729         ber_tag_t       o_authtype;     /* auth method used to bind dn    */
730                                         /* values taken from ldap.h       */
731                                         /* LDAP_AUTH_*                    */
732         char            *o_authmech; /* SASL mechanism used to bind dn */
733
734         LDAPControl     **o_ctrls;       /* controls */
735
736         unsigned long   o_connid; /* id of conn initiating this op  */
737
738 #ifdef LDAP_CONNECTIONLESS
739         int             o_cldap;        /* != 0 if this came in via CLDAP */
740         struct sockaddr o_clientaddr;   /* client address if via CLDAP    */
741         char            o_searchbase;   /* search base if via CLDAP       */
742 #endif
743
744         ldap_pvt_thread_mutex_t o_abandonmutex; /* protects o_abandon  */
745         int             o_abandon;      /* abandon flag */
746
747         struct slap_op  *o_next;        /* next operation in list         */
748         void    *o_private;     /* anything the backend needs     */
749 } Operation;
750
751 /*
752  * represents a connection from an ldap client
753  */
754
755 typedef struct slap_conn {
756         int                     c_struct_state; /* structure management state */
757         int                     c_conn_state;   /* connection state */
758
759         ldap_pvt_thread_mutex_t c_mutex; /* protect the connection */
760         Sockbuf         *c_sb;                  /* ber connection stuff           */
761
762         /* only can be changed by connect_init */
763         time_t          c_starttime;    /* when the connection was opened */
764         time_t          c_activitytime; /* when the connection was last used */
765         unsigned long           c_connid;       /* id of this connection for stats*/
766
767         char            *c_listener_url;        /* listener URL */
768         char            *c_peer_domain; /* DNS name of client */
769         char            *c_peer_name;   /* peer name (trans=addr:port) */
770         char            *c_sock_name;   /* sock name (trans=addr:port) */
771
772         /* only can be changed by binding thread */
773         int             c_bind_in_progress;     /* multi-op bind in progress */
774 #ifdef HAVE_CYRUS_SASL
775         sasl_conn_t     *c_sasl_context;
776 #endif
777         void    *c_authstate;   /* SASL state data */
778
779         Backend *c_authc_backend;
780
781         /* authorization backend */
782         Backend *c_authz_backend;
783
784 #ifdef SLAP_AUTHZID
785         /* authentication backend */
786         /* should only be used for reporting purposes */
787         char    *c_authc_dn;    /* authentication DN */
788
789         /* should be used as the DN of the User */
790         char    *c_authz_dn;    /* authorization DN */
791         char    *c_authz_ndn;   /* authorization NDN */
792
793 #else
794         char    *c_cdn;         /* DN provided by the client */
795         char    *c_dn;          /* DN bound to this conn  */
796 #endif
797
798         ber_int_t       c_protocol;     /* version of the LDAP protocol used by client */
799         ber_tag_t       c_authtype;/* auth method used to bind c_dn  */
800         char    *c_authmech;    /* SASL mechanism used to bind c_dn */
801
802         Operation       *c_ops;                 /* list of operations being processed */
803         Operation       *c_pending_ops; /* list of pending operations */
804
805         ldap_pvt_thread_mutex_t c_write_mutex;  /* only one pdu written at a time */
806         ldap_pvt_thread_cond_t  c_write_cv;             /* used to wait for sd write-ready*/
807
808         BerElement      *c_currentber;  /* ber we're attempting to read */
809         int             c_writewaiter;  /* true if writer is waiting */
810
811 #ifdef HAVE_TLS
812         int     c_is_tls;               /* true if this LDAP over raw TLS */
813         int     c_needs_tls_accept;     /* true if SSL_accept should be called */
814 #endif
815
816         long    c_n_ops_received;               /* num of ops received (next op_id) */
817         long    c_n_ops_executing;      /* num of ops currently executing */
818         long    c_n_ops_pending;                /* num of ops pending execution */
819         long    c_n_ops_completed;      /* num of ops completed */
820
821         long    c_n_get;                /* num of get calls */
822         long    c_n_read;               /* num of read calls */
823         long    c_n_write;              /* num of write calls */
824 } Connection;
825
826 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
827 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )  \
828         do { \
829                 if ( ldap_debug & (level) ) \
830                         fprintf( stderr, (fmt), (connid), (opid), (arg1), (arg2), (arg3) );\
831                 if ( ldap_syslog & (level) ) \
832                         syslog( ldap_syslog_level, (fmt), (connid), (opid), (arg1), \
833                                 (arg2), (arg3) ); \
834         } while (0)
835 #else
836 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )
837 #endif
838
839 LDAP_END_DECL
840
841 #include "proto-slap.h"
842
843 #endif /* _slap_h_ */