blob: 75e50ce25169237689eb31e268921a7368c0a0ce [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * scp - secure remote copy. This is basically patched BSD rcp which
3 * uses ssh to do the data transfer (instead of using rcmd).
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Millere4340be2000-09-16 13:29:08 +11005 * NOTE: This version should NOT be suid root. (This uses ssh to
6 * do the transfer and ssh has the necessary privileges.)
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Miller95def091999-11-25 00:26:21 +11008 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 */
16/*
17 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41/*
Damien Millerad833b32000-08-23 10:46:23 +100042 * Parts from:
43 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044 * Copyright (c) 1983, 1990, 1992, 1993, 1995
45 * The Regents of the University of California. All rights reserved.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075 */
76
77#include "includes.h"
Damien Miller50a41ed2000-10-16 12:14:42 +110078RCSID("$OpenBSD: scp.c,v 1.42 2000/10/14 10:07:21 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
80#include "ssh.h"
81#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller78315eb2000-09-29 23:01:36 +110083#ifndef _PATH_CP
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084#define _PATH_CP "cp"
Damien Miller78315eb2000-09-29 23:01:36 +110085#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87/* For progressmeter() -- number of seconds before xfer considered "stalled" */
88#define STALLTIME 5
89
Damien Miller484118e2000-07-02 19:13:56 +100090/* Progress meter bar */
91#define BAR \
92 "************************************************************"\
93 "************************************************************"\
94 "************************************************************"\
95 "************************************************************"
96#define MAX_BARLENGTH (sizeof(BAR) - 1)
97
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098/* Visual statistics about files as they are transferred. */
99void progressmeter(int);
100
101/* Returns width of the terminal (for progress meter calculations). */
102int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000103int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller874d77b2000-10-14 16:23:11 +1100105/* setup arguments for the call to ssh */
106void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
107
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108/* Time a transfer started. */
109static struct timeval start;
110
111/* Number of bytes of current file transferred so far. */
112volatile unsigned long statbytes;
113
114/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100115off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
117/* Name of current file being transferred. */
118char *curfile;
119
120/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100121int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123/* This is set to non-zero if compression is desired. */
Damien Millerd8087f61999-11-25 12:31:26 +1100124int compress_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
126/* This is set to zero if the progressmeter is not desired. */
127int showprogress = 1;
128
Damien Millerad833b32000-08-23 10:46:23 +1000129/* This is the program to execute for the secured connection. ("ssh" or -S) */
130char *ssh_program = SSH_PROGRAM;
131
Damien Miller874d77b2000-10-14 16:23:11 +1100132/* This is the list of arguments that scp passes to ssh */
133struct {
134 char **list;
135 int num;
136 int nalloc;
137} args;
138
Damien Miller5428f641999-11-25 11:54:57 +1100139/*
140 * This function executes the given command as the specified user on the
141 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
142 * assigns the input and output file descriptors on success.
143 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller4af51302000-04-16 11:18:38 +1000145int
Damien Millerad833b32000-08-23 10:46:23 +1000146do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147{
Damien Miller95def091999-11-25 00:26:21 +1100148 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller95def091999-11-25 00:26:21 +1100150 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100151 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
152 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Damien Miller5428f641999-11-25 11:54:57 +1100154 /*
155 * Reserve two descriptors so that the real pipes won't get
156 * descriptors 0 and 1 because that will screw up dup2 below.
157 */
Damien Miller95def091999-11-25 00:26:21 +1100158 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller95def091999-11-25 00:26:21 +1100160 /* Create a socket pair for communicating with ssh. */
161 if (pipe(pin) < 0)
162 fatal("pipe: %s", strerror(errno));
163 if (pipe(pout) < 0)
164 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Damien Miller95def091999-11-25 00:26:21 +1100166 /* Free the reserved descriptors. */
167 close(reserved[0]);
168 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller95def091999-11-25 00:26:21 +1100170 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100171 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100172 /* Child. */
173 close(pin[1]);
174 close(pout[0]);
175 dup2(pin[0], 0);
176 dup2(pout[1], 1);
177 close(pin[0]);
178 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller874d77b2000-10-14 16:23:11 +1100180 args.list[0] = ssh_program;
181 if (remuser != NULL)
Damien Millere4041c92000-10-14 17:45:58 +1100182 addargs("-l%s", remuser);
Damien Miller874d77b2000-10-14 16:23:11 +1100183 addargs("%s", host);
184 addargs("%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100185
Damien Miller874d77b2000-10-14 16:23:11 +1100186 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000187 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100188 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189 }
Damien Miller95def091999-11-25 00:26:21 +1100190 /* Parent. Close the other side, and return the local side. */
191 close(pin[0]);
192 *fdout = pin[1];
193 close(pout[1]);
194 *fdin = pout[0];
195 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196}
197
Damien Miller4af51302000-04-16 11:18:38 +1000198void
Damien Miller95def091999-11-25 00:26:21 +1100199fatal(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200{
Damien Miller95def091999-11-25 00:26:21 +1100201 va_list ap;
202 char buf[1024];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Miller95def091999-11-25 00:26:21 +1100204 va_start(ap, fmt);
205 vsnprintf(buf, sizeof(buf), fmt, ap);
206 va_end(ap);
207 fprintf(stderr, "%s\n", buf);
208 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
210
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211typedef 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
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100227uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228int errs, remin, remout;
229int pflag, iamremote, iamrecursive, targetshouldbedirectory;
230
231#define CMDNEEDS 64
232char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
233
Damien Miller95def091999-11-25 00:26:21 +1100234int response(void);
235void rsource(char *, struct stat *);
236void sink(int, char *[]);
237void source(int, char *[]);
238void tolocal(int, char *[]);
239void toremote(char *, int, char *[]);
240void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
242int
243main(argc, argv)
244 int argc;
245 char *argv[];
246{
247 int ch, fflag, tflag;
248 char *targ;
249 extern char *optarg;
250 extern int optind;
251
Damien Miller874d77b2000-10-14 16:23:11 +1100252 args.list = NULL;
253 addargs("ssh"); /* overwritten with ssh_program */
254 addargs("-x");
255 addargs("-oFallBackToRsh no");
256
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257 fflag = tflag = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100258 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
Damien Miller95def091999-11-25 00:26:21 +1100259 switch (ch) {
260 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100261 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100262 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100263 case 'C':
264 addargs("-%c", ch);
265 break;
266 case 'o':
267 case 'c':
268 case 'i':
Damien Miller50a41ed2000-10-16 12:14:42 +1100269 addargs("-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100270 break;
271 case 'P':
Damien Miller50a41ed2000-10-16 12:14:42 +1100272 addargs("-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100273 break;
274 case 'B':
Damien Miller50a41ed2000-10-16 12:14:42 +1100275 addargs("-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100276 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277 case 'p':
278 pflag = 1;
279 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280 case 'r':
281 iamrecursive = 1;
282 break;
Damien Millerad833b32000-08-23 10:46:23 +1000283 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100284 ssh_program = xstrdup(optarg);
285 break;
286 case 'v':
287 verbose_mode = 1;
288 break;
289 case 'q':
290 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000291 break;
292
Damien Miller95def091999-11-25 00:26:21 +1100293 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 case 'd':
295 targetshouldbedirectory = 1;
296 break;
Damien Miller95def091999-11-25 00:26:21 +1100297 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298 iamremote = 1;
299 fflag = 1;
300 break;
Damien Miller95def091999-11-25 00:26:21 +1100301 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302 iamremote = 1;
303 tflag = 1;
304 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305 case '?':
306 default:
307 usage();
308 }
309 argc -= optind;
310 argv += optind;
311
312 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100313 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller95def091999-11-25 00:26:21 +1100315 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316 showprogress = 0;
317
318 remin = STDIN_FILENO;
319 remout = STDOUT_FILENO;
320
Damien Miller95def091999-11-25 00:26:21 +1100321 if (fflag) {
322 /* Follow "protocol", send data. */
323 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324 source(argc, argv);
325 exit(errs != 0);
326 }
Damien Miller95def091999-11-25 00:26:21 +1100327 if (tflag) {
328 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000329 sink(argc, argv);
330 exit(errs != 0);
331 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332 if (argc < 2)
333 usage();
334 if (argc > 2)
335 targetshouldbedirectory = 1;
336
337 remin = remout = -1;
338 /* Command to be executed on remote system using "ssh". */
Damien Miller95def091999-11-25 00:26:21 +1100339 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000340 iamrecursive ? " -r" : "", pflag ? " -p" : "",
341 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
Damien Miller95def091999-11-25 00:26:21 +1100343 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
345 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
346 toremote(targ, argc, argv);
347 else {
Damien Miller95def091999-11-25 00:26:21 +1100348 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349 if (targetshouldbedirectory)
350 verifydir(argv[argc - 1]);
351 }
352 exit(errs != 0);
353}
354
Damien Miller34132e52000-01-14 15:45:46 +1100355char *
356cleanhostname(host)
357 char *host;
358{
359 if (*host == '[' && host[strlen(host) - 1] == ']') {
360 host[strlen(host) - 1] = '\0';
361 return (host + 1);
362 } else
363 return host;
364}
365
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366void
367toremote(targ, argc, argv)
368 char *targ, *argv[];
369 int argc;
370{
371 int i, len;
372 char *bp, *host, *src, *suser, *thost, *tuser;
373
374 *targ++ = 0;
375 if (*targ == 0)
376 targ = ".";
377
378 if ((thost = strchr(argv[argc - 1], '@'))) {
379 /* user@host */
380 *thost++ = 0;
381 tuser = argv[argc - 1];
382 if (*tuser == '\0')
383 tuser = NULL;
384 else if (!okname(tuser))
385 exit(1);
386 } else {
387 thost = argv[argc - 1];
388 tuser = NULL;
389 }
390
391 for (i = 0; i < argc - 1; i++) {
392 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100393 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394 *src++ = 0;
395 if (*src == 0)
396 src = ".";
397 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000398 len = strlen(ssh_program) + strlen(argv[i]) +
399 strlen(src) + (tuser ? strlen(tuser) : 0) +
400 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100401 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402 if (host) {
403 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100404 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405 suser = argv[i];
406 if (*suser == '\0')
407 suser = pwd->pw_name;
408 else if (!okname(suser))
409 continue;
Damien Miller95def091999-11-25 00:26:21 +1100410 (void) sprintf(bp,
Damien Millerad833b32000-08-23 10:46:23 +1000411 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
412 ssh_program, verbose_mode ? " -v" : "",
413 suser, host, cmd, src,
414 tuser ? tuser : "", tuser ? "@" : "",
415 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100416 } else {
417 host = cleanhostname(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100418 (void) sprintf(bp,
Damien Millerad833b32000-08-23 10:46:23 +1000419 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
420 ssh_program, verbose_mode ? " -v" : "",
421 host, cmd, src,
422 tuser ? tuser : "", tuser ? "@" : "",
423 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100424 }
Damien Miller95def091999-11-25 00:26:21 +1100425 if (verbose_mode)
426 fprintf(stderr, "Executing: %s\n", bp);
427 (void) system(bp);
428 (void) xfree(bp);
429 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430 if (remin == -1) {
431 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100432 bp = xmalloc(len);
433 (void) sprintf(bp, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100434 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000435 if (do_cmd(host, tuser, bp, &remin,
436 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100437 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 if (response() < 0)
439 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100440 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441 }
Damien Miller95def091999-11-25 00:26:21 +1100442 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 }
444 }
445}
446
447void
448tolocal(argc, argv)
449 int argc;
450 char *argv[];
451{
452 int i, len;
453 char *bp, *host, *src, *suser;
454
455 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100456 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000458 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459 bp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +1100460 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000461 iamrecursive ? " -r" : "", pflag ? " -p" : "",
462 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100463 if (verbose_mode)
464 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 if (system(bp))
466 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100467 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468 continue;
469 }
470 *src++ = 0;
471 if (*src == 0)
472 src = ".";
473 if ((host = strchr(argv[i], '@')) == NULL) {
474 host = argv[i];
475 suser = NULL;
476 } else {
477 *host++ = 0;
478 suser = argv[i];
479 if (*suser == '\0')
480 suser = pwd->pw_name;
481 else if (!okname(suser))
482 continue;
483 }
Damien Miller34132e52000-01-14 15:45:46 +1100484 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100486 bp = xmalloc(len);
487 (void) sprintf(bp, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000488 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100489 (void) xfree(bp);
490 ++errs;
491 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 }
Damien Miller95def091999-11-25 00:26:21 +1100493 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100495 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496 remin = remout = -1;
497 }
498}
499
500void
501source(argc, argv)
502 int argc;
503 char *argv[];
504{
505 struct stat stb;
506 static BUF buffer;
507 BUF *bp;
508 off_t i;
509 int amt, fd, haderr, indx, result;
510 char *last, *name, buf[2048];
511
512 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100513 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514 statbytes = 0;
515 if ((fd = open(name, O_RDONLY, 0)) < 0)
516 goto syserr;
517 if (fstat(fd, &stb) < 0) {
518syserr: run_err("%s: %s", name, strerror(errno));
519 goto next;
520 }
521 switch (stb.st_mode & S_IFMT) {
522 case S_IFREG:
523 break;
524 case S_IFDIR:
525 if (iamrecursive) {
526 rsource(name, &stb);
527 goto next;
528 }
529 /* FALLTHROUGH */
530 default:
531 run_err("%s: not a regular file", name);
532 goto next;
533 }
534 if ((last = strrchr(name, '/')) == NULL)
535 last = name;
536 else
537 ++last;
538 curfile = last;
539 if (pflag) {
540 /*
541 * Make it compatible with possible future
542 * versions expecting microseconds.
543 */
Damien Miller95def091999-11-25 00:26:21 +1100544 (void) sprintf(buf, "T%lu 0 %lu 0\n",
Damien Millerad833b32000-08-23 10:46:23 +1000545 (unsigned long) stb.st_mtime,
546 (unsigned long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000547 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000548 if (response() < 0)
549 goto next;
550 }
551#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller95def091999-11-25 00:26:21 +1100552 (void) sprintf(buf, "C%04o %lu %s\n",
553 (unsigned int) (stb.st_mode & FILEMODEMASK),
554 (unsigned long) stb.st_size,
555 last);
556 if (verbose_mode) {
557 fprintf(stderr, "Sending file modes: %s", buf);
558 fflush(stderr);
559 }
Damien Miller35dabd02000-05-01 21:10:33 +1000560 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 if (response() < 0)
562 goto next;
563 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100564next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 continue;
566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567 if (showprogress) {
568 totalbytes = stb.st_size;
569 progressmeter(-1);
570 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571 /* Keep writing after an error so that we stay sync'd up. */
572 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
573 amt = bp->cnt;
574 if (i + amt > stb.st_size)
575 amt = stb.st_size - i;
576 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000577 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578 if (result != amt)
579 haderr = result >= 0 ? EIO : errno;
580 }
581 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000582 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 else {
Damien Miller864ea591999-12-15 11:04:25 +1100584 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 if (result != amt)
586 haderr = result >= 0 ? EIO : errno;
587 statbytes += result;
588 }
589 }
Damien Miller95def091999-11-25 00:26:21 +1100590 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 progressmeter(1);
592
593 if (close(fd) < 0 && !haderr)
594 haderr = errno;
595 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000596 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597 else
598 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100599 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000600 }
601}
602
603void
604rsource(name, statp)
605 char *name;
606 struct stat *statp;
607{
608 DIR *dirp;
609 struct dirent *dp;
610 char *last, *vect[1], path[1100];
611
612 if (!(dirp = opendir(name))) {
613 run_err("%s: %s", name, strerror(errno));
614 return;
615 }
616 last = strrchr(name, '/');
617 if (last == 0)
618 last = name;
619 else
620 last++;
621 if (pflag) {
Damien Miller95def091999-11-25 00:26:21 +1100622 (void) sprintf(path, "T%lu 0 %lu 0\n",
Damien Millerad833b32000-08-23 10:46:23 +1000623 (unsigned long) statp->st_mtime,
624 (unsigned long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000625 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626 if (response() < 0) {
627 closedir(dirp);
628 return;
629 }
630 }
Damien Miller95def091999-11-25 00:26:21 +1100631 (void) sprintf(path, "D%04o %d %.1024s\n",
Damien Millerad833b32000-08-23 10:46:23 +1000632 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100633 if (verbose_mode)
634 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000635 (void) atomicio(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);
Damien Miller35dabd02000-05-01 21:10:33 +1000654 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100655 (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;
Damien Millercaf6dd62000-08-29 11:33:50 +1100671 off_t size;
672 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100674 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100675 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676
677#define SCREWUP(str) { why = str; goto screwup; }
678
679 setimes = targisdir = 0;
680 mask = umask(0);
681 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100682 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683 if (argc != 1) {
684 run_err("ambiguous target");
685 exit(1);
686 }
687 targ = *argv;
688 if (targetshouldbedirectory)
689 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100690
Damien Miller35dabd02000-05-01 21:10:33 +1000691 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
693 targisdir = 1;
694 for (first = 1;; first = 0) {
695 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000696 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697 return;
698 if (*cp++ == '\n')
699 SCREWUP("unexpected <newline>");
700 do {
Damien Millere247cc42000-05-07 12:03:14 +1000701 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702 SCREWUP("lost connection");
703 *cp++ = ch;
704 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
705 *cp = 0;
706
707 if (buf[0] == '\01' || buf[0] == '\02') {
708 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000709 (void) atomicio(write, STDERR_FILENO,
Damien Miller95def091999-11-25 00:26:21 +1100710 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711 if (buf[0] == '\02')
712 exit(1);
713 ++errs;
714 continue;
715 }
716 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000717 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718 return;
719 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720 if (ch == '\n')
721 *--cp = 0;
722
723#define getnum(t) (t) = 0; \
724 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
725 cp = buf;
726 if (*cp == 'T') {
727 setimes++;
728 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100729 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730 if (*cp++ != ' ')
731 SCREWUP("mtime.sec not delimited");
732 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100733 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734 if (*cp++ != ' ')
735 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100736 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737 if (*cp++ != ' ')
738 SCREWUP("atime.sec not delimited");
739 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100740 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 if (*cp++ != '\0')
742 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000743 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744 continue;
745 }
746 if (*cp != 'C' && *cp != 'D') {
747 /*
748 * Check for the case "rcp remote:foo\* local:bar".
749 * In this case, the line "No match." can be returned
750 * by the shell before the rcp command on the remote is
751 * executed so the ^Aerror_message convention isn't
752 * followed.
753 */
754 if (first) {
755 run_err("%s", cp);
756 exit(1);
757 }
758 SCREWUP("expected control record");
759 }
760 mode = 0;
761 for (++cp; cp < buf + 5; cp++) {
762 if (*cp < '0' || *cp > '7')
763 SCREWUP("bad mode");
764 mode = (mode << 3) | (*cp - '0');
765 }
766 if (*cp++ != ' ')
767 SCREWUP("mode not delimited");
768
Damien Miller95def091999-11-25 00:26:21 +1100769 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770 size = size * 10 + (*cp++ - '0');
771 if (*cp++ != ' ')
772 SCREWUP("size not delimited");
773 if (targisdir) {
774 static char *namebuf;
775 static int cursize;
776 size_t need;
777
778 need = strlen(targ) + strlen(cp) + 250;
779 if (need > cursize)
Damien Miller95def091999-11-25 00:26:21 +1100780 namebuf = xmalloc(need);
781 (void) sprintf(namebuf, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000782 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783 np = namebuf;
784 } else
785 np = targ;
786 curfile = cp;
787 exists = stat(np, &stb) == 0;
788 if (buf[0] == 'D') {
789 int mod_flag = pflag;
790 if (exists) {
791 if (!S_ISDIR(stb.st_mode)) {
792 errno = ENOTDIR;
793 goto bad;
794 }
795 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100796 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797 } else {
Damien Miller95def091999-11-25 00:26:21 +1100798 /* Handle copying from a read-only
799 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800 mod_flag = 1;
801 if (mkdir(np, mode | S_IRWXU) < 0)
802 goto bad;
803 }
804 vect[0] = np;
805 sink(1, vect);
806 if (setimes) {
807 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100808 if (utimes(np, tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100809 run_err("%s: set times: %s",
810 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811 }
812 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100813 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814 continue;
815 }
816 omode = mode;
817 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100818 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819bad: run_err("%s: %s", np, strerror(errno));
820 continue;
821 }
Damien Miller35dabd02000-05-01 21:10:33 +1000822 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100824 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825 continue;
826 }
827 cp = bp->buf;
828 wrerr = NO;
829
830 if (showprogress) {
831 totalbytes = size;
832 progressmeter(-1);
833 }
834 statbytes = 0;
835 for (count = i = 0; i < size; i += 4096) {
836 amt = 4096;
837 if (i + amt > size)
838 amt = size - i;
839 count += amt;
840 do {
Damien Millere247cc42000-05-07 12:03:14 +1000841 j = atomicio(read, remin, cp, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842 if (j <= 0) {
843 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100844 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845 exit(1);
846 }
847 amt -= j;
848 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100849 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 } while (amt > 0);
851 if (count == bp->cnt) {
852 /* Keep reading so we stay sync'd up. */
853 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000854 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 if (j != count) {
856 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100857 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858 }
859 }
860 count = 0;
861 cp = bp->buf;
862 }
863 }
864 if (showprogress)
865 progressmeter(1);
866 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000867 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100869 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870 }
871#if 0
872 if (ftruncate(ofd, size)) {
873 run_err("%s: truncate: %s", np, strerror(errno));
874 wrerr = DISPLAYED;
875 }
876#endif
877 if (pflag) {
878 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100879#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100881#else /* HAVE_FCHMOD */
882 if (chmod(np, omode))
883#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100885 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 } else {
887 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100888#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100890#else /* HAVE_FCHMOD */
891 if (chmod(np, omode & ~mask))
892#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100894 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 }
Damien Millerbe484b52000-07-15 14:14:16 +1000896 if (close(ofd) == -1) {
897 wrerr = YES;
898 wrerrno = errno;
899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 if (setimes && wrerr == NO) {
902 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100903 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100905 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 wrerr = DISPLAYED;
907 }
908 }
Damien Miller95def091999-11-25 00:26:21 +1100909 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910 case YES:
911 run_err("%s: %s", np, strerror(wrerrno));
912 break;
913 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000914 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 break;
916 case DISPLAYED:
917 break;
918 }
919 }
920screwup:
921 run_err("protocol error: %s", why);
922 exit(1);
923}
924
925int
926response()
927{
928 char ch, *cp, resp, rbuf[2048];
929
Damien Millere247cc42000-05-07 12:03:14 +1000930 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931 lostconn(0);
932
933 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100934 switch (resp) {
935 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 return (0);
937 default:
938 *cp++ = resp;
939 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100940 case 1: /* error, followed by error msg */
941 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 do {
Damien Millere247cc42000-05-07 12:03:14 +1000943 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944 lostconn(0);
945 *cp++ = ch;
946 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
947
948 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000949 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950 ++errs;
951 if (resp == 1)
952 return (-1);
953 exit(1);
954 }
955 /* NOTREACHED */
956}
957
958void
959usage()
960{
Damien Millerad833b32000-08-23 10:46:23 +1000961 (void) fprintf(stderr, "usage: scp "
962 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
963 " scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964 exit(1);
965}
966
967void
Damien Miller95def091999-11-25 00:26:21 +1100968run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969{
970 static FILE *fp;
971 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
973 ++errs;
974 if (fp == NULL && !(fp = fdopen(remout, "w")))
975 return;
Damien Miller95def091999-11-25 00:26:21 +1100976 (void) fprintf(fp, "%c", 0x01);
977 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100978 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100979 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100980 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100981 (void) fprintf(fp, "\n");
982 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Damien Miller95def091999-11-25 00:26:21 +1100984 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100985 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100986 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100987 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100988 fprintf(stderr, "\n");
989 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990}
991
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992char *
993colon(cp)
994 char *cp;
995{
Damien Miller34132e52000-01-14 15:45:46 +1100996 int flag = 0;
997
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998 if (*cp == ':') /* Leading colon is part of file name. */
999 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001000 if (*cp == '[')
1001 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
1003 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001004 if (*cp == '@' && *(cp+1) == '[')
1005 flag = 1;
1006 if (*cp == ']' && *(cp+1) == ':' && flag)
1007 return (cp+1);
1008 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009 return (cp);
1010 if (*cp == '/')
1011 return (0);
1012 }
1013 return (0);
1014}
1015
1016void
1017verifydir(cp)
1018 char *cp;
1019{
1020 struct stat stb;
1021
1022 if (!stat(cp, &stb)) {
1023 if (S_ISDIR(stb.st_mode))
1024 return;
1025 errno = ENOTDIR;
1026 }
1027 run_err("%s: %s", cp, strerror(errno));
1028 exit(1);
1029}
1030
1031int
1032okname(cp0)
1033 char *cp0;
1034{
1035 int c;
1036 char *cp;
1037
1038 cp = cp0;
1039 do {
1040 c = *cp;
1041 if (c & 0200)
1042 goto bad;
1043 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1044 goto bad;
1045 } while (*++cp);
1046 return (1);
1047
Damien Miller98c7ad62000-03-09 21:27:49 +11001048bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049 return (0);
1050}
1051
1052BUF *
1053allocbuf(bp, fd, blksize)
1054 BUF *bp;
1055 int fd, blksize;
1056{
1057 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001058#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059 struct stat stb;
1060
1061 if (fstat(fd, &stb) < 0) {
1062 run_err("fstat: %s", strerror(errno));
1063 return (0);
1064 }
Damien Miller95def091999-11-25 00:26:21 +11001065 if (stb.st_blksize == 0)
1066 size = blksize;
1067 else
1068 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001069 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001070#else /* HAVE_ST_BLKSIZE */
1071 size = blksize;
1072#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073 if (bp->cnt >= size)
1074 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001075 if (bp->buf == NULL)
1076 bp->buf = xmalloc(size);
1077 else
1078 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079 bp->cnt = size;
1080 return (bp);
1081}
1082
1083void
1084lostconn(signo)
1085 int signo;
1086{
1087 if (!iamremote)
1088 fprintf(stderr, "lost connection\n");
1089 exit(1);
1090}
1091
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001092
1093void
1094alarmtimer(int wait)
1095{
Damien Miller95def091999-11-25 00:26:21 +11001096 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097
Damien Miller95def091999-11-25 00:26:21 +11001098 itv.it_value.tv_sec = wait;
1099 itv.it_value.tv_usec = 0;
1100 itv.it_interval = itv.it_value;
1101 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102}
1103
1104void
Damien Miller7684ee12000-03-17 23:40:15 +11001105updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106{
1107 int save_errno = errno;
1108
1109 progressmeter(0);
1110 errno = save_errno;
1111}
1112
Damien Miller81428f91999-11-18 09:28:11 +11001113int
1114foregroundproc()
1115{
1116 static pid_t pgrp = -1;
1117 int ctty_pgrp;
1118
1119 if (pgrp == -1)
1120 pgrp = getpgrp();
1121
Damien Millerbac2d8a2000-09-05 16:13:06 +11001122#ifdef HAVE_CYGWIN
1123 /*
1124 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1125 * currently.
1126 */
1127 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1128 ctty_pgrp == pgrp);
1129#else
Damien Miller95def091999-11-25 00:26:21 +11001130 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1131 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001132#endif
Damien Miller81428f91999-11-18 09:28:11 +11001133}
1134
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135void
1136progressmeter(int flag)
1137{
1138 static const char prefixes[] = " KMGTP";
1139 static struct timeval lastupdate;
1140 static off_t lastsize;
1141 struct timeval now, td, wait;
1142 off_t cursize, abbrevsize;
1143 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001144 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145 char buf[256];
1146
1147 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001148 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149 lastupdate = start;
1150 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001151 }
Damien Miller81428f91999-11-18 09:28:11 +11001152 if (foregroundproc() == 0)
1153 return;
1154
Damien Miller95def091999-11-25 00:26:21 +11001155 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001157 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001158 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159 ratio = MAX(ratio, 0);
1160 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001161 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 ratio = 100;
1163
Damien Miller95def091999-11-25 00:26:21 +11001164 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165
1166 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001167 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 if (barlength > 0) {
1169 i = barlength * ratio / 100;
1170 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001171 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 i = 0;
1174 abbrevsize = cursize;
1175 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1176 i++;
1177 abbrevsize >>= 10;
1178 }
Damien Miller864ea591999-12-15 11:04:25 +11001179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1180 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
Damien Miller95def091999-11-25 00:26:21 +11001181 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182
1183 timersub(&now, &lastupdate, &wait);
1184 if (cursize > lastsize) {
1185 lastupdate = now;
1186 lastsize = cursize;
1187 if (wait.tv_sec >= STALLTIME) {
1188 start.tv_sec += wait.tv_sec;
1189 start.tv_usec += wait.tv_usec;
1190 }
1191 wait.tv_sec = 0;
1192 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 timersub(&now, &start, &td);
1194 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1195
1196 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1197 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001198 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199 } else if (wait.tv_sec >= STALLTIME) {
1200 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001201 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001203 if (flag != 1)
1204 remaining =
1205 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1206 else
1207 remaining = elapsed;
1208
Damien Miller01ab4a21999-10-28 15:23:30 +10001209 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210 if (i)
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001212 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213 else
1214 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001215 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216 i = remaining % 3600;
1217 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001218 "%02d:%02d%s", i / 60, i % 60,
1219 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220 }
1221 atomicio(write, fileno(stdout), buf, strlen(buf));
1222
1223 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001224 struct sigaction sa;
1225 sa.sa_handler = updateprogressmeter;
Damien Miller77aba9d2000-08-30 10:11:30 +11001226 sigemptyset((sigset_t *)&sa.sa_mask);
Damien Miller615f9392000-05-17 22:53:33 +10001227#ifdef SA_RESTART
Damien Miller864ea591999-12-15 11:04:25 +11001228 sa.sa_flags = SA_RESTART;
Damien Miller615f9392000-05-17 22:53:33 +10001229#endif
Damien Miller864ea591999-12-15 11:04:25 +11001230 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231 alarmtimer(1);
1232 } else if (flag == 1) {
1233 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001234 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235 statbytes = 0;
1236 }
1237}
1238
1239int
1240getttywidth(void)
1241{
1242 struct winsize winsize;
1243
1244 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001245 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246 else
Damien Miller95def091999-11-25 00:26:21 +11001247 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248}
Damien Miller874d77b2000-10-14 16:23:11 +11001249
1250void
1251addargs(char *fmt, ...)
1252{
1253 va_list ap;
1254 char buf[1024];
1255
1256 va_start(ap, fmt);
1257 vsnprintf(buf, sizeof(buf), fmt, ap);
1258 va_end(ap);
1259
1260 if (args.list == NULL) {
1261 args.nalloc = 32;
1262 args.num = 0;
1263 args.list = xmalloc(args.nalloc * sizeof(char *));
1264 } else if (args.num+2 >= args.nalloc) {
1265 args.nalloc *= 2;
1266 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1267 }
1268 args.list[args.num++] = xstrdup(buf);
1269 args.list[args.num] = NULL;
1270}