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 xfersum_type;
51 extern int checksum_type;
52 extern int do_compression;
53 extern char *shell_cmd;
54 extern char *partial_dir;
55 extern char *dest_option;
56 extern char *files_from;
57 extern char *filesfrom_host;
58 extern char *checksum_choice;
59 extern char *compress_choice;
60 extern filter_rule_list filter_list;
61 extern int need_unsorted_flist;
63 extern iconv_t ic_send, ic_recv;
64 extern char *iconv_opt;
66 extern struct name_num_obj valid_checksums;
68 int remote_protocol = 0;
69 int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
72 int use_safe_inc_flist = 0;
73 int want_xattr_optim = 0;
74 int proper_seed_order = 0;
75 int inplace_partial = 0;
76 int do_negotiated_strings = 0;
78 /* These index values are for the file-list's extra-attribute array. */
79 int pathname_ndx, depth_ndx, atimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
81 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
82 int sender_symlink_iconv = 0; /* sender should convert symlink content */
85 int filesfrom_convert = 0;
88 #define MAX_NSTR_STRLEN 256
90 struct name_num_obj valid_compressions = {
91 "compress", NULL, NULL, 0, 0, {
92 { CPRES_ZLIBX, "zlibx", NULL },
93 { CPRES_ZLIB, "zlib", NULL },
94 { CPRES_NONE, "none", NULL },
99 #define CF_INC_RECURSE (1<<0)
100 #define CF_SYMLINK_TIMES (1<<1)
101 #define CF_SYMLINK_ICONV (1<<2)
102 #define CF_SAFE_FLIST (1<<3)
103 #define CF_AVOID_XATTR_OPTIM (1<<4)
104 #define CF_CHKSUM_SEED_FIX (1<<5)
105 #define CF_INPLACE_PARTIAL_DIR (1<<6)
106 #define CF_VARINT_FLIST_FLAGS (1<<7)
108 static const char *client_info;
110 /* The server makes sure that if either side only supports a pre-release
111 * version of a protocol, that both sides must speak a compatible version
112 * of that protocol for it to be advertised as available. */
113 static void check_sub_protocol(void)
116 int their_protocol, their_sub;
117 #if SUBPROTOCOL_VERSION != 0
118 int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
123 /* client_info starts with VER.SUB string if client is a pre-release. */
124 if (!(their_protocol = atoi(client_info))
125 || !(dot = strchr(client_info, '.'))
126 || !(their_sub = atoi(dot+1))) {
127 #if SUBPROTOCOL_VERSION != 0
134 if (their_protocol < protocol_version) {
136 protocol_version = their_protocol - 1;
140 if (their_protocol > protocol_version)
141 their_sub = 0; /* 0 == final version of older protocol */
142 if (their_sub != our_sub)
146 void set_allow_inc_recurse(void)
148 client_info = shell_cmd ? shell_cmd : "";
150 if (!recurse || use_qsort)
151 allow_inc_recurse = 0;
153 && (delete_before || delete_after
154 || delay_updates || prune_empty_dirs))
155 allow_inc_recurse = 0;
156 else if (am_server && !local_server
157 && (strchr(client_info, 'i') == NULL))
158 allow_inc_recurse = 0;
161 void parse_compress_choice(int final_call)
163 if (valid_compressions.negotiated_name)
164 do_compression = valid_compressions.negotiated_num;
165 else if (compress_choice) {
166 struct name_num_item *nni = get_nni_by_name(&valid_compressions, compress_choice, -1);
168 rprintf(FERROR, "unknown compress name: %s\n", compress_choice);
169 exit_cleanup(RERR_UNSUPPORTED);
171 do_compression = nni->num;
172 } else if (do_compression)
173 do_compression = CPRES_ZLIB;
175 do_compression = CPRES_NONE;
177 if (do_compression == CPRES_NONE)
178 compress_choice = NULL;
180 if (final_call && DEBUG_GTE(NSTR, am_server ? 2 : 1)) {
181 const char *c_s = am_server ? "Server" : "Client";
182 if (valid_compressions.negotiated_name)
183 rprintf(FINFO, "%s negotiated compress: %s\n", c_s, valid_compressions.negotiated_name);
185 struct name_num_item *nni = get_nni_by_num(&valid_compressions, do_compression);
186 rprintf(FINFO, "%s compress: %s\n", c_s, nni ? nni->name : "UNKNOWN");
191 struct name_num_item *get_nni_by_name(struct name_num_obj *nno, const char *name, int len)
193 struct name_num_item *nni;
198 for (nni = nno->list; nni->name; nni++) {
199 if (strncasecmp(name, nni->name, len) == 0 && nni->name[len] == '\0')
206 struct name_num_item *get_nni_by_num(struct name_num_obj *nno, int num)
208 struct name_num_item *nni;
210 for (nni = nno->list; nni->name; nni++) {
218 static void init_nno_saw(struct name_num_obj *nno, int val)
220 struct name_num_item *nni;
224 for (nni = nno->list; nni->name; nni++) {
225 if (nni->num >= nno->saw_len)
226 nno->saw_len = nni->num + 1;
231 if (!(nno->saw = new_array0(uchar, nno->saw_len)))
232 out_of_memory("init_nno_saw");
234 /* We'll take this opportunity to make sure that the main_name values are set right. */
235 for (cnt = 1, nni = nno->list; nni->name; nni++, cnt++) {
236 if (nno->saw[nni->num])
237 nni->main_name = nno->list[nno->saw[nni->num]-1].name;
239 nno->saw[nni->num] = cnt;
243 memset(nno->saw, val, nno->saw_len);
246 /* Simplify the user-provided string so that it contains valid names without any duplicates.
247 * It also sets the "saw" flags to a 1-relative count of which name was seen first. */
248 static int parse_nni_str(struct name_num_obj *nno, const char *from, char *tobuf, int tobuf_len)
250 char *to = tobuf, *tok = NULL;
254 if (*from == ' ' || !*from) {
256 struct name_num_item *nni = get_nni_by_name(nno, tok, to - tok);
257 if (nni && !nno->saw[nni->num]) {
258 nno->saw[nni->num] = ++cnt;
259 if (nni->main_name && *nni->main_name) {
260 to = tok + strlcpy(tok, nni->main_name, tobuf_len - (tok - tobuf));
261 if (to - tobuf >= tobuf_len) {
267 to = tok - (tok != tobuf);
279 if (to - tobuf >= tobuf_len - 1) {
280 to = tok - (tok != tobuf);
290 static void recv_negotiate_str(int f_in, struct name_num_obj *nno, char *tmpbuf, int len)
292 struct name_num_item *ret = NULL;
295 len = read_vstring(f_in, tmpbuf, MAX_NSTR_STRLEN);
297 if (DEBUG_GTE(NSTR, am_server ? 4 : 2)) {
299 rprintf(FINFO, "Client %s list (on server): %s\n", nno->type, tmpbuf);
301 rprintf(FINFO, "Server %s list (on client): %s\n", nno->type, tmpbuf);
305 int best = nno->saw_len; /* We want best == 1 from the client list, so start with a big number. */
308 init_nno_saw(nno, 1); /* Since we're parsing client names, anything we parse first is #1. */
309 for (tok = strtok(tmpbuf, " \t"); tok; tok = strtok(NULL, " \t")) {
310 struct name_num_item *nni = get_nni_by_name(nno, tok, -1);
311 if (!nni || !nno->saw[nni->num] || best <= nno->saw[nni->num])
314 best = nno->saw[nni->num];
321 nno->negotiated_name = ret->main_name ? ret->main_name : ret->name;
322 nno->negotiated_num = ret->num;
328 rprintf(FERROR, "Failed to negotiate a common %s\n", nno->type);
329 exit_cleanup(RERR_UNSUPPORTED);
332 static void send_negotiate_str(int f_out, struct name_num_obj *nno, const char *env_name)
334 char tmpbuf[MAX_NSTR_STRLEN];
335 struct name_num_item *nni;
336 const char *list_str = getenv(env_name);
337 int len, fail_if_empty = list_str && strstr(list_str, "FAIL");
339 if (!do_negotiated_strings) {
340 if (!am_server && fail_if_empty) {
341 rprintf(FERROR, "Remote rsync is too old for %s negotation\n", nno->type);
342 exit_cleanup(RERR_UNSUPPORTED);
347 init_nno_saw(nno, 0);
349 if (list_str && *list_str && (!am_server || local_server)) {
350 len = parse_nni_str(nno, list_str, tmpbuf, MAX_NSTR_STRLEN);
351 if (fail_if_empty && !len)
352 len = strlcpy(tmpbuf, "FAIL", MAX_NSTR_STRLEN);
357 if (!list_str || !*list_str) {
359 for (nni = nno->list, len = 0; nni->name; nni++) {
364 len += strlcpy(tmpbuf+len, nni->name, MAX_NSTR_STRLEN - len);
365 if (len >= (int)MAX_NSTR_STRLEN - 1)
366 exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE... */
367 nno->saw[nni->num] = ++cnt;
371 if (DEBUG_GTE(NSTR, am_server ? 4 : 2)) {
373 rprintf(FINFO, "Server %s list (on server): %s\n", nno->type, tmpbuf);
375 rprintf(FINFO, "Client %s list (on client): %s\n", nno->type, tmpbuf);
379 /* A local server doesn't bother to send/recv the strings, it just constructs
380 * and parses the same string on both sides. */
382 recv_negotiate_str(-1, nno, tmpbuf, len);
384 /* Each side sends their list of valid names to the other side and then both sides
385 * pick the first name in the client's list that is also in the server's list. */
386 write_vstring(f_out, tmpbuf, len);
390 static void negotiate_the_strings(int f_in, int f_out)
392 /* We send all the negotiation strings before we start to read them to help avoid a slow startup. */
394 if (!checksum_choice)
395 send_negotiate_str(f_out, &valid_checksums, "RSYNC_CHECKSUM_LIST");
397 if (do_compression && !compress_choice)
398 send_negotiate_str(f_out, &valid_compressions, "RSYNC_COMPRESS_LIST");
400 if (valid_checksums.saw) {
401 char tmpbuf[MAX_NSTR_STRLEN];
402 recv_negotiate_str(f_in, &valid_checksums, tmpbuf, -1);
404 if (valid_checksums.negotiated_name)
405 xfersum_type = checksum_type = valid_checksums.negotiated_num;
407 if (valid_compressions.saw) {
408 char tmpbuf[MAX_NSTR_STRLEN];
409 recv_negotiate_str(f_in, &valid_compressions, tmpbuf, -1);
412 if (valid_compressions.negotiated_name)
413 compress_type = valid_checksums.negotiated_num;
417 void setup_protocol(int f_out,int f_in)
419 assert(file_extra_cnt == 0);
420 assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
422 /* All int64 values must be set first so that they are guaranteed to be
423 * aligned for direct int64-pointer memory access. */
425 atimes_ndx = (file_extra_cnt += EXTRA64_CNT);
426 if (am_sender) /* This is most likely in the in64 union as well. */
427 pathname_ndx = (file_extra_cnt += PTR_EXTRA_CNT);
429 depth_ndx = ++file_extra_cnt;
431 uid_ndx = ++file_extra_cnt;
433 gid_ndx = ++file_extra_cnt;
434 if (preserve_acls && !am_sender)
435 acls_ndx = ++file_extra_cnt;
437 xattrs_ndx = ++file_extra_cnt;
440 set_allow_inc_recurse();
442 if (remote_protocol == 0) {
443 if (am_server && !local_server)
444 check_sub_protocol();
446 write_int(f_out, protocol_version);
447 remote_protocol = read_int(f_in);
448 if (protocol_version > remote_protocol)
449 protocol_version = remote_protocol;
451 if (read_batch && remote_protocol > protocol_version) {
452 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
453 remote_protocol, protocol_version);
454 exit_cleanup(RERR_PROTOCOL);
457 if (DEBUG_GTE(PROTO, 1)) {
458 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
459 am_server? "Server" : "Client", remote_protocol, protocol_version);
461 if (remote_protocol < MIN_PROTOCOL_VERSION
462 || remote_protocol > MAX_PROTOCOL_VERSION) {
463 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
464 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
465 exit_cleanup(RERR_PROTOCOL);
467 if (remote_protocol < OLD_PROTOCOL_VERSION) {
468 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
469 am_server? "Client" : "Server");
471 if (protocol_version < MIN_PROTOCOL_VERSION) {
472 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
473 MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
474 exit_cleanup(RERR_PROTOCOL);
476 if (protocol_version > PROTOCOL_VERSION) {
477 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
478 PROTOCOL_VERSION, am_server? "Server" : "Client");
479 exit_cleanup(RERR_PROTOCOL);
484 #ifndef SUPPORT_PREALLOCATION
485 if (preallocate_files && !am_sender) {
486 rprintf(FERROR, "preallocation is not supported on this %s\n",
487 am_server ? "Server" : "Client");
488 exit_cleanup(RERR_SYNTAX);
492 if (protocol_version < 30) {
493 if (append_mode == 1)
495 if (preserve_acls && !local_server) {
497 "--acls requires protocol 30 or higher"
498 " (negotiated %d).\n",
500 exit_cleanup(RERR_PROTOCOL);
502 if (preserve_xattrs && !local_server) {
504 "--xattrs requires protocol 30 or higher"
505 " (negotiated %d).\n",
507 exit_cleanup(RERR_PROTOCOL);
511 if (delete_mode && !(delete_before+delete_during+delete_after)) {
512 if (protocol_version < 30)
518 if (protocol_version < 29) {
521 "--fuzzy requires protocol 29 or higher"
522 " (negotiated %d).\n",
524 exit_cleanup(RERR_PROTOCOL);
527 if (basis_dir_cnt && inplace) {
529 "%s with --inplace requires protocol 29 or higher"
530 " (negotiated %d).\n",
531 dest_option, protocol_version);
532 exit_cleanup(RERR_PROTOCOL);
535 if (basis_dir_cnt > 1) {
537 "Using more than one %s option requires protocol"
538 " 29 or higher (negotiated %d).\n",
539 dest_option, protocol_version);
540 exit_cleanup(RERR_PROTOCOL);
543 if (prune_empty_dirs) {
545 "--prune-empty-dirs requires protocol 29 or higher"
546 " (negotiated %d).\n",
548 exit_cleanup(RERR_PROTOCOL);
550 } else if (protocol_version >= 30) {
552 compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
553 #ifdef CAN_SET_SYMLINK_TIMES
554 compat_flags |= CF_SYMLINK_TIMES;
557 compat_flags |= CF_SYMLINK_ICONV;
559 if (local_server || strchr(client_info, 'f') != NULL)
560 compat_flags |= CF_SAFE_FLIST;
561 if (local_server || strchr(client_info, 'x') != NULL)
562 compat_flags |= CF_AVOID_XATTR_OPTIM;
563 if (local_server || strchr(client_info, 'C') != NULL)
564 compat_flags |= CF_CHKSUM_SEED_FIX;
565 if (local_server || strchr(client_info, 'I') != NULL)
566 compat_flags |= CF_INPLACE_PARTIAL_DIR;
567 if (local_server || strchr(client_info, 'v') != NULL) {
568 if (!write_batch || protocol_version >= 30) {
569 do_negotiated_strings = 1;
570 compat_flags |= CF_VARINT_FLIST_FLAGS;
573 if (strchr(client_info, 'V') != NULL) { /* Support a pre-release 'V' that got superseded */
575 compat_flags |= CF_VARINT_FLIST_FLAGS;
576 write_byte(f_out, compat_flags);
578 write_varint(f_out, compat_flags);
579 } else { /* read_varint() is compatible with the older write_byte() when the 0x80 bit isn't on. */
580 compat_flags = read_varint(f_in);
581 if (compat_flags & CF_VARINT_FLIST_FLAGS)
582 do_negotiated_strings = 1;
584 /* The inc_recurse var MUST be set to 0 or 1. */
585 inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
586 want_xattr_optim = protocol_version >= 31 && !(compat_flags & CF_AVOID_XATTR_OPTIM);
587 proper_seed_order = compat_flags & CF_CHKSUM_SEED_FIX ? 1 : 0;
588 xfer_flags_as_varint = compat_flags & CF_VARINT_FLIST_FLAGS ? 1 : 0;
590 receiver_symlink_times = am_server
591 ? strchr(client_info, 'L') != NULL
592 : !!(compat_flags & CF_SYMLINK_TIMES);
594 #ifdef CAN_SET_SYMLINK_TIMES
596 receiver_symlink_times = 1;
599 sender_symlink_iconv = iconv_opt && (am_server
600 ? local_server || strchr(client_info, 's') != NULL
601 : !!(compat_flags & CF_SYMLINK_ICONV));
603 if (inc_recurse && !allow_inc_recurse) {
604 /* This should only be able to happen in a batch. */
606 "Incompatible options specified for inc-recursive %s.\n",
607 read_batch ? "batch file" : "connection");
608 exit_cleanup(RERR_SYNTAX);
610 use_safe_inc_flist = (compat_flags & CF_SAFE_FLIST) || protocol_version >= 31;
611 need_messages_from_generator = 1;
612 if (compat_flags & CF_INPLACE_PARTIAL_DIR)
614 #ifdef CAN_SET_SYMLINK_TIMES
615 } else if (!am_sender) {
616 receiver_symlink_times = 1;
620 if (need_unsorted_flist && (!am_sender || inc_recurse))
621 unsort_ndx = ++file_extra_cnt;
623 if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
624 int rflags = FILTRULE_NO_PREFIXES | FILTRULE_DIRECTORY;
625 if (!am_sender || protocol_version >= 30)
626 rflags |= FILTRULE_PERISHABLE;
627 parse_filter_str(&filter_list, partial_dir, rule_template(rflags), 0);
632 if (protect_args && files_from) {
634 filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
636 filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
640 negotiate_the_strings(f_in, f_out);
644 checksum_seed = time(NULL) ^ (getpid() << 6);
645 write_int(f_out, checksum_seed);
647 checksum_seed = read_int(f_in);
653 void maybe_write_negotiated_strings(int batch_fd)
655 if (valid_checksums.negotiated_name)
656 write_vstring(batch_fd, valid_checksums.negotiated_name, strlen(valid_checksums.negotiated_name));
658 if (valid_compressions.negotiated_name)
659 write_vstring(batch_fd, valid_compressions.negotiated_name, strlen(valid_compressions.negotiated_name));