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 #define ftruncate(a,b) _chsize(a,b)
47 #define truncate(a,b) _lclose( _lcreat(a, 0))
53 #define SERVICE_NAME OPENLDAP_PACKAGE "-slurpd"
55 /* Default directory for slurpd's private copy of replication logs */
56 #define DEFAULT_SLURPD_REPLICA_DIR LDAP_RUNDIR LDAP_DIRSEP "openldap-slurp"
58 /* Default name for slurpd's private copy of the replication log */
59 #define DEFAULT_SLURPD_REPLOGFILE "slurpd.replog"
61 /* Name of file which stores saved slurpd state info, for restarting */
62 #define DEFAULT_SLURPD_STATUS_FILE "slurpd.status"
64 /* slurpd dump file - contents of rq struct are written here (debugging) */
65 #define SLURPD_DUMPFILE LDAP_TMPDIR LDAP_DIRSEP "slurpd.dump"
67 /* default srvtab file. Can be overridden */
68 #define SRVTAB "/etc/srvtab"
70 /* Amount of time to sleep if no more work to do */
71 #define DEFAULT_NO_WORK_INTERVAL 3
73 /* The time we wait between checks to see if the replog file needs trimming */
74 #define TRIMCHECK_INTERVAL ( 60 * 5 )
76 /* Only try to trim slurpd replica files larger than this size */
77 #define MIN_TRIM_FILESIZE ( 10L * 1024L )
79 /* Maximum line length we can read from replication log */
80 #define REPLBUFLEN 256
85 #define TLS_CRITICAL 2
87 /* We support simple (plaintext password) and SASL authentication */
89 #define AUTH_KERBEROS 2
92 /* Rejection records are prefaced with this string */
93 #define ERROR_STR "ERROR"
95 /* Strings found in replication entries */
96 #define T_CHANGETYPESTR "changetype"
97 #define T_CHANGETYPE 1
98 #define T_TIMESTR "time"
103 #define T_ADDCTSTR "add"
105 #define T_MODIFYCTSTR "modify"
107 #define T_DELETECTSTR "delete"
109 #define T_MODRDNCTSTR "modrdn"
110 #define T_MODDNCTSTR "moddn"
111 #define T_RENAMECTSTR "rename"
114 #define T_MODOPADDSTR "add"
116 #define T_MODOPREPLACESTR "replace"
117 #define T_MODOPREPLACE 9
118 #define T_MODOPDELETESTR "delete"
119 #define T_MODOPDELETE 10
120 #define T_MODOPINCREMENTSTR "increment"
121 #define T_MODOPINCREMENT 11
122 #define T_MODSEPSTR "-"
125 #define T_NEWRDNSTR "newrdn"
126 #define T_DELOLDRDNSTR "deleteoldrdn"
127 #define T_NEWSUPSTR "newsuperior"
131 /* Config file keywords */
132 #define HOSTSTR "host"
134 #define ATTRSTR "attr"
135 #define SUFFIXSTR "suffix"
136 #define BINDDNSTR "binddn"
137 #define BINDMETHSTR "bindmethod"
138 #define KERBEROSSTR "kerberos"
139 #define SIMPLESTR "simple"
140 #define SASLSTR "sasl"
141 #define CREDSTR "credentials"
142 #define OLDAUTHCSTR "bindprincipal"
143 #define AUTHCSTR "authcID"
144 #define AUTHZSTR "authzID"
145 #define SRVTABSTR "srvtab"
146 #define SASLMECHSTR "saslmech"
147 #define REALMSTR "realm"
148 #define SECPROPSSTR "secprops"
149 #define STARTTLSSTR "starttls"
151 #define CRITICALSTR "critical"
153 #define REPLICA_SLEEP_TIME ( 10 )
155 /* Enumeration of various types of bind failures */
157 #define BIND_ERR_BADLDP 1
158 #define BIND_ERR_OPEN 2
159 #define BIND_ERR_BAD_ATYPE 3
160 #define BIND_ERR_SIMPLE_FAILED 4
161 #define BIND_ERR_KERBEROS_FAILED 5
162 #define BIND_ERR_BADRI 6
163 #define BIND_ERR_VERSION 7
164 #define BIND_ERR_REFERRALS 8
165 #define BIND_ERR_MANAGEDSAIT 9
166 #define BIND_ERR_SASL_FAILED 10
167 #define BIND_ERR_TLS_FAILED 11
169 /* Return codes for do_ldap() */
171 #define DO_LDAP_ERR_RETRYABLE 1
172 #define DO_LDAP_ERR_FATAL 2
175 * Types of counts one can request from the Rq rq_getcount()
179 #define RQ_COUNT_ALL 1
180 /* all elements with nonzero refcnt */
181 #define RQ_COUNT_NZRC 2
183 /* Amount of time, in seconds, for a thread to sleep when it encounters
184 * a retryable error in do_ldap().
186 #define RETRY_SLEEP_TIME 60
192 * ****************************************************************************
193 * Data types for replication queue and queue elements.
194 * ****************************************************************************
199 * Replica host information. An Ri struct will contain an array of these,
200 * with one entry for each replica. The end of the array is signaled
201 * by a NULL value in the rh_hostname field.
204 char *rh_hostname; /* replica hostname */
205 int rh_port; /* replica port */
210 * Per-replica information.
213 * - Private data should not be manipulated expect by Ri member functions.
215 typedef struct ri Ri;
218 char *ri_hostname; /* canonical hostname of replica */
219 int ri_port; /* port where slave slapd running */
220 char *ri_uri; /* e.g. "ldaps://ldap-1.example.com:636" */
221 LDAP *ri_ldp; /* LDAP struct for this replica */
222 int ri_tls; /* TLS: 0=no, 1=yes, 2=critical */
223 int ri_bind_method; /* AUTH_SIMPLE or AUTH_KERBEROS */
224 char *ri_bind_dn; /* DN to bind as when replicating */
225 char *ri_password; /* Password for any method */
226 char *ri_secprops; /* SASL security properties */
227 char *ri_realm; /* realm for any mechanism */
228 char *ri_authcId; /* authentication ID for any mechanism */
229 char *ri_authzId; /* authorization ID for any mechanism */
230 char *ri_srvtab; /* srvtab file for kerberos bind */
231 char *ri_saslmech; /* SASL mechanism to use */
232 struct re *ri_curr; /* current repl entry being processed */
233 struct stel *ri_stel; /* pointer to Stel for this replica */
235 ri_seq; /* seq number of last repl */
236 ldap_pvt_thread_t ri_tid; /* ID of thread for this replica */
238 /* Member functions */
239 int (*ri_process) LDAP_P(( Ri * )); /* process the next repl entry */
240 void (*ri_wake) LDAP_P(( Ri * )); /* wake up a sleeping thread */
247 * Information about one particular modification to make. This data should
248 * be considered private to routines in re.c, and to routines in ri.c.
253 char *mi_type; /* attr or type */
254 char *mi_val; /* value */
255 int mi_len; /* length of mi_val */
262 * Information about one particular replication entry. Only routines in
263 * re.c and rq.c should touch the private data. Other routines should
264 * only use member functions.
266 typedef struct re Re;
270 ldap_pvt_thread_mutex_t
271 re_mutex; /* mutex for this Re */
272 int re_refcnt; /* ref count, 0 = done */
273 time_t re_timestamp; /* timestamp of this re */
274 int re_seq; /* sequence number */
275 Rh *re_replicas; /* array of replica info */
276 char *re_dn; /* dn of entry being modified */
277 int re_changetype; /* type of modification */
278 Mi *re_mods; /* array of modifications to make */
279 struct re *re_next; /* pointer to next element */
281 /* Public functions */
282 int (*re_free) LDAP_P(( Re * )); /* free an re struct */
283 Re *(*re_getnext) LDAP_P(( Re * )); /* return next Re in linked list */
284 int (*re_parse) LDAP_P(( Re *, char * )); /* parse replication log entry */
285 int (*re_write) LDAP_P(( Ri *, Re *, FILE * )); /* write repl. log entry */
286 void (*re_dump) LDAP_P(( Re *, FILE * )); /* debugging - print contents */
287 int (*re_lock) LDAP_P(( Re * )); /* lock this re */
288 int (*re_unlock) LDAP_P(( Re * )); /* unlock this re */
289 int (*re_decrefcnt) LDAP_P(( Re * )); /* decrement the refcnt */
290 int (*re_getrefcnt) LDAP_P(( Re * )); /* get the refcnt */
297 * Definition for the queue of replica information. Private data is
298 * private to rq.c. Other routines should only touch public data or
299 * use member functions. Note that although we have a member function
300 * for locking the queue's mutex, we need to expose the rq_mutex
301 * variable so routines in ri.c can use it as a mutex for the
302 * rq_more condition variable.
304 typedef struct rq Rq;
308 Re *rq_head; /* pointer to head */
309 Re *rq_tail; /* pointer to tail */
310 int rq_nre; /* total number of Re's in queue */
311 int rq_ndel; /* number of deleted Re's in queue */
312 time_t rq_lasttrim; /* Last time we trimmed file */
315 ldap_pvt_thread_mutex_t
316 rq_mutex; /* mutex for whole queue */
317 ldap_pvt_thread_cond_t
318 rq_more; /* condition var - more work added */
320 /* Member functions */
321 Re * (*rq_gethead) LDAP_P(( Rq * )); /* get the element at head */
322 Re * (*rq_getnext) LDAP_P(( Re * )); /* get the next element */
323 int (*rq_delhead) LDAP_P(( Rq * )); /* delete the element at head */
324 int (*rq_add) LDAP_P(( Rq *, char * )); /* add at tail */
325 void (*rq_gc) LDAP_P(( Rq * )); /* garbage-collect queue */
326 int (*rq_lock) LDAP_P(( Rq * )); /* lock the queue */
327 int (*rq_unlock) LDAP_P(( Rq * )); /* unlock the queue */
328 int (*rq_needtrim) LDAP_P(( Rq * )); /* see if queue needs trimming */
329 int (*rq_write) LDAP_P(( Rq *, FILE * )); /*write Rq contents to file*/
330 int (*rq_getcount) LDAP_P(( Rq *, int )); /* return queue counts */
331 void (*rq_dump) LDAP_P(( Rq * )); /* debugging - print contents */
336 * An Stel (status element) contains information about one replica.
337 * Stel structs are associated with the St (status) struct, defined
340 typedef struct stel {
341 char *hostname; /* host name of replica */
342 int port; /* port number of replica */
343 time_t last; /* timestamp of last successful repl */
344 int seq; /* Sequence number of last repl */
349 * An St struct in an in-core structure which contains the current
350 * slurpd state. Most importantly, it contains an array of Stel
351 * structs which contain the timestamp and sequence number of the last
352 * successful replication for each replica. The st_write() member
353 * function is called periodically to flush status information to
354 * disk. At startup time, slurpd checks for the status file, and
355 * if present, uses the timestamps to avoid "replaying" replications
356 * which have already been sent to a given replica.
358 typedef struct st St;
361 ldap_pvt_thread_mutex_t
362 st_mutex; /* mutex to serialize access */
363 Stel **st_data; /* array of pointers to Stel structs */
364 int st_nreplicas; /* number of repl hosts */
365 int st_err_logged; /* 1 if fopen err logged */
366 FILE *st_fp; /* st file kept open */
367 FILE *st_lfp; /* lockfile fp */
369 /* Public member functions */
370 int (*st_update) LDAP_P(( St *, Stel*, Re* ));/*update entry for a host*/
371 Stel*(*st_add) LDAP_P(( St *, Ri * )); /*add a new repl host*/
372 int (*st_write) LDAP_P(( St * )); /* write status to disk */
373 int (*st_read) LDAP_P(( St * )); /* read status info from disk */
374 int (*st_lock) LDAP_P(( St * )); /* read status info from disk */
375 int (*st_unlock) LDAP_P(( St * )); /* read status info from disk */
378 #if defined( HAVE_LWP )
380 thread_t tl_tid; /* thread being managed */
381 time_t tl_wake; /* time thread should be resumed */
382 struct tl *tl_next; /* next node in list */
389 #endif /* HAVE_LWP */
392 * Public functions used to instantiate and initialize queue objects.
394 extern int Ri_init LDAP_P(( Ri **ri ));
395 extern int Rq_init LDAP_P(( Rq **rq ));
396 extern int Re_init LDAP_P(( Re **re ));
398 #include "proto-slurp.h"
402 #endif /* _SLURPD_H_ */