blob: 52fd513c6422c45649d564c482640029f40ab133 [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 Lindstrom387c4722001-05-08 20:27:25 +000078RCSID("$OpenBSD: scp.c,v 1.70 2001/05/08 19:45:24 mouring Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "atomicio.h"
82#include "pathnames.h"
83#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +000084#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrom49a79c02000-11-17 03:47:20 +000086#ifdef HAVE___PROGNAME
87extern char *__progname;
88#else
89char *__progname;
90#endif
91
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092/* For progressmeter() -- number of seconds before xfer considered "stalled" */
93#define STALLTIME 5
94
Damien Miller484118e2000-07-02 19:13:56 +100095/* Progress meter bar */
96#define BAR \
97 "************************************************************"\
98 "************************************************************"\
99 "************************************************************"\
100 "************************************************************"
101#define MAX_BARLENGTH (sizeof(BAR) - 1)
102
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103/* Visual statistics about files as they are transferred. */
104void progressmeter(int);
105
106/* Returns width of the terminal (for progress meter calculations). */
107int getttywidth(void);
Damien Millerad833b32000-08-23 10:46:23 +1000108int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Ben Lindstrom387c4722001-05-08 20:27:25 +0000110/* Struct for addargs */
111arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +1100112
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113/* Time a transfer started. */
114static struct timeval start;
115
116/* Number of bytes of current file transferred so far. */
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000117volatile off_t statbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
119/* Total size of current file. */
Damien Millera2d6efe1999-11-13 13:22:46 +1100120off_t totalbytes = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122/* Name of current file being transferred. */
123char *curfile;
124
125/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100126int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128/* This is set to zero if the progressmeter is not desired. */
129int showprogress = 1;
130
Damien Millerad833b32000-08-23 10:46:23 +1000131/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000132char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
135 * This function executes the given command as the specified user on the
136 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
137 * assigns the input and output file descriptors on success.
138 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller4af51302000-04-16 11:18:38 +1000140int
Damien Millerad833b32000-08-23 10:46:23 +1000141do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142{
Damien Miller95def091999-11-25 00:26:21 +1100143 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100146 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
147 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller5428f641999-11-25 11:54:57 +1100149 /*
150 * Reserve two descriptors so that the real pipes won't get
151 * descriptors 0 and 1 because that will screw up dup2 below.
152 */
Damien Miller95def091999-11-25 00:26:21 +1100153 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 /* Create a socket pair for communicating with ssh. */
156 if (pipe(pin) < 0)
157 fatal("pipe: %s", strerror(errno));
158 if (pipe(pout) < 0)
159 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* Free the reserved descriptors. */
162 close(reserved[0]);
163 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Damien Miller95def091999-11-25 00:26:21 +1100165 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100166 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Child. */
168 close(pin[1]);
169 close(pout[0]);
170 dup2(pin[0], 0);
171 dup2(pout[1], 1);
172 close(pin[0]);
173 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174
Damien Miller874d77b2000-10-14 16:23:11 +1100175 args.list[0] = ssh_program;
176 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000177 addargs(&args, "-l%s", remuser);
178 addargs(&args, "%s", host);
179 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100180
Damien Miller874d77b2000-10-14 16:23:11 +1100181 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000182 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100183 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184 }
Damien Miller95def091999-11-25 00:26:21 +1100185 /* Parent. Close the other side, and return the local side. */
186 close(pin[0]);
187 *fdout = pin[1];
188 close(pout[1]);
189 *fdin = pout[0];
190 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}
192
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193typedef struct {
194 int cnt;
195 char *buf;
196} BUF;
197
Damien Miller95def091999-11-25 00:26:21 +1100198BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100199void lostconn(int);
200void nospace(void);
201int okname(char *);
202void run_err(const char *,...);
203void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100206uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207int errs, remin, remout;
208int pflag, iamremote, iamrecursive, targetshouldbedirectory;
209
210#define CMDNEEDS 64
211char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
212
Damien Miller95def091999-11-25 00:26:21 +1100213int response(void);
214void rsource(char *, struct stat *);
215void sink(int, char *[]);
216void source(int, char *[]);
217void tolocal(int, char *[]);
218void toremote(char *, int, char *[]);
219void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220
221int
222main(argc, argv)
223 int argc;
224 char *argv[];
225{
226 int ch, fflag, tflag;
227 char *targ;
228 extern char *optarg;
229 extern int optind;
230
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000231 __progname = get_progname(argv[0]);
232
Damien Miller874d77b2000-10-14 16:23:11 +1100233 args.list = NULL;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000234 addargs(&args, "ssh"); /* overwritten with ssh_program */
235 addargs(&args, "-x");
236 addargs(&args, "-oFallBackToRsh no");
Damien Miller874d77b2000-10-14 16:23:11 +1100237
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000239 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100240 switch (ch) {
241 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100242 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100243 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100244 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000245 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100246 break;
247 case 'o':
248 case 'c':
249 case 'i':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000250 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100251 break;
252 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000253 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100254 break;
255 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000256 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100257 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258 case 'p':
259 pflag = 1;
260 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261 case 'r':
262 iamrecursive = 1;
263 break;
Damien Millerad833b32000-08-23 10:46:23 +1000264 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100265 ssh_program = xstrdup(optarg);
266 break;
267 case 'v':
268 verbose_mode = 1;
269 break;
270 case 'q':
271 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000272 break;
273
Damien Miller95def091999-11-25 00:26:21 +1100274 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275 case 'd':
276 targetshouldbedirectory = 1;
277 break;
Damien Miller95def091999-11-25 00:26:21 +1100278 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279 iamremote = 1;
280 fflag = 1;
281 break;
Damien Miller95def091999-11-25 00:26:21 +1100282 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283 iamremote = 1;
284 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000285#ifdef HAVE_CYGWIN
286 setmode(0, O_BINARY);
287#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289 default:
290 usage();
291 }
292 argc -= optind;
293 argv += optind;
294
295 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100296 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller95def091999-11-25 00:26:21 +1100298 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299 showprogress = 0;
300
301 remin = STDIN_FILENO;
302 remout = STDOUT_FILENO;
303
Kevin Stevesef4eea92001-02-05 12:42:17 +0000304 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100305 /* Follow "protocol", send data. */
306 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307 source(argc, argv);
308 exit(errs != 0);
309 }
Damien Miller95def091999-11-25 00:26:21 +1100310 if (tflag) {
311 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312 sink(argc, argv);
313 exit(errs != 0);
314 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315 if (argc < 2)
316 usage();
317 if (argc > 2)
318 targetshouldbedirectory = 1;
319
320 remin = remout = -1;
321 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000322 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
323 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000324 iamrecursive ? " -r" : "", pflag ? " -p" : "",
325 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller95def091999-11-25 00:26:21 +1100327 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328
329 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
330 toremote(targ, argc, argv);
331 else {
Damien Miller95def091999-11-25 00:26:21 +1100332 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333 if (targetshouldbedirectory)
334 verifydir(argv[argc - 1]);
335 }
336 exit(errs != 0);
337}
338
339void
340toremote(targ, argc, argv)
341 char *targ, *argv[];
342 int argc;
343{
344 int i, len;
345 char *bp, *host, *src, *suser, *thost, *tuser;
346
347 *targ++ = 0;
348 if (*targ == 0)
349 targ = ".";
350
351 if ((thost = strchr(argv[argc - 1], '@'))) {
352 /* user@host */
353 *thost++ = 0;
354 tuser = argv[argc - 1];
355 if (*tuser == '\0')
356 tuser = NULL;
357 else if (!okname(tuser))
358 exit(1);
359 } else {
360 thost = argv[argc - 1];
361 tuser = NULL;
362 }
363
364 for (i = 0; i < argc - 1; i++) {
365 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100366 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367 *src++ = 0;
368 if (*src == 0)
369 src = ".";
370 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000371 len = strlen(ssh_program) + strlen(argv[i]) +
372 strlen(src) + (tuser ? strlen(tuser) : 0) +
373 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100374 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375 if (host) {
376 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100377 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378 suser = argv[i];
379 if (*suser == '\0')
380 suser = pwd->pw_name;
381 else if (!okname(suser))
382 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000383 snprintf(bp, len,
384 "%s%s -x -o'FallBackToRsh no' -n "
385 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000386 ssh_program, verbose_mode ? " -v" : "",
387 suser, host, cmd, src,
388 tuser ? tuser : "", tuser ? "@" : "",
389 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100390 } else {
391 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000392 snprintf(bp, len,
393 "exec %s%s -x -o'FallBackToRsh no' -n %s "
394 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000395 ssh_program, verbose_mode ? " -v" : "",
396 host, cmd, src,
397 tuser ? tuser : "", tuser ? "@" : "",
398 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100399 }
Damien Miller95def091999-11-25 00:26:21 +1100400 if (verbose_mode)
401 fprintf(stderr, "Executing: %s\n", bp);
402 (void) system(bp);
403 (void) xfree(bp);
404 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405 if (remin == -1) {
406 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100407 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000408 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100409 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000410 if (do_cmd(host, tuser, bp, &remin,
411 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100412 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413 if (response() < 0)
414 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100415 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416 }
Damien Miller95def091999-11-25 00:26:21 +1100417 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418 }
419 }
420}
421
422void
423tolocal(argc, argv)
424 int argc;
425 char *argv[];
426{
427 int i, len;
428 char *bp, *host, *src, *suser;
429
430 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100431 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000433 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000435 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000436 iamrecursive ? " -r" : "", pflag ? " -p" : "",
437 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100438 if (verbose_mode)
439 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440 if (system(bp))
441 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100442 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 continue;
444 }
445 *src++ = 0;
446 if (*src == 0)
447 src = ".";
448 if ((host = strchr(argv[i], '@')) == NULL) {
449 host = argv[i];
450 suser = NULL;
451 } else {
452 *host++ = 0;
453 suser = argv[i];
454 if (*suser == '\0')
455 suser = pwd->pw_name;
456 else if (!okname(suser))
457 continue;
458 }
Damien Miller34132e52000-01-14 15:45:46 +1100459 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100461 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000462 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000463 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100464 (void) xfree(bp);
465 ++errs;
466 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 }
Damien Miller95def091999-11-25 00:26:21 +1100468 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100470 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 remin = remout = -1;
472 }
473}
474
475void
476source(argc, argv)
477 int argc;
478 char *argv[];
479{
480 struct stat stb;
481 static BUF buffer;
482 BUF *bp;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000483 off_t i, amt, result;
484 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000486 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487
488 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100489 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000491 len = strlen(name);
492 while (len > 1 && name[len-1] == '/')
493 name[--len] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 if ((fd = open(name, O_RDONLY, 0)) < 0)
495 goto syserr;
496 if (fstat(fd, &stb) < 0) {
497syserr: run_err("%s: %s", name, strerror(errno));
498 goto next;
499 }
500 switch (stb.st_mode & S_IFMT) {
501 case S_IFREG:
502 break;
503 case S_IFDIR:
504 if (iamrecursive) {
505 rsource(name, &stb);
506 goto next;
507 }
508 /* FALLTHROUGH */
509 default:
510 run_err("%s: not a regular file", name);
511 goto next;
512 }
513 if ((last = strrchr(name, '/')) == NULL)
514 last = name;
515 else
516 ++last;
517 curfile = last;
518 if (pflag) {
519 /*
520 * Make it compatible with possible future
521 * versions expecting microseconds.
522 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000523 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000524 (u_long) stb.st_mtime,
525 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000526 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527 if (response() < 0)
528 goto next;
529 }
530#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100531#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000532 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000533 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000534 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100535#else
536 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000537 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100538 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000539 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100540#endif
541
Damien Miller95def091999-11-25 00:26:21 +1100542 if (verbose_mode) {
543 fprintf(stderr, "Sending file modes: %s", buf);
544 fflush(stderr);
545 }
Damien Miller35dabd02000-05-01 21:10:33 +1000546 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547 if (response() < 0)
548 goto next;
549 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100550next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551 continue;
552 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000553 if (showprogress) {
554 totalbytes = stb.st_size;
555 progressmeter(-1);
556 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 /* Keep writing after an error so that we stay sync'd up. */
558 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
559 amt = bp->cnt;
560 if (i + amt > stb.st_size)
561 amt = stb.st_size - i;
562 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000563 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 if (result != amt)
565 haderr = result >= 0 ? EIO : errno;
566 }
567 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000568 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 else {
Damien Miller864ea591999-12-15 11:04:25 +1100570 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571 if (result != amt)
572 haderr = result >= 0 ? EIO : errno;
573 statbytes += result;
574 }
575 }
Damien Miller95def091999-11-25 00:26:21 +1100576 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 progressmeter(1);
578
579 if (close(fd) < 0 && !haderr)
580 haderr = errno;
581 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000582 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583 else
584 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100585 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586 }
587}
588
589void
590rsource(name, statp)
591 char *name;
592 struct stat *statp;
593{
594 DIR *dirp;
595 struct dirent *dp;
596 char *last, *vect[1], path[1100];
597
598 if (!(dirp = opendir(name))) {
599 run_err("%s: %s", name, strerror(errno));
600 return;
601 }
602 last = strrchr(name, '/');
603 if (last == 0)
604 last = name;
605 else
606 last++;
607 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000608 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000609 (u_long) statp->st_mtime,
610 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000611 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612 if (response() < 0) {
613 closedir(dirp);
614 return;
615 }
616 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000617 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000618 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100619 if (verbose_mode)
620 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000621 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000622 if (response() < 0) {
623 closedir(dirp);
624 return;
625 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000626 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000627 if (dp->d_ino == 0)
628 continue;
629 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
630 continue;
631 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
632 run_err("%s/%s: name too long", name, dp->d_name);
633 continue;
634 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000635 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636 vect[0] = path;
637 source(1, vect);
638 }
Damien Miller95def091999-11-25 00:26:21 +1100639 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000640 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100641 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000642}
643
644void
645sink(argc, argv)
646 int argc;
647 char *argv[];
648{
649 static BUF buffer;
650 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100651 enum {
652 YES, NO, DISPLAYED
653 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654 BUF *bp;
655 off_t i, j;
656 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100657 off_t size;
658 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100660 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661
Ben Lindstromf719a202001-04-14 23:14:22 +0000662#define atime tv[0]
663#define mtime tv[1]
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000664#define SCREWUP(str) { why = str; goto screwup; }
665
666 setimes = targisdir = 0;
667 mask = umask(0);
668 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100669 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000670 if (argc != 1) {
671 run_err("ambiguous target");
672 exit(1);
673 }
674 targ = *argv;
675 if (targetshouldbedirectory)
676 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100677
Damien Miller35dabd02000-05-01 21:10:33 +1000678 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
680 targisdir = 1;
681 for (first = 1;; first = 0) {
682 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000683 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684 return;
685 if (*cp++ == '\n')
686 SCREWUP("unexpected <newline>");
687 do {
Damien Millere247cc42000-05-07 12:03:14 +1000688 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689 SCREWUP("lost connection");
690 *cp++ = ch;
691 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
692 *cp = 0;
693
694 if (buf[0] == '\01' || buf[0] == '\02') {
695 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000696 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000697 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698 if (buf[0] == '\02')
699 exit(1);
700 ++errs;
701 continue;
702 }
703 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000704 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705 return;
706 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707 if (ch == '\n')
708 *--cp = 0;
709
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 cp = buf;
711 if (*cp == 'T') {
712 setimes++;
713 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000714 mtime.tv_sec = strtol(cp, &cp, 10);
715 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000717 mtime.tv_usec = strtol(cp, &cp, 10);
718 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000720 atime.tv_sec = strtol(cp, &cp, 10);
721 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000722 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000723 atime.tv_usec = strtol(cp, &cp, 10);
724 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000726 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 continue;
728 }
729 if (*cp != 'C' && *cp != 'D') {
730 /*
731 * Check for the case "rcp remote:foo\* local:bar".
732 * In this case, the line "No match." can be returned
733 * by the shell before the rcp command on the remote is
734 * executed so the ^Aerror_message convention isn't
735 * followed.
736 */
737 if (first) {
738 run_err("%s", cp);
739 exit(1);
740 }
741 SCREWUP("expected control record");
742 }
743 mode = 0;
744 for (++cp; cp < buf + 5; cp++) {
745 if (*cp < '0' || *cp > '7')
746 SCREWUP("bad mode");
747 mode = (mode << 3) | (*cp - '0');
748 }
749 if (*cp++ != ' ')
750 SCREWUP("mode not delimited");
751
Ben Lindstrombd472262001-03-29 00:39:55 +0000752 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753 size = size * 10 + (*cp++ - '0');
754 if (*cp++ != ' ')
755 SCREWUP("size not delimited");
756 if (targisdir) {
757 static char *namebuf;
758 static int cursize;
759 size_t need;
760
761 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000762 if (need > cursize) {
763 if (namebuf)
764 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100765 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000766 cursize = need;
767 }
768 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000769 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000770 np = namebuf;
771 } else
772 np = targ;
773 curfile = cp;
774 exists = stat(np, &stb) == 0;
775 if (buf[0] == 'D') {
776 int mod_flag = pflag;
777 if (exists) {
778 if (!S_ISDIR(stb.st_mode)) {
779 errno = ENOTDIR;
780 goto bad;
781 }
782 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100783 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784 } else {
Damien Miller95def091999-11-25 00:26:21 +1100785 /* Handle copying from a read-only
786 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787 mod_flag = 1;
788 if (mkdir(np, mode | S_IRWXU) < 0)
789 goto bad;
790 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000791 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792 sink(1, vect);
793 if (setimes) {
794 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100795 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100796 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100797 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798 }
799 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100800 (void) chmod(vect[0], mode);
801 if (vect[0])
802 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803 continue;
804 }
805 omode = mode;
806 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100807 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808bad: run_err("%s: %s", np, strerror(errno));
809 continue;
810 }
Damien Miller35dabd02000-05-01 21:10:33 +1000811 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100813 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814 continue;
815 }
816 cp = bp->buf;
817 wrerr = NO;
818
819 if (showprogress) {
820 totalbytes = size;
821 progressmeter(-1);
822 }
823 statbytes = 0;
824 for (count = i = 0; i < size; i += 4096) {
825 amt = 4096;
826 if (i + amt > size)
827 amt = size - i;
828 count += amt;
829 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100830 j = read(remin, cp, amt);
831 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
832 continue;
833 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000835 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000836 exit(1);
837 }
838 amt -= j;
839 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100840 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 } while (amt > 0);
842 if (count == bp->cnt) {
843 /* Keep reading so we stay sync'd up. */
844 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000845 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846 if (j != count) {
847 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100848 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849 }
850 }
851 count = 0;
852 cp = bp->buf;
853 }
854 }
855 if (showprogress)
856 progressmeter(1);
857 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000858 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100860 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861 }
862#if 0
863 if (ftruncate(ofd, size)) {
864 run_err("%s: truncate: %s", np, strerror(errno));
865 wrerr = DISPLAYED;
866 }
867#endif
868 if (pflag) {
869 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100870#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100872#else /* HAVE_FCHMOD */
873 if (chmod(np, omode))
874#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000876 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877 } else {
878 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100879#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100881#else /* HAVE_FCHMOD */
882 if (chmod(np, omode & ~mask))
883#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000885 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000886 }
Damien Millerbe484b52000-07-15 14:14:16 +1000887 if (close(ofd) == -1) {
888 wrerr = YES;
889 wrerrno = errno;
890 }
Damien Miller95def091999-11-25 00:26:21 +1100891 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892 if (setimes && wrerr == NO) {
893 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100894 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000896 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897 wrerr = DISPLAYED;
898 }
899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 case YES:
902 run_err("%s: %s", np, strerror(wrerrno));
903 break;
904 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000905 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 break;
907 case DISPLAYED:
908 break;
909 }
910 }
911screwup:
912 run_err("protocol error: %s", why);
913 exit(1);
914}
915
916int
917response()
918{
919 char ch, *cp, resp, rbuf[2048];
920
Damien Millere247cc42000-05-07 12:03:14 +1000921 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 lostconn(0);
923
924 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100925 switch (resp) {
926 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927 return (0);
928 default:
929 *cp++ = resp;
930 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100931 case 1: /* error, followed by error msg */
932 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 do {
Damien Millere247cc42000-05-07 12:03:14 +1000934 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935 lostconn(0);
936 *cp++ = ch;
937 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
938
939 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000940 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941 ++errs;
942 if (resp == 1)
943 return (-1);
944 exit(1);
945 }
946 /* NOTREACHED */
947}
948
949void
950usage()
951{
Damien Millerad833b32000-08-23 10:46:23 +1000952 (void) fprintf(stderr, "usage: scp "
Ben Lindstromff2618c2001-03-29 00:43:54 +0000953 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n"
954 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955 exit(1);
956}
957
958void
Damien Miller95def091999-11-25 00:26:21 +1100959run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960{
961 static FILE *fp;
962 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963
964 ++errs;
965 if (fp == NULL && !(fp = fdopen(remout, "w")))
966 return;
Damien Miller95def091999-11-25 00:26:21 +1100967 (void) fprintf(fp, "%c", 0x01);
968 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100969 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100970 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100971 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100972 (void) fprintf(fp, "\n");
973 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974
Damien Miller95def091999-11-25 00:26:21 +1100975 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100976 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100977 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100978 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100979 fprintf(stderr, "\n");
980 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981}
982
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983void
984verifydir(cp)
985 char *cp;
986{
987 struct stat stb;
988
989 if (!stat(cp, &stb)) {
990 if (S_ISDIR(stb.st_mode))
991 return;
992 errno = ENOTDIR;
993 }
994 run_err("%s: %s", cp, strerror(errno));
995 exit(1);
996}
997
998int
999okname(cp0)
1000 char *cp0;
1001{
1002 int c;
1003 char *cp;
1004
1005 cp = cp0;
1006 do {
1007 c = *cp;
1008 if (c & 0200)
1009 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001010 if (!isalpha(c) && !isdigit(c) &&
1011 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 goto bad;
1013 } while (*++cp);
1014 return (1);
1015
Damien Miller98c7ad62000-03-09 21:27:49 +11001016bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017 return (0);
1018}
1019
1020BUF *
1021allocbuf(bp, fd, blksize)
1022 BUF *bp;
1023 int fd, blksize;
1024{
1025 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001026#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027 struct stat stb;
1028
1029 if (fstat(fd, &stb) < 0) {
1030 run_err("fstat: %s", strerror(errno));
1031 return (0);
1032 }
Damien Miller95def091999-11-25 00:26:21 +11001033 if (stb.st_blksize == 0)
1034 size = blksize;
1035 else
1036 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001037 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001038#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001039 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001040#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041 if (bp->cnt >= size)
1042 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001043 if (bp->buf == NULL)
1044 bp->buf = xmalloc(size);
1045 else
1046 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047 bp->cnt = size;
1048 return (bp);
1049}
1050
1051void
1052lostconn(signo)
1053 int signo;
1054{
1055 if (!iamremote)
1056 fprintf(stderr, "lost connection\n");
1057 exit(1);
1058}
1059
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060
1061void
1062alarmtimer(int wait)
1063{
Damien Miller95def091999-11-25 00:26:21 +11001064 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 itv.it_value.tv_sec = wait;
1067 itv.it_value.tv_usec = 0;
1068 itv.it_interval = itv.it_value;
1069 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070}
1071
1072void
Damien Miller7684ee12000-03-17 23:40:15 +11001073updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074{
1075 int save_errno = errno;
1076
1077 progressmeter(0);
1078 errno = save_errno;
1079}
1080
Damien Miller81428f91999-11-18 09:28:11 +11001081int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001082foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001083{
1084 static pid_t pgrp = -1;
1085 int ctty_pgrp;
1086
1087 if (pgrp == -1)
1088 pgrp = getpgrp();
1089
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001090#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001091 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1092 ctty_pgrp == pgrp);
1093#else
Damien Miller95def091999-11-25 00:26:21 +11001094 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1095 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001096#endif
Damien Miller81428f91999-11-18 09:28:11 +11001097}
1098
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099void
1100progressmeter(int flag)
1101{
1102 static const char prefixes[] = " KMGTP";
1103 static struct timeval lastupdate;
1104 static off_t lastsize;
1105 struct timeval now, td, wait;
1106 off_t cursize, abbrevsize;
1107 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001108 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109 char buf[256];
1110
1111 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001112 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113 lastupdate = start;
1114 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001115 }
Damien Miller81428f91999-11-18 09:28:11 +11001116 if (foregroundproc() == 0)
1117 return;
1118
Damien Miller95def091999-11-25 00:26:21 +11001119 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001120 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001121 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001122 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123 ratio = MAX(ratio, 0);
1124 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001125 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126 ratio = 100;
1127
Damien Miller95def091999-11-25 00:26:21 +11001128 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129
1130 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001131 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132 if (barlength > 0) {
1133 i = barlength * ratio / 100;
1134 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001135 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137 i = 0;
1138 abbrevsize = cursize;
1139 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1140 i++;
1141 abbrevsize >>= 10;
1142 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001143 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001144 (unsigned long) abbrevsize, prefixes[i],
1145 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146
1147 timersub(&now, &lastupdate, &wait);
1148 if (cursize > lastsize) {
1149 lastupdate = now;
1150 lastsize = cursize;
1151 if (wait.tv_sec >= STALLTIME) {
1152 start.tv_sec += wait.tv_sec;
1153 start.tv_usec += wait.tv_usec;
1154 }
1155 wait.tv_sec = 0;
1156 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 timersub(&now, &start, &td);
1158 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1159
Kevin Steves7d00ba42000-12-15 23:03:10 +00001160 if (flag != 1 &&
1161 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001163 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 } else if (wait.tv_sec >= STALLTIME) {
1165 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001166 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001168 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001169 remaining = (int)(totalbytes / (statbytes / elapsed) -
1170 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001171 else
1172 remaining = elapsed;
1173
Damien Miller01ab4a21999-10-28 15:23:30 +10001174 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175 if (i)
1176 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001177 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178 else
1179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001180 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 i = remaining % 3600;
1182 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001183 "%02d:%02d%s", i / 60, i % 60,
1184 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 }
1186 atomicio(write, fileno(stdout), buf, strlen(buf));
1187
1188 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001189 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190 alarmtimer(1);
1191 } else if (flag == 1) {
1192 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001193 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194 statbytes = 0;
1195 }
1196}
1197
1198int
1199getttywidth(void)
1200{
1201 struct winsize winsize;
1202
1203 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001204 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205 else
Damien Miller95def091999-11-25 00:26:21 +11001206 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207}