blob: f9f48e0759cffc50cc8e3149daeba45a72ef92e8 [file] [log] [blame]
jmc@openbsd.org4f1ca822017-05-02 08:06:33 +00001/* $OpenBSD: scp.c,v 1.191 2017/05/02 08:06:33 jmc Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Millere4340be2000-09-16 13:29:08 +11003 * scp - secure remote copy. This is basically patched BSD rcp which
4 * uses ssh to do the data transfer (instead of using rcmd).
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * NOTE: This version should NOT be suid root. (This uses ssh to
7 * do the transfer and ssh has the necessary privileges.)
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Miller95def091999-11-25 00:26:21 +11009 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +100010 *
Damien Millere4340be2000-09-16 13:29:08 +110011 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
16 */
17/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +000018 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110020 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42/*
Damien Millerad833b32000-08-23 10:46:23 +100043 * Parts from:
44 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045 * Copyright (c) 1983, 1990, 1992, 1993, 1995
46 * The Regents of the University of California. All rights reserved.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
Damien Millerdafb12e2003-06-03 13:06:18 +100056 * 3. Neither the name of the University nor the names of its contributors
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072 */
73
74#include "includes.h"
Damien Miller9cf6d072006-03-15 11:29:24 +110075
76#include <sys/types.h>
Damien Millerf17883e2006-03-15 11:45:54 +110077#ifdef HAVE_SYS_STAT_H
78# include <sys/stat.h>
79#endif
Tim Rice3eb14492008-03-13 17:59:50 -070080#ifdef HAVE_POLL_H
81#include <poll.h>
82#else
83# ifdef HAVE_SYS_POLL_H
84# include <sys/poll.h>
85# endif
Darren Tuckerccb13ee2008-02-29 15:07:01 +110086#endif
Damien Miller9aec9192006-08-05 10:57:45 +100087#ifdef HAVE_SYS_TIME_H
88# include <sys/time.h>
89#endif
Damien Miller9cf6d072006-03-15 11:29:24 +110090#include <sys/wait.h>
Damien Millerd7834352006-08-05 12:39:39 +100091#include <sys/uio.h>
Damien Miller88f254b2006-03-15 11:25:13 +110092
Damien Millerc7b06362006-03-15 11:53:45 +110093#include <ctype.h>
Damien Miller88f254b2006-03-15 11:25:13 +110094#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100095#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100096#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000097#include <limits.h>
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +000098#include <locale.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100099#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +1100100#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +1000101#include <stdarg.h>
Darren Tucker329037e2017-05-01 13:19:41 +1000102#ifdef HAVE_STDINT_H
millert@openbsd.org06643712017-04-28 03:21:12 +0000103#include <stdint.h>
Darren Tucker329037e2017-05-01 13:19:41 +1000104#endif
Damien Millera7a73ee2006-08-05 11:37:59 +1000105#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +1000106#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +1000107#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +1000108#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +1000109#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +1100110#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Darren Tuckerbed63112007-06-13 00:02:07 +1000111#include <vis.h>
112#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000115#include "atomicio.h"
116#include "pathnames.h"
117#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +0000118#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +1100119#include "progressmeter.h"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000120#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000122extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000123
Damien Millerc77cadb2007-10-26 15:39:15 +1000124#define COPY_BUFLEN 16384
125
Damien Miller07aa1322006-04-23 12:04:27 +1000126int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
Damien Millerf1211432011-01-06 22:40:30 +1100127int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000128
Ben Lindstrom387c4722001-05-08 20:27:25 +0000129/* Struct for addargs */
130arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100131arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100132
Damien Millerff74d742003-01-24 11:36:58 +1100133/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000134long long limit_kbps = 0;
135struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100136
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137/* Name of current file being transferred. */
138char *curfile;
139
140/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100141int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143/* This is set to zero if the progressmeter is not desired. */
144int showprogress = 1;
145
Damien Millerf1211432011-01-06 22:40:30 +1100146/*
147 * This is set to non-zero if remote-remote copy should be piped
148 * through this process.
149 */
150int throughlocal = 0;
151
Damien Millerad833b32000-08-23 10:46:23 +1000152/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100153char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000154
Ben Lindstrom007eb912002-11-09 15:54:08 +0000155/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000156pid_t do_cmd_pid = -1;
157
158static void
159killchild(int signo)
160{
Darren Tuckerba66df82005-01-24 21:57:40 +1100161 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000162 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100163 waitpid(do_cmd_pid, NULL, 0);
164 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000165
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000166 if (signo)
167 _exit(1);
168 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000169}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000170
Damien Miller0979b402010-07-02 13:37:33 +1000171static void
172suspchild(int signo)
173{
174 int status;
175
176 if (do_cmd_pid > 1) {
177 kill(do_cmd_pid, signo);
178 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
179 errno == EINTR)
180 ;
181 kill(getpid(), SIGSTOP);
182 }
183}
184
Damien Miller3eec6b72006-01-31 21:49:27 +1100185static int
186do_local_cmd(arglist *a)
187{
188 u_int i;
189 int status;
190 pid_t pid;
191
192 if (a->num == 0)
193 fatal("do_local_cmd: no arguments");
194
195 if (verbose_mode) {
196 fprintf(stderr, "Executing:");
197 for (i = 0; i < a->num; i++)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000198 fmprintf(stderr, " %s", a->list[i]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100199 fprintf(stderr, "\n");
200 }
201 if ((pid = fork()) == -1)
202 fatal("do_local_cmd: fork: %s", strerror(errno));
203
204 if (pid == 0) {
205 execvp(a->list[0], a->list);
206 perror(a->list[0]);
207 exit(1);
208 }
209
210 do_cmd_pid = pid;
211 signal(SIGTERM, killchild);
212 signal(SIGINT, killchild);
213 signal(SIGHUP, killchild);
214
215 while (waitpid(pid, &status, 0) == -1)
216 if (errno != EINTR)
217 fatal("do_local_cmd: waitpid: %s", strerror(errno));
218
219 do_cmd_pid = -1;
220
221 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
222 return (-1);
223
224 return (0);
225}
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
228 * This function executes the given command as the specified user on the
229 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
230 * assigns the input and output file descriptors on success.
231 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
Damien Miller4af51302000-04-16 11:18:38 +1000233int
Damien Miller07aa1322006-04-23 12:04:27 +1000234do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235{
Damien Miller95def091999-11-25 00:26:21 +1100236 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller95def091999-11-25 00:26:21 +1100238 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000239 fmprintf(stderr,
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000240 "Executing: program %s host %s, user %s, command %s\n",
241 ssh_program, host,
242 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
Damien Miller5428f641999-11-25 11:54:57 +1100244 /*
245 * Reserve two descriptors so that the real pipes won't get
246 * descriptors 0 and 1 because that will screw up dup2 below.
247 */
Damien Miller40b59852006-06-13 13:00:25 +1000248 if (pipe(reserved) < 0)
249 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller95def091999-11-25 00:26:21 +1100251 /* Create a socket pair for communicating with ssh. */
252 if (pipe(pin) < 0)
253 fatal("pipe: %s", strerror(errno));
254 if (pipe(pout) < 0)
255 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 /* Free the reserved descriptors. */
258 close(reserved[0]);
259 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller0979b402010-07-02 13:37:33 +1000261 signal(SIGTSTP, suspchild);
262 signal(SIGTTIN, suspchild);
263 signal(SIGTTOU, suspchild);
264
Damien Millerb2cdcb52003-06-18 20:26:34 +1000265 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000266 do_cmd_pid = fork();
267 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100268 /* Child. */
269 close(pin[1]);
270 close(pout[0]);
271 dup2(pin[0], 0);
272 dup2(pout[1], 1);
273 close(pin[0]);
274 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275
Damien Miller3eec6b72006-01-31 21:49:27 +1100276 replacearg(&args, 0, "%s", ssh_program);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100277 if (remuser != NULL) {
278 addargs(&args, "-l");
279 addargs(&args, "%s", remuser);
280 }
281 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000282 addargs(&args, "%s", host);
283 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100284
Damien Miller874d77b2000-10-14 16:23:11 +1100285 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000286 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100287 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000288 } else if (do_cmd_pid == -1) {
289 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290 }
Damien Miller95def091999-11-25 00:26:21 +1100291 /* Parent. Close the other side, and return the local side. */
292 close(pin[0]);
293 *fdout = pin[1];
294 close(pout[1]);
295 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000296 signal(SIGTERM, killchild);
297 signal(SIGINT, killchild);
298 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100299 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300}
301
Damien Millerf1211432011-01-06 22:40:30 +1100302/*
303 * This functions executes a command simlar to do_cmd(), but expects the
304 * input and output descriptors to be setup by a previous call to do_cmd().
305 * This way the input and output of two commands can be connected.
306 */
307int
308do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout)
309{
310 pid_t pid;
311 int status;
312
313 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000314 fmprintf(stderr,
Damien Millerf1211432011-01-06 22:40:30 +1100315 "Executing: 2nd program %s host %s, user %s, command %s\n",
316 ssh_program, host,
317 remuser ? remuser : "(unspecified)", cmd);
318
319 /* Fork a child to execute the command on the remote host using ssh. */
320 pid = fork();
321 if (pid == 0) {
322 dup2(fdin, 0);
323 dup2(fdout, 1);
324
325 replacearg(&args, 0, "%s", ssh_program);
326 if (remuser != NULL) {
327 addargs(&args, "-l");
328 addargs(&args, "%s", remuser);
329 }
330 addargs(&args, "--");
331 addargs(&args, "%s", host);
332 addargs(&args, "%s", cmd);
333
334 execvp(ssh_program, args.list);
335 perror(ssh_program);
336 exit(1);
337 } else if (pid == -1) {
338 fatal("fork: %s", strerror(errno));
339 }
340 while (waitpid(pid, &status, 0) == -1)
341 if (errno != EINTR)
342 fatal("do_cmd2: waitpid: %s", strerror(errno));
343 return 0;
344}
345
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000346typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000347 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348 char *buf;
349} BUF;
350
Damien Miller95def091999-11-25 00:26:21 +1100351BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100352void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100353int okname(char *);
354void run_err(const char *,...);
355void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100358uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359int errs, remin, remout;
360int pflag, iamremote, iamrecursive, targetshouldbedirectory;
361
362#define CMDNEEDS 64
363char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
364
Damien Miller95def091999-11-25 00:26:21 +1100365int response(void);
366void rsource(char *, struct stat *);
367void sink(int, char *[]);
368void source(int, char *[]);
369void tolocal(int, char *[]);
370void toremote(char *, int, char *[]);
371void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
373int
Damien Miller65d1f572003-06-04 22:51:08 +1000374main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000376 int ch, fflag, tflag, status, n;
Damien Miller65e42f82010-09-24 22:15:11 +1000377 char *targ, **newargv;
378 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379 extern char *optarg;
380 extern int optind;
381
Darren Tuckerce321d82005-10-03 18:11:24 +1000382 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
383 sanitise_stdfd();
384
Damien Millerdda78a02016-12-12 13:57:10 +1100385 msetlocale();
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000386
Damien Miller3d1a9f42006-07-10 22:19:53 +1000387 /* Copy argv, because we modify it */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000388 newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
Damien Miller3d1a9f42006-07-10 22:19:53 +1000389 for (n = 0; n < argc; n++)
390 newargv[n] = xstrdup(argv[n]);
391 argv = newargv;
392
Damien Miller59d3d5b2003-08-22 09:34:41 +1000393 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000394
Damien Miller3eec6b72006-01-31 21:49:27 +1100395 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100396 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
397 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100398 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000399 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100400 addargs(&args, "-oForwardAgent=no");
401 addargs(&args, "-oPermitLocalCommand=no");
402 addargs(&args, "-oClearAllForwardings=yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100403
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404 fflag = tflag = 0;
Damien Millerf1211432011-01-06 22:40:30 +1100405 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100406 switch (ch) {
407 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100408 case '1':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000409 fatal("SSH protocol v.1 is no longer supported");
410 break;
Damien Miller8e121472003-01-24 11:37:38 +1100411 case '2':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000412 /* Ignored */
413 break;
Damien Miller34132e52000-01-14 15:45:46 +1100414 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100415 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100416 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000417 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100418 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100419 break;
Damien Millerf1211432011-01-06 22:40:30 +1100420 case '3':
421 throughlocal = 1;
422 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100423 case 'o':
424 case 'c':
425 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000426 case 'F':
Damien Miller87dc0a42010-12-01 12:03:19 +1100427 addargs(&remote_remote_args, "-%c", ch);
428 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100429 addargs(&args, "-%c", ch);
430 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100431 break;
432 case 'P':
Damien Miller87dc0a42010-12-01 12:03:19 +1100433 addargs(&remote_remote_args, "-p");
434 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100435 addargs(&args, "-p");
436 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100437 break;
438 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100439 addargs(&remote_remote_args, "-oBatchmode=yes");
440 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100441 break;
Damien Millerff74d742003-01-24 11:36:58 +1100442 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000443 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
444 &errstr);
445 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100446 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000447 limit_kbps *= 1024; /* kbps */
448 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100449 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000450 case 'p':
451 pflag = 1;
452 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000453 case 'r':
454 iamrecursive = 1;
455 break;
Damien Millerad833b32000-08-23 10:46:23 +1000456 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100457 ssh_program = xstrdup(optarg);
458 break;
459 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000460 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100461 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100462 verbose_mode = 1;
463 break;
464 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100465 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100466 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100467 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000468 break;
469
Damien Miller95def091999-11-25 00:26:21 +1100470 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471 case 'd':
472 targetshouldbedirectory = 1;
473 break;
Damien Miller95def091999-11-25 00:26:21 +1100474 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 iamremote = 1;
476 fflag = 1;
477 break;
Damien Miller95def091999-11-25 00:26:21 +1100478 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479 iamremote = 1;
480 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000481#ifdef HAVE_CYGWIN
482 setmode(0, O_BINARY);
483#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 default:
486 usage();
487 }
488 argc -= optind;
489 argv += optind;
490
491 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000492 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100494 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495 showprogress = 0;
496
deraadt@openbsd.org9080bd02015-11-27 00:49:31 +0000497 if (pflag) {
498 /* Cannot pledge: -p allows setuid/setgid files... */
499 } else {
500 if (pledge("stdio rpath wpath cpath fattr tty proc exec",
501 NULL) == -1) {
502 perror("pledge");
503 exit(1);
504 }
505 }
506
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507 remin = STDIN_FILENO;
508 remout = STDOUT_FILENO;
509
Kevin Stevesef4eea92001-02-05 12:42:17 +0000510 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100511 /* Follow "protocol", send data. */
512 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513 source(argc, argv);
514 exit(errs != 0);
515 }
Damien Miller95def091999-11-25 00:26:21 +1100516 if (tflag) {
517 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518 sink(argc, argv);
519 exit(errs != 0);
520 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521 if (argc < 2)
522 usage();
523 if (argc > 2)
524 targetshouldbedirectory = 1;
525
526 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000527 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000529 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
530 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000531 iamrecursive ? " -r" : "", pflag ? " -p" : "",
532 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533
Damien Miller95def091999-11-25 00:26:21 +1100534 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535
536 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
537 toremote(targ, argc, argv);
538 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539 if (targetshouldbedirectory)
540 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100541 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000543 /*
544 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100545 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000546 */
547 if (do_cmd_pid != -1 && errs == 0) {
548 if (remin != -1)
549 (void) close(remin);
550 if (remout != -1)
551 (void) close(remout);
552 if (waitpid(do_cmd_pid, &status, 0) == -1)
553 errs = 1;
554 else {
555 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
556 errs = 1;
557 }
558 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 exit(errs != 0);
560}
561
Damien Miller65e42f82010-09-24 22:15:11 +1000562/* Callback from atomicio6 to update progress meter and limit bandwidth */
563static int
564scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000565{
Damien Miller65e42f82010-09-24 22:15:11 +1000566 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000567
Damien Miller65e42f82010-09-24 22:15:11 +1000568 *cnt += s;
569 if (limit_kbps > 0)
570 bandwidth_limit(&bwlimit, s);
571 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000572}
573
Damien Miller044bd2a2013-07-18 16:09:25 +1000574static int
575do_times(int fd, int verb, const struct stat *sb)
576{
577 /* strlen(2^64) == 20; strlen(10^6) == 7 */
578 char buf[(20 + 7 + 2) * 2 + 2];
579
580 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
581 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
582 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
583 if (verb) {
584 fprintf(stderr, "File mtime %lld atime %lld\n",
585 (long long)sb->st_mtime, (long long)sb->st_atime);
586 fprintf(stderr, "Sending file timestamps: %s", buf);
587 }
588 (void) atomicio(vwrite, fd, buf, strlen(buf));
589 return (response());
590}
591
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592void
Damien Miller65d1f572003-06-04 22:51:08 +1000593toremote(char *targ, int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594{
Damien Miller3dae15c2005-04-03 10:16:11 +1000595 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
Damien Miller3eec6b72006-01-31 21:49:27 +1100596 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000597 int i;
Damien Miller87dc0a42010-12-01 12:03:19 +1100598 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100599
600 memset(&alist, '\0', sizeof(alist));
601 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602
603 *targ++ = 0;
604 if (*targ == 0)
605 targ = ".";
606
Damien Miller4942de52005-04-03 10:16:39 +1000607 arg = xstrdup(argv[argc - 1]);
Damien Miller3dae15c2005-04-03 10:16:11 +1000608 if ((thost = strrchr(arg, '@'))) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609 /* user@host */
610 *thost++ = 0;
Damien Miller3dae15c2005-04-03 10:16:11 +1000611 tuser = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612 if (*tuser == '\0')
613 tuser = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000614 } else {
Damien Miller3dae15c2005-04-03 10:16:11 +1000615 thost = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000616 tuser = NULL;
617 }
618
Damien Miller3eec6b72006-01-31 21:49:27 +1100619 if (tuser != NULL && !okname(tuser)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000620 free(arg);
Damien Miller3eec6b72006-01-31 21:49:27 +1100621 return;
622 }
623
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624 for (i = 0; i < argc - 1; i++) {
625 src = colon(argv[i]);
Damien Millerf1211432011-01-06 22:40:30 +1100626 if (src && throughlocal) { /* extended remote to remote */
627 *src++ = 0;
628 if (*src == 0)
629 src = ".";
630 host = strrchr(argv[i], '@');
631 if (host) {
632 *host++ = 0;
633 host = cleanhostname(host);
634 suser = argv[i];
635 if (*suser == '\0')
636 suser = pwd->pw_name;
637 else if (!okname(suser))
638 continue;
639 } else {
640 host = cleanhostname(argv[i]);
641 suser = NULL;
642 }
Damien Miller0603d982011-09-22 21:38:00 +1000643 xasprintf(&bp, "%s -f %s%s", cmd,
644 *src == '-' ? "-- " : "", src);
Damien Millerf1211432011-01-06 22:40:30 +1100645 if (do_cmd(host, suser, bp, &remin, &remout) < 0)
646 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000647 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100648 host = cleanhostname(thost);
Damien Miller0603d982011-09-22 21:38:00 +1000649 xasprintf(&bp, "%s -t %s%s", cmd,
650 *targ == '-' ? "-- " : "", targ);
Damien Millerf1211432011-01-06 22:40:30 +1100651 if (do_cmd2(host, tuser, bp, remin, remout) < 0)
652 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000653 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100654 (void) close(remin);
655 (void) close(remout);
656 remin = remout = -1;
657 } else if (src) { /* standard remote to remote */
Damien Miller3eec6b72006-01-31 21:49:27 +1100658 freeargs(&alist);
659 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100660 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100661 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100662 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100663 for (j = 0; j < remote_remote_args.num; j++) {
664 addargs(&alist, "%s",
665 remote_remote_args.list[j]);
666 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000667 *src++ = 0;
668 if (*src == 0)
669 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000670 host = strrchr(argv[i], '@');
Damien Miller3eec6b72006-01-31 21:49:27 +1100671
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672 if (host) {
673 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100674 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675 suser = argv[i];
676 if (*suser == '\0')
677 suser = pwd->pw_name;
Damien Miller3eec6b72006-01-31 21:49:27 +1100678 else if (!okname(suser))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000679 continue;
Damien Miller3eec6b72006-01-31 21:49:27 +1100680 addargs(&alist, "-l");
681 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100682 } else {
683 host = cleanhostname(argv[i]);
Damien Miller34132e52000-01-14 15:45:46 +1100684 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100685 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100686 addargs(&alist, "%s", host);
687 addargs(&alist, "%s", cmd);
688 addargs(&alist, "%s", src);
689 addargs(&alist, "%s%s%s:%s",
690 tuser ? tuser : "", tuser ? "@" : "",
691 thost, targ);
692 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000693 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100694 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000696 xasprintf(&bp, "%s -t %s%s", cmd,
697 *targ == '-' ? "-- " : "", targ);
Damien Miller34132e52000-01-14 15:45:46 +1100698 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000699 if (do_cmd(host, tuser, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000700 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100701 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000702 if (response() < 0)
703 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000704 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000705 }
Damien Miller95def091999-11-25 00:26:21 +1100706 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707 }
708 }
Darren Tuckera627d422013-06-02 07:31:17 +1000709 free(arg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710}
711
712void
Damien Miller65d1f572003-06-04 22:51:08 +1000713tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714{
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715 char *bp, *host, *src, *suser;
Damien Miller3eec6b72006-01-31 21:49:27 +1100716 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000717 int i;
Damien Miller3eec6b72006-01-31 21:49:27 +1100718
719 memset(&alist, '\0', sizeof(alist));
720 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
722 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100723 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +1100724 freeargs(&alist);
725 addargs(&alist, "%s", _PATH_CP);
726 if (iamrecursive)
727 addargs(&alist, "-r");
728 if (pflag)
729 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100730 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100731 addargs(&alist, "%s", argv[i]);
732 addargs(&alist, "%s", argv[argc-1]);
733 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735 continue;
736 }
737 *src++ = 0;
738 if (*src == 0)
739 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000740 if ((host = strrchr(argv[i], '@')) == NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 host = argv[i];
742 suser = NULL;
743 } else {
744 *host++ = 0;
745 suser = argv[i];
746 if (*suser == '\0')
747 suser = pwd->pw_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000748 }
Damien Miller34132e52000-01-14 15:45:46 +1100749 host = cleanhostname(host);
Damien Miller0603d982011-09-22 21:38:00 +1000750 xasprintf(&bp, "%s -f %s%s",
751 cmd, *src == '-' ? "-- " : "", src);
Damien Miller07aa1322006-04-23 12:04:27 +1000752 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000753 free(bp);
Damien Miller95def091999-11-25 00:26:21 +1100754 ++errs;
755 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756 }
Darren Tuckera627d422013-06-02 07:31:17 +1000757 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000758 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100759 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000760 remin = remout = -1;
761 }
762}
763
764void
Damien Miller65d1f572003-06-04 22:51:08 +1000765source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766{
767 struct stat stb;
768 static BUF buffer;
769 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000770 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +1000771 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +1000772 int fd = -1, haderr, indx;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000773 char *last, *name, buf[2048], encname[PATH_MAX];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000774 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775
776 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100777 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000779 len = strlen(name);
780 while (len > 1 && name[len-1] == '/')
781 name[--len] = '\0';
Darren Tucker43ce9022007-06-12 23:41:06 +1000782 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +1000784 if (strchr(name, '\n') != NULL) {
785 strnvis(encname, name, sizeof(encname), VIS_NL);
786 name = encname;
787 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788 if (fstat(fd, &stb) < 0) {
789syserr: run_err("%s: %s", name, strerror(errno));
790 goto next;
791 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000792 if (stb.st_size < 0) {
793 run_err("%s: %s", name, "Negative file size");
794 goto next;
795 }
Darren Tucker43ce9022007-06-12 23:41:06 +1000796 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797 switch (stb.st_mode & S_IFMT) {
798 case S_IFREG:
799 break;
800 case S_IFDIR:
801 if (iamrecursive) {
802 rsource(name, &stb);
803 goto next;
804 }
805 /* FALLTHROUGH */
806 default:
807 run_err("%s: not a regular file", name);
808 goto next;
809 }
810 if ((last = strrchr(name, '/')) == NULL)
811 last = name;
812 else
813 ++last;
814 curfile = last;
815 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000816 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817 goto next;
818 }
819#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +0000820 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000821 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +1100822 (long long)stb.st_size, last);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000823 if (verbose_mode)
824 fmprintf(stderr, "Sending file modes: %s", buf);
Darren Tucker9f63f222003-07-03 13:46:56 +1000825 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826 if (response() < 0)
827 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +1000828 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +1100829next: if (fd != -1) {
830 (void) close(fd);
831 fd = -1;
832 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000833 continue;
834 }
Damien Miller62d57f62003-01-10 21:43:24 +1100835 if (showprogress)
836 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +1000837 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
839 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000840 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 amt = stb.st_size - i;
842 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +1000843 if ((nr = atomicio(read, fd,
844 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +1000845 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +1000846 memset(bp->buf + nr, 0, amt - nr);
847 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000849 /* Keep writing after error to retain sync */
850 if (haderr) {
851 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +1000852 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +1000853 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854 }
Damien Miller65e42f82010-09-24 22:15:11 +1000855 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +1000856 &statbytes) != amt)
857 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000859 unset_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860
Darren Tucker33f86bc2005-11-22 19:38:06 +1100861 if (fd != -1) {
862 if (close(fd) < 0 && !haderr)
863 haderr = errno;
864 fd = -1;
865 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +1000867 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868 else
869 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100870 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +0000871 if (showprogress)
872 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873 }
874}
875
876void
Damien Miller65d1f572003-06-04 22:51:08 +1000877rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878{
879 DIR *dirp;
880 struct dirent *dp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000881 char *last, *vect[1], path[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882
883 if (!(dirp = opendir(name))) {
884 run_err("%s: %s", name, strerror(errno));
885 return;
886 }
887 last = strrchr(name, '/');
mmcc@openbsd.orge92c4992015-10-16 17:07:24 +0000888 if (last == NULL)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 last = name;
890 else
891 last++;
892 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000893 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894 closedir(dirp);
895 return;
896 }
897 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000898 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000899 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100900 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000901 fmprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +1000902 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903 if (response() < 0) {
904 closedir(dirp);
905 return;
906 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000907 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908 if (dp->d_ino == 0)
909 continue;
910 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
911 continue;
912 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
913 run_err("%s/%s: name too long", name, dp->d_name);
914 continue;
915 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000916 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917 vect[0] = path;
918 source(1, vect);
919 }
Damien Miller95def091999-11-25 00:26:21 +1100920 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +1000921 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100922 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000923}
924
millert@openbsd.org06643712017-04-28 03:21:12 +0000925#define TYPE_OVERFLOW(type, val) \
926 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
927 (sizeof(type) == 8 && (val) > INT64_MAX) || \
928 (sizeof(type) != 4 && sizeof(type) != 8))
929
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930void
Damien Miller65d1f572003-06-04 22:51:08 +1000931sink(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932{
933 static BUF buffer;
934 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100935 enum {
936 YES, NO, DISPLAYED
937 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +1000939 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +1000940 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +1000941 int amt, exists, first, ofd;
942 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +1100943 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +1000944 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +1100945 int setimes, targisdir, wrerrno = 0;
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000946 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100947 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948
Ben Lindstromf719a202001-04-14 23:14:22 +0000949#define atime tv[0]
950#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +1100951#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
millert@openbsd.org06643712017-04-28 03:21:12 +0000953 if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
954 SCREWUP("Unexpected off_t/time_t size");
955
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956 setimes = targisdir = 0;
957 mask = umask(0);
958 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100959 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960 if (argc != 1) {
961 run_err("ambiguous target");
962 exit(1);
963 }
964 targ = *argv;
965 if (targetshouldbedirectory)
966 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100967
Darren Tucker9f63f222003-07-03 13:46:56 +1000968 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
970 targisdir = 1;
971 for (first = 1;; first = 0) {
972 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +1000973 if (atomicio(read, remin, cp, 1) != 1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974 return;
975 if (*cp++ == '\n')
976 SCREWUP("unexpected <newline>");
977 do {
Damien Millere247cc42000-05-07 12:03:14 +1000978 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979 SCREWUP("lost connection");
980 *cp++ = ch;
981 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
982 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +1000983 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000984 fmprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985
986 if (buf[0] == '\01' || buf[0] == '\02') {
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000987 if (iamremote == 0) {
988 (void) snmprintf(visbuf, sizeof(visbuf),
989 NULL, "%s", buf + 1);
Darren Tucker9f63f222003-07-03 13:46:56 +1000990 (void) atomicio(vwrite, STDERR_FILENO,
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000991 visbuf, strlen(visbuf));
992 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993 if (buf[0] == '\02')
994 exit(1);
995 ++errs;
996 continue;
997 }
998 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +1000999 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000 return;
1001 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002 if (ch == '\n')
1003 *--cp = 0;
1004
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005 cp = buf;
1006 if (*cp == 'T') {
1007 setimes++;
1008 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +10001009 if (!isdigit((unsigned char)*cp))
1010 SCREWUP("mtime.sec not present");
1011 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001012 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013 SCREWUP("mtime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001014 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001015 setimes = 0; /* out of range */
1016 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001017 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001018 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1019 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +10001021 if (!isdigit((unsigned char)*cp))
1022 SCREWUP("atime.sec not present");
1023 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001024 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001025 SCREWUP("atime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001026 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001027 setimes = 0; /* out of range */
1028 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001029 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001030 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1031 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001033 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001034 continue;
1035 }
1036 if (*cp != 'C' && *cp != 'D') {
1037 /*
1038 * Check for the case "rcp remote:foo\* local:bar".
1039 * In this case, the line "No match." can be returned
1040 * by the shell before the rcp command on the remote is
1041 * executed so the ^Aerror_message convention isn't
1042 * followed.
1043 */
1044 if (first) {
1045 run_err("%s", cp);
1046 exit(1);
1047 }
1048 SCREWUP("expected control record");
1049 }
1050 mode = 0;
1051 for (++cp; cp < buf + 5; cp++) {
1052 if (*cp < '0' || *cp > '7')
1053 SCREWUP("bad mode");
1054 mode = (mode << 3) | (*cp - '0');
1055 }
1056 if (*cp++ != ' ')
1057 SCREWUP("mode not delimited");
1058
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001059 if (!isdigit((unsigned char)*cp))
1060 SCREWUP("size not present");
1061 ull = strtoull(cp, &cp, 10);
1062 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063 SCREWUP("size not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001064 if (TYPE_OVERFLOW(off_t, ull))
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001065 SCREWUP("size out of range");
1066 size = (off_t)ull;
1067
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001068 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
1069 run_err("error: unexpected filename: %s", cp);
1070 exit(1);
1071 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072 if (targisdir) {
1073 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001074 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075 size_t need;
1076
1077 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001078 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001079 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001080 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001081 cursize = need;
1082 }
1083 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001084 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001085 np = namebuf;
1086 } else
1087 np = targ;
1088 curfile = cp;
1089 exists = stat(np, &stb) == 0;
1090 if (buf[0] == 'D') {
1091 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001092 if (!iamrecursive)
1093 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001094 if (exists) {
1095 if (!S_ISDIR(stb.st_mode)) {
1096 errno = ENOTDIR;
1097 goto bad;
1098 }
1099 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001100 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101 } else {
Damien Miller95def091999-11-25 00:26:21 +11001102 /* Handle copying from a read-only
1103 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104 mod_flag = 1;
1105 if (mkdir(np, mode | S_IRWXU) < 0)
1106 goto bad;
1107 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001108 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001109 sink(1, vect);
1110 if (setimes) {
1111 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +11001112 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001113 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001114 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115 }
1116 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001117 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001118 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119 continue;
1120 }
1121 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001122 mode |= S_IWUSR;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +00001123 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124bad: run_err("%s: %s", np, strerror(errno));
1125 continue;
1126 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001127 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001128 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001129 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130 continue;
1131 }
1132 cp = bp->buf;
1133 wrerr = NO;
1134
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001136 if (showprogress)
1137 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001138 set_nonblock(remin);
1139 for (count = i = 0; i < size; i += bp->cnt) {
1140 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141 if (i + amt > size)
1142 amt = size - i;
1143 count += amt;
1144 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001145 j = atomicio6(read, remin, cp, amt,
1146 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001147 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001148 run_err("%s", j != EPIPE ?
1149 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001150 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151 exit(1);
1152 }
1153 amt -= j;
1154 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001156
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 if (count == bp->cnt) {
1158 /* Keep reading so we stay sync'd up. */
1159 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001160 if (atomicio(vwrite, ofd, bp->buf,
1161 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001163 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 }
1165 }
1166 count = 0;
1167 cp = bp->buf;
1168 }
1169 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001170 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001172 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001173 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001174 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175 }
Darren Tucker067263e2007-06-25 18:32:33 +10001176 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1177 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178 run_err("%s: truncate: %s", np, strerror(errno));
1179 wrerr = DISPLAYED;
1180 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 if (pflag) {
1182 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001183#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001184 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001185#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001186 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001187#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001189 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001190 wrerr = DISPLAYED;
1191 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 } else {
1193 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001194#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001195 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001196#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001197 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001198#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001200 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001201 wrerr = DISPLAYED;
1202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203 }
Damien Millerbe484b52000-07-15 14:14:16 +10001204 if (close(ofd) == -1) {
1205 wrerr = YES;
1206 wrerrno = errno;
1207 }
Damien Miller95def091999-11-25 00:26:21 +11001208 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001209 if (showprogress)
1210 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 if (setimes && wrerr == NO) {
1212 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001213 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001215 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001216 wrerr = DISPLAYED;
1217 }
1218 }
Damien Miller95def091999-11-25 00:26:21 +11001219 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220 case YES:
1221 run_err("%s: %s", np, strerror(wrerrno));
1222 break;
1223 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001224 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225 break;
1226 case DISPLAYED:
1227 break;
1228 }
1229 }
1230screwup:
1231 run_err("protocol error: %s", why);
1232 exit(1);
1233}
1234
1235int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001236response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237{
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001238 char ch, *cp, resp, rbuf[2048], visbuf[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239
Damien Millere247cc42000-05-07 12:03:14 +10001240 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 lostconn(0);
1242
1243 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001244 switch (resp) {
1245 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246 return (0);
1247 default:
1248 *cp++ = resp;
1249 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001250 case 1: /* error, followed by error msg */
1251 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252 do {
Damien Millere247cc42000-05-07 12:03:14 +10001253 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254 lostconn(0);
1255 *cp++ = ch;
1256 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1257
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001258 if (!iamremote) {
1259 cp[-1] = '\0';
1260 (void) snmprintf(visbuf, sizeof(visbuf),
1261 NULL, "%s\n", rbuf);
1262 (void) atomicio(vwrite, STDERR_FILENO,
1263 visbuf, strlen(visbuf));
1264 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265 ++errs;
1266 if (resp == 1)
1267 return (-1);
1268 exit(1);
1269 }
1270 /* NOTREACHED */
1271}
1272
1273void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001274usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001276 (void) fprintf(stderr,
jmc@openbsd.org4f1ca822017-05-02 08:06:33 +00001277 "usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
Darren Tucker1f203942003-10-15 15:50:42 +10001278 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
Damien Miller647d97b2007-08-08 14:29:58 +10001279 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280 exit(1);
1281}
1282
1283void
Damien Miller95def091999-11-25 00:26:21 +11001284run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285{
1286 static FILE *fp;
1287 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001288
1289 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001290 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1291 (void) fprintf(fp, "%c", 0x01);
1292 (void) fprintf(fp, "scp: ");
1293 va_start(ap, fmt);
1294 (void) vfprintf(fp, fmt, ap);
1295 va_end(ap);
1296 (void) fprintf(fp, "\n");
1297 (void) fflush(fp);
1298 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299
Damien Miller95def091999-11-25 00:26:21 +11001300 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001301 va_start(ap, fmt);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001302 vfmprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001303 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001304 fprintf(stderr, "\n");
1305 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001306}
1307
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308void
Damien Miller65d1f572003-06-04 22:51:08 +10001309verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310{
1311 struct stat stb;
1312
1313 if (!stat(cp, &stb)) {
1314 if (S_ISDIR(stb.st_mode))
1315 return;
1316 errno = ENOTDIR;
1317 }
1318 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001319 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320}
1321
1322int
Damien Miller65d1f572003-06-04 22:51:08 +10001323okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001324{
1325 int c;
1326 char *cp;
1327
1328 cp = cp0;
1329 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001330 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331 if (c & 0200)
1332 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001333 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001334 switch (c) {
1335 case '\'':
1336 case '"':
1337 case '`':
1338 case ' ':
1339 case '#':
1340 goto bad;
1341 default:
1342 break;
1343 }
1344 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001345 } while (*++cp);
1346 return (1);
1347
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001348bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349 return (0);
1350}
1351
1352BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001353allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001354{
1355 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001356#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357 struct stat stb;
1358
1359 if (fstat(fd, &stb) < 0) {
1360 run_err("fstat: %s", strerror(errno));
1361 return (0);
1362 }
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001363 size = ROUNDUP(stb.st_blksize, blksize);
Ben Lindstrom418e0782002-12-23 02:22:09 +00001364 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001365 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001366#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001367 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001368#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001369 if (bp->cnt >= size)
1370 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001371 if (bp->buf == NULL)
1372 bp->buf = xmalloc(size);
1373 else
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001374 bp->buf = xreallocarray(bp->buf, 1, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001375 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376 bp->cnt = size;
1377 return (bp);
1378}
1379
1380void
Damien Miller65d1f572003-06-04 22:51:08 +10001381lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382{
1383 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001384 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001385 if (signo)
1386 _exit(1);
1387 else
1388 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389}