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