blob: 8324549d7c949369544d3cabfb8a038a7f7ea768 [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 Lindstrom40b95032002-12-23 02:53:08 +000078RCSID("$OpenBSD: scp.c,v 1.96 2002/12/13 15:20:52 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
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
Ben Lindstrom007eb912002-11-09 15:54:08 +0000128/* This is used to store the pid of ssh_program */
129pid_t do_cmd_pid;
130
Damien Miller5428f641999-11-25 11:54:57 +1100131/*
132 * This function executes the given command as the specified user on the
133 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
134 * assigns the input and output file descriptors on success.
135 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller4af51302000-04-16 11:18:38 +1000137int
Damien Millerad833b32000-08-23 10:46:23 +1000138do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139{
Damien Miller95def091999-11-25 00:26:21 +1100140 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000143 fprintf(stderr,
144 "Executing: program %s host %s, user %s, command %s\n",
145 ssh_program, host,
146 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller5428f641999-11-25 11:54:57 +1100148 /*
149 * Reserve two descriptors so that the real pipes won't get
150 * descriptors 0 and 1 because that will screw up dup2 below.
151 */
Damien Miller95def091999-11-25 00:26:21 +1100152 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Damien Miller95def091999-11-25 00:26:21 +1100154 /* Create a socket pair for communicating with ssh. */
155 if (pipe(pin) < 0)
156 fatal("pipe: %s", strerror(errno));
157 if (pipe(pout) < 0)
158 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller95def091999-11-25 00:26:21 +1100160 /* Free the reserved descriptors. */
161 close(reserved[0]);
162 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163
Damien Miller95def091999-11-25 00:26:21 +1100164 /* For a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000165 do_cmd_pid = fork();
166 if (do_cmd_pid == 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);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000184 } else if (do_cmd_pid == -1) {
185 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186 }
Damien Miller95def091999-11-25 00:26:21 +1100187 /* Parent. Close the other side, and return the local side. */
188 close(pin[0]);
189 *fdout = pin[1];
190 close(pout[1]);
191 *fdin = pout[0];
192 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193}
194
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195typedef struct {
196 int cnt;
197 char *buf;
198} BUF;
199
Damien Miller95def091999-11-25 00:26:21 +1100200BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100201void lostconn(int);
202void nospace(void);
203int okname(char *);
204void run_err(const char *,...);
205void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100208uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209int errs, remin, remout;
210int pflag, iamremote, iamrecursive, targetshouldbedirectory;
211
212#define CMDNEEDS 64
213char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
214
Damien Miller95def091999-11-25 00:26:21 +1100215int response(void);
216void rsource(char *, struct stat *);
217void sink(int, char *[]);
218void source(int, char *[]);
219void tolocal(int, char *[]);
220void toremote(char *, int, char *[]);
221void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
223int
224main(argc, argv)
225 int argc;
226 char *argv[];
227{
Ben Lindstrom007eb912002-11-09 15:54:08 +0000228 int ch, fflag, tflag, status;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229 char *targ;
230 extern char *optarg;
231 extern int optind;
232
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000233 __progname = get_progname(argv[0]);
234
Damien Miller874d77b2000-10-14 16:23:11 +1100235 args.list = NULL;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000236 addargs(&args, "ssh"); /* overwritten with ssh_program */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000237 addargs(&args, "-x");
Ben Lindstrom4213c552001-09-12 18:45:09 +0000238 addargs(&args, "-oForwardAgent no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000239 addargs(&args, "-oClearAllForwardings yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100240
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241 fflag = tflag = 0;
Ben Lindstrom1e243242001-09-18 05:38:44 +0000242 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100243 switch (ch) {
244 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100245 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100246 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100247 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000248 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100249 break;
250 case 'o':
251 case 'c':
252 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000253 case 'F':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000254 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100255 break;
256 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000257 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100258 break;
259 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000260 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100261 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262 case 'p':
263 pflag = 1;
264 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 case 'r':
266 iamrecursive = 1;
267 break;
Damien Millerad833b32000-08-23 10:46:23 +1000268 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100269 ssh_program = xstrdup(optarg);
270 break;
271 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000272 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100273 verbose_mode = 1;
274 break;
275 case 'q':
276 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000277 break;
278
Damien Miller95def091999-11-25 00:26:21 +1100279 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280 case 'd':
281 targetshouldbedirectory = 1;
282 break;
Damien Miller95def091999-11-25 00:26:21 +1100283 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284 iamremote = 1;
285 fflag = 1;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 iamremote = 1;
289 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000290#ifdef HAVE_CYGWIN
291 setmode(0, O_BINARY);
292#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294 default:
295 usage();
296 }
297 argc -= optind;
298 argv += optind;
299
300 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100301 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller95def091999-11-25 00:26:21 +1100303 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304 showprogress = 0;
305
306 remin = STDIN_FILENO;
307 remout = STDOUT_FILENO;
308
Kevin Stevesef4eea92001-02-05 12:42:17 +0000309 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100310 /* Follow "protocol", send data. */
311 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312 source(argc, argv);
313 exit(errs != 0);
314 }
Damien Miller95def091999-11-25 00:26:21 +1100315 if (tflag) {
316 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317 sink(argc, argv);
318 exit(errs != 0);
319 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320 if (argc < 2)
321 usage();
322 if (argc > 2)
323 targetshouldbedirectory = 1;
324
325 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000326 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000328 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
329 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000330 iamrecursive ? " -r" : "", pflag ? " -p" : "",
331 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
335 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
336 toremote(targ, argc, argv);
337 else {
Damien Miller95def091999-11-25 00:26:21 +1100338 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339 if (targetshouldbedirectory)
340 verifydir(argv[argc - 1]);
341 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000342 /*
343 * Finally check the exit status of the ssh process, if one was forked
344 * and no error has occured yet
345 */
346 if (do_cmd_pid != -1 && errs == 0) {
347 if (remin != -1)
348 (void) close(remin);
349 if (remout != -1)
350 (void) close(remout);
351 if (waitpid(do_cmd_pid, &status, 0) == -1)
352 errs = 1;
353 else {
354 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
355 errs = 1;
356 }
357 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358 exit(errs != 0);
359}
360
361void
362toremote(targ, argc, argv)
363 char *targ, *argv[];
364 int argc;
365{
366 int i, len;
367 char *bp, *host, *src, *suser, *thost, *tuser;
368
369 *targ++ = 0;
370 if (*targ == 0)
371 targ = ".";
372
Ben Lindstromc276c122002-12-23 02:14:51 +0000373 if ((thost = strrchr(argv[argc - 1], '@'))) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374 /* user@host */
375 *thost++ = 0;
376 tuser = argv[argc - 1];
377 if (*tuser == '\0')
378 tuser = NULL;
379 else if (!okname(tuser))
380 exit(1);
381 } else {
382 thost = argv[argc - 1];
383 tuser = NULL;
384 }
385
386 for (i = 0; i < argc - 1; i++) {
387 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100388 if (src) { /* remote to remote */
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000389 static char *ssh_options =
Ben Lindstromaf0c6d62002-06-09 20:06:29 +0000390 "-x -o'ClearAllForwardings yes'";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391 *src++ = 0;
392 if (*src == 0)
393 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000394 host = strrchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000395 len = strlen(ssh_program) + strlen(argv[i]) +
396 strlen(src) + (tuser ? strlen(tuser) : 0) +
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000397 strlen(thost) + strlen(targ) +
398 strlen(ssh_options) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100399 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400 if (host) {
401 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100402 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403 suser = argv[i];
404 if (*suser == '\0')
405 suser = pwd->pw_name;
406 else if (!okname(suser))
407 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000408 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000409 "%s%s %s -n "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000410 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000411 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000412 ssh_options, suser, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000413 tuser ? tuser : "", tuser ? "@" : "",
414 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100415 } else {
416 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000417 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000418 "exec %s%s %s -n %s "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000419 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000420 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000421 ssh_options, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000422 tuser ? tuser : "", tuser ? "@" : "",
423 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100424 }
Damien Miller95def091999-11-25 00:26:21 +1100425 if (verbose_mode)
426 fprintf(stderr, "Executing: %s\n", bp);
427 (void) system(bp);
428 (void) xfree(bp);
429 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430 if (remin == -1) {
431 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100432 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000433 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100434 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000435 if (do_cmd(host, tuser, bp, &remin,
436 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100437 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 if (response() < 0)
439 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100440 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441 }
Damien Miller95def091999-11-25 00:26:21 +1100442 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 }
444 }
445}
446
447void
448tolocal(argc, argv)
449 int argc;
450 char *argv[];
451{
452 int i, len;
453 char *bp, *host, *src, *suser;
454
455 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100456 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000458 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000460 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000461 iamrecursive ? " -r" : "", pflag ? " -p" : "",
462 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100463 if (verbose_mode)
464 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 if (system(bp))
466 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100467 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468 continue;
469 }
470 *src++ = 0;
471 if (*src == 0)
472 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000473 if ((host = strrchr(argv[i], '@')) == NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 host = argv[i];
475 suser = NULL;
476 } else {
477 *host++ = 0;
478 suser = argv[i];
479 if (*suser == '\0')
480 suser = pwd->pw_name;
481 else if (!okname(suser))
482 continue;
483 }
Damien Miller34132e52000-01-14 15:45:46 +1100484 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100486 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000487 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000488 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100489 (void) xfree(bp);
490 ++errs;
491 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 }
Damien Miller95def091999-11-25 00:26:21 +1100493 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100495 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496 remin = remout = -1;
497 }
498}
499
500void
501source(argc, argv)
502 int argc;
503 char *argv[];
504{
505 struct stat stb;
506 static BUF buffer;
507 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000508 off_t i, amt, result;
509 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000511 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
513 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100514 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000516 len = strlen(name);
517 while (len > 1 && name[len-1] == '/')
518 name[--len] = '\0';
Ben Lindstrom3e45e4c2001-10-03 17:30:58 +0000519 if (strchr(name, '\n') != NULL) {
520 run_err("%s: skipping, filename contains a newline",
521 name);
522 goto next;
523 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524 if ((fd = open(name, O_RDONLY, 0)) < 0)
525 goto syserr;
526 if (fstat(fd, &stb) < 0) {
527syserr: run_err("%s: %s", name, strerror(errno));
528 goto next;
529 }
530 switch (stb.st_mode & S_IFMT) {
531 case S_IFREG:
532 break;
533 case S_IFDIR:
534 if (iamrecursive) {
535 rsource(name, &stb);
536 goto next;
537 }
538 /* FALLTHROUGH */
539 default:
540 run_err("%s: not a regular file", name);
541 goto next;
542 }
543 if ((last = strrchr(name, '/')) == NULL)
544 last = name;
545 else
546 ++last;
547 curfile = last;
548 if (pflag) {
549 /*
550 * Make it compatible with possible future
551 * versions expecting microseconds.
552 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000553 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000554 (u_long) stb.st_mtime,
555 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000556 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 if (response() < 0)
558 goto next;
559 }
560#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100561#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000562 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000563 (u_int) (stb.st_mode & FILEMODEMASK),
Kevin Steves399ec972002-03-05 18:59:45 +0000564 (long long)stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100565#else
566 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000567 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100568 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000569 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100570#endif
Damien Miller95def091999-11-25 00:26:21 +1100571 if (verbose_mode) {
572 fprintf(stderr, "Sending file modes: %s", buf);
Damien Miller95def091999-11-25 00:26:21 +1100573 }
Damien Miller35dabd02000-05-01 21:10:33 +1000574 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575 if (response() < 0)
576 goto next;
577 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100578next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000579 continue;
580 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581 if (showprogress) {
582 totalbytes = stb.st_size;
583 progressmeter(-1);
584 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585 /* Keep writing after an error so that we stay sync'd up. */
586 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
587 amt = bp->cnt;
588 if (i + amt > stb.st_size)
589 amt = stb.st_size - i;
590 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000591 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592 if (result != amt)
593 haderr = result >= 0 ? EIO : errno;
594 }
595 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000596 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597 else {
Damien Miller864ea591999-12-15 11:04:25 +1100598 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599 if (result != amt)
600 haderr = result >= 0 ? EIO : errno;
601 statbytes += result;
602 }
603 }
Damien Miller95def091999-11-25 00:26:21 +1100604 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605 progressmeter(1);
606
607 if (close(fd) < 0 && !haderr)
608 haderr = errno;
609 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000610 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611 else
612 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100613 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614 }
615}
616
617void
618rsource(name, statp)
619 char *name;
620 struct stat *statp;
621{
622 DIR *dirp;
623 struct dirent *dp;
624 char *last, *vect[1], path[1100];
625
626 if (!(dirp = opendir(name))) {
627 run_err("%s: %s", name, strerror(errno));
628 return;
629 }
630 last = strrchr(name, '/');
631 if (last == 0)
632 last = name;
633 else
634 last++;
635 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000636 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000637 (u_long) statp->st_mtime,
638 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000639 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640 if (response() < 0) {
641 closedir(dirp);
642 return;
643 }
644 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000645 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000646 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100647 if (verbose_mode)
648 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000649 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000650 if (response() < 0) {
651 closedir(dirp);
652 return;
653 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000654 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655 if (dp->d_ino == 0)
656 continue;
657 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
658 continue;
659 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
660 run_err("%s/%s: name too long", name, dp->d_name);
661 continue;
662 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000663 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664 vect[0] = path;
665 source(1, vect);
666 }
Damien Miller95def091999-11-25 00:26:21 +1100667 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000668 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100669 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670}
671
672void
673sink(argc, argv)
674 int argc;
675 char *argv[];
676{
677 static BUF buffer;
678 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100679 enum {
680 YES, NO, DISPLAYED
681 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682 BUF *bp;
683 off_t i, j;
684 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100685 off_t size;
686 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100688 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689
Ben Lindstromf719a202001-04-14 23:14:22 +0000690#define atime tv[0]
691#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000692#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693
694 setimes = targisdir = 0;
695 mask = umask(0);
696 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100697 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698 if (argc != 1) {
699 run_err("ambiguous target");
700 exit(1);
701 }
702 targ = *argv;
703 if (targetshouldbedirectory)
704 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100705
Damien Miller35dabd02000-05-01 21:10:33 +1000706 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
708 targisdir = 1;
709 for (first = 1;; first = 0) {
710 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000711 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000712 return;
713 if (*cp++ == '\n')
714 SCREWUP("unexpected <newline>");
715 do {
Damien Millere247cc42000-05-07 12:03:14 +1000716 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717 SCREWUP("lost connection");
718 *cp++ = ch;
719 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
720 *cp = 0;
721
722 if (buf[0] == '\01' || buf[0] == '\02') {
723 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000724 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000725 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726 if (buf[0] == '\02')
727 exit(1);
728 ++errs;
729 continue;
730 }
731 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000732 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 return;
734 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735 if (ch == '\n')
736 *--cp = 0;
737
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738 cp = buf;
739 if (*cp == 'T') {
740 setimes++;
741 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000742 mtime.tv_sec = strtol(cp, &cp, 10);
743 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000745 mtime.tv_usec = strtol(cp, &cp, 10);
746 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000747 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000748 atime.tv_sec = strtol(cp, &cp, 10);
749 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000750 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000751 atime.tv_usec = strtol(cp, &cp, 10);
752 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000754 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755 continue;
756 }
757 if (*cp != 'C' && *cp != 'D') {
758 /*
759 * Check for the case "rcp remote:foo\* local:bar".
760 * In this case, the line "No match." can be returned
761 * by the shell before the rcp command on the remote is
762 * executed so the ^Aerror_message convention isn't
763 * followed.
764 */
765 if (first) {
766 run_err("%s", cp);
767 exit(1);
768 }
769 SCREWUP("expected control record");
770 }
771 mode = 0;
772 for (++cp; cp < buf + 5; cp++) {
773 if (*cp < '0' || *cp > '7')
774 SCREWUP("bad mode");
775 mode = (mode << 3) | (*cp - '0');
776 }
777 if (*cp++ != ' ')
778 SCREWUP("mode not delimited");
779
Ben Lindstrombd472262001-03-29 00:39:55 +0000780 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781 size = size * 10 + (*cp++ - '0');
782 if (*cp++ != ' ')
783 SCREWUP("size not delimited");
784 if (targisdir) {
785 static char *namebuf;
786 static int cursize;
787 size_t need;
788
789 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000790 if (need > cursize) {
791 if (namebuf)
792 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100793 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000794 cursize = need;
795 }
796 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +0000797 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798 np = namebuf;
799 } else
800 np = targ;
801 curfile = cp;
802 exists = stat(np, &stb) == 0;
803 if (buf[0] == 'D') {
804 int mod_flag = pflag;
805 if (exists) {
806 if (!S_ISDIR(stb.st_mode)) {
807 errno = ENOTDIR;
808 goto bad;
809 }
810 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100811 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812 } else {
Damien Miller95def091999-11-25 00:26:21 +1100813 /* Handle copying from a read-only
814 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815 mod_flag = 1;
816 if (mkdir(np, mode | S_IRWXU) < 0)
817 goto bad;
818 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000819 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820 sink(1, vect);
821 if (setimes) {
822 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100823 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100824 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100825 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826 }
827 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100828 (void) chmod(vect[0], mode);
829 if (vect[0])
830 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831 continue;
832 }
833 omode = mode;
834 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000835 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000836bad: run_err("%s: %s", np, strerror(errno));
837 continue;
838 }
Damien Miller35dabd02000-05-01 21:10:33 +1000839 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100841 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842 continue;
843 }
844 cp = bp->buf;
845 wrerr = NO;
846
847 if (showprogress) {
848 totalbytes = size;
849 progressmeter(-1);
850 }
851 statbytes = 0;
852 for (count = i = 0; i < size; i += 4096) {
853 amt = 4096;
854 if (i + amt > size)
855 amt = size - i;
856 count += amt;
857 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100858 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000859 if (j == -1 && (errno == EINTR ||
860 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100861 continue;
862 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000864 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865 exit(1);
866 }
867 amt -= j;
868 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100869 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870 } while (amt > 0);
871 if (count == bp->cnt) {
872 /* Keep reading so we stay sync'd up. */
873 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000874 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 if (j != count) {
876 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100877 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 }
879 }
880 count = 0;
881 cp = bp->buf;
882 }
883 }
884 if (showprogress)
885 progressmeter(1);
886 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000887 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100889 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 if (ftruncate(ofd, size)) {
892 run_err("%s: truncate: %s", np, strerror(errno));
893 wrerr = DISPLAYED;
894 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 if (pflag) {
896 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100897#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100899#else /* HAVE_FCHMOD */
900 if (chmod(np, omode))
901#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000903 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 } else {
905 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100906#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100908#else /* HAVE_FCHMOD */
909 if (chmod(np, omode & ~mask))
910#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000912 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 }
Damien Millerbe484b52000-07-15 14:14:16 +1000914 if (close(ofd) == -1) {
915 wrerr = YES;
916 wrerrno = errno;
917 }
Damien Miller95def091999-11-25 00:26:21 +1100918 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919 if (setimes && wrerr == NO) {
920 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100921 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000923 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924 wrerr = DISPLAYED;
925 }
926 }
Damien Miller95def091999-11-25 00:26:21 +1100927 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928 case YES:
929 run_err("%s: %s", np, strerror(wrerrno));
930 break;
931 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000932 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 break;
934 case DISPLAYED:
935 break;
936 }
937 }
938screwup:
939 run_err("protocol error: %s", why);
940 exit(1);
941}
942
943int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000944response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945{
946 char ch, *cp, resp, rbuf[2048];
947
Damien Millere247cc42000-05-07 12:03:14 +1000948 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949 lostconn(0);
950
951 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100952 switch (resp) {
953 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954 return (0);
955 default:
956 *cp++ = resp;
957 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100958 case 1: /* error, followed by error msg */
959 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960 do {
Damien Millere247cc42000-05-07 12:03:14 +1000961 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962 lostconn(0);
963 *cp++ = ch;
964 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
965
966 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000967 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968 ++errs;
969 if (resp == 1)
970 return (-1);
971 exit(1);
972 }
973 /* NOTREACHED */
974}
975
976void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000977usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978{
Ben Lindstrom1e243242001-09-18 05:38:44 +0000979 (void) fprintf(stderr,
Ben Lindstrom45933dd2002-06-21 00:10:58 +0000980 "usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
981 " [-c cipher] [-i identity] [-o option]\n"
982 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983 exit(1);
984}
985
986void
Damien Miller95def091999-11-25 00:26:21 +1100987run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988{
989 static FILE *fp;
990 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
992 ++errs;
993 if (fp == NULL && !(fp = fdopen(remout, "w")))
994 return;
Damien Miller95def091999-11-25 00:26:21 +1100995 (void) fprintf(fp, "%c", 0x01);
996 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100997 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100998 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100999 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001000 (void) fprintf(fp, "\n");
1001 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002
Damien Miller95def091999-11-25 00:26:21 +11001003 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001004 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001005 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001006 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001007 fprintf(stderr, "\n");
1008 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009}
1010
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011void
1012verifydir(cp)
1013 char *cp;
1014{
1015 struct stat stb;
1016
1017 if (!stat(cp, &stb)) {
1018 if (S_ISDIR(stb.st_mode))
1019 return;
1020 errno = ENOTDIR;
1021 }
1022 run_err("%s: %s", cp, strerror(errno));
1023 exit(1);
1024}
1025
1026int
1027okname(cp0)
1028 char *cp0;
1029{
1030 int c;
1031 char *cp;
1032
1033 cp = cp0;
1034 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001035 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036 if (c & 0200)
1037 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001038 if (!isalpha(c) && !isdigit(c) &&
Ben Lindstromc276c122002-12-23 02:14:51 +00001039 c != '@' && c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040 goto bad;
1041 } while (*++cp);
1042 return (1);
1043
Damien Miller98c7ad62000-03-09 21:27:49 +11001044bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045 return (0);
1046}
1047
1048BUF *
1049allocbuf(bp, fd, blksize)
1050 BUF *bp;
1051 int fd, blksize;
1052{
1053 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001054#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055 struct stat stb;
1056
1057 if (fstat(fd, &stb) < 0) {
1058 run_err("fstat: %s", strerror(errno));
1059 return (0);
1060 }
Ben Lindstrom418e0782002-12-23 02:22:09 +00001061 size = roundup(stb.st_blksize, blksize);
1062 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001063 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001064#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001065 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001066#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067 if (bp->cnt >= size)
1068 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001069 if (bp->buf == NULL)
1070 bp->buf = xmalloc(size);
1071 else
1072 bp->buf = xrealloc(bp->buf, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001073 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074 bp->cnt = size;
1075 return (bp);
1076}
1077
1078void
1079lostconn(signo)
1080 int signo;
1081{
1082 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001083 write(STDERR_FILENO, "lost connection\n", 16);
1084 if (signo)
1085 _exit(1);
1086 else
1087 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088}
1089
Ben Lindstrombba81212001-06-25 05:01:22 +00001090static void
Damien Miller7684ee12000-03-17 23:40:15 +11001091updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001092{
1093 int save_errno = errno;
1094
1095 progressmeter(0);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001096 signal(SIGALRM, updateprogressmeter);
1097 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098 errno = save_errno;
1099}
1100
Ben Lindstrombba81212001-06-25 05:01:22 +00001101static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001102foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001103{
1104 static pid_t pgrp = -1;
1105 int ctty_pgrp;
1106
1107 if (pgrp == -1)
1108 pgrp = getpgrp();
1109
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001110#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001111 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1112 ctty_pgrp == pgrp);
1113#else
Damien Miller95def091999-11-25 00:26:21 +11001114 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1115 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001116#endif
Damien Miller81428f91999-11-18 09:28:11 +11001117}
1118
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119void
1120progressmeter(int flag)
1121{
Ben Lindstrom40b95032002-12-23 02:53:08 +00001122 static const char spaces[] = " "
1123 " "
1124 " "
1125 " "
1126 " "
1127 " ";
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128 static const char prefixes[] = " KMGTP";
1129 static struct timeval lastupdate;
1130 static off_t lastsize;
1131 struct timeval now, td, wait;
Ben Lindstrom40b95032002-12-23 02:53:08 +00001132 off_t cursize, abbrevsize, bytespersec;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133 double elapsed;
Ben Lindstrom40b95032002-12-23 02:53:08 +00001134 int ratio, remaining, i, ai, bi, nspaces;
Ben Lindstromcdb66e02002-04-02 20:17:43 +00001135 char buf[512];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136
1137 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001138 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139 lastupdate = start;
1140 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001141 }
Damien Miller81428f91999-11-18 09:28:11 +11001142 if (foregroundproc() == 0)
1143 return;
1144
Damien Miller95def091999-11-25 00:26:21 +11001145 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001147 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001148 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149 ratio = MAX(ratio, 0);
1150 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001151 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001152 ratio = 100;
1153
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154 abbrevsize = cursize;
Ben Lindstrom40b95032002-12-23 02:53:08 +00001155 for (ai = 0; abbrevsize >= 10000 && ai < sizeof(prefixes); ai++)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 abbrevsize >>= 10;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157
1158 timersub(&now, &lastupdate, &wait);
1159 if (cursize > lastsize) {
1160 lastupdate = now;
1161 lastsize = cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 wait.tv_sec = 0;
1163 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 timersub(&now, &start, &td);
1165 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1166
Ben Lindstrom40b95032002-12-23 02:53:08 +00001167 bytespersec = 0;
1168 if (statbytes > 0) {
1169 bytespersec = statbytes;
1170 if (elapsed > 0.0)
1171 bytespersec /= elapsed;
1172 }
1173 for (bi = 1; bytespersec >= 1024000 && bi < sizeof(prefixes); bi++)
1174 bytespersec >>= 10;
1175
1176 nspaces = MIN(getttywidth() - 79, sizeof(spaces) - 1);
1177
Tim Rice13b2e552003-01-08 20:09:30 -08001178#ifdef HAVE_LONG_LONG_INT
Ben Lindstrom40b95032002-12-23 02:53:08 +00001179 snprintf(buf, sizeof(buf),
1180 "\r%-45.45s%.*s%3d%% %4lld%c%c %3lld.%01d%cB/s",
1181 curfile,
1182 nspaces,
1183 spaces,
1184 ratio,
1185 (long long)abbrevsize,
1186 prefixes[ai],
1187 ai == 0 ? ' ' : 'B',
1188 (long long)(bytespersec / 1024),
1189 (int)((bytespersec % 1024) * 10 / 1024),
1190 prefixes[bi]
1191 );
Tim Rice13b2e552003-01-08 20:09:30 -08001192#else
1193 snprintf(buf, sizeof(buf),
1194 "\r%-45.45s%.*s%3d%% %4lld%c%c %3lu.%01d%cB/s",
1195 curfile,
1196 nspaces,
1197 spaces,
1198 ratio,
1199 (u_long)abbrevsize,
1200 prefixes[ai],
1201 ai == 0 ? ' ' : 'B',
1202 (u_long)(bytespersec / 1024),
1203 (int)((bytespersec % 1024) * 10 / 1024),
1204 prefixes[bi]
1205 );
1206#endif
Ben Lindstrom40b95032002-12-23 02:53:08 +00001207
Kevin Steves7d00ba42000-12-15 23:03:10 +00001208 if (flag != 1 &&
1209 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001211 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212 } else if (wait.tv_sec >= STALLTIME) {
1213 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001214 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001216 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001217 remaining = (int)(totalbytes / (statbytes / elapsed) -
1218 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001219 else
1220 remaining = elapsed;
1221
Damien Miller01ab4a21999-10-28 15:23:30 +10001222 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223 if (i)
1224 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001225 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 else
1227 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001228 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229 i = remaining % 3600;
1230 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001231 "%02d:%02d%s", i / 60, i % 60,
1232 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001233 }
1234 atomicio(write, fileno(stdout), buf, strlen(buf));
1235
1236 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001237 mysignal(SIGALRM, updateprogressmeter);
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001238 alarm(PROGRESSTIME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239 } else if (flag == 1) {
Ben Lindstrom958d9f62001-08-06 22:48:19 +00001240 alarm(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001241 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242 statbytes = 0;
1243 }
1244}
1245
1246int
1247getttywidth(void)
1248{
1249 struct winsize winsize;
1250
1251 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001252 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253 else
Damien Miller95def091999-11-25 00:26:21 +11001254 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255}