blob: 4708f0ed4a178dc81c5741a05527dca9c40f0a99 [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 Lindstrom958d9f62001-08-06 22:48:19 +000078RCSID("$OpenBSD: scp.c,v 1.79 2001/08/06 19:47:05 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
Ben Lindstrombba81212001-06-25 05:01:22 +0000217int main(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100218int response(void);
219void rsource(char *, struct stat *);
220void sink(int, char *[]);
221void source(int, char *[]);
222void tolocal(int, char *[]);
223void toremote(char *, int, char *[]);
224void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
226int
227main(argc, argv)
228 int argc;
229 char *argv[];
230{
231 int ch, fflag, tflag;
232 char *targ;
233 extern char *optarg;
234 extern int optind;
235
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000236 __progname = get_progname(argv[0]);
237
Damien Miller874d77b2000-10-14 16:23:11 +1100238 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000239 addargs(&args, "ssh"); /* overwritten with ssh_program */
240 addargs(&args, "-x");
241 addargs(&args, "-oFallBackToRsh no");
Damien Miller874d77b2000-10-14 16:23:11 +1100242
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000244 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100245 switch (ch) {
246 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100247 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100248 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100249 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000250 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100251 break;
252 case 'o':
253 case 'c':
254 case 'i':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000255 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100256 break;
257 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000258 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100259 break;
260 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000261 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100262 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263 case 'p':
264 pflag = 1;
265 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266 case 'r':
267 iamrecursive = 1;
268 break;
Damien Millerad833b32000-08-23 10:46:23 +1000269 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100270 ssh_program = xstrdup(optarg);
271 break;
272 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000273 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100274 verbose_mode = 1;
275 break;
276 case 'q':
277 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000278 break;
279
Damien Miller95def091999-11-25 00:26:21 +1100280 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281 case 'd':
282 targetshouldbedirectory = 1;
283 break;
Damien Miller95def091999-11-25 00:26:21 +1100284 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285 iamremote = 1;
286 fflag = 1;
287 break;
Damien Miller95def091999-11-25 00:26:21 +1100288 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289 iamremote = 1;
290 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000291#ifdef HAVE_CYGWIN
292 setmode(0, O_BINARY);
293#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295 default:
296 usage();
297 }
298 argc -= optind;
299 argv += optind;
300
301 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100302 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305 showprogress = 0;
306
307 remin = STDIN_FILENO;
308 remout = STDOUT_FILENO;
309
Kevin Stevesef4eea92001-02-05 12:42:17 +0000310 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100311 /* Follow "protocol", send data. */
312 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313 source(argc, argv);
314 exit(errs != 0);
315 }
Damien Miller95def091999-11-25 00:26:21 +1100316 if (tflag) {
317 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318 sink(argc, argv);
319 exit(errs != 0);
320 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321 if (argc < 2)
322 usage();
323 if (argc > 2)
324 targetshouldbedirectory = 1;
325
326 remin = remout = -1;
327 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000328 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
329 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000330 iamrecursive ? " -r" : "", pflag ? " -p" : "",
331 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
335 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
336 toremote(targ, argc, argv);
337 else {
Damien Miller95def091999-11-25 00:26:21 +1100338 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339 if (targetshouldbedirectory)
340 verifydir(argv[argc - 1]);
341 }
342 exit(errs != 0);
343}
344
345void
346toremote(targ, argc, argv)
347 char *targ, *argv[];
348 int argc;
349{
350 int i, len;
351 char *bp, *host, *src, *suser, *thost, *tuser;
352
353 *targ++ = 0;
354 if (*targ == 0)
355 targ = ".";
356
357 if ((thost = strchr(argv[argc - 1], '@'))) {
358 /* user@host */
359 *thost++ = 0;
360 tuser = argv[argc - 1];
361 if (*tuser == '\0')
362 tuser = NULL;
363 else if (!okname(tuser))
364 exit(1);
365 } else {
366 thost = argv[argc - 1];
367 tuser = NULL;
368 }
369
370 for (i = 0; i < argc - 1; i++) {
371 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100372 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373 *src++ = 0;
374 if (*src == 0)
375 src = ".";
376 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000377 len = strlen(ssh_program) + strlen(argv[i]) +
378 strlen(src) + (tuser ? strlen(tuser) : 0) +
379 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100380 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381 if (host) {
382 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100383 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384 suser = argv[i];
385 if (*suser == '\0')
386 suser = pwd->pw_name;
387 else if (!okname(suser))
388 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000389 snprintf(bp, len,
390 "%s%s -x -o'FallBackToRsh no' -n "
391 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000392 ssh_program, verbose_mode ? " -v" : "",
393 suser, host, cmd, src,
394 tuser ? tuser : "", tuser ? "@" : "",
395 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100396 } else {
397 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000398 snprintf(bp, len,
399 "exec %s%s -x -o'FallBackToRsh no' -n %s "
400 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000401 ssh_program, verbose_mode ? " -v" : "",
402 host, cmd, src,
403 tuser ? tuser : "", tuser ? "@" : "",
404 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100405 }
Damien Miller95def091999-11-25 00:26:21 +1100406 if (verbose_mode)
407 fprintf(stderr, "Executing: %s\n", bp);
408 (void) system(bp);
409 (void) xfree(bp);
410 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411 if (remin == -1) {
412 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100413 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000414 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100415 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000416 if (do_cmd(host, tuser, bp, &remin,
417 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100418 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419 if (response() < 0)
420 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100421 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 }
Damien Miller95def091999-11-25 00:26:21 +1100423 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424 }
425 }
426}
427
428void
429tolocal(argc, argv)
430 int argc;
431 char *argv[];
432{
433 int i, len;
434 char *bp, *host, *src, *suser;
435
436 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100437 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000439 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000441 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000442 iamrecursive ? " -r" : "", pflag ? " -p" : "",
443 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100444 if (verbose_mode)
445 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 if (system(bp))
447 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100448 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449 continue;
450 }
451 *src++ = 0;
452 if (*src == 0)
453 src = ".";
454 if ((host = strchr(argv[i], '@')) == NULL) {
455 host = argv[i];
456 suser = NULL;
457 } else {
458 *host++ = 0;
459 suser = argv[i];
460 if (*suser == '\0')
461 suser = pwd->pw_name;
462 else if (!okname(suser))
463 continue;
464 }
Damien Miller34132e52000-01-14 15:45:46 +1100465 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100467 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000468 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000469 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100470 (void) xfree(bp);
471 ++errs;
472 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 }
Damien Miller95def091999-11-25 00:26:21 +1100474 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100476 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477 remin = remout = -1;
478 }
479}
480
481void
482source(argc, argv)
483 int argc;
484 char *argv[];
485{
486 struct stat stb;
487 static BUF buffer;
488 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000489 off_t i, amt, result;
490 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000492 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
494 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100495 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000497 len = strlen(name);
498 while (len > 1 && name[len-1] == '/')
499 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 if ((fd = open(name, O_RDONLY, 0)) < 0)
501 goto syserr;
502 if (fstat(fd, &stb) < 0) {
503syserr: run_err("%s: %s", name, strerror(errno));
504 goto next;
505 }
506 switch (stb.st_mode & S_IFMT) {
507 case S_IFREG:
508 break;
509 case S_IFDIR:
510 if (iamrecursive) {
511 rsource(name, &stb);
512 goto next;
513 }
514 /* FALLTHROUGH */
515 default:
516 run_err("%s: not a regular file", name);
517 goto next;
518 }
519 if ((last = strrchr(name, '/')) == NULL)
520 last = name;
521 else
522 ++last;
523 curfile = last;
524 if (pflag) {
525 /*
526 * Make it compatible with possible future
527 * versions expecting microseconds.
528 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000529 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000530 (u_long) stb.st_mtime,
531 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000532 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533 if (response() < 0)
534 goto next;
535 }
536#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100537#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000538 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000539 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000540 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100541#else
542 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000543 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100544 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000545 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100546#endif
547
Damien Miller95def091999-11-25 00:26:21 +1100548 if (verbose_mode) {
549 fprintf(stderr, "Sending file modes: %s", buf);
550 fflush(stderr);
551 }
Damien Miller35dabd02000-05-01 21:10:33 +1000552 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553 if (response() < 0)
554 goto next;
555 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100556next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 continue;
558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 if (showprogress) {
560 totalbytes = stb.st_size;
561 progressmeter(-1);
562 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563 /* Keep writing after an error so that we stay sync'd up. */
564 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
565 amt = bp->cnt;
566 if (i + amt > stb.st_size)
567 amt = stb.st_size - i;
568 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000569 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 if (result != amt)
571 haderr = result >= 0 ? EIO : errno;
572 }
573 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000574 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575 else {
Damien Miller864ea591999-12-15 11:04:25 +1100576 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 if (result != amt)
578 haderr = result >= 0 ? EIO : errno;
579 statbytes += result;
580 }
581 }
Damien Miller95def091999-11-25 00:26:21 +1100582 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 progressmeter(1);
584
585 if (close(fd) < 0 && !haderr)
586 haderr = errno;
587 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000588 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 else
590 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100591 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592 }
593}
594
595void
596rsource(name, statp)
597 char *name;
598 struct stat *statp;
599{
600 DIR *dirp;
601 struct dirent *dp;
602 char *last, *vect[1], path[1100];
603
604 if (!(dirp = opendir(name))) {
605 run_err("%s: %s", name, strerror(errno));
606 return;
607 }
608 last = strrchr(name, '/');
609 if (last == 0)
610 last = name;
611 else
612 last++;
613 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000614 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000615 (u_long) statp->st_mtime,
616 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000617 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618 if (response() < 0) {
619 closedir(dirp);
620 return;
621 }
622 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000623 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000624 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100625 if (verbose_mode)
626 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000627 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628 if (response() < 0) {
629 closedir(dirp);
630 return;
631 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000632 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633 if (dp->d_ino == 0)
634 continue;
635 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
636 continue;
637 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
638 run_err("%s/%s: name too long", name, dp->d_name);
639 continue;
640 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000641 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642 vect[0] = path;
643 source(1, vect);
644 }
Damien Miller95def091999-11-25 00:26:21 +1100645 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000646 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100647 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648}
649
650void
651sink(argc, argv)
652 int argc;
653 char *argv[];
654{
655 static BUF buffer;
656 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100657 enum {
658 YES, NO, DISPLAYED
659 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660 BUF *bp;
661 off_t i, j;
662 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100663 off_t size;
664 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100666 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
Ben Lindstromf719a202001-04-14 23:14:22 +0000668#define atime tv[0]
669#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000670#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671
672 setimes = targisdir = 0;
673 mask = umask(0);
674 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100675 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676 if (argc != 1) {
677 run_err("ambiguous target");
678 exit(1);
679 }
680 targ = *argv;
681 if (targetshouldbedirectory)
682 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100683
Damien Miller35dabd02000-05-01 21:10:33 +1000684 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
686 targisdir = 1;
687 for (first = 1;; first = 0) {
688 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000689 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690 return;
691 if (*cp++ == '\n')
692 SCREWUP("unexpected <newline>");
693 do {
Damien Millere247cc42000-05-07 12:03:14 +1000694 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695 SCREWUP("lost connection");
696 *cp++ = ch;
697 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
698 *cp = 0;
699
700 if (buf[0] == '\01' || buf[0] == '\02') {
701 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000702 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000703 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000704 if (buf[0] == '\02')
705 exit(1);
706 ++errs;
707 continue;
708 }
709 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000710 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711 return;
712 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713 if (ch == '\n')
714 *--cp = 0;
715
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716 cp = buf;
717 if (*cp == 'T') {
718 setimes++;
719 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000720 mtime.tv_sec = strtol(cp, &cp, 10);
721 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000723 mtime.tv_usec = strtol(cp, &cp, 10);
724 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000726 atime.tv_sec = strtol(cp, &cp, 10);
727 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000729 atime.tv_usec = strtol(cp, &cp, 10);
730 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000732 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 continue;
734 }
735 if (*cp != 'C' && *cp != 'D') {
736 /*
737 * Check for the case "rcp remote:foo\* local:bar".
738 * In this case, the line "No match." can be returned
739 * by the shell before the rcp command on the remote is
740 * executed so the ^Aerror_message convention isn't
741 * followed.
742 */
743 if (first) {
744 run_err("%s", cp);
745 exit(1);
746 }
747 SCREWUP("expected control record");
748 }
749 mode = 0;
750 for (++cp; cp < buf + 5; cp++) {
751 if (*cp < '0' || *cp > '7')
752 SCREWUP("bad mode");
753 mode = (mode << 3) | (*cp - '0');
754 }
755 if (*cp++ != ' ')
756 SCREWUP("mode not delimited");
757
Ben Lindstrombd472262001-03-29 00:39:55 +0000758 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759 size = size * 10 + (*cp++ - '0');
760 if (*cp++ != ' ')
761 SCREWUP("size not delimited");
762 if (targisdir) {
763 static char *namebuf;
764 static int cursize;
765 size_t need;
766
767 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000768 if (need > cursize) {
769 if (namebuf)
770 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100771 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000772 cursize = need;
773 }
774 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000775 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776 np = namebuf;
777 } else
778 np = targ;
779 curfile = cp;
780 exists = stat(np, &stb) == 0;
781 if (buf[0] == 'D') {
782 int mod_flag = pflag;
783 if (exists) {
784 if (!S_ISDIR(stb.st_mode)) {
785 errno = ENOTDIR;
786 goto bad;
787 }
788 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100789 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790 } else {
Damien Miller95def091999-11-25 00:26:21 +1100791 /* Handle copying from a read-only
792 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793 mod_flag = 1;
794 if (mkdir(np, mode | S_IRWXU) < 0)
795 goto bad;
796 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000797 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798 sink(1, vect);
799 if (setimes) {
800 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100801 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100802 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100803 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804 }
805 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100806 (void) chmod(vect[0], mode);
807 if (vect[0])
808 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809 continue;
810 }
811 omode = mode;
812 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000813 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814bad: run_err("%s: %s", np, strerror(errno));
815 continue;
816 }
Damien Miller35dabd02000-05-01 21:10:33 +1000817 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100819 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820 continue;
821 }
822 cp = bp->buf;
823 wrerr = NO;
824
825 if (showprogress) {
826 totalbytes = size;
827 progressmeter(-1);
828 }
829 statbytes = 0;
830 for (count = i = 0; i < size; i += 4096) {
831 amt = 4096;
832 if (i + amt > size)
833 amt = size - i;
834 count += amt;
835 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100836 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000837 if (j == -1 && (errno == EINTR ||
838 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100839 continue;
840 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000842 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843 exit(1);
844 }
845 amt -= j;
846 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100847 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 } while (amt > 0);
849 if (count == bp->cnt) {
850 /* Keep reading so we stay sync'd up. */
851 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000852 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 if (j != count) {
854 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100855 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856 }
857 }
858 count = 0;
859 cp = bp->buf;
860 }
861 }
862 if (showprogress)
863 progressmeter(1);
864 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000865 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100867 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869 if (ftruncate(ofd, size)) {
870 run_err("%s: truncate: %s", np, strerror(errno));
871 wrerr = DISPLAYED;
872 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873 if (pflag) {
874 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100875#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100877#else /* HAVE_FCHMOD */
878 if (chmod(np, omode))
879#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000881 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 } else {
883 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100884#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100886#else /* HAVE_FCHMOD */
887 if (chmod(np, omode & ~mask))
888#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000890 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 }
Damien Millerbe484b52000-07-15 14:14:16 +1000892 if (close(ofd) == -1) {
893 wrerr = YES;
894 wrerrno = errno;
895 }
Damien Miller95def091999-11-25 00:26:21 +1100896 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897 if (setimes && wrerr == NO) {
898 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100899 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000901 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902 wrerr = DISPLAYED;
903 }
904 }
Damien Miller95def091999-11-25 00:26:21 +1100905 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 case YES:
907 run_err("%s: %s", np, strerror(wrerrno));
908 break;
909 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000910 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911 break;
912 case DISPLAYED:
913 break;
914 }
915 }
916screwup:
917 run_err("protocol error: %s", why);
918 exit(1);
919}
920
921int
922response()
923{
924 char ch, *cp, resp, rbuf[2048];
925
Damien Millere247cc42000-05-07 12:03:14 +1000926 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927 lostconn(0);
928
929 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100930 switch (resp) {
931 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932 return (0);
933 default:
934 *cp++ = resp;
935 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100936 case 1: /* error, followed by error msg */
937 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 do {
Damien Millere247cc42000-05-07 12:03:14 +1000939 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940 lostconn(0);
941 *cp++ = ch;
942 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
943
944 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000945 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946 ++errs;
947 if (resp == 1)
948 return (-1);
949 exit(1);
950 }
951 /* NOTREACHED */
952}
953
954void
955usage()
956{
Damien Millerad833b32000-08-23 10:46:23 +1000957 (void) fprintf(stderr, "usage: scp "
Ben Lindstromdc00c932001-07-18 16:50:33 +0000958 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] "
959 "[-o option] f1 f2\n"
Ben Lindstromff2618c2001-03-29 00:43:54 +0000960 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961 exit(1);
962}
963
964void
Damien Miller95def091999-11-25 00:26:21 +1100965run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966{
967 static FILE *fp;
968 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969
970 ++errs;
971 if (fp == NULL && !(fp = fdopen(remout, "w")))
972 return;
Damien Miller95def091999-11-25 00:26:21 +1100973 (void) fprintf(fp, "%c", 0x01);
974 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100975 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100976 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100977 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100978 (void) fprintf(fp, "\n");
979 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100982 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100983 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100984 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100985 fprintf(stderr, "\n");
986 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987}
988
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989void
990verifydir(cp)
991 char *cp;
992{
993 struct stat stb;
994
995 if (!stat(cp, &stb)) {
996 if (S_ISDIR(stb.st_mode))
997 return;
998 errno = ENOTDIR;
999 }
1000 run_err("%s: %s", cp, strerror(errno));
1001 exit(1);
1002}
1003
1004int
1005okname(cp0)
1006 char *cp0;
1007{
1008 int c;
1009 char *cp;
1010
1011 cp = cp0;
1012 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001013 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014 if (c & 0200)
1015 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001016 if (!isalpha(c) && !isdigit(c) &&
1017 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018 goto bad;
1019 } while (*++cp);
1020 return (1);
1021
Damien Miller98c7ad62000-03-09 21:27:49 +11001022bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023 return (0);
1024}
1025
1026BUF *
1027allocbuf(bp, fd, blksize)
1028 BUF *bp;
1029 int fd, blksize;
1030{
1031 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001032#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033 struct stat stb;
1034
1035 if (fstat(fd, &stb) < 0) {
1036 run_err("fstat: %s", strerror(errno));
1037 return (0);
1038 }
Damien Miller95def091999-11-25 00:26:21 +11001039 if (stb.st_blksize == 0)
1040 size = blksize;
1041 else
1042 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001043 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001044#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001045 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001046#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047 if (bp->cnt >= size)
1048 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001049 if (bp->buf == NULL)
1050 bp->buf = xmalloc(size);
1051 else
1052 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053 bp->cnt = size;
1054 return (bp);
1055}
1056
1057void
1058lostconn(signo)
1059 int signo;
1060{
1061 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001062 write(STDERR_FILENO, "lost connection\n", 16);
1063 if (signo)
1064 _exit(1);
1065 else
1066 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067}
1068
Ben Lindstrombba81212001-06-25 05:01:22 +00001069static void
Damien Miller7684ee12000-03-17 23:40:15 +11001070updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071{
1072 int save_errno = errno;
1073
1074 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001075 signal(SIGALRM, updateprogressmeter);
1076 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077 errno = save_errno;
1078}
1079
Ben Lindstrombba81212001-06-25 05:01:22 +00001080static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001081foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001082{
1083 static pid_t pgrp = -1;
1084 int ctty_pgrp;
1085
1086 if (pgrp == -1)
1087 pgrp = getpgrp();
1088
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001089#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001090 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1091 ctty_pgrp == pgrp);
1092#else
Damien Miller95def091999-11-25 00:26:21 +11001093 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1094 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001095#endif
Damien Miller81428f91999-11-18 09:28:11 +11001096}
1097
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098void
1099progressmeter(int flag)
1100{
1101 static const char prefixes[] = " KMGTP";
1102 static struct timeval lastupdate;
1103 static off_t lastsize;
1104 struct timeval now, td, wait;
1105 off_t cursize, abbrevsize;
1106 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001107 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108 char buf[256];
1109
1110 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001111 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112 lastupdate = start;
1113 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001114 }
Damien Miller81428f91999-11-18 09:28:11 +11001115 if (foregroundproc() == 0)
1116 return;
1117
Damien Miller95def091999-11-25 00:26:21 +11001118 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001120 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001121 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122 ratio = MAX(ratio, 0);
1123 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001124 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125 ratio = 100;
1126
Damien Miller95def091999-11-25 00:26:21 +11001127 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128
1129 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001130 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131 if (barlength > 0) {
1132 i = barlength * ratio / 100;
1133 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001134 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136 i = 0;
1137 abbrevsize = cursize;
1138 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1139 i++;
1140 abbrevsize >>= 10;
1141 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001142 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001143 (unsigned long) abbrevsize, prefixes[i],
1144 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
1146 timersub(&now, &lastupdate, &wait);
1147 if (cursize > lastsize) {
1148 lastupdate = now;
1149 lastsize = cursize;
1150 if (wait.tv_sec >= STALLTIME) {
1151 start.tv_sec += wait.tv_sec;
1152 start.tv_usec += wait.tv_usec;
1153 }
1154 wait.tv_sec = 0;
1155 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 timersub(&now, &start, &td);
1157 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1158
Kevin Steves7d00ba42000-12-15 23:03:10 +00001159 if (flag != 1 &&
1160 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001162 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163 } else if (wait.tv_sec >= STALLTIME) {
1164 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001165 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001167 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001168 remaining = (int)(totalbytes / (statbytes / elapsed) -
1169 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001170 else
1171 remaining = elapsed;
1172
Damien Miller01ab4a21999-10-28 15:23:30 +10001173 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174 if (i)
1175 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001176 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177 else
1178 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001179 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180 i = remaining % 3600;
1181 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001182 "%02d:%02d%s", i / 60, i % 60,
1183 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 }
1185 atomicio(write, fileno(stdout), buf, strlen(buf));
1186
1187 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001188 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001189 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001191 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001192 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 statbytes = 0;
1194 }
1195}
1196
1197int
1198getttywidth(void)
1199{
1200 struct winsize winsize;
1201
1202 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001203 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204 else
Damien Miller95def091999-11-25 00:26:21 +11001205 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206}