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