| 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 | |
| 26 | #define URL_PREFIX "rsync://" |
| 27 | |
| 28 | #define BACKUP_SUFFIX "~" |
| 29 | |
| 30 | /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is |
| 31 | imcompatible with older versions :-( */ |
| 32 | #define CHAR_OFFSET 0 |
| 33 | |
| 34 | |
| 35 | #define FLAG_DELETE (1<<0) |
| 36 | #define SAME_MODE (1<<1) |
| 37 | #define SAME_RDEV (1<<2) |
| 38 | #define SAME_UID (1<<3) |
| 39 | #define SAME_GID (1<<4) |
| 40 | #define SAME_DIR (1<<5) |
| 41 | #define SAME_NAME SAME_DIR |
| 42 | #define LONG_NAME (1<<6) |
| 43 | #define SAME_TIME (1<<7) |
| 44 | |
| 45 | /* update this if you make incompatible changes */ |
| 46 | #define PROTOCOL_VERSION 19 |
| 47 | #define MIN_PROTOCOL_VERSION 11 |
| 48 | #define MAX_PROTOCOL_VERSION 30 |
| 49 | |
| 50 | #define RSYNC_PORT 873 |
| 51 | |
| 52 | #define SPARSE_WRITE_SIZE (1024) |
| 53 | #define WRITE_SIZE (32*1024) |
| 54 | #define CHUNK_SIZE (32*1024) |
| 55 | #define MAX_MAP_SIZE (1*1024*1024) |
| 56 | #define IO_BUFFER_SIZE (4092) |
| 57 | #define MAX_READ_BUFFER (1024*1024) |
| 58 | |
| 59 | #define MAX_ARGS 1000 |
| 60 | |
| 61 | #define MPLEX_BASE 7 |
| 62 | #define FERROR 1 |
| 63 | #define FINFO 2 |
| 64 | #define FLOG 3 |
| 65 | |
| 66 | #include "errcode.h" |
| 67 | |
| 68 | #include "config.h" |
| 69 | |
| 70 | #if HAVE_REMSH |
| 71 | #define RSYNC_RSH "remsh" |
| 72 | #else |
| 73 | #define RSYNC_RSH "rsh" |
| 74 | #endif |
| 75 | |
| 76 | #include <sys/types.h> |
| 77 | |
| 78 | #ifdef HAVE_GETOPT_LONG |
| 79 | #include <getopt.h> |
| 80 | #else |
| 81 | #include "lib/getopt.h" |
| 82 | #endif |
| 83 | |
| 84 | #ifdef HAVE_UNISTD_H |
| 85 | #include <unistd.h> |
| 86 | #endif |
| 87 | #include <stdio.h> |
| 88 | #include <stddef.h> |
| 89 | |
| 90 | #ifdef HAVE_SYS_PARAM_H |
| 91 | #include <sys/param.h> |
| 92 | #endif |
| 93 | |
| 94 | #ifdef HAVE_STDLIB_H |
| 95 | #include <stdlib.h> |
| 96 | #endif |
| 97 | |
| 98 | #ifdef HAVE_SYS_SOCKET_H |
| 99 | #include <sys/socket.h> |
| 100 | #endif |
| 101 | |
| 102 | #ifdef HAVE_STRING_H |
| 103 | #include <string.h> |
| 104 | #endif |
| 105 | |
| 106 | #ifdef HAVE_MALLOC_H |
| 107 | #include <malloc.h> |
| 108 | #endif |
| 109 | |
| 110 | #ifdef TIME_WITH_SYS_TIME |
| 111 | #include <sys/time.h> |
| 112 | #include <time.h> |
| 113 | #else |
| 114 | #ifdef HAVE_SYS_TIME_H |
| 115 | #include <sys/time.h> |
| 116 | #else |
| 117 | #include <time.h> |
| 118 | #endif |
| 119 | #endif |
| 120 | |
| 121 | #ifdef HAVE_FCNTL_H |
| 122 | #include <fcntl.h> |
| 123 | #else |
| 124 | #ifdef HAVE_SYS_FCNTL_H |
| 125 | #include <sys/fcntl.h> |
| 126 | #endif |
| 127 | #endif |
| 128 | |
| 129 | #include <sys/stat.h> |
| 130 | |
| 131 | #ifdef HAVE_SYS_IOCTL_H |
| 132 | #include <sys/ioctl.h> |
| 133 | #endif |
| 134 | |
| 135 | #ifdef HAVE_SYS_FILIO_H |
| 136 | #include <sys/filio.h> |
| 137 | #endif |
| 138 | |
| 139 | #include <signal.h> |
| 140 | #ifdef HAVE_SYS_WAIT_H |
| 141 | #include <sys/wait.h> |
| 142 | #endif |
| 143 | #ifdef HAVE_CTYPE_H |
| 144 | #include <ctype.h> |
| 145 | #endif |
| 146 | #ifdef HAVE_GRP_H |
| 147 | #include <grp.h> |
| 148 | #endif |
| 149 | #include <errno.h> |
| 150 | |
| 151 | #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP) |
| 152 | #include <sys/mman.h> |
| 153 | #define USE_MMAP 1 |
| 154 | #endif |
| 155 | |
| 156 | #ifdef HAVE_UTIME_H |
| 157 | #include <utime.h> |
| 158 | #endif |
| 159 | |
| 160 | #ifdef HAVE_SYS_SELECT_H |
| 161 | #include <sys/select.h> |
| 162 | #endif |
| 163 | |
| 164 | #ifdef HAVE_SYS_MODE_H |
| 165 | /* apparently AIX needs this for S_ISLNK */ |
| 166 | #ifndef S_ISLNK |
| 167 | #include <sys/mode.h> |
| 168 | #endif |
| 169 | #endif |
| 170 | |
| 171 | #ifdef HAVE_FNMATCH |
| 172 | #include <fnmatch.h> |
| 173 | #else |
| 174 | #include "lib/fnmatch.h" |
| 175 | #endif |
| 176 | |
| 177 | #ifdef HAVE_GLOB_H |
| 178 | #include <glob.h> |
| 179 | #endif |
| 180 | |
| 181 | /* these are needed for the uid/gid mapping code */ |
| 182 | #include <pwd.h> |
| 183 | #include <grp.h> |
| 184 | |
| 185 | #include <stdarg.h> |
| 186 | #include <netinet/in.h> |
| 187 | #include <arpa/inet.h> |
| 188 | #include <netdb.h> |
| 189 | #include <syslog.h> |
| 190 | #include <sys/file.h> |
| 191 | |
| 192 | #if HAVE_DIRENT_H |
| 193 | # include <dirent.h> |
| 194 | #else |
| 195 | # define dirent direct |
| 196 | # if HAVE_SYS_NDIR_H |
| 197 | # include <sys/ndir.h> |
| 198 | # endif |
| 199 | # if HAVE_SYS_DIR_H |
| 200 | # include <sys/dir.h> |
| 201 | # endif |
| 202 | # if HAVE_NDIR_H |
| 203 | # include <ndir.h> |
| 204 | # endif |
| 205 | #endif |
| 206 | |
| 207 | #ifdef HAVE_COMPAT_H |
| 208 | #include <compat.h> |
| 209 | #endif |
| 210 | |
| 211 | |
| 212 | #define BOOL int |
| 213 | |
| 214 | #ifndef uchar |
| 215 | #define uchar unsigned char |
| 216 | #endif |
| 217 | |
| 218 | #if HAVE_UNSIGNED_CHAR |
| 219 | #define schar signed char |
| 220 | #else |
| 221 | #define schar char |
| 222 | #endif |
| 223 | |
| 224 | #ifndef int32 |
| 225 | #if (SIZEOF_INT == 4) |
| 226 | #define int32 int |
| 227 | #elif (SIZEOF_LONG == 4) |
| 228 | #define int32 long |
| 229 | #elif (SIZEOF_SHORT == 4) |
| 230 | #define int32 short |
| 231 | #else |
| 232 | /* I hope this works */ |
| 233 | #define int32 int |
| 234 | #define LARGE_INT32 |
| 235 | #endif |
| 236 | #endif |
| 237 | |
| 238 | #ifndef uint32 |
| 239 | #define uint32 unsigned int32 |
| 240 | #endif |
| 241 | |
| 242 | #if HAVE_OFF64_T |
| 243 | #define OFF_T off64_t |
| 244 | #define STRUCT_STAT struct stat64 |
| 245 | #else |
| 246 | #define OFF_T off_t |
| 247 | #define STRUCT_STAT struct stat |
| 248 | #endif |
| 249 | |
| 250 | #if HAVE_OFF64_T |
| 251 | #define int64 off64_t |
| 252 | #elif (SIZEOF_LONG == 8) |
| 253 | #define int64 long |
| 254 | #elif (SIZEOF_INT == 8) |
| 255 | #define int64 int |
| 256 | #elif HAVE_LONGLONG |
| 257 | #define int64 long long |
| 258 | #else |
| 259 | #define int64 off_t |
| 260 | #define NO_INT64 |
| 261 | #endif |
| 262 | |
| 263 | #if HAVE_SHORT_INO_T |
| 264 | #define INO_T uint32 |
| 265 | #else |
| 266 | #define INO_T ino_t |
| 267 | #endif |
| 268 | |
| 269 | #ifndef MIN |
| 270 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
| 271 | #endif |
| 272 | |
| 273 | #ifndef MAX |
| 274 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
| 275 | #endif |
| 276 | |
| 277 | /* the length of the md4 checksum */ |
| 278 | #define MD4_SUM_LENGTH 16 |
| 279 | #define SUM_LENGTH 16 |
| 280 | |
| 281 | #ifndef MAXPATHLEN |
| 282 | #define MAXPATHLEN 1024 |
| 283 | #endif |
| 284 | |
| 285 | #ifndef INADDR_NONE |
| 286 | #define INADDR_NONE 0xffffffff |
| 287 | #endif |
| 288 | |
| 289 | struct file_struct { |
| 290 | unsigned flags; |
| 291 | time_t modtime; |
| 292 | OFF_T length; |
| 293 | mode_t mode; |
| 294 | INO_T inode; |
| 295 | dev_t dev; |
| 296 | dev_t rdev; |
| 297 | uid_t uid; |
| 298 | gid_t gid; |
| 299 | char *basename; |
| 300 | char *dirname; |
| 301 | char *basedir; |
| 302 | char *link; |
| 303 | char *sum; |
| 304 | }; |
| 305 | |
| 306 | struct file_list { |
| 307 | int count; |
| 308 | int malloced; |
| 309 | struct file_struct **files; |
| 310 | }; |
| 311 | |
| 312 | struct sum_buf { |
| 313 | OFF_T offset; /* offset in file of this chunk */ |
| 314 | int len; /* length of chunk of file */ |
| 315 | int i; /* index of this chunk */ |
| 316 | uint32 sum1; /* simple checksum */ |
| 317 | char sum2[SUM_LENGTH]; /* checksum */ |
| 318 | }; |
| 319 | |
| 320 | struct sum_struct { |
| 321 | OFF_T flength; /* total file length */ |
| 322 | int count; /* how many chunks */ |
| 323 | int remainder; /* flength % block_length */ |
| 324 | int n; /* block_length */ |
| 325 | struct sum_buf *sums; /* points to info for each chunk */ |
| 326 | }; |
| 327 | |
| 328 | struct map_struct { |
| 329 | char *map,*p; |
| 330 | int fd,p_size,p_len; |
| 331 | OFF_T size, p_offset; |
| 332 | }; |
| 333 | |
| 334 | struct exclude_struct { |
| 335 | char *orig; |
| 336 | char *pattern; |
| 337 | int regular_exp; |
| 338 | int include; |
| 339 | int directory; |
| 340 | int local; |
| 341 | }; |
| 342 | |
| 343 | struct stats { |
| 344 | int64 total_size; |
| 345 | int64 total_transferred_size; |
| 346 | int64 total_written; |
| 347 | int64 total_read; |
| 348 | int64 literal_data; |
| 349 | int64 matched_data; |
| 350 | int flist_size; |
| 351 | int num_files; |
| 352 | int num_transferred_files; |
| 353 | }; |
| 354 | |
| 355 | |
| 356 | /* we need this function because of the silly way in which duplicate |
| 357 | entries are handled in the file lists - we can't change this |
| 358 | without breaking existing versions */ |
| 359 | static inline int flist_up(struct file_list *flist, int i) |
| 360 | { |
| 361 | while (!flist->files[i]->basename) i++; |
| 362 | return i; |
| 363 | } |
| 364 | |
| 365 | #include "byteorder.h" |
| 366 | #include "version.h" |
| 367 | #include "proto.h" |
| 368 | #include "lib/mdfour.h" |
| 369 | |
| 370 | #if !HAVE_STRERROR |
| 371 | extern char *sys_errlist[]; |
| 372 | #define strerror(i) sys_errlist[i] |
| 373 | #endif |
| 374 | |
| 375 | #ifndef HAVE_STRCHR |
| 376 | # define strchr index |
| 377 | # define strrchr rindex |
| 378 | #endif |
| 379 | |
| 380 | #ifndef HAVE_ERRNO_DECL |
| 381 | extern int errno; |
| 382 | #endif |
| 383 | |
| 384 | #define SUPPORT_LINKS HAVE_READLINK |
| 385 | #define SUPPORT_HARD_LINKS HAVE_LINK |
| 386 | |
| 387 | #ifndef HAVE_LCHOWN |
| 388 | #define lchown chown |
| 389 | #endif |
| 390 | |
| 391 | #define SIGNAL_CAST (RETSIGTYPE (*)()) |
| 392 | |
| 393 | #ifndef EWOULDBLOCK |
| 394 | #define EWOULDBLOCK EAGAIN |
| 395 | #endif |
| 396 | |
| 397 | #ifndef STDIN_FILENO |
| 398 | #define STDIN_FILENO 0 |
| 399 | #endif |
| 400 | |
| 401 | #ifndef STDOUT_FILENO |
| 402 | #define STDOUT_FILENO 1 |
| 403 | #endif |
| 404 | |
| 405 | #ifndef STDERR_FILENO |
| 406 | #define STDERR_FILENO 2 |
| 407 | #endif |
| 408 | |
| 409 | #ifndef S_IWUSR |
| 410 | #define S_IWUSR 0200 |
| 411 | #endif |
| 412 | |
| 413 | #ifndef _S_IFMT |
| 414 | #define _S_IFMT 0170000 |
| 415 | #endif |
| 416 | |
| 417 | #ifndef _S_IFLNK |
| 418 | #define _S_IFLNK 0120000 |
| 419 | #endif |
| 420 | |
| 421 | #ifndef S_ISLNK |
| 422 | #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK)) |
| 423 | #endif |
| 424 | |
| 425 | #ifndef S_ISBLK |
| 426 | #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK)) |
| 427 | #endif |
| 428 | |
| 429 | #ifndef S_ISCHR |
| 430 | #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR)) |
| 431 | #endif |
| 432 | |
| 433 | #ifndef S_ISSOCK |
| 434 | #ifdef _S_IFSOCK |
| 435 | #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK)) |
| 436 | #else |
| 437 | #define S_ISSOCK(mode) (0) |
| 438 | #endif |
| 439 | #endif |
| 440 | |
| 441 | #ifndef S_ISFIFO |
| 442 | #ifdef _S_IFIFO |
| 443 | #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO)) |
| 444 | #else |
| 445 | #define S_ISFIFO(mode) (0) |
| 446 | #endif |
| 447 | #endif |
| 448 | |
| 449 | #ifndef S_ISDIR |
| 450 | #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR)) |
| 451 | #endif |
| 452 | |
| 453 | #ifndef S_ISREG |
| 454 | #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG)) |
| 455 | #endif |
| 456 | |
| 457 | |
| 458 | #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode)) |
| 459 | |
| 460 | #ifndef ACCESSPERMS |
| 461 | #define ACCESSPERMS 0777 |
| 462 | #endif |
| 463 | |
| 464 | /* handler for null strings in printf format */ |
| 465 | #define NS(s) ((s)?(s):"<NULL>") |
| 466 | |
| 467 | /* use magic gcc attributes to catch format errors */ |
| 468 | void rprintf(int , const char *, ...) |
| 469 | #ifdef __GNUC__ |
| 470 | __attribute__ ((format (printf, 2, 3))) |
| 471 | #endif |
| 472 | ; |
| 473 | |
| 474 | #ifdef REPLACE_INET_NTOA |
| 475 | #define inet_ntoa rep_inet_ntoa |
| 476 | #endif |
| 477 | |
| 478 | |
| 479 | #ifndef HAVE_STRLCPY |
| 480 | size_t strlcpy(char *d, const char *s, size_t bufsize); |
| 481 | #endif |
| 482 | |
| 483 | #ifndef HAVE_STRLCAT |
| 484 | size_t strlcat(char *d, const char *s, size_t bufsize); |
| 485 | #endif |
| 486 | |
| 487 | #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__) |