3 * Copyright (c) 1996 Regents of the University of Michigan.
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.
14 /* slurp.h - Standalone Ldap Update Replication Daemon (slurpd) */
25 #include <ac/signal.h>
26 #include <ac/syslog.h>
29 #include <sys/types.h>
34 #define ldap_debug slurp_debug
37 #include "ldap_pvt_thread.h"
38 #include "ldap_defaults.h"
42 /* Default directory for slurpd's private copy of replication logs */
43 #define DEFAULT_SLURPD_REPLICA_DIR "/usr/tmp"
45 /* Default name for slurpd's private copy of the replication log */
46 #define DEFAULT_SLURPD_REPLOGFILE "slurpd.replog"
48 /* Name of file which stores saved slurpd state info, for restarting */
49 #define DEFAULT_SLURPD_STATUS_FILE "slurpd.status"
51 /* slurpd dump file - contents of rq struct are written here (debugging) */
52 #define SLURPD_DUMPFILE "/tmp/slurpd.dump"
54 /* default srvtab file. Can be overridden */
55 #define SRVTAB "/etc/srvtab"
57 /* Amount of time to sleep if no more work to do */
58 #define DEFAULT_NO_WORK_INTERVAL 3
60 /* The time we wait between checks to see if the replog file needs trimming */
61 #define TRIMCHECK_INTERVAL ( 60 * 5 )
63 /* Only try to trim slurpd replica files larger than this size */
64 #define MIN_TRIM_FILESIZE ( 10L * 1024L )
66 /* Maximum line length we can read from replication log */
67 #define REPLBUFLEN 256
69 /* We support simple (plaintext password) and kerberos authentication */
71 #define AUTH_KERBEROS 2
73 /* Rejection records are prefaced with this string */
74 #define ERROR_STR "ERROR"
76 /* Strings found in replication entries */
77 #define T_CHANGETYPESTR "changetype"
78 #define T_CHANGETYPE 1
79 #define T_TIMESTR "time"
84 #define T_ADDCTSTR "add"
86 #define T_MODIFYCTSTR "modify"
88 #define T_DELETECTSTR "delete"
90 #define T_MODRDNCTSTR "modrdn"
91 #define T_MODDNCTSTR "moddn"
92 #define T_RENAMECTSTR "rename"
95 #define T_MODOPADDSTR "add"
97 #define T_MODOPREPLACESTR "replace"
98 #define T_MODOPREPLACE 9
99 #define T_MODOPDELETESTR "delete"
100 #define T_MODOPDELETE 10
101 #define T_MODSEPSTR "-"
104 #define T_NEWRDNSTR "newrdn"
105 #define T_DELOLDRDNSTR "deleteoldrdn"
106 #define T_NEWSUPSTR "newsuperior"
110 /* Config file keywords */
111 #define HOSTSTR "host"
112 #define BINDDNSTR "binddn"
113 #define BINDMETHSTR "bindmethod"
114 #define KERBEROSSTR "kerberos"
115 #define SIMPLESTR "simple"
116 #define CREDSTR "credentials"
117 #define BINDPSTR "bindprincipal"
118 #define SRVTABSTR "srvtab"
120 #define REPLICA_SLEEP_TIME ( 10 )
122 /* Enumeration of various types of bind failures */
124 #define BIND_ERR_BADLDP 1
125 #define BIND_ERR_OPEN 2
126 #define BIND_ERR_BAD_ATYPE 3
127 #define BIND_ERR_SIMPLE_FAILED 4
128 #define BIND_ERR_KERBEROS_FAILED 5
129 #define BIND_ERR_BADRI 6
130 #define BIND_ERR_VERSION 7
131 #define BIND_ERR_REFERRALS 8
132 #define BIND_ERR_MANAGEDSAIT 9
134 /* Return codes for do_ldap() */
136 #define DO_LDAP_ERR_RETRYABLE 1
137 #define DO_LDAP_ERR_FATAL 2
140 * Types of counts one can request from the Rq rq_getcount()
144 #define RQ_COUNT_ALL 1
145 /* all elements with nonzero refcnt */
146 #define RQ_COUNT_NZRC 2
148 /* Amount of time, in seconds, for a thread to sleep when it encounters
149 * a retryable error in do_ldap().
151 #define RETRY_SLEEP_TIME 60
157 * ****************************************************************************
158 * Data types for replication queue and queue elements.
159 * ****************************************************************************
164 * Replica host information. An Ri struct will contain an array of these,
165 * with one entry for each replica. The end of the array is signaled
166 * by a NULL value in the rh_hostname field.
169 char *rh_hostname; /* replica hostname */
170 int rh_port; /* replica port */
175 * Per-replica information.
178 * - Private data should not be manipulated expect by Ri member functions.
180 typedef struct ri Ri;
184 char *ri_hostname; /* canonical hostname of replica */
185 int ri_port; /* port where slave slapd running */
186 LDAP *ri_ldp; /* LDAP struct for this replica */
187 int ri_bind_method; /* AUTH_SIMPLE or AUTH_KERBEROS */
188 char *ri_bind_dn; /* DN to bind as when replicating */
189 char *ri_password; /* Password for AUTH_SIMPLE */
190 char *ri_principal; /* principal for kerberos bind */
191 char *ri_srvtab; /* srvtab file for kerberos bind */
192 struct re *ri_curr; /* current repl entry being processed */
193 struct stel *ri_stel; /* pointer to Stel for this replica */
195 ri_seq; /* seq number of last repl */
196 ldap_pvt_thread_t ri_tid; /* ID of thread for this replica */
198 /* Member functions */
199 int (*ri_process) LDAP_P(( Ri * )); /* process the next repl entry */
200 void (*ri_wake) LDAP_P(( Ri * )); /* wake up a sleeping thread */
207 * Information about one particular modification to make. This data should
208 * be considered private to routines in re.c, and to routines in ri.c.
213 char *mi_type; /* attr or type */
214 char *mi_val; /* value */
215 int mi_len; /* length of mi_val */
222 * Information about one particular replication entry. Only routines in
223 * re.c and rq.c should touch the private data. Other routines should
224 * only use member functions.
226 typedef struct re Re;
230 ldap_pvt_thread_mutex_t
231 re_mutex; /* mutex for this Re */
232 int re_refcnt; /* ref count, 0 = done */
233 char *re_timestamp; /* timestamp of this re */
234 int re_seq; /* sequence number */
235 Rh *re_replicas; /* array of replica info */
236 char *re_dn; /* dn of entry being modified */
237 int re_changetype; /* type of modification */
238 Mi *re_mods; /* array of modifications to make */
239 struct re *re_next; /* pointer to next element */
241 /* Public functions */
242 int (*re_free) LDAP_P(( Re * )); /* free an re struct */
243 Re *(*re_getnext) LDAP_P(( Re * )); /* return next Re in linked list */
244 int (*re_parse) LDAP_P(( Re *, char * )); /* parse replication log entry */
245 int (*re_write) LDAP_P(( Ri *, Re *, FILE * )); /* write repl. log entry */
246 void (*re_dump) LDAP_P(( Re *, FILE * )); /* debugging - print contents */
247 int (*re_lock) LDAP_P(( Re * )); /* lock this re */
248 int (*re_unlock) LDAP_P(( Re * )); /* unlock this re */
249 int (*re_decrefcnt) LDAP_P(( Re * )); /* decrement the refcnt */
250 int (*re_getrefcnt) LDAP_P(( Re * )); /* get the refcnt */
257 * Definition for the queue of replica information. Private data is
258 * private to rq.c. Other routines should only touch public data or
259 * use member functions. Note that although we have a member function
260 * for locking the queue's mutex, we need to expose the rq_mutex
261 * variable so routines in ri.c can use it as a mutex for the
262 * rq_more condition variable.
264 typedef struct rq Rq;
268 Re *rq_head; /* pointer to head */
269 Re *rq_tail; /* pointer to tail */
270 int rq_nre; /* total number of Re's in queue */
271 int rq_ndel; /* number of deleted Re's in queue */
272 time_t rq_lasttrim; /* Last time we trimmed file */
275 ldap_pvt_thread_mutex_t
276 rq_mutex; /* mutex for whole queue */
277 ldap_pvt_thread_cond_t
278 rq_more; /* condition var - more work added */
280 /* Member functions */
281 Re * (*rq_gethead) LDAP_P(( Rq * )); /* get the element at head */
282 Re * (*rq_getnext) LDAP_P(( Re * )); /* get the next element */
283 int (*rq_delhead) LDAP_P(( Rq * )); /* delete the element at head */
284 int (*rq_add) LDAP_P(( Rq *, char * )); /* add at tail */
285 void (*rq_gc) LDAP_P(( Rq * )); /* garbage-collect queue */
286 int (*rq_lock) LDAP_P(( Rq * )); /* lock the queue */
287 int (*rq_unlock) LDAP_P(( Rq * )); /* unlock the queue */
288 int (*rq_needtrim) LDAP_P(( Rq * )); /* see if queue needs trimming */
289 int (*rq_write) LDAP_P(( Rq *, FILE * )); /*write Rq contents to file*/
290 int (*rq_getcount) LDAP_P(( Rq *, int )); /* return queue counts */
291 void (*rq_dump) LDAP_P(( Rq * )); /* debugging - print contents */
296 * An Stel (status element) contains information about one replica.
297 * Stel structs are associated with the St (status) struct, defined
300 typedef struct stel {
301 char *hostname; /* host name of replica */
302 int port; /* port number of replica */
303 char last[ 64 ]; /* timestamp of last successful repl */
304 int seq; /* Sequence number of last repl */
309 * An St struct in an in-core structure which contains the current
310 * slurpd state. Most importantly, it contains an array of Stel
311 * structs which contain the timestamp and sequence number of the last
312 * successful replication for each replica. The st_write() member
313 * function is called periodically to flush status information to
314 * disk. At startup time, slurpd checks for the status file, and
315 * if present, uses the timestamps to avoid "replaying" replications
316 * which have already been sent to a given replica.
318 typedef struct st St;
321 ldap_pvt_thread_mutex_t
322 st_mutex; /* mutex to serialize access */
323 Stel **st_data; /* array of pointers to Stel structs */
324 int st_nreplicas; /* number of repl hosts */
325 int st_err_logged; /* 1 if fopen err logged */
326 FILE *st_fp; /* st file kept open */
327 FILE *st_lfp; /* lockfile fp */
329 /* Public member functions */
330 int (*st_update) LDAP_P(( St *, Stel*, Re* ));/*update entry for a host*/
331 Stel*(*st_add) LDAP_P(( St *, Ri * )); /*add a new repl host*/
332 int (*st_write) LDAP_P(( St * )); /* write status to disk */
333 int (*st_read) LDAP_P(( St * )); /* read status info from disk */
334 int (*st_lock) LDAP_P(( St * )); /* read status info from disk */
335 int (*st_unlock) LDAP_P(( St * )); /* read status info from disk */
338 #if defined( HAVE_LWP )
340 thread_t tl_tid; /* thread being managed */
341 time_t tl_wake; /* time thread should be resumed */
342 struct tl *tl_next; /* next node in list */
349 #endif /* HAVE_LWP */
352 * Public functions used to instantiate and initialize queue objects.
354 extern int Ri_init LDAP_P(( Ri **ri ));
355 extern int Rq_init LDAP_P(( Rq **rq ));
356 extern int Re_init LDAP_P(( Re **re ));
358 #include "proto-slurp.h"
362 #endif /* _SLURPD_H_ */