blob: 2103a54ea2c083235bb832808152ac1c7f50b554 [file] [log] [blame]
millert@openbsd.org887669e2017-10-21 23:06:24 +00001/* $OpenBSD: scp.c,v 1.193 2017/10/21 23:06:24 millert 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"
millert@openbsd.org887669e2017-10-21 23:06:24 +0000115#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000116#include "atomicio.h"
117#include "pathnames.h"
118#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +0000119#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +1100120#include "progressmeter.h"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000121#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000123extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000124
Damien Millerc77cadb2007-10-26 15:39:15 +1000125#define COPY_BUFLEN 16384
126
millert@openbsd.org887669e2017-10-21 23:06:24 +0000127int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
128int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000129
Ben Lindstrom387c4722001-05-08 20:27:25 +0000130/* Struct for addargs */
131arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100132arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100133
Damien Millerff74d742003-01-24 11:36:58 +1100134/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000135long long limit_kbps = 0;
136struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* Name of current file being transferred. */
139char *curfile;
140
141/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100142int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144/* This is set to zero if the progressmeter is not desired. */
145int showprogress = 1;
146
Damien Millerf1211432011-01-06 22:40:30 +1100147/*
148 * This is set to non-zero if remote-remote copy should be piped
149 * through this process.
150 */
151int throughlocal = 0;
152
millert@openbsd.org887669e2017-10-21 23:06:24 +0000153/* Non-standard port to use for the ssh connection or -1. */
154int sshport = -1;
155
Damien Millerad833b32000-08-23 10:46:23 +1000156/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100157char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000158
Ben Lindstrom007eb912002-11-09 15:54:08 +0000159/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000160pid_t do_cmd_pid = -1;
161
162static void
163killchild(int signo)
164{
Darren Tuckerba66df82005-01-24 21:57:40 +1100165 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000166 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100167 waitpid(do_cmd_pid, NULL, 0);
168 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000169
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000170 if (signo)
171 _exit(1);
172 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000173}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000174
Damien Miller0979b402010-07-02 13:37:33 +1000175static void
176suspchild(int signo)
177{
178 int status;
179
180 if (do_cmd_pid > 1) {
181 kill(do_cmd_pid, signo);
182 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
183 errno == EINTR)
184 ;
185 kill(getpid(), SIGSTOP);
186 }
187}
188
Damien Miller3eec6b72006-01-31 21:49:27 +1100189static int
190do_local_cmd(arglist *a)
191{
192 u_int i;
193 int status;
194 pid_t pid;
195
196 if (a->num == 0)
197 fatal("do_local_cmd: no arguments");
198
199 if (verbose_mode) {
200 fprintf(stderr, "Executing:");
201 for (i = 0; i < a->num; i++)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000202 fmprintf(stderr, " %s", a->list[i]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100203 fprintf(stderr, "\n");
204 }
205 if ((pid = fork()) == -1)
206 fatal("do_local_cmd: fork: %s", strerror(errno));
207
208 if (pid == 0) {
209 execvp(a->list[0], a->list);
210 perror(a->list[0]);
211 exit(1);
212 }
213
214 do_cmd_pid = pid;
215 signal(SIGTERM, killchild);
216 signal(SIGINT, killchild);
217 signal(SIGHUP, killchild);
218
219 while (waitpid(pid, &status, 0) == -1)
220 if (errno != EINTR)
221 fatal("do_local_cmd: waitpid: %s", strerror(errno));
222
223 do_cmd_pid = -1;
224
225 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
226 return (-1);
227
228 return (0);
229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
232 * This function executes the given command as the specified user on the
233 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
234 * assigns the input and output file descriptors on success.
235 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Miller4af51302000-04-16 11:18:38 +1000237int
millert@openbsd.org887669e2017-10-21 23:06:24 +0000238do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Miller95def091999-11-25 00:26:21 +1100240 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
Damien Miller95def091999-11-25 00:26:21 +1100242 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000243 fmprintf(stderr,
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000244 "Executing: program %s host %s, user %s, command %s\n",
245 ssh_program, host,
246 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
millert@openbsd.org887669e2017-10-21 23:06:24 +0000248 if (port == -1)
249 port = sshport;
250
Damien Miller5428f641999-11-25 11:54:57 +1100251 /*
252 * Reserve two descriptors so that the real pipes won't get
253 * descriptors 0 and 1 because that will screw up dup2 below.
254 */
Damien Miller40b59852006-06-13 13:00:25 +1000255 if (pipe(reserved) < 0)
256 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller95def091999-11-25 00:26:21 +1100258 /* Create a socket pair for communicating with ssh. */
259 if (pipe(pin) < 0)
260 fatal("pipe: %s", strerror(errno));
261 if (pipe(pout) < 0)
262 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Free the reserved descriptors. */
265 close(reserved[0]);
266 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller0979b402010-07-02 13:37:33 +1000268 signal(SIGTSTP, suspchild);
269 signal(SIGTTIN, suspchild);
270 signal(SIGTTOU, suspchild);
271
Damien Millerb2cdcb52003-06-18 20:26:34 +1000272 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000273 do_cmd_pid = fork();
274 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100275 /* Child. */
276 close(pin[1]);
277 close(pout[0]);
278 dup2(pin[0], 0);
279 dup2(pout[1], 1);
280 close(pin[0]);
281 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282
Damien Miller3eec6b72006-01-31 21:49:27 +1100283 replacearg(&args, 0, "%s", ssh_program);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000284 if (port != -1) {
285 addargs(&args, "-p");
286 addargs(&args, "%d", port);
287 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100288 if (remuser != NULL) {
289 addargs(&args, "-l");
290 addargs(&args, "%s", remuser);
291 }
292 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000293 addargs(&args, "%s", host);
294 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100295
Damien Miller874d77b2000-10-14 16:23:11 +1100296 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000297 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100298 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000299 } else if (do_cmd_pid == -1) {
300 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301 }
Damien Miller95def091999-11-25 00:26:21 +1100302 /* Parent. Close the other side, and return the local side. */
303 close(pin[0]);
304 *fdout = pin[1];
305 close(pout[1]);
306 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000307 signal(SIGTERM, killchild);
308 signal(SIGINT, killchild);
309 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100310 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
312
Damien Millerf1211432011-01-06 22:40:30 +1100313/*
314 * This functions executes a command simlar to do_cmd(), but expects the
315 * input and output descriptors to be setup by a previous call to do_cmd().
316 * This way the input and output of two commands can be connected.
317 */
318int
millert@openbsd.org887669e2017-10-21 23:06:24 +0000319do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
Damien Millerf1211432011-01-06 22:40:30 +1100320{
321 pid_t pid;
322 int status;
323
324 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000325 fmprintf(stderr,
Damien Millerf1211432011-01-06 22:40:30 +1100326 "Executing: 2nd program %s host %s, user %s, command %s\n",
327 ssh_program, host,
328 remuser ? remuser : "(unspecified)", cmd);
329
millert@openbsd.org887669e2017-10-21 23:06:24 +0000330 if (port == -1)
331 port = sshport;
332
Damien Millerf1211432011-01-06 22:40:30 +1100333 /* Fork a child to execute the command on the remote host using ssh. */
334 pid = fork();
335 if (pid == 0) {
336 dup2(fdin, 0);
337 dup2(fdout, 1);
338
339 replacearg(&args, 0, "%s", ssh_program);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000340 if (port != -1) {
341 addargs(&args, "-p");
342 addargs(&args, "%d", port);
343 }
Damien Millerf1211432011-01-06 22:40:30 +1100344 if (remuser != NULL) {
345 addargs(&args, "-l");
346 addargs(&args, "%s", remuser);
347 }
348 addargs(&args, "--");
349 addargs(&args, "%s", host);
350 addargs(&args, "%s", cmd);
351
352 execvp(ssh_program, args.list);
353 perror(ssh_program);
354 exit(1);
355 } else if (pid == -1) {
356 fatal("fork: %s", strerror(errno));
357 }
358 while (waitpid(pid, &status, 0) == -1)
359 if (errno != EINTR)
360 fatal("do_cmd2: waitpid: %s", strerror(errno));
361 return 0;
362}
363
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000365 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366 char *buf;
367} BUF;
368
Damien Miller95def091999-11-25 00:26:21 +1100369BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100370void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100371int okname(char *);
372void run_err(const char *,...);
373void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100376uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377int errs, remin, remout;
378int pflag, iamremote, iamrecursive, targetshouldbedirectory;
379
380#define CMDNEEDS 64
381char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
382
Damien Miller95def091999-11-25 00:26:21 +1100383int response(void);
384void rsource(char *, struct stat *);
385void sink(int, char *[]);
386void source(int, char *[]);
387void tolocal(int, char *[]);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000388void toremote(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100389void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390
391int
Damien Miller65d1f572003-06-04 22:51:08 +1000392main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000394 int ch, fflag, tflag, status, n;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000395 char **newargv;
Damien Miller65e42f82010-09-24 22:15:11 +1000396 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397 extern char *optarg;
398 extern int optind;
399
Darren Tuckerce321d82005-10-03 18:11:24 +1000400 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
401 sanitise_stdfd();
402
Damien Millerdda78a02016-12-12 13:57:10 +1100403 msetlocale();
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000404
Damien Miller3d1a9f42006-07-10 22:19:53 +1000405 /* Copy argv, because we modify it */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000406 newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
Damien Miller3d1a9f42006-07-10 22:19:53 +1000407 for (n = 0; n < argc; n++)
408 newargv[n] = xstrdup(argv[n]);
409 argv = newargv;
410
Damien Miller59d3d5b2003-08-22 09:34:41 +1000411 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000412
Damien Miller3eec6b72006-01-31 21:49:27 +1100413 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100414 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
415 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100416 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000417 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100418 addargs(&args, "-oForwardAgent=no");
419 addargs(&args, "-oPermitLocalCommand=no");
420 addargs(&args, "-oClearAllForwardings=yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100421
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 fflag = tflag = 0;
Damien Millerf1211432011-01-06 22:40:30 +1100423 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100424 switch (ch) {
425 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100426 case '1':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000427 fatal("SSH protocol v.1 is no longer supported");
428 break;
Damien Miller8e121472003-01-24 11:37:38 +1100429 case '2':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000430 /* Ignored */
431 break;
Damien Miller34132e52000-01-14 15:45:46 +1100432 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100433 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100434 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000435 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100436 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100437 break;
Damien Millerf1211432011-01-06 22:40:30 +1100438 case '3':
439 throughlocal = 1;
440 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100441 case 'o':
442 case 'c':
443 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000444 case 'F':
Damien Miller87dc0a42010-12-01 12:03:19 +1100445 addargs(&remote_remote_args, "-%c", ch);
446 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100447 addargs(&args, "-%c", ch);
448 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100449 break;
450 case 'P':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000451 sshport = a2port(optarg);
452 if (sshport <= 0)
453 fatal("bad port \"%s\"\n", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100454 break;
455 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100456 addargs(&remote_remote_args, "-oBatchmode=yes");
457 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100458 break;
Damien Millerff74d742003-01-24 11:36:58 +1100459 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000460 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
461 &errstr);
462 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100463 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000464 limit_kbps *= 1024; /* kbps */
465 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100466 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 case 'p':
468 pflag = 1;
469 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470 case 'r':
471 iamrecursive = 1;
472 break;
Damien Millerad833b32000-08-23 10:46:23 +1000473 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100474 ssh_program = xstrdup(optarg);
475 break;
476 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000477 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100478 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100479 verbose_mode = 1;
480 break;
481 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100482 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100483 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100484 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000485 break;
486
Damien Miller95def091999-11-25 00:26:21 +1100487 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 case 'd':
489 targetshouldbedirectory = 1;
490 break;
Damien Miller95def091999-11-25 00:26:21 +1100491 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 iamremote = 1;
493 fflag = 1;
494 break;
Damien Miller95def091999-11-25 00:26:21 +1100495 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000496 iamremote = 1;
497 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000498#ifdef HAVE_CYGWIN
499 setmode(0, O_BINARY);
500#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502 default:
503 usage();
504 }
505 argc -= optind;
506 argv += optind;
507
508 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000509 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100511 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512 showprogress = 0;
513
deraadt@openbsd.org9080bd02015-11-27 00:49:31 +0000514 if (pflag) {
515 /* Cannot pledge: -p allows setuid/setgid files... */
516 } else {
517 if (pledge("stdio rpath wpath cpath fattr tty proc exec",
518 NULL) == -1) {
519 perror("pledge");
520 exit(1);
521 }
522 }
523
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524 remin = STDIN_FILENO;
525 remout = STDOUT_FILENO;
526
Kevin Stevesef4eea92001-02-05 12:42:17 +0000527 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100528 /* Follow "protocol", send data. */
529 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000530 source(argc, argv);
531 exit(errs != 0);
532 }
Damien Miller95def091999-11-25 00:26:21 +1100533 if (tflag) {
534 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535 sink(argc, argv);
536 exit(errs != 0);
537 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538 if (argc < 2)
539 usage();
540 if (argc > 2)
541 targetshouldbedirectory = 1;
542
543 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000544 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000546 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
547 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000548 iamrecursive ? " -r" : "", pflag ? " -p" : "",
549 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000550
Damien Miller95def091999-11-25 00:26:21 +1100551 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552
millert@openbsd.org887669e2017-10-21 23:06:24 +0000553 if (colon(argv[argc - 1])) /* Dest is remote host. */
554 toremote(argc, argv);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000555 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 if (targetshouldbedirectory)
557 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100558 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000559 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000560 /*
561 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100562 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000563 */
564 if (do_cmd_pid != -1 && errs == 0) {
565 if (remin != -1)
566 (void) close(remin);
567 if (remout != -1)
568 (void) close(remout);
569 if (waitpid(do_cmd_pid, &status, 0) == -1)
570 errs = 1;
571 else {
572 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
573 errs = 1;
574 }
575 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000576 exit(errs != 0);
577}
578
Damien Miller65e42f82010-09-24 22:15:11 +1000579/* Callback from atomicio6 to update progress meter and limit bandwidth */
580static int
581scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000582{
Damien Miller65e42f82010-09-24 22:15:11 +1000583 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000584
Damien Miller65e42f82010-09-24 22:15:11 +1000585 *cnt += s;
586 if (limit_kbps > 0)
587 bandwidth_limit(&bwlimit, s);
588 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000589}
590
Damien Miller044bd2a2013-07-18 16:09:25 +1000591static int
592do_times(int fd, int verb, const struct stat *sb)
593{
594 /* strlen(2^64) == 20; strlen(10^6) == 7 */
595 char buf[(20 + 7 + 2) * 2 + 2];
596
597 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
598 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
599 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
600 if (verb) {
601 fprintf(stderr, "File mtime %lld atime %lld\n",
602 (long long)sb->st_mtime, (long long)sb->st_atime);
603 fprintf(stderr, "Sending file timestamps: %s", buf);
604 }
605 (void) atomicio(vwrite, fd, buf, strlen(buf));
606 return (response());
607}
608
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609void
millert@openbsd.org887669e2017-10-21 23:06:24 +0000610toremote(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000611{
millert@openbsd.org887669e2017-10-21 23:06:24 +0000612 char *suser = NULL, *host = NULL, *src = NULL;
613 char *bp, *tuser, *thost, *targ;
614 int sport = -1, tport = -1;
Damien Miller3eec6b72006-01-31 21:49:27 +1100615 arglist alist;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000616 int i, r;
Damien Miller87dc0a42010-12-01 12:03:19 +1100617 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100618
619 memset(&alist, '\0', sizeof(alist));
620 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000621
millert@openbsd.org887669e2017-10-21 23:06:24 +0000622 /* Parse target */
623 r = parse_uri("scp", argv[argc - 1], &tuser, &thost, &tport, &targ);
624 if (r == -1)
625 goto out; /* invalid URI */
626 if (r != 0) {
627 if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
628 &targ) == -1)
629 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000630 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000631 if (tuser != NULL && !okname(tuser))
632 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000633
millert@openbsd.org887669e2017-10-21 23:06:24 +0000634 /* Parse source files */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000635 for (i = 0; i < argc - 1; i++) {
millert@openbsd.org887669e2017-10-21 23:06:24 +0000636 free(suser);
637 free(host);
638 free(src);
639 r = parse_uri("scp", argv[i], &suser, &host, &sport, &src);
640 if (r == -1)
641 continue; /* invalid URI */
642 if (r != 0)
643 parse_user_host_path(argv[i], &suser, &host, &src);
644 if (suser != NULL && !okname(suser)) {
645 ++errs;
646 continue;
647 }
648 if (host && throughlocal) { /* extended remote to remote */
Damien Miller0603d982011-09-22 21:38:00 +1000649 xasprintf(&bp, "%s -f %s%s", cmd,
650 *src == '-' ? "-- " : "", src);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000651 if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
Damien Millerf1211432011-01-06 22:40:30 +1100652 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000653 free(bp);
Damien Miller0603d982011-09-22 21:38:00 +1000654 xasprintf(&bp, "%s -t %s%s", cmd,
655 *targ == '-' ? "-- " : "", targ);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000656 if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
Damien Millerf1211432011-01-06 22:40:30 +1100657 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000658 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100659 (void) close(remin);
660 (void) close(remout);
661 remin = remout = -1;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000662 } else if (host) { /* standard remote to remote */
663 if (tport != -1 && tport != SSH_DEFAULT_PORT) {
664 /* This would require the remote support URIs */
665 fatal("target port not supported with two "
666 "remote hosts without the -3 option");
667 }
668
Damien Miller3eec6b72006-01-31 21:49:27 +1100669 freeargs(&alist);
670 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100671 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100672 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100673 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100674 for (j = 0; j < remote_remote_args.num; j++) {
675 addargs(&alist, "%s",
676 remote_remote_args.list[j]);
677 }
Damien Miller3eec6b72006-01-31 21:49:27 +1100678
millert@openbsd.org887669e2017-10-21 23:06:24 +0000679 if (sport != -1) {
680 addargs(&alist, "-p");
681 addargs(&alist, "%d", sport);
682 }
683 if (suser) {
Damien Miller3eec6b72006-01-31 21:49:27 +1100684 addargs(&alist, "-l");
685 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100686 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100687 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100688 addargs(&alist, "%s", host);
689 addargs(&alist, "%s", cmd);
690 addargs(&alist, "%s", src);
691 addargs(&alist, "%s%s%s:%s",
692 tuser ? tuser : "", tuser ? "@" : "",
693 thost, targ);
694 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000695 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100696 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000697 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000698 xasprintf(&bp, "%s -t %s%s", cmd,
699 *targ == '-' ? "-- " : "", targ);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000700 if (do_cmd(thost, tuser, tport, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000701 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100702 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703 if (response() < 0)
704 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000705 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706 }
Damien Miller95def091999-11-25 00:26:21 +1100707 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708 }
709 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000710out:
711 free(tuser);
712 free(thost);
713 free(targ);
714 free(suser);
715 free(host);
716 free(src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000717}
718
719void
Damien Miller65d1f572003-06-04 22:51:08 +1000720tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721{
millert@openbsd.org887669e2017-10-21 23:06:24 +0000722 char *bp, *host = NULL, *src = NULL, *suser = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100723 arglist alist;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000724 int i, r, sport = -1;
Damien Miller3eec6b72006-01-31 21:49:27 +1100725
726 memset(&alist, '\0', sizeof(alist));
727 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728
729 for (i = 0; i < argc - 1; i++) {
millert@openbsd.org887669e2017-10-21 23:06:24 +0000730 free(suser);
731 free(host);
732 free(src);
733 r = parse_uri("scp", argv[i], &suser, &host, &sport, &src);
734 if (r == -1) {
735 ++errs;
736 continue;
737 }
738 if (r != 0)
739 parse_user_host_path(argv[i], &suser, &host, &src);
740 if (suser != NULL && !okname(suser)) {
741 ++errs;
742 continue;
743 }
744 if (!host) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +1100745 freeargs(&alist);
746 addargs(&alist, "%s", _PATH_CP);
747 if (iamrecursive)
748 addargs(&alist, "-r");
749 if (pflag)
750 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100751 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100752 addargs(&alist, "%s", argv[i]);
753 addargs(&alist, "%s", argv[argc-1]);
754 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756 continue;
757 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000758 /* Remote to local. */
Damien Miller0603d982011-09-22 21:38:00 +1000759 xasprintf(&bp, "%s -f %s%s",
760 cmd, *src == '-' ? "-- " : "", src);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000761 if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000762 free(bp);
Damien Miller95def091999-11-25 00:26:21 +1100763 ++errs;
764 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000765 }
Darren Tuckera627d422013-06-02 07:31:17 +1000766 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100768 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000769 remin = remout = -1;
770 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000771 free(suser);
772 free(host);
773 free(src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774}
775
776void
Damien Miller65d1f572003-06-04 22:51:08 +1000777source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000778{
779 struct stat stb;
780 static BUF buffer;
781 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000782 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +1000783 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +1000784 int fd = -1, haderr, indx;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000785 char *last, *name, buf[2048], encname[PATH_MAX];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000786 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787
788 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100789 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000791 len = strlen(name);
792 while (len > 1 && name[len-1] == '/')
793 name[--len] = '\0';
Darren Tucker43ce9022007-06-12 23:41:06 +1000794 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +1000796 if (strchr(name, '\n') != NULL) {
797 strnvis(encname, name, sizeof(encname), VIS_NL);
798 name = encname;
799 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800 if (fstat(fd, &stb) < 0) {
801syserr: run_err("%s: %s", name, strerror(errno));
802 goto next;
803 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000804 if (stb.st_size < 0) {
805 run_err("%s: %s", name, "Negative file size");
806 goto next;
807 }
Darren Tucker43ce9022007-06-12 23:41:06 +1000808 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809 switch (stb.st_mode & S_IFMT) {
810 case S_IFREG:
811 break;
812 case S_IFDIR:
813 if (iamrecursive) {
814 rsource(name, &stb);
815 goto next;
816 }
817 /* FALLTHROUGH */
818 default:
819 run_err("%s: not a regular file", name);
820 goto next;
821 }
822 if ((last = strrchr(name, '/')) == NULL)
823 last = name;
824 else
825 ++last;
826 curfile = last;
827 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000828 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829 goto next;
830 }
831#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +0000832 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000833 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +1100834 (long long)stb.st_size, last);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000835 if (verbose_mode)
836 fmprintf(stderr, "Sending file modes: %s", buf);
Darren Tucker9f63f222003-07-03 13:46:56 +1000837 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838 if (response() < 0)
839 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +1000840 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +1100841next: if (fd != -1) {
842 (void) close(fd);
843 fd = -1;
844 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845 continue;
846 }
Damien Miller62d57f62003-01-10 21:43:24 +1100847 if (showprogress)
848 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +1000849 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
851 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000852 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 amt = stb.st_size - i;
854 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +1000855 if ((nr = atomicio(read, fd,
856 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +1000857 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +1000858 memset(bp->buf + nr, 0, amt - nr);
859 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000861 /* Keep writing after error to retain sync */
862 if (haderr) {
863 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +1000864 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +1000865 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 }
Damien Miller65e42f82010-09-24 22:15:11 +1000867 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +1000868 &statbytes) != amt)
869 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000871 unset_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Darren Tucker33f86bc2005-11-22 19:38:06 +1100873 if (fd != -1) {
874 if (close(fd) < 0 && !haderr)
875 haderr = errno;
876 fd = -1;
877 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +1000879 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880 else
881 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100882 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +0000883 if (showprogress)
884 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885 }
886}
887
888void
Damien Miller65d1f572003-06-04 22:51:08 +1000889rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890{
891 DIR *dirp;
892 struct dirent *dp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000893 char *last, *vect[1], path[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
895 if (!(dirp = opendir(name))) {
896 run_err("%s: %s", name, strerror(errno));
897 return;
898 }
899 last = strrchr(name, '/');
mmcc@openbsd.orge92c4992015-10-16 17:07:24 +0000900 if (last == NULL)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 last = name;
902 else
903 last++;
904 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000905 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906 closedir(dirp);
907 return;
908 }
909 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000910 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000911 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100912 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000913 fmprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +1000914 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915 if (response() < 0) {
916 closedir(dirp);
917 return;
918 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000919 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920 if (dp->d_ino == 0)
921 continue;
922 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
923 continue;
924 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
925 run_err("%s/%s: name too long", name, dp->d_name);
926 continue;
927 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000928 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929 vect[0] = path;
930 source(1, vect);
931 }
Damien Miller95def091999-11-25 00:26:21 +1100932 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +1000933 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100934 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
millert@openbsd.org06643712017-04-28 03:21:12 +0000937#define TYPE_OVERFLOW(type, val) \
938 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
939 (sizeof(type) == 8 && (val) > INT64_MAX) || \
940 (sizeof(type) != 4 && sizeof(type) != 8))
941
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942void
Damien Miller65d1f572003-06-04 22:51:08 +1000943sink(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944{
945 static BUF buffer;
946 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100947 enum {
948 YES, NO, DISPLAYED
949 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +1000951 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +1000952 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +1000953 int amt, exists, first, ofd;
954 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +1100955 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +1000956 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +1100957 int setimes, targisdir, wrerrno = 0;
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000958 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100959 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960
Ben Lindstromf719a202001-04-14 23:14:22 +0000961#define atime tv[0]
962#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +1100963#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
millert@openbsd.org06643712017-04-28 03:21:12 +0000965 if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
966 SCREWUP("Unexpected off_t/time_t size");
967
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968 setimes = targisdir = 0;
969 mask = umask(0);
970 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100971 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972 if (argc != 1) {
973 run_err("ambiguous target");
974 exit(1);
975 }
976 targ = *argv;
977 if (targetshouldbedirectory)
978 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100979
Darren Tucker9f63f222003-07-03 13:46:56 +1000980 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
982 targisdir = 1;
983 for (first = 1;; first = 0) {
984 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +1000985 if (atomicio(read, remin, cp, 1) != 1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986 return;
987 if (*cp++ == '\n')
988 SCREWUP("unexpected <newline>");
989 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 SCREWUP("lost connection");
992 *cp++ = ch;
993 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
994 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +1000995 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000996 fmprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997
998 if (buf[0] == '\01' || buf[0] == '\02') {
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000999 if (iamremote == 0) {
1000 (void) snmprintf(visbuf, sizeof(visbuf),
1001 NULL, "%s", buf + 1);
Darren Tucker9f63f222003-07-03 13:46:56 +10001002 (void) atomicio(vwrite, STDERR_FILENO,
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001003 visbuf, strlen(visbuf));
1004 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005 if (buf[0] == '\02')
1006 exit(1);
1007 ++errs;
1008 continue;
1009 }
1010 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +10001011 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012 return;
1013 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014 if (ch == '\n')
1015 *--cp = 0;
1016
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017 cp = buf;
1018 if (*cp == 'T') {
1019 setimes++;
1020 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +10001021 if (!isdigit((unsigned char)*cp))
1022 SCREWUP("mtime.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("mtime.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 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001029 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001030 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1031 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +10001033 if (!isdigit((unsigned char)*cp))
1034 SCREWUP("atime.sec not present");
1035 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001036 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037 SCREWUP("atime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001038 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001039 setimes = 0; /* out of range */
1040 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001041 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001042 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1043 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001045 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046 continue;
1047 }
1048 if (*cp != 'C' && *cp != 'D') {
1049 /*
1050 * Check for the case "rcp remote:foo\* local:bar".
1051 * In this case, the line "No match." can be returned
1052 * by the shell before the rcp command on the remote is
1053 * executed so the ^Aerror_message convention isn't
1054 * followed.
1055 */
1056 if (first) {
1057 run_err("%s", cp);
1058 exit(1);
1059 }
1060 SCREWUP("expected control record");
1061 }
1062 mode = 0;
1063 for (++cp; cp < buf + 5; cp++) {
1064 if (*cp < '0' || *cp > '7')
1065 SCREWUP("bad mode");
1066 mode = (mode << 3) | (*cp - '0');
1067 }
1068 if (*cp++ != ' ')
1069 SCREWUP("mode not delimited");
1070
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001071 if (!isdigit((unsigned char)*cp))
1072 SCREWUP("size not present");
1073 ull = strtoull(cp, &cp, 10);
1074 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075 SCREWUP("size not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001076 if (TYPE_OVERFLOW(off_t, ull))
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001077 SCREWUP("size out of range");
1078 size = (off_t)ull;
1079
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001080 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
1081 run_err("error: unexpected filename: %s", cp);
1082 exit(1);
1083 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001084 if (targisdir) {
1085 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001086 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087 size_t need;
1088
1089 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001090 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001091 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001092 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001093 cursize = need;
1094 }
1095 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001096 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097 np = namebuf;
1098 } else
1099 np = targ;
1100 curfile = cp;
1101 exists = stat(np, &stb) == 0;
1102 if (buf[0] == 'D') {
1103 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001104 if (!iamrecursive)
1105 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106 if (exists) {
1107 if (!S_ISDIR(stb.st_mode)) {
1108 errno = ENOTDIR;
1109 goto bad;
1110 }
1111 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001112 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113 } else {
Damien Miller95def091999-11-25 00:26:21 +11001114 /* Handle copying from a read-only
1115 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116 mod_flag = 1;
1117 if (mkdir(np, mode | S_IRWXU) < 0)
1118 goto bad;
1119 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001120 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121 sink(1, vect);
1122 if (setimes) {
1123 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +11001124 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001125 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001126 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127 }
1128 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001129 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001130 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131 continue;
1132 }
1133 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001134 mode |= S_IWUSR;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +00001135 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136bad: run_err("%s: %s", np, strerror(errno));
1137 continue;
1138 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001139 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001140 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001141 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142 continue;
1143 }
1144 cp = bp->buf;
1145 wrerr = NO;
1146
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001148 if (showprogress)
1149 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001150 set_nonblock(remin);
1151 for (count = i = 0; i < size; i += bp->cnt) {
1152 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153 if (i + amt > size)
1154 amt = size - i;
1155 count += amt;
1156 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001157 j = atomicio6(read, remin, cp, amt,
1158 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001159 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001160 run_err("%s", j != EPIPE ?
1161 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001162 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163 exit(1);
1164 }
1165 amt -= j;
1166 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001168
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169 if (count == bp->cnt) {
1170 /* Keep reading so we stay sync'd up. */
1171 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001172 if (atomicio(vwrite, ofd, bp->buf,
1173 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001175 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176 }
1177 }
1178 count = 0;
1179 cp = bp->buf;
1180 }
1181 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001182 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001184 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001186 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187 }
Darren Tucker067263e2007-06-25 18:32:33 +10001188 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1189 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190 run_err("%s: truncate: %s", np, strerror(errno));
1191 wrerr = DISPLAYED;
1192 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 if (pflag) {
1194 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001195#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001196 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001197#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001198 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001199#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001201 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001202 wrerr = DISPLAYED;
1203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204 } else {
1205 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001206#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001207 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001208#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001209 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001210#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001211 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001212 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001213 wrerr = DISPLAYED;
1214 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215 }
Damien Millerbe484b52000-07-15 14:14:16 +10001216 if (close(ofd) == -1) {
1217 wrerr = YES;
1218 wrerrno = errno;
1219 }
Damien Miller95def091999-11-25 00:26:21 +11001220 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001221 if (showprogress)
1222 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223 if (setimes && wrerr == NO) {
1224 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001225 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001227 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228 wrerr = DISPLAYED;
1229 }
1230 }
Damien Miller95def091999-11-25 00:26:21 +11001231 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001232 case YES:
1233 run_err("%s: %s", np, strerror(wrerrno));
1234 break;
1235 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001236 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237 break;
1238 case DISPLAYED:
1239 break;
1240 }
1241 }
1242screwup:
1243 run_err("protocol error: %s", why);
1244 exit(1);
1245}
1246
1247int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001248response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249{
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001250 char ch, *cp, resp, rbuf[2048], visbuf[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251
Damien Millere247cc42000-05-07 12:03:14 +10001252 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253 lostconn(0);
1254
1255 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001256 switch (resp) {
1257 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258 return (0);
1259 default:
1260 *cp++ = resp;
1261 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001262 case 1: /* error, followed by error msg */
1263 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264 do {
Damien Millere247cc42000-05-07 12:03:14 +10001265 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266 lostconn(0);
1267 *cp++ = ch;
1268 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1269
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001270 if (!iamremote) {
1271 cp[-1] = '\0';
1272 (void) snmprintf(visbuf, sizeof(visbuf),
1273 NULL, "%s\n", rbuf);
1274 (void) atomicio(vwrite, STDERR_FILENO,
1275 visbuf, strlen(visbuf));
1276 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277 ++errs;
1278 if (resp == 1)
1279 return (-1);
1280 exit(1);
1281 }
1282 /* NOTREACHED */
1283}
1284
1285void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001286usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001288 (void) fprintf(stderr,
jmc@openbsd.org4f1ca822017-05-02 08:06:33 +00001289 "usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
millert@openbsd.org887669e2017-10-21 23:06:24 +00001290 " [-l limit] [-o ssh_option] [-P port] [-S program] source ... target\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291 exit(1);
1292}
1293
1294void
Damien Miller95def091999-11-25 00:26:21 +11001295run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296{
1297 static FILE *fp;
1298 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299
1300 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001301 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1302 (void) fprintf(fp, "%c", 0x01);
1303 (void) fprintf(fp, "scp: ");
1304 va_start(ap, fmt);
1305 (void) vfprintf(fp, fmt, ap);
1306 va_end(ap);
1307 (void) fprintf(fp, "\n");
1308 (void) fflush(fp);
1309 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310
Damien Miller95def091999-11-25 00:26:21 +11001311 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001312 va_start(ap, fmt);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001313 vfmprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001314 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001315 fprintf(stderr, "\n");
1316 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001317}
1318
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319void
Damien Miller65d1f572003-06-04 22:51:08 +10001320verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321{
1322 struct stat stb;
1323
1324 if (!stat(cp, &stb)) {
1325 if (S_ISDIR(stb.st_mode))
1326 return;
1327 errno = ENOTDIR;
1328 }
1329 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001330 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331}
1332
1333int
Damien Miller65d1f572003-06-04 22:51:08 +10001334okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335{
1336 int c;
1337 char *cp;
1338
1339 cp = cp0;
1340 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001341 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342 if (c & 0200)
1343 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001344 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001345 switch (c) {
1346 case '\'':
1347 case '"':
1348 case '`':
1349 case ' ':
1350 case '#':
1351 goto bad;
1352 default:
1353 break;
1354 }
1355 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356 } while (*++cp);
1357 return (1);
1358
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001359bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360 return (0);
1361}
1362
1363BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001364allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365{
1366 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001367#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368 struct stat stb;
1369
1370 if (fstat(fd, &stb) < 0) {
1371 run_err("fstat: %s", strerror(errno));
1372 return (0);
1373 }
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001374 size = ROUNDUP(stb.st_blksize, blksize);
Ben Lindstrom418e0782002-12-23 02:22:09 +00001375 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001376 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001377#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001378 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001379#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380 if (bp->cnt >= size)
1381 return (bp);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001382 bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383 bp->cnt = size;
1384 return (bp);
1385}
1386
1387void
Damien Miller65d1f572003-06-04 22:51:08 +10001388lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389{
1390 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001391 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001392 if (signo)
1393 _exit(1);
1394 else
1395 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396}