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;
177 validate_choice_vs_env(do_compression, -1);
178 } else if (do_compression)
179 do_compression = CPRES_ZLIB;
181 do_compression = CPRES_NONE;
183 if (do_compression != CPRES_NONE && final_call)
184 init_compression_level(); /* There's a chance this might turn compression off! */
186 if (do_compression == CPRES_NONE)
187 compress_choice = NULL;
189 /* Snag the compression name for both write_batch's option output & the following debug output. */
190 if (valid_compressions.negotiated_name)
191 compress_choice = valid_compressions.negotiated_name;
192 else if (compress_choice == NULL) {
193 struct name_num_item *nni = get_nni_by_num(&valid_compressions, do_compression);
194 compress_choice = nni ? nni->name : "UNKNOWN";
197 if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)
198 && (do_compression != CPRES_NONE || do_compression_level != CLVL_NOT_SPECIFIED)) {
199 rprintf(FINFO, "%s%s compress: %s (level %d)\n",
200 am_server ? "Server" : "Client",
201 valid_compressions.negotiated_name ? " negotiated" : "",
202 compress_choice, do_compression_level);
206 struct name_num_item *get_nni_by_name(struct name_num_obj *nno, const char *name, int len)
208 struct name_num_item *nni;
213 for (nni = nno->list; nni->name; nni++) {
214 if (strncasecmp(name, nni->name, len) == 0 && nni->name[len] == '\0')
221 struct name_num_item *get_nni_by_num(struct name_num_obj *nno, int num)
223 struct name_num_item *nni;
225 for (nni = nno->list; nni->name; nni++) {
233 static void init_nno_saw(struct name_num_obj *nno, int val)
235 struct name_num_item *nni;
239 for (nni = nno->list; nni->name; nni++) {
240 if (nni->num >= nno->saw_len)
241 nno->saw_len = nni->num + 1;
246 nno->saw = new_array0(uchar, nno->saw_len);
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. */
323 /* Since we're parsing client names, anything in our list that we parse first is #1. */
324 for (j = 0; j < nno->saw_len; j++) {
329 for (tok = strtok(tmpbuf, " \t"); tok; tok = strtok(NULL, " \t")) {
330 struct name_num_item *nni = get_nni_by_name(nno, tok, -1);
331 if (!nni || !nno->saw[nni->num] || best <= nno->saw[nni->num])
334 best = nno->saw[nni->num];
341 nno->negotiated_name = ret->main_name ? ret->main_name : ret->name;
342 nno->negotiated_num = ret->num;
347 if (!am_server || !do_negotiated_strings)
348 rprintf(FERROR, "Failed to negotiate a %s choice.\n", nno->type);
349 exit_cleanup(RERR_UNSUPPORTED);
352 /* If num2 < 0 then the caller is checking compress values, otherwise checksum values. */
353 void validate_choice_vs_env(int num1, int num2)
355 struct name_num_obj *nno = num2 < 0 ? &valid_compressions : &valid_checksums;
356 const char *list_str = getenv(num2 < 0 ? "RSYNC_COMPRESS_LIST" : "RSYNC_CHECKSUM_LIST");
357 char tmpbuf[MAX_NSTR_STRLEN];
359 if (!list_str || !*list_str)
362 init_nno_saw(nno, 0);
363 parse_nni_str(nno, list_str, tmpbuf, MAX_NSTR_STRLEN);
365 if (num2 >= 0) /* If "md4" is in the env list, all the old MD4 choices are OK too. */
366 nno->saw[CSUM_MD4_ARCHAIC] = nno->saw[CSUM_MD4_BUSTED] = nno->saw[CSUM_MD4_OLD] = nno->saw[CSUM_MD4];
368 if (!nno->saw[num1] || (num2 >= 0 && !nno->saw[num2])) {
369 rprintf(FERROR, "Your --%s-choice value (%s) was refused by the server.\n",
370 num2 < 0 ? "compress" : "checksum", num2 < 0 ? compress_choice : checksum_choice);
371 exit_cleanup(RERR_UNSUPPORTED);
378 /* The saw buffer is initialized and used to store ordinal values from 1 to N
379 * for the order of the args in the array. If dup_markup == '\0', duplicates
380 * are removed otherwise the char is prefixed to the duplicate term and, if it
381 * is an opening paren/bracket/brace, the matching closing char is suffixed.
382 * "none" is removed on the client side unless dup_markup != '\0'. */
383 int get_default_nno_list(struct name_num_obj *nno, char *to_buf, int to_buf_len, char dup_markup)
385 struct name_num_item *nni;
386 int len = 0, cnt = 0;
387 char delim = '\0', post_delim;
389 switch (dup_markup) {
390 case '(': post_delim = ')'; break;
391 case '[': post_delim = ']'; break;
392 case '{': post_delim = '}'; break;
393 default: post_delim = '\0'; break;
396 init_nno_saw(nno, 0);
398 for (nni = nno->list, len = 0; nni->name; nni++) {
399 if (nni->main_name) {
404 if (nni->num == 0 && !am_server && !dup_markup)
409 to_buf[len++]= delim;
412 len += strlcpy(to_buf+len, nni->name, to_buf_len - len);
413 if (len >= to_buf_len - 3)
414 exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE... */
416 to_buf[len++]= delim;
419 nno->saw[nni->num] = ++cnt;
425 static void send_negotiate_str(int f_out, struct name_num_obj *nno, const char *env_name)
427 char tmpbuf[MAX_NSTR_STRLEN];
428 const char *list_str = getenv(env_name);
431 if (list_str && *list_str) {
432 init_nno_saw(nno, 0);
433 len = parse_nni_str(nno, list_str, tmpbuf, MAX_NSTR_STRLEN);
435 len = strlcpy(tmpbuf, "FAIL", MAX_NSTR_STRLEN);
440 if (!list_str || !*list_str)
441 len = get_default_nno_list(nno, tmpbuf, MAX_NSTR_STRLEN, '\0');
443 if (DEBUG_GTE(NSTR, am_server ? 3 : 2)) {
445 rprintf(FINFO, "Server %s list (on server): %s\n", nno->type, tmpbuf);
447 rprintf(FINFO, "Client %s list (on client): %s\n", nno->type, tmpbuf);
451 /* A local server doesn't bother to send/recv the strings, it just constructs
452 * and parses the same string on both sides. */
453 recv_negotiate_str(-1, nno, tmpbuf, len);
454 } else if (do_negotiated_strings) {
455 /* Each side sends their list of valid names to the other side and then both sides
456 * pick the first name in the client's list that is also in the server's list. */
457 write_vstring(f_out, tmpbuf, len);
461 static void negotiate_the_strings(int f_in, int f_out)
463 /* We send all the negotiation strings before we start to read them to help avoid a slow startup. */
465 if (!checksum_choice)
466 send_negotiate_str(f_out, &valid_checksums, "RSYNC_CHECKSUM_LIST");
468 if (do_compression && !compress_choice)
469 send_negotiate_str(f_out, &valid_compressions, "RSYNC_COMPRESS_LIST");
471 if (valid_checksums.saw) {
472 char tmpbuf[MAX_NSTR_STRLEN];
474 if (do_negotiated_strings)
477 len = strlcpy(tmpbuf, protocol_version >= 30 ? "md5" : "md4", MAX_NSTR_STRLEN);
478 recv_negotiate_str(f_in, &valid_checksums, tmpbuf, len);
481 if (valid_compressions.saw) {
482 char tmpbuf[MAX_NSTR_STRLEN];
484 if (do_negotiated_strings)
487 len = strlcpy(tmpbuf, "zlib", MAX_NSTR_STRLEN);
488 recv_negotiate_str(f_in, &valid_compressions, tmpbuf, len);
491 /* If the other side is too old to negotiate, the above steps just made sure that
492 * the env didn't disallow the old algorithm. Mark things as non-negotiated. */
493 if (!do_negotiated_strings)
494 valid_checksums.negotiated_name = valid_compressions.negotiated_name = NULL;
497 void setup_protocol(int f_out,int f_in)
499 assert(file_extra_cnt == 0);
500 assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
502 /* All int64 values must be set first so that they are guaranteed to be
503 * aligned for direct int64-pointer memory access. */
505 atimes_ndx = (file_extra_cnt += EXTRA64_CNT);
506 if (am_sender) /* This is most likely in the in64 union as well. */
507 pathname_ndx = (file_extra_cnt += PTR_EXTRA_CNT);
509 depth_ndx = ++file_extra_cnt;
511 uid_ndx = ++file_extra_cnt;
513 gid_ndx = ++file_extra_cnt;
514 if (preserve_acls && !am_sender)
515 acls_ndx = ++file_extra_cnt;
517 xattrs_ndx = ++file_extra_cnt;
520 set_allow_inc_recurse();
522 if (remote_protocol == 0) {
523 if (am_server && !local_server)
524 check_sub_protocol();
526 write_int(f_out, protocol_version);
527 remote_protocol = read_int(f_in);
528 if (protocol_version > remote_protocol)
529 protocol_version = remote_protocol;
531 if (read_batch && remote_protocol > protocol_version) {
532 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
533 remote_protocol, protocol_version);
534 exit_cleanup(RERR_PROTOCOL);
537 if (DEBUG_GTE(PROTO, 1)) {
538 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
539 am_server? "Server" : "Client", remote_protocol, protocol_version);
541 if (remote_protocol < MIN_PROTOCOL_VERSION
542 || remote_protocol > MAX_PROTOCOL_VERSION) {
543 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
544 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
545 exit_cleanup(RERR_PROTOCOL);
547 if (remote_protocol < OLD_PROTOCOL_VERSION) {
548 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
549 am_server? "Client" : "Server");
551 if (protocol_version < MIN_PROTOCOL_VERSION) {
552 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
553 MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
554 exit_cleanup(RERR_PROTOCOL);
556 if (protocol_version > PROTOCOL_VERSION) {
557 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
558 PROTOCOL_VERSION, am_server? "Server" : "Client");
559 exit_cleanup(RERR_PROTOCOL);
564 #ifndef SUPPORT_PREALLOCATION
565 if (preallocate_files && !am_sender) {
566 rprintf(FERROR, "preallocation is not supported on this %s\n",
567 am_server ? "Server" : "Client");
568 exit_cleanup(RERR_SYNTAX);
572 if (protocol_version < 30) {
573 if (append_mode == 1)
575 if (preserve_acls && !local_server) {
577 "--acls requires protocol 30 or higher"
578 " (negotiated %d).\n",
580 exit_cleanup(RERR_PROTOCOL);
582 if (preserve_xattrs && !local_server) {
584 "--xattrs requires protocol 30 or higher"
585 " (negotiated %d).\n",
587 exit_cleanup(RERR_PROTOCOL);
591 if (delete_mode && !(delete_before+delete_during+delete_after)) {
592 if (protocol_version < 30)
598 if (protocol_version < 29) {
601 "--fuzzy requires protocol 29 or higher"
602 " (negotiated %d).\n",
604 exit_cleanup(RERR_PROTOCOL);
607 if (basis_dir_cnt && inplace) {
609 "%s with --inplace requires protocol 29 or higher"
610 " (negotiated %d).\n",
611 alt_dest_opt(0), protocol_version);
612 exit_cleanup(RERR_PROTOCOL);
615 if (basis_dir_cnt > 1) {
617 "Using more than one %s option requires protocol"
618 " 29 or higher (negotiated %d).\n",
619 alt_dest_opt(0), protocol_version);
620 exit_cleanup(RERR_PROTOCOL);
623 if (prune_empty_dirs) {
625 "--prune-empty-dirs requires protocol 29 or higher"
626 " (negotiated %d).\n",
628 exit_cleanup(RERR_PROTOCOL);
630 } else if (protocol_version >= 30) {
632 compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
633 #ifdef CAN_SET_SYMLINK_TIMES
634 compat_flags |= CF_SYMLINK_TIMES;
637 compat_flags |= CF_SYMLINK_ICONV;
639 if (local_server || strchr(client_info, 'f') != NULL)
640 compat_flags |= CF_SAFE_FLIST;
641 if (local_server || strchr(client_info, 'x') != NULL)
642 compat_flags |= CF_AVOID_XATTR_OPTIM;
643 if (local_server || strchr(client_info, 'C') != NULL)
644 compat_flags |= CF_CHKSUM_SEED_FIX;
645 if (local_server || strchr(client_info, 'I') != NULL)
646 compat_flags |= CF_INPLACE_PARTIAL_DIR;
647 if (local_server || strchr(client_info, 'v') != NULL) {
648 if (!write_batch || protocol_version >= 30) {
649 do_negotiated_strings = 1;
650 compat_flags |= CF_VARINT_FLIST_FLAGS;
653 if (strchr(client_info, 'V') != NULL) { /* Support a pre-release 'V' that got superseded */
655 compat_flags |= CF_VARINT_FLIST_FLAGS;
656 write_byte(f_out, compat_flags);
658 write_varint(f_out, compat_flags);
659 } else { /* read_varint() is compatible with the older write_byte() when the 0x80 bit isn't on. */
660 compat_flags = read_varint(f_in);
661 if (compat_flags & CF_VARINT_FLIST_FLAGS)
662 do_negotiated_strings = 1;
664 /* The inc_recurse var MUST be set to 0 or 1. */
665 inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
666 want_xattr_optim = protocol_version >= 31 && !(compat_flags & CF_AVOID_XATTR_OPTIM);
667 proper_seed_order = compat_flags & CF_CHKSUM_SEED_FIX ? 1 : 0;
668 xfer_flags_as_varint = compat_flags & CF_VARINT_FLIST_FLAGS ? 1 : 0;
670 receiver_symlink_times = am_server
671 ? strchr(client_info, 'L') != NULL
672 : !!(compat_flags & CF_SYMLINK_TIMES);
674 #ifdef CAN_SET_SYMLINK_TIMES
676 receiver_symlink_times = 1;
679 sender_symlink_iconv = iconv_opt && (am_server
680 ? local_server || strchr(client_info, 's') != NULL
681 : !!(compat_flags & CF_SYMLINK_ICONV));
683 if (inc_recurse && !allow_inc_recurse) {
684 /* This should only be able to happen in a batch. */
686 "Incompatible options specified for inc-recursive %s.\n",
687 read_batch ? "batch file" : "connection");
688 exit_cleanup(RERR_SYNTAX);
690 use_safe_inc_flist = (compat_flags & CF_SAFE_FLIST) || protocol_version >= 31;
691 need_messages_from_generator = 1;
692 if (compat_flags & CF_INPLACE_PARTIAL_DIR)
694 #ifdef CAN_SET_SYMLINK_TIMES
695 } else if (!am_sender) {
696 receiver_symlink_times = 1;
700 if (need_unsorted_flist && (!am_sender || inc_recurse))
701 unsort_ndx = ++file_extra_cnt;
703 if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
704 int rflags = FILTRULE_NO_PREFIXES | FILTRULE_DIRECTORY;
705 if (!am_sender || protocol_version >= 30)
706 rflags |= FILTRULE_PERISHABLE;
707 parse_filter_str(&filter_list, partial_dir, rule_template(rflags), 0);
712 if (protect_args && files_from) {
714 filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
716 filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
720 negotiate_the_strings(f_in, f_out);
724 checksum_seed = time(NULL) ^ (getpid() << 6);
725 write_int(f_out, checksum_seed);
727 checksum_seed = read_int(f_in);
730 parse_checksum_choice(1); /* Sets checksum_type & xfersum_type */
731 parse_compress_choice(1); /* Sets do_compression */
733 if (write_batch && !am_server)
734 write_batch_shell_file();