blob: 9af5c54dd667017f084ebfae5de8c88d2e0f14c5 [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 Miller69b69aa2000-10-28 14:19:58 +110078RCSID("$OpenBSD: scp.c,v 1.43 2000/10/18 18:23:02 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
Ben Lindstrom49a79c02000-11-17 03:47:20 +000087#ifdef HAVE___PROGNAME
88extern char *__progname;
89#else
90char *__progname;
91#endif
92
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093/* For progressmeter() -- number of seconds before xfer considered "stalled" */
94#define STALLTIME 5
95
Damien Miller484118e2000-07-02 19:13:56 +100096/* Progress meter bar */
97#define BAR \
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"\
101 "************************************************************"
102#define MAX_BARLENGTH (sizeof(BAR) - 1)
103
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104/* Visual statistics about files as they are transferred. */
105void progressmeter(int);
106
107/* Returns width of the terminal (for progress meter calculations). */
108int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000109int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller874d77b2000-10-14 16:23:11 +1100111/* setup arguments for the call to ssh */
112void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
113
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114/* Time a transfer started. */
115static struct timeval start;
116
117/* Number of bytes of current file transferred so far. */
118volatile unsigned long statbytes;
119
120/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100121off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123/* Name of current file being transferred. */
124char *curfile;
125
126/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100127int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
129/* This is set to non-zero if compression is desired. */
Damien Millerd8087f61999-11-25 12:31:26 +1100130int compress_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
132/* This is set to zero if the progressmeter is not desired. */
133int showprogress = 1;
134
Damien Millerad833b32000-08-23 10:46:23 +1000135/* This is the program to execute for the secured connection. ("ssh" or -S) */
136char *ssh_program = SSH_PROGRAM;
137
Damien Miller874d77b2000-10-14 16:23:11 +1100138/* This is the list of arguments that scp passes to ssh */
139struct {
140 char **list;
141 int num;
142 int nalloc;
143} args;
144
Damien Miller5428f641999-11-25 11:54:57 +1100145/*
146 * This function executes the given command as the specified user on the
147 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
148 * assigns the input and output file descriptors on success.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller4af51302000-04-16 11:18:38 +1000151int
Damien Millerad833b32000-08-23 10:46:23 +1000152do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller95def091999-11-25 00:26:21 +1100156 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100157 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
158 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller5428f641999-11-25 11:54:57 +1100160 /*
161 * Reserve two descriptors so that the real pipes won't get
162 * descriptors 0 and 1 because that will screw up dup2 below.
163 */
Damien Miller95def091999-11-25 00:26:21 +1100164 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Damien Miller95def091999-11-25 00:26:21 +1100166 /* Create a socket pair for communicating with ssh. */
167 if (pipe(pin) < 0)
168 fatal("pipe: %s", strerror(errno));
169 if (pipe(pout) < 0)
170 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171
Damien Miller95def091999-11-25 00:26:21 +1100172 /* Free the reserved descriptors. */
173 close(reserved[0]);
174 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller95def091999-11-25 00:26:21 +1100176 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100177 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100178 /* Child. */
179 close(pin[1]);
180 close(pout[0]);
181 dup2(pin[0], 0);
182 dup2(pout[1], 1);
183 close(pin[0]);
184 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185
Damien Miller874d77b2000-10-14 16:23:11 +1100186 args.list[0] = ssh_program;
187 if (remuser != NULL)
Damien Millere4041c92000-10-14 17:45:58 +1100188 addargs("-l%s", remuser);
Damien Miller874d77b2000-10-14 16:23:11 +1100189 addargs("%s", host);
190 addargs("%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100191
Damien Miller874d77b2000-10-14 16:23:11 +1100192 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000193 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100194 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195 }
Damien Miller95def091999-11-25 00:26:21 +1100196 /* Parent. Close the other side, and return the local side. */
197 close(pin[0]);
198 *fdout = pin[1];
199 close(pout[1]);
200 *fdin = pout[0];
201 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202}
203
Damien Miller4af51302000-04-16 11:18:38 +1000204void
Damien Miller95def091999-11-25 00:26:21 +1100205fatal(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Damien Miller95def091999-11-25 00:26:21 +1100207 va_list ap;
208 char buf[1024];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Damien Miller95def091999-11-25 00:26:21 +1100210 va_start(ap, fmt);
211 vsnprintf(buf, sizeof(buf), fmt, ap);
212 va_end(ap);
213 fprintf(stderr, "%s\n", buf);
214 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215}
216
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217typedef struct {
218 int cnt;
219 char *buf;
220} BUF;
221
222extern int iamremote;
223
Damien Miller95def091999-11-25 00:26:21 +1100224BUF *allocbuf(BUF *, int, int);
225char *colon(char *);
226void lostconn(int);
227void nospace(void);
228int okname(char *);
229void run_err(const char *,...);
230void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100233uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234int errs, remin, remout;
235int pflag, iamremote, iamrecursive, targetshouldbedirectory;
236
237#define CMDNEEDS 64
238char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
239
Damien Miller95def091999-11-25 00:26:21 +1100240int response(void);
241void rsource(char *, struct stat *);
242void sink(int, char *[]);
243void source(int, char *[]);
244void tolocal(int, char *[]);
245void toremote(char *, int, char *[]);
246void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
248int
249main(argc, argv)
250 int argc;
251 char *argv[];
252{
253 int ch, fflag, tflag;
254 char *targ;
255 extern char *optarg;
256 extern int optind;
257
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000258 __progname = get_progname(argv[0]);
259
Damien Miller874d77b2000-10-14 16:23:11 +1100260 args.list = NULL;
261 addargs("ssh"); /* overwritten with ssh_program */
262 addargs("-x");
263 addargs("-oFallBackToRsh no");
264
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 fflag = tflag = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100266 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != EOF)
Damien Miller95def091999-11-25 00:26:21 +1100267 switch (ch) {
268 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100269 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100270 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100271 case 'C':
272 addargs("-%c", ch);
273 break;
274 case 'o':
275 case 'c':
276 case 'i':
Damien Miller50a41ed2000-10-16 12:14:42 +1100277 addargs("-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100278 break;
279 case 'P':
Damien Miller50a41ed2000-10-16 12:14:42 +1100280 addargs("-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100281 break;
282 case 'B':
Damien Miller50a41ed2000-10-16 12:14:42 +1100283 addargs("-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100284 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285 case 'p':
286 pflag = 1;
287 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 case 'r':
289 iamrecursive = 1;
290 break;
Damien Millerad833b32000-08-23 10:46:23 +1000291 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100292 ssh_program = xstrdup(optarg);
293 break;
294 case 'v':
295 verbose_mode = 1;
296 break;
297 case 'q':
298 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000299 break;
300
Damien Miller95def091999-11-25 00:26:21 +1100301 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302 case 'd':
303 targetshouldbedirectory = 1;
304 break;
Damien Miller95def091999-11-25 00:26:21 +1100305 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306 iamremote = 1;
307 fflag = 1;
308 break;
Damien Miller95def091999-11-25 00:26:21 +1100309 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310 iamremote = 1;
311 tflag = 1;
312 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313 case '?':
314 default:
315 usage();
316 }
317 argc -= optind;
318 argv += optind;
319
320 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100321 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Miller95def091999-11-25 00:26:21 +1100323 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324 showprogress = 0;
325
326 remin = STDIN_FILENO;
327 remout = STDOUT_FILENO;
328
Damien Miller95def091999-11-25 00:26:21 +1100329 if (fflag) {
330 /* Follow "protocol", send data. */
331 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332 source(argc, argv);
333 exit(errs != 0);
334 }
Damien Miller95def091999-11-25 00:26:21 +1100335 if (tflag) {
336 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337 sink(argc, argv);
338 exit(errs != 0);
339 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340 if (argc < 2)
341 usage();
342 if (argc > 2)
343 targetshouldbedirectory = 1;
344
345 remin = remout = -1;
346 /* Command to be executed on remote system using "ssh". */
Damien Miller95def091999-11-25 00:26:21 +1100347 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000348 iamrecursive ? " -r" : "", pflag ? " -p" : "",
349 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
353 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
354 toremote(targ, argc, argv);
355 else {
Damien Miller95def091999-11-25 00:26:21 +1100356 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357 if (targetshouldbedirectory)
358 verifydir(argv[argc - 1]);
359 }
360 exit(errs != 0);
361}
362
Damien Miller34132e52000-01-14 15:45:46 +1100363char *
364cleanhostname(host)
365 char *host;
366{
367 if (*host == '[' && host[strlen(host) - 1] == ']') {
368 host[strlen(host) - 1] = '\0';
369 return (host + 1);
370 } else
371 return host;
372}
373
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374void
375toremote(targ, argc, argv)
376 char *targ, *argv[];
377 int argc;
378{
379 int i, len;
380 char *bp, *host, *src, *suser, *thost, *tuser;
381
382 *targ++ = 0;
383 if (*targ == 0)
384 targ = ".";
385
386 if ((thost = strchr(argv[argc - 1], '@'))) {
387 /* user@host */
388 *thost++ = 0;
389 tuser = argv[argc - 1];
390 if (*tuser == '\0')
391 tuser = NULL;
392 else if (!okname(tuser))
393 exit(1);
394 } else {
395 thost = argv[argc - 1];
396 tuser = NULL;
397 }
398
399 for (i = 0; i < argc - 1; i++) {
400 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100401 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402 *src++ = 0;
403 if (*src == 0)
404 src = ".";
405 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000406 len = strlen(ssh_program) + strlen(argv[i]) +
407 strlen(src) + (tuser ? strlen(tuser) : 0) +
408 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100409 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410 if (host) {
411 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100412 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413 suser = argv[i];
414 if (*suser == '\0')
415 suser = pwd->pw_name;
416 else if (!okname(suser))
417 continue;
Damien Miller95def091999-11-25 00:26:21 +1100418 (void) sprintf(bp,
Damien Millerad833b32000-08-23 10:46:23 +1000419 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
420 ssh_program, verbose_mode ? " -v" : "",
421 suser, host, cmd, src,
422 tuser ? tuser : "", tuser ? "@" : "",
423 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100424 } else {
425 host = cleanhostname(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100426 (void) sprintf(bp,
Damien Millerad833b32000-08-23 10:46:23 +1000427 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
428 ssh_program, verbose_mode ? " -v" : "",
429 host, cmd, src,
430 tuser ? tuser : "", tuser ? "@" : "",
431 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100432 }
Damien Miller95def091999-11-25 00:26:21 +1100433 if (verbose_mode)
434 fprintf(stderr, "Executing: %s\n", bp);
435 (void) system(bp);
436 (void) xfree(bp);
437 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 if (remin == -1) {
439 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100440 bp = xmalloc(len);
441 (void) sprintf(bp, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100442 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000443 if (do_cmd(host, tuser, bp, &remin,
444 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100445 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 if (response() < 0)
447 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100448 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449 }
Damien Miller95def091999-11-25 00:26:21 +1100450 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451 }
452 }
453}
454
455void
456tolocal(argc, argv)
457 int argc;
458 char *argv[];
459{
460 int i, len;
461 char *bp, *host, *src, *suser;
462
463 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100464 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000466 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 bp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +1100468 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000469 iamrecursive ? " -r" : "", pflag ? " -p" : "",
470 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100471 if (verbose_mode)
472 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 if (system(bp))
474 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100475 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476 continue;
477 }
478 *src++ = 0;
479 if (*src == 0)
480 src = ".";
481 if ((host = strchr(argv[i], '@')) == NULL) {
482 host = argv[i];
483 suser = NULL;
484 } else {
485 *host++ = 0;
486 suser = argv[i];
487 if (*suser == '\0')
488 suser = pwd->pw_name;
489 else if (!okname(suser))
490 continue;
491 }
Damien Miller34132e52000-01-14 15:45:46 +1100492 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100494 bp = xmalloc(len);
495 (void) sprintf(bp, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000496 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100497 (void) xfree(bp);
498 ++errs;
499 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 }
Damien Miller95def091999-11-25 00:26:21 +1100501 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100503 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504 remin = remout = -1;
505 }
506}
507
508void
509source(argc, argv)
510 int argc;
511 char *argv[];
512{
513 struct stat stb;
514 static BUF buffer;
515 BUF *bp;
516 off_t i;
517 int amt, fd, haderr, indx, result;
518 char *last, *name, buf[2048];
519
520 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100521 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522 statbytes = 0;
523 if ((fd = open(name, O_RDONLY, 0)) < 0)
524 goto syserr;
525 if (fstat(fd, &stb) < 0) {
526syserr: run_err("%s: %s", name, strerror(errno));
527 goto next;
528 }
529 switch (stb.st_mode & S_IFMT) {
530 case S_IFREG:
531 break;
532 case S_IFDIR:
533 if (iamrecursive) {
534 rsource(name, &stb);
535 goto next;
536 }
537 /* FALLTHROUGH */
538 default:
539 run_err("%s: not a regular file", name);
540 goto next;
541 }
542 if ((last = strrchr(name, '/')) == NULL)
543 last = name;
544 else
545 ++last;
546 curfile = last;
547 if (pflag) {
548 /*
549 * Make it compatible with possible future
550 * versions expecting microseconds.
551 */
Damien Miller95def091999-11-25 00:26:21 +1100552 (void) sprintf(buf, "T%lu 0 %lu 0\n",
Damien Millerad833b32000-08-23 10:46:23 +1000553 (unsigned long) stb.st_mtime,
554 (unsigned long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000555 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 if (response() < 0)
557 goto next;
558 }
559#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller95def091999-11-25 00:26:21 +1100560 (void) sprintf(buf, "C%04o %lu %s\n",
561 (unsigned int) (stb.st_mode & FILEMODEMASK),
562 (unsigned long) stb.st_size,
563 last);
564 if (verbose_mode) {
565 fprintf(stderr, "Sending file modes: %s", buf);
566 fflush(stderr);
567 }
Damien Miller35dabd02000-05-01 21:10:33 +1000568 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 if (response() < 0)
570 goto next;
571 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100572next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 continue;
574 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575 if (showprogress) {
576 totalbytes = stb.st_size;
577 progressmeter(-1);
578 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 /* Keep writing after an error so that we stay sync'd up. */
580 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
581 amt = bp->cnt;
582 if (i + amt > stb.st_size)
583 amt = stb.st_size - i;
584 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000585 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586 if (result != amt)
587 haderr = result >= 0 ? EIO : errno;
588 }
589 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000590 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 else {
Damien Miller864ea591999-12-15 11:04:25 +1100592 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593 if (result != amt)
594 haderr = result >= 0 ? EIO : errno;
595 statbytes += result;
596 }
597 }
Damien Miller95def091999-11-25 00:26:21 +1100598 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599 progressmeter(1);
600
601 if (close(fd) < 0 && !haderr)
602 haderr = errno;
603 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000604 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605 else
606 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100607 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608 }
609}
610
611void
612rsource(name, statp)
613 char *name;
614 struct stat *statp;
615{
616 DIR *dirp;
617 struct dirent *dp;
618 char *last, *vect[1], path[1100];
619
620 if (!(dirp = opendir(name))) {
621 run_err("%s: %s", name, strerror(errno));
622 return;
623 }
624 last = strrchr(name, '/');
625 if (last == 0)
626 last = name;
627 else
628 last++;
629 if (pflag) {
Damien Miller95def091999-11-25 00:26:21 +1100630 (void) sprintf(path, "T%lu 0 %lu 0\n",
Damien Millerad833b32000-08-23 10:46:23 +1000631 (unsigned long) statp->st_mtime,
632 (unsigned long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000633 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000634 if (response() < 0) {
635 closedir(dirp);
636 return;
637 }
638 }
Damien Miller95def091999-11-25 00:26:21 +1100639 (void) sprintf(path, "D%04o %d %.1024s\n",
Damien Millerad833b32000-08-23 10:46:23 +1000640 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100641 if (verbose_mode)
642 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000643 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644 if (response() < 0) {
645 closedir(dirp);
646 return;
647 }
648 while ((dp = readdir(dirp))) {
649 if (dp->d_ino == 0)
650 continue;
651 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
652 continue;
653 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
654 run_err("%s/%s: name too long", name, dp->d_name);
655 continue;
656 }
Damien Miller95def091999-11-25 00:26:21 +1100657 (void) sprintf(path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658 vect[0] = path;
659 source(1, vect);
660 }
Damien Miller95def091999-11-25 00:26:21 +1100661 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000662 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100663 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664}
665
666void
667sink(argc, argv)
668 int argc;
669 char *argv[];
670{
671 static BUF buffer;
672 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100673 enum {
674 YES, NO, DISPLAYED
675 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000676 BUF *bp;
677 off_t i, j;
678 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100679 off_t size;
680 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100682 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100683 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684
685#define SCREWUP(str) { why = str; goto screwup; }
686
687 setimes = targisdir = 0;
688 mask = umask(0);
689 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100690 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 if (argc != 1) {
692 run_err("ambiguous target");
693 exit(1);
694 }
695 targ = *argv;
696 if (targetshouldbedirectory)
697 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100698
Damien Miller35dabd02000-05-01 21:10:33 +1000699 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
701 targisdir = 1;
702 for (first = 1;; first = 0) {
703 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000704 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705 return;
706 if (*cp++ == '\n')
707 SCREWUP("unexpected <newline>");
708 do {
Damien Millere247cc42000-05-07 12:03:14 +1000709 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 SCREWUP("lost connection");
711 *cp++ = ch;
712 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
713 *cp = 0;
714
715 if (buf[0] == '\01' || buf[0] == '\02') {
716 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000717 (void) atomicio(write, STDERR_FILENO,
Damien Miller95def091999-11-25 00:26:21 +1100718 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719 if (buf[0] == '\02')
720 exit(1);
721 ++errs;
722 continue;
723 }
724 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000725 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726 return;
727 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 if (ch == '\n')
729 *--cp = 0;
730
731#define getnum(t) (t) = 0; \
732 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
733 cp = buf;
734 if (*cp == 'T') {
735 setimes++;
736 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100737 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738 if (*cp++ != ' ')
739 SCREWUP("mtime.sec not delimited");
740 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100741 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742 if (*cp++ != ' ')
743 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100744 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745 if (*cp++ != ' ')
746 SCREWUP("atime.sec not delimited");
747 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100748 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749 if (*cp++ != '\0')
750 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000751 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752 continue;
753 }
754 if (*cp != 'C' && *cp != 'D') {
755 /*
756 * Check for the case "rcp remote:foo\* local:bar".
757 * In this case, the line "No match." can be returned
758 * by the shell before the rcp command on the remote is
759 * executed so the ^Aerror_message convention isn't
760 * followed.
761 */
762 if (first) {
763 run_err("%s", cp);
764 exit(1);
765 }
766 SCREWUP("expected control record");
767 }
768 mode = 0;
769 for (++cp; cp < buf + 5; cp++) {
770 if (*cp < '0' || *cp > '7')
771 SCREWUP("bad mode");
772 mode = (mode << 3) | (*cp - '0');
773 }
774 if (*cp++ != ' ')
775 SCREWUP("mode not delimited");
776
Damien Miller95def091999-11-25 00:26:21 +1100777 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778 size = size * 10 + (*cp++ - '0');
779 if (*cp++ != ' ')
780 SCREWUP("size not delimited");
781 if (targisdir) {
782 static char *namebuf;
783 static int cursize;
784 size_t need;
785
786 need = strlen(targ) + strlen(cp) + 250;
787 if (need > cursize)
Damien Miller95def091999-11-25 00:26:21 +1100788 namebuf = xmalloc(need);
789 (void) sprintf(namebuf, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000790 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791 np = namebuf;
792 } else
793 np = targ;
794 curfile = cp;
795 exists = stat(np, &stb) == 0;
796 if (buf[0] == 'D') {
797 int mod_flag = pflag;
798 if (exists) {
799 if (!S_ISDIR(stb.st_mode)) {
800 errno = ENOTDIR;
801 goto bad;
802 }
803 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100804 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805 } else {
Damien Miller95def091999-11-25 00:26:21 +1100806 /* Handle copying from a read-only
807 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808 mod_flag = 1;
809 if (mkdir(np, mode | S_IRWXU) < 0)
810 goto bad;
811 }
812 vect[0] = np;
813 sink(1, vect);
814 if (setimes) {
815 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100816 if (utimes(np, tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100817 run_err("%s: set times: %s",
818 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 }
820 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100821 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822 continue;
823 }
824 omode = mode;
825 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100826 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827bad: run_err("%s: %s", np, strerror(errno));
828 continue;
829 }
Damien Miller35dabd02000-05-01 21:10:33 +1000830 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100832 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833 continue;
834 }
835 cp = bp->buf;
836 wrerr = NO;
837
838 if (showprogress) {
839 totalbytes = size;
840 progressmeter(-1);
841 }
842 statbytes = 0;
843 for (count = i = 0; i < size; i += 4096) {
844 amt = 4096;
845 if (i + amt > size)
846 amt = size - i;
847 count += amt;
848 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100849 j = read(remin, cp, amt);
850 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
851 continue;
852 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100854 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 exit(1);
856 }
857 amt -= j;
858 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100859 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860 } while (amt > 0);
861 if (count == bp->cnt) {
862 /* Keep reading so we stay sync'd up. */
863 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000864 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865 if (j != count) {
866 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100867 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 }
869 }
870 count = 0;
871 cp = bp->buf;
872 }
873 }
874 if (showprogress)
875 progressmeter(1);
876 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000877 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100879 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 }
881#if 0
882 if (ftruncate(ofd, size)) {
883 run_err("%s: truncate: %s", np, strerror(errno));
884 wrerr = DISPLAYED;
885 }
886#endif
887 if (pflag) {
888 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100889#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100891#else /* HAVE_FCHMOD */
892 if (chmod(np, omode))
893#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100895 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 } else {
897 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100898#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100900#else /* HAVE_FCHMOD */
901 if (chmod(np, omode & ~mask))
902#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100904 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905 }
Damien Millerbe484b52000-07-15 14:14:16 +1000906 if (close(ofd) == -1) {
907 wrerr = YES;
908 wrerrno = errno;
909 }
Damien Miller95def091999-11-25 00:26:21 +1100910 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911 if (setimes && wrerr == NO) {
912 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100913 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100915 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916 wrerr = DISPLAYED;
917 }
918 }
Damien Miller95def091999-11-25 00:26:21 +1100919 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920 case YES:
921 run_err("%s: %s", np, strerror(wrerrno));
922 break;
923 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000924 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925 break;
926 case DISPLAYED:
927 break;
928 }
929 }
930screwup:
931 run_err("protocol error: %s", why);
932 exit(1);
933}
934
935int
936response()
937{
938 char ch, *cp, resp, rbuf[2048];
939
Damien Millere247cc42000-05-07 12:03:14 +1000940 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941 lostconn(0);
942
943 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100944 switch (resp) {
945 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946 return (0);
947 default:
948 *cp++ = resp;
949 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100950 case 1: /* error, followed by error msg */
951 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952 do {
Damien Millere247cc42000-05-07 12:03:14 +1000953 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954 lostconn(0);
955 *cp++ = ch;
956 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
957
958 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000959 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960 ++errs;
961 if (resp == 1)
962 return (-1);
963 exit(1);
964 }
965 /* NOTREACHED */
966}
967
968void
969usage()
970{
Damien Millerad833b32000-08-23 10:46:23 +1000971 (void) fprintf(stderr, "usage: scp "
972 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
973 " scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974 exit(1);
975}
976
977void
Damien Miller95def091999-11-25 00:26:21 +1100978run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979{
980 static FILE *fp;
981 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
983 ++errs;
984 if (fp == NULL && !(fp = fdopen(remout, "w")))
985 return;
Damien Miller95def091999-11-25 00:26:21 +1100986 (void) fprintf(fp, "%c", 0x01);
987 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100988 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100989 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100990 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100991 (void) fprintf(fp, "\n");
992 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993
Damien Miller95def091999-11-25 00:26:21 +1100994 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100995 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100996 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100997 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100998 fprintf(stderr, "\n");
999 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000}
1001
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002char *
1003colon(cp)
1004 char *cp;
1005{
Damien Miller34132e52000-01-14 15:45:46 +11001006 int flag = 0;
1007
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008 if (*cp == ':') /* Leading colon is part of file name. */
1009 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001010 if (*cp == '[')
1011 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
1013 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001014 if (*cp == '@' && *(cp+1) == '[')
1015 flag = 1;
1016 if (*cp == ']' && *(cp+1) == ':' && flag)
1017 return (cp+1);
1018 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019 return (cp);
1020 if (*cp == '/')
1021 return (0);
1022 }
1023 return (0);
1024}
1025
1026void
1027verifydir(cp)
1028 char *cp;
1029{
1030 struct stat stb;
1031
1032 if (!stat(cp, &stb)) {
1033 if (S_ISDIR(stb.st_mode))
1034 return;
1035 errno = ENOTDIR;
1036 }
1037 run_err("%s: %s", cp, strerror(errno));
1038 exit(1);
1039}
1040
1041int
1042okname(cp0)
1043 char *cp0;
1044{
1045 int c;
1046 char *cp;
1047
1048 cp = cp0;
1049 do {
1050 c = *cp;
1051 if (c & 0200)
1052 goto bad;
1053 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1054 goto bad;
1055 } while (*++cp);
1056 return (1);
1057
Damien Miller98c7ad62000-03-09 21:27:49 +11001058bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059 return (0);
1060}
1061
1062BUF *
1063allocbuf(bp, fd, blksize)
1064 BUF *bp;
1065 int fd, blksize;
1066{
1067 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001068#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069 struct stat stb;
1070
1071 if (fstat(fd, &stb) < 0) {
1072 run_err("fstat: %s", strerror(errno));
1073 return (0);
1074 }
Damien Miller95def091999-11-25 00:26:21 +11001075 if (stb.st_blksize == 0)
1076 size = blksize;
1077 else
1078 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001079 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001080#else /* HAVE_ST_BLKSIZE */
1081 size = blksize;
1082#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083 if (bp->cnt >= size)
1084 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001085 if (bp->buf == NULL)
1086 bp->buf = xmalloc(size);
1087 else
1088 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089 bp->cnt = size;
1090 return (bp);
1091}
1092
1093void
1094lostconn(signo)
1095 int signo;
1096{
1097 if (!iamremote)
1098 fprintf(stderr, "lost connection\n");
1099 exit(1);
1100}
1101
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102
1103void
1104alarmtimer(int wait)
1105{
Damien Miller95def091999-11-25 00:26:21 +11001106 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107
Damien Miller95def091999-11-25 00:26:21 +11001108 itv.it_value.tv_sec = wait;
1109 itv.it_value.tv_usec = 0;
1110 itv.it_interval = itv.it_value;
1111 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112}
1113
1114void
Damien Miller7684ee12000-03-17 23:40:15 +11001115updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116{
1117 int save_errno = errno;
1118
1119 progressmeter(0);
1120 errno = save_errno;
1121}
1122
Damien Miller81428f91999-11-18 09:28:11 +11001123int
1124foregroundproc()
1125{
1126 static pid_t pgrp = -1;
1127 int ctty_pgrp;
1128
1129 if (pgrp == -1)
1130 pgrp = getpgrp();
1131
Damien Millerbac2d8a2000-09-05 16:13:06 +11001132#ifdef HAVE_CYGWIN
1133 /*
1134 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1135 * currently.
1136 */
1137 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1138 ctty_pgrp == pgrp);
1139#else
Damien Miller95def091999-11-25 00:26:21 +11001140 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1141 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001142#endif
Damien Miller81428f91999-11-18 09:28:11 +11001143}
1144
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145void
1146progressmeter(int flag)
1147{
1148 static const char prefixes[] = " KMGTP";
1149 static struct timeval lastupdate;
1150 static off_t lastsize;
1151 struct timeval now, td, wait;
1152 off_t cursize, abbrevsize;
1153 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001154 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 char buf[256];
1156
1157 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001158 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159 lastupdate = start;
1160 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001161 }
Damien Miller81428f91999-11-18 09:28:11 +11001162 if (foregroundproc() == 0)
1163 return;
1164
Damien Miller95def091999-11-25 00:26:21 +11001165 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001167 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001168 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169 ratio = MAX(ratio, 0);
1170 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001171 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172 ratio = 100;
1173
Damien Miller95def091999-11-25 00:26:21 +11001174 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175
1176 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001177 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178 if (barlength > 0) {
1179 i = barlength * ratio / 100;
1180 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001181 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 i = 0;
1184 abbrevsize = cursize;
1185 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1186 i++;
1187 abbrevsize >>= 10;
1188 }
Damien Miller864ea591999-12-15 11:04:25 +11001189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1190 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
Damien Miller95def091999-11-25 00:26:21 +11001191 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192
1193 timersub(&now, &lastupdate, &wait);
1194 if (cursize > lastsize) {
1195 lastupdate = now;
1196 lastsize = cursize;
1197 if (wait.tv_sec >= STALLTIME) {
1198 start.tv_sec += wait.tv_sec;
1199 start.tv_usec += wait.tv_usec;
1200 }
1201 wait.tv_sec = 0;
1202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 timersub(&now, &start, &td);
1204 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1205
1206 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1207 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001208 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209 } else if (wait.tv_sec >= STALLTIME) {
1210 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001211 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001213 if (flag != 1)
1214 remaining =
1215 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1216 else
1217 remaining = elapsed;
1218
Damien Miller01ab4a21999-10-28 15:23:30 +10001219 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220 if (i)
1221 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001222 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223 else
1224 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001225 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 i = remaining % 3600;
1227 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001228 "%02d:%02d%s", i / 60, i % 60,
1229 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230 }
1231 atomicio(write, fileno(stdout), buf, strlen(buf));
1232
1233 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001234 struct sigaction sa;
1235 sa.sa_handler = updateprogressmeter;
Damien Miller77aba9d2000-08-30 10:11:30 +11001236 sigemptyset((sigset_t *)&sa.sa_mask);
Damien Miller615f9392000-05-17 22:53:33 +10001237#ifdef SA_RESTART
Damien Miller864ea591999-12-15 11:04:25 +11001238 sa.sa_flags = SA_RESTART;
Damien Miller615f9392000-05-17 22:53:33 +10001239#endif
Damien Miller864ea591999-12-15 11:04:25 +11001240 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 alarmtimer(1);
1242 } else if (flag == 1) {
1243 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001244 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245 statbytes = 0;
1246 }
1247}
1248
1249int
1250getttywidth(void)
1251{
1252 struct winsize winsize;
1253
1254 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001255 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256 else
Damien Miller95def091999-11-25 00:26:21 +11001257 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258}
Damien Miller874d77b2000-10-14 16:23:11 +11001259
1260void
1261addargs(char *fmt, ...)
1262{
1263 va_list ap;
1264 char buf[1024];
1265
1266 va_start(ap, fmt);
1267 vsnprintf(buf, sizeof(buf), fmt, ap);
1268 va_end(ap);
1269
1270 if (args.list == NULL) {
1271 args.nalloc = 32;
1272 args.num = 0;
1273 args.list = xmalloc(args.nalloc * sizeof(char *));
1274 } else if (args.num+2 >= args.nalloc) {
1275 args.nalloc *= 2;
1276 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1277 }
1278 args.list[args.num++] = xstrdup(buf);
1279 args.list[args.num] = NULL;
1280}