]> git.sur5r.net Git - openldap/blob - servers/slapd/slap.h
More bind changes to support SASL/DIGEST.
[openldap] / servers / slapd / slap.h
1 /* slap.h - stand alone ldap server include file */
2
3 #ifndef _SLDAPD_H_
4 #define _SLDAPD_H_
5
6 #include <ac/stdlib.h>
7
8 #include <sys/types.h>
9 #include <ac/syslog.h>
10 #include <ac/regex.h>
11 #include <ac/socket.h>
12
13 #include "avl.h"
14
15 #ifndef ldap_debug
16 #define ldap_debug slap_debug
17 #endif
18
19
20 #include "ldap_log.h"
21
22 #include "lber.h"
23 #include "ldap.h"
24
25 #include "ldap_schema.h"
26
27 #include "ldap_pvt_thread.h"
28 #include "ldif.h"
29
30 LDAP_BEGIN_DECL
31
32 #ifdef f_next
33 #undef f_next /* name conflict between sys/file.h on SCO and struct filter */
34 #endif
35
36 #define SERVICE_NAME  OPENLDAP_PACKAGE "-slapd"
37
38 /* LDAPMod.mod_op value ===> Must be kept in sync with ldap.h!
39  *
40  * This is a value used internally by the backends. It is needed to allow
41  * adding values that already exist without getting an error as required by
42  * modrdn when the new rdn was already an attribute value itself.
43  * JCG 05/1999 (gomez@engr.sgi.com)
44  */
45 #define LDAP_MOD_SOFTADD        0x1000
46
47 #define DN_DNS  0
48 #define DN_X500 1
49
50 #define ON      1
51 #define OFF     (-1)
52 #define UNDEFINED 0
53
54 #define MAXREMATCHES 10
55
56 #define DNSEPARATOR(c)  ((c) == ',' || (c) == ';')
57 #define SEPARATOR(c)    ((c) == ',' || (c) == ';' || (c) == '+')
58 #define SPACE(c)        ((c) == ' ' || (c) == '\n')
59 #define NEEDSESCAPE(c)  ((c) == '\\' || (c) == '"')
60
61 #define SLAP_SCHERR_OUTOFMEM            1
62 #define SLAP_SCHERR_CLASS_NOT_FOUND     2
63 #define SLAP_SCHERR_ATTR_NOT_FOUND      3
64 #define SLAP_SCHERR_DUP_CLASS           4
65 #define SLAP_SCHERR_DUP_ATTR            5
66 #define SLAP_SCHERR_DUP_SYNTAX          6
67 #define SLAP_SCHERR_DUP_RULE            7
68 #define SLAP_SCHERR_NO_NAME             8
69 #define SLAP_SCHERR_ATTR_INCOMPLETE     9
70 #define SLAP_SCHERR_MR_NOT_FOUND        10
71 #define SLAP_SCHERR_SYN_NOT_FOUND       11
72 #define SLAP_SCHERR_MR_INCOMPLETE       12
73
74 extern int slap_debug;
75
76 struct slap_op;
77 struct slap_conn;
78
79 /*
80  * represents an attribute value assertion (i.e., attr=value)
81  */
82 typedef struct ava {
83         char            *ava_type;
84         struct berval   ava_value;
85 } Ava;
86
87 /*
88  * represents a search filter
89  */
90 typedef struct filter {
91         ber_tag_t       f_choice;       /* values taken from ldap.h */
92
93         union f_un_u {
94                 /* present */
95                 char            *f_un_type;
96
97                 /* equality, lessorequal, greaterorequal, approx */
98                 Ava             f_un_ava;
99
100                 /* and, or, not */
101                 struct filter   *f_un_complex;
102
103                 /* substrings */
104                 struct sub {
105                         char    *f_un_sub_type;
106                         char    *f_un_sub_initial;
107                         char    **f_un_sub_any;
108                         char    *f_un_sub_final;
109                 } f_un_sub;
110         } f_un;
111 #define f_type          f_un.f_un_type
112 #define f_ava           f_un.f_un_ava
113 #define f_avtype        f_un.f_un_ava.ava_type
114 #define f_avvalue       f_un.f_un_ava.ava_value
115 #define f_and           f_un.f_un_complex
116 #define f_or            f_un.f_un_complex
117 #define f_not           f_un.f_un_complex
118 #define f_list          f_un.f_un_complex
119 #define f_sub           f_un.f_un_sub
120 #define f_sub_type      f_un.f_un_sub.f_un_sub_type
121 #define f_sub_initial   f_un.f_un_sub.f_un_sub_initial
122 #define f_sub_any       f_un.f_un_sub.f_un_sub_any
123 #define f_sub_final     f_un.f_un_sub.f_un_sub_final
124
125         struct filter   *f_next;
126 } Filter;
127
128 /*
129  * represents an attribute (type + values + syntax)
130  */
131 typedef struct attr {
132         char            *a_type;
133         struct berval   **a_vals;
134         int             a_syntax;
135         struct attr     *a_next;
136 } Attribute;
137
138 /*
139  * the attr_syntax() routine returns one of these values
140  * telling what kind of syntax an attribute supports.
141  */
142 #define SYNTAX_CIS      0x01    /* case insensitive string              */
143 #define SYNTAX_CES      0x02    /* case sensitive string                */
144 #define SYNTAX_BIN      0x04    /* binary data                          */
145 #define SYNTAX_TEL      0x08    /* telephone number string              */
146 #define SYNTAX_DN       0x10    /* dn string                            */
147
148 /*
149  * the id used in the indexes to refer to an entry
150  */
151 typedef unsigned long   ID;
152 #define NOID    ((unsigned long)~0)
153
154 /*
155  * represents an entry in core
156  */
157 typedef struct entry {
158         /*
159          * The ID field should only be changed before entry is
160          * inserted into a cache.  The ID value is backend
161          * specific.
162          */
163         ID              e_id;
164
165         char            *e_dn;          /* DN of this entry */
166         char            *e_ndn;         /* normalized DN of this entry */
167         Attribute       *e_attrs;       /* list of attributes + values */
168
169         /* for use by the backend for any purpose */
170         void*   e_private;
171 } Entry;
172
173 /*
174  * represents an access control list
175  */
176
177 /* the "by" part */
178 struct access {
179 #define ACL_NONE        0x01
180 #define ACL_COMPARE     0x02
181 #define ACL_SEARCH      0x04
182 #define ACL_READ        0x08
183 #define ACL_WRITE       0x10
184 #define ACL_SELF        0x40
185         int                     a_access;
186
187         char            *a_dnpat;
188         char            *a_addrpat;
189         char            *a_domainpat;
190         char            *a_dnattr;
191
192 #ifdef SLAPD_ACLGROUPS
193         char            *a_group;
194         char            *a_objectclassvalue;
195         char            *a_groupattrname;
196 #endif
197         struct access   *a_next;
198 };
199
200 /* the "to" part */
201 struct acl {
202         /* "to" part: the entries this acl applies to */
203         Filter          *acl_filter;
204         regex_t         acl_dnre;
205         char            *acl_dnpat;
206         char            **acl_attrs;
207
208         /* "by" part: list of who has what access to the entries */
209         struct access   *acl_access;
210
211         struct acl      *acl_next;
212 };
213
214 /*
215  * A list of LDAPMods
216  */
217 typedef struct ldapmodlist {
218         struct ldapmod ml_mod;
219         struct ldapmodlist *ml_next;
220 #define ml_op           ml_mod.mod_op
221 #define ml_type         ml_mod.mod_type
222 #define ml_values       ml_mod.mod_values
223 #define ml_bvalues      ml_mod.mod_bvalues
224 } LDAPModList;
225
226 /*
227  * represents schema information for a database
228  */
229 typedef int slap_syntax_check_func LDAP_P((struct berval * val));
230
231 typedef struct slap_syntax {
232         LDAP_SYNTAX                     ssyn_syn;
233         slap_syntax_check_func          *ssyn_check;
234         struct slap_syntax              *ssyn_next;
235 } Syntax;
236 #define ssyn_oid                        ssyn_syn.syn_oid
237 #define ssyn_desc                       ssyn_syn.syn_desc
238
239 typedef int slap_mr_normalize_func LDAP_P((struct berval * val, struct berval **normalized));
240 typedef int slap_mr_compare_func LDAP_P((struct berval * val1, struct berval * val2));
241
242 typedef struct slap_matching_rule {
243         LDAP_MATCHING_RULE              smr_mrule;
244         slap_mr_normalize_func          *smr_normalize;
245         slap_mr_compare_func            *smr_compare;
246         Syntax                          *smr_syntax;
247         struct slap_matching_rule       *smr_next;
248 } MatchingRule;
249 #define smr_oid                         smr_mrule.mr_oid
250 #define smr_names                       smr_mrule.mr_names
251 #define smr_desc                        smr_mrule.mr_desc
252 #define smr_obsolete                    smr_mrule.mr_obsolete
253 #define smr_syntax_oid                  smr_mrule.mr_syntax_oid
254
255 typedef struct slap_attribute_type {
256         LDAP_ATTRIBUTE_TYPE             sat_atype;
257         struct slap_attribute_type      *sat_sup;
258         struct slap_attribute_type      **sat_subtypes;
259         MatchingRule                    *sat_equality;
260         MatchingRule                    *sat_ordering;
261         MatchingRule                    *sat_substr;
262         Syntax                          *sat_syntax;
263         /* The next one is created to help in the transition */
264         int                             sat_syntax_compat;
265         struct slap_attribute_type      *sat_next;
266 } AttributeType;
267 #define sat_oid                 sat_atype.at_oid
268 #define sat_names               sat_atype.at_names
269 #define sat_desc                sat_atype.at_desc
270 #define sat_obsolete            sat_atype.at_obsolete
271 #define sat_sup_oid             sat_atype.at_sup_oid
272 #define sat_equality_oid        sat_atype.at_equality_oid
273 #define sat_ordering_oid        sat_atype.at_ordering_oid
274 #define sat_substr_oid          sat_atype.at_substr_oid
275 #define sat_syntax_oid          sat_atype.at_syntax_oid
276 #define sat_syntax_len          sat_atype.at_syntax_len
277 #define sat_single_value        sat_atype.at_single_value
278 #define sat_collective          sat_atype.at_collective
279 #define sat_no_user_mods        sat_atype.at_no_user_mods
280 #define sat_usage               sat_atype.at_usage
281
282 typedef struct slap_object_class {
283         LDAP_OBJECT_CLASS               soc_oclass;
284         struct slap_object_class        **soc_sups;
285         AttributeType                   **soc_required;
286         AttributeType                   **soc_allowed;
287         struct slap_object_class        *soc_next;
288 } ObjectClass;
289 #define soc_oid                 soc_oclass.oc_oid
290 #define soc_names               soc_oclass.oc_names
291 #define soc_desc                soc_oclass.oc_desc
292 #define soc_obsolete            soc_oclass.oc_obsolete
293 #define soc_sup_oids            soc_oclass.oc_sup_oids
294 #define soc_kind                soc_oclass.oc_kind
295 #define soc_at_oids_must        soc_oclass.oc_at_oids_must
296 #define soc_at_oids_may         soc_oclass.oc_at_oids_may
297
298 /*
299  * Backend-info
300  * represents a backend 
301  */
302
303 typedef struct backend_info BackendInfo;        /* per backend type */
304 typedef struct backend_db BackendDB;            /* per backend database */
305
306 extern int nBackendInfo;
307 extern int nBackendDB;
308 extern BackendInfo      *backendInfo;
309 extern BackendDB        *backendDB;
310
311 extern int                      slapMode;       
312 #define SLAP_UNDEFINED_MODE     0
313 #define SLAP_SERVER_MODE        1
314 #define SLAP_TOOL_MODE          2
315 #ifdef SLAPD_BDB2
316 #  define SLAP_TIMEDSERVER_MODE  3
317 #  define SLAP_TOOLID_MODE       4
318 #endif
319
320 /* temporary aliases */
321 typedef BackendDB Backend;
322 #define nbackends nBackendDB
323 #define backends backendDB
324
325 struct backend_db {
326         BackendInfo     *bd_info;       /* pointer to shared backend info */
327
328         /* BackendInfo accessors */
329 #define         be_config       bd_info->bi_db_config
330 #define         be_type         bd_info->bi_type
331
332 #define         be_bind         bd_info->bi_op_bind
333 #define         be_unbind       bd_info->bi_op_unbind
334 #define         be_add          bd_info->bi_op_add
335 #define         be_compare      bd_info->bi_op_compare
336 #define         be_delete       bd_info->bi_op_delete
337 #define         be_modify       bd_info->bi_op_modify
338 #define         be_modrdn       bd_info->bi_op_modrdn
339 #define         be_search       bd_info->bi_op_search
340
341 #define         be_release      bd_info->bi_entry_release_rw
342 #define         be_group        bd_info->bi_acl_group
343
344 #define         be_connection_init      bd_info->bi_connection_init
345 #define         be_connection_destroy   bd_info->bi_connection_destroy
346
347
348         /* these should be renamed from be_ to bd_ */
349         char    **be_suffix;    /* the DN suffixes of data in this backend */
350         char    **be_nsuffix;   /* the normalized DN suffixes in this backend */
351         char    **be_suffixAlias;       /* the DN suffix aliases of data in this backend */
352         char    *be_root_dn;    /* the magic "root" dn for this db      */
353         char    *be_root_ndn;   /* the magic "root" normalized dn for this db   */
354         char    *be_root_pw;    /* the magic "root" password for this db        */
355         int     be_readonly;    /* 1 => db is in "read only" mode          */
356         int     be_maxDerefDepth;       /* limit for depth of an alias deref  */
357         int     be_sizelimit;   /* size limit for this backend             */
358         int     be_timelimit;   /* time limit for this backend             */
359         struct acl *be_acl;     /* access control list for this backend    */
360         int     be_dfltaccess;  /* access given if no acl matches          */
361         char    **be_replica;   /* replicas of this backend (in master)    */
362         char    *be_replogfile; /* replication log file (in master)        */
363         char    *be_update_ndn; /* allowed to make changes (in replicas)   */
364         int     be_lastmod;     /* keep track of lastmodified{by,time}     */
365
366         char    *be_realm;
367
368         void    *be_private;    /* anything the backend database needs     */
369 };
370
371 struct backend_info {
372         char    *bi_type;       /* type of backend */
373
374         /*
375          * per backend type routines:
376          * bi_init: called to allocate a backend_info structure,
377          *              called once BEFORE configuration file is read.
378          *              bi_init() initializes this structure hence is
379          *              called directly from be_initialize()
380          * bi_config: called per 'backend' specific option
381          *              all such options must before any 'database' options
382          *              bi_config() is called only from read_config()
383          * bi_open: called to open each database, called
384          *              once AFTER configuration file is read but
385          *              BEFORE any bi_db_open() calls.
386          *              bi_open() is called from backend_startup()
387          * bi_close: called to close each database, called
388          *              once during shutdown after all bi_db_close calls.
389          *              bi_close() is called from backend_shutdown()
390          * bi_destroy: called to destroy each database, called
391          *              once during shutdown after all bi_db_destroy calls.
392          *              bi_destory() is called from backend_destroy()
393          */
394         int (*bi_init)  LDAP_P((BackendInfo *bi));
395         int     (*bi_config) LDAP_P((BackendInfo *bi,
396                 char *fname, int lineno, int argc, char **argv ));
397         int (*bi_open) LDAP_P((BackendInfo *bi));
398         int (*bi_close) LDAP_P((BackendInfo *bi));
399         int (*bi_destroy) LDAP_P((BackendInfo *bi));
400
401         /*
402          * per database routines:
403          * bi_db_init: called to initialize each database,
404          *      called upon reading 'database <type>' 
405          *      called only from backend_db_init()
406          * bi_db_config: called to configure each database,
407          *  called per database to handle per database options
408          *      called only from read_config()
409          * bi_db_open: called to open each database
410          *      called once per database immediately AFTER bi_open()
411          *      calls but before daemon startup.
412          *  called only by backend_startup()
413          * bi_db_close: called to close each database
414          *      called once per database during shutdown but BEFORE
415          *  any bi_close call.
416          *  called only by backend_shutdown()
417          * bi_db_destroy: called to destroy each database
418          *  called once per database during shutdown AFTER all
419          *  bi_close calls but before bi_destory calls.
420          *  called only by backend_destory()
421          */
422         int (*bi_db_init) LDAP_P((Backend *bd));
423         int     (*bi_db_config) LDAP_P((Backend *bd,
424                 char *fname, int lineno, int argc, char **argv ));
425         int (*bi_db_open) LDAP_P((Backend *bd));
426         int (*bi_db_close) LDAP_P((Backend *bd));
427         int (*bi_db_destroy) LDAP_P((Backend *db));
428
429         /* LDAP Operations Handling Routines */
430         int     (*bi_op_bind)  LDAP_P(( BackendDB *bd,
431                 struct slap_conn *c, struct slap_op *o,
432                 char *dn, int method, char* mechanism,
433                 struct berval *cred, char** edn ));
434         int (*bi_op_unbind) LDAP_P((BackendDB *bd,
435                 struct slap_conn *c, struct slap_op *o ));
436         int     (*bi_op_search) LDAP_P((BackendDB *bd,
437                 struct slap_conn *c, struct slap_op *o,
438                 char *base, int scope, int deref,
439                 int slimit, int tlimit,
440                 Filter *f, char *filterstr, char **attrs,
441                 int attrsonly));
442         int     (*bi_op_compare)LDAP_P((BackendDB *bd,
443                 struct slap_conn *c, struct slap_op *o,
444                 char *dn, Ava *ava));
445         int     (*bi_op_modify) LDAP_P((BackendDB *bd,
446                 struct slap_conn *c, struct slap_op *o,
447                 char *dn, LDAPModList *m));
448         int     (*bi_op_modrdn) LDAP_P((BackendDB *bd,
449                 struct slap_conn *c, struct slap_op *o,
450                 char *dn, char *newrdn, int deleteoldrdn,
451                 char *newSuperior));
452         int     (*bi_op_add)    LDAP_P((BackendDB *bd,
453                 struct slap_conn *c, struct slap_op *o,
454                 Entry *e));
455         int     (*bi_op_delete) LDAP_P((BackendDB *bd,
456                 struct slap_conn *c, struct slap_op *o,
457                 char *dn));
458         /* Bug: be_op_abandon in unused! */
459         int     (*bi_op_abandon) LDAP_P((BackendDB *bd,
460                 struct slap_conn *c, struct slap_op *o,
461                 ber_int_t msgid));
462
463         /* Auxilary Functions */
464         int     (*bi_entry_release_rw) LDAP_P((BackendDB *bd, Entry *e, int rw));
465 #ifdef SLAPD_ACLGROUPS
466         int     (*bi_acl_group)  LDAP_P((Backend *bd,
467                 Entry *e, char *bdn, char *edn,
468                 char *objectclassValue, char *groupattrName ));
469 #endif
470
471         int     (*bi_connection_init) LDAP_P((BackendDB *bd,
472                 struct slap_conn *c));
473         int     (*bi_connection_destroy) LDAP_P((BackendDB *bd,
474                 struct slap_conn *c));
475
476
477         unsigned int bi_nDB;    /* number of databases of this type */
478         void    *bi_private;    /* anything the backend type needs */
479 };
480
481 /*
482  * represents an operation pending from an ldap client
483  */
484
485 typedef struct slap_op {
486         ber_int_t       o_opid;         /* id of this operation           */
487         ber_int_t       o_msgid;        /* msgid of the request           */
488
489         ldap_pvt_thread_t       o_tid;          /* thread handling this op        */
490
491         BerElement      *o_ber;         /* ber of the request             */
492
493         ber_tag_t       o_tag;          /* tag of the request             */
494         time_t          o_time;         /* time op was initiated          */
495
496         int             o_bind_in_progress;     /* multi-op bind in progress */
497
498         char            *o_dn;          /* dn bound when op was initiated */
499         char            *o_ndn;         /* normalized dn bound when op was initiated */
500         ber_int_t       o_protocol;     /* version of the LDAP protocol used by client */
501         ber_tag_t       o_authtype;     /* auth method used to bind dn    */
502                                         /* values taken from ldap.h       */
503                                         /* LDAP_AUTH_*                    */
504         char            *o_authmech; /* SASL mechanism used to bind dn */
505
506         LDAPControl     **o_ctrls;       /* controls */
507
508 /*       long   o_connid;       *//* id of conn initiating this op  */
509
510 #ifdef LDAP_CONNECTIONLESS
511         int             o_cldap;        /* != 0 if this came in via CLDAP */
512         struct sockaddr o_clientaddr;   /* client address if via CLDAP    */
513         char            o_searchbase;   /* search base if via CLDAP       */
514 #endif
515
516         ldap_pvt_thread_mutex_t o_abandonmutex; /* protects o_abandon  */
517         int             o_abandon;      /* abandon flag */
518
519         struct slap_op  *o_next;        /* next operation in list         */
520         void    *o_private;     /* anything the backend needs     */
521 } Operation;
522
523 /*
524  * represents a connection from an ldap client
525  */
526
527 typedef struct slap_conn {
528         int                     c_struct_state; /* structure management state */
529         int                     c_conn_state;   /* connection state */
530
531         ldap_pvt_thread_mutex_t c_mutex; /* protect the connection */
532         Sockbuf         *c_sb;                  /* ber connection stuff           */
533
534         /* only can be changed by connect_init */
535         time_t          c_starttime;    /* when the connection was opened */
536         time_t          c_activitytime; /* when the connection was last used */
537         unsigned long           c_connid;       /* id of this connection for stats*/
538         char            *c_client_addr; /* address of client */
539         char            *c_client_name; /* name of client */
540
541         /* only can be changed by binding thread */
542         int             c_bind_in_progress;     /* multi-op bind in progress */
543
544         char    *c_cdn;         /* DN provided by the client */
545         char    *c_dn;          /* DN bound to this conn  */
546         ber_int_t       c_protocol;     /* version of the LDAP protocol used by client */
547         ber_tag_t       c_authtype;/* auth method used to bind c_dn  */
548         char    *c_authmech;    /* SASL mechanism used to bind c_dn */
549         void    *c_authstate;   /* SASL state data */
550
551         Operation       *c_ops;                 /* list of operations being processed */
552         Operation       *c_pending_ops; /* list of pending operations */
553
554         ldap_pvt_thread_mutex_t c_write_mutex;  /* only one pdu written at a time */
555         ldap_pvt_thread_cond_t  c_write_cv;             /* used to wait for sd write-ready*/
556
557         BerElement      *c_currentber;  /* ber we're attempting to read */
558         int             c_writewaiter;  /* true if writer is waiting */
559
560         long    c_n_ops_received;               /* num of ops received (next op_id) */
561         long    c_n_ops_executing;      /* num of ops currently executing */
562         long    c_n_ops_pending;                /* num of ops pending execution */
563         long    c_n_ops_completed;      /* num of ops completed */
564
565         long    c_n_get;                /* num of get calls */
566         long    c_n_read;               /* num of read calls */
567         long    c_n_write;              /* num of write calls */
568 } Connection;
569
570 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
571 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )  \
572         do { \
573                 if ( ldap_debug & (level) ) \
574                         fprintf( stderr, (fmt), (connid), (opid), (arg1), (arg2), (arg3) );\
575                 if ( ldap_syslog & (level) ) \
576                         syslog( ldap_syslog_level, (fmt), (connid), (opid), (arg1), \
577                                 (arg2), (arg3) ); \
578         } while (0)
579 #else
580 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )
581 #endif
582
583 LDAP_END_DECL
584
585 #include "proto-slap.h"
586
587 #endif /* _slap_h_ */