527201ace54e1a07e01bf0e5d0d3defcc8d4167f
[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 #include "itypes.h"
24
25 extern int am_server;
26 extern int am_sender;
27 extern int local_server;
28 extern int inplace;
29 extern int recurse;
30 extern int use_qsort;
31 extern int allow_inc_recurse;
32 extern int preallocate_files;
33 extern int append_mode;
34 extern int fuzzy_basis;
35 extern int read_batch;
36 extern int write_batch;
37 extern int delay_updates;
38 extern int checksum_seed;
39 extern int basis_dir_cnt;
40 extern int prune_empty_dirs;
41 extern int protocol_version;
42 extern int protect_args;
43 extern int preserve_uid;
44 extern int preserve_gid;
45 extern int preserve_atimes;
46 extern int preserve_acls;
47 extern int preserve_xattrs;
48 extern int xfer_flags_as_varint;
49 extern int need_messages_from_generator;
50 extern int delete_mode, delete_before, delete_during, delete_after;
51 extern int do_compression;
52 extern int do_compression_level;
53 extern char *shell_cmd;
54 extern char *partial_dir;
55 extern char *files_from;
56 extern char *filesfrom_host;
57 extern const char *checksum_choice;
58 extern const char *compress_choice;
59 extern filter_rule_list filter_list;
60 extern int need_unsorted_flist;
61 #ifdef ICONV_OPTION
62 extern iconv_t ic_send, ic_recv;
63 extern char *iconv_opt;
64 #endif
65 extern struct name_num_obj valid_checksums;
66
67 int remote_protocol = 0;
68 int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
69 int inc_recurse = 0;
70 int compat_flags = 0;
71 int use_safe_inc_flist = 0;
72 int want_xattr_optim = 0;
73 int proper_seed_order = 0;
74 int inplace_partial = 0;
75 int do_negotiated_strings = 0;
76
77 /* These index values are for the file-list's extra-attribute array. */
78 int pathname_ndx, depth_ndx, atimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
79
80 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
81 int sender_symlink_iconv = 0;   /* sender should convert symlink content */
82
83 #ifdef ICONV_OPTION
84 int filesfrom_convert = 0;
85 #endif
86
87 #define MAX_NSTR_STRLEN 256
88
89 struct name_num_obj valid_compressions = {
90         "compress", NULL, NULL, 0, 0, {
91 #ifdef SUPPORT_ZSTD
92                 { CPRES_ZSTD, "zstd", NULL },
93 #endif
94 #ifdef SUPPORT_LZ4
95                 { CPRES_LZ4, "lz4", NULL },
96 #endif
97                 { CPRES_ZLIBX, "zlibx", NULL },
98                 { CPRES_ZLIB, "zlib", NULL },
99                 { CPRES_NONE, "none", NULL },
100                 { 0, NULL, NULL }
101         }
102 };
103
104 #define CF_INC_RECURSE   (1<<0)
105 #define CF_SYMLINK_TIMES (1<<1)
106 #define CF_SYMLINK_ICONV (1<<2)
107 #define CF_SAFE_FLIST    (1<<3)
108 #define CF_AVOID_XATTR_OPTIM (1<<4)
109 #define CF_CHKSUM_SEED_FIX (1<<5)
110 #define CF_INPLACE_PARTIAL_DIR (1<<6)
111 #define CF_VARINT_FLIST_FLAGS (1<<7)
112
113 static const char *client_info;
114
115 /* The server makes sure that if either side only supports a pre-release
116  * version of a protocol, that both sides must speak a compatible version
117  * of that protocol for it to be advertised as available. */
118 static void check_sub_protocol(void)
119 {
120         char *dot;
121         int their_protocol, their_sub;
122 #if SUBPROTOCOL_VERSION != 0
123         int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
124 #else
125         int our_sub = 0;
126 #endif
127
128         /* client_info starts with VER.SUB string if client is a pre-release. */
129         if (!(their_protocol = atoi(client_info))
130          || !(dot = strchr(client_info, '.'))
131          || !(their_sub = atoi(dot+1))) {
132 #if SUBPROTOCOL_VERSION != 0
133                 if (our_sub)
134                         protocol_version--;
135 #endif
136                 return;
137         }
138
139         if (their_protocol < protocol_version) {
140                 if (their_sub)
141                         protocol_version = their_protocol - 1;
142                 return;
143         }
144
145         if (their_protocol > protocol_version)
146                 their_sub = 0; /* 0 == final version of older protocol */
147         if (their_sub != our_sub)
148                 protocol_version--;
149 }
150
151 void set_allow_inc_recurse(void)
152 {
153         client_info = shell_cmd ? shell_cmd : "";
154
155         if (!recurse || use_qsort)
156                 allow_inc_recurse = 0;
157         else if (!am_sender
158          && (delete_before || delete_after
159           || delay_updates || prune_empty_dirs))
160                 allow_inc_recurse = 0;
161         else if (am_server && !local_server
162          && (strchr(client_info, 'i') == NULL))
163                 allow_inc_recurse = 0;
164 }
165
166 void parse_compress_choice(int final_call)
167 {
168         if (valid_compressions.negotiated_name)
169                 do_compression = valid_compressions.negotiated_num;
170         else if (compress_choice) {
171                 struct name_num_item *nni = get_nni_by_name(&valid_compressions, compress_choice, -1);
172                 if (!nni) {
173                         rprintf(FERROR, "unknown compress name: %s\n", compress_choice);
174                         exit_cleanup(RERR_UNSUPPORTED);
175                 }
176                 do_compression = nni->num;
177                 if (am_server)
178                         validate_choice_vs_env(NSTR_COMPRESS, do_compression, -1);
179         } else if (do_compression)
180                 do_compression = CPRES_ZLIB;
181         else
182                 do_compression = CPRES_NONE;
183
184         if (do_compression != CPRES_NONE && final_call)
185                 init_compression_level(); /* There's a chance this might turn compression off! */
186
187         if (do_compression == CPRES_NONE)
188                 compress_choice = NULL;
189
190         /* Snag the compression name for both write_batch's option output & the following debug output. */
191         if (valid_compressions.negotiated_name)
192                 compress_choice = valid_compressions.negotiated_name;
193         else if (compress_choice == NULL) {
194                 struct name_num_item *nni = get_nni_by_num(&valid_compressions, do_compression);
195                 compress_choice = nni ? nni->name : "UNKNOWN";
196         }
197
198         if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)
199          && (do_compression != CPRES_NONE || do_compression_level != CLVL_NOT_SPECIFIED)) {
200                 rprintf(FINFO, "%s%s compress: %s (level %d)\n",
201                         am_server ? "Server" : "Client",
202                         valid_compressions.negotiated_name ? " negotiated" : "",
203                         compress_choice, do_compression_level);
204         }
205 }
206
207 struct name_num_item *get_nni_by_name(struct name_num_obj *nno, const char *name, int len)
208 {
209         struct name_num_item *nni;
210
211         if (len < 0)
212                 len = strlen(name);
213
214         for (nni = nno->list; nni->name; nni++) {
215                 if (strncasecmp(name, nni->name, len) == 0 && nni->name[len] == '\0')
216                         return nni;
217         }
218
219         return NULL;
220 }
221
222 struct name_num_item *get_nni_by_num(struct name_num_obj *nno, int num)
223 {
224         struct name_num_item *nni;
225
226         for (nni = nno->list; nni->name; nni++) {
227                 if (num == nni->num)
228                         return nni;
229         }
230
231         return NULL;
232 }
233
234 static void init_nno_saw(struct name_num_obj *nno, int val)
235 {
236         struct name_num_item *nni;
237         int cnt;
238
239         if (!nno->saw_len) {
240                 for (nni = nno->list; nni->name; nni++) {
241                         if (nni->num >= nno->saw_len)
242                                 nno->saw_len = nni->num + 1;
243                 }
244         }
245
246         if (!nno->saw) {
247                 nno->saw = new_array0(uchar, nno->saw_len);
248
249                 /* We'll take this opportunity to make sure that the main_name values are set right. */
250                 for (cnt = 1, nni = nno->list; nni->name; nni++, cnt++) {
251                         if (nno->saw[nni->num])
252                                 nni->main_name = nno->list[nno->saw[nni->num]-1].name;
253                         else
254                                 nno->saw[nni->num] = cnt;
255                 }
256         }
257
258         memset(nno->saw, val, nno->saw_len);
259 }
260
261 /* Simplify the user-provided string so that it contains valid names without any duplicates.
262  * It also sets the "saw" flags to a 1-relative count of which name was seen first. */
263 static int parse_nni_str(struct name_num_obj *nno, const char *from, char *tobuf, int tobuf_len)
264 {
265         char *to = tobuf, *tok = NULL;
266         int saw_tok = 0, cnt = 0;
267
268         while (1) {
269                 int at_space = isSpace(from);
270                 char ch = *from++;
271                 if (ch == '&')
272                         ch = '\0';
273                 if (!ch || at_space) {
274                         if (tok) {
275                                 struct name_num_item *nni = get_nni_by_name(nno, tok, to - tok);
276                                 if (nni && !nno->saw[nni->num]) {
277                                         nno->saw[nni->num] = ++cnt;
278                                         if (nni->main_name) {
279                                                 to = tok + strlcpy(tok, nni->main_name, tobuf_len - (tok - tobuf));
280                                                 if (to - tobuf >= tobuf_len) {
281                                                         to = tok - 1;
282                                                         break;
283                                                 }
284                                         }
285                                 } else
286                                         to = tok - (tok != tobuf);
287                                 saw_tok = 1;
288                                 tok = NULL;
289                         }
290                         if (!ch)
291                                 break;
292                         continue;
293                 }
294                 if (!tok) {
295                         if (to != tobuf)
296                                 *to++ = ' ';
297                         tok = to;
298                 }
299                 if (to - tobuf >= tobuf_len - 1) {
300                         to = tok - (tok != tobuf);
301                         break;
302                 }
303                 *to++ = ch;
304         }
305         *to = '\0';
306
307         if (saw_tok && to == tobuf)
308                 return strlcpy(tobuf, "INVALID", MAX_NSTR_STRLEN);
309
310         return to - tobuf;
311 }
312
313 /* This routine is always called with a tmpbuf of MAX_NSTR_STRLEN length, but the
314  * buffer may be pre-populated with a "len" length string to use OR a len of -1
315  * to tell us to read a string from the fd. */
316 static void recv_negotiate_str(int f_in, struct name_num_obj *nno, char *tmpbuf, int len)
317 {
318         struct name_num_item *ret = NULL;
319
320         if (len < 0)
321                 len = read_vstring(f_in, tmpbuf, MAX_NSTR_STRLEN);
322
323         if (DEBUG_GTE(NSTR, am_server ? 3 : 2)) {
324                 if (am_server)
325                         rprintf(FINFO, "Client %s list (on server): %s\n", nno->type, tmpbuf);
326                 else
327                         rprintf(FINFO, "Server %s list (on client): %s\n", nno->type, tmpbuf);
328         }
329
330         if (len > 0) {
331                 struct name_num_item *nni;
332                 int best = nno->saw_len; /* We want best == 1 from the client list, so start with a big number. */
333                 char *space, *tok = tmpbuf;
334                 while (tok) {
335                         while (*tok == ' ') tok++; /* Should be unneeded... */
336                         if (!*tok)
337                                 break;
338                         if ((space = strchr(tok, ' ')) != NULL)
339                                 *space = '\0';
340                         nni = get_nni_by_name(nno, tok, -1);
341                         if (space) {
342                                 *space = ' ';
343                                 tok = space + 1;
344                         } else
345                                 tok = NULL;
346                         if (!nni || !nno->saw[nni->num] || best <= nno->saw[nni->num])
347                                 continue;
348                         ret = nni;
349                         best = nno->saw[nni->num];
350                         if (best == 1 || am_server) /* The server side stops at the first acceptable client choice */
351                                 break;
352                 }
353                 if (ret) {
354                         free(nno->saw);
355                         nno->saw = NULL;
356                         nno->negotiated_name = ret->main_name ? ret->main_name : ret->name;
357                         nno->negotiated_num = ret->num;
358                         return;
359                 }
360         }
361
362         if (!am_server || !do_negotiated_strings) {
363                 char *cp = tmpbuf;
364                 int j;
365                 rprintf(FERROR, "Failed to negotiate a %s choice.\n", nno->type);
366                 rprintf(FERROR, "%s list: %s\n", am_server ? "Client" : "Server", tmpbuf);
367                 /* Recreate our original list from the saw values. This can't overflow our huge
368                  * buffer because we don't have enough valid entries to get anywhere close. */
369                 for (j = 1, *cp = '\0'; j <= nno->saw_len; j++) {
370                         struct name_num_item *nni;
371                         for (nni = nno->list; nni->name; nni++) {
372                                 if (nno->saw[nni->num] == j) {
373                                         *cp++ = ' ';
374                                         cp += strlcpy(cp, nni->name, MAX_NSTR_STRLEN - (cp - tmpbuf));
375                                         break;
376                                 }
377                         }
378                 }
379                 if (!*tmpbuf)
380                         strlcpy(cp, " INVALID", MAX_NSTR_STRLEN);
381                 rprintf(FERROR, "%s list:%s\n", am_server ? "Server" : "Client", tmpbuf);
382         }
383
384         exit_cleanup(RERR_UNSUPPORTED);
385 }
386
387 static const char *getenv_nstr(int ntype)
388 {
389         const char *env_str = getenv(ntype == NSTR_COMPRESS ? "RSYNC_COMPRESS_LIST" : "RSYNC_CHECKSUM_LIST");
390
391         /* When writing a batch file, we always negotiate an old-style choice. */
392         if (write_batch) 
393                 env_str = ntype == NSTR_COMPRESS ? "zlib" : protocol_version >= 30 ? "md5" : "md4";
394
395         if (am_server && env_str) {
396                 char *cp = strchr(env_str, '&');
397                 if (cp)
398                         env_str = cp + 1;
399         }
400
401         return env_str;
402 }
403
404 void validate_choice_vs_env(int ntype, int num1, int num2)
405 {
406         struct name_num_obj *nno = ntype == NSTR_COMPRESS ? &valid_compressions : &valid_checksums;
407         const char *list_str = getenv_nstr(ntype);
408         char tmpbuf[MAX_NSTR_STRLEN];
409
410         if (!list_str)
411                 return;
412
413         while (isSpace(list_str)) list_str++;
414
415         if (!*list_str)
416                 return;
417
418         init_nno_saw(nno, 0);
419         parse_nni_str(nno, list_str, tmpbuf, MAX_NSTR_STRLEN);
420
421         if (ntype == NSTR_CHECKSUM) /* If "md4" is in the env list, all the old MD4 choices are OK too. */
422                 nno->saw[CSUM_MD4_ARCHAIC] = nno->saw[CSUM_MD4_BUSTED] = nno->saw[CSUM_MD4_OLD] = nno->saw[CSUM_MD4];
423
424         if (!nno->saw[num1] || (num2 >= 0 && !nno->saw[num2])) {
425                 rprintf(FERROR, "Your --%s-choice value (%s) was refused by the server.\n", 
426                         ntype == NSTR_COMPRESS ? "compress" : "checksum",
427                         ntype == NSTR_COMPRESS ? compress_choice : checksum_choice);
428                 exit_cleanup(RERR_UNSUPPORTED);
429         }
430
431         free(nno->saw);
432         nno->saw = NULL;
433 }
434
435 /* The saw buffer is initialized and used to store ordinal values from 1 to N
436  * for the order of the args in the array.  If dup_markup == '\0', duplicates
437  * are removed otherwise the char is prefixed to the duplicate term and, if it
438  * is an opening paren/bracket/brace, the matching closing char is suffixed.
439  * "none" is removed on the client side unless dup_markup != '\0'. */
440 int get_default_nno_list(struct name_num_obj *nno, char *to_buf, int to_buf_len, char dup_markup)
441 {
442         struct name_num_item *nni;
443         int len = 0, cnt = 0;
444         char delim = '\0', post_delim;
445
446         switch (dup_markup) {
447         case '(': post_delim = ')'; break;
448         case '[': post_delim = ']'; break;
449         case '{': post_delim = '}'; break;
450         default: post_delim = '\0'; break;
451         }
452
453         init_nno_saw(nno, 0);
454
455         for (nni = nno->list, len = 0; nni->name; nni++) {
456                 if (nni->main_name) {
457                         if (!dup_markup)
458                                 continue;
459                         delim = dup_markup;
460                 }
461                 if (nni->num == 0 && !am_server && !dup_markup)
462                         continue;
463                 if (len)
464                         to_buf[len++]= ' ';
465                 if (delim) {
466                         to_buf[len++]= delim;
467                         delim = post_delim;
468                 }
469                 len += strlcpy(to_buf+len, nni->name, to_buf_len - len);
470                 if (len >= to_buf_len - 3)
471                         exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE... */
472                 if (delim) {
473                         to_buf[len++]= delim;
474                         delim = '\0';
475                 }
476                 nno->saw[nni->num] = ++cnt;
477         }
478
479         return len;
480 }
481
482 static void send_negotiate_str(int f_out, struct name_num_obj *nno, int ntype)
483 {
484         char tmpbuf[MAX_NSTR_STRLEN];
485         const char *list_str = getenv_nstr(ntype);
486         int len;
487
488         if (list_str && *list_str) {
489                 init_nno_saw(nno, 0);
490                 len = parse_nni_str(nno, list_str, tmpbuf, MAX_NSTR_STRLEN);
491                 list_str = tmpbuf;
492         } else
493                 list_str = NULL;
494
495         if (!list_str || !*list_str)
496                 len = get_default_nno_list(nno, tmpbuf, MAX_NSTR_STRLEN, '\0');
497
498         if (DEBUG_GTE(NSTR, am_server ? 3 : 2)) {
499                 if (am_server)
500                         rprintf(FINFO, "Server %s list (on server): %s\n", nno->type, tmpbuf);
501                 else
502                         rprintf(FINFO, "Client %s list (on client): %s\n", nno->type, tmpbuf);
503         }
504
505         /* Each side sends their list of valid names to the other side and then both sides
506          * pick the first name in the client's list that is also in the server's list. */
507         if (do_negotiated_strings)
508                 write_vstring(f_out, tmpbuf, len);
509 }
510
511 static void negotiate_the_strings(int f_in, int f_out)
512 {
513         /* We send all the negotiation strings before we start to read them to help avoid a slow startup. */
514
515         if (!checksum_choice)
516                 send_negotiate_str(f_out, &valid_checksums, NSTR_CHECKSUM);
517
518         if (do_compression && !compress_choice)
519                 send_negotiate_str(f_out, &valid_compressions, NSTR_COMPRESS);
520
521         if (valid_checksums.saw) {
522                 char tmpbuf[MAX_NSTR_STRLEN];
523                 int len;
524                 if (do_negotiated_strings)
525                         len = -1;
526                 else
527                         len = strlcpy(tmpbuf, protocol_version >= 30 ? "md5" : "md4", MAX_NSTR_STRLEN);
528                 recv_negotiate_str(f_in, &valid_checksums, tmpbuf, len);
529         }
530
531         if (valid_compressions.saw) {
532                 char tmpbuf[MAX_NSTR_STRLEN];
533                 int len;
534                 if (do_negotiated_strings)
535                         len = -1;
536                 else
537                         len = strlcpy(tmpbuf, "zlib", MAX_NSTR_STRLEN);
538                 recv_negotiate_str(f_in, &valid_compressions, tmpbuf, len);
539         }
540
541         /* If the other side is too old to negotiate, the above steps just made sure that
542          * the env didn't disallow the old algorithm. Mark things as non-negotiated. */
543         if (!do_negotiated_strings)
544                 valid_checksums.negotiated_name = valid_compressions.negotiated_name = NULL;
545 }
546
547 void setup_protocol(int f_out,int f_in)
548 {
549         assert(file_extra_cnt == 0);
550         assert(EXTRA64_CNT == 2 || EXTRA64_CNT == 1);
551
552         /* All int64 values must be set first so that they are guaranteed to be
553          * aligned for direct int64-pointer memory access. */
554         if (preserve_atimes)
555                 atimes_ndx = (file_extra_cnt += EXTRA64_CNT);
556         if (am_sender) /* This is most likely in the in64 union as well. */
557                 pathname_ndx = (file_extra_cnt += PTR_EXTRA_CNT);
558         else
559                 depth_ndx = ++file_extra_cnt;
560         if (preserve_uid)
561                 uid_ndx = ++file_extra_cnt;
562         if (preserve_gid)
563                 gid_ndx = ++file_extra_cnt;
564         if (preserve_acls && !am_sender)
565                 acls_ndx = ++file_extra_cnt;
566         if (preserve_xattrs)
567                 xattrs_ndx = ++file_extra_cnt;
568
569         if (am_server)
570                 set_allow_inc_recurse();
571
572         if (remote_protocol == 0) {
573                 if (am_server && !local_server)
574                         check_sub_protocol();
575                 if (!read_batch)
576                         write_int(f_out, protocol_version);
577                 remote_protocol = read_int(f_in);
578                 if (protocol_version > remote_protocol)
579                         protocol_version = remote_protocol;
580         }
581         if (read_batch && remote_protocol > protocol_version) {
582                 rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
583                         remote_protocol, protocol_version);
584                 exit_cleanup(RERR_PROTOCOL);
585         }
586
587         if (DEBUG_GTE(PROTO, 1)) {
588                 rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
589                         am_server? "Server" : "Client", remote_protocol, protocol_version);
590         }
591         if (remote_protocol < MIN_PROTOCOL_VERSION
592          || remote_protocol > MAX_PROTOCOL_VERSION) {
593                 rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
594                 rprintf(FERROR,"(see the rsync man page for an explanation)\n");
595                 exit_cleanup(RERR_PROTOCOL);
596         }
597         if (remote_protocol < OLD_PROTOCOL_VERSION) {
598                 rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
599                         am_server? "Client" : "Server");
600         }
601         if (protocol_version < MIN_PROTOCOL_VERSION) {
602                 rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
603                         MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
604                 exit_cleanup(RERR_PROTOCOL);
605         }
606         if (protocol_version > PROTOCOL_VERSION) {
607                 rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
608                         PROTOCOL_VERSION, am_server? "Server" : "Client");
609                 exit_cleanup(RERR_PROTOCOL);
610         }
611         if (read_batch)
612                 check_batch_flags();
613
614 #ifndef SUPPORT_PREALLOCATION
615         if (preallocate_files && !am_sender) {
616                 rprintf(FERROR, "preallocation is not supported on this %s\n",
617                         am_server ? "Server" : "Client");
618                 exit_cleanup(RERR_SYNTAX);
619         }
620 #endif
621
622         if (protocol_version < 30) {
623                 if (append_mode == 1)
624                         append_mode = 2;
625                 if (preserve_acls && !local_server) {
626                         rprintf(FERROR,
627                                 "--acls requires protocol 30 or higher"
628                                 " (negotiated %d).\n",
629                                 protocol_version);
630                         exit_cleanup(RERR_PROTOCOL);
631                 }
632                 if (preserve_xattrs && !local_server) {
633                         rprintf(FERROR,
634                                 "--xattrs requires protocol 30 or higher"
635                                 " (negotiated %d).\n",
636                                 protocol_version);
637                         exit_cleanup(RERR_PROTOCOL);
638                 }
639         }
640
641         if (delete_mode && !(delete_before+delete_during+delete_after)) {
642                 if (protocol_version < 30)
643                         delete_before = 1;
644                 else
645                         delete_during = 1;
646         }
647
648         if (protocol_version < 29) {
649                 if (fuzzy_basis) {
650                         rprintf(FERROR,
651                                 "--fuzzy requires protocol 29 or higher"
652                                 " (negotiated %d).\n",
653                                 protocol_version);
654                         exit_cleanup(RERR_PROTOCOL);
655                 }
656
657                 if (basis_dir_cnt && inplace) {
658                         rprintf(FERROR,
659                                 "%s with --inplace requires protocol 29 or higher"
660                                 " (negotiated %d).\n",
661                                 alt_dest_opt(0), protocol_version);
662                         exit_cleanup(RERR_PROTOCOL);
663                 }
664
665                 if (basis_dir_cnt > 1) {
666                         rprintf(FERROR,
667                                 "Using more than one %s option requires protocol"
668                                 " 29 or higher (negotiated %d).\n",
669                                 alt_dest_opt(0), protocol_version);
670                         exit_cleanup(RERR_PROTOCOL);
671                 }
672
673                 if (prune_empty_dirs) {
674                         rprintf(FERROR,
675                                 "--prune-empty-dirs requires protocol 29 or higher"
676                                 " (negotiated %d).\n",
677                                 protocol_version);
678                         exit_cleanup(RERR_PROTOCOL);
679                 }
680         } else if (protocol_version >= 30) {
681                 if (am_server) {
682                         compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
683 #ifdef CAN_SET_SYMLINK_TIMES
684                         compat_flags |= CF_SYMLINK_TIMES;
685 #endif
686 #ifdef ICONV_OPTION
687                         compat_flags |= CF_SYMLINK_ICONV;
688 #endif
689                         if (local_server || strchr(client_info, 'f') != NULL)
690                                 compat_flags |= CF_SAFE_FLIST;
691                         if (local_server || strchr(client_info, 'x') != NULL)
692                                 compat_flags |= CF_AVOID_XATTR_OPTIM;
693                         if (local_server || strchr(client_info, 'C') != NULL)
694                                 compat_flags |= CF_CHKSUM_SEED_FIX;
695                         if (local_server || strchr(client_info, 'I') != NULL)
696                                 compat_flags |= CF_INPLACE_PARTIAL_DIR;
697                         if (local_server || strchr(client_info, 'v') != NULL) {
698                                 do_negotiated_strings = 1;
699                                 compat_flags |= CF_VARINT_FLIST_FLAGS;
700                         }
701                         if (strchr(client_info, 'V') != NULL) { /* Support a pre-release 'V' that got superseded */
702                                 if (!write_batch)
703                                         compat_flags |= CF_VARINT_FLIST_FLAGS;
704                                 write_byte(f_out, compat_flags);
705                         } else
706                                 write_varint(f_out, compat_flags);
707                 } else { /* read_varint() is compatible with the older write_byte() when the 0x80 bit isn't on. */
708                         compat_flags = read_varint(f_in);
709                         if  (compat_flags & CF_VARINT_FLIST_FLAGS)
710                                 do_negotiated_strings = 1;
711                 }
712                 /* The inc_recurse var MUST be set to 0 or 1. */
713                 inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
714                 want_xattr_optim = protocol_version >= 31 && !(compat_flags & CF_AVOID_XATTR_OPTIM);
715                 proper_seed_order = compat_flags & CF_CHKSUM_SEED_FIX ? 1 : 0;
716                 xfer_flags_as_varint = compat_flags & CF_VARINT_FLIST_FLAGS ? 1 : 0;
717                 if (am_sender) {
718                         receiver_symlink_times = am_server
719                             ? strchr(client_info, 'L') != NULL
720                             : !!(compat_flags & CF_SYMLINK_TIMES);
721                 }
722 #ifdef CAN_SET_SYMLINK_TIMES
723                 else
724                         receiver_symlink_times = 1;
725 #endif
726 #ifdef ICONV_OPTION
727                 sender_symlink_iconv = iconv_opt && (am_server
728                     ? local_server || strchr(client_info, 's') != NULL
729                     : !!(compat_flags & CF_SYMLINK_ICONV));
730 #endif
731                 if (inc_recurse && !allow_inc_recurse) {
732                         /* This should only be able to happen in a batch. */
733                         fprintf(stderr,
734                                 "Incompatible options specified for inc-recursive %s.\n",
735                                 read_batch ? "batch file" : "connection");
736                         exit_cleanup(RERR_SYNTAX);
737                 }
738                 use_safe_inc_flist = (compat_flags & CF_SAFE_FLIST) || protocol_version >= 31;
739                 need_messages_from_generator = 1;
740                 if (compat_flags & CF_INPLACE_PARTIAL_DIR)
741                         inplace_partial = 1;
742 #ifdef CAN_SET_SYMLINK_TIMES
743         } else if (!am_sender) {
744                 receiver_symlink_times = 1;
745 #endif
746         }
747
748         if (read_batch)
749                 do_negotiated_strings = 0;
750
751         if (need_unsorted_flist && (!am_sender || inc_recurse))
752                 unsort_ndx = ++file_extra_cnt;
753
754         if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
755                 int rflags = FILTRULE_NO_PREFIXES | FILTRULE_DIRECTORY;
756                 if (!am_sender || protocol_version >= 30)
757                         rflags |= FILTRULE_PERISHABLE;
758                 parse_filter_str(&filter_list, partial_dir, rule_template(rflags), 0);
759         }
760
761
762 #ifdef ICONV_OPTION
763         if (protect_args && files_from) {
764                 if (am_sender)
765                         filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
766                 else
767                         filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
768         }
769 #endif
770
771         negotiate_the_strings(f_in, f_out);
772
773         if (am_server) {
774                 if (!checksum_seed)
775                         checksum_seed = time(NULL) ^ (getpid() << 6);
776                 write_int(f_out, checksum_seed);
777         } else {
778                 checksum_seed = read_int(f_in);
779         }
780
781         parse_checksum_choice(1); /* Sets checksum_type & xfersum_type */
782         parse_compress_choice(1); /* Sets do_compression */
783
784         if (write_batch && !am_server)
785                 write_batch_shell_file();
786
787         init_flist();
788 }