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