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