xref: /titanic_44/usr/src/cmd/ssh/scp/scp.c (revision 12fcfd5af4d13749fc322b29438543787680068b)
17c478bd9Sstevel@tonic-gate /*
2*12fcfd5aSjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * scp - secure remote copy.  This is basically patched BSD rcp which
77c478bd9Sstevel@tonic-gate  * uses ssh to do the data transfer (instead of using rcmd).
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * NOTE: This version should NOT be suid root.  (This uses ssh to
107c478bd9Sstevel@tonic-gate  * do the transfer and ssh has the necessary privileges.)
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
157c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
167c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
177c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
187c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
197c478bd9Sstevel@tonic-gate  */
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Aaron Campbell.  All rights reserved.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
257c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
267c478bd9Sstevel@tonic-gate  * are met:
277c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
287c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
297c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
307c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
317c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
347c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
357c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
367c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
377c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
387c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
397c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
407c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
417c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
427c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Parts from:
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1990, 1992, 1993, 1995
497c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
527c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
537c478bd9Sstevel@tonic-gate  * are met:
547c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
557c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
567c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
577c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
587c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
597c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
607c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
617c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
627c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
637c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
647c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
657c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
687c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
697c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
707c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
717c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
727c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
737c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
747c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
757c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
767c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
777c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #include "includes.h"
827c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: scp.c,v 1.91 2002/06/19 00:27:55 deraadt Exp $");
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include "xmalloc.h"
877c478bd9Sstevel@tonic-gate #include "atomicio.h"
887c478bd9Sstevel@tonic-gate #include "pathnames.h"
897c478bd9Sstevel@tonic-gate #include "log.h"
907c478bd9Sstevel@tonic-gate #include "misc.h"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
937c478bd9Sstevel@tonic-gate extern char *__progname;
947c478bd9Sstevel@tonic-gate #else
957c478bd9Sstevel@tonic-gate char *__progname;
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* For progressmeter() -- number of seconds before xfer considered "stalled" */
997c478bd9Sstevel@tonic-gate #define	STALLTIME	5
1007c478bd9Sstevel@tonic-gate /* alarm() interval for updating progress meter */
1017c478bd9Sstevel@tonic-gate #define	PROGRESSTIME	1
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* Visual statistics about files as they are transferred. */
1047c478bd9Sstevel@tonic-gate void progressmeter(int);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* Returns width of the terminal (for progress meter calculations). */
1077c478bd9Sstevel@tonic-gate int getttywidth(void);
1087c478bd9Sstevel@tonic-gate int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout,
1097c478bd9Sstevel@tonic-gate     int argc);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Struct for addargs */
1127c478bd9Sstevel@tonic-gate arglist args;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Time a transfer started. */
1157c478bd9Sstevel@tonic-gate static struct timeval start;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* Number of bytes of current file transferred so far. */
1187c478bd9Sstevel@tonic-gate volatile off_t statbytes;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* Total size of current file. */
1217c478bd9Sstevel@tonic-gate off_t totalbytes = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* Name of current file being transferred. */
1247c478bd9Sstevel@tonic-gate char *curfile;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* This is set to non-zero to enable verbose mode. */
1277c478bd9Sstevel@tonic-gate int verbose_mode = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* This is set to zero if the progressmeter is not desired. */
1307c478bd9Sstevel@tonic-gate int showprogress = 1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* This is the program to execute for the secured connection. ("ssh" or -S) */
1337c478bd9Sstevel@tonic-gate char *ssh_program = _PATH_SSH_PROGRAM;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /* This is used to store the pid of ssh_program */
1367c478bd9Sstevel@tonic-gate static pid_t do_cmd_pid;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * This function executes the given command as the specified user on the
1407c478bd9Sstevel@tonic-gate  * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
1417c478bd9Sstevel@tonic-gate  * assigns the input and output file descriptors on success.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate int
1457c478bd9Sstevel@tonic-gate do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	int pin[2], pout[2], reserved[2];
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (verbose_mode)
1507c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1517c478bd9Sstevel@tonic-gate 			gettext("Executing: program %s host %s, "
1527c478bd9Sstevel@tonic-gate 				"user %s, command %s\n"),
1537c478bd9Sstevel@tonic-gate 			ssh_program, host,
1547c478bd9Sstevel@tonic-gate 			remuser ? remuser : gettext("(unspecified)"), cmd);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * Reserve two descriptors so that the real pipes won't get
1587c478bd9Sstevel@tonic-gate 	 * descriptors 0 and 1 because that will screw up dup2 below.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	pipe(reserved);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* Create a socket pair for communicating with ssh. */
1637c478bd9Sstevel@tonic-gate 	if (pipe(pin) < 0)
1647c478bd9Sstevel@tonic-gate 		fatal("pipe: %s", strerror(errno));
1657c478bd9Sstevel@tonic-gate 	if (pipe(pout) < 0)
1667c478bd9Sstevel@tonic-gate 		fatal("pipe: %s", strerror(errno));
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/* Free the reserved descriptors. */
1697c478bd9Sstevel@tonic-gate 	close(reserved[0]);
1707c478bd9Sstevel@tonic-gate 	close(reserved[1]);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/* For a child to execute the command on the remote host using ssh. */
1737c478bd9Sstevel@tonic-gate 	if ((do_cmd_pid = fork()) == 0)  {
1747c478bd9Sstevel@tonic-gate 		/* Child. */
1757c478bd9Sstevel@tonic-gate 		close(pin[1]);
1767c478bd9Sstevel@tonic-gate 		close(pout[0]);
1777c478bd9Sstevel@tonic-gate 		dup2(pin[0], 0);
1787c478bd9Sstevel@tonic-gate 		dup2(pout[1], 1);
1797c478bd9Sstevel@tonic-gate 		close(pin[0]);
1807c478bd9Sstevel@tonic-gate 		close(pout[1]);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		args.list[0] = ssh_program;
1837c478bd9Sstevel@tonic-gate 		if (remuser != NULL)
1847c478bd9Sstevel@tonic-gate 			addargs(&args, "-l%s", remuser);
1857c478bd9Sstevel@tonic-gate 		addargs(&args, "%s", host);
1867c478bd9Sstevel@tonic-gate 		addargs(&args, "%s", cmd);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		execvp(ssh_program, args.list);
1897c478bd9Sstevel@tonic-gate 		perror(ssh_program);
1907c478bd9Sstevel@tonic-gate 		exit(1);
1917c478bd9Sstevel@tonic-gate 	} else if (do_cmd_pid == (pid_t)-1) {
1927c478bd9Sstevel@tonic-gate 		/* fork() failed */
1937c478bd9Sstevel@tonic-gate 		fatal("fork: %s", strerror(errno));
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/* Parent.  Close the other side, and return the local side. */
1977c478bd9Sstevel@tonic-gate 	close(pin[0]);
1987c478bd9Sstevel@tonic-gate 	*fdout = pin[1];
1997c478bd9Sstevel@tonic-gate 	close(pout[1]);
2007c478bd9Sstevel@tonic-gate 	*fdin = pout[0];
2017c478bd9Sstevel@tonic-gate 	return (0);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate typedef struct {
2057c478bd9Sstevel@tonic-gate 	int cnt;
2067c478bd9Sstevel@tonic-gate 	char *buf;
2077c478bd9Sstevel@tonic-gate } BUF;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate BUF *allocbuf(BUF *, int, int);
2107c478bd9Sstevel@tonic-gate void lostconn(int);
2117c478bd9Sstevel@tonic-gate void nospace(void);
2127c478bd9Sstevel@tonic-gate int okname(char *);
2137c478bd9Sstevel@tonic-gate void run_err(const char *, ...);
2147c478bd9Sstevel@tonic-gate void verifydir(char *);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate struct passwd *pwd;
2177c478bd9Sstevel@tonic-gate uid_t userid;
2187c478bd9Sstevel@tonic-gate int errs, remin, remout;
2197c478bd9Sstevel@tonic-gate int pflag, iamremote, iamrecursive, targetshouldbedirectory;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate #define	CMDNEEDS	64
2227c478bd9Sstevel@tonic-gate char cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate int response(void);
2257c478bd9Sstevel@tonic-gate void rsource(char *, struct stat *);
2267c478bd9Sstevel@tonic-gate void sink(int, char *[]);
2277c478bd9Sstevel@tonic-gate void source(int, char *[]);
2287c478bd9Sstevel@tonic-gate void tolocal(int, char *[]);
2297c478bd9Sstevel@tonic-gate void toremote(char *, int, char *[]);
2307c478bd9Sstevel@tonic-gate void usage(void);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate int
2337c478bd9Sstevel@tonic-gate main(argc, argv)
2347c478bd9Sstevel@tonic-gate 	int argc;
2357c478bd9Sstevel@tonic-gate 	char *argv[];
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	int ch, fflag, tflag, status;
2387c478bd9Sstevel@tonic-gate 	char *targ;
2397c478bd9Sstevel@tonic-gate 	extern char *optarg;
2407c478bd9Sstevel@tonic-gate 	extern int optind;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	__progname = get_progname(argv[0]);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	g11n_setlocale(LC_ALL, "");
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	args.list = NULL;
2477c478bd9Sstevel@tonic-gate 	addargs(&args, "ssh");		/* overwritten with ssh_program */
2487c478bd9Sstevel@tonic-gate 	addargs(&args, "-x");
2497c478bd9Sstevel@tonic-gate 	addargs(&args, "-oForwardAgent no");
2507c478bd9Sstevel@tonic-gate 	addargs(&args, "-oClearAllForwardings yes");
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	fflag = tflag = 0;
2537c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
2547c478bd9Sstevel@tonic-gate 		switch (ch) {
2557c478bd9Sstevel@tonic-gate 		/* User-visible flags. */
2567c478bd9Sstevel@tonic-gate 		case '4':
2577c478bd9Sstevel@tonic-gate 		case '6':
2587c478bd9Sstevel@tonic-gate 		case 'C':
2597c478bd9Sstevel@tonic-gate 			addargs(&args, "-%c", ch);
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		case 'o':
2627c478bd9Sstevel@tonic-gate 		case 'c':
2637c478bd9Sstevel@tonic-gate 		case 'i':
2647c478bd9Sstevel@tonic-gate 		case 'F':
2657c478bd9Sstevel@tonic-gate 			addargs(&args, "-%c%s", ch, optarg);
2667c478bd9Sstevel@tonic-gate 			break;
2677c478bd9Sstevel@tonic-gate 		case 'P':
2687c478bd9Sstevel@tonic-gate 			addargs(&args, "-p%s", optarg);
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 		case 'B':
2717c478bd9Sstevel@tonic-gate 			addargs(&args, "-oBatchmode yes");
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 		case 'p':
2747c478bd9Sstevel@tonic-gate 			pflag = 1;
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 		case 'r':
2777c478bd9Sstevel@tonic-gate 			iamrecursive = 1;
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case 'S':
2807c478bd9Sstevel@tonic-gate 			ssh_program = xstrdup(optarg);
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		case 'v':
2837c478bd9Sstevel@tonic-gate 			addargs(&args, "-v");
2847c478bd9Sstevel@tonic-gate 			verbose_mode = 1;
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 		case 'q':
2877c478bd9Sstevel@tonic-gate 			showprogress = 0;
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		/* Server options. */
2917c478bd9Sstevel@tonic-gate 		case 'd':
2927c478bd9Sstevel@tonic-gate 			targetshouldbedirectory = 1;
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 		case 'f':	/* "from" */
2957c478bd9Sstevel@tonic-gate 			iamremote = 1;
2967c478bd9Sstevel@tonic-gate 			fflag = 1;
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 		case 't':	/* "to" */
2997c478bd9Sstevel@tonic-gate 			iamremote = 1;
3007c478bd9Sstevel@tonic-gate 			tflag = 1;
3017c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
3027c478bd9Sstevel@tonic-gate 			setmode(0, O_BINARY);
3037c478bd9Sstevel@tonic-gate #endif
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 		default:
3067c478bd9Sstevel@tonic-gate 			usage();
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	argc -= optind;
3097c478bd9Sstevel@tonic-gate 	argv += optind;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if ((pwd = getpwuid(userid = getuid())) == NULL)
3127c478bd9Sstevel@tonic-gate 		fatal("unknown user %d", (int)userid);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (!isatty(STDERR_FILENO))
3157c478bd9Sstevel@tonic-gate 		showprogress = 0;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	remin = STDIN_FILENO;
3187c478bd9Sstevel@tonic-gate 	remout = STDOUT_FILENO;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (fflag) {
3217c478bd9Sstevel@tonic-gate 		/* Follow "protocol", send data. */
3227c478bd9Sstevel@tonic-gate 		(void) response();
3237c478bd9Sstevel@tonic-gate 		source(argc, argv);
3247c478bd9Sstevel@tonic-gate 		exit(errs != 0);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	if (tflag) {
3277c478bd9Sstevel@tonic-gate 		/* Receive data. */
3287c478bd9Sstevel@tonic-gate 		sink(argc, argv);
3297c478bd9Sstevel@tonic-gate 		exit(errs != 0);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	if (argc < 2)
3327c478bd9Sstevel@tonic-gate 		usage();
3337c478bd9Sstevel@tonic-gate 	if (argc > 2)
3347c478bd9Sstevel@tonic-gate 		targetshouldbedirectory = 1;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	remin = remout = -1;
3377c478bd9Sstevel@tonic-gate 	do_cmd_pid = (pid_t)-1;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/* Command to be executed on remote system using "ssh". */
3407c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd, sizeof (cmd), "scp%s%s%s%s",
3417c478bd9Sstevel@tonic-gate 	    verbose_mode ? " -v" : "",
3427c478bd9Sstevel@tonic-gate 	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
3437c478bd9Sstevel@tonic-gate 	    targetshouldbedirectory ? " -d" : "");
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, lostconn);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if ((targ = colon(argv[argc - 1])))	/* Dest is remote host. */
3487c478bd9Sstevel@tonic-gate 		toremote(targ, argc, argv);
3497c478bd9Sstevel@tonic-gate 	else {
3507c478bd9Sstevel@tonic-gate 		tolocal(argc, argv);	/* Dest is local host. */
3517c478bd9Sstevel@tonic-gate 		if (targetshouldbedirectory)
3527c478bd9Sstevel@tonic-gate 			verifydir(argv[argc - 1]);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Finally check the exit status of the ssh process, if one was forked
3567c478bd9Sstevel@tonic-gate 	 * and no error has occurred yet
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	if (do_cmd_pid != (pid_t)-1 && errs == 0) {
3597c478bd9Sstevel@tonic-gate 		if (remin != -1) {
3607c478bd9Sstevel@tonic-gate 			(void) close(remin);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		if (remout != -1) {
3637c478bd9Sstevel@tonic-gate 			(void) close(remout);
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		if (waitpid(do_cmd_pid, &status, 0) == -1) {
3667c478bd9Sstevel@tonic-gate 			errs = 1;
3677c478bd9Sstevel@tonic-gate 		} else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
3687c478bd9Sstevel@tonic-gate 			errs = 1;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	return (errs != 0);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate void
3767c478bd9Sstevel@tonic-gate toremote(targ, argc, argv)
3777c478bd9Sstevel@tonic-gate 	char *targ, *argv[];
3787c478bd9Sstevel@tonic-gate 	int argc;
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	int i, len;
3817c478bd9Sstevel@tonic-gate 	char *bp, *host, *src, *suser, *thost, *tuser;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	*targ++ = 0;
3847c478bd9Sstevel@tonic-gate 	if (*targ == 0)
3857c478bd9Sstevel@tonic-gate 		targ = ".";
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	if ((thost = strchr(argv[argc - 1], '@'))) {
3887c478bd9Sstevel@tonic-gate 		/* user@host */
3897c478bd9Sstevel@tonic-gate 		*thost++ = 0;
3907c478bd9Sstevel@tonic-gate 		tuser = argv[argc - 1];
3917c478bd9Sstevel@tonic-gate 		if (*tuser == '\0')
3927c478bd9Sstevel@tonic-gate 			tuser = NULL;
3937c478bd9Sstevel@tonic-gate 		else if (!okname(tuser))
3947c478bd9Sstevel@tonic-gate 			exit(1);
3957c478bd9Sstevel@tonic-gate 	} else {
3967c478bd9Sstevel@tonic-gate 		thost = argv[argc - 1];
3977c478bd9Sstevel@tonic-gate 		tuser = NULL;
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
4017c478bd9Sstevel@tonic-gate 		src = colon(argv[i]);
4027c478bd9Sstevel@tonic-gate 		if (src) {	/* remote to remote */
4037c478bd9Sstevel@tonic-gate 			static char *ssh_options =
4047c478bd9Sstevel@tonic-gate 			    "-x -o'ClearAllForwardings yes'";
4057c478bd9Sstevel@tonic-gate 			*src++ = 0;
4067c478bd9Sstevel@tonic-gate 			if (*src == 0)
4077c478bd9Sstevel@tonic-gate 				src = ".";
4087c478bd9Sstevel@tonic-gate 			host = strchr(argv[i], '@');
4097c478bd9Sstevel@tonic-gate 			len = strlen(ssh_program) + strlen(argv[i]) +
4107c478bd9Sstevel@tonic-gate 			    strlen(src) + (tuser ? strlen(tuser) : 0) +
4117c478bd9Sstevel@tonic-gate 			    strlen(thost) + strlen(targ) +
4127c478bd9Sstevel@tonic-gate 			    strlen(ssh_options) + CMDNEEDS + 20;
4137c478bd9Sstevel@tonic-gate 			bp = xmalloc(len);
4147c478bd9Sstevel@tonic-gate 			if (host) {
4157c478bd9Sstevel@tonic-gate 				*host++ = 0;
4167c478bd9Sstevel@tonic-gate 				host = cleanhostname(host);
4177c478bd9Sstevel@tonic-gate 				suser = argv[i];
4187c478bd9Sstevel@tonic-gate 				if (*suser == '\0')
4197c478bd9Sstevel@tonic-gate 					suser = pwd->pw_name;
4207c478bd9Sstevel@tonic-gate 				else if (!okname(suser))
4217c478bd9Sstevel@tonic-gate 					continue;
4227c478bd9Sstevel@tonic-gate 				snprintf(bp, len,
4237c478bd9Sstevel@tonic-gate 				    "%s%s %s -n "
4247c478bd9Sstevel@tonic-gate 				    "-l %s %s %s %s '%s%s%s:%s'",
4257c478bd9Sstevel@tonic-gate 				    ssh_program, verbose_mode ? " -v" : "",
4267c478bd9Sstevel@tonic-gate 				    ssh_options, suser, host, cmd, src,
4277c478bd9Sstevel@tonic-gate 				    tuser ? tuser : "", tuser ? "@" : "",
4287c478bd9Sstevel@tonic-gate 				    thost, targ);
4297c478bd9Sstevel@tonic-gate 			} else {
4307c478bd9Sstevel@tonic-gate 				host = cleanhostname(argv[i]);
4317c478bd9Sstevel@tonic-gate 				snprintf(bp, len,
4327c478bd9Sstevel@tonic-gate 				    "exec %s%s %s -n %s "
4337c478bd9Sstevel@tonic-gate 				    "%s %s '%s%s%s:%s'",
4347c478bd9Sstevel@tonic-gate 				    ssh_program, verbose_mode ? " -v" : "",
4357c478bd9Sstevel@tonic-gate 				    ssh_options, host, cmd, src,
4367c478bd9Sstevel@tonic-gate 				    tuser ? tuser : "", tuser ? "@" : "",
4377c478bd9Sstevel@tonic-gate 				    thost, targ);
4387c478bd9Sstevel@tonic-gate 			}
4397c478bd9Sstevel@tonic-gate 			if (verbose_mode)
4407c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Executing: %s\n"), bp);
4417c478bd9Sstevel@tonic-gate 			(void) system(bp);
4427c478bd9Sstevel@tonic-gate 			(void) xfree(bp);
4437c478bd9Sstevel@tonic-gate 		} else {	/* local to remote */
4447c478bd9Sstevel@tonic-gate 			if (remin == -1) {
4457c478bd9Sstevel@tonic-gate 				len = strlen(targ) + CMDNEEDS + 20;
4467c478bd9Sstevel@tonic-gate 				bp = xmalloc(len);
4477c478bd9Sstevel@tonic-gate 				(void) snprintf(bp, len, "%s -t %s", cmd, targ);
4487c478bd9Sstevel@tonic-gate 				host = cleanhostname(thost);
4497c478bd9Sstevel@tonic-gate 				if (do_cmd(host, tuser, bp, &remin,
4507c478bd9Sstevel@tonic-gate 				    &remout, argc) < 0)
4517c478bd9Sstevel@tonic-gate 					exit(1);
4527c478bd9Sstevel@tonic-gate 				if (response() < 0)
4537c478bd9Sstevel@tonic-gate 					exit(1);
4547c478bd9Sstevel@tonic-gate 				(void) xfree(bp);
4557c478bd9Sstevel@tonic-gate 			}
4567c478bd9Sstevel@tonic-gate 			source(1, argv + i);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate void
4627c478bd9Sstevel@tonic-gate tolocal(argc, argv)
4637c478bd9Sstevel@tonic-gate 	int argc;
4647c478bd9Sstevel@tonic-gate 	char *argv[];
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	int i, len;
4677c478bd9Sstevel@tonic-gate 	char *bp, *host, *src, *suser;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
4707c478bd9Sstevel@tonic-gate 		if (!(src = colon(argv[i]))) {	/* Local to local. */
4717c478bd9Sstevel@tonic-gate 			len = strlen(_PATH_CP) + strlen(argv[i]) +
4727c478bd9Sstevel@tonic-gate 			    strlen(argv[argc - 1]) + 20;
4737c478bd9Sstevel@tonic-gate 			bp = xmalloc(len);
4747c478bd9Sstevel@tonic-gate 			(void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
4757c478bd9Sstevel@tonic-gate 			    iamrecursive ? " -r" : "", pflag ? " -p" : "",
4767c478bd9Sstevel@tonic-gate 			    argv[i], argv[argc - 1]);
4777c478bd9Sstevel@tonic-gate 			if (verbose_mode)
4787c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Executing: %s\n"), bp);
4797c478bd9Sstevel@tonic-gate 			if (system(bp))
4807c478bd9Sstevel@tonic-gate 				++errs;
4817c478bd9Sstevel@tonic-gate 			(void) xfree(bp);
4827c478bd9Sstevel@tonic-gate 			continue;
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		*src++ = 0;
4857c478bd9Sstevel@tonic-gate 		if (*src == 0)
4867c478bd9Sstevel@tonic-gate 			src = ".";
4877c478bd9Sstevel@tonic-gate 		if ((host = strchr(argv[i], '@')) == NULL) {
4887c478bd9Sstevel@tonic-gate 			host = argv[i];
4897c478bd9Sstevel@tonic-gate 			suser = NULL;
4907c478bd9Sstevel@tonic-gate 		} else {
4917c478bd9Sstevel@tonic-gate 			*host++ = 0;
4927c478bd9Sstevel@tonic-gate 			suser = argv[i];
4937c478bd9Sstevel@tonic-gate 			if (*suser == '\0')
4947c478bd9Sstevel@tonic-gate 				suser = pwd->pw_name;
4957c478bd9Sstevel@tonic-gate 			else if (!okname(suser))
4967c478bd9Sstevel@tonic-gate 				continue;
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 		host = cleanhostname(host);
4997c478bd9Sstevel@tonic-gate 		len = strlen(src) + CMDNEEDS + 20;
5007c478bd9Sstevel@tonic-gate 		bp = xmalloc(len);
5017c478bd9Sstevel@tonic-gate 		(void) snprintf(bp, len, "%s -f %s", cmd, src);
5027c478bd9Sstevel@tonic-gate 		if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
5037c478bd9Sstevel@tonic-gate 			(void) xfree(bp);
5047c478bd9Sstevel@tonic-gate 			++errs;
5057c478bd9Sstevel@tonic-gate 			continue;
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		xfree(bp);
5087c478bd9Sstevel@tonic-gate 		sink(1, argv + argc - 1);
5097c478bd9Sstevel@tonic-gate 		(void) close(remin);
5107c478bd9Sstevel@tonic-gate 		remin = remout = -1;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate void
5157c478bd9Sstevel@tonic-gate source(argc, argv)
5167c478bd9Sstevel@tonic-gate 	int argc;
5177c478bd9Sstevel@tonic-gate 	char *argv[];
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	struct stat stb;
5207c478bd9Sstevel@tonic-gate 	static BUF buffer;
5217c478bd9Sstevel@tonic-gate 	BUF *bp;
5227c478bd9Sstevel@tonic-gate 	off_t i, amt, result;
5237c478bd9Sstevel@tonic-gate 	int fd, haderr, indx;
5247c478bd9Sstevel@tonic-gate 	char *last, *name, buf[2048];
5257c478bd9Sstevel@tonic-gate 	int len;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	for (indx = 0; indx < argc; ++indx) {
5287c478bd9Sstevel@tonic-gate 		name = argv[indx];
5297c478bd9Sstevel@tonic-gate 		statbytes = 0;
5307c478bd9Sstevel@tonic-gate 		len = strlen(name);
5317c478bd9Sstevel@tonic-gate 		while (len > 1 && name[len-1] == '/')
5327c478bd9Sstevel@tonic-gate 			name[--len] = '\0';
5337c478bd9Sstevel@tonic-gate 		if (strchr(name, '\n') != NULL) {
5347c478bd9Sstevel@tonic-gate 			run_err("%s: skipping, filename contains a newline",
5357c478bd9Sstevel@tonic-gate 			    name);
5367c478bd9Sstevel@tonic-gate 			goto next;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 		if ((fd = open(name, O_RDONLY, 0)) < 0)
5397c478bd9Sstevel@tonic-gate 			goto syserr;
5407c478bd9Sstevel@tonic-gate 		if (fstat(fd, &stb) < 0) {
5417c478bd9Sstevel@tonic-gate syserr:			run_err("%s: %s", name, strerror(errno));
5427c478bd9Sstevel@tonic-gate 			goto next;
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 		switch (stb.st_mode & S_IFMT) {
5457c478bd9Sstevel@tonic-gate 		case S_IFREG:
5467c478bd9Sstevel@tonic-gate 			break;
5477c478bd9Sstevel@tonic-gate 		case S_IFDIR:
5487c478bd9Sstevel@tonic-gate 			if (iamrecursive) {
5497c478bd9Sstevel@tonic-gate 				rsource(name, &stb);
5507c478bd9Sstevel@tonic-gate 				goto next;
5517c478bd9Sstevel@tonic-gate 			}
5527c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
5537c478bd9Sstevel@tonic-gate 		default:
5547c478bd9Sstevel@tonic-gate 			run_err("%s: not a regular file", name);
5557c478bd9Sstevel@tonic-gate 			goto next;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		if ((last = strrchr(name, '/')) == NULL)
5587c478bd9Sstevel@tonic-gate 			last = name;
5597c478bd9Sstevel@tonic-gate 		else
5607c478bd9Sstevel@tonic-gate 			++last;
5617c478bd9Sstevel@tonic-gate 		curfile = last;
5627c478bd9Sstevel@tonic-gate 		if (pflag) {
5637c478bd9Sstevel@tonic-gate 			/*
5647c478bd9Sstevel@tonic-gate 			 * Make it compatible with possible future
5657c478bd9Sstevel@tonic-gate 			 * versions expecting microseconds.
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "T%lu 0 %lu 0\n",
5687c478bd9Sstevel@tonic-gate 			    (ulong_t)stb.st_mtime,
5697c478bd9Sstevel@tonic-gate 			    (ulong_t)stb.st_atime);
5707c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, buf, strlen(buf));
5717c478bd9Sstevel@tonic-gate 			if (response() < 0)
5727c478bd9Sstevel@tonic-gate 				goto next;
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate #define	FILEMODEMASK	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
5757c478bd9Sstevel@tonic-gate #ifdef HAVE_LONG_LONG_INT
5767c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "C%04o %lld %s\n",
5777c478bd9Sstevel@tonic-gate 		    (uint_t)(stb.st_mode & FILEMODEMASK),
5787c478bd9Sstevel@tonic-gate 		    (long long)stb.st_size, last);
5797c478bd9Sstevel@tonic-gate #else
5807c478bd9Sstevel@tonic-gate 		/* XXX: Handle integer overflow? */
5817c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "C%04o %lu %s\n",
5827c478bd9Sstevel@tonic-gate 		    (uint_t)(stb.st_mode & FILEMODEMASK),
5837c478bd9Sstevel@tonic-gate 		    (ulong_t)stb.st_size, last);
5847c478bd9Sstevel@tonic-gate #endif
5857c478bd9Sstevel@tonic-gate 		if (verbose_mode) {
5867c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Sending file modes: %s"), buf);
5877c478bd9Sstevel@tonic-gate 			fflush(stderr);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 		(void) atomicio(write, remout, buf, strlen(buf));
5907c478bd9Sstevel@tonic-gate 		if (response() < 0)
5917c478bd9Sstevel@tonic-gate 			goto next;
5927c478bd9Sstevel@tonic-gate 		if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
5937c478bd9Sstevel@tonic-gate next:			(void) close(fd);
5947c478bd9Sstevel@tonic-gate 			continue;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 		if (showprogress) {
5977c478bd9Sstevel@tonic-gate 			totalbytes = stb.st_size;
5987c478bd9Sstevel@tonic-gate 			progressmeter(-1);
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		/* Keep writing after an error so that we stay sync'd up. */
6017c478bd9Sstevel@tonic-gate 		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
6027c478bd9Sstevel@tonic-gate 			amt = bp->cnt;
6037c478bd9Sstevel@tonic-gate 			if (i + amt > stb.st_size)
6047c478bd9Sstevel@tonic-gate 				amt = stb.st_size - i;
6057c478bd9Sstevel@tonic-gate 			if (!haderr) {
6067c478bd9Sstevel@tonic-gate 				result = atomicio(read, fd, bp->buf, amt);
6077c478bd9Sstevel@tonic-gate 				if (result != amt)
6087c478bd9Sstevel@tonic-gate 					haderr = result >= 0 ? EIO : errno;
6097c478bd9Sstevel@tonic-gate 			}
6107c478bd9Sstevel@tonic-gate 			if (haderr)
6117c478bd9Sstevel@tonic-gate 				(void) atomicio(write, remout, bp->buf, amt);
6127c478bd9Sstevel@tonic-gate 			else {
6137c478bd9Sstevel@tonic-gate 				result = atomicio(write, remout, bp->buf, amt);
6147c478bd9Sstevel@tonic-gate 				if (result != amt)
6157c478bd9Sstevel@tonic-gate 					haderr = result >= 0 ? EIO : errno;
6167c478bd9Sstevel@tonic-gate 				statbytes += result;
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 		if (showprogress)
6207c478bd9Sstevel@tonic-gate 			progressmeter(1);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		if (close(fd) < 0 && !haderr)
6237c478bd9Sstevel@tonic-gate 			haderr = errno;
6247c478bd9Sstevel@tonic-gate 		if (!haderr)
6257c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
6267c478bd9Sstevel@tonic-gate 		else
6277c478bd9Sstevel@tonic-gate 			run_err("%s: %s", name, strerror(haderr));
6287c478bd9Sstevel@tonic-gate 		(void) response();
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate void
6337c478bd9Sstevel@tonic-gate rsource(name, statp)
6347c478bd9Sstevel@tonic-gate 	char *name;
6357c478bd9Sstevel@tonic-gate 	struct stat *statp;
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	DIR *dirp;
6387c478bd9Sstevel@tonic-gate 	struct dirent *dp;
6397c478bd9Sstevel@tonic-gate 	char *last, *vect[1], path[1100];
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (!(dirp = opendir(name))) {
6427c478bd9Sstevel@tonic-gate 		run_err("%s: %s", name, strerror(errno));
6437c478bd9Sstevel@tonic-gate 		return;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	last = strrchr(name, '/');
6467c478bd9Sstevel@tonic-gate 	if (last == 0)
6477c478bd9Sstevel@tonic-gate 		last = name;
6487c478bd9Sstevel@tonic-gate 	else
6497c478bd9Sstevel@tonic-gate 		last++;
6507c478bd9Sstevel@tonic-gate 	if (pflag) {
6517c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "T%lu 0 %lu 0\n",
6527c478bd9Sstevel@tonic-gate 		    (ulong_t)statp->st_mtime,
6537c478bd9Sstevel@tonic-gate 		    (ulong_t)statp->st_atime);
6547c478bd9Sstevel@tonic-gate 		(void) atomicio(write, remout, path, strlen(path));
6557c478bd9Sstevel@tonic-gate 		if (response() < 0) {
6567c478bd9Sstevel@tonic-gate 			closedir(dirp);
6577c478bd9Sstevel@tonic-gate 			return;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "D%04o %d %.1024s\n",
6617c478bd9Sstevel@tonic-gate 	    (uint_t)(statp->st_mode & FILEMODEMASK), 0, last);
6627c478bd9Sstevel@tonic-gate 	if (verbose_mode)
6637c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Entering directory: %s"), path);
6647c478bd9Sstevel@tonic-gate 	(void) atomicio(write, remout, path, strlen(path));
6657c478bd9Sstevel@tonic-gate 	if (response() < 0) {
6667c478bd9Sstevel@tonic-gate 		closedir(dirp);
6677c478bd9Sstevel@tonic-gate 		return;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
6707c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
6717c478bd9Sstevel@tonic-gate 			continue;
6727c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
6737c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
6747c478bd9Sstevel@tonic-gate 			continue;
6757c478bd9Sstevel@tonic-gate 		if (strlen(name) + 1 + strlen(dp->d_name) >=
6767c478bd9Sstevel@tonic-gate 		    sizeof (path) - 1) {
6777c478bd9Sstevel@tonic-gate 			run_err("%s/%s: name too long", name, dp->d_name);
6787c478bd9Sstevel@tonic-gate 			continue;
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s/%s", name, dp->d_name);
6817c478bd9Sstevel@tonic-gate 		vect[0] = path;
6827c478bd9Sstevel@tonic-gate 		source(1, vect);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
6857c478bd9Sstevel@tonic-gate 	(void) atomicio(write, remout, "E\n", 2);
6867c478bd9Sstevel@tonic-gate 	(void) response();
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate void
6907c478bd9Sstevel@tonic-gate sink(argc, argv)
6917c478bd9Sstevel@tonic-gate 	int argc;
6927c478bd9Sstevel@tonic-gate 	char *argv[];
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	static BUF buffer;
6957c478bd9Sstevel@tonic-gate 	struct stat stb;
6967c478bd9Sstevel@tonic-gate 	enum {
6977c478bd9Sstevel@tonic-gate 		YES, NO, DISPLAYED
6987c478bd9Sstevel@tonic-gate 	} wrerr;
6997c478bd9Sstevel@tonic-gate 	BUF *bp;
7007c478bd9Sstevel@tonic-gate 	off_t i, j;
7017c478bd9Sstevel@tonic-gate 	int amt, count, exists, first, mask, mode, ofd, omode;
7027c478bd9Sstevel@tonic-gate 	off_t size;
7037c478bd9Sstevel@tonic-gate 	int setimes, targisdir, wrerrno = 0;
7047c478bd9Sstevel@tonic-gate 	char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
7057c478bd9Sstevel@tonic-gate 	struct timeval tv[2];
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate #define	atime	tv[0]
7087c478bd9Sstevel@tonic-gate #define	mtime	tv[1]
7097c478bd9Sstevel@tonic-gate #define	SCREWUP(str)	{ why = str; goto screwup; }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	setimes = targisdir = 0;
7127c478bd9Sstevel@tonic-gate 	mask = umask(0);
7137c478bd9Sstevel@tonic-gate 	if (!pflag)
7147c478bd9Sstevel@tonic-gate 		(void) umask(mask);
7157c478bd9Sstevel@tonic-gate 	if (argc != 1) {
7167c478bd9Sstevel@tonic-gate 		run_err("ambiguous target");
7177c478bd9Sstevel@tonic-gate 		exit(1);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	targ = *argv;
7207c478bd9Sstevel@tonic-gate 	if (targetshouldbedirectory)
7217c478bd9Sstevel@tonic-gate 		verifydir(targ);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	(void) atomicio(write, remout, "", 1);
7247c478bd9Sstevel@tonic-gate 	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
7257c478bd9Sstevel@tonic-gate 		targisdir = 1;
7267c478bd9Sstevel@tonic-gate 	for (first = 1; ; first = 0) {
7277c478bd9Sstevel@tonic-gate 		cp = buf;
7287c478bd9Sstevel@tonic-gate 		if (atomicio(read, remin, cp, 1) <= 0)
7297c478bd9Sstevel@tonic-gate 			return;
7307c478bd9Sstevel@tonic-gate 		if (*cp++ == '\n')
7317c478bd9Sstevel@tonic-gate 			SCREWUP("unexpected <newline>")
7327c478bd9Sstevel@tonic-gate 		do {
7337c478bd9Sstevel@tonic-gate 			if (atomicio(read, remin, &ch, sizeof (ch)) !=
7347c478bd9Sstevel@tonic-gate 			    sizeof (ch))
7357c478bd9Sstevel@tonic-gate 				SCREWUP("lost connection")
7367c478bd9Sstevel@tonic-gate 			*cp++ = ch;
7377c478bd9Sstevel@tonic-gate 		} while (cp < &buf[sizeof (buf) - 1] && ch != '\n');
7387c478bd9Sstevel@tonic-gate 		*cp = 0;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		if (buf[0] == '\01' || buf[0] == '\02') {
7417c478bd9Sstevel@tonic-gate 			if (iamremote == 0)
7427c478bd9Sstevel@tonic-gate 				(void) atomicio(write, STDERR_FILENO,
7437c478bd9Sstevel@tonic-gate 				    buf + 1, strlen(buf + 1));
7447c478bd9Sstevel@tonic-gate 			if (buf[0] == '\02')
7457c478bd9Sstevel@tonic-gate 				exit(1);
7467c478bd9Sstevel@tonic-gate 			++errs;
7477c478bd9Sstevel@tonic-gate 			continue;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 		if (buf[0] == 'E') {
7507c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
7517c478bd9Sstevel@tonic-gate 			return;
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 		if (ch == '\n')
7547c478bd9Sstevel@tonic-gate 			*--cp = 0;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		cp = buf;
7577c478bd9Sstevel@tonic-gate 		if (*cp == 'T') {
7587c478bd9Sstevel@tonic-gate 			setimes++;
7597c478bd9Sstevel@tonic-gate 			cp++;
7607c478bd9Sstevel@tonic-gate 			mtime.tv_sec = strtol(cp, &cp, 10);
7617c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != ' ')
7627c478bd9Sstevel@tonic-gate 				SCREWUP("mtime.sec not delimited")
7637c478bd9Sstevel@tonic-gate 			mtime.tv_usec = strtol(cp, &cp, 10);
7647c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != ' ')
7657c478bd9Sstevel@tonic-gate 				SCREWUP("mtime.usec not delimited")
7667c478bd9Sstevel@tonic-gate 			atime.tv_sec = strtol(cp, &cp, 10);
7677c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != ' ')
7687c478bd9Sstevel@tonic-gate 				SCREWUP("atime.sec not delimited")
7697c478bd9Sstevel@tonic-gate 			atime.tv_usec = strtol(cp, &cp, 10);
7707c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != '\0')
7717c478bd9Sstevel@tonic-gate 				SCREWUP("atime.usec not delimited")
7727c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
7737c478bd9Sstevel@tonic-gate 			continue;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 		if (*cp != 'C' && *cp != 'D') {
7767c478bd9Sstevel@tonic-gate 			/*
7777c478bd9Sstevel@tonic-gate 			 * Check for the case "rcp remote:foo\* local:bar".
7787c478bd9Sstevel@tonic-gate 			 * In this case, the line "No match." can be returned
7797c478bd9Sstevel@tonic-gate 			 * by the shell before the rcp command on the remote is
7807c478bd9Sstevel@tonic-gate 			 * executed so the ^Aerror_message convention isn't
7817c478bd9Sstevel@tonic-gate 			 * followed.
7827c478bd9Sstevel@tonic-gate 			 */
7837c478bd9Sstevel@tonic-gate 			if (first) {
7847c478bd9Sstevel@tonic-gate 				run_err("%s", cp);
7857c478bd9Sstevel@tonic-gate 				exit(1);
7867c478bd9Sstevel@tonic-gate 			}
7877c478bd9Sstevel@tonic-gate 			SCREWUP("expected control record")
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		mode = 0;
7907c478bd9Sstevel@tonic-gate 		for (++cp; cp < buf + 5; cp++) {
7917c478bd9Sstevel@tonic-gate 			if (*cp < '0' || *cp > '7')
7927c478bd9Sstevel@tonic-gate 				SCREWUP("bad mode")
7937c478bd9Sstevel@tonic-gate 			mode = (mode << 3) | (*cp - '0');
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		if (*cp++ != ' ')
7967c478bd9Sstevel@tonic-gate 			SCREWUP("mode not delimited")
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		for (size = 0; isdigit(*cp); )
7997c478bd9Sstevel@tonic-gate 			size = size * 10 + (*cp++ - '0');
8007c478bd9Sstevel@tonic-gate 		if (*cp++ != ' ')
8017c478bd9Sstevel@tonic-gate 			SCREWUP("size not delimited")
8027c478bd9Sstevel@tonic-gate 		if (targisdir) {
8037c478bd9Sstevel@tonic-gate 			static char *namebuf;
8047c478bd9Sstevel@tonic-gate 			static int cursize;
8057c478bd9Sstevel@tonic-gate 			size_t need;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 			need = strlen(targ) + strlen(cp) + 250;
8087c478bd9Sstevel@tonic-gate 			if (need > cursize) {
8097c478bd9Sstevel@tonic-gate 				if (namebuf)
8107c478bd9Sstevel@tonic-gate 					xfree(namebuf);
8117c478bd9Sstevel@tonic-gate 				namebuf = xmalloc(need);
8127c478bd9Sstevel@tonic-gate 				cursize = need;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 			(void) snprintf(namebuf, need, "%s%s%s", targ,
8157c478bd9Sstevel@tonic-gate 			    strcmp(targ, "/") ? "/" : "", cp);
8167c478bd9Sstevel@tonic-gate 			np = namebuf;
8177c478bd9Sstevel@tonic-gate 		} else
8187c478bd9Sstevel@tonic-gate 			np = targ;
8197c478bd9Sstevel@tonic-gate 		curfile = cp;
8207c478bd9Sstevel@tonic-gate 		exists = stat(np, &stb) == 0;
8217c478bd9Sstevel@tonic-gate 		if (buf[0] == 'D') {
8227c478bd9Sstevel@tonic-gate 			int mod_flag = pflag;
8237c478bd9Sstevel@tonic-gate 			if (exists) {
8247c478bd9Sstevel@tonic-gate 				if (!S_ISDIR(stb.st_mode)) {
8257c478bd9Sstevel@tonic-gate 					errno = ENOTDIR;
8267c478bd9Sstevel@tonic-gate 					goto bad;
8277c478bd9Sstevel@tonic-gate 				}
8287c478bd9Sstevel@tonic-gate 				if (pflag)
8297c478bd9Sstevel@tonic-gate 					(void) chmod(np, mode);
8307c478bd9Sstevel@tonic-gate 			} else {
8317c478bd9Sstevel@tonic-gate 				/*
8327c478bd9Sstevel@tonic-gate 				 * Handle copying from a read-only
8337c478bd9Sstevel@tonic-gate 				 * directory
8347c478bd9Sstevel@tonic-gate 				 */
8357c478bd9Sstevel@tonic-gate 				mod_flag = 1;
8367c478bd9Sstevel@tonic-gate 				if (mkdir(np, mode | S_IRWXU) < 0)
8377c478bd9Sstevel@tonic-gate 					goto bad;
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 			vect[0] = xstrdup(np);
8407c478bd9Sstevel@tonic-gate 			sink(1, vect);
8417c478bd9Sstevel@tonic-gate 			if (setimes) {
8427c478bd9Sstevel@tonic-gate 				setimes = 0;
8437c478bd9Sstevel@tonic-gate 				if (utimes(vect[0], tv) < 0)
8447c478bd9Sstevel@tonic-gate 					run_err("%s: set times: %s",
8457c478bd9Sstevel@tonic-gate 					    vect[0], strerror(errno));
8467c478bd9Sstevel@tonic-gate 			}
8477c478bd9Sstevel@tonic-gate 			if (mod_flag)
8487c478bd9Sstevel@tonic-gate 				(void) chmod(vect[0], mode);
8497c478bd9Sstevel@tonic-gate 			if (vect[0])
8507c478bd9Sstevel@tonic-gate 				xfree(vect[0]);
8517c478bd9Sstevel@tonic-gate 			continue;
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 		omode = mode;
8547c478bd9Sstevel@tonic-gate 		mode |= S_IWRITE;
8557c478bd9Sstevel@tonic-gate 		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
8567c478bd9Sstevel@tonic-gate bad:			run_err("%s: %s", np, strerror(errno));
8577c478bd9Sstevel@tonic-gate 			continue;
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 		(void) atomicio(write, remout, "", 1);
8607c478bd9Sstevel@tonic-gate 		if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
8617c478bd9Sstevel@tonic-gate 			(void) close(ofd);
8627c478bd9Sstevel@tonic-gate 			continue;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		wrerr = NO;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		if (showprogress) {
8677c478bd9Sstevel@tonic-gate 			totalbytes = size;
8687c478bd9Sstevel@tonic-gate 			progressmeter(-1);
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 		statbytes = 0;
871*12fcfd5aSjp161948 		for (i = 0; i < size; i += bp->cnt) {
872*12fcfd5aSjp161948 			amt = bp->cnt;
873*12fcfd5aSjp161948 			cp = bp->buf;
8747c478bd9Sstevel@tonic-gate 			if (i + amt > size)
8757c478bd9Sstevel@tonic-gate 				amt = size - i;
876*12fcfd5aSjp161948 			count = amt;
8777c478bd9Sstevel@tonic-gate 			do {
8787c478bd9Sstevel@tonic-gate 				j = read(remin, cp, amt);
8797c478bd9Sstevel@tonic-gate 				if (j == -1 && (errno == EINTR ||
8807c478bd9Sstevel@tonic-gate 				    errno == EAGAIN)) {
8817c478bd9Sstevel@tonic-gate 					continue;
8827c478bd9Sstevel@tonic-gate 				} else if (j <= 0) {
8837c478bd9Sstevel@tonic-gate 					run_err("%s", j ? strerror(errno) :
8847c478bd9Sstevel@tonic-gate 					    "dropped connection");
8857c478bd9Sstevel@tonic-gate 					exit(1);
8867c478bd9Sstevel@tonic-gate 				}
8877c478bd9Sstevel@tonic-gate 				amt -= j;
8887c478bd9Sstevel@tonic-gate 				cp += j;
8897c478bd9Sstevel@tonic-gate 				statbytes += j;
8907c478bd9Sstevel@tonic-gate 			} while (amt > 0);
8917c478bd9Sstevel@tonic-gate 			/* Keep reading so we stay sync'd up. */
8927c478bd9Sstevel@tonic-gate 			if (wrerr == NO) {
8937c478bd9Sstevel@tonic-gate 				j = atomicio(write, ofd, bp->buf,
8947c478bd9Sstevel@tonic-gate 				    count);
8957c478bd9Sstevel@tonic-gate 				if (j != count) {
8967c478bd9Sstevel@tonic-gate 					wrerr = YES;
8977c478bd9Sstevel@tonic-gate 					wrerrno = j >= 0 ? EIO : errno;
8987c478bd9Sstevel@tonic-gate 				}
8997c478bd9Sstevel@tonic-gate 			}
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		if (showprogress)
9027c478bd9Sstevel@tonic-gate 			progressmeter(1);
9037c478bd9Sstevel@tonic-gate 		if (count != 0 && wrerr == NO &&
9047c478bd9Sstevel@tonic-gate 		    (j = atomicio(write, ofd, bp->buf, count)) != count) {
9057c478bd9Sstevel@tonic-gate 			wrerr = YES;
9067c478bd9Sstevel@tonic-gate 			wrerrno = j >= 0 ? EIO : errno;
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 		if (ftruncate(ofd, size)) {
9097c478bd9Sstevel@tonic-gate 			run_err("%s: truncate: %s", np, strerror(errno));
9107c478bd9Sstevel@tonic-gate 			wrerr = DISPLAYED;
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 		if (pflag) {
9137c478bd9Sstevel@tonic-gate 			if (exists || omode != mode)
9147c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
9157c478bd9Sstevel@tonic-gate 				if (fchmod(ofd, omode))
9167c478bd9Sstevel@tonic-gate #else /* HAVE_FCHMOD */
9177c478bd9Sstevel@tonic-gate 				if (chmod(np, omode))
9187c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9197c478bd9Sstevel@tonic-gate 					run_err("%s: set mode: %s",
9207c478bd9Sstevel@tonic-gate 					    np, strerror(errno));
9217c478bd9Sstevel@tonic-gate 		} else {
9227c478bd9Sstevel@tonic-gate 			if (!exists && omode != mode)
9237c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
9247c478bd9Sstevel@tonic-gate 				if (fchmod(ofd, omode & ~mask))
9257c478bd9Sstevel@tonic-gate #else /* HAVE_FCHMOD */
9267c478bd9Sstevel@tonic-gate 				if (chmod(np, omode & ~mask))
9277c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9287c478bd9Sstevel@tonic-gate 					run_err("%s: set mode: %s",
9297c478bd9Sstevel@tonic-gate 					    np, strerror(errno));
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 		if (close(ofd) == -1) {
9327c478bd9Sstevel@tonic-gate 			wrerr = YES;
9337c478bd9Sstevel@tonic-gate 			wrerrno = errno;
9347c478bd9Sstevel@tonic-gate 		}
9357c478bd9Sstevel@tonic-gate 		(void) response();
9367c478bd9Sstevel@tonic-gate 		if (setimes && wrerr == NO) {
9377c478bd9Sstevel@tonic-gate 			setimes = 0;
9387c478bd9Sstevel@tonic-gate 			if (utimes(np, tv) < 0) {
9397c478bd9Sstevel@tonic-gate 				run_err("%s: set times: %s",
9407c478bd9Sstevel@tonic-gate 				    np, strerror(errno));
9417c478bd9Sstevel@tonic-gate 				wrerr = DISPLAYED;
9427c478bd9Sstevel@tonic-gate 			}
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 		switch (wrerr) {
9457c478bd9Sstevel@tonic-gate 		case YES:
9467c478bd9Sstevel@tonic-gate 			run_err("%s: %s", np, strerror(wrerrno));
9477c478bd9Sstevel@tonic-gate 			break;
9487c478bd9Sstevel@tonic-gate 		case NO:
9497c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
9507c478bd9Sstevel@tonic-gate 			break;
9517c478bd9Sstevel@tonic-gate 		case DISPLAYED:
9527c478bd9Sstevel@tonic-gate 			break;
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate screwup:
9567c478bd9Sstevel@tonic-gate 	run_err("protocol error: %s", why);
9577c478bd9Sstevel@tonic-gate 	exit(1);
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate int
9617c478bd9Sstevel@tonic-gate response(void)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	char ch, *cp, resp, rbuf[2048];
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (atomicio(read, remin, &resp, sizeof (resp)) != sizeof (resp))
9667c478bd9Sstevel@tonic-gate 		lostconn(0);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	cp = rbuf;
9697c478bd9Sstevel@tonic-gate 	switch (resp) {
9707c478bd9Sstevel@tonic-gate 	case 0:		/* ok */
9717c478bd9Sstevel@tonic-gate 		return (0);
9727c478bd9Sstevel@tonic-gate 	default:
9737c478bd9Sstevel@tonic-gate 		*cp++ = resp;
9747c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
9757c478bd9Sstevel@tonic-gate 	case 1:		/* error, followed by error msg */
9767c478bd9Sstevel@tonic-gate 	case 2:		/* fatal error, "" */
9777c478bd9Sstevel@tonic-gate 		do {
9787c478bd9Sstevel@tonic-gate 			if (atomicio(read, remin, &ch, sizeof (ch)) !=
9797c478bd9Sstevel@tonic-gate 			    sizeof (ch))
9807c478bd9Sstevel@tonic-gate 				lostconn(0);
9817c478bd9Sstevel@tonic-gate 			*cp++ = ch;
9827c478bd9Sstevel@tonic-gate 		} while (cp < &rbuf[sizeof (rbuf) - 1] && ch != '\n');
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		if (!iamremote)
9857c478bd9Sstevel@tonic-gate 			(void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
9867c478bd9Sstevel@tonic-gate 		++errs;
9877c478bd9Sstevel@tonic-gate 		if (resp == 1)
9887c478bd9Sstevel@tonic-gate 			return (-1);
9897c478bd9Sstevel@tonic-gate 		exit(1);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate void
9957c478bd9Sstevel@tonic-gate usage(void)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
9987c478bd9Sstevel@tonic-gate 	    gettext(
9997c478bd9Sstevel@tonic-gate 		"Usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
10007c478bd9Sstevel@tonic-gate 		"           [-c cipher] [-i identity] [-o option]\n"
10017c478bd9Sstevel@tonic-gate 		"           [[user@]host1:]file1 [...] "
10027c478bd9Sstevel@tonic-gate 		"[[user@]host2:]file2\n"));
10037c478bd9Sstevel@tonic-gate 	exit(1);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
10077c478bd9Sstevel@tonic-gate void
10087c478bd9Sstevel@tonic-gate run_err(const char *fmt, ...)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 	static FILE *fp;
10117c478bd9Sstevel@tonic-gate 	va_list ap;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	++errs;
10147c478bd9Sstevel@tonic-gate 	if (fp == NULL && !(fp = fdopen(remout, "w")))
10157c478bd9Sstevel@tonic-gate 		return;
10167c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%c", 0x01);
10177c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "scp: ");
10187c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
10197c478bd9Sstevel@tonic-gate 	(void) vfprintf(fp, fmt, ap);
10207c478bd9Sstevel@tonic-gate 	va_end(ap);
10217c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\n");
10227c478bd9Sstevel@tonic-gate 	(void) fflush(fp);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	if (!iamremote) {
10257c478bd9Sstevel@tonic-gate 		va_start(ap, fmt);
10267c478bd9Sstevel@tonic-gate 		vfprintf(stderr, fmt, ap);
10277c478bd9Sstevel@tonic-gate 		va_end(ap);
10287c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\n");
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate void
10337c478bd9Sstevel@tonic-gate verifydir(cp)
10347c478bd9Sstevel@tonic-gate 	char *cp;
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	struct stat stb;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (!stat(cp, &stb)) {
10397c478bd9Sstevel@tonic-gate 		if (S_ISDIR(stb.st_mode))
10407c478bd9Sstevel@tonic-gate 			return;
10417c478bd9Sstevel@tonic-gate 		errno = ENOTDIR;
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 	run_err("%s: %s", cp, strerror(errno));
10447c478bd9Sstevel@tonic-gate 	exit(1);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate int
10487c478bd9Sstevel@tonic-gate okname(cp0)
10497c478bd9Sstevel@tonic-gate 	char *cp0;
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	int c;
10527c478bd9Sstevel@tonic-gate 	char *cp;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	cp = cp0;
10557c478bd9Sstevel@tonic-gate 	do {
10567c478bd9Sstevel@tonic-gate 		c = (int)*cp;
10577c478bd9Sstevel@tonic-gate 		if (c & 0200)
10587c478bd9Sstevel@tonic-gate 			goto bad;
10597c478bd9Sstevel@tonic-gate 		if (!isalpha(c) && !isdigit(c) &&
10607c478bd9Sstevel@tonic-gate 		    c != '_' && c != '-' && c != '.' && c != '+')
10617c478bd9Sstevel@tonic-gate 			goto bad;
10627c478bd9Sstevel@tonic-gate 	} while (*++cp);
10637c478bd9Sstevel@tonic-gate 	return (1);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate bad:	fprintf(stderr, gettext("%s: invalid user name\n"), cp0);
10667c478bd9Sstevel@tonic-gate 	return (0);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate BUF *
10707c478bd9Sstevel@tonic-gate allocbuf(bp, fd, blksize)
10717c478bd9Sstevel@tonic-gate 	BUF *bp;
10727c478bd9Sstevel@tonic-gate 	int fd, blksize;
10737c478bd9Sstevel@tonic-gate {
10747c478bd9Sstevel@tonic-gate 	size_t size;
10757c478bd9Sstevel@tonic-gate #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
10767c478bd9Sstevel@tonic-gate 	struct stat stb;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stb) < 0) {
10797c478bd9Sstevel@tonic-gate 		run_err("fstat: %s", strerror(errno));
10807c478bd9Sstevel@tonic-gate 		return (0);
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 	if (stb.st_blksize == 0)
10837c478bd9Sstevel@tonic-gate 		size = blksize;
10847c478bd9Sstevel@tonic-gate 	else
10857c478bd9Sstevel@tonic-gate 		size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
10867c478bd9Sstevel@tonic-gate 		    stb.st_blksize;
10877c478bd9Sstevel@tonic-gate #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
10887c478bd9Sstevel@tonic-gate 	size = blksize;
10897c478bd9Sstevel@tonic-gate #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
10907c478bd9Sstevel@tonic-gate 	if (bp->cnt >= size)
10917c478bd9Sstevel@tonic-gate 		return (bp);
10927c478bd9Sstevel@tonic-gate 	if (bp->buf == NULL)
10937c478bd9Sstevel@tonic-gate 		bp->buf = xmalloc(size);
10947c478bd9Sstevel@tonic-gate 	else
10957c478bd9Sstevel@tonic-gate 		bp->buf = xrealloc(bp->buf, size);
10967c478bd9Sstevel@tonic-gate 	memset(bp->buf, 0, size);
10977c478bd9Sstevel@tonic-gate 	bp->cnt = size;
10987c478bd9Sstevel@tonic-gate 	return (bp);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate void
11027c478bd9Sstevel@tonic-gate lostconn(signo)
11037c478bd9Sstevel@tonic-gate 	int signo;
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	if (!iamremote)
11067c478bd9Sstevel@tonic-gate 		write(STDERR_FILENO, "lost connection\n", 16);
11077c478bd9Sstevel@tonic-gate 	if (signo)
11087c478bd9Sstevel@tonic-gate 		_exit(1);
11097c478bd9Sstevel@tonic-gate 	else
11107c478bd9Sstevel@tonic-gate 		exit(1);
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate static void
11147c478bd9Sstevel@tonic-gate updateprogressmeter(int ignore)
11157c478bd9Sstevel@tonic-gate {
11167c478bd9Sstevel@tonic-gate 	int save_errno = errno;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	progressmeter(0);
11197c478bd9Sstevel@tonic-gate 	signal(SIGALRM, updateprogressmeter);
11207c478bd9Sstevel@tonic-gate 	alarm(PROGRESSTIME);
11217c478bd9Sstevel@tonic-gate 	errno = save_errno;
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate static int
11257c478bd9Sstevel@tonic-gate foregroundproc(void)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	static pid_t pgrp = -1;
11287c478bd9Sstevel@tonic-gate 	int ctty_pgrp;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (pgrp == -1)
11317c478bd9Sstevel@tonic-gate 		pgrp = getpgrp();
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate #ifdef HAVE_TCGETPGRP
11347c478bd9Sstevel@tonic-gate 	return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
11357c478bd9Sstevel@tonic-gate 		ctty_pgrp == pgrp);
11367c478bd9Sstevel@tonic-gate #else
11377c478bd9Sstevel@tonic-gate 	return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
11387c478bd9Sstevel@tonic-gate 	    ctty_pgrp == pgrp));
11397c478bd9Sstevel@tonic-gate #endif
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate void
11437c478bd9Sstevel@tonic-gate progressmeter(int flag)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 	static const char prefixes[] = " KMGTP";
11467c478bd9Sstevel@tonic-gate 	static struct timeval lastupdate;
11477c478bd9Sstevel@tonic-gate 	static off_t lastsize;
11487c478bd9Sstevel@tonic-gate 	struct timeval now, td, wait;
11497c478bd9Sstevel@tonic-gate 	off_t cursize, abbrevsize;
11507c478bd9Sstevel@tonic-gate 	double elapsed;
11517c478bd9Sstevel@tonic-gate 	int ratio, barlength, i, remaining;
11527c478bd9Sstevel@tonic-gate 	char buf[512];
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (flag == -1) {
11557c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&start, (struct timezone *)0);
11567c478bd9Sstevel@tonic-gate 		lastupdate = start;
11577c478bd9Sstevel@tonic-gate 		lastsize = 0;
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	if (foregroundproc() == 0)
11607c478bd9Sstevel@tonic-gate 		return;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, (struct timezone *)0);
11637c478bd9Sstevel@tonic-gate 	cursize = statbytes;
11647c478bd9Sstevel@tonic-gate 	if (totalbytes != 0) {
11657c478bd9Sstevel@tonic-gate 		ratio = (int)(100.0 * cursize / totalbytes);
11667c478bd9Sstevel@tonic-gate 		ratio = MAX(ratio, 0);
11677c478bd9Sstevel@tonic-gate 		ratio = MIN(ratio, 100);
11687c478bd9Sstevel@tonic-gate 	} else
11697c478bd9Sstevel@tonic-gate 		ratio = 100;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "\r%-20.20s %3d%% ", curfile, ratio);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	barlength = getttywidth() - 51;
11747c478bd9Sstevel@tonic-gate 	if (barlength > 0) {
11757c478bd9Sstevel@tonic-gate 		i = barlength * ratio / 100;
11767c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
11777c478bd9Sstevel@tonic-gate 		    "|%.*s%*s|", i,
11787c478bd9Sstevel@tonic-gate 		    "*******************************************************"
11797c478bd9Sstevel@tonic-gate 		    "*******************************************************"
11807c478bd9Sstevel@tonic-gate 		    "*******************************************************"
11817c478bd9Sstevel@tonic-gate 		    "*******************************************************"
11827c478bd9Sstevel@tonic-gate 		    "*******************************************************"
11837c478bd9Sstevel@tonic-gate 		    "*******************************************************"
11847c478bd9Sstevel@tonic-gate 		    "*******************************************************",
11857c478bd9Sstevel@tonic-gate 		    barlength - i, "");
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 	i = 0;
11887c478bd9Sstevel@tonic-gate 	abbrevsize = cursize;
11897c478bd9Sstevel@tonic-gate 	while (abbrevsize >= 100000 && i < sizeof (prefixes)) {
11907c478bd9Sstevel@tonic-gate 		i++;
11917c478bd9Sstevel@tonic-gate 		abbrevsize >>= 10;
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 	snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf), " %5lu %c%c ",
11947c478bd9Sstevel@tonic-gate 	    (unsigned long) abbrevsize, prefixes[i],
11957c478bd9Sstevel@tonic-gate 	    prefixes[i] == ' ' ? ' ' : 'B');
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	timersub(&now, &lastupdate, &wait);
11987c478bd9Sstevel@tonic-gate 	if (cursize > lastsize) {
11997c478bd9Sstevel@tonic-gate 		lastupdate = now;
12007c478bd9Sstevel@tonic-gate 		lastsize = cursize;
12017c478bd9Sstevel@tonic-gate 		if (wait.tv_sec >= STALLTIME) {
12027c478bd9Sstevel@tonic-gate 			start.tv_sec += wait.tv_sec;
12037c478bd9Sstevel@tonic-gate 			start.tv_usec += wait.tv_usec;
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 		wait.tv_sec = 0;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 	timersub(&now, &start, &td);
12087c478bd9Sstevel@tonic-gate 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	if (flag != 1 &&
12117c478bd9Sstevel@tonic-gate 	    (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
12127c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12137c478bd9Sstevel@tonic-gate 		    "   --:-- ETA");
12147c478bd9Sstevel@tonic-gate 	} else if (wait.tv_sec >= STALLTIME) {
12157c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12167c478bd9Sstevel@tonic-gate 		    " - stalled -");
12177c478bd9Sstevel@tonic-gate 	} else {
12187c478bd9Sstevel@tonic-gate 		if (flag != 1)
12197c478bd9Sstevel@tonic-gate 			remaining = (int)(totalbytes / (statbytes / elapsed) -
12207c478bd9Sstevel@tonic-gate 			    elapsed);
12217c478bd9Sstevel@tonic-gate 		else
12227c478bd9Sstevel@tonic-gate 			remaining = (int)elapsed;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		i = remaining / 3600;
12257c478bd9Sstevel@tonic-gate 		if (i)
12267c478bd9Sstevel@tonic-gate 			snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12277c478bd9Sstevel@tonic-gate 			    "%2d:", i);
12287c478bd9Sstevel@tonic-gate 		else
12297c478bd9Sstevel@tonic-gate 			snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12307c478bd9Sstevel@tonic-gate 			    "   ");
12317c478bd9Sstevel@tonic-gate 		i = remaining % 3600;
12327c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12337c478bd9Sstevel@tonic-gate 		    "%02d:%02d%s", i / 60, i % 60,
12347c478bd9Sstevel@tonic-gate 		    (flag != 1) ? " ETA" : "    ");
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	atomicio(write, fileno(stdout), buf, strlen(buf));
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (flag == -1) {
12397c478bd9Sstevel@tonic-gate 		mysignal(SIGALRM, updateprogressmeter);
12407c478bd9Sstevel@tonic-gate 		alarm(PROGRESSTIME);
12417c478bd9Sstevel@tonic-gate 	} else if (flag == 1) {
12427c478bd9Sstevel@tonic-gate 		alarm(0);
12437c478bd9Sstevel@tonic-gate 		atomicio(write, fileno(stdout), "\n", 1);
12447c478bd9Sstevel@tonic-gate 		statbytes = 0;
12457c478bd9Sstevel@tonic-gate 	}
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate int
12497c478bd9Sstevel@tonic-gate getttywidth(void)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	struct winsize winsize;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
12547c478bd9Sstevel@tonic-gate 		return (winsize.ws_col ? winsize.ws_col : 80);
12557c478bd9Sstevel@tonic-gate 	else
12567c478bd9Sstevel@tonic-gate 		return (80);
12577c478bd9Sstevel@tonic-gate }
1258