blob: 27a502964c61b80d2c3c2d1af4019123f8f9521c [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 Lindstroma4c57662001-03-17 00:10:20 +000078RCSID("$OpenBSD: scp.c,v 1.61 2001/03/15 15:05:59 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "atomicio.h"
82#include "pathnames.h"
83#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +000084#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrom49a79c02000-11-17 03:47:20 +000086#ifdef HAVE___PROGNAME
87extern char *__progname;
88#else
89char *__progname;
90#endif
91
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092/* For progressmeter() -- number of seconds before xfer considered "stalled" */
93#define STALLTIME 5
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
205extern int iamremote;
206
Damien Miller95def091999-11-25 00:26:21 +1100207BUF *allocbuf(BUF *, int, int);
208char *colon(char *);
209void lostconn(int);
210void nospace(void);
211int okname(char *);
212void run_err(const char *,...);
213void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100216uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217int errs, remin, remout;
218int pflag, iamremote, iamrecursive, targetshouldbedirectory;
219
220#define CMDNEEDS 64
221char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
222
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000223int main(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100224int response(void);
225void rsource(char *, struct stat *);
226void sink(int, char *[]);
227void source(int, char *[]);
228void tolocal(int, char *[]);
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000229char *cleanhostname(char *);
Damien Miller95def091999-11-25 00:26:21 +1100230void toremote(char *, int, char *[]);
231void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
233int
234main(argc, argv)
235 int argc;
236 char *argv[];
237{
238 int ch, fflag, tflag;
239 char *targ;
240 extern char *optarg;
241 extern int optind;
242
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000243 __progname = get_progname(argv[0]);
244
Damien Miller874d77b2000-10-14 16:23:11 +1100245 args.list = NULL;
246 addargs("ssh"); /* overwritten with ssh_program */
247 addargs("-x");
248 addargs("-oFallBackToRsh no");
249
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250 fflag = tflag = 0;
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000251 while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100252 switch (ch) {
253 /* User-visible flags. */
Damien Miller34132e52000-01-14 15:45:46 +1100254 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100255 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100256 case 'C':
257 addargs("-%c", ch);
258 break;
259 case 'o':
260 case 'c':
261 case 'i':
Damien Miller50a41ed2000-10-16 12:14:42 +1100262 addargs("-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100263 break;
264 case 'P':
Damien Miller50a41ed2000-10-16 12:14:42 +1100265 addargs("-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100266 break;
267 case 'B':
Damien Miller50a41ed2000-10-16 12:14:42 +1100268 addargs("-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100269 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270 case 'p':
271 pflag = 1;
272 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273 case 'r':
274 iamrecursive = 1;
275 break;
Damien Millerad833b32000-08-23 10:46:23 +1000276 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100277 ssh_program = xstrdup(optarg);
278 break;
279 case 'v':
280 verbose_mode = 1;
281 break;
282 case 'q':
283 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000284 break;
285
Damien Miller95def091999-11-25 00:26:21 +1100286 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287 case 'd':
288 targetshouldbedirectory = 1;
289 break;
Damien Miller95def091999-11-25 00:26:21 +1100290 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291 iamremote = 1;
292 fflag = 1;
293 break;
Damien Miller95def091999-11-25 00:26:21 +1100294 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295 iamremote = 1;
296 tflag = 1;
297 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298 case '?':
299 default:
300 usage();
301 }
302 argc -= optind;
303 argv += optind;
304
305 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100306 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307
Damien Miller95def091999-11-25 00:26:21 +1100308 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309 showprogress = 0;
310
311 remin = STDIN_FILENO;
312 remout = STDOUT_FILENO;
313
Kevin Stevesef4eea92001-02-05 12:42:17 +0000314 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100315 /* Follow "protocol", send data. */
316 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317 source(argc, argv);
318 exit(errs != 0);
319 }
Damien Miller95def091999-11-25 00:26:21 +1100320 if (tflag) {
321 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322 sink(argc, argv);
323 exit(errs != 0);
324 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325 if (argc < 2)
326 usage();
327 if (argc > 2)
328 targetshouldbedirectory = 1;
329
330 remin = remout = -1;
331 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000332 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
333 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000334 iamrecursive ? " -r" : "", pflag ? " -p" : "",
335 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Damien Miller95def091999-11-25 00:26:21 +1100337 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338
339 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
340 toremote(targ, argc, argv);
341 else {
Damien Miller95def091999-11-25 00:26:21 +1100342 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343 if (targetshouldbedirectory)
344 verifydir(argv[argc - 1]);
345 }
346 exit(errs != 0);
347}
348
Damien Miller34132e52000-01-14 15:45:46 +1100349char *
350cleanhostname(host)
351 char *host;
352{
353 if (*host == '[' && host[strlen(host) - 1] == ']') {
354 host[strlen(host) - 1] = '\0';
355 return (host + 1);
356 } else
357 return host;
358}
359
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360void
361toremote(targ, argc, argv)
362 char *targ, *argv[];
363 int argc;
364{
365 int i, len;
366 char *bp, *host, *src, *suser, *thost, *tuser;
367
368 *targ++ = 0;
369 if (*targ == 0)
370 targ = ".";
371
372 if ((thost = strchr(argv[argc - 1], '@'))) {
373 /* user@host */
374 *thost++ = 0;
375 tuser = argv[argc - 1];
376 if (*tuser == '\0')
377 tuser = NULL;
378 else if (!okname(tuser))
379 exit(1);
380 } else {
381 thost = argv[argc - 1];
382 tuser = NULL;
383 }
384
385 for (i = 0; i < argc - 1; i++) {
386 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100387 if (src) { /* remote to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388 *src++ = 0;
389 if (*src == 0)
390 src = ".";
391 host = strchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000392 len = strlen(ssh_program) + strlen(argv[i]) +
393 strlen(src) + (tuser ? strlen(tuser) : 0) +
394 strlen(thost) + strlen(targ) + CMDNEEDS + 32;
Damien Miller95def091999-11-25 00:26:21 +1100395 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396 if (host) {
397 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100398 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399 suser = argv[i];
400 if (*suser == '\0')
401 suser = pwd->pw_name;
402 else if (!okname(suser))
403 continue;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000404 snprintf(bp, len,
405 "%s%s -x -o'FallBackToRsh no' -n "
406 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000407 ssh_program, verbose_mode ? " -v" : "",
408 suser, host, cmd, src,
409 tuser ? tuser : "", tuser ? "@" : "",
410 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100411 } else {
412 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000413 snprintf(bp, len,
414 "exec %s%s -x -o'FallBackToRsh no' -n %s "
415 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000416 ssh_program, verbose_mode ? " -v" : "",
417 host, cmd, src,
418 tuser ? tuser : "", tuser ? "@" : "",
419 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100420 }
Damien Miller95def091999-11-25 00:26:21 +1100421 if (verbose_mode)
422 fprintf(stderr, "Executing: %s\n", bp);
423 (void) system(bp);
424 (void) xfree(bp);
425 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426 if (remin == -1) {
427 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100428 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000429 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100430 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000431 if (do_cmd(host, tuser, bp, &remin,
432 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100433 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000434 if (response() < 0)
435 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100436 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437 }
Damien Miller95def091999-11-25 00:26:21 +1100438 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439 }
440 }
441}
442
443void
444tolocal(argc, argv)
445 int argc;
446 char *argv[];
447{
448 int i, len;
449 char *bp, *host, *src, *suser;
450
451 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100452 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000454 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000456 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000457 iamrecursive ? " -r" : "", pflag ? " -p" : "",
458 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100459 if (verbose_mode)
460 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461 if (system(bp))
462 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100463 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 continue;
465 }
466 *src++ = 0;
467 if (*src == 0)
468 src = ".";
469 if ((host = strchr(argv[i], '@')) == NULL) {
470 host = argv[i];
471 suser = NULL;
472 } else {
473 *host++ = 0;
474 suser = argv[i];
475 if (*suser == '\0')
476 suser = pwd->pw_name;
477 else if (!okname(suser))
478 continue;
479 }
Damien Miller34132e52000-01-14 15:45:46 +1100480 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000481 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100482 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000483 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000484 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100485 (void) xfree(bp);
486 ++errs;
487 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 }
Damien Miller95def091999-11-25 00:26:21 +1100489 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100491 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 remin = remout = -1;
493 }
494}
495
496void
497source(argc, argv)
498 int argc;
499 char *argv[];
500{
501 struct stat stb;
502 static BUF buffer;
503 BUF *bp;
504 off_t i;
505 int amt, fd, haderr, indx, result;
506 char *last, *name, buf[2048];
507
508 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100509 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510 statbytes = 0;
511 if ((fd = open(name, O_RDONLY, 0)) < 0)
512 goto syserr;
513 if (fstat(fd, &stb) < 0) {
514syserr: run_err("%s: %s", name, strerror(errno));
515 goto next;
516 }
517 switch (stb.st_mode & S_IFMT) {
518 case S_IFREG:
519 break;
520 case S_IFDIR:
521 if (iamrecursive) {
522 rsource(name, &stb);
523 goto next;
524 }
525 /* FALLTHROUGH */
526 default:
527 run_err("%s: not a regular file", name);
528 goto next;
529 }
530 if ((last = strrchr(name, '/')) == NULL)
531 last = name;
532 else
533 ++last;
534 curfile = last;
535 if (pflag) {
536 /*
537 * Make it compatible with possible future
538 * versions expecting microseconds.
539 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000540 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000541 (u_long) stb.st_mtime,
542 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000543 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000544 if (response() < 0)
545 goto next;
546 }
547#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +0000548 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000549 (u_int) (stb.st_mode & FILEMODEMASK),
Ben Lindstromfea72782001-03-17 18:07:46 +0000550 (long long) stb.st_size, last);
Damien Miller95def091999-11-25 00:26:21 +1100551 if (verbose_mode) {
552 fprintf(stderr, "Sending file modes: %s", buf);
553 fflush(stderr);
554 }
Damien Miller35dabd02000-05-01 21:10:33 +1000555 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 if (response() < 0)
557 goto next;
558 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100559next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 continue;
561 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000562 if (showprogress) {
563 totalbytes = stb.st_size;
564 progressmeter(-1);
565 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 /* Keep writing after an error so that we stay sync'd up. */
567 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
568 amt = bp->cnt;
569 if (i + amt > stb.st_size)
570 amt = stb.st_size - i;
571 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000572 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573 if (result != amt)
574 haderr = result >= 0 ? EIO : errno;
575 }
576 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000577 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578 else {
Damien Miller864ea591999-12-15 11:04:25 +1100579 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580 if (result != amt)
581 haderr = result >= 0 ? EIO : errno;
582 statbytes += result;
583 }
584 }
Damien Miller95def091999-11-25 00:26:21 +1100585 if (showprogress)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586 progressmeter(1);
587
588 if (close(fd) < 0 && !haderr)
589 haderr = errno;
590 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000591 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592 else
593 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100594 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595 }
596}
597
598void
599rsource(name, statp)
600 char *name;
601 struct stat *statp;
602{
603 DIR *dirp;
604 struct dirent *dp;
605 char *last, *vect[1], path[1100];
606
607 if (!(dirp = opendir(name))) {
608 run_err("%s: %s", name, strerror(errno));
609 return;
610 }
611 last = strrchr(name, '/');
612 if (last == 0)
613 last = name;
614 else
615 last++;
616 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000617 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000618 (u_long) statp->st_mtime,
619 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000620 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621 if (response() < 0) {
622 closedir(dirp);
623 return;
624 }
625 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000626 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000627 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100628 if (verbose_mode)
629 fprintf(stderr, "Entering directory: %s", path);
Damien Miller35dabd02000-05-01 21:10:33 +1000630 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000631 if (response() < 0) {
632 closedir(dirp);
633 return;
634 }
635 while ((dp = readdir(dirp))) {
636 if (dp->d_ino == 0)
637 continue;
638 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
639 continue;
640 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
641 run_err("%s/%s: name too long", name, dp->d_name);
642 continue;
643 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000644 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645 vect[0] = path;
646 source(1, vect);
647 }
Damien Miller95def091999-11-25 00:26:21 +1100648 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000649 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100650 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651}
652
653void
654sink(argc, argv)
655 int argc;
656 char *argv[];
657{
658 static BUF buffer;
659 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100660 enum {
661 YES, NO, DISPLAYED
662 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000663 BUF *bp;
664 off_t i, j;
665 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Millercaf6dd62000-08-29 11:33:50 +1100666 off_t size;
667 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller95def091999-11-25 00:26:21 +1100669 int dummy_usec;
Damien Miller62cee002000-09-23 17:15:56 +1100670 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000671
672#define SCREWUP(str) { why = str; goto screwup; }
673
674 setimes = targisdir = 0;
675 mask = umask(0);
676 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100677 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000678 if (argc != 1) {
679 run_err("ambiguous target");
680 exit(1);
681 }
682 targ = *argv;
683 if (targetshouldbedirectory)
684 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100685
Damien Miller35dabd02000-05-01 21:10:33 +1000686 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
688 targisdir = 1;
689 for (first = 1;; first = 0) {
690 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000691 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000692 return;
693 if (*cp++ == '\n')
694 SCREWUP("unexpected <newline>");
695 do {
Damien Millere247cc42000-05-07 12:03:14 +1000696 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697 SCREWUP("lost connection");
698 *cp++ = ch;
699 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
700 *cp = 0;
701
702 if (buf[0] == '\01' || buf[0] == '\02') {
703 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000704 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000705 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706 if (buf[0] == '\02')
707 exit(1);
708 ++errs;
709 continue;
710 }
711 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000712 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713 return;
714 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715 if (ch == '\n')
716 *--cp = 0;
717
718#define getnum(t) (t) = 0; \
719 while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
720 cp = buf;
721 if (*cp == 'T') {
722 setimes++;
723 cp++;
Damien Miller62cee002000-09-23 17:15:56 +1100724 getnum(tv[1].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725 if (*cp++ != ' ')
726 SCREWUP("mtime.sec not delimited");
727 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100728 tv[1].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729 if (*cp++ != ' ')
730 SCREWUP("mtime.usec not delimited");
Damien Miller62cee002000-09-23 17:15:56 +1100731 getnum(tv[0].tv_sec);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000732 if (*cp++ != ' ')
733 SCREWUP("atime.sec not delimited");
734 getnum(dummy_usec);
Damien Miller62cee002000-09-23 17:15:56 +1100735 tv[0].tv_usec = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736 if (*cp++ != '\0')
737 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000738 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739 continue;
740 }
741 if (*cp != 'C' && *cp != 'D') {
742 /*
743 * Check for the case "rcp remote:foo\* local:bar".
744 * In this case, the line "No match." can be returned
745 * by the shell before the rcp command on the remote is
746 * executed so the ^Aerror_message convention isn't
747 * followed.
748 */
749 if (first) {
750 run_err("%s", cp);
751 exit(1);
752 }
753 SCREWUP("expected control record");
754 }
755 mode = 0;
756 for (++cp; cp < buf + 5; cp++) {
757 if (*cp < '0' || *cp > '7')
758 SCREWUP("bad mode");
759 mode = (mode << 3) | (*cp - '0');
760 }
761 if (*cp++ != ' ')
762 SCREWUP("mode not delimited");
763
Damien Miller95def091999-11-25 00:26:21 +1100764 for (size = 0; *cp >= '0' && *cp <= '9';)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765 size = size * 10 + (*cp++ - '0');
766 if (*cp++ != ' ')
767 SCREWUP("size not delimited");
768 if (targisdir) {
769 static char *namebuf;
770 static int cursize;
771 size_t need;
772
773 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000774 if (need > cursize) {
775 if (namebuf)
776 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100777 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000778 cursize = need;
779 }
780 (void) snprintf(namebuf, need, "%s%s%s", targ,
Damien Millerad833b32000-08-23 10:46:23 +1000781 *targ ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782 np = namebuf;
783 } else
784 np = targ;
785 curfile = cp;
786 exists = stat(np, &stb) == 0;
787 if (buf[0] == 'D') {
788 int mod_flag = pflag;
789 if (exists) {
790 if (!S_ISDIR(stb.st_mode)) {
791 errno = ENOTDIR;
792 goto bad;
793 }
794 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100795 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796 } else {
Damien Miller95def091999-11-25 00:26:21 +1100797 /* Handle copying from a read-only
798 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799 mod_flag = 1;
800 if (mkdir(np, mode | S_IRWXU) < 0)
801 goto bad;
802 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000803 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804 sink(1, vect);
805 if (setimes) {
806 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100807 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100808 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100809 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810 }
811 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100812 (void) chmod(vect[0], mode);
813 if (vect[0])
814 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815 continue;
816 }
817 omode = mode;
818 mode |= S_IWRITE;
Damien Miller95def091999-11-25 00:26:21 +1100819 if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820bad: run_err("%s: %s", np, strerror(errno));
821 continue;
822 }
Damien Miller35dabd02000-05-01 21:10:33 +1000823 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100825 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826 continue;
827 }
828 cp = bp->buf;
829 wrerr = NO;
830
831 if (showprogress) {
832 totalbytes = size;
833 progressmeter(-1);
834 }
835 statbytes = 0;
836 for (count = i = 0; i < size; i += 4096) {
837 amt = 4096;
838 if (i + amt > size)
839 amt = size - i;
840 count += amt;
841 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100842 j = read(remin, cp, amt);
843 if (j == -1 && (errno == EINTR || errno == EAGAIN)) {
844 continue;
845 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846 run_err("%s", j ? strerror(errno) :
Damien Miller95def091999-11-25 00:26:21 +1100847 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 exit(1);
849 }
850 amt -= j;
851 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100852 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 } while (amt > 0);
854 if (count == bp->cnt) {
855 /* Keep reading so we stay sync'd up. */
856 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000857 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858 if (j != count) {
859 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100860 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861 }
862 }
863 count = 0;
864 cp = bp->buf;
865 }
866 }
867 if (showprogress)
868 progressmeter(1);
869 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000870 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100872 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873 }
874#if 0
875 if (ftruncate(ofd, size)) {
876 run_err("%s: truncate: %s", np, strerror(errno));
877 wrerr = DISPLAYED;
878 }
879#endif
880 if (pflag) {
881 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100882#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100884#else /* HAVE_FCHMOD */
885 if (chmod(np, omode))
886#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100888 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 } else {
890 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100891#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100893#else /* HAVE_FCHMOD */
894 if (chmod(np, omode & ~mask))
895#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 run_err("%s: set mode: %s",
Damien Miller95def091999-11-25 00:26:21 +1100897 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898 }
Damien Millerbe484b52000-07-15 14:14:16 +1000899 if (close(ofd) == -1) {
900 wrerr = YES;
901 wrerrno = errno;
902 }
Damien Miller95def091999-11-25 00:26:21 +1100903 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 if (setimes && wrerr == NO) {
905 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100906 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907 run_err("%s: set times: %s",
Damien Miller95def091999-11-25 00:26:21 +1100908 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909 wrerr = DISPLAYED;
910 }
911 }
Damien Miller95def091999-11-25 00:26:21 +1100912 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 case YES:
914 run_err("%s: %s", np, strerror(wrerrno));
915 break;
916 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000917 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918 break;
919 case DISPLAYED:
920 break;
921 }
922 }
923screwup:
924 run_err("protocol error: %s", why);
925 exit(1);
926}
927
928int
929response()
930{
931 char ch, *cp, resp, rbuf[2048];
932
Damien Millere247cc42000-05-07 12:03:14 +1000933 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000934 lostconn(0);
935
936 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100937 switch (resp) {
938 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939 return (0);
940 default:
941 *cp++ = resp;
942 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100943 case 1: /* error, followed by error msg */
944 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945 do {
Damien Millere247cc42000-05-07 12:03:14 +1000946 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947 lostconn(0);
948 *cp++ = ch;
949 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
950
951 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000952 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953 ++errs;
954 if (resp == 1)
955 return (-1);
956 exit(1);
957 }
958 /* NOTREACHED */
959}
960
961void
962usage()
963{
Damien Millerad833b32000-08-23 10:46:23 +1000964 (void) fprintf(stderr, "usage: scp "
965 "[-pqrvC46] [-S ssh] [-P port] [-c cipher] [-i identity] f1 f2; or:\n"
966 " scp [options] f1 ... fn directory\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967 exit(1);
968}
969
970void
Damien Miller95def091999-11-25 00:26:21 +1100971run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972{
973 static FILE *fp;
974 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975
976 ++errs;
977 if (fp == NULL && !(fp = fdopen(remout, "w")))
978 return;
Damien Miller95def091999-11-25 00:26:21 +1100979 (void) fprintf(fp, "%c", 0x01);
980 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +1100981 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100982 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100983 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100984 (void) fprintf(fp, "\n");
985 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986
Damien Miller95def091999-11-25 00:26:21 +1100987 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +1100988 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +1100989 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +1100990 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +1100991 fprintf(stderr, "\n");
992 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993}
994
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995char *
996colon(cp)
997 char *cp;
998{
Damien Miller34132e52000-01-14 15:45:46 +1100999 int flag = 0;
1000
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001 if (*cp == ':') /* Leading colon is part of file name. */
1002 return (0);
Damien Miller34132e52000-01-14 15:45:46 +11001003 if (*cp == '[')
1004 flag = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005
1006 for (; *cp; ++cp) {
Damien Miller34132e52000-01-14 15:45:46 +11001007 if (*cp == '@' && *(cp+1) == '[')
1008 flag = 1;
1009 if (*cp == ']' && *(cp+1) == ':' && flag)
1010 return (cp+1);
1011 if (*cp == ':' && !flag)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 return (cp);
1013 if (*cp == '/')
1014 return (0);
1015 }
1016 return (0);
1017}
1018
1019void
1020verifydir(cp)
1021 char *cp;
1022{
1023 struct stat stb;
1024
1025 if (!stat(cp, &stb)) {
1026 if (S_ISDIR(stb.st_mode))
1027 return;
1028 errno = ENOTDIR;
1029 }
1030 run_err("%s: %s", cp, strerror(errno));
1031 exit(1);
1032}
1033
1034int
1035okname(cp0)
1036 char *cp0;
1037{
1038 int c;
1039 char *cp;
1040
1041 cp = cp0;
1042 do {
1043 c = *cp;
1044 if (c & 0200)
1045 goto bad;
Kevin Steves8daed182000-12-16 19:21:03 +00001046 if (!isalpha(c) && !isdigit(c) &&
1047 c != '_' && c != '-' && c != '.' && c != '+')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048 goto bad;
1049 } while (*++cp);
1050 return (1);
1051
Damien Miller98c7ad62000-03-09 21:27:49 +11001052bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053 return (0);
1054}
1055
1056BUF *
1057allocbuf(bp, fd, blksize)
1058 BUF *bp;
1059 int fd, blksize;
1060{
1061 size_t size;
Damien Miller78315eb2000-09-29 23:01:36 +11001062#ifdef HAVE_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063 struct stat stb;
1064
1065 if (fstat(fd, &stb) < 0) {
1066 run_err("fstat: %s", strerror(errno));
1067 return (0);
1068 }
Damien Miller95def091999-11-25 00:26:21 +11001069 if (stb.st_blksize == 0)
1070 size = blksize;
1071 else
1072 size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
Damien Millerad833b32000-08-23 10:46:23 +10001073 stb.st_blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001074#else /* HAVE_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001075 size = blksize;
Damien Miller78315eb2000-09-29 23:01:36 +11001076#endif /* HAVE_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077 if (bp->cnt >= size)
1078 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001079 if (bp->buf == NULL)
1080 bp->buf = xmalloc(size);
1081 else
1082 bp->buf = xrealloc(bp->buf, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083 bp->cnt = size;
1084 return (bp);
1085}
1086
1087void
1088lostconn(signo)
1089 int signo;
1090{
1091 if (!iamremote)
1092 fprintf(stderr, "lost connection\n");
1093 exit(1);
1094}
1095
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096
1097void
1098alarmtimer(int wait)
1099{
Damien Miller95def091999-11-25 00:26:21 +11001100 struct itimerval itv;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101
Damien Miller95def091999-11-25 00:26:21 +11001102 itv.it_value.tv_sec = wait;
1103 itv.it_value.tv_usec = 0;
1104 itv.it_interval = itv.it_value;
1105 setitimer(ITIMER_REAL, &itv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106}
1107
1108void
Damien Miller7684ee12000-03-17 23:40:15 +11001109updateprogressmeter(int ignore)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001110{
1111 int save_errno = errno;
1112
1113 progressmeter(0);
1114 errno = save_errno;
1115}
1116
Damien Miller81428f91999-11-18 09:28:11 +11001117int
Ben Lindstrom31ca54a2001-02-09 02:11:24 +00001118foregroundproc(void)
Damien Miller81428f91999-11-18 09:28:11 +11001119{
1120 static pid_t pgrp = -1;
1121 int ctty_pgrp;
1122
1123 if (pgrp == -1)
1124 pgrp = getpgrp();
1125
Ben Lindstromdd5c5a32001-02-02 18:58:33 +00001126#ifdef HAVE_TCGETPGRP
Damien Millerbac2d8a2000-09-05 16:13:06 +11001127 return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
1128 ctty_pgrp == pgrp);
1129#else
Damien Miller95def091999-11-25 00:26:21 +11001130 return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
1131 ctty_pgrp == pgrp));
Damien Millerbac2d8a2000-09-05 16:13:06 +11001132#endif
Damien Miller81428f91999-11-18 09:28:11 +11001133}
1134
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135void
1136progressmeter(int flag)
1137{
1138 static const char prefixes[] = " KMGTP";
1139 static struct timeval lastupdate;
1140 static off_t lastsize;
1141 struct timeval now, td, wait;
1142 off_t cursize, abbrevsize;
1143 double elapsed;
Damien Miller7c64ba31999-11-11 21:39:50 +11001144 int ratio, barlength, i, remaining;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145 char buf[256];
1146
1147 if (flag == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001148 (void) gettimeofday(&start, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149 lastupdate = start;
1150 lastsize = 0;
Damien Miller95def091999-11-25 00:26:21 +11001151 }
Damien Miller81428f91999-11-18 09:28:11 +11001152 if (foregroundproc() == 0)
1153 return;
1154
Damien Miller95def091999-11-25 00:26:21 +11001155 (void) gettimeofday(&now, (struct timezone *) 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 cursize = statbytes;
Damien Millera2d6efe1999-11-13 13:22:46 +11001157 if (totalbytes != 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001158 ratio = 100.0 * cursize / totalbytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159 ratio = MAX(ratio, 0);
1160 ratio = MIN(ratio, 100);
Damien Miller95def091999-11-25 00:26:21 +11001161 } else
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 ratio = 100;
1163
Damien Miller95def091999-11-25 00:26:21 +11001164 snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165
1166 barlength = getttywidth() - 51;
Damien Miller484118e2000-07-02 19:13:56 +10001167 barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 if (barlength > 0) {
1169 i = barlength * ratio / 100;
1170 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Miller484118e2000-07-02 19:13:56 +10001171 "|%.*s%*s|", i, BAR, barlength - i, "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 i = 0;
1174 abbrevsize = cursize;
1175 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
1176 i++;
1177 abbrevsize >>= 10;
1178 }
Kevin Stevesadf74cd2001-02-05 14:22:50 +00001179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ",
Kevin Stevesd2e0d7d2001-02-11 14:19:40 +00001180 (unsigned long) abbrevsize, prefixes[i],
1181 prefixes[i] == ' ' ? ' ' : 'B');
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182
1183 timersub(&now, &lastupdate, &wait);
1184 if (cursize > lastsize) {
1185 lastupdate = now;
1186 lastsize = cursize;
1187 if (wait.tv_sec >= STALLTIME) {
1188 start.tv_sec += wait.tv_sec;
1189 start.tv_usec += wait.tv_usec;
1190 }
1191 wait.tv_sec = 0;
1192 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 timersub(&now, &start, &td);
1194 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1195
Kevin Steves7d00ba42000-12-15 23:03:10 +00001196 if (flag != 1 &&
1197 (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001199 " --:-- ETA");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200 } else if (wait.tv_sec >= STALLTIME) {
1201 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Kevin Steves7d00ba42000-12-15 23:03:10 +00001202 " - stalled -");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 } else {
Damien Miller8bb73be2000-04-19 16:26:12 +10001204 if (flag != 1)
Kevin Steves7d00ba42000-12-15 23:03:10 +00001205 remaining = (int)(totalbytes / (statbytes / elapsed) -
1206 elapsed);
Damien Miller8bb73be2000-04-19 16:26:12 +10001207 else
1208 remaining = elapsed;
1209
Damien Miller01ab4a21999-10-28 15:23:30 +10001210 i = remaining / 3600;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 if (i)
1212 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001213 "%2d:", i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214 else
1215 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001216 " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217 i = remaining % 3600;
1218 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
Damien Millerad833b32000-08-23 10:46:23 +10001219 "%02d:%02d%s", i / 60, i % 60,
1220 (flag != 1) ? " ETA" : " ");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221 }
1222 atomicio(write, fileno(stdout), buf, strlen(buf));
1223
1224 if (flag == -1) {
Kevin Steves93c17d92001-02-18 03:55:16 +00001225 mysignal(SIGALRM, updateprogressmeter);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 alarmtimer(1);
1227 } else if (flag == 1) {
1228 alarmtimer(0);
Damien Miller35dabd02000-05-01 21:10:33 +10001229 atomicio(write, fileno(stdout), "\n", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230 statbytes = 0;
1231 }
1232}
1233
1234int
1235getttywidth(void)
1236{
1237 struct winsize winsize;
1238
1239 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
Damien Miller95def091999-11-25 00:26:21 +11001240 return (winsize.ws_col ? winsize.ws_col : 80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 else
Damien Miller95def091999-11-25 00:26:21 +11001242 return (80);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243}
Damien Miller874d77b2000-10-14 16:23:11 +11001244
1245void
1246addargs(char *fmt, ...)
1247{
1248 va_list ap;
1249 char buf[1024];
1250
1251 va_start(ap, fmt);
1252 vsnprintf(buf, sizeof(buf), fmt, ap);
1253 va_end(ap);
1254
1255 if (args.list == NULL) {
1256 args.nalloc = 32;
1257 args.num = 0;
1258 args.list = xmalloc(args.nalloc * sizeof(char *));
1259 } else if (args.num+2 >= args.nalloc) {
1260 args.nalloc *= 2;
1261 args.list = xrealloc(args.list, args.nalloc * sizeof(char *));
1262 }
1263 args.list[args.num++] = xstrdup(buf);
1264 args.list[args.num] = NULL;
1265}