blob: fb4d3096c1a64ea01c1c2449ae858119eb0d1d25 [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 Lindstrom3e45e4c2001-10-03 17:30:58 +000078RCSID("$OpenBSD: scp.c,v 1.85 2001/10/01 08:06:28 markus 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';
Ben Lindstrom3e45e4c2001-10-03 17:30:58 +0000506 if (strchr(name, '\n') != NULL) {
507 run_err("%s: skipping, filename contains a newline",
508 name);
509 goto next;
510 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511 if ((fd = open(name, O_RDONLY, 0)) < 0)
512 goto syserr;
513 if (fstat(fd, &stb) < 0) {
514syserr: run_err("%s: %s", name, strerror(errno));
515 goto next;
516 }
517 switch (stb.st_mode & S_IFMT) {
518 case S_IFREG:
519 break;
520 case S_IFDIR:
521 if (iamrecursive) {
522 rsource(name, &stb);
523 goto next;
524 }
525 /* FALLTHROUGH */
526 default:
527 run_err("%s: not a regular file", name);
528 goto next;
529 }
530 if ((last = strrchr(name, '/')) == NULL)
531 last = name;
532 else
533 ++last;
534 curfile = last;
535 if (pflag) {
536 /*
537 * Make it compatible with possible future
538 * versions expecting microseconds.
539 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000540 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000541 (u_long) stb.st_mtime,
542 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000543 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544 if (response() < 0)
545 goto next;
546 }
547#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100548#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000549 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000550 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000551 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100552#else
553 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000554 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100555 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000556 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100557#endif
558
Damien Miller95def091999-11-25 00:26:21 +1100559 if (verbose_mode) {
560 fprintf(stderr, "Sending file modes: %s", buf);
561 fflush(stderr);
562 }
Damien Miller35dabd02000-05-01 21:10:33 +1000563 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 if (response() < 0)
565 goto next;
566 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100567next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568 continue;
569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 if (showprogress) {
571 totalbytes = stb.st_size;
572 progressmeter(-1);
573 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574 /* Keep writing after an error so that we stay sync'd up. */
575 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
576 amt = bp->cnt;
577 if (i + amt > stb.st_size)
578 amt = stb.st_size - i;
579 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000580 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581 if (result != amt)
582 haderr = result >= 0 ? EIO : errno;
583 }
584 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000585 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586 else {
Damien Miller864ea591999-12-15 11:04:25 +1100587 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 if (result != amt)
589 haderr = result >= 0 ? EIO : errno;
590 statbytes += result;
591 }
592 }
Damien Miller95def091999-11-25 00:26:21 +1100593 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594 progressmeter(1);
595
596 if (close(fd) < 0 && !haderr)
597 haderr = errno;
598 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000599 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600 else
601 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100602 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603 }
604}
605
606void
607rsource(name, statp)
608 char *name;
609 struct stat *statp;
610{
611 DIR *dirp;
612 struct dirent *dp;
613 char *last, *vect[1], path[1100];
614
615 if (!(dirp = opendir(name))) {
616 run_err("%s: %s", name, strerror(errno));
617 return;
618 }
619 last = strrchr(name, '/');
620 if (last == 0)
621 last = name;
622 else
623 last++;
624 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000625 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000626 (u_long) statp->st_mtime,
627 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000628 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629 if (response() < 0) {
630 closedir(dirp);
631 return;
632 }
633 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000634 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000635 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100636 if (verbose_mode)
637 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000638 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639 if (response() < 0) {
640 closedir(dirp);
641 return;
642 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000643 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644 if (dp->d_ino == 0)
645 continue;
646 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
647 continue;
648 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
649 run_err("%s/%s: name too long", name, dp->d_name);
650 continue;
651 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000652 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000653 vect[0] = path;
654 source(1, vect);
655 }
Damien Miller95def091999-11-25 00:26:21 +1100656 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000657 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100658 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659}
660
661void
662sink(argc, argv)
663 int argc;
664 char *argv[];
665{
666 static BUF buffer;
667 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100668 enum {
669 YES, NO, DISPLAYED
670 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671 BUF *bp;
672 off_t i, j;
673 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100674 off_t size;
675 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100677 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678
Ben Lindstromf719a202001-04-14 23:14:22 +0000679#define atime tv[0]
680#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000681#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682
683 setimes = targisdir = 0;
684 mask = umask(0);
685 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100686 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687 if (argc != 1) {
688 run_err("ambiguous target");
689 exit(1);
690 }
691 targ = *argv;
692 if (targetshouldbedirectory)
693 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100694
Damien Miller35dabd02000-05-01 21:10:33 +1000695 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
697 targisdir = 1;
698 for (first = 1;; first = 0) {
699 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000700 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701 return;
702 if (*cp++ == '\n')
703 SCREWUP("unexpected <newline>");
704 do {
Damien Millere247cc42000-05-07 12:03:14 +1000705 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706 SCREWUP("lost connection");
707 *cp++ = ch;
708 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
709 *cp = 0;
710
711 if (buf[0] == '\01' || buf[0] == '\02') {
712 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000713 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000714 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715 if (buf[0] == '\02')
716 exit(1);
717 ++errs;
718 continue;
719 }
720 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000721 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722 return;
723 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 if (ch == '\n')
725 *--cp = 0;
726
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 cp = buf;
728 if (*cp == 'T') {
729 setimes++;
730 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000731 mtime.tv_sec = strtol(cp, &cp, 10);
732 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000734 mtime.tv_usec = strtol(cp, &cp, 10);
735 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000737 atime.tv_sec = strtol(cp, &cp, 10);
738 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000740 atime.tv_usec = strtol(cp, &cp, 10);
741 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000743 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744 continue;
745 }
746 if (*cp != 'C' && *cp != 'D') {
747 /*
748 * Check for the case "rcp remote:foo\* local:bar".
749 * In this case, the line "No match." can be returned
750 * by the shell before the rcp command on the remote is
751 * executed so the ^Aerror_message convention isn't
752 * followed.
753 */
754 if (first) {
755 run_err("%s", cp);
756 exit(1);
757 }
758 SCREWUP("expected control record");
759 }
760 mode = 0;
761 for (++cp; cp < buf + 5; cp++) {
762 if (*cp < '0' || *cp > '7')
763 SCREWUP("bad mode");
764 mode = (mode << 3) | (*cp - '0');
765 }
766 if (*cp++ != ' ')
767 SCREWUP("mode not delimited");
768
Ben Lindstrombd472262001-03-29 00:39:55 +0000769 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770 size = size * 10 + (*cp++ - '0');
771 if (*cp++ != ' ')
772 SCREWUP("size not delimited");
773 if (targisdir) {
774 static char *namebuf;
775 static int cursize;
776 size_t need;
777
778 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000779 if (need > cursize) {
780 if (namebuf)
781 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100782 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000783 cursize = need;
784 }
785 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000786 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787 np = namebuf;
788 } else
789 np = targ;
790 curfile = cp;
791 exists = stat(np, &stb) == 0;
792 if (buf[0] == 'D') {
793 int mod_flag = pflag;
794 if (exists) {
795 if (!S_ISDIR(stb.st_mode)) {
796 errno = ENOTDIR;
797 goto bad;
798 }
799 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100800 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801 } else {
Damien Miller95def091999-11-25 00:26:21 +1100802 /* Handle copying from a read-only
803 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804 mod_flag = 1;
805 if (mkdir(np, mode | S_IRWXU) < 0)
806 goto bad;
807 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000808 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809 sink(1, vect);
810 if (setimes) {
811 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100812 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100813 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100814 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815 }
816 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100817 (void) chmod(vect[0], mode);
818 if (vect[0])
819 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820 continue;
821 }
822 omode = mode;
823 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000824 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825bad: run_err("%s: %s", np, strerror(errno));
826 continue;
827 }
Damien Miller35dabd02000-05-01 21:10:33 +1000828 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100830 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831 continue;
832 }
833 cp = bp->buf;
834 wrerr = NO;
835
836 if (showprogress) {
837 totalbytes = size;
838 progressmeter(-1);
839 }
840 statbytes = 0;
841 for (count = i = 0; i < size; i += 4096) {
842 amt = 4096;
843 if (i + amt > size)
844 amt = size - i;
845 count += amt;
846 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100847 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000848 if (j == -1 && (errno == EINTR ||
849 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100850 continue;
851 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000853 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854 exit(1);
855 }
856 amt -= j;
857 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100858 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 } while (amt > 0);
860 if (count == bp->cnt) {
861 /* Keep reading so we stay sync'd up. */
862 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000863 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864 if (j != count) {
865 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100866 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867 }
868 }
869 count = 0;
870 cp = bp->buf;
871 }
872 }
873 if (showprogress)
874 progressmeter(1);
875 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000876 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100878 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 if (ftruncate(ofd, size)) {
881 run_err("%s: truncate: %s", np, strerror(errno));
882 wrerr = DISPLAYED;
883 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 if (pflag) {
885 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100886#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100888#else /* HAVE_FCHMOD */
889 if (chmod(np, omode))
890#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000892 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 } else {
894 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100895#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100897#else /* HAVE_FCHMOD */
898 if (chmod(np, omode & ~mask))
899#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000901 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902 }
Damien Millerbe484b52000-07-15 14:14:16 +1000903 if (close(ofd) == -1) {
904 wrerr = YES;
905 wrerrno = errno;
906 }
Damien Miller95def091999-11-25 00:26:21 +1100907 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908 if (setimes && wrerr == NO) {
909 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100910 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000912 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 wrerr = DISPLAYED;
914 }
915 }
Damien Miller95def091999-11-25 00:26:21 +1100916 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917 case YES:
918 run_err("%s: %s", np, strerror(wrerrno));
919 break;
920 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000921 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 break;
923 case DISPLAYED:
924 break;
925 }
926 }
927screwup:
928 run_err("protocol error: %s", why);
929 exit(1);
930}
931
932int
933response()
934{
935 char ch, *cp, resp, rbuf[2048];
936
Damien Millere247cc42000-05-07 12:03:14 +1000937 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 lostconn(0);
939
940 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100941 switch (resp) {
942 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943 return (0);
944 default:
945 *cp++ = resp;
946 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100947 case 1: /* error, followed by error msg */
948 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949 do {
Damien Millere247cc42000-05-07 12:03:14 +1000950 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951 lostconn(0);
952 *cp++ = ch;
953 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
954
955 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000956 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957 ++errs;
958 if (resp == 1)
959 return (-1);
960 exit(1);
961 }
962 /* NOTREACHED */
963}
964
965void
966usage()
967{
Ben Lindstrom1e243242001-09-18 05:38:44 +0000968 (void) fprintf(stderr,
969 "usage: scp [-pqrvBC46] [-F config] [-S ssh] [-P port] [-c cipher] [-i identity]\n"
970 " [-o option] f1 f2\n"
Ben Lindstromff2618c2001-03-29 00:43:54 +0000971 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972 exit(1);
973}
974
975void
Damien Miller95def091999-11-25 00:26:21 +1100976run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977{
978 static FILE *fp;
979 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
981 ++errs;
982 if (fp == NULL && !(fp = fdopen(remout, "w")))
983 return;
Damien Miller95def091999-11-25 00:26:21 +1100984 (void) fprintf(fp, "%c", 0x01);
985 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100986 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100987 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100988 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100989 (void) fprintf(fp, "\n");
990 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100993 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100994 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100995 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100996 fprintf(stderr, "\n");
997 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998}
999
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000void
1001verifydir(cp)
1002 char *cp;
1003{
1004 struct stat stb;
1005
1006 if (!stat(cp, &stb)) {
1007 if (S_ISDIR(stb.st_mode))
1008 return;
1009 errno = ENOTDIR;
1010 }
1011 run_err("%s: %s", cp, strerror(errno));
1012 exit(1);
1013}
1014
1015int
1016okname(cp0)
1017 char *cp0;
1018{
1019 int c;
1020 char *cp;
1021
1022 cp = cp0;
1023 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001024 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025 if (c & 0200)
1026 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001027 if (!isalpha(c) && !isdigit(c) &&
1028 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029 goto bad;
1030 } while (*++cp);
1031 return (1);
1032
Damien Miller98c7ad62000-03-09 21:27:49 +11001033bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001034 return (0);
1035}
1036
1037BUF *
1038allocbuf(bp, fd, blksize)
1039 BUF *bp;
1040 int fd, blksize;
1041{
1042 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001043#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044 struct stat stb;
1045
1046 if (fstat(fd, &stb) < 0) {
1047 run_err("fstat: %s", strerror(errno));
1048 return (0);
1049 }
Damien Miller95def091999-11-25 00:26:21 +11001050 if (stb.st_blksize == 0)
1051 size = blksize;
1052 else
1053 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001054 stb.st_blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001055#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001056 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001057#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058 if (bp->cnt >= size)
1059 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001060 if (bp->buf == NULL)
1061 bp->buf = xmalloc(size);
1062 else
1063 bp->buf = xrealloc(bp->buf, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001064 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065 bp->cnt = size;
1066 return (bp);
1067}
1068
1069void
1070lostconn(signo)
1071 int signo;
1072{
1073 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001074 write(STDERR_FILENO, "lost connection\n", 16);
1075 if (signo)
1076 _exit(1);
1077 else
1078 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079}
1080
Ben Lindstrombba81212001-06-25 05:01:22 +00001081static void
Damien Miller7684ee12000-03-17 23:40:15 +11001082updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083{
1084 int save_errno = errno;
1085
1086 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001087 signal(SIGALRM, updateprogressmeter);
1088 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089 errno = save_errno;
1090}
1091
Ben Lindstrombba81212001-06-25 05:01:22 +00001092static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001093foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001094{
1095 static pid_t pgrp = -1;
1096 int ctty_pgrp;
1097
1098 if (pgrp == -1)
1099 pgrp = getpgrp();
1100
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001101#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001102 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1103 ctty_pgrp == pgrp);
1104#else
Damien Miller95def091999-11-25 00:26:21 +11001105 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1106 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001107#endif
Damien Miller81428f91999-11-18 09:28:11 +11001108}
1109
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110void
1111progressmeter(int flag)
1112{
1113 static const char prefixes[] = " KMGTP";
1114 static struct timeval lastupdate;
1115 static off_t lastsize;
1116 struct timeval now, td, wait;
1117 off_t cursize, abbrevsize;
1118 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001119 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120 char buf[256];
1121
1122 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001123 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124 lastupdate = start;
1125 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001126 }
Damien Miller81428f91999-11-18 09:28:11 +11001127 if (foregroundproc() == 0)
1128 return;
1129
Damien Miller95def091999-11-25 00:26:21 +11001130 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001132 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001133 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134 ratio = MAX(ratio, 0);
1135 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001136 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137 ratio = 100;
1138
Damien Miller95def091999-11-25 00:26:21 +11001139 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140
1141 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001142 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143 if (barlength > 0) {
1144 i = barlength * ratio / 100;
1145 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001146 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148 i = 0;
1149 abbrevsize = cursize;
1150 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1151 i++;
1152 abbrevsize >>= 10;
1153 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001154 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001155 (unsigned long) abbrevsize, prefixes[i],
1156 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157
1158 timersub(&now, &lastupdate, &wait);
1159 if (cursize > lastsize) {
1160 lastupdate = now;
1161 lastsize = cursize;
1162 if (wait.tv_sec >= STALLTIME) {
1163 start.tv_sec += wait.tv_sec;
1164 start.tv_usec += wait.tv_usec;
1165 }
1166 wait.tv_sec = 0;
1167 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 timersub(&now, &start, &td);
1169 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1170
Kevin Steves7d00ba42000-12-15 23:03:10 +00001171 if (flag != 1 &&
1172 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001174 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175 } else if (wait.tv_sec >= STALLTIME) {
1176 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001177 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001179 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001180 remaining = (int)(totalbytes / (statbytes / elapsed) -
1181 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001182 else
1183 remaining = elapsed;
1184
Damien Miller01ab4a21999-10-28 15:23:30 +10001185 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186 if (i)
1187 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001188 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189 else
1190 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001191 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 i = remaining % 3600;
1193 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001194 "%02d:%02d%s", i / 60, i % 60,
1195 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196 }
1197 atomicio(write, fileno(stdout), buf, strlen(buf));
1198
1199 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001200 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001201 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001203 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001204 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205 statbytes = 0;
1206 }
1207}
1208
1209int
1210getttywidth(void)
1211{
1212 struct winsize winsize;
1213
1214 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001215 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216 else
Damien Miller95def091999-11-25 00:26:21 +11001217 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218}