17c478bd9Sstevel@tonic-gate /*
26c91005fSHuie-Ying Lee * Copyright 2009 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 #include "xmalloc.h"
857c478bd9Sstevel@tonic-gate #include "atomicio.h"
867c478bd9Sstevel@tonic-gate #include "pathnames.h"
877c478bd9Sstevel@tonic-gate #include "log.h"
887c478bd9Sstevel@tonic-gate #include "misc.h"
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
917c478bd9Sstevel@tonic-gate extern char *__progname;
927c478bd9Sstevel@tonic-gate #else
937c478bd9Sstevel@tonic-gate char *__progname;
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /* For progressmeter() -- number of seconds before xfer considered "stalled" */
977c478bd9Sstevel@tonic-gate #define STALLTIME 5
987c478bd9Sstevel@tonic-gate /* alarm() interval for updating progress meter */
997c478bd9Sstevel@tonic-gate #define PROGRESSTIME 1
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* Visual statistics about files as they are transferred. */
1027c478bd9Sstevel@tonic-gate void progressmeter(int);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /* Returns width of the terminal (for progress meter calculations). */
1057c478bd9Sstevel@tonic-gate int getttywidth(void);
1067c478bd9Sstevel@tonic-gate int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout,
1077c478bd9Sstevel@tonic-gate int argc);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /* Struct for addargs */
1107c478bd9Sstevel@tonic-gate arglist args;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /* Time a transfer started. */
1137c478bd9Sstevel@tonic-gate static struct timeval start;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate /* Number of bytes of current file transferred so far. */
1167c478bd9Sstevel@tonic-gate volatile off_t statbytes;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /* Total size of current file. */
1197c478bd9Sstevel@tonic-gate off_t totalbytes = 0;
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /* Name of current file being transferred. */
1227c478bd9Sstevel@tonic-gate char *curfile;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /* This is set to non-zero to enable verbose mode. */
1257c478bd9Sstevel@tonic-gate int verbose_mode = 0;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate /* This is set to zero if the progressmeter is not desired. */
1287c478bd9Sstevel@tonic-gate int showprogress = 1;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate /* This is the program to execute for the secured connection. ("ssh" or -S) */
1317c478bd9Sstevel@tonic-gate char *ssh_program = _PATH_SSH_PROGRAM;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /* This is used to store the pid of ssh_program */
134a7e50fb1Sjp161948 static pid_t do_cmd_pid = -1;
135a7e50fb1Sjp161948
136a7e50fb1Sjp161948 static void
killchild(int signo)137a7e50fb1Sjp161948 killchild(int signo)
138a7e50fb1Sjp161948 {
139a7e50fb1Sjp161948 if (do_cmd_pid > 1) {
140a7e50fb1Sjp161948 kill(do_cmd_pid, signo ? signo : SIGTERM);
141a7e50fb1Sjp161948 waitpid(do_cmd_pid, NULL, 0);
142a7e50fb1Sjp161948 }
143a7e50fb1Sjp161948
144a7e50fb1Sjp161948 if (signo)
145a7e50fb1Sjp161948 _exit(1);
146a7e50fb1Sjp161948 exit(1);
147a7e50fb1Sjp161948 }
148a7e50fb1Sjp161948
149a7e50fb1Sjp161948 /*
150a7e50fb1Sjp161948 * Run a command via fork(2)/exec(2). This can be a local-to-local copy via
151a7e50fb1Sjp161948 * cp(1) or one side of a remote-to-remote copy. We must not use system(3) here
152a7e50fb1Sjp161948 * because we don't want filenames to go through a command expansion in the
153a7e50fb1Sjp161948 * underlying shell. Note that the user can create a filename that is a piece of
154a7e50fb1Sjp161948 * shell code itself and this must not be executed.
155a7e50fb1Sjp161948 */
156a7e50fb1Sjp161948 static int
do_local_cmd(arglist * a)157a7e50fb1Sjp161948 do_local_cmd(arglist *a)
158a7e50fb1Sjp161948 {
159a7e50fb1Sjp161948 uint_t i;
160a7e50fb1Sjp161948 int status;
161a7e50fb1Sjp161948 pid_t pid;
162a7e50fb1Sjp161948
163a7e50fb1Sjp161948 if (a->num == 0)
164a7e50fb1Sjp161948 fatal("do_local_cmd: no arguments");
165a7e50fb1Sjp161948
166a7e50fb1Sjp161948 if (verbose_mode) {
167a7e50fb1Sjp161948 fprintf(stderr, gettext("Executing:"));
168a7e50fb1Sjp161948 for (i = 0; i < a->num; i++)
169a7e50fb1Sjp161948 fprintf(stderr, " %s", a->list[i]);
170a7e50fb1Sjp161948 fprintf(stderr, "\n");
171a7e50fb1Sjp161948 }
172a7e50fb1Sjp161948 if ((pid = fork()) == -1)
173a7e50fb1Sjp161948 fatal("do_local_cmd: fork: %s", strerror(errno));
174a7e50fb1Sjp161948
175a7e50fb1Sjp161948 if (pid == 0) {
176a7e50fb1Sjp161948 execvp(a->list[0], a->list);
177a7e50fb1Sjp161948 perror(a->list[0]);
178a7e50fb1Sjp161948 exit(1);
179a7e50fb1Sjp161948 }
180a7e50fb1Sjp161948
181a7e50fb1Sjp161948 do_cmd_pid = pid;
182a7e50fb1Sjp161948 signal(SIGTERM, killchild);
183a7e50fb1Sjp161948 signal(SIGINT, killchild);
184a7e50fb1Sjp161948 signal(SIGHUP, killchild);
185a7e50fb1Sjp161948
186a7e50fb1Sjp161948 while (waitpid(pid, &status, 0) == -1)
187a7e50fb1Sjp161948 if (errno != EINTR)
188a7e50fb1Sjp161948 fatal("do_local_cmd: waitpid: %s", strerror(errno));
189a7e50fb1Sjp161948
190a7e50fb1Sjp161948 do_cmd_pid = -1;
191a7e50fb1Sjp161948
192a7e50fb1Sjp161948 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
193a7e50fb1Sjp161948 return (-1);
194a7e50fb1Sjp161948
195a7e50fb1Sjp161948 return (0);
196a7e50fb1Sjp161948 }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * This function executes the given command as the specified user on the
2007c478bd9Sstevel@tonic-gate * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
2017c478bd9Sstevel@tonic-gate * assigns the input and output file descriptors on success.
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate int
do_cmd(char * host,char * remuser,char * cmd,int * fdin,int * fdout,int argc)2057c478bd9Sstevel@tonic-gate do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate int pin[2], pout[2], reserved[2];
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (verbose_mode)
2107c478bd9Sstevel@tonic-gate fprintf(stderr,
2117c478bd9Sstevel@tonic-gate gettext("Executing: program %s host %s, "
2127c478bd9Sstevel@tonic-gate "user %s, command %s\n"),
2137c478bd9Sstevel@tonic-gate ssh_program, host,
2147c478bd9Sstevel@tonic-gate remuser ? remuser : gettext("(unspecified)"), cmd);
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * Reserve two descriptors so that the real pipes won't get
2187c478bd9Sstevel@tonic-gate * descriptors 0 and 1 because that will screw up dup2 below.
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate pipe(reserved);
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate /* Create a socket pair for communicating with ssh. */
2237c478bd9Sstevel@tonic-gate if (pipe(pin) < 0)
2247c478bd9Sstevel@tonic-gate fatal("pipe: %s", strerror(errno));
2257c478bd9Sstevel@tonic-gate if (pipe(pout) < 0)
2267c478bd9Sstevel@tonic-gate fatal("pipe: %s", strerror(errno));
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate /* Free the reserved descriptors. */
2297c478bd9Sstevel@tonic-gate close(reserved[0]);
2307c478bd9Sstevel@tonic-gate close(reserved[1]);
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /* For a child to execute the command on the remote host using ssh. */
2337c478bd9Sstevel@tonic-gate if ((do_cmd_pid = fork()) == 0) {
2347c478bd9Sstevel@tonic-gate /* Child. */
2357c478bd9Sstevel@tonic-gate close(pin[1]);
2367c478bd9Sstevel@tonic-gate close(pout[0]);
2377c478bd9Sstevel@tonic-gate dup2(pin[0], 0);
2387c478bd9Sstevel@tonic-gate dup2(pout[1], 1);
2397c478bd9Sstevel@tonic-gate close(pin[0]);
2407c478bd9Sstevel@tonic-gate close(pout[1]);
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate args.list[0] = ssh_program;
2437c478bd9Sstevel@tonic-gate if (remuser != NULL)
2447c478bd9Sstevel@tonic-gate addargs(&args, "-l%s", remuser);
2457c478bd9Sstevel@tonic-gate addargs(&args, "%s", host);
2467c478bd9Sstevel@tonic-gate addargs(&args, "%s", cmd);
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate execvp(ssh_program, args.list);
2497c478bd9Sstevel@tonic-gate perror(ssh_program);
2507c478bd9Sstevel@tonic-gate exit(1);
2517c478bd9Sstevel@tonic-gate } else if (do_cmd_pid == (pid_t)-1) {
2527c478bd9Sstevel@tonic-gate /* fork() failed */
2537c478bd9Sstevel@tonic-gate fatal("fork: %s", strerror(errno));
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate /* Parent. Close the other side, and return the local side. */
2577c478bd9Sstevel@tonic-gate close(pin[0]);
2587c478bd9Sstevel@tonic-gate *fdout = pin[1];
2597c478bd9Sstevel@tonic-gate close(pout[1]);
2607c478bd9Sstevel@tonic-gate *fdin = pout[0];
2617c478bd9Sstevel@tonic-gate return (0);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate typedef struct {
2657c478bd9Sstevel@tonic-gate int cnt;
2667c478bd9Sstevel@tonic-gate char *buf;
2677c478bd9Sstevel@tonic-gate } BUF;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate BUF *allocbuf(BUF *, int, int);
2707c478bd9Sstevel@tonic-gate void lostconn(int);
2717c478bd9Sstevel@tonic-gate void nospace(void);
2727c478bd9Sstevel@tonic-gate int okname(char *);
2737c478bd9Sstevel@tonic-gate void run_err(const char *, ...);
2747c478bd9Sstevel@tonic-gate void verifydir(char *);
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate struct passwd *pwd;
2777c478bd9Sstevel@tonic-gate uid_t userid;
2787c478bd9Sstevel@tonic-gate int errs, remin, remout;
2797c478bd9Sstevel@tonic-gate int pflag, iamremote, iamrecursive, targetshouldbedirectory;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate #define CMDNEEDS 64
2827c478bd9Sstevel@tonic-gate char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate int response(void);
2857c478bd9Sstevel@tonic-gate void rsource(char *, struct stat *);
2867c478bd9Sstevel@tonic-gate void sink(int, char *[]);
2877c478bd9Sstevel@tonic-gate void source(int, char *[]);
2887c478bd9Sstevel@tonic-gate void tolocal(int, char *[]);
2897c478bd9Sstevel@tonic-gate void toremote(char *, int, char *[]);
2907c478bd9Sstevel@tonic-gate void usage(void);
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate int
main(argc,argv)2937c478bd9Sstevel@tonic-gate main(argc, argv)
2947c478bd9Sstevel@tonic-gate int argc;
2957c478bd9Sstevel@tonic-gate char *argv[];
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate int ch, fflag, tflag, status;
2987c478bd9Sstevel@tonic-gate char *targ;
2997c478bd9Sstevel@tonic-gate extern char *optarg;
3007c478bd9Sstevel@tonic-gate extern int optind;
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate __progname = get_progname(argv[0]);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate g11n_setlocale(LC_ALL, "");
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate args.list = NULL;
3077c478bd9Sstevel@tonic-gate addargs(&args, "ssh"); /* overwritten with ssh_program */
3087c478bd9Sstevel@tonic-gate addargs(&args, "-x");
3097c478bd9Sstevel@tonic-gate addargs(&args, "-oForwardAgent no");
3107c478bd9Sstevel@tonic-gate addargs(&args, "-oClearAllForwardings yes");
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate fflag = tflag = 0;
3137c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
3147c478bd9Sstevel@tonic-gate switch (ch) {
3157c478bd9Sstevel@tonic-gate /* User-visible flags. */
3167c478bd9Sstevel@tonic-gate case '4':
3177c478bd9Sstevel@tonic-gate case '6':
3187c478bd9Sstevel@tonic-gate case 'C':
3197c478bd9Sstevel@tonic-gate addargs(&args, "-%c", ch);
3207c478bd9Sstevel@tonic-gate break;
3217c478bd9Sstevel@tonic-gate case 'o':
3227c478bd9Sstevel@tonic-gate case 'c':
3237c478bd9Sstevel@tonic-gate case 'i':
3247c478bd9Sstevel@tonic-gate case 'F':
3257c478bd9Sstevel@tonic-gate addargs(&args, "-%c%s", ch, optarg);
3267c478bd9Sstevel@tonic-gate break;
3277c478bd9Sstevel@tonic-gate case 'P':
3287c478bd9Sstevel@tonic-gate addargs(&args, "-p%s", optarg);
3297c478bd9Sstevel@tonic-gate break;
3307c478bd9Sstevel@tonic-gate case 'B':
3317c478bd9Sstevel@tonic-gate addargs(&args, "-oBatchmode yes");
3327c478bd9Sstevel@tonic-gate break;
3337c478bd9Sstevel@tonic-gate case 'p':
3347c478bd9Sstevel@tonic-gate pflag = 1;
3357c478bd9Sstevel@tonic-gate break;
3367c478bd9Sstevel@tonic-gate case 'r':
3377c478bd9Sstevel@tonic-gate iamrecursive = 1;
3387c478bd9Sstevel@tonic-gate break;
3397c478bd9Sstevel@tonic-gate case 'S':
3407c478bd9Sstevel@tonic-gate ssh_program = xstrdup(optarg);
3417c478bd9Sstevel@tonic-gate break;
3427c478bd9Sstevel@tonic-gate case 'v':
3437c478bd9Sstevel@tonic-gate addargs(&args, "-v");
3447c478bd9Sstevel@tonic-gate verbose_mode = 1;
3457c478bd9Sstevel@tonic-gate break;
3467c478bd9Sstevel@tonic-gate case 'q':
3477c478bd9Sstevel@tonic-gate showprogress = 0;
3487c478bd9Sstevel@tonic-gate break;
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate /* Server options. */
3517c478bd9Sstevel@tonic-gate case 'd':
3527c478bd9Sstevel@tonic-gate targetshouldbedirectory = 1;
3537c478bd9Sstevel@tonic-gate break;
3547c478bd9Sstevel@tonic-gate case 'f': /* "from" */
3557c478bd9Sstevel@tonic-gate iamremote = 1;
3567c478bd9Sstevel@tonic-gate fflag = 1;
3577c478bd9Sstevel@tonic-gate break;
3587c478bd9Sstevel@tonic-gate case 't': /* "to" */
3597c478bd9Sstevel@tonic-gate iamremote = 1;
3607c478bd9Sstevel@tonic-gate tflag = 1;
3617c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
3627c478bd9Sstevel@tonic-gate setmode(0, O_BINARY);
3637c478bd9Sstevel@tonic-gate #endif
3647c478bd9Sstevel@tonic-gate break;
3657c478bd9Sstevel@tonic-gate default:
3667c478bd9Sstevel@tonic-gate usage();
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate argc -= optind;
3697c478bd9Sstevel@tonic-gate argv += optind;
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate if ((pwd = getpwuid(userid = getuid())) == NULL)
3727c478bd9Sstevel@tonic-gate fatal("unknown user %d", (int)userid);
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate if (!isatty(STDERR_FILENO))
3757c478bd9Sstevel@tonic-gate showprogress = 0;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate remin = STDIN_FILENO;
3787c478bd9Sstevel@tonic-gate remout = STDOUT_FILENO;
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate if (fflag) {
3817c478bd9Sstevel@tonic-gate /* Follow "protocol", send data. */
3827c478bd9Sstevel@tonic-gate (void) response();
3837c478bd9Sstevel@tonic-gate source(argc, argv);
3847c478bd9Sstevel@tonic-gate exit(errs != 0);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate if (tflag) {
3877c478bd9Sstevel@tonic-gate /* Receive data. */
3887c478bd9Sstevel@tonic-gate sink(argc, argv);
3897c478bd9Sstevel@tonic-gate exit(errs != 0);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate if (argc < 2)
3927c478bd9Sstevel@tonic-gate usage();
3937c478bd9Sstevel@tonic-gate if (argc > 2)
3947c478bd9Sstevel@tonic-gate targetshouldbedirectory = 1;
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate remin = remout = -1;
3977c478bd9Sstevel@tonic-gate do_cmd_pid = (pid_t)-1;
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate /* Command to be executed on remote system using "ssh". */
4007c478bd9Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "scp%s%s%s%s",
4017c478bd9Sstevel@tonic-gate verbose_mode ? " -v" : "",
4027c478bd9Sstevel@tonic-gate iamrecursive ? " -r" : "", pflag ? " -p" : "",
4037c478bd9Sstevel@tonic-gate targetshouldbedirectory ? " -d" : "");
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, lostconn);
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
4087c478bd9Sstevel@tonic-gate toremote(targ, argc, argv);
4097c478bd9Sstevel@tonic-gate else {
4107c478bd9Sstevel@tonic-gate if (targetshouldbedirectory)
4117c478bd9Sstevel@tonic-gate verifydir(argv[argc - 1]);
41226ba1984Sjp161948 tolocal(argc, argv); /* Dest is local host. */
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate * Finally check the exit status of the ssh process, if one was forked
4167c478bd9Sstevel@tonic-gate * and no error has occurred yet
4177c478bd9Sstevel@tonic-gate */
4187c478bd9Sstevel@tonic-gate if (do_cmd_pid != (pid_t)-1 && errs == 0) {
4197c478bd9Sstevel@tonic-gate if (remin != -1) {
4207c478bd9Sstevel@tonic-gate (void) close(remin);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate if (remout != -1) {
4237c478bd9Sstevel@tonic-gate (void) close(remout);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate if (waitpid(do_cmd_pid, &status, 0) == -1) {
4267c478bd9Sstevel@tonic-gate errs = 1;
4277c478bd9Sstevel@tonic-gate } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
4287c478bd9Sstevel@tonic-gate errs = 1;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate return (errs != 0);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate void
toremote(targ,argc,argv)4367c478bd9Sstevel@tonic-gate toremote(targ, argc, argv)
4377c478bd9Sstevel@tonic-gate char *targ, *argv[];
4387c478bd9Sstevel@tonic-gate int argc;
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate int i, len;
441a7e50fb1Sjp161948 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
442a7e50fb1Sjp161948 arglist alist;
443a7e50fb1Sjp161948
444a7e50fb1Sjp161948 memset(&alist, '\0', sizeof (alist));
445a7e50fb1Sjp161948 alist.list = NULL;
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate *targ++ = 0;
4487c478bd9Sstevel@tonic-gate if (*targ == 0)
4497c478bd9Sstevel@tonic-gate targ = ".";
4507c478bd9Sstevel@tonic-gate
451a7e50fb1Sjp161948 arg = xstrdup(argv[argc - 1]);
452a7e50fb1Sjp161948 if ((thost = strchr(arg, '@'))) {
4537c478bd9Sstevel@tonic-gate /* user@host */
4547c478bd9Sstevel@tonic-gate *thost++ = 0;
455a7e50fb1Sjp161948 tuser = arg;
4567c478bd9Sstevel@tonic-gate if (*tuser == '\0')
4577c478bd9Sstevel@tonic-gate tuser = NULL;
4587c478bd9Sstevel@tonic-gate else if (!okname(tuser))
4597c478bd9Sstevel@tonic-gate exit(1);
4607c478bd9Sstevel@tonic-gate } else {
461a7e50fb1Sjp161948 thost = arg;
4627c478bd9Sstevel@tonic-gate tuser = NULL;
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
465a7e50fb1Sjp161948 if (tuser != NULL && !okname(tuser)) {
466a7e50fb1Sjp161948 xfree(arg);
467a7e50fb1Sjp161948 return;
468a7e50fb1Sjp161948 }
469a7e50fb1Sjp161948
4707c478bd9Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) {
4717c478bd9Sstevel@tonic-gate src = colon(argv[i]);
4727c478bd9Sstevel@tonic-gate if (src) { /* remote to remote */
473a7e50fb1Sjp161948 freeargs(&alist);
474a7e50fb1Sjp161948 addargs(&alist, "%s", ssh_program);
475a7e50fb1Sjp161948 if (verbose_mode)
476a7e50fb1Sjp161948 addargs(&alist, "-v");
477a7e50fb1Sjp161948 addargs(&alist, "-x");
478a7e50fb1Sjp161948 addargs(&alist, "-oClearAllForwardings yes");
479a7e50fb1Sjp161948 addargs(&alist, "-n");
480a7e50fb1Sjp161948
4817c478bd9Sstevel@tonic-gate *src++ = 0;
4827c478bd9Sstevel@tonic-gate if (*src == 0)
4837c478bd9Sstevel@tonic-gate src = ".";
4847c478bd9Sstevel@tonic-gate host = strchr(argv[i], '@');
485a7e50fb1Sjp161948
4867c478bd9Sstevel@tonic-gate if (host) {
4877c478bd9Sstevel@tonic-gate *host++ = 0;
4887c478bd9Sstevel@tonic-gate host = cleanhostname(host);
4897c478bd9Sstevel@tonic-gate suser = argv[i];
4907c478bd9Sstevel@tonic-gate if (*suser == '\0')
4917c478bd9Sstevel@tonic-gate suser = pwd->pw_name;
4927c478bd9Sstevel@tonic-gate else if (!okname(suser))
4937c478bd9Sstevel@tonic-gate continue;
494a7e50fb1Sjp161948 addargs(&alist, "-l");
495a7e50fb1Sjp161948 addargs(&alist, "%s", suser);
4967c478bd9Sstevel@tonic-gate } else {
4977c478bd9Sstevel@tonic-gate host = cleanhostname(argv[i]);
498a7e50fb1Sjp161948 }
499a7e50fb1Sjp161948 addargs(&alist, "%s", host);
500a7e50fb1Sjp161948 addargs(&alist, "%s", cmd);
501a7e50fb1Sjp161948 addargs(&alist, "%s", src);
502a7e50fb1Sjp161948 addargs(&alist, "%s%s%s:%s",
5037c478bd9Sstevel@tonic-gate tuser ? tuser : "", tuser ? "@" : "",
5047c478bd9Sstevel@tonic-gate thost, targ);
505a7e50fb1Sjp161948 if (do_local_cmd(&alist) != 0)
506a7e50fb1Sjp161948 errs = 1;
5077c478bd9Sstevel@tonic-gate } else { /* local to remote */
5087c478bd9Sstevel@tonic-gate if (remin == -1) {
5097c478bd9Sstevel@tonic-gate len = strlen(targ) + CMDNEEDS + 20;
5107c478bd9Sstevel@tonic-gate bp = xmalloc(len);
5117c478bd9Sstevel@tonic-gate (void) snprintf(bp, len, "%s -t %s", cmd, targ);
5127c478bd9Sstevel@tonic-gate host = cleanhostname(thost);
5137c478bd9Sstevel@tonic-gate if (do_cmd(host, tuser, bp, &remin,
5147c478bd9Sstevel@tonic-gate &remout, argc) < 0)
5157c478bd9Sstevel@tonic-gate exit(1);
5167c478bd9Sstevel@tonic-gate if (response() < 0)
5177c478bd9Sstevel@tonic-gate exit(1);
5187c478bd9Sstevel@tonic-gate (void) xfree(bp);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate source(1, argv + i);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate void
tolocal(argc,argv)5267c478bd9Sstevel@tonic-gate tolocal(argc, argv)
5277c478bd9Sstevel@tonic-gate int argc;
5287c478bd9Sstevel@tonic-gate char *argv[];
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate int i, len;
5317c478bd9Sstevel@tonic-gate char *bp, *host, *src, *suser;
532a7e50fb1Sjp161948 arglist alist;
533a7e50fb1Sjp161948
534a7e50fb1Sjp161948 memset(&alist, '\0', sizeof (alist));
535a7e50fb1Sjp161948 alist.list = NULL;
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) {
5387c478bd9Sstevel@tonic-gate if (!(src = colon(argv[i]))) { /* Local to local. */
539a7e50fb1Sjp161948 freeargs(&alist);
540a7e50fb1Sjp161948 addargs(&alist, "%s", _PATH_CP);
541a7e50fb1Sjp161948 if (iamrecursive)
542a7e50fb1Sjp161948 addargs(&alist, "-r");
543a7e50fb1Sjp161948 if (pflag)
544a7e50fb1Sjp161948 addargs(&alist, "-p");
545a7e50fb1Sjp161948 addargs(&alist, "%s", argv[i]);
546a7e50fb1Sjp161948 addargs(&alist, "%s", argv[argc-1]);
547a7e50fb1Sjp161948 if (do_local_cmd(&alist))
5487c478bd9Sstevel@tonic-gate ++errs;
5497c478bd9Sstevel@tonic-gate continue;
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate *src++ = 0;
5527c478bd9Sstevel@tonic-gate if (*src == 0)
5537c478bd9Sstevel@tonic-gate src = ".";
5547c478bd9Sstevel@tonic-gate if ((host = strchr(argv[i], '@')) == NULL) {
5557c478bd9Sstevel@tonic-gate host = argv[i];
5567c478bd9Sstevel@tonic-gate suser = NULL;
5577c478bd9Sstevel@tonic-gate } else {
5587c478bd9Sstevel@tonic-gate *host++ = 0;
5597c478bd9Sstevel@tonic-gate suser = argv[i];
5607c478bd9Sstevel@tonic-gate if (*suser == '\0')
5617c478bd9Sstevel@tonic-gate suser = pwd->pw_name;
5627c478bd9Sstevel@tonic-gate else if (!okname(suser))
5637c478bd9Sstevel@tonic-gate continue;
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate host = cleanhostname(host);
5667c478bd9Sstevel@tonic-gate len = strlen(src) + CMDNEEDS + 20;
5677c478bd9Sstevel@tonic-gate bp = xmalloc(len);
5687c478bd9Sstevel@tonic-gate (void) snprintf(bp, len, "%s -f %s", cmd, src);
5697c478bd9Sstevel@tonic-gate if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
5707c478bd9Sstevel@tonic-gate (void) xfree(bp);
5717c478bd9Sstevel@tonic-gate ++errs;
5727c478bd9Sstevel@tonic-gate continue;
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate xfree(bp);
5757c478bd9Sstevel@tonic-gate sink(1, argv + argc - 1);
5767c478bd9Sstevel@tonic-gate (void) close(remin);
5777c478bd9Sstevel@tonic-gate remin = remout = -1;
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate void
source(argc,argv)5827c478bd9Sstevel@tonic-gate source(argc, argv)
5837c478bd9Sstevel@tonic-gate int argc;
5847c478bd9Sstevel@tonic-gate char *argv[];
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate struct stat stb;
5877c478bd9Sstevel@tonic-gate static BUF buffer;
5887c478bd9Sstevel@tonic-gate BUF *bp;
5897c478bd9Sstevel@tonic-gate off_t i, amt, result;
5907c478bd9Sstevel@tonic-gate int fd, haderr, indx;
5917c478bd9Sstevel@tonic-gate char *last, *name, buf[2048];
5927c478bd9Sstevel@tonic-gate int len;
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate for (indx = 0; indx < argc; ++indx) {
5957c478bd9Sstevel@tonic-gate name = argv[indx];
5967c478bd9Sstevel@tonic-gate statbytes = 0;
5977c478bd9Sstevel@tonic-gate len = strlen(name);
5987c478bd9Sstevel@tonic-gate while (len > 1 && name[len-1] == '/')
5997c478bd9Sstevel@tonic-gate name[--len] = '\0';
6007c478bd9Sstevel@tonic-gate if (strchr(name, '\n') != NULL) {
6017c478bd9Sstevel@tonic-gate run_err("%s: skipping, filename contains a newline",
6027c478bd9Sstevel@tonic-gate name);
6037c478bd9Sstevel@tonic-gate goto next;
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate if ((fd = open(name, O_RDONLY, 0)) < 0)
6067c478bd9Sstevel@tonic-gate goto syserr;
6077c478bd9Sstevel@tonic-gate if (fstat(fd, &stb) < 0) {
6087c478bd9Sstevel@tonic-gate syserr: run_err("%s: %s", name, strerror(errno));
6097c478bd9Sstevel@tonic-gate goto next;
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate switch (stb.st_mode & S_IFMT) {
6127c478bd9Sstevel@tonic-gate case S_IFREG:
6137c478bd9Sstevel@tonic-gate break;
6147c478bd9Sstevel@tonic-gate case S_IFDIR:
6157c478bd9Sstevel@tonic-gate if (iamrecursive) {
6167c478bd9Sstevel@tonic-gate rsource(name, &stb);
6177c478bd9Sstevel@tonic-gate goto next;
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
6207c478bd9Sstevel@tonic-gate default:
6217c478bd9Sstevel@tonic-gate run_err("%s: not a regular file", name);
6227c478bd9Sstevel@tonic-gate goto next;
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate if ((last = strrchr(name, '/')) == NULL)
6257c478bd9Sstevel@tonic-gate last = name;
6267c478bd9Sstevel@tonic-gate else
6277c478bd9Sstevel@tonic-gate ++last;
6287c478bd9Sstevel@tonic-gate curfile = last;
6297c478bd9Sstevel@tonic-gate if (pflag) {
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate * Make it compatible with possible future
6327c478bd9Sstevel@tonic-gate * versions expecting microseconds.
6337c478bd9Sstevel@tonic-gate */
6347c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "T%lu 0 %lu 0\n",
6357c478bd9Sstevel@tonic-gate (ulong_t)stb.st_mtime,
6367c478bd9Sstevel@tonic-gate (ulong_t)stb.st_atime);
6377c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, buf, strlen(buf));
6387c478bd9Sstevel@tonic-gate if (response() < 0)
6397c478bd9Sstevel@tonic-gate goto next;
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
6427c478bd9Sstevel@tonic-gate #ifdef HAVE_LONG_LONG_INT
6437c478bd9Sstevel@tonic-gate snprintf(buf, sizeof (buf), "C%04o %lld %s\n",
6447c478bd9Sstevel@tonic-gate (uint_t)(stb.st_mode & FILEMODEMASK),
6457c478bd9Sstevel@tonic-gate (long long)stb.st_size, last);
6467c478bd9Sstevel@tonic-gate #else
6477c478bd9Sstevel@tonic-gate /* XXX: Handle integer overflow? */
6487c478bd9Sstevel@tonic-gate snprintf(buf, sizeof (buf), "C%04o %lu %s\n",
6497c478bd9Sstevel@tonic-gate (uint_t)(stb.st_mode & FILEMODEMASK),
6507c478bd9Sstevel@tonic-gate (ulong_t)stb.st_size, last);
6517c478bd9Sstevel@tonic-gate #endif
6527c478bd9Sstevel@tonic-gate if (verbose_mode) {
6537c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Sending file modes: %s"), buf);
6547c478bd9Sstevel@tonic-gate fflush(stderr);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, buf, strlen(buf));
6577c478bd9Sstevel@tonic-gate if (response() < 0)
6587c478bd9Sstevel@tonic-gate goto next;
6597c478bd9Sstevel@tonic-gate if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
6607c478bd9Sstevel@tonic-gate next: (void) close(fd);
6617c478bd9Sstevel@tonic-gate continue;
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate if (showprogress) {
6647c478bd9Sstevel@tonic-gate totalbytes = stb.st_size;
6657c478bd9Sstevel@tonic-gate progressmeter(-1);
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate /* Keep writing after an error so that we stay sync'd up. */
6687c478bd9Sstevel@tonic-gate for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
6697c478bd9Sstevel@tonic-gate amt = bp->cnt;
6707c478bd9Sstevel@tonic-gate if (i + amt > stb.st_size)
6717c478bd9Sstevel@tonic-gate amt = stb.st_size - i;
6727c478bd9Sstevel@tonic-gate if (!haderr) {
6737c478bd9Sstevel@tonic-gate result = atomicio(read, fd, bp->buf, amt);
6747c478bd9Sstevel@tonic-gate if (result != amt)
6757c478bd9Sstevel@tonic-gate haderr = result >= 0 ? EIO : errno;
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate if (haderr)
6787c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, bp->buf, amt);
6797c478bd9Sstevel@tonic-gate else {
6807c478bd9Sstevel@tonic-gate result = atomicio(write, remout, bp->buf, amt);
6817c478bd9Sstevel@tonic-gate if (result != amt)
6827c478bd9Sstevel@tonic-gate haderr = result >= 0 ? EIO : errno;
6837c478bd9Sstevel@tonic-gate statbytes += result;
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate if (showprogress)
6877c478bd9Sstevel@tonic-gate progressmeter(1);
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate if (close(fd) < 0 && !haderr)
6907c478bd9Sstevel@tonic-gate haderr = errno;
6917c478bd9Sstevel@tonic-gate if (!haderr)
6927c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
6937c478bd9Sstevel@tonic-gate else
6947c478bd9Sstevel@tonic-gate run_err("%s: %s", name, strerror(haderr));
6957c478bd9Sstevel@tonic-gate (void) response();
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate void
rsource(name,statp)7007c478bd9Sstevel@tonic-gate rsource(name, statp)
7017c478bd9Sstevel@tonic-gate char *name;
7027c478bd9Sstevel@tonic-gate struct stat *statp;
7037c478bd9Sstevel@tonic-gate {
7047c478bd9Sstevel@tonic-gate DIR *dirp;
7057c478bd9Sstevel@tonic-gate struct dirent *dp;
7067c478bd9Sstevel@tonic-gate char *last, *vect[1], path[1100];
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate if (!(dirp = opendir(name))) {
7097c478bd9Sstevel@tonic-gate run_err("%s: %s", name, strerror(errno));
7107c478bd9Sstevel@tonic-gate return;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate last = strrchr(name, '/');
7137c478bd9Sstevel@tonic-gate if (last == 0)
7147c478bd9Sstevel@tonic-gate last = name;
7157c478bd9Sstevel@tonic-gate else
7167c478bd9Sstevel@tonic-gate last++;
7177c478bd9Sstevel@tonic-gate if (pflag) {
7187c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "T%lu 0 %lu 0\n",
7197c478bd9Sstevel@tonic-gate (ulong_t)statp->st_mtime,
7207c478bd9Sstevel@tonic-gate (ulong_t)statp->st_atime);
7217c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, path, strlen(path));
7227c478bd9Sstevel@tonic-gate if (response() < 0) {
7237c478bd9Sstevel@tonic-gate closedir(dirp);
7247c478bd9Sstevel@tonic-gate return;
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "D%04o %d %.1024s\n",
7287c478bd9Sstevel@tonic-gate (uint_t)(statp->st_mode & FILEMODEMASK), 0, last);
7297c478bd9Sstevel@tonic-gate if (verbose_mode)
7307c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Entering directory: %s"), path);
7317c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, path, strlen(path));
7327c478bd9Sstevel@tonic-gate if (response() < 0) {
7337c478bd9Sstevel@tonic-gate closedir(dirp);
7347c478bd9Sstevel@tonic-gate return;
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
7377c478bd9Sstevel@tonic-gate if (dp->d_ino == 0)
7387c478bd9Sstevel@tonic-gate continue;
7397c478bd9Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) ||
7407c478bd9Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0))
7417c478bd9Sstevel@tonic-gate continue;
7427c478bd9Sstevel@tonic-gate if (strlen(name) + 1 + strlen(dp->d_name) >=
7437c478bd9Sstevel@tonic-gate sizeof (path) - 1) {
7447c478bd9Sstevel@tonic-gate run_err("%s/%s: name too long", name, dp->d_name);
7457c478bd9Sstevel@tonic-gate continue;
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s/%s", name, dp->d_name);
7487c478bd9Sstevel@tonic-gate vect[0] = path;
7497c478bd9Sstevel@tonic-gate source(1, vect);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate (void) closedir(dirp);
7527c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "E\n", 2);
7537c478bd9Sstevel@tonic-gate (void) response();
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate void
sink(argc,argv)7577c478bd9Sstevel@tonic-gate sink(argc, argv)
7587c478bd9Sstevel@tonic-gate int argc;
7597c478bd9Sstevel@tonic-gate char *argv[];
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate static BUF buffer;
7627c478bd9Sstevel@tonic-gate struct stat stb;
7637c478bd9Sstevel@tonic-gate enum {
7647c478bd9Sstevel@tonic-gate YES, NO, DISPLAYED
7657c478bd9Sstevel@tonic-gate } wrerr;
7667c478bd9Sstevel@tonic-gate BUF *bp;
7677c478bd9Sstevel@tonic-gate off_t i, j;
7687c478bd9Sstevel@tonic-gate int amt, count, exists, first, mask, mode, ofd, omode;
7697c478bd9Sstevel@tonic-gate off_t size;
7707c478bd9Sstevel@tonic-gate int setimes, targisdir, wrerrno = 0;
7717c478bd9Sstevel@tonic-gate char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
7727c478bd9Sstevel@tonic-gate struct timeval tv[2];
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate #define atime tv[0]
7757c478bd9Sstevel@tonic-gate #define mtime tv[1]
7767c478bd9Sstevel@tonic-gate #define SCREWUP(str) { why = str; goto screwup; }
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate setimes = targisdir = 0;
7797c478bd9Sstevel@tonic-gate mask = umask(0);
7807c478bd9Sstevel@tonic-gate if (!pflag)
7817c478bd9Sstevel@tonic-gate (void) umask(mask);
7827c478bd9Sstevel@tonic-gate if (argc != 1) {
7837c478bd9Sstevel@tonic-gate run_err("ambiguous target");
7847c478bd9Sstevel@tonic-gate exit(1);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate targ = *argv;
7877c478bd9Sstevel@tonic-gate if (targetshouldbedirectory)
7887c478bd9Sstevel@tonic-gate verifydir(targ);
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
7917c478bd9Sstevel@tonic-gate if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
7927c478bd9Sstevel@tonic-gate targisdir = 1;
7937c478bd9Sstevel@tonic-gate for (first = 1; ; first = 0) {
7947c478bd9Sstevel@tonic-gate cp = buf;
7957c478bd9Sstevel@tonic-gate if (atomicio(read, remin, cp, 1) <= 0)
7967c478bd9Sstevel@tonic-gate return;
7977c478bd9Sstevel@tonic-gate if (*cp++ == '\n')
7987c478bd9Sstevel@tonic-gate SCREWUP("unexpected <newline>")
7997c478bd9Sstevel@tonic-gate do {
8007c478bd9Sstevel@tonic-gate if (atomicio(read, remin, &ch, sizeof (ch)) !=
8017c478bd9Sstevel@tonic-gate sizeof (ch))
8027c478bd9Sstevel@tonic-gate SCREWUP("lost connection")
8037c478bd9Sstevel@tonic-gate *cp++ = ch;
8047c478bd9Sstevel@tonic-gate } while (cp < &buf[sizeof (buf) - 1] && ch != '\n');
8057c478bd9Sstevel@tonic-gate *cp = 0;
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate if (buf[0] == '\01' || buf[0] == '\02') {
8087c478bd9Sstevel@tonic-gate if (iamremote == 0)
8097c478bd9Sstevel@tonic-gate (void) atomicio(write, STDERR_FILENO,
8107c478bd9Sstevel@tonic-gate buf + 1, strlen(buf + 1));
8117c478bd9Sstevel@tonic-gate if (buf[0] == '\02')
8127c478bd9Sstevel@tonic-gate exit(1);
8137c478bd9Sstevel@tonic-gate ++errs;
8147c478bd9Sstevel@tonic-gate continue;
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate if (buf[0] == 'E') {
8177c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
8187c478bd9Sstevel@tonic-gate return;
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate if (ch == '\n')
8217c478bd9Sstevel@tonic-gate *--cp = 0;
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate cp = buf;
8247c478bd9Sstevel@tonic-gate if (*cp == 'T') {
8257c478bd9Sstevel@tonic-gate setimes++;
8267c478bd9Sstevel@tonic-gate cp++;
8277c478bd9Sstevel@tonic-gate mtime.tv_sec = strtol(cp, &cp, 10);
8287c478bd9Sstevel@tonic-gate if (!cp || *cp++ != ' ')
8297c478bd9Sstevel@tonic-gate SCREWUP("mtime.sec not delimited")
8307c478bd9Sstevel@tonic-gate mtime.tv_usec = strtol(cp, &cp, 10);
8317c478bd9Sstevel@tonic-gate if (!cp || *cp++ != ' ')
8327c478bd9Sstevel@tonic-gate SCREWUP("mtime.usec not delimited")
8337c478bd9Sstevel@tonic-gate atime.tv_sec = strtol(cp, &cp, 10);
8347c478bd9Sstevel@tonic-gate if (!cp || *cp++ != ' ')
8357c478bd9Sstevel@tonic-gate SCREWUP("atime.sec not delimited")
8367c478bd9Sstevel@tonic-gate atime.tv_usec = strtol(cp, &cp, 10);
8377c478bd9Sstevel@tonic-gate if (!cp || *cp++ != '\0')
8387c478bd9Sstevel@tonic-gate SCREWUP("atime.usec not delimited")
8397c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
8407c478bd9Sstevel@tonic-gate continue;
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate if (*cp != 'C' && *cp != 'D') {
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate * Check for the case "rcp remote:foo\* local:bar".
8457c478bd9Sstevel@tonic-gate * In this case, the line "No match." can be returned
8467c478bd9Sstevel@tonic-gate * by the shell before the rcp command on the remote is
8477c478bd9Sstevel@tonic-gate * executed so the ^Aerror_message convention isn't
8487c478bd9Sstevel@tonic-gate * followed.
8497c478bd9Sstevel@tonic-gate */
8507c478bd9Sstevel@tonic-gate if (first) {
8517c478bd9Sstevel@tonic-gate run_err("%s", cp);
8527c478bd9Sstevel@tonic-gate exit(1);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate SCREWUP("expected control record")
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate mode = 0;
8577c478bd9Sstevel@tonic-gate for (++cp; cp < buf + 5; cp++) {
8587c478bd9Sstevel@tonic-gate if (*cp < '0' || *cp > '7')
8597c478bd9Sstevel@tonic-gate SCREWUP("bad mode")
8607c478bd9Sstevel@tonic-gate mode = (mode << 3) | (*cp - '0');
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate if (*cp++ != ' ')
8637c478bd9Sstevel@tonic-gate SCREWUP("mode not delimited")
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate for (size = 0; isdigit(*cp); )
8667c478bd9Sstevel@tonic-gate size = size * 10 + (*cp++ - '0');
8677c478bd9Sstevel@tonic-gate if (*cp++ != ' ')
8687c478bd9Sstevel@tonic-gate SCREWUP("size not delimited")
86926ba1984Sjp161948 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
87026ba1984Sjp161948 run_err("error: unexpected filename: %s", cp);
87126ba1984Sjp161948 exit(1);
87226ba1984Sjp161948 }
8737c478bd9Sstevel@tonic-gate if (targisdir) {
8747c478bd9Sstevel@tonic-gate static char *namebuf;
8757c478bd9Sstevel@tonic-gate static int cursize;
8767c478bd9Sstevel@tonic-gate size_t need;
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate need = strlen(targ) + strlen(cp) + 250;
8797c478bd9Sstevel@tonic-gate if (need > cursize) {
8807c478bd9Sstevel@tonic-gate if (namebuf)
8817c478bd9Sstevel@tonic-gate xfree(namebuf);
8827c478bd9Sstevel@tonic-gate namebuf = xmalloc(need);
8837c478bd9Sstevel@tonic-gate cursize = need;
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate (void) snprintf(namebuf, need, "%s%s%s", targ,
8867c478bd9Sstevel@tonic-gate strcmp(targ, "/") ? "/" : "", cp);
8877c478bd9Sstevel@tonic-gate np = namebuf;
8887c478bd9Sstevel@tonic-gate } else
8897c478bd9Sstevel@tonic-gate np = targ;
8907c478bd9Sstevel@tonic-gate curfile = cp;
8917c478bd9Sstevel@tonic-gate exists = stat(np, &stb) == 0;
8927c478bd9Sstevel@tonic-gate if (buf[0] == 'D') {
8937c478bd9Sstevel@tonic-gate int mod_flag = pflag;
89426ba1984Sjp161948 if (!iamrecursive)
89526ba1984Sjp161948 SCREWUP("received directory without -r");
8967c478bd9Sstevel@tonic-gate if (exists) {
8977c478bd9Sstevel@tonic-gate if (!S_ISDIR(stb.st_mode)) {
8987c478bd9Sstevel@tonic-gate errno = ENOTDIR;
8997c478bd9Sstevel@tonic-gate goto bad;
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate if (pflag)
9027c478bd9Sstevel@tonic-gate (void) chmod(np, mode);
9037c478bd9Sstevel@tonic-gate } else {
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate * Handle copying from a read-only
9067c478bd9Sstevel@tonic-gate * directory
9077c478bd9Sstevel@tonic-gate */
9087c478bd9Sstevel@tonic-gate mod_flag = 1;
9097c478bd9Sstevel@tonic-gate if (mkdir(np, mode | S_IRWXU) < 0)
9107c478bd9Sstevel@tonic-gate goto bad;
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate vect[0] = xstrdup(np);
9137c478bd9Sstevel@tonic-gate sink(1, vect);
9147c478bd9Sstevel@tonic-gate if (setimes) {
9157c478bd9Sstevel@tonic-gate setimes = 0;
9167c478bd9Sstevel@tonic-gate if (utimes(vect[0], tv) < 0)
9177c478bd9Sstevel@tonic-gate run_err("%s: set times: %s",
9187c478bd9Sstevel@tonic-gate vect[0], strerror(errno));
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate if (mod_flag)
9217c478bd9Sstevel@tonic-gate (void) chmod(vect[0], mode);
9227c478bd9Sstevel@tonic-gate if (vect[0])
9237c478bd9Sstevel@tonic-gate xfree(vect[0]);
9247c478bd9Sstevel@tonic-gate continue;
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate omode = mode;
9277c478bd9Sstevel@tonic-gate mode |= S_IWRITE;
9287c478bd9Sstevel@tonic-gate if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
9297c478bd9Sstevel@tonic-gate bad: run_err("%s: %s", np, strerror(errno));
9307c478bd9Sstevel@tonic-gate continue;
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
9337c478bd9Sstevel@tonic-gate if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
9347c478bd9Sstevel@tonic-gate (void) close(ofd);
9357c478bd9Sstevel@tonic-gate continue;
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate wrerr = NO;
9387c478bd9Sstevel@tonic-gate
9397c478bd9Sstevel@tonic-gate if (showprogress) {
9407c478bd9Sstevel@tonic-gate totalbytes = size;
9417c478bd9Sstevel@tonic-gate progressmeter(-1);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate statbytes = 0;
94412fcfd5aSjp161948 for (i = 0; i < size; i += bp->cnt) {
94512fcfd5aSjp161948 amt = bp->cnt;
94612fcfd5aSjp161948 cp = bp->buf;
9477c478bd9Sstevel@tonic-gate if (i + amt > size)
9487c478bd9Sstevel@tonic-gate amt = size - i;
94912fcfd5aSjp161948 count = amt;
9507c478bd9Sstevel@tonic-gate do {
9517c478bd9Sstevel@tonic-gate j = read(remin, cp, amt);
9527c478bd9Sstevel@tonic-gate if (j == -1 && (errno == EINTR ||
9537c478bd9Sstevel@tonic-gate errno == EAGAIN)) {
9547c478bd9Sstevel@tonic-gate continue;
9557c478bd9Sstevel@tonic-gate } else if (j <= 0) {
9567c478bd9Sstevel@tonic-gate run_err("%s", j ? strerror(errno) :
9577c478bd9Sstevel@tonic-gate "dropped connection");
9587c478bd9Sstevel@tonic-gate exit(1);
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate amt -= j;
9617c478bd9Sstevel@tonic-gate cp += j;
9627c478bd9Sstevel@tonic-gate statbytes += j;
9637c478bd9Sstevel@tonic-gate } while (amt > 0);
9647c478bd9Sstevel@tonic-gate /* Keep reading so we stay sync'd up. */
9657c478bd9Sstevel@tonic-gate if (wrerr == NO) {
9667c478bd9Sstevel@tonic-gate j = atomicio(write, ofd, bp->buf,
9677c478bd9Sstevel@tonic-gate count);
9687c478bd9Sstevel@tonic-gate if (j != count) {
9697c478bd9Sstevel@tonic-gate wrerr = YES;
9707c478bd9Sstevel@tonic-gate wrerrno = j >= 0 ? EIO : errno;
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate if (showprogress)
9757c478bd9Sstevel@tonic-gate progressmeter(1);
9767c478bd9Sstevel@tonic-gate if (ftruncate(ofd, size)) {
9777c478bd9Sstevel@tonic-gate run_err("%s: truncate: %s", np, strerror(errno));
9787c478bd9Sstevel@tonic-gate wrerr = DISPLAYED;
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate if (pflag) {
9817c478bd9Sstevel@tonic-gate if (exists || omode != mode)
9827c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
983038a8f41Sjp161948 if (fchmod(ofd, omode)) {
9847c478bd9Sstevel@tonic-gate #else /* HAVE_FCHMOD */
985038a8f41Sjp161948 if (chmod(np, omode)) {
9867c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9877c478bd9Sstevel@tonic-gate run_err("%s: set mode: %s",
9887c478bd9Sstevel@tonic-gate np, strerror(errno));
989038a8f41Sjp161948 wrerr = DISPLAYED;
990038a8f41Sjp161948 }
9917c478bd9Sstevel@tonic-gate } else {
9927c478bd9Sstevel@tonic-gate if (!exists && omode != mode)
9937c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
994038a8f41Sjp161948 if (fchmod(ofd, omode & ~mask)) {
9957c478bd9Sstevel@tonic-gate #else /* HAVE_FCHMOD */
996038a8f41Sjp161948 if (chmod(np, omode & ~mask)) {
9977c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9987c478bd9Sstevel@tonic-gate run_err("%s: set mode: %s",
9997c478bd9Sstevel@tonic-gate np, strerror(errno));
1000038a8f41Sjp161948 wrerr = DISPLAYED;
1001038a8f41Sjp161948 }
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate if (close(ofd) == -1) {
10047c478bd9Sstevel@tonic-gate wrerr = YES;
10057c478bd9Sstevel@tonic-gate wrerrno = errno;
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate (void) response();
10087c478bd9Sstevel@tonic-gate if (setimes && wrerr == NO) {
10097c478bd9Sstevel@tonic-gate setimes = 0;
10107c478bd9Sstevel@tonic-gate if (utimes(np, tv) < 0) {
10117c478bd9Sstevel@tonic-gate run_err("%s: set times: %s",
10127c478bd9Sstevel@tonic-gate np, strerror(errno));
10137c478bd9Sstevel@tonic-gate wrerr = DISPLAYED;
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate switch (wrerr) {
10177c478bd9Sstevel@tonic-gate case YES:
10187c478bd9Sstevel@tonic-gate run_err("%s: %s", np, strerror(wrerrno));
10197c478bd9Sstevel@tonic-gate break;
10207c478bd9Sstevel@tonic-gate case NO:
10217c478bd9Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
10227c478bd9Sstevel@tonic-gate break;
10237c478bd9Sstevel@tonic-gate case DISPLAYED:
10247c478bd9Sstevel@tonic-gate break;
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate screwup:
10287c478bd9Sstevel@tonic-gate run_err("protocol error: %s", why);
10297c478bd9Sstevel@tonic-gate exit(1);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate
10327c478bd9Sstevel@tonic-gate int
response(void)10337c478bd9Sstevel@tonic-gate response(void)
10347c478bd9Sstevel@tonic-gate {
10357c478bd9Sstevel@tonic-gate char ch, *cp, resp, rbuf[2048];
10367c478bd9Sstevel@tonic-gate
10377c478bd9Sstevel@tonic-gate if (atomicio(read, remin, &resp, sizeof (resp)) != sizeof (resp))
10387c478bd9Sstevel@tonic-gate lostconn(0);
10397c478bd9Sstevel@tonic-gate
10407c478bd9Sstevel@tonic-gate cp = rbuf;
10417c478bd9Sstevel@tonic-gate switch (resp) {
10427c478bd9Sstevel@tonic-gate case 0: /* ok */
10437c478bd9Sstevel@tonic-gate return (0);
10447c478bd9Sstevel@tonic-gate default:
10457c478bd9Sstevel@tonic-gate *cp++ = resp;
10467c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
10477c478bd9Sstevel@tonic-gate case 1: /* error, followed by error msg */
10487c478bd9Sstevel@tonic-gate case 2: /* fatal error, "" */
10497c478bd9Sstevel@tonic-gate do {
10507c478bd9Sstevel@tonic-gate if (atomicio(read, remin, &ch, sizeof (ch)) !=
10517c478bd9Sstevel@tonic-gate sizeof (ch))
10527c478bd9Sstevel@tonic-gate lostconn(0);
10537c478bd9Sstevel@tonic-gate *cp++ = ch;
10547c478bd9Sstevel@tonic-gate } while (cp < &rbuf[sizeof (rbuf) - 1] && ch != '\n');
10557c478bd9Sstevel@tonic-gate
10567c478bd9Sstevel@tonic-gate if (!iamremote)
10577c478bd9Sstevel@tonic-gate (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
10587c478bd9Sstevel@tonic-gate ++errs;
10597c478bd9Sstevel@tonic-gate if (resp == 1)
10607c478bd9Sstevel@tonic-gate return (-1);
10617c478bd9Sstevel@tonic-gate exit(1);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate /* NOTREACHED */
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate
10667c478bd9Sstevel@tonic-gate void
usage(void)10677c478bd9Sstevel@tonic-gate usage(void)
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10707c478bd9Sstevel@tonic-gate gettext(
10717c478bd9Sstevel@tonic-gate "Usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
10727c478bd9Sstevel@tonic-gate " [-c cipher] [-i identity] [-o option]\n"
10737c478bd9Sstevel@tonic-gate " [[user@]host1:]file1 [...] "
10747c478bd9Sstevel@tonic-gate "[[user@]host2:]file2\n"));
10757c478bd9Sstevel@tonic-gate exit(1);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate
10787c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
10797c478bd9Sstevel@tonic-gate void
run_err(const char * fmt,...)10807c478bd9Sstevel@tonic-gate run_err(const char *fmt, ...)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate static FILE *fp;
10837c478bd9Sstevel@tonic-gate va_list ap;
10847c478bd9Sstevel@tonic-gate
10857c478bd9Sstevel@tonic-gate ++errs;
108626ba1984Sjp161948
108726ba1984Sjp161948 if (!iamremote) {
108826ba1984Sjp161948 va_start(ap, fmt);
108926ba1984Sjp161948 vfprintf(stderr, fmt, ap);
109026ba1984Sjp161948 va_end(ap);
109126ba1984Sjp161948 fprintf(stderr, "\n");
109226ba1984Sjp161948 }
109326ba1984Sjp161948
10947c478bd9Sstevel@tonic-gate if (fp == NULL && !(fp = fdopen(remout, "w")))
10957c478bd9Sstevel@tonic-gate return;
109626ba1984Sjp161948
10977c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%c", 0x01);
10987c478bd9Sstevel@tonic-gate (void) fprintf(fp, "scp: ");
10997c478bd9Sstevel@tonic-gate va_start(ap, fmt);
11007c478bd9Sstevel@tonic-gate (void) vfprintf(fp, fmt, ap);
11017c478bd9Sstevel@tonic-gate va_end(ap);
11027c478bd9Sstevel@tonic-gate (void) fprintf(fp, "\n");
11037c478bd9Sstevel@tonic-gate (void) fflush(fp);
11047c478bd9Sstevel@tonic-gate
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate void
verifydir(cp)11087c478bd9Sstevel@tonic-gate verifydir(cp)
11097c478bd9Sstevel@tonic-gate char *cp;
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate struct stat stb;
11127c478bd9Sstevel@tonic-gate
11137c478bd9Sstevel@tonic-gate if (!stat(cp, &stb)) {
11147c478bd9Sstevel@tonic-gate if (S_ISDIR(stb.st_mode))
11157c478bd9Sstevel@tonic-gate return;
11167c478bd9Sstevel@tonic-gate errno = ENOTDIR;
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate run_err("%s: %s", cp, strerror(errno));
11197c478bd9Sstevel@tonic-gate exit(1);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate int
okname(cp0)11237c478bd9Sstevel@tonic-gate okname(cp0)
11247c478bd9Sstevel@tonic-gate char *cp0;
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate int c;
11277c478bd9Sstevel@tonic-gate char *cp;
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate cp = cp0;
11307c478bd9Sstevel@tonic-gate do {
11317c478bd9Sstevel@tonic-gate c = (int)*cp;
11327c478bd9Sstevel@tonic-gate if (c & 0200)
11337c478bd9Sstevel@tonic-gate goto bad;
1134*1b94c6efSPravas K Panda if (!isalpha(c) && !isdigit(c)) {
1135*1b94c6efSPravas K Panda switch (c) {
1136*1b94c6efSPravas K Panda case '\'':
1137*1b94c6efSPravas K Panda case '"':
1138*1b94c6efSPravas K Panda case '`':
1139*1b94c6efSPravas K Panda case ' ':
1140*1b94c6efSPravas K Panda case '#':
11417c478bd9Sstevel@tonic-gate goto bad;
1142*1b94c6efSPravas K Panda default:
1143*1b94c6efSPravas K Panda break;
1144*1b94c6efSPravas K Panda }
1145*1b94c6efSPravas K Panda }
11467c478bd9Sstevel@tonic-gate } while (*++cp);
11477c478bd9Sstevel@tonic-gate return (1);
11487c478bd9Sstevel@tonic-gate
11497c478bd9Sstevel@tonic-gate bad: fprintf(stderr, gettext("%s: invalid user name\n"), cp0);
11507c478bd9Sstevel@tonic-gate return (0);
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate
11537c478bd9Sstevel@tonic-gate BUF *
allocbuf(bp,fd,blksize)11547c478bd9Sstevel@tonic-gate allocbuf(bp, fd, blksize)
11557c478bd9Sstevel@tonic-gate BUF *bp;
11567c478bd9Sstevel@tonic-gate int fd, blksize;
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate size_t size;
11597c478bd9Sstevel@tonic-gate #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
11607c478bd9Sstevel@tonic-gate struct stat stb;
11617c478bd9Sstevel@tonic-gate
11627c478bd9Sstevel@tonic-gate if (fstat(fd, &stb) < 0) {
11637c478bd9Sstevel@tonic-gate run_err("fstat: %s", strerror(errno));
11647c478bd9Sstevel@tonic-gate return (0);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate if (stb.st_blksize == 0)
11677c478bd9Sstevel@tonic-gate size = blksize;
11687c478bd9Sstevel@tonic-gate else
11697c478bd9Sstevel@tonic-gate size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
11707c478bd9Sstevel@tonic-gate stb.st_blksize;
11717c478bd9Sstevel@tonic-gate #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
11727c478bd9Sstevel@tonic-gate size = blksize;
11737c478bd9Sstevel@tonic-gate #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
11747c478bd9Sstevel@tonic-gate if (bp->cnt >= size)
11757c478bd9Sstevel@tonic-gate return (bp);
11767c478bd9Sstevel@tonic-gate if (bp->buf == NULL)
11777c478bd9Sstevel@tonic-gate bp->buf = xmalloc(size);
11787c478bd9Sstevel@tonic-gate else
11797c478bd9Sstevel@tonic-gate bp->buf = xrealloc(bp->buf, size);
11807c478bd9Sstevel@tonic-gate memset(bp->buf, 0, size);
11817c478bd9Sstevel@tonic-gate bp->cnt = size;
11827c478bd9Sstevel@tonic-gate return (bp);
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate
11857c478bd9Sstevel@tonic-gate void
lostconn(signo)11867c478bd9Sstevel@tonic-gate lostconn(signo)
11877c478bd9Sstevel@tonic-gate int signo;
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate if (!iamremote)
11907c478bd9Sstevel@tonic-gate write(STDERR_FILENO, "lost connection\n", 16);
11917c478bd9Sstevel@tonic-gate if (signo)
11927c478bd9Sstevel@tonic-gate _exit(1);
11937c478bd9Sstevel@tonic-gate else
11947c478bd9Sstevel@tonic-gate exit(1);
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate
11977c478bd9Sstevel@tonic-gate static void
updateprogressmeter(int ignore)11987c478bd9Sstevel@tonic-gate updateprogressmeter(int ignore)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate int save_errno = errno;
12017c478bd9Sstevel@tonic-gate
12027c478bd9Sstevel@tonic-gate progressmeter(0);
12037c478bd9Sstevel@tonic-gate signal(SIGALRM, updateprogressmeter);
12047c478bd9Sstevel@tonic-gate alarm(PROGRESSTIME);
12057c478bd9Sstevel@tonic-gate errno = save_errno;
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate
12087c478bd9Sstevel@tonic-gate static int
foregroundproc(void)12097c478bd9Sstevel@tonic-gate foregroundproc(void)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate static pid_t pgrp = -1;
12127c478bd9Sstevel@tonic-gate int ctty_pgrp;
12137c478bd9Sstevel@tonic-gate
12147c478bd9Sstevel@tonic-gate if (pgrp == -1)
12157c478bd9Sstevel@tonic-gate pgrp = getpgrp();
12167c478bd9Sstevel@tonic-gate
12177c478bd9Sstevel@tonic-gate #ifdef HAVE_TCGETPGRP
12187c478bd9Sstevel@tonic-gate return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
12197c478bd9Sstevel@tonic-gate ctty_pgrp == pgrp);
12207c478bd9Sstevel@tonic-gate #else
12217c478bd9Sstevel@tonic-gate return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
12227c478bd9Sstevel@tonic-gate ctty_pgrp == pgrp));
12237c478bd9Sstevel@tonic-gate #endif
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate
12267c478bd9Sstevel@tonic-gate void
progressmeter(int flag)12277c478bd9Sstevel@tonic-gate progressmeter(int flag)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate static const char prefixes[] = " KMGTP";
12307c478bd9Sstevel@tonic-gate static struct timeval lastupdate;
12317c478bd9Sstevel@tonic-gate static off_t lastsize;
12327c478bd9Sstevel@tonic-gate struct timeval now, td, wait;
12337c478bd9Sstevel@tonic-gate off_t cursize, abbrevsize;
12347c478bd9Sstevel@tonic-gate double elapsed;
12357c478bd9Sstevel@tonic-gate int ratio, barlength, i, remaining;
12367c478bd9Sstevel@tonic-gate char buf[512];
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate if (flag == -1) {
12397c478bd9Sstevel@tonic-gate (void) gettimeofday(&start, (struct timezone *)0);
12407c478bd9Sstevel@tonic-gate lastupdate = start;
12417c478bd9Sstevel@tonic-gate lastsize = 0;
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate if (foregroundproc() == 0)
12447c478bd9Sstevel@tonic-gate return;
12457c478bd9Sstevel@tonic-gate
12467c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, (struct timezone *)0);
12477c478bd9Sstevel@tonic-gate cursize = statbytes;
12487c478bd9Sstevel@tonic-gate if (totalbytes != 0) {
12497c478bd9Sstevel@tonic-gate ratio = (int)(100.0 * cursize / totalbytes);
12507c478bd9Sstevel@tonic-gate ratio = MAX(ratio, 0);
12517c478bd9Sstevel@tonic-gate ratio = MIN(ratio, 100);
12527c478bd9Sstevel@tonic-gate } else
12537c478bd9Sstevel@tonic-gate ratio = 100;
12547c478bd9Sstevel@tonic-gate
12557c478bd9Sstevel@tonic-gate snprintf(buf, sizeof (buf), "\r%-20.20s %3d%% ", curfile, ratio);
12567c478bd9Sstevel@tonic-gate
12577c478bd9Sstevel@tonic-gate barlength = getttywidth() - 51;
12587c478bd9Sstevel@tonic-gate if (barlength > 0) {
12597c478bd9Sstevel@tonic-gate i = barlength * ratio / 100;
12607c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12617c478bd9Sstevel@tonic-gate "|%.*s%*s|", i,
12627c478bd9Sstevel@tonic-gate "*******************************************************"
12637c478bd9Sstevel@tonic-gate "*******************************************************"
12647c478bd9Sstevel@tonic-gate "*******************************************************"
12657c478bd9Sstevel@tonic-gate "*******************************************************"
12667c478bd9Sstevel@tonic-gate "*******************************************************"
12677c478bd9Sstevel@tonic-gate "*******************************************************"
12687c478bd9Sstevel@tonic-gate "*******************************************************",
12697c478bd9Sstevel@tonic-gate barlength - i, "");
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate i = 0;
12727c478bd9Sstevel@tonic-gate abbrevsize = cursize;
12736c91005fSHuie-Ying Lee while (abbrevsize >= 100000 && i < strlen(prefixes) - 1) {
12747c478bd9Sstevel@tonic-gate i++;
12757c478bd9Sstevel@tonic-gate abbrevsize >>= 10;
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf), " %5lu %c%c ",
12787c478bd9Sstevel@tonic-gate (unsigned long) abbrevsize, prefixes[i],
12797c478bd9Sstevel@tonic-gate prefixes[i] == ' ' ? ' ' : 'B');
12807c478bd9Sstevel@tonic-gate
12817c478bd9Sstevel@tonic-gate timersub(&now, &lastupdate, &wait);
12827c478bd9Sstevel@tonic-gate if (cursize > lastsize) {
12837c478bd9Sstevel@tonic-gate lastupdate = now;
12847c478bd9Sstevel@tonic-gate lastsize = cursize;
12857c478bd9Sstevel@tonic-gate if (wait.tv_sec >= STALLTIME) {
12867c478bd9Sstevel@tonic-gate start.tv_sec += wait.tv_sec;
12877c478bd9Sstevel@tonic-gate start.tv_usec += wait.tv_usec;
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate wait.tv_sec = 0;
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate timersub(&now, &start, &td);
12927c478bd9Sstevel@tonic-gate elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
12937c478bd9Sstevel@tonic-gate
12947c478bd9Sstevel@tonic-gate if (flag != 1 &&
12957c478bd9Sstevel@tonic-gate (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
12967c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12977c478bd9Sstevel@tonic-gate " --:-- ETA");
12987c478bd9Sstevel@tonic-gate } else if (wait.tv_sec >= STALLTIME) {
12997c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13007c478bd9Sstevel@tonic-gate " - stalled -");
13017c478bd9Sstevel@tonic-gate } else {
13027c478bd9Sstevel@tonic-gate if (flag != 1)
13037c478bd9Sstevel@tonic-gate remaining = (int)(totalbytes / (statbytes / elapsed) -
13047c478bd9Sstevel@tonic-gate elapsed);
13057c478bd9Sstevel@tonic-gate else
13067c478bd9Sstevel@tonic-gate remaining = (int)elapsed;
13077c478bd9Sstevel@tonic-gate
13087c478bd9Sstevel@tonic-gate i = remaining / 3600;
13097c478bd9Sstevel@tonic-gate if (i)
13107c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13117c478bd9Sstevel@tonic-gate "%2d:", i);
13127c478bd9Sstevel@tonic-gate else
13137c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13147c478bd9Sstevel@tonic-gate " ");
13157c478bd9Sstevel@tonic-gate i = remaining % 3600;
13167c478bd9Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13177c478bd9Sstevel@tonic-gate "%02d:%02d%s", i / 60, i % 60,
13187c478bd9Sstevel@tonic-gate (flag != 1) ? " ETA" : " ");
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate atomicio(write, fileno(stdout), buf, strlen(buf));
13217c478bd9Sstevel@tonic-gate
13227c478bd9Sstevel@tonic-gate if (flag == -1) {
13237c478bd9Sstevel@tonic-gate mysignal(SIGALRM, updateprogressmeter);
13247c478bd9Sstevel@tonic-gate alarm(PROGRESSTIME);
13257c478bd9Sstevel@tonic-gate } else if (flag == 1) {
13267c478bd9Sstevel@tonic-gate alarm(0);
13277c478bd9Sstevel@tonic-gate atomicio(write, fileno(stdout), "\n", 1);
13287c478bd9Sstevel@tonic-gate statbytes = 0;
13297c478bd9Sstevel@tonic-gate }
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate
13327c478bd9Sstevel@tonic-gate int
getttywidth(void)13337c478bd9Sstevel@tonic-gate getttywidth(void)
13347c478bd9Sstevel@tonic-gate {
13357c478bd9Sstevel@tonic-gate struct winsize winsize;
13367c478bd9Sstevel@tonic-gate
13377c478bd9Sstevel@tonic-gate if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
13387c478bd9Sstevel@tonic-gate return (winsize.ws_col ? winsize.ws_col : 80);
13397c478bd9Sstevel@tonic-gate else
13407c478bd9Sstevel@tonic-gate return (80);
13417c478bd9Sstevel@tonic-gate }
1342