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