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