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 delay_updates;
45 extern int checksum_seed;
46 extern int basis_dir_cnt;
47 extern int prune_empty_dirs;
48 extern int protocol_version;
49 extern int protect_args;
50 extern int preserve_uid;
51 extern int preserve_gid;
52 extern int preserve_atimes;
53 extern int preserve_acls;
54 extern int preserve_xattrs;
55 extern int need_messages_from_generator;
56 extern int delete_mode, delete_before, delete_during, delete_after;
57 extern char *shell_cmd;
58 extern char *partial_dir;
59 extern char *dest_option;
60 extern char *files_from;
61 extern char *filesfrom_host;
62 extern filter_rule_list filter_list;
63 extern int need_unsorted_flist;
65 extern iconv_t ic_send, ic_recv;
66 extern char *iconv_opt;
69 /* These index values are for the file-list's extra-attribute array. */
70 int pathname_ndx, depth_ndx, atimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
72 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
73 int sender_symlink_iconv = 0; /* sender should convert symlink content */
76 int filesfrom_convert = 0;
79 #define CF_INC_RECURSE (1<<0)
80 #define CF_SYMLINK_TIMES (1<<1)
81 #define CF_SYMLINK_ICONV (1<<2)
82 #define CF_SAFE_FLIST (1<<3)
83 #define CF_AVOID_XATTR_OPTIM (1<<4)
84 #define CF_CHKSUM_SEED_FIX (1<<5)
85 #define CF_INPLACE_PARTIAL_DIR (1<<6)
87 static const char *client_info;
89 /* The server makes sure that if either side only supports a pre-release
90 * version of a protocol, that both sides must speak a compatible version
91 * of that protocol for it to be advertised as available. */
92 static void check_sub_protocol(void)
95 int their_protocol, their_sub;
96 #if SUBPROTOCOL_VERSION != 0
97 int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
102 /* client_info starts with VER.SUB string if client is a pre-release. */
103 if (!(their_protocol = atoi(client_info))
104 || !(dot = strchr(client_info, '.'))
105 || !(their_sub = atoi(dot+1))) {
106 #if SUBPROTOCOL_VERSION != 0
113 if (their_protocol < protocol_version) {
115 protocol_version = their_protocol - 1;
119 if (their_protocol > protocol_version)
120 their_sub = 0; /* 0 == final version of older protocol */
121 if (their_sub != our_sub)
125 void set_allow_inc_recurse(void)
127 client_info = shell_cmd ? shell_cmd : "";
129 if (!recurse || use_qsort)
130 allow_inc_recurse = 0;
132 && (delete_before || delete_after
133 || delay_updates || prune_empty_dirs))
134 allow_inc_recurse = 0;
135 else if (am_server && !local_server
136 && (strchr(client_info, 'i') == NULL))
137 allow_inc_recurse = 0;
140 void setup_protocol(int f_out,int f_in)
142 assert(file_extra_cnt == 0);
143 assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
145 /* All int64 values must be set first so that they are guaranteed to be
146 * aligned for direct int64-pointer memory access. */
148 atimes_ndx = (file_extra_cnt += EXTRA64_CNT);
149 if (am_sender) /* This is most likely in the in64 union as well. */
150 pathname_ndx = (file_extra_cnt += PTR_EXTRA_CNT);
152 depth_ndx = ++file_extra_cnt;
154 uid_ndx = ++file_extra_cnt;
156 gid_ndx = ++file_extra_cnt;
157 if (preserve_acls && !am_sender)
158 acls_ndx = ++file_extra_cnt;
160 xattrs_ndx = ++file_extra_cnt;
163 set_allow_inc_recurse();
165 if (remote_protocol == 0) {
166 if (am_server && !local_server)
167 check_sub_protocol();
169 write_int(f_out, protocol_version);
170 remote_protocol = read_int(f_in);
171 if (protocol_version > remote_protocol)
172 protocol_version = remote_protocol;
174 if (read_batch && remote_protocol > protocol_version) {
175 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
176 remote_protocol, protocol_version);
177 exit_cleanup(RERR_PROTOCOL);
180 if (DEBUG_GTE(PROTO, 1)) {
181 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
182 am_server? "Server" : "Client", remote_protocol, protocol_version);
184 if (remote_protocol < MIN_PROTOCOL_VERSION
185 || remote_protocol > MAX_PROTOCOL_VERSION) {
186 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
187 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
188 exit_cleanup(RERR_PROTOCOL);
190 if (remote_protocol < OLD_PROTOCOL_VERSION) {
191 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
192 am_server? "Client" : "Server");
194 if (protocol_version < MIN_PROTOCOL_VERSION) {
195 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
196 MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
197 exit_cleanup(RERR_PROTOCOL);
199 if (protocol_version > PROTOCOL_VERSION) {
200 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
201 PROTOCOL_VERSION, am_server? "Server" : "Client");
202 exit_cleanup(RERR_PROTOCOL);
207 #ifndef SUPPORT_PREALLOCATION
208 if (preallocate_files && !am_sender) {
209 rprintf(FERROR, "preallocation is not supported on this %s\n",
210 am_server ? "Server" : "Client");
211 exit_cleanup(RERR_SYNTAX);
215 if (protocol_version < 30) {
216 if (append_mode == 1)
218 if (preserve_acls && !local_server) {
220 "--acls requires protocol 30 or higher"
221 " (negotiated %d).\n",
223 exit_cleanup(RERR_PROTOCOL);
225 if (preserve_xattrs && !local_server) {
227 "--xattrs requires protocol 30 or higher"
228 " (negotiated %d).\n",
230 exit_cleanup(RERR_PROTOCOL);
234 if (delete_mode && !(delete_before+delete_during+delete_after)) {
235 if (protocol_version < 30)
241 if (protocol_version < 29) {
244 "--fuzzy requires protocol 29 or higher"
245 " (negotiated %d).\n",
247 exit_cleanup(RERR_PROTOCOL);
250 if (basis_dir_cnt && inplace) {
252 "%s with --inplace requires protocol 29 or higher"
253 " (negotiated %d).\n",
254 dest_option, protocol_version);
255 exit_cleanup(RERR_PROTOCOL);
258 if (basis_dir_cnt > 1) {
260 "Using more than one %s option requires protocol"
261 " 29 or higher (negotiated %d).\n",
262 dest_option, protocol_version);
263 exit_cleanup(RERR_PROTOCOL);
266 if (prune_empty_dirs) {
268 "--prune-empty-dirs requires protocol 29 or higher"
269 " (negotiated %d).\n",
271 exit_cleanup(RERR_PROTOCOL);
273 } else if (protocol_version >= 30) {
275 compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
276 #ifdef CAN_SET_SYMLINK_TIMES
277 compat_flags |= CF_SYMLINK_TIMES;
280 compat_flags |= CF_SYMLINK_ICONV;
282 if (local_server || strchr(client_info, 'f') != NULL)
283 compat_flags |= CF_SAFE_FLIST;
284 if (local_server || strchr(client_info, 'x') != NULL)
285 compat_flags |= CF_AVOID_XATTR_OPTIM;
286 if (local_server || strchr(client_info, 'C') != NULL)
287 compat_flags |= CF_CHKSUM_SEED_FIX;
288 if (local_server || strchr(client_info, 'I') != NULL)
289 compat_flags |= CF_INPLACE_PARTIAL_DIR;
290 write_byte(f_out, compat_flags);
292 compat_flags = read_byte(f_in);
293 /* The inc_recurse var MUST be set to 0 or 1. */
294 inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
295 want_xattr_optim = protocol_version >= 31 && !(compat_flags & CF_AVOID_XATTR_OPTIM);
296 proper_seed_order = compat_flags & CF_CHKSUM_SEED_FIX ? 1 : 0;
298 receiver_symlink_times = am_server
299 ? strchr(client_info, 'L') != NULL
300 : !!(compat_flags & CF_SYMLINK_TIMES);
302 #ifdef CAN_SET_SYMLINK_TIMES
304 receiver_symlink_times = 1;
307 sender_symlink_iconv = iconv_opt && (am_server
308 ? local_server || strchr(client_info, 's') != NULL
309 : !!(compat_flags & CF_SYMLINK_ICONV));
311 if (inc_recurse && !allow_inc_recurse) {
312 /* This should only be able to happen in a batch. */
314 "Incompatible options specified for inc-recursive %s.\n",
315 read_batch ? "batch file" : "connection");
316 exit_cleanup(RERR_SYNTAX);
318 use_safe_inc_flist = (compat_flags & CF_SAFE_FLIST) || protocol_version >= 31;
319 need_messages_from_generator = 1;
320 if (compat_flags & CF_INPLACE_PARTIAL_DIR)
322 #ifdef CAN_SET_SYMLINK_TIMES
323 } else if (!am_sender) {
324 receiver_symlink_times = 1;
328 if (need_unsorted_flist && (!am_sender || inc_recurse))
329 unsort_ndx = ++file_extra_cnt;
331 if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
332 int rflags = FILTRULE_NO_PREFIXES | FILTRULE_DIRECTORY;
333 if (!am_sender || protocol_version >= 30)
334 rflags |= FILTRULE_PERISHABLE;
335 parse_filter_str(&filter_list, partial_dir, rule_template(rflags), 0);
340 if (protect_args && files_from) {
342 filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
344 filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
350 checksum_seed = time(NULL) ^ (getpid() << 6);
351 write_int(f_out, checksum_seed);
353 checksum_seed = read_int(f_in);