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