blob: 71fa299db4abd7316408f68d2069165af1839ed9 [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 Lindstrom2772a3f2001-08-06 21:17:12 +000078RCSID("$OpenBSD: scp.c,v 1.78 2001/07/27 17:26:16 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "atomicio.h"
82#include "pathnames.h"
83#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +000084#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrom49a79c02000-11-17 03:47:20 +000086#ifdef HAVE___PROGNAME
87extern char *__progname;
88#else
89char *__progname;
90#endif
91
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092/* For progressmeter() -- number of seconds before xfer considered "stalled" */
93#define STALLTIME 5
94
Damien Miller484118e2000-07-02 19:13:56 +100095/* Progress meter bar */
96#define BAR \
97 "************************************************************"\
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"
101#define MAX_BARLENGTH (sizeof(BAR) - 1)
102
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103/* Visual statistics about files as they are transferred. */
104void progressmeter(int);
105
106/* Returns width of the terminal (for progress meter calculations). */
107int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000108int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Ben Lindstrom387c4722001-05-08 20:27:25 +0000110/* Struct for addargs */
111arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +1100112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113/* Time a transfer started. */
114static struct timeval start;
115
116/* Number of bytes of current file transferred so far. */
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000117volatile off_t statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
119/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100120off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122/* Name of current file being transferred. */
123char *curfile;
124
125/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100126int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128/* This is set to zero if the progressmeter is not desired. */
129int showprogress = 1;
130
Damien Millerad833b32000-08-23 10:46:23 +1000131/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000132char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
135 * This function executes the given command as the specified user on the
136 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
137 * assigns the input and output file descriptors on success.
138 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller4af51302000-04-16 11:18:38 +1000140int
Damien Millerad833b32000-08-23 10:46:23 +1000141do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142{
Damien Miller95def091999-11-25 00:26:21 +1100143 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000146 fprintf(stderr,
147 "Executing: program %s host %s, user %s, command %s\n",
148 ssh_program, host,
149 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller5428f641999-11-25 11:54:57 +1100151 /*
152 * Reserve two descriptors so that the real pipes won't get
153 * descriptors 0 and 1 because that will screw up dup2 below.
154 */
Damien Miller95def091999-11-25 00:26:21 +1100155 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 /* Create a socket pair for communicating with ssh. */
158 if (pipe(pin) < 0)
159 fatal("pipe: %s", strerror(errno));
160 if (pipe(pout) < 0)
161 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Damien Miller95def091999-11-25 00:26:21 +1100163 /* Free the reserved descriptors. */
164 close(reserved[0]);
165 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller95def091999-11-25 00:26:21 +1100167 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100168 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100169 /* Child. */
170 close(pin[1]);
171 close(pout[0]);
172 dup2(pin[0], 0);
173 dup2(pout[1], 1);
174 close(pin[0]);
175 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176
Damien Miller874d77b2000-10-14 16:23:11 +1100177 args.list[0] = ssh_program;
178 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000179 addargs(&args, "-l%s", remuser);
180 addargs(&args, "%s", host);
181 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100182
Damien Miller874d77b2000-10-14 16:23:11 +1100183 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000184 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100185 exit(1);
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
Ben Lindstrombba81212001-06-25 05:01:22 +0000215int main(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100216int response(void);
217void rsource(char *, struct stat *);
218void sink(int, char *[]);
219void source(int, char *[]);
220void tolocal(int, char *[]);
221void toremote(char *, int, char *[]);
222void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
224int
225main(argc, argv)
226 int argc;
227 char *argv[];
228{
229 int ch, fflag, tflag;
230 char *targ;
231 extern char *optarg;
232 extern int optind;
233
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000234 __progname = get_progname(argv[0]);
235
Damien Miller874d77b2000-10-14 16:23:11 +1100236 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000237 addargs(&args, "ssh"); /* overwritten with ssh_program */
238 addargs(&args, "-x");
239 addargs(&args, "-oFallBackToRsh no");
Damien Miller874d77b2000-10-14 16:23:11 +1100240
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000242 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -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 Lindstrom387c4722001-05-08 20:27:25 +0000253 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100254 break;
255 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000256 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100257 break;
258 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000259 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100260 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261 case 'p':
262 pflag = 1;
263 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264 case 'r':
265 iamrecursive = 1;
266 break;
Damien Millerad833b32000-08-23 10:46:23 +1000267 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100268 ssh_program = xstrdup(optarg);
269 break;
270 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000271 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100272 verbose_mode = 1;
273 break;
274 case 'q':
275 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000276 break;
277
Damien Miller95def091999-11-25 00:26:21 +1100278 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279 case 'd':
280 targetshouldbedirectory = 1;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283 iamremote = 1;
284 fflag = 1;
285 break;
Damien Miller95def091999-11-25 00:26:21 +1100286 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287 iamremote = 1;
288 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000289#ifdef HAVE_CYGWIN
290 setmode(0, O_BINARY);
291#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293 default:
294 usage();
295 }
296 argc -= optind;
297 argv += optind;
298
299 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100300 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301
Damien Miller95def091999-11-25 00:26:21 +1100302 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303 showprogress = 0;
304
305 remin = STDIN_FILENO;
306 remout = STDOUT_FILENO;
307
Kevin Stevesef4eea92001-02-05 12:42:17 +0000308 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100309 /* Follow "protocol", send data. */
310 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311 source(argc, argv);
312 exit(errs != 0);
313 }
Damien Miller95def091999-11-25 00:26:21 +1100314 if (tflag) {
315 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316 sink(argc, argv);
317 exit(errs != 0);
318 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319 if (argc < 2)
320 usage();
321 if (argc > 2)
322 targetshouldbedirectory = 1;
323
324 remin = remout = -1;
325 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000326 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
327 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000328 iamrecursive ? " -r" : "", pflag ? " -p" : "",
329 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330
Damien Miller95def091999-11-25 00:26:21 +1100331 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
333 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
334 toremote(targ, argc, argv);
335 else {
Damien Miller95def091999-11-25 00:26:21 +1100336 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337 if (targetshouldbedirectory)
338 verifydir(argv[argc - 1]);
339 }
340 exit(errs != 0);
341}
342
343void
344toremote(targ, argc, argv)
345 char *targ, *argv[];
346 int argc;
347{
348 int i, len;
349 char *bp, *host, *src, *suser, *thost, *tuser;
350
351 *targ++ = 0;
352 if (*targ == 0)
353 targ = ".";
354
355 if ((thost = strchr(argv[argc - 1], '@'))) {
356 /* user@host */
357 *thost++ = 0;
358 tuser = argv[argc - 1];
359 if (*tuser == '\0')
360 tuser = NULL;
361 else if (!okname(tuser))
362 exit(1);
363 } else {
364 thost = argv[argc - 1];
365 tuser = NULL;
366 }
367
368 for (i = 0; i < argc - 1; i++) {
369 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100370 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371 *src++ = 0;
372 if (*src == 0)
373 src = ".";
374 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000375 len = strlen(ssh_program) + strlen(argv[i]) +
376 strlen(src) + (tuser ? strlen(tuser) : 0) +
377 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100378 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379 if (host) {
380 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100381 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382 suser = argv[i];
383 if (*suser == '\0')
384 suser = pwd->pw_name;
385 else if (!okname(suser))
386 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000387 snprintf(bp, len,
388 "%s%s -x -o'FallBackToRsh no' -n "
389 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000390 ssh_program, verbose_mode ? " -v" : "",
391 suser, host, cmd, src,
392 tuser ? tuser : "", tuser ? "@" : "",
393 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100394 } else {
395 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000396 snprintf(bp, len,
397 "exec %s%s -x -o'FallBackToRsh no' -n %s "
398 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000399 ssh_program, verbose_mode ? " -v" : "",
400 host, cmd, src,
401 tuser ? tuser : "", tuser ? "@" : "",
402 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100403 }
Damien Miller95def091999-11-25 00:26:21 +1100404 if (verbose_mode)
405 fprintf(stderr, "Executing: %s\n", bp);
406 (void) system(bp);
407 (void) xfree(bp);
408 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409 if (remin == -1) {
410 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100411 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000412 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100413 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000414 if (do_cmd(host, tuser, bp, &remin,
415 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100416 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417 if (response() < 0)
418 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100419 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420 }
Damien Miller95def091999-11-25 00:26:21 +1100421 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 }
423 }
424}
425
426void
427tolocal(argc, argv)
428 int argc;
429 char *argv[];
430{
431 int i, len;
432 char *bp, *host, *src, *suser;
433
434 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100435 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000437 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000438 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000439 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000440 iamrecursive ? " -r" : "", pflag ? " -p" : "",
441 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100442 if (verbose_mode)
443 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444 if (system(bp))
445 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100446 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447 continue;
448 }
449 *src++ = 0;
450 if (*src == 0)
451 src = ".";
452 if ((host = strchr(argv[i], '@')) == NULL) {
453 host = argv[i];
454 suser = NULL;
455 } else {
456 *host++ = 0;
457 suser = argv[i];
458 if (*suser == '\0')
459 suser = pwd->pw_name;
460 else if (!okname(suser))
461 continue;
462 }
Damien Miller34132e52000-01-14 15:45:46 +1100463 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100465 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000466 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000467 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100468 (void) xfree(bp);
469 ++errs;
470 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 }
Damien Miller95def091999-11-25 00:26:21 +1100472 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100474 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 remin = remout = -1;
476 }
477}
478
479void
480source(argc, argv)
481 int argc;
482 char *argv[];
483{
484 struct stat stb;
485 static BUF buffer;
486 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000487 off_t i, amt, result;
488 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000490 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000491
492 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100493 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000495 len = strlen(name);
496 while (len > 1 && name[len-1] == '/')
497 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498 if ((fd = open(name, O_RDONLY, 0)) < 0)
499 goto syserr;
500 if (fstat(fd, &stb) < 0) {
501syserr: run_err("%s: %s", name, strerror(errno));
502 goto next;
503 }
504 switch (stb.st_mode & S_IFMT) {
505 case S_IFREG:
506 break;
507 case S_IFDIR:
508 if (iamrecursive) {
509 rsource(name, &stb);
510 goto next;
511 }
512 /* FALLTHROUGH */
513 default:
514 run_err("%s: not a regular file", name);
515 goto next;
516 }
517 if ((last = strrchr(name, '/')) == NULL)
518 last = name;
519 else
520 ++last;
521 curfile = last;
522 if (pflag) {
523 /*
524 * Make it compatible with possible future
525 * versions expecting microseconds.
526 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000527 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000528 (u_long) stb.st_mtime,
529 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000530 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000531 if (response() < 0)
532 goto next;
533 }
534#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100535#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000536 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000537 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000538 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100539#else
540 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000541 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100542 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000543 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100544#endif
545
Damien Miller95def091999-11-25 00:26:21 +1100546 if (verbose_mode) {
547 fprintf(stderr, "Sending file modes: %s", buf);
548 fflush(stderr);
549 }
Damien Miller35dabd02000-05-01 21:10:33 +1000550 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551 if (response() < 0)
552 goto next;
553 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100554next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 continue;
556 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 if (showprogress) {
558 totalbytes = stb.st_size;
559 progressmeter(-1);
560 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561 /* Keep writing after an error so that we stay sync'd up. */
562 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
563 amt = bp->cnt;
564 if (i + amt > stb.st_size)
565 amt = stb.st_size - i;
566 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000567 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568 if (result != amt)
569 haderr = result >= 0 ? EIO : errno;
570 }
571 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000572 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 else {
Damien Miller864ea591999-12-15 11:04:25 +1100574 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000575 if (result != amt)
576 haderr = result >= 0 ? EIO : errno;
577 statbytes += result;
578 }
579 }
Damien Miller95def091999-11-25 00:26:21 +1100580 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581 progressmeter(1);
582
583 if (close(fd) < 0 && !haderr)
584 haderr = errno;
585 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000586 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587 else
588 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100589 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590 }
591}
592
593void
594rsource(name, statp)
595 char *name;
596 struct stat *statp;
597{
598 DIR *dirp;
599 struct dirent *dp;
600 char *last, *vect[1], path[1100];
601
602 if (!(dirp = opendir(name))) {
603 run_err("%s: %s", name, strerror(errno));
604 return;
605 }
606 last = strrchr(name, '/');
607 if (last == 0)
608 last = name;
609 else
610 last++;
611 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000612 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000613 (u_long) statp->st_mtime,
614 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000615 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616 if (response() < 0) {
617 closedir(dirp);
618 return;
619 }
620 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000621 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000622 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100623 if (verbose_mode)
624 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000625 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000626 if (response() < 0) {
627 closedir(dirp);
628 return;
629 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000630 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631 if (dp->d_ino == 0)
632 continue;
633 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
634 continue;
635 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
636 run_err("%s/%s: name too long", name, dp->d_name);
637 continue;
638 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000639 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640 vect[0] = path;
641 source(1, vect);
642 }
Damien Miller95def091999-11-25 00:26:21 +1100643 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000644 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100645 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646}
647
648void
649sink(argc, argv)
650 int argc;
651 char *argv[];
652{
653 static BUF buffer;
654 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100655 enum {
656 YES, NO, DISPLAYED
657 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658 BUF *bp;
659 off_t i, j;
660 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100661 off_t size;
662 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100664 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665
Ben Lindstromf719a202001-04-14 23:14:22 +0000666#define atime tv[0]
667#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000668#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000669
670 setimes = targisdir = 0;
671 mask = umask(0);
672 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100673 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674 if (argc != 1) {
675 run_err("ambiguous target");
676 exit(1);
677 }
678 targ = *argv;
679 if (targetshouldbedirectory)
680 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100681
Damien Miller35dabd02000-05-01 21:10:33 +1000682 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
684 targisdir = 1;
685 for (first = 1;; first = 0) {
686 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000687 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688 return;
689 if (*cp++ == '\n')
690 SCREWUP("unexpected <newline>");
691 do {
Damien Millere247cc42000-05-07 12:03:14 +1000692 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693 SCREWUP("lost connection");
694 *cp++ = ch;
695 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
696 *cp = 0;
697
698 if (buf[0] == '\01' || buf[0] == '\02') {
699 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000700 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000701 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702 if (buf[0] == '\02')
703 exit(1);
704 ++errs;
705 continue;
706 }
707 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000708 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 return;
710 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711 if (ch == '\n')
712 *--cp = 0;
713
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714 cp = buf;
715 if (*cp == 'T') {
716 setimes++;
717 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000718 mtime.tv_sec = strtol(cp, &cp, 10);
719 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000721 mtime.tv_usec = strtol(cp, &cp, 10);
722 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000723 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000724 atime.tv_sec = strtol(cp, &cp, 10);
725 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000726 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000727 atime.tv_usec = strtol(cp, &cp, 10);
728 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000730 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731 continue;
732 }
733 if (*cp != 'C' && *cp != 'D') {
734 /*
735 * Check for the case "rcp remote:foo\* local:bar".
736 * In this case, the line "No match." can be returned
737 * by the shell before the rcp command on the remote is
738 * executed so the ^Aerror_message convention isn't
739 * followed.
740 */
741 if (first) {
742 run_err("%s", cp);
743 exit(1);
744 }
745 SCREWUP("expected control record");
746 }
747 mode = 0;
748 for (++cp; cp < buf + 5; cp++) {
749 if (*cp < '0' || *cp > '7')
750 SCREWUP("bad mode");
751 mode = (mode << 3) | (*cp - '0');
752 }
753 if (*cp++ != ' ')
754 SCREWUP("mode not delimited");
755
Ben Lindstrombd472262001-03-29 00:39:55 +0000756 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757 size = size * 10 + (*cp++ - '0');
758 if (*cp++ != ' ')
759 SCREWUP("size not delimited");
760 if (targisdir) {
761 static char *namebuf;
762 static int cursize;
763 size_t need;
764
765 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000766 if (need > cursize) {
767 if (namebuf)
768 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100769 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000770 cursize = need;
771 }
772 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000773 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774 np = namebuf;
775 } else
776 np = targ;
777 curfile = cp;
778 exists = stat(np, &stb) == 0;
779 if (buf[0] == 'D') {
780 int mod_flag = pflag;
781 if (exists) {
782 if (!S_ISDIR(stb.st_mode)) {
783 errno = ENOTDIR;
784 goto bad;
785 }
786 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100787 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788 } else {
Damien Miller95def091999-11-25 00:26:21 +1100789 /* Handle copying from a read-only
790 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791 mod_flag = 1;
792 if (mkdir(np, mode | S_IRWXU) < 0)
793 goto bad;
794 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000795 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796 sink(1, vect);
797 if (setimes) {
798 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100799 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100800 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100801 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802 }
803 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100804 (void) chmod(vect[0], mode);
805 if (vect[0])
806 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 continue;
808 }
809 omode = mode;
810 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000811 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812bad: run_err("%s: %s", np, strerror(errno));
813 continue;
814 }
Damien Miller35dabd02000-05-01 21:10:33 +1000815 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100817 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818 continue;
819 }
820 cp = bp->buf;
821 wrerr = NO;
822
823 if (showprogress) {
824 totalbytes = size;
825 progressmeter(-1);
826 }
827 statbytes = 0;
828 for (count = i = 0; i < size; i += 4096) {
829 amt = 4096;
830 if (i + amt > size)
831 amt = size - i;
832 count += amt;
833 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100834 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000835 if (j == -1 && (errno == EINTR ||
836 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100837 continue;
838 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000840 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 exit(1);
842 }
843 amt -= j;
844 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100845 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846 } while (amt > 0);
847 if (count == bp->cnt) {
848 /* Keep reading so we stay sync'd up. */
849 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000850 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851 if (j != count) {
852 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100853 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854 }
855 }
856 count = 0;
857 cp = bp->buf;
858 }
859 }
860 if (showprogress)
861 progressmeter(1);
862 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000863 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000864 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100865 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867 if (ftruncate(ofd, size)) {
868 run_err("%s: truncate: %s", np, strerror(errno));
869 wrerr = DISPLAYED;
870 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871 if (pflag) {
872 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100873#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100875#else /* HAVE_FCHMOD */
876 if (chmod(np, omode))
877#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000879 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 } else {
881 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100882#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100884#else /* HAVE_FCHMOD */
885 if (chmod(np, omode & ~mask))
886#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000888 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 }
Damien Millerbe484b52000-07-15 14:14:16 +1000890 if (close(ofd) == -1) {
891 wrerr = YES;
892 wrerrno = errno;
893 }
Damien Miller95def091999-11-25 00:26:21 +1100894 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 if (setimes && wrerr == NO) {
896 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100897 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000899 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 wrerr = DISPLAYED;
901 }
902 }
Damien Miller95def091999-11-25 00:26:21 +1100903 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 case YES:
905 run_err("%s: %s", np, strerror(wrerrno));
906 break;
907 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000908 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909 break;
910 case DISPLAYED:
911 break;
912 }
913 }
914screwup:
915 run_err("protocol error: %s", why);
916 exit(1);
917}
918
919int
920response()
921{
922 char ch, *cp, resp, rbuf[2048];
923
Damien Millere247cc42000-05-07 12:03:14 +1000924 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925 lostconn(0);
926
927 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100928 switch (resp) {
929 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930 return (0);
931 default:
932 *cp++ = resp;
933 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100934 case 1: /* error, followed by error msg */
935 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 do {
Damien Millere247cc42000-05-07 12:03:14 +1000937 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 lostconn(0);
939 *cp++ = ch;
940 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
941
942 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000943 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944 ++errs;
945 if (resp == 1)
946 return (-1);
947 exit(1);
948 }
949 /* NOTREACHED */
950}
951
952void
953usage()
954{
Damien Millerad833b32000-08-23 10:46:23 +1000955 (void) fprintf(stderr, "usage: scp "
Ben Lindstromdc00c932001-07-18 16:50:33 +0000956 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] "
957 "[-o option] f1 f2\n"
Ben Lindstromff2618c2001-03-29 00:43:54 +0000958 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959 exit(1);
960}
961
962void
Damien Miller95def091999-11-25 00:26:21 +1100963run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964{
965 static FILE *fp;
966 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
968 ++errs;
969 if (fp == NULL && !(fp = fdopen(remout, "w")))
970 return;
Damien Miller95def091999-11-25 00:26:21 +1100971 (void) fprintf(fp, "%c", 0x01);
972 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100973 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100974 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100975 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100976 (void) fprintf(fp, "\n");
977 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100980 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100981 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100982 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100983 fprintf(stderr, "\n");
984 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985}
986
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987void
988verifydir(cp)
989 char *cp;
990{
991 struct stat stb;
992
993 if (!stat(cp, &stb)) {
994 if (S_ISDIR(stb.st_mode))
995 return;
996 errno = ENOTDIR;
997 }
998 run_err("%s: %s", cp, strerror(errno));
999 exit(1);
1000}
1001
1002int
1003okname(cp0)
1004 char *cp0;
1005{
1006 int c;
1007 char *cp;
1008
1009 cp = cp0;
1010 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001011 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 if (c & 0200)
1013 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001014 if (!isalpha(c) && !isdigit(c) &&
1015 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016 goto bad;
1017 } while (*++cp);
1018 return (1);
1019
Damien Miller98c7ad62000-03-09 21:27:49 +11001020bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021 return (0);
1022}
1023
1024BUF *
1025allocbuf(bp, fd, blksize)
1026 BUF *bp;
1027 int fd, blksize;
1028{
1029 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001030#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031 struct stat stb;
1032
1033 if (fstat(fd, &stb) < 0) {
1034 run_err("fstat: %s", strerror(errno));
1035 return (0);
1036 }
Damien Miller95def091999-11-25 00:26:21 +11001037 if (stb.st_blksize == 0)
1038 size = blksize;
1039 else
1040 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001041 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001042#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001043 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001044#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045 if (bp->cnt >= size)
1046 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001047 if (bp->buf == NULL)
1048 bp->buf = xmalloc(size);
1049 else
1050 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051 bp->cnt = size;
1052 return (bp);
1053}
1054
1055void
1056lostconn(signo)
1057 int signo;
1058{
1059 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001060 write(STDERR_FILENO, "lost connection\n", 16);
1061 if (signo)
1062 _exit(1);
1063 else
1064 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065}
1066
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Ben Lindstrombba81212001-06-25 05:01:22 +00001068static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069alarmtimer(int wait)
1070{
Damien Miller95def091999-11-25 00:26:21 +11001071 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072
Damien Miller95def091999-11-25 00:26:21 +11001073 itv.it_value.tv_sec = wait;
1074 itv.it_value.tv_usec = 0;
1075 itv.it_interval = itv.it_value;
1076 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077}
1078
Ben Lindstrombba81212001-06-25 05:01:22 +00001079static void
Damien Miller7684ee12000-03-17 23:40:15 +11001080updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081{
1082 int save_errno = errno;
1083
1084 progressmeter(0);
1085 errno = save_errno;
1086}
1087
Ben Lindstrombba81212001-06-25 05:01:22 +00001088static int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001089foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001090{
1091 static pid_t pgrp = -1;
1092 int ctty_pgrp;
1093
1094 if (pgrp == -1)
1095 pgrp = getpgrp();
1096
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001097#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001098 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1099 ctty_pgrp == pgrp);
1100#else
Damien Miller95def091999-11-25 00:26:21 +11001101 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1102 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001103#endif
Damien Miller81428f91999-11-18 09:28:11 +11001104}
1105
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106void
1107progressmeter(int flag)
1108{
1109 static const char prefixes[] = " KMGTP";
1110 static struct timeval lastupdate;
1111 static off_t lastsize;
1112 struct timeval now, td, wait;
1113 off_t cursize, abbrevsize;
1114 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001115 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116 char buf[256];
1117
1118 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001119 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120 lastupdate = start;
1121 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001122 }
Damien Miller81428f91999-11-18 09:28:11 +11001123 if (foregroundproc() == 0)
1124 return;
1125
Damien Miller95def091999-11-25 00:26:21 +11001126 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001128 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001129 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130 ratio = MAX(ratio, 0);
1131 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001132 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133 ratio = 100;
1134
Damien Miller95def091999-11-25 00:26:21 +11001135 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136
1137 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001138 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139 if (barlength > 0) {
1140 i = barlength * ratio / 100;
1141 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001142 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144 i = 0;
1145 abbrevsize = cursize;
1146 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1147 i++;
1148 abbrevsize >>= 10;
1149 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001150 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001151 (unsigned long) abbrevsize, prefixes[i],
1152 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153
1154 timersub(&now, &lastupdate, &wait);
1155 if (cursize > lastsize) {
1156 lastupdate = now;
1157 lastsize = cursize;
1158 if (wait.tv_sec >= STALLTIME) {
1159 start.tv_sec += wait.tv_sec;
1160 start.tv_usec += wait.tv_usec;
1161 }
1162 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
Kevin Steves7d00ba42000-12-15 23:03:10 +00001167 if (flag != 1 &&
1168 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001170 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 } else if (wait.tv_sec >= STALLTIME) {
1172 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001173 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001175 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001176 remaining = (int)(totalbytes / (statbytes / elapsed) -
1177 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001178 else
1179 remaining = elapsed;
1180
Damien Miller01ab4a21999-10-28 15:23:30 +10001181 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182 if (i)
1183 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001184 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 else
1186 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001187 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188 i = remaining % 3600;
1189 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001190 "%02d:%02d%s", i / 60, i % 60,
1191 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 }
1193 atomicio(write, fileno(stdout), buf, strlen(buf));
1194
1195 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001196 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197 alarmtimer(1);
1198 } else if (flag == 1) {
1199 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001200 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201 statbytes = 0;
1202 }
1203}
1204
1205int
1206getttywidth(void)
1207{
1208 struct winsize winsize;
1209
1210 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001211 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212 else
Damien Miller95def091999-11-25 00:26:21 +11001213 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214}