blob: 86204d8f1c93cb881340c3fbfc27d5c9a96c0ebf [file] [log] [blame]
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001/* $OpenBSD: scp.c,v 1.205 2019/06/28 13:35:04 deraadt 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>
djm@openbsd.org391ffc42019-01-26 22:41:28 +000097#include <fnmatch.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000098#include <limits.h>
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +000099#include <locale.h>
Damien Miller9f2abc42006-07-10 20:53:08 +1000100#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +1100101#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +1000102#include <stdarg.h>
Darren Tucker329037e2017-05-01 13:19:41 +1000103#ifdef HAVE_STDINT_H
millert@openbsd.org06643712017-04-28 03:21:12 +0000104#include <stdint.h>
Darren Tucker329037e2017-05-01 13:19:41 +1000105#endif
Damien Millera7a73ee2006-08-05 11:37:59 +1000106#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +1000107#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +1000108#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +1000109#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +1000110#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +1100111#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Darren Tuckerbed63112007-06-13 00:02:07 +1000112#include <vis.h>
113#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115#include "xmalloc.h"
millert@openbsd.org887669e2017-10-21 23:06:24 +0000116#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000117#include "atomicio.h"
118#include "pathnames.h"
119#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +0000120#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +1100121#include "progressmeter.h"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000122#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000124extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000125
Damien Millerc77cadb2007-10-26 15:39:15 +1000126#define COPY_BUFLEN 16384
127
millert@openbsd.org887669e2017-10-21 23:06:24 +0000128int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
129int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000130
Ben Lindstrom387c4722001-05-08 20:27:25 +0000131/* Struct for addargs */
132arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100133arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100134
Damien Millerff74d742003-01-24 11:36:58 +1100135/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000136long long limit_kbps = 0;
137struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100138
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139/* Name of current file being transferred. */
140char *curfile;
141
142/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100143int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145/* This is set to zero if the progressmeter is not desired. */
146int showprogress = 1;
147
Damien Millerf1211432011-01-06 22:40:30 +1100148/*
149 * This is set to non-zero if remote-remote copy should be piped
150 * through this process.
151 */
152int throughlocal = 0;
153
millert@openbsd.org887669e2017-10-21 23:06:24 +0000154/* Non-standard port to use for the ssh connection or -1. */
155int sshport = -1;
156
Damien Millerad833b32000-08-23 10:46:23 +1000157/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100158char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000159
Ben Lindstrom007eb912002-11-09 15:54:08 +0000160/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000161pid_t do_cmd_pid = -1;
162
163static void
164killchild(int signo)
165{
Darren Tuckerba66df82005-01-24 21:57:40 +1100166 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000167 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100168 waitpid(do_cmd_pid, NULL, 0);
169 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000170
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000171 if (signo)
172 _exit(1);
173 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000174}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000175
Damien Miller0979b402010-07-02 13:37:33 +1000176static void
177suspchild(int signo)
178{
179 int status;
180
181 if (do_cmd_pid > 1) {
182 kill(do_cmd_pid, signo);
183 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
184 errno == EINTR)
185 ;
186 kill(getpid(), SIGSTOP);
187 }
188}
189
Damien Miller3eec6b72006-01-31 21:49:27 +1100190static int
191do_local_cmd(arglist *a)
192{
193 u_int i;
194 int status;
195 pid_t pid;
196
197 if (a->num == 0)
198 fatal("do_local_cmd: no arguments");
199
200 if (verbose_mode) {
201 fprintf(stderr, "Executing:");
202 for (i = 0; i < a->num; i++)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000203 fmprintf(stderr, " %s", a->list[i]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100204 fprintf(stderr, "\n");
205 }
206 if ((pid = fork()) == -1)
207 fatal("do_local_cmd: fork: %s", strerror(errno));
208
209 if (pid == 0) {
210 execvp(a->list[0], a->list);
211 perror(a->list[0]);
212 exit(1);
213 }
214
215 do_cmd_pid = pid;
216 signal(SIGTERM, killchild);
217 signal(SIGINT, killchild);
218 signal(SIGHUP, killchild);
219
220 while (waitpid(pid, &status, 0) == -1)
221 if (errno != EINTR)
222 fatal("do_local_cmd: waitpid: %s", strerror(errno));
223
224 do_cmd_pid = -1;
225
226 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
227 return (-1);
228
229 return (0);
230}
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
233 * This function executes the given command as the specified user on the
234 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
235 * assigns the input and output file descriptors on success.
236 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller4af51302000-04-16 11:18:38 +1000238int
millert@openbsd.org887669e2017-10-21 23:06:24 +0000239do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Damien Miller95def091999-11-25 00:26:21 +1100241 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000244 fmprintf(stderr,
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000245 "Executing: program %s host %s, user %s, command %s\n",
246 ssh_program, host,
247 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
millert@openbsd.org887669e2017-10-21 23:06:24 +0000249 if (port == -1)
250 port = sshport;
251
Damien Miller5428f641999-11-25 11:54:57 +1100252 /*
253 * Reserve two descriptors so that the real pipes won't get
254 * descriptors 0 and 1 because that will screw up dup2 below.
255 */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000256 if (pipe(reserved) == -1)
Damien Miller40b59852006-06-13 13:00:25 +1000257 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 /* Create a socket pair for communicating with ssh. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000260 if (pipe(pin) == -1)
Damien Miller95def091999-11-25 00:26:21 +1100261 fatal("pipe: %s", strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000262 if (pipe(pout) == -1)
Damien Miller95def091999-11-25 00:26:21 +1100263 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Miller95def091999-11-25 00:26:21 +1100265 /* Free the reserved descriptors. */
266 close(reserved[0]);
267 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Damien Miller0979b402010-07-02 13:37:33 +1000269 signal(SIGTSTP, suspchild);
270 signal(SIGTTIN, suspchild);
271 signal(SIGTTOU, suspchild);
272
Damien Millerb2cdcb52003-06-18 20:26:34 +1000273 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000274 do_cmd_pid = fork();
275 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100276 /* Child. */
277 close(pin[1]);
278 close(pout[0]);
279 dup2(pin[0], 0);
280 dup2(pout[1], 1);
281 close(pin[0]);
282 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller3eec6b72006-01-31 21:49:27 +1100284 replacearg(&args, 0, "%s", ssh_program);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000285 if (port != -1) {
286 addargs(&args, "-p");
287 addargs(&args, "%d", port);
288 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100289 if (remuser != NULL) {
290 addargs(&args, "-l");
291 addargs(&args, "%s", remuser);
292 }
293 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000294 addargs(&args, "%s", host);
295 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100296
Damien Miller874d77b2000-10-14 16:23:11 +1100297 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000298 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100299 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000300 } else if (do_cmd_pid == -1) {
301 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302 }
Damien Miller95def091999-11-25 00:26:21 +1100303 /* Parent. Close the other side, and return the local side. */
304 close(pin[0]);
305 *fdout = pin[1];
306 close(pout[1]);
307 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000308 signal(SIGTERM, killchild);
309 signal(SIGINT, killchild);
310 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100311 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312}
313
Damien Millerf1211432011-01-06 22:40:30 +1100314/*
djm@openbsd.org001aa552018-04-10 00:10:49 +0000315 * This function executes a command similar to do_cmd(), but expects the
Damien Millerf1211432011-01-06 22:40:30 +1100316 * input and output descriptors to be setup by a previous call to do_cmd().
317 * This way the input and output of two commands can be connected.
318 */
319int
millert@openbsd.org887669e2017-10-21 23:06:24 +0000320do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
Damien Millerf1211432011-01-06 22:40:30 +1100321{
322 pid_t pid;
323 int status;
324
325 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000326 fmprintf(stderr,
Damien Millerf1211432011-01-06 22:40:30 +1100327 "Executing: 2nd program %s host %s, user %s, command %s\n",
328 ssh_program, host,
329 remuser ? remuser : "(unspecified)", cmd);
330
millert@openbsd.org887669e2017-10-21 23:06:24 +0000331 if (port == -1)
332 port = sshport;
333
Damien Millerf1211432011-01-06 22:40:30 +1100334 /* Fork a child to execute the command on the remote host using ssh. */
335 pid = fork();
336 if (pid == 0) {
337 dup2(fdin, 0);
338 dup2(fdout, 1);
339
340 replacearg(&args, 0, "%s", ssh_program);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000341 if (port != -1) {
342 addargs(&args, "-p");
343 addargs(&args, "%d", port);
344 }
Damien Millerf1211432011-01-06 22:40:30 +1100345 if (remuser != NULL) {
346 addargs(&args, "-l");
347 addargs(&args, "%s", remuser);
348 }
349 addargs(&args, "--");
350 addargs(&args, "%s", host);
351 addargs(&args, "%s", cmd);
352
353 execvp(ssh_program, args.list);
354 perror(ssh_program);
355 exit(1);
356 } else if (pid == -1) {
357 fatal("fork: %s", strerror(errno));
358 }
359 while (waitpid(pid, &status, 0) == -1)
360 if (errno != EINTR)
361 fatal("do_cmd2: waitpid: %s", strerror(errno));
362 return 0;
363}
364
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000366 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367 char *buf;
368} BUF;
369
Damien Miller95def091999-11-25 00:26:21 +1100370BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100371void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100372int okname(char *);
373void run_err(const char *,...);
374void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100377uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378int errs, remin, remout;
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000379int Tflag, pflag, iamremote, iamrecursive, targetshouldbedirectory;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380
381#define CMDNEEDS 64
382char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
383
Damien Miller95def091999-11-25 00:26:21 +1100384int response(void);
385void rsource(char *, struct stat *);
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000386void sink(int, char *[], const char *);
Damien Miller95def091999-11-25 00:26:21 +1100387void source(int, char *[]);
388void tolocal(int, char *[]);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000389void toremote(int, char *[]);
Damien Miller95def091999-11-25 00:26:21 +1100390void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
392int
Damien Miller65d1f572003-06-04 22:51:08 +1000393main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000395 int ch, fflag, tflag, status, n;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000396 char **newargv;
Damien Miller65e42f82010-09-24 22:15:11 +1000397 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398 extern char *optarg;
399 extern int optind;
400
Darren Tuckerce321d82005-10-03 18:11:24 +1000401 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
402 sanitise_stdfd();
403
Damien Miller42c5ec42018-11-23 10:40:06 +1100404 seed_rng();
405
Damien Millerdda78a02016-12-12 13:57:10 +1100406 msetlocale();
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000407
Damien Miller3d1a9f42006-07-10 22:19:53 +1000408 /* Copy argv, because we modify it */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000409 newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
Damien Miller3d1a9f42006-07-10 22:19:53 +1000410 for (n = 0; n < argc; n++)
411 newargv[n] = xstrdup(argv[n]);
412 argv = newargv;
413
Damien Miller59d3d5b2003-08-22 09:34:41 +1000414 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000415
Damien Miller3eec6b72006-01-31 21:49:27 +1100416 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100417 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
418 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100419 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000420 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100421 addargs(&args, "-oForwardAgent=no");
422 addargs(&args, "-oPermitLocalCommand=no");
423 addargs(&args, "-oClearAllForwardings=yes");
djm@openbsd.org77e05392018-02-10 06:15:12 +0000424 addargs(&args, "-oRemoteCommand=none");
425 addargs(&args, "-oRequestTTY=no");
Damien Miller874d77b2000-10-14 16:23:11 +1100426
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000427 fflag = Tflag = tflag = 0;
428 while ((ch = getopt(argc, argv,
429 "dfl:prtTvBCc:i:P:q12346S:o:F:J:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +1100430 switch (ch) {
431 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100432 case '1':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000433 fatal("SSH protocol v.1 is no longer supported");
434 break;
Damien Miller8e121472003-01-24 11:37:38 +1100435 case '2':
djm@openbsd.orga3710d52017-04-30 23:28:12 +0000436 /* Ignored */
437 break;
Damien Miller34132e52000-01-14 15:45:46 +1100438 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100439 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100440 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000441 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100442 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100443 break;
Damien Millerf1211432011-01-06 22:40:30 +1100444 case '3':
445 throughlocal = 1;
446 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100447 case 'o':
448 case 'c':
449 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000450 case 'F':
tb@openbsd.org622dedf2019-01-21 22:50:42 +0000451 case 'J':
Damien Miller87dc0a42010-12-01 12:03:19 +1100452 addargs(&remote_remote_args, "-%c", ch);
453 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100454 addargs(&args, "-%c", ch);
455 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100456 break;
457 case 'P':
millert@openbsd.org887669e2017-10-21 23:06:24 +0000458 sshport = a2port(optarg);
459 if (sshport <= 0)
460 fatal("bad port \"%s\"\n", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100461 break;
462 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100463 addargs(&remote_remote_args, "-oBatchmode=yes");
464 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100465 break;
Damien Millerff74d742003-01-24 11:36:58 +1100466 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000467 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
468 &errstr);
469 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100470 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000471 limit_kbps *= 1024; /* kbps */
472 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100473 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 case 'p':
475 pflag = 1;
476 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477 case 'r':
478 iamrecursive = 1;
479 break;
Damien Millerad833b32000-08-23 10:46:23 +1000480 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100481 ssh_program = xstrdup(optarg);
482 break;
483 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000484 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100485 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100486 verbose_mode = 1;
487 break;
488 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100489 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100490 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100491 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000492 break;
493
Damien Miller95def091999-11-25 00:26:21 +1100494 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000495 case 'd':
496 targetshouldbedirectory = 1;
497 break;
Damien Miller95def091999-11-25 00:26:21 +1100498 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499 iamremote = 1;
500 fflag = 1;
501 break;
Damien Miller95def091999-11-25 00:26:21 +1100502 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503 iamremote = 1;
504 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000505#ifdef HAVE_CYGWIN
506 setmode(0, O_BINARY);
507#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508 break;
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000509 case 'T':
510 Tflag = 1;
511 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512 default:
513 usage();
514 }
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000515 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516 argc -= optind;
517 argv += optind;
518
519 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000520 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100522 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523 showprogress = 0;
524
deraadt@openbsd.org9080bd02015-11-27 00:49:31 +0000525 if (pflag) {
526 /* Cannot pledge: -p allows setuid/setgid files... */
527 } else {
528 if (pledge("stdio rpath wpath cpath fattr tty proc exec",
529 NULL) == -1) {
530 perror("pledge");
531 exit(1);
532 }
533 }
534
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535 remin = STDIN_FILENO;
536 remout = STDOUT_FILENO;
537
Kevin Stevesef4eea92001-02-05 12:42:17 +0000538 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100539 /* Follow "protocol", send data. */
540 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000541 source(argc, argv);
542 exit(errs != 0);
543 }
Damien Miller95def091999-11-25 00:26:21 +1100544 if (tflag) {
545 /* Receive data. */
djm@openbsd.org391ffc42019-01-26 22:41:28 +0000546 sink(argc, argv, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000547 exit(errs != 0);
548 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000549 if (argc < 2)
550 usage();
551 if (argc > 2)
552 targetshouldbedirectory = 1;
553
554 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000555 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000556 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000557 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
558 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000559 iamrecursive ? " -r" : "", pflag ? " -p" : "",
560 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000561
Damien Miller95def091999-11-25 00:26:21 +1100562 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000563
millert@openbsd.org887669e2017-10-21 23:06:24 +0000564 if (colon(argv[argc - 1])) /* Dest is remote host. */
565 toremote(argc, argv);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000566 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000567 if (targetshouldbedirectory)
568 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100569 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000570 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000571 /*
572 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100573 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000574 */
575 if (do_cmd_pid != -1 && errs == 0) {
576 if (remin != -1)
577 (void) close(remin);
578 if (remout != -1)
579 (void) close(remout);
580 if (waitpid(do_cmd_pid, &status, 0) == -1)
581 errs = 1;
582 else {
583 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
584 errs = 1;
585 }
586 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587 exit(errs != 0);
588}
589
Damien Miller65e42f82010-09-24 22:15:11 +1000590/* Callback from atomicio6 to update progress meter and limit bandwidth */
591static int
592scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000593{
Damien Miller65e42f82010-09-24 22:15:11 +1000594 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000595
Damien Miller65e42f82010-09-24 22:15:11 +1000596 *cnt += s;
dtucker@openbsd.orgbdc6c632019-01-24 16:52:17 +0000597 refresh_progress_meter(0);
Damien Miller65e42f82010-09-24 22:15:11 +1000598 if (limit_kbps > 0)
599 bandwidth_limit(&bwlimit, s);
600 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000601}
602
Damien Miller044bd2a2013-07-18 16:09:25 +1000603static int
604do_times(int fd, int verb, const struct stat *sb)
605{
606 /* strlen(2^64) == 20; strlen(10^6) == 7 */
607 char buf[(20 + 7 + 2) * 2 + 2];
608
609 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
610 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
611 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
612 if (verb) {
613 fprintf(stderr, "File mtime %lld atime %lld\n",
614 (long long)sb->st_mtime, (long long)sb->st_atime);
615 fprintf(stderr, "Sending file timestamps: %s", buf);
616 }
617 (void) atomicio(vwrite, fd, buf, strlen(buf));
618 return (response());
619}
620
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000621static int
622parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
623 char **pathp)
624{
625 int r;
626
627 r = parse_uri("scp", uri, userp, hostp, portp, pathp);
628 if (r == 0 && *pathp == NULL)
629 *pathp = xstrdup(".");
630 return r;
631}
632
djm@openbsd.org3d896c12019-02-10 11:15:52 +0000633/* Appends a string to an array; returns 0 on success, -1 on alloc failure */
634static int
635append(char *cp, char ***ap, size_t *np)
636{
637 char **tmp;
638
639 if ((tmp = reallocarray(*ap, *np + 1, sizeof(*tmp))) == NULL)
640 return -1;
641 tmp[(*np)] = cp;
642 (*np)++;
643 *ap = tmp;
644 return 0;
645}
646
647/*
648 * Finds the start and end of the first brace pair in the pattern.
649 * returns 0 on success or -1 for invalid patterns.
650 */
651static int
652find_brace(const char *pattern, int *startp, int *endp)
653{
654 int i;
655 int in_bracket, brace_level;
656
657 *startp = *endp = -1;
658 in_bracket = brace_level = 0;
659 for (i = 0; i < INT_MAX && *endp < 0 && pattern[i] != '\0'; i++) {
660 switch (pattern[i]) {
661 case '\\':
662 /* skip next character */
663 if (pattern[i + 1] != '\0')
664 i++;
665 break;
666 case '[':
667 in_bracket = 1;
668 break;
669 case ']':
670 in_bracket = 0;
671 break;
672 case '{':
673 if (in_bracket)
674 break;
675 if (pattern[i + 1] == '}') {
676 /* Protect a single {}, for find(1), like csh */
677 i++; /* skip */
678 break;
679 }
680 if (*startp == -1)
681 *startp = i;
682 brace_level++;
683 break;
684 case '}':
685 if (in_bracket)
686 break;
687 if (*startp < 0) {
688 /* Unbalanced brace */
689 return -1;
690 }
691 if (--brace_level <= 0)
692 *endp = i;
693 break;
694 }
695 }
696 /* unbalanced brackets/braces */
697 if (*endp < 0 && (*startp >= 0 || in_bracket))
698 return -1;
699 return 0;
700}
701
702/*
703 * Assembles and records a successfully-expanded pattern, returns -1 on
704 * alloc failure.
705 */
706static int
707emit_expansion(const char *pattern, int brace_start, int brace_end,
708 int sel_start, int sel_end, char ***patternsp, size_t *npatternsp)
709{
710 char *cp;
711 int o = 0, tail_len = strlen(pattern + brace_end + 1);
712
713 if ((cp = malloc(brace_start + (sel_end - sel_start) +
714 tail_len + 1)) == NULL)
715 return -1;
716
717 /* Pattern before initial brace */
718 if (brace_start > 0) {
719 memcpy(cp, pattern, brace_start);
720 o = brace_start;
721 }
722 /* Current braced selection */
723 if (sel_end - sel_start > 0) {
724 memcpy(cp + o, pattern + sel_start,
725 sel_end - sel_start);
726 o += sel_end - sel_start;
727 }
728 /* Remainder of pattern after closing brace */
729 if (tail_len > 0) {
730 memcpy(cp + o, pattern + brace_end + 1, tail_len);
731 o += tail_len;
732 }
733 cp[o] = '\0';
734 if (append(cp, patternsp, npatternsp) != 0) {
735 free(cp);
736 return -1;
737 }
738 return 0;
739}
740
741/*
742 * Expand the first encountered brace in pattern, appending the expanded
743 * patterns it yielded to the *patternsp array.
744 *
745 * Returns 0 on success or -1 on allocation failure.
746 *
747 * Signals whether expansion was performed via *expanded and whether
748 * pattern was invalid via *invalid.
749 */
750static int
751brace_expand_one(const char *pattern, char ***patternsp, size_t *npatternsp,
752 int *expanded, int *invalid)
753{
754 int i;
755 int in_bracket, brace_start, brace_end, brace_level;
756 int sel_start, sel_end;
757
758 *invalid = *expanded = 0;
759
760 if (find_brace(pattern, &brace_start, &brace_end) != 0) {
761 *invalid = 1;
762 return 0;
763 } else if (brace_start == -1)
764 return 0;
765
766 in_bracket = brace_level = 0;
767 for (i = sel_start = brace_start + 1; i < brace_end; i++) {
768 switch (pattern[i]) {
769 case '{':
770 if (in_bracket)
771 break;
772 brace_level++;
773 break;
774 case '}':
775 if (in_bracket)
776 break;
777 brace_level--;
778 break;
779 case '[':
780 in_bracket = 1;
781 break;
782 case ']':
783 in_bracket = 0;
784 break;
785 case '\\':
786 if (i < brace_end - 1)
787 i++; /* skip */
788 break;
789 }
790 if (pattern[i] == ',' || i == brace_end - 1) {
791 if (in_bracket || brace_level > 0)
792 continue;
793 /* End of a selection, emit an expanded pattern */
794
795 /* Adjust end index for last selection */
796 sel_end = (i == brace_end - 1) ? brace_end : i;
797 if (emit_expansion(pattern, brace_start, brace_end,
798 sel_start, sel_end, patternsp, npatternsp) != 0)
799 return -1;
800 /* move on to the next selection */
801 sel_start = i + 1;
802 continue;
803 }
804 }
805 if (in_bracket || brace_level > 0) {
806 *invalid = 1;
807 return 0;
808 }
809 /* success */
810 *expanded = 1;
811 return 0;
812}
813
814/* Expand braces from pattern. Returns 0 on success, -1 on failure */
815static int
816brace_expand(const char *pattern, char ***patternsp, size_t *npatternsp)
817{
818 char *cp, *cp2, **active = NULL, **done = NULL;
819 size_t i, nactive = 0, ndone = 0;
820 int ret = -1, invalid = 0, expanded = 0;
821
822 *patternsp = NULL;
823 *npatternsp = 0;
824
825 /* Start the worklist with the original pattern */
826 if ((cp = strdup(pattern)) == NULL)
827 return -1;
828 if (append(cp, &active, &nactive) != 0) {
829 free(cp);
830 return -1;
831 }
832 while (nactive > 0) {
833 cp = active[nactive - 1];
834 nactive--;
835 if (brace_expand_one(cp, &active, &nactive,
836 &expanded, &invalid) == -1) {
837 free(cp);
838 goto fail;
839 }
840 if (invalid)
841 fatal("%s: invalid brace pattern \"%s\"", __func__, cp);
842 if (expanded) {
843 /*
844 * Current entry expanded to new entries on the
845 * active list; discard the progenitor pattern.
846 */
847 free(cp);
848 continue;
849 }
850 /*
851 * Pattern did not expand; append the finename component to
852 * the completed list
853 */
854 if ((cp2 = strrchr(cp, '/')) != NULL)
855 *cp2++ = '\0';
856 else
857 cp2 = cp;
858 if (append(xstrdup(cp2), &done, &ndone) != 0) {
859 free(cp);
860 goto fail;
861 }
862 free(cp);
863 }
864 /* success */
865 *patternsp = done;
866 *npatternsp = ndone;
867 done = NULL;
868 ndone = 0;
869 ret = 0;
870 fail:
871 for (i = 0; i < nactive; i++)
872 free(active[i]);
873 free(active);
874 for (i = 0; i < ndone; i++)
875 free(done[i]);
876 free(done);
877 return ret;
878}
879
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880void
millert@openbsd.org887669e2017-10-21 23:06:24 +0000881toremote(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882{
millert@openbsd.org887669e2017-10-21 23:06:24 +0000883 char *suser = NULL, *host = NULL, *src = NULL;
884 char *bp, *tuser, *thost, *targ;
885 int sport = -1, tport = -1;
Damien Miller3eec6b72006-01-31 21:49:27 +1100886 arglist alist;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000887 int i, r;
Damien Miller87dc0a42010-12-01 12:03:19 +1100888 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100889
890 memset(&alist, '\0', sizeof(alist));
891 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
millert@openbsd.org887669e2017-10-21 23:06:24 +0000893 /* Parse target */
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000894 r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ);
895 if (r == -1) {
896 fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]);
897 ++errs;
898 goto out;
899 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000900 if (r != 0) {
901 if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000902 &targ) == -1) {
903 fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
904 ++errs;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000905 goto out;
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000906 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907 }
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000908 if (tuser != NULL && !okname(tuser)) {
909 ++errs;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000910 goto out;
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000911 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912
millert@openbsd.org887669e2017-10-21 23:06:24 +0000913 /* Parse source files */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914 for (i = 0; i < argc - 1; i++) {
millert@openbsd.org887669e2017-10-21 23:06:24 +0000915 free(suser);
916 free(host);
917 free(src);
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000918 r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
919 if (r == -1) {
920 fmprintf(stderr, "%s: invalid uri\n", argv[i]);
921 ++errs;
922 continue;
923 }
924 if (r != 0) {
millert@openbsd.org887669e2017-10-21 23:06:24 +0000925 parse_user_host_path(argv[i], &suser, &host, &src);
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +0000926 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000927 if (suser != NULL && !okname(suser)) {
928 ++errs;
929 continue;
930 }
931 if (host && throughlocal) { /* extended remote to remote */
Damien Miller0603d982011-09-22 21:38:00 +1000932 xasprintf(&bp, "%s -f %s%s", cmd,
933 *src == '-' ? "-- " : "", src);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000934 if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
Damien Millerf1211432011-01-06 22:40:30 +1100935 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000936 free(bp);
Damien Miller0603d982011-09-22 21:38:00 +1000937 xasprintf(&bp, "%s -t %s%s", cmd,
938 *targ == '-' ? "-- " : "", targ);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000939 if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
Damien Millerf1211432011-01-06 22:40:30 +1100940 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000941 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100942 (void) close(remin);
943 (void) close(remout);
944 remin = remout = -1;
millert@openbsd.org887669e2017-10-21 23:06:24 +0000945 } else if (host) { /* standard remote to remote */
946 if (tport != -1 && tport != SSH_DEFAULT_PORT) {
947 /* This would require the remote support URIs */
948 fatal("target port not supported with two "
949 "remote hosts without the -3 option");
950 }
951
Damien Miller3eec6b72006-01-31 21:49:27 +1100952 freeargs(&alist);
953 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100954 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100955 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100956 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100957 for (j = 0; j < remote_remote_args.num; j++) {
958 addargs(&alist, "%s",
959 remote_remote_args.list[j]);
960 }
Damien Miller3eec6b72006-01-31 21:49:27 +1100961
millert@openbsd.org887669e2017-10-21 23:06:24 +0000962 if (sport != -1) {
963 addargs(&alist, "-p");
964 addargs(&alist, "%d", sport);
965 }
966 if (suser) {
Damien Miller3eec6b72006-01-31 21:49:27 +1100967 addargs(&alist, "-l");
968 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100969 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100970 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100971 addargs(&alist, "%s", host);
972 addargs(&alist, "%s", cmd);
973 addargs(&alist, "%s", src);
974 addargs(&alist, "%s%s%s:%s",
975 tuser ? tuser : "", tuser ? "@" : "",
976 thost, targ);
977 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000978 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100979 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000981 xasprintf(&bp, "%s -t %s%s", cmd,
982 *targ == '-' ? "-- " : "", targ);
millert@openbsd.org887669e2017-10-21 23:06:24 +0000983 if (do_cmd(thost, tuser, tport, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000984 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100985 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000986 if (response() < 0)
987 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000988 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989 }
Damien Miller95def091999-11-25 00:26:21 +1100990 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991 }
992 }
millert@openbsd.org887669e2017-10-21 23:06:24 +0000993out:
994 free(tuser);
995 free(thost);
996 free(targ);
997 free(suser);
998 free(host);
999 free(src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000}
1001
1002void
Damien Miller65d1f572003-06-04 22:51:08 +10001003tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004{
millert@openbsd.org887669e2017-10-21 23:06:24 +00001005 char *bp, *host = NULL, *src = NULL, *suser = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +11001006 arglist alist;
millert@openbsd.org887669e2017-10-21 23:06:24 +00001007 int i, r, sport = -1;
Damien Miller3eec6b72006-01-31 21:49:27 +11001008
1009 memset(&alist, '\0', sizeof(alist));
1010 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011
1012 for (i = 0; i < argc - 1; i++) {
millert@openbsd.org887669e2017-10-21 23:06:24 +00001013 free(suser);
1014 free(host);
1015 free(src);
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +00001016 r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
millert@openbsd.org887669e2017-10-21 23:06:24 +00001017 if (r == -1) {
millert@openbsd.orgc9e37a82017-12-18 17:28:54 +00001018 fmprintf(stderr, "%s: invalid uri\n", argv[i]);
millert@openbsd.org887669e2017-10-21 23:06:24 +00001019 ++errs;
1020 continue;
1021 }
1022 if (r != 0)
1023 parse_user_host_path(argv[i], &suser, &host, &src);
1024 if (suser != NULL && !okname(suser)) {
1025 ++errs;
1026 continue;
1027 }
1028 if (!host) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +11001029 freeargs(&alist);
1030 addargs(&alist, "%s", _PATH_CP);
1031 if (iamrecursive)
1032 addargs(&alist, "-r");
1033 if (pflag)
1034 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +11001035 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +11001036 addargs(&alist, "%s", argv[i]);
1037 addargs(&alist, "%s", argv[argc-1]);
1038 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040 continue;
1041 }
millert@openbsd.org887669e2017-10-21 23:06:24 +00001042 /* Remote to local. */
Damien Miller0603d982011-09-22 21:38:00 +10001043 xasprintf(&bp, "%s -f %s%s",
1044 cmd, *src == '-' ? "-- " : "", src);
millert@openbsd.org887669e2017-10-21 23:06:24 +00001045 if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001046 free(bp);
Damien Miller95def091999-11-25 00:26:21 +11001047 ++errs;
1048 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049 }
Darren Tuckera627d422013-06-02 07:31:17 +10001050 free(bp);
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001051 sink(1, argv + argc - 1, src);
Damien Miller95def091999-11-25 00:26:21 +11001052 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001053 remin = remout = -1;
1054 }
millert@openbsd.org887669e2017-10-21 23:06:24 +00001055 free(suser);
1056 free(host);
1057 free(src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058}
1059
1060void
Damien Miller65d1f572003-06-04 22:51:08 +10001061source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001062{
1063 struct stat stb;
1064 static BUF buffer;
1065 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +10001066 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +10001067 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +10001068 int fd = -1, haderr, indx;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001069 char *last, *name, buf[2048], encname[PATH_MAX];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +00001070 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071
1072 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +11001073 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +00001075 len = strlen(name);
1076 while (len > 1 && name[len-1] == '/')
1077 name[--len] = '\0';
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001078 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) == -1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +10001080 if (strchr(name, '\n') != NULL) {
1081 strnvis(encname, name, sizeof(encname), VIS_NL);
1082 name = encname;
1083 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001084 if (fstat(fd, &stb) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001085syserr: run_err("%s: %s", name, strerror(errno));
1086 goto next;
1087 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +10001088 if (stb.st_size < 0) {
1089 run_err("%s: %s", name, "Negative file size");
1090 goto next;
1091 }
Darren Tucker43ce9022007-06-12 23:41:06 +10001092 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093 switch (stb.st_mode & S_IFMT) {
1094 case S_IFREG:
1095 break;
1096 case S_IFDIR:
1097 if (iamrecursive) {
1098 rsource(name, &stb);
1099 goto next;
1100 }
1101 /* FALLTHROUGH */
1102 default:
1103 run_err("%s: not a regular file", name);
1104 goto next;
1105 }
1106 if ((last = strrchr(name, '/')) == NULL)
1107 last = name;
1108 else
1109 ++last;
1110 curfile = last;
1111 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +10001112 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113 goto next;
1114 }
1115#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +00001116 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +00001117 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +11001118 (long long)stb.st_size, last);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001119 if (verbose_mode)
1120 fmprintf(stderr, "Sending file modes: %s", buf);
Darren Tucker9f63f222003-07-03 13:46:56 +10001121 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122 if (response() < 0)
1123 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +10001124 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +11001125next: if (fd != -1) {
1126 (void) close(fd);
1127 fd = -1;
1128 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129 continue;
1130 }
Damien Miller62d57f62003-01-10 21:43:24 +11001131 if (showprogress)
1132 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001133 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
1135 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +10001136 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137 amt = stb.st_size - i;
1138 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +10001139 if ((nr = atomicio(read, fd,
1140 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +10001141 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +10001142 memset(bp->buf + nr, 0, amt - nr);
1143 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001145 /* Keep writing after error to retain sync */
1146 if (haderr) {
1147 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +10001148 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +10001149 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001150 }
Damien Miller65e42f82010-09-24 22:15:11 +10001151 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +10001152 &statbytes) != amt)
1153 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001155 unset_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156
Darren Tucker33f86bc2005-11-22 19:38:06 +11001157 if (fd != -1) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001158 if (close(fd) == -1 && !haderr)
Darren Tucker33f86bc2005-11-22 19:38:06 +11001159 haderr = errno;
1160 fd = -1;
1161 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +10001163 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001164 else
1165 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +11001166 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001167 if (showprogress)
1168 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169 }
1170}
1171
1172void
Damien Miller65d1f572003-06-04 22:51:08 +10001173rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174{
1175 DIR *dirp;
1176 struct dirent *dp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +00001177 char *last, *vect[1], path[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178
1179 if (!(dirp = opendir(name))) {
1180 run_err("%s: %s", name, strerror(errno));
1181 return;
1182 }
1183 last = strrchr(name, '/');
mmcc@openbsd.orge92c4992015-10-16 17:07:24 +00001184 if (last == NULL)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 last = name;
1186 else
1187 last++;
1188 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +10001189 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190 closedir(dirp);
1191 return;
1192 }
1193 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001194 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +00001195 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +11001196 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001197 fmprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +10001198 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199 if (response() < 0) {
1200 closedir(dirp);
1201 return;
1202 }
Ben Lindstrombd472262001-03-29 00:39:55 +00001203 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204 if (dp->d_ino == 0)
1205 continue;
1206 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1207 continue;
1208 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
1209 run_err("%s/%s: name too long", name, dp->d_name);
1210 continue;
1211 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001212 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213 vect[0] = path;
1214 source(1, vect);
1215 }
Damien Miller95def091999-11-25 00:26:21 +11001216 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +10001217 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +11001218 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219}
1220
millert@openbsd.org06643712017-04-28 03:21:12 +00001221#define TYPE_OVERFLOW(type, val) \
1222 ((sizeof(type) == 4 && (val) > INT32_MAX) || \
1223 (sizeof(type) == 8 && (val) > INT64_MAX) || \
1224 (sizeof(type) != 4 && sizeof(type) != 8))
1225
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226void
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001227sink(int argc, char **argv, const char *src)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228{
1229 static BUF buffer;
1230 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +11001231 enum {
1232 YES, NO, DISPLAYED
1233 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +10001235 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +10001236 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +10001237 int amt, exists, first, ofd;
1238 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +11001239 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +10001240 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +11001241 int setimes, targisdir, wrerrno = 0;
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001242 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001243 char **patterns = NULL;
1244 size_t n, npatterns = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001245 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246
Ben Lindstromf719a202001-04-14 23:14:22 +00001247#define atime tv[0]
1248#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +11001249#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250
millert@openbsd.org06643712017-04-28 03:21:12 +00001251 if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
1252 SCREWUP("Unexpected off_t/time_t size");
1253
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254 setimes = targisdir = 0;
1255 mask = umask(0);
1256 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +11001257 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001258 if (argc != 1) {
1259 run_err("ambiguous target");
1260 exit(1);
1261 }
1262 targ = *argv;
1263 if (targetshouldbedirectory)
1264 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +11001265
Darren Tucker9f63f222003-07-03 13:46:56 +10001266 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
1268 targisdir = 1;
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001269 if (src != NULL && !iamrecursive && !Tflag) {
1270 /*
1271 * Prepare to try to restrict incoming filenames to match
1272 * the requested destination file glob.
1273 */
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001274 if (brace_expand(src, &patterns, &npatterns) != 0)
1275 fatal("%s: could not expand pattern", __func__);
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001276 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277 for (first = 1;; first = 0) {
1278 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +10001279 if (atomicio(read, remin, cp, 1) != 1)
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001280 goto done;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281 if (*cp++ == '\n')
1282 SCREWUP("unexpected <newline>");
1283 do {
Damien Millere247cc42000-05-07 12:03:14 +10001284 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285 SCREWUP("lost connection");
1286 *cp++ = ch;
1287 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
1288 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001289 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001290 fmprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291
1292 if (buf[0] == '\01' || buf[0] == '\02') {
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001293 if (iamremote == 0) {
1294 (void) snmprintf(visbuf, sizeof(visbuf),
1295 NULL, "%s", buf + 1);
Darren Tucker9f63f222003-07-03 13:46:56 +10001296 (void) atomicio(vwrite, STDERR_FILENO,
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001297 visbuf, strlen(visbuf));
1298 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299 if (buf[0] == '\02')
1300 exit(1);
1301 ++errs;
1302 continue;
1303 }
1304 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +10001305 (void) atomicio(vwrite, remout, "", 1);
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001306 goto done;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308 if (ch == '\n')
1309 *--cp = 0;
1310
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311 cp = buf;
1312 if (*cp == 'T') {
1313 setimes++;
1314 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +10001315 if (!isdigit((unsigned char)*cp))
1316 SCREWUP("mtime.sec not present");
1317 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001318 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319 SCREWUP("mtime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001320 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001321 setimes = 0; /* out of range */
1322 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001323 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001324 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1325 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +10001327 if (!isdigit((unsigned char)*cp))
1328 SCREWUP("atime.sec not present");
1329 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001330 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331 SCREWUP("atime.sec not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001332 if (TYPE_OVERFLOW(time_t, ull))
Damien Miller044bd2a2013-07-18 16:09:25 +10001333 setimes = 0; /* out of range */
1334 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001335 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001336 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1337 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001339 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340 continue;
1341 }
1342 if (*cp != 'C' && *cp != 'D') {
1343 /*
1344 * Check for the case "rcp remote:foo\* local:bar".
1345 * In this case, the line "No match." can be returned
1346 * by the shell before the rcp command on the remote is
1347 * executed so the ^Aerror_message convention isn't
1348 * followed.
1349 */
1350 if (first) {
1351 run_err("%s", cp);
1352 exit(1);
1353 }
1354 SCREWUP("expected control record");
1355 }
1356 mode = 0;
1357 for (++cp; cp < buf + 5; cp++) {
1358 if (*cp < '0' || *cp > '7')
1359 SCREWUP("bad mode");
1360 mode = (mode << 3) | (*cp - '0');
1361 }
dtucker@openbsd.orge5019da2018-06-01 04:31:48 +00001362 if (!pflag)
1363 mode &= ~mask;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364 if (*cp++ != ' ')
1365 SCREWUP("mode not delimited");
1366
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001367 if (!isdigit((unsigned char)*cp))
1368 SCREWUP("size not present");
1369 ull = strtoull(cp, &cp, 10);
1370 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371 SCREWUP("size not delimited");
millert@openbsd.org06643712017-04-28 03:21:12 +00001372 if (TYPE_OVERFLOW(off_t, ull))
millert@openbsd.org91bd2182017-04-27 11:53:12 +00001373 SCREWUP("size out of range");
1374 size = (off_t)ull;
1375
djm@openbsd.org6010c032018-11-16 03:03:10 +00001376 if (*cp == '\0' || strchr(cp, '/') != NULL ||
1377 strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0) {
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001378 run_err("error: unexpected filename: %s", cp);
1379 exit(1);
1380 }
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001381 if (npatterns > 0) {
1382 for (n = 0; n < npatterns; n++) {
1383 if (fnmatch(patterns[n], cp, 0) == 0)
1384 break;
1385 }
1386 if (n >= npatterns)
1387 SCREWUP("filename does not match request");
1388 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389 if (targisdir) {
1390 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001391 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001392 size_t need;
1393
1394 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001395 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001396 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001397 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001398 cursize = need;
1399 }
1400 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001401 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402 np = namebuf;
1403 } else
1404 np = targ;
1405 curfile = cp;
1406 exists = stat(np, &stb) == 0;
1407 if (buf[0] == 'D') {
1408 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001409 if (!iamrecursive)
1410 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411 if (exists) {
1412 if (!S_ISDIR(stb.st_mode)) {
1413 errno = ENOTDIR;
1414 goto bad;
1415 }
1416 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001417 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418 } else {
Damien Miller95def091999-11-25 00:26:21 +11001419 /* Handle copying from a read-only
1420 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001421 mod_flag = 1;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001422 if (mkdir(np, mode | S_IRWXU) == -1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423 goto bad;
1424 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001425 vect[0] = xstrdup(np);
djm@openbsd.org391ffc42019-01-26 22:41:28 +00001426 sink(1, vect, src);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001427 if (setimes) {
1428 setimes = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001429 if (utimes(vect[0], tv) == -1)
Damien Miller95def091999-11-25 00:26:21 +11001430 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001431 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432 }
1433 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001434 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001435 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001436 continue;
1437 }
1438 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001439 mode |= S_IWUSR;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001440 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001441bad: run_err("%s: %s", np, strerror(errno));
1442 continue;
1443 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001444 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001445 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001446 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001447 continue;
1448 }
1449 cp = bp->buf;
1450 wrerr = NO;
1451
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001453 if (showprogress)
1454 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001455 set_nonblock(remin);
1456 for (count = i = 0; i < size; i += bp->cnt) {
1457 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458 if (i + amt > size)
1459 amt = size - i;
1460 count += amt;
1461 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001462 j = atomicio6(read, remin, cp, amt,
1463 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001464 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001465 run_err("%s", j != EPIPE ?
1466 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001467 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001468 exit(1);
1469 }
1470 amt -= j;
1471 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001473
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474 if (count == bp->cnt) {
1475 /* Keep reading so we stay sync'd up. */
1476 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001477 if (atomicio(vwrite, ofd, bp->buf,
1478 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001480 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001481 }
1482 }
1483 count = 0;
1484 cp = bp->buf;
1485 }
1486 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001487 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001489 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001491 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492 }
Darren Tucker067263e2007-06-25 18:32:33 +10001493 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1494 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495 run_err("%s: truncate: %s", np, strerror(errno));
1496 wrerr = DISPLAYED;
1497 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498 if (pflag) {
1499 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001500#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001501 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001502#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001503 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001504#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001505 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001506 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001507 wrerr = DISPLAYED;
1508 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001509 } else {
1510 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001511#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001512 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001513#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001514 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001515#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001517 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001518 wrerr = DISPLAYED;
1519 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520 }
Damien Millerbe484b52000-07-15 14:14:16 +10001521 if (close(ofd) == -1) {
1522 wrerr = YES;
1523 wrerrno = errno;
1524 }
Damien Miller95def091999-11-25 00:26:21 +11001525 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001526 if (showprogress)
1527 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528 if (setimes && wrerr == NO) {
1529 setimes = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001530 if (utimes(np, tv) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001531 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001532 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533 wrerr = DISPLAYED;
1534 }
1535 }
Damien Miller95def091999-11-25 00:26:21 +11001536 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537 case YES:
1538 run_err("%s: %s", np, strerror(wrerrno));
1539 break;
1540 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001541 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001542 break;
1543 case DISPLAYED:
1544 break;
1545 }
1546 }
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001547done:
1548 for (n = 0; n < npatterns; n++)
1549 free(patterns[n]);
1550 free(patterns);
1551 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001552screwup:
djm@openbsd.org3d896c12019-02-10 11:15:52 +00001553 for (n = 0; n < npatterns; n++)
1554 free(patterns[n]);
1555 free(patterns);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556 run_err("protocol error: %s", why);
1557 exit(1);
1558}
1559
1560int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001561response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562{
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001563 char ch, *cp, resp, rbuf[2048], visbuf[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564
Damien Millere247cc42000-05-07 12:03:14 +10001565 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566 lostconn(0);
1567
1568 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001569 switch (resp) {
1570 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001571 return (0);
1572 default:
1573 *cp++ = resp;
1574 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001575 case 1: /* error, followed by error msg */
1576 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001577 do {
Damien Millere247cc42000-05-07 12:03:14 +10001578 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001579 lostconn(0);
1580 *cp++ = ch;
1581 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1582
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001583 if (!iamremote) {
1584 cp[-1] = '\0';
1585 (void) snmprintf(visbuf, sizeof(visbuf),
1586 NULL, "%s\n", rbuf);
1587 (void) atomicio(vwrite, STDERR_FILENO,
1588 visbuf, strlen(visbuf));
1589 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001590 ++errs;
1591 if (resp == 1)
1592 return (-1);
1593 exit(1);
1594 }
1595 /* NOTREACHED */
1596}
1597
1598void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001599usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001601 (void) fprintf(stderr,
jmc@openbsd.org2c21b752019-01-27 07:14:11 +00001602 "usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
tb@openbsd.org622dedf2019-01-21 22:50:42 +00001603 " [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
1604 " [-S program] source ... target\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001605 exit(1);
1606}
1607
1608void
Damien Miller95def091999-11-25 00:26:21 +11001609run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001610{
1611 static FILE *fp;
1612 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613
1614 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001615 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1616 (void) fprintf(fp, "%c", 0x01);
1617 (void) fprintf(fp, "scp: ");
1618 va_start(ap, fmt);
1619 (void) vfprintf(fp, fmt, ap);
1620 va_end(ap);
1621 (void) fprintf(fp, "\n");
1622 (void) fflush(fp);
1623 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624
Damien Miller95def091999-11-25 00:26:21 +11001625 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001626 va_start(ap, fmt);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001627 vfmprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001628 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001629 fprintf(stderr, "\n");
1630 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001631}
1632
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001633void
Damien Miller65d1f572003-06-04 22:51:08 +10001634verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635{
1636 struct stat stb;
1637
1638 if (!stat(cp, &stb)) {
1639 if (S_ISDIR(stb.st_mode))
1640 return;
1641 errno = ENOTDIR;
1642 }
1643 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001644 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001645}
1646
1647int
Damien Miller65d1f572003-06-04 22:51:08 +10001648okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001649{
1650 int c;
1651 char *cp;
1652
1653 cp = cp0;
1654 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001655 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001656 if (c & 0200)
1657 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001658 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001659 switch (c) {
1660 case '\'':
1661 case '"':
1662 case '`':
1663 case ' ':
1664 case '#':
1665 goto bad;
1666 default:
1667 break;
1668 }
1669 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001670 } while (*++cp);
1671 return (1);
1672
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001673bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001674 return (0);
1675}
1676
1677BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001678allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679{
1680 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001681#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001682 struct stat stb;
1683
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001684 if (fstat(fd, &stb) == -1) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001685 run_err("fstat: %s", strerror(errno));
1686 return (0);
1687 }
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001688 size = ROUNDUP(stb.st_blksize, blksize);
Ben Lindstrom418e0782002-12-23 02:22:09 +00001689 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001690 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001691#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001692 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001693#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001694 if (bp->cnt >= size)
1695 return (bp);
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001696 bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001697 bp->cnt = size;
1698 return (bp);
1699}
1700
1701void
Damien Miller65d1f572003-06-04 22:51:08 +10001702lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703{
1704 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001705 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001706 if (signo)
1707 _exit(1);
1708 else
1709 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001710}