]> git.sur5r.net Git - openldap/blob - servers/slapd/slap.h
10f0135bf2c5ae98a5b0db9db8b88c39f400d945
[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 <stdlib.h>
7
8 #ifndef LDAP_SYSLOG
9 #define LDAP_SYSLOG 1
10 #endif
11
12 #include <sys/types.h>
13 #include <ac/syslog.h>
14 #include <ac/regex.h>
15
16 #undef NDEBUG
17 #include <assert.h>
18
19 #include "avl.h"
20
21 #ifndef ldap_debug
22 #define ldap_debug slap_debug
23 #endif
24
25 #include "ldap_log.h"
26
27 #include "../../libraries/liblber/lber-int.h"
28 #include "ldap.h"
29
30 #include "ldap_pvt_thread.h"
31 #include "ldif.h"
32 #ifdef f_next
33 #undef f_next /* name conflict between sys/file.h on SCO and struct filter */
34 #endif
35
36 #define DN_DNS  0
37 #define DN_X500 1
38
39 #define ON      1
40 #define OFF     (-1)
41 #define UNDEFINED 0
42
43 #define MAXREMATCHES 10
44
45 #define DNSEPARATOR(c)  ((c) == ',' || (c) == ';')
46 #define SEPARATOR(c)    ((c) == ',' || (c) == ';' || (c) == '+')
47 #define SPACE(c)        ((c) == ' ' || (c) == '\n')
48 #define NEEDSESCAPE(c)  ((c) == '\\' || (c) == '"')
49
50 LDAP_BEGIN_DECL
51
52 extern int slap_debug;
53
54 struct slap_op;
55 struct slap_conn;
56
57 /*
58  * represents an attribute value assertion (i.e., attr=value)
59  */
60 typedef struct ava {
61         char            *ava_type;
62         struct berval   ava_value;
63 } Ava;
64
65 /*
66  * represents a search filter
67  */
68 typedef struct filter {
69         unsigned long   f_choice;       /* values taken from ldap.h */
70
71         union {
72                 /* present */
73                 char            *f_un_type;
74
75                 /* equality, lessorequal, greaterorequal, approx */
76                 Ava             f_un_ava;
77
78                 /* and, or, not */
79                 struct filter   *f_un_complex;
80
81                 /* substrings */
82                 struct sub {
83                         char    *f_un_sub_type;
84                         char    *f_un_sub_initial;
85                         char    **f_un_sub_any;
86                         char    *f_un_sub_final;
87                 } f_un_sub;
88         } f_un;
89 #define f_type          f_un.f_un_type
90 #define f_ava           f_un.f_un_ava
91 #define f_avtype        f_un.f_un_ava.ava_type
92 #define f_avvalue       f_un.f_un_ava.ava_value
93 #define f_and           f_un.f_un_complex
94 #define f_or            f_un.f_un_complex
95 #define f_not           f_un.f_un_complex
96 #define f_list          f_un.f_un_complex
97 #define f_sub           f_un.f_un_sub
98 #define f_sub_type      f_un.f_un_sub.f_un_sub_type
99 #define f_sub_initial   f_un.f_un_sub.f_un_sub_initial
100 #define f_sub_any       f_un.f_un_sub.f_un_sub_any
101 #define f_sub_final     f_un.f_un_sub.f_un_sub_final
102
103         struct filter   *f_next;
104 } Filter;
105
106 /*
107  * represents an attribute (type + values + syntax)
108  */
109 typedef struct attr {
110         char            *a_type;
111         struct berval   **a_vals;
112         int             a_syntax;
113         struct attr     *a_next;
114 } Attribute;
115
116 /*
117  * the attr_syntax() routine returns one of these values
118  * telling what kind of syntax an attribute supports.
119  */
120 #define SYNTAX_CIS      0x01    /* case insensitive string              */
121 #define SYNTAX_CES      0x02    /* case sensitive string                */
122 #define SYNTAX_BIN      0x04    /* binary data                          */
123 #define SYNTAX_TEL      0x08    /* telephone number string              */
124 #define SYNTAX_DN       0x10    /* dn string                            */
125
126 /*
127  * the id used in the indexes to refer to an entry
128  */
129 typedef unsigned long   ID;
130 #define NOID    ((unsigned long)-1)
131
132 /*
133  * represents an entry in core
134  */
135 typedef struct entry {
136         ldap_pvt_thread_rdwr_t  e_rdwr; /* reader/writer lock */
137
138         char            *e_dn;          /* DN of this entry               */
139         char            *e_ndn;         /* normalized DN of this entry    */
140         Attribute       *e_attrs;       /* list of attributes + values    */
141
142
143         /*
144          * The ID field should only be changed before entry is
145          * inserted into a cache.  The ID value is backend
146          * specific.
147          */
148         ID              e_id;           /* id of this entry - this should */
149                                         /* really be private to back-ldbm */
150
151         /*
152          * remaining fields require backend cache lock to access
153          * These items are specific to the LDBM backend and should
154          * and should be hidden.
155          */
156         char            e_state;        /* for the cache                  */
157 #define ENTRY_STATE_DELETED             1
158 #define ENTRY_STATE_CREATING    2
159
160         int             e_refcnt;       /* # threads ref'ing this entry   */
161         struct entry    *e_lrunext;     /* for cache lru list             */
162         struct entry    *e_lruprev;
163 } Entry;
164
165 /*
166  * represents an access control list
167  */
168
169 /* the "by" part */
170 struct access {
171 #define ACL_NONE        0x01
172 #define ACL_COMPARE     0x02
173 #define ACL_SEARCH      0x04
174 #define ACL_READ        0x08
175 #define ACL_WRITE       0x10
176 #define ACL_SELF        0x40
177         int                     a_access;
178
179         char            *a_dnpat;
180         char            *a_addrpat;
181         char            *a_domainpat;
182         char            *a_dnattr;
183
184 #ifdef SLAPD_ACLGROUPS
185         char            *a_group;
186         char            *a_objectclassvalue;
187         char            *a_groupattrname;
188 #endif
189         struct access   *a_next;
190 };
191
192 /* the "to" part */
193 struct acl {
194         /* "to" part: the entries this acl applies to */
195         Filter          *acl_filter;
196         regex_t         acl_dnre;
197         char            *acl_dnpat;
198         char            **acl_attrs;
199
200         /* "by" part: list of who has what access to the entries */
201         struct access   *acl_access;
202
203         struct acl      *acl_next;
204 };
205
206 /*
207  * A list of LDAPMods
208  */
209 typedef struct ldapmodlist {
210         struct ldapmod ml_mod;
211         struct ldapmodlist *ml_next;
212 #define ml_op           ml_mod.mod_op
213 #define ml_type         ml_mod.mod_type
214 #define ml_values       ml_mod.mod_values
215 #define ml_bvalues      ml_mod.mod_bvalues
216 } LDAPModList;
217
218 /*
219  * represents schema information for a database
220  */
221
222 struct objclass {
223         char            *oc_name;
224         char            **oc_required;
225         char            **oc_allowed;
226         struct objclass *oc_next;
227 };
228
229 /*
230  * Backend-info
231  * represents a backend 
232  */
233
234 typedef struct backend_info BackendInfo;        /* per backend type */
235 typedef struct backend_db BackendDB;            /* per backend database */
236
237 extern int nBackendInfo;
238 extern int nBackendDB;
239 extern BackendInfo      *backendInfo;
240 extern BackendDB        *backendDB;
241
242 extern int                      slapMode;       
243 #define SLAP_UNDEFINED_MODE     0
244 #define SLAP_SERVER_MODE        1
245 #define SLAP_TOOL_MODE          2
246
247 /* temporary aliases */
248 typedef BackendDB Backend;
249 #define nbackends nBackendDB
250 #define backends backendDB
251
252 struct backend_db {
253         BackendInfo     *bd_info;       /* pointer to shared backend info */
254
255         /* BackendInfo accessors */
256 #define         be_config       bd_info->bi_db_config
257 #define         be_type         bd_info->bi_type
258
259 #define         be_bind         bd_info->bi_op_bind
260 #define         be_unbind       bd_info->bi_op_unbind
261 #define         be_add          bd_info->bi_op_add
262 #define         be_compare      bd_info->bi_op_compare
263 #define         be_delete       bd_info->bi_op_delete
264 #define         be_modify       bd_info->bi_op_modify
265 #define         be_modrdn       bd_info->bi_op_modrdn
266 #define         be_search       bd_info->bi_op_search
267
268 #define         be_group        bd_info->bi_acl_group
269
270         /* these should be renamed from be_ to bd_ */
271         char    **be_suffix;    /* the DN suffixes of data in this backend */
272         char    **be_suffixAlias;       /* the DN suffix aliases of data in this backend */
273         char    *be_root_dn;    /* the magic "root" dn for this db      */
274         char    *be_root_ndn;   /* the magic "root" normalized dn for this db   */
275         char    *be_root_pw;    /* the magic "root" password for this db        */
276         int     be_readonly;    /* 1 => db is in "read only" mode          */
277         int     be_maxDerefDepth;       /* limit for depth of an alias deref  */
278         int     be_sizelimit;   /* size limit for this backend             */
279         int     be_timelimit;   /* time limit for this backend             */
280         struct acl *be_acl;     /* access control list for this backend    */
281         int     be_dfltaccess;  /* access given if no acl matches          */
282         char    **be_replica;   /* replicas of this backend (in master)    */
283         char    *be_replogfile; /* replication log file (in master)        */
284         char    *be_update_ndn; /* allowed to make changes (in replicas)   */
285         int     be_lastmod;     /* keep track of lastmodified{by,time}     */
286
287         void    *be_private;    /* anything the backend needs              */
288 };
289
290 struct backend_info {
291         char    *bi_type;       /* type of backend */
292
293         /*
294          * per backend type routines:
295          * bi_init: called to allocate a backend_info structure,
296          *              called once BEFORE configuration file is read.
297          *              bi_init() initializes this structure hence is
298          *              called directly from be_initialize()
299          * bi_config: called per 'backend' specific option
300          *              all such options must before any 'database' options
301          *              bi_config() is called only from read_config()
302          * bi_open: called to open each database, called
303          *              once AFTER configuration file is read but
304          *              BEFORE any bi_db_open() calls.
305          *              bi_open() is called from backend_startup()
306          * bi_close: called to close each database, called
307          *              once during shutdown after all bi_db_close calls.
308          *              bi_close() is called from backend_shutdown()
309          * bi_destroy: called to destroy each database, called
310          *              once during shutdown after all bi_db_destroy calls.
311          *              bi_destory() is called from backend_destroy()
312          */
313         int (*bi_init)  LDAP_P((BackendInfo *bi));
314         int     (*bi_config) LDAP_P((BackendInfo *bi,
315                 char *fname, int lineno, int argc, char **argv ));
316         int (*bi_open) LDAP_P((BackendInfo *bi));
317         int (*bi_close) LDAP_P((BackendInfo *bi));
318         int (*bi_destroy) LDAP_P((BackendInfo *bi));
319
320         /*
321          * per database routines:
322          * bi_db_init: called to initialize each database,
323          *      called upon reading 'database <type>' 
324          *      called only from backend_db_init()
325          * bi_db_config: called to configure each database,
326          *  called per database to handle per database options
327          *      called only from read_config()
328          * bi_db_open: called to open each database
329          *      called once per database immediately AFTER bi_open()
330          *      calls but before daemon startup.
331          *  called only by backend_startup()
332          * bi_db_close: called to close each database
333          *      called once per database during shutdown but BEFORE
334          *  any bi_close call.
335          *  called only by backend_shutdown()
336          * bi_db_destroy: called to destroy each database
337          *  called once per database during shutdown AFTER all
338          *  bi_close calls but before bi_destory calls.
339          *  called only by backend_destory()
340          */
341         int (*bi_db_init) LDAP_P((Backend *bd));
342         int     (*bi_db_config) LDAP_P((Backend *bd,
343                 char *fname, int lineno, int argc, char **argv ));
344         int (*bi_db_open) LDAP_P((Backend *bd));
345         int (*bi_db_close) LDAP_P((Backend *bd));
346         int (*bi_db_destroy) LDAP_P((Backend *db));
347
348         /* LDAP Operations Handling Routines */
349         int     (*bi_op_bind)  LDAP_P(( BackendDB *bd,
350                 struct slap_conn *c, struct slap_op *o,
351                 char *dn, int method, struct berval *cred, char** edn ));
352         int (*bi_op_unbind) LDAP_P((BackendDB *bd,
353                 struct slap_conn *c, struct slap_op *o ));
354         int     (*bi_op_search) LDAP_P((BackendDB *bd,
355                 struct slap_conn *c, struct slap_op *o,
356                 char *base, int scope, int deref, int slimit, int tlimit,
357                 Filter *f, char *filterstr, char **attrs, int attrsonly));
358         int     (*bi_op_compare)LDAP_P((BackendDB *bd,
359                 struct slap_conn *c, struct slap_op *o,
360                 char *dn, Ava *ava));
361         int     (*bi_op_modify) LDAP_P((BackendDB *bd,
362                 struct slap_conn *c, struct slap_op *o,
363                 char *dn, LDAPModList *m));
364         int     (*bi_op_modrdn) LDAP_P((BackendDB *bd,
365                 struct slap_conn *c, struct slap_op *o,
366                 char *dn, char *newrdn, int deleteoldrdn ));
367         int     (*bi_op_add)    LDAP_P((BackendDB *bd,
368                 struct slap_conn *c, struct slap_op *o,
369                 Entry *e));
370         int     (*bi_op_delete) LDAP_P((BackendDB *bd,
371                 struct slap_conn *c, struct slap_op *o,
372                 char *dn));
373         /* Bug: be_op_abandon in unused! */
374         int     (*bi_op_abandon) LDAP_P((BackendDB *bd,
375                 struct slap_conn *c, struct slap_op *o,
376                 int msgid));
377
378         /* Auxilary Functions */
379 #ifdef SLAPD_ACLGROUPS
380         int     (*bi_acl_group)  LDAP_P((Backend *bd,
381                 Entry *e, char *bdn, char *edn,
382                 char *objectclassValue, char *groupattrName ));
383 #endif
384
385         void    *bi_private;    /* anything the backend needs */
386 };
387
388 /*
389  * represents an operation pending from an ldap client
390  */
391
392 typedef struct slap_op {
393         BerElement      *o_ber;         /* ber of the request             */
394         long            o_msgid;        /* msgid of the request           */
395         unsigned long   o_tag;          /* tag of the request             */
396         time_t          o_time;         /* time op was initiated          */
397         char            *o_dn;          /* dn bound when op was initiated */
398         char            *o_ndn;         /* normalized dn bound when op was initiated */
399         int             o_authtype;     /* auth method used to bind dn    */
400                                         /* values taken from ldap.h       */
401                                         /* LDAP_AUTH_*                    */
402         int             o_opid;         /* id of this operation           */
403         int             o_connid;       /* id of conn initiating this op  */
404 #ifdef LDAP_CONNECTIONLESS
405         int             o_cldap;        /* != 0 if this came in via CLDAP */
406         struct sockaddr o_clientaddr;   /* client address if via CLDAP    */
407         char            o_searchbase;   /* search base if via CLDAP       */
408 #endif
409         struct slap_op  *o_next;        /* next operation pending         */
410         ldap_pvt_thread_t       o_tid;          /* thread handling this op        */
411         int             o_abandon;      /* signals op has been abandoned  */
412         ldap_pvt_thread_mutex_t o_abandonmutex; /* signals op has been abandoned  */
413
414         void    *o_private;     /* anything the backend needs     */
415 } Operation;
416
417 /*
418  * represents a connection from an ldap client
419  */
420
421 typedef struct slap_conn {
422         Sockbuf         c_sb;           /* ber connection stuff           */
423         char            *c_cdn;         /* DN provided by the client */
424         char            *c_dn;          /* DN bound to this conn  */
425         ldap_pvt_thread_mutex_t c_dnmutex;      /* mutex for c_dn field           */
426         int             c_protocol;     /* version of the LDAP protocol used by client */
427         int             c_authtype;     /* auth method used to bind c_dn  */
428 #ifdef LDAP_COMPAT
429         int             c_version;      /* for compatibility w/2.0, 3.0   */
430 #endif
431         char            *c_addr;        /* address of client on this conn */
432         char            *c_domain;      /* domain of client on this conn  */
433         Operation       *c_ops;         /* list of pending operations     */
434         ldap_pvt_thread_mutex_t c_opsmutex;     /* mutex for c_ops list & stats   */
435         ldap_pvt_thread_mutex_t c_pdumutex;     /* only one pdu written at a time */
436         ldap_pvt_thread_cond_t  c_wcv;          /* used to wait for sd write-ready*/
437         int             c_gettingber;   /* in the middle of ber_get_next  */
438         BerElement      *c_currentber;  /* ber we're getting              */
439         int             c_writewaiter;  /* signals write-ready sd waiter  */
440         int             c_pduwaiters;   /* signals threads waiting 4 pdu  */
441         time_t          c_starttime;    /* when the connection was opened */
442         int             c_connid;       /* id of this connection for stats*/
443         int             c_opsinitiated; /* # ops initiated/next op id     */
444         int             c_opscompleted; /* # ops completed                */
445 } Connection;
446
447 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
448 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )  \
449         { \
450                 if ( ldap_debug & level ) \
451                         fprintf( stderr, fmt, connid, opid, arg1, arg2, arg3 );\
452                 if ( ldap_syslog & level ) \
453                         syslog( ldap_syslog_level, fmt, connid, opid, arg1, \
454                             arg2, arg3 ); \
455         }
456 #else
457 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )
458 #endif
459
460 #include "proto-slap.h"
461
462 LDAP_END_DECL
463
464 #endif /* _slap_h_ */