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