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