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