blob: 6d2692e16478149957cbbe9941159d3ca6eb870a [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.
Damien Millerdafb12e2003-06-03 13:06:18 +100055 * 3. Neither the name of the University nor the names of its contributors
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071 */
72
73#include "includes.h"
Damien Miller65d1f572003-06-04 22:51:08 +100074RCSID("$OpenBSD: scp.c,v 1.104 2003/06/04 12:18:49 djm Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "atomicio.h"
78#include "pathnames.h"
79#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +000080#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +110081#include "progressmeter.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Ben Lindstrom49a79c02000-11-17 03:47:20 +000083#ifdef HAVE___PROGNAME
84extern char *__progname;
85#else
86char *__progname;
87#endif
88
Damien Millerff74d742003-01-24 11:36:58 +110089void bwlimit(int);
90
Ben Lindstrom387c4722001-05-08 20:27:25 +000091/* Struct for addargs */
92arglist args;
Damien Miller874d77b2000-10-14 16:23:11 +110093
Damien Millerff74d742003-01-24 11:36:58 +110094/* Bandwidth limit */
Ben Lindstromd54d9382003-03-21 00:55:32 +000095off_t limitbw = 0;
Damien Millerff74d742003-01-24 11:36:58 +110096
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097/* Name of current file being transferred. */
98char *curfile;
99
100/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100101int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103/* This is set to zero if the progressmeter is not desired. */
104int showprogress = 1;
105
Damien Millerad833b32000-08-23 10:46:23 +1000106/* This is the program to execute for the secured connection. ("ssh" or -S) */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000107char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000108
Ben Lindstrom007eb912002-11-09 15:54:08 +0000109/* This is used to store the pid of ssh_program */
110pid_t do_cmd_pid;
111
Damien Miller5428f641999-11-25 11:54:57 +1100112/*
113 * This function executes the given command as the specified user on the
114 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
115 * assigns the input and output file descriptors on success.
116 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller4af51302000-04-16 11:18:38 +1000118int
Damien Millerad833b32000-08-23 10:46:23 +1000119do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120{
Damien Miller95def091999-11-25 00:26:21 +1100121 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000124 fprintf(stderr,
125 "Executing: program %s host %s, user %s, command %s\n",
126 ssh_program, host,
127 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Damien Miller5428f641999-11-25 11:54:57 +1100129 /*
130 * Reserve two descriptors so that the real pipes won't get
131 * descriptors 0 and 1 because that will screw up dup2 below.
132 */
Damien Miller95def091999-11-25 00:26:21 +1100133 pipe(reserved);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller95def091999-11-25 00:26:21 +1100135 /* Create a socket pair for communicating with ssh. */
136 if (pipe(pin) < 0)
137 fatal("pipe: %s", strerror(errno));
138 if (pipe(pout) < 0)
139 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller95def091999-11-25 00:26:21 +1100141 /* Free the reserved descriptors. */
142 close(reserved[0]);
143 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 /* For a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000146 do_cmd_pid = fork();
147 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100148 /* Child. */
149 close(pin[1]);
150 close(pout[0]);
151 dup2(pin[0], 0);
152 dup2(pout[1], 1);
153 close(pin[0]);
154 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller874d77b2000-10-14 16:23:11 +1100156 args.list[0] = ssh_program;
157 if (remuser != NULL)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000158 addargs(&args, "-l%s", remuser);
159 addargs(&args, "%s", host);
160 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100161
Damien Miller874d77b2000-10-14 16:23:11 +1100162 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000163 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100164 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000165 } else if (do_cmd_pid == -1) {
166 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167 }
Damien Miller95def091999-11-25 00:26:21 +1100168 /* Parent. Close the other side, and return the local side. */
169 close(pin[0]);
170 *fdout = pin[1];
171 close(pout[1]);
172 *fdin = pout[0];
173 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
175
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176typedef struct {
177 int cnt;
178 char *buf;
179} BUF;
180
Damien Miller95def091999-11-25 00:26:21 +1100181BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100182void lostconn(int);
183void nospace(void);
184int okname(char *);
185void run_err(const char *,...);
186void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100189uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190int errs, remin, remout;
191int pflag, iamremote, iamrecursive, targetshouldbedirectory;
192
193#define CMDNEEDS 64
194char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
195
Damien Miller95def091999-11-25 00:26:21 +1100196int response(void);
197void rsource(char *, struct stat *);
198void sink(int, char *[]);
199void source(int, char *[]);
200void tolocal(int, char *[]);
201void toremote(char *, int, char *[]);
202void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
204int
Damien Miller65d1f572003-06-04 22:51:08 +1000205main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Ben Lindstrom007eb912002-11-09 15:54:08 +0000207 int ch, fflag, tflag, status;
Damien Millerff74d742003-01-24 11:36:58 +1100208 double speed;
209 char *targ, *endp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210 extern char *optarg;
211 extern int optind;
212
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000213 __progname = get_progname(argv[0]);
214
Damien Miller874d77b2000-10-14 16:23:11 +1100215 args.list = NULL;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000216 addargs(&args, "ssh"); /* overwritten with ssh_program */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000217 addargs(&args, "-x");
Ben Lindstrom4213c552001-09-12 18:45:09 +0000218 addargs(&args, "-oForwardAgent no");
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000219 addargs(&args, "-oClearAllForwardings yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100220
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221 fflag = tflag = 0;
Damien Miller8e121472003-01-24 11:37:38 +1100222 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100223 switch (ch) {
224 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100225 case '1':
226 case '2':
Damien Miller34132e52000-01-14 15:45:46 +1100227 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100228 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100229 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000230 addargs(&args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100231 break;
232 case 'o':
233 case 'c':
234 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000235 case 'F':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000236 addargs(&args, "-%c%s", ch, optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100237 break;
238 case 'P':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000239 addargs(&args, "-p%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100240 break;
241 case 'B':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000242 addargs(&args, "-oBatchmode yes");
Damien Miller34132e52000-01-14 15:45:46 +1100243 break;
Damien Millerff74d742003-01-24 11:36:58 +1100244 case 'l':
245 speed = strtod(optarg, &endp);
246 if (speed <= 0 || *endp != '\0')
247 usage();
Ben Lindstromd54d9382003-03-21 00:55:32 +0000248 limitbw = speed * 1024;
Damien Millerff74d742003-01-24 11:36:58 +1100249 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250 case 'p':
251 pflag = 1;
252 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253 case 'r':
254 iamrecursive = 1;
255 break;
Damien Millerad833b32000-08-23 10:46:23 +1000256 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100257 ssh_program = xstrdup(optarg);
258 break;
259 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000260 addargs(&args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100261 verbose_mode = 1;
262 break;
263 case 'q':
264 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000265 break;
266
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268 case 'd':
269 targetshouldbedirectory = 1;
270 break;
Damien Miller95def091999-11-25 00:26:21 +1100271 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272 iamremote = 1;
273 fflag = 1;
274 break;
Damien Miller95def091999-11-25 00:26:21 +1100275 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276 iamremote = 1;
277 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000278#ifdef HAVE_CYGWIN
279 setmode(0, O_BINARY);
280#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282 default:
283 usage();
284 }
285 argc -= optind;
286 argv += optind;
287
288 if ((pwd = getpwuid(userid = getuid())) == NULL)
Damien Miller95def091999-11-25 00:26:21 +1100289 fatal("unknown user %d", (int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290
Damien Miller95def091999-11-25 00:26:21 +1100291 if (!isatty(STDERR_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292 showprogress = 0;
293
294 remin = STDIN_FILENO;
295 remout = STDOUT_FILENO;
296
Kevin Stevesef4eea92001-02-05 12:42:17 +0000297 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100298 /* Follow "protocol", send data. */
299 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300 source(argc, argv);
301 exit(errs != 0);
302 }
Damien Miller95def091999-11-25 00:26:21 +1100303 if (tflag) {
304 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305 sink(argc, argv);
306 exit(errs != 0);
307 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308 if (argc < 2)
309 usage();
310 if (argc > 2)
311 targetshouldbedirectory = 1;
312
313 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000314 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000316 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
317 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000318 iamrecursive ? " -r" : "", pflag ? " -p" : "",
319 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Damien Miller95def091999-11-25 00:26:21 +1100321 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
323 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
324 toremote(targ, argc, argv);
325 else {
Damien Miller95def091999-11-25 00:26:21 +1100326 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327 if (targetshouldbedirectory)
328 verifydir(argv[argc - 1]);
329 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000330 /*
331 * Finally check the exit status of the ssh process, if one was forked
332 * and no error has occured yet
333 */
334 if (do_cmd_pid != -1 && errs == 0) {
335 if (remin != -1)
336 (void) close(remin);
337 if (remout != -1)
338 (void) close(remout);
339 if (waitpid(do_cmd_pid, &status, 0) == -1)
340 errs = 1;
341 else {
342 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
343 errs = 1;
344 }
345 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346 exit(errs != 0);
347}
348
349void
Damien Miller65d1f572003-06-04 22:51:08 +1000350toremote(char *targ, int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351{
352 int i, len;
353 char *bp, *host, *src, *suser, *thost, *tuser;
354
355 *targ++ = 0;
356 if (*targ == 0)
357 targ = ".";
358
Ben Lindstromc276c122002-12-23 02:14:51 +0000359 if ((thost = strrchr(argv[argc - 1], '@'))) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360 /* user@host */
361 *thost++ = 0;
362 tuser = argv[argc - 1];
363 if (*tuser == '\0')
364 tuser = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365 } else {
366 thost = argv[argc - 1];
367 tuser = NULL;
368 }
369
370 for (i = 0; i < argc - 1; i++) {
371 src = colon(argv[i]);
Damien Miller95def091999-11-25 00:26:21 +1100372 if (src) { /* remote to remote */
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000373 static char *ssh_options =
Ben Lindstromaf0c6d62002-06-09 20:06:29 +0000374 "-x -o'ClearAllForwardings yes'";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375 *src++ = 0;
376 if (*src == 0)
377 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000378 host = strrchr(argv[i], '@');
Damien Millerad833b32000-08-23 10:46:23 +1000379 len = strlen(ssh_program) + strlen(argv[i]) +
380 strlen(src) + (tuser ? strlen(tuser) : 0) +
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000381 strlen(thost) + strlen(targ) +
382 strlen(ssh_options) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100383 bp = xmalloc(len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384 if (host) {
385 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100386 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387 suser = argv[i];
388 if (*suser == '\0')
389 suser = pwd->pw_name;
Damien Miller00111382003-03-10 11:21:17 +1100390 else if (!okname(suser)) {
391 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392 continue;
Damien Miller00111382003-03-10 11:21:17 +1100393 }
394 if (tuser && !okname(tuser)) {
395 xfree(bp);
Damien Millereeeeb352003-02-24 11:52:58 +1100396 continue;
Damien Miller00111382003-03-10 11:21:17 +1100397 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000398 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000399 "%s%s %s -n "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000400 "-l %s %s %s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000401 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000402 ssh_options, suser, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000403 tuser ? tuser : "", tuser ? "@" : "",
404 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100405 } else {
406 host = cleanhostname(argv[i]);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000407 snprintf(bp, len,
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000408 "exec %s%s %s -n %s "
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000409 "%s %s '%s%s%s:%s'",
Kevin Steves7d00ba42000-12-15 23:03:10 +0000410 ssh_program, verbose_mode ? " -v" : "",
Ben Lindstrom2b7a0e92001-09-20 00:57:55 +0000411 ssh_options, host, cmd, src,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000412 tuser ? tuser : "", tuser ? "@" : "",
413 thost, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100414 }
Damien Miller95def091999-11-25 00:26:21 +1100415 if (verbose_mode)
416 fprintf(stderr, "Executing: %s\n", bp);
417 (void) system(bp);
418 (void) xfree(bp);
419 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420 if (remin == -1) {
421 len = strlen(targ) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100422 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000423 (void) snprintf(bp, len, "%s -t %s", cmd, targ);
Damien Miller34132e52000-01-14 15:45:46 +1100424 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000425 if (do_cmd(host, tuser, bp, &remin,
426 &remout, argc) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100427 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428 if (response() < 0)
429 exit(1);
Damien Miller95def091999-11-25 00:26:21 +1100430 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431 }
Damien Miller95def091999-11-25 00:26:21 +1100432 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433 }
434 }
435}
436
437void
Damien Miller65d1f572003-06-04 22:51:08 +1000438tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439{
440 int i, len;
441 char *bp, *host, *src, *suser;
442
443 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100444 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445 len = strlen(_PATH_CP) + strlen(argv[i]) +
Damien Millerad833b32000-08-23 10:46:23 +1000446 strlen(argv[argc - 1]) + 20;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000448 (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
Damien Millerad833b32000-08-23 10:46:23 +1000449 iamrecursive ? " -r" : "", pflag ? " -p" : "",
450 argv[i], argv[argc - 1]);
Damien Miller95def091999-11-25 00:26:21 +1100451 if (verbose_mode)
452 fprintf(stderr, "Executing: %s\n", bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453 if (system(bp))
454 ++errs;
Damien Miller95def091999-11-25 00:26:21 +1100455 (void) xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000456 continue;
457 }
458 *src++ = 0;
459 if (*src == 0)
460 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000461 if ((host = strrchr(argv[i], '@')) == NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462 host = argv[i];
463 suser = NULL;
464 } else {
465 *host++ = 0;
466 suser = argv[i];
467 if (*suser == '\0')
468 suser = pwd->pw_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 }
Damien Miller34132e52000-01-14 15:45:46 +1100470 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 len = strlen(src) + CMDNEEDS + 20;
Damien Miller95def091999-11-25 00:26:21 +1100472 bp = xmalloc(len);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000473 (void) snprintf(bp, len, "%s -f %s", cmd, src);
Damien Millerad833b32000-08-23 10:46:23 +1000474 if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100475 (void) xfree(bp);
476 ++errs;
477 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478 }
Damien Miller95def091999-11-25 00:26:21 +1100479 xfree(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100481 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482 remin = remout = -1;
483 }
484}
485
486void
Damien Miller65d1f572003-06-04 22:51:08 +1000487source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488{
489 struct stat stb;
490 static BUF buffer;
491 BUF *bp;
Damien Miller62d57f62003-01-10 21:43:24 +1100492 off_t i, amt, result, statbytes;
Ben Lindstrom4eda71d2001-04-22 17:13:20 +0000493 int fd, haderr, indx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000494 char *last, *name, buf[2048];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000495 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496
497 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100498 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000500 len = strlen(name);
501 while (len > 1 && name[len-1] == '/')
502 name[--len] = '\0';
Ben Lindstrom3e45e4c2001-10-03 17:30:58 +0000503 if (strchr(name, '\n') != NULL) {
504 run_err("%s: skipping, filename contains a newline",
505 name);
506 goto next;
507 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508 if ((fd = open(name, O_RDONLY, 0)) < 0)
509 goto syserr;
510 if (fstat(fd, &stb) < 0) {
511syserr: run_err("%s: %s", name, strerror(errno));
512 goto next;
513 }
514 switch (stb.st_mode & S_IFMT) {
515 case S_IFREG:
516 break;
517 case S_IFDIR:
518 if (iamrecursive) {
519 rsource(name, &stb);
520 goto next;
521 }
522 /* FALLTHROUGH */
523 default:
524 run_err("%s: not a regular file", name);
525 goto next;
526 }
527 if ((last = strrchr(name, '/')) == NULL)
528 last = name;
529 else
530 ++last;
531 curfile = last;
532 if (pflag) {
533 /*
534 * Make it compatible with possible future
535 * versions expecting microseconds.
536 */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000537 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000538 (u_long) stb.st_mtime,
539 (u_long) stb.st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000540 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541 if (response() < 0)
542 goto next;
543 }
544#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
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 Lindstrom0e7f4362003-04-28 23:30:43 +0000547 (int64_t)stb.st_size, last);
Damien Miller95def091999-11-25 00:26:21 +1100548 if (verbose_mode) {
549 fprintf(stderr, "Sending file modes: %s", buf);
Damien Miller95def091999-11-25 00:26:21 +1100550 }
Damien Miller35dabd02000-05-01 21:10:33 +1000551 (void) atomicio(write, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552 if (response() < 0)
553 goto next;
554 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100555next: (void) close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 continue;
557 }
Damien Miller62d57f62003-01-10 21:43:24 +1100558 if (showprogress)
559 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560 /* Keep writing after an error so that we stay sync'd up. */
561 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
562 amt = bp->cnt;
563 if (i + amt > stb.st_size)
564 amt = stb.st_size - i;
565 if (!haderr) {
Damien Millere247cc42000-05-07 12:03:14 +1000566 result = atomicio(read, fd, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567 if (result != amt)
568 haderr = result >= 0 ? EIO : errno;
569 }
570 if (haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000571 (void) atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572 else {
Damien Miller864ea591999-12-15 11:04:25 +1100573 result = atomicio(write, remout, bp->buf, amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574 if (result != amt)
575 haderr = result >= 0 ? EIO : errno;
576 statbytes += result;
577 }
Ben Lindstromd54d9382003-03-21 00:55:32 +0000578 if (limitbw)
Damien Millerff74d742003-01-24 11:36:58 +1100579 bwlimit(amt);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580 }
Damien Miller95def091999-11-25 00:26:21 +1100581 if (showprogress)
Damien Miller62d57f62003-01-10 21:43:24 +1100582 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000583
584 if (close(fd) < 0 && !haderr)
585 haderr = errno;
586 if (!haderr)
Damien Miller35dabd02000-05-01 21:10:33 +1000587 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 else
589 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100590 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 }
592}
593
594void
Damien Miller65d1f572003-06-04 22:51:08 +1000595rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596{
597 DIR *dirp;
598 struct dirent *dp;
599 char *last, *vect[1], path[1100];
600
601 if (!(dirp = opendir(name))) {
602 run_err("%s: %s", name, strerror(errno));
603 return;
604 }
605 last = strrchr(name, '/');
606 if (last == 0)
607 last = name;
608 else
609 last++;
610 if (pflag) {
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000611 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000612 (u_long) statp->st_mtime,
613 (u_long) statp->st_atime);
Damien Miller35dabd02000-05-01 21:10:33 +1000614 (void) atomicio(write, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000615 if (response() < 0) {
616 closedir(dirp);
617 return;
618 }
619 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000620 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000621 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100622 if (verbose_mode)
623 fprintf(stderr, "Entering directory: %s", path);
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 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000629 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630 if (dp->d_ino == 0)
631 continue;
632 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
633 continue;
634 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
635 run_err("%s/%s: name too long", name, dp->d_name);
636 continue;
637 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000638 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000639 vect[0] = path;
640 source(1, vect);
641 }
Damien Miller95def091999-11-25 00:26:21 +1100642 (void) closedir(dirp);
Damien Miller35dabd02000-05-01 21:10:33 +1000643 (void) atomicio(write, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100644 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645}
646
647void
Damien Millerff74d742003-01-24 11:36:58 +1100648bwlimit(int amount)
649{
650 static struct timeval bwstart, bwend;
651 static int lamt, thresh = 16384;
652 u_int64_t wait;
653 struct timespec ts, rm;
654
655 if (!timerisset(&bwstart)) {
656 gettimeofday(&bwstart, NULL);
657 return;
658 }
659
660 lamt += amount;
661 if (lamt < thresh)
662 return;
663
664 gettimeofday(&bwend, NULL);
665 timersub(&bwend, &bwstart, &bwend);
666 if (!timerisset(&bwend))
667 return;
668
669 lamt *= 8;
Ben Lindstromd54d9382003-03-21 00:55:32 +0000670 wait = (double)1000000L * lamt / limitbw;
Damien Millerff74d742003-01-24 11:36:58 +1100671
672 bwstart.tv_sec = wait / 1000000L;
673 bwstart.tv_usec = wait % 1000000L;
674
675 if (timercmp(&bwstart, &bwend, >)) {
676 timersub(&bwstart, &bwend, &bwend);
677
678 /* Adjust the wait time */
679 if (bwend.tv_sec) {
680 thresh /= 2;
681 if (thresh < 2048)
682 thresh = 2048;
683 } else if (bwend.tv_usec < 100) {
684 thresh *= 2;
685 if (thresh > 32768)
686 thresh = 32768;
687 }
688
689 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
690 while (nanosleep(&ts, &rm) == -1) {
691 if (errno != EINTR)
692 break;
693 ts = rm;
694 }
695 }
696
697 lamt = 0;
698 gettimeofday(&bwstart, NULL);
699}
700
701void
Damien Miller65d1f572003-06-04 22:51:08 +1000702sink(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703{
704 static BUF buffer;
705 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100706 enum {
707 YES, NO, DISPLAYED
708 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709 BUF *bp;
710 off_t i, j;
711 int amt, count, exists, first, mask, mode, ofd, omode;
Damien Miller62d57f62003-01-10 21:43:24 +1100712 off_t size, statbytes;
Damien Millercaf6dd62000-08-29 11:33:50 +1100713 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100715 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716
Ben Lindstromf719a202001-04-14 23:14:22 +0000717#define atime tv[0]
718#define mtime tv[1]
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +0000719#define SCREWUP(str) do { why = str; goto screwup; } while (0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
721 setimes = targisdir = 0;
722 mask = umask(0);
723 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100724 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000725 if (argc != 1) {
726 run_err("ambiguous target");
727 exit(1);
728 }
729 targ = *argv;
730 if (targetshouldbedirectory)
731 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100732
Damien Miller35dabd02000-05-01 21:10:33 +1000733 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
735 targisdir = 1;
736 for (first = 1;; first = 0) {
737 cp = buf;
Damien Millere247cc42000-05-07 12:03:14 +1000738 if (atomicio(read, remin, cp, 1) <= 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739 return;
740 if (*cp++ == '\n')
741 SCREWUP("unexpected <newline>");
742 do {
Damien Millere247cc42000-05-07 12:03:14 +1000743 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000744 SCREWUP("lost connection");
745 *cp++ = ch;
746 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
747 *cp = 0;
748
749 if (buf[0] == '\01' || buf[0] == '\02') {
750 if (iamremote == 0)
Damien Miller35dabd02000-05-01 21:10:33 +1000751 (void) atomicio(write, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000752 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753 if (buf[0] == '\02')
754 exit(1);
755 ++errs;
756 continue;
757 }
758 if (buf[0] == 'E') {
Damien Miller35dabd02000-05-01 21:10:33 +1000759 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000760 return;
761 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762 if (ch == '\n')
763 *--cp = 0;
764
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765 cp = buf;
766 if (*cp == 'T') {
767 setimes++;
768 cp++;
Ben Lindstromf719a202001-04-14 23:14:22 +0000769 mtime.tv_sec = strtol(cp, &cp, 10);
770 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771 SCREWUP("mtime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000772 mtime.tv_usec = strtol(cp, &cp, 10);
773 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774 SCREWUP("mtime.usec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000775 atime.tv_sec = strtol(cp, &cp, 10);
776 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777 SCREWUP("atime.sec not delimited");
Ben Lindstromf719a202001-04-14 23:14:22 +0000778 atime.tv_usec = strtol(cp, &cp, 10);
779 if (!cp || *cp++ != '\0')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000780 SCREWUP("atime.usec not delimited");
Damien Miller35dabd02000-05-01 21:10:33 +1000781 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782 continue;
783 }
784 if (*cp != 'C' && *cp != 'D') {
785 /*
786 * Check for the case "rcp remote:foo\* local:bar".
787 * In this case, the line "No match." can be returned
788 * by the shell before the rcp command on the remote is
789 * executed so the ^Aerror_message convention isn't
790 * followed.
791 */
792 if (first) {
793 run_err("%s", cp);
794 exit(1);
795 }
796 SCREWUP("expected control record");
797 }
798 mode = 0;
799 for (++cp; cp < buf + 5; cp++) {
800 if (*cp < '0' || *cp > '7')
801 SCREWUP("bad mode");
802 mode = (mode << 3) | (*cp - '0');
803 }
804 if (*cp++ != ' ')
805 SCREWUP("mode not delimited");
806
Ben Lindstrombd472262001-03-29 00:39:55 +0000807 for (size = 0; isdigit(*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808 size = size * 10 + (*cp++ - '0');
809 if (*cp++ != ' ')
810 SCREWUP("size not delimited");
811 if (targisdir) {
812 static char *namebuf;
813 static int cursize;
814 size_t need;
815
816 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000817 if (need > cursize) {
818 if (namebuf)
819 xfree(namebuf);
Damien Miller95def091999-11-25 00:26:21 +1100820 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000821 cursize = need;
822 }
823 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +0000824 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825 np = namebuf;
826 } else
827 np = targ;
828 curfile = cp;
829 exists = stat(np, &stb) == 0;
830 if (buf[0] == 'D') {
831 int mod_flag = pflag;
832 if (exists) {
833 if (!S_ISDIR(stb.st_mode)) {
834 errno = ENOTDIR;
835 goto bad;
836 }
837 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +1100838 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839 } else {
Damien Miller95def091999-11-25 00:26:21 +1100840 /* Handle copying from a read-only
841 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842 mod_flag = 1;
843 if (mkdir(np, mode | S_IRWXU) < 0)
844 goto bad;
845 }
Ben Lindstrom550bc542001-02-10 21:50:00 +0000846 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847 sink(1, vect);
848 if (setimes) {
849 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +1100850 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100851 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +1100852 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 }
854 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +1100855 (void) chmod(vect[0], mode);
856 if (vect[0])
857 xfree(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858 continue;
859 }
860 omode = mode;
861 mode |= S_IWRITE;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +0000862 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863bad: run_err("%s: %s", np, strerror(errno));
864 continue;
865 }
Damien Miller35dabd02000-05-01 21:10:33 +1000866 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100868 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869 continue;
870 }
871 cp = bp->buf;
872 wrerr = NO;
873
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +1100875 if (showprogress)
876 start_progress_meter(curfile, size, &statbytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877 for (count = i = 0; i < size; i += 4096) {
878 amt = 4096;
879 if (i + amt > size)
880 amt = size - i;
881 count += amt;
882 do {
Damien Miller69b69aa2000-10-28 14:19:58 +1100883 j = read(remin, cp, amt);
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000884 if (j == -1 && (errno == EINTR ||
885 errno == EAGAIN)) {
Damien Miller69b69aa2000-10-28 14:19:58 +1100886 continue;
887 } else if (j <= 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888 run_err("%s", j ? strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +0000889 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890 exit(1);
891 }
892 amt -= j;
893 cp += j;
Damien Miller95def091999-11-25 00:26:21 +1100894 statbytes += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895 } while (amt > 0);
Damien Millerff74d742003-01-24 11:36:58 +1100896
Ben Lindstromd54d9382003-03-21 00:55:32 +0000897 if (limitbw)
Damien Millerff74d742003-01-24 11:36:58 +1100898 bwlimit(4096);
899
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900 if (count == bp->cnt) {
901 /* Keep reading so we stay sync'd up. */
902 if (wrerr == NO) {
Damien Millere247cc42000-05-07 12:03:14 +1000903 j = atomicio(write, ofd, bp->buf, count);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904 if (j != count) {
905 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100906 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907 }
908 }
909 count = 0;
910 cp = bp->buf;
911 }
912 }
913 if (showprogress)
Damien Miller62d57f62003-01-10 21:43:24 +1100914 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 if (count != 0 && wrerr == NO &&
Damien Millere247cc42000-05-07 12:03:14 +1000916 (j = atomicio(write, ofd, bp->buf, count)) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917 wrerr = YES;
Damien Miller95def091999-11-25 00:26:21 +1100918 wrerrno = j >= 0 ? EIO : errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919 }
Damien Miller86b781c2003-01-10 21:44:48 +1100920 if (wrerr == NO && ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921 run_err("%s: truncate: %s", np, strerror(errno));
922 wrerr = DISPLAYED;
923 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924 if (pflag) {
925 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100926#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927 if (fchmod(ofd, omode))
Damien Miller78315eb2000-09-29 23:01:36 +1100928#else /* HAVE_FCHMOD */
929 if (chmod(np, omode))
930#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000932 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 } else {
934 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +1100935#ifdef HAVE_FCHMOD
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936 if (fchmod(ofd, omode & ~mask))
Damien Miller78315eb2000-09-29 23:01:36 +1100937#else /* HAVE_FCHMOD */
938 if (chmod(np, omode & ~mask))
939#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000941 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 }
Damien Millerbe484b52000-07-15 14:14:16 +1000943 if (close(ofd) == -1) {
944 wrerr = YES;
945 wrerrno = errno;
946 }
Damien Miller95def091999-11-25 00:26:21 +1100947 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948 if (setimes && wrerr == NO) {
949 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100950 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +0000952 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953 wrerr = DISPLAYED;
954 }
955 }
Damien Miller95def091999-11-25 00:26:21 +1100956 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957 case YES:
958 run_err("%s: %s", np, strerror(wrerrno));
959 break;
960 case NO:
Damien Miller35dabd02000-05-01 21:10:33 +1000961 (void) atomicio(write, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962 break;
963 case DISPLAYED:
964 break;
965 }
966 }
967screwup:
968 run_err("protocol error: %s", why);
969 exit(1);
970}
971
972int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000973response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974{
975 char ch, *cp, resp, rbuf[2048];
976
Damien Millere247cc42000-05-07 12:03:14 +1000977 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978 lostconn(0);
979
980 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +1100981 switch (resp) {
982 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983 return (0);
984 default:
985 *cp++ = resp;
986 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +1100987 case 1: /* error, followed by error msg */
988 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989 do {
Damien Millere247cc42000-05-07 12:03:14 +1000990 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991 lostconn(0);
992 *cp++ = ch;
993 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
994
995 if (!iamremote)
Damien Miller35dabd02000-05-01 21:10:33 +1000996 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997 ++errs;
998 if (resp == 1)
999 return (-1);
1000 exit(1);
1001 }
1002 /* NOTREACHED */
1003}
1004
1005void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001006usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001008 (void) fprintf(stderr,
Damien Miller8e121472003-01-24 11:37:38 +11001009 "usage: scp [-pqrvBC1246] [-F config] [-S program] [-P port]\n"
Damien Millerff74d742003-01-24 11:36:58 +11001010 " [-c cipher] [-i identity] [-l limit] [-o option]\n"
Ben Lindstrom45933dd2002-06-21 00:10:58 +00001011 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 exit(1);
1013}
1014
1015void
Damien Miller95def091999-11-25 00:26:21 +11001016run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017{
1018 static FILE *fp;
1019 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020
1021 ++errs;
1022 if (fp == NULL && !(fp = fdopen(remout, "w")))
1023 return;
Damien Miller95def091999-11-25 00:26:21 +11001024 (void) fprintf(fp, "%c", 0x01);
1025 (void) fprintf(fp, "scp: ");
Damien Miller03783f01999-12-31 09:16:40 +11001026 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001027 (void) vfprintf(fp, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001028 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001029 (void) fprintf(fp, "\n");
1030 (void) fflush(fp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031
Damien Miller95def091999-11-25 00:26:21 +11001032 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001033 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001034 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001035 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001036 fprintf(stderr, "\n");
1037 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038}
1039
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040void
Damien Miller65d1f572003-06-04 22:51:08 +10001041verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042{
1043 struct stat stb;
1044
1045 if (!stat(cp, &stb)) {
1046 if (S_ISDIR(stb.st_mode))
1047 return;
1048 errno = ENOTDIR;
1049 }
1050 run_err("%s: %s", cp, strerror(errno));
1051 exit(1);
1052}
1053
1054int
Damien Miller65d1f572003-06-04 22:51:08 +10001055okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056{
1057 int c;
1058 char *cp;
1059
1060 cp = cp0;
1061 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001062 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063 if (c & 0200)
1064 goto bad;
Damien Millereeeeb352003-02-24 11:52:58 +11001065 if (!isalpha(c) && !isdigit(c)) {
1066 switch (c) {
1067 case '\'':
1068 case '"':
1069 case '`':
1070 case ' ':
1071 case '#':
1072 goto bad;
1073 default:
1074 break;
1075 }
1076 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077 } while (*++cp);
1078 return (1);
1079
Damien Miller98c7ad62000-03-09 21:27:49 +11001080bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081 return (0);
1082}
1083
1084BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001085allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086{
1087 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001088#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001089 struct stat stb;
1090
1091 if (fstat(fd, &stb) < 0) {
1092 run_err("fstat: %s", strerror(errno));
1093 return (0);
1094 }
Ben Lindstrom418e0782002-12-23 02:22:09 +00001095 size = roundup(stb.st_blksize, blksize);
1096 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001097 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001098#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001099 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001100#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101 if (bp->cnt >= size)
1102 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001103 if (bp->buf == NULL)
1104 bp->buf = xmalloc(size);
1105 else
1106 bp->buf = xrealloc(bp->buf, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001107 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108 bp->cnt = size;
1109 return (bp);
1110}
1111
1112void
Damien Miller65d1f572003-06-04 22:51:08 +10001113lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114{
1115 if (!iamremote)
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001116 write(STDERR_FILENO, "lost connection\n", 16);
1117 if (signo)
1118 _exit(1);
1119 else
1120 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121}