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