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