blob: e5e3a97b8796224b1b32085afbfac8370861c008 [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 Lindstrombba81212001-06-25 05:01:22 +000078RCSID("$OpenBSD: scp.c,v 1.76 2001/06/23 15:12:19 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"
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
Ben Lindstrom387c4722001-05-08 20:27:25 +0000110/* Struct for addargs */
111arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +1100112
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 Lindstrom4eda71d2001-04-22 17:13:20 +0000117volatile off_t 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 Miller5428f641999-11-25 11:54:57 +1100134/*
135 * This function executes the given command as the specified user on the
136 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
137 * assigns the input and output file descriptors on success.
138 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller4af51302000-04-16 11:18:38 +1000140int
Damien Millerad833b32000-08-23 10:46:23 +1000141do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142{
Damien Miller95def091999-11-25 00:26:21 +1100143 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100146 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
147 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller5428f641999-11-25 11:54:57 +1100149 /*
150 * Reserve two descriptors so that the real pipes won't get
151 * descriptors 0 and 1 because that will screw up dup2 below.
152 */
Damien Miller95def091999-11-25 00:26:21 +1100153 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 /* Create a socket pair for communicating with ssh. */
156 if (pipe(pin) < 0)
157 fatal("pipe: %s", strerror(errno));
158 if (pipe(pout) < 0)
159 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* Free the reserved descriptors. */
162 close(reserved[0]);
163 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Damien Miller95def091999-11-25 00:26:21 +1100165 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100166 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Child. */
168 close(pin[1]);
169 close(pout[0]);
170 dup2(pin[0], 0);
171 dup2(pout[1], 1);
172 close(pin[0]);
173 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174
Damien Miller874d77b2000-10-14 16:23:11 +1100175 args.list[0] = ssh_program;
176 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000177 addargs(&args, "-l%s", remuser);
178 addargs(&args, "%s", host);
179 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100180
Damien Miller874d77b2000-10-14 16:23:11 +1100181 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000182 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100183 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184 }
Damien Miller95def091999-11-25 00:26:21 +1100185 /* Parent. Close the other side, and return the local side. */
186 close(pin[0]);
187 *fdout = pin[1];
188 close(pout[1]);
189 *fdin = pout[0];
190 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193typedef struct {
194 int cnt;
195 char *buf;
196} BUF;
197
Damien Miller95def091999-11-25 00:26:21 +1100198BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100199void lostconn(int);
200void nospace(void);
201int okname(char *);
202void run_err(const char *,...);
203void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100206uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207int errs, remin, remout;
208int pflag, iamremote, iamrecursive, targetshouldbedirectory;
209
210#define CMDNEEDS 64
211char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
212
Ben Lindstrombba81212001-06-25 05:01:22 +0000213int main(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100214int response(void);
215void rsource(char *, struct stat *);
216void sink(int, char *[]);
217void source(int, char *[]);
218void tolocal(int, char *[]);
219void toremote(char *, int, char *[]);
220void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
222int
223main(argc, argv)
224 int argc;
225 char *argv[];
226{
227 int ch, fflag, tflag;
228 char *targ;
229 extern char *optarg;
230 extern int optind;
231
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000232 __progname = get_progname(argv[0]);
233
Damien Miller874d77b2000-10-14 16:23:11 +1100234 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000235 addargs(&args, "ssh"); /* overwritten with ssh_program */
236 addargs(&args, "-x");
237 addargs(&args, "-oFallBackToRsh no");
Damien Miller874d77b2000-10-14 16:23:11 +1100238
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000240 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100241 switch (ch) {
242 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100243 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100244 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100245 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000246 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100247 break;
248 case 'o':
249 case 'c':
250 case 'i':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000251 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100252 break;
253 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000254 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100255 break;
256 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000257 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100258 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259 case 'p':
260 pflag = 1;
261 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262 case 'r':
263 iamrecursive = 1;
264 break;
Damien Millerad833b32000-08-23 10:46:23 +1000265 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100266 ssh_program = xstrdup(optarg);
267 break;
268 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000269 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100270 verbose_mode = 1;
271 break;
272 case 'q':
273 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000274 break;
275
Damien Miller95def091999-11-25 00:26:21 +1100276 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277 case 'd':
278 targetshouldbedirectory = 1;
279 break;
Damien Miller95def091999-11-25 00:26:21 +1100280 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281 iamremote = 1;
282 fflag = 1;
283 break;
Damien Miller95def091999-11-25 00:26:21 +1100284 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285 iamremote = 1;
286 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000287#ifdef HAVE_CYGWIN
288 setmode(0, O_BINARY);
289#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291 default:
292 usage();
293 }
294 argc -= optind;
295 argv += optind;
296
297 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100298 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299
Damien Miller95def091999-11-25 00:26:21 +1100300 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301 showprogress = 0;
302
303 remin = STDIN_FILENO;
304 remout = STDOUT_FILENO;
305
Kevin Stevesef4eea92001-02-05 12:42:17 +0000306 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100307 /* Follow "protocol", send data. */
308 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309 source(argc, argv);
310 exit(errs != 0);
311 }
Damien Miller95def091999-11-25 00:26:21 +1100312 if (tflag) {
313 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314 sink(argc, argv);
315 exit(errs != 0);
316 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317 if (argc < 2)
318 usage();
319 if (argc > 2)
320 targetshouldbedirectory = 1;
321
322 remin = remout = -1;
323 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000324 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
325 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000326 iamrecursive ? " -r" : "", pflag ? " -p" : "",
327 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
Damien Miller95def091999-11-25 00:26:21 +1100329 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
331 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
332 toremote(targ, argc, argv);
333 else {
Damien Miller95def091999-11-25 00:26:21 +1100334 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335 if (targetshouldbedirectory)
336 verifydir(argv[argc - 1]);
337 }
338 exit(errs != 0);
339}
340
341void
342toremote(targ, argc, argv)
343 char *targ, *argv[];
344 int argc;
345{
346 int i, len;
347 char *bp, *host, *src, *suser, *thost, *tuser;
348
349 *targ++ = 0;
350 if (*targ == 0)
351 targ = ".";
352
353 if ((thost = strchr(argv[argc - 1], '@'))) {
354 /* user@host */
355 *thost++ = 0;
356 tuser = argv[argc - 1];
357 if (*tuser == '\0')
358 tuser = NULL;
359 else if (!okname(tuser))
360 exit(1);
361 } else {
362 thost = argv[argc - 1];
363 tuser = NULL;
364 }
365
366 for (i = 0; i < argc - 1; i++) {
367 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100368 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369 *src++ = 0;
370 if (*src == 0)
371 src = ".";
372 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000373 len = strlen(ssh_program) + strlen(argv[i]) +
374 strlen(src) + (tuser ? strlen(tuser) : 0) +
375 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100376 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377 if (host) {
378 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100379 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380 suser = argv[i];
381 if (*suser == '\0')
382 suser = pwd->pw_name;
383 else if (!okname(suser))
384 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000385 snprintf(bp, len,
386 "%s%s -x -o'FallBackToRsh no' -n "
387 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000388 ssh_program, verbose_mode ? " -v" : "",
389 suser, host, cmd, src,
390 tuser ? tuser : "", tuser ? "@" : "",
391 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100392 } else {
393 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000394 snprintf(bp, len,
395 "exec %s%s -x -o'FallBackToRsh no' -n %s "
396 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000397 ssh_program, verbose_mode ? " -v" : "",
398 host, cmd, src,
399 tuser ? tuser : "", tuser ? "@" : "",
400 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100401 }
Damien Miller95def091999-11-25 00:26:21 +1100402 if (verbose_mode)
403 fprintf(stderr, "Executing: %s\n", bp);
404 (void) system(bp);
405 (void) xfree(bp);
406 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407 if (remin == -1) {
408 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100409 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000410 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100411 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000412 if (do_cmd(host, tuser, bp, &remin,
413 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100414 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415 if (response() < 0)
416 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100417 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418 }
Damien Miller95def091999-11-25 00:26:21 +1100419 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420 }
421 }
422}
423
424void
425tolocal(argc, argv)
426 int argc;
427 char *argv[];
428{
429 int i, len;
430 char *bp, *host, *src, *suser;
431
432 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100433 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000435 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000437 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000438 iamrecursive ? " -r" : "", pflag ? " -p" : "",
439 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100440 if (verbose_mode)
441 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442 if (system(bp))
443 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100444 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 continue;
446 }
447 *src++ = 0;
448 if (*src == 0)
449 src = ".";
450 if ((host = strchr(argv[i], '@')) == NULL) {
451 host = argv[i];
452 suser = NULL;
453 } else {
454 *host++ = 0;
455 suser = argv[i];
456 if (*suser == '\0')
457 suser = pwd->pw_name;
458 else if (!okname(suser))
459 continue;
460 }
Damien Miller34132e52000-01-14 15:45:46 +1100461 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100463 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000464 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000465 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100466 (void) xfree(bp);
467 ++errs;
468 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 }
Damien Miller95def091999-11-25 00:26:21 +1100470 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100472 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 remin = remout = -1;
474 }
475}
476
477void
478source(argc, argv)
479 int argc;
480 char *argv[];
481{
482 struct stat stb;
483 static BUF buffer;
484 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000485 off_t i, amt, result;
486 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000488 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
490 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100491 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000493 len = strlen(name);
494 while (len > 1 && name[len-1] == '/')
495 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496 if ((fd = open(name, O_RDONLY, 0)) < 0)
497 goto syserr;
498 if (fstat(fd, &stb) < 0) {
499syserr: run_err("%s: %s", name, strerror(errno));
500 goto next;
501 }
502 switch (stb.st_mode & S_IFMT) {
503 case S_IFREG:
504 break;
505 case S_IFDIR:
506 if (iamrecursive) {
507 rsource(name, &stb);
508 goto next;
509 }
510 /* FALLTHROUGH */
511 default:
512 run_err("%s: not a regular file", name);
513 goto next;
514 }
515 if ((last = strrchr(name, '/')) == NULL)
516 last = name;
517 else
518 ++last;
519 curfile = last;
520 if (pflag) {
521 /*
522 * Make it compatible with possible future
523 * versions expecting microseconds.
524 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000525 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000526 (u_long) stb.st_mtime,
527 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000528 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000529 if (response() < 0)
530 goto next;
531 }
532#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100533#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000534 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000535 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000536 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100537#else
538 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000539 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100540 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000541 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100542#endif
543
Damien Miller95def091999-11-25 00:26:21 +1100544 if (verbose_mode) {
545 fprintf(stderr, "Sending file modes: %s", buf);
546 fflush(stderr);
547 }
Damien Miller35dabd02000-05-01 21:10:33 +1000548 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549 if (response() < 0)
550 goto next;
551 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100552next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553 continue;
554 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 if (showprogress) {
556 totalbytes = stb.st_size;
557 progressmeter(-1);
558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 /* Keep writing after an error so that we stay sync'd up. */
560 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
561 amt = bp->cnt;
562 if (i + amt > stb.st_size)
563 amt = stb.st_size - i;
564 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000565 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 if (result != amt)
567 haderr = result >= 0 ? EIO : errno;
568 }
569 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000570 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571 else {
Damien Miller864ea591999-12-15 11:04:25 +1100572 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 if (result != amt)
574 haderr = result >= 0 ? EIO : errno;
575 statbytes += result;
576 }
577 }
Damien Miller95def091999-11-25 00:26:21 +1100578 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 progressmeter(1);
580
581 if (close(fd) < 0 && !haderr)
582 haderr = errno;
583 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000584 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 else
586 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100587 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 }
589}
590
591void
592rsource(name, statp)
593 char *name;
594 struct stat *statp;
595{
596 DIR *dirp;
597 struct dirent *dp;
598 char *last, *vect[1], path[1100];
599
600 if (!(dirp = opendir(name))) {
601 run_err("%s: %s", name, strerror(errno));
602 return;
603 }
604 last = strrchr(name, '/');
605 if (last == 0)
606 last = name;
607 else
608 last++;
609 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000610 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000611 (u_long) statp->st_mtime,
612 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000613 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614 if (response() < 0) {
615 closedir(dirp);
616 return;
617 }
618 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000619 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000620 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100621 if (verbose_mode)
622 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000623 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 if (response() < 0) {
625 closedir(dirp);
626 return;
627 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000628 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000629 if (dp->d_ino == 0)
630 continue;
631 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
632 continue;
633 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
634 run_err("%s/%s: name too long", name, dp->d_name);
635 continue;
636 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000637 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638 vect[0] = path;
639 source(1, vect);
640 }
Damien Miller95def091999-11-25 00:26:21 +1100641 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000642 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100643 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644}
645
646void
647sink(argc, argv)
648 int argc;
649 char *argv[];
650{
651 static BUF buffer;
652 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100653 enum {
654 YES, NO, DISPLAYED
655 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000656 BUF *bp;
657 off_t i, j;
658 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100659 off_t size;
660 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100662 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663
Ben Lindstromf719a202001-04-14 23:14:22 +0000664#define atime tv[0]
665#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000666#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667
668 setimes = targisdir = 0;
669 mask = umask(0);
670 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100671 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672 if (argc != 1) {
673 run_err("ambiguous target");
674 exit(1);
675 }
676 targ = *argv;
677 if (targetshouldbedirectory)
678 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100679
Damien Miller35dabd02000-05-01 21:10:33 +1000680 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
682 targisdir = 1;
683 for (first = 1;; first = 0) {
684 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000685 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686 return;
687 if (*cp++ == '\n')
688 SCREWUP("unexpected <newline>");
689 do {
Damien Millere247cc42000-05-07 12:03:14 +1000690 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 SCREWUP("lost connection");
692 *cp++ = ch;
693 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
694 *cp = 0;
695
696 if (buf[0] == '\01' || buf[0] == '\02') {
697 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000698 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000699 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700 if (buf[0] == '\02')
701 exit(1);
702 ++errs;
703 continue;
704 }
705 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000706 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707 return;
708 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 if (ch == '\n')
710 *--cp = 0;
711
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712 cp = buf;
713 if (*cp == 'T') {
714 setimes++;
715 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000716 mtime.tv_sec = strtol(cp, &cp, 10);
717 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000719 mtime.tv_usec = strtol(cp, &cp, 10);
720 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000722 atime.tv_sec = strtol(cp, &cp, 10);
723 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000725 atime.tv_usec = strtol(cp, &cp, 10);
726 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000728 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729 continue;
730 }
731 if (*cp != 'C' && *cp != 'D') {
732 /*
733 * Check for the case "rcp remote:foo\* local:bar".
734 * In this case, the line "No match." can be returned
735 * by the shell before the rcp command on the remote is
736 * executed so the ^Aerror_message convention isn't
737 * followed.
738 */
739 if (first) {
740 run_err("%s", cp);
741 exit(1);
742 }
743 SCREWUP("expected control record");
744 }
745 mode = 0;
746 for (++cp; cp < buf + 5; cp++) {
747 if (*cp < '0' || *cp > '7')
748 SCREWUP("bad mode");
749 mode = (mode << 3) | (*cp - '0');
750 }
751 if (*cp++ != ' ')
752 SCREWUP("mode not delimited");
753
Ben Lindstrombd472262001-03-29 00:39:55 +0000754 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755 size = size * 10 + (*cp++ - '0');
756 if (*cp++ != ' ')
757 SCREWUP("size not delimited");
758 if (targisdir) {
759 static char *namebuf;
760 static int cursize;
761 size_t need;
762
763 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000764 if (need > cursize) {
765 if (namebuf)
766 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100767 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000768 cursize = need;
769 }
770 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000771 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000772 np = namebuf;
773 } else
774 np = targ;
775 curfile = cp;
776 exists = stat(np, &stb) == 0;
777 if (buf[0] == 'D') {
778 int mod_flag = pflag;
779 if (exists) {
780 if (!S_ISDIR(stb.st_mode)) {
781 errno = ENOTDIR;
782 goto bad;
783 }
784 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100785 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786 } else {
Damien Miller95def091999-11-25 00:26:21 +1100787 /* Handle copying from a read-only
788 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789 mod_flag = 1;
790 if (mkdir(np, mode | S_IRWXU) < 0)
791 goto bad;
792 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000793 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794 sink(1, vect);
795 if (setimes) {
796 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100797 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100798 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100799 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800 }
801 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100802 (void) chmod(vect[0], mode);
803 if (vect[0])
804 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805 continue;
806 }
807 omode = mode;
808 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000809 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810bad: run_err("%s: %s", np, strerror(errno));
811 continue;
812 }
Damien Miller35dabd02000-05-01 21:10:33 +1000813 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100815 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816 continue;
817 }
818 cp = bp->buf;
819 wrerr = NO;
820
821 if (showprogress) {
822 totalbytes = size;
823 progressmeter(-1);
824 }
825 statbytes = 0;
826 for (count = i = 0; i < size; i += 4096) {
827 amt = 4096;
828 if (i + amt > size)
829 amt = size - i;
830 count += amt;
831 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100832 j = read(remin, cp, amt);
833 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
834 continue;
835 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000836 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000837 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838 exit(1);
839 }
840 amt -= j;
841 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100842 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843 } while (amt > 0);
844 if (count == bp->cnt) {
845 /* Keep reading so we stay sync'd up. */
846 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000847 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 if (j != count) {
849 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100850 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851 }
852 }
853 count = 0;
854 cp = bp->buf;
855 }
856 }
857 if (showprogress)
858 progressmeter(1);
859 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000860 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100862 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864 if (ftruncate(ofd, size)) {
865 run_err("%s: truncate: %s", np, strerror(errno));
866 wrerr = DISPLAYED;
867 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 if (pflag) {
869 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100870#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100872#else /* HAVE_FCHMOD */
873 if (chmod(np, omode))
874#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000876 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877 } else {
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 & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100881#else /* HAVE_FCHMOD */
882 if (chmod(np, omode & ~mask))
883#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000885 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 }
Damien Millerbe484b52000-07-15 14:14:16 +1000887 if (close(ofd) == -1) {
888 wrerr = YES;
889 wrerrno = errno;
890 }
Damien Miller95def091999-11-25 00:26:21 +1100891 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892 if (setimes && wrerr == NO) {
893 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100894 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000896 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897 wrerr = DISPLAYED;
898 }
899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 case YES:
902 run_err("%s: %s", np, strerror(wrerrno));
903 break;
904 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000905 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 break;
907 case DISPLAYED:
908 break;
909 }
910 }
911screwup:
912 run_err("protocol error: %s", why);
913 exit(1);
914}
915
916int
917response()
918{
919 char ch, *cp, resp, rbuf[2048];
920
Damien Millere247cc42000-05-07 12:03:14 +1000921 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 lostconn(0);
923
924 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100925 switch (resp) {
926 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927 return (0);
928 default:
929 *cp++ = resp;
930 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100931 case 1: /* error, followed by error msg */
932 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 do {
Damien Millere247cc42000-05-07 12:03:14 +1000934 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935 lostconn(0);
936 *cp++ = ch;
937 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
938
939 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000940 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941 ++errs;
942 if (resp == 1)
943 return (-1);
944 exit(1);
945 }
946 /* NOTREACHED */
947}
948
949void
950usage()
951{
Damien Millerad833b32000-08-23 10:46:23 +1000952 (void) fprintf(stderr, "usage: scp "
Ben Lindstromff2618c2001-03-29 00:43:54 +0000953 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n"
954 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955 exit(1);
956}
957
958void
Damien Miller95def091999-11-25 00:26:21 +1100959run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960{
961 static FILE *fp;
962 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
964 ++errs;
965 if (fp == NULL && !(fp = fdopen(remout, "w")))
966 return;
Damien Miller95def091999-11-25 00:26:21 +1100967 (void) fprintf(fp, "%c", 0x01);
968 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100969 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100970 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100971 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100972 (void) fprintf(fp, "\n");
973 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller95def091999-11-25 00:26:21 +1100975 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100976 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100977 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100978 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100979 fprintf(stderr, "\n");
980 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981}
982
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983void
984verifydir(cp)
985 char *cp;
986{
987 struct stat stb;
988
989 if (!stat(cp, &stb)) {
990 if (S_ISDIR(stb.st_mode))
991 return;
992 errno = ENOTDIR;
993 }
994 run_err("%s: %s", cp, strerror(errno));
995 exit(1);
996}
997
998int
999okname(cp0)
1000 char *cp0;
1001{
1002 int c;
1003 char *cp;
1004
1005 cp = cp0;
1006 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001007 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008 if (c & 0200)
1009 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001010 if (!isalpha(c) && !isdigit(c) &&
1011 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 goto bad;
1013 } while (*++cp);
1014 return (1);
1015
Damien Miller98c7ad62000-03-09 21:27:49 +11001016bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017 return (0);
1018}
1019
1020BUF *
1021allocbuf(bp, fd, blksize)
1022 BUF *bp;
1023 int fd, blksize;
1024{
1025 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001026#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027 struct stat stb;
1028
1029 if (fstat(fd, &stb) < 0) {
1030 run_err("fstat: %s", strerror(errno));
1031 return (0);
1032 }
Damien Miller95def091999-11-25 00:26:21 +11001033 if (stb.st_blksize == 0)
1034 size = blksize;
1035 else
1036 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001037 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001038#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001039 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001040#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041 if (bp->cnt >= size)
1042 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001043 if (bp->buf == NULL)
1044 bp->buf = xmalloc(size);
1045 else
1046 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047 bp->cnt = size;
1048 return (bp);
1049}
1050
1051void
1052lostconn(signo)
1053 int signo;
1054{
1055 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001056 write(STDERR_FILENO, "lost connection\n", 16);
1057 if (signo)
1058 _exit(1);
1059 else
1060 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061}
1062
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Ben Lindstrombba81212001-06-25 05:01:22 +00001064static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065alarmtimer(int wait)
1066{
Damien Miller95def091999-11-25 00:26:21 +11001067 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068
Damien Miller95def091999-11-25 00:26:21 +11001069 itv.it_value.tv_sec = wait;
1070 itv.it_value.tv_usec = 0;
1071 itv.it_interval = itv.it_value;
1072 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073}
1074
Ben Lindstrombba81212001-06-25 05:01:22 +00001075static void
Damien Miller7684ee12000-03-17 23:40:15 +11001076updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077{
1078 int save_errno = errno;
1079
1080 progressmeter(0);
1081 errno = save_errno;
1082}
1083
Ben Lindstrombba81212001-06-25 05:01:22 +00001084static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001085foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001086{
1087 static pid_t pgrp = -1;
1088 int ctty_pgrp;
1089
1090 if (pgrp == -1)
1091 pgrp = getpgrp();
1092
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001093#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001094 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1095 ctty_pgrp == pgrp);
1096#else
Damien Miller95def091999-11-25 00:26:21 +11001097 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1098 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001099#endif
Damien Miller81428f91999-11-18 09:28:11 +11001100}
1101
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102void
1103progressmeter(int flag)
1104{
1105 static const char prefixes[] = " KMGTP";
1106 static struct timeval lastupdate;
1107 static off_t lastsize;
1108 struct timeval now, td, wait;
1109 off_t cursize, abbrevsize;
1110 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001111 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112 char buf[256];
1113
1114 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001115 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116 lastupdate = start;
1117 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001118 }
Damien Miller81428f91999-11-18 09:28:11 +11001119 if (foregroundproc() == 0)
1120 return;
1121
Damien Miller95def091999-11-25 00:26:21 +11001122 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001124 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001125 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126 ratio = MAX(ratio, 0);
1127 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001128 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129 ratio = 100;
1130
Damien Miller95def091999-11-25 00:26:21 +11001131 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132
1133 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001134 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135 if (barlength > 0) {
1136 i = barlength * ratio / 100;
1137 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001138 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140 i = 0;
1141 abbrevsize = cursize;
1142 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1143 i++;
1144 abbrevsize >>= 10;
1145 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001146 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001147 (unsigned long) abbrevsize, prefixes[i],
1148 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149
1150 timersub(&now, &lastupdate, &wait);
1151 if (cursize > lastsize) {
1152 lastupdate = now;
1153 lastsize = cursize;
1154 if (wait.tv_sec >= STALLTIME) {
1155 start.tv_sec += wait.tv_sec;
1156 start.tv_usec += wait.tv_usec;
1157 }
1158 wait.tv_sec = 0;
1159 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 timersub(&now, &start, &td);
1161 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1162
Kevin Steves7d00ba42000-12-15 23:03:10 +00001163 if (flag != 1 &&
1164 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001166 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 } else if (wait.tv_sec >= STALLTIME) {
1168 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001169 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001171 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001172 remaining = (int)(totalbytes / (statbytes / elapsed) -
1173 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001174 else
1175 remaining = elapsed;
1176
Damien Miller01ab4a21999-10-28 15:23:30 +10001177 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178 if (i)
1179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001180 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 else
1182 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001183 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 i = remaining % 3600;
1185 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001186 "%02d:%02d%s", i / 60, i % 60,
1187 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188 }
1189 atomicio(write, fileno(stdout), buf, strlen(buf));
1190
1191 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001192 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 alarmtimer(1);
1194 } else if (flag == 1) {
1195 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001196 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197 statbytes = 0;
1198 }
1199}
1200
1201int
1202getttywidth(void)
1203{
1204 struct winsize winsize;
1205
1206 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001207 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001208 else
Damien Miller95def091999-11-25 00:26:21 +11001209 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210}