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