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