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