blob: bd2c5c751736f8cb9983867a4620bcafa10a0744 [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 Lindstromff2618c2001-03-29 00:43:54 +000078RCSID("$OpenBSD: scp.c,v 1.64 2001/03/28 20:04:38 stevesk 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
Damien Miller874d77b2000-10-14 16:23:11 +1100110/* setup arguments for the call to ssh */
111void addargs(char *fmt, ...) __attribute__((format(printf, 1, 2)));
112
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 Lindstrom46c16222000-12-22 01:43:59 +0000117volatile u_long 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 Miller874d77b2000-10-14 16:23:11 +1100134/* This is the list of arguments that scp passes to ssh */
135struct {
136 char **list;
137 int num;
138 int nalloc;
139} args;
140
Damien Miller5428f641999-11-25 11:54:57 +1100141/*
142 * This function executes the given command as the specified user on the
143 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
144 * assigns the input and output file descriptors on success.
145 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller4af51302000-04-16 11:18:38 +1000147int
Damien Millerad833b32000-08-23 10:46:23 +1000148do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149{
Damien Miller95def091999-11-25 00:26:21 +1100150 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 if (verbose_mode)
Damien Miller874d77b2000-10-14 16:23:11 +1100153 fprintf(stderr, "Executing: program %s host %s, user %s, command %s\n",
154 ssh_program, host, remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller5428f641999-11-25 11:54:57 +1100156 /*
157 * Reserve two descriptors so that the real pipes won't get
158 * descriptors 0 and 1 because that will screw up dup2 below.
159 */
Damien Miller95def091999-11-25 00:26:21 +1100160 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Damien Miller95def091999-11-25 00:26:21 +1100162 /* Create a socket pair for communicating with ssh. */
163 if (pipe(pin) < 0)
164 fatal("pipe: %s", strerror(errno));
165 if (pipe(pout) < 0)
166 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
Damien Miller95def091999-11-25 00:26:21 +1100168 /* Free the reserved descriptors. */
169 close(reserved[0]);
170 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000171
Damien Miller95def091999-11-25 00:26:21 +1100172 /* For a child to execute the command on the remote host using ssh. */
Damien Miller874d77b2000-10-14 16:23:11 +1100173 if (fork() == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Child. */
175 close(pin[1]);
176 close(pout[0]);
177 dup2(pin[0], 0);
178 dup2(pout[1], 1);
179 close(pin[0]);
180 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181
Damien Miller874d77b2000-10-14 16:23:11 +1100182 args.list[0] = ssh_program;
183 if (remuser != NULL)
Damien Millere4041c92000-10-14 17:45:58 +1100184 addargs("-l%s", remuser);
Damien Miller874d77b2000-10-14 16:23:11 +1100185 addargs("%s", host);
186 addargs("%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100187
Damien Miller874d77b2000-10-14 16:23:11 +1100188 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000189 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100190 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191 }
Damien Miller95def091999-11-25 00:26:21 +1100192 /* Parent. Close the other side, and return the local side. */
193 close(pin[0]);
194 *fdout = pin[1];
195 close(pout[1]);
196 *fdin = pout[0];
197 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198}
199
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200typedef struct {
201 int cnt;
202 char *buf;
203} BUF;
204
Damien Miller95def091999-11-25 00:26:21 +1100205BUF *allocbuf(BUF *, int, int);
206char *colon(char *);
207void lostconn(int);
208void nospace(void);
209int okname(char *);
210void run_err(const char *,...);
211void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100214uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215int errs, remin, remout;
216int pflag, iamremote, iamrecursive, targetshouldbedirectory;
217
218#define CMDNEEDS 64
219char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
220
Damien Miller95def091999-11-25 00:26:21 +1100221int response(void);
222void rsource(char *, struct stat *);
223void sink(int, char *[]);
224void source(int, char *[]);
225void tolocal(int, char *[]);
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000226char *cleanhostname(char *);
Damien Miller95def091999-11-25 00:26:21 +1100227void toremote(char *, int, char *[]);
228void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
230int
231main(argc, argv)
232 int argc;
233 char *argv[];
234{
235 int ch, fflag, tflag;
236 char *targ;
237 extern char *optarg;
238 extern int optind;
239
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000240 __progname = get_progname(argv[0]);
241
Damien Miller874d77b2000-10-14 16:23:11 +1100242 args.list = NULL;
243 addargs("ssh"); /* overwritten with ssh_program */
244 addargs("-x");
245 addargs("-oFallBackToRsh no");
246
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000248 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100249 switch (ch) {
250 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100251 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100252 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100253 case 'C':
254 addargs("-%c", ch);
255 break;
256 case 'o':
257 case 'c':
258 case 'i':
Damien Miller50a41ed2000-10-16 12:14:42 +1100259 addargs("-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100260 break;
261 case 'P':
Damien Miller50a41ed2000-10-16 12:14:42 +1100262 addargs("-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100263 break;
264 case 'B':
Damien Miller50a41ed2000-10-16 12:14:42 +1100265 addargs("-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100266 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267 case 'p':
268 pflag = 1;
269 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270 case 'r':
271 iamrecursive = 1;
272 break;
Damien Millerad833b32000-08-23 10:46:23 +1000273 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100274 ssh_program = xstrdup(optarg);
275 break;
276 case 'v':
277 verbose_mode = 1;
278 break;
279 case 'q':
280 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000281 break;
282
Damien Miller95def091999-11-25 00:26:21 +1100283 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284 case 'd':
285 targetshouldbedirectory = 1;
286 break;
Damien Miller95def091999-11-25 00:26:21 +1100287 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288 iamremote = 1;
289 fflag = 1;
290 break;
Damien Miller95def091999-11-25 00:26:21 +1100291 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292 iamremote = 1;
293 tflag = 1;
294 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295 default:
296 usage();
297 }
298 argc -= optind;
299 argv += optind;
300
301 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100302 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305 showprogress = 0;
306
307 remin = STDIN_FILENO;
308 remout = STDOUT_FILENO;
309
Kevin Stevesef4eea92001-02-05 12:42:17 +0000310 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100311 /* Follow "protocol", send data. */
312 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313 source(argc, argv);
314 exit(errs != 0);
315 }
Damien Miller95def091999-11-25 00:26:21 +1100316 if (tflag) {
317 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318 sink(argc, argv);
319 exit(errs != 0);
320 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321 if (argc < 2)
322 usage();
323 if (argc > 2)
324 targetshouldbedirectory = 1;
325
326 remin = remout = -1;
327 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000328 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
329 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000330 iamrecursive ? " -r" : "", pflag ? " -p" : "",
331 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334
335 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
336 toremote(targ, argc, argv);
337 else {
Damien Miller95def091999-11-25 00:26:21 +1100338 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339 if (targetshouldbedirectory)
340 verifydir(argv[argc - 1]);
341 }
342 exit(errs != 0);
343}
344
Damien Miller34132e52000-01-14 15:45:46 +1100345char *
346cleanhostname(host)
347 char *host;
348{
349 if (*host == '[' && host[strlen(host) - 1] == ']') {
350 host[strlen(host) - 1] = '\0';
351 return (host + 1);
352 } else
353 return host;
354}
355
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356void
357toremote(targ, argc, argv)
358 char *targ, *argv[];
359 int argc;
360{
361 int i, len;
362 char *bp, *host, *src, *suser, *thost, *tuser;
363
364 *targ++ = 0;
365 if (*targ == 0)
366 targ = ".";
367
368 if ((thost = strchr(argv[argc - 1], '@'))) {
369 /* user@host */
370 *thost++ = 0;
371 tuser = argv[argc - 1];
372 if (*tuser == '\0')
373 tuser = NULL;
374 else if (!okname(tuser))
375 exit(1);
376 } else {
377 thost = argv[argc - 1];
378 tuser = NULL;
379 }
380
381 for (i = 0; i < argc - 1; i++) {
382 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100383 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384 *src++ = 0;
385 if (*src == 0)
386 src = ".";
387 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000388 len = strlen(ssh_program) + strlen(argv[i]) +
389 strlen(src) + (tuser ? strlen(tuser) : 0) +
390 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100391 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392 if (host) {
393 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100394 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395 suser = argv[i];
396 if (*suser == '\0')
397 suser = pwd->pw_name;
398 else if (!okname(suser))
399 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000400 snprintf(bp, len,
401 "%s%s -x -o'FallBackToRsh no' -n "
402 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000403 ssh_program, verbose_mode ? " -v" : "",
404 suser, host, cmd, src,
405 tuser ? tuser : "", tuser ? "@" : "",
406 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100407 } else {
408 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000409 snprintf(bp, len,
410 "exec %s%s -x -o'FallBackToRsh no' -n %s "
411 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000412 ssh_program, verbose_mode ? " -v" : "",
413 host, cmd, src,
414 tuser ? tuser : "", tuser ? "@" : "",
415 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100416 }
Damien Miller95def091999-11-25 00:26:21 +1100417 if (verbose_mode)
418 fprintf(stderr, "Executing: %s\n", bp);
419 (void) system(bp);
420 (void) xfree(bp);
421 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 if (remin == -1) {
423 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100424 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000425 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100426 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000427 if (do_cmd(host, tuser, bp, &remin,
428 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100429 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430 if (response() < 0)
431 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100432 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433 }
Damien Miller95def091999-11-25 00:26:21 +1100434 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000435 }
436 }
437}
438
439void
440tolocal(argc, argv)
441 int argc;
442 char *argv[];
443{
444 int i, len;
445 char *bp, *host, *src, *suser;
446
447 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100448 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000450 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000452 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000453 iamrecursive ? " -r" : "", pflag ? " -p" : "",
454 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100455 if (verbose_mode)
456 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000457 if (system(bp))
458 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100459 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 continue;
461 }
462 *src++ = 0;
463 if (*src == 0)
464 src = ".";
465 if ((host = strchr(argv[i], '@')) == NULL) {
466 host = argv[i];
467 suser = NULL;
468 } else {
469 *host++ = 0;
470 suser = argv[i];
471 if (*suser == '\0')
472 suser = pwd->pw_name;
473 else if (!okname(suser))
474 continue;
475 }
Damien Miller34132e52000-01-14 15:45:46 +1100476 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100478 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000479 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000480 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100481 (void) xfree(bp);
482 ++errs;
483 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484 }
Damien Miller95def091999-11-25 00:26:21 +1100485 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100487 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 remin = remout = -1;
489 }
490}
491
492void
493source(argc, argv)
494 int argc;
495 char *argv[];
496{
497 struct stat stb;
498 static BUF buffer;
499 BUF *bp;
500 off_t i;
501 int amt, fd, haderr, indx, result;
502 char *last, *name, buf[2048];
503
504 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100505 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506 statbytes = 0;
507 if ((fd = open(name, O_RDONLY, 0)) < 0)
508 goto syserr;
509 if (fstat(fd, &stb) < 0) {
510syserr: run_err("%s: %s", name, strerror(errno));
511 goto next;
512 }
513 switch (stb.st_mode & S_IFMT) {
514 case S_IFREG:
515 break;
516 case S_IFDIR:
517 if (iamrecursive) {
518 rsource(name, &stb);
519 goto next;
520 }
521 /* FALLTHROUGH */
522 default:
523 run_err("%s: not a regular file", name);
524 goto next;
525 }
526 if ((last = strrchr(name, '/')) == NULL)
527 last = name;
528 else
529 ++last;
530 curfile = last;
531 if (pflag) {
532 /*
533 * Make it compatible with possible future
534 * versions expecting microseconds.
535 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000536 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000537 (u_long) stb.st_mtime,
538 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000539 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000540 if (response() < 0)
541 goto next;
542 }
543#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Damien Miller753b1c02001-03-19 12:56:14 +1100544#ifdef HAVE_LONG_LONG_INT
Ben Lindstroma4c57662001-03-17 00:10:20 +0000545 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000546 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000547 (long long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100548#else
549 /* XXX: Handle integer overflow? */
Ben Lindstrom8feff452001-03-19 03:09:40 +0000550 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
Damien Millerffd0e102001-03-19 12:45:02 +1100551 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstrom8feff452001-03-19 03:09:40 +0000552 (u_long) stb.st_size, last);
Damien Millerffd0e102001-03-19 12:45:02 +1100553#endif
554
Damien Miller95def091999-11-25 00:26:21 +1100555 if (verbose_mode) {
556 fprintf(stderr, "Sending file modes: %s", buf);
557 fflush(stderr);
558 }
Damien Miller35dabd02000-05-01 21:10:33 +1000559 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 if (response() < 0)
561 goto next;
562 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100563next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564 continue;
565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 if (showprogress) {
567 totalbytes = stb.st_size;
568 progressmeter(-1);
569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 /* Keep writing after an error so that we stay sync'd up. */
571 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
572 amt = bp->cnt;
573 if (i + amt > stb.st_size)
574 amt = stb.st_size - i;
575 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000576 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000577 if (result != amt)
578 haderr = result >= 0 ? EIO : errno;
579 }
580 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000581 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582 else {
Damien Miller864ea591999-12-15 11:04:25 +1100583 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584 if (result != amt)
585 haderr = result >= 0 ? EIO : errno;
586 statbytes += result;
587 }
588 }
Damien Miller95def091999-11-25 00:26:21 +1100589 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590 progressmeter(1);
591
592 if (close(fd) < 0 && !haderr)
593 haderr = errno;
594 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000595 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596 else
597 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100598 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000599 }
600}
601
602void
603rsource(name, statp)
604 char *name;
605 struct stat *statp;
606{
607 DIR *dirp;
608 struct dirent *dp;
609 char *last, *vect[1], path[1100];
610
611 if (!(dirp = opendir(name))) {
612 run_err("%s: %s", name, strerror(errno));
613 return;
614 }
615 last = strrchr(name, '/');
616 if (last == 0)
617 last = name;
618 else
619 last++;
620 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000621 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000622 (u_long) statp->st_mtime,
623 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000624 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000625 if (response() < 0) {
626 closedir(dirp);
627 return;
628 }
629 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000630 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000631 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100632 if (verbose_mode)
633 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000634 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635 if (response() < 0) {
636 closedir(dirp);
637 return;
638 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000639 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640 if (dp->d_ino == 0)
641 continue;
642 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
643 continue;
644 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
645 run_err("%s/%s: name too long", name, dp->d_name);
646 continue;
647 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000648 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000649 vect[0] = path;
650 source(1, vect);
651 }
Damien Miller95def091999-11-25 00:26:21 +1100652 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000653 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100654 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655}
656
657void
658sink(argc, argv)
659 int argc;
660 char *argv[];
661{
662 static BUF buffer;
663 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100664 enum {
665 YES, NO, DISPLAYED
666 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 BUF *bp;
668 off_t i, j;
669 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100670 off_t size;
671 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100673 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100674 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675
676#define SCREWUP(str) { why = str; goto screwup; }
677
678 setimes = targisdir = 0;
679 mask = umask(0);
680 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100681 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682 if (argc != 1) {
683 run_err("ambiguous target");
684 exit(1);
685 }
686 targ = *argv;
687 if (targetshouldbedirectory)
688 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100689
Damien Miller35dabd02000-05-01 21:10:33 +1000690 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
692 targisdir = 1;
693 for (first = 1;; first = 0) {
694 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000695 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696 return;
697 if (*cp++ == '\n')
698 SCREWUP("unexpected <newline>");
699 do {
Damien Millere247cc42000-05-07 12:03:14 +1000700 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701 SCREWUP("lost connection");
702 *cp++ = ch;
703 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
704 *cp = 0;
705
706 if (buf[0] == '\01' || buf[0] == '\02') {
707 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000708 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000709 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710 if (buf[0] == '\02')
711 exit(1);
712 ++errs;
713 continue;
714 }
715 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000716 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717 return;
718 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719 if (ch == '\n')
720 *--cp = 0;
721
722#define getnum(t) (t) = 0; \
723 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
724 cp = buf;
725 if (*cp == 'T') {
726 setimes++;
727 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100728 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729 if (*cp++ != ' ')
730 SCREWUP("mtime.sec not delimited");
731 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100732 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000733 if (*cp++ != ' ')
734 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100735 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736 if (*cp++ != ' ')
737 SCREWUP("atime.sec not delimited");
738 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100739 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740 if (*cp++ != '\0')
741 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000742 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000743 continue;
744 }
745 if (*cp != 'C' && *cp != 'D') {
746 /*
747 * Check for the case "rcp remote:foo\* local:bar".
748 * In this case, the line "No match." can be returned
749 * by the shell before the rcp command on the remote is
750 * executed so the ^Aerror_message convention isn't
751 * followed.
752 */
753 if (first) {
754 run_err("%s", cp);
755 exit(1);
756 }
757 SCREWUP("expected control record");
758 }
759 mode = 0;
760 for (++cp; cp < buf + 5; cp++) {
761 if (*cp < '0' || *cp > '7')
762 SCREWUP("bad mode");
763 mode = (mode << 3) | (*cp - '0');
764 }
765 if (*cp++ != ' ')
766 SCREWUP("mode not delimited");
767
Ben Lindstrombd472262001-03-29 00:39:55 +0000768 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769 size = size * 10 + (*cp++ - '0');
770 if (*cp++ != ' ')
771 SCREWUP("size not delimited");
772 if (targisdir) {
773 static char *namebuf;
774 static int cursize;
775 size_t need;
776
777 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000778 if (need > cursize) {
779 if (namebuf)
780 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100781 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000782 cursize = need;
783 }
784 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000785 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786 np = namebuf;
787 } else
788 np = targ;
789 curfile = cp;
790 exists = stat(np, &stb) == 0;
791 if (buf[0] == 'D') {
792 int mod_flag = pflag;
793 if (exists) {
794 if (!S_ISDIR(stb.st_mode)) {
795 errno = ENOTDIR;
796 goto bad;
797 }
798 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100799 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800 } else {
Damien Miller95def091999-11-25 00:26:21 +1100801 /* Handle copying from a read-only
802 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803 mod_flag = 1;
804 if (mkdir(np, mode | S_IRWXU) < 0)
805 goto bad;
806 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000807 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808 sink(1, vect);
809 if (setimes) {
810 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100811 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100812 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100813 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814 }
815 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100816 (void) chmod(vect[0], mode);
817 if (vect[0])
818 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 continue;
820 }
821 omode = mode;
822 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100823 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824bad: run_err("%s: %s", np, strerror(errno));
825 continue;
826 }
Damien Miller35dabd02000-05-01 21:10:33 +1000827 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100829 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830 continue;
831 }
832 cp = bp->buf;
833 wrerr = NO;
834
835 if (showprogress) {
836 totalbytes = size;
837 progressmeter(-1);
838 }
839 statbytes = 0;
840 for (count = i = 0; i < size; i += 4096) {
841 amt = 4096;
842 if (i + amt > size)
843 amt = size - i;
844 count += amt;
845 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100846 j = read(remin, cp, amt);
847 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
848 continue;
849 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000851 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852 exit(1);
853 }
854 amt -= j;
855 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100856 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857 } while (amt > 0);
858 if (count == bp->cnt) {
859 /* Keep reading so we stay sync'd up. */
860 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000861 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862 if (j != count) {
863 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100864 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000865 }
866 }
867 count = 0;
868 cp = bp->buf;
869 }
870 }
871 if (showprogress)
872 progressmeter(1);
873 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000874 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100876 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877 }
878#if 0
879 if (ftruncate(ofd, size)) {
880 run_err("%s: truncate: %s", np, strerror(errno));
881 wrerr = DISPLAYED;
882 }
883#endif
884 if (pflag) {
885 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100886#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100888#else /* HAVE_FCHMOD */
889 if (chmod(np, omode))
890#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000892 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893 } else {
894 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100895#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100897#else /* HAVE_FCHMOD */
898 if (chmod(np, omode & ~mask))
899#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000901 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902 }
Damien Millerbe484b52000-07-15 14:14:16 +1000903 if (close(ofd) == -1) {
904 wrerr = YES;
905 wrerrno = errno;
906 }
Damien Miller95def091999-11-25 00:26:21 +1100907 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908 if (setimes && wrerr == NO) {
909 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100910 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000912 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 wrerr = DISPLAYED;
914 }
915 }
Damien Miller95def091999-11-25 00:26:21 +1100916 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917 case YES:
918 run_err("%s: %s", np, strerror(wrerrno));
919 break;
920 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000921 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 break;
923 case DISPLAYED:
924 break;
925 }
926 }
927screwup:
928 run_err("protocol error: %s", why);
929 exit(1);
930}
931
932int
933response()
934{
935 char ch, *cp, resp, rbuf[2048];
936
Damien Millere247cc42000-05-07 12:03:14 +1000937 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 lostconn(0);
939
940 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100941 switch (resp) {
942 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943 return (0);
944 default:
945 *cp++ = resp;
946 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100947 case 1: /* error, followed by error msg */
948 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949 do {
Damien Millere247cc42000-05-07 12:03:14 +1000950 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951 lostconn(0);
952 *cp++ = ch;
953 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
954
955 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000956 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957 ++errs;
958 if (resp == 1)
959 return (-1);
960 exit(1);
961 }
962 /* NOTREACHED */
963}
964
965void
966usage()
967{
Damien Millerad833b32000-08-23 10:46:23 +1000968 (void) fprintf(stderr, "usage: scp "
Ben Lindstromff2618c2001-03-29 00:43:54 +0000969 "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2\n"
970 " or: scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971 exit(1);
972}
973
974void
Damien Miller95def091999-11-25 00:26:21 +1100975run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976{
977 static FILE *fp;
978 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
980 ++errs;
981 if (fp == NULL && !(fp = fdopen(remout, "w")))
982 return;
Damien Miller95def091999-11-25 00:26:21 +1100983 (void) fprintf(fp, "%c", 0x01);
984 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100985 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100986 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100987 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100988 (void) fprintf(fp, "\n");
989 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller95def091999-11-25 00:26:21 +1100991 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100992 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100993 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100994 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100995 fprintf(stderr, "\n");
996 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997}
998
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000999char *
1000colon(cp)
1001 char *cp;
1002{
Damien Miller34132e52000-01-14 15:45:46 +11001003 int flag = 0;
1004
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005 if (*cp == ':') /* Leading colon is part of file name. */
1006 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001007 if (*cp == '[')
1008 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001009
1010 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001011 if (*cp == '@' && *(cp+1) == '[')
1012 flag = 1;
1013 if (*cp == ']' && *(cp+1) == ':' && flag)
1014 return (cp+1);
1015 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016 return (cp);
1017 if (*cp == '/')
1018 return (0);
1019 }
1020 return (0);
1021}
1022
1023void
1024verifydir(cp)
1025 char *cp;
1026{
1027 struct stat stb;
1028
1029 if (!stat(cp, &stb)) {
1030 if (S_ISDIR(stb.st_mode))
1031 return;
1032 errno = ENOTDIR;
1033 }
1034 run_err("%s: %s", cp, strerror(errno));
1035 exit(1);
1036}
1037
1038int
1039okname(cp0)
1040 char *cp0;
1041{
1042 int c;
1043 char *cp;
1044
1045 cp = cp0;
1046 do {
1047 c = *cp;
1048 if (c & 0200)
1049 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001050 if (!isalpha(c) && !isdigit(c) &&
1051 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052 goto bad;
1053 } while (*++cp);
1054 return (1);
1055
Damien Miller98c7ad62000-03-09 21:27:49 +11001056bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057 return (0);
1058}
1059
1060BUF *
1061allocbuf(bp, fd, blksize)
1062 BUF *bp;
1063 int fd, blksize;
1064{
1065 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001066#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067 struct stat stb;
1068
1069 if (fstat(fd, &stb) < 0) {
1070 run_err("fstat: %s", strerror(errno));
1071 return (0);
1072 }
Damien Miller95def091999-11-25 00:26:21 +11001073 if (stb.st_blksize == 0)
1074 size = blksize;
1075 else
1076 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001077 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001078#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001079 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001080#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081 if (bp->cnt >= size)
1082 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001083 if (bp->buf == NULL)
1084 bp->buf = xmalloc(size);
1085 else
1086 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087 bp->cnt = size;
1088 return (bp);
1089}
1090
1091void
1092lostconn(signo)
1093 int signo;
1094{
1095 if (!iamremote)
1096 fprintf(stderr, "lost connection\n");
1097 exit(1);
1098}
1099
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001100
1101void
1102alarmtimer(int wait)
1103{
Damien Miller95def091999-11-25 00:26:21 +11001104 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105
Damien Miller95def091999-11-25 00:26:21 +11001106 itv.it_value.tv_sec = wait;
1107 itv.it_value.tv_usec = 0;
1108 itv.it_interval = itv.it_value;
1109 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110}
1111
1112void
Damien Miller7684ee12000-03-17 23:40:15 +11001113updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114{
1115 int save_errno = errno;
1116
1117 progressmeter(0);
1118 errno = save_errno;
1119}
1120
Damien Miller81428f91999-11-18 09:28:11 +11001121int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001122foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001123{
1124 static pid_t pgrp = -1;
1125 int ctty_pgrp;
1126
1127 if (pgrp == -1)
1128 pgrp = getpgrp();
1129
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001130#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001131 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1132 ctty_pgrp == pgrp);
1133#else
Damien Miller95def091999-11-25 00:26:21 +11001134 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1135 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001136#endif
Damien Miller81428f91999-11-18 09:28:11 +11001137}
1138
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139void
1140progressmeter(int flag)
1141{
1142 static const char prefixes[] = " KMGTP";
1143 static struct timeval lastupdate;
1144 static off_t lastsize;
1145 struct timeval now, td, wait;
1146 off_t cursize, abbrevsize;
1147 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001148 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149 char buf[256];
1150
1151 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001152 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153 lastupdate = start;
1154 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001155 }
Damien Miller81428f91999-11-18 09:28:11 +11001156 if (foregroundproc() == 0)
1157 return;
1158
Damien Miller95def091999-11-25 00:26:21 +11001159 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001161 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001162 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163 ratio = MAX(ratio, 0);
1164 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001165 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166 ratio = 100;
1167
Damien Miller95def091999-11-25 00:26:21 +11001168 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169
1170 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001171 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172 if (barlength > 0) {
1173 i = barlength * ratio / 100;
1174 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001175 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177 i = 0;
1178 abbrevsize = cursize;
1179 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1180 i++;
1181 abbrevsize >>= 10;
1182 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001183 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001184 (unsigned long) abbrevsize, prefixes[i],
1185 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186
1187 timersub(&now, &lastupdate, &wait);
1188 if (cursize > lastsize) {
1189 lastupdate = now;
1190 lastsize = cursize;
1191 if (wait.tv_sec >= STALLTIME) {
1192 start.tv_sec += wait.tv_sec;
1193 start.tv_usec += wait.tv_usec;
1194 }
1195 wait.tv_sec = 0;
1196 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197 timersub(&now, &start, &td);
1198 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1199
Kevin Steves7d00ba42000-12-15 23:03:10 +00001200 if (flag != 1 &&
1201 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001203 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204 } else if (wait.tv_sec >= STALLTIME) {
1205 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001206 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001208 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001209 remaining = (int)(totalbytes / (statbytes / elapsed) -
1210 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001211 else
1212 remaining = elapsed;
1213
Damien Miller01ab4a21999-10-28 15:23:30 +10001214 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215 if (i)
1216 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001217 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218 else
1219 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001220 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221 i = remaining % 3600;
1222 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001223 "%02d:%02d%s", i / 60, i % 60,
1224 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225 }
1226 atomicio(write, fileno(stdout), buf, strlen(buf));
1227
1228 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001229 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230 alarmtimer(1);
1231 } else if (flag == 1) {
1232 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001233 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234 statbytes = 0;
1235 }
1236}
1237
1238int
1239getttywidth(void)
1240{
1241 struct winsize winsize;
1242
1243 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001244 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245 else
Damien Miller95def091999-11-25 00:26:21 +11001246 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
Damien Miller874d77b2000-10-14 16:23:11 +11001248
1249void
1250addargs(char *fmt, ...)
1251{
1252 va_list ap;
1253 char buf[1024];
1254
1255 va_start(ap, fmt);
1256 vsnprintf(buf, sizeof(buf), fmt, ap);
1257 va_end(ap);
1258
1259 if (args.list == NULL) {
1260 args.nalloc = 32;
1261 args.num = 0;
1262 args.list = xmalloc(args.nalloc * sizeof(char *));
1263 } else if (args.num+2 >= args.nalloc) {
1264 args.nalloc *= 2;
1265 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1266 }
1267 args.list[args.num++] = xstrdup(buf);
1268 args.list[args.num] = NULL;
1269}