blob: 0a89985de1d5dbb893a7352e54395976f52f3e98 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * scp - secure remote copy. This is basically patched BSD rcp which uses ssh
4 * to do the data transfer (instead of using rcmd).
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Miller95def091999-11-25 00:26:21 +11006 * NOTE: This version should NOT be suid root. (This uses ssh to do the transfer
7 * and ssh has the necessary privileges.)
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Miller95def091999-11-25 00:26:21 +11009 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +100010 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100011*/
12
13/*
14 * Copyright (c) 1983, 1990, 1992, 1993, 1995
15 * The Regents of the University of California. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045 */
46
47#include "includes.h"
Damien Miller6536c7d2000-06-22 21:32:31 +100048RCSID("$OpenBSD: scp.c,v 1.32 2000/06/20 01:39:44 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
50#include "ssh.h"
51#include "xmalloc.h"
52#include <utime.h>
53
54#define _PATH_CP "cp"
55
56/* For progressmeter() -- number of seconds before xfer considered "stalled" */
57#define STALLTIME 5
58
59/* Visual statistics about files as they are transferred. */
60void progressmeter(int);
61
62/* Returns width of the terminal (for progress meter calculations). */
63int getttywidth(void);
64
65/* Time a transfer started. */
66static struct timeval start;
67
68/* Number of bytes of current file transferred so far. */
69volatile unsigned long statbytes;
70
71/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +110072off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
74/* Name of current file being transferred. */
75char *curfile;
76
Damien Miller34132e52000-01-14 15:45:46 +110077/* This is set to non-zero if IPv4 is desired. */
78int IPv4 = 0;
79
80/* This is set to non-zero if IPv6 is desired. */
81int IPv6 = 0;
82
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +110084int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
86/* This is set to non-zero if compression is desired. */
Damien Millerd8087f61999-11-25 12:31:26 +110087int compress_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* This is set to zero if the progressmeter is not desired. */
90int showprogress = 1;
91
92/* This is set to non-zero if running in batch mode (that is, password
93 and passphrase queries are not allowed). */
94int batchmode = 0;
95
96/* This is set to the cipher type string if given on the command line. */
97char *cipher = NULL;
98
Damien Miller95def091999-11-25 00:26:21 +110099/* This is set to the RSA authentication identity file name if given on
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100 the command line. */
101char *identity = NULL;
102
103/* This is the port to use in contacting the remote site (is non-NULL). */
104char *port = NULL;
105
Damien Miller5428f641999-11-25 11:54:57 +1100106/*
107 * This function executes the given command as the specified user on the
108 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
109 * assigns the input and output file descriptors on success.
110 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller4af51302000-04-16 11:18:38 +1000112int
Damien Miller95def091999-11-25 00:26:21 +1100113do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114{
Damien Miller95def091999-11-25 00:26:21 +1100115 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117 if (verbose_mode)
118 fprintf(stderr, "Executing: host %s, user %s, command %s\n",
119 host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller5428f641999-11-25 11:54:57 +1100121 /*
122 * Reserve two descriptors so that the real pipes won't get
123 * descriptors 0 and 1 because that will screw up dup2 below.
124 */
Damien Miller95def091999-11-25 00:26:21 +1100125 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 /* Create a socket pair for communicating with ssh. */
128 if (pipe(pin) < 0)
129 fatal("pipe: %s", strerror(errno));
130 if (pipe(pout) < 0)
131 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller95def091999-11-25 00:26:21 +1100133 /* Free the reserved descriptors. */
134 close(reserved[0]);
135 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller95def091999-11-25 00:26:21 +1100137 /* For a child to execute the command on the remote host using ssh. */
138 if (fork() == 0) {
139 char *args[100];
140 unsigned int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 /* Child. */
143 close(pin[1]);
144 close(pout[0]);
145 dup2(pin[0], 0);
146 dup2(pout[1], 1);
147 close(pin[0]);
148 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller95def091999-11-25 00:26:21 +1100150 i = 0;
151 args[i++] = SSH_PROGRAM;
152 args[i++] = "-x";
153 args[i++] = "-oFallBackToRsh no";
Damien Miller34132e52000-01-14 15:45:46 +1100154 if (IPv4)
155 args[i++] = "-4";
156 if (IPv6)
157 args[i++] = "-6";
Damien Miller95def091999-11-25 00:26:21 +1100158 if (verbose_mode)
159 args[i++] = "-v";
Damien Millerd8087f61999-11-25 12:31:26 +1100160 if (compress_flag)
Damien Miller95def091999-11-25 00:26:21 +1100161 args[i++] = "-C";
162 if (batchmode)
163 args[i++] = "-oBatchMode yes";
164 if (cipher != NULL) {
165 args[i++] = "-c";
166 args[i++] = cipher;
167 }
168 if (identity != NULL) {
169 args[i++] = "-i";
170 args[i++] = identity;
171 }
172 if (port != NULL) {
173 args[i++] = "-p";
174 args[i++] = port;
175 }
176 if (remuser != NULL) {
177 args[i++] = "-l";
178 args[i++] = remuser;
179 }
180 args[i++] = host;
181 args[i++] = cmd;
182 args[i++] = NULL;
183
184 execvp(SSH_PROGRAM, args);
185 perror(SSH_PROGRAM);
186 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187 }
Damien Miller95def091999-11-25 00:26:21 +1100188 /* Parent. Close the other side, and return the local side. */
189 close(pin[0]);
190 *fdout = pin[1];
191 close(pout[1]);
192 *fdin = pout[0];
193 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194}
195
Damien Miller4af51302000-04-16 11:18:38 +1000196void
Damien Miller95def091999-11-25 00:26:21 +1100197fatal(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198{
Damien Miller95def091999-11-25 00:26:21 +1100199 va_list ap;
200 char buf[1024];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201
Damien Miller95def091999-11-25 00:26:21 +1100202 va_start(ap, fmt);
203 vsnprintf(buf, sizeof(buf), fmt, ap);
204 va_end(ap);
205 fprintf(stderr, "%s\n", buf);
206 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207}
208
209/* This stuff used to be in BSD rcp extern.h. */
210
211typedef struct {
212 int cnt;
213 char *buf;
214} BUF;
215
216extern int iamremote;
217
Damien Miller95def091999-11-25 00:26:21 +1100218BUF *allocbuf(BUF *, int, int);
219char *colon(char *);
220void lostconn(int);
221void nospace(void);
222int okname(char *);
223void run_err(const char *,...);
224void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
226/* Stuff from BSD rcp.c continues. */
227
228struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100229uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230int errs, remin, remout;
231int pflag, iamremote, iamrecursive, targetshouldbedirectory;
232
233#define CMDNEEDS 64
234char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
235
Damien Miller95def091999-11-25 00:26:21 +1100236int response(void);
237void rsource(char *, struct stat *);
238void sink(int, char *[]);
239void source(int, char *[]);
240void tolocal(int, char *[]);
241void toremote(char *, int, char *[]);
242void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
244int
245main(argc, argv)
246 int argc;
247 char *argv[];
248{
249 int ch, fflag, tflag;
250 char *targ;
251 extern char *optarg;
252 extern int optind;
253
254 fflag = tflag = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100255 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46")) != EOF)
Damien Miller95def091999-11-25 00:26:21 +1100256 switch (ch) {
257 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100258 case '4':
Damien Miller4af51302000-04-16 11:18:38 +1000259 IPv4 = 1;
Damien Miller34132e52000-01-14 15:45:46 +1100260 break;
261 case '6':
Damien Miller4af51302000-04-16 11:18:38 +1000262 IPv6 = 1;
Damien Miller34132e52000-01-14 15:45:46 +1100263 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264 case 'p':
265 pflag = 1;
266 break;
267 case 'P':
Damien Miller95def091999-11-25 00:26:21 +1100268 port = optarg;
269 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270 case 'r':
271 iamrecursive = 1;
272 break;
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;
284 break;
285 case 'c':
286 cipher = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100287 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 case 'i':
Damien Miller95def091999-11-25 00:26:21 +1100289 identity = optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290 break;
291 case 'v':
Damien Miller95def091999-11-25 00:26:21 +1100292 verbose_mode = 1;
293 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 case 'B':
Damien Miller95def091999-11-25 00:26:21 +1100295 batchmode = 1;
296 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297 case 'C':
Damien Millerd8087f61999-11-25 12:31:26 +1100298 compress_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100299 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300 case 'q':
Damien Miller95def091999-11-25 00:26:21 +1100301 showprogress = 0;
302 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303 case '?':
304 default:
305 usage();
306 }
307 argc -= optind;
308 argv += optind;
309
310 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100311 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Damien Miller95def091999-11-25 00:26:21 +1100313 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314 showprogress = 0;
315
316 remin = STDIN_FILENO;
317 remout = STDOUT_FILENO;
318
Damien Miller95def091999-11-25 00:26:21 +1100319 if (fflag) {
320 /* Follow "protocol", send data. */
321 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322 source(argc, argv);
323 exit(errs != 0);
324 }
Damien Miller95def091999-11-25 00:26:21 +1100325 if (tflag) {
326 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327 sink(argc, argv);
328 exit(errs != 0);
329 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330 if (argc < 2)
331 usage();
332 if (argc > 2)
333 targetshouldbedirectory = 1;
334
335 remin = remout = -1;
336 /* Command to be executed on remote system using "ssh". */
Damien Miller95def091999-11-25 00:26:21 +1100337 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
338 iamrecursive ? " -r" : "", pflag ? " -p" : "",
339 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Miller95def091999-11-25 00:26:21 +1100341 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
343 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
344 toremote(targ, argc, argv);
345 else {
Damien Miller95def091999-11-25 00:26:21 +1100346 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347 if (targetshouldbedirectory)
348 verifydir(argv[argc - 1]);
349 }
350 exit(errs != 0);
351}
352
Damien Miller34132e52000-01-14 15:45:46 +1100353char *
354cleanhostname(host)
355 char *host;
356{
357 if (*host == '[' && host[strlen(host) - 1] == ']') {
358 host[strlen(host) - 1] = '\0';
359 return (host + 1);
360 } else
361 return host;
362}
363
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364void
365toremote(targ, argc, argv)
366 char *targ, *argv[];
367 int argc;
368{
369 int i, len;
370 char *bp, *host, *src, *suser, *thost, *tuser;
371
372 *targ++ = 0;
373 if (*targ == 0)
374 targ = ".";
375
376 if ((thost = strchr(argv[argc - 1], '@'))) {
377 /* user@host */
378 *thost++ = 0;
379 tuser = argv[argc - 1];
380 if (*tuser == '\0')
381 tuser = NULL;
382 else if (!okname(tuser))
383 exit(1);
384 } else {
385 thost = argv[argc - 1];
386 tuser = NULL;
387 }
388
389 for (i = 0; i < argc - 1; i++) {
390 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100391 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392 *src++ = 0;
393 if (*src == 0)
394 src = ".";
395 host = strchr(argv[i], '@');
396 len = strlen(SSH_PROGRAM) + strlen(argv[i]) +
Damien Miller95def091999-11-25 00:26:21 +1100397 strlen(src) + (tuser ? strlen(tuser) : 0) +
398 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
399 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400 if (host) {
401 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100402 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403 suser = argv[i];
404 if (*suser == '\0')
405 suser = pwd->pw_name;
406 else if (!okname(suser))
407 continue;
Damien Miller95def091999-11-25 00:26:21 +1100408 (void) sprintf(bp,
409 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
410 SSH_PROGRAM, verbose_mode ? " -v" : "",
411 suser, host, cmd, src,
412 tuser ? tuser : "", tuser ? "@" : "",
413 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100414 } else {
415 host = cleanhostname(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100416 (void) sprintf(bp,
417 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
418 SSH_PROGRAM, verbose_mode ? " -v" : "",
Damien Miller34132e52000-01-14 15:45:46 +1100419 host, cmd, src,
Damien Miller95def091999-11-25 00:26:21 +1100420 tuser ? tuser : "", tuser ? "@" : "",
421 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100422 }
Damien Miller95def091999-11-25 00:26:21 +1100423 if (verbose_mode)
424 fprintf(stderr, "Executing: %s\n", bp);
425 (void) system(bp);
426 (void) xfree(bp);
427 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428 if (remin == -1) {
429 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100430 bp = xmalloc(len);
431 (void) sprintf(bp, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100432 host = cleanhostname(thost);
Damien Miller95def091999-11-25 00:26:21 +1100433 if (do_cmd(host, tuser,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 bp, &remin, &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100435 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 if (response() < 0)
437 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100438 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439 }
Damien Miller95def091999-11-25 00:26:21 +1100440 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441 }
442 }
443}
444
445void
446tolocal(argc, argv)
447 int argc;
448 char *argv[];
449{
450 int i, len;
451 char *bp, *host, *src, *suser;
452
453 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100454 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Miller95def091999-11-25 00:26:21 +1100456 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 bp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +1100458 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
459 iamrecursive ? " -r" : "", pflag ? " -p" : "",
460 argv[i], argv[argc - 1]);
461 if (verbose_mode)
462 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463 if (system(bp))
464 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100465 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466 continue;
467 }
468 *src++ = 0;
469 if (*src == 0)
470 src = ".";
471 if ((host = strchr(argv[i], '@')) == NULL) {
472 host = argv[i];
473 suser = NULL;
474 } else {
475 *host++ = 0;
476 suser = argv[i];
477 if (*suser == '\0')
478 suser = pwd->pw_name;
479 else if (!okname(suser))
480 continue;
481 }
Damien Miller34132e52000-01-14 15:45:46 +1100482 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100484 bp = xmalloc(len);
485 (void) sprintf(bp, "%s -f %s", cmd, src);
486 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
487 (void) xfree(bp);
488 ++errs;
489 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 }
Damien Miller95def091999-11-25 00:26:21 +1100491 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100493 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 remin = remout = -1;
495 }
496}
497
498void
499source(argc, argv)
500 int argc;
501 char *argv[];
502{
503 struct stat stb;
504 static BUF buffer;
505 BUF *bp;
506 off_t i;
507 int amt, fd, haderr, indx, result;
508 char *last, *name, buf[2048];
509
510 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100511 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512 statbytes = 0;
513 if ((fd = open(name, O_RDONLY, 0)) < 0)
514 goto syserr;
515 if (fstat(fd, &stb) < 0) {
516syserr: run_err("%s: %s", name, strerror(errno));
517 goto next;
518 }
519 switch (stb.st_mode & S_IFMT) {
520 case S_IFREG:
521 break;
522 case S_IFDIR:
523 if (iamrecursive) {
524 rsource(name, &stb);
525 goto next;
526 }
527 /* FALLTHROUGH */
528 default:
529 run_err("%s: not a regular file", name);
530 goto next;
531 }
532 if ((last = strrchr(name, '/')) == NULL)
533 last = name;
534 else
535 ++last;
536 curfile = last;
537 if (pflag) {
538 /*
539 * Make it compatible with possible future
540 * versions expecting microseconds.
541 */
Damien Miller95def091999-11-25 00:26:21 +1100542 (void) sprintf(buf, "T%lu 0 %lu 0\n",
543 (unsigned long) stb.st_mtime,
544 (unsigned long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000545 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546 if (response() < 0)
547 goto next;
548 }
549#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller95def091999-11-25 00:26:21 +1100550 (void) sprintf(buf, "C%04o %lu %s\n",
551 (unsigned int) (stb.st_mode & FILEMODEMASK),
552 (unsigned long) stb.st_size,
553 last);
554 if (verbose_mode) {
555 fprintf(stderr, "Sending file modes: %s", buf);
556 fflush(stderr);
557 }
Damien Miller35dabd02000-05-01 21:10:33 +1000558 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 if (response() < 0)
560 goto next;
561 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100562next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563 continue;
564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 if (showprogress) {
566 totalbytes = stb.st_size;
567 progressmeter(-1);
568 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 /* Keep writing after an error so that we stay sync'd up. */
570 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
571 amt = bp->cnt;
572 if (i + amt > stb.st_size)
573 amt = stb.st_size - i;
574 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000575 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 if (result != amt)
577 haderr = result >= 0 ? EIO : errno;
578 }
579 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000580 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581 else {
Damien Miller864ea591999-12-15 11:04:25 +1100582 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 if (result != amt)
584 haderr = result >= 0 ? EIO : errno;
585 statbytes += result;
586 }
587 }
Damien Miller95def091999-11-25 00:26:21 +1100588 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 progressmeter(1);
590
591 if (close(fd) < 0 && !haderr)
592 haderr = errno;
593 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000594 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595 else
596 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100597 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000598 }
599}
600
601void
602rsource(name, statp)
603 char *name;
604 struct stat *statp;
605{
606 DIR *dirp;
607 struct dirent *dp;
608 char *last, *vect[1], path[1100];
609
610 if (!(dirp = opendir(name))) {
611 run_err("%s: %s", name, strerror(errno));
612 return;
613 }
614 last = strrchr(name, '/');
615 if (last == 0)
616 last = name;
617 else
618 last++;
619 if (pflag) {
Damien Miller95def091999-11-25 00:26:21 +1100620 (void) sprintf(path, "T%lu 0 %lu 0\n",
621 (unsigned long) statp->st_mtime,
622 (unsigned long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000623 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 if (response() < 0) {
625 closedir(dirp);
626 return;
627 }
628 }
Damien Miller95def091999-11-25 00:26:21 +1100629 (void) sprintf(path, "D%04o %d %.1024s\n",
630 (unsigned int) (statp->st_mode & FILEMODEMASK),
631 0, last);
632 if (verbose_mode)
633 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000634 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635 if (response() < 0) {
636 closedir(dirp);
637 return;
638 }
639 while ((dp = readdir(dirp))) {
640 if (dp->d_ino == 0)
641 continue;
642 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
643 continue;
644 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
645 run_err("%s/%s: name too long", name, dp->d_name);
646 continue;
647 }
Damien Miller95def091999-11-25 00:26:21 +1100648 (void) sprintf(path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649 vect[0] = path;
650 source(1, vect);
651 }
Damien Miller95def091999-11-25 00:26:21 +1100652 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000653 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100654 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655}
656
657void
658sink(argc, argv)
659 int argc;
660 char *argv[];
661{
662 static BUF buffer;
663 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100664 enum {
665 YES, NO, DISPLAYED
666 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 BUF *bp;
668 off_t i, j;
669 int amt, count, exists, first, mask, mode, ofd, omode;
670 int setimes, size, targisdir, wrerrno = 0;
671 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100672 struct utimbuf ut;
673 int dummy_usec;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674
675#define SCREWUP(str) { why = str; goto screwup; }
676
677 setimes = targisdir = 0;
678 mask = umask(0);
679 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100680 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681 if (argc != 1) {
682 run_err("ambiguous target");
683 exit(1);
684 }
685 targ = *argv;
686 if (targetshouldbedirectory)
687 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100688
Damien Miller35dabd02000-05-01 21:10:33 +1000689 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
691 targisdir = 1;
692 for (first = 1;; first = 0) {
693 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000694 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695 return;
696 if (*cp++ == '\n')
697 SCREWUP("unexpected <newline>");
698 do {
Damien Millere247cc42000-05-07 12:03:14 +1000699 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700 SCREWUP("lost connection");
701 *cp++ = ch;
702 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
703 *cp = 0;
704
705 if (buf[0] == '\01' || buf[0] == '\02') {
706 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000707 (void) atomicio(write, STDERR_FILENO,
Damien Miller95def091999-11-25 00:26:21 +1100708 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 if (buf[0] == '\02')
710 exit(1);
711 ++errs;
712 continue;
713 }
714 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000715 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716 return;
717 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718 if (ch == '\n')
719 *--cp = 0;
720
721#define getnum(t) (t) = 0; \
722 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
723 cp = buf;
724 if (*cp == 'T') {
725 setimes++;
726 cp++;
727 getnum(ut.modtime);
728 if (*cp++ != ' ')
729 SCREWUP("mtime.sec not delimited");
730 getnum(dummy_usec);
731 if (*cp++ != ' ')
732 SCREWUP("mtime.usec not delimited");
733 getnum(ut.actime);
734 if (*cp++ != ' ')
735 SCREWUP("atime.sec not delimited");
736 getnum(dummy_usec);
737 if (*cp++ != '\0')
738 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000739 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740 continue;
741 }
742 if (*cp != 'C' && *cp != 'D') {
743 /*
744 * Check for the case "rcp remote:foo\* local:bar".
745 * In this case, the line "No match." can be returned
746 * by the shell before the rcp command on the remote is
747 * executed so the ^Aerror_message convention isn't
748 * followed.
749 */
750 if (first) {
751 run_err("%s", cp);
752 exit(1);
753 }
754 SCREWUP("expected control record");
755 }
756 mode = 0;
757 for (++cp; cp < buf + 5; cp++) {
758 if (*cp < '0' || *cp > '7')
759 SCREWUP("bad mode");
760 mode = (mode << 3) | (*cp - '0');
761 }
762 if (*cp++ != ' ')
763 SCREWUP("mode not delimited");
764
Damien Miller95def091999-11-25 00:26:21 +1100765 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766 size = size * 10 + (*cp++ - '0');
767 if (*cp++ != ' ')
768 SCREWUP("size not delimited");
769 if (targisdir) {
770 static char *namebuf;
771 static int cursize;
772 size_t need;
773
774 need = strlen(targ) + strlen(cp) + 250;
775 if (need > cursize)
Damien Miller95def091999-11-25 00:26:21 +1100776 namebuf = xmalloc(need);
777 (void) sprintf(namebuf, "%s%s%s", targ,
778 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779 np = namebuf;
780 } else
781 np = targ;
782 curfile = cp;
783 exists = stat(np, &stb) == 0;
784 if (buf[0] == 'D') {
785 int mod_flag = pflag;
786 if (exists) {
787 if (!S_ISDIR(stb.st_mode)) {
788 errno = ENOTDIR;
789 goto bad;
790 }
791 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100792 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793 } else {
Damien Miller95def091999-11-25 00:26:21 +1100794 /* Handle copying from a read-only
795 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796 mod_flag = 1;
797 if (mkdir(np, mode | S_IRWXU) < 0)
798 goto bad;
799 }
800 vect[0] = np;
801 sink(1, vect);
802 if (setimes) {
803 setimes = 0;
804 if (utime(np, &ut) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100805 run_err("%s: set times: %s",
806 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 }
808 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100809 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810 continue;
811 }
812 omode = mode;
813 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100814 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815bad: run_err("%s: %s", np, strerror(errno));
816 continue;
817 }
Damien Miller35dabd02000-05-01 21:10:33 +1000818 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100820 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821 continue;
822 }
823 cp = bp->buf;
824 wrerr = NO;
825
826 if (showprogress) {
827 totalbytes = size;
828 progressmeter(-1);
829 }
830 statbytes = 0;
831 for (count = i = 0; i < size; i += 4096) {
832 amt = 4096;
833 if (i + amt > size)
834 amt = size - i;
835 count += amt;
836 do {
Damien Millere247cc42000-05-07 12:03:14 +1000837 j = atomicio(read, remin, cp, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838 if (j <= 0) {
839 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100840 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 exit(1);
842 }
843 amt -= j;
844 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100845 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846 } while (amt > 0);
847 if (count == bp->cnt) {
848 /* Keep reading so we stay sync'd up. */
849 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000850 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851 if (j != count) {
852 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100853 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854 }
855 }
856 count = 0;
857 cp = bp->buf;
858 }
859 }
860 if (showprogress)
861 progressmeter(1);
862 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000863 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100865 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 }
867#if 0
868 if (ftruncate(ofd, size)) {
869 run_err("%s: truncate: %s", np, strerror(errno));
870 wrerr = DISPLAYED;
871 }
872#endif
873 if (pflag) {
874 if (exists || omode != mode)
875 if (fchmod(ofd, omode))
876 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100877 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 } else {
879 if (!exists && omode != mode)
880 if (fchmod(ofd, omode & ~mask))
881 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100882 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883 }
Damien Miller95def091999-11-25 00:26:21 +1100884 (void) close(ofd);
885 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 if (setimes && wrerr == NO) {
887 setimes = 0;
888 if (utime(np, &ut) < 0) {
889 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100890 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 wrerr = DISPLAYED;
892 }
893 }
Damien Miller95def091999-11-25 00:26:21 +1100894 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 case YES:
896 run_err("%s: %s", np, strerror(wrerrno));
897 break;
898 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000899 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 break;
901 case DISPLAYED:
902 break;
903 }
904 }
905screwup:
906 run_err("protocol error: %s", why);
907 exit(1);
908}
909
910int
911response()
912{
913 char ch, *cp, resp, rbuf[2048];
914
Damien Millere247cc42000-05-07 12:03:14 +1000915 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916 lostconn(0);
917
918 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100919 switch (resp) {
920 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921 return (0);
922 default:
923 *cp++ = resp;
924 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100925 case 1: /* error, followed by error msg */
926 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927 do {
Damien Millere247cc42000-05-07 12:03:14 +1000928 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929 lostconn(0);
930 *cp++ = ch;
931 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
932
933 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000934 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935 ++errs;
936 if (resp == 1)
937 return (-1);
938 exit(1);
939 }
940 /* NOTREACHED */
941}
942
943void
944usage()
945{
Damien Miller95def091999-11-25 00:26:21 +1100946 (void) fprintf(stderr,
Damien Miller34132e52000-01-14 15:45:46 +1100947 "usage: scp [-pqrvC46] [-P port] [-c cipher] [-i identity] f1 f2; or:\n scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948 exit(1);
949}
950
951void
Damien Miller95def091999-11-25 00:26:21 +1100952run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953{
954 static FILE *fp;
955 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956
957 ++errs;
958 if (fp == NULL && !(fp = fdopen(remout, "w")))
959 return;
Damien Miller95def091999-11-25 00:26:21 +1100960 (void) fprintf(fp, "%c", 0x01);
961 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100962 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100963 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100964 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100965 (void) fprintf(fp, "\n");
966 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller95def091999-11-25 00:26:21 +1100968 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100969 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100970 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100971 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100972 fprintf(stderr, "\n");
973 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974}
975
976/* Stuff below is from BSD rcp util.c. */
977
978/*-
979 * Copyright (c) 1992, 1993
980 * The Regents of the University of California. All rights reserved.
981 *
982 * Redistribution and use in source and binary forms, with or without
983 * modification, are permitted provided that the following conditions
984 * are met:
985 * 1. Redistributions of source code must retain the above copyright
986 * notice, this list of conditions and the following disclaimer.
987 * 2. Redistributions in binary form must reproduce the above copyright
988 * notice, this list of conditions and the following disclaimer in the
989 * documentation and/or other materials provided with the distribution.
990 * 3. All advertising materials mentioning features or use of this software
991 * must display the following acknowledgement:
992 * This product includes software developed by the University of
993 * California, Berkeley and its contributors.
994 * 4. Neither the name of the University nor the names of its contributors
995 * may be used to endorse or promote products derived from this software
996 * without specific prior written permission.
997 *
998 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
999 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1000 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1001 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1002 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1003 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1004 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1005 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1006 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1007 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1008 * SUCH DAMAGE.
1009 *
Damien Miller6536c7d2000-06-22 21:32:31 +10001010 * $OpenBSD: scp.c,v 1.32 2000/06/20 01:39:44 markus Exp $
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011 */
1012
1013char *
1014colon(cp)
1015 char *cp;
1016{
Damien Miller34132e52000-01-14 15:45:46 +11001017 int flag = 0;
1018
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019 if (*cp == ':') /* Leading colon is part of file name. */
1020 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001021 if (*cp == '[')
1022 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
1024 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001025 if (*cp == '@' && *(cp+1) == '[')
1026 flag = 1;
1027 if (*cp == ']' && *(cp+1) == ':' && flag)
1028 return (cp+1);
1029 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030 return (cp);
1031 if (*cp == '/')
1032 return (0);
1033 }
1034 return (0);
1035}
1036
1037void
1038verifydir(cp)
1039 char *cp;
1040{
1041 struct stat stb;
1042
1043 if (!stat(cp, &stb)) {
1044 if (S_ISDIR(stb.st_mode))
1045 return;
1046 errno = ENOTDIR;
1047 }
1048 run_err("%s: %s", cp, strerror(errno));
1049 exit(1);
1050}
1051
1052int
1053okname(cp0)
1054 char *cp0;
1055{
1056 int c;
1057 char *cp;
1058
1059 cp = cp0;
1060 do {
1061 c = *cp;
1062 if (c & 0200)
1063 goto bad;
1064 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1065 goto bad;
1066 } while (*++cp);
1067 return (1);
1068
Damien Miller98c7ad62000-03-09 21:27:49 +11001069bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070 return (0);
1071}
1072
1073BUF *
1074allocbuf(bp, fd, blksize)
1075 BUF *bp;
1076 int fd, blksize;
1077{
1078 size_t size;
1079 struct stat stb;
1080
1081 if (fstat(fd, &stb) < 0) {
1082 run_err("fstat: %s", strerror(errno));
1083 return (0);
1084 }
Damien Miller95def091999-11-25 00:26:21 +11001085 if (stb.st_blksize == 0)
1086 size = blksize;
1087 else
1088 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1089 stb.st_blksize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090 if (bp->cnt >= size)
1091 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001092 if (bp->buf == NULL)
1093 bp->buf = xmalloc(size);
1094 else
1095 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096 bp->cnt = size;
1097 return (bp);
1098}
1099
1100void
1101lostconn(signo)
1102 int signo;
1103{
1104 if (!iamremote)
1105 fprintf(stderr, "lost connection\n");
1106 exit(1);
1107}
1108
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109
1110void
1111alarmtimer(int wait)
1112{
Damien Miller95def091999-11-25 00:26:21 +11001113 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114
Damien Miller95def091999-11-25 00:26:21 +11001115 itv.it_value.tv_sec = wait;
1116 itv.it_value.tv_usec = 0;
1117 itv.it_interval = itv.it_value;
1118 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119}
1120
1121void
Damien Miller7684ee12000-03-17 23:40:15 +11001122updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123{
1124 int save_errno = errno;
1125
1126 progressmeter(0);
1127 errno = save_errno;
1128}
1129
Damien Miller81428f91999-11-18 09:28:11 +11001130int
1131foregroundproc()
1132{
1133 static pid_t pgrp = -1;
1134 int ctty_pgrp;
1135
1136 if (pgrp == -1)
1137 pgrp = getpgrp();
1138
Damien Miller95def091999-11-25 00:26:21 +11001139 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1140 ctty_pgrp == pgrp));
Damien Miller81428f91999-11-18 09:28:11 +11001141}
1142
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143void
1144progressmeter(int flag)
1145{
1146 static const char prefixes[] = " KMGTP";
1147 static struct timeval lastupdate;
1148 static off_t lastsize;
1149 struct timeval now, td, wait;
1150 off_t cursize, abbrevsize;
1151 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001152 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153 char buf[256];
1154
1155 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001156 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 lastupdate = start;
1158 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001159 }
Damien Miller81428f91999-11-18 09:28:11 +11001160 if (foregroundproc() == 0)
1161 return;
1162
Damien Miller95def091999-11-25 00:26:21 +11001163 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001165 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001166 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 ratio = MAX(ratio, 0);
1168 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001169 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170 ratio = 100;
1171
Damien Miller95def091999-11-25 00:26:21 +11001172 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173
1174 barlength = getttywidth() - 51;
1175 if (barlength > 0) {
1176 i = barlength * ratio / 100;
1177 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001178 "|%.*s%*s|", i,
1179 "*****************************************************************************"
1180 "*****************************************************************************",
1181 barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 i = 0;
1184 abbrevsize = cursize;
1185 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1186 i++;
1187 abbrevsize >>= 10;
1188 }
Damien Miller864ea591999-12-15 11:04:25 +11001189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1190 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
Damien Miller95def091999-11-25 00:26:21 +11001191 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192
1193 timersub(&now, &lastupdate, &wait);
1194 if (cursize > lastsize) {
1195 lastupdate = now;
1196 lastsize = cursize;
1197 if (wait.tv_sec >= STALLTIME) {
1198 start.tv_sec += wait.tv_sec;
1199 start.tv_usec += wait.tv_usec;
1200 }
1201 wait.tv_sec = 0;
1202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 timersub(&now, &start, &td);
1204 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1205
1206 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1207 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001208 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209 } else if (wait.tv_sec >= STALLTIME) {
1210 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001211 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001213 if (flag != 1)
1214 remaining =
1215 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1216 else
1217 remaining = elapsed;
1218
Damien Miller01ab4a21999-10-28 15:23:30 +10001219 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220 if (i)
1221 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001222 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223 else
1224 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001225 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 i = remaining % 3600;
1227 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller8bb73be2000-04-19 16:26:12 +10001228 "%02d:%02d%s", i / 60, i % 60,
1229 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230 }
1231 atomicio(write, fileno(stdout), buf, strlen(buf));
1232
1233 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001234 struct sigaction sa;
1235 sa.sa_handler = updateprogressmeter;
1236 sigemptyset(&sa.sa_mask);
Damien Miller615f9392000-05-17 22:53:33 +10001237#ifdef SA_RESTART
Damien Miller864ea591999-12-15 11:04:25 +11001238 sa.sa_flags = SA_RESTART;
Damien Miller615f9392000-05-17 22:53:33 +10001239#endif
Damien Miller864ea591999-12-15 11:04:25 +11001240 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 alarmtimer(1);
1242 } else if (flag == 1) {
1243 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001244 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245 statbytes = 0;
1246 }
1247}
1248
1249int
1250getttywidth(void)
1251{
1252 struct winsize winsize;
1253
1254 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001255 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256 else
Damien Miller95def091999-11-25 00:26:21 +11001257 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258}