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