blob: 1fab48385688c1289b6c8ff473af3c699d91c11a [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 Lindstromde3895d2002-04-06 18:29:59 +000078RCSID("$OpenBSD: scp.c,v 1.88 2002/04/06 18:24:09 mouring 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
Ben Lindstrom958d9f62001-08-06 22:48:19 +000094/* alarm() interval for updating progress meter */
95#define PROGRESSTIME 1
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* Visual statistics about files as they are transferred. */
98void progressmeter(int);
99
100/* Returns width of the terminal (for progress meter calculations). */
101int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000102int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Ben Lindstrom387c4722001-05-08 20:27:25 +0000104/* Struct for addargs */
105arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +1100106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107/* Time a transfer started. */
108static struct timeval start;
109
110/* Number of bytes of current file transferred so far. */
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000111volatile off_t statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100114off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
116/* Name of current file being transferred. */
117char *curfile;
118
119/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100120int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122/* This is set to zero if the progressmeter is not desired. */
123int showprogress = 1;
124
Damien Millerad833b32000-08-23 10:46:23 +1000125/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000126char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000127
Damien Miller5428f641999-11-25 11:54:57 +1100128/*
129 * This function executes the given command as the specified user on the
130 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
131 * assigns the input and output file descriptors on success.
132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
Damien Miller4af51302000-04-16 11:18:38 +1000134int
Damien Millerad833b32000-08-23 10:46:23 +1000135do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136{
Damien Miller95def091999-11-25 00:26:21 +1100137 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller95def091999-11-25 00:26:21 +1100139 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000140 fprintf(stderr,
141 "Executing: program %s host %s, user %s, command %s\n",
142 ssh_program, host,
143 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller5428f641999-11-25 11:54:57 +1100145 /*
146 * Reserve two descriptors so that the real pipes won't get
147 * descriptors 0 and 1 because that will screw up dup2 below.
148 */
Damien Miller95def091999-11-25 00:26:21 +1100149 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 /* Create a socket pair for communicating with ssh. */
152 if (pipe(pin) < 0)
153 fatal("pipe: %s", strerror(errno));
154 if (pipe(pout) < 0)
155 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 /* Free the reserved descriptors. */
158 close(reserved[0]);
159 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100162 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100163 /* Child. */
164 close(pin[1]);
165 close(pout[0]);
166 dup2(pin[0], 0);
167 dup2(pout[1], 1);
168 close(pin[0]);
169 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Damien Miller874d77b2000-10-14 16:23:11 +1100171 args.list[0] = ssh_program;
172 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000173 addargs(&args, "-l%s", remuser);
174 addargs(&args, "%s", host);
175 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100176
Damien Miller874d77b2000-10-14 16:23:11 +1100177 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000178 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100179 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180 }
Damien Miller95def091999-11-25 00:26:21 +1100181 /* Parent. Close the other side, and return the local side. */
182 close(pin[0]);
183 *fdout = pin[1];
184 close(pout[1]);
185 *fdin = pout[0];
186 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187}
188
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189typedef struct {
190 int cnt;
191 char *buf;
192} BUF;
193
Damien Miller95def091999-11-25 00:26:21 +1100194BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100195void lostconn(int);
196void nospace(void);
197int okname(char *);
198void run_err(const char *,...);
199void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100202uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203int errs, remin, remout;
204int pflag, iamremote, iamrecursive, targetshouldbedirectory;
205
206#define CMDNEEDS 64
207char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
208
Damien Miller95def091999-11-25 00:26:21 +1100209int response(void);
210void rsource(char *, struct stat *);
211void sink(int, char *[]);
212void source(int, char *[]);
213void tolocal(int, char *[]);
214void toremote(char *, int, char *[]);
215void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
217int
218main(argc, argv)
219 int argc;
220 char *argv[];
221{
222 int ch, fflag, tflag;
223 char *targ;
224 extern char *optarg;
225 extern int optind;
226
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000227 __progname = get_progname(argv[0]);
228
Damien Miller874d77b2000-10-14 16:23:11 +1100229 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000230 addargs(&args, "ssh"); /* overwritten with ssh_program */
231 addargs(&args, "-x");
Ben Lindstrom4213c552001-09-12 18:45:09 +0000232 addargs(&args, "-oForwardAgent no");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000233 addargs(&args, "-oFallBackToRsh no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000234 addargs(&args, "-oClearAllForwardings yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100235
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236 fflag = tflag = 0;
Ben Lindstrom1e243242001-09-18 05:38:44 +0000237 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100238 switch (ch) {
239 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100240 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100241 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100242 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000243 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100244 break;
245 case 'o':
246 case 'c':
247 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000248 case 'F':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000249 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100250 break;
251 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000252 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100253 break;
254 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000255 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100256 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257 case 'p':
258 pflag = 1;
259 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260 case 'r':
261 iamrecursive = 1;
262 break;
Damien Millerad833b32000-08-23 10:46:23 +1000263 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100264 ssh_program = xstrdup(optarg);
265 break;
266 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000267 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100268 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 */
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000367 static char *ssh_options =
368 "-x -o'FallBackToRsh no' "
369 "-o'ClearAllForwardings yes'";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370 *src++ = 0;
371 if (*src == 0)
372 src = ".";
373 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000374 len = strlen(ssh_program) + strlen(argv[i]) +
375 strlen(src) + (tuser ? strlen(tuser) : 0) +
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000376 strlen(thost) + strlen(targ) +
377 strlen(ssh_options) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100378 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379 if (host) {
380 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100381 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382 suser = argv[i];
383 if (*suser == '\0')
384 suser = pwd->pw_name;
385 else if (!okname(suser))
386 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000387 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000388 "%s%s %s -n "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000389 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000390 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000391 ssh_options, suser, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000392 tuser ? tuser : "", tuser ? "@" : "",
393 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100394 } else {
395 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000396 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000397 "exec %s%s %s -n %s "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000398 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000399 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000400 ssh_options, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000401 tuser ? tuser : "", tuser ? "@" : "",
402 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100403 }
Damien Miller95def091999-11-25 00:26:21 +1100404 if (verbose_mode)
405 fprintf(stderr, "Executing: %s\n", bp);
406 (void) system(bp);
407 (void) xfree(bp);
408 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409 if (remin == -1) {
410 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100411 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000412 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100413 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000414 if (do_cmd(host, tuser, bp, &remin,
415 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100416 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417 if (response() < 0)
418 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100419 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420 }
Damien Miller95def091999-11-25 00:26:21 +1100421 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 }
423 }
424}
425
426void
427tolocal(argc, argv)
428 int argc;
429 char *argv[];
430{
431 int i, len;
432 char *bp, *host, *src, *suser;
433
434 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100435 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000437 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000439 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000440 iamrecursive ? " -r" : "", pflag ? " -p" : "",
441 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100442 if (verbose_mode)
443 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444 if (system(bp))
445 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100446 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447 continue;
448 }
449 *src++ = 0;
450 if (*src == 0)
451 src = ".";
452 if ((host = strchr(argv[i], '@')) == NULL) {
453 host = argv[i];
454 suser = NULL;
455 } else {
456 *host++ = 0;
457 suser = argv[i];
458 if (*suser == '\0')
459 suser = pwd->pw_name;
460 else if (!okname(suser))
461 continue;
462 }
Damien Miller34132e52000-01-14 15:45:46 +1100463 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100465 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000466 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000467 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100468 (void) xfree(bp);
469 ++errs;
470 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 }
Damien Miller95def091999-11-25 00:26:21 +1100472 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100474 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 remin = remout = -1;
476 }
477}
478
479void
480source(argc, argv)
481 int argc;
482 char *argv[];
483{
484 struct stat stb;
485 static BUF buffer;
486 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000487 off_t i, amt, result;
488 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000490 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
492 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100493 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000495 len = strlen(name);
496 while (len > 1 && name[len-1] == '/')
497 name[--len] = '\0';
Ben Lindstrom3e45e4c2001-10-03 17:30:58 +0000498 if (strchr(name, '\n') != NULL) {
499 run_err("%s: skipping, filename contains a newline",
500 name);
501 goto next;
502 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503 if ((fd = open(name, O_RDONLY, 0)) < 0)
504 goto syserr;
505 if (fstat(fd, &stb) < 0) {
506syserr: run_err("%s: %s", name, strerror(errno));
507 goto next;
508 }
509 switch (stb.st_mode & S_IFMT) {
510 case S_IFREG:
511 break;
512 case S_IFDIR:
513 if (iamrecursive) {
514 rsource(name, &stb);
515 goto next;
516 }
517 /* FALLTHROUGH */
518 default:
519 run_err("%s: not a regular file", name);
520 goto next;
521 }
522 if ((last = strrchr(name, '/')) == NULL)
523 last = name;
524 else
525 ++last;
526 curfile = last;
527 if (pflag) {
528 /*
529 * Make it compatible with possible future
530 * versions expecting microseconds.
531 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000532 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000533 (u_long) stb.st_mtime,
534 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000535 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536 if (response() < 0)
537 goto next;
538 }
539#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100540#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000541 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000542 (u_int) (stb.st_mode & FILEMODEMASK),
Kevin Steves399ec972002-03-05 18:59:45 +0000543 (long long)stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100544#else
545 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000546 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100547 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000548 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100549#endif
Damien Miller95def091999-11-25 00:26:21 +1100550 if (verbose_mode) {
551 fprintf(stderr, "Sending file modes: %s", buf);
552 fflush(stderr);
553 }
Damien Miller35dabd02000-05-01 21:10:33 +1000554 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 if (response() < 0)
556 goto next;
557 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100558next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 continue;
560 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 if (showprogress) {
562 totalbytes = stb.st_size;
563 progressmeter(-1);
564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565 /* Keep writing after an error so that we stay sync'd up. */
566 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
567 amt = bp->cnt;
568 if (i + amt > stb.st_size)
569 amt = stb.st_size - i;
570 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000571 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572 if (result != amt)
573 haderr = result >= 0 ? EIO : errno;
574 }
575 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000576 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 else {
Damien Miller864ea591999-12-15 11:04:25 +1100578 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 if (result != amt)
580 haderr = result >= 0 ? EIO : errno;
581 statbytes += result;
582 }
583 }
Damien Miller95def091999-11-25 00:26:21 +1100584 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 progressmeter(1);
586
587 if (close(fd) < 0 && !haderr)
588 haderr = errno;
589 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000590 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 else
592 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100593 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594 }
595}
596
597void
598rsource(name, statp)
599 char *name;
600 struct stat *statp;
601{
602 DIR *dirp;
603 struct dirent *dp;
604 char *last, *vect[1], path[1100];
605
606 if (!(dirp = opendir(name))) {
607 run_err("%s: %s", name, strerror(errno));
608 return;
609 }
610 last = strrchr(name, '/');
611 if (last == 0)
612 last = name;
613 else
614 last++;
615 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000616 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000617 (u_long) statp->st_mtime,
618 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000619 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000620 if (response() < 0) {
621 closedir(dirp);
622 return;
623 }
624 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000625 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000626 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100627 if (verbose_mode)
628 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000629 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630 if (response() < 0) {
631 closedir(dirp);
632 return;
633 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000634 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635 if (dp->d_ino == 0)
636 continue;
637 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
638 continue;
639 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
640 run_err("%s/%s: name too long", name, dp->d_name);
641 continue;
642 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000643 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000644 vect[0] = path;
645 source(1, vect);
646 }
Damien Miller95def091999-11-25 00:26:21 +1100647 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000648 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100649 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650}
651
652void
653sink(argc, argv)
654 int argc;
655 char *argv[];
656{
657 static BUF buffer;
658 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100659 enum {
660 YES, NO, DISPLAYED
661 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662 BUF *bp;
663 off_t i, j;
664 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100665 off_t size;
666 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100668 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669
Ben Lindstromf719a202001-04-14 23:14:22 +0000670#define atime tv[0]
671#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000672#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673
674 setimes = targisdir = 0;
675 mask = umask(0);
676 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100677 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678 if (argc != 1) {
679 run_err("ambiguous target");
680 exit(1);
681 }
682 targ = *argv;
683 if (targetshouldbedirectory)
684 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100685
Damien Miller35dabd02000-05-01 21:10:33 +1000686 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
688 targisdir = 1;
689 for (first = 1;; first = 0) {
690 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000691 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692 return;
693 if (*cp++ == '\n')
694 SCREWUP("unexpected <newline>");
695 do {
Damien Millere247cc42000-05-07 12:03:14 +1000696 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697 SCREWUP("lost connection");
698 *cp++ = ch;
699 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
700 *cp = 0;
701
702 if (buf[0] == '\01' || buf[0] == '\02') {
703 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000704 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000705 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706 if (buf[0] == '\02')
707 exit(1);
708 ++errs;
709 continue;
710 }
711 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000712 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713 return;
714 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715 if (ch == '\n')
716 *--cp = 0;
717
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718 cp = buf;
719 if (*cp == 'T') {
720 setimes++;
721 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000722 mtime.tv_sec = strtol(cp, &cp, 10);
723 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000725 mtime.tv_usec = strtol(cp, &cp, 10);
726 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000728 atime.tv_sec = strtol(cp, &cp, 10);
729 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000730 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000731 atime.tv_usec = strtol(cp, &cp, 10);
732 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000734 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735 continue;
736 }
737 if (*cp != 'C' && *cp != 'D') {
738 /*
739 * Check for the case "rcp remote:foo\* local:bar".
740 * In this case, the line "No match." can be returned
741 * by the shell before the rcp command on the remote is
742 * executed so the ^Aerror_message convention isn't
743 * followed.
744 */
745 if (first) {
746 run_err("%s", cp);
747 exit(1);
748 }
749 SCREWUP("expected control record");
750 }
751 mode = 0;
752 for (++cp; cp < buf + 5; cp++) {
753 if (*cp < '0' || *cp > '7')
754 SCREWUP("bad mode");
755 mode = (mode << 3) | (*cp - '0');
756 }
757 if (*cp++ != ' ')
758 SCREWUP("mode not delimited");
759
Ben Lindstrombd472262001-03-29 00:39:55 +0000760 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761 size = size * 10 + (*cp++ - '0');
762 if (*cp++ != ' ')
763 SCREWUP("size not delimited");
764 if (targisdir) {
765 static char *namebuf;
766 static int cursize;
767 size_t need;
768
769 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000770 if (need > cursize) {
771 if (namebuf)
772 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100773 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000774 cursize = need;
775 }
776 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +0000777 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778 np = namebuf;
779 } else
780 np = targ;
781 curfile = cp;
782 exists = stat(np, &stb) == 0;
783 if (buf[0] == 'D') {
784 int mod_flag = pflag;
785 if (exists) {
786 if (!S_ISDIR(stb.st_mode)) {
787 errno = ENOTDIR;
788 goto bad;
789 }
790 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100791 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792 } else {
Damien Miller95def091999-11-25 00:26:21 +1100793 /* Handle copying from a read-only
794 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795 mod_flag = 1;
796 if (mkdir(np, mode | S_IRWXU) < 0)
797 goto bad;
798 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000799 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800 sink(1, vect);
801 if (setimes) {
802 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100803 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100804 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100805 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806 }
807 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100808 (void) chmod(vect[0], mode);
809 if (vect[0])
810 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811 continue;
812 }
813 omode = mode;
814 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000815 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816bad: run_err("%s: %s", np, strerror(errno));
817 continue;
818 }
Damien Miller35dabd02000-05-01 21:10:33 +1000819 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100821 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822 continue;
823 }
824 cp = bp->buf;
825 wrerr = NO;
826
827 if (showprogress) {
828 totalbytes = size;
829 progressmeter(-1);
830 }
831 statbytes = 0;
832 for (count = i = 0; i < size; i += 4096) {
833 amt = 4096;
834 if (i + amt > size)
835 amt = size - i;
836 count += amt;
837 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100838 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000839 if (j == -1 && (errno == EINTR ||
840 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100841 continue;
842 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000844 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845 exit(1);
846 }
847 amt -= j;
848 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100849 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 } while (amt > 0);
851 if (count == bp->cnt) {
852 /* Keep reading so we stay sync'd up. */
853 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000854 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 if (j != count) {
856 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100857 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858 }
859 }
860 count = 0;
861 cp = bp->buf;
862 }
863 }
864 if (showprogress)
865 progressmeter(1);
866 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000867 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100869 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871 if (ftruncate(ofd, size)) {
872 run_err("%s: truncate: %s", np, strerror(errno));
873 wrerr = DISPLAYED;
874 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 if (pflag) {
876 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100877#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100879#else /* HAVE_FCHMOD */
880 if (chmod(np, omode))
881#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000883 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 } else {
885 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100886#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100888#else /* HAVE_FCHMOD */
889 if (chmod(np, omode & ~mask))
890#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000892 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 }
Damien Millerbe484b52000-07-15 14:14:16 +1000894 if (close(ofd) == -1) {
895 wrerr = YES;
896 wrerrno = errno;
897 }
Damien Miller95def091999-11-25 00:26:21 +1100898 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899 if (setimes && wrerr == NO) {
900 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100901 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000903 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 wrerr = DISPLAYED;
905 }
906 }
Damien Miller95def091999-11-25 00:26:21 +1100907 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908 case YES:
909 run_err("%s: %s", np, strerror(wrerrno));
910 break;
911 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000912 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 break;
914 case DISPLAYED:
915 break;
916 }
917 }
918screwup:
919 run_err("protocol error: %s", why);
920 exit(1);
921}
922
923int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000924response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925{
926 char ch, *cp, resp, rbuf[2048];
927
Damien Millere247cc42000-05-07 12:03:14 +1000928 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929 lostconn(0);
930
931 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100932 switch (resp) {
933 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934 return (0);
935 default:
936 *cp++ = resp;
937 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100938 case 1: /* error, followed by error msg */
939 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940 do {
Damien Millere247cc42000-05-07 12:03:14 +1000941 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 lostconn(0);
943 *cp++ = ch;
944 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
945
946 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000947 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948 ++errs;
949 if (resp == 1)
950 return (-1);
951 exit(1);
952 }
953 /* NOTREACHED */
954}
955
956void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000957usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958{
Ben Lindstrom1e243242001-09-18 05:38:44 +0000959 (void) fprintf(stderr,
960 "usage: scp [-pqrvBC46] [-F config] [-S ssh] [-P port] [-c cipher] [-i identity]\n"
961 " [-o option] f1 f2\n"
Ben Lindstromff2618c2001-03-29 00:43:54 +0000962 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963 exit(1);
964}
965
966void
Damien Miller95def091999-11-25 00:26:21 +1100967run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968{
969 static FILE *fp;
970 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
972 ++errs;
973 if (fp == NULL && !(fp = fdopen(remout, "w")))
974 return;
Damien Miller95def091999-11-25 00:26:21 +1100975 (void) fprintf(fp, "%c", 0x01);
976 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100977 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100978 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100979 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100980 (void) fprintf(fp, "\n");
981 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982
Damien Miller95def091999-11-25 00:26:21 +1100983 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100984 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100985 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100986 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100987 fprintf(stderr, "\n");
988 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989}
990
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991void
992verifydir(cp)
993 char *cp;
994{
995 struct stat stb;
996
997 if (!stat(cp, &stb)) {
998 if (S_ISDIR(stb.st_mode))
999 return;
1000 errno = ENOTDIR;
1001 }
1002 run_err("%s: %s", cp, strerror(errno));
1003 exit(1);
1004}
1005
1006int
1007okname(cp0)
1008 char *cp0;
1009{
1010 int c;
1011 char *cp;
1012
1013 cp = cp0;
1014 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001015 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016 if (c & 0200)
1017 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001018 if (!isalpha(c) && !isdigit(c) &&
1019 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020 goto bad;
1021 } while (*++cp);
1022 return (1);
1023
Damien Miller98c7ad62000-03-09 21:27:49 +11001024bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025 return (0);
1026}
1027
1028BUF *
1029allocbuf(bp, fd, blksize)
1030 BUF *bp;
1031 int fd, blksize;
1032{
1033 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001034#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035 struct stat stb;
1036
1037 if (fstat(fd, &stb) < 0) {
1038 run_err("fstat: %s", strerror(errno));
1039 return (0);
1040 }
Damien Miller95def091999-11-25 00:26:21 +11001041 if (stb.st_blksize == 0)
1042 size = blksize;
1043 else
1044 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001045 stb.st_blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001046#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001047 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001048#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049 if (bp->cnt >= size)
1050 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001051 if (bp->buf == NULL)
1052 bp->buf = xmalloc(size);
1053 else
1054 bp->buf = xrealloc(bp->buf, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001055 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056 bp->cnt = size;
1057 return (bp);
1058}
1059
1060void
1061lostconn(signo)
1062 int signo;
1063{
1064 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001065 write(STDERR_FILENO, "lost connection\n", 16);
1066 if (signo)
1067 _exit(1);
1068 else
1069 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070}
1071
Ben Lindstrombba81212001-06-25 05:01:22 +00001072static void
Damien Miller7684ee12000-03-17 23:40:15 +11001073updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074{
1075 int save_errno = errno;
1076
1077 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001078 signal(SIGALRM, updateprogressmeter);
1079 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001080 errno = save_errno;
1081}
1082
Ben Lindstrombba81212001-06-25 05:01:22 +00001083static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001084foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001085{
1086 static pid_t pgrp = -1;
1087 int ctty_pgrp;
1088
1089 if (pgrp == -1)
1090 pgrp = getpgrp();
1091
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001092#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001093 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1094 ctty_pgrp == pgrp);
1095#else
Damien Miller95def091999-11-25 00:26:21 +11001096 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1097 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001098#endif
Damien Miller81428f91999-11-18 09:28:11 +11001099}
1100
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101void
1102progressmeter(int flag)
1103{
1104 static const char prefixes[] = " KMGTP";
1105 static struct timeval lastupdate;
1106 static off_t lastsize;
1107 struct timeval now, td, wait;
1108 off_t cursize, abbrevsize;
1109 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001110 int ratio, barlength, i, remaining;
Ben Lindstromcdb66e02002-04-02 20:17:43 +00001111 char buf[512];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
1113 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001114 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115 lastupdate = start;
1116 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001117 }
Damien Miller81428f91999-11-18 09:28:11 +11001118 if (foregroundproc() == 0)
1119 return;
1120
Damien Miller95def091999-11-25 00:26:21 +11001121 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001123 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001124 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125 ratio = MAX(ratio, 0);
1126 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001127 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128 ratio = 100;
1129
Damien Miller95def091999-11-25 00:26:21 +11001130 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131
1132 barlength = getttywidth() - 51;
1133 if (barlength > 0) {
1134 i = barlength * ratio / 100;
1135 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Ben Lindstromcdb66e02002-04-02 20:17:43 +00001136 "|%.*s%*s|", i,
1137 "*******************************************************"
1138 "*******************************************************"
1139 "*******************************************************"
1140 "*******************************************************"
1141 "*******************************************************"
1142 "*******************************************************"
1143 "*******************************************************",
1144 barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146 i = 0;
1147 abbrevsize = cursize;
1148 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1149 i++;
1150 abbrevsize >>= 10;
1151 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001152 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001153 (unsigned long) abbrevsize, prefixes[i],
1154 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155
1156 timersub(&now, &lastupdate, &wait);
1157 if (cursize > lastsize) {
1158 lastupdate = now;
1159 lastsize = cursize;
1160 if (wait.tv_sec >= STALLTIME) {
1161 start.tv_sec += wait.tv_sec;
1162 start.tv_usec += wait.tv_usec;
1163 }
1164 wait.tv_sec = 0;
1165 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166 timersub(&now, &start, &td);
1167 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1168
Kevin Steves7d00ba42000-12-15 23:03:10 +00001169 if (flag != 1 &&
1170 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001172 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 } else if (wait.tv_sec >= STALLTIME) {
1174 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001175 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001177 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001178 remaining = (int)(totalbytes / (statbytes / elapsed) -
1179 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001180 else
1181 remaining = elapsed;
1182
Damien Miller01ab4a21999-10-28 15:23:30 +10001183 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184 if (i)
1185 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001186 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187 else
1188 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001189 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190 i = remaining % 3600;
1191 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001192 "%02d:%02d%s", i / 60, i % 60,
1193 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194 }
1195 atomicio(write, fileno(stdout), buf, strlen(buf));
1196
1197 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001198 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001199 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001201 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001202 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 statbytes = 0;
1204 }
1205}
1206
1207int
1208getttywidth(void)
1209{
1210 struct winsize winsize;
1211
1212 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001213 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214 else
Damien Miller95def091999-11-25 00:26:21 +11001215 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216}