3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
18 /* slurp.h - Standalone Ldap Update Replication Daemon (slurpd) */
23 #if !defined(HAVE_WINSOCK) && !defined(LDAP_SYSLOG)
29 #include <ac/signal.h>
30 #include <ac/syslog.h>
33 #include <sys/types.h>
38 #define ldap_debug slurp_debug
41 #include "ldap_pvt_thread.h"
42 #include "ldap_defaults.h"
46 /* should be moved to portable.h.nt */
47 #define ftruncate(a,b) _chsize(a,b)
48 #define truncate(a,b) _lclose( _lcreat(a, 0))
54 #define SERVICE_NAME OPENLDAP_PACKAGE "-slurpd"
56 /* Default directory for slurpd's private copy of replication logs */
57 #define DEFAULT_SLURPD_REPLICA_DIR LDAP_RUNDIR LDAP_DIRSEP "openldap-slurp"
59 /* Default name for slurpd's private copy of the replication log */
60 #define DEFAULT_SLURPD_REPLOGFILE "slurpd.replog"
62 /* Name of file which stores saved slurpd state info, for restarting */
63 #define DEFAULT_SLURPD_STATUS_FILE "slurpd.status"
65 /* slurpd dump file - contents of rq struct are written here (debugging) */
66 #define SLURPD_DUMPFILE LDAP_TMPDIR LDAP_DIRSEP "slurpd.dump"
68 /* Amount of time to sleep if no more work to do */
69 #define DEFAULT_NO_WORK_INTERVAL 3
71 /* The time we wait between checks to see if the replog file needs trimming */
72 #define TRIMCHECK_INTERVAL ( 60 * 5 )
74 /* Only try to trim slurpd replica files larger than this size */
75 #define MIN_TRIM_FILESIZE ( 10L * 1024L )
77 /* Maximum line length we can read from replication log */
78 #define REPLBUFLEN 256
83 #define TLS_CRITICAL 2
85 /* Rejection records are prefaced with this string */
86 #define ERROR_STR "ERROR"
88 /* Strings found in replication entries */
89 #define T_CHANGETYPESTR "changetype"
90 #define T_CHANGETYPE 1
91 #define T_TIMESTR "time"
96 #define T_ADDCTSTR "add"
98 #define T_MODIFYCTSTR "modify"
100 #define T_DELETECTSTR "delete"
102 #define T_MODRDNCTSTR "modrdn"
103 #define T_MODDNCTSTR "moddn"
104 #define T_RENAMECTSTR "rename"
107 #define T_MODOPADDSTR "add"
109 #define T_MODOPREPLACESTR "replace"
110 #define T_MODOPREPLACE 9
111 #define T_MODOPDELETESTR "delete"
112 #define T_MODOPDELETE 10
113 #define T_MODOPINCREMENTSTR "increment"
114 #define T_MODOPINCREMENT 11
115 #define T_MODSEPSTR "-"
118 #define T_NEWRDNSTR "newrdn"
119 #define T_DELOLDRDNSTR "deleteoldrdn"
120 #define T_NEWSUPSTR "newsuperior"
124 /* Config file keywords */
125 #define HOSTSTR "host"
127 #define ATTRSTR "attr"
128 #define SUFFIXSTR "suffix"
129 #define BINDDNSTR "binddn"
130 #define BINDMETHSTR "bindmethod"
131 #define KERBEROSSTR "kerberos"
132 #define SIMPLESTR "simple"
133 #define SASLSTR "sasl"
134 #define CREDSTR "credentials"
135 #define OLDAUTHCSTR "bindprincipal"
136 #define AUTHCSTR "authcID"
137 #define AUTHZSTR "authzID"
138 #define SRVTABSTR "srvtab"
139 #define SASLMECHSTR "saslmech"
140 #define REALMSTR "realm"
141 #define SECPROPSSTR "secprops"
142 #define STARTTLSSTR "starttls"
144 #define CRITICALSTR "critical"
146 #define REPLICA_SLEEP_TIME ( 10 )
148 /* Enumeration of various types of bind failures */
150 #define BIND_ERR_BADLDP 1
151 #define BIND_ERR_OPEN 2
152 #define BIND_ERR_BAD_ATYPE 3
153 #define BIND_ERR_SIMPLE_FAILED 4
154 #define BIND_ERR_KERBEROS_FAILED 5
155 #define BIND_ERR_BADRI 6
156 #define BIND_ERR_VERSION 7
157 #define BIND_ERR_REFERRALS 8
158 #define BIND_ERR_MANAGEDSAIT 9
159 #define BIND_ERR_SASL_FAILED 10
160 #define BIND_ERR_TLS_FAILED 11
162 /* Return codes for do_ldap() */
164 #define DO_LDAP_ERR_RETRYABLE 1
165 #define DO_LDAP_ERR_FATAL 2
168 * Types of counts one can request from the Rq rq_getcount()
172 #define RQ_COUNT_ALL 1
173 /* all elements with nonzero refcnt */
174 #define RQ_COUNT_NZRC 2
176 /* Amount of time, in seconds, for a thread to sleep when it encounters
177 * a retryable error in do_ldap().
179 #define RETRY_SLEEP_TIME 60
185 * ****************************************************************************
186 * Data types for replication queue and queue elements.
187 * ****************************************************************************
192 * Replica host information. An Ri struct will contain an array of these,
193 * with one entry for each replica. The end of the array is signaled
194 * by a NULL value in the rh_hostname field.
197 char *rh_hostname; /* replica hostname */
198 int rh_port; /* replica port */
203 * Per-replica information.
206 * - Private data should not be manipulated expect by Ri member functions.
208 typedef struct ri Ri;
211 char *ri_hostname; /* canonical hostname of replica */
212 int ri_port; /* port where slave slapd running */
213 char *ri_uri; /* e.g. "ldaps://ldap-1.example.com:636" */
214 LDAP *ri_ldp; /* LDAP struct for this replica */
215 int ri_tls; /* TLS: 0=no, 1=yes, 2=critical */
216 int ri_bind_method; /* AUTH_SIMPLE or AUTH_KERBEROS */
217 char *ri_bind_dn; /* DN to bind as when replicating */
218 char *ri_password; /* Password for any method */
219 char *ri_secprops; /* SASL security properties */
220 char *ri_realm; /* realm for any mechanism */
221 char *ri_authcId; /* authentication ID for any mechanism */
222 char *ri_authzId; /* authorization ID for any mechanism */
223 char *ri_srvtab; /* srvtab file for kerberos bind */
224 char *ri_saslmech; /* SASL mechanism to use */
225 struct re *ri_curr; /* current repl entry being processed */
226 struct stel *ri_stel; /* pointer to Stel for this replica */
228 ri_seq; /* seq number of last repl */
229 ldap_pvt_thread_t ri_tid; /* ID of thread for this replica */
231 /* Member functions */
232 int (*ri_process) LDAP_P(( Ri * )); /* process the next repl entry */
233 void (*ri_wake) LDAP_P(( Ri * )); /* wake up a sleeping thread */
239 * Information about one particular modification to make. This data should
240 * be considered private to routines in re.c, and to routines in ri.c.
244 char *mi_type; /* attr or type */
245 char *mi_val; /* value */
246 int mi_len; /* length of mi_val */
252 * Information about one particular replication entry. Only routines in
253 * re.c and rq.c should touch the private data. Other routines should
254 * only use member functions.
256 typedef struct re Re;
259 ldap_pvt_thread_mutex_t
260 re_mutex; /* mutex for this Re */
261 int re_refcnt; /* ref count, 0 = done */
262 time_t re_timestamp; /* timestamp of this re */
263 int re_seq; /* sequence number */
264 Rh *re_replicas; /* array of replica info */
265 char *re_dn; /* dn of entry being modified */
266 int re_changetype; /* type of modification */
267 Mi *re_mods; /* array of modifications to make */
268 struct re *re_next; /* pointer to next element */
270 /* Public functions */
271 int (*re_free) LDAP_P(( Re * )); /* free an re struct */
272 Re *(*re_getnext) LDAP_P(( Re * )); /* return next Re in linked list */
273 int (*re_parse) LDAP_P(( Re *, char * )); /* parse replication log entry */
274 int (*re_write) LDAP_P(( Ri *, Re *, FILE * )); /* write repl. log entry */
275 void (*re_dump) LDAP_P(( Re *, FILE * )); /* debugging - print contents */
276 int (*re_lock) LDAP_P(( Re * )); /* lock this re */
277 int (*re_unlock) LDAP_P(( Re * )); /* unlock this re */
278 int (*re_decrefcnt) LDAP_P(( Re * )); /* decrement the refcnt */
279 int (*re_getrefcnt) LDAP_P(( Re * )); /* get the refcnt */
286 * Definition for the queue of replica information. Private data is
287 * private to rq.c. Other routines should only touch public data or
288 * use member functions. Note that although we have a member function
289 * for locking the queue's mutex, we need to expose the rq_mutex
290 * variable so routines in ri.c can use it as a mutex for the
291 * rq_more condition variable.
293 typedef struct rq Rq;
297 Re *rq_head; /* pointer to head */
298 Re *rq_tail; /* pointer to tail */
299 int rq_nre; /* total number of Re's in queue */
300 int rq_ndel; /* number of deleted Re's in queue */
301 time_t rq_lasttrim; /* Last time we trimmed file */
304 ldap_pvt_thread_mutex_t
305 rq_mutex; /* mutex for whole queue */
306 ldap_pvt_thread_cond_t
307 rq_more; /* condition var - more work added */
309 /* Member functions */
310 Re * (*rq_gethead) LDAP_P(( Rq * )); /* get the element at head */
311 Re * (*rq_getnext) LDAP_P(( Re * )); /* get the next element */
312 int (*rq_delhead) LDAP_P(( Rq * )); /* delete the element at head */
313 int (*rq_add) LDAP_P(( Rq *, char * )); /* add at tail */
314 void (*rq_gc) LDAP_P(( Rq * )); /* garbage-collect queue */
315 int (*rq_lock) LDAP_P(( Rq * )); /* lock the queue */
316 int (*rq_unlock) LDAP_P(( Rq * )); /* unlock the queue */
317 int (*rq_needtrim) LDAP_P(( Rq * )); /* see if queue needs trimming */
318 int (*rq_write) LDAP_P(( Rq *, FILE * )); /*write Rq contents to file*/
319 int (*rq_getcount) LDAP_P(( Rq *, int )); /* return queue counts */
320 void (*rq_dump) LDAP_P(( Rq * )); /* debugging - print contents */
325 * An Stel (status element) contains information about one replica.
326 * Stel structs are associated with the St (status) struct, defined
329 typedef struct stel {
330 char *hostname; /* host name of replica */
331 int port; /* port number of replica */
332 time_t last; /* timestamp of last successful repl */
333 int seq; /* Sequence number of last repl */
338 * An St struct in an in-core structure which contains the current
339 * slurpd state. Most importantly, it contains an array of Stel
340 * structs which contain the timestamp and sequence number of the last
341 * successful replication for each replica. The st_write() member
342 * function is called periodically to flush status information to
343 * disk. At startup time, slurpd checks for the status file, and
344 * if present, uses the timestamps to avoid "replaying" replications
345 * which have already been sent to a given replica.
347 typedef struct st St;
350 ldap_pvt_thread_mutex_t
351 st_mutex; /* mutex to serialize access */
352 Stel **st_data; /* array of pointers to Stel structs */
353 int st_nreplicas; /* number of repl hosts */
354 int st_err_logged; /* 1 if fopen err logged */
355 FILE *st_fp; /* st file kept open */
356 FILE *st_lfp; /* lockfile fp */
358 /* Public member functions */
359 int (*st_update) LDAP_P(( St *, Stel*, Re* ));/*update entry for a host*/
360 Stel*(*st_add) LDAP_P(( St *, Ri * )); /*add a new repl host*/
361 int (*st_write) LDAP_P(( St * )); /* write status to disk */
362 int (*st_read) LDAP_P(( St * )); /* read status info from disk */
363 int (*st_lock) LDAP_P(( St * )); /* read status info from disk */
364 int (*st_unlock) LDAP_P(( St * )); /* read status info from disk */
367 #if defined( HAVE_LWP )
369 thread_t tl_tid; /* thread being managed */
370 time_t tl_wake; /* time thread should be resumed */
371 struct tl *tl_next; /* next node in list */
378 #endif /* HAVE_LWP */
381 * Public functions used to instantiate and initialize queue objects.
383 extern int Ri_init LDAP_P(( Ri **ri ));
384 extern int Rq_init LDAP_P(( Rq **rq ));
385 extern int Re_init LDAP_P(( Re **re ));
387 #include "proto-slurp.h"
391 #endif /* _SLURPD_H_ */