blob: 762286c73aaa896a2da3c88fbf7af0a204869611 [file] [log] [blame]
dtucker@openbsd.orgdf780112019-09-09 02:31:19 +00001/* $OpenBSD: scp.c,v 1.206 2019/09/09 02:31:19 dtucker 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>
Darren Tucker0e3c5bc2019-11-01 18:24:29 +110097#ifdef HAVE_FNMATCH_H
djm@openbsd.org391ffc42019-01-26 22:41:28 +000098#include <fnmatch.h>
Darren Tucker0e3c5bc2019-11-01 18:24:29 +110099#endif
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000100#include <limits.h>
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000101#include <locale.h>
Damien Miller9f2abc42006-07-10 20:53:08 +1000102#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +1100103#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +1000104#include <stdarg.h>
Darren Tucker329037e2017-05-01 13:19:41 +1000105#ifdef HAVE_STDINT_H
Damien Millercfc18972019-10-09 09:06:35 +1100106# include <stdint.h>
Darren Tucker329037e2017-05-01 13:19:41 +1000107#endif
Damien Millera7a73ee2006-08-05 11:37:59 +1000108#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +1000109#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +1000110#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +1000111#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +1000112#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +1100113#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Darren Tuckerbed63112007-06-13 00:02:07 +1000114#include <vis.h>
115#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117#include "xmalloc.h"
millert@openbsd.org887669e2017-10-21 23:06:24 +0000118#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000119#include "atomicio.h"
120#include "pathnames.h"
121#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +0000122#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +1100123#include "progressmeter.h"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000124#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000126extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000127
Damien Millerc77cadb2007-10-26 15:39:15 +1000128#define COPY_BUFLEN 16384
129
millert@openbsd.org887669e2017-10-21 23:06:24 +0000130int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
131int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000132
Ben Lindstrom387c4722001-05-08 20:27:25 +0000133/* Struct for addargs */
134arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100135arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100136
Damien Millerff74d742003-01-24 11:36:58 +1100137/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000138long long limit_kbps = 0;
139struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100140
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141/* Name of current file being transferred. */
142char *curfile;
143
144/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100145int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147/* This is set to zero if the progressmeter is not desired. */
148int showprogress = 1;
149
Damien Millerf1211432011-01-06 22:40:30 +1100150/*
151 * This is set to non-zero if remote-remote copy should be piped
152 * through this process.
153 */
154int throughlocal = 0;
155
millert@openbsd.org887669e2017-10-21 23:06:24 +0000156/* Non-standard port to use for the ssh connection or -1. */
157int sshport = -1;
158
Damien Millerad833b32000-08-23 10:46:23 +1000159/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100160char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000161
Ben Lindstrom007eb912002-11-09 15:54:08 +0000162/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000163pid_t do_cmd_pid = -1;
164
165static void
166killchild(int signo)
167{
Darren Tuckerba66df82005-01-24 21:57:40 +1100168 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000169 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100170 waitpid(do_cmd_pid, NULL, 0);
171 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000172
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000173 if (signo)
174 _exit(1);
175 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000176}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000177
Damien Miller0979b402010-07-02 13:37:33 +1000178static void
179suspchild(int signo)
180{
181 int status;
182
183 if (do_cmd_pid > 1) {
184 kill(do_cmd_pid, signo);
185 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
186 errno == EINTR)
187 ;
188 kill(getpid(), SIGSTOP);
189 }
190}
191
Damien Miller3eec6b72006-01-31 21:49:27 +1100192static int
193do_local_cmd(arglist *a)
194{
195 u_int i;
196 int status;
197 pid_t pid;
198
199 if (a->num == 0)
200 fatal("do_local_cmd: no arguments");
201
202 if (verbose_mode) {
203 fprintf(stderr, "Executing:");
204 for (i = 0; i < a->num; i++)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000205 fmprintf(stderr, " %s", a->list[i]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100206 fprintf(stderr, "\n");
207 }
208 if ((pid = fork()) == -1)
209 fatal("do_local_cmd: fork: %s", strerror(errno));
210
211 if (pid == 0) {
212 execvp(a->list[0], a->list);
213 perror(a->list[0]);
214 exit(1);
215 }
216
217 do_cmd_pid = pid;
218 signal(SIGTERM, killchild);
219 signal(SIGINT, killchild);
220 signal(SIGHUP, killchild);
221
222 while (waitpid(pid, &status, 0) == -1)
223 if (errno != EINTR)
224 fatal("do_local_cmd: waitpid: %s", strerror(errno));
225
226 do_cmd_pid = -1;
227
228 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
229 return (-1);
230
231 return (0);
232}
233
Damien Miller5428f641999-11-25 11:54:57 +1100234/*
235 * This function executes the given command as the specified user on the
236 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
237 * assigns the input and output file descriptors on success.
238 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller4af51302000-04-16 11:18:38 +1000240int
millert@openbsd.org887669e2017-10-21 23:06:24 +0000241do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242{
Damien Miller95def091999-11-25 00:26:21 +1100243 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller95def091999-11-25 00:26:21 +1100245 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000246 fmprintf(stderr,
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000247 "Executing: program %s host %s, user %s, command %s\n",
248 ssh_program, host,
249 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
millert@openbsd.org887669e2017-10-21 23:06:24 +0000251 if (port == -1)
252 port = sshport;
253
Damien Miller5428f641999-11-25 11:54:57 +1100254 /*
255 * Reserve two descriptors so that the real pipes won't get
256 * descriptors 0 and 1 because that will screw up dup2 below.
257 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000258 if (pipe(reserved) == -1)
Damien Miller40b59852006-06-13 13:00:25 +1000259 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller95def091999-11-25 00:26:21 +1100261 /* Create a socket pair for communicating with ssh. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000262 if (pipe(pin) == -1)
Damien Miller95def091999-11-25 00:26:21 +1100263 fatal("pipe: %s", strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000264 if (pipe(pout) == -1)
Damien Miller95def091999-11-25 00:26:21 +1100265 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Free the reserved descriptors. */
268 close(reserved[0]);
269 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller0979b402010-07-02 13:37:33 +1000271 signal(SIGTSTP, suspchild);
272 signal(SIGTTIN, suspchild);
273 signal(SIGTTOU, suspchild);
274
Damien Millerb2cdcb52003-06-18 20:26:34 +1000275 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000276 do_cmd_pid = fork();
277 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100278 /* Child. */
279 close(pin[1]);
280 close(pout[0]);
281 dup2(pin[0], 0);
282 dup2(pout[1], 1);
283 close(pin[0]);
284 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
Damien Miller3eec6b72006-01-31 21:49:27 +1100286 replacearg(&args, 0, "%s", ssh_program);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000287 if (port != -1) {
288 addargs(&args, "-p");
289 addargs(&args, "%d", port);
290 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100291 if (remuser != NULL) {
292 addargs(&args, "-l");
293 addargs(&args, "%s", remuser);
294 }
295 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000296 addargs(&args, "%s", host);
297 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100298
Damien Miller874d77b2000-10-14 16:23:11 +1100299 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000300 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100301 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000302 } else if (do_cmd_pid == -1) {
303 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304 }
Damien Miller95def091999-11-25 00:26:21 +1100305 /* Parent. Close the other side, and return the local side. */
306 close(pin[0]);
307 *fdout = pin[1];
308 close(pout[1]);
309 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000310 signal(SIGTERM, killchild);
311 signal(SIGINT, killchild);
312 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100313 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314}
315
Damien Millerf1211432011-01-06 22:40:30 +1100316/*
djm@openbsd.org001aa552018-04-10 00:10:49 +0000317 * This function executes a command similar to do_cmd(), but expects the
Damien Millerf1211432011-01-06 22:40:30 +1100318 * input and output descriptors to be setup by a previous call to do_cmd().
319 * This way the input and output of two commands can be connected.
320 */
321int
millert@openbsd.org887669e2017-10-21 23:06:24 +0000322do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
Damien Millerf1211432011-01-06 22:40:30 +1100323{
324 pid_t pid;
325 int status;
326
327 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000328 fmprintf(stderr,
Damien Millerf1211432011-01-06 22:40:30 +1100329 "Executing: 2nd program %s host %s, user %s, command %s\n",
330 ssh_program, host,
331 remuser ? remuser : "(unspecified)", cmd);
332
millert@openbsd.org887669e2017-10-21 23:06:24 +0000333 if (port == -1)
334 port = sshport;
335
Damien Millerf1211432011-01-06 22:40:30 +1100336 /* Fork a child to execute the command on the remote host using ssh. */
337 pid = fork();
338 if (pid == 0) {
339 dup2(fdin, 0);
340 dup2(fdout, 1);
341
342 replacearg(&args, 0, "%s", ssh_program);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000343 if (port != -1) {
344 addargs(&args, "-p");
345 addargs(&args, "%d", port);
346 }
Damien Millerf1211432011-01-06 22:40:30 +1100347 if (remuser != NULL) {
348 addargs(&args, "-l");
349 addargs(&args, "%s", remuser);
350 }
351 addargs(&args, "--");
352 addargs(&args, "%s", host);
353 addargs(&args, "%s", cmd);
354
355 execvp(ssh_program, args.list);
356 perror(ssh_program);
357 exit(1);
358 } else if (pid == -1) {
359 fatal("fork: %s", strerror(errno));
360 }
361 while (waitpid(pid, &status, 0) == -1)
362 if (errno != EINTR)
363 fatal("do_cmd2: waitpid: %s", strerror(errno));
364 return 0;
365}
366
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000368 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369 char *buf;
370} BUF;
371
Damien Miller95def091999-11-25 00:26:21 +1100372BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100373void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100374int okname(char *);
375void run_err(const char *,...);
376void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100379uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380int errs, remin, remout;
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000381int Tflag, pflag, iamremote, iamrecursive, targetshouldbedirectory;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382
383#define CMDNEEDS 64
384char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
385
Damien Miller95def091999-11-25 00:26:21 +1100386int response(void);
387void rsource(char *, struct stat *);
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000388void sink(int, char *[], const char *);
Damien Miller95def091999-11-25 00:26:21 +1100389void source(int, char *[]);
390void tolocal(int, char *[]);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000391void toremote(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100392void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393
394int
Damien Miller65d1f572003-06-04 22:51:08 +1000395main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000397 int ch, fflag, tflag, status, n;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000398 char **newargv;
Damien Miller65e42f82010-09-24 22:15:11 +1000399 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400 extern char *optarg;
401 extern int optind;
402
Darren Tuckerce321d82005-10-03 18:11:24 +1000403 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
404 sanitise_stdfd();
405
Damien Miller42c5ec42018-11-23 10:40:06 +1100406 seed_rng();
407
Damien Millerdda78a02016-12-12 13:57:10 +1100408 msetlocale();
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000409
Damien Miller3d1a9f42006-07-10 22:19:53 +1000410 /* Copy argv, because we modify it */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000411 newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
Damien Miller3d1a9f42006-07-10 22:19:53 +1000412 for (n = 0; n < argc; n++)
413 newargv[n] = xstrdup(argv[n]);
414 argv = newargv;
415
Damien Miller59d3d5b2003-08-22 09:34:41 +1000416 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000417
Damien Miller3eec6b72006-01-31 21:49:27 +1100418 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100419 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
420 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100421 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000422 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100423 addargs(&args, "-oForwardAgent=no");
424 addargs(&args, "-oPermitLocalCommand=no");
425 addargs(&args, "-oClearAllForwardings=yes");
djm@openbsd.org77e05392018-02-10 06:15:12 +0000426 addargs(&args, "-oRemoteCommand=none");
427 addargs(&args, "-oRequestTTY=no");
Damien Miller874d77b2000-10-14 16:23:11 +1100428
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000429 fflag = Tflag = tflag = 0;
430 while ((ch = getopt(argc, argv,
431 "dfl:prtTvBCc:i:P:q12346S:o:F:J:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100432 switch (ch) {
433 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100434 case '1':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000435 fatal("SSH protocol v.1 is no longer supported");
436 break;
Damien Miller8e121472003-01-24 11:37:38 +1100437 case '2':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000438 /* Ignored */
439 break;
Damien Miller34132e52000-01-14 15:45:46 +1100440 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100441 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100442 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000443 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100444 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100445 break;
Damien Millerf1211432011-01-06 22:40:30 +1100446 case '3':
447 throughlocal = 1;
448 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100449 case 'o':
450 case 'c':
451 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000452 case 'F':
tb@openbsd.org622dedf2019-01-21 22:50:42 +0000453 case 'J':
Damien Miller87dc0a42010-12-01 12:03:19 +1100454 addargs(&remote_remote_args, "-%c", ch);
455 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100456 addargs(&args, "-%c", ch);
457 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100458 break;
459 case 'P':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000460 sshport = a2port(optarg);
461 if (sshport <= 0)
462 fatal("bad port \"%s\"\n", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100463 break;
464 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100465 addargs(&remote_remote_args, "-oBatchmode=yes");
466 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100467 break;
Damien Millerff74d742003-01-24 11:36:58 +1100468 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000469 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
470 &errstr);
471 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100472 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000473 limit_kbps *= 1024; /* kbps */
474 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100475 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000476 case 'p':
477 pflag = 1;
478 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479 case 'r':
480 iamrecursive = 1;
481 break;
Damien Millerad833b32000-08-23 10:46:23 +1000482 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100483 ssh_program = xstrdup(optarg);
484 break;
485 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000486 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100487 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100488 verbose_mode = 1;
489 break;
490 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100491 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100492 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100493 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000494 break;
495
Damien Miller95def091999-11-25 00:26:21 +1100496 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497 case 'd':
498 targetshouldbedirectory = 1;
499 break;
Damien Miller95def091999-11-25 00:26:21 +1100500 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 iamremote = 1;
502 fflag = 1;
503 break;
Damien Miller95def091999-11-25 00:26:21 +1100504 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505 iamremote = 1;
506 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000507#ifdef HAVE_CYGWIN
508 setmode(0, O_BINARY);
509#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510 break;
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000511 case 'T':
512 Tflag = 1;
513 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514 default:
515 usage();
516 }
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000517 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518 argc -= optind;
519 argv += optind;
520
521 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000522 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100524 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525 showprogress = 0;
526
deraadt@openbsd.org9080bd02015-11-27 00:49:31 +0000527 if (pflag) {
528 /* Cannot pledge: -p allows setuid/setgid files... */
529 } else {
530 if (pledge("stdio rpath wpath cpath fattr tty proc exec",
531 NULL) == -1) {
532 perror("pledge");
533 exit(1);
534 }
535 }
536
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000537 remin = STDIN_FILENO;
538 remout = STDOUT_FILENO;
539
Kevin Stevesef4eea92001-02-05 12:42:17 +0000540 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100541 /* Follow "protocol", send data. */
542 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543 source(argc, argv);
544 exit(errs != 0);
545 }
Damien Miller95def091999-11-25 00:26:21 +1100546 if (tflag) {
547 /* Receive data. */
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000548 sink(argc, argv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549 exit(errs != 0);
550 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000551 if (argc < 2)
552 usage();
553 if (argc > 2)
554 targetshouldbedirectory = 1;
555
556 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000557 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000558 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000559 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
560 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000561 iamrecursive ? " -r" : "", pflag ? " -p" : "",
562 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
Damien Miller95def091999-11-25 00:26:21 +1100564 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000565
millert@openbsd.org887669e2017-10-21 23:06:24 +0000566 if (colon(argv[argc - 1])) /* Dest is remote host. */
567 toremote(argc, argv);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000568 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000569 if (targetshouldbedirectory)
570 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100571 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000573 /*
574 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100575 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000576 */
577 if (do_cmd_pid != -1 && errs == 0) {
578 if (remin != -1)
579 (void) close(remin);
580 if (remout != -1)
581 (void) close(remout);
582 if (waitpid(do_cmd_pid, &status, 0) == -1)
583 errs = 1;
584 else {
585 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
586 errs = 1;
587 }
588 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 exit(errs != 0);
590}
591
Damien Miller65e42f82010-09-24 22:15:11 +1000592/* Callback from atomicio6 to update progress meter and limit bandwidth */
593static int
594scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000595{
Damien Miller65e42f82010-09-24 22:15:11 +1000596 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000597
Damien Miller65e42f82010-09-24 22:15:11 +1000598 *cnt += s;
dtucker@openbsd.orgbdc6c632019-01-24 16:52:17 +0000599 refresh_progress_meter(0);
Damien Miller65e42f82010-09-24 22:15:11 +1000600 if (limit_kbps > 0)
601 bandwidth_limit(&bwlimit, s);
602 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000603}
604
Damien Miller044bd2a2013-07-18 16:09:25 +1000605static int
606do_times(int fd, int verb, const struct stat *sb)
607{
608 /* strlen(2^64) == 20; strlen(10^6) == 7 */
609 char buf[(20 + 7 + 2) * 2 + 2];
610
611 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
612 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
613 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
614 if (verb) {
615 fprintf(stderr, "File mtime %lld atime %lld\n",
616 (long long)sb->st_mtime, (long long)sb->st_atime);
617 fprintf(stderr, "Sending file timestamps: %s", buf);
618 }
619 (void) atomicio(vwrite, fd, buf, strlen(buf));
620 return (response());
621}
622
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000623static int
624parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
625 char **pathp)
626{
627 int r;
628
629 r = parse_uri("scp", uri, userp, hostp, portp, pathp);
630 if (r == 0 && *pathp == NULL)
631 *pathp = xstrdup(".");
632 return r;
633}
634
djm@openbsd.org3d896c12019-02-10 11:15:52 +0000635/* Appends a string to an array; returns 0 on success, -1 on alloc failure */
636static int
637append(char *cp, char ***ap, size_t *np)
638{
639 char **tmp;
640
641 if ((tmp = reallocarray(*ap, *np + 1, sizeof(*tmp))) == NULL)
642 return -1;
643 tmp[(*np)] = cp;
644 (*np)++;
645 *ap = tmp;
646 return 0;
647}
648
649/*
650 * Finds the start and end of the first brace pair in the pattern.
651 * returns 0 on success or -1 for invalid patterns.
652 */
653static int
654find_brace(const char *pattern, int *startp, int *endp)
655{
656 int i;
657 int in_bracket, brace_level;
658
659 *startp = *endp = -1;
660 in_bracket = brace_level = 0;
661 for (i = 0; i < INT_MAX && *endp < 0 && pattern[i] != '\0'; i++) {
662 switch (pattern[i]) {
663 case '\\':
664 /* skip next character */
665 if (pattern[i + 1] != '\0')
666 i++;
667 break;
668 case '[':
669 in_bracket = 1;
670 break;
671 case ']':
672 in_bracket = 0;
673 break;
674 case '{':
675 if (in_bracket)
676 break;
677 if (pattern[i + 1] == '}') {
678 /* Protect a single {}, for find(1), like csh */
679 i++; /* skip */
680 break;
681 }
682 if (*startp == -1)
683 *startp = i;
684 brace_level++;
685 break;
686 case '}':
687 if (in_bracket)
688 break;
689 if (*startp < 0) {
690 /* Unbalanced brace */
691 return -1;
692 }
693 if (--brace_level <= 0)
694 *endp = i;
695 break;
696 }
697 }
698 /* unbalanced brackets/braces */
699 if (*endp < 0 && (*startp >= 0 || in_bracket))
700 return -1;
701 return 0;
702}
703
704/*
705 * Assembles and records a successfully-expanded pattern, returns -1 on
706 * alloc failure.
707 */
708static int
709emit_expansion(const char *pattern, int brace_start, int brace_end,
710 int sel_start, int sel_end, char ***patternsp, size_t *npatternsp)
711{
712 char *cp;
713 int o = 0, tail_len = strlen(pattern + brace_end + 1);
714
715 if ((cp = malloc(brace_start + (sel_end - sel_start) +
716 tail_len + 1)) == NULL)
717 return -1;
718
719 /* Pattern before initial brace */
720 if (brace_start > 0) {
721 memcpy(cp, pattern, brace_start);
722 o = brace_start;
723 }
724 /* Current braced selection */
725 if (sel_end - sel_start > 0) {
726 memcpy(cp + o, pattern + sel_start,
727 sel_end - sel_start);
728 o += sel_end - sel_start;
729 }
730 /* Remainder of pattern after closing brace */
731 if (tail_len > 0) {
732 memcpy(cp + o, pattern + brace_end + 1, tail_len);
733 o += tail_len;
734 }
735 cp[o] = '\0';
736 if (append(cp, patternsp, npatternsp) != 0) {
737 free(cp);
738 return -1;
739 }
740 return 0;
741}
742
743/*
744 * Expand the first encountered brace in pattern, appending the expanded
745 * patterns it yielded to the *patternsp array.
746 *
747 * Returns 0 on success or -1 on allocation failure.
748 *
749 * Signals whether expansion was performed via *expanded and whether
750 * pattern was invalid via *invalid.
751 */
752static int
753brace_expand_one(const char *pattern, char ***patternsp, size_t *npatternsp,
754 int *expanded, int *invalid)
755{
756 int i;
757 int in_bracket, brace_start, brace_end, brace_level;
758 int sel_start, sel_end;
759
760 *invalid = *expanded = 0;
761
762 if (find_brace(pattern, &brace_start, &brace_end) != 0) {
763 *invalid = 1;
764 return 0;
765 } else if (brace_start == -1)
766 return 0;
767
768 in_bracket = brace_level = 0;
769 for (i = sel_start = brace_start + 1; i < brace_end; i++) {
770 switch (pattern[i]) {
771 case '{':
772 if (in_bracket)
773 break;
774 brace_level++;
775 break;
776 case '}':
777 if (in_bracket)
778 break;
779 brace_level--;
780 break;
781 case '[':
782 in_bracket = 1;
783 break;
784 case ']':
785 in_bracket = 0;
786 break;
787 case '\\':
788 if (i < brace_end - 1)
789 i++; /* skip */
790 break;
791 }
792 if (pattern[i] == ',' || i == brace_end - 1) {
793 if (in_bracket || brace_level > 0)
794 continue;
795 /* End of a selection, emit an expanded pattern */
796
797 /* Adjust end index for last selection */
798 sel_end = (i == brace_end - 1) ? brace_end : i;
799 if (emit_expansion(pattern, brace_start, brace_end,
800 sel_start, sel_end, patternsp, npatternsp) != 0)
801 return -1;
802 /* move on to the next selection */
803 sel_start = i + 1;
804 continue;
805 }
806 }
807 if (in_bracket || brace_level > 0) {
808 *invalid = 1;
809 return 0;
810 }
811 /* success */
812 *expanded = 1;
813 return 0;
814}
815
816/* Expand braces from pattern. Returns 0 on success, -1 on failure */
817static int
818brace_expand(const char *pattern, char ***patternsp, size_t *npatternsp)
819{
820 char *cp, *cp2, **active = NULL, **done = NULL;
821 size_t i, nactive = 0, ndone = 0;
822 int ret = -1, invalid = 0, expanded = 0;
823
824 *patternsp = NULL;
825 *npatternsp = 0;
826
827 /* Start the worklist with the original pattern */
828 if ((cp = strdup(pattern)) == NULL)
829 return -1;
830 if (append(cp, &active, &nactive) != 0) {
831 free(cp);
832 return -1;
833 }
834 while (nactive > 0) {
835 cp = active[nactive - 1];
836 nactive--;
837 if (brace_expand_one(cp, &active, &nactive,
838 &expanded, &invalid) == -1) {
839 free(cp);
840 goto fail;
841 }
842 if (invalid)
843 fatal("%s: invalid brace pattern \"%s\"", __func__, cp);
844 if (expanded) {
845 /*
846 * Current entry expanded to new entries on the
847 * active list; discard the progenitor pattern.
848 */
849 free(cp);
850 continue;
851 }
852 /*
853 * Pattern did not expand; append the finename component to
854 * the completed list
855 */
856 if ((cp2 = strrchr(cp, '/')) != NULL)
857 *cp2++ = '\0';
858 else
859 cp2 = cp;
860 if (append(xstrdup(cp2), &done, &ndone) != 0) {
861 free(cp);
862 goto fail;
863 }
864 free(cp);
865 }
866 /* success */
867 *patternsp = done;
868 *npatternsp = ndone;
869 done = NULL;
870 ndone = 0;
871 ret = 0;
872 fail:
873 for (i = 0; i < nactive; i++)
874 free(active[i]);
875 free(active);
876 for (i = 0; i < ndone; i++)
877 free(done[i]);
878 free(done);
879 return ret;
880}
881
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882void
millert@openbsd.org887669e2017-10-21 23:06:24 +0000883toremote(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884{
millert@openbsd.org887669e2017-10-21 23:06:24 +0000885 char *suser = NULL, *host = NULL, *src = NULL;
886 char *bp, *tuser, *thost, *targ;
887 int sport = -1, tport = -1;
Damien Miller3eec6b72006-01-31 21:49:27 +1100888 arglist alist;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000889 int i, r;
Damien Miller87dc0a42010-12-01 12:03:19 +1100890 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100891
892 memset(&alist, '\0', sizeof(alist));
893 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894
millert@openbsd.org887669e2017-10-21 23:06:24 +0000895 /* Parse target */
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000896 r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ);
897 if (r == -1) {
898 fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]);
899 ++errs;
900 goto out;
901 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000902 if (r != 0) {
903 if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000904 &targ) == -1) {
905 fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
906 ++errs;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000907 goto out;
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000908 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909 }
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000910 if (tuser != NULL && !okname(tuser)) {
911 ++errs;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000912 goto out;
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000913 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
millert@openbsd.org887669e2017-10-21 23:06:24 +0000915 /* Parse source files */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916 for (i = 0; i < argc - 1; i++) {
millert@openbsd.org887669e2017-10-21 23:06:24 +0000917 free(suser);
918 free(host);
919 free(src);
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000920 r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
921 if (r == -1) {
922 fmprintf(stderr, "%s: invalid uri\n", argv[i]);
923 ++errs;
924 continue;
925 }
926 if (r != 0) {
millert@openbsd.org887669e2017-10-21 23:06:24 +0000927 parse_user_host_path(argv[i], &suser, &host, &src);
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000928 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000929 if (suser != NULL && !okname(suser)) {
930 ++errs;
931 continue;
932 }
933 if (host && throughlocal) { /* extended remote to remote */
Damien Miller0603d982011-09-22 21:38:00 +1000934 xasprintf(&bp, "%s -f %s%s", cmd,
935 *src == '-' ? "-- " : "", src);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000936 if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
Damien Millerf1211432011-01-06 22:40:30 +1100937 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000938 free(bp);
Damien Miller0603d982011-09-22 21:38:00 +1000939 xasprintf(&bp, "%s -t %s%s", cmd,
940 *targ == '-' ? "-- " : "", targ);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000941 if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
Damien Millerf1211432011-01-06 22:40:30 +1100942 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000943 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100944 (void) close(remin);
945 (void) close(remout);
946 remin = remout = -1;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000947 } else if (host) { /* standard remote to remote */
948 if (tport != -1 && tport != SSH_DEFAULT_PORT) {
949 /* This would require the remote support URIs */
950 fatal("target port not supported with two "
951 "remote hosts without the -3 option");
952 }
953
Damien Miller3eec6b72006-01-31 21:49:27 +1100954 freeargs(&alist);
955 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100956 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100957 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100958 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100959 for (j = 0; j < remote_remote_args.num; j++) {
960 addargs(&alist, "%s",
961 remote_remote_args.list[j]);
962 }
Damien Miller3eec6b72006-01-31 21:49:27 +1100963
millert@openbsd.org887669e2017-10-21 23:06:24 +0000964 if (sport != -1) {
965 addargs(&alist, "-p");
966 addargs(&alist, "%d", sport);
967 }
968 if (suser) {
Damien Miller3eec6b72006-01-31 21:49:27 +1100969 addargs(&alist, "-l");
970 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100971 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100972 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100973 addargs(&alist, "%s", host);
974 addargs(&alist, "%s", cmd);
975 addargs(&alist, "%s", src);
976 addargs(&alist, "%s%s%s:%s",
977 tuser ? tuser : "", tuser ? "@" : "",
978 thost, targ);
979 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000980 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100981 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000983 xasprintf(&bp, "%s -t %s%s", cmd,
984 *targ == '-' ? "-- " : "", targ);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000985 if (do_cmd(thost, tuser, tport, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000986 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100987 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988 if (response() < 0)
989 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000990 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991 }
Damien Miller95def091999-11-25 00:26:21 +1100992 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993 }
994 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000995out:
996 free(tuser);
997 free(thost);
998 free(targ);
999 free(suser);
1000 free(host);
1001 free(src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002}
1003
1004void
Damien Miller65d1f572003-06-04 22:51:08 +10001005tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006{
millert@openbsd.org887669e2017-10-21 23:06:24 +00001007 char *bp, *host = NULL, *src = NULL, *suser = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +11001008 arglist alist;
millert@openbsd.org887669e2017-10-21 23:06:24 +00001009 int i, r, sport = -1;
Damien Miller3eec6b72006-01-31 21:49:27 +11001010
1011 memset(&alist, '\0', sizeof(alist));
1012 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013
1014 for (i = 0; i < argc - 1; i++) {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001015 free(suser);
1016 free(host);
1017 free(src);
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +00001018 r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
millert@openbsd.org887669e2017-10-21 23:06:24 +00001019 if (r == -1) {
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +00001020 fmprintf(stderr, "%s: invalid uri\n", argv[i]);
millert@openbsd.org887669e2017-10-21 23:06:24 +00001021 ++errs;
1022 continue;
1023 }
1024 if (r != 0)
1025 parse_user_host_path(argv[i], &suser, &host, &src);
1026 if (suser != NULL && !okname(suser)) {
1027 ++errs;
1028 continue;
1029 }
1030 if (!host) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +11001031 freeargs(&alist);
1032 addargs(&alist, "%s", _PATH_CP);
1033 if (iamrecursive)
1034 addargs(&alist, "-r");
1035 if (pflag)
1036 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +11001037 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +11001038 addargs(&alist, "%s", argv[i]);
1039 addargs(&alist, "%s", argv[argc-1]);
1040 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042 continue;
1043 }
millert@openbsd.org887669e2017-10-21 23:06:24 +00001044 /* Remote to local. */
Damien Miller0603d982011-09-22 21:38:00 +10001045 xasprintf(&bp, "%s -f %s%s",
1046 cmd, *src == '-' ? "-- " : "", src);
millert@openbsd.org887669e2017-10-21 23:06:24 +00001047 if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001048 free(bp);
Damien Miller95def091999-11-25 00:26:21 +11001049 ++errs;
1050 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051 }
Darren Tuckera627d422013-06-02 07:31:17 +10001052 free(bp);
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001053 sink(1, argv + argc - 1, src);
Damien Miller95def091999-11-25 00:26:21 +11001054 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055 remin = remout = -1;
1056 }
millert@openbsd.org887669e2017-10-21 23:06:24 +00001057 free(suser);
1058 free(host);
1059 free(src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060}
1061
1062void
Damien Miller65d1f572003-06-04 22:51:08 +10001063source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064{
1065 struct stat stb;
1066 static BUF buffer;
1067 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +10001068 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +10001069 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +10001070 int fd = -1, haderr, indx;
dtucker@openbsd.orgdf780112019-09-09 02:31:19 +00001071 char *last, *name, buf[PATH_MAX + 128], encname[PATH_MAX];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +00001072 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073
1074 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +11001075 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +00001077 len = strlen(name);
1078 while (len > 1 && name[len-1] == '/')
1079 name[--len] = '\0';
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001080 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) == -1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +10001082 if (strchr(name, '\n') != NULL) {
1083 strnvis(encname, name, sizeof(encname), VIS_NL);
1084 name = encname;
1085 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001086 if (fstat(fd, &stb) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087syserr: run_err("%s: %s", name, strerror(errno));
1088 goto next;
1089 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +10001090 if (stb.st_size < 0) {
1091 run_err("%s: %s", name, "Negative file size");
1092 goto next;
1093 }
Darren Tucker43ce9022007-06-12 23:41:06 +10001094 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095 switch (stb.st_mode & S_IFMT) {
1096 case S_IFREG:
1097 break;
1098 case S_IFDIR:
1099 if (iamrecursive) {
1100 rsource(name, &stb);
1101 goto next;
1102 }
1103 /* FALLTHROUGH */
1104 default:
1105 run_err("%s: not a regular file", name);
1106 goto next;
1107 }
1108 if ((last = strrchr(name, '/')) == NULL)
1109 last = name;
1110 else
1111 ++last;
1112 curfile = last;
1113 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +10001114 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001115 goto next;
1116 }
1117#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +00001118 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +00001119 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +11001120 (long long)stb.st_size, last);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001121 if (verbose_mode)
1122 fmprintf(stderr, "Sending file modes: %s", buf);
Darren Tucker9f63f222003-07-03 13:46:56 +10001123 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124 if (response() < 0)
1125 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +10001126 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +11001127next: if (fd != -1) {
1128 (void) close(fd);
1129 fd = -1;
1130 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131 continue;
1132 }
Damien Miller62d57f62003-01-10 21:43:24 +11001133 if (showprogress)
1134 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001135 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
1137 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +10001138 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139 amt = stb.st_size - i;
1140 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +10001141 if ((nr = atomicio(read, fd,
1142 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +10001143 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +10001144 memset(bp->buf + nr, 0, amt - nr);
1145 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001147 /* Keep writing after error to retain sync */
1148 if (haderr) {
1149 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +10001150 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +10001151 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001152 }
Damien Miller65e42f82010-09-24 22:15:11 +10001153 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +10001154 &statbytes) != amt)
1155 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001157 unset_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158
Darren Tucker33f86bc2005-11-22 19:38:06 +11001159 if (fd != -1) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001160 if (close(fd) == -1 && !haderr)
Darren Tucker33f86bc2005-11-22 19:38:06 +11001161 haderr = errno;
1162 fd = -1;
1163 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +10001165 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166 else
1167 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +11001168 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001169 if (showprogress)
1170 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 }
1172}
1173
1174void
Damien Miller65d1f572003-06-04 22:51:08 +10001175rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176{
1177 DIR *dirp;
1178 struct dirent *dp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001179 char *last, *vect[1], path[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180
1181 if (!(dirp = opendir(name))) {
1182 run_err("%s: %s", name, strerror(errno));
1183 return;
1184 }
1185 last = strrchr(name, '/');
mmcc@openbsd.orge92c4992015-10-16 17:07:24 +00001186 if (last == NULL)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187 last = name;
1188 else
1189 last++;
1190 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +10001191 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 closedir(dirp);
1193 return;
1194 }
1195 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001196 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +00001197 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +11001198 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001199 fmprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +10001200 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201 if (response() < 0) {
1202 closedir(dirp);
1203 return;
1204 }
Ben Lindstrombd472262001-03-29 00:39:55 +00001205 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206 if (dp->d_ino == 0)
1207 continue;
1208 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1209 continue;
1210 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
1211 run_err("%s/%s: name too long", name, dp->d_name);
1212 continue;
1213 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001214 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001215 vect[0] = path;
1216 source(1, vect);
1217 }
Damien Miller95def091999-11-25 00:26:21 +11001218 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +10001219 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +11001220 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221}
1222
millert@openbsd.org06643712017-04-28 03:21:12 +00001223#define TYPE_OVERFLOW(type, val) \
1224 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
1225 (sizeof(type) == 8 && (val) > INT64_MAX) || \
1226 (sizeof(type) != 4 && sizeof(type) != 8))
1227
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228void
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001229sink(int argc, char **argv, const char *src)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230{
1231 static BUF buffer;
1232 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +11001233 enum {
1234 YES, NO, DISPLAYED
1235 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +10001237 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +10001238 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +10001239 int amt, exists, first, ofd;
1240 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +11001241 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +10001242 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +11001243 int setimes, targisdir, wrerrno = 0;
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001244 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001245 char **patterns = NULL;
1246 size_t n, npatterns = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001247 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001248
Ben Lindstromf719a202001-04-14 23:14:22 +00001249#define atime tv[0]
1250#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +11001251#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252
millert@openbsd.org06643712017-04-28 03:21:12 +00001253 if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
1254 SCREWUP("Unexpected off_t/time_t size");
1255
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256 setimes = targisdir = 0;
1257 mask = umask(0);
1258 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +11001259 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260 if (argc != 1) {
1261 run_err("ambiguous target");
1262 exit(1);
1263 }
1264 targ = *argv;
1265 if (targetshouldbedirectory)
1266 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +11001267
Darren Tucker9f63f222003-07-03 13:46:56 +10001268 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
1270 targisdir = 1;
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001271 if (src != NULL && !iamrecursive && !Tflag) {
1272 /*
1273 * Prepare to try to restrict incoming filenames to match
1274 * the requested destination file glob.
1275 */
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001276 if (brace_expand(src, &patterns, &npatterns) != 0)
1277 fatal("%s: could not expand pattern", __func__);
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001278 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001279 for (first = 1;; first = 0) {
1280 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +10001281 if (atomicio(read, remin, cp, 1) != 1)
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001282 goto done;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001283 if (*cp++ == '\n')
1284 SCREWUP("unexpected <newline>");
1285 do {
Damien Millere247cc42000-05-07 12:03:14 +10001286 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287 SCREWUP("lost connection");
1288 *cp++ = ch;
1289 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
1290 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001291 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001292 fmprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
1294 if (buf[0] == '\01' || buf[0] == '\02') {
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001295 if (iamremote == 0) {
1296 (void) snmprintf(visbuf, sizeof(visbuf),
1297 NULL, "%s", buf + 1);
Darren Tucker9f63f222003-07-03 13:46:56 +10001298 (void) atomicio(vwrite, STDERR_FILENO,
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001299 visbuf, strlen(visbuf));
1300 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301 if (buf[0] == '\02')
1302 exit(1);
1303 ++errs;
1304 continue;
1305 }
1306 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +10001307 (void) atomicio(vwrite, remout, "", 1);
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001308 goto done;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310 if (ch == '\n')
1311 *--cp = 0;
1312
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313 cp = buf;
1314 if (*cp == 'T') {
1315 setimes++;
1316 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +10001317 if (!isdigit((unsigned char)*cp))
1318 SCREWUP("mtime.sec not present");
1319 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001320 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321 SCREWUP("mtime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001322 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001323 setimes = 0; /* out of range */
1324 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001325 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001326 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1327 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001328 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +10001329 if (!isdigit((unsigned char)*cp))
1330 SCREWUP("atime.sec not present");
1331 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001332 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333 SCREWUP("atime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001334 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001335 setimes = 0; /* out of range */
1336 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001337 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001338 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1339 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001341 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342 continue;
1343 }
1344 if (*cp != 'C' && *cp != 'D') {
1345 /*
1346 * Check for the case "rcp remote:foo\* local:bar".
1347 * In this case, the line "No match." can be returned
1348 * by the shell before the rcp command on the remote is
1349 * executed so the ^Aerror_message convention isn't
1350 * followed.
1351 */
1352 if (first) {
1353 run_err("%s", cp);
1354 exit(1);
1355 }
1356 SCREWUP("expected control record");
1357 }
1358 mode = 0;
1359 for (++cp; cp < buf + 5; cp++) {
1360 if (*cp < '0' || *cp > '7')
1361 SCREWUP("bad mode");
1362 mode = (mode << 3) | (*cp - '0');
1363 }
dtucker@openbsd.orge5019da2018-06-01 04:31:48 +00001364 if (!pflag)
1365 mode &= ~mask;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366 if (*cp++ != ' ')
1367 SCREWUP("mode not delimited");
1368
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001369 if (!isdigit((unsigned char)*cp))
1370 SCREWUP("size not present");
1371 ull = strtoull(cp, &cp, 10);
1372 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001373 SCREWUP("size not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001374 if (TYPE_OVERFLOW(off_t, ull))
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001375 SCREWUP("size out of range");
1376 size = (off_t)ull;
1377
djm@openbsd.org6010c032018-11-16 03:03:10 +00001378 if (*cp == '\0' || strchr(cp, '/') != NULL ||
1379 strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0) {
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001380 run_err("error: unexpected filename: %s", cp);
1381 exit(1);
1382 }
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001383 if (npatterns > 0) {
1384 for (n = 0; n < npatterns; n++) {
1385 if (fnmatch(patterns[n], cp, 0) == 0)
1386 break;
1387 }
1388 if (n >= npatterns)
1389 SCREWUP("filename does not match request");
1390 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391 if (targisdir) {
1392 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001393 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394 size_t need;
1395
1396 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001397 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001398 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001399 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001400 cursize = need;
1401 }
1402 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001403 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404 np = namebuf;
1405 } else
1406 np = targ;
1407 curfile = cp;
1408 exists = stat(np, &stb) == 0;
1409 if (buf[0] == 'D') {
1410 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001411 if (!iamrecursive)
1412 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413 if (exists) {
1414 if (!S_ISDIR(stb.st_mode)) {
1415 errno = ENOTDIR;
1416 goto bad;
1417 }
1418 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001419 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001420 } else {
Damien Miller95def091999-11-25 00:26:21 +11001421 /* Handle copying from a read-only
1422 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423 mod_flag = 1;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001424 if (mkdir(np, mode | S_IRWXU) == -1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425 goto bad;
1426 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001427 vect[0] = xstrdup(np);
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001428 sink(1, vect, src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429 if (setimes) {
1430 setimes = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001431 if (utimes(vect[0], tv) == -1)
Damien Miller95def091999-11-25 00:26:21 +11001432 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001433 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434 }
1435 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001436 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001437 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438 continue;
1439 }
1440 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001441 mode |= S_IWUSR;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001442 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443bad: run_err("%s: %s", np, strerror(errno));
1444 continue;
1445 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001446 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001447 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001448 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449 continue;
1450 }
1451 cp = bp->buf;
1452 wrerr = NO;
1453
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001455 if (showprogress)
1456 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001457 set_nonblock(remin);
1458 for (count = i = 0; i < size; i += bp->cnt) {
1459 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460 if (i + amt > size)
1461 amt = size - i;
1462 count += amt;
1463 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001464 j = atomicio6(read, remin, cp, amt,
1465 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001466 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001467 run_err("%s", j != EPIPE ?
1468 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001469 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470 exit(1);
1471 }
1472 amt -= j;
1473 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001475
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476 if (count == bp->cnt) {
1477 /* Keep reading so we stay sync'd up. */
1478 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001479 if (atomicio(vwrite, ofd, bp->buf,
1480 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001482 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001483 }
1484 }
1485 count = 0;
1486 cp = bp->buf;
1487 }
1488 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001489 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001491 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001493 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494 }
Darren Tucker067263e2007-06-25 18:32:33 +10001495 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1496 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497 run_err("%s: truncate: %s", np, strerror(errno));
1498 wrerr = DISPLAYED;
1499 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500 if (pflag) {
1501 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001502#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001503 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001504#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001505 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001506#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001508 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001509 wrerr = DISPLAYED;
1510 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001511 } else {
1512 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001513#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001514 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001515#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001516 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001517#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001519 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001520 wrerr = DISPLAYED;
1521 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522 }
Damien Millerbe484b52000-07-15 14:14:16 +10001523 if (close(ofd) == -1) {
1524 wrerr = YES;
1525 wrerrno = errno;
1526 }
Damien Miller95def091999-11-25 00:26:21 +11001527 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001528 if (showprogress)
1529 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530 if (setimes && wrerr == NO) {
1531 setimes = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001532 if (utimes(np, tv) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001534 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001535 wrerr = DISPLAYED;
1536 }
1537 }
Damien Miller95def091999-11-25 00:26:21 +11001538 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539 case YES:
1540 run_err("%s: %s", np, strerror(wrerrno));
1541 break;
1542 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001543 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544 break;
1545 case DISPLAYED:
1546 break;
1547 }
1548 }
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001549done:
1550 for (n = 0; n < npatterns; n++)
1551 free(patterns[n]);
1552 free(patterns);
1553 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001554screwup:
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001555 for (n = 0; n < npatterns; n++)
1556 free(patterns[n]);
1557 free(patterns);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001558 run_err("protocol error: %s", why);
1559 exit(1);
1560}
1561
1562int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001563response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564{
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001565 char ch, *cp, resp, rbuf[2048], visbuf[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566
Damien Millere247cc42000-05-07 12:03:14 +10001567 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001568 lostconn(0);
1569
1570 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001571 switch (resp) {
1572 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573 return (0);
1574 default:
1575 *cp++ = resp;
1576 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001577 case 1: /* error, followed by error msg */
1578 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001579 do {
Damien Millere247cc42000-05-07 12:03:14 +10001580 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001581 lostconn(0);
1582 *cp++ = ch;
1583 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1584
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001585 if (!iamremote) {
1586 cp[-1] = '\0';
1587 (void) snmprintf(visbuf, sizeof(visbuf),
1588 NULL, "%s\n", rbuf);
1589 (void) atomicio(vwrite, STDERR_FILENO,
1590 visbuf, strlen(visbuf));
1591 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001592 ++errs;
1593 if (resp == 1)
1594 return (-1);
1595 exit(1);
1596 }
1597 /* NOTREACHED */
1598}
1599
1600void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001601usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001602{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001603 (void) fprintf(stderr,
jmc@openbsd.org2c21b752019-01-27 07:14:11 +00001604 "usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
tb@openbsd.org622dedf2019-01-21 22:50:42 +00001605 " [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
1606 " [-S program] source ... target\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607 exit(1);
1608}
1609
1610void
Damien Miller95def091999-11-25 00:26:21 +11001611run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001612{
1613 static FILE *fp;
1614 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001615
1616 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001617 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1618 (void) fprintf(fp, "%c", 0x01);
1619 (void) fprintf(fp, "scp: ");
1620 va_start(ap, fmt);
1621 (void) vfprintf(fp, fmt, ap);
1622 va_end(ap);
1623 (void) fprintf(fp, "\n");
1624 (void) fflush(fp);
1625 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001626
Damien Miller95def091999-11-25 00:26:21 +11001627 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001628 va_start(ap, fmt);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001629 vfmprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001630 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001631 fprintf(stderr, "\n");
1632 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001633}
1634
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635void
Damien Miller65d1f572003-06-04 22:51:08 +10001636verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637{
1638 struct stat stb;
1639
1640 if (!stat(cp, &stb)) {
1641 if (S_ISDIR(stb.st_mode))
1642 return;
1643 errno = ENOTDIR;
1644 }
1645 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001646 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647}
1648
1649int
Damien Miller65d1f572003-06-04 22:51:08 +10001650okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651{
1652 int c;
1653 char *cp;
1654
1655 cp = cp0;
1656 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001657 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658 if (c & 0200)
1659 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001660 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001661 switch (c) {
1662 case '\'':
1663 case '"':
1664 case '`':
1665 case ' ':
1666 case '#':
1667 goto bad;
1668 default:
1669 break;
1670 }
1671 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001672 } while (*++cp);
1673 return (1);
1674
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001675bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001676 return (0);
1677}
1678
1679BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001680allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001681{
1682 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001683#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001684 struct stat stb;
1685
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001686 if (fstat(fd, &stb) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001687 run_err("fstat: %s", strerror(errno));
1688 return (0);
1689 }
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001690 size = ROUNDUP(stb.st_blksize, blksize);
Ben Lindstrom418e0782002-12-23 02:22:09 +00001691 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001692 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001693#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001694 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001695#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001696 if (bp->cnt >= size)
1697 return (bp);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001698 bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001699 bp->cnt = size;
1700 return (bp);
1701}
1702
1703void
Damien Miller65d1f572003-06-04 22:51:08 +10001704lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001705{
1706 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001707 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001708 if (signo)
1709 _exit(1);
1710 else
1711 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001712}