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