2 * Compatibility routines for older rsync protocol versions.
4 * Copyright (C) Andrew Tridgell 1996
5 * Copyright (C) Paul Mackerras 1996
6 * Copyright (C) 2004-2020 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
26 extern int local_server
;
30 extern int allow_inc_recurse
;
31 extern int preallocate_files
;
32 extern int append_mode
;
33 extern int fuzzy_basis
;
34 extern int read_batch
;
35 extern int write_batch
;
36 extern int delay_updates
;
37 extern int checksum_seed
;
38 extern int basis_dir_cnt
;
39 extern int prune_empty_dirs
;
40 extern int protocol_version
;
41 extern int protect_args
;
42 extern int preserve_uid
;
43 extern int preserve_gid
;
44 extern int preserve_atimes
;
45 extern int preserve_acls
;
46 extern int preserve_xattrs
;
47 extern int xfer_flags_as_varint
;
48 extern int need_messages_from_generator
;
49 extern int delete_mode
, delete_before
, delete_during
, delete_after
;
50 extern int do_compression
;
51 extern int do_compression_level
;
52 extern char *shell_cmd
;
53 extern char *partial_dir
;
54 extern char *files_from
;
55 extern char *filesfrom_host
;
56 extern const char *checksum_choice
;
57 extern const char *compress_choice
;
58 extern filter_rule_list filter_list
;
59 extern int need_unsorted_flist
;
61 extern iconv_t ic_send
, ic_recv
;
62 extern char *iconv_opt
;
64 extern struct name_num_obj valid_checksums
;
66 int remote_protocol
= 0;
67 int file_extra_cnt
= 0; /* count of file-list extras that everyone gets */
70 int use_safe_inc_flist
= 0;
71 int want_xattr_optim
= 0;
72 int proper_seed_order
= 0;
73 int inplace_partial
= 0;
74 int do_negotiated_strings
= 0;
76 /* These index values are for the file-list's extra-attribute array. */
77 int pathname_ndx
, depth_ndx
, atimes_ndx
, uid_ndx
, gid_ndx
, acls_ndx
, xattrs_ndx
, unsort_ndx
;
79 int receiver_symlink_times
= 0; /* receiver can set the time on a symlink */
80 int sender_symlink_iconv
= 0; /* sender should convert symlink content */
83 int filesfrom_convert
= 0;
86 #define MAX_NSTR_STRLEN 256
88 struct name_num_obj valid_compressions
= {
89 "compress", NULL
, NULL
, 0, 0, {
91 { CPRES_ZSTD
, "zstd", NULL
},
94 { CPRES_LZ4
, "lz4", NULL
},
96 { CPRES_ZLIBX
, "zlibx", NULL
},
97 { CPRES_ZLIB
, "zlib", NULL
},
98 { CPRES_NONE
, "none", NULL
},
103 #define CF_INC_RECURSE (1<<0)
104 #define CF_SYMLINK_TIMES (1<<1)
105 #define CF_SYMLINK_ICONV (1<<2)
106 #define CF_SAFE_FLIST (1<<3)
107 #define CF_AVOID_XATTR_OPTIM (1<<4)
108 #define CF_CHKSUM_SEED_FIX (1<<5)
109 #define CF_INPLACE_PARTIAL_DIR (1<<6)
110 #define CF_VARINT_FLIST_FLAGS (1<<7)
112 static const char *client_info
;
114 /* The server makes sure that if either side only supports a pre-release
115 * version of a protocol, that both sides must speak a compatible version
116 * of that protocol for it to be advertised as available. */
117 static void check_sub_protocol(void)
120 int their_protocol
, their_sub
;
121 #if SUBPROTOCOL_VERSION != 0
122 int our_sub
= protocol_version
< PROTOCOL_VERSION
? 0 : SUBPROTOCOL_VERSION
;
127 /* client_info starts with VER.SUB string if client is a pre-release. */
128 if (!(their_protocol
= atoi(client_info
))
129 || !(dot
= strchr(client_info
, '.'))
130 || !(their_sub
= atoi(dot
+1))) {
131 #if SUBPROTOCOL_VERSION != 0
138 if (their_protocol
< protocol_version
) {
140 protocol_version
= their_protocol
- 1;
144 if (their_protocol
> protocol_version
)
145 their_sub
= 0; /* 0 == final version of older protocol */
146 if (their_sub
!= our_sub
)
150 void set_allow_inc_recurse(void)
152 client_info
= shell_cmd
? shell_cmd
: "";
154 if (!recurse
|| use_qsort
)
155 allow_inc_recurse
= 0;
157 && (delete_before
|| delete_after
158 || delay_updates
|| prune_empty_dirs
))
159 allow_inc_recurse
= 0;
160 else if (am_server
&& !local_server
161 && (strchr(client_info
, 'i') == NULL
))
162 allow_inc_recurse
= 0;
165 void parse_compress_choice(int final_call
)
167 if (valid_compressions
.negotiated_name
)
168 do_compression
= valid_compressions
.negotiated_num
;
169 else if (compress_choice
) {
170 struct name_num_item
*nni
= get_nni_by_name(&valid_compressions
, compress_choice
, -1);
172 rprintf(FERROR
, "unknown compress name: %s\n", compress_choice
);
173 exit_cleanup(RERR_UNSUPPORTED
);
175 do_compression
= nni
->num
;
176 } else if (do_compression
)
177 do_compression
= CPRES_ZLIB
;
179 do_compression
= CPRES_NONE
;
181 if (do_compression
!= CPRES_NONE
&& final_call
)
182 init_compression_level(); /* There's a chance this might turn compression off! */
184 if (do_compression
== CPRES_NONE
)
185 compress_choice
= NULL
;
187 /* Snag the compression name for both write_batch's option output & the following debug output. */
188 if (valid_compressions
.negotiated_name
)
189 compress_choice
= valid_compressions
.negotiated_name
;
190 else if (compress_choice
== NULL
) {
191 struct name_num_item
*nni
= get_nni_by_num(&valid_compressions
, do_compression
);
192 compress_choice
= nni
? nni
->name
: "UNKNOWN";
195 if (final_call
&& DEBUG_GTE(NSTR
, am_server
? 3 : 1)
196 && (do_compression
!= CPRES_NONE
|| do_compression_level
!= CLVL_NOT_SPECIFIED
)) {
197 rprintf(FINFO
, "%s%s compress: %s (level %d)\n",
198 am_server
? "Server" : "Client",
199 valid_compressions
.negotiated_name
? " negotiated" : "",
200 compress_choice
, do_compression_level
);
204 struct name_num_item
*get_nni_by_name(struct name_num_obj
*nno
, const char *name
, int len
)
206 struct name_num_item
*nni
;
211 for (nni
= nno
->list
; nni
->name
; nni
++) {
212 if (strncasecmp(name
, nni
->name
, len
) == 0 && nni
->name
[len
] == '\0')
219 struct name_num_item
*get_nni_by_num(struct name_num_obj
*nno
, int num
)
221 struct name_num_item
*nni
;
223 for (nni
= nno
->list
; nni
->name
; nni
++) {
231 static void init_nno_saw(struct name_num_obj
*nno
, int val
)
233 struct name_num_item
*nni
;
237 for (nni
= nno
->list
; nni
->name
; nni
++) {
238 if (nni
->num
>= nno
->saw_len
)
239 nno
->saw_len
= nni
->num
+ 1;
244 if (!(nno
->saw
= new_array0(uchar
, nno
->saw_len
)))
245 out_of_memory("init_nno_saw");
247 /* We'll take this opportunity to make sure that the main_name values are set right. */
248 for (cnt
= 1, nni
= nno
->list
; nni
->name
; nni
++, cnt
++) {
249 if (nno
->saw
[nni
->num
])
250 nni
->main_name
= nno
->list
[nno
->saw
[nni
->num
]-1].name
;
252 nno
->saw
[nni
->num
] = cnt
;
256 memset(nno
->saw
, val
, nno
->saw_len
);
259 /* Simplify the user-provided string so that it contains valid names without any duplicates.
260 * It also sets the "saw" flags to a 1-relative count of which name was seen first. */
261 static int parse_nni_str(struct name_num_obj
*nno
, const char *from
, char *tobuf
, int tobuf_len
)
263 char *to
= tobuf
, *tok
= NULL
;
267 if (*from
== ' ' || !*from
) {
269 struct name_num_item
*nni
= get_nni_by_name(nno
, tok
, to
- tok
);
270 if (nni
&& !nno
->saw
[nni
->num
]) {
271 nno
->saw
[nni
->num
] = ++cnt
;
272 if (nni
->main_name
) {
273 to
= tok
+ strlcpy(tok
, nni
->main_name
, tobuf_len
- (tok
- tobuf
));
274 if (to
- tobuf
>= tobuf_len
) {
280 to
= tok
- (tok
!= tobuf
);
292 if (to
- tobuf
>= tobuf_len
- 1) {
293 to
= tok
- (tok
!= tobuf
);
303 static void recv_negotiate_str(int f_in
, struct name_num_obj
*nno
, char *tmpbuf
, int len
)
305 struct name_num_item
*ret
= NULL
;
308 len
= read_vstring(f_in
, tmpbuf
, MAX_NSTR_STRLEN
);
310 if (DEBUG_GTE(NSTR
, am_server
? 3 : 2)) {
312 rprintf(FINFO
, "Client %s list (on server): %s\n", nno
->type
, tmpbuf
);
314 rprintf(FINFO
, "Server %s list (on client): %s\n", nno
->type
, tmpbuf
);
318 int best
= nno
->saw_len
; /* We want best == 1 from the client list, so start with a big number. */
321 init_nno_saw(nno
, 1); /* Since we're parsing client names, anything we parse first is #1. */
322 for (tok
= strtok(tmpbuf
, " \t"); tok
; tok
= strtok(NULL
, " \t")) {
323 struct name_num_item
*nni
= get_nni_by_name(nno
, tok
, -1);
324 if (!nni
|| !nno
->saw
[nni
->num
] || best
<= nno
->saw
[nni
->num
])
327 best
= nno
->saw
[nni
->num
];
334 nno
->negotiated_name
= ret
->main_name
? ret
->main_name
: ret
->name
;
335 nno
->negotiated_num
= ret
->num
;
341 rprintf(FERROR
, "Failed to negotiate a common %s\n", nno
->type
);
342 exit_cleanup(RERR_UNSUPPORTED
);
345 /* The saw buffer is initialized and used to store ordinal values from 1 to N
346 * for the order of the args in the array. If dup_markup == '\0', duplicates
347 * are removed otherwise the char is prefixed to the duplicate term and, if it
348 * is an opening paren/bracket/brace, the matching closing char is suffixed. */
349 int get_default_nno_list(struct name_num_obj
*nno
, char *to_buf
, int to_buf_len
, char dup_markup
)
351 struct name_num_item
*nni
;
352 int len
= 0, cnt
= 0;
353 char delim
= '\0', post_delim
;
355 switch (dup_markup
) {
356 case '(': post_delim
= ')'; break;
357 case '[': post_delim
= ']'; break;
358 case '{': post_delim
= '}'; break;
359 default: post_delim
= '\0'; break;
362 init_nno_saw(nno
, 0);
364 for (nni
= nno
->list
, len
= 0; nni
->name
; nni
++) {
365 if (nni
->main_name
) {
373 to_buf
[len
++]= delim
;
376 len
+= strlcpy(to_buf
+len
, nni
->name
, to_buf_len
- len
);
377 if (len
>= to_buf_len
- 3)
378 exit_cleanup(RERR_UNSUPPORTED
); /* IMPOSSIBLE... */
380 to_buf
[len
++]= delim
;
383 nno
->saw
[nni
->num
] = ++cnt
;
389 static void send_negotiate_str(int f_out
, struct name_num_obj
*nno
, const char *env_name
)
391 char tmpbuf
[MAX_NSTR_STRLEN
];
392 const char *list_str
= getenv(env_name
);
393 int len
, fail_if_empty
= list_str
&& strstr(list_str
, "FAIL");
395 if (!do_negotiated_strings
) {
396 if (!am_server
&& fail_if_empty
) {
397 rprintf(FERROR
, "Remote rsync is too old for %s negotiation\n", nno
->type
);
398 exit_cleanup(RERR_UNSUPPORTED
);
403 if (list_str
&& *list_str
&& (!am_server
|| local_server
)) {
404 init_nno_saw(nno
, 0);
405 len
= parse_nni_str(nno
, list_str
, tmpbuf
, MAX_NSTR_STRLEN
);
406 if (fail_if_empty
&& !len
)
407 len
= strlcpy(tmpbuf
, "FAIL", MAX_NSTR_STRLEN
);
412 if (!list_str
|| !*list_str
)
413 len
= get_default_nno_list(nno
, tmpbuf
, MAX_NSTR_STRLEN
, '\0');
415 if (DEBUG_GTE(NSTR
, am_server
? 3 : 2)) {
417 rprintf(FINFO
, "Server %s list (on server): %s\n", nno
->type
, tmpbuf
);
419 rprintf(FINFO
, "Client %s list (on client): %s\n", nno
->type
, tmpbuf
);
423 /* A local server doesn't bother to send/recv the strings, it just constructs
424 * and parses the same string on both sides. */
425 recv_negotiate_str(-1, nno
, tmpbuf
, len
);
427 /* Each side sends their list of valid names to the other side and then both sides
428 * pick the first name in the client's list that is also in the server's list. */
429 write_vstring(f_out
, tmpbuf
, len
);
433 static void negotiate_the_strings(int f_in
, int f_out
)
435 /* We send all the negotiation strings before we start to read them to help avoid a slow startup. */
437 if (!checksum_choice
)
438 send_negotiate_str(f_out
, &valid_checksums
, "RSYNC_CHECKSUM_LIST");
440 if (do_compression
&& !compress_choice
)
441 send_negotiate_str(f_out
, &valid_compressions
, "RSYNC_COMPRESS_LIST");
443 if (valid_checksums
.saw
) {
444 char tmpbuf
[MAX_NSTR_STRLEN
];
445 recv_negotiate_str(f_in
, &valid_checksums
, tmpbuf
, -1);
448 if (valid_compressions
.saw
) {
449 char tmpbuf
[MAX_NSTR_STRLEN
];
450 recv_negotiate_str(f_in
, &valid_compressions
, tmpbuf
, -1);
454 void setup_protocol(int f_out
,int f_in
)
456 assert(file_extra_cnt
== 0);
457 assert(EXTRA64_CNT
== 2 || EXTRA64_CNT
== 1);
459 /* All int64 values must be set first so that they are guaranteed to be
460 * aligned for direct int64-pointer memory access. */
462 atimes_ndx
= (file_extra_cnt
+= EXTRA64_CNT
);
463 if (am_sender
) /* This is most likely in the in64 union as well. */
464 pathname_ndx
= (file_extra_cnt
+= PTR_EXTRA_CNT
);
466 depth_ndx
= ++file_extra_cnt
;
468 uid_ndx
= ++file_extra_cnt
;
470 gid_ndx
= ++file_extra_cnt
;
471 if (preserve_acls
&& !am_sender
)
472 acls_ndx
= ++file_extra_cnt
;
474 xattrs_ndx
= ++file_extra_cnt
;
477 set_allow_inc_recurse();
479 if (remote_protocol
== 0) {
480 if (am_server
&& !local_server
)
481 check_sub_protocol();
483 write_int(f_out
, protocol_version
);
484 remote_protocol
= read_int(f_in
);
485 if (protocol_version
> remote_protocol
)
486 protocol_version
= remote_protocol
;
488 if (read_batch
&& remote_protocol
> protocol_version
) {
489 rprintf(FERROR
, "The protocol version in the batch file is too new (%d > %d).\n",
490 remote_protocol
, protocol_version
);
491 exit_cleanup(RERR_PROTOCOL
);
494 if (DEBUG_GTE(PROTO
, 1)) {
495 rprintf(FINFO
, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
496 am_server
? "Server" : "Client", remote_protocol
, protocol_version
);
498 if (remote_protocol
< MIN_PROTOCOL_VERSION
499 || remote_protocol
> MAX_PROTOCOL_VERSION
) {
500 rprintf(FERROR
,"protocol version mismatch -- is your shell clean?\n");
501 rprintf(FERROR
,"(see the rsync man page for an explanation)\n");
502 exit_cleanup(RERR_PROTOCOL
);
504 if (remote_protocol
< OLD_PROTOCOL_VERSION
) {
505 rprintf(FINFO
,"%s is very old version of rsync, upgrade recommended.\n",
506 am_server
? "Client" : "Server");
508 if (protocol_version
< MIN_PROTOCOL_VERSION
) {
509 rprintf(FERROR
, "--protocol must be at least %d on the %s.\n",
510 MIN_PROTOCOL_VERSION
, am_server
? "Server" : "Client");
511 exit_cleanup(RERR_PROTOCOL
);
513 if (protocol_version
> PROTOCOL_VERSION
) {
514 rprintf(FERROR
, "--protocol must be no more than %d on the %s.\n",
515 PROTOCOL_VERSION
, am_server
? "Server" : "Client");
516 exit_cleanup(RERR_PROTOCOL
);
521 #ifndef SUPPORT_PREALLOCATION
522 if (preallocate_files
&& !am_sender
) {
523 rprintf(FERROR
, "preallocation is not supported on this %s\n",
524 am_server
? "Server" : "Client");
525 exit_cleanup(RERR_SYNTAX
);
529 if (protocol_version
< 30) {
530 if (append_mode
== 1)
532 if (preserve_acls
&& !local_server
) {
534 "--acls requires protocol 30 or higher"
535 " (negotiated %d).\n",
537 exit_cleanup(RERR_PROTOCOL
);
539 if (preserve_xattrs
&& !local_server
) {
541 "--xattrs requires protocol 30 or higher"
542 " (negotiated %d).\n",
544 exit_cleanup(RERR_PROTOCOL
);
548 if (delete_mode
&& !(delete_before
+delete_during
+delete_after
)) {
549 if (protocol_version
< 30)
555 if (protocol_version
< 29) {
558 "--fuzzy requires protocol 29 or higher"
559 " (negotiated %d).\n",
561 exit_cleanup(RERR_PROTOCOL
);
564 if (basis_dir_cnt
&& inplace
) {
566 "%s with --inplace requires protocol 29 or higher"
567 " (negotiated %d).\n",
568 alt_dest_opt(0), protocol_version
);
569 exit_cleanup(RERR_PROTOCOL
);
572 if (basis_dir_cnt
> 1) {
574 "Using more than one %s option requires protocol"
575 " 29 or higher (negotiated %d).\n",
576 alt_dest_opt(0), protocol_version
);
577 exit_cleanup(RERR_PROTOCOL
);
580 if (prune_empty_dirs
) {
582 "--prune-empty-dirs requires protocol 29 or higher"
583 " (negotiated %d).\n",
585 exit_cleanup(RERR_PROTOCOL
);
587 } else if (protocol_version
>= 30) {
589 compat_flags
= allow_inc_recurse
? CF_INC_RECURSE
: 0;
590 #ifdef CAN_SET_SYMLINK_TIMES
591 compat_flags
|= CF_SYMLINK_TIMES
;
594 compat_flags
|= CF_SYMLINK_ICONV
;
596 if (local_server
|| strchr(client_info
, 'f') != NULL
)
597 compat_flags
|= CF_SAFE_FLIST
;
598 if (local_server
|| strchr(client_info
, 'x') != NULL
)
599 compat_flags
|= CF_AVOID_XATTR_OPTIM
;
600 if (local_server
|| strchr(client_info
, 'C') != NULL
)
601 compat_flags
|= CF_CHKSUM_SEED_FIX
;
602 if (local_server
|| strchr(client_info
, 'I') != NULL
)
603 compat_flags
|= CF_INPLACE_PARTIAL_DIR
;
604 if (local_server
|| strchr(client_info
, 'v') != NULL
) {
605 if (!write_batch
|| protocol_version
>= 30) {
606 do_negotiated_strings
= 1;
607 compat_flags
|= CF_VARINT_FLIST_FLAGS
;
610 if (strchr(client_info
, 'V') != NULL
) { /* Support a pre-release 'V' that got superseded */
612 compat_flags
|= CF_VARINT_FLIST_FLAGS
;
613 write_byte(f_out
, compat_flags
);
615 write_varint(f_out
, compat_flags
);
616 } else { /* read_varint() is compatible with the older write_byte() when the 0x80 bit isn't on. */
617 compat_flags
= read_varint(f_in
);
618 if (compat_flags
& CF_VARINT_FLIST_FLAGS
)
619 do_negotiated_strings
= 1;
621 /* The inc_recurse var MUST be set to 0 or 1. */
622 inc_recurse
= compat_flags
& CF_INC_RECURSE
? 1 : 0;
623 want_xattr_optim
= protocol_version
>= 31 && !(compat_flags
& CF_AVOID_XATTR_OPTIM
);
624 proper_seed_order
= compat_flags
& CF_CHKSUM_SEED_FIX
? 1 : 0;
625 xfer_flags_as_varint
= compat_flags
& CF_VARINT_FLIST_FLAGS
? 1 : 0;
627 receiver_symlink_times
= am_server
628 ? strchr(client_info
, 'L') != NULL
629 : !!(compat_flags
& CF_SYMLINK_TIMES
);
631 #ifdef CAN_SET_SYMLINK_TIMES
633 receiver_symlink_times
= 1;
636 sender_symlink_iconv
= iconv_opt
&& (am_server
637 ? local_server
|| strchr(client_info
, 's') != NULL
638 : !!(compat_flags
& CF_SYMLINK_ICONV
));
640 if (inc_recurse
&& !allow_inc_recurse
) {
641 /* This should only be able to happen in a batch. */
643 "Incompatible options specified for inc-recursive %s.\n",
644 read_batch
? "batch file" : "connection");
645 exit_cleanup(RERR_SYNTAX
);
647 use_safe_inc_flist
= (compat_flags
& CF_SAFE_FLIST
) || protocol_version
>= 31;
648 need_messages_from_generator
= 1;
649 if (compat_flags
& CF_INPLACE_PARTIAL_DIR
)
651 #ifdef CAN_SET_SYMLINK_TIMES
652 } else if (!am_sender
) {
653 receiver_symlink_times
= 1;
657 if (need_unsorted_flist
&& (!am_sender
|| inc_recurse
))
658 unsort_ndx
= ++file_extra_cnt
;
660 if (partial_dir
&& *partial_dir
!= '/' && (!am_server
|| local_server
)) {
661 int rflags
= FILTRULE_NO_PREFIXES
| FILTRULE_DIRECTORY
;
662 if (!am_sender
|| protocol_version
>= 30)
663 rflags
|= FILTRULE_PERISHABLE
;
664 parse_filter_str(&filter_list
, partial_dir
, rule_template(rflags
), 0);
669 if (protect_args
&& files_from
) {
671 filesfrom_convert
= filesfrom_host
&& ic_send
!= (iconv_t
)-1;
673 filesfrom_convert
= !filesfrom_host
&& ic_recv
!= (iconv_t
)-1;
677 negotiate_the_strings(f_in
, f_out
);
681 checksum_seed
= time(NULL
) ^ (getpid() << 6);
682 write_int(f_out
, checksum_seed
);
684 checksum_seed
= read_int(f_in
);
687 parse_checksum_choice(1); /* Sets checksum_type & xfersum_type */
688 parse_compress_choice(1); /* Sets do_compression */
690 if (write_batch
&& !am_server
)
691 write_batch_shell_file();