blob: 356d97e6370643d51c72bb9cd85efa3cb3f93c3d [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/*
17 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
19 *
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"
Damien Miller62cee002000-09-23 17:15:56 +110078RCSID("$OpenBSD: scp.c,v 1.40 2000/09/21 11:11:42 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
80#include "ssh.h"
81#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller78315eb2000-09-29 23:01:36 +110083#ifndef _PATH_CP
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084#define _PATH_CP "cp"
Damien Miller78315eb2000-09-29 23:01:36 +110085#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
87/* For progressmeter() -- number of seconds before xfer considered "stalled" */
88#define STALLTIME 5
89
Damien Miller484118e2000-07-02 19:13:56 +100090/* Progress meter bar */
91#define BAR \
92 "************************************************************"\
93 "************************************************************"\
94 "************************************************************"\
95 "************************************************************"
96#define MAX_BARLENGTH (sizeof(BAR) - 1)
97
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098/* Visual statistics about files as they are transferred. */
99void progressmeter(int);
100
101/* Returns width of the terminal (for progress meter calculations). */
102int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000103int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105/* Time a transfer started. */
106static struct timeval start;
107
108/* Number of bytes of current file transferred so far. */
109volatile unsigned long statbytes;
110
111/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100112off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
114/* Name of current file being transferred. */
115char *curfile;
116
Damien Miller34132e52000-01-14 15:45:46 +1100117/* This is set to non-zero if IPv4 is desired. */
118int IPv4 = 0;
119
120/* This is set to non-zero if IPv6 is desired. */
121int IPv6 = 0;
122
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100124int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
126/* This is set to non-zero if compression is desired. */
Damien Millerd8087f61999-11-25 12:31:26 +1100127int compress_flag = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
129/* This is set to zero if the progressmeter is not desired. */
130int showprogress = 1;
131
132/* This is set to non-zero if running in batch mode (that is, password
133 and passphrase queries are not allowed). */
134int batchmode = 0;
135
136/* This is set to the cipher type string if given on the command line. */
137char *cipher = NULL;
138
Damien Miller95def091999-11-25 00:26:21 +1100139/* This is set to the RSA authentication identity file name if given on
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 the command line. */
141char *identity = NULL;
142
143/* This is the port to use in contacting the remote site (is non-NULL). */
144char *port = NULL;
145
Damien Millerad833b32000-08-23 10:46:23 +1000146/* This is the program to execute for the secured connection. ("ssh" or -S) */
147char *ssh_program = SSH_PROGRAM;
148
Damien Miller5428f641999-11-25 11:54:57 +1100149/*
150 * This function executes the given command as the specified user on the
151 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
152 * assigns the input and output file descriptors on success.
153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller4af51302000-04-16 11:18:38 +1000155int
Damien Millerad833b32000-08-23 10:46:23 +1000156do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157{
Damien Miller95def091999-11-25 00:26:21 +1100158 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159
Damien Miller95def091999-11-25 00:26:21 +1100160 if (verbose_mode)
161 fprintf(stderr, "Executing: host %s, user %s, command %s\n",
Damien Millerad833b32000-08-23 10:46:23 +1000162 host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163
Damien Miller5428f641999-11-25 11:54:57 +1100164 /*
165 * Reserve two descriptors so that the real pipes won't get
166 * descriptors 0 and 1 because that will screw up dup2 below.
167 */
Damien Miller95def091999-11-25 00:26:21 +1100168 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller95def091999-11-25 00:26:21 +1100170 /* Create a socket pair for communicating with ssh. */
171 if (pipe(pin) < 0)
172 fatal("pipe: %s", strerror(errno));
173 if (pipe(pout) < 0)
174 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller95def091999-11-25 00:26:21 +1100176 /* Free the reserved descriptors. */
177 close(reserved[0]);
178 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller95def091999-11-25 00:26:21 +1100180 /* For a child to execute the command on the remote host using ssh. */
181 if (fork() == 0) {
Damien Millerad833b32000-08-23 10:46:23 +1000182 char *args[100]; /* XXX careful */
Damien Miller95def091999-11-25 00:26:21 +1100183 unsigned int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
Damien Miller95def091999-11-25 00:26:21 +1100185 /* Child. */
186 close(pin[1]);
187 close(pout[0]);
188 dup2(pin[0], 0);
189 dup2(pout[1], 1);
190 close(pin[0]);
191 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192
Damien Miller95def091999-11-25 00:26:21 +1100193 i = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000194 args[i++] = ssh_program;
Damien Miller95def091999-11-25 00:26:21 +1100195 args[i++] = "-x";
196 args[i++] = "-oFallBackToRsh no";
Damien Miller34132e52000-01-14 15:45:46 +1100197 if (IPv4)
198 args[i++] = "-4";
199 if (IPv6)
200 args[i++] = "-6";
Damien Miller95def091999-11-25 00:26:21 +1100201 if (verbose_mode)
202 args[i++] = "-v";
Damien Millerd8087f61999-11-25 12:31:26 +1100203 if (compress_flag)
Damien Miller95def091999-11-25 00:26:21 +1100204 args[i++] = "-C";
205 if (batchmode)
206 args[i++] = "-oBatchMode yes";
207 if (cipher != NULL) {
208 args[i++] = "-c";
209 args[i++] = cipher;
210 }
211 if (identity != NULL) {
212 args[i++] = "-i";
213 args[i++] = identity;
214 }
215 if (port != NULL) {
216 args[i++] = "-p";
217 args[i++] = port;
218 }
219 if (remuser != NULL) {
220 args[i++] = "-l";
221 args[i++] = remuser;
222 }
223 args[i++] = host;
224 args[i++] = cmd;
225 args[i++] = NULL;
226
Damien Millerad833b32000-08-23 10:46:23 +1000227 execvp(ssh_program, args);
228 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100229 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230 }
Damien Miller95def091999-11-25 00:26:21 +1100231 /* Parent. Close the other side, and return the local side. */
232 close(pin[0]);
233 *fdout = pin[1];
234 close(pout[1]);
235 *fdin = pout[0];
236 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237}
238
Damien Miller4af51302000-04-16 11:18:38 +1000239void
Damien Miller95def091999-11-25 00:26:21 +1100240fatal(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241{
Damien Miller95def091999-11-25 00:26:21 +1100242 va_list ap;
243 char buf[1024];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller95def091999-11-25 00:26:21 +1100245 va_start(ap, fmt);
246 vsnprintf(buf, sizeof(buf), fmt, ap);
247 va_end(ap);
248 fprintf(stderr, "%s\n", buf);
249 exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250}
251
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252typedef struct {
253 int cnt;
254 char *buf;
255} BUF;
256
257extern int iamremote;
258
Damien Miller95def091999-11-25 00:26:21 +1100259BUF *allocbuf(BUF *, int, int);
260char *colon(char *);
261void lostconn(int);
262void nospace(void);
263int okname(char *);
264void run_err(const char *,...);
265void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100268uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269int errs, remin, remout;
270int pflag, iamremote, iamrecursive, targetshouldbedirectory;
271
272#define CMDNEEDS 64
273char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
274
Damien Miller95def091999-11-25 00:26:21 +1100275int response(void);
276void rsource(char *, struct stat *);
277void sink(int, char *[]);
278void source(int, char *[]);
279void tolocal(int, char *[]);
280void toremote(char *, int, char *[]);
281void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
283int
284main(argc, argv)
285 int argc;
286 char *argv[];
287{
288 int ch, fflag, tflag;
289 char *targ;
290 extern char *optarg;
291 extern int optind;
292
293 fflag = tflag = 0;
Damien Miller7b28dc52000-09-05 13:34:53 +1100294 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:")) != EOF)
Damien Miller95def091999-11-25 00:26:21 +1100295 switch (ch) {
296 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100297 case '4':
Damien Miller4af51302000-04-16 11:18:38 +1000298 IPv4 = 1;
Damien Miller34132e52000-01-14 15:45:46 +1100299 break;
300 case '6':
Damien Miller4af51302000-04-16 11:18:38 +1000301 IPv6 = 1;
Damien Miller34132e52000-01-14 15:45:46 +1100302 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303 case 'p':
304 pflag = 1;
305 break;
306 case 'P':
Damien Miller95def091999-11-25 00:26:21 +1100307 port = optarg;
308 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309 case 'r':
310 iamrecursive = 1;
311 break;
Damien Millerad833b32000-08-23 10:46:23 +1000312 case 'S':
313 ssh_program = optarg;
314 break;
315
Damien Miller95def091999-11-25 00:26:21 +1100316 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317 case 'd':
318 targetshouldbedirectory = 1;
319 break;
Damien Miller95def091999-11-25 00:26:21 +1100320 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321 iamremote = 1;
322 fflag = 1;
323 break;
Damien Miller95def091999-11-25 00:26:21 +1100324 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325 iamremote = 1;
326 tflag = 1;
327 break;
328 case 'c':
329 cipher = optarg;
Damien Miller95def091999-11-25 00:26:21 +1100330 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331 case 'i':
Damien Miller95def091999-11-25 00:26:21 +1100332 identity = optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333 break;
334 case 'v':
Damien Miller95def091999-11-25 00:26:21 +1100335 verbose_mode = 1;
336 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337 case 'B':
Damien Miller95def091999-11-25 00:26:21 +1100338 batchmode = 1;
339 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340 case 'C':
Damien Millerd8087f61999-11-25 12:31:26 +1100341 compress_flag = 1;
Damien Miller95def091999-11-25 00:26:21 +1100342 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343 case 'q':
Damien Miller95def091999-11-25 00:26:21 +1100344 showprogress = 0;
345 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346 case '?':
347 default:
348 usage();
349 }
350 argc -= optind;
351 argv += optind;
352
353 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100354 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Miller95def091999-11-25 00:26:21 +1100356 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357 showprogress = 0;
358
359 remin = STDIN_FILENO;
360 remout = STDOUT_FILENO;
361
Damien Miller95def091999-11-25 00:26:21 +1100362 if (fflag) {
363 /* Follow "protocol", send data. */
364 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365 source(argc, argv);
366 exit(errs != 0);
367 }
Damien Miller95def091999-11-25 00:26:21 +1100368 if (tflag) {
369 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370 sink(argc, argv);
371 exit(errs != 0);
372 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373 if (argc < 2)
374 usage();
375 if (argc > 2)
376 targetshouldbedirectory = 1;
377
378 remin = remout = -1;
379 /* Command to be executed on remote system using "ssh". */
Damien Miller95def091999-11-25 00:26:21 +1100380 (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000381 iamrecursive ? " -r" : "", pflag ? " -p" : "",
382 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383
Damien Miller95def091999-11-25 00:26:21 +1100384 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
386 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
387 toremote(targ, argc, argv);
388 else {
Damien Miller95def091999-11-25 00:26:21 +1100389 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390 if (targetshouldbedirectory)
391 verifydir(argv[argc - 1]);
392 }
393 exit(errs != 0);
394}
395
Damien Miller34132e52000-01-14 15:45:46 +1100396char *
397cleanhostname(host)
398 char *host;
399{
400 if (*host == '[' && host[strlen(host) - 1] == ']') {
401 host[strlen(host) - 1] = '\0';
402 return (host + 1);
403 } else
404 return host;
405}
406
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407void
408toremote(targ, argc, argv)
409 char *targ, *argv[];
410 int argc;
411{
412 int i, len;
413 char *bp, *host, *src, *suser, *thost, *tuser;
414
415 *targ++ = 0;
416 if (*targ == 0)
417 targ = ".";
418
419 if ((thost = strchr(argv[argc - 1], '@'))) {
420 /* user@host */
421 *thost++ = 0;
422 tuser = argv[argc - 1];
423 if (*tuser == '\0')
424 tuser = NULL;
425 else if (!okname(tuser))
426 exit(1);
427 } else {
428 thost = argv[argc - 1];
429 tuser = NULL;
430 }
431
432 for (i = 0; i < argc - 1; i++) {
433 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100434 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435 *src++ = 0;
436 if (*src == 0)
437 src = ".";
438 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000439 len = strlen(ssh_program) + strlen(argv[i]) +
440 strlen(src) + (tuser ? strlen(tuser) : 0) +
441 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100442 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 if (host) {
444 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100445 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 suser = argv[i];
447 if (*suser == '\0')
448 suser = pwd->pw_name;
449 else if (!okname(suser))
450 continue;
Damien Miller95def091999-11-25 00:26:21 +1100451 (void) sprintf(bp,
Damien Millerad833b32000-08-23 10:46:23 +1000452 "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
453 ssh_program, verbose_mode ? " -v" : "",
454 suser, host, cmd, src,
455 tuser ? tuser : "", tuser ? "@" : "",
456 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100457 } else {
458 host = cleanhostname(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100459 (void) sprintf(bp,
Damien Millerad833b32000-08-23 10:46:23 +1000460 "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
461 ssh_program, verbose_mode ? " -v" : "",
462 host, cmd, src,
463 tuser ? tuser : "", tuser ? "@" : "",
464 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100465 }
Damien Miller95def091999-11-25 00:26:21 +1100466 if (verbose_mode)
467 fprintf(stderr, "Executing: %s\n", bp);
468 (void) system(bp);
469 (void) xfree(bp);
470 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 if (remin == -1) {
472 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100473 bp = xmalloc(len);
474 (void) sprintf(bp, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100475 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000476 if (do_cmd(host, tuser, bp, &remin,
477 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100478 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479 if (response() < 0)
480 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100481 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482 }
Damien Miller95def091999-11-25 00:26:21 +1100483 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484 }
485 }
486}
487
488void
489tolocal(argc, argv)
490 int argc;
491 char *argv[];
492{
493 int i, len;
494 char *bp, *host, *src, *suser;
495
496 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100497 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000499 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 bp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +1100501 (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000502 iamrecursive ? " -r" : "", pflag ? " -p" : "",
503 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100504 if (verbose_mode)
505 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506 if (system(bp))
507 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100508 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509 continue;
510 }
511 *src++ = 0;
512 if (*src == 0)
513 src = ".";
514 if ((host = strchr(argv[i], '@')) == NULL) {
515 host = argv[i];
516 suser = NULL;
517 } else {
518 *host++ = 0;
519 suser = argv[i];
520 if (*suser == '\0')
521 suser = pwd->pw_name;
522 else if (!okname(suser))
523 continue;
524 }
Damien Miller34132e52000-01-14 15:45:46 +1100525 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100527 bp = xmalloc(len);
528 (void) sprintf(bp, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000529 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100530 (void) xfree(bp);
531 ++errs;
532 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533 }
Damien Miller95def091999-11-25 00:26:21 +1100534 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100536 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537 remin = remout = -1;
538 }
539}
540
541void
542source(argc, argv)
543 int argc;
544 char *argv[];
545{
546 struct stat stb;
547 static BUF buffer;
548 BUF *bp;
549 off_t i;
550 int amt, fd, haderr, indx, result;
551 char *last, *name, buf[2048];
552
553 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100554 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 statbytes = 0;
556 if ((fd = open(name, O_RDONLY, 0)) < 0)
557 goto syserr;
558 if (fstat(fd, &stb) < 0) {
559syserr: run_err("%s: %s", name, strerror(errno));
560 goto next;
561 }
562 switch (stb.st_mode & S_IFMT) {
563 case S_IFREG:
564 break;
565 case S_IFDIR:
566 if (iamrecursive) {
567 rsource(name, &stb);
568 goto next;
569 }
570 /* FALLTHROUGH */
571 default:
572 run_err("%s: not a regular file", name);
573 goto next;
574 }
575 if ((last = strrchr(name, '/')) == NULL)
576 last = name;
577 else
578 ++last;
579 curfile = last;
580 if (pflag) {
581 /*
582 * Make it compatible with possible future
583 * versions expecting microseconds.
584 */
Damien Miller95def091999-11-25 00:26:21 +1100585 (void) sprintf(buf, "T%lu 0 %lu 0\n",
Damien Millerad833b32000-08-23 10:46:23 +1000586 (unsigned long) stb.st_mtime,
587 (unsigned long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000588 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 if (response() < 0)
590 goto next;
591 }
592#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller95def091999-11-25 00:26:21 +1100593 (void) sprintf(buf, "C%04o %lu %s\n",
594 (unsigned int) (stb.st_mode & FILEMODEMASK),
595 (unsigned long) stb.st_size,
596 last);
597 if (verbose_mode) {
598 fprintf(stderr, "Sending file modes: %s", buf);
599 fflush(stderr);
600 }
Damien Miller35dabd02000-05-01 21:10:33 +1000601 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602 if (response() < 0)
603 goto next;
604 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100605next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000606 continue;
607 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608 if (showprogress) {
609 totalbytes = stb.st_size;
610 progressmeter(-1);
611 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612 /* Keep writing after an error so that we stay sync'd up. */
613 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
614 amt = bp->cnt;
615 if (i + amt > stb.st_size)
616 amt = stb.st_size - i;
617 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000618 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000619 if (result != amt)
620 haderr = result >= 0 ? EIO : errno;
621 }
622 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000623 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 else {
Damien Miller864ea591999-12-15 11:04:25 +1100625 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626 if (result != amt)
627 haderr = result >= 0 ? EIO : errno;
628 statbytes += result;
629 }
630 }
Damien Miller95def091999-11-25 00:26:21 +1100631 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000632 progressmeter(1);
633
634 if (close(fd) < 0 && !haderr)
635 haderr = errno;
636 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000637 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000638 else
639 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100640 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000641 }
642}
643
644void
645rsource(name, statp)
646 char *name;
647 struct stat *statp;
648{
649 DIR *dirp;
650 struct dirent *dp;
651 char *last, *vect[1], path[1100];
652
653 if (!(dirp = opendir(name))) {
654 run_err("%s: %s", name, strerror(errno));
655 return;
656 }
657 last = strrchr(name, '/');
658 if (last == 0)
659 last = name;
660 else
661 last++;
662 if (pflag) {
Damien Miller95def091999-11-25 00:26:21 +1100663 (void) sprintf(path, "T%lu 0 %lu 0\n",
Damien Millerad833b32000-08-23 10:46:23 +1000664 (unsigned long) statp->st_mtime,
665 (unsigned long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000666 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 if (response() < 0) {
668 closedir(dirp);
669 return;
670 }
671 }
Damien Miller95def091999-11-25 00:26:21 +1100672 (void) sprintf(path, "D%04o %d %.1024s\n",
Damien Millerad833b32000-08-23 10:46:23 +1000673 (unsigned int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100674 if (verbose_mode)
675 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000676 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000677 if (response() < 0) {
678 closedir(dirp);
679 return;
680 }
681 while ((dp = readdir(dirp))) {
682 if (dp->d_ino == 0)
683 continue;
684 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
685 continue;
686 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
687 run_err("%s/%s: name too long", name, dp->d_name);
688 continue;
689 }
Damien Miller95def091999-11-25 00:26:21 +1100690 (void) sprintf(path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 vect[0] = path;
692 source(1, vect);
693 }
Damien Miller95def091999-11-25 00:26:21 +1100694 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000695 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100696 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697}
698
699void
700sink(argc, argv)
701 int argc;
702 char *argv[];
703{
704 static BUF buffer;
705 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100706 enum {
707 YES, NO, DISPLAYED
708 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 BUF *bp;
710 off_t i, j;
711 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100712 off_t size;
713 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100715 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100716 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717
718#define SCREWUP(str) { why = str; goto screwup; }
719
720 setimes = targisdir = 0;
721 mask = umask(0);
722 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100723 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000724 if (argc != 1) {
725 run_err("ambiguous target");
726 exit(1);
727 }
728 targ = *argv;
729 if (targetshouldbedirectory)
730 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100731
Damien Miller35dabd02000-05-01 21:10:33 +1000732 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
734 targisdir = 1;
735 for (first = 1;; first = 0) {
736 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000737 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738 return;
739 if (*cp++ == '\n')
740 SCREWUP("unexpected <newline>");
741 do {
Damien Millere247cc42000-05-07 12:03:14 +1000742 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743 SCREWUP("lost connection");
744 *cp++ = ch;
745 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
746 *cp = 0;
747
748 if (buf[0] == '\01' || buf[0] == '\02') {
749 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000750 (void) atomicio(write, STDERR_FILENO,
Damien Miller95def091999-11-25 00:26:21 +1100751 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752 if (buf[0] == '\02')
753 exit(1);
754 ++errs;
755 continue;
756 }
757 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000758 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759 return;
760 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000761 if (ch == '\n')
762 *--cp = 0;
763
764#define getnum(t) (t) = 0; \
765 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
766 cp = buf;
767 if (*cp == 'T') {
768 setimes++;
769 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100770 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771 if (*cp++ != ' ')
772 SCREWUP("mtime.sec not delimited");
773 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100774 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775 if (*cp++ != ' ')
776 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100777 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778 if (*cp++ != ' ')
779 SCREWUP("atime.sec not delimited");
780 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100781 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782 if (*cp++ != '\0')
783 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000784 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785 continue;
786 }
787 if (*cp != 'C' && *cp != 'D') {
788 /*
789 * Check for the case "rcp remote:foo\* local:bar".
790 * In this case, the line "No match." can be returned
791 * by the shell before the rcp command on the remote is
792 * executed so the ^Aerror_message convention isn't
793 * followed.
794 */
795 if (first) {
796 run_err("%s", cp);
797 exit(1);
798 }
799 SCREWUP("expected control record");
800 }
801 mode = 0;
802 for (++cp; cp < buf + 5; cp++) {
803 if (*cp < '0' || *cp > '7')
804 SCREWUP("bad mode");
805 mode = (mode << 3) | (*cp - '0');
806 }
807 if (*cp++ != ' ')
808 SCREWUP("mode not delimited");
809
Damien Miller95def091999-11-25 00:26:21 +1100810 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811 size = size * 10 + (*cp++ - '0');
812 if (*cp++ != ' ')
813 SCREWUP("size not delimited");
814 if (targisdir) {
815 static char *namebuf;
816 static int cursize;
817 size_t need;
818
819 need = strlen(targ) + strlen(cp) + 250;
820 if (need > cursize)
Damien Miller95def091999-11-25 00:26:21 +1100821 namebuf = xmalloc(need);
822 (void) sprintf(namebuf, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000823 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824 np = namebuf;
825 } else
826 np = targ;
827 curfile = cp;
828 exists = stat(np, &stb) == 0;
829 if (buf[0] == 'D') {
830 int mod_flag = pflag;
831 if (exists) {
832 if (!S_ISDIR(stb.st_mode)) {
833 errno = ENOTDIR;
834 goto bad;
835 }
836 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100837 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838 } else {
Damien Miller95def091999-11-25 00:26:21 +1100839 /* Handle copying from a read-only
840 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 mod_flag = 1;
842 if (mkdir(np, mode | S_IRWXU) < 0)
843 goto bad;
844 }
845 vect[0] = np;
846 sink(1, vect);
847 if (setimes) {
848 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100849 if (utimes(np, tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100850 run_err("%s: set times: %s",
851 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852 }
853 if (mod_flag)
Damien Miller95def091999-11-25 00:26:21 +1100854 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855 continue;
856 }
857 omode = mode;
858 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100859 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860bad: run_err("%s: %s", np, strerror(errno));
861 continue;
862 }
Damien Miller35dabd02000-05-01 21:10:33 +1000863 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100865 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 continue;
867 }
868 cp = bp->buf;
869 wrerr = NO;
870
871 if (showprogress) {
872 totalbytes = size;
873 progressmeter(-1);
874 }
875 statbytes = 0;
876 for (count = i = 0; i < size; i += 4096) {
877 amt = 4096;
878 if (i + amt > size)
879 amt = size - i;
880 count += amt;
881 do {
Damien Millere247cc42000-05-07 12:03:14 +1000882 j = atomicio(read, remin, cp, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883 if (j <= 0) {
884 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100885 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 exit(1);
887 }
888 amt -= j;
889 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100890 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 } while (amt > 0);
892 if (count == bp->cnt) {
893 /* Keep reading so we stay sync'd up. */
894 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000895 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 if (j != count) {
897 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100898 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899 }
900 }
901 count = 0;
902 cp = bp->buf;
903 }
904 }
905 if (showprogress)
906 progressmeter(1);
907 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000908 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100910 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911 }
912#if 0
913 if (ftruncate(ofd, size)) {
914 run_err("%s: truncate: %s", np, strerror(errno));
915 wrerr = DISPLAYED;
916 }
917#endif
918 if (pflag) {
919 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100920#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100922#else /* HAVE_FCHMOD */
923 if (chmod(np, omode))
924#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100926 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927 } else {
928 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100929#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100931#else /* HAVE_FCHMOD */
932 if (chmod(np, omode & ~mask))
933#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100935 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 }
Damien Millerbe484b52000-07-15 14:14:16 +1000937 if (close(ofd) == -1) {
938 wrerr = YES;
939 wrerrno = errno;
940 }
Damien Miller95def091999-11-25 00:26:21 +1100941 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 if (setimes && wrerr == NO) {
943 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100944 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100946 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947 wrerr = DISPLAYED;
948 }
949 }
Damien Miller95def091999-11-25 00:26:21 +1100950 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951 case YES:
952 run_err("%s: %s", np, strerror(wrerrno));
953 break;
954 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000955 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956 break;
957 case DISPLAYED:
958 break;
959 }
960 }
961screwup:
962 run_err("protocol error: %s", why);
963 exit(1);
964}
965
966int
967response()
968{
969 char ch, *cp, resp, rbuf[2048];
970
Damien Millere247cc42000-05-07 12:03:14 +1000971 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972 lostconn(0);
973
974 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100975 switch (resp) {
976 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977 return (0);
978 default:
979 *cp++ = resp;
980 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100981 case 1: /* error, followed by error msg */
982 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983 do {
Damien Millere247cc42000-05-07 12:03:14 +1000984 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985 lostconn(0);
986 *cp++ = ch;
987 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
988
989 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000990 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991 ++errs;
992 if (resp == 1)
993 return (-1);
994 exit(1);
995 }
996 /* NOTREACHED */
997}
998
999void
1000usage()
1001{
Damien Millerad833b32000-08-23 10:46:23 +10001002 (void) fprintf(stderr, "usage: scp "
1003 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
1004 " scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005 exit(1);
1006}
1007
1008void
Damien Miller95def091999-11-25 00:26:21 +11001009run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010{
1011 static FILE *fp;
1012 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
1014 ++errs;
1015 if (fp == NULL && !(fp = fdopen(remout, "w")))
1016 return;
Damien Miller95def091999-11-25 00:26:21 +11001017 (void) fprintf(fp, "%c", 0x01);
1018 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +11001019 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001020 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001021 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001022 (void) fprintf(fp, "\n");
1023 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Damien Miller95def091999-11-25 00:26:21 +11001025 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001026 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001027 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001028 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001029 fprintf(stderr, "\n");
1030 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031}
1032
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033char *
1034colon(cp)
1035 char *cp;
1036{
Damien Miller34132e52000-01-14 15:45:46 +11001037 int flag = 0;
1038
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039 if (*cp == ':') /* Leading colon is part of file name. */
1040 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001041 if (*cp == '[')
1042 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043
1044 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001045 if (*cp == '@' && *(cp+1) == '[')
1046 flag = 1;
1047 if (*cp == ']' && *(cp+1) == ':' && flag)
1048 return (cp+1);
1049 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050 return (cp);
1051 if (*cp == '/')
1052 return (0);
1053 }
1054 return (0);
1055}
1056
1057void
1058verifydir(cp)
1059 char *cp;
1060{
1061 struct stat stb;
1062
1063 if (!stat(cp, &stb)) {
1064 if (S_ISDIR(stb.st_mode))
1065 return;
1066 errno = ENOTDIR;
1067 }
1068 run_err("%s: %s", cp, strerror(errno));
1069 exit(1);
1070}
1071
1072int
1073okname(cp0)
1074 char *cp0;
1075{
1076 int c;
1077 char *cp;
1078
1079 cp = cp0;
1080 do {
1081 c = *cp;
1082 if (c & 0200)
1083 goto bad;
1084 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1085 goto bad;
1086 } while (*++cp);
1087 return (1);
1088
Damien Miller98c7ad62000-03-09 21:27:49 +11001089bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090 return (0);
1091}
1092
1093BUF *
1094allocbuf(bp, fd, blksize)
1095 BUF *bp;
1096 int fd, blksize;
1097{
1098 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001099#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001100 struct stat stb;
1101
1102 if (fstat(fd, &stb) < 0) {
1103 run_err("fstat: %s", strerror(errno));
1104 return (0);
1105 }
Damien Miller95def091999-11-25 00:26:21 +11001106 if (stb.st_blksize == 0)
1107 size = blksize;
1108 else
1109 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001110 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001111#else /* HAVE_ST_BLKSIZE */
1112 size = blksize;
1113#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114 if (bp->cnt >= size)
1115 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001116 if (bp->buf == NULL)
1117 bp->buf = xmalloc(size);
1118 else
1119 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120 bp->cnt = size;
1121 return (bp);
1122}
1123
1124void
1125lostconn(signo)
1126 int signo;
1127{
1128 if (!iamremote)
1129 fprintf(stderr, "lost connection\n");
1130 exit(1);
1131}
1132
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133
1134void
1135alarmtimer(int wait)
1136{
Damien Miller95def091999-11-25 00:26:21 +11001137 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138
Damien Miller95def091999-11-25 00:26:21 +11001139 itv.it_value.tv_sec = wait;
1140 itv.it_value.tv_usec = 0;
1141 itv.it_interval = itv.it_value;
1142 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143}
1144
1145void
Damien Miller7684ee12000-03-17 23:40:15 +11001146updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147{
1148 int save_errno = errno;
1149
1150 progressmeter(0);
1151 errno = save_errno;
1152}
1153
Damien Miller81428f91999-11-18 09:28:11 +11001154int
1155foregroundproc()
1156{
1157 static pid_t pgrp = -1;
1158 int ctty_pgrp;
1159
1160 if (pgrp == -1)
1161 pgrp = getpgrp();
1162
Damien Millerbac2d8a2000-09-05 16:13:06 +11001163#ifdef HAVE_CYGWIN
1164 /*
1165 * Cygwin only supports tcgetpgrp() for getting the controlling tty
1166 * currently.
1167 */
1168 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1169 ctty_pgrp == pgrp);
1170#else
Damien Miller95def091999-11-25 00:26:21 +11001171 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1172 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001173#endif
Damien Miller81428f91999-11-18 09:28:11 +11001174}
1175
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176void
1177progressmeter(int flag)
1178{
1179 static const char prefixes[] = " KMGTP";
1180 static struct timeval lastupdate;
1181 static off_t lastsize;
1182 struct timeval now, td, wait;
1183 off_t cursize, abbrevsize;
1184 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001185 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186 char buf[256];
1187
1188 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001189 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190 lastupdate = start;
1191 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001192 }
Damien Miller81428f91999-11-18 09:28:11 +11001193 if (foregroundproc() == 0)
1194 return;
1195
Damien Miller95def091999-11-25 00:26:21 +11001196 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001198 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001199 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200 ratio = MAX(ratio, 0);
1201 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001202 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 ratio = 100;
1204
Damien Miller95def091999-11-25 00:26:21 +11001205 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206
1207 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001208 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209 if (barlength > 0) {
1210 i = barlength * ratio / 100;
1211 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001212 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214 i = 0;
1215 abbrevsize = cursize;
1216 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1217 i++;
1218 abbrevsize >>= 10;
1219 }
Damien Miller864ea591999-12-15 11:04:25 +11001220 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ",
1221 (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
Damien Miller95def091999-11-25 00:26:21 +11001222 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223
1224 timersub(&now, &lastupdate, &wait);
1225 if (cursize > lastsize) {
1226 lastupdate = now;
1227 lastsize = cursize;
1228 if (wait.tv_sec >= STALLTIME) {
1229 start.tv_sec += wait.tv_sec;
1230 start.tv_usec += wait.tv_usec;
1231 }
1232 wait.tv_sec = 0;
1233 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234 timersub(&now, &start, &td);
1235 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1236
1237 if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
1238 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001239 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240 } else if (wait.tv_sec >= STALLTIME) {
1241 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller95def091999-11-25 00:26:21 +11001242 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001244 if (flag != 1)
1245 remaining =
1246 (int)(totalbytes / (statbytes / elapsed) - elapsed);
1247 else
1248 remaining = elapsed;
1249
Damien Miller01ab4a21999-10-28 15:23:30 +10001250 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251 if (i)
1252 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001253 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254 else
1255 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001256 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257 i = remaining % 3600;
1258 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001259 "%02d:%02d%s", i / 60, i % 60,
1260 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261 }
1262 atomicio(write, fileno(stdout), buf, strlen(buf));
1263
1264 if (flag == -1) {
Damien Miller864ea591999-12-15 11:04:25 +11001265 struct sigaction sa;
1266 sa.sa_handler = updateprogressmeter;
Damien Miller77aba9d2000-08-30 10:11:30 +11001267 sigemptyset((sigset_t *)&sa.sa_mask);
Damien Miller615f9392000-05-17 22:53:33 +10001268#ifdef SA_RESTART
Damien Miller864ea591999-12-15 11:04:25 +11001269 sa.sa_flags = SA_RESTART;
Damien Miller615f9392000-05-17 22:53:33 +10001270#endif
Damien Miller864ea591999-12-15 11:04:25 +11001271 sigaction(SIGALRM, &sa, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272 alarmtimer(1);
1273 } else if (flag == 1) {
1274 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001275 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276 statbytes = 0;
1277 }
1278}
1279
1280int
1281getttywidth(void)
1282{
1283 struct winsize winsize;
1284
1285 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001286 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287 else
Damien Miller95def091999-11-25 00:26:21 +11001288 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289}