]> git.sur5r.net Git - openldap/blob - servers/slurpd/slurp.h
Add TLS Support
[openldap] / servers / slurpd / slurp.h
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 /* slurp.h - Standalone Ldap Update Replication Daemon (slurpd) */
15
16 #ifndef _SLURPD_H_
17 #define _SLURPD_H_
18
19 #ifndef LDAP_SYSLOG
20 #define LDAP_SYSLOG 1
21 #endif
22
23 #include <ac/errno.h>
24 #include <ac/param.h>
25 #include <ac/signal.h>
26 #include <ac/syslog.h>
27 #include <ac/time.h>
28
29 #include <sys/types.h>
30
31 #include <ldap.h>
32
33 #undef  ldap_debug
34 #define ldap_debug slurp_debug
35 #include "ldap_log.h"
36
37 #include "ldap_pvt_thread.h"
38 #include "ldap_defaults.h"
39 #include "ldif.h"
40
41
42 /* Default directory for slurpd's private copy of replication logs */
43 #define DEFAULT_SLURPD_REPLICA_DIR      LDAP_RUNDIR LDAP_DIRSEP "openldap-slurp"
44
45 /* Default name for slurpd's private copy of the replication log */
46 #define DEFAULT_SLURPD_REPLOGFILE       "slurpd.replog"
47
48 /* Name of file which stores saved slurpd state info, for restarting */
49 #define DEFAULT_SLURPD_STATUS_FILE      "slurpd.status"
50
51 /* slurpd dump file - contents of rq struct are written here (debugging) */
52 #define SLURPD_DUMPFILE                 LDAP_TMPDIR LDAP_DIRSEP "slurpd.dump"
53
54 /* default srvtab file.  Can be overridden */
55 #define SRVTAB                          "/etc/srvtab"
56
57 /* Amount of time to sleep if no more work to do */
58 #define DEFAULT_NO_WORK_INTERVAL        3
59
60 /* The time we wait between checks to see if the replog file needs trimming */
61 #define TRIMCHECK_INTERVAL              ( 60 * 5 )
62
63 /* Only try to trim slurpd replica files larger than this size */
64 #define MIN_TRIM_FILESIZE               ( 10L * 1024L )
65
66 /* Maximum line length we can read from replication log */
67 #define REPLBUFLEN                      256
68
69 /* TLS flags */
70 #define TLS_OFF                 0
71 #define TLS_ON                  1
72 #define TLS_CRITICAL    2
73
74 /* We support simple (plaintext password) and SASL authentication */
75 #define AUTH_SIMPLE     1
76 #define AUTH_KERBEROS   2
77 #define AUTH_SASL 3
78
79 /* Rejection records are prefaced with this string */
80 #define ERROR_STR       "ERROR"
81
82 /* Strings found in replication entries */
83 #define T_CHANGETYPESTR         "changetype"
84 #define T_CHANGETYPE            1
85 #define T_TIMESTR               "time"
86 #define T_TIME                  2
87 #define T_DNSTR                 "dn"
88 #define T_DN                    3
89
90 #define T_ADDCTSTR              "add"
91 #define T_ADDCT                 4
92 #define T_MODIFYCTSTR           "modify"
93 #define T_MODIFYCT              5
94 #define T_DELETECTSTR           "delete"
95 #define T_DELETECT              6
96 #define T_MODRDNCTSTR           "modrdn"
97 #define T_MODDNCTSTR            "moddn"
98 #define T_RENAMECTSTR           "rename"
99 #define T_MODRDNCT              7
100
101 #define T_MODOPADDSTR           "add"
102 #define T_MODOPADD              8
103 #define T_MODOPREPLACESTR       "replace"
104 #define T_MODOPREPLACE          9
105 #define T_MODOPDELETESTR        "delete"
106 #define T_MODOPDELETE           10
107 #define T_MODSEPSTR             "-"
108 #define T_MODSEP                11
109
110 #define T_NEWRDNSTR             "newrdn"
111 #define T_DELOLDRDNSTR  "deleteoldrdn"
112 #define T_NEWSUPSTR             "newsuperior"
113
114 #define T_ERR                   -1
115
116 /* Config file keywords */
117 #define HOSTSTR                 "host"
118 #define BINDDNSTR               "binddn"
119 #define BINDMETHSTR             "bindmethod"
120 #define KERBEROSSTR             "kerberos"
121 #define SIMPLESTR               "simple"
122 #define SASLSTR                 "sasl"
123 #define CREDSTR                 "credentials"
124 #define OLDAUTHCSTR             "bindprincipal"
125 #define AUTHCSTR                "authcID"
126 #define AUTHZSTR                "authzID"
127 #define SRVTABSTR               "srvtab"
128 #define SASLMECHSTR             "saslmech"
129 #define REALMSTR                "realm"
130 #define SECPROPSSTR             "secprops"
131 #define TLSSTR                  "tls"
132 #define TLSCRITICALSTR  "critical"
133
134 #define REPLICA_SLEEP_TIME      ( 10 )
135
136 /* Enumeration of various types of bind failures */
137 #define BIND_OK                                         0
138 #define BIND_ERR_BADLDP                         1
139 #define BIND_ERR_OPEN                           2
140 #define BIND_ERR_BAD_ATYPE                      3
141 #define BIND_ERR_SIMPLE_FAILED          4
142 #define BIND_ERR_KERBEROS_FAILED        5
143 #define BIND_ERR_BADRI                          6
144 #define BIND_ERR_VERSION                        7
145 #define BIND_ERR_REFERRALS                      8
146 #define BIND_ERR_MANAGEDSAIT            9
147 #define BIND_ERR_SASL_FAILED            10
148 #define BIND_ERR_TLS_FAILED                     11
149
150 /* Return codes for do_ldap() */
151 #define DO_LDAP_OK                      0
152 #define DO_LDAP_ERR_RETRYABLE           1
153 #define DO_LDAP_ERR_FATAL               2
154
155 /*
156  * Types of counts one can request from the Rq rq_getcount()
157  * member function
158  */
159 /* all elements */
160 #define RQ_COUNT_ALL                    1
161 /* all elements with nonzero refcnt */
162 #define RQ_COUNT_NZRC                   2
163
164 /* Amount of time, in seconds, for a thread to sleep when it encounters
165  * a retryable error in do_ldap().
166  */
167 #define RETRY_SLEEP_TIME                60
168
169
170 LDAP_BEGIN_DECL
171
172 /*
173  * ****************************************************************************
174  * Data types for replication queue and queue elements.
175  * ****************************************************************************
176  */
177
178
179 /*
180  * Replica host information.  An Ri struct will contain an array of these,
181  * with one entry for each replica.  The end of the array is signaled
182  * by a NULL value in the rh_hostname field.
183  */
184 typedef struct rh {
185     char        *rh_hostname;           /* replica hostname  */
186     int         rh_port;                /* replica port */
187 } Rh;
188
189
190 /*
191  * Per-replica information.
192  *
193  * Notes:
194  *  - Private data should not be manipulated expect by Ri member functions.
195  */
196 typedef struct ri Ri;
197 struct ri {
198     /* Private data */
199     char        *ri_hostname;           /* canonical hostname of replica */
200     int         ri_port;                /* port where slave slapd running */
201     LDAP        *ri_ldp;                /* LDAP struct for this replica */
202     int         ri_tls;                 /* TLS: 0=no, 1=yes, 2=critical */
203     int         ri_bind_method;         /* AUTH_SIMPLE or AUTH_KERBEROS */
204     char        *ri_bind_dn;            /* DN to bind as when replicating */
205     char        *ri_password;           /* Password for any method */
206     char        *ri_secprops;           /* SASL security properties */
207     char        *ri_realm;                      /* realm for any mechanism */
208     char        *ri_authcId;            /* authentication ID for any mechanism */
209     char        *ri_authzId;            /* authorization ID for any mechanism */
210     char        *ri_srvtab;             /* srvtab file for kerberos bind */
211     char        *ri_saslmech;           /* SASL mechanism to use */
212     struct re   *ri_curr;               /* current repl entry being processed */
213     struct stel *ri_stel;               /* pointer to Stel for this replica */
214     unsigned long
215                 ri_seq;                 /* seq number of last repl */
216     ldap_pvt_thread_t   ri_tid;                 /* ID of thread for this replica */
217
218     /* Member functions */
219     int (*ri_process) LDAP_P(( Ri * )); /* process the next repl entry */
220     void (*ri_wake)   LDAP_P(( Ri * )); /* wake up a sleeping thread */
221 };
222     
223
224
225
226 /*
227  * Information about one particular modification to make.  This data should
228  * be considered private to routines in re.c, and to routines in ri.c.
229  */
230 typedef struct mi {
231     
232     /* Private data */
233     char        *mi_type;               /* attr or type */
234     char        *mi_val;                /* value */
235     int         mi_len;                 /* length of mi_val */
236
237 } Mi;
238
239
240
241 /* 
242  * Information about one particular replication entry.  Only routines in
243  * re.c  and rq.c should touch the private data.  Other routines should
244  * only use member functions.
245  */
246 typedef struct re Re;
247 struct re {
248
249     /* Private data */
250     ldap_pvt_thread_mutex_t
251                 re_mutex;               /* mutex for this Re */
252     int         re_refcnt;              /* ref count, 0 = done */
253     char        *re_timestamp;          /* timestamp of this re */
254     int         re_seq;                 /* sequence number */
255     Rh          *re_replicas;           /* array of replica info */
256     char        *re_dn;                 /* dn of entry being modified */
257     int         re_changetype;          /* type of modification */
258     Mi          *re_mods;               /* array of modifications to make */
259     struct re   *re_next;               /* pointer to next element */
260
261     /* Public functions */
262     int (*re_free)    LDAP_P(( Re * )); /* free an re struct */
263     Re *(*re_getnext) LDAP_P(( Re * )); /* return next Re in linked list */
264     int (*re_parse) LDAP_P(( Re *, char * )); /* parse replication log entry */
265     int (*re_write) LDAP_P(( Ri *, Re *, FILE * )); /* write repl. log entry */
266     void (*re_dump)  LDAP_P(( Re *, FILE * )); /* debugging - print contents */
267     int (*re_lock)   LDAP_P(( Re * ));    /* lock this re */
268     int (*re_unlock) LDAP_P(( Re * ));    /* unlock this re */
269     int (*re_decrefcnt) LDAP_P(( Re * )); /* decrement the refcnt */
270     int (*re_getrefcnt) LDAP_P(( Re * )); /* get the refcnt */
271 };
272
273
274
275
276 /* 
277  * Definition for the queue of replica information.  Private data is
278  * private to rq.c.  Other routines should only touch public data or
279  * use member functions.  Note that although we have a member function
280  * for locking the queue's mutex, we need to expose the rq_mutex
281  * variable so routines in ri.c can use it as a mutex for the
282  * rq_more condition variable.
283  */
284 typedef struct rq Rq;
285 struct rq {
286
287     /* Private data */
288     Re          *rq_head;               /* pointer to head */
289     Re          *rq_tail;               /* pointer to tail */
290     int         rq_nre;                 /* total number of Re's in queue */
291     int         rq_ndel;                /* number of deleted Re's in queue */
292     time_t      rq_lasttrim;            /* Last time we trimmed file */
293     
294     /* Public data */
295     ldap_pvt_thread_mutex_t
296                 rq_mutex;               /* mutex for whole queue */
297     ldap_pvt_thread_cond_t
298                 rq_more;                /* condition var - more work added */
299
300     /* Member functions */
301     Re * (*rq_gethead)  LDAP_P(( Rq * )); /* get the element at head */
302     Re * (*rq_getnext)  LDAP_P(( Re * )); /* get the next element */
303     int  (*rq_delhead)  LDAP_P(( Rq * )); /* delete the element at head */
304     int  (*rq_add)      LDAP_P(( Rq *, char * )); /* add at tail */
305     void (*rq_gc)       LDAP_P(( Rq * )); /* garbage-collect queue */
306     int  (*rq_lock)     LDAP_P(( Rq * )); /* lock the queue */
307     int  (*rq_unlock)   LDAP_P(( Rq * )); /* unlock the queue */
308     int  (*rq_needtrim) LDAP_P(( Rq * )); /* see if queue needs trimming */
309     int  (*rq_write)    LDAP_P(( Rq *, FILE * )); /*write Rq contents to file*/
310     int  (*rq_getcount) LDAP_P(( Rq *, int )); /* return queue counts */
311     void (*rq_dump)     LDAP_P(( Rq * )); /* debugging - print contents */
312 };
313
314
315 /*
316  * An Stel (status element) contains information about one replica.
317  * Stel structs are associated with the St (status) struct, defined 
318  * below.
319  */
320 typedef struct stel {
321     char        *hostname;              /* host name of replica */
322     int         port;                   /* port number of replica */
323     char        last[ 64 ];             /* timestamp of last successful repl */
324     int         seq;                    /* Sequence number of last repl */
325 } Stel;
326
327
328 /*
329  * An St struct in an in-core structure which contains the current
330  * slurpd state.  Most importantly, it contains an array of Stel
331  * structs which contain the timestamp and sequence number of the last
332  * successful replication for each replica.  The st_write() member
333  * function is called periodically to flush status information to
334  * disk.  At startup time, slurpd checks for the status file, and
335  * if present, uses the timestamps to avoid "replaying" replications
336  * which have already been sent to a given replica.
337  */
338 typedef struct st St;
339 struct st {
340     /* Private data */
341     ldap_pvt_thread_mutex_t
342                 st_mutex;               /* mutex to serialize access */
343     Stel        **st_data;              /* array of pointers to Stel structs */
344     int         st_nreplicas;           /* number of repl hosts */
345     int         st_err_logged;          /* 1 if fopen err logged */
346     FILE        *st_fp;                 /* st file kept open */
347     FILE        *st_lfp;                /* lockfile fp */
348
349     /* Public member functions */
350     int  (*st_update) LDAP_P(( St *, Stel*, Re* ));/*update entry for a host*/
351     Stel*(*st_add)    LDAP_P(( St *, Ri * ));      /*add a new repl host*/
352     int  (*st_write)  LDAP_P(( St * )); /* write status to disk */
353     int  (*st_read)   LDAP_P(( St * )); /* read status info from disk */
354     int  (*st_lock)   LDAP_P(( St * )); /* read status info from disk */
355     int  (*st_unlock) LDAP_P(( St * )); /* read status info from disk */
356 };
357
358 #if defined( HAVE_LWP )
359 typedef struct tl {
360     thread_t    tl_tid;         /* thread being managed */
361     time_t      tl_wake;        /* time thread should be resumed */
362     struct tl   *tl_next;       /* next node in list */
363 } tl_t;
364
365 typedef struct tsl {
366     tl_t        *tsl_list;
367     mon_t       tsl_mon;
368 } tsl_t;
369 #endif /* HAVE_LWP */
370
371 /* 
372  * Public functions used to instantiate and initialize queue objects.
373  */
374 extern int Ri_init LDAP_P(( Ri **ri ));
375 extern int Rq_init LDAP_P(( Rq **rq ));
376 extern int Re_init LDAP_P(( Re **re ));
377
378 #include "proto-slurp.h"
379
380 LDAP_END_DECL
381
382 #endif /* _SLURPD_H_ */