blob: e603646b6ad68fa3373fb7dcfeb62ddfca1bb8a2 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * scp - secure remote copy. This is basically patched BSD rcp which
3 * uses ssh to do the data transfer (instead of using rcmd).
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Millere4340be2000-09-16 13:29:08 +11005 * NOTE: This version should NOT be suid root. (This uses ssh to
6 * do the transfer and ssh has the necessary privileges.)
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Miller95def091999-11-25 00:26:21 +11008 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 */
16/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +000017 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110019 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41/*
Damien Millerad833b32000-08-23 10:46:23 +100042 * Parts from:
43 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044 * Copyright (c) 1983, 1990, 1992, 1993, 1995
45 * The Regents of the University of California. All rights reserved.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075 */
76
77#include "includes.h"
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +000078RCSID("$OpenBSD: scp.c,v 1.84 2001/09/19 19:24:19 stevesk Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "atomicio.h"
82#include "pathnames.h"
83#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +000084#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrom49a79c02000-11-17 03:47:20 +000086#ifdef HAVE___PROGNAME
87extern char *__progname;
88#else
89char *__progname;
90#endif
91
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092/* For progressmeter() -- number of seconds before xfer considered "stalled" */
93#define STALLTIME 5
Ben Lindstrom958d9f62001-08-06 22:48:19 +000094/* alarm() interval for updating progress meter */
95#define PROGRESSTIME 1
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller484118e2000-07-02 19:13:56 +100097/* Progress meter bar */
98#define BAR \
99 "************************************************************"\
100 "************************************************************"\
101 "************************************************************"\
102 "************************************************************"
103#define MAX_BARLENGTH (sizeof(BAR) - 1)
104
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105/* Visual statistics about files as they are transferred. */
106void progressmeter(int);
107
108/* Returns width of the terminal (for progress meter calculations). */
109int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000110int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Ben Lindstrom387c4722001-05-08 20:27:25 +0000112/* Struct for addargs */
113arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +1100114
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115/* Time a transfer started. */
116static struct timeval start;
117
118/* Number of bytes of current file transferred so far. */
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000119volatile off_t statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100122off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
124/* Name of current file being transferred. */
125char *curfile;
126
127/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100128int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130/* This is set to zero if the progressmeter is not desired. */
131int showprogress = 1;
132
Damien Millerad833b32000-08-23 10:46:23 +1000133/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000134char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000135
Damien Miller5428f641999-11-25 11:54:57 +1100136/*
137 * This function executes the given command as the specified user on the
138 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
139 * assigns the input and output file descriptors on success.
140 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller4af51302000-04-16 11:18:38 +1000142int
Damien Millerad833b32000-08-23 10:46:23 +1000143do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144{
Damien Miller95def091999-11-25 00:26:21 +1100145 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller95def091999-11-25 00:26:21 +1100147 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000148 fprintf(stderr,
149 "Executing: program %s host %s, user %s, command %s\n",
150 ssh_program, host,
151 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Miller5428f641999-11-25 11:54:57 +1100153 /*
154 * Reserve two descriptors so that the real pipes won't get
155 * descriptors 0 and 1 because that will screw up dup2 below.
156 */
Damien Miller95def091999-11-25 00:26:21 +1100157 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Damien Miller95def091999-11-25 00:26:21 +1100159 /* Create a socket pair for communicating with ssh. */
160 if (pipe(pin) < 0)
161 fatal("pipe: %s", strerror(errno));
162 if (pipe(pout) < 0)
163 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Damien Miller95def091999-11-25 00:26:21 +1100165 /* Free the reserved descriptors. */
166 close(reserved[0]);
167 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller95def091999-11-25 00:26:21 +1100169 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100170 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Child. */
172 close(pin[1]);
173 close(pout[0]);
174 dup2(pin[0], 0);
175 dup2(pout[1], 1);
176 close(pin[0]);
177 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller874d77b2000-10-14 16:23:11 +1100179 args.list[0] = ssh_program;
180 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000181 addargs(&args, "-l%s", remuser);
182 addargs(&args, "%s", host);
183 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100184
Damien Miller874d77b2000-10-14 16:23:11 +1100185 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000186 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100187 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188 }
Damien Miller95def091999-11-25 00:26:21 +1100189 /* Parent. Close the other side, and return the local side. */
190 close(pin[0]);
191 *fdout = pin[1];
192 close(pout[1]);
193 *fdin = pout[0];
194 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197typedef struct {
198 int cnt;
199 char *buf;
200} BUF;
201
Damien Miller95def091999-11-25 00:26:21 +1100202BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100203void lostconn(int);
204void nospace(void);
205int okname(char *);
206void run_err(const char *,...);
207void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100210uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211int errs, remin, remout;
212int pflag, iamremote, iamrecursive, targetshouldbedirectory;
213
214#define CMDNEEDS 64
215char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
216
Damien Miller95def091999-11-25 00:26:21 +1100217int response(void);
218void rsource(char *, struct stat *);
219void sink(int, char *[]);
220void source(int, char *[]);
221void tolocal(int, char *[]);
222void toremote(char *, int, char *[]);
223void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
225int
226main(argc, argv)
227 int argc;
228 char *argv[];
229{
230 int ch, fflag, tflag;
231 char *targ;
232 extern char *optarg;
233 extern int optind;
234
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000235 __progname = get_progname(argv[0]);
236
Damien Miller874d77b2000-10-14 16:23:11 +1100237 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000238 addargs(&args, "ssh"); /* overwritten with ssh_program */
239 addargs(&args, "-x");
Ben Lindstrom4213c552001-09-12 18:45:09 +0000240 addargs(&args, "-oForwardAgent no");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000241 addargs(&args, "-oFallBackToRsh no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000242 addargs(&args, "-oClearAllForwardings yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100243
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244 fflag = tflag = 0;
Ben Lindstrom1e243242001-09-18 05:38:44 +0000245 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100246 switch (ch) {
247 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100248 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100249 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100250 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000251 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100252 break;
253 case 'o':
254 case 'c':
255 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000256 case 'F':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000257 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100258 break;
259 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000260 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100261 break;
262 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000263 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100264 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 case 'p':
266 pflag = 1;
267 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268 case 'r':
269 iamrecursive = 1;
270 break;
Damien Millerad833b32000-08-23 10:46:23 +1000271 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100272 ssh_program = xstrdup(optarg);
273 break;
274 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000275 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100276 verbose_mode = 1;
277 break;
278 case 'q':
279 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000280 break;
281
Damien Miller95def091999-11-25 00:26:21 +1100282 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283 case 'd':
284 targetshouldbedirectory = 1;
285 break;
Damien Miller95def091999-11-25 00:26:21 +1100286 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287 iamremote = 1;
288 fflag = 1;
289 break;
Damien Miller95def091999-11-25 00:26:21 +1100290 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291 iamremote = 1;
292 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000293#ifdef HAVE_CYGWIN
294 setmode(0, O_BINARY);
295#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297 default:
298 usage();
299 }
300 argc -= optind;
301 argv += optind;
302
303 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100304 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller95def091999-11-25 00:26:21 +1100306 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307 showprogress = 0;
308
309 remin = STDIN_FILENO;
310 remout = STDOUT_FILENO;
311
Kevin Stevesef4eea92001-02-05 12:42:17 +0000312 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100313 /* Follow "protocol", send data. */
314 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315 source(argc, argv);
316 exit(errs != 0);
317 }
Damien Miller95def091999-11-25 00:26:21 +1100318 if (tflag) {
319 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320 sink(argc, argv);
321 exit(errs != 0);
322 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323 if (argc < 2)
324 usage();
325 if (argc > 2)
326 targetshouldbedirectory = 1;
327
328 remin = remout = -1;
329 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000330 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
331 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000332 iamrecursive ? " -r" : "", pflag ? " -p" : "",
333 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
Damien Miller95def091999-11-25 00:26:21 +1100335 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
337 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
338 toremote(targ, argc, argv);
339 else {
Damien Miller95def091999-11-25 00:26:21 +1100340 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341 if (targetshouldbedirectory)
342 verifydir(argv[argc - 1]);
343 }
344 exit(errs != 0);
345}
346
347void
348toremote(targ, argc, argv)
349 char *targ, *argv[];
350 int argc;
351{
352 int i, len;
353 char *bp, *host, *src, *suser, *thost, *tuser;
354
355 *targ++ = 0;
356 if (*targ == 0)
357 targ = ".";
358
359 if ((thost = strchr(argv[argc - 1], '@'))) {
360 /* user@host */
361 *thost++ = 0;
362 tuser = argv[argc - 1];
363 if (*tuser == '\0')
364 tuser = NULL;
365 else if (!okname(tuser))
366 exit(1);
367 } else {
368 thost = argv[argc - 1];
369 tuser = NULL;
370 }
371
372 for (i = 0; i < argc - 1; i++) {
373 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100374 if (src) { /* remote to remote */
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000375 static char *ssh_options =
376 "-x -o'FallBackToRsh no' "
377 "-o'ClearAllForwardings yes'";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378 *src++ = 0;
379 if (*src == 0)
380 src = ".";
381 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000382 len = strlen(ssh_program) + strlen(argv[i]) +
383 strlen(src) + (tuser ? strlen(tuser) : 0) +
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000384 strlen(thost) + strlen(targ) +
385 strlen(ssh_options) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100386 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387 if (host) {
388 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100389 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390 suser = argv[i];
391 if (*suser == '\0')
392 suser = pwd->pw_name;
393 else if (!okname(suser))
394 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000395 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000396 "%s%s %s -n "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000397 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000398 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000399 ssh_options, suser, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000400 tuser ? tuser : "", tuser ? "@" : "",
401 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100402 } else {
403 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000404 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000405 "exec %s%s %s -n %s "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000406 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000407 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000408 ssh_options, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000409 tuser ? tuser : "", tuser ? "@" : "",
410 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100411 }
Damien Miller95def091999-11-25 00:26:21 +1100412 if (verbose_mode)
413 fprintf(stderr, "Executing: %s\n", bp);
414 (void) system(bp);
415 (void) xfree(bp);
416 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417 if (remin == -1) {
418 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100419 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000420 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100421 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000422 if (do_cmd(host, tuser, bp, &remin,
423 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100424 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425 if (response() < 0)
426 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100427 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428 }
Damien Miller95def091999-11-25 00:26:21 +1100429 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430 }
431 }
432}
433
434void
435tolocal(argc, argv)
436 int argc;
437 char *argv[];
438{
439 int i, len;
440 char *bp, *host, *src, *suser;
441
442 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100443 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000445 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000447 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000448 iamrecursive ? " -r" : "", pflag ? " -p" : "",
449 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100450 if (verbose_mode)
451 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 if (system(bp))
453 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100454 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455 continue;
456 }
457 *src++ = 0;
458 if (*src == 0)
459 src = ".";
460 if ((host = strchr(argv[i], '@')) == NULL) {
461 host = argv[i];
462 suser = NULL;
463 } else {
464 *host++ = 0;
465 suser = argv[i];
466 if (*suser == '\0')
467 suser = pwd->pw_name;
468 else if (!okname(suser))
469 continue;
470 }
Damien Miller34132e52000-01-14 15:45:46 +1100471 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100473 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000474 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000475 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100476 (void) xfree(bp);
477 ++errs;
478 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479 }
Damien Miller95def091999-11-25 00:26:21 +1100480 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100482 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483 remin = remout = -1;
484 }
485}
486
487void
488source(argc, argv)
489 int argc;
490 char *argv[];
491{
492 struct stat stb;
493 static BUF buffer;
494 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000495 off_t i, amt, result;
496 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000498 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499
500 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100501 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000503 len = strlen(name);
504 while (len > 1 && name[len-1] == '/')
505 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506 if ((fd = open(name, O_RDONLY, 0)) < 0)
507 goto syserr;
508 if (fstat(fd, &stb) < 0) {
509syserr: run_err("%s: %s", name, strerror(errno));
510 goto next;
511 }
512 switch (stb.st_mode & S_IFMT) {
513 case S_IFREG:
514 break;
515 case S_IFDIR:
516 if (iamrecursive) {
517 rsource(name, &stb);
518 goto next;
519 }
520 /* FALLTHROUGH */
521 default:
522 run_err("%s: not a regular file", name);
523 goto next;
524 }
525 if ((last = strrchr(name, '/')) == NULL)
526 last = name;
527 else
528 ++last;
529 curfile = last;
530 if (pflag) {
531 /*
532 * Make it compatible with possible future
533 * versions expecting microseconds.
534 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000535 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000536 (u_long) stb.st_mtime,
537 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000538 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539 if (response() < 0)
540 goto next;
541 }
542#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100543#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000544 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000545 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000546 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100547#else
548 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000549 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100550 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000551 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100552#endif
553
Damien Miller95def091999-11-25 00:26:21 +1100554 if (verbose_mode) {
555 fprintf(stderr, "Sending file modes: %s", buf);
556 fflush(stderr);
557 }
Damien Miller35dabd02000-05-01 21:10:33 +1000558 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 if (response() < 0)
560 goto next;
561 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100562next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563 continue;
564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 if (showprogress) {
566 totalbytes = stb.st_size;
567 progressmeter(-1);
568 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 /* Keep writing after an error so that we stay sync'd up. */
570 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
571 amt = bp->cnt;
572 if (i + amt > stb.st_size)
573 amt = stb.st_size - i;
574 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000575 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 if (result != amt)
577 haderr = result >= 0 ? EIO : errno;
578 }
579 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000580 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581 else {
Damien Miller864ea591999-12-15 11:04:25 +1100582 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 if (result != amt)
584 haderr = result >= 0 ? EIO : errno;
585 statbytes += result;
586 }
587 }
Damien Miller95def091999-11-25 00:26:21 +1100588 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 progressmeter(1);
590
591 if (close(fd) < 0 && !haderr)
592 haderr = errno;
593 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000594 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595 else
596 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100597 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598 }
599}
600
601void
602rsource(name, statp)
603 char *name;
604 struct stat *statp;
605{
606 DIR *dirp;
607 struct dirent *dp;
608 char *last, *vect[1], path[1100];
609
610 if (!(dirp = opendir(name))) {
611 run_err("%s: %s", name, strerror(errno));
612 return;
613 }
614 last = strrchr(name, '/');
615 if (last == 0)
616 last = name;
617 else
618 last++;
619 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000620 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000621 (u_long) statp->st_mtime,
622 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000623 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 if (response() < 0) {
625 closedir(dirp);
626 return;
627 }
628 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000629 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000630 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100631 if (verbose_mode)
632 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000633 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634 if (response() < 0) {
635 closedir(dirp);
636 return;
637 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000638 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639 if (dp->d_ino == 0)
640 continue;
641 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
642 continue;
643 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
644 run_err("%s/%s: name too long", name, dp->d_name);
645 continue;
646 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000647 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648 vect[0] = path;
649 source(1, vect);
650 }
Damien Miller95def091999-11-25 00:26:21 +1100651 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000652 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100653 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654}
655
656void
657sink(argc, argv)
658 int argc;
659 char *argv[];
660{
661 static BUF buffer;
662 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100663 enum {
664 YES, NO, DISPLAYED
665 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666 BUF *bp;
667 off_t i, j;
668 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100669 off_t size;
670 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100672 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673
Ben Lindstromf719a202001-04-14 23:14:22 +0000674#define atime tv[0]
675#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000676#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677
678 setimes = targisdir = 0;
679 mask = umask(0);
680 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100681 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682 if (argc != 1) {
683 run_err("ambiguous target");
684 exit(1);
685 }
686 targ = *argv;
687 if (targetshouldbedirectory)
688 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100689
Damien Miller35dabd02000-05-01 21:10:33 +1000690 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
692 targisdir = 1;
693 for (first = 1;; first = 0) {
694 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000695 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696 return;
697 if (*cp++ == '\n')
698 SCREWUP("unexpected <newline>");
699 do {
Damien Millere247cc42000-05-07 12:03:14 +1000700 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701 SCREWUP("lost connection");
702 *cp++ = ch;
703 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
704 *cp = 0;
705
706 if (buf[0] == '\01' || buf[0] == '\02') {
707 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000708 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000709 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 if (buf[0] == '\02')
711 exit(1);
712 ++errs;
713 continue;
714 }
715 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000716 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717 return;
718 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719 if (ch == '\n')
720 *--cp = 0;
721
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722 cp = buf;
723 if (*cp == 'T') {
724 setimes++;
725 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000726 mtime.tv_sec = strtol(cp, &cp, 10);
727 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000729 mtime.tv_usec = strtol(cp, &cp, 10);
730 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000732 atime.tv_sec = strtol(cp, &cp, 10);
733 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000735 atime.tv_usec = strtol(cp, &cp, 10);
736 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000738 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739 continue;
740 }
741 if (*cp != 'C' && *cp != 'D') {
742 /*
743 * Check for the case "rcp remote:foo\* local:bar".
744 * In this case, the line "No match." can be returned
745 * by the shell before the rcp command on the remote is
746 * executed so the ^Aerror_message convention isn't
747 * followed.
748 */
749 if (first) {
750 run_err("%s", cp);
751 exit(1);
752 }
753 SCREWUP("expected control record");
754 }
755 mode = 0;
756 for (++cp; cp < buf + 5; cp++) {
757 if (*cp < '0' || *cp > '7')
758 SCREWUP("bad mode");
759 mode = (mode << 3) | (*cp - '0');
760 }
761 if (*cp++ != ' ')
762 SCREWUP("mode not delimited");
763
Ben Lindstrombd472262001-03-29 00:39:55 +0000764 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765 size = size * 10 + (*cp++ - '0');
766 if (*cp++ != ' ')
767 SCREWUP("size not delimited");
768 if (targisdir) {
769 static char *namebuf;
770 static int cursize;
771 size_t need;
772
773 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000774 if (need > cursize) {
775 if (namebuf)
776 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100777 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000778 cursize = need;
779 }
780 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000781 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782 np = namebuf;
783 } else
784 np = targ;
785 curfile = cp;
786 exists = stat(np, &stb) == 0;
787 if (buf[0] == 'D') {
788 int mod_flag = pflag;
789 if (exists) {
790 if (!S_ISDIR(stb.st_mode)) {
791 errno = ENOTDIR;
792 goto bad;
793 }
794 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100795 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796 } else {
Damien Miller95def091999-11-25 00:26:21 +1100797 /* Handle copying from a read-only
798 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799 mod_flag = 1;
800 if (mkdir(np, mode | S_IRWXU) < 0)
801 goto bad;
802 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000803 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804 sink(1, vect);
805 if (setimes) {
806 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100807 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100808 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100809 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810 }
811 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100812 (void) chmod(vect[0], mode);
813 if (vect[0])
814 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815 continue;
816 }
817 omode = mode;
818 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000819 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820bad: run_err("%s: %s", np, strerror(errno));
821 continue;
822 }
Damien Miller35dabd02000-05-01 21:10:33 +1000823 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100825 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826 continue;
827 }
828 cp = bp->buf;
829 wrerr = NO;
830
831 if (showprogress) {
832 totalbytes = size;
833 progressmeter(-1);
834 }
835 statbytes = 0;
836 for (count = i = 0; i < size; i += 4096) {
837 amt = 4096;
838 if (i + amt > size)
839 amt = size - i;
840 count += amt;
841 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100842 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000843 if (j == -1 && (errno == EINTR ||
844 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100845 continue;
846 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000848 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849 exit(1);
850 }
851 amt -= j;
852 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100853 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854 } while (amt > 0);
855 if (count == bp->cnt) {
856 /* Keep reading so we stay sync'd up. */
857 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000858 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 if (j != count) {
860 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100861 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862 }
863 }
864 count = 0;
865 cp = bp->buf;
866 }
867 }
868 if (showprogress)
869 progressmeter(1);
870 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000871 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100873 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 if (ftruncate(ofd, size)) {
876 run_err("%s: truncate: %s", np, strerror(errno));
877 wrerr = DISPLAYED;
878 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879 if (pflag) {
880 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100881#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100883#else /* HAVE_FCHMOD */
884 if (chmod(np, omode))
885#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000887 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888 } else {
889 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100890#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100892#else /* HAVE_FCHMOD */
893 if (chmod(np, omode & ~mask))
894#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000896 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897 }
Damien Millerbe484b52000-07-15 14:14:16 +1000898 if (close(ofd) == -1) {
899 wrerr = YES;
900 wrerrno = errno;
901 }
Damien Miller95def091999-11-25 00:26:21 +1100902 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903 if (setimes && wrerr == NO) {
904 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100905 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000907 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908 wrerr = DISPLAYED;
909 }
910 }
Damien Miller95def091999-11-25 00:26:21 +1100911 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912 case YES:
913 run_err("%s: %s", np, strerror(wrerrno));
914 break;
915 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000916 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917 break;
918 case DISPLAYED:
919 break;
920 }
921 }
922screwup:
923 run_err("protocol error: %s", why);
924 exit(1);
925}
926
927int
928response()
929{
930 char ch, *cp, resp, rbuf[2048];
931
Damien Millere247cc42000-05-07 12:03:14 +1000932 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 lostconn(0);
934
935 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100936 switch (resp) {
937 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 return (0);
939 default:
940 *cp++ = resp;
941 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100942 case 1: /* error, followed by error msg */
943 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944 do {
Damien Millere247cc42000-05-07 12:03:14 +1000945 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946 lostconn(0);
947 *cp++ = ch;
948 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
949
950 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000951 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952 ++errs;
953 if (resp == 1)
954 return (-1);
955 exit(1);
956 }
957 /* NOTREACHED */
958}
959
960void
961usage()
962{
Ben Lindstrom1e243242001-09-18 05:38:44 +0000963 (void) fprintf(stderr,
964 "usage: scp [-pqrvBC46] [-F config] [-S ssh] [-P port] [-c cipher] [-i identity]\n"
965 " [-o option] f1 f2\n"
Ben Lindstromff2618c2001-03-29 00:43:54 +0000966 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967 exit(1);
968}
969
970void
Damien Miller95def091999-11-25 00:26:21 +1100971run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972{
973 static FILE *fp;
974 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
976 ++errs;
977 if (fp == NULL && !(fp = fdopen(remout, "w")))
978 return;
Damien Miller95def091999-11-25 00:26:21 +1100979 (void) fprintf(fp, "%c", 0x01);
980 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100981 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100982 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100983 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100984 (void) fprintf(fp, "\n");
985 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller95def091999-11-25 00:26:21 +1100987 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100988 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100989 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100990 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100991 fprintf(stderr, "\n");
992 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993}
994
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995void
996verifydir(cp)
997 char *cp;
998{
999 struct stat stb;
1000
1001 if (!stat(cp, &stb)) {
1002 if (S_ISDIR(stb.st_mode))
1003 return;
1004 errno = ENOTDIR;
1005 }
1006 run_err("%s: %s", cp, strerror(errno));
1007 exit(1);
1008}
1009
1010int
1011okname(cp0)
1012 char *cp0;
1013{
1014 int c;
1015 char *cp;
1016
1017 cp = cp0;
1018 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001019 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020 if (c & 0200)
1021 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001022 if (!isalpha(c) && !isdigit(c) &&
1023 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024 goto bad;
1025 } while (*++cp);
1026 return (1);
1027
Damien Miller98c7ad62000-03-09 21:27:49 +11001028bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029 return (0);
1030}
1031
1032BUF *
1033allocbuf(bp, fd, blksize)
1034 BUF *bp;
1035 int fd, blksize;
1036{
1037 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001038#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039 struct stat stb;
1040
1041 if (fstat(fd, &stb) < 0) {
1042 run_err("fstat: %s", strerror(errno));
1043 return (0);
1044 }
Damien Miller95def091999-11-25 00:26:21 +11001045 if (stb.st_blksize == 0)
1046 size = blksize;
1047 else
1048 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001049 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001050#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001051 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001052#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053 if (bp->cnt >= size)
1054 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001055 if (bp->buf == NULL)
1056 bp->buf = xmalloc(size);
1057 else
1058 bp->buf = xrealloc(bp->buf, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001059 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060 bp->cnt = size;
1061 return (bp);
1062}
1063
1064void
1065lostconn(signo)
1066 int signo;
1067{
1068 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001069 write(STDERR_FILENO, "lost connection\n", 16);
1070 if (signo)
1071 _exit(1);
1072 else
1073 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074}
1075
Ben Lindstrombba81212001-06-25 05:01:22 +00001076static void
Damien Miller7684ee12000-03-17 23:40:15 +11001077updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001078{
1079 int save_errno = errno;
1080
1081 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001082 signal(SIGALRM, updateprogressmeter);
1083 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001084 errno = save_errno;
1085}
1086
Ben Lindstrombba81212001-06-25 05:01:22 +00001087static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001088foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001089{
1090 static pid_t pgrp = -1;
1091 int ctty_pgrp;
1092
1093 if (pgrp == -1)
1094 pgrp = getpgrp();
1095
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001096#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001097 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1098 ctty_pgrp == pgrp);
1099#else
Damien Miller95def091999-11-25 00:26:21 +11001100 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1101 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001102#endif
Damien Miller81428f91999-11-18 09:28:11 +11001103}
1104
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105void
1106progressmeter(int flag)
1107{
1108 static const char prefixes[] = " KMGTP";
1109 static struct timeval lastupdate;
1110 static off_t lastsize;
1111 struct timeval now, td, wait;
1112 off_t cursize, abbrevsize;
1113 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001114 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115 char buf[256];
1116
1117 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001118 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119 lastupdate = start;
1120 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001121 }
Damien Miller81428f91999-11-18 09:28:11 +11001122 if (foregroundproc() == 0)
1123 return;
1124
Damien Miller95def091999-11-25 00:26:21 +11001125 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001127 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001128 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129 ratio = MAX(ratio, 0);
1130 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001131 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132 ratio = 100;
1133
Damien Miller95def091999-11-25 00:26:21 +11001134 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135
1136 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001137 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138 if (barlength > 0) {
1139 i = barlength * ratio / 100;
1140 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001141 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143 i = 0;
1144 abbrevsize = cursize;
1145 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1146 i++;
1147 abbrevsize >>= 10;
1148 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001149 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001150 (unsigned long) abbrevsize, prefixes[i],
1151 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001152
1153 timersub(&now, &lastupdate, &wait);
1154 if (cursize > lastsize) {
1155 lastupdate = now;
1156 lastsize = cursize;
1157 if (wait.tv_sec >= STALLTIME) {
1158 start.tv_sec += wait.tv_sec;
1159 start.tv_usec += wait.tv_usec;
1160 }
1161 wait.tv_sec = 0;
1162 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163 timersub(&now, &start, &td);
1164 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1165
Kevin Steves7d00ba42000-12-15 23:03:10 +00001166 if (flag != 1 &&
1167 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001169 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170 } else if (wait.tv_sec >= STALLTIME) {
1171 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001172 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001174 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001175 remaining = (int)(totalbytes / (statbytes / elapsed) -
1176 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001177 else
1178 remaining = elapsed;
1179
Damien Miller01ab4a21999-10-28 15:23:30 +10001180 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 if (i)
1182 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001183 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 else
1185 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001186 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187 i = remaining % 3600;
1188 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001189 "%02d:%02d%s", i / 60, i % 60,
1190 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191 }
1192 atomicio(write, fileno(stdout), buf, strlen(buf));
1193
1194 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001195 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001196 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001198 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001199 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200 statbytes = 0;
1201 }
1202}
1203
1204int
1205getttywidth(void)
1206{
1207 struct winsize winsize;
1208
1209 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001210 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 else
Damien Miller95def091999-11-25 00:26:21 +11001212 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213}