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