When using daemon mode over a remote shell program and not running as root,
[rsync/rsync.git] / rsync.h
1 /* 
2    Copyright (C) by Andrew Tridgell 1996, 2000
3    Copyright (C) Paul Mackerras 1996
4    Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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
21
22 #define False 0
23 #define True 1
24
25 #define BLOCK_SIZE 700
26 #define RSYNC_RSH_ENV "RSYNC_RSH"
27
28 #define RSYNC_NAME "rsync"
29 #define RSYNCD_SYSCONF "/etc/rsyncd.conf"
30 #define RSYNCD_USERCONF "rsyncd.conf"
31
32 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
33 #define URL_PREFIX "rsync://"
34
35 #define BACKUP_SUFFIX "~"
36
37 /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
38    incompatible with older versions :-( */
39 #define CHAR_OFFSET 0
40
41
42 #define FLAG_DELETE (1<<0)
43 #define SAME_MODE (1<<1)
44 #define SAME_RDEV (1<<2)
45 #define SAME_UID (1<<3)
46 #define SAME_GID (1<<4)
47 #define SAME_DIR (1<<5)
48 #define SAME_NAME SAME_DIR
49 #define LONG_NAME (1<<6)
50 #define SAME_TIME (1<<7)
51
52 /* update this if you make incompatible changes */
53 #define PROTOCOL_VERSION 26
54
55 /* We refuse to interoperate with versions that are not in this range.
56  * Note that we assume we'll work with later versions: the onus is on
57  * people writing them to make sure that they don't send us anything
58  * we won't understand.
59  *
60  * There are two possible explanations for the limit at thirty: either
61  * to allow new major-rev versions that do not interoperate with us,
62  * and (more likely) so that we can detect an attempt to connect rsync
63  * to a non-rsync server, which is unlikely to begin by sending a byte
64  * between 15 and 30. */
65 #define MIN_PROTOCOL_VERSION 15
66 #define MAX_PROTOCOL_VERSION 30
67
68 #define RSYNC_PORT 873
69
70 #define SPARSE_WRITE_SIZE (1024)
71 #define WRITE_SIZE (32*1024)
72 #define CHUNK_SIZE (32*1024)
73 #define MAX_MAP_SIZE (256*1024)
74 #define IO_BUFFER_SIZE (4092)
75
76 #define MAX_ARGS 1000
77
78 #define MPLEX_BASE 7
79
80 /* Log values.  I *think* what these mean is: FLOG goes to the server
81  * logfile; FERROR and FINFO try to end up on the client, with
82  * different levels of filtering. */
83 enum logcode {FNONE=0, FERROR=1, FINFO=2, FLOG=3 };
84
85 #include "errcode.h"
86
87 #include "config.h"
88
89 /* The default RSYNC_RSH is always set in config.h, either to "remsh",
90  * "rsh", or otherwise something specified by the user.  HAVE_REMSH
91  * controls parameter munging for HP/UX, etc. */
92
93 #include <sys/types.h>
94
95 #ifdef HAVE_UNISTD_H
96 #include <unistd.h>
97 #endif
98 #include <stdio.h>
99 #include <stddef.h>
100
101 #ifdef HAVE_SYS_PARAM_H
102 #include <sys/param.h>
103 #endif
104
105 #ifdef HAVE_STDLIB_H
106 #include <stdlib.h>
107 #endif
108
109 #ifdef HAVE_SYS_SOCKET_H
110 #include <sys/socket.h>
111 #endif
112
113 #ifdef HAVE_STRING_H
114 #include <string.h>
115 #endif
116
117 #ifdef HAVE_MALLOC_H
118 #include <malloc.h>
119 #endif
120
121 #ifdef TIME_WITH_SYS_TIME
122 #include <sys/time.h>
123 #include <time.h>
124 #else
125 #ifdef HAVE_SYS_TIME_H
126 #include <sys/time.h>
127 #else
128 #include <time.h>
129 #endif
130 #endif
131
132 #ifdef HAVE_FCNTL_H
133 #include <fcntl.h>
134 #else
135 #ifdef HAVE_SYS_FCNTL_H
136 #include <sys/fcntl.h>
137 #endif
138 #endif
139
140 #include <sys/stat.h>
141
142 #ifdef HAVE_SYS_IOCTL_H
143 #include <sys/ioctl.h>
144 #endif
145
146 #ifdef HAVE_SYS_FILIO_H
147 #include <sys/filio.h>
148 #endif
149
150 #include <signal.h>
151 #ifdef HAVE_SYS_WAIT_H
152 #include <sys/wait.h>
153 #endif
154 #ifdef HAVE_CTYPE_H
155 #include <ctype.h>
156 #endif
157 #ifdef HAVE_GRP_H
158 #include <grp.h>
159 #endif
160 #include <errno.h>
161
162 #ifdef HAVE_UTIME_H
163 #include <utime.h>
164 #endif
165
166 #ifdef HAVE_SYS_SELECT_H
167 #include <sys/select.h>
168 #endif
169
170 #ifdef HAVE_SYS_MODE_H
171 /* apparently AIX needs this for S_ISLNK */
172 #ifndef S_ISLNK
173 #include <sys/mode.h>
174 #endif
175 #endif
176
177 #ifdef HAVE_FNMATCH
178 #include <fnmatch.h>
179 #else
180 #include "lib/fnmatch.h"
181 #endif
182
183 #ifdef HAVE_GLOB_H
184 #include <glob.h>
185 #endif
186
187 #ifdef HAVE_MALLOC_H
188 #  include <malloc.h>
189 #endif
190
191 /* these are needed for the uid/gid mapping code */
192 #include <pwd.h>
193 #include <grp.h>
194
195 #include <stdarg.h>
196 #include <netinet/in.h>
197 #include <arpa/inet.h>
198 #include <netdb.h>
199 #include <syslog.h>
200 #include <sys/file.h>
201
202 #if HAVE_DIRENT_H
203 # include <dirent.h>
204 #else
205 # define dirent direct
206 # if HAVE_SYS_NDIR_H
207 #  include <sys/ndir.h>
208 # endif
209 # if HAVE_SYS_DIR_H
210 #  include <sys/dir.h>
211 # endif
212 # if HAVE_NDIR_H
213 #  include <ndir.h>
214 # endif
215 #endif
216
217 #ifdef HAVE_COMPAT_H
218 #include <compat.h>
219 #endif
220
221 #include <assert.h>
222
223
224 #define BOOL int
225
226 #ifndef uchar
227 #define uchar unsigned char
228 #endif
229
230 #if HAVE_UNSIGNED_CHAR
231 #define schar signed char
232 #else
233 #define schar char
234 #endif
235
236 #ifndef int32
237 #if (SIZEOF_INT == 4)
238 #define int32 int
239 #elif (SIZEOF_LONG == 4)
240 #define int32 long
241 #elif (SIZEOF_SHORT == 4)
242 #define int32 short
243 #else
244 /* I hope this works */
245 #define int32 int
246 #define LARGE_INT32
247 #endif
248 #endif
249
250 #ifndef uint32
251 #define uint32 unsigned int32
252 #endif
253
254 #if HAVE_OFF64_T
255 #define OFF_T off64_t
256 #define STRUCT_STAT struct stat64
257 #else
258 #define OFF_T off_t
259 #define STRUCT_STAT struct stat
260 #endif
261
262 #if HAVE_OFF64_T
263 #define int64 off64_t
264 #elif (SIZEOF_LONG == 8) 
265 #define int64 long
266 #elif (SIZEOF_INT == 8) 
267 #define int64 int
268 #elif HAVE_LONGLONG
269 #define int64 long long
270 #else
271 /* As long as it gets... */
272 #define int64 off_t
273 #define NO_INT64
274 #endif
275
276 /* Starting from protocol version 26, we always use 64-bit
277  * ino_t and dev_t internally, even if this platform does not
278  * allow files to have 64-bit inums.  That's because the
279  * receiver needs to find duplicate (dev,ino) tuples to detect
280  * hardlinks, and it might have files coming from a platform
281  * that has 64-bit inums.
282  *
283  * The only exception is if we're on a platform with no 64-bit type at
284  * all.
285  *
286  * Because we use read_longint() to get these off the wire, if you
287  * transfer devices or hardlinks with dev or inum > 2**32 to a machine
288  * with no 64-bit types then you will get an overflow error.  Probably
289  * not many people have that combination of machines, and you can
290  * avoid it by not preserving hardlinks or not transferring device
291  * nodes.  It's not clear that any other behaviour is better.
292  *
293  * Note that if you transfer devices from a 64-bit-devt machine (say,
294  * Solaris) to a 32-bit-devt machine (say, Linux-2.2/x86) then the
295  * device numbers will be truncated.  But it's a kind of silly thing
296  * to do anyhow.
297  *
298  * FIXME: In future, we should probable split the device number into
299  * major/minor, and transfer the two parts as 32-bit ints.  That gives
300  * you somewhat more of a chance that they'll come from a big machine
301  * to a little one in a useful way.
302  *
303  * FIXME: Really we need an unsigned type, and we perhaps ought to
304  * cope with platforms on which this is an unsigned int or even a
305  * struct.  Later.
306  */ 
307 #define INO64_T int64
308 #define DEV64_T int64
309
310 #ifndef MIN
311 #define MIN(a,b) ((a)<(b)?(a):(b))
312 #endif
313
314 #ifndef MAX
315 #define MAX(a,b) ((a)>(b)?(a):(b))
316 #endif
317
318 #ifndef MAXHOSTNAMELEN
319 #define MAXHOSTNAMELEN 256
320 #endif
321
322 /* the length of the md4 checksum */
323 #define MD4_SUM_LENGTH 16
324 #define SUM_LENGTH 16
325
326 #ifndef MAXPATHLEN
327 #define MAXPATHLEN 1024
328 #endif
329
330 #ifndef INADDR_NONE
331 #define INADDR_NONE 0xffffffff
332 #endif
333
334 struct file_struct {
335         unsigned flags;
336         time_t modtime;
337         OFF_T length;
338         mode_t mode;
339
340         INO64_T inode;
341         /** Device this file lives upon */
342         DEV64_T dev;
343
344         /** If this is a device node, the device number. */
345         DEV64_T rdev;
346         uid_t uid;
347         gid_t gid;
348         char *basename;
349         char *dirname;
350         char *basedir;
351         char *link;
352         char *sum;
353 };
354
355
356 #define ARENA_SIZE      (32 * 1024)
357
358 struct string_area {
359         char *base;
360         char *end;
361         char *current;
362         struct string_area *next;
363 };
364
365 struct file_list {
366         int count;
367         int malloced;
368         struct file_struct **files;
369         struct string_area *string_area;
370 };
371
372 struct sum_buf {
373         OFF_T offset;           /**< offset in file of this chunk */
374         int len;                /**< length of chunk of file */
375         int i;                  /**< index of this chunk */
376         uint32 sum1;            /**< simple checksum */
377         char sum2[SUM_LENGTH];  /**< checksum  */
378 };
379
380 struct sum_struct {
381         OFF_T flength;          /**< total file length */
382         size_t count;           /**< how many chunks */
383         size_t remainder;       /**< flength % block_length */
384         size_t n;               /**< block_length */
385         struct sum_buf *sums;   /**< points to info for each chunk */
386 };
387
388 struct map_struct {
389         char *p;
390         int fd,p_size,p_len;
391         OFF_T file_size, p_offset, p_fd_offset;
392 };
393
394 struct exclude_struct {
395         char *pattern;
396         int regular_exp;
397         int fnmatch_flags;
398         int include;
399         int directory;
400         int local;
401 };
402
403 struct stats {
404         int64 total_size;
405         int64 total_transferred_size;
406         int64 total_written;
407         int64 total_read;
408         int64 literal_data;
409         int64 matched_data;
410         int flist_size;
411         int num_files;
412         int num_transferred_files;
413 };
414
415
416 /* we need this function because of the silly way in which duplicate
417    entries are handled in the file lists - we can't change this
418    without breaking existing versions */
419 static inline int flist_up(struct file_list *flist, int i)
420 {
421         while (!flist->files[i]->basename) i++;
422         return i;
423 }
424
425 #include "byteorder.h"
426 #include "lib/mdfour.h"
427 #include "lib/permstring.h"
428 #include "lib/addrinfo.h"
429
430 #include "proto.h"
431
432 /* We have replacement versions of these if they're missing. */
433 #ifndef HAVE_ASPRINTF
434 int asprintf(char **ptr, const char *format, ...);
435 #endif
436
437 #ifndef HAVE_VASPRINTF
438 int vasprintf(char **ptr, const char *format, va_list ap);
439 #endif
440
441 #if !defined(HAVE_VSNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
442 int vsnprintf (char *str, size_t count, const char *fmt, va_list args);
443 #endif
444
445 #if !defined(HAVE_SNPRINTF) && !defined(HAVE_C99_VSNPRINTF)
446 int snprintf(char *str,size_t count,const char *fmt,...);
447 #endif
448
449
450 #if !HAVE_STRERROR
451 extern char *sys_errlist[];
452 #define strerror(i) sys_errlist[i]
453 #endif
454
455 #ifndef HAVE_STRCHR
456 # define strchr                 index
457 # define strrchr                rindex
458 #endif
459
460 #ifndef HAVE_ERRNO_DECL
461 extern int errno;
462 #endif
463
464 #define SUPPORT_LINKS HAVE_READLINK
465 #define SUPPORT_HARD_LINKS HAVE_LINK
466
467 /* This could be bad on systems which have no lchown and where chown
468  * follows symbollic links.  On such systems it might be better not to
469  * try to chown symlinks at all. */
470 #ifndef HAVE_LCHOWN
471 #define lchown chown
472 #endif
473
474 #define SIGNAL_CAST (RETSIGTYPE (*)())
475
476 #ifndef EWOULDBLOCK
477 #define EWOULDBLOCK EAGAIN
478 #endif
479
480 #ifndef STDIN_FILENO
481 #define STDIN_FILENO 0
482 #endif
483
484 #ifndef STDOUT_FILENO
485 #define STDOUT_FILENO 1
486 #endif
487
488 #ifndef STDERR_FILENO
489 #define STDERR_FILENO 2
490 #endif
491
492 #ifndef S_IWUSR
493 #define S_IWUSR 0200
494 #endif
495
496 #ifndef _S_IFMT
497 #define _S_IFMT        0170000
498 #endif
499
500 #ifndef _S_IFLNK
501 #define _S_IFLNK  0120000
502 #endif
503
504 #ifndef S_ISLNK
505 #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
506 #endif
507
508 #ifndef S_ISBLK
509 #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
510 #endif
511
512 #ifndef S_ISCHR
513 #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
514 #endif
515
516 #ifndef S_ISSOCK
517 #ifdef _S_IFSOCK
518 #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
519 #else
520 #define S_ISSOCK(mode) (0)
521 #endif
522 #endif
523
524 #ifndef S_ISFIFO
525 #ifdef _S_IFIFO
526 #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
527 #else
528 #define S_ISFIFO(mode) (0)
529 #endif
530 #endif
531
532 #ifndef S_ISDIR
533 #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
534 #endif
535
536 #ifndef S_ISREG
537 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
538 #endif
539
540 /* work out what fcntl flag to use for non-blocking */
541 #ifdef O_NONBLOCK
542 # define NONBLOCK_FLAG O_NONBLOCK
543 #elif defined(SYSV)
544 # define NONBLOCK_FLAG O_NDELAY
545 #else 
546 # define NONBLOCK_FLAG FNDELAY
547 #endif
548
549 #ifndef INADDR_LOOPBACK
550 #define INADDR_LOOPBACK 0x7f000001
551 #endif
552
553 #ifndef INADDR_NONE
554 #define INADDR_NONE 0xffffffff
555 #endif
556
557 #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode) || S_ISSOCK(mode) || S_ISFIFO(mode))
558
559 #ifndef ACCESSPERMS
560 #define ACCESSPERMS 0777
561 #endif
562 /* Initial mask on permissions given to temporary files.  Mask off setuid
563      bits and group access because of potential race-condition security
564      holes, and mask other access because mode 707 is bizarre */
565 #define INITACCESSPERMS 0700
566
567 /* handler for null strings in printf format */
568 #define NS(s) ((s)?(s):"<NULL>")
569
570 #if !defined(__GNUC__) || defined(APPLE)
571 /* Apparently the OS X port of gcc gags on __attribute__.
572  *
573  * <http://www.opensource.apple.com/bugs/X/gcc/2512150.html> */
574 #define __attribute__(x) 
575
576 #endif
577
578
579 /* use magic gcc attributes to catch format errors */
580  void rprintf(enum logcode , const char *, ...)
581      __attribute__((format (printf, 2, 3)))
582 ;
583
584 /* This is just like rprintf, but it also tries to print some
585  * representation of the error code.  Normally errcode = errno. */
586 void rsyserr(enum logcode, int, const char *, ...)
587      __attribute__((format (printf, 3, 4)))
588      ;
589
590 #ifdef REPLACE_INET_NTOA
591 #define inet_ntoa rep_inet_ntoa
592 #endif
593
594
595 #ifndef HAVE_STRLCPY
596 size_t strlcpy(char *d, const char *s, size_t bufsize);
597 #endif
598
599 #ifndef HAVE_STRLCAT
600 size_t strlcat(char *d, const char *s, size_t bufsize);
601 #endif
602
603 #ifndef WEXITSTATUS
604 #define WEXITSTATUS(stat)       ((int)(((stat)>>8)&0xFF))
605 #endif
606
607 #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
608
609
610 extern int verbose;
611
612 #ifndef HAVE_INET_NTOP
613 const char *                 
614 inet_ntop(int af, const void *src, char *dst, size_t size);
615 #endif /* !HAVE_INET_NTOP */
616
617 #ifndef HAVE_INET_PTON
618 int isc_net_pton(int af, const char *src, void *dst);
619 #endif
620
621 #ifdef MAINTAINER_MODE
622 const char *get_panic_action(void);
623 #endif
624
625 #define UNUSED(x) x __attribute__((__unused__))
626
627 const char *io_write_phase, *io_read_phase;