903 lines · 26.2 KB · C
RawDownload
/* * Compatibility routines for older rsync protocol versions. * * Copyright (C) Andrew Tridgell 1996 * Copyright (C) Paul Mackerras 1996 * Copyright (C) 2004-2022 Wayne Davison * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, visit the http://fsf.org website. */ #include "rsync.h"#include "itypes.h"#include "ifuncs.h" extern int am_server;extern int am_sender;extern int local_server;extern int inplace;extern int recurse;extern int use_qsort;extern int allow_inc_recurse;extern int preallocate_files;extern int append_mode;extern int fuzzy_basis;extern int read_batch;extern int write_batch;extern int delay_updates;extern int checksum_seed;extern int basis_dir_cnt;extern int prune_empty_dirs;extern int protocol_version;extern int protect_args;extern int preserve_uid;extern int preserve_gid;extern int preserve_atimes;extern int preserve_crtimes;extern int preserve_acls;extern int preserve_xattrs;extern int xfer_flags_as_varint;extern int need_messages_from_generator;extern int delete_mode, delete_before, delete_during, delete_after;extern int do_compression;extern int do_compression_level;extern int do_compression_threads;extern int saw_stderr_opt;extern int msgs2stderr;extern char *shell_cmd;extern char *partial_dir;extern char *files_from;extern char *filesfrom_host;extern const char *checksum_choice;extern const char *compress_choice;extern char *daemon_auth_choices;extern filter_rule_list filter_list;extern int need_unsorted_flist;#ifdef ICONV_OPTIONextern iconv_t ic_send, ic_recv;extern char *iconv_opt;#endifextern struct name_num_obj valid_checksums, valid_auth_checksums; extern struct name_num_item *xfer_sum_nni; int remote_protocol = 0;int file_extra_cnt = 0; /* count of file-list extras that everyone gets */int inc_recurse = 0;int compat_flags = 0;int use_safe_inc_flist = 0;int want_xattr_optim = 0;int proper_seed_order = 0;int inplace_partial = 0;int do_negotiated_strings = 0;int xmit_id0_names = 0; struct name_num_item *xattr_sum_nni;int xattr_sum_len = 0; /* These index values are for the file-list's extra-attribute array. */int pathname_ndx, depth_ndx, atimes_ndx, crtimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx; int receiver_symlink_times = 0; /* receiver can set the time on a symlink */int sender_symlink_iconv = 0;	/* sender should convert symlink content */ #ifdef ICONV_OPTIONint filesfrom_convert = 0;#endif #define MAX_NSTR_STRLEN 256 struct name_num_item valid_compressions_items[] = {#ifdef SUPPORT_ZSTD	{ CPRES_ZSTD, 0, "zstd", NULL },#endif#ifdef SUPPORT_LZ4	{ CPRES_LZ4, 0, "lz4", NULL },#endif	{ CPRES_ZLIBX, 0, "zlibx", NULL },	{ CPRES_ZLIB, 0, "zlib", NULL },	{ CPRES_NONE, 0, "none", NULL },	{ 0, 0, NULL, NULL }}; struct name_num_obj valid_compressions = {	"compress", NULL, 0, 0, valid_compressions_items}; #define CF_INC_RECURSE	 (1<<0)#define CF_SYMLINK_TIMES (1<<1)#define CF_SYMLINK_ICONV (1<<2)#define CF_SAFE_FLIST	 (1<<3)#define CF_AVOID_XATTR_OPTIM (1<<4)#define CF_CHKSUM_SEED_FIX (1<<5)#define CF_INPLACE_PARTIAL_DIR (1<<6)#define CF_VARINT_FLIST_FLAGS (1<<7)#define CF_ID0_NAMES (1<<8) static const char *client_info; /* The server makes sure that if either side only supports a pre-release * version of a protocol, that both sides must speak a compatible version * of that protocol for it to be advertised as available. */static void check_sub_protocol(void){	const char *dot;	int their_protocol, their_sub;	int our_sub = get_subprotocol_version(); 	/* client_info starts with VER.SUB string if client is a pre-release. */	if (!(their_protocol = atoi(client_info))	 || !(dot = strchr(client_info, '.'))	 || !(their_sub = atoi(dot+1))) {#if SUBPROTOCOL_VERSION != 0		if (our_sub)			protocol_version--;#endif		return;	} 	if (their_protocol < protocol_version) {		if (their_sub)			protocol_version = their_protocol - 1;		return;	} 	if (their_protocol > protocol_version)		their_sub = 0; /* 0 == final version of older protocol */	if (their_sub != our_sub)		protocol_version--;} void set_allow_inc_recurse(void){	if (!local_server)		client_info = shell_cmd ? shell_cmd : "";	else if (am_server) {		char buf[64];		maybe_add_e_option(buf, sizeof buf);		client_info = *buf ? strdup(buf+1) : ""; /* The +1 skips the leading "e". */	} 	if (!recurse || use_qsort)		allow_inc_recurse = 0;	else if (!am_sender	 && (delete_before || delete_after	  || delay_updates || prune_empty_dirs))		allow_inc_recurse = 0;	else if (am_server && strchr(client_info, 'i') == NULL)		allow_inc_recurse = 0;} void parse_compress_choice(int final_call){	if (valid_compressions.negotiated_nni)		do_compression = valid_compressions.negotiated_nni->num;	else if (compress_choice) {		struct name_num_item *nni = get_nni_by_name(&valid_compressions, compress_choice, -1);		if (!nni) {			rprintf(FERROR, "unknown compress name: %s\n", compress_choice);			exit_cleanup(RERR_UNSUPPORTED);		}		do_compression = nni->num;		if (am_server)			validate_choice_vs_env(NSTR_COMPRESS, do_compression, -1);	} else if (do_compression)		do_compression = CPRES_ZLIB;	else		do_compression = CPRES_NONE; 	if (do_compression != CPRES_NONE && final_call)		init_compression_level(); /* There's a chance this might turn compression off! */ 	if (do_compression == CPRES_NONE)		compress_choice = NULL; 	/* Snag the compression name for both write_batch's option output & the following debug output. */	if (valid_compressions.negotiated_nni)		compress_choice = valid_compressions.negotiated_nni->name;	else if (compress_choice == NULL) {		struct name_num_item *nni = get_nni_by_num(&valid_compressions, do_compression);		compress_choice = nni ? nni->name : "UNKNOWN";	} 	if (final_call && DEBUG_GTE(NSTR, am_server ? 3 : 1)	 && (do_compression != CPRES_NONE || do_compression_level != CLVL_NOT_SPECIFIED)) {		rprintf(FINFO, "%s%s compress: %s (level %d)\n",			am_server ? "Server" : "Client",			valid_compressions.negotiated_nni ? " negotiated" : "",			compress_choice, do_compression_level);	}} struct name_num_item *get_nni_by_name(struct name_num_obj *nno, const char *name, int len){	struct name_num_item *nni; 	if (len < 0)		len = strlen(name); 	for (nni = nno->list; nni->name; nni++) {		if (nni->num == CSUM_gone)			continue;		if (strncasecmp(name, nni->name, len) == 0 && nni->name[len] == '\0')			return nni;	} 	return NULL;} struct name_num_item *get_nni_by_num(struct name_num_obj *nno, int num){	struct name_num_item *nni; 	for (nni = nno->list; nni->name; nni++) {		if (num == nni->num)			return nni;	} 	return NULL;} static void init_nno_saw(struct name_num_obj *nno, int val){	struct name_num_item *nni;	int cnt; 	if (!nno->saw_len) {		for (nni = nno->list; nni->name; nni++) {			if (nni->num >= nno->saw_len)				nno->saw_len = nni->num + 1;		}	} 	if (!nno->saw) {		nno->saw = new_array0(uchar, nno->saw_len); 		/* We'll take this opportunity to set the main_nni values for duplicates. */		for (cnt = 1, nni = nno->list; nni->name; nni++, cnt++) {			if (nni->num == CSUM_gone)				continue;			if (nno->saw[nni->num])				nni->main_nni = &nno->list[nno->saw[nni->num]-1];			else				nno->saw[nni->num] = cnt;		}	} 	memset(nno->saw, val, nno->saw_len);} /* Simplify the user-provided string so that it contains valid names without any duplicates. * It also sets the "saw" flags to a 1-relative count of which name was seen first. */static int parse_nni_str(struct name_num_obj *nno, const char *from, char *tobuf, int tobuf_len){	char *to = tobuf, *tok = NULL;	int saw_tok = 0, cnt = 0; 	while (1) {		int at_space = isSpace(from);		char ch = *from++;		if (ch == '&')			ch = '\0';		if (!ch || at_space) {			if (tok) {				struct name_num_item *nni = get_nni_by_name(nno, tok, to - tok);				if (nni && !nno->saw[nni->num]) {					nno->saw[nni->num] = ++cnt;					if (nni->main_nni) {						to = tok + strlcpy(tok, nni->main_nni->name, tobuf_len - (tok - tobuf));						if (to - tobuf >= tobuf_len) {							to = tok - 1;							break;						}					}				} else					to = tok - (tok != tobuf);				saw_tok = 1;				tok = NULL;			}			if (!ch)				break;			continue;		}		if (!tok) {			if (to != tobuf)				*to++ = ' ';			tok = to;		}		if (to - tobuf >= tobuf_len - 1) {			to = tok - (tok != tobuf);			break;		}		*to++ = ch;	}	*to = '\0'; 	if (saw_tok && to == tobuf)		return strlcpy(tobuf, "INVALID", MAX_NSTR_STRLEN); 	return to - tobuf;} static int parse_negotiate_str(struct name_num_obj *nno, char *tmpbuf){	struct name_num_item *nni, *ret = NULL;	int best = nno->saw_len; /* We want best == 1 from the client list, so start with a big number. */	char *space, *tok = tmpbuf;	while (tok) {		while (*tok == ' ') tok++; /* Should be unneeded... */		if (!*tok)			break;		if ((space = strchr(tok, ' ')) != NULL)			*space = '\0';		nni = get_nni_by_name(nno, tok, -1);		if (space) {			*space = ' ';			tok = space + 1;		} else			tok = NULL;		if (!nni || !nno->saw[nni->num] || best <= nno->saw[nni->num])			continue;		ret = nni;		best = nno->saw[nni->num];		if (best == 1) /* Can't improve on our own #1 preference */			break;	}	if (ret) {		free(nno->saw);		nno->saw = NULL;		nno->negotiated_nni = ret->main_nni ? ret->main_nni : ret;		return 1;	}	return 0;} /* This routine is always called with a tmpbuf of MAX_NSTR_STRLEN length, but the * buffer may be pre-populated with a "len" length string to use OR a len of -1 * to tell us to read a string from the fd. */static void recv_negotiate_str(int f_in, struct name_num_obj *nno, char *tmpbuf, int len){	if (len < 0)		len = read_vstring(f_in, tmpbuf, MAX_NSTR_STRLEN); 	if (DEBUG_GTE(NSTR, am_server ? 3 : 2)) {		if (am_server)			rprintf(FINFO, "Client %s list (on server): %s\n", nno->type, tmpbuf);		else			rprintf(FINFO, "Server %s list (on client): %s\n", nno->type, tmpbuf);	} 	if (len > 0 && parse_negotiate_str(nno, tmpbuf))		return; 	if (!am_server || !do_negotiated_strings) {		char *cp = tmpbuf;		int j;		rprintf(FERROR, "Failed to negotiate a %s choice.\n", nno->type);		rprintf(FERROR, "%s list: %s\n", am_server ? "Client" : "Server", tmpbuf);		/* Recreate our original list from the saw values. This can't overflow our huge		 * buffer because we don't have enough valid entries to get anywhere close. */		for (j = 1, *cp = '\0'; j <= nno->saw_len; j++) {			struct name_num_item *nni;			for (nni = nno->list; nni->name; nni++) {				if (nno->saw[nni->num] == j) {					*cp++ = ' ';					cp += strlcpy(cp, nni->name, MAX_NSTR_STRLEN - (cp - tmpbuf));					break;				}			}		}		if (!*tmpbuf)			strlcpy(cp, " INVALID", MAX_NSTR_STRLEN);		rprintf(FERROR, "%s list:%s\n", am_server ? "Server" : "Client", tmpbuf);	} 	exit_cleanup(RERR_UNSUPPORTED);} static const char *getenv_nstr(int ntype){	const char *env_str = getenv(ntype == NSTR_COMPRESS ? "RSYNC_COMPRESS_LIST" : "RSYNC_CHECKSUM_LIST"); 	/* When writing a batch file, we always negotiate an old-style choice. */	if (write_batch)		env_str = ntype == NSTR_COMPRESS ? "zlib" : protocol_version >= 30 ? "md5" : "md4"; 	if (am_server && env_str) {		const char *cp = strchr(env_str, '&');		if (cp)			env_str = cp + 1;	} 	return env_str;} void validate_choice_vs_env(int ntype, int num1, int num2){	struct name_num_obj *nno = ntype == NSTR_COMPRESS ? &valid_compressions : &valid_checksums;	const char *list_str = getenv_nstr(ntype);	char tmpbuf[MAX_NSTR_STRLEN]; 	if (!list_str)		return; 	while (isSpace(list_str)) list_str++; 	if (!*list_str)		return; 	init_nno_saw(nno, 0);	parse_nni_str(nno, list_str, tmpbuf, MAX_NSTR_STRLEN); 	if (ntype == NSTR_CHECKSUM) /* If "md4" is in the env list, all the old MD4 choices are OK too. */		nno->saw[CSUM_MD4_ARCHAIC] = nno->saw[CSUM_MD4_BUSTED] = nno->saw[CSUM_MD4_OLD] = nno->saw[CSUM_MD4]; 	if (!nno->saw[num1] || (num2 >= 0 && !nno->saw[num2])) {		rprintf(FERROR, "Your --%s-choice value (%s) was refused by the server.\n",			ntype == NSTR_COMPRESS ? "compress" : "checksum",			ntype == NSTR_COMPRESS ? compress_choice : checksum_choice);		exit_cleanup(RERR_UNSUPPORTED);	} 	free(nno->saw);	nno->saw = NULL;} /* The saw buffer is initialized and used to store ordinal values from 1 to N * for the order of the args in the array.  If dup_markup == '\0', duplicates * are removed otherwise the char is prefixed to the duplicate term and, if it * is an opening paren/bracket/brace, the matching closing char is suffixed. * "none" is removed on the client side unless dup_markup != '\0'. */int get_default_nno_list(struct name_num_obj *nno, char *to_buf, int to_buf_len, char dup_markup){	struct name_num_item *nni;	int len = 0, cnt = 0;	char delim = '\0', post_delim; 	switch (dup_markup) {	case '(': post_delim = ')'; break;	case '[': post_delim = ']'; break;	case '{': post_delim = '}'; break;	default: post_delim = '\0'; break;	} 	init_nno_saw(nno, 0); 	for (nni = nno->list, len = 0; nni->name; nni++) {		if (nni->num == CSUM_gone)			continue;		if (nni->main_nni) {			if (!dup_markup || nni->main_nni->num == CSUM_gone)				continue;			delim = dup_markup;		}		if (nni->num == 0 && !am_server && !dup_markup)			continue;		if (len)			to_buf[len++]= ' ';		if (delim) {			to_buf[len++]= delim;			delim = post_delim;		}		len += strlcpy(to_buf+len, nni->name, to_buf_len - len);		if (len >= to_buf_len - 3)			exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE... */		if (delim) {			to_buf[len++]= delim;			delim = '\0';		}		nno->saw[nni->num] = ++cnt;

Lines 1–500 of 903.