blob: 89f05ad96ecd9b11b32e821836fa1e88df3f6f39 [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 Lindstrom7bad55b2001-06-05 19:31:41 +000078RCSID("$OpenBSD: scp.c,v 1.71 2001/05/19 16:05:41 markus 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
Damien Miller95def091999-11-25 00:26:21 +1100213int response(void);
214void rsource(char *, struct stat *);
215void sink(int, char *[]);
216void source(int, char *[]);
217void tolocal(int, char *[]);
218void toremote(char *, int, char *[]);
219void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
221int
222main(argc, argv)
223 int argc;
224 char *argv[];
225{
226 int ch, fflag, tflag;
227 char *targ;
228 extern char *optarg;
229 extern int optind;
230
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000231 __progname = get_progname(argv[0]);
232
Damien Miller874d77b2000-10-14 16:23:11 +1100233 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000234 addargs(&args, "ssh"); /* overwritten with ssh_program */
235 addargs(&args, "-x");
236 addargs(&args, "-oFallBackToRsh no");
Damien Miller874d77b2000-10-14 16:23:11 +1100237
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000239 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100240 switch (ch) {
241 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100242 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100243 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100244 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000245 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100246 break;
247 case 'o':
248 case 'c':
249 case 'i':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000250 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100251 break;
252 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000253 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100254 break;
255 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000256 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100257 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258 case 'p':
259 pflag = 1;
260 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261 case 'r':
262 iamrecursive = 1;
263 break;
Damien Millerad833b32000-08-23 10:46:23 +1000264 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100265 ssh_program = xstrdup(optarg);
266 break;
267 case 'v':
268 verbose_mode = 1;
269 break;
270 case 'q':
271 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000272 break;
273
Damien Miller95def091999-11-25 00:26:21 +1100274 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275 case 'd':
276 targetshouldbedirectory = 1;
277 break;
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279 iamremote = 1;
280 fflag = 1;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283 iamremote = 1;
284 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000285#ifdef HAVE_CYGWIN
286 setmode(0, O_BINARY);
287#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289 default:
290 usage();
291 }
292 argc -= optind;
293 argv += optind;
294
295 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100296 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller95def091999-11-25 00:26:21 +1100298 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299 showprogress = 0;
300
301 remin = STDIN_FILENO;
302 remout = STDOUT_FILENO;
303
Kevin Stevesef4eea92001-02-05 12:42:17 +0000304 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100305 /* Follow "protocol", send data. */
306 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307 source(argc, argv);
308 exit(errs != 0);
309 }
Damien Miller95def091999-11-25 00:26:21 +1100310 if (tflag) {
311 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312 sink(argc, argv);
313 exit(errs != 0);
314 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315 if (argc < 2)
316 usage();
317 if (argc > 2)
318 targetshouldbedirectory = 1;
319
320 remin = remout = -1;
321 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000322 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
323 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000324 iamrecursive ? " -r" : "", pflag ? " -p" : "",
325 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
329 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
330 toremote(targ, argc, argv);
331 else {
Damien Miller95def091999-11-25 00:26:21 +1100332 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333 if (targetshouldbedirectory)
334 verifydir(argv[argc - 1]);
335 }
336 exit(errs != 0);
337}
338
339void
340toremote(targ, argc, argv)
341 char *targ, *argv[];
342 int argc;
343{
344 int i, len;
345 char *bp, *host, *src, *suser, *thost, *tuser;
346
347 *targ++ = 0;
348 if (*targ == 0)
349 targ = ".";
350
351 if ((thost = strchr(argv[argc - 1], '@'))) {
352 /* user@host */
353 *thost++ = 0;
354 tuser = argv[argc - 1];
355 if (*tuser == '\0')
356 tuser = NULL;
357 else if (!okname(tuser))
358 exit(1);
359 } else {
360 thost = argv[argc - 1];
361 tuser = NULL;
362 }
363
364 for (i = 0; i < argc - 1; i++) {
365 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100366 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367 *src++ = 0;
368 if (*src == 0)
369 src = ".";
370 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000371 len = strlen(ssh_program) + strlen(argv[i]) +
372 strlen(src) + (tuser ? strlen(tuser) : 0) +
373 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100374 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375 if (host) {
376 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100377 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378 suser = argv[i];
379 if (*suser == '\0')
380 suser = pwd->pw_name;
381 else if (!okname(suser))
382 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000383 snprintf(bp, len,
384 "%s%s -x -o'FallBackToRsh no' -n "
385 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000386 ssh_program, verbose_mode ? " -v" : "",
387 suser, host, cmd, src,
388 tuser ? tuser : "", tuser ? "@" : "",
389 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100390 } else {
391 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000392 snprintf(bp, len,
393 "exec %s%s -x -o'FallBackToRsh no' -n %s "
394 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000395 ssh_program, verbose_mode ? " -v" : "",
396 host, cmd, src,
397 tuser ? tuser : "", tuser ? "@" : "",
398 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100399 }
Damien Miller95def091999-11-25 00:26:21 +1100400 if (verbose_mode)
401 fprintf(stderr, "Executing: %s\n", bp);
402 (void) system(bp);
403 (void) xfree(bp);
404 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405 if (remin == -1) {
406 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100407 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000408 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100409 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000410 if (do_cmd(host, tuser, bp, &remin,
411 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100412 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413 if (response() < 0)
414 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100415 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416 }
Damien Miller95def091999-11-25 00:26:21 +1100417 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418 }
419 }
420}
421
422void
423tolocal(argc, argv)
424 int argc;
425 char *argv[];
426{
427 int i, len;
428 char *bp, *host, *src, *suser;
429
430 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100431 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000433 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000435 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000436 iamrecursive ? " -r" : "", pflag ? " -p" : "",
437 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100438 if (verbose_mode)
439 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440 if (system(bp))
441 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100442 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 continue;
444 }
445 *src++ = 0;
446 if (*src == 0)
447 src = ".";
448 if ((host = strchr(argv[i], '@')) == NULL) {
449 host = argv[i];
450 suser = NULL;
451 } else {
452 *host++ = 0;
453 suser = argv[i];
454 if (*suser == '\0')
455 suser = pwd->pw_name;
456 else if (!okname(suser))
457 continue;
458 }
Damien Miller34132e52000-01-14 15:45:46 +1100459 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100461 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000462 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000463 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100464 (void) xfree(bp);
465 ++errs;
466 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 }
Damien Miller95def091999-11-25 00:26:21 +1100468 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100470 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 remin = remout = -1;
472 }
473}
474
475void
476source(argc, argv)
477 int argc;
478 char *argv[];
479{
480 struct stat stb;
481 static BUF buffer;
482 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000483 off_t i, amt, result;
484 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000486 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
488 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100489 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000491 len = strlen(name);
492 while (len > 1 && name[len-1] == '/')
493 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 if ((fd = open(name, O_RDONLY, 0)) < 0)
495 goto syserr;
496 if (fstat(fd, &stb) < 0) {
497syserr: run_err("%s: %s", name, strerror(errno));
498 goto next;
499 }
500 switch (stb.st_mode & S_IFMT) {
501 case S_IFREG:
502 break;
503 case S_IFDIR:
504 if (iamrecursive) {
505 rsource(name, &stb);
506 goto next;
507 }
508 /* FALLTHROUGH */
509 default:
510 run_err("%s: not a regular file", name);
511 goto next;
512 }
513 if ((last = strrchr(name, '/')) == NULL)
514 last = name;
515 else
516 ++last;
517 curfile = last;
518 if (pflag) {
519 /*
520 * Make it compatible with possible future
521 * versions expecting microseconds.
522 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000523 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000524 (u_long) stb.st_mtime,
525 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000526 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527 if (response() < 0)
528 goto next;
529 }
530#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100531#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000532 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000533 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000534 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100535#else
536 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000537 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100538 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000539 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100540#endif
541
Damien Miller95def091999-11-25 00:26:21 +1100542 if (verbose_mode) {
543 fprintf(stderr, "Sending file modes: %s", buf);
544 fflush(stderr);
545 }
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 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100550next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551 continue;
552 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553 if (showprogress) {
554 totalbytes = stb.st_size;
555 progressmeter(-1);
556 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 /* Keep writing after an error so that we stay sync'd up. */
558 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
559 amt = bp->cnt;
560 if (i + amt > stb.st_size)
561 amt = stb.st_size - i;
562 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000563 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 if (result != amt)
565 haderr = result >= 0 ? EIO : errno;
566 }
567 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000568 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 else {
Damien Miller864ea591999-12-15 11:04:25 +1100570 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571 if (result != amt)
572 haderr = result >= 0 ? EIO : errno;
573 statbytes += result;
574 }
575 }
Damien Miller95def091999-11-25 00:26:21 +1100576 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 progressmeter(1);
578
579 if (close(fd) < 0 && !haderr)
580 haderr = errno;
581 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000582 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 else
584 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100585 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586 }
587}
588
589void
590rsource(name, statp)
591 char *name;
592 struct stat *statp;
593{
594 DIR *dirp;
595 struct dirent *dp;
596 char *last, *vect[1], path[1100];
597
598 if (!(dirp = opendir(name))) {
599 run_err("%s: %s", name, strerror(errno));
600 return;
601 }
602 last = strrchr(name, '/');
603 if (last == 0)
604 last = name;
605 else
606 last++;
607 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000608 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000609 (u_long) statp->st_mtime,
610 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000611 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612 if (response() < 0) {
613 closedir(dirp);
614 return;
615 }
616 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000617 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000618 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100619 if (verbose_mode)
620 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000621 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622 if (response() < 0) {
623 closedir(dirp);
624 return;
625 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000626 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627 if (dp->d_ino == 0)
628 continue;
629 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
630 continue;
631 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
632 run_err("%s/%s: name too long", name, dp->d_name);
633 continue;
634 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000635 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636 vect[0] = path;
637 source(1, vect);
638 }
Damien Miller95def091999-11-25 00:26:21 +1100639 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000640 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100641 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642}
643
644void
645sink(argc, argv)
646 int argc;
647 char *argv[];
648{
649 static BUF buffer;
650 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100651 enum {
652 YES, NO, DISPLAYED
653 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654 BUF *bp;
655 off_t i, j;
656 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100657 off_t size;
658 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100660 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Ben Lindstromf719a202001-04-14 23:14:22 +0000662#define atime tv[0]
663#define mtime tv[1]
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664#define SCREWUP(str) { why = str; goto screwup; }
665
666 setimes = targisdir = 0;
667 mask = umask(0);
668 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100669 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670 if (argc != 1) {
671 run_err("ambiguous target");
672 exit(1);
673 }
674 targ = *argv;
675 if (targetshouldbedirectory)
676 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100677
Damien Miller35dabd02000-05-01 21:10:33 +1000678 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
680 targisdir = 1;
681 for (first = 1;; first = 0) {
682 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000683 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684 return;
685 if (*cp++ == '\n')
686 SCREWUP("unexpected <newline>");
687 do {
Damien Millere247cc42000-05-07 12:03:14 +1000688 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689 SCREWUP("lost connection");
690 *cp++ = ch;
691 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
692 *cp = 0;
693
694 if (buf[0] == '\01' || buf[0] == '\02') {
695 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000696 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000697 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698 if (buf[0] == '\02')
699 exit(1);
700 ++errs;
701 continue;
702 }
703 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000704 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705 return;
706 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707 if (ch == '\n')
708 *--cp = 0;
709
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 cp = buf;
711 if (*cp == 'T') {
712 setimes++;
713 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000714 mtime.tv_sec = strtol(cp, &cp, 10);
715 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000717 mtime.tv_usec = strtol(cp, &cp, 10);
718 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000720 atime.tv_sec = strtol(cp, &cp, 10);
721 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000723 atime.tv_usec = strtol(cp, &cp, 10);
724 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000726 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 continue;
728 }
729 if (*cp != 'C' && *cp != 'D') {
730 /*
731 * Check for the case "rcp remote:foo\* local:bar".
732 * In this case, the line "No match." can be returned
733 * by the shell before the rcp command on the remote is
734 * executed so the ^Aerror_message convention isn't
735 * followed.
736 */
737 if (first) {
738 run_err("%s", cp);
739 exit(1);
740 }
741 SCREWUP("expected control record");
742 }
743 mode = 0;
744 for (++cp; cp < buf + 5; cp++) {
745 if (*cp < '0' || *cp > '7')
746 SCREWUP("bad mode");
747 mode = (mode << 3) | (*cp - '0');
748 }
749 if (*cp++ != ' ')
750 SCREWUP("mode not delimited");
751
Ben Lindstrombd472262001-03-29 00:39:55 +0000752 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753 size = size * 10 + (*cp++ - '0');
754 if (*cp++ != ' ')
755 SCREWUP("size not delimited");
756 if (targisdir) {
757 static char *namebuf;
758 static int cursize;
759 size_t need;
760
761 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000762 if (need > cursize) {
763 if (namebuf)
764 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100765 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000766 cursize = need;
767 }
768 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000769 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770 np = namebuf;
771 } else
772 np = targ;
773 curfile = cp;
774 exists = stat(np, &stb) == 0;
775 if (buf[0] == 'D') {
776 int mod_flag = pflag;
777 if (exists) {
778 if (!S_ISDIR(stb.st_mode)) {
779 errno = ENOTDIR;
780 goto bad;
781 }
782 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100783 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784 } else {
Damien Miller95def091999-11-25 00:26:21 +1100785 /* Handle copying from a read-only
786 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787 mod_flag = 1;
788 if (mkdir(np, mode | S_IRWXU) < 0)
789 goto bad;
790 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000791 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792 sink(1, vect);
793 if (setimes) {
794 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100795 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100796 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100797 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798 }
799 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100800 (void) chmod(vect[0], mode);
801 if (vect[0])
802 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803 continue;
804 }
805 omode = mode;
806 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000807 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808bad: run_err("%s: %s", np, strerror(errno));
809 continue;
810 }
Damien Miller35dabd02000-05-01 21:10:33 +1000811 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100813 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814 continue;
815 }
816 cp = bp->buf;
817 wrerr = NO;
818
819 if (showprogress) {
820 totalbytes = size;
821 progressmeter(-1);
822 }
823 statbytes = 0;
824 for (count = i = 0; i < size; i += 4096) {
825 amt = 4096;
826 if (i + amt > size)
827 amt = size - i;
828 count += amt;
829 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100830 j = read(remin, cp, amt);
831 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
832 continue;
833 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000835 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000836 exit(1);
837 }
838 amt -= j;
839 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100840 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 } while (amt > 0);
842 if (count == bp->cnt) {
843 /* Keep reading so we stay sync'd up. */
844 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000845 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846 if (j != count) {
847 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100848 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849 }
850 }
851 count = 0;
852 cp = bp->buf;
853 }
854 }
855 if (showprogress)
856 progressmeter(1);
857 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000858 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100860 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862 if (ftruncate(ofd, size)) {
863 run_err("%s: truncate: %s", np, strerror(errno));
864 wrerr = DISPLAYED;
865 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 if (pflag) {
867 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100868#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100870#else /* HAVE_FCHMOD */
871 if (chmod(np, omode))
872#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000874 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 } else {
876 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100877#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100879#else /* HAVE_FCHMOD */
880 if (chmod(np, omode & ~mask))
881#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000883 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 }
Damien Millerbe484b52000-07-15 14:14:16 +1000885 if (close(ofd) == -1) {
886 wrerr = YES;
887 wrerrno = errno;
888 }
Damien Miller95def091999-11-25 00:26:21 +1100889 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890 if (setimes && wrerr == NO) {
891 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100892 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000894 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 wrerr = DISPLAYED;
896 }
897 }
Damien Miller95def091999-11-25 00:26:21 +1100898 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899 case YES:
900 run_err("%s: %s", np, strerror(wrerrno));
901 break;
902 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000903 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 break;
905 case DISPLAYED:
906 break;
907 }
908 }
909screwup:
910 run_err("protocol error: %s", why);
911 exit(1);
912}
913
914int
915response()
916{
917 char ch, *cp, resp, rbuf[2048];
918
Damien Millere247cc42000-05-07 12:03:14 +1000919 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920 lostconn(0);
921
922 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100923 switch (resp) {
924 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925 return (0);
926 default:
927 *cp++ = resp;
928 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100929 case 1: /* error, followed by error msg */
930 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931 do {
Damien Millere247cc42000-05-07 12:03:14 +1000932 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 lostconn(0);
934 *cp++ = ch;
935 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
936
937 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000938 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939 ++errs;
940 if (resp == 1)
941 return (-1);
942 exit(1);
943 }
944 /* NOTREACHED */
945}
946
947void
948usage()
949{
Damien Millerad833b32000-08-23 10:46:23 +1000950 (void) fprintf(stderr, "usage: scp "
Ben Lindstromff2618c2001-03-29 00:43:54 +0000951 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n"
952 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953 exit(1);
954}
955
956void
Damien Miller95def091999-11-25 00:26:21 +1100957run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958{
959 static FILE *fp;
960 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961
962 ++errs;
963 if (fp == NULL && !(fp = fdopen(remout, "w")))
964 return;
Damien Miller95def091999-11-25 00:26:21 +1100965 (void) fprintf(fp, "%c", 0x01);
966 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100967 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100968 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100969 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100970 (void) fprintf(fp, "\n");
971 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972
Damien Miller95def091999-11-25 00:26:21 +1100973 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100974 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100975 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100976 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100977 fprintf(stderr, "\n");
978 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979}
980
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981void
982verifydir(cp)
983 char *cp;
984{
985 struct stat stb;
986
987 if (!stat(cp, &stb)) {
988 if (S_ISDIR(stb.st_mode))
989 return;
990 errno = ENOTDIR;
991 }
992 run_err("%s: %s", cp, strerror(errno));
993 exit(1);
994}
995
996int
997okname(cp0)
998 char *cp0;
999{
1000 int c;
1001 char *cp;
1002
1003 cp = cp0;
1004 do {
1005 c = *cp;
1006 if (c & 0200)
1007 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001008 if (!isalpha(c) && !isdigit(c) &&
1009 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010 goto bad;
1011 } while (*++cp);
1012 return (1);
1013
Damien Miller98c7ad62000-03-09 21:27:49 +11001014bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015 return (0);
1016}
1017
1018BUF *
1019allocbuf(bp, fd, blksize)
1020 BUF *bp;
1021 int fd, blksize;
1022{
1023 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001024#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025 struct stat stb;
1026
1027 if (fstat(fd, &stb) < 0) {
1028 run_err("fstat: %s", strerror(errno));
1029 return (0);
1030 }
Damien Miller95def091999-11-25 00:26:21 +11001031 if (stb.st_blksize == 0)
1032 size = blksize;
1033 else
1034 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001035 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001036#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001037 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001038#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039 if (bp->cnt >= size)
1040 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001041 if (bp->buf == NULL)
1042 bp->buf = xmalloc(size);
1043 else
1044 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045 bp->cnt = size;
1046 return (bp);
1047}
1048
1049void
1050lostconn(signo)
1051 int signo;
1052{
1053 if (!iamremote)
1054 fprintf(stderr, "lost connection\n");
1055 exit(1);
1056}
1057
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
1059void
1060alarmtimer(int wait)
1061{
Damien Miller95def091999-11-25 00:26:21 +11001062 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Damien Miller95def091999-11-25 00:26:21 +11001064 itv.it_value.tv_sec = wait;
1065 itv.it_value.tv_usec = 0;
1066 itv.it_interval = itv.it_value;
1067 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068}
1069
1070void
Damien Miller7684ee12000-03-17 23:40:15 +11001071updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072{
1073 int save_errno = errno;
1074
1075 progressmeter(0);
1076 errno = save_errno;
1077}
1078
Damien Miller81428f91999-11-18 09:28:11 +11001079int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001080foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001081{
1082 static pid_t pgrp = -1;
1083 int ctty_pgrp;
1084
1085 if (pgrp == -1)
1086 pgrp = getpgrp();
1087
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001088#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001089 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1090 ctty_pgrp == pgrp);
1091#else
Damien Miller95def091999-11-25 00:26:21 +11001092 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1093 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001094#endif
Damien Miller81428f91999-11-18 09:28:11 +11001095}
1096
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097void
1098progressmeter(int flag)
1099{
1100 static const char prefixes[] = " KMGTP";
1101 static struct timeval lastupdate;
1102 static off_t lastsize;
1103 struct timeval now, td, wait;
1104 off_t cursize, abbrevsize;
1105 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001106 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107 char buf[256];
1108
1109 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001110 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001111 lastupdate = start;
1112 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001113 }
Damien Miller81428f91999-11-18 09:28:11 +11001114 if (foregroundproc() == 0)
1115 return;
1116
Damien Miller95def091999-11-25 00:26:21 +11001117 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001119 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001120 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121 ratio = MAX(ratio, 0);
1122 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001123 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124 ratio = 100;
1125
Damien Miller95def091999-11-25 00:26:21 +11001126 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
1128 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001129 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130 if (barlength > 0) {
1131 i = barlength * ratio / 100;
1132 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001133 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135 i = 0;
1136 abbrevsize = cursize;
1137 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1138 i++;
1139 abbrevsize >>= 10;
1140 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001141 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001142 (unsigned long) abbrevsize, prefixes[i],
1143 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144
1145 timersub(&now, &lastupdate, &wait);
1146 if (cursize > lastsize) {
1147 lastupdate = now;
1148 lastsize = cursize;
1149 if (wait.tv_sec >= STALLTIME) {
1150 start.tv_sec += wait.tv_sec;
1151 start.tv_usec += wait.tv_usec;
1152 }
1153 wait.tv_sec = 0;
1154 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 timersub(&now, &start, &td);
1156 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1157
Kevin Steves7d00ba42000-12-15 23:03:10 +00001158 if (flag != 1 &&
1159 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001161 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 } else if (wait.tv_sec >= STALLTIME) {
1163 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001164 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001166 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001167 remaining = (int)(totalbytes / (statbytes / elapsed) -
1168 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001169 else
1170 remaining = elapsed;
1171
Damien Miller01ab4a21999-10-28 15:23:30 +10001172 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 if (i)
1174 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001175 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176 else
1177 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001178 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001179 i = remaining % 3600;
1180 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001181 "%02d:%02d%s", i / 60, i % 60,
1182 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 }
1184 atomicio(write, fileno(stdout), buf, strlen(buf));
1185
1186 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001187 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188 alarmtimer(1);
1189 } else if (flag == 1) {
1190 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001191 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 statbytes = 0;
1193 }
1194}
1195
1196int
1197getttywidth(void)
1198{
1199 struct winsize winsize;
1200
1201 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001202 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 else
Damien Miller95def091999-11-25 00:26:21 +11001204 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205}