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