Debian Codegraph @

compat.c

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