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