blob: 868a8eb3d4fce8dd58f2bad41efd044c082bc746 [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 Lindstrom1fa90102001-08-15 23:21:01 +000078RCSID("$OpenBSD: scp.c,v 1.80 2001/08/13 23:38:54 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");
240 addargs(&args, "-oFallBackToRsh no");
Damien Miller874d77b2000-10-14 16:23:11 +1100241
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000243 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100244 switch (ch) {
245 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100246 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100247 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100248 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000249 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100250 break;
251 case 'o':
252 case 'c':
253 case 'i':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000254 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100255 break;
256 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000257 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100258 break;
259 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000260 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100261 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262 case 'p':
263 pflag = 1;
264 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 case 'r':
266 iamrecursive = 1;
267 break;
Damien Millerad833b32000-08-23 10:46:23 +1000268 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100269 ssh_program = xstrdup(optarg);
270 break;
271 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000272 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100273 verbose_mode = 1;
274 break;
275 case 'q':
276 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000277 break;
278
Damien Miller95def091999-11-25 00:26:21 +1100279 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280 case 'd':
281 targetshouldbedirectory = 1;
282 break;
Damien Miller95def091999-11-25 00:26:21 +1100283 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284 iamremote = 1;
285 fflag = 1;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 iamremote = 1;
289 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000290#ifdef HAVE_CYGWIN
291 setmode(0, O_BINARY);
292#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 default:
295 usage();
296 }
297 argc -= optind;
298 argv += optind;
299
300 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100301 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller95def091999-11-25 00:26:21 +1100303 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304 showprogress = 0;
305
306 remin = STDIN_FILENO;
307 remout = STDOUT_FILENO;
308
Kevin Stevesef4eea92001-02-05 12:42:17 +0000309 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100310 /* Follow "protocol", send data. */
311 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312 source(argc, argv);
313 exit(errs != 0);
314 }
Damien Miller95def091999-11-25 00:26:21 +1100315 if (tflag) {
316 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317 sink(argc, argv);
318 exit(errs != 0);
319 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320 if (argc < 2)
321 usage();
322 if (argc > 2)
323 targetshouldbedirectory = 1;
324
325 remin = remout = -1;
326 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000327 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
328 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000329 iamrecursive ? " -r" : "", pflag ? " -p" : "",
330 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331
Damien Miller95def091999-11-25 00:26:21 +1100332 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
334 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
335 toremote(targ, argc, argv);
336 else {
Damien Miller95def091999-11-25 00:26:21 +1100337 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338 if (targetshouldbedirectory)
339 verifydir(argv[argc - 1]);
340 }
341 exit(errs != 0);
342}
343
344void
345toremote(targ, argc, argv)
346 char *targ, *argv[];
347 int argc;
348{
349 int i, len;
350 char *bp, *host, *src, *suser, *thost, *tuser;
351
352 *targ++ = 0;
353 if (*targ == 0)
354 targ = ".";
355
356 if ((thost = strchr(argv[argc - 1], '@'))) {
357 /* user@host */
358 *thost++ = 0;
359 tuser = argv[argc - 1];
360 if (*tuser == '\0')
361 tuser = NULL;
362 else if (!okname(tuser))
363 exit(1);
364 } else {
365 thost = argv[argc - 1];
366 tuser = NULL;
367 }
368
369 for (i = 0; i < argc - 1; i++) {
370 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100371 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372 *src++ = 0;
373 if (*src == 0)
374 src = ".";
375 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000376 len = strlen(ssh_program) + strlen(argv[i]) +
377 strlen(src) + (tuser ? strlen(tuser) : 0) +
378 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100379 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380 if (host) {
381 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100382 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383 suser = argv[i];
384 if (*suser == '\0')
385 suser = pwd->pw_name;
386 else if (!okname(suser))
387 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000388 snprintf(bp, len,
389 "%s%s -x -o'FallBackToRsh no' -n "
390 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000391 ssh_program, verbose_mode ? " -v" : "",
392 suser, host, cmd, src,
393 tuser ? tuser : "", tuser ? "@" : "",
394 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100395 } else {
396 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000397 snprintf(bp, len,
398 "exec %s%s -x -o'FallBackToRsh no' -n %s "
399 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000400 ssh_program, verbose_mode ? " -v" : "",
401 host, cmd, src,
402 tuser ? tuser : "", tuser ? "@" : "",
403 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100404 }
Damien Miller95def091999-11-25 00:26:21 +1100405 if (verbose_mode)
406 fprintf(stderr, "Executing: %s\n", bp);
407 (void) system(bp);
408 (void) xfree(bp);
409 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410 if (remin == -1) {
411 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100412 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000413 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100414 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000415 if (do_cmd(host, tuser, bp, &remin,
416 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100417 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418 if (response() < 0)
419 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100420 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421 }
Damien Miller95def091999-11-25 00:26:21 +1100422 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423 }
424 }
425}
426
427void
428tolocal(argc, argv)
429 int argc;
430 char *argv[];
431{
432 int i, len;
433 char *bp, *host, *src, *suser;
434
435 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100436 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000438 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000440 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000441 iamrecursive ? " -r" : "", pflag ? " -p" : "",
442 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100443 if (verbose_mode)
444 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 if (system(bp))
446 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100447 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000448 continue;
449 }
450 *src++ = 0;
451 if (*src == 0)
452 src = ".";
453 if ((host = strchr(argv[i], '@')) == NULL) {
454 host = argv[i];
455 suser = NULL;
456 } else {
457 *host++ = 0;
458 suser = argv[i];
459 if (*suser == '\0')
460 suser = pwd->pw_name;
461 else if (!okname(suser))
462 continue;
463 }
Damien Miller34132e52000-01-14 15:45:46 +1100464 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100466 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000467 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000468 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100469 (void) xfree(bp);
470 ++errs;
471 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472 }
Damien Miller95def091999-11-25 00:26:21 +1100473 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100475 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476 remin = remout = -1;
477 }
478}
479
480void
481source(argc, argv)
482 int argc;
483 char *argv[];
484{
485 struct stat stb;
486 static BUF buffer;
487 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000488 off_t i, amt, result;
489 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000491 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492
493 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100494 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000496 len = strlen(name);
497 while (len > 1 && name[len-1] == '/')
498 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499 if ((fd = open(name, O_RDONLY, 0)) < 0)
500 goto syserr;
501 if (fstat(fd, &stb) < 0) {
502syserr: run_err("%s: %s", name, strerror(errno));
503 goto next;
504 }
505 switch (stb.st_mode & S_IFMT) {
506 case S_IFREG:
507 break;
508 case S_IFDIR:
509 if (iamrecursive) {
510 rsource(name, &stb);
511 goto next;
512 }
513 /* FALLTHROUGH */
514 default:
515 run_err("%s: not a regular file", name);
516 goto next;
517 }
518 if ((last = strrchr(name, '/')) == NULL)
519 last = name;
520 else
521 ++last;
522 curfile = last;
523 if (pflag) {
524 /*
525 * Make it compatible with possible future
526 * versions expecting microseconds.
527 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000528 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000529 (u_long) stb.st_mtime,
530 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000531 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532 if (response() < 0)
533 goto next;
534 }
535#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100536#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000537 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000538 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000539 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100540#else
541 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000542 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100543 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000544 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100545#endif
546
Damien Miller95def091999-11-25 00:26:21 +1100547 if (verbose_mode) {
548 fprintf(stderr, "Sending file modes: %s", buf);
549 fflush(stderr);
550 }
Damien Miller35dabd02000-05-01 21:10:33 +1000551 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552 if (response() < 0)
553 goto next;
554 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100555next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 continue;
557 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558 if (showprogress) {
559 totalbytes = stb.st_size;
560 progressmeter(-1);
561 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562 /* Keep writing after an error so that we stay sync'd up. */
563 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
564 amt = bp->cnt;
565 if (i + amt > stb.st_size)
566 amt = stb.st_size - i;
567 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000568 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 if (result != amt)
570 haderr = result >= 0 ? EIO : errno;
571 }
572 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000573 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574 else {
Damien Miller864ea591999-12-15 11:04:25 +1100575 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 if (result != amt)
577 haderr = result >= 0 ? EIO : errno;
578 statbytes += result;
579 }
580 }
Damien Miller95def091999-11-25 00:26:21 +1100581 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582 progressmeter(1);
583
584 if (close(fd) < 0 && !haderr)
585 haderr = errno;
586 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000587 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 else
589 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100590 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 }
592}
593
594void
595rsource(name, statp)
596 char *name;
597 struct stat *statp;
598{
599 DIR *dirp;
600 struct dirent *dp;
601 char *last, *vect[1], path[1100];
602
603 if (!(dirp = opendir(name))) {
604 run_err("%s: %s", name, strerror(errno));
605 return;
606 }
607 last = strrchr(name, '/');
608 if (last == 0)
609 last = name;
610 else
611 last++;
612 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000613 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000614 (u_long) statp->st_mtime,
615 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000616 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617 if (response() < 0) {
618 closedir(dirp);
619 return;
620 }
621 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000622 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000623 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100624 if (verbose_mode)
625 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000626 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627 if (response() < 0) {
628 closedir(dirp);
629 return;
630 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000631 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632 if (dp->d_ino == 0)
633 continue;
634 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
635 continue;
636 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
637 run_err("%s/%s: name too long", name, dp->d_name);
638 continue;
639 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000640 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641 vect[0] = path;
642 source(1, vect);
643 }
Damien Miller95def091999-11-25 00:26:21 +1100644 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000645 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100646 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647}
648
649void
650sink(argc, argv)
651 int argc;
652 char *argv[];
653{
654 static BUF buffer;
655 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100656 enum {
657 YES, NO, DISPLAYED
658 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659 BUF *bp;
660 off_t i, j;
661 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100662 off_t size;
663 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100665 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000666
Ben Lindstromf719a202001-04-14 23:14:22 +0000667#define atime tv[0]
668#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000669#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670
671 setimes = targisdir = 0;
672 mask = umask(0);
673 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100674 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675 if (argc != 1) {
676 run_err("ambiguous target");
677 exit(1);
678 }
679 targ = *argv;
680 if (targetshouldbedirectory)
681 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100682
Damien Miller35dabd02000-05-01 21:10:33 +1000683 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
685 targisdir = 1;
686 for (first = 1;; first = 0) {
687 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000688 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689 return;
690 if (*cp++ == '\n')
691 SCREWUP("unexpected <newline>");
692 do {
Damien Millere247cc42000-05-07 12:03:14 +1000693 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694 SCREWUP("lost connection");
695 *cp++ = ch;
696 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
697 *cp = 0;
698
699 if (buf[0] == '\01' || buf[0] == '\02') {
700 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000701 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000702 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703 if (buf[0] == '\02')
704 exit(1);
705 ++errs;
706 continue;
707 }
708 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000709 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 return;
711 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712 if (ch == '\n')
713 *--cp = 0;
714
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715 cp = buf;
716 if (*cp == 'T') {
717 setimes++;
718 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000719 mtime.tv_sec = strtol(cp, &cp, 10);
720 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000722 mtime.tv_usec = strtol(cp, &cp, 10);
723 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000725 atime.tv_sec = strtol(cp, &cp, 10);
726 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000728 atime.tv_usec = strtol(cp, &cp, 10);
729 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000731 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732 continue;
733 }
734 if (*cp != 'C' && *cp != 'D') {
735 /*
736 * Check for the case "rcp remote:foo\* local:bar".
737 * In this case, the line "No match." can be returned
738 * by the shell before the rcp command on the remote is
739 * executed so the ^Aerror_message convention isn't
740 * followed.
741 */
742 if (first) {
743 run_err("%s", cp);
744 exit(1);
745 }
746 SCREWUP("expected control record");
747 }
748 mode = 0;
749 for (++cp; cp < buf + 5; cp++) {
750 if (*cp < '0' || *cp > '7')
751 SCREWUP("bad mode");
752 mode = (mode << 3) | (*cp - '0');
753 }
754 if (*cp++ != ' ')
755 SCREWUP("mode not delimited");
756
Ben Lindstrombd472262001-03-29 00:39:55 +0000757 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000758 size = size * 10 + (*cp++ - '0');
759 if (*cp++ != ' ')
760 SCREWUP("size not delimited");
761 if (targisdir) {
762 static char *namebuf;
763 static int cursize;
764 size_t need;
765
766 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000767 if (need > cursize) {
768 if (namebuf)
769 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100770 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000771 cursize = need;
772 }
773 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000774 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775 np = namebuf;
776 } else
777 np = targ;
778 curfile = cp;
779 exists = stat(np, &stb) == 0;
780 if (buf[0] == 'D') {
781 int mod_flag = pflag;
782 if (exists) {
783 if (!S_ISDIR(stb.st_mode)) {
784 errno = ENOTDIR;
785 goto bad;
786 }
787 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100788 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789 } else {
Damien Miller95def091999-11-25 00:26:21 +1100790 /* Handle copying from a read-only
791 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792 mod_flag = 1;
793 if (mkdir(np, mode | S_IRWXU) < 0)
794 goto bad;
795 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000796 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797 sink(1, vect);
798 if (setimes) {
799 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100800 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100801 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100802 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803 }
804 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100805 (void) chmod(vect[0], mode);
806 if (vect[0])
807 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808 continue;
809 }
810 omode = mode;
811 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000812 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813bad: run_err("%s: %s", np, strerror(errno));
814 continue;
815 }
Damien Miller35dabd02000-05-01 21:10:33 +1000816 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100818 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 continue;
820 }
821 cp = bp->buf;
822 wrerr = NO;
823
824 if (showprogress) {
825 totalbytes = size;
826 progressmeter(-1);
827 }
828 statbytes = 0;
829 for (count = i = 0; i < size; i += 4096) {
830 amt = 4096;
831 if (i + amt > size)
832 amt = size - i;
833 count += amt;
834 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100835 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000836 if (j == -1 && (errno == EINTR ||
837 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100838 continue;
839 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000841 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842 exit(1);
843 }
844 amt -= j;
845 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100846 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847 } while (amt > 0);
848 if (count == bp->cnt) {
849 /* Keep reading so we stay sync'd up. */
850 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000851 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852 if (j != count) {
853 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100854 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 }
856 }
857 count = 0;
858 cp = bp->buf;
859 }
860 }
861 if (showprogress)
862 progressmeter(1);
863 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000864 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100866 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 if (ftruncate(ofd, size)) {
869 run_err("%s: truncate: %s", np, strerror(errno));
870 wrerr = DISPLAYED;
871 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872 if (pflag) {
873 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100874#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100876#else /* HAVE_FCHMOD */
877 if (chmod(np, omode))
878#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000880 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881 } else {
882 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100883#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100885#else /* HAVE_FCHMOD */
886 if (chmod(np, omode & ~mask))
887#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000889 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890 }
Damien Millerbe484b52000-07-15 14:14:16 +1000891 if (close(ofd) == -1) {
892 wrerr = YES;
893 wrerrno = errno;
894 }
Damien Miller95def091999-11-25 00:26:21 +1100895 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 if (setimes && wrerr == NO) {
897 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100898 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000900 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 wrerr = DISPLAYED;
902 }
903 }
Damien Miller95def091999-11-25 00:26:21 +1100904 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905 case YES:
906 run_err("%s: %s", np, strerror(wrerrno));
907 break;
908 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000909 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910 break;
911 case DISPLAYED:
912 break;
913 }
914 }
915screwup:
916 run_err("protocol error: %s", why);
917 exit(1);
918}
919
920int
921response()
922{
923 char ch, *cp, resp, rbuf[2048];
924
Damien Millere247cc42000-05-07 12:03:14 +1000925 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926 lostconn(0);
927
928 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100929 switch (resp) {
930 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931 return (0);
932 default:
933 *cp++ = resp;
934 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100935 case 1: /* error, followed by error msg */
936 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937 do {
Damien Millere247cc42000-05-07 12:03:14 +1000938 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939 lostconn(0);
940 *cp++ = ch;
941 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
942
943 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000944 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945 ++errs;
946 if (resp == 1)
947 return (-1);
948 exit(1);
949 }
950 /* NOTREACHED */
951}
952
953void
954usage()
955{
Damien Millerad833b32000-08-23 10:46:23 +1000956 (void) fprintf(stderr, "usage: scp "
Ben Lindstromdc00c932001-07-18 16:50:33 +0000957 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] "
958 "[-o option] f1 f2\n"
Ben Lindstromff2618c2001-03-29 00:43:54 +0000959 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960 exit(1);
961}
962
963void
Damien Miller95def091999-11-25 00:26:21 +1100964run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965{
966 static FILE *fp;
967 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
969 ++errs;
970 if (fp == NULL && !(fp = fdopen(remout, "w")))
971 return;
Damien Miller95def091999-11-25 00:26:21 +1100972 (void) fprintf(fp, "%c", 0x01);
973 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100974 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100975 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100976 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100977 (void) fprintf(fp, "\n");
978 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100981 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100982 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100983 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100984 fprintf(stderr, "\n");
985 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986}
987
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988void
989verifydir(cp)
990 char *cp;
991{
992 struct stat stb;
993
994 if (!stat(cp, &stb)) {
995 if (S_ISDIR(stb.st_mode))
996 return;
997 errno = ENOTDIR;
998 }
999 run_err("%s: %s", cp, strerror(errno));
1000 exit(1);
1001}
1002
1003int
1004okname(cp0)
1005 char *cp0;
1006{
1007 int c;
1008 char *cp;
1009
1010 cp = cp0;
1011 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001012 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013 if (c & 0200)
1014 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001015 if (!isalpha(c) && !isdigit(c) &&
1016 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017 goto bad;
1018 } while (*++cp);
1019 return (1);
1020
Damien Miller98c7ad62000-03-09 21:27:49 +11001021bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001022 return (0);
1023}
1024
1025BUF *
1026allocbuf(bp, fd, blksize)
1027 BUF *bp;
1028 int fd, blksize;
1029{
1030 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001031#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032 struct stat stb;
1033
1034 if (fstat(fd, &stb) < 0) {
1035 run_err("fstat: %s", strerror(errno));
1036 return (0);
1037 }
Damien Miller95def091999-11-25 00:26:21 +11001038 if (stb.st_blksize == 0)
1039 size = blksize;
1040 else
1041 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001042 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001043#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001044 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001045#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046 if (bp->cnt >= size)
1047 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001048 if (bp->buf == NULL)
1049 bp->buf = xmalloc(size);
1050 else
1051 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052 bp->cnt = size;
1053 return (bp);
1054}
1055
1056void
1057lostconn(signo)
1058 int signo;
1059{
1060 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001061 write(STDERR_FILENO, "lost connection\n", 16);
1062 if (signo)
1063 _exit(1);
1064 else
1065 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066}
1067
Ben Lindstrombba81212001-06-25 05:01:22 +00001068static void
Damien Miller7684ee12000-03-17 23:40:15 +11001069updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070{
1071 int save_errno = errno;
1072
1073 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001074 signal(SIGALRM, updateprogressmeter);
1075 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076 errno = save_errno;
1077}
1078
Ben Lindstrombba81212001-06-25 05:01:22 +00001079static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001080foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001081{
1082 static pid_t pgrp = -1;
1083 int ctty_pgrp;
1084
1085 if (pgrp == -1)
1086 pgrp = getpgrp();
1087
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001088#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001089 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1090 ctty_pgrp == pgrp);
1091#else
Damien Miller95def091999-11-25 00:26:21 +11001092 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1093 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001094#endif
Damien Miller81428f91999-11-18 09:28:11 +11001095}
1096
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097void
1098progressmeter(int flag)
1099{
1100 static const char prefixes[] = " KMGTP";
1101 static struct timeval lastupdate;
1102 static off_t lastsize;
1103 struct timeval now, td, wait;
1104 off_t cursize, abbrevsize;
1105 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001106 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107 char buf[256];
1108
1109 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001110 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001111 lastupdate = start;
1112 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001113 }
Damien Miller81428f91999-11-18 09:28:11 +11001114 if (foregroundproc() == 0)
1115 return;
1116
Damien Miller95def091999-11-25 00:26:21 +11001117 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001119 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001120 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121 ratio = MAX(ratio, 0);
1122 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001123 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124 ratio = 100;
1125
Damien Miller95def091999-11-25 00:26:21 +11001126 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
1128 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001129 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130 if (barlength > 0) {
1131 i = barlength * ratio / 100;
1132 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001133 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135 i = 0;
1136 abbrevsize = cursize;
1137 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1138 i++;
1139 abbrevsize >>= 10;
1140 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001141 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001142 (unsigned long) abbrevsize, prefixes[i],
1143 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
1145 timersub(&now, &lastupdate, &wait);
1146 if (cursize > lastsize) {
1147 lastupdate = now;
1148 lastsize = cursize;
1149 if (wait.tv_sec >= STALLTIME) {
1150 start.tv_sec += wait.tv_sec;
1151 start.tv_usec += wait.tv_usec;
1152 }
1153 wait.tv_sec = 0;
1154 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 timersub(&now, &start, &td);
1156 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1157
Kevin Steves7d00ba42000-12-15 23:03:10 +00001158 if (flag != 1 &&
1159 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001161 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 } else if (wait.tv_sec >= STALLTIME) {
1163 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001164 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001166 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001167 remaining = (int)(totalbytes / (statbytes / elapsed) -
1168 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001169 else
1170 remaining = elapsed;
1171
Damien Miller01ab4a21999-10-28 15:23:30 +10001172 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 if (i)
1174 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001175 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176 else
1177 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001178 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179 i = remaining % 3600;
1180 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001181 "%02d:%02d%s", i / 60, i % 60,
1182 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 }
1184 atomicio(write, fileno(stdout), buf, strlen(buf));
1185
1186 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001187 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001188 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001190 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001191 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 statbytes = 0;
1193 }
1194}
1195
1196int
1197getttywidth(void)
1198{
1199 struct winsize winsize;
1200
1201 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001202 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 else
Damien Miller95def091999-11-25 00:26:21 +11001204 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205}