blob: 0095d6ff8d9680ace82f2813f03b94e02b141e2a [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 Lindstrom31ca54a2001-02-09 02:11:24 +000078RCSID("$OpenBSD: scp.c,v 1.56 2001/02/08 19:30:52 itojun 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
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000222int main(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100223int response(void);
224void rsource(char *, struct stat *);
225void sink(int, char *[]);
226void source(int, char *[]);
227void tolocal(int, char *[]);
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000228char *cleanhostname(char *);
Damien Miller95def091999-11-25 00:26:21 +1100229void toremote(char *, int, char *[]);
230void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231
232int
233main(argc, argv)
234 int argc;
235 char *argv[];
236{
237 int ch, fflag, tflag;
238 char *targ;
239 extern char *optarg;
240 extern int optind;
241
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000242 __progname = get_progname(argv[0]);
243
Damien Miller874d77b2000-10-14 16:23:11 +1100244 args.list = NULL;
245 addargs("ssh"); /* overwritten with ssh_program */
246 addargs("-x");
247 addargs("-oFallBackToRsh no");
248
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000250 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100251 switch (ch) {
252 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100253 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100254 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100255 case 'C':
256 addargs("-%c", ch);
257 break;
258 case 'o':
259 case 'c':
260 case 'i':
Damien Miller50a41ed2000-10-16 12:14:42 +1100261 addargs("-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100262 break;
263 case 'P':
Damien Miller50a41ed2000-10-16 12:14:42 +1100264 addargs("-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100265 break;
266 case 'B':
Damien Miller50a41ed2000-10-16 12:14:42 +1100267 addargs("-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100268 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269 case 'p':
270 pflag = 1;
271 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272 case 'r':
273 iamrecursive = 1;
274 break;
Damien Millerad833b32000-08-23 10:46:23 +1000275 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100276 ssh_program = xstrdup(optarg);
277 break;
278 case 'v':
279 verbose_mode = 1;
280 break;
281 case 'q':
282 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000283 break;
284
Damien Miller95def091999-11-25 00:26:21 +1100285 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286 case 'd':
287 targetshouldbedirectory = 1;
288 break;
Damien Miller95def091999-11-25 00:26:21 +1100289 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290 iamremote = 1;
291 fflag = 1;
292 break;
Damien Miller95def091999-11-25 00:26:21 +1100293 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 iamremote = 1;
295 tflag = 1;
296 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297 case '?':
298 default:
299 usage();
300 }
301 argc -= optind;
302 argv += optind;
303
304 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100305 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Damien Miller95def091999-11-25 00:26:21 +1100307 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308 showprogress = 0;
309
310 remin = STDIN_FILENO;
311 remout = STDOUT_FILENO;
312
Kevin Stevesef4eea92001-02-05 12:42:17 +0000313 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100314 /* Follow "protocol", send data. */
315 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316 source(argc, argv);
317 exit(errs != 0);
318 }
Damien Miller95def091999-11-25 00:26:21 +1100319 if (tflag) {
320 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321 sink(argc, argv);
322 exit(errs != 0);
323 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324 if (argc < 2)
325 usage();
326 if (argc > 2)
327 targetshouldbedirectory = 1;
328
329 remin = remout = -1;
330 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000331 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
332 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000333 iamrecursive ? " -r" : "", pflag ? " -p" : "",
334 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335
Damien Miller95def091999-11-25 00:26:21 +1100336 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
338 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
339 toremote(targ, argc, argv);
340 else {
Damien Miller95def091999-11-25 00:26:21 +1100341 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342 if (targetshouldbedirectory)
343 verifydir(argv[argc - 1]);
344 }
345 exit(errs != 0);
346}
347
Damien Miller34132e52000-01-14 15:45:46 +1100348char *
349cleanhostname(host)
350 char *host;
351{
352 if (*host == '[' && host[strlen(host) - 1] == ']') {
353 host[strlen(host) - 1] = '\0';
354 return (host + 1);
355 } else
356 return host;
357}
358
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359void
360toremote(targ, argc, argv)
361 char *targ, *argv[];
362 int argc;
363{
364 int i, len;
365 char *bp, *host, *src, *suser, *thost, *tuser;
366
367 *targ++ = 0;
368 if (*targ == 0)
369 targ = ".";
370
371 if ((thost = strchr(argv[argc - 1], '@'))) {
372 /* user@host */
373 *thost++ = 0;
374 tuser = argv[argc - 1];
375 if (*tuser == '\0')
376 tuser = NULL;
377 else if (!okname(tuser))
378 exit(1);
379 } else {
380 thost = argv[argc - 1];
381 tuser = NULL;
382 }
383
384 for (i = 0; i < argc - 1; i++) {
385 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100386 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387 *src++ = 0;
388 if (*src == 0)
389 src = ".";
390 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000391 len = strlen(ssh_program) + strlen(argv[i]) +
392 strlen(src) + (tuser ? strlen(tuser) : 0) +
393 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100394 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395 if (host) {
396 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100397 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398 suser = argv[i];
399 if (*suser == '\0')
400 suser = pwd->pw_name;
401 else if (!okname(suser))
402 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000403 snprintf(bp, len,
404 "%s%s -x -o'FallBackToRsh no' -n "
405 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000406 ssh_program, verbose_mode ? " -v" : "",
407 suser, host, cmd, src,
408 tuser ? tuser : "", tuser ? "@" : "",
409 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100410 } else {
411 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000412 snprintf(bp, len,
413 "exec %s%s -x -o'FallBackToRsh no' -n %s "
414 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000415 ssh_program, verbose_mode ? " -v" : "",
416 host, cmd, src,
417 tuser ? tuser : "", tuser ? "@" : "",
418 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100419 }
Damien Miller95def091999-11-25 00:26:21 +1100420 if (verbose_mode)
421 fprintf(stderr, "Executing: %s\n", bp);
422 (void) system(bp);
423 (void) xfree(bp);
424 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425 if (remin == -1) {
426 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100427 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000428 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100429 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000430 if (do_cmd(host, tuser, bp, &remin,
431 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100432 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433 if (response() < 0)
434 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100435 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 }
Damien Miller95def091999-11-25 00:26:21 +1100437 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 }
439 }
440}
441
442void
443tolocal(argc, argv)
444 int argc;
445 char *argv[];
446{
447 int i, len;
448 char *bp, *host, *src, *suser;
449
450 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100451 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000452 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000453 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000455 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000456 iamrecursive ? " -r" : "", pflag ? " -p" : "",
457 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100458 if (verbose_mode)
459 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 if (system(bp))
461 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100462 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463 continue;
464 }
465 *src++ = 0;
466 if (*src == 0)
467 src = ".";
468 if ((host = strchr(argv[i], '@')) == NULL) {
469 host = argv[i];
470 suser = NULL;
471 } else {
472 *host++ = 0;
473 suser = argv[i];
474 if (*suser == '\0')
475 suser = pwd->pw_name;
476 else if (!okname(suser))
477 continue;
478 }
Damien Miller34132e52000-01-14 15:45:46 +1100479 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100481 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000482 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000483 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100484 (void) xfree(bp);
485 ++errs;
486 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 }
Damien Miller95def091999-11-25 00:26:21 +1100488 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100490 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491 remin = remout = -1;
492 }
493}
494
495void
496source(argc, argv)
497 int argc;
498 char *argv[];
499{
500 struct stat stb;
501 static BUF buffer;
502 BUF *bp;
503 off_t i;
504 int amt, fd, haderr, indx, result;
505 char *last, *name, buf[2048];
506
507 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100508 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509 statbytes = 0;
510 if ((fd = open(name, O_RDONLY, 0)) < 0)
511 goto syserr;
512 if (fstat(fd, &stb) < 0) {
513syserr: run_err("%s: %s", name, strerror(errno));
514 goto next;
515 }
516 switch (stb.st_mode & S_IFMT) {
517 case S_IFREG:
518 break;
519 case S_IFDIR:
520 if (iamrecursive) {
521 rsource(name, &stb);
522 goto next;
523 }
524 /* FALLTHROUGH */
525 default:
526 run_err("%s: not a regular file", name);
527 goto next;
528 }
529 if ((last = strrchr(name, '/')) == NULL)
530 last = name;
531 else
532 ++last;
533 curfile = last;
534 if (pflag) {
535 /*
536 * Make it compatible with possible future
537 * versions expecting microseconds.
538 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000539 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000540 (u_long) stb.st_mtime,
541 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000542 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543 if (response() < 0)
544 goto next;
545 }
546#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000547 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000548 (u_int) (stb.st_mode & FILEMODEMASK),
549 (u_long) stb.st_size, last);
Damien Miller95def091999-11-25 00:26:21 +1100550 if (verbose_mode) {
551 fprintf(stderr, "Sending file modes: %s", buf);
552 fflush(stderr);
553 }
Damien Miller35dabd02000-05-01 21:10:33 +1000554 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 if (response() < 0)
556 goto next;
557 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100558next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 continue;
560 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 if (showprogress) {
562 totalbytes = stb.st_size;
563 progressmeter(-1);
564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 /* Keep writing after an error so that we stay sync'd up. */
566 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
567 amt = bp->cnt;
568 if (i + amt > stb.st_size)
569 amt = stb.st_size - i;
570 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000571 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572 if (result != amt)
573 haderr = result >= 0 ? EIO : errno;
574 }
575 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000576 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 else {
Damien Miller864ea591999-12-15 11:04:25 +1100578 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 if (result != amt)
580 haderr = result >= 0 ? EIO : errno;
581 statbytes += result;
582 }
583 }
Damien Miller95def091999-11-25 00:26:21 +1100584 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 progressmeter(1);
586
587 if (close(fd) < 0 && !haderr)
588 haderr = errno;
589 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000590 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 else
592 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100593 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594 }
595}
596
597void
598rsource(name, statp)
599 char *name;
600 struct stat *statp;
601{
602 DIR *dirp;
603 struct dirent *dp;
604 char *last, *vect[1], path[1100];
605
606 if (!(dirp = opendir(name))) {
607 run_err("%s: %s", name, strerror(errno));
608 return;
609 }
610 last = strrchr(name, '/');
611 if (last == 0)
612 last = name;
613 else
614 last++;
615 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000616 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000617 (u_long) statp->st_mtime,
618 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000619 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620 if (response() < 0) {
621 closedir(dirp);
622 return;
623 }
624 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000625 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000626 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100627 if (verbose_mode)
628 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000629 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630 if (response() < 0) {
631 closedir(dirp);
632 return;
633 }
634 while ((dp = readdir(dirp))) {
635 if (dp->d_ino == 0)
636 continue;
637 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
638 continue;
639 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
640 run_err("%s/%s: name too long", name, dp->d_name);
641 continue;
642 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000643 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644 vect[0] = path;
645 source(1, vect);
646 }
Damien Miller95def091999-11-25 00:26:21 +1100647 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000648 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100649 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650}
651
652void
653sink(argc, argv)
654 int argc;
655 char *argv[];
656{
657 static BUF buffer;
658 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100659 enum {
660 YES, NO, DISPLAYED
661 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662 BUF *bp;
663 off_t i, j;
664 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100665 off_t size;
666 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100668 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100669 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670
671#define SCREWUP(str) { why = str; goto screwup; }
672
673 setimes = targisdir = 0;
674 mask = umask(0);
675 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100676 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677 if (argc != 1) {
678 run_err("ambiguous target");
679 exit(1);
680 }
681 targ = *argv;
682 if (targetshouldbedirectory)
683 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100684
Damien Miller35dabd02000-05-01 21:10:33 +1000685 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
687 targisdir = 1;
688 for (first = 1;; first = 0) {
689 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000690 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 return;
692 if (*cp++ == '\n')
693 SCREWUP("unexpected <newline>");
694 do {
Damien Millere247cc42000-05-07 12:03:14 +1000695 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696 SCREWUP("lost connection");
697 *cp++ = ch;
698 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
699 *cp = 0;
700
701 if (buf[0] == '\01' || buf[0] == '\02') {
702 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000703 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000704 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705 if (buf[0] == '\02')
706 exit(1);
707 ++errs;
708 continue;
709 }
710 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000711 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712 return;
713 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714 if (ch == '\n')
715 *--cp = 0;
716
717#define getnum(t) (t) = 0; \
718 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
719 cp = buf;
720 if (*cp == 'T') {
721 setimes++;
722 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100723 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 if (*cp++ != ' ')
725 SCREWUP("mtime.sec not delimited");
726 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100727 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 if (*cp++ != ' ')
729 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100730 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731 if (*cp++ != ' ')
732 SCREWUP("atime.sec not delimited");
733 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100734 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735 if (*cp++ != '\0')
736 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000737 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738 continue;
739 }
740 if (*cp != 'C' && *cp != 'D') {
741 /*
742 * Check for the case "rcp remote:foo\* local:bar".
743 * In this case, the line "No match." can be returned
744 * by the shell before the rcp command on the remote is
745 * executed so the ^Aerror_message convention isn't
746 * followed.
747 */
748 if (first) {
749 run_err("%s", cp);
750 exit(1);
751 }
752 SCREWUP("expected control record");
753 }
754 mode = 0;
755 for (++cp; cp < buf + 5; cp++) {
756 if (*cp < '0' || *cp > '7')
757 SCREWUP("bad mode");
758 mode = (mode << 3) | (*cp - '0');
759 }
760 if (*cp++ != ' ')
761 SCREWUP("mode not delimited");
762
Damien Miller95def091999-11-25 00:26:21 +1100763 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000764 size = size * 10 + (*cp++ - '0');
765 if (*cp++ != ' ')
766 SCREWUP("size not delimited");
767 if (targisdir) {
768 static char *namebuf;
769 static int cursize;
770 size_t need;
771
772 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000773 if (need > cursize) {
774 if (namebuf)
775 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100776 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000777 cursize = need;
778 }
779 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000780 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781 np = namebuf;
782 } else
783 np = targ;
784 curfile = cp;
785 exists = stat(np, &stb) == 0;
786 if (buf[0] == 'D') {
787 int mod_flag = pflag;
788 if (exists) {
789 if (!S_ISDIR(stb.st_mode)) {
790 errno = ENOTDIR;
791 goto bad;
792 }
793 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100794 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795 } else {
Damien Miller95def091999-11-25 00:26:21 +1100796 /* Handle copying from a read-only
797 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798 mod_flag = 1;
799 if (mkdir(np, mode | S_IRWXU) < 0)
800 goto bad;
801 }
802 vect[0] = np;
803 sink(1, vect);
804 if (setimes) {
805 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100806 if (utimes(np, tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100807 run_err("%s: set times: %s",
808 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809 }
810 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100811 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812 continue;
813 }
814 omode = mode;
815 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100816 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817bad: run_err("%s: %s", np, strerror(errno));
818 continue;
819 }
Damien Miller35dabd02000-05-01 21:10:33 +1000820 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100822 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000823 continue;
824 }
825 cp = bp->buf;
826 wrerr = NO;
827
828 if (showprogress) {
829 totalbytes = size;
830 progressmeter(-1);
831 }
832 statbytes = 0;
833 for (count = i = 0; i < size; i += 4096) {
834 amt = 4096;
835 if (i + amt > size)
836 amt = size - i;
837 count += amt;
838 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100839 j = read(remin, cp, amt);
840 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
841 continue;
842 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100844 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845 exit(1);
846 }
847 amt -= j;
848 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100849 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 } while (amt > 0);
851 if (count == bp->cnt) {
852 /* Keep reading so we stay sync'd up. */
853 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000854 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 if (j != count) {
856 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100857 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858 }
859 }
860 count = 0;
861 cp = bp->buf;
862 }
863 }
864 if (showprogress)
865 progressmeter(1);
866 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000867 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100869 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870 }
871#if 0
872 if (ftruncate(ofd, size)) {
873 run_err("%s: truncate: %s", np, strerror(errno));
874 wrerr = DISPLAYED;
875 }
876#endif
877 if (pflag) {
878 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100879#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100881#else /* HAVE_FCHMOD */
882 if (chmod(np, omode))
883#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100885 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 } else {
887 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100888#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100890#else /* HAVE_FCHMOD */
891 if (chmod(np, omode & ~mask))
892#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100894 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 }
Damien Millerbe484b52000-07-15 14:14:16 +1000896 if (close(ofd) == -1) {
897 wrerr = YES;
898 wrerrno = errno;
899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 if (setimes && wrerr == NO) {
902 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100903 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100905 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 wrerr = DISPLAYED;
907 }
908 }
Damien Miller95def091999-11-25 00:26:21 +1100909 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910 case YES:
911 run_err("%s: %s", np, strerror(wrerrno));
912 break;
913 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000914 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 break;
916 case DISPLAYED:
917 break;
918 }
919 }
920screwup:
921 run_err("protocol error: %s", why);
922 exit(1);
923}
924
925int
926response()
927{
928 char ch, *cp, resp, rbuf[2048];
929
Damien Millere247cc42000-05-07 12:03:14 +1000930 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931 lostconn(0);
932
933 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100934 switch (resp) {
935 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 return (0);
937 default:
938 *cp++ = resp;
939 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100940 case 1: /* error, followed by error msg */
941 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 do {
Damien Millere247cc42000-05-07 12:03:14 +1000943 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944 lostconn(0);
945 *cp++ = ch;
946 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
947
948 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000949 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950 ++errs;
951 if (resp == 1)
952 return (-1);
953 exit(1);
954 }
955 /* NOTREACHED */
956}
957
958void
959usage()
960{
Damien Millerad833b32000-08-23 10:46:23 +1000961 (void) fprintf(stderr, "usage: scp "
962 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
963 " scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964 exit(1);
965}
966
967void
Damien Miller95def091999-11-25 00:26:21 +1100968run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969{
970 static FILE *fp;
971 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
973 ++errs;
974 if (fp == NULL && !(fp = fdopen(remout, "w")))
975 return;
Damien Miller95def091999-11-25 00:26:21 +1100976 (void) fprintf(fp, "%c", 0x01);
977 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100978 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100979 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100980 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100981 (void) fprintf(fp, "\n");
982 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Damien Miller95def091999-11-25 00:26:21 +1100984 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100985 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100986 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100987 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100988 fprintf(stderr, "\n");
989 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990}
991
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992char *
993colon(cp)
994 char *cp;
995{
Damien Miller34132e52000-01-14 15:45:46 +1100996 int flag = 0;
997
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998 if (*cp == ':') /* Leading colon is part of file name. */
999 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001000 if (*cp == '[')
1001 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
1003 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001004 if (*cp == '@' && *(cp+1) == '[')
1005 flag = 1;
1006 if (*cp == ']' && *(cp+1) == ':' && flag)
1007 return (cp+1);
1008 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009 return (cp);
1010 if (*cp == '/')
1011 return (0);
1012 }
1013 return (0);
1014}
1015
1016void
1017verifydir(cp)
1018 char *cp;
1019{
1020 struct stat stb;
1021
1022 if (!stat(cp, &stb)) {
1023 if (S_ISDIR(stb.st_mode))
1024 return;
1025 errno = ENOTDIR;
1026 }
1027 run_err("%s: %s", cp, strerror(errno));
1028 exit(1);
1029}
1030
1031int
1032okname(cp0)
1033 char *cp0;
1034{
1035 int c;
1036 char *cp;
1037
1038 cp = cp0;
1039 do {
1040 c = *cp;
1041 if (c & 0200)
1042 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001043 if (!isalpha(c) && !isdigit(c) &&
1044 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045 goto bad;
1046 } while (*++cp);
1047 return (1);
1048
Damien Miller98c7ad62000-03-09 21:27:49 +11001049bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050 return (0);
1051}
1052
1053BUF *
1054allocbuf(bp, fd, blksize)
1055 BUF *bp;
1056 int fd, blksize;
1057{
1058 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001059#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060 struct stat stb;
1061
1062 if (fstat(fd, &stb) < 0) {
1063 run_err("fstat: %s", strerror(errno));
1064 return (0);
1065 }
Damien Miller95def091999-11-25 00:26:21 +11001066 if (stb.st_blksize == 0)
1067 size = blksize;
1068 else
1069 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001070 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001071#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001072 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001073#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074 if (bp->cnt >= size)
1075 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001076 if (bp->buf == NULL)
1077 bp->buf = xmalloc(size);
1078 else
1079 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080 bp->cnt = size;
1081 return (bp);
1082}
1083
1084void
1085lostconn(signo)
1086 int signo;
1087{
1088 if (!iamremote)
1089 fprintf(stderr, "lost connection\n");
1090 exit(1);
1091}
1092
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093
1094void
1095alarmtimer(int wait)
1096{
Damien Miller95def091999-11-25 00:26:21 +11001097 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098
Damien Miller95def091999-11-25 00:26:21 +11001099 itv.it_value.tv_sec = wait;
1100 itv.it_value.tv_usec = 0;
1101 itv.it_interval = itv.it_value;
1102 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001103}
1104
1105void
Damien Miller7684ee12000-03-17 23:40:15 +11001106updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107{
1108 int save_errno = errno;
1109
1110 progressmeter(0);
1111 errno = save_errno;
1112}
1113
Damien Miller81428f91999-11-18 09:28:11 +11001114int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001115foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001116{
1117 static pid_t pgrp = -1;
1118 int ctty_pgrp;
1119
1120 if (pgrp == -1)
1121 pgrp = getpgrp();
1122
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001123#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001124 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1125 ctty_pgrp == pgrp);
1126#else
Damien Miller95def091999-11-25 00:26:21 +11001127 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1128 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001129#endif
Damien Miller81428f91999-11-18 09:28:11 +11001130}
1131
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132void
1133progressmeter(int flag)
1134{
1135 static const char prefixes[] = " KMGTP";
1136 static struct timeval lastupdate;
1137 static off_t lastsize;
1138 struct timeval now, td, wait;
1139 off_t cursize, abbrevsize;
1140 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001141 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142 char buf[256];
1143
1144 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001145 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146 lastupdate = start;
1147 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001148 }
Damien Miller81428f91999-11-18 09:28:11 +11001149 if (foregroundproc() == 0)
1150 return;
1151
Damien Miller95def091999-11-25 00:26:21 +11001152 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001154 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001155 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 ratio = MAX(ratio, 0);
1157 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001158 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159 ratio = 100;
1160
Damien Miller95def091999-11-25 00:26:21 +11001161 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162
1163 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001164 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 if (barlength > 0) {
1166 i = barlength * ratio / 100;
1167 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001168 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170 i = 0;
1171 abbrevsize = cursize;
1172 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1173 i++;
1174 abbrevsize >>= 10;
1175 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001176 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
1177 (unsigned long) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
1179 timersub(&now, &lastupdate, &wait);
1180 if (cursize > lastsize) {
1181 lastupdate = now;
1182 lastsize = cursize;
1183 if (wait.tv_sec >= STALLTIME) {
1184 start.tv_sec += wait.tv_sec;
1185 start.tv_usec += wait.tv_usec;
1186 }
1187 wait.tv_sec = 0;
1188 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189 timersub(&now, &start, &td);
1190 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1191
Kevin Steves7d00ba42000-12-15 23:03:10 +00001192 if (flag != 1 &&
1193 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001195 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196 } else if (wait.tv_sec >= STALLTIME) {
1197 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001198 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001200 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001201 remaining = (int)(totalbytes / (statbytes / elapsed) -
1202 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001203 else
1204 remaining = elapsed;
1205
Damien Miller01ab4a21999-10-28 15:23:30 +10001206 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207 if (i)
1208 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001209 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210 else
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001212 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213 i = remaining % 3600;
1214 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001215 "%02d:%02d%s", i / 60, i % 60,
1216 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217 }
1218 atomicio(write, fileno(stdout), buf, strlen(buf));
1219
1220 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001221 struct sigaction sa;
1222 sa.sa_handler = updateprogressmeter;
Damien Miller77aba9d2000-08-30 10:11:30 +11001223 sigemptyset((sigset_t *)&sa.sa_mask);
Damien Miller615f9392000-05-17 22:53:33 +10001224#ifdef SA_RESTART
Damien Miller864ea591999-12-15 11:04:25 +11001225 sa.sa_flags = SA_RESTART;
Damien Miller615f9392000-05-17 22:53:33 +10001226#endif
Damien Miller864ea591999-12-15 11:04:25 +11001227 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228 alarmtimer(1);
1229 } else if (flag == 1) {
1230 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001231 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232 statbytes = 0;
1233 }
1234}
1235
1236int
1237getttywidth(void)
1238{
1239 struct winsize winsize;
1240
1241 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001242 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243 else
Damien Miller95def091999-11-25 00:26:21 +11001244 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245}
Damien Miller874d77b2000-10-14 16:23:11 +11001246
1247void
1248addargs(char *fmt, ...)
1249{
1250 va_list ap;
1251 char buf[1024];
1252
1253 va_start(ap, fmt);
1254 vsnprintf(buf, sizeof(buf), fmt, ap);
1255 va_end(ap);
1256
1257 if (args.list == NULL) {
1258 args.nalloc = 32;
1259 args.num = 0;
1260 args.list = xmalloc(args.nalloc * sizeof(char *));
1261 } else if (args.num+2 >= args.nalloc) {
1262 args.nalloc *= 2;
1263 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1264 }
1265 args.list[args.num++] = xstrdup(buf);
1266 args.list[args.num] = NULL;
1267}