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