blob: 921ffeedc96daf550e2f5e08bed06f20c53658dd [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 Lindstromcb72e4f2002-06-21 00:41:51 +000078RCSID("$OpenBSD: scp.c,v 1.91 2002/06/19 00:27:55 deraadt 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
97/* Visual statistics about files as they are transferred. */
98void progressmeter(int);
99
100/* Returns width of the terminal (for progress meter calculations). */
101int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000102int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Ben Lindstrom387c4722001-05-08 20:27:25 +0000104/* Struct for addargs */
105arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +1100106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107/* Time a transfer started. */
108static struct timeval start;
109
110/* Number of bytes of current file transferred so far. */
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000111volatile off_t statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100114off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
116/* Name of current file being transferred. */
117char *curfile;
118
119/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100120int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122/* This is set to zero if the progressmeter is not desired. */
123int showprogress = 1;
124
Damien Millerad833b32000-08-23 10:46:23 +1000125/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000126char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * This function executes the given command as the specified user on the
130 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
131 * assigns the input and output file descriptors on success.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller4af51302000-04-16 11:18:38 +1000134int
Damien Millerad833b32000-08-23 10:46:23 +1000135do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136{
Damien Miller95def091999-11-25 00:26:21 +1100137 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller95def091999-11-25 00:26:21 +1100139 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000140 fprintf(stderr,
141 "Executing: program %s host %s, user %s, command %s\n",
142 ssh_program, host,
143 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller5428f641999-11-25 11:54:57 +1100145 /*
146 * Reserve two descriptors so that the real pipes won't get
147 * descriptors 0 and 1 because that will screw up dup2 below.
148 */
Damien Miller95def091999-11-25 00:26:21 +1100149 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 /* Create a socket pair for communicating with ssh. */
152 if (pipe(pin) < 0)
153 fatal("pipe: %s", strerror(errno));
154 if (pipe(pout) < 0)
155 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 /* Free the reserved descriptors. */
158 close(reserved[0]);
159 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100162 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100163 /* Child. */
164 close(pin[1]);
165 close(pout[0]);
166 dup2(pin[0], 0);
167 dup2(pout[1], 1);
168 close(pin[0]);
169 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Damien Miller874d77b2000-10-14 16:23:11 +1100171 args.list[0] = ssh_program;
172 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000173 addargs(&args, "-l%s", remuser);
174 addargs(&args, "%s", host);
175 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100176
Damien Miller874d77b2000-10-14 16:23:11 +1100177 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000178 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100179 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180 }
Damien Miller95def091999-11-25 00:26:21 +1100181 /* Parent. Close the other side, and return the local side. */
182 close(pin[0]);
183 *fdout = pin[1];
184 close(pout[1]);
185 *fdin = pout[0];
186 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187}
188
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189typedef struct {
190 int cnt;
191 char *buf;
192} BUF;
193
Damien Miller95def091999-11-25 00:26:21 +1100194BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100195void lostconn(int);
196void nospace(void);
197int okname(char *);
198void run_err(const char *,...);
199void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100202uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203int errs, remin, remout;
204int pflag, iamremote, iamrecursive, targetshouldbedirectory;
205
206#define CMDNEEDS 64
207char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
208
Damien Miller95def091999-11-25 00:26:21 +1100209int response(void);
210void rsource(char *, struct stat *);
211void sink(int, char *[]);
212void source(int, char *[]);
213void tolocal(int, char *[]);
214void toremote(char *, int, char *[]);
215void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
217int
218main(argc, argv)
219 int argc;
220 char *argv[];
221{
222 int ch, fflag, tflag;
223 char *targ;
224 extern char *optarg;
225 extern int optind;
226
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000227 __progname = get_progname(argv[0]);
228
Damien Miller874d77b2000-10-14 16:23:11 +1100229 args.list = NULL;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000230 addargs(&args, "ssh"); /* overwritten with ssh_program */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000231 addargs(&args, "-x");
Ben Lindstrom4213c552001-09-12 18:45:09 +0000232 addargs(&args, "-oForwardAgent no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000233 addargs(&args, "-oClearAllForwardings yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100234
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235 fflag = tflag = 0;
Ben Lindstrom1e243242001-09-18 05:38:44 +0000236 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100237 switch (ch) {
238 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100239 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100240 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100241 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000242 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100243 break;
244 case 'o':
245 case 'c':
246 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000247 case 'F':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000248 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100249 break;
250 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000251 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100252 break;
253 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000254 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100255 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256 case 'p':
257 pflag = 1;
258 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259 case 'r':
260 iamrecursive = 1;
261 break;
Damien Millerad833b32000-08-23 10:46:23 +1000262 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100263 ssh_program = xstrdup(optarg);
264 break;
265 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000266 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100267 verbose_mode = 1;
268 break;
269 case 'q':
270 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000271 break;
272
Damien Miller95def091999-11-25 00:26:21 +1100273 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274 case 'd':
275 targetshouldbedirectory = 1;
276 break;
Damien Miller95def091999-11-25 00:26:21 +1100277 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278 iamremote = 1;
279 fflag = 1;
280 break;
Damien Miller95def091999-11-25 00:26:21 +1100281 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282 iamremote = 1;
283 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000284#ifdef HAVE_CYGWIN
285 setmode(0, O_BINARY);
286#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 default:
289 usage();
290 }
291 argc -= optind;
292 argv += optind;
293
294 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100295 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Damien Miller95def091999-11-25 00:26:21 +1100297 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298 showprogress = 0;
299
300 remin = STDIN_FILENO;
301 remout = STDOUT_FILENO;
302
Kevin Stevesef4eea92001-02-05 12:42:17 +0000303 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100304 /* Follow "protocol", send data. */
305 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306 source(argc, argv);
307 exit(errs != 0);
308 }
Damien Miller95def091999-11-25 00:26:21 +1100309 if (tflag) {
310 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311 sink(argc, argv);
312 exit(errs != 0);
313 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314 if (argc < 2)
315 usage();
316 if (argc > 2)
317 targetshouldbedirectory = 1;
318
319 remin = remout = -1;
320 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000321 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
322 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000323 iamrecursive ? " -r" : "", pflag ? " -p" : "",
324 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325
Damien Miller95def091999-11-25 00:26:21 +1100326 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
328 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
329 toremote(targ, argc, argv);
330 else {
Damien Miller95def091999-11-25 00:26:21 +1100331 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332 if (targetshouldbedirectory)
333 verifydir(argv[argc - 1]);
334 }
335 exit(errs != 0);
336}
337
338void
339toremote(targ, argc, argv)
340 char *targ, *argv[];
341 int argc;
342{
343 int i, len;
344 char *bp, *host, *src, *suser, *thost, *tuser;
345
346 *targ++ = 0;
347 if (*targ == 0)
348 targ = ".";
349
350 if ((thost = strchr(argv[argc - 1], '@'))) {
351 /* user@host */
352 *thost++ = 0;
353 tuser = argv[argc - 1];
354 if (*tuser == '\0')
355 tuser = NULL;
356 else if (!okname(tuser))
357 exit(1);
358 } else {
359 thost = argv[argc - 1];
360 tuser = NULL;
361 }
362
363 for (i = 0; i < argc - 1; i++) {
364 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100365 if (src) { /* remote to remote */
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000366 static char *ssh_options =
Ben Lindstromaf0c6d62002-06-09 20:06:29 +0000367 "-x -o'ClearAllForwardings yes'";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368 *src++ = 0;
369 if (*src == 0)
370 src = ".";
371 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000372 len = strlen(ssh_program) + strlen(argv[i]) +
373 strlen(src) + (tuser ? strlen(tuser) : 0) +
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000374 strlen(thost) + strlen(targ) +
375 strlen(ssh_options) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100376 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377 if (host) {
378 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100379 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380 suser = argv[i];
381 if (*suser == '\0')
382 suser = pwd->pw_name;
383 else if (!okname(suser))
384 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000385 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000386 "%s%s %s -n "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000387 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000388 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000389 ssh_options, suser, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000390 tuser ? tuser : "", tuser ? "@" : "",
391 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100392 } else {
393 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000394 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000395 "exec %s%s %s -n %s "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000396 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000397 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000398 ssh_options, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000399 tuser ? tuser : "", tuser ? "@" : "",
400 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100401 }
Damien Miller95def091999-11-25 00:26:21 +1100402 if (verbose_mode)
403 fprintf(stderr, "Executing: %s\n", bp);
404 (void) system(bp);
405 (void) xfree(bp);
406 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407 if (remin == -1) {
408 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100409 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000410 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100411 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000412 if (do_cmd(host, tuser, bp, &remin,
413 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100414 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415 if (response() < 0)
416 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100417 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418 }
Damien Miller95def091999-11-25 00:26:21 +1100419 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420 }
421 }
422}
423
424void
425tolocal(argc, argv)
426 int argc;
427 char *argv[];
428{
429 int i, len;
430 char *bp, *host, *src, *suser;
431
432 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100433 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000435 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000437 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000438 iamrecursive ? " -r" : "", pflag ? " -p" : "",
439 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100440 if (verbose_mode)
441 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442 if (system(bp))
443 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100444 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 continue;
446 }
447 *src++ = 0;
448 if (*src == 0)
449 src = ".";
450 if ((host = strchr(argv[i], '@')) == NULL) {
451 host = argv[i];
452 suser = NULL;
453 } else {
454 *host++ = 0;
455 suser = argv[i];
456 if (*suser == '\0')
457 suser = pwd->pw_name;
458 else if (!okname(suser))
459 continue;
460 }
Damien Miller34132e52000-01-14 15:45:46 +1100461 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100463 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000464 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000465 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100466 (void) xfree(bp);
467 ++errs;
468 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 }
Damien Miller95def091999-11-25 00:26:21 +1100470 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100472 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 remin = remout = -1;
474 }
475}
476
477void
478source(argc, argv)
479 int argc;
480 char *argv[];
481{
482 struct stat stb;
483 static BUF buffer;
484 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000485 off_t i, amt, result;
486 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000488 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
490 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100491 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000493 len = strlen(name);
494 while (len > 1 && name[len-1] == '/')
495 name[--len] = '\0';
Ben Lindstrom3e45e4c2001-10-03 17:30:58 +0000496 if (strchr(name, '\n') != NULL) {
497 run_err("%s: skipping, filename contains a newline",
498 name);
499 goto next;
500 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 if ((fd = open(name, O_RDONLY, 0)) < 0)
502 goto syserr;
503 if (fstat(fd, &stb) < 0) {
504syserr: run_err("%s: %s", name, strerror(errno));
505 goto next;
506 }
507 switch (stb.st_mode & S_IFMT) {
508 case S_IFREG:
509 break;
510 case S_IFDIR:
511 if (iamrecursive) {
512 rsource(name, &stb);
513 goto next;
514 }
515 /* FALLTHROUGH */
516 default:
517 run_err("%s: not a regular file", name);
518 goto next;
519 }
520 if ((last = strrchr(name, '/')) == NULL)
521 last = name;
522 else
523 ++last;
524 curfile = last;
525 if (pflag) {
526 /*
527 * Make it compatible with possible future
528 * versions expecting microseconds.
529 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000530 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000531 (u_long) stb.st_mtime,
532 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000533 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000534 if (response() < 0)
535 goto next;
536 }
537#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100538#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000539 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000540 (u_int) (stb.st_mode & FILEMODEMASK),
Kevin Steves399ec972002-03-05 18:59:45 +0000541 (long long)stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100542#else
543 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000544 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100545 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000546 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100547#endif
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,
Ben Lindstromde3895d2002-04-06 18:29:59 +0000775 strcmp(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
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000922response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000923{
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
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000955usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956{
Ben Lindstrom1e243242001-09-18 05:38:44 +0000957 (void) fprintf(stderr,
Ben Lindstrom45933dd2002-06-21 00:10:58 +0000958 "usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
959 " [-c cipher] [-i identity] [-o option]\n"
960 " [[user@]host1:]file1 [...] [[user@]host2:]file2\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;
Tim Rice13aae5e2001-10-21 17:53:58 -07001032#ifdef HAVE_STRUCT_STAT_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;
Tim Rice13aae5e2001-10-21 17:53:58 -07001044#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001045 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001046#endif /* HAVE_STRUCT_STAT_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);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001053 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001054 bp->cnt = size;
1055 return (bp);
1056}
1057
1058void
1059lostconn(signo)
1060 int signo;
1061{
1062 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001063 write(STDERR_FILENO, "lost connection\n", 16);
1064 if (signo)
1065 _exit(1);
1066 else
1067 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068}
1069
Ben Lindstrombba81212001-06-25 05:01:22 +00001070static void
Damien Miller7684ee12000-03-17 23:40:15 +11001071updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072{
1073 int save_errno = errno;
1074
1075 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001076 signal(SIGALRM, updateprogressmeter);
1077 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001078 errno = save_errno;
1079}
1080
Ben Lindstrombba81212001-06-25 05:01:22 +00001081static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001082foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001083{
1084 static pid_t pgrp = -1;
1085 int ctty_pgrp;
1086
1087 if (pgrp == -1)
1088 pgrp = getpgrp();
1089
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001090#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001091 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1092 ctty_pgrp == pgrp);
1093#else
Damien Miller95def091999-11-25 00:26:21 +11001094 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1095 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001096#endif
Damien Miller81428f91999-11-18 09:28:11 +11001097}
1098
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099void
1100progressmeter(int flag)
1101{
1102 static const char prefixes[] = " KMGTP";
1103 static struct timeval lastupdate;
1104 static off_t lastsize;
1105 struct timeval now, td, wait;
1106 off_t cursize, abbrevsize;
1107 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001108 int ratio, barlength, i, remaining;
Ben Lindstromcdb66e02002-04-02 20:17:43 +00001109 char buf[512];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110
1111 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001112 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113 lastupdate = start;
1114 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001115 }
Damien Miller81428f91999-11-18 09:28:11 +11001116 if (foregroundproc() == 0)
1117 return;
1118
Damien Miller95def091999-11-25 00:26:21 +11001119 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001121 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001122 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123 ratio = MAX(ratio, 0);
1124 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001125 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126 ratio = 100;
1127
Damien Miller95def091999-11-25 00:26:21 +11001128 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
1130 barlength = getttywidth() - 51;
1131 if (barlength > 0) {
1132 i = barlength * ratio / 100;
1133 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Ben Lindstromcdb66e02002-04-02 20:17:43 +00001134 "|%.*s%*s|", i,
1135 "*******************************************************"
1136 "*******************************************************"
1137 "*******************************************************"
1138 "*******************************************************"
1139 "*******************************************************"
1140 "*******************************************************"
1141 "*******************************************************",
1142 barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144 i = 0;
1145 abbrevsize = cursize;
1146 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1147 i++;
1148 abbrevsize >>= 10;
1149 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001150 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001151 (unsigned long) abbrevsize, prefixes[i],
1152 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153
1154 timersub(&now, &lastupdate, &wait);
1155 if (cursize > lastsize) {
1156 lastupdate = now;
1157 lastsize = cursize;
1158 if (wait.tv_sec >= STALLTIME) {
1159 start.tv_sec += wait.tv_sec;
1160 start.tv_usec += wait.tv_usec;
1161 }
1162 wait.tv_sec = 0;
1163 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 timersub(&now, &start, &td);
1165 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1166
Kevin Steves7d00ba42000-12-15 23:03:10 +00001167 if (flag != 1 &&
1168 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001170 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 } else if (wait.tv_sec >= STALLTIME) {
1172 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001173 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001175 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001176 remaining = (int)(totalbytes / (statbytes / elapsed) -
1177 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001178 else
1179 remaining = elapsed;
1180
Damien Miller01ab4a21999-10-28 15:23:30 +10001181 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182 if (i)
1183 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001184 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 else
1186 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001187 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188 i = remaining % 3600;
1189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001190 "%02d:%02d%s", i / 60, i % 60,
1191 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 }
1193 atomicio(write, fileno(stdout), buf, strlen(buf));
1194
1195 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001196 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001197 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001199 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001200 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201 statbytes = 0;
1202 }
1203}
1204
1205int
1206getttywidth(void)
1207{
1208 struct winsize winsize;
1209
1210 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001211 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212 else
Damien Miller95def091999-11-25 00:26:21 +11001213 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214}