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