configuration parsing and loading code for rsyncd. This is based
[rsync/rsync.git] / rsync.h
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #define BLOCK_SIZE 700
21 #define RSYNC_RSH_ENV "RSYNC_RSH"
22
23 #define RSYNC_NAME "rsync"
24 #define RSYNCD_CONF "/etc/rsyncd.conf"
25 #define RSYNCD_LOG "/var/adm/rsyncd.log"
26
27 #define BACKUP_SUFFIX "~"
28
29 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
30    imcompatible with older versions :-( */
31 #define CHAR_OFFSET 0
32
33
34 #define FLAG_DELETE (1<<0)
35 #define SAME_MODE (1<<1)
36 #define SAME_RDEV (1<<2)
37 #define SAME_UID (1<<3)
38 #define SAME_GID (1<<4)
39 #define SAME_DIR (1<<5)
40 #define SAME_NAME SAME_DIR
41 #define LONG_NAME (1<<6)
42 #define SAME_TIME (1<<7)
43
44 /* update this if you make incompatible changes */
45 #define PROTOCOL_VERSION 17
46 #define MIN_PROTOCOL_VERSION 11
47 #define MAX_PROTOCOL_VERSION 30
48
49 #define RSYNC_PORT 873
50
51 #define SPARSE_WRITE_SIZE (1024)
52 #define WRITE_SIZE (32*1024)
53 #define CHUNK_SIZE (32*1024)
54 #define MAX_MAP_SIZE (4*1024*1024)
55
56 #define MAX_ARGS 100
57
58 #define BLOCKING_TIMEOUT 10
59
60 #define FERROR 1
61 #define FINFO 2
62
63 #include "config.h"
64
65 #if HAVE_REMSH
66 #define RSYNC_RSH "remsh"
67 #else
68 #define RSYNC_RSH "rsh"
69 #endif
70
71 #include <sys/types.h>
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75 #include <stdio.h>
76
77 #ifdef HAVE_SYS_PARAM_H
78 #include <sys/param.h>
79 #endif
80
81 #ifdef HAVE_STDLIB_H
82 #include <stdlib.h>
83 #endif
84
85 #ifdef HAVE_SYS_SOCKET_H
86 #include <sys/socket.h>
87 #endif
88
89 #ifdef HAVE_STRING_H
90 #include <string.h>
91 #endif
92
93 #ifdef HAVE_COMPAT_H
94 #include <compat.h>
95 #endif
96
97 #ifdef HAVE_MALLOC_H
98 #include <malloc.h>
99 #endif
100
101 #ifdef TIME_WITH_SYS_TIME
102 #include <sys/time.h>
103 #include <time.h>
104 #else
105 #ifdef HAVE_SYS_TIME_H
106 #include <sys/time.h>
107 #else
108 #include <time.h>
109 #endif
110 #endif
111
112 #ifdef HAVE_FCNTL_H
113 #include <fcntl.h>
114 #else
115 #ifdef HAVE_SYS_FCNTL_H
116 #include <sys/fcntl.h>
117 #endif
118 #endif
119
120 #include <sys/stat.h>
121
122 #ifdef HAVE_SYS_IOCTL_H
123 #include <sys/ioctl.h>
124 #endif
125
126 #ifdef HAVE_SYS_FILIO_H
127 #include <sys/filio.h>
128 #endif
129
130 #include <signal.h>
131 #ifdef HAVE_SYS_WAIT_H
132 #include <sys/wait.h>
133 #endif
134 #ifdef HAVE_CTYPE_H
135 #include <ctype.h>
136 #endif
137 #ifdef HAVE_GRP_H
138 #include <grp.h>
139 #endif
140 #include <errno.h>
141
142 #ifdef HAVE_MMAP
143 #include <sys/mman.h>
144 #endif
145
146 #ifdef HAVE_UTIME_H
147 #include <utime.h>
148 #endif
149
150 #ifdef HAVE_SYS_SELECT_H
151 #include <sys/select.h>
152 #endif
153
154 #ifdef HAVE_SYS_MODE_H
155 /* apparently AIX needs this for S_ISLNK */
156 #ifndef S_ISLNK
157 #include <sys/mode.h>
158 #endif
159 #endif
160
161 #ifdef HAVE_FNMATCH
162 #include <fnmatch.h>
163 #else
164 #include "lib/fnmatch.h"
165 #endif
166
167 #ifdef HAVE_GETOPT_LONG
168 #include <getopt.h>
169 #else
170 #include "lib/getopt.h"
171 #endif
172
173 /* these are needed for the uid/gid mapping code */
174 #include <pwd.h>
175 #include <grp.h>
176
177 #include <stdarg.h>
178 #include <netinet/in.h>
179 #include <arpa/inet.h>
180 #include <netdb.h>
181
182 #ifndef S_IFLNK
183 #define S_IFLNK  0120000
184 #endif
185
186 #ifndef S_ISLNK
187 #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK)
188 #endif
189
190 #define BOOL int
191
192 #ifndef uchar
193 #define uchar unsigned char
194 #endif
195
196 #if HAVE_UNSIGNED_CHAR
197 #define schar signed char
198 #else
199 #define schar char
200 #endif
201
202 #ifndef int32
203 #if (SIZEOF_INT == 4)
204 #define int32 int
205 #elif (SIZEOF_LONG == 4)
206 #define int32 long
207 #elif (SIZEOF_SHORT == 4)
208 #define int32 short
209 #endif
210 #endif
211
212 #ifndef uint32
213 #define uint32 unsigned int32
214 #endif
215
216 #if HAVE_OFF64_T
217 #define OFF_T off64_t
218 #define STRUCT_STAT struct stat64
219 #else
220 #define OFF_T off_t
221 #define STRUCT_STAT struct stat
222 #endif
223
224 #if HAVE_OFF64_T
225 #define int64 off64_t
226 #elif (SIZEOF_LONG == 8) 
227 #define int64 long
228 #elif (SIZEOF_INT == 8) 
229 #define int64 int
230 #elif HAVE_LONGLONG
231 #define int64 long long
232 #else
233 #define int64 off_t
234 #define NO_INT64
235 #endif
236
237 #ifndef MIN
238 #define MIN(a,b) ((a)<(b)?(a):(b))
239 #endif
240
241 #ifndef MAX
242 #define MAX(a,b) ((a)>(b)?(a):(b))
243 #endif
244
245 /* the length of the md4 checksum */
246 #define MD4_SUM_LENGTH 16
247 #define SUM_LENGTH 16
248
249 #ifndef MAXPATHLEN
250 #define MAXPATHLEN 1024
251 #endif
252
253 struct file_struct {
254         unsigned flags;
255         time_t modtime;
256         OFF_T length;
257         mode_t mode;
258         ino_t inode;
259         dev_t dev;
260         dev_t rdev;
261         uid_t uid;
262         gid_t gid;
263         char *basename;
264         char *dirname;
265         char *basedir;
266         char *link;
267         char *sum;
268 };
269
270 struct file_list {
271   int count;
272   int malloced;
273   struct file_struct **files;
274 };
275
276 struct sum_buf {
277   OFF_T offset;                 /* offset in file of this chunk */
278   int len;                      /* length of chunk of file */
279   int i;                        /* index of this chunk */
280   uint32 sum1;                  /* simple checksum */
281   char sum2[SUM_LENGTH];        /* checksum  */
282 };
283
284 struct sum_struct {
285   OFF_T flength;                /* total file length */
286   int count;                    /* how many chunks */
287   int remainder;                /* flength % block_length */
288   int n;                        /* block_length */
289   struct sum_buf *sums;         /* points to info for each chunk */
290 };
291
292 struct map_struct {
293         char *map,*p;
294         int fd,p_size,p_len;
295         OFF_T size, p_offset;
296 };
297
298 /* we need this function because of the silly way in which duplicate
299    entries are handled in the file lists - we can't change this
300    without breaking existing versions */
301 static inline int flist_up(struct file_list *flist, int i)
302 {
303         while (!flist->files[i]->basename) i++;
304         return i;
305 }
306
307 #if HAVE_DIRENT_H
308 # include <dirent.h>
309 #else
310 # define dirent direct
311 # if HAVE_SYS_NDIR_H
312 #  include <sys/ndir.h>
313 # endif
314 # if HAVE_SYS_DIR_H
315 #  include <sys/dir.h>
316 # endif
317 # if HAVE_NDIR_H
318 #  include <ndir.h>
319 # endif
320 #endif
321
322 #include "byteorder.h"
323 #include "version.h"
324 #include "proto.h"
325 #include "md4.h"
326
327 #if !HAVE_STRERROR
328 extern char *sys_errlist[];
329 #define strerror(i) sys_errlist[i]
330 #endif
331
332 #ifndef HAVE_STRCHR
333 # define strchr                 index
334 # define strrchr                rindex
335 #endif
336
337 #ifndef HAVE_ERRNO_DECL
338 extern int errno;
339 #endif
340
341 #ifndef HAVE_BCOPY
342 #define bcopy(src,dest,n) memcpy(dest,src,n)
343 #endif
344
345 #ifndef HAVE_BZERO
346 #define bzero(buf,n) memset(buf,0,n)
347 #endif
348
349 #define SUPPORT_LINKS HAVE_READLINK
350 #define SUPPORT_HARD_LINKS HAVE_LINK
351
352 #ifndef HAVE_LCHOWN
353 #define lchown chown
354 #endif
355
356 #define SIGNAL_CAST (RETSIGTYPE (*)())
357
358 #ifndef EWOULDBLOCK
359 #define EWOULDBLOCK EAGAIN
360 #endif
361
362 #ifndef STDIN_FILENO
363 #define STDIN_FILENO 0
364 #endif
365
366 #ifndef STDOUT_FILENO
367 #define STDOUT_FILENO 1
368 #endif
369
370 #ifndef STDERR_FILENO
371 #define STDERR_FILENO 2
372 #endif
373
374 #ifndef S_IWUSR
375 #define S_IWUSR 0200
376 #endif
377
378 #ifndef S_ISBLK
379 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
380 #endif
381
382 #ifndef S_ISCHR
383 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
384 #endif
385
386 #ifndef S_ISSOCK
387 #ifdef _S_IFSOCK
388 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
389 #else
390 #define S_ISSOCK(mode) (0)
391 #endif
392 #endif
393
394 #ifndef S_ISFIFO
395 #ifdef _S_IFIFO
396 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
397 #else
398 #define S_ISFIFO(mode) (0)
399 #endif
400 #endif
401
402 #ifndef S_ISDIR
403 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
404 #endif
405
406 #ifndef S_ISREG
407 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
408 #endif
409
410
411 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
412