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