00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define False 0
00023 #define True 1
00024
00025 #define BLOCK_SIZE 700
00026 #define RSYNC_RSH_ENV "RSYNC_RSH"
00027
00028 #define RSYNC_NAME "rsync"
00029 #define RSYNCD_CONF "/etc/rsyncd.conf"
00030
00031 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
00032 #define URL_PREFIX "rsync://"
00033
00034 #define BACKUP_SUFFIX "~"
00035
00036
00037
00038 #define CHAR_OFFSET 0
00039
00040
00041 #define FLAG_DELETE (1<<0)
00042 #define SAME_MODE (1<<1)
00043 #define SAME_RDEV (1<<2)
00044 #define SAME_UID (1<<3)
00045 #define SAME_GID (1<<4)
00046 #define SAME_DIR (1<<5)
00047 #define SAME_NAME SAME_DIR
00048 #define LONG_NAME (1<<6)
00049 #define SAME_TIME (1<<7)
00050
00051
00052 #define PROTOCOL_VERSION 26
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #define MIN_PROTOCOL_VERSION 15
00065 #define MAX_PROTOCOL_VERSION 30
00066
00067 #define RSYNC_PORT 873
00068
00069 #define SPARSE_WRITE_SIZE (1024)
00070 #define WRITE_SIZE (32*1024)
00071 #define CHUNK_SIZE (32*1024)
00072 #define MAX_MAP_SIZE (256*1024)
00073 #define IO_BUFFER_SIZE (4092)
00074
00075 #define MAX_ARGS 1000
00076
00077 #define MPLEX_BASE 7
00078
00079
00080
00081
00082 enum logcode {FNONE=0, FERROR=1, FINFO=2, FLOG=3 };
00083
00084 #include "errcode.h"
00085
00086 #include "config.h"
00087
00088
00089
00090
00091
00092 #include <sys/types.h>
00093
00094 #ifdef HAVE_UNISTD_H
00095 #include <unistd.h>
00096 #endif
00097 #include <stdio.h>
00098 #include <stddef.h>
00099
00100 #ifdef HAVE_SYS_PARAM_H
00101 #include <sys/param.h>
00102 #endif
00103
00104 #ifdef HAVE_STDLIB_H
00105 #include <stdlib.h>
00106 #endif
00107
00108 #ifdef HAVE_SYS_SOCKET_H
00109 #include <sys/socket.h>
00110 #endif
00111
00112 #ifdef HAVE_STRING_H
00113 #include <string.h>
00114 #endif
00115
00116 #ifdef HAVE_MALLOC_H
00117 #include <malloc.h>
00118 #endif
00119
00120 #ifdef TIME_WITH_SYS_TIME
00121 #include <sys/time.h>
00122 #include <time.h>
00123 #else
00124 #ifdef HAVE_SYS_TIME_H
00125 #include <sys/time.h>
00126 #else
00127 #include <time.h>
00128 #endif
00129 #endif
00130
00131 #ifdef HAVE_FCNTL_H
00132 #include <fcntl.h>
00133 #else
00134 #ifdef HAVE_SYS_FCNTL_H
00135 #include <sys/fcntl.h>
00136 #endif
00137 #endif
00138
00139 #include <sys/stat.h>
00140
00141 #ifdef HAVE_SYS_IOCTL_H
00142 #include <sys/ioctl.h>
00143 #endif
00144
00145 #ifdef HAVE_SYS_FILIO_H
00146 #include <sys/filio.h>
00147 #endif
00148
00149 #include <signal.h>
00150 #ifdef HAVE_SYS_WAIT_H
00151 #include <sys/wait.h>
00152 #endif
00153 #ifdef HAVE_CTYPE_H
00154 #include <ctype.h>
00155 #endif
00156 #ifdef HAVE_GRP_H
00157 #include <grp.h>
00158 #endif
00159 #include <errno.h>
00160
00161 #ifdef HAVE_UTIME_H
00162 #include <utime.h>
00163 #endif
00164
00165 #ifdef HAVE_SYS_SELECT_H
00166 #include <sys/select.h>
00167 #endif
00168
00169 #ifdef HAVE_SYS_MODE_H
00170
00171 #ifndef S_ISLNK
00172 #include <sys/mode.h>
00173 #endif
00174 #endif
00175
00176 #ifdef HAVE_FNMATCH
00177 #include <fnmatch.h>
00178 #else
00179 #include "lib/fnmatch.h"
00180 #endif
00181
00182 #ifdef HAVE_GLOB_H
00183 #include <glob.h>
00184 #endif
00185
00186 #ifdef HAVE_MALLOC_H
00187 # include <malloc.h>
00188 #endif
00189
00190
00191 #include <pwd.h>
00192 #include <grp.h>
00193
00194 #include <stdarg.h>
00195 #include <netinet/in.h>
00196 #include <arpa/inet.h>
00197 #include <netdb.h>
00198 #include <syslog.h>
00199 #include <sys/file.h>
00200
00201 #if HAVE_DIRENT_H
00202 # include <dirent.h>
00203 #else
00204 # define dirent direct
00205 # if HAVE_SYS_NDIR_H
00206 # include <sys/ndir.h>
00207 # endif
00208 # if HAVE_SYS_DIR_H
00209 # include <sys/dir.h>
00210 # endif
00211 # if HAVE_NDIR_H
00212 # include <ndir.h>
00213 # endif
00214 #endif
00215
00216 #ifdef HAVE_COMPAT_H
00217 #include <compat.h>
00218 #endif
00219
00220 #include <assert.h>
00221
00222
00223 #define BOOL int
00224
00225 #ifndef uchar
00226 #define uchar unsigned char
00227 #endif
00228
00229 #if HAVE_UNSIGNED_CHAR
00230 #define schar signed char
00231 #else
00232 #define schar char
00233 #endif
00234
00235 #ifndef int32
00236 #if (SIZEOF_INT == 4)
00237 #define int32 int
00238 #elif (SIZEOF_LONG == 4)
00239 #define int32 long
00240 #elif (SIZEOF_SHORT == 4)
00241 #define int32 short
00242 #else
00243
00244 #define int32 int
00245 #define LARGE_INT32
00246 #endif
00247 #endif
00248
00249 #ifndef uint32
00250 #define uint32 unsigned int32
00251 #endif
00252
00253 #if HAVE_OFF64_T
00254 #define OFF_T off64_t
00255 #define STRUCT_STAT struct stat64
00256 #else
00257 #define OFF_T off_t
00258 #define STRUCT_STAT struct stat
00259 #endif
00260
00261 #if HAVE_OFF64_T
00262 #define int64 off64_t
00263 #elif (SIZEOF_LONG == 8)
00264 #define int64 long
00265 #elif (SIZEOF_INT == 8)
00266 #define int64 int
00267 #elif HAVE_LONGLONG
00268 #define int64 long long
00269 #else
00270
00271 #define int64 off_t
00272 #define NO_INT64
00273 #endif
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 #define INO64_T int64
00307 #define DEV64_T int64
00308
00309 #ifndef MIN
00310 #define MIN(a,b) ((a)<(b)?(a):(b))
00311 #endif
00312
00313 #ifndef MAX
00314 #define MAX(a,b) ((a)>(b)?(a):(b))
00315 #endif
00316
00317 #ifndef MAXHOSTNAMELEN
00318 #define MAXHOSTNAMELEN 256
00319 #endif
00320
00321
00322 #define MD4_SUM_LENGTH 16
00323 #define SUM_LENGTH 16
00324
00325 #ifndef MAXPATHLEN
00326 #define MAXPATHLEN 1024
00327 #endif
00328
00329 #ifndef INADDR_NONE
00330 #define INADDR_NONE 0xffffffff
00331 #endif
00332
00333 struct file_struct {
00334 unsigned flags;
00335 time_t modtime;
00336 OFF_T length;
00337 mode_t mode;
00338
00339 INO64_T inode;
00340
00341 DEV64_T dev;
00342
00343
00344 DEV64_T rdev;
00345 uid_t uid;
00346 gid_t gid;
00347 char *basename;
00348 char *dirname;
00349 char *basedir;
00350 char *link;
00351 char *sum;
00352 };
00353
00354
00355 #define ARENA_SIZE (32 * 1024)
00356
00357 struct string_area {
00358 char *base;
00359 char *end;
00360 char *current;
00361 struct string_area *next;
00362 };
00363
00364 struct file_list {
00365 int count;
00366 int malloced;
00367 struct file_struct **files;
00368 struct string_area *string_area;
00369 };
00370
00371 struct sum_buf {
00372 OFF_T offset;
00373 int len;
00374 int i;
00375 uint32 sum1;
00376 char sum2[SUM_LENGTH];
00377 };
00378
00379 struct sum_struct {
00380 OFF_T flength;
00381 size_t count;
00382 size_t remainder;
00383 size_t n;
00384 struct sum_buf *sums;
00385 };
00386
00387 struct map_struct {
00388 char *p;
00389 int fd,p_size,p_len;
00390 OFF_T file_size, p_offset, p_fd_offset;
00391 };
00392
00393 struct exclude_struct {
00394 char *pattern;
00395 int regular_exp;
00396 int fnmatch_flags;
00397 int include;
00398 int directory;
00399 int local;
00400 };
00401
00402 struct stats {
00403 int64 total_size;
00404 int64 total_transferred_size;
00405 int64 total_written;
00406 int64 total_read;
00407 int64 literal_data;
00408 int64 matched_data;
00409 int flist_size;
00410 int num_files;
00411 int num_transferred_files;
00412 };
00413
00414
00415
00416
00417
00418 static inline int flist_up(struct file_list *flist, int i)
00419 {
00420 while (!flist->files[i]->basename) i++;
00421 return i;
00422 }
00423
00424 #include "byteorder.h"
00425 #include "lib/mdfour.h"
00426 #include "lib/permstring.h"
00427 #include "lib/addrinfo.h"
00428
00429 #include "proto.h"
00430
00431
00432 #ifndef HAVE_ASPRINTF
00433 int asprintf(char **ptr, const char *format, ...);
00434 #endif
00435
00436 #ifndef HAVE_VASPRINTF
00437 int vasprintf(char **ptr, const char *format, va_list ap);
00438 #endif
00439
00440 #if !defined(HAVE_VSNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
00441 int vsnprintf (char *str, size_t count, const char *fmt, va_list args);
00442 #endif
00443
00444 #if !defined(HAVE_SNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
00445 int snprintf(char *str,size_t count,const char *fmt,...);
00446 #endif
00447
00448
00449 #if !HAVE_STRERROR
00450 extern char *sys_errlist[];
00451 #define strerror(i) sys_errlist[i]
00452 #endif
00453
00454 #ifndef HAVE_STRCHR
00455 # define strchr index
00456 # define strrchr rindex
00457 #endif
00458
00459 #ifndef HAVE_ERRNO_DECL
00460 extern int errno;
00461 #endif
00462
00463 #define SUPPORT_LINKS HAVE_READLINK
00464 #define SUPPORT_HARD_LINKS HAVE_LINK
00465
00466
00467
00468
00469 #ifndef HAVE_LCHOWN
00470 #define lchown chown
00471 #endif
00472
00473 #define SIGNAL_CAST (RETSIGTYPE (*)())
00474
00475 #ifndef EWOULDBLOCK
00476 #define EWOULDBLOCK EAGAIN
00477 #endif
00478
00479 #ifndef STDIN_FILENO
00480 #define STDIN_FILENO 0
00481 #endif
00482
00483 #ifndef STDOUT_FILENO
00484 #define STDOUT_FILENO 1
00485 #endif
00486
00487 #ifndef STDERR_FILENO
00488 #define STDERR_FILENO 2
00489 #endif
00490
00491 #ifndef S_IWUSR
00492 #define S_IWUSR 0200
00493 #endif
00494
00495 #ifndef _S_IFMT
00496 #define _S_IFMT 0170000
00497 #endif
00498
00499 #ifndef _S_IFLNK
00500 #define _S_IFLNK 0120000
00501 #endif
00502
00503 #ifndef S_ISLNK
00504 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
00505 #endif
00506
00507 #ifndef S_ISBLK
00508 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
00509 #endif
00510
00511 #ifndef S_ISCHR
00512 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
00513 #endif
00514
00515 #ifndef S_ISSOCK
00516 #ifdef _S_IFSOCK
00517 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
00518 #else
00519 #define S_ISSOCK(mode) (0)
00520 #endif
00521 #endif
00522
00523 #ifndef S_ISFIFO
00524 #ifdef _S_IFIFO
00525 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
00526 #else
00527 #define S_ISFIFO(mode) (0)
00528 #endif
00529 #endif
00530
00531 #ifndef S_ISDIR
00532 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
00533 #endif
00534
00535 #ifndef S_ISREG
00536 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
00537 #endif
00538
00539
00540 #ifdef O_NONBLOCK
00541 # define NONBLOCK_FLAG O_NONBLOCK
00542 #elif defined(SYSV)
00543 # define NONBLOCK_FLAG O_NDELAY
00544 #else
00545 # define NONBLOCK_FLAG FNDELAY
00546 #endif
00547
00548 #ifndef INADDR_LOOPBACK
00549 #define INADDR_LOOPBACK 0x7f000001
00550 #endif
00551
00552 #ifndef INADDR_NONE
00553 #define INADDR_NONE 0xffffffff
00554 #endif
00555
00556 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
00557
00558 #ifndef ACCESSPERMS
00559 #define ACCESSPERMS 0777
00560 #endif
00561
00562
00563
00564 #define INITACCESSPERMS 0700
00565
00566
00567 #define NS(s) ((s)?(s):"<NULL>")
00568
00569 #if !defined(__GNUC__) || defined(APPLE)
00570
00571
00572
00573 #define __attribute__(x)
00574
00575 #endif
00576
00577
00578
00579 void rprintf(enum logcode , const char *, ...)
00580 __attribute__((format (printf, 2, 3)))
00581 ;
00582
00583
00584
00585 void rsyserr(enum logcode, int, const char *, ...)
00586 __attribute__((format (printf, 3, 4)))
00587 ;
00588
00589 #ifdef REPLACE_INET_NTOA
00590 #define inet_ntoa rep_inet_ntoa
00591 #endif
00592
00593
00594 #ifndef HAVE_STRLCPY
00595 size_t strlcpy(char *d, const char *s, size_t bufsize);
00596 #endif
00597
00598 #ifndef HAVE_STRLCAT
00599 size_t strlcat(char *d, const char *s, size_t bufsize);
00600 #endif
00601
00602 #ifndef WEXITSTATUS
00603 #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
00604 #endif
00605
00606 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
00607
00608
00609 extern int verbose;
00610
00611 #ifndef HAVE_INET_NTOP
00612 const char *
00613 inet_ntop(int af, const void *src, char *dst, size_t size);
00614 #endif
00615
00616 #ifndef HAVE_INET_PTON
00617 int isc_net_pton(int af, const char *src, void *dst);
00618 #endif
00619
00620 #ifdef MAINTAINER_MODE
00621 const char *get_panic_action(void);
00622 #endif
00623
00624 #define UNUSED(x) x __attribute__((__unused__))
00625
00626 const char *io_write_phase, *io_read_phase;