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