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