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