blob: 6e07879f051b50aca2c337aad62582eee97d9ae6 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * scp - secure remote copy. This is basically patched BSD rcp which uses ssh
4 * to do the data transfer (instead of using rcmd).
5 *
6 * NOTE: This version should NOT be suid root. (This uses ssh to do the transfer
7 * and ssh has the necessary privileges.)
8 *
9 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
10 *
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 Miller98c7ad62000-03-09 21:27:49 +110048RCSID("$Id: scp.c,v 1.17 2000/03/09 10:27:51 damien 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 Miller95def091999-11-25 00:26:21 +1100112int
113do_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";
158 args[i++] = "-oFallBackToRsh no";
Damien Miller95def091999-11-25 00:26:21 +1100159 if (verbose_mode)
160 args[i++] = "-v";
Damien Millerd8087f61999-11-25 12:31:26 +1100161 if (compress_flag)
Damien Miller95def091999-11-25 00:26:21 +1100162 args[i++] = "-C";
163 if (batchmode)
164 args[i++] = "-oBatchMode yes";
165 if (cipher != NULL) {
166 args[i++] = "-c";
167 args[i++] = cipher;
168 }
169 if (identity != NULL) {
170 args[i++] = "-i";
171 args[i++] = identity;
172 }
173 if (port != NULL) {
174 args[i++] = "-p";
175 args[i++] = port;
176 }
177 if (remuser != NULL) {
178 args[i++] = "-l";
179 args[i++] = remuser;
180 }
181 args[i++] = host;
182 args[i++] = cmd;
183 args[i++] = NULL;
184
185 execvp(SSH_PROGRAM, args);
186 perror(SSH_PROGRAM);
187 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188 }
Damien Miller95def091999-11-25 00:26:21 +1100189 /* Parent. Close the other side, and return the local side. */
190 close(pin[0]);
191 *fdout = pin[1];
192 close(pout[1]);
193 *fdin = pout[0];
194 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Damien Miller95def091999-11-25 00:26:21 +1100197void
198fatal(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller95def091999-11-25 00:26:21 +1100200 va_list ap;
201 char buf[1024];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Damien Miller95def091999-11-25 00:26:21 +1100203 va_start(ap, fmt);
204 vsnprintf(buf, sizeof(buf), fmt, ap);
205 va_end(ap);
206 fprintf(stderr, "%s\n", buf);
207 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208}
209
210/* This stuff used to be in BSD rcp extern.h. */
211
212typedef struct {
213 int cnt;
214 char *buf;
215} BUF;
216
217extern int iamremote;
218
Damien Miller95def091999-11-25 00:26:21 +1100219BUF *allocbuf(BUF *, int, int);
220char *colon(char *);
221void lostconn(int);
222void nospace(void);
223int okname(char *);
224void run_err(const char *,...);
225void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226
227/* Stuff from BSD rcp.c continues. */
228
229struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100230uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231int errs, remin, remout;
232int pflag, iamremote, iamrecursive, targetshouldbedirectory;
233
234#define CMDNEEDS 64
235char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
236
Damien Miller95def091999-11-25 00:26:21 +1100237int response(void);
238void rsource(char *, struct stat *);
239void sink(int, char *[]);
240void source(int, char *[]);
241void tolocal(int, char *[]);
242void toremote(char *, int, char *[]);
243void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
245int
246main(argc, argv)
247 int argc;
248 char *argv[];
249{
250 int ch, fflag, tflag;
251 char *targ;
252 extern char *optarg;
253 extern int optind;
254
255 fflag = tflag = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100256 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46")) != EOF)
Damien Miller95def091999-11-25 00:26:21 +1100257 switch (ch) {
258 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100259 case '4':
260 IPv4 = 1;
261 break;
262 case '6':
263 IPv6 = 1;
264 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 case 'p':
266 pflag = 1;
267 break;
268 case 'P':
Damien Miller95def091999-11-25 00:26:21 +1100269 port = optarg;
270 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271 case 'r':
272 iamrecursive = 1;
273 break;
Damien Miller95def091999-11-25 00:26:21 +1100274 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275 case 'd':
276 targetshouldbedirectory = 1;
277 break;
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279 iamremote = 1;
280 fflag = 1;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283 iamremote = 1;
284 tflag = 1;
285 break;
286 case 'c':
287 cipher = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100288 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289 case 'i':
Damien Miller95def091999-11-25 00:26:21 +1100290 identity = optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291 break;
292 case 'v':
Damien Miller95def091999-11-25 00:26:21 +1100293 verbose_mode = 1;
294 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295 case 'B':
Damien Miller95def091999-11-25 00:26:21 +1100296 batchmode = 1;
297 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298 case 'C':
Damien Millerd8087f61999-11-25 12:31:26 +1100299 compress_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100300 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301 case 'q':
Damien Miller95def091999-11-25 00:26:21 +1100302 showprogress = 0;
303 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304 case '?':
305 default:
306 usage();
307 }
308 argc -= optind;
309 argv += optind;
310
311 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100312 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313
Damien Miller95def091999-11-25 00:26:21 +1100314 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315 showprogress = 0;
316
317 remin = STDIN_FILENO;
318 remout = STDOUT_FILENO;
319
Damien Miller95def091999-11-25 00:26:21 +1100320 if (fflag) {
321 /* Follow "protocol", send data. */
322 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323 source(argc, argv);
324 exit(errs != 0);
325 }
Damien Miller95def091999-11-25 00:26:21 +1100326 if (tflag) {
327 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328 sink(argc, argv);
329 exit(errs != 0);
330 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331 if (argc < 2)
332 usage();
333 if (argc > 2)
334 targetshouldbedirectory = 1;
335
336 remin = remout = -1;
337 /* Command to be executed on remote system using "ssh". */
Damien Miller95def091999-11-25 00:26:21 +1100338 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
339 iamrecursive ? " -r" : "", pflag ? " -p" : "",
340 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Miller95def091999-11-25 00:26:21 +1100342 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343
344 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
345 toremote(targ, argc, argv);
346 else {
Damien Miller95def091999-11-25 00:26:21 +1100347 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348 if (targetshouldbedirectory)
349 verifydir(argv[argc - 1]);
350 }
351 exit(errs != 0);
352}
353
Damien Miller34132e52000-01-14 15:45:46 +1100354char *
355cleanhostname(host)
356 char *host;
357{
358 if (*host == '[' && host[strlen(host) - 1] == ']') {
359 host[strlen(host) - 1] = '\0';
360 return (host + 1);
361 } else
362 return host;
363}
364
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365void
366toremote(targ, argc, argv)
367 char *targ, *argv[];
368 int argc;
369{
370 int i, len;
371 char *bp, *host, *src, *suser, *thost, *tuser;
372
373 *targ++ = 0;
374 if (*targ == 0)
375 targ = ".";
376
377 if ((thost = strchr(argv[argc - 1], '@'))) {
378 /* user@host */
379 *thost++ = 0;
380 tuser = argv[argc - 1];
381 if (*tuser == '\0')
382 tuser = NULL;
383 else if (!okname(tuser))
384 exit(1);
385 } else {
386 thost = argv[argc - 1];
387 tuser = NULL;
388 }
389
390 for (i = 0; i < argc - 1; i++) {
391 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100392 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393 *src++ = 0;
394 if (*src == 0)
395 src = ".";
396 host = strchr(argv[i], '@');
397 len = strlen(SSH_PROGRAM) + strlen(argv[i]) +
Damien Miller95def091999-11-25 00:26:21 +1100398 strlen(src) + (tuser ? strlen(tuser) : 0) +
399 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
400 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401 if (host) {
402 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100403 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404 suser = argv[i];
405 if (*suser == '\0')
406 suser = pwd->pw_name;
407 else if (!okname(suser))
408 continue;
Damien Miller95def091999-11-25 00:26:21 +1100409 (void) sprintf(bp,
410 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
411 SSH_PROGRAM, verbose_mode ? " -v" : "",
412 suser, host, cmd, src,
413 tuser ? tuser : "", tuser ? "@" : "",
414 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100415 } else {
416 host = cleanhostname(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100417 (void) sprintf(bp,
418 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
419 SSH_PROGRAM, verbose_mode ? " -v" : "",
Damien Miller34132e52000-01-14 15:45:46 +1100420 host, cmd, src,
Damien Miller95def091999-11-25 00:26:21 +1100421 tuser ? tuser : "", tuser ? "@" : "",
422 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100423 }
Damien Miller95def091999-11-25 00:26:21 +1100424 if (verbose_mode)
425 fprintf(stderr, "Executing: %s\n", bp);
426 (void) system(bp);
427 (void) xfree(bp);
428 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429 if (remin == -1) {
430 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100431 bp = xmalloc(len);
432 (void) sprintf(bp, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100433 host = cleanhostname(thost);
Damien Miller95def091999-11-25 00:26:21 +1100434 if (do_cmd(host, tuser,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435 bp, &remin, &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100436 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437 if (response() < 0)
438 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100439 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440 }
Damien Miller95def091999-11-25 00:26:21 +1100441 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442 }
443 }
444}
445
446void
447tolocal(argc, argv)
448 int argc;
449 char *argv[];
450{
451 int i, len;
452 char *bp, *host, *src, *suser;
453
454 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100455 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Miller95def091999-11-25 00:26:21 +1100457 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458 bp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +1100459 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
460 iamrecursive ? " -r" : "", pflag ? " -p" : "",
461 argv[i], argv[argc - 1]);
462 if (verbose_mode)
463 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 if (system(bp))
465 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100466 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 continue;
468 }
469 *src++ = 0;
470 if (*src == 0)
471 src = ".";
472 if ((host = strchr(argv[i], '@')) == NULL) {
473 host = argv[i];
474 suser = NULL;
475 } else {
476 *host++ = 0;
477 suser = argv[i];
478 if (*suser == '\0')
479 suser = pwd->pw_name;
480 else if (!okname(suser))
481 continue;
482 }
Damien Miller34132e52000-01-14 15:45:46 +1100483 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100485 bp = xmalloc(len);
486 (void) sprintf(bp, "%s -f %s", cmd, src);
487 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
488 (void) xfree(bp);
489 ++errs;
490 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491 }
Damien Miller95def091999-11-25 00:26:21 +1100492 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100494 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495 remin = remout = -1;
496 }
497}
498
499void
500source(argc, argv)
501 int argc;
502 char *argv[];
503{
504 struct stat stb;
505 static BUF buffer;
506 BUF *bp;
507 off_t i;
508 int amt, fd, haderr, indx, result;
509 char *last, *name, buf[2048];
510
511 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100512 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513 statbytes = 0;
514 if ((fd = open(name, O_RDONLY, 0)) < 0)
515 goto syserr;
516 if (fstat(fd, &stb) < 0) {
517syserr: run_err("%s: %s", name, strerror(errno));
518 goto next;
519 }
520 switch (stb.st_mode & S_IFMT) {
521 case S_IFREG:
522 break;
523 case S_IFDIR:
524 if (iamrecursive) {
525 rsource(name, &stb);
526 goto next;
527 }
528 /* FALLTHROUGH */
529 default:
530 run_err("%s: not a regular file", name);
531 goto next;
532 }
533 if ((last = strrchr(name, '/')) == NULL)
534 last = name;
535 else
536 ++last;
537 curfile = last;
538 if (pflag) {
539 /*
540 * Make it compatible with possible future
541 * versions expecting microseconds.
542 */
Damien Miller95def091999-11-25 00:26:21 +1100543 (void) sprintf(buf, "T%lu 0 %lu 0\n",
544 (unsigned long) stb.st_mtime,
545 (unsigned long) stb.st_atime);
546 (void) write(remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547 if (response() < 0)
548 goto next;
549 }
550#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller95def091999-11-25 00:26:21 +1100551 (void) sprintf(buf, "C%04o %lu %s\n",
552 (unsigned int) (stb.st_mode & FILEMODEMASK),
553 (unsigned long) stb.st_size,
554 last);
555 if (verbose_mode) {
556 fprintf(stderr, "Sending file modes: %s", buf);
557 fflush(stderr);
558 }
559 (void) write(remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 if (response() < 0)
561 goto next;
562 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100563next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 continue;
565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 if (showprogress) {
567 totalbytes = stb.st_size;
568 progressmeter(-1);
569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 /* Keep writing after an error so that we stay sync'd up. */
571 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
572 amt = bp->cnt;
573 if (i + amt > stb.st_size)
574 amt = stb.st_size - i;
575 if (!haderr) {
576 result = read(fd, bp->buf, amt);
577 if (result != amt)
578 haderr = result >= 0 ? EIO : errno;
579 }
580 if (haderr)
Damien Miller95def091999-11-25 00:26:21 +1100581 (void) write(remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582 else {
Damien Miller864ea591999-12-15 11:04:25 +1100583 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584 if (result != amt)
585 haderr = result >= 0 ? EIO : errno;
586 statbytes += result;
587 }
588 }
Damien Miller95def091999-11-25 00:26:21 +1100589 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590 progressmeter(1);
591
592 if (close(fd) < 0 && !haderr)
593 haderr = errno;
594 if (!haderr)
Damien Miller95def091999-11-25 00:26:21 +1100595 (void) write(remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596 else
597 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100598 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599 }
600}
601
602void
603rsource(name, statp)
604 char *name;
605 struct stat *statp;
606{
607 DIR *dirp;
608 struct dirent *dp;
609 char *last, *vect[1], path[1100];
610
611 if (!(dirp = opendir(name))) {
612 run_err("%s: %s", name, strerror(errno));
613 return;
614 }
615 last = strrchr(name, '/');
616 if (last == 0)
617 last = name;
618 else
619 last++;
620 if (pflag) {
Damien Miller95def091999-11-25 00:26:21 +1100621 (void) sprintf(path, "T%lu 0 %lu 0\n",
622 (unsigned long) statp->st_mtime,
623 (unsigned long) statp->st_atime);
624 (void) write(remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625 if (response() < 0) {
626 closedir(dirp);
627 return;
628 }
629 }
Damien Miller95def091999-11-25 00:26:21 +1100630 (void) sprintf(path, "D%04o %d %.1024s\n",
631 (unsigned int) (statp->st_mode & FILEMODEMASK),
632 0, last);
633 if (verbose_mode)
634 fprintf(stderr, "Entering directory: %s", path);
635 (void) write(remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636 if (response() < 0) {
637 closedir(dirp);
638 return;
639 }
640 while ((dp = readdir(dirp))) {
641 if (dp->d_ino == 0)
642 continue;
643 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
644 continue;
645 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
646 run_err("%s/%s: name too long", name, dp->d_name);
647 continue;
648 }
Damien Miller95def091999-11-25 00:26:21 +1100649 (void) sprintf(path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650 vect[0] = path;
651 source(1, vect);
652 }
Damien Miller95def091999-11-25 00:26:21 +1100653 (void) closedir(dirp);
654 (void) write(remout, "E\n", 2);
655 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656}
657
658void
659sink(argc, argv)
660 int argc;
661 char *argv[];
662{
663 static BUF buffer;
664 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100665 enum {
666 YES, NO, DISPLAYED
667 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668 BUF *bp;
669 off_t i, j;
670 int amt, count, exists, first, mask, mode, ofd, omode;
671 int setimes, size, targisdir, wrerrno = 0;
672 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100673 struct utimbuf ut;
674 int dummy_usec;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675
676#define SCREWUP(str) { why = str; goto screwup; }
677
678 setimes = targisdir = 0;
679 mask = umask(0);
680 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100681 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682 if (argc != 1) {
683 run_err("ambiguous target");
684 exit(1);
685 }
686 targ = *argv;
687 if (targetshouldbedirectory)
688 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100689
690 (void) write(remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
692 targisdir = 1;
693 for (first = 1;; first = 0) {
694 cp = buf;
695 if (read(remin, cp, 1) <= 0)
696 return;
697 if (*cp++ == '\n')
698 SCREWUP("unexpected <newline>");
699 do {
700 if (read(remin, &ch, sizeof(ch)) != sizeof(ch))
701 SCREWUP("lost connection");
702 *cp++ = ch;
703 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
704 *cp = 0;
705
706 if (buf[0] == '\01' || buf[0] == '\02') {
707 if (iamremote == 0)
Damien Miller95def091999-11-25 00:26:21 +1100708 (void) write(STDERR_FILENO,
709 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 if (buf[0] == '\02')
711 exit(1);
712 ++errs;
713 continue;
714 }
715 if (buf[0] == 'E') {
Damien Miller95def091999-11-25 00:26:21 +1100716 (void) write(remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717 return;
718 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719 if (ch == '\n')
720 *--cp = 0;
721
722#define getnum(t) (t) = 0; \
723 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
724 cp = buf;
725 if (*cp == 'T') {
726 setimes++;
727 cp++;
728 getnum(ut.modtime);
729 if (*cp++ != ' ')
730 SCREWUP("mtime.sec not delimited");
731 getnum(dummy_usec);
732 if (*cp++ != ' ')
733 SCREWUP("mtime.usec not delimited");
734 getnum(ut.actime);
735 if (*cp++ != ' ')
736 SCREWUP("atime.sec not delimited");
737 getnum(dummy_usec);
738 if (*cp++ != '\0')
739 SCREWUP("atime.usec not delimited");
Damien Miller95def091999-11-25 00:26:21 +1100740 (void) write(remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 continue;
742 }
743 if (*cp != 'C' && *cp != 'D') {
744 /*
745 * Check for the case "rcp remote:foo\* local:bar".
746 * In this case, the line "No match." can be returned
747 * by the shell before the rcp command on the remote is
748 * executed so the ^Aerror_message convention isn't
749 * followed.
750 */
751 if (first) {
752 run_err("%s", cp);
753 exit(1);
754 }
755 SCREWUP("expected control record");
756 }
757 mode = 0;
758 for (++cp; cp < buf + 5; cp++) {
759 if (*cp < '0' || *cp > '7')
760 SCREWUP("bad mode");
761 mode = (mode << 3) | (*cp - '0');
762 }
763 if (*cp++ != ' ')
764 SCREWUP("mode not delimited");
765
Damien Miller95def091999-11-25 00:26:21 +1100766 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767 size = size * 10 + (*cp++ - '0');
768 if (*cp++ != ' ')
769 SCREWUP("size not delimited");
770 if (targisdir) {
771 static char *namebuf;
772 static int cursize;
773 size_t need;
774
775 need = strlen(targ) + strlen(cp) + 250;
776 if (need > cursize)
Damien Miller95def091999-11-25 00:26:21 +1100777 namebuf = xmalloc(need);
778 (void) sprintf(namebuf, "%s%s%s", targ,
779 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780 np = namebuf;
781 } else
782 np = targ;
783 curfile = cp;
784 exists = stat(np, &stb) == 0;
785 if (buf[0] == 'D') {
786 int mod_flag = pflag;
787 if (exists) {
788 if (!S_ISDIR(stb.st_mode)) {
789 errno = ENOTDIR;
790 goto bad;
791 }
792 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100793 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794 } else {
Damien Miller95def091999-11-25 00:26:21 +1100795 /* Handle copying from a read-only
796 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797 mod_flag = 1;
798 if (mkdir(np, mode | S_IRWXU) < 0)
799 goto bad;
800 }
801 vect[0] = np;
802 sink(1, vect);
803 if (setimes) {
804 setimes = 0;
805 if (utime(np, &ut) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100806 run_err("%s: set times: %s",
807 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808 }
809 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100810 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811 continue;
812 }
813 omode = mode;
814 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100815 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816bad: run_err("%s: %s", np, strerror(errno));
817 continue;
818 }
Damien Miller95def091999-11-25 00:26:21 +1100819 (void) write(remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100821 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822 continue;
823 }
824 cp = bp->buf;
825 wrerr = NO;
826
827 if (showprogress) {
828 totalbytes = size;
829 progressmeter(-1);
830 }
831 statbytes = 0;
832 for (count = i = 0; i < size; i += 4096) {
833 amt = 4096;
834 if (i + amt > size)
835 amt = size - i;
836 count += amt;
837 do {
838 j = read(remin, cp, amt);
839 if (j <= 0) {
840 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100841 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842 exit(1);
843 }
844 amt -= j;
845 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100846 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847 } while (amt > 0);
848 if (count == bp->cnt) {
849 /* Keep reading so we stay sync'd up. */
850 if (wrerr == NO) {
851 j = write(ofd, bp->buf, count);
852 if (j != count) {
853 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100854 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 }
856 }
857 count = 0;
858 cp = bp->buf;
859 }
860 }
861 if (showprogress)
862 progressmeter(1);
863 if (count != 0 && wrerr == NO &&
864 (j = write(ofd, bp->buf, count)) != count) {
865 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100866 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867 }
868#if 0
869 if (ftruncate(ofd, size)) {
870 run_err("%s: truncate: %s", np, strerror(errno));
871 wrerr = DISPLAYED;
872 }
873#endif
874 if (pflag) {
875 if (exists || omode != mode)
876 if (fchmod(ofd, omode))
877 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100878 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879 } else {
880 if (!exists && omode != mode)
881 if (fchmod(ofd, omode & ~mask))
882 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100883 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 }
Damien Miller95def091999-11-25 00:26:21 +1100885 (void) close(ofd);
886 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 if (setimes && wrerr == NO) {
888 setimes = 0;
889 if (utime(np, &ut) < 0) {
890 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100891 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892 wrerr = DISPLAYED;
893 }
894 }
Damien Miller95def091999-11-25 00:26:21 +1100895 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 case YES:
897 run_err("%s: %s", np, strerror(wrerrno));
898 break;
899 case NO:
Damien Miller95def091999-11-25 00:26:21 +1100900 (void) write(remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 break;
902 case DISPLAYED:
903 break;
904 }
905 }
906screwup:
907 run_err("protocol error: %s", why);
908 exit(1);
909}
910
911int
912response()
913{
914 char ch, *cp, resp, rbuf[2048];
915
916 if (read(remin, &resp, sizeof(resp)) != sizeof(resp))
917 lostconn(0);
918
919 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100920 switch (resp) {
921 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 return (0);
923 default:
924 *cp++ = resp;
925 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100926 case 1: /* error, followed by error msg */
927 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928 do {
929 if (read(remin, &ch, sizeof(ch)) != sizeof(ch))
930 lostconn(0);
931 *cp++ = ch;
932 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
933
934 if (!iamremote)
Damien Miller95def091999-11-25 00:26:21 +1100935 (void) write(STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 ++errs;
937 if (resp == 1)
938 return (-1);
939 exit(1);
940 }
941 /* NOTREACHED */
942}
943
944void
945usage()
946{
Damien Miller95def091999-11-25 00:26:21 +1100947 (void) fprintf(stderr,
Damien Miller34132e52000-01-14 15:45:46 +1100948 "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 +1000949 exit(1);
950}
951
952void
Damien Miller95def091999-11-25 00:26:21 +1100953run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954{
955 static FILE *fp;
956 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
958 ++errs;
959 if (fp == NULL && !(fp = fdopen(remout, "w")))
960 return;
Damien Miller95def091999-11-25 00:26:21 +1100961 (void) fprintf(fp, "%c", 0x01);
962 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100963 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100964 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100965 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100966 (void) fprintf(fp, "\n");
967 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100970 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100971 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100972 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100973 fprintf(stderr, "\n");
974 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975}
976
977/* Stuff below is from BSD rcp util.c. */
978
979/*-
980 * Copyright (c) 1992, 1993
981 * The Regents of the University of California. All rights reserved.
982 *
983 * Redistribution and use in source and binary forms, with or without
984 * modification, are permitted provided that the following conditions
985 * are met:
986 * 1. Redistributions of source code must retain the above copyright
987 * notice, this list of conditions and the following disclaimer.
988 * 2. Redistributions in binary form must reproduce the above copyright
989 * notice, this list of conditions and the following disclaimer in the
990 * documentation and/or other materials provided with the distribution.
991 * 3. All advertising materials mentioning features or use of this software
992 * must display the following acknowledgement:
993 * This product includes software developed by the University of
994 * California, Berkeley and its contributors.
995 * 4. Neither the name of the University nor the names of its contributors
996 * may be used to endorse or promote products derived from this software
997 * without specific prior written permission.
998 *
999 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1000 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1001 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1002 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1003 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1004 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1005 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1006 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1007 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1008 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1009 * SUCH DAMAGE.
1010 *
Damien Miller98c7ad62000-03-09 21:27:49 +11001011 * $Id: scp.c,v 1.17 2000/03/09 10:27:51 damien Exp $
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 */
1013
1014char *
1015colon(cp)
1016 char *cp;
1017{
Damien Miller34132e52000-01-14 15:45:46 +11001018 int flag = 0;
1019
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020 if (*cp == ':') /* Leading colon is part of file name. */
1021 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001022 if (*cp == '[')
1023 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
1025 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001026 if (*cp == '@' && *(cp+1) == '[')
1027 flag = 1;
1028 if (*cp == ']' && *(cp+1) == ':' && flag)
1029 return (cp+1);
1030 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031 return (cp);
1032 if (*cp == '/')
1033 return (0);
1034 }
1035 return (0);
1036}
1037
1038void
1039verifydir(cp)
1040 char *cp;
1041{
1042 struct stat stb;
1043
1044 if (!stat(cp, &stb)) {
1045 if (S_ISDIR(stb.st_mode))
1046 return;
1047 errno = ENOTDIR;
1048 }
1049 run_err("%s: %s", cp, strerror(errno));
1050 exit(1);
1051}
1052
1053int
1054okname(cp0)
1055 char *cp0;
1056{
1057 int c;
1058 char *cp;
1059
1060 cp = cp0;
1061 do {
1062 c = *cp;
1063 if (c & 0200)
1064 goto bad;
1065 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1066 goto bad;
1067 } while (*++cp);
1068 return (1);
1069
Damien Miller98c7ad62000-03-09 21:27:49 +11001070bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071 return (0);
1072}
1073
1074BUF *
1075allocbuf(bp, fd, blksize)
1076 BUF *bp;
1077 int fd, blksize;
1078{
1079 size_t size;
1080 struct stat stb;
1081
1082 if (fstat(fd, &stb) < 0) {
1083 run_err("fstat: %s", strerror(errno));
1084 return (0);
1085 }
Damien Miller95def091999-11-25 00:26:21 +11001086 if (stb.st_blksize == 0)
1087 size = blksize;
1088 else
1089 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1090 stb.st_blksize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091 if (bp->cnt >= size)
1092 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001093 if (bp->buf == NULL)
1094 bp->buf = xmalloc(size);
1095 else
1096 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097 bp->cnt = size;
1098 return (bp);
1099}
1100
1101void
1102lostconn(signo)
1103 int signo;
1104{
1105 if (!iamremote)
1106 fprintf(stderr, "lost connection\n");
1107 exit(1);
1108}
1109
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110
1111void
1112alarmtimer(int wait)
1113{
Damien Miller95def091999-11-25 00:26:21 +11001114 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115
Damien Miller95def091999-11-25 00:26:21 +11001116 itv.it_value.tv_sec = wait;
1117 itv.it_value.tv_usec = 0;
1118 itv.it_interval = itv.it_value;
1119 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120}
1121
1122void
Damien Millerceb483f1999-12-16 13:05:17 +11001123updateprogressmeter(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124{
1125 int save_errno = errno;
1126
1127 progressmeter(0);
1128 errno = save_errno;
1129}
1130
Damien Miller81428f91999-11-18 09:28:11 +11001131int
1132foregroundproc()
1133{
1134 static pid_t pgrp = -1;
1135 int ctty_pgrp;
1136
1137 if (pgrp == -1)
1138 pgrp = getpgrp();
1139
Damien Miller95def091999-11-25 00:26:21 +11001140 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1141 ctty_pgrp == pgrp));
Damien Miller81428f91999-11-18 09:28:11 +11001142}
1143
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144void
1145progressmeter(int flag)
1146{
1147 static const char prefixes[] = " KMGTP";
1148 static struct timeval lastupdate;
1149 static off_t lastsize;
1150 struct timeval now, td, wait;
1151 off_t cursize, abbrevsize;
1152 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001153 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154 char buf[256];
1155
1156 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001157 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158 lastupdate = start;
1159 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001160 }
Damien Miller81428f91999-11-18 09:28:11 +11001161 if (foregroundproc() == 0)
1162 return;
1163
Damien Miller95def091999-11-25 00:26:21 +11001164 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001166 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001167 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 ratio = MAX(ratio, 0);
1169 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001170 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 ratio = 100;
1172
Damien Miller95def091999-11-25 00:26:21 +11001173 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174
1175 barlength = getttywidth() - 51;
1176 if (barlength > 0) {
1177 i = barlength * ratio / 100;
1178 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001179 "|%.*s%*s|", i,
1180 "*****************************************************************************"
1181 "*****************************************************************************",
1182 barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 i = 0;
1185 abbrevsize = cursize;
1186 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1187 i++;
1188 abbrevsize >>= 10;
1189 }
Damien Miller864ea591999-12-15 11:04:25 +11001190 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1191 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
Damien Miller95def091999-11-25 00:26:21 +11001192 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
1194 timersub(&now, &lastupdate, &wait);
1195 if (cursize > lastsize) {
1196 lastupdate = now;
1197 lastsize = cursize;
1198 if (wait.tv_sec >= STALLTIME) {
1199 start.tv_sec += wait.tv_sec;
1200 start.tv_usec += wait.tv_usec;
1201 }
1202 wait.tv_sec = 0;
1203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204 timersub(&now, &start, &td);
1205 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1206
1207 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1208 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001209 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210 } else if (wait.tv_sec >= STALLTIME) {
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001212 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213 } else {
Damien Miller95def091999-11-25 00:26:21 +11001214 remaining = (int) (totalbytes / (statbytes / elapsed) - elapsed);
Damien Miller01ab4a21999-10-28 15:23:30 +10001215 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216 if (i)
1217 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001218 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219 else
1220 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001221 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222 i = remaining % 3600;
1223 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001224 "%02d:%02d ETA", i / 60, i % 60);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225 }
1226 atomicio(write, fileno(stdout), buf, strlen(buf));
1227
1228 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001229 struct sigaction sa;
1230 sa.sa_handler = updateprogressmeter;
1231 sigemptyset(&sa.sa_mask);
1232 sa.sa_flags = SA_RESTART;
1233 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234 alarmtimer(1);
1235 } else if (flag == 1) {
1236 alarmtimer(0);
1237 write(fileno(stdout), "\n", 1);
1238 statbytes = 0;
1239 }
1240}
1241
1242int
1243getttywidth(void)
1244{
1245 struct winsize winsize;
1246
1247 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001248 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249 else
Damien Miller95def091999-11-25 00:26:21 +11001250 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251}