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