blob: c654cb66f141efa4a80a208d4e319d73480d4c8c [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/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +000017 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110019 *
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 Lindstromd47cf4d2001-04-07 01:14:38 +000078RCSID("$OpenBSD: scp.c,v 1.65 2001/04/06 16:46:59 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"
Kevin Steves93c17d92001-02-18 03:55:16 +000084#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrom49a79c02000-11-17 03:47:20 +000086#ifdef HAVE___PROGNAME
87extern char *__progname;
88#else
89char *__progname;
90#endif
91
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092/* For progressmeter() -- number of seconds before xfer considered "stalled" */
93#define STALLTIME 5
94
Damien Miller484118e2000-07-02 19:13:56 +100095/* Progress meter bar */
96#define BAR \
97 "************************************************************"\
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"
101#define MAX_BARLENGTH (sizeof(BAR) - 1)
102
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103/* Visual statistics about files as they are transferred. */
104void progressmeter(int);
105
106/* Returns width of the terminal (for progress meter calculations). */
107int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000108int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller874d77b2000-10-14 16:23:11 +1100110/* setup arguments for the call to ssh */
111void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113/* Time a transfer started. */
114static struct timeval start;
115
116/* Number of bytes of current file transferred so far. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000117volatile u_long statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
119/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100120off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122/* Name of current file being transferred. */
123char *curfile;
124
125/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100126int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128/* This is set to zero if the progressmeter is not desired. */
129int showprogress = 1;
130
Damien Millerad833b32000-08-23 10:46:23 +1000131/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000132char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000133
Damien Miller874d77b2000-10-14 16:23:11 +1100134/* This is the list of arguments that scp passes to ssh */
135struct {
136 char **list;
137 int num;
138 int nalloc;
139} args;
140
Damien Miller5428f641999-11-25 11:54:57 +1100141/*
142 * This function executes the given command as the specified user on the
143 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
144 * assigns the input and output file descriptors on success.
145 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller4af51302000-04-16 11:18:38 +1000147int
Damien Millerad833b32000-08-23 10:46:23 +1000148do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149{
Damien Miller95def091999-11-25 00:26:21 +1100150 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100153 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
154 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller5428f641999-11-25 11:54:57 +1100156 /*
157 * Reserve two descriptors so that the real pipes won't get
158 * descriptors 0 and 1 because that will screw up dup2 below.
159 */
Damien Miller95def091999-11-25 00:26:21 +1100160 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Damien Miller95def091999-11-25 00:26:21 +1100162 /* Create a socket pair for communicating with ssh. */
163 if (pipe(pin) < 0)
164 fatal("pipe: %s", strerror(errno));
165 if (pipe(pout) < 0)
166 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Damien Miller95def091999-11-25 00:26:21 +1100168 /* Free the reserved descriptors. */
169 close(reserved[0]);
170 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171
Damien Miller95def091999-11-25 00:26:21 +1100172 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100173 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Child. */
175 close(pin[1]);
176 close(pout[0]);
177 dup2(pin[0], 0);
178 dup2(pout[1], 1);
179 close(pin[0]);
180 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181
Damien Miller874d77b2000-10-14 16:23:11 +1100182 args.list[0] = ssh_program;
183 if (remuser != NULL)
Damien Millere4041c92000-10-14 17:45:58 +1100184 addargs("-l%s", remuser);
Damien Miller874d77b2000-10-14 16:23:11 +1100185 addargs("%s", host);
186 addargs("%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100187
Damien Miller874d77b2000-10-14 16:23:11 +1100188 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000189 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100190 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191 }
Damien Miller95def091999-11-25 00:26:21 +1100192 /* Parent. Close the other side, and return the local side. */
193 close(pin[0]);
194 *fdout = pin[1];
195 close(pout[1]);
196 *fdin = pout[0];
197 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198}
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200typedef struct {
201 int cnt;
202 char *buf;
203} BUF;
204
Damien Miller95def091999-11-25 00:26:21 +1100205BUF *allocbuf(BUF *, int, int);
206char *colon(char *);
207void lostconn(int);
208void nospace(void);
209int okname(char *);
210void run_err(const char *,...);
211void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100214uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215int errs, remin, remout;
216int pflag, iamremote, iamrecursive, targetshouldbedirectory;
217
218#define CMDNEEDS 64
219char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
220
Damien Miller95def091999-11-25 00:26:21 +1100221int response(void);
222void rsource(char *, struct stat *);
223void sink(int, char *[]);
224void source(int, char *[]);
225void tolocal(int, char *[]);
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000226char *cleanhostname(char *);
Damien Miller95def091999-11-25 00:26:21 +1100227void 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;
Damien Miller402b3312001-04-14 00:28:42 +1000294#ifdef HAVE_CYGWIN
295 setmode(0, O_BINARY);
296#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298 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];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000506 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
508 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100509 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000511 len = strlen(name);
512 while (len > 1 && name[len-1] == '/')
513 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514 if ((fd = open(name, O_RDONLY, 0)) < 0)
515 goto syserr;
516 if (fstat(fd, &stb) < 0) {
517syserr: run_err("%s: %s", name, strerror(errno));
518 goto next;
519 }
520 switch (stb.st_mode & S_IFMT) {
521 case S_IFREG:
522 break;
523 case S_IFDIR:
524 if (iamrecursive) {
525 rsource(name, &stb);
526 goto next;
527 }
528 /* FALLTHROUGH */
529 default:
530 run_err("%s: not a regular file", name);
531 goto next;
532 }
533 if ((last = strrchr(name, '/')) == NULL)
534 last = name;
535 else
536 ++last;
537 curfile = last;
538 if (pflag) {
539 /*
540 * Make it compatible with possible future
541 * versions expecting microseconds.
542 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000543 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000544 (u_long) stb.st_mtime,
545 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000546 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547 if (response() < 0)
548 goto next;
549 }
550#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100551#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000552 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000553 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000554 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100555#else
556 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000557 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100558 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000559 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100560#endif
561
Damien Miller95def091999-11-25 00:26:21 +1100562 if (verbose_mode) {
563 fprintf(stderr, "Sending file modes: %s", buf);
564 fflush(stderr);
565 }
Damien Miller35dabd02000-05-01 21:10:33 +1000566 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567 if (response() < 0)
568 goto next;
569 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100570next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571 continue;
572 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 if (showprogress) {
574 totalbytes = stb.st_size;
575 progressmeter(-1);
576 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 /* Keep writing after an error so that we stay sync'd up. */
578 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
579 amt = bp->cnt;
580 if (i + amt > stb.st_size)
581 amt = stb.st_size - i;
582 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000583 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584 if (result != amt)
585 haderr = result >= 0 ? EIO : errno;
586 }
587 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000588 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 else {
Damien Miller864ea591999-12-15 11:04:25 +1100590 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 if (result != amt)
592 haderr = result >= 0 ? EIO : errno;
593 statbytes += result;
594 }
595 }
Damien Miller95def091999-11-25 00:26:21 +1100596 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597 progressmeter(1);
598
599 if (close(fd) < 0 && !haderr)
600 haderr = errno;
601 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000602 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603 else
604 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100605 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606 }
607}
608
609void
610rsource(name, statp)
611 char *name;
612 struct stat *statp;
613{
614 DIR *dirp;
615 struct dirent *dp;
616 char *last, *vect[1], path[1100];
617
618 if (!(dirp = opendir(name))) {
619 run_err("%s: %s", name, strerror(errno));
620 return;
621 }
622 last = strrchr(name, '/');
623 if (last == 0)
624 last = name;
625 else
626 last++;
627 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000628 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000629 (u_long) statp->st_mtime,
630 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000631 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632 if (response() < 0) {
633 closedir(dirp);
634 return;
635 }
636 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000637 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000638 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100639 if (verbose_mode)
640 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000641 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642 if (response() < 0) {
643 closedir(dirp);
644 return;
645 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000646 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647 if (dp->d_ino == 0)
648 continue;
649 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
650 continue;
651 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
652 run_err("%s/%s: name too long", name, dp->d_name);
653 continue;
654 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000655 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656 vect[0] = path;
657 source(1, vect);
658 }
Damien Miller95def091999-11-25 00:26:21 +1100659 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000660 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100661 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662}
663
664void
665sink(argc, argv)
666 int argc;
667 char *argv[];
668{
669 static BUF buffer;
670 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100671 enum {
672 YES, NO, DISPLAYED
673 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674 BUF *bp;
675 off_t i, j;
676 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100677 off_t size;
678 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100680 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100681 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682
683#define SCREWUP(str) { why = str; goto screwup; }
684
685 setimes = targisdir = 0;
686 mask = umask(0);
687 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100688 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689 if (argc != 1) {
690 run_err("ambiguous target");
691 exit(1);
692 }
693 targ = *argv;
694 if (targetshouldbedirectory)
695 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100696
Damien Miller35dabd02000-05-01 21:10:33 +1000697 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
699 targisdir = 1;
700 for (first = 1;; first = 0) {
701 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000702 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703 return;
704 if (*cp++ == '\n')
705 SCREWUP("unexpected <newline>");
706 do {
Damien Millere247cc42000-05-07 12:03:14 +1000707 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708 SCREWUP("lost connection");
709 *cp++ = ch;
710 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
711 *cp = 0;
712
713 if (buf[0] == '\01' || buf[0] == '\02') {
714 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000715 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000716 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717 if (buf[0] == '\02')
718 exit(1);
719 ++errs;
720 continue;
721 }
722 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000723 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 return;
725 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726 if (ch == '\n')
727 *--cp = 0;
728
729#define getnum(t) (t) = 0; \
730 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
731 cp = buf;
732 if (*cp == 'T') {
733 setimes++;
734 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100735 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736 if (*cp++ != ' ')
737 SCREWUP("mtime.sec not delimited");
738 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100739 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740 if (*cp++ != ' ')
741 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100742 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743 if (*cp++ != ' ')
744 SCREWUP("atime.sec not delimited");
745 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100746 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747 if (*cp++ != '\0')
748 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000749 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750 continue;
751 }
752 if (*cp != 'C' && *cp != 'D') {
753 /*
754 * Check for the case "rcp remote:foo\* local:bar".
755 * In this case, the line "No match." can be returned
756 * by the shell before the rcp command on the remote is
757 * executed so the ^Aerror_message convention isn't
758 * followed.
759 */
760 if (first) {
761 run_err("%s", cp);
762 exit(1);
763 }
764 SCREWUP("expected control record");
765 }
766 mode = 0;
767 for (++cp; cp < buf + 5; cp++) {
768 if (*cp < '0' || *cp > '7')
769 SCREWUP("bad mode");
770 mode = (mode << 3) | (*cp - '0');
771 }
772 if (*cp++ != ' ')
773 SCREWUP("mode not delimited");
774
Ben Lindstrombd472262001-03-29 00:39:55 +0000775 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776 size = size * 10 + (*cp++ - '0');
777 if (*cp++ != ' ')
778 SCREWUP("size not delimited");
779 if (targisdir) {
780 static char *namebuf;
781 static int cursize;
782 size_t need;
783
784 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000785 if (need > cursize) {
786 if (namebuf)
787 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100788 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000789 cursize = need;
790 }
791 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000792 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793 np = namebuf;
794 } else
795 np = targ;
796 curfile = cp;
797 exists = stat(np, &stb) == 0;
798 if (buf[0] == 'D') {
799 int mod_flag = pflag;
800 if (exists) {
801 if (!S_ISDIR(stb.st_mode)) {
802 errno = ENOTDIR;
803 goto bad;
804 }
805 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100806 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 } else {
Damien Miller95def091999-11-25 00:26:21 +1100808 /* Handle copying from a read-only
809 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810 mod_flag = 1;
811 if (mkdir(np, mode | S_IRWXU) < 0)
812 goto bad;
813 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000814 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815 sink(1, vect);
816 if (setimes) {
817 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100818 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100819 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100820 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821 }
822 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100823 (void) chmod(vect[0], mode);
824 if (vect[0])
825 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826 continue;
827 }
828 omode = mode;
829 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100830 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831bad: run_err("%s: %s", np, strerror(errno));
832 continue;
833 }
Damien Miller35dabd02000-05-01 21:10:33 +1000834 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100836 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837 continue;
838 }
839 cp = bp->buf;
840 wrerr = NO;
841
842 if (showprogress) {
843 totalbytes = size;
844 progressmeter(-1);
845 }
846 statbytes = 0;
847 for (count = i = 0; i < size; i += 4096) {
848 amt = 4096;
849 if (i + amt > size)
850 amt = size - i;
851 count += amt;
852 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100853 j = read(remin, cp, amt);
854 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
855 continue;
856 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000858 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 exit(1);
860 }
861 amt -= j;
862 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100863 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864 } while (amt > 0);
865 if (count == bp->cnt) {
866 /* Keep reading so we stay sync'd up. */
867 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000868 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869 if (j != count) {
870 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100871 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872 }
873 }
874 count = 0;
875 cp = bp->buf;
876 }
877 }
878 if (showprogress)
879 progressmeter(1);
880 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000881 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100883 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 }
885#if 0
886 if (ftruncate(ofd, size)) {
887 run_err("%s: truncate: %s", np, strerror(errno));
888 wrerr = DISPLAYED;
889 }
890#endif
891 if (pflag) {
892 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100893#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100895#else /* HAVE_FCHMOD */
896 if (chmod(np, omode))
897#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000899 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 } else {
901 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100902#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100904#else /* HAVE_FCHMOD */
905 if (chmod(np, omode & ~mask))
906#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000908 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909 }
Damien Millerbe484b52000-07-15 14:14:16 +1000910 if (close(ofd) == -1) {
911 wrerr = YES;
912 wrerrno = errno;
913 }
Damien Miller95def091999-11-25 00:26:21 +1100914 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 if (setimes && wrerr == NO) {
916 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100917 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000919 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920 wrerr = DISPLAYED;
921 }
922 }
Damien Miller95def091999-11-25 00:26:21 +1100923 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924 case YES:
925 run_err("%s: %s", np, strerror(wrerrno));
926 break;
927 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000928 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929 break;
930 case DISPLAYED:
931 break;
932 }
933 }
934screwup:
935 run_err("protocol error: %s", why);
936 exit(1);
937}
938
939int
940response()
941{
942 char ch, *cp, resp, rbuf[2048];
943
Damien Millere247cc42000-05-07 12:03:14 +1000944 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945 lostconn(0);
946
947 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100948 switch (resp) {
949 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950 return (0);
951 default:
952 *cp++ = resp;
953 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100954 case 1: /* error, followed by error msg */
955 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956 do {
Damien Millere247cc42000-05-07 12:03:14 +1000957 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958 lostconn(0);
959 *cp++ = ch;
960 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
961
962 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000963 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964 ++errs;
965 if (resp == 1)
966 return (-1);
967 exit(1);
968 }
969 /* NOTREACHED */
970}
971
972void
973usage()
974{
Damien Millerad833b32000-08-23 10:46:23 +1000975 (void) fprintf(stderr, "usage: scp "
Ben Lindstromff2618c2001-03-29 00:43:54 +0000976 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n"
977 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978 exit(1);
979}
980
981void
Damien Miller95def091999-11-25 00:26:21 +1100982run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983{
984 static FILE *fp;
985 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
987 ++errs;
988 if (fp == NULL && !(fp = fdopen(remout, "w")))
989 return;
Damien Miller95def091999-11-25 00:26:21 +1100990 (void) fprintf(fp, "%c", 0x01);
991 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100992 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100993 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100994 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100995 (void) fprintf(fp, "\n");
996 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
Damien Miller95def091999-11-25 00:26:21 +1100998 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100999 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001000 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001001 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001002 fprintf(stderr, "\n");
1003 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004}
1005
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006char *
1007colon(cp)
1008 char *cp;
1009{
Damien Miller34132e52000-01-14 15:45:46 +11001010 int flag = 0;
1011
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 if (*cp == ':') /* Leading colon is part of file name. */
1013 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001014 if (*cp == '[')
1015 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
1017 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001018 if (*cp == '@' && *(cp+1) == '[')
1019 flag = 1;
1020 if (*cp == ']' && *(cp+1) == ':' && flag)
1021 return (cp+1);
1022 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023 return (cp);
1024 if (*cp == '/')
1025 return (0);
1026 }
1027 return (0);
1028}
1029
1030void
1031verifydir(cp)
1032 char *cp;
1033{
1034 struct stat stb;
1035
1036 if (!stat(cp, &stb)) {
1037 if (S_ISDIR(stb.st_mode))
1038 return;
1039 errno = ENOTDIR;
1040 }
1041 run_err("%s: %s", cp, strerror(errno));
1042 exit(1);
1043}
1044
1045int
1046okname(cp0)
1047 char *cp0;
1048{
1049 int c;
1050 char *cp;
1051
1052 cp = cp0;
1053 do {
1054 c = *cp;
1055 if (c & 0200)
1056 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001057 if (!isalpha(c) && !isdigit(c) &&
1058 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001059 goto bad;
1060 } while (*++cp);
1061 return (1);
1062
Damien Miller98c7ad62000-03-09 21:27:49 +11001063bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064 return (0);
1065}
1066
1067BUF *
1068allocbuf(bp, fd, blksize)
1069 BUF *bp;
1070 int fd, blksize;
1071{
1072 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001073#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074 struct stat stb;
1075
1076 if (fstat(fd, &stb) < 0) {
1077 run_err("fstat: %s", strerror(errno));
1078 return (0);
1079 }
Damien Miller95def091999-11-25 00:26:21 +11001080 if (stb.st_blksize == 0)
1081 size = blksize;
1082 else
1083 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001084 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001085#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001086 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001087#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088 if (bp->cnt >= size)
1089 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001090 if (bp->buf == NULL)
1091 bp->buf = xmalloc(size);
1092 else
1093 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001094 bp->cnt = size;
1095 return (bp);
1096}
1097
1098void
1099lostconn(signo)
1100 int signo;
1101{
1102 if (!iamremote)
1103 fprintf(stderr, "lost connection\n");
1104 exit(1);
1105}
1106
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107
1108void
1109alarmtimer(int wait)
1110{
Damien Miller95def091999-11-25 00:26:21 +11001111 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Damien Miller95def091999-11-25 00:26:21 +11001113 itv.it_value.tv_sec = wait;
1114 itv.it_value.tv_usec = 0;
1115 itv.it_interval = itv.it_value;
1116 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117}
1118
1119void
Damien Miller7684ee12000-03-17 23:40:15 +11001120updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121{
1122 int save_errno = errno;
1123
1124 progressmeter(0);
1125 errno = save_errno;
1126}
1127
Damien Miller81428f91999-11-18 09:28:11 +11001128int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001129foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001130{
1131 static pid_t pgrp = -1;
1132 int ctty_pgrp;
1133
1134 if (pgrp == -1)
1135 pgrp = getpgrp();
1136
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001137#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001138 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1139 ctty_pgrp == pgrp);
1140#else
Damien Miller95def091999-11-25 00:26:21 +11001141 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1142 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001143#endif
Damien Miller81428f91999-11-18 09:28:11 +11001144}
1145
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146void
1147progressmeter(int flag)
1148{
1149 static const char prefixes[] = " KMGTP";
1150 static struct timeval lastupdate;
1151 static off_t lastsize;
1152 struct timeval now, td, wait;
1153 off_t cursize, abbrevsize;
1154 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001155 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 char buf[256];
1157
1158 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001159 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 lastupdate = start;
1161 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001162 }
Damien Miller81428f91999-11-18 09:28:11 +11001163 if (foregroundproc() == 0)
1164 return;
1165
Damien Miller95def091999-11-25 00:26:21 +11001166 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001168 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001169 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170 ratio = MAX(ratio, 0);
1171 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001172 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 ratio = 100;
1174
Damien Miller95def091999-11-25 00:26:21 +11001175 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176
1177 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001178 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179 if (barlength > 0) {
1180 i = barlength * ratio / 100;
1181 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001182 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 i = 0;
1185 abbrevsize = cursize;
1186 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1187 i++;
1188 abbrevsize >>= 10;
1189 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001190 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001191 (unsigned long) abbrevsize, prefixes[i],
1192 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
1194 timersub(&now, &lastupdate, &wait);
1195 if (cursize > lastsize) {
1196 lastupdate = now;
1197 lastsize = cursize;
1198 if (wait.tv_sec >= STALLTIME) {
1199 start.tv_sec += wait.tv_sec;
1200 start.tv_usec += wait.tv_usec;
1201 }
1202 wait.tv_sec = 0;
1203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204 timersub(&now, &start, &td);
1205 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1206
Kevin Steves7d00ba42000-12-15 23:03:10 +00001207 if (flag != 1 &&
1208 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001210 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 } else if (wait.tv_sec >= STALLTIME) {
1212 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001213 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001215 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001216 remaining = (int)(totalbytes / (statbytes / elapsed) -
1217 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001218 else
1219 remaining = elapsed;
1220
Damien Miller01ab4a21999-10-28 15:23:30 +10001221 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222 if (i)
1223 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001224 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225 else
1226 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001227 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228 i = remaining % 3600;
1229 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001230 "%02d:%02d%s", i / 60, i % 60,
1231 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232 }
1233 atomicio(write, fileno(stdout), buf, strlen(buf));
1234
1235 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001236 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237 alarmtimer(1);
1238 } else if (flag == 1) {
1239 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001240 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 statbytes = 0;
1242 }
1243}
1244
1245int
1246getttywidth(void)
1247{
1248 struct winsize winsize;
1249
1250 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001251 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252 else
Damien Miller95def091999-11-25 00:26:21 +11001253 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}
Damien Miller874d77b2000-10-14 16:23:11 +11001255
1256void
1257addargs(char *fmt, ...)
1258{
1259 va_list ap;
1260 char buf[1024];
1261
1262 va_start(ap, fmt);
1263 vsnprintf(buf, sizeof(buf), fmt, ap);
1264 va_end(ap);
1265
1266 if (args.list == NULL) {
1267 args.nalloc = 32;
1268 args.num = 0;
1269 args.list = xmalloc(args.nalloc * sizeof(char *));
1270 } else if (args.num+2 >= args.nalloc) {
1271 args.nalloc *= 2;
1272 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1273 }
1274 args.list[args.num++] = xstrdup(buf);
1275 args.list[args.num] = NULL;
1276}