2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
30 /* slurp.h - Standalone Ldap Update Replication Daemon (slurpd) */
35 #if !defined(HAVE_WINSOCK) && !defined(LDAP_SYSLOG)
41 #include <ac/signal.h>
42 #include <ac/syslog.h>
45 #include <sys/types.h>
50 #define ldap_debug slurp_debug
53 #include "ldap_pvt_thread.h"
54 #include "ldap_defaults.h"
58 /* should be moved to portable.h.nt */
59 #define ftruncate(a,b) _chsize(a,b)
60 #define truncate(a,b) _lclose( _lcreat(a, 0))
61 #define mkdir(a,b) mkdir(a)
74 #define S_IRUSR S_IREAD
77 #define S_IWUSR S_IWRITE
82 #define SERVICE_NAME OPENLDAP_PACKAGE "-slurpd"
84 /* Default directory for slurpd's private copy of replication logs */
85 #define DEFAULT_SLURPD_REPLICA_DIR LDAP_RUNDIR LDAP_DIRSEP "openldap-slurp"
87 /* Default name for slurpd's private copy of the replication log */
88 #define DEFAULT_SLURPD_REPLOGFILE "slurpd.replog"
90 /* Name of file which stores saved slurpd state info, for restarting */
91 #define DEFAULT_SLURPD_STATUS_FILE "slurpd.status"
93 /* slurpd dump file - contents of rq struct are written here (debugging) */
94 #define SLURPD_DUMPFILE LDAP_TMPDIR LDAP_DIRSEP "slurpd.dump"
96 /* Amount of time to sleep if no more work to do */
97 #define DEFAULT_NO_WORK_INTERVAL 3
99 /* The time we wait between checks to see if the replog file needs trimming */
100 #define TRIMCHECK_INTERVAL ( 60 * 5 )
102 /* Only try to trim slurpd replica files larger than this size */
103 #define MIN_TRIM_FILESIZE ( 10L * 1024L )
105 /* Maximum line length we can read from replication log */
106 #define REPLBUFLEN 256
111 #define TLS_CRITICAL 2
113 /* Rejection records are prefaced with this string */
114 #define ERROR_STR "ERROR"
116 /* Strings found in replication entries */
117 #define T_CHANGETYPESTR "changetype"
118 #define T_CHANGETYPE 1
119 #define T_TIMESTR "time"
124 #define T_ADDCTSTR "add"
126 #define T_MODIFYCTSTR "modify"
128 #define T_DELETECTSTR "delete"
130 #define T_MODRDNCTSTR "modrdn"
131 #define T_MODDNCTSTR "moddn"
132 #define T_RENAMECTSTR "rename"
135 #define T_MODOPADDSTR "add"
137 #define T_MODOPREPLACESTR "replace"
138 #define T_MODOPREPLACE 9
139 #define T_MODOPDELETESTR "delete"
140 #define T_MODOPDELETE 10
141 #define T_MODOPINCREMENTSTR "increment"
142 #define T_MODOPINCREMENT 11
143 #define T_MODSEPSTR "-"
146 #define T_NEWRDNSTR "newrdn"
147 #define T_DELOLDRDNSTR "deleteoldrdn"
148 #define T_NEWSUPSTR "newsuperior"
152 /* Config file keywords */
153 #define HOSTSTR "host"
155 #define ATTRSTR "attr"
156 #define SUFFIXSTR "suffix"
157 #define BINDDNSTR "binddn"
158 #define BINDMETHSTR "bindmethod"
159 #define SIMPLESTR "simple"
160 #define SASLSTR "sasl"
161 #define CREDSTR "credentials"
162 #define OLDAUTHCSTR "bindprincipal"
163 #define AUTHCSTR "authcID"
164 #define AUTHZSTR "authzID"
165 #define SRVTABSTR "srvtab"
166 #define SASLMECHSTR "saslmech"
167 #define REALMSTR "realm"
168 #define SECPROPSSTR "secprops"
169 #define STARTTLSSTR "starttls"
171 #define CRITICALSTR "critical"
173 #define REPLICA_SLEEP_TIME ( 10 )
175 /* Enumeration of various types of bind failures */
177 #define BIND_ERR_BADLDP 1
178 #define BIND_ERR_OPEN 2
179 #define BIND_ERR_BAD_ATYPE 3
180 #define BIND_ERR_SIMPLE_FAILED 4
181 #define BIND_ERR_BADRI 6
182 #define BIND_ERR_VERSION 7
183 #define BIND_ERR_REFERRALS 8
184 #define BIND_ERR_MANAGEDSAIT 9
185 #define BIND_ERR_SASL_FAILED 10
186 #define BIND_ERR_TLS_FAILED 11
188 /* Return codes for do_ldap() */
190 #define DO_LDAP_ERR_RETRYABLE 1
191 #define DO_LDAP_ERR_FATAL 2
194 * Types of counts one can request from the Rq rq_getcount()
198 #define RQ_COUNT_ALL 1
199 /* all elements with nonzero refcnt */
200 #define RQ_COUNT_NZRC 2
202 /* Amount of time, in seconds, for a thread to sleep when it encounters
203 * a retryable error in do_ldap().
205 #define RETRY_SLEEP_TIME 60
211 * ****************************************************************************
212 * Data types for replication queue and queue elements.
213 * ****************************************************************************
218 * Replica host information. An Ri struct will contain an array of these,
219 * with one entry for each replica. The end of the array is signaled
220 * by a NULL value in the rh_hostname field.
223 char *rh_hostname; /* replica hostname */
224 int rh_port; /* replica port */
229 * Per-replica information.
232 * - Private data should not be manipulated expect by Ri member functions.
234 typedef struct ri Ri;
237 char *ri_hostname; /* canonical hostname of replica */
238 int ri_port; /* port where slave slapd running */
239 char *ri_uri; /* e.g. "ldaps://ldap-1.example.com:636" */
240 LDAP *ri_ldp; /* LDAP struct for this replica */
241 int ri_tls; /* TLS: 0=no, 1=yes, 2=critical */
242 int ri_bind_method; /* AUTH_SIMPLE or AUTH_SASL */
243 char *ri_bind_dn; /* DN to bind as when replicating */
244 char *ri_password; /* Password for any method */
245 char *ri_secprops; /* SASL security properties */
246 char *ri_realm; /* realm for any mechanism */
247 char *ri_authcId; /* authentication ID for any mechanism */
248 char *ri_authzId; /* authorization ID for any mechanism */
249 char *ri_saslmech; /* SASL mechanism to use */
250 struct re *ri_curr; /* current repl entry being processed */
251 struct stel *ri_stel; /* pointer to Stel for this replica */
253 ri_seq; /* seq number of last repl */
254 ldap_pvt_thread_t ri_tid; /* ID of thread for this replica */
256 /* Member functions */
257 int (*ri_process) LDAP_P(( Ri * )); /* process the next repl entry */
258 void (*ri_wake) LDAP_P(( Ri * )); /* wake up a sleeping thread */
264 * Information about one particular modification to make. This data should
265 * be considered private to routines in re.c, and to routines in ri.c.
269 char *mi_type; /* attr or type */
270 char *mi_val; /* value */
271 int mi_len; /* length of mi_val */
277 * Information about one particular replication entry. Only routines in
278 * re.c and rq.c should touch the private data. Other routines should
279 * only use member functions.
281 typedef struct re Re;
284 ldap_pvt_thread_mutex_t
285 re_mutex; /* mutex for this Re */
286 int re_refcnt; /* ref count, 0 = done */
287 time_t re_timestamp; /* timestamp of this re */
288 int re_seq; /* sequence number */
289 Rh *re_replicas; /* array of replica info */
290 char *re_dn; /* dn of entry being modified */
291 int re_changetype; /* type of modification */
292 Mi *re_mods; /* array of modifications to make */
293 struct re *re_next; /* pointer to next element */
295 /* Public functions */
296 int (*re_free) LDAP_P(( Re * )); /* free an re struct */
297 Re *(*re_getnext) LDAP_P(( Re * )); /* return next Re in linked list */
298 int (*re_parse) LDAP_P(( Re *, char * )); /* parse replication log entry */
299 int (*re_write) LDAP_P(( Ri *, Re *, FILE * )); /* write repl. log entry */
300 void (*re_dump) LDAP_P(( Re *, FILE * )); /* debugging - print contents */
301 int (*re_lock) LDAP_P(( Re * )); /* lock this re */
302 int (*re_unlock) LDAP_P(( Re * )); /* unlock this re */
303 int (*re_decrefcnt) LDAP_P(( Re * )); /* decrement the refcnt */
304 int (*re_getrefcnt) LDAP_P(( Re * )); /* get the refcnt */
311 * Definition for the queue of replica information. Private data is
312 * private to rq.c. Other routines should only touch public data or
313 * use member functions. Note that although we have a member function
314 * for locking the queue's mutex, we need to expose the rq_mutex
315 * variable so routines in ri.c can use it as a mutex for the
316 * rq_more condition variable.
318 typedef struct rq Rq;
322 Re *rq_head; /* pointer to head */
323 Re *rq_tail; /* pointer to tail */
324 int rq_nre; /* total number of Re's in queue */
325 int rq_ndel; /* number of deleted Re's in queue */
326 time_t rq_lasttrim; /* Last time we trimmed file */
329 ldap_pvt_thread_mutex_t
330 rq_mutex; /* mutex for whole queue */
331 ldap_pvt_thread_cond_t
332 rq_more; /* condition var - more work added */
334 /* Member functions */
335 Re * (*rq_gethead) LDAP_P(( Rq * )); /* get the element at head */
336 Re * (*rq_getnext) LDAP_P(( Re * )); /* get the next element */
337 int (*rq_delhead) LDAP_P(( Rq * )); /* delete the element at head */
338 int (*rq_add) LDAP_P(( Rq *, char * )); /* add at tail */
339 void (*rq_gc) LDAP_P(( Rq * )); /* garbage-collect queue */
340 int (*rq_lock) LDAP_P(( Rq * )); /* lock the queue */
341 int (*rq_unlock) LDAP_P(( Rq * )); /* unlock the queue */
342 int (*rq_needtrim) LDAP_P(( Rq * )); /* see if queue needs trimming */
343 int (*rq_write) LDAP_P(( Rq *, FILE * )); /*write Rq contents to file*/
344 int (*rq_getcount) LDAP_P(( Rq *, int )); /* return queue counts */
345 void (*rq_dump) LDAP_P(( Rq * )); /* debugging - print contents */
350 * An Stel (status element) contains information about one replica.
351 * Stel structs are associated with the St (status) struct, defined
354 typedef struct stel {
355 char *hostname; /* host name of replica */
356 int port; /* port number of replica */
357 time_t last; /* timestamp of last successful repl */
358 int seq; /* Sequence number of last repl */
363 * An St struct in an in-core structure which contains the current
364 * slurpd state. Most importantly, it contains an array of Stel
365 * structs which contain the timestamp and sequence number of the last
366 * successful replication for each replica. The st_write() member
367 * function is called periodically to flush status information to
368 * disk. At startup time, slurpd checks for the status file, and
369 * if present, uses the timestamps to avoid "replaying" replications
370 * which have already been sent to a given replica.
372 typedef struct st St;
375 ldap_pvt_thread_mutex_t
376 st_mutex; /* mutex to serialize access */
377 Stel **st_data; /* array of pointers to Stel structs */
378 int st_nreplicas; /* number of repl hosts */
379 int st_err_logged; /* 1 if fopen err logged */
380 FILE *st_fp; /* st file kept open */
381 FILE *st_lfp; /* lockfile fp */
383 /* Public member functions */
384 int (*st_update) LDAP_P(( St *, Stel*, Re* ));/*update entry for a host*/
385 Stel*(*st_add) LDAP_P(( St *, Ri * )); /*add a new repl host*/
386 int (*st_write) LDAP_P(( St * )); /* write status to disk */
387 int (*st_read) LDAP_P(( St * )); /* read status info from disk */
388 int (*st_lock) LDAP_P(( St * )); /* read status info from disk */
389 int (*st_unlock) LDAP_P(( St * )); /* read status info from disk */
392 #if defined( HAVE_LWP )
394 thread_t tl_tid; /* thread being managed */
395 time_t tl_wake; /* time thread should be resumed */
396 struct tl *tl_next; /* next node in list */
403 #endif /* HAVE_LWP */
406 * Public functions used to instantiate and initialize queue objects.
408 extern int Ri_init LDAP_P(( Ri **ri ));
409 extern int Rq_init LDAP_P(( Rq **rq ));
410 extern int Re_init LDAP_P(( Re **re ));
412 #include "proto-slurp.h"
416 #endif /* _SLURPD_H_ */