Commit | Line | Data |
---|---|---|
c627d613 | 1 | /* |
a039749b | 2 | Copyright (C) by Andrew Tridgell 1996, 2000 |
c627d613 | 3 | Copyright (C) Paul Mackerras 1996 |
e420b9d8 | 4 | Copyright (C) 2001 by Martin Pool <mbp@samba.org> |
c627d613 AT |
5 | |
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
d41c7d02 DD |
21 | #define False 0 |
22 | #define True 1 | |
23 | ||
c627d613 AT |
24 | #define BLOCK_SIZE 700 |
25 | #define RSYNC_RSH_ENV "RSYNC_RSH" | |
7b8356d0 | 26 | |
c627d613 | 27 | #define RSYNC_NAME "rsync" |
f0fca04e | 28 | #define RSYNCD_CONF "/etc/rsyncd.conf" |
f0fca04e | 29 | |
5e71c444 | 30 | #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock" |
f7632fc6 AT |
31 | #define URL_PREFIX "rsync://" |
32 | ||
c627d613 AT |
33 | #define BACKUP_SUFFIX "~" |
34 | ||
1b01b295 | 35 | /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is |
6543dc0c | 36 | incompatible with older versions :-( */ |
1b01b295 AT |
37 | #define CHAR_OFFSET 0 |
38 | ||
39 | ||
3333ffbd | 40 | #define FLAG_DELETE (1<<0) |
4fe159a8 | 41 | #define SAME_MODE (1<<1) |
dc5ddbcc | 42 | #define SAME_RDEV (1<<2) |
4fe159a8 AT |
43 | #define SAME_UID (1<<3) |
44 | #define SAME_GID (1<<4) | |
45 | #define SAME_DIR (1<<5) | |
46 | #define SAME_NAME SAME_DIR | |
47 | #define LONG_NAME (1<<6) | |
48 | #define SAME_TIME (1<<7) | |
49 | ||
c627d613 | 50 | /* update this if you make incompatible changes */ |
8ada7518 | 51 | #define PROTOCOL_VERSION 24 |
ff41a59f | 52 | #define MIN_PROTOCOL_VERSION 15 |
6ba9279f | 53 | #define MAX_PROTOCOL_VERSION 30 |
c627d613 | 54 | |
9486289c AT |
55 | #define RSYNC_PORT 873 |
56 | ||
d867229b | 57 | #define SPARSE_WRITE_SIZE (1024) |
dc5ddbcc | 58 | #define WRITE_SIZE (32*1024) |
34ccb63e | 59 | #define CHUNK_SIZE (32*1024) |
4440b8aa | 60 | #define MAX_MAP_SIZE (256*1024) |
e3fe383a | 61 | #define IO_BUFFER_SIZE (4092) |
c627d613 | 62 | |
874895d5 | 63 | #define MAX_ARGS 1000 |
f0fca04e | 64 | |
8d9dc9f9 | 65 | #define MPLEX_BASE 7 |
ff41a59f | 66 | |
e420b9d8 MP |
67 | /* Log values. I *think* what these mean is: FLOG goes to the server |
68 | * logfile; FERROR and FINFO try to end up on the client, with | |
69 | * different levels of filtering. */ | |
a039749b | 70 | enum logcode {FNONE=0, FERROR=1, FINFO=2, FLOG=3 }; |
7bec6a5c | 71 | |
65417579 AT |
72 | #include "errcode.h" |
73 | ||
c627d613 AT |
74 | #include "config.h" |
75 | ||
24d95c03 AT |
76 | #if HAVE_REMSH |
77 | #define RSYNC_RSH "remsh" | |
78 | #else | |
79 | #define RSYNC_RSH "rsh" | |
80 | #endif | |
81 | ||
b557c4c7 MP |
82 | /* This tries to turn on large file support on some Unix platforms, |
83 | * making off_t be a 64-bit type, and so on. | |
84 | * | |
85 | * http://www.rs6000.ibm.com/doc_link/en_US/a_doc_lib/aixprggd/genprogc/prg_lrg_files.htm | |
86 | */ | |
87 | #define _LARGE_FILES 1 | |
88 | ||
c627d613 | 89 | #include <sys/types.h> |
6ed67e6d | 90 | |
c627d613 AT |
91 | #ifdef HAVE_UNISTD_H |
92 | #include <unistd.h> | |
93 | #endif | |
94 | #include <stdio.h> | |
3591c066 | 95 | #include <stddef.h> |
c627d613 AT |
96 | |
97 | #ifdef HAVE_SYS_PARAM_H | |
98 | #include <sys/param.h> | |
99 | #endif | |
100 | ||
d7b305fd AT |
101 | #ifdef HAVE_STDLIB_H |
102 | #include <stdlib.h> | |
103 | #endif | |
104 | ||
fdd71e17 AT |
105 | #ifdef HAVE_SYS_SOCKET_H |
106 | #include <sys/socket.h> | |
107 | #endif | |
108 | ||
d7b305fd AT |
109 | #ifdef HAVE_STRING_H |
110 | #include <string.h> | |
c627d613 AT |
111 | #endif |
112 | ||
c627d613 AT |
113 | #ifdef HAVE_MALLOC_H |
114 | #include <malloc.h> | |
115 | #endif | |
116 | ||
117 | #ifdef TIME_WITH_SYS_TIME | |
118 | #include <sys/time.h> | |
119 | #include <time.h> | |
120 | #else | |
121 | #ifdef HAVE_SYS_TIME_H | |
122 | #include <sys/time.h> | |
123 | #else | |
124 | #include <time.h> | |
125 | #endif | |
126 | #endif | |
127 | ||
128 | #ifdef HAVE_FCNTL_H | |
129 | #include <fcntl.h> | |
130 | #else | |
131 | #ifdef HAVE_SYS_FCNTL_H | |
132 | #include <sys/fcntl.h> | |
133 | #endif | |
134 | #endif | |
135 | ||
136 | #include <sys/stat.h> | |
137 | ||
94481d91 AT |
138 | #ifdef HAVE_SYS_IOCTL_H |
139 | #include <sys/ioctl.h> | |
140 | #endif | |
141 | ||
142 | #ifdef HAVE_SYS_FILIO_H | |
143 | #include <sys/filio.h> | |
144 | #endif | |
145 | ||
c627d613 AT |
146 | #include <signal.h> |
147 | #ifdef HAVE_SYS_WAIT_H | |
148 | #include <sys/wait.h> | |
149 | #endif | |
150 | #ifdef HAVE_CTYPE_H | |
151 | #include <ctype.h> | |
152 | #endif | |
153 | #ifdef HAVE_GRP_H | |
154 | #include <grp.h> | |
155 | #endif | |
156 | #include <errno.h> | |
157 | ||
c627d613 AT |
158 | #ifdef HAVE_UTIME_H |
159 | #include <utime.h> | |
160 | #endif | |
161 | ||
8bf73749 AT |
162 | #ifdef HAVE_SYS_SELECT_H |
163 | #include <sys/select.h> | |
164 | #endif | |
165 | ||
773f2bd4 AT |
166 | #ifdef HAVE_SYS_MODE_H |
167 | /* apparently AIX needs this for S_ISLNK */ | |
05a6556d | 168 | #ifndef S_ISLNK |
773f2bd4 AT |
169 | #include <sys/mode.h> |
170 | #endif | |
05a6556d | 171 | #endif |
773f2bd4 | 172 | |
c627d613 AT |
173 | #ifdef HAVE_FNMATCH |
174 | #include <fnmatch.h> | |
175 | #else | |
176 | #include "lib/fnmatch.h" | |
177 | #endif | |
178 | ||
932be9aa | 179 | #ifdef HAVE_GLOB_H |
874895d5 AT |
180 | #include <glob.h> |
181 | #endif | |
182 | ||
f6c34742 AT |
183 | /* these are needed for the uid/gid mapping code */ |
184 | #include <pwd.h> | |
185 | #include <grp.h> | |
186 | ||
9486289c | 187 | #include <stdarg.h> |
f0fca04e AT |
188 | #include <netinet/in.h> |
189 | #include <arpa/inet.h> | |
190 | #include <netdb.h> | |
ff8b29b8 | 191 | #include <syslog.h> |
6c8f5373 | 192 | #include <sys/file.h> |
9486289c | 193 | |
e8f5b936 AT |
194 | #if HAVE_DIRENT_H |
195 | # include <dirent.h> | |
196 | #else | |
197 | # define dirent direct | |
198 | # if HAVE_SYS_NDIR_H | |
199 | # include <sys/ndir.h> | |
200 | # endif | |
201 | # if HAVE_SYS_DIR_H | |
202 | # include <sys/dir.h> | |
203 | # endif | |
204 | # if HAVE_NDIR_H | |
205 | # include <ndir.h> | |
206 | # endif | |
207 | #endif | |
208 | ||
209 | #ifdef HAVE_COMPAT_H | |
210 | #include <compat.h> | |
211 | #endif | |
212 | ||
213 | ||
f0fca04e AT |
214 | #define BOOL int |
215 | ||
c627d613 AT |
216 | #ifndef uchar |
217 | #define uchar unsigned char | |
218 | #endif | |
219 | ||
debb4505 AT |
220 | #if HAVE_UNSIGNED_CHAR |
221 | #define schar signed char | |
222 | #else | |
223 | #define schar char | |
224 | #endif | |
225 | ||
c627d613 AT |
226 | #ifndef int32 |
227 | #if (SIZEOF_INT == 4) | |
228 | #define int32 int | |
229 | #elif (SIZEOF_LONG == 4) | |
230 | #define int32 long | |
231 | #elif (SIZEOF_SHORT == 4) | |
232 | #define int32 short | |
8de330a3 AT |
233 | #else |
234 | /* I hope this works */ | |
235 | #define int32 int | |
f3737e06 | 236 | #define LARGE_INT32 |
c627d613 AT |
237 | #endif |
238 | #endif | |
239 | ||
240 | #ifndef uint32 | |
241 | #define uint32 unsigned int32 | |
242 | #endif | |
243 | ||
bcacc18b AT |
244 | #if HAVE_OFF64_T |
245 | #define OFF_T off64_t | |
246 | #define STRUCT_STAT struct stat64 | |
247 | #else | |
248 | #define OFF_T off_t | |
249 | #define STRUCT_STAT struct stat | |
250 | #endif | |
251 | ||
252 | #if HAVE_OFF64_T | |
253 | #define int64 off64_t | |
254 | #elif (SIZEOF_LONG == 8) | |
3bee6733 AT |
255 | #define int64 long |
256 | #elif (SIZEOF_INT == 8) | |
257 | #define int64 int | |
258 | #elif HAVE_LONGLONG | |
71c46176 AT |
259 | #define int64 long long |
260 | #else | |
261 | #define int64 off_t | |
3bee6733 | 262 | #define NO_INT64 |
71c46176 | 263 | #endif |
c627d613 | 264 | |
c29ee43d AT |
265 | #if HAVE_SHORT_INO_T |
266 | #define INO_T uint32 | |
267 | #else | |
268 | #define INO_T ino_t | |
269 | #endif | |
270 | ||
c627d613 AT |
271 | #ifndef MIN |
272 | #define MIN(a,b) ((a)<(b)?(a):(b)) | |
273 | #endif | |
274 | ||
275 | #ifndef MAX | |
276 | #define MAX(a,b) ((a)>(b)?(a):(b)) | |
277 | #endif | |
278 | ||
d41c7d02 DD |
279 | #ifndef MAXHOSTNAMELEN |
280 | #define MAXHOSTNAMELEN 256 | |
281 | #endif | |
282 | ||
c627d613 | 283 | /* the length of the md4 checksum */ |
ebb0a6f6 | 284 | #define MD4_SUM_LENGTH 16 |
c627d613 AT |
285 | #define SUM_LENGTH 16 |
286 | ||
287 | #ifndef MAXPATHLEN | |
288 | #define MAXPATHLEN 1024 | |
289 | #endif | |
290 | ||
0473e2a1 AT |
291 | #ifndef INADDR_NONE |
292 | #define INADDR_NONE 0xffffffff | |
293 | #endif | |
294 | ||
c627d613 | 295 | struct file_struct { |
3333ffbd | 296 | unsigned flags; |
3ec4dd97 | 297 | time_t modtime; |
bcacc18b | 298 | OFF_T length; |
3ec4dd97 | 299 | mode_t mode; |
c29ee43d | 300 | INO_T inode; |
3ec4dd97 AT |
301 | dev_t dev; |
302 | dev_t rdev; | |
303 | uid_t uid; | |
304 | gid_t gid; | |
305 | char *basename; | |
306 | char *dirname; | |
307 | char *basedir; | |
308 | char *link; | |
2d0bb8eb | 309 | char *sum; |
c627d613 AT |
310 | }; |
311 | ||
4440b8aa | 312 | |
3d382777 AT |
313 | #define ARENA_SIZE (32 * 1024) |
314 | ||
315 | struct string_area { | |
316 | char *base; | |
317 | char *end; | |
318 | char *current; | |
319 | struct string_area *next; | |
320 | }; | |
321 | ||
c627d613 | 322 | struct file_list { |
a800434a AT |
323 | int count; |
324 | int malloced; | |
325 | struct file_struct **files; | |
3d382777 | 326 | struct string_area *string_area; |
c627d613 AT |
327 | }; |
328 | ||
329 | struct sum_buf { | |
c29ee43d AT |
330 | OFF_T offset; /* offset in file of this chunk */ |
331 | int len; /* length of chunk of file */ | |
332 | int i; /* index of this chunk */ | |
333 | uint32 sum1; /* simple checksum */ | |
334 | char sum2[SUM_LENGTH]; /* checksum */ | |
c627d613 AT |
335 | }; |
336 | ||
337 | struct sum_struct { | |
bcacc18b | 338 | OFF_T flength; /* total file length */ |
c627d613 AT |
339 | int count; /* how many chunks */ |
340 | int remainder; /* flength % block_length */ | |
341 | int n; /* block_length */ | |
342 | struct sum_buf *sums; /* points to info for each chunk */ | |
343 | }; | |
344 | ||
c6e7fcb4 | 345 | struct map_struct { |
4440b8aa | 346 | char *p; |
0b910560 | 347 | int fd,p_size,p_len; |
4440b8aa | 348 | OFF_T file_size, p_offset, p_fd_offset; |
c6e7fcb4 | 349 | }; |
c627d613 | 350 | |
2b6b4d53 | 351 | struct exclude_struct { |
2b6b4d53 AT |
352 | char *pattern; |
353 | int regular_exp; | |
a8b9d4ed | 354 | int fnmatch_flags; |
2b6b4d53 AT |
355 | int include; |
356 | int directory; | |
357 | int local; | |
358 | }; | |
359 | ||
a800434a AT |
360 | struct stats { |
361 | int64 total_size; | |
362 | int64 total_transferred_size; | |
363 | int64 total_written; | |
364 | int64 total_read; | |
365 | int64 literal_data; | |
366 | int64 matched_data; | |
367 | int flist_size; | |
368 | int num_files; | |
369 | int num_transferred_files; | |
370 | }; | |
371 | ||
2b6b4d53 | 372 | |
3b3a2fbc AT |
373 | /* we need this function because of the silly way in which duplicate |
374 | entries are handled in the file lists - we can't change this | |
375 | without breaking existing versions */ | |
71c46176 | 376 | static inline int flist_up(struct file_list *flist, int i) |
3b3a2fbc | 377 | { |
3ec4dd97 | 378 | while (!flist->files[i]->basename) i++; |
3b3a2fbc AT |
379 | return i; |
380 | } | |
381 | ||
c627d613 AT |
382 | #include "byteorder.h" |
383 | #include "version.h" | |
384 | #include "proto.h" | |
8de330a3 | 385 | #include "lib/mdfour.h" |
c627d613 AT |
386 | |
387 | #if !HAVE_STRERROR | |
388 | extern char *sys_errlist[]; | |
389 | #define strerror(i) sys_errlist[i] | |
390 | #endif | |
391 | ||
392 | #ifndef HAVE_STRCHR | |
393 | # define strchr index | |
394 | # define strrchr rindex | |
395 | #endif | |
396 | ||
c627d613 AT |
397 | #ifndef HAVE_ERRNO_DECL |
398 | extern int errno; | |
399 | #endif | |
400 | ||
cbbe4892 | 401 | #define SUPPORT_LINKS HAVE_READLINK |
dc5ddbcc | 402 | #define SUPPORT_HARD_LINKS HAVE_LINK |
c627d613 | 403 | |
8bf73749 AT |
404 | #ifndef HAVE_LCHOWN |
405 | #define lchown chown | |
406 | #endif | |
407 | ||
ac1eb754 | 408 | #define SIGNAL_CAST (RETSIGTYPE (*)()) |
720b47f2 AT |
409 | |
410 | #ifndef EWOULDBLOCK | |
411 | #define EWOULDBLOCK EAGAIN | |
412 | #endif | |
182dca5c | 413 | |
7b8356d0 AT |
414 | #ifndef STDIN_FILENO |
415 | #define STDIN_FILENO 0 | |
416 | #endif | |
417 | ||
418 | #ifndef STDOUT_FILENO | |
419 | #define STDOUT_FILENO 1 | |
420 | #endif | |
421 | ||
422 | #ifndef STDERR_FILENO | |
423 | #define STDERR_FILENO 2 | |
424 | #endif | |
425 | ||
426 | #ifndef S_IWUSR | |
427 | #define S_IWUSR 0200 | |
428 | #endif | |
429 | ||
b280a1f4 AT |
430 | #ifndef _S_IFMT |
431 | #define _S_IFMT 0170000 | |
432 | #endif | |
433 | ||
434 | #ifndef _S_IFLNK | |
435 | #define _S_IFLNK 0120000 | |
436 | #endif | |
437 | ||
438 | #ifndef S_ISLNK | |
439 | #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK)) | |
440 | #endif | |
441 | ||
1e9f155a AT |
442 | #ifndef S_ISBLK |
443 | #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK)) | |
444 | #endif | |
445 | ||
446 | #ifndef S_ISCHR | |
447 | #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR)) | |
448 | #endif | |
449 | ||
a0b65b18 AT |
450 | #ifndef S_ISSOCK |
451 | #ifdef _S_IFSOCK | |
452 | #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK)) | |
453 | #else | |
454 | #define S_ISSOCK(mode) (0) | |
455 | #endif | |
456 | #endif | |
457 | ||
458 | #ifndef S_ISFIFO | |
459 | #ifdef _S_IFIFO | |
460 | #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO)) | |
461 | #else | |
462 | #define S_ISFIFO(mode) (0) | |
463 | #endif | |
464 | #endif | |
465 | ||
1e9f155a AT |
466 | #ifndef S_ISDIR |
467 | #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR)) | |
468 | #endif | |
469 | ||
470 | #ifndef S_ISREG | |
471 | #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG)) | |
472 | #endif | |
473 | ||
f0359dd0 AT |
474 | /* work out what fcntl flag to use for non-blocking */ |
475 | #ifdef O_NONBLOCK | |
476 | # define NONBLOCK_FLAG O_NONBLOCK | |
477 | #elif defined(SYSV) | |
478 | # define NONBLOCK_FLAG O_NDELAY | |
479 | #else | |
480 | # define NONBLOCK_FLAG FNDELAY | |
481 | #endif | |
482 | ||
a0b65b18 AT |
483 | |
484 | #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode)) | |
7bec6a5c | 485 | |
972a3619 DD |
486 | #ifndef ACCESSPERMS |
487 | #define ACCESSPERMS 0777 | |
488 | #endif | |
5afd8aed DD |
489 | /* Initial mask on permissions given to temporary files. Mask off setuid |
490 | bits and group access because of potential race-condition security | |
491 | holes, and mask other access because mode 707 is bizarre */ | |
972a3619 | 492 | #define INITACCESSPERMS 0700 |
e08bfe12 AT |
493 | |
494 | /* handler for null strings in printf format */ | |
495 | #define NS(s) ((s)?(s):"<NULL>") | |
496 | ||
497 | /* use magic gcc attributes to catch format errors */ | |
ff41a59f | 498 | void rprintf(enum logcode , const char *, ...) |
e08bfe12 AT |
499 | #ifdef __GNUC__ |
500 | __attribute__ ((format (printf, 2, 3))) | |
501 | #endif | |
502 | ; | |
7b3d4257 | 503 | |
a039749b MP |
504 | /* This is just like rprintf, but it also tries to print some |
505 | * representation of the error code. Normally errcode = errno. */ | |
506 | void rsyserr(enum logcode, int, const char *, ...) | |
507 | #ifdef __GNUC__ | |
508 | __attribute__ ((format (printf, 3, 4))) | |
509 | #endif | |
510 | ; | |
511 | ||
7b3d4257 AT |
512 | #ifdef REPLACE_INET_NTOA |
513 | #define inet_ntoa rep_inet_ntoa | |
514 | #endif | |
5a788ade AT |
515 | |
516 | ||
517 | #ifndef HAVE_STRLCPY | |
518 | size_t strlcpy(char *d, const char *s, size_t bufsize); | |
519 | #endif | |
520 | ||
521 | #ifndef HAVE_STRLCAT | |
522 | size_t strlcat(char *d, const char *s, size_t bufsize); | |
523 | #endif | |
524 | ||
82980a23 AT |
525 | #ifndef WEXITSTATUS |
526 | #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF)) | |
527 | #endif | |
528 | ||
a9766ef1 | 529 | #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__) |