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 *dest_option
;
55 extern char *files_from
;
56 extern char *filesfrom_host
;
57 extern const char *checksum_choice
;
58 extern const char *compress_choice
;
59 extern filter_rule_list filter_list
;
60 extern int need_unsorted_flist
;
62 extern iconv_t ic_send
, ic_recv
;
63 extern char *iconv_opt
;
65 extern struct name_num_obj valid_checksums
;
67 int remote_protocol
= 0;
68 int file_extra_cnt
= 0; /* count of file-list extras that everyone gets */
71 int use_safe_inc_flist
= 0;
72 int want_xattr_optim
= 0;
73 int proper_seed_order
= 0;
74 int inplace_partial
= 0;
75 int do_negotiated_strings
= 0;
77 /* These index values are for the file-list's extra-attribute array. */
78 int pathname_ndx
, depth_ndx
, atimes_ndx
, uid_ndx
, gid_ndx
, acls_ndx
, xattrs_ndx
, unsort_ndx
;
80 int receiver_symlink_times
= 0; /* receiver can set the time on a symlink */
81 int sender_symlink_iconv
= 0; /* sender should convert symlink content */
84 int filesfrom_convert
= 0;
87 #define MAX_NSTR_STRLEN 256
89 struct name_num_obj valid_compressions
= {
90 "compress", NULL
, NULL
, 0, 0, {
92 { CPRES_ZSTD
, "zstd", NULL
},
95 { CPRES_LZ4
, "lz4", NULL
},
97 { CPRES_ZLIBX
, "zlibx", NULL
},
98 { CPRES_ZLIB
, "zlib", NULL
},
99 { CPRES_NONE
, "none", NULL
},
104 #define CF_INC_RECURSE (1<<0)
105 #define CF_SYMLINK_TIMES (1<<1)
106 #define CF_SYMLINK_ICONV (1<<2)
107 #define CF_SAFE_FLIST (1<<3)
108 #define CF_AVOID_XATTR_OPTIM (1<<4)
109 #define CF_CHKSUM_SEED_FIX (1<<5)
110 #define CF_INPLACE_PARTIAL_DIR (1<<6)
111 #define CF_VARINT_FLIST_FLAGS (1<<7)
113 static const char *client_info
;
115 /* The server makes sure that if either side only supports a pre-release
116 * version of a protocol, that both sides must speak a compatible version
117 * of that protocol for it to be advertised as available. */
118 static void check_sub_protocol(void)
121 int their_protocol
, their_sub
;
122 #if SUBPROTOCOL_VERSION != 0
123 int our_sub
= protocol_version
< PROTOCOL_VERSION
? 0 : SUBPROTOCOL_VERSION
;
128 /* client_info starts with VER.SUB string if client is a pre-release. */
129 if (!(their_protocol
= atoi(client_info
))
130 || !(dot
= strchr(client_info
, '.'))
131 || !(their_sub
= atoi(dot
+1))) {
132 #if SUBPROTOCOL_VERSION != 0
139 if (their_protocol
< protocol_version
) {
141 protocol_version
= their_protocol
- 1;
145 if (their_protocol
> protocol_version
)
146 their_sub
= 0; /* 0 == final version of older protocol */
147 if (their_sub
!= our_sub
)
151 void set_allow_inc_recurse(void)
153 client_info
= shell_cmd
? shell_cmd
: "";
155 if (!recurse
|| use_qsort
)
156 allow_inc_recurse
= 0;
158 && (delete_before
|| delete_after
159 || delay_updates
|| prune_empty_dirs
))
160 allow_inc_recurse
= 0;
161 else if (am_server
&& !local_server
162 && (strchr(client_info
, 'i') == NULL
))
163 allow_inc_recurse
= 0;
166 void parse_compress_choice(int final_call
)
168 if (valid_compressions
.negotiated_name
)
169 do_compression
= valid_compressions
.negotiated_num
;
170 else if (compress_choice
) {
171 struct name_num_item
*nni
= get_nni_by_name(&valid_compressions
, compress_choice
, -1);
173 rprintf(FERROR
, "unknown compress name: %s\n", compress_choice
);
174 exit_cleanup(RERR_UNSUPPORTED
);
176 do_compression
= nni
->num
;
177 } else if (do_compression
)
178 do_compression
= CPRES_ZLIB
;
180 do_compression
= CPRES_NONE
;
182 if (do_compression
!= CPRES_NONE
&& final_call
)
183 init_compression_level(); /* There's a chance this might turn compression off! */
185 if (do_compression
== CPRES_NONE
)
186 compress_choice
= NULL
;
188 /* Snag the compression name for both write_batch's option output & the following debug output. */
189 if (valid_compressions
.negotiated_name
)
190 compress_choice
= valid_compressions
.negotiated_name
;
191 else if (compress_choice
== NULL
) {
192 struct name_num_item
*nni
= get_nni_by_num(&valid_compressions
, do_compression
);
193 compress_choice
= nni
? nni
->name
: "UNKNOWN";
196 if (final_call
&& DEBUG_GTE(NSTR
, am_server
? 3 : 1)
197 && (do_compression
!= CPRES_NONE
|| do_compression_level
!= CLVL_NOT_SPECIFIED
)) {
198 rprintf(FINFO
, "%s%s compress: %s (level %d)\n",
199 am_server
? "Server" : "Client",
200 valid_compressions
.negotiated_name
? " negotiated" : "",
201 compress_choice
, do_compression_level
);
205 struct name_num_item
*get_nni_by_name(struct name_num_obj
*nno
, const char *name
, int len
)
207 struct name_num_item
*nni
;
212 for (nni
= nno
->list
; nni
->name
; nni
++) {
213 if (strncasecmp(name
, nni
->name
, len
) == 0 && nni
->name
[len
] == '\0')
220 struct name_num_item
*get_nni_by_num(struct name_num_obj
*nno
, int num
)
222 struct name_num_item
*nni
;
224 for (nni
= nno
->list
; nni
->name
; nni
++) {
232 static void init_nno_saw(struct name_num_obj
*nno
, int val
)
234 struct name_num_item
*nni
;
238 for (nni
= nno
->list
; nni
->name
; nni
++) {
239 if (nni
->num
>= nno
->saw_len
)
240 nno
->saw_len
= nni
->num
+ 1;
245 if (!(nno
->saw
= new_array0(uchar
, nno
->saw_len
)))
246 out_of_memory("init_nno_saw");
248 /* We'll take this opportunity to make sure that the main_name values are set right. */
249 for (cnt
= 1, nni
= nno
->list
; nni
->name
; nni
++, cnt
++) {
250 if (nno
->saw
[nni
->num
])
251 nni
->main_name
= nno
->list
[nno
->saw
[nni
->num
]-1].name
;
253 nno
->saw
[nni
->num
] = cnt
;
257 memset(nno
->saw
, val
, nno
->saw_len
);
260 /* Simplify the user-provided string so that it contains valid names without any duplicates.
261 * It also sets the "saw" flags to a 1-relative count of which name was seen first. */
262 static int parse_nni_str(struct name_num_obj
*nno
, const char *from
, char *tobuf
, int tobuf_len
)
264 char *to
= tobuf
, *tok
= NULL
;
268 if (*from
== ' ' || !*from
) {
270 struct name_num_item
*nni
= get_nni_by_name(nno
, tok
, to
- tok
);
271 if (nni
&& !nno
->saw
[nni
->num
]) {
272 nno
->saw
[nni
->num
] = ++cnt
;
273 if (nni
->main_name
) {
274 to
= tok
+ strlcpy(tok
, nni
->main_name
, tobuf_len
- (tok
- tobuf
));
275 if (to
- tobuf
>= tobuf_len
) {
281 to
= tok
- (tok
!= tobuf
);
293 if (to
- tobuf
>= tobuf_len
- 1) {
294 to
= tok
- (tok
!= tobuf
);
304 static void recv_negotiate_str(int f_in
, struct name_num_obj
*nno
, char *tmpbuf
, int len
)
306 struct name_num_item
*ret
= NULL
;
309 len
= read_vstring(f_in
, tmpbuf
, MAX_NSTR_STRLEN
);
311 if (DEBUG_GTE(NSTR
, am_server
? 3 : 2)) {
313 rprintf(FINFO
, "Client %s list (on server): %s\n", nno
->type
, tmpbuf
);
315 rprintf(FINFO
, "Server %s list (on client): %s\n", nno
->type
, tmpbuf
);
319 int best
= nno
->saw_len
; /* We want best == 1 from the client list, so start with a big number. */
322 init_nno_saw(nno
, 1); /* Since we're parsing client names, anything we parse first is #1. */
323 for (tok
= strtok(tmpbuf
, " \t"); tok
; tok
= strtok(NULL
, " \t")) {
324 struct name_num_item
*nni
= get_nni_by_name(nno
, tok
, -1);
325 if (!nni
|| !nno
->saw
[nni
->num
] || best
<= nno
->saw
[nni
->num
])
328 best
= nno
->saw
[nni
->num
];
335 nno
->negotiated_name
= ret
->main_name
? ret
->main_name
: ret
->name
;
336 nno
->negotiated_num
= ret
->num
;
342 rprintf(FERROR
, "Failed to negotiate a common %s\n", nno
->type
);
343 exit_cleanup(RERR_UNSUPPORTED
);
346 /* The saw buffer is initialized and used to store ordinal values from 1 to N
347 * for the order of the args in the array. If dup_markup == '\0', duplicates
348 * are removed otherwise the char is prefixed to the duplicate term and, if it
349 * is an opening paren/bracket/brace, the matching closing char is suffixed. */
350 int get_default_nno_list(struct name_num_obj
*nno
, char *to_buf
, int to_buf_len
, char dup_markup
)
352 struct name_num_item
*nni
;
353 int len
= 0, cnt
= 0;
354 char delim
= '\0', post_delim
;
356 switch (dup_markup
) {
357 case '(': post_delim
= ')'; break;
358 case '[': post_delim
= ']'; break;
359 case '{': post_delim
= '}'; break;
360 default: post_delim
= '\0'; break;
363 init_nno_saw(nno
, 0);
365 for (nni
= nno
->list
, len
= 0; nni
->name
; nni
++) {
366 if (nni
->main_name
) {
374 to_buf
[len
++]= delim
;
377 len
+= strlcpy(to_buf
+len
, nni
->name
, to_buf_len
- len
);
378 if (len
>= to_buf_len
- 3)
379 exit_cleanup(RERR_UNSUPPORTED
); /* IMPOSSIBLE... */
381 to_buf
[len
++]= delim
;
384 nno
->saw
[nni
->num
] = ++cnt
;
390 static void send_negotiate_str(int f_out
, struct name_num_obj
*nno
, const char *env_name
)
392 char tmpbuf
[MAX_NSTR_STRLEN
];
393 const char *list_str
= getenv(env_name
);
394 int len
, fail_if_empty
= list_str
&& strstr(list_str
, "FAIL");
396 if (!do_negotiated_strings
) {
397 if (!am_server
&& fail_if_empty
) {
398 rprintf(FERROR
, "Remote rsync is too old for %s negotation\n", nno
->type
);
399 exit_cleanup(RERR_UNSUPPORTED
);
404 if (list_str
&& *list_str
&& (!am_server
|| local_server
)) {
405 init_nno_saw(nno
, 0);
406 len
= parse_nni_str(nno
, list_str
, tmpbuf
, MAX_NSTR_STRLEN
);
407 if (fail_if_empty
&& !len
)
408 len
= strlcpy(tmpbuf
, "FAIL", MAX_NSTR_STRLEN
);
413 if (!list_str
|| !*list_str
)
414 len
= get_default_nno_list(nno
, tmpbuf
, MAX_NSTR_STRLEN
, '\0');
416 if (DEBUG_GTE(NSTR
, am_server
? 3 : 2)) {
418 rprintf(FINFO
, "Server %s list (on server): %s\n", nno
->type
, tmpbuf
);
420 rprintf(FINFO
, "Client %s list (on client): %s\n", nno
->type
, tmpbuf
);
424 /* A local server doesn't bother to send/recv the strings, it just constructs
425 * and parses the same string on both sides. */
426 recv_negotiate_str(-1, nno
, tmpbuf
, len
);
428 /* Each side sends their list of valid names to the other side and then both sides
429 * pick the first name in the client's list that is also in the server's list. */
430 write_vstring(f_out
, tmpbuf
, len
);
434 static void negotiate_the_strings(int f_in
, int f_out
)
436 /* We send all the negotiation strings before we start to read them to help avoid a slow startup. */
438 if (!checksum_choice
)
439 send_negotiate_str(f_out
, &valid_checksums
, "RSYNC_CHECKSUM_LIST");
441 if (do_compression
&& !compress_choice
)
442 send_negotiate_str(f_out
, &valid_compressions
, "RSYNC_COMPRESS_LIST");
444 if (valid_checksums
.saw
) {
445 char tmpbuf
[MAX_NSTR_STRLEN
];
446 recv_negotiate_str(f_in
, &valid_checksums
, tmpbuf
, -1);
449 if (valid_compressions
.saw
) {
450 char tmpbuf
[MAX_NSTR_STRLEN
];
451 recv_negotiate_str(f_in
, &valid_compressions
, tmpbuf
, -1);
455 void setup_protocol(int f_out
,int f_in
)
457 assert(file_extra_cnt
== 0);
458 assert(EXTRA64_CNT
== 2 || EXTRA64_CNT
== 1);
460 /* All int64 values must be set first so that they are guaranteed to be
461 * aligned for direct int64-pointer memory access. */
463 atimes_ndx
= (file_extra_cnt
+= EXTRA64_CNT
);
464 if (am_sender
) /* This is most likely in the in64 union as well. */
465 pathname_ndx
= (file_extra_cnt
+= PTR_EXTRA_CNT
);
467 depth_ndx
= ++file_extra_cnt
;
469 uid_ndx
= ++file_extra_cnt
;
471 gid_ndx
= ++file_extra_cnt
;
472 if (preserve_acls
&& !am_sender
)
473 acls_ndx
= ++file_extra_cnt
;
475 xattrs_ndx
= ++file_extra_cnt
;
478 set_allow_inc_recurse();
480 if (remote_protocol
== 0) {
481 if (am_server
&& !local_server
)
482 check_sub_protocol();
484 write_int(f_out
, protocol_version
);
485 remote_protocol
= read_int(f_in
);
486 if (protocol_version
> remote_protocol
)
487 protocol_version
= remote_protocol
;
489 if (read_batch
&& remote_protocol
> protocol_version
) {
490 rprintf(FERROR
, "The protocol version in the batch file is too new (%d > %d).\n",
491 remote_protocol
, protocol_version
);
492 exit_cleanup(RERR_PROTOCOL
);
495 if (DEBUG_GTE(PROTO
, 1)) {
496 rprintf(FINFO
, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
497 am_server
? "Server" : "Client", remote_protocol
, protocol_version
);
499 if (remote_protocol
< MIN_PROTOCOL_VERSION
500 || remote_protocol
> MAX_PROTOCOL_VERSION
) {
501 rprintf(FERROR
,"protocol version mismatch -- is your shell clean?\n");
502 rprintf(FERROR
,"(see the rsync man page for an explanation)\n");
503 exit_cleanup(RERR_PROTOCOL
);
505 if (remote_protocol
< OLD_PROTOCOL_VERSION
) {
506 rprintf(FINFO
,"%s is very old version of rsync, upgrade recommended.\n",
507 am_server
? "Client" : "Server");
509 if (protocol_version
< MIN_PROTOCOL_VERSION
) {
510 rprintf(FERROR
, "--protocol must be at least %d on the %s.\n",
511 MIN_PROTOCOL_VERSION
, am_server
? "Server" : "Client");
512 exit_cleanup(RERR_PROTOCOL
);
514 if (protocol_version
> PROTOCOL_VERSION
) {
515 rprintf(FERROR
, "--protocol must be no more than %d on the %s.\n",
516 PROTOCOL_VERSION
, am_server
? "Server" : "Client");
517 exit_cleanup(RERR_PROTOCOL
);
522 #ifndef SUPPORT_PREALLOCATION
523 if (preallocate_files
&& !am_sender
) {
524 rprintf(FERROR
, "preallocation is not supported on this %s\n",
525 am_server
? "Server" : "Client");
526 exit_cleanup(RERR_SYNTAX
);
530 if (protocol_version
< 30) {
531 if (append_mode
== 1)
533 if (preserve_acls
&& !local_server
) {
535 "--acls requires protocol 30 or higher"
536 " (negotiated %d).\n",
538 exit_cleanup(RERR_PROTOCOL
);
540 if (preserve_xattrs
&& !local_server
) {
542 "--xattrs requires protocol 30 or higher"
543 " (negotiated %d).\n",
545 exit_cleanup(RERR_PROTOCOL
);
549 if (delete_mode
&& !(delete_before
+delete_during
+delete_after
)) {
550 if (protocol_version
< 30)
556 if (protocol_version
< 29) {
559 "--fuzzy requires protocol 29 or higher"
560 " (negotiated %d).\n",
562 exit_cleanup(RERR_PROTOCOL
);
565 if (basis_dir_cnt
&& inplace
) {
567 "%s with --inplace requires protocol 29 or higher"
568 " (negotiated %d).\n",
569 dest_option
, protocol_version
);
570 exit_cleanup(RERR_PROTOCOL
);
573 if (basis_dir_cnt
> 1) {
575 "Using more than one %s option requires protocol"
576 " 29 or higher (negotiated %d).\n",
577 dest_option
, protocol_version
);
578 exit_cleanup(RERR_PROTOCOL
);
581 if (prune_empty_dirs
) {
583 "--prune-empty-dirs requires protocol 29 or higher"
584 " (negotiated %d).\n",
586 exit_cleanup(RERR_PROTOCOL
);
588 } else if (protocol_version
>= 30) {
590 compat_flags
= allow_inc_recurse
? CF_INC_RECURSE
: 0;
591 #ifdef CAN_SET_SYMLINK_TIMES
592 compat_flags
|= CF_SYMLINK_TIMES
;
595 compat_flags
|= CF_SYMLINK_ICONV
;
597 if (local_server
|| strchr(client_info
, 'f') != NULL
)
598 compat_flags
|= CF_SAFE_FLIST
;
599 if (local_server
|| strchr(client_info
, 'x') != NULL
)
600 compat_flags
|= CF_AVOID_XATTR_OPTIM
;
601 if (local_server
|| strchr(client_info
, 'C') != NULL
)
602 compat_flags
|= CF_CHKSUM_SEED_FIX
;
603 if (local_server
|| strchr(client_info
, 'I') != NULL
)
604 compat_flags
|= CF_INPLACE_PARTIAL_DIR
;
605 if (local_server
|| strchr(client_info
, 'v') != NULL
) {
606 if (!write_batch
|| protocol_version
>= 30) {
607 do_negotiated_strings
= 1;
608 compat_flags
|= CF_VARINT_FLIST_FLAGS
;
611 if (strchr(client_info
, 'V') != NULL
) { /* Support a pre-release 'V' that got superseded */
613 compat_flags
|= CF_VARINT_FLIST_FLAGS
;
614 write_byte(f_out
, compat_flags
);
616 write_varint(f_out
, compat_flags
);
617 } else { /* read_varint() is compatible with the older write_byte() when the 0x80 bit isn't on. */
618 compat_flags
= read_varint(f_in
);
619 if (compat_flags
& CF_VARINT_FLIST_FLAGS
)
620 do_negotiated_strings
= 1;
622 /* The inc_recurse var MUST be set to 0 or 1. */
623 inc_recurse
= compat_flags
& CF_INC_RECURSE
? 1 : 0;
624 want_xattr_optim
= protocol_version
>= 31 && !(compat_flags
& CF_AVOID_XATTR_OPTIM
);
625 proper_seed_order
= compat_flags
& CF_CHKSUM_SEED_FIX
? 1 : 0;
626 xfer_flags_as_varint
= compat_flags
& CF_VARINT_FLIST_FLAGS
? 1 : 0;
628 receiver_symlink_times
= am_server
629 ? strchr(client_info
, 'L') != NULL
630 : !!(compat_flags
& CF_SYMLINK_TIMES
);
632 #ifdef CAN_SET_SYMLINK_TIMES
634 receiver_symlink_times
= 1;
637 sender_symlink_iconv
= iconv_opt
&& (am_server
638 ? local_server
|| strchr(client_info
, 's') != NULL
639 : !!(compat_flags
& CF_SYMLINK_ICONV
));
641 if (inc_recurse
&& !allow_inc_recurse
) {
642 /* This should only be able to happen in a batch. */
644 "Incompatible options specified for inc-recursive %s.\n",
645 read_batch
? "batch file" : "connection");
646 exit_cleanup(RERR_SYNTAX
);
648 use_safe_inc_flist
= (compat_flags
& CF_SAFE_FLIST
) || protocol_version
>= 31;
649 need_messages_from_generator
= 1;
650 if (compat_flags
& CF_INPLACE_PARTIAL_DIR
)
652 #ifdef CAN_SET_SYMLINK_TIMES
653 } else if (!am_sender
) {
654 receiver_symlink_times
= 1;
658 if (need_unsorted_flist
&& (!am_sender
|| inc_recurse
))
659 unsort_ndx
= ++file_extra_cnt
;
661 if (partial_dir
&& *partial_dir
!= '/' && (!am_server
|| local_server
)) {
662 int rflags
= FILTRULE_NO_PREFIXES
| FILTRULE_DIRECTORY
;
663 if (!am_sender
|| protocol_version
>= 30)
664 rflags
|= FILTRULE_PERISHABLE
;
665 parse_filter_str(&filter_list
, partial_dir
, rule_template(rflags
), 0);
670 if (protect_args
&& files_from
) {
672 filesfrom_convert
= filesfrom_host
&& ic_send
!= (iconv_t
)-1;
674 filesfrom_convert
= !filesfrom_host
&& ic_recv
!= (iconv_t
)-1;
678 negotiate_the_strings(f_in
, f_out
);
682 checksum_seed
= time(NULL
) ^ (getpid() << 6);
683 write_int(f_out
, checksum_seed
);
685 checksum_seed
= read_int(f_in
);
688 parse_checksum_choice(1); /* Sets checksum_type & xfersum_type */
689 parse_compress_choice(1); /* Sets do_compression */
691 if (write_batch
&& !am_server
)
692 write_batch_shell_file();