A resumed partial-dir file is transferred in-place.
[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 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;
64 #ifdef ICONV_OPTION
65 extern iconv_t ic_send, ic_recv;
66 extern char *iconv_opt;
67 #endif
68
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;
71
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 */
74
75 #ifdef ICONV_OPTION
76 int filesfrom_convert = 0;
77 #endif
78
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)
86
87 static const char *client_info;
88
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)
93 {
94         char *dot;
95         int their_protocol, their_sub;
96 #if SUBPROTOCOL_VERSION != 0
97         int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
98 #else
99         int our_sub = 0;
100 #endif
101
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
107                 if (our_sub)
108                         protocol_version--;
109 #endif
110                 return;
111         }
112
113         if (their_protocol < protocol_version) {
114                 if (their_sub)
115                         protocol_version = their_protocol - 1;
116                 return;
117         }
118
119         if (their_protocol > protocol_version)
120                 their_sub = 0; /* 0 == final version of older protocol */
121         if (their_sub != our_sub)
122                 protocol_version--;
123 }
124
125 void set_allow_inc_recurse(void)
126 {
127         client_info = shell_cmd ? shell_cmd : "";
128
129         if (!recurse || use_qsort)
130                 allow_inc_recurse = 0;
131         else if (!am_sender
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;
138 }
139
140 void setup_protocol(int f_out,int f_in)
141 {
142         assert(file_extra_cnt == 0);
143         assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
144
145         /* All int64 values must be set first so that they are guaranteed to be
146          * aligned for direct int64-pointer memory access. */
147         if (preserve_atimes)
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);
151         else
152                 depth_ndx = ++file_extra_cnt;
153         if (preserve_uid)
154                 uid_ndx = ++file_extra_cnt;
155         if (preserve_gid)
156                 gid_ndx = ++file_extra_cnt;
157         if (preserve_acls && !am_sender)
158                 acls_ndx = ++file_extra_cnt;
159         if (preserve_xattrs)
160                 xattrs_ndx = ++file_extra_cnt;
161
162         if (am_server)
163                 set_allow_inc_recurse();
164
165         if (remote_protocol == 0) {
166                 if (am_server && !local_server)
167                         check_sub_protocol();
168                 if (!read_batch)
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;
173         }
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);
178         }
179
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);
183         }
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);
189         }
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");
193         }
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);
198         }
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);
203         }
204         if (read_batch)
205                 check_batch_flags();
206
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);
212         }
213 #endif
214
215         if (protocol_version < 30) {
216                 if (append_mode == 1)
217                         append_mode = 2;
218                 if (preserve_acls && !local_server) {
219                         rprintf(FERROR,
220                             "--acls requires protocol 30 or higher"
221                             " (negotiated %d).\n",
222                             protocol_version);
223                         exit_cleanup(RERR_PROTOCOL);
224                 }
225                 if (preserve_xattrs && !local_server) {
226                         rprintf(FERROR,
227                             "--xattrs requires protocol 30 or higher"
228                             " (negotiated %d).\n",
229                             protocol_version);
230                         exit_cleanup(RERR_PROTOCOL);
231                 }
232         }
233
234         if (delete_mode && !(delete_before+delete_during+delete_after)) {
235                 if (protocol_version < 30)
236                         delete_before = 1;
237                 else
238                         delete_during = 1;
239         }
240
241         if (protocol_version < 29) {
242                 if (fuzzy_basis) {
243                         rprintf(FERROR,
244                             "--fuzzy requires protocol 29 or higher"
245                             " (negotiated %d).\n",
246                             protocol_version);
247                         exit_cleanup(RERR_PROTOCOL);
248                 }
249
250                 if (basis_dir_cnt && inplace) {
251                         rprintf(FERROR,
252                             "%s with --inplace requires protocol 29 or higher"
253                             " (negotiated %d).\n",
254                             dest_option, protocol_version);
255                         exit_cleanup(RERR_PROTOCOL);
256                 }
257
258                 if (basis_dir_cnt > 1) {
259                         rprintf(FERROR,
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);
264                 }
265
266                 if (prune_empty_dirs) {
267                         rprintf(FERROR,
268                             "--prune-empty-dirs requires protocol 29 or higher"
269                             " (negotiated %d).\n",
270                             protocol_version);
271                         exit_cleanup(RERR_PROTOCOL);
272                 }
273         } else if (protocol_version >= 30) {
274                 if (am_server) {
275                         compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
276 #ifdef CAN_SET_SYMLINK_TIMES
277                         compat_flags |= CF_SYMLINK_TIMES;
278 #endif
279 #ifdef ICONV_OPTION
280                         compat_flags |= CF_SYMLINK_ICONV;
281 #endif
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);
291                 } else
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;
297                 if (am_sender) {
298                         receiver_symlink_times = am_server
299                             ? strchr(client_info, 'L') != NULL
300                             : !!(compat_flags & CF_SYMLINK_TIMES);
301                 }
302 #ifdef CAN_SET_SYMLINK_TIMES
303                 else
304                         receiver_symlink_times = 1;
305 #endif
306 #ifdef ICONV_OPTION
307                 sender_symlink_iconv = iconv_opt && (am_server
308                     ? local_server || strchr(client_info, 's') != NULL
309                     : !!(compat_flags & CF_SYMLINK_ICONV));
310 #endif
311                 if (inc_recurse && !allow_inc_recurse) {
312                         /* This should only be able to happen in a batch. */
313                         fprintf(stderr,
314                             "Incompatible options specified for inc-recursive %s.\n",
315                             read_batch ? "batch file" : "connection");
316                         exit_cleanup(RERR_SYNTAX);
317                 }
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)
321                         inplace_partial = 1;
322 #ifdef CAN_SET_SYMLINK_TIMES
323         } else if (!am_sender) {
324                 receiver_symlink_times = 1;
325 #endif
326         }
327
328         if (need_unsorted_flist && (!am_sender || inc_recurse))
329                 unsort_ndx = ++file_extra_cnt;
330
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);
336         }
337
338
339 #ifdef ICONV_OPTION
340         if (protect_args && files_from) {
341                 if (am_sender)
342                         filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
343                 else
344                         filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
345         }
346 #endif
347
348         if (am_server) {
349                 if (!checksum_seed)
350                         checksum_seed = time(NULL) ^ (getpid() << 6);
351                 write_int(f_out, checksum_seed);
352         } else {
353                 checksum_seed = read_int(f_in);
354         }
355
356         init_flist();
357 }