]> git.sur5r.net Git - openldap/blob - servers/slapd/slap.h
Another round of changes behind -DSLAPD_SCHEMA_NOT_COMPAT
[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.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 #endif
482
483 typedef struct slap_mod_list {
484         Modification ml_mod;
485 #ifdef SLAPD_SCHEMA_NOT_COMPAT
486 #define ml_op           ml_mod.sm_op
487 #define ml_desc         ml_mod.sm_desc
488 #define ml_bvalues      ml_mod.sm_bvalues
489 #else
490 #define ml_op           ml_mod.mod_op
491 #define ml_type         ml_mod.mod_type
492 #define ml_values       ml_mod.mod_values
493 #define ml_bvalues      ml_mod.mod_bvalues
494 #endif
495         struct slap_mod_list *ml_next;
496 } Modifications;
497
498 /*
499  * represents an access control list
500  */
501
502 typedef enum slap_access_e {
503         ACL_INVALID_ACCESS = -1,
504         ACL_NONE = 0,
505         ACL_AUTH,
506         ACL_COMPARE,
507         ACL_SEARCH,
508         ACL_READ,
509         ACL_WRITE
510 } slap_access_t;
511
512 typedef enum slap_control_e {
513         ACL_INVALID_CONTROL     = 0,
514         ACL_STOP,
515         ACL_CONTINUE,
516         ACL_BREAK
517 } slap_control_t;
518
519 typedef unsigned long slap_access_mask_t;
520
521 /* the "by" part */
522 typedef struct slap_access {
523         slap_control_t a_type;
524
525 #define ACL_ACCESS2PRIV(access) (0x01U << (access))
526
527 #define ACL_PRIV_NONE                   ACL_ACCESS2PRIV( ACL_NONE )
528 #define ACL_PRIV_AUTH                   ACL_ACCESS2PRIV( ACL_AUTH )
529 #define ACL_PRIV_COMPARE                ACL_ACCESS2PRIV( ACL_COMPARE )
530 #define ACL_PRIV_SEARCH                 ACL_ACCESS2PRIV( ACL_SEARCH )
531 #define ACL_PRIV_READ                   ACL_ACCESS2PRIV( ACL_READ )
532 #define ACL_PRIV_WRITE                  ACL_ACCESS2PRIV( ACL_WRITE )
533
534 #define ACL_PRIV_MASK                   0x00ffUL
535
536 /* priv flags */
537 #define ACL_PRIV_LEVEL                  0x1000UL
538 #define ACL_PRIV_ADDITIVE               0x2000UL
539 #define ACL_PRIV_SUBSTRACTIVE   0x4000UL
540
541 /* invalid privs */
542 #define ACL_PRIV_INVALID                0x0UL
543
544 #define ACL_PRIV_ISSET(m,p)             (((m) & (p)) == (p))
545 #define ACL_PRIV_ASSIGN(m,p)    do { (m)  =  (p); } while(0)
546 #define ACL_PRIV_SET(m,p)               do { (m) |=  (p); } while(0)
547 #define ACL_PRIV_CLR(m,p)               do { (m) &= ~(p); } while(0)
548
549 #define ACL_INIT(m)                             ACL_PRIV_ASSIGN(m, ACL_PRIV_NONE)
550 #define ACL_INVALIDATE(m)               ACL_PRIV_ASSIGN(m, ACL_PRIV_INVALID)
551
552 #define ACL_GRANT(m,a)                  ACL_PRIV_ISSET((m),ACL_ACCESS2PRIV(a))
553
554 #define ACL_IS_INVALID(m)               ((m) == ACL_PRIV_INVALID)
555
556 #define ACL_IS_LEVEL(m)                 ACL_PRIV_ISSET((m),ACL_PRIV_LEVEL)
557 #define ACL_IS_ADDITIVE(m)              ACL_PRIV_ISSET((m),ACL_PRIV_ADDITIVE)
558 #define ACL_IS_SUBTRACTIVE(m)   ACL_PRIV_ISSET((m),ACL_PRIV_SUBSTRACTIVE)
559
560 #define ACL_LVL_NONE                    (ACL_PRIV_NONE|ACL_PRIV_LEVEL)
561 #define ACL_LVL_AUTH                    (ACL_PRIV_AUTH|ACL_LVL_NONE)
562 #define ACL_LVL_COMPARE                 (ACL_PRIV_COMPARE|ACL_LVL_AUTH)
563 #define ACL_LVL_SEARCH                  (ACL_PRIV_SEARCH|ACL_LVL_COMPARE)
564 #define ACL_LVL_READ                    (ACL_PRIV_READ|ACL_LVL_SEARCH)
565 #define ACL_LVL_WRITE                   (ACL_PRIV_WRITE|ACL_LVL_READ)
566
567 #define ACL_LVL(m,l)                    (((m)&ACL_PRIV_MASK) == ((l)&ACL_PRIV_MASK))
568 #define ACL_LVL_IS_NONE(m)              ACL_LVL((m),ACL_LVL_NONE)
569 #define ACL_LVL_IS_AUTH(m)              ACL_LVL((m),ACL_LVL_AUTH)
570 #define ACL_LVL_IS_COMPARE(m)   ACL_LVL((m),ACL_LVL_COMPARE)
571 #define ACL_LVL_IS_SEARCH(m)    ACL_LVL((m),ACL_LVL_SEARCH)
572 #define ACL_LVL_IS_READ(m)              ACL_LVL((m),ACL_LVL_READ)
573 #define ACL_LVL_IS_WRITE(m)             ACL_LVL((m),ACL_LVL_WRITE)
574
575 #define ACL_LVL_ASSIGN_NONE(m)          ACL_PRIV_ASSIGN((m),ACL_LVL_NONE)
576 #define ACL_LVL_ASSIGN_AUTH(m)          ACL_PRIV_ASSIGN((m),ACL_LVL_AUTH)
577 #define ACL_LVL_ASSIGN_COMPARE(m)       ACL_PRIV_ASSIGN((m),ACL_LVL_COMPARE)
578 #define ACL_LVL_ASSIGN_SEARCH(m)        ACL_PRIV_ASSIGN((m),ACL_LVL_SEARCH)
579 #define ACL_LVL_ASSIGN_READ(m)          ACL_PRIV_ASSIGN((m),ACL_LVL_READ)
580 #define ACL_LVL_ASSIGN_WRITE(m)         ACL_PRIV_ASSIGN((m),ACL_LVL_WRITE)
581
582         slap_access_mask_t      a_mask;
583
584         char            *a_dn_pat;
585 #ifdef SLAPD_SCHEMA_NOT_COMPAT
586         AttributeDescription    *a_dn_at;
587 #else
588         char            *a_dn_at;
589 #endif
590         int                     a_dn_self;
591
592         char            *a_peername_pat;
593         char            *a_sockname_pat;
594
595         char            *a_domain_pat;
596         char            *a_sockurl_pat;
597
598 #ifdef SLAPD_ACI_ENABLED
599 #ifdef SLAPD_SCHEMA_NOT_COMPAT
600         AttributeDescription    *a_aci_at;
601 #else
602         char            *a_aci_at;
603 #endif
604 #endif
605
606         /* ACL Groups */
607         char            *a_group_pat;
608         char            *a_group_oc;
609 #ifdef SLAPD_SCHEMA_NOT_COMPAT
610         AttributeDescription    *a_group_at;
611 #else
612         char            *a_group_at;
613 #endif
614
615         struct slap_access      *a_next;
616 } Access;
617
618 /* the "to" part */
619 typedef struct slap_acl {
620         /* "to" part: the entries this acl applies to */
621         Filter          *acl_filter;
622         regex_t         acl_dn_re;
623         char            *acl_dn_pat;
624         char            **acl_attrs;
625
626         /* "by" part: list of who has what access to the entries */
627         Access  *acl_access;
628
629         struct slap_acl *acl_next;
630 } AccessControl;
631
632 /*
633  * replog moddn param structure
634  */
635 struct replog_moddn {
636         char *newrdn;
637         int     deloldrdn;
638         char *newsup;
639 };
640
641 /*
642  * Backend-info
643  * represents a backend 
644  */
645
646 typedef struct slap_backend_info BackendInfo;   /* per backend type */
647 typedef struct slap_backend_db BackendDB;               /* per backend database */
648
649 LIBSLAPD_F (int) nBackendInfo;
650 LIBSLAPD_F (int) nBackendDB;
651 LIBSLAPD_F (BackendInfo *) backendInfo;
652 LIBSLAPD_F (BackendDB *) backendDB;
653
654 LIBSLAPD_F (int) slapMode;      
655 #define SLAP_UNDEFINED_MODE     0x0000
656 #define SLAP_SERVER_MODE        0x0001
657 #define SLAP_TOOL_MODE          0x0002
658 #define SLAP_MODE                       0x0003
659
660 #define SLAP_TRUNCATE_MODE      0x0100
661 #ifdef SLAPD_BDB2
662 #define SLAP_TIMED_MODE         0x1000
663 #endif
664 #define SLAP_TOOLID_MODE    4
665
666 /* temporary aliases */
667 typedef BackendDB Backend;
668 #define nbackends nBackendDB
669 #define backends backendDB
670
671 struct slap_backend_db {
672         BackendInfo     *bd_info;       /* pointer to shared backend info */
673
674         /* BackendInfo accessors */
675 #define         be_config       bd_info->bi_db_config
676 #define         be_type         bd_info->bi_type
677
678 #define         be_bind         bd_info->bi_op_bind
679 #define         be_unbind       bd_info->bi_op_unbind
680 #define         be_add          bd_info->bi_op_add
681 #define         be_compare      bd_info->bi_op_compare
682 #define         be_delete       bd_info->bi_op_delete
683 #define         be_modify       bd_info->bi_op_modify
684 #define         be_modrdn       bd_info->bi_op_modrdn
685 #define         be_search       bd_info->bi_op_search
686
687 #define         be_extended     bd_info->bi_extended
688
689 #define         be_release      bd_info->bi_entry_release_rw
690 #define         be_group        bd_info->bi_acl_group
691
692 #define         be_controls     bd_info->bi_controls
693
694 #define         be_connection_init      bd_info->bi_connection_init
695 #define         be_connection_destroy   bd_info->bi_connection_destroy
696
697 #ifdef SLAPD_TOOLS
698 #define         be_entry_open bd_info->bi_tool_entry_open
699 #define         be_entry_close bd_info->bi_tool_entry_close
700 #define         be_entry_first bd_info->bi_tool_entry_first
701 #define         be_entry_next bd_info->bi_tool_entry_next
702 #define         be_entry_get bd_info->bi_tool_entry_get
703 #define         be_entry_put bd_info->bi_tool_entry_put
704 #define         be_index_attr bd_info->bi_tool_index_attr
705 #define         be_index_change bd_info->bi_tool_index_change
706 #define         be_sync bd_info->bi_tool_sync
707 #endif
708
709 #ifdef HAVE_CYRUS_SASL
710 #define         be_sasl_authorize bd_info->bi_sasl_authorize
711 #define         be_sasl_getsecret bd_info->bi_sasl_getsecret
712 #define         be_sasl_putsecret bd_info->bi_sasl_putsecret
713 #endif
714
715         /* these should be renamed from be_ to bd_ */
716         char    **be_suffix;    /* the DN suffixes of data in this backend */
717         char    **be_nsuffix;   /* the normalized DN suffixes in this backend */
718         char    **be_suffixAlias; /* pairs of DN suffix aliases and deref values */
719         char    *be_root_dn;    /* the magic "root" dn for this db      */
720         char    *be_root_ndn;   /* the magic "root" normalized dn for this db   */
721         struct berval be_root_pw;       /* the magic "root" password for this db        */
722         int     be_readonly;    /* 1 => db is in "read only" mode          */
723         unsigned int be_max_deref_depth;       /* limit for depth of an alias deref  */
724         int     be_sizelimit;   /* size limit for this backend             */
725         int     be_timelimit;   /* time limit for this backend             */
726         AccessControl *be_acl;  /* access control list for this backend    */
727         slap_access_t   be_dfltaccess;  /* access given if no acl matches          */
728         char    **be_replica;   /* replicas of this backend (in master)    */
729         char    *be_replogfile; /* replication log file (in master)        */
730         char    *be_update_ndn; /* allowed to make changes (in replicas) */
731         struct berval **be_update_refs; /* where to refer modifying clients to */
732         int     be_lastmod;     /* keep track of lastmodified{by,time}     */
733
734         char    *be_realm;
735
736         void    *be_private;    /* anything the backend database needs     */
737 };
738
739 struct slap_conn;
740 struct slap_op;
741
742 typedef int (*SLAP_EXTENDED_FN) LDAP_P((
743     Backend             *be,
744     struct slap_conn            *conn,
745     struct slap_op              *op,
746         char            *reqoid,
747     struct berval * reqdata,
748         char            **rspoid,
749     struct berval ** rspdata,
750         LDAPControl *** rspctrls,
751         char ** text,
752         struct berval *** refs ));
753
754 struct slap_backend_info {
755         char    *bi_type;       /* type of backend */
756
757         /*
758          * per backend type routines:
759          * bi_init: called to allocate a backend_info structure,
760          *              called once BEFORE configuration file is read.
761          *              bi_init() initializes this structure hence is
762          *              called directly from be_initialize()
763          * bi_config: called per 'backend' specific option
764          *              all such options must before any 'database' options
765          *              bi_config() is called only from read_config()
766          * bi_open: called to open each database, called
767          *              once AFTER configuration file is read but
768          *              BEFORE any bi_db_open() calls.
769          *              bi_open() is called from backend_startup()
770          * bi_close: called to close each database, called
771          *              once during shutdown after all bi_db_close calls.
772          *              bi_close() is called from backend_shutdown()
773          * bi_destroy: called to destroy each database, called
774          *              once during shutdown after all bi_db_destroy calls.
775          *              bi_destory() is called from backend_destroy()
776          */
777         int (*bi_init)  LDAP_P((BackendInfo *bi));
778         int     (*bi_config) LDAP_P((BackendInfo *bi,
779                 const char *fname, int lineno, int argc, char **argv ));
780         int (*bi_open) LDAP_P((BackendInfo *bi));
781         int (*bi_close) LDAP_P((BackendInfo *bi));
782         int (*bi_destroy) LDAP_P((BackendInfo *bi));
783
784         /*
785          * per database routines:
786          * bi_db_init: called to initialize each database,
787          *      called upon reading 'database <type>' 
788          *      called only from backend_db_init()
789          * bi_db_config: called to configure each database,
790          *  called per database to handle per database options
791          *      called only from read_config()
792          * bi_db_open: called to open each database
793          *      called once per database immediately AFTER bi_open()
794          *      calls but before daemon startup.
795          *  called only by backend_startup()
796          * bi_db_close: called to close each database
797          *      called once per database during shutdown but BEFORE
798          *  any bi_close call.
799          *  called only by backend_shutdown()
800          * bi_db_destroy: called to destroy each database
801          *  called once per database during shutdown AFTER all
802          *  bi_close calls but before bi_destory calls.
803          *  called only by backend_destory()
804          */
805         int (*bi_db_init) LDAP_P((Backend *bd));
806         int     (*bi_db_config) LDAP_P((Backend *bd,
807                 const char *fname, int lineno, int argc, char **argv ));
808         int (*bi_db_open) LDAP_P((Backend *bd));
809         int (*bi_db_close) LDAP_P((Backend *bd));
810         int (*bi_db_destroy) LDAP_P((Backend *db));
811
812         /* LDAP Operations Handling Routines */
813         int     (*bi_op_bind)  LDAP_P(( BackendDB *bd,
814                 struct slap_conn *c, struct slap_op *o,
815                 char *dn, char *ndn, int method, char* mechanism,
816                 struct berval *cred, char** edn ));
817         int (*bi_op_unbind) LDAP_P((BackendDB *bd,
818                 struct slap_conn *c, struct slap_op *o ));
819         int     (*bi_op_search) LDAP_P((BackendDB *bd,
820                 struct slap_conn *c, struct slap_op *o,
821                 char *base, char *nbase, int scope, int deref,
822                 int slimit, int tlimit,
823                 Filter *f, char *filterstr, char **attrs,
824                 int attrsonly));
825 #ifdef SLAPD_SCHEMA_NOT_COMPAT
826         int     (*bi_op_compare)LDAP_P((BackendDB *bd,
827                 struct slap_conn *c, struct slap_op *o,
828                 char *dn, char *ndn, AttributeAssertion *ava));
829 #else
830         int     (*bi_op_compare)LDAP_P((BackendDB *bd,
831                 struct slap_conn *c, struct slap_op *o,
832                 char *dn, char *ndn, Ava *ava));
833 #endif
834         int     (*bi_op_modify) LDAP_P((BackendDB *bd,
835                 struct slap_conn *c, struct slap_op *o,
836                 char *dn, char *ndn, Modifications *m));
837         int     (*bi_op_modrdn) LDAP_P((BackendDB *bd,
838                 struct slap_conn *c, struct slap_op *o,
839                 char *dn, char *ndn, char *newrdn, int deleteoldrdn,
840                 char *newSuperior));
841         int     (*bi_op_add)    LDAP_P((BackendDB *bd,
842                 struct slap_conn *c, struct slap_op *o,
843                 Entry *e));
844         int     (*bi_op_delete) LDAP_P((BackendDB *bd,
845                 struct slap_conn *c, struct slap_op *o,
846                 char *dn, char *ndn));
847         int     (*bi_op_abandon) LDAP_P((BackendDB *bd,
848                 struct slap_conn *c, struct slap_op *o,
849                 ber_int_t msgid));
850
851         /* Extended Operations Helper */
852         SLAP_EXTENDED_FN bi_extended;
853
854         /* Auxilary Functions */
855         int     (*bi_entry_release_rw) LDAP_P((BackendDB *bd, Entry *e, int rw));
856
857 #ifdef SLAPD_SCHEMA_NOT_COMPAT
858         int     (*bi_acl_group)  LDAP_P((Backend *bd,
859                 Entry *e, const char *bdn, const char *edn,
860                 const char *objectclassValue,
861                 AttributeDescription *group_at ));
862 #else
863         int     (*bi_acl_group)  LDAP_P((Backend *bd,
864                 Entry *e, const char *bdn, const char *edn,
865                 const char *objectclassValue,
866                 const char *group_at ));
867 #endif
868
869         int     (*bi_connection_init) LDAP_P((BackendDB *bd,
870                 struct slap_conn *c));
871         int     (*bi_connection_destroy) LDAP_P((BackendDB *bd,
872                 struct slap_conn *c));
873
874         /* hooks for slap tools */
875         int (*bi_tool_entry_open) LDAP_P(( BackendDB *be, int mode ));
876         int (*bi_tool_entry_close) LDAP_P(( BackendDB *be ));
877         ID (*bi_tool_entry_first) LDAP_P(( BackendDB *be ));
878         ID (*bi_tool_entry_next) LDAP_P(( BackendDB *be ));
879         Entry* (*bi_tool_entry_get) LDAP_P(( BackendDB *be, ID id ));
880         ID (*bi_tool_entry_put) LDAP_P(( BackendDB *be, Entry *e ));
881         int (*bi_tool_index_attr) LDAP_P(( BackendDB *be, char* type ));
882         int (*bi_tool_index_change) LDAP_P(( BackendDB *be, char* type,
883                 struct berval **bv, ID id, int op ));
884         int (*bi_tool_sync) LDAP_P(( BackendDB *be ));
885
886 #ifdef HAVE_CYRUS_SASL
887         int (*bi_sasl_authorize) LDAP_P(( BackendDB *be,
888                 const char *authnid, const char *authzid,
889                 const char **canon_authzid, const char **errstr ));
890         int (*bi_sasl_getsecret) LDAP_P(( BackendDB *be,
891                 const char *mechanism, const char *authzid,
892                 const char *realm, sasl_secret_t **secret ));
893         int (*bi_sasl_putsecret) LDAP_P(( BackendDB *be,
894                 const char *mechanism, const char *auth_identity,
895                 const char *realm, const sasl_secret_t *secret ));
896 #endif /* HAVE_CYRUS_SASL */
897
898 #define SLAP_INDEX_ADD_OP               0x0001
899 #define SLAP_INDEX_DELETE_OP    0x0002
900
901         char **bi_controls;             /* supported controls */
902
903         unsigned int bi_nDB;    /* number of databases of this type */
904         void    *bi_private;    /* anything the backend type needs */
905 };
906
907 /*
908  * represents an operation pending from an ldap client
909  */
910
911 typedef struct slap_op {
912         ber_int_t       o_opid;         /* id of this operation           */
913         ber_int_t       o_msgid;        /* msgid of the request           */
914
915         ldap_pvt_thread_t       o_tid;          /* thread handling this op        */
916
917         BerElement      *o_ber;         /* ber of the request             */
918
919         ber_tag_t       o_tag;          /* tag of the request             */
920         time_t          o_time;         /* time op was initiated          */
921
922         int             o_bind_in_progress;     /* multi-step bind in progress */
923 #ifdef SLAP_AUTHZID
924         /* should only be used for reporting purposes */
925         char    *o_authc_dn;    /* authentication DN */
926
927         /* should be used as the DN of the User */
928         char    *o_authz_dn;    /* authorization DN */
929         char    *o_authz_ndn;   /* authorizaiton NDN */
930
931 #else
932         char            *o_dn;          /* dn bound when op was initiated */
933         char            *o_ndn;         /* normalized dn bound when op was initiated */
934 #endif
935
936         ber_int_t       o_protocol;     /* version of the LDAP protocol used by client */
937         ber_tag_t       o_authtype;     /* auth method used to bind dn    */
938                                         /* values taken from ldap.h       */
939                                         /* LDAP_AUTH_*                    */
940         char            *o_authmech; /* SASL mechanism used to bind dn */
941
942         LDAPControl     **o_ctrls;       /* controls */
943
944         unsigned long   o_connid; /* id of conn initiating this op  */
945
946 #ifdef LDAP_CONNECTIONLESS
947         int             o_cldap;        /* != 0 if this came in via CLDAP */
948         struct sockaddr o_clientaddr;   /* client address if via CLDAP    */
949         char            o_searchbase;   /* search base if via CLDAP       */
950 #endif
951
952         ldap_pvt_thread_mutex_t o_abandonmutex; /* protects o_abandon  */
953         int             o_abandon;      /* abandon flag */
954
955         struct slap_op  *o_next;        /* next operation in list         */
956         void    *o_private;     /* anything the backend needs     */
957 } Operation;
958
959 /*
960  * represents a connection from an ldap client
961  */
962
963 typedef struct slap_conn {
964         int                     c_struct_state; /* structure management state */
965         int                     c_conn_state;   /* connection state */
966
967         ldap_pvt_thread_mutex_t c_mutex; /* protect the connection */
968         Sockbuf         *c_sb;                  /* ber connection stuff           */
969
970         /* only can be changed by connect_init */
971         time_t          c_starttime;    /* when the connection was opened */
972         time_t          c_activitytime; /* when the connection was last used */
973         unsigned long           c_connid;       /* id of this connection for stats*/
974
975         char            *c_listener_url;        /* listener URL */
976         char            *c_peer_domain; /* DNS name of client */
977         char            *c_peer_name;   /* peer name (trans=addr:port) */
978         char            *c_sock_name;   /* sock name (trans=addr:port) */
979
980         /* only can be changed by binding thread */
981         int             c_bind_in_progress;     /* multi-op bind in progress */
982 #ifdef HAVE_CYRUS_SASL
983         sasl_conn_t     *c_sasl_context;
984 #endif
985         void    *c_authstate;   /* SASL state data */
986
987         Backend *c_authc_backend;
988
989         /* authorization backend */
990         Backend *c_authz_backend;
991
992 #ifdef SLAP_AUTHZID
993         /* authentication backend */
994         /* should only be used for reporting purposes */
995         char    *c_authc_dn;    /* authentication DN */
996
997         /* should be used as the DN of the User */
998         char    *c_authz_dn;    /* authorization DN */
999         char    *c_authz_ndn;   /* authorization NDN */
1000
1001 #else
1002         char    *c_cdn;         /* DN provided by the client */
1003         char    *c_dn;          /* DN bound to this conn  */
1004 #endif
1005
1006         ber_int_t       c_protocol;     /* version of the LDAP protocol used by client */
1007         ber_tag_t       c_authtype;/* auth method used to bind c_dn  */
1008         char    *c_authmech;    /* SASL mechanism used to bind c_dn */
1009
1010         Operation       *c_ops;                 /* list of operations being processed */
1011         Operation       *c_pending_ops; /* list of pending operations */
1012
1013         ldap_pvt_thread_mutex_t c_write_mutex;  /* only one pdu written at a time */
1014         ldap_pvt_thread_cond_t  c_write_cv;             /* used to wait for sd write-ready*/
1015
1016         BerElement      *c_currentber;  /* ber we're attempting to read */
1017         int             c_writewaiter;  /* true if writer is waiting */
1018
1019 #ifdef HAVE_TLS
1020         int     c_is_tls;               /* true if this LDAP over raw TLS */
1021         int     c_needs_tls_accept;     /* true if SSL_accept should be called */
1022 #endif
1023
1024         long    c_n_ops_received;               /* num of ops received (next op_id) */
1025         long    c_n_ops_executing;      /* num of ops currently executing */
1026         long    c_n_ops_pending;                /* num of ops pending execution */
1027         long    c_n_ops_completed;      /* num of ops completed */
1028
1029         long    c_n_get;                /* num of get calls */
1030         long    c_n_read;               /* num of read calls */
1031         long    c_n_write;              /* num of write calls */
1032 } Connection;
1033
1034 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
1035 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )  \
1036         do { \
1037                 if ( ldap_debug & (level) ) \
1038                         fprintf( stderr, (fmt), (connid), (opid), (arg1), (arg2), (arg3) );\
1039                 if ( ldap_syslog & (level) ) \
1040                         syslog( ldap_syslog_level, (fmt), (connid), (opid), (arg1), \
1041                                 (arg2), (arg3) ); \
1042         } while (0)
1043 #else
1044 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )
1045 #endif
1046
1047 LDAP_END_DECL
1048
1049 #include "proto-slap.h"
1050
1051 #endif /* _slap_h_ */