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