blob: 14d8a8af85c4bb22ef0d988b70684f57e2f80c1d [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"
Ben Lindstromf6b7b092001-02-09 01:23:39 +000078RCSID("$OpenBSD: scp.c,v 1.55 2001/02/08 14:38:54 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "atomicio.h"
82#include "pathnames.h"
83#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Ben Lindstrom49a79c02000-11-17 03:47:20 +000085#ifdef HAVE___PROGNAME
86extern char *__progname;
87#else
88char *__progname;
89#endif
90
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091/* For progressmeter() -- number of seconds before xfer considered "stalled" */
92#define STALLTIME 5
93
Damien Miller484118e2000-07-02 19:13:56 +100094/* Progress meter bar */
95#define BAR \
96 "************************************************************"\
97 "************************************************************"\
98 "************************************************************"\
99 "************************************************************"
100#define MAX_BARLENGTH (sizeof(BAR) - 1)
101
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102/* Visual statistics about files as they are transferred. */
103void progressmeter(int);
104
105/* Returns width of the terminal (for progress meter calculations). */
106int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000107int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller874d77b2000-10-14 16:23:11 +1100109/* setup arguments for the call to ssh */
110void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
111
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112/* Time a transfer started. */
113static struct timeval start;
114
115/* Number of bytes of current file transferred so far. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000116volatile u_long statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
118/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100119off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121/* Name of current file being transferred. */
122char *curfile;
123
124/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100125int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127/* This is set to zero if the progressmeter is not desired. */
128int showprogress = 1;
129
Damien Millerad833b32000-08-23 10:46:23 +1000130/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000131char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000132
Damien Miller874d77b2000-10-14 16:23:11 +1100133/* This is the list of arguments that scp passes to ssh */
134struct {
135 char **list;
136 int num;
137 int nalloc;
138} args;
139
Damien Miller5428f641999-11-25 11:54:57 +1100140/*
141 * This function executes the given command as the specified user on the
142 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
143 * assigns the input and output file descriptors on success.
144 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Miller4af51302000-04-16 11:18:38 +1000146int
Damien Millerad833b32000-08-23 10:46:23 +1000147do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148{
Damien Miller95def091999-11-25 00:26:21 +1100149 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100152 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
153 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller5428f641999-11-25 11:54:57 +1100155 /*
156 * Reserve two descriptors so that the real pipes won't get
157 * descriptors 0 and 1 because that will screw up dup2 below.
158 */
Damien Miller95def091999-11-25 00:26:21 +1100159 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* Create a socket pair for communicating with ssh. */
162 if (pipe(pin) < 0)
163 fatal("pipe: %s", strerror(errno));
164 if (pipe(pout) < 0)
165 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Free the reserved descriptors. */
168 close(reserved[0]);
169 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Damien Miller95def091999-11-25 00:26:21 +1100171 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100172 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100173 /* Child. */
174 close(pin[1]);
175 close(pout[0]);
176 dup2(pin[0], 0);
177 dup2(pout[1], 1);
178 close(pin[0]);
179 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Damien Miller874d77b2000-10-14 16:23:11 +1100181 args.list[0] = ssh_program;
182 if (remuser != NULL)
Damien Millere4041c92000-10-14 17:45:58 +1100183 addargs("-l%s", remuser);
Damien Miller874d77b2000-10-14 16:23:11 +1100184 addargs("%s", host);
185 addargs("%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100186
Damien Miller874d77b2000-10-14 16:23:11 +1100187 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000188 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100189 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190 }
Damien Miller95def091999-11-25 00:26:21 +1100191 /* Parent. Close the other side, and return the local side. */
192 close(pin[0]);
193 *fdout = pin[1];
194 close(pout[1]);
195 *fdin = pout[0];
196 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197}
198
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199typedef struct {
200 int cnt;
201 char *buf;
202} BUF;
203
204extern int iamremote;
205
Damien Miller95def091999-11-25 00:26:21 +1100206BUF *allocbuf(BUF *, int, int);
207char *colon(char *);
208void lostconn(int);
209void nospace(void);
210int okname(char *);
211void run_err(const char *,...);
212void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100215uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216int errs, remin, remout;
217int pflag, iamremote, iamrecursive, targetshouldbedirectory;
218
219#define CMDNEEDS 64
220char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
221
Damien Miller95def091999-11-25 00:26:21 +1100222int response(void);
223void rsource(char *, struct stat *);
224void sink(int, char *[]);
225void source(int, char *[]);
226void tolocal(int, char *[]);
227void toremote(char *, int, char *[]);
228void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
230int
231main(argc, argv)
232 int argc;
233 char *argv[];
234{
235 int ch, fflag, tflag;
236 char *targ;
237 extern char *optarg;
238 extern int optind;
239
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000240 __progname = get_progname(argv[0]);
241
Damien Miller874d77b2000-10-14 16:23:11 +1100242 args.list = NULL;
243 addargs("ssh"); /* overwritten with ssh_program */
244 addargs("-x");
245 addargs("-oFallBackToRsh no");
246
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000248 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100249 switch (ch) {
250 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100251 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100252 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100253 case 'C':
254 addargs("-%c", ch);
255 break;
256 case 'o':
257 case 'c':
258 case 'i':
Damien Miller50a41ed2000-10-16 12:14:42 +1100259 addargs("-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100260 break;
261 case 'P':
Damien Miller50a41ed2000-10-16 12:14:42 +1100262 addargs("-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100263 break;
264 case 'B':
Damien Miller50a41ed2000-10-16 12:14:42 +1100265 addargs("-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100266 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267 case 'p':
268 pflag = 1;
269 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270 case 'r':
271 iamrecursive = 1;
272 break;
Damien Millerad833b32000-08-23 10:46:23 +1000273 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100274 ssh_program = xstrdup(optarg);
275 break;
276 case 'v':
277 verbose_mode = 1;
278 break;
279 case 'q':
280 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000281 break;
282
Damien Miller95def091999-11-25 00:26:21 +1100283 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284 case 'd':
285 targetshouldbedirectory = 1;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 iamremote = 1;
289 fflag = 1;
290 break;
Damien Miller95def091999-11-25 00:26:21 +1100291 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292 iamremote = 1;
293 tflag = 1;
294 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295 case '?':
296 default:
297 usage();
298 }
299 argc -= optind;
300 argv += optind;
301
302 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100303 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304
Damien Miller95def091999-11-25 00:26:21 +1100305 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306 showprogress = 0;
307
308 remin = STDIN_FILENO;
309 remout = STDOUT_FILENO;
310
Kevin Stevesef4eea92001-02-05 12:42:17 +0000311 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100312 /* Follow "protocol", send data. */
313 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314 source(argc, argv);
315 exit(errs != 0);
316 }
Damien Miller95def091999-11-25 00:26:21 +1100317 if (tflag) {
318 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319 sink(argc, argv);
320 exit(errs != 0);
321 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322 if (argc < 2)
323 usage();
324 if (argc > 2)
325 targetshouldbedirectory = 1;
326
327 remin = remout = -1;
328 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000329 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
330 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000331 iamrecursive ? " -r" : "", pflag ? " -p" : "",
332 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333
Damien Miller95def091999-11-25 00:26:21 +1100334 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
336 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
337 toremote(targ, argc, argv);
338 else {
Damien Miller95def091999-11-25 00:26:21 +1100339 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340 if (targetshouldbedirectory)
341 verifydir(argv[argc - 1]);
342 }
343 exit(errs != 0);
344}
345
Damien Miller34132e52000-01-14 15:45:46 +1100346char *
347cleanhostname(host)
348 char *host;
349{
350 if (*host == '[' && host[strlen(host) - 1] == ']') {
351 host[strlen(host) - 1] = '\0';
352 return (host + 1);
353 } else
354 return host;
355}
356
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357void
358toremote(targ, argc, argv)
359 char *targ, *argv[];
360 int argc;
361{
362 int i, len;
363 char *bp, *host, *src, *suser, *thost, *tuser;
364
365 *targ++ = 0;
366 if (*targ == 0)
367 targ = ".";
368
369 if ((thost = strchr(argv[argc - 1], '@'))) {
370 /* user@host */
371 *thost++ = 0;
372 tuser = argv[argc - 1];
373 if (*tuser == '\0')
374 tuser = NULL;
375 else if (!okname(tuser))
376 exit(1);
377 } else {
378 thost = argv[argc - 1];
379 tuser = NULL;
380 }
381
382 for (i = 0; i < argc - 1; i++) {
383 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100384 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385 *src++ = 0;
386 if (*src == 0)
387 src = ".";
388 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000389 len = strlen(ssh_program) + strlen(argv[i]) +
390 strlen(src) + (tuser ? strlen(tuser) : 0) +
391 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100392 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393 if (host) {
394 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100395 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396 suser = argv[i];
397 if (*suser == '\0')
398 suser = pwd->pw_name;
399 else if (!okname(suser))
400 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000401 snprintf(bp, len,
402 "%s%s -x -o'FallBackToRsh no' -n "
403 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000404 ssh_program, verbose_mode ? " -v" : "",
405 suser, host, cmd, src,
406 tuser ? tuser : "", tuser ? "@" : "",
407 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100408 } else {
409 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000410 snprintf(bp, len,
411 "exec %s%s -x -o'FallBackToRsh no' -n %s "
412 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000413 ssh_program, verbose_mode ? " -v" : "",
414 host, cmd, src,
415 tuser ? tuser : "", tuser ? "@" : "",
416 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100417 }
Damien Miller95def091999-11-25 00:26:21 +1100418 if (verbose_mode)
419 fprintf(stderr, "Executing: %s\n", bp);
420 (void) system(bp);
421 (void) xfree(bp);
422 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423 if (remin == -1) {
424 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100425 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000426 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100427 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000428 if (do_cmd(host, tuser, bp, &remin,
429 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100430 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431 if (response() < 0)
432 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100433 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 }
Damien Miller95def091999-11-25 00:26:21 +1100435 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 }
437 }
438}
439
440void
441tolocal(argc, argv)
442 int argc;
443 char *argv[];
444{
445 int i, len;
446 char *bp, *host, *src, *suser;
447
448 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100449 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000451 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000453 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000454 iamrecursive ? " -r" : "", pflag ? " -p" : "",
455 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100456 if (verbose_mode)
457 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000458 if (system(bp))
459 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100460 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461 continue;
462 }
463 *src++ = 0;
464 if (*src == 0)
465 src = ".";
466 if ((host = strchr(argv[i], '@')) == NULL) {
467 host = argv[i];
468 suser = NULL;
469 } else {
470 *host++ = 0;
471 suser = argv[i];
472 if (*suser == '\0')
473 suser = pwd->pw_name;
474 else if (!okname(suser))
475 continue;
476 }
Damien Miller34132e52000-01-14 15:45:46 +1100477 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100479 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000480 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000481 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100482 (void) xfree(bp);
483 ++errs;
484 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 }
Damien Miller95def091999-11-25 00:26:21 +1100486 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100488 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 remin = remout = -1;
490 }
491}
492
493void
494source(argc, argv)
495 int argc;
496 char *argv[];
497{
498 struct stat stb;
499 static BUF buffer;
500 BUF *bp;
501 off_t i;
502 int amt, fd, haderr, indx, result;
503 char *last, *name, buf[2048];
504
505 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100506 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507 statbytes = 0;
508 if ((fd = open(name, O_RDONLY, 0)) < 0)
509 goto syserr;
510 if (fstat(fd, &stb) < 0) {
511syserr: run_err("%s: %s", name, strerror(errno));
512 goto next;
513 }
514 switch (stb.st_mode & S_IFMT) {
515 case S_IFREG:
516 break;
517 case S_IFDIR:
518 if (iamrecursive) {
519 rsource(name, &stb);
520 goto next;
521 }
522 /* FALLTHROUGH */
523 default:
524 run_err("%s: not a regular file", name);
525 goto next;
526 }
527 if ((last = strrchr(name, '/')) == NULL)
528 last = name;
529 else
530 ++last;
531 curfile = last;
532 if (pflag) {
533 /*
534 * Make it compatible with possible future
535 * versions expecting microseconds.
536 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000537 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000538 (u_long) stb.st_mtime,
539 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000540 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541 if (response() < 0)
542 goto next;
543 }
544#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000545 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000546 (u_int) (stb.st_mode & FILEMODEMASK),
547 (u_long) stb.st_size, last);
Damien Miller95def091999-11-25 00:26:21 +1100548 if (verbose_mode) {
549 fprintf(stderr, "Sending file modes: %s", buf);
550 fflush(stderr);
551 }
Damien Miller35dabd02000-05-01 21:10:33 +1000552 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553 if (response() < 0)
554 goto next;
555 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100556next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 continue;
558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 if (showprogress) {
560 totalbytes = stb.st_size;
561 progressmeter(-1);
562 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563 /* Keep writing after an error so that we stay sync'd up. */
564 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
565 amt = bp->cnt;
566 if (i + amt > stb.st_size)
567 amt = stb.st_size - i;
568 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000569 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 if (result != amt)
571 haderr = result >= 0 ? EIO : errno;
572 }
573 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000574 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575 else {
Damien Miller864ea591999-12-15 11:04:25 +1100576 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 if (result != amt)
578 haderr = result >= 0 ? EIO : errno;
579 statbytes += result;
580 }
581 }
Damien Miller95def091999-11-25 00:26:21 +1100582 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 progressmeter(1);
584
585 if (close(fd) < 0 && !haderr)
586 haderr = errno;
587 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000588 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 else
590 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100591 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592 }
593}
594
595void
596rsource(name, statp)
597 char *name;
598 struct stat *statp;
599{
600 DIR *dirp;
601 struct dirent *dp;
602 char *last, *vect[1], path[1100];
603
604 if (!(dirp = opendir(name))) {
605 run_err("%s: %s", name, strerror(errno));
606 return;
607 }
608 last = strrchr(name, '/');
609 if (last == 0)
610 last = name;
611 else
612 last++;
613 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000614 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000615 (u_long) statp->st_mtime,
616 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000617 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618 if (response() < 0) {
619 closedir(dirp);
620 return;
621 }
622 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000623 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000624 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100625 if (verbose_mode)
626 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000627 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000628 if (response() < 0) {
629 closedir(dirp);
630 return;
631 }
632 while ((dp = readdir(dirp))) {
633 if (dp->d_ino == 0)
634 continue;
635 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
636 continue;
637 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
638 run_err("%s/%s: name too long", name, dp->d_name);
639 continue;
640 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000641 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642 vect[0] = path;
643 source(1, vect);
644 }
Damien Miller95def091999-11-25 00:26:21 +1100645 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000646 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100647 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000648}
649
650void
651sink(argc, argv)
652 int argc;
653 char *argv[];
654{
655 static BUF buffer;
656 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100657 enum {
658 YES, NO, DISPLAYED
659 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660 BUF *bp;
661 off_t i, j;
662 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100663 off_t size;
664 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100666 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100667 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668
669#define SCREWUP(str) { why = str; goto screwup; }
670
671 setimes = targisdir = 0;
672 mask = umask(0);
673 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100674 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675 if (argc != 1) {
676 run_err("ambiguous target");
677 exit(1);
678 }
679 targ = *argv;
680 if (targetshouldbedirectory)
681 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100682
Damien Miller35dabd02000-05-01 21:10:33 +1000683 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
685 targisdir = 1;
686 for (first = 1;; first = 0) {
687 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000688 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689 return;
690 if (*cp++ == '\n')
691 SCREWUP("unexpected <newline>");
692 do {
Damien Millere247cc42000-05-07 12:03:14 +1000693 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694 SCREWUP("lost connection");
695 *cp++ = ch;
696 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
697 *cp = 0;
698
699 if (buf[0] == '\01' || buf[0] == '\02') {
700 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000701 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000702 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703 if (buf[0] == '\02')
704 exit(1);
705 ++errs;
706 continue;
707 }
708 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000709 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 return;
711 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712 if (ch == '\n')
713 *--cp = 0;
714
715#define getnum(t) (t) = 0; \
716 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
717 cp = buf;
718 if (*cp == 'T') {
719 setimes++;
720 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100721 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722 if (*cp++ != ' ')
723 SCREWUP("mtime.sec not delimited");
724 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100725 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726 if (*cp++ != ' ')
727 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100728 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729 if (*cp++ != ' ')
730 SCREWUP("atime.sec not delimited");
731 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100732 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 if (*cp++ != '\0')
734 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000735 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736 continue;
737 }
738 if (*cp != 'C' && *cp != 'D') {
739 /*
740 * Check for the case "rcp remote:foo\* local:bar".
741 * In this case, the line "No match." can be returned
742 * by the shell before the rcp command on the remote is
743 * executed so the ^Aerror_message convention isn't
744 * followed.
745 */
746 if (first) {
747 run_err("%s", cp);
748 exit(1);
749 }
750 SCREWUP("expected control record");
751 }
752 mode = 0;
753 for (++cp; cp < buf + 5; cp++) {
754 if (*cp < '0' || *cp > '7')
755 SCREWUP("bad mode");
756 mode = (mode << 3) | (*cp - '0');
757 }
758 if (*cp++ != ' ')
759 SCREWUP("mode not delimited");
760
Damien Miller95def091999-11-25 00:26:21 +1100761 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762 size = size * 10 + (*cp++ - '0');
763 if (*cp++ != ' ')
764 SCREWUP("size not delimited");
765 if (targisdir) {
766 static char *namebuf;
767 static int cursize;
768 size_t need;
769
770 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000771 if (need > cursize) {
772 if (namebuf)
773 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100774 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000775 cursize = need;
776 }
777 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000778 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779 np = namebuf;
780 } else
781 np = targ;
782 curfile = cp;
783 exists = stat(np, &stb) == 0;
784 if (buf[0] == 'D') {
785 int mod_flag = pflag;
786 if (exists) {
787 if (!S_ISDIR(stb.st_mode)) {
788 errno = ENOTDIR;
789 goto bad;
790 }
791 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100792 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793 } else {
Damien Miller95def091999-11-25 00:26:21 +1100794 /* Handle copying from a read-only
795 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796 mod_flag = 1;
797 if (mkdir(np, mode | S_IRWXU) < 0)
798 goto bad;
799 }
800 vect[0] = np;
801 sink(1, vect);
802 if (setimes) {
803 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100804 if (utimes(np, tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100805 run_err("%s: set times: %s",
806 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 }
808 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100809 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810 continue;
811 }
812 omode = mode;
813 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100814 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815bad: run_err("%s: %s", np, strerror(errno));
816 continue;
817 }
Damien Miller35dabd02000-05-01 21:10:33 +1000818 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100820 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821 continue;
822 }
823 cp = bp->buf;
824 wrerr = NO;
825
826 if (showprogress) {
827 totalbytes = size;
828 progressmeter(-1);
829 }
830 statbytes = 0;
831 for (count = i = 0; i < size; i += 4096) {
832 amt = 4096;
833 if (i + amt > size)
834 amt = size - i;
835 count += amt;
836 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100837 j = read(remin, cp, amt);
838 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
839 continue;
840 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100842 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843 exit(1);
844 }
845 amt -= j;
846 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100847 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 } while (amt > 0);
849 if (count == bp->cnt) {
850 /* Keep reading so we stay sync'd up. */
851 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000852 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 if (j != count) {
854 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100855 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856 }
857 }
858 count = 0;
859 cp = bp->buf;
860 }
861 }
862 if (showprogress)
863 progressmeter(1);
864 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000865 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100867 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 }
869#if 0
870 if (ftruncate(ofd, size)) {
871 run_err("%s: truncate: %s", np, strerror(errno));
872 wrerr = DISPLAYED;
873 }
874#endif
875 if (pflag) {
876 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100877#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100879#else /* HAVE_FCHMOD */
880 if (chmod(np, omode))
881#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100883 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 } else {
885 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100886#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100888#else /* HAVE_FCHMOD */
889 if (chmod(np, omode & ~mask))
890#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100892 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 }
Damien Millerbe484b52000-07-15 14:14:16 +1000894 if (close(ofd) == -1) {
895 wrerr = YES;
896 wrerrno = errno;
897 }
Damien Miller95def091999-11-25 00:26:21 +1100898 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899 if (setimes && wrerr == NO) {
900 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100901 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100903 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 wrerr = DISPLAYED;
905 }
906 }
Damien Miller95def091999-11-25 00:26:21 +1100907 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908 case YES:
909 run_err("%s: %s", np, strerror(wrerrno));
910 break;
911 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000912 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 break;
914 case DISPLAYED:
915 break;
916 }
917 }
918screwup:
919 run_err("protocol error: %s", why);
920 exit(1);
921}
922
923int
924response()
925{
926 char ch, *cp, resp, rbuf[2048];
927
Damien Millere247cc42000-05-07 12:03:14 +1000928 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929 lostconn(0);
930
931 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100932 switch (resp) {
933 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934 return (0);
935 default:
936 *cp++ = resp;
937 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100938 case 1: /* error, followed by error msg */
939 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940 do {
Damien Millere247cc42000-05-07 12:03:14 +1000941 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 lostconn(0);
943 *cp++ = ch;
944 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
945
946 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000947 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948 ++errs;
949 if (resp == 1)
950 return (-1);
951 exit(1);
952 }
953 /* NOTREACHED */
954}
955
956void
957usage()
958{
Damien Millerad833b32000-08-23 10:46:23 +1000959 (void) fprintf(stderr, "usage: scp "
960 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
961 " scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962 exit(1);
963}
964
965void
Damien Miller95def091999-11-25 00:26:21 +1100966run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967{
968 static FILE *fp;
969 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
971 ++errs;
972 if (fp == NULL && !(fp = fdopen(remout, "w")))
973 return;
Damien Miller95def091999-11-25 00:26:21 +1100974 (void) fprintf(fp, "%c", 0x01);
975 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100976 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100977 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100978 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100979 (void) fprintf(fp, "\n");
980 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100983 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100984 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100985 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100986 fprintf(stderr, "\n");
987 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988}
989
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990char *
991colon(cp)
992 char *cp;
993{
Damien Miller34132e52000-01-14 15:45:46 +1100994 int flag = 0;
995
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996 if (*cp == ':') /* Leading colon is part of file name. */
997 return (0);
Damien Miller34132e52000-01-14 15:45:46 +1100998 if (*cp == '[')
999 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
1001 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001002 if (*cp == '@' && *(cp+1) == '[')
1003 flag = 1;
1004 if (*cp == ']' && *(cp+1) == ':' && flag)
1005 return (cp+1);
1006 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007 return (cp);
1008 if (*cp == '/')
1009 return (0);
1010 }
1011 return (0);
1012}
1013
1014void
1015verifydir(cp)
1016 char *cp;
1017{
1018 struct stat stb;
1019
1020 if (!stat(cp, &stb)) {
1021 if (S_ISDIR(stb.st_mode))
1022 return;
1023 errno = ENOTDIR;
1024 }
1025 run_err("%s: %s", cp, strerror(errno));
1026 exit(1);
1027}
1028
1029int
1030okname(cp0)
1031 char *cp0;
1032{
1033 int c;
1034 char *cp;
1035
1036 cp = cp0;
1037 do {
1038 c = *cp;
1039 if (c & 0200)
1040 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001041 if (!isalpha(c) && !isdigit(c) &&
1042 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043 goto bad;
1044 } while (*++cp);
1045 return (1);
1046
Damien Miller98c7ad62000-03-09 21:27:49 +11001047bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048 return (0);
1049}
1050
1051BUF *
1052allocbuf(bp, fd, blksize)
1053 BUF *bp;
1054 int fd, blksize;
1055{
1056 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001057#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058 struct stat stb;
1059
1060 if (fstat(fd, &stb) < 0) {
1061 run_err("fstat: %s", strerror(errno));
1062 return (0);
1063 }
Damien Miller95def091999-11-25 00:26:21 +11001064 if (stb.st_blksize == 0)
1065 size = blksize;
1066 else
1067 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001068 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001069#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001070 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001071#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072 if (bp->cnt >= size)
1073 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001074 if (bp->buf == NULL)
1075 bp->buf = xmalloc(size);
1076 else
1077 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001078 bp->cnt = size;
1079 return (bp);
1080}
1081
1082void
1083lostconn(signo)
1084 int signo;
1085{
1086 if (!iamremote)
1087 fprintf(stderr, "lost connection\n");
1088 exit(1);
1089}
1090
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091
1092void
1093alarmtimer(int wait)
1094{
Damien Miller95def091999-11-25 00:26:21 +11001095 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096
Damien Miller95def091999-11-25 00:26:21 +11001097 itv.it_value.tv_sec = wait;
1098 itv.it_value.tv_usec = 0;
1099 itv.it_interval = itv.it_value;
1100 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101}
1102
1103void
Damien Miller7684ee12000-03-17 23:40:15 +11001104updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105{
1106 int save_errno = errno;
1107
1108 progressmeter(0);
1109 errno = save_errno;
1110}
1111
Damien Miller81428f91999-11-18 09:28:11 +11001112int
1113foregroundproc()
1114{
1115 static pid_t pgrp = -1;
1116 int ctty_pgrp;
1117
1118 if (pgrp == -1)
1119 pgrp = getpgrp();
1120
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001121#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001122 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1123 ctty_pgrp == pgrp);
1124#else
Damien Miller95def091999-11-25 00:26:21 +11001125 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1126 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001127#endif
Damien Miller81428f91999-11-18 09:28:11 +11001128}
1129
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130void
1131progressmeter(int flag)
1132{
1133 static const char prefixes[] = " KMGTP";
1134 static struct timeval lastupdate;
1135 static off_t lastsize;
1136 struct timeval now, td, wait;
1137 off_t cursize, abbrevsize;
1138 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001139 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140 char buf[256];
1141
1142 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001143 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144 lastupdate = start;
1145 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001146 }
Damien Miller81428f91999-11-18 09:28:11 +11001147 if (foregroundproc() == 0)
1148 return;
1149
Damien Miller95def091999-11-25 00:26:21 +11001150 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001152 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001153 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154 ratio = MAX(ratio, 0);
1155 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001156 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 ratio = 100;
1158
Damien Miller95def091999-11-25 00:26:21 +11001159 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160
1161 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001162 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163 if (barlength > 0) {
1164 i = barlength * ratio / 100;
1165 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001166 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 i = 0;
1169 abbrevsize = cursize;
1170 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1171 i++;
1172 abbrevsize >>= 10;
1173 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001174 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1175 (unsigned long) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176
1177 timersub(&now, &lastupdate, &wait);
1178 if (cursize > lastsize) {
1179 lastupdate = now;
1180 lastsize = cursize;
1181 if (wait.tv_sec >= STALLTIME) {
1182 start.tv_sec += wait.tv_sec;
1183 start.tv_usec += wait.tv_usec;
1184 }
1185 wait.tv_sec = 0;
1186 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187 timersub(&now, &start, &td);
1188 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1189
Kevin Steves7d00ba42000-12-15 23:03:10 +00001190 if (flag != 1 &&
1191 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001193 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194 } else if (wait.tv_sec >= STALLTIME) {
1195 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001196 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001198 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001199 remaining = (int)(totalbytes / (statbytes / elapsed) -
1200 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001201 else
1202 remaining = elapsed;
1203
Damien Miller01ab4a21999-10-28 15:23:30 +10001204 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205 if (i)
1206 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001207 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001208 else
1209 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001210 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 i = remaining % 3600;
1212 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001213 "%02d:%02d%s", i / 60, i % 60,
1214 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215 }
1216 atomicio(write, fileno(stdout), buf, strlen(buf));
1217
1218 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001219 struct sigaction sa;
1220 sa.sa_handler = updateprogressmeter;
Damien Miller77aba9d2000-08-30 10:11:30 +11001221 sigemptyset((sigset_t *)&sa.sa_mask);
Damien Miller615f9392000-05-17 22:53:33 +10001222#ifdef SA_RESTART
Damien Miller864ea591999-12-15 11:04:25 +11001223 sa.sa_flags = SA_RESTART;
Damien Miller615f9392000-05-17 22:53:33 +10001224#endif
Damien Miller864ea591999-12-15 11:04:25 +11001225 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 alarmtimer(1);
1227 } else if (flag == 1) {
1228 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001229 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230 statbytes = 0;
1231 }
1232}
1233
1234int
1235getttywidth(void)
1236{
1237 struct winsize winsize;
1238
1239 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001240 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 else
Damien Miller95def091999-11-25 00:26:21 +11001242 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243}
Damien Miller874d77b2000-10-14 16:23:11 +11001244
1245void
1246addargs(char *fmt, ...)
1247{
1248 va_list ap;
1249 char buf[1024];
1250
1251 va_start(ap, fmt);
1252 vsnprintf(buf, sizeof(buf), fmt, ap);
1253 va_end(ap);
1254
1255 if (args.list == NULL) {
1256 args.nalloc = 32;
1257 args.num = 0;
1258 args.list = xmalloc(args.nalloc * sizeof(char *));
1259 } else if (args.num+2 >= args.nalloc) {
1260 args.nalloc *= 2;
1261 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1262 }
1263 args.list[args.num++] = xstrdup(buf);
1264 args.list[args.num] = NULL;
1265}