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