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.
24 int remote_protocol = 0;
25 int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
28 int use_safe_inc_flist = 0;
29 int want_xattr_optim = 0;
30 int proper_seed_order = 0;
31 int inplace_partial = 0;
35 extern int local_server;
39 extern int allow_inc_recurse;
40 extern int preallocate_files;
41 extern int append_mode;
42 extern int fuzzy_basis;
43 extern int read_batch;
44 extern int write_batch;
45 extern int delay_updates;
46 extern int checksum_seed;
47 extern int basis_dir_cnt;
48 extern int prune_empty_dirs;
49 extern int protocol_version;
50 extern int protect_args;
51 extern int preserve_uid;
52 extern int preserve_gid;
53 extern int preserve_atimes;
54 extern int preserve_acls;
55 extern int preserve_xattrs;
56 extern int xfer_flags_as_varint;
57 extern int need_messages_from_generator;
58 extern int delete_mode, delete_before, delete_during, delete_after;
59 extern char *shell_cmd;
60 extern char *partial_dir;
61 extern char *dest_option;
62 extern char *files_from;
63 extern char *filesfrom_host;
64 extern char *checksum_choice;
65 extern filter_rule_list filter_list;
66 extern int need_unsorted_flist;
68 extern iconv_t ic_send, ic_recv;
69 extern char *iconv_opt;
71 extern const char *negotiated_csum_name;
73 /* These index values are for the file-list's extra-attribute array. */
74 int pathname_ndx, depth_ndx, atimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
76 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
77 int sender_symlink_iconv = 0; /* sender should convert symlink content */
80 int filesfrom_convert = 0;
83 #define CF_INC_RECURSE (1<<0)
84 #define CF_SYMLINK_TIMES (1<<1)
85 #define CF_SYMLINK_ICONV (1<<2)
86 #define CF_SAFE_FLIST (1<<3)
87 #define CF_AVOID_XATTR_OPTIM (1<<4)
88 #define CF_CHKSUM_SEED_FIX (1<<5)
89 #define CF_INPLACE_PARTIAL_DIR (1<<6)
90 #define CF_VARINT_FLIST_FLAGS (1<<7)
92 static const char *client_info;
94 /* The server makes sure that if either side only supports a pre-release
95 * version of a protocol, that both sides must speak a compatible version
96 * of that protocol for it to be advertised as available. */
97 static void check_sub_protocol(void)
100 int their_protocol, their_sub;
101 #if SUBPROTOCOL_VERSION != 0
102 int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
107 /* client_info starts with VER.SUB string if client is a pre-release. */
108 if (!(their_protocol = atoi(client_info))
109 || !(dot = strchr(client_info, '.'))
110 || !(their_sub = atoi(dot+1))) {
111 #if SUBPROTOCOL_VERSION != 0
118 if (their_protocol < protocol_version) {
120 protocol_version = their_protocol - 1;
124 if (their_protocol > protocol_version)
125 their_sub = 0; /* 0 == final version of older protocol */
126 if (their_sub != our_sub)
130 void set_allow_inc_recurse(void)
132 client_info = shell_cmd ? shell_cmd : "";
134 if (!recurse || use_qsort)
135 allow_inc_recurse = 0;
137 && (delete_before || delete_after
138 || delay_updates || prune_empty_dirs))
139 allow_inc_recurse = 0;
140 else if (am_server && !local_server
141 && (strchr(client_info, 'i') == NULL))
142 allow_inc_recurse = 0;
145 void setup_protocol(int f_out,int f_in)
147 int csum_exchange = 0;
149 assert(file_extra_cnt == 0);
150 assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
152 /* All int64 values must be set first so that they are guaranteed to be
153 * aligned for direct int64-pointer memory access. */
155 atimes_ndx = (file_extra_cnt += EXTRA64_CNT);
156 if (am_sender) /* This is most likely in the in64 union as well. */
157 pathname_ndx = (file_extra_cnt += PTR_EXTRA_CNT);
159 depth_ndx = ++file_extra_cnt;
161 uid_ndx = ++file_extra_cnt;
163 gid_ndx = ++file_extra_cnt;
164 if (preserve_acls && !am_sender)
165 acls_ndx = ++file_extra_cnt;
167 xattrs_ndx = ++file_extra_cnt;
170 set_allow_inc_recurse();
172 if (remote_protocol == 0) {
173 if (am_server && !local_server)
174 check_sub_protocol();
176 write_int(f_out, protocol_version);
177 remote_protocol = read_int(f_in);
178 if (protocol_version > remote_protocol)
179 protocol_version = remote_protocol;
181 if (read_batch && remote_protocol > protocol_version) {
182 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
183 remote_protocol, protocol_version);
184 exit_cleanup(RERR_PROTOCOL);
187 if (DEBUG_GTE(PROTO, 1)) {
188 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
189 am_server? "Server" : "Client", remote_protocol, protocol_version);
191 if (remote_protocol < MIN_PROTOCOL_VERSION
192 || remote_protocol > MAX_PROTOCOL_VERSION) {
193 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
194 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
195 exit_cleanup(RERR_PROTOCOL);
197 if (remote_protocol < OLD_PROTOCOL_VERSION) {
198 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
199 am_server? "Client" : "Server");
201 if (protocol_version < MIN_PROTOCOL_VERSION) {
202 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
203 MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
204 exit_cleanup(RERR_PROTOCOL);
206 if (protocol_version > PROTOCOL_VERSION) {
207 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
208 PROTOCOL_VERSION, am_server? "Server" : "Client");
209 exit_cleanup(RERR_PROTOCOL);
214 #ifndef SUPPORT_PREALLOCATION
215 if (preallocate_files && !am_sender) {
216 rprintf(FERROR, "preallocation is not supported on this %s\n",
217 am_server ? "Server" : "Client");
218 exit_cleanup(RERR_SYNTAX);
222 if (protocol_version < 30) {
223 if (append_mode == 1)
225 if (preserve_acls && !local_server) {
227 "--acls requires protocol 30 or higher"
228 " (negotiated %d).\n",
230 exit_cleanup(RERR_PROTOCOL);
232 if (preserve_xattrs && !local_server) {
234 "--xattrs requires protocol 30 or higher"
235 " (negotiated %d).\n",
237 exit_cleanup(RERR_PROTOCOL);
241 if (delete_mode && !(delete_before+delete_during+delete_after)) {
242 if (protocol_version < 30)
248 if (protocol_version < 29) {
251 "--fuzzy requires protocol 29 or higher"
252 " (negotiated %d).\n",
254 exit_cleanup(RERR_PROTOCOL);
257 if (basis_dir_cnt && inplace) {
259 "%s with --inplace requires protocol 29 or higher"
260 " (negotiated %d).\n",
261 dest_option, protocol_version);
262 exit_cleanup(RERR_PROTOCOL);
265 if (basis_dir_cnt > 1) {
267 "Using more than one %s option requires protocol"
268 " 29 or higher (negotiated %d).\n",
269 dest_option, protocol_version);
270 exit_cleanup(RERR_PROTOCOL);
273 if (prune_empty_dirs) {
275 "--prune-empty-dirs requires protocol 29 or higher"
276 " (negotiated %d).\n",
278 exit_cleanup(RERR_PROTOCOL);
280 } else if (protocol_version >= 30) {
282 compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
283 #ifdef CAN_SET_SYMLINK_TIMES
284 compat_flags |= CF_SYMLINK_TIMES;
287 compat_flags |= CF_SYMLINK_ICONV;
289 if (local_server || strchr(client_info, 'f') != NULL)
290 compat_flags |= CF_SAFE_FLIST;
291 if (local_server || strchr(client_info, 'x') != NULL)
292 compat_flags |= CF_AVOID_XATTR_OPTIM;
293 if (local_server || strchr(client_info, 'C') != NULL)
294 compat_flags |= CF_CHKSUM_SEED_FIX;
295 if (local_server || strchr(client_info, 'I') != NULL)
296 compat_flags |= CF_INPLACE_PARTIAL_DIR;
297 if (local_server || strchr(client_info, 'v') != NULL) {
298 if (!write_batch || protocol_version >= 30) {
300 compat_flags |= CF_VARINT_FLIST_FLAGS;
303 if (strchr(client_info, 'V') != NULL) { /* Support a pre-release 'V' that got superseded */
305 compat_flags |= CF_VARINT_FLIST_FLAGS;
306 write_byte(f_out, compat_flags);
308 write_varint(f_out, compat_flags);
309 } else { /* read_varint() is compatible with the older write_byte() when the 0x80 bit isn't on. */
310 compat_flags = read_varint(f_in);
311 if (compat_flags & CF_VARINT_FLIST_FLAGS)
314 /* The inc_recurse var MUST be set to 0 or 1. */
315 inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
316 want_xattr_optim = protocol_version >= 31 && !(compat_flags & CF_AVOID_XATTR_OPTIM);
317 proper_seed_order = compat_flags & CF_CHKSUM_SEED_FIX ? 1 : 0;
318 xfer_flags_as_varint = compat_flags & CF_VARINT_FLIST_FLAGS ? 1 : 0;
320 receiver_symlink_times = am_server
321 ? strchr(client_info, 'L') != NULL
322 : !!(compat_flags & CF_SYMLINK_TIMES);
324 #ifdef CAN_SET_SYMLINK_TIMES
326 receiver_symlink_times = 1;
329 sender_symlink_iconv = iconv_opt && (am_server
330 ? local_server || strchr(client_info, 's') != NULL
331 : !!(compat_flags & CF_SYMLINK_ICONV));
333 if (inc_recurse && !allow_inc_recurse) {
334 /* This should only be able to happen in a batch. */
336 "Incompatible options specified for inc-recursive %s.\n",
337 read_batch ? "batch file" : "connection");
338 exit_cleanup(RERR_SYNTAX);
340 use_safe_inc_flist = (compat_flags & CF_SAFE_FLIST) || protocol_version >= 31;
341 need_messages_from_generator = 1;
342 if (compat_flags & CF_INPLACE_PARTIAL_DIR)
344 #ifdef CAN_SET_SYMLINK_TIMES
345 } else if (!am_sender) {
346 receiver_symlink_times = 1;
350 if (need_unsorted_flist && (!am_sender || inc_recurse))
351 unsort_ndx = ++file_extra_cnt;
353 if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
354 int rflags = FILTRULE_NO_PREFIXES | FILTRULE_DIRECTORY;
355 if (!am_sender || protocol_version >= 30)
356 rflags |= FILTRULE_PERISHABLE;
357 parse_filter_str(&filter_list, partial_dir, rule_template(rflags), 0);
362 if (protect_args && files_from) {
364 filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
366 filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
372 checksum_seed = time(NULL) ^ (getpid() << 6);
373 write_int(f_out, checksum_seed);
375 checksum_seed = read_int(f_in);
378 if (!checksum_choice) {
379 const char *rcl = getenv("RSYNC_CHECKSUM_LIST");
381 negotiate_checksum(f_in, f_out, rcl);
382 else if (!am_server && rcl && *rcl && strstr(rcl, "FAIL")) {
383 rprintf(FERROR, "Remote rsync is too old for checksum negotation\n");
384 exit_cleanup(RERR_UNSUPPORTED);
391 void maybe_write_checksum(int batch_fd)
393 assert(negotiated_csum_name != NULL);
394 if (compat_flags & CF_VARINT_FLIST_FLAGS)
395 write_vstring(batch_fd, negotiated_csum_name, strlen(negotiated_csum_name));