Checksum negotiation & more bits for compat_flags
[rsync.git] / compat.c
1 /*
2  * Compatibility routines for older rsync protocol versions.
3  *
4  * Copyright (C) Andrew Tridgell 1996
5  * Copyright (C) Paul Mackerras 1996
6  * Copyright (C) 2004-2020 Wayne Davison
7  *
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.
12  *
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.
17  *
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.
20  */
21
22 #include "rsync.h"
23
24 int remote_protocol = 0;
25 int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
26 int inc_recurse = 0;
27 int compat_flags = 0;
28 int use_safe_inc_flist = 0;
29 int want_xattr_optim = 0;
30 int proper_seed_order = 0;
31 int inplace_partial = 0;
32
33 extern int am_server;
34 extern int am_sender;
35 extern int local_server;
36 extern int inplace;
37 extern int recurse;
38 extern int use_qsort;
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;
67 #ifdef ICONV_OPTION
68 extern iconv_t ic_send, ic_recv;
69 extern char *iconv_opt;
70 #endif
71 extern const char *negotiated_csum_name;
72
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;
75
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 */
78
79 #ifdef ICONV_OPTION
80 int filesfrom_convert = 0;
81 #endif
82
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)
91
92 static const char *client_info;
93
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)
98 {
99         char *dot;
100         int their_protocol, their_sub;
101 #if SUBPROTOCOL_VERSION != 0
102         int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
103 #else
104         int our_sub = 0;
105 #endif
106
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
112                 if (our_sub)
113                         protocol_version--;
114 #endif
115                 return;
116         }
117
118         if (their_protocol < protocol_version) {
119                 if (their_sub)
120                         protocol_version = their_protocol - 1;
121                 return;
122         }
123
124         if (their_protocol > protocol_version)
125                 their_sub = 0; /* 0 == final version of older protocol */
126         if (their_sub != our_sub)
127                 protocol_version--;
128 }
129
130 void set_allow_inc_recurse(void)
131 {
132         client_info = shell_cmd ? shell_cmd : "";
133
134         if (!recurse || use_qsort)
135                 allow_inc_recurse = 0;
136         else if (!am_sender
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;
143 }
144
145 void setup_protocol(int f_out,int f_in)
146 {
147         int csum_exchange = 0;
148
149         assert(file_extra_cnt == 0);
150         assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
151
152         /* All int64 values must be set first so that they are guaranteed to be
153          * aligned for direct int64-pointer memory access. */
154         if (preserve_atimes)
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);
158         else
159                 depth_ndx = ++file_extra_cnt;
160         if (preserve_uid)
161                 uid_ndx = ++file_extra_cnt;
162         if (preserve_gid)
163                 gid_ndx = ++file_extra_cnt;
164         if (preserve_acls && !am_sender)
165                 acls_ndx = ++file_extra_cnt;
166         if (preserve_xattrs)
167                 xattrs_ndx = ++file_extra_cnt;
168
169         if (am_server)
170                 set_allow_inc_recurse();
171
172         if (remote_protocol == 0) {
173                 if (am_server && !local_server)
174                         check_sub_protocol();
175                 if (!read_batch)
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;
180         }
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);
185         }
186
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);
190         }
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);
196         }
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");
200         }
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);
205         }
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);
210         }
211         if (read_batch)
212                 check_batch_flags();
213
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);
219         }
220 #endif
221
222         if (protocol_version < 30) {
223                 if (append_mode == 1)
224                         append_mode = 2;
225                 if (preserve_acls && !local_server) {
226                         rprintf(FERROR,
227                             "--acls requires protocol 30 or higher"
228                             " (negotiated %d).\n",
229                             protocol_version);
230                         exit_cleanup(RERR_PROTOCOL);
231                 }
232                 if (preserve_xattrs && !local_server) {
233                         rprintf(FERROR,
234                             "--xattrs requires protocol 30 or higher"
235                             " (negotiated %d).\n",
236                             protocol_version);
237                         exit_cleanup(RERR_PROTOCOL);
238                 }
239         }
240
241         if (delete_mode && !(delete_before+delete_during+delete_after)) {
242                 if (protocol_version < 30)
243                         delete_before = 1;
244                 else
245                         delete_during = 1;
246         }
247
248         if (protocol_version < 29) {
249                 if (fuzzy_basis) {
250                         rprintf(FERROR,
251                             "--fuzzy requires protocol 29 or higher"
252                             " (negotiated %d).\n",
253                             protocol_version);
254                         exit_cleanup(RERR_PROTOCOL);
255                 }
256
257                 if (basis_dir_cnt && inplace) {
258                         rprintf(FERROR,
259                             "%s with --inplace requires protocol 29 or higher"
260                             " (negotiated %d).\n",
261                             dest_option, protocol_version);
262                         exit_cleanup(RERR_PROTOCOL);
263                 }
264
265                 if (basis_dir_cnt > 1) {
266                         rprintf(FERROR,
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);
271                 }
272
273                 if (prune_empty_dirs) {
274                         rprintf(FERROR,
275                             "--prune-empty-dirs requires protocol 29 or higher"
276                             " (negotiated %d).\n",
277                             protocol_version);
278                         exit_cleanup(RERR_PROTOCOL);
279                 }
280         } else if (protocol_version >= 30) {
281                 if (am_server) {
282                         compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
283 #ifdef CAN_SET_SYMLINK_TIMES
284                         compat_flags |= CF_SYMLINK_TIMES;
285 #endif
286 #ifdef ICONV_OPTION
287                         compat_flags |= CF_SYMLINK_ICONV;
288 #endif
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) {
299                                         csum_exchange = 1;
300                                         compat_flags |= CF_VARINT_FLIST_FLAGS;
301                                 }
302                         }
303                         if (strchr(client_info, 'V') != NULL) { /* Support a pre-release 'V' that got superseded */
304                                 if (!write_batch)
305                                         compat_flags |= CF_VARINT_FLIST_FLAGS;
306                                 write_byte(f_out, compat_flags);
307                         } else
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)
312                                 csum_exchange = 1;
313                 }
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;
319                 if (am_sender) {
320                         receiver_symlink_times = am_server
321                             ? strchr(client_info, 'L') != NULL
322                             : !!(compat_flags & CF_SYMLINK_TIMES);
323                 }
324 #ifdef CAN_SET_SYMLINK_TIMES
325                 else
326                         receiver_symlink_times = 1;
327 #endif
328 #ifdef ICONV_OPTION
329                 sender_symlink_iconv = iconv_opt && (am_server
330                     ? local_server || strchr(client_info, 's') != NULL
331                     : !!(compat_flags & CF_SYMLINK_ICONV));
332 #endif
333                 if (inc_recurse && !allow_inc_recurse) {
334                         /* This should only be able to happen in a batch. */
335                         fprintf(stderr,
336                             "Incompatible options specified for inc-recursive %s.\n",
337                             read_batch ? "batch file" : "connection");
338                         exit_cleanup(RERR_SYNTAX);
339                 }
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)
343                         inplace_partial = 1;
344 #ifdef CAN_SET_SYMLINK_TIMES
345         } else if (!am_sender) {
346                 receiver_symlink_times = 1;
347 #endif
348         }
349
350         if (need_unsorted_flist && (!am_sender || inc_recurse))
351                 unsort_ndx = ++file_extra_cnt;
352
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);
358         }
359
360
361 #ifdef ICONV_OPTION
362         if (protect_args && files_from) {
363                 if (am_sender)
364                         filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
365                 else
366                         filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
367         }
368 #endif
369
370         if (am_server) {
371                 if (!checksum_seed)
372                         checksum_seed = time(NULL) ^ (getpid() << 6);
373                 write_int(f_out, checksum_seed);
374         } else {
375                 checksum_seed = read_int(f_in);
376         }
377
378         if (!checksum_choice) {
379                 const char *rcl = getenv("RSYNC_CHECKSUM_LIST");
380                 if (csum_exchange)
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);
385                 }
386         }
387
388         init_flist();
389 }
390
391 void maybe_write_checksum(int batch_fd)
392 {
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));
396 }