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