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