blob: 593fe89bded2c812e68367ec5024678384cc5013 [file] [log] [blame]
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001/* $OpenBSD: scp.c,v 1.182 2015/04/24 01:36:00 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 Miller8dbffe72006-08-05 11:02:17 +100077#include <sys/param.h>
Damien Millerf17883e2006-03-15 11:45:54 +110078#ifdef HAVE_SYS_STAT_H
79# include <sys/stat.h>
80#endif
Tim Rice3eb14492008-03-13 17:59:50 -070081#ifdef HAVE_POLL_H
82#include <poll.h>
83#else
84# ifdef HAVE_SYS_POLL_H
85# include <sys/poll.h>
86# endif
Darren Tuckerccb13ee2008-02-29 15:07:01 +110087#endif
Damien Miller9aec9192006-08-05 10:57:45 +100088#ifdef HAVE_SYS_TIME_H
89# include <sys/time.h>
90#endif
Damien Miller9cf6d072006-03-15 11:29:24 +110091#include <sys/wait.h>
Damien Millerd7834352006-08-05 12:39:39 +100092#include <sys/uio.h>
Damien Miller88f254b2006-03-15 11:25:13 +110093
Damien Millerc7b06362006-03-15 11:53:45 +110094#include <ctype.h>
Damien Miller88f254b2006-03-15 11:25:13 +110095#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100096#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100097#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000098#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100099#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +1100100#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +1000101#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +1000102#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +1000103#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +1000104#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +1000105#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +1000106#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +1100107#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Darren Tuckerbed63112007-06-13 00:02:07 +1000108#include <vis.h>
109#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000112#include "atomicio.h"
113#include "pathnames.h"
114#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +0000115#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +1100116#include "progressmeter.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000118extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000119
Damien Millerc77cadb2007-10-26 15:39:15 +1000120#define COPY_BUFLEN 16384
121
Damien Miller07aa1322006-04-23 12:04:27 +1000122int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
Damien Millerf1211432011-01-06 22:40:30 +1100123int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000124
Ben Lindstrom387c4722001-05-08 20:27:25 +0000125/* Struct for addargs */
126arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100127arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100128
Damien Millerff74d742003-01-24 11:36:58 +1100129/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000130long long limit_kbps = 0;
131struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100132
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133/* Name of current file being transferred. */
134char *curfile;
135
136/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100137int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139/* This is set to zero if the progressmeter is not desired. */
140int showprogress = 1;
141
Damien Millerf1211432011-01-06 22:40:30 +1100142/*
143 * This is set to non-zero if remote-remote copy should be piped
144 * through this process.
145 */
146int throughlocal = 0;
147
Damien Millerad833b32000-08-23 10:46:23 +1000148/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100149char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000150
Ben Lindstrom007eb912002-11-09 15:54:08 +0000151/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000152pid_t do_cmd_pid = -1;
153
154static void
155killchild(int signo)
156{
Darren Tuckerba66df82005-01-24 21:57:40 +1100157 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000158 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100159 waitpid(do_cmd_pid, NULL, 0);
160 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000161
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000162 if (signo)
163 _exit(1);
164 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000165}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000166
Damien Miller0979b402010-07-02 13:37:33 +1000167static void
168suspchild(int signo)
169{
170 int status;
171
172 if (do_cmd_pid > 1) {
173 kill(do_cmd_pid, signo);
174 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
175 errno == EINTR)
176 ;
177 kill(getpid(), SIGSTOP);
178 }
179}
180
Damien Miller3eec6b72006-01-31 21:49:27 +1100181static int
182do_local_cmd(arglist *a)
183{
184 u_int i;
185 int status;
186 pid_t pid;
187
188 if (a->num == 0)
189 fatal("do_local_cmd: no arguments");
190
191 if (verbose_mode) {
192 fprintf(stderr, "Executing:");
193 for (i = 0; i < a->num; i++)
194 fprintf(stderr, " %s", a->list[i]);
195 fprintf(stderr, "\n");
196 }
197 if ((pid = fork()) == -1)
198 fatal("do_local_cmd: fork: %s", strerror(errno));
199
200 if (pid == 0) {
201 execvp(a->list[0], a->list);
202 perror(a->list[0]);
203 exit(1);
204 }
205
206 do_cmd_pid = pid;
207 signal(SIGTERM, killchild);
208 signal(SIGINT, killchild);
209 signal(SIGHUP, killchild);
210
211 while (waitpid(pid, &status, 0) == -1)
212 if (errno != EINTR)
213 fatal("do_local_cmd: waitpid: %s", strerror(errno));
214
215 do_cmd_pid = -1;
216
217 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
218 return (-1);
219
220 return (0);
221}
222
Damien Miller5428f641999-11-25 11:54:57 +1100223/*
224 * This function executes the given command as the specified user on the
225 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
226 * assigns the input and output file descriptors on success.
227 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller4af51302000-04-16 11:18:38 +1000229int
Damien Miller07aa1322006-04-23 12:04:27 +1000230do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231{
Damien Miller95def091999-11-25 00:26:21 +1100232 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233
Damien Miller95def091999-11-25 00:26:21 +1100234 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000235 fprintf(stderr,
236 "Executing: program %s host %s, user %s, command %s\n",
237 ssh_program, host,
238 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller5428f641999-11-25 11:54:57 +1100240 /*
241 * Reserve two descriptors so that the real pipes won't get
242 * descriptors 0 and 1 because that will screw up dup2 below.
243 */
Damien Miller40b59852006-06-13 13:00:25 +1000244 if (pipe(reserved) < 0)
245 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller95def091999-11-25 00:26:21 +1100247 /* Create a socket pair for communicating with ssh. */
248 if (pipe(pin) < 0)
249 fatal("pipe: %s", strerror(errno));
250 if (pipe(pout) < 0)
251 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 /* Free the reserved descriptors. */
254 close(reserved[0]);
255 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller0979b402010-07-02 13:37:33 +1000257 signal(SIGTSTP, suspchild);
258 signal(SIGTTIN, suspchild);
259 signal(SIGTTOU, suspchild);
260
Damien Millerb2cdcb52003-06-18 20:26:34 +1000261 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000262 do_cmd_pid = fork();
263 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Child. */
265 close(pin[1]);
266 close(pout[0]);
267 dup2(pin[0], 0);
268 dup2(pout[1], 1);
269 close(pin[0]);
270 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller3eec6b72006-01-31 21:49:27 +1100272 replacearg(&args, 0, "%s", ssh_program);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100273 if (remuser != NULL) {
274 addargs(&args, "-l");
275 addargs(&args, "%s", remuser);
276 }
277 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000278 addargs(&args, "%s", host);
279 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100280
Damien Miller874d77b2000-10-14 16:23:11 +1100281 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000282 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100283 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000284 } else if (do_cmd_pid == -1) {
285 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286 }
Damien Miller95def091999-11-25 00:26:21 +1100287 /* Parent. Close the other side, and return the local side. */
288 close(pin[0]);
289 *fdout = pin[1];
290 close(pout[1]);
291 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000292 signal(SIGTERM, killchild);
293 signal(SIGINT, killchild);
294 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100295 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296}
297
Damien Millerf1211432011-01-06 22:40:30 +1100298/*
299 * This functions executes a command simlar to do_cmd(), but expects the
300 * input and output descriptors to be setup by a previous call to do_cmd().
301 * This way the input and output of two commands can be connected.
302 */
303int
304do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout)
305{
306 pid_t pid;
307 int status;
308
309 if (verbose_mode)
310 fprintf(stderr,
311 "Executing: 2nd program %s host %s, user %s, command %s\n",
312 ssh_program, host,
313 remuser ? remuser : "(unspecified)", cmd);
314
315 /* Fork a child to execute the command on the remote host using ssh. */
316 pid = fork();
317 if (pid == 0) {
318 dup2(fdin, 0);
319 dup2(fdout, 1);
320
321 replacearg(&args, 0, "%s", ssh_program);
322 if (remuser != NULL) {
323 addargs(&args, "-l");
324 addargs(&args, "%s", remuser);
325 }
326 addargs(&args, "--");
327 addargs(&args, "%s", host);
328 addargs(&args, "%s", cmd);
329
330 execvp(ssh_program, args.list);
331 perror(ssh_program);
332 exit(1);
333 } else if (pid == -1) {
334 fatal("fork: %s", strerror(errno));
335 }
336 while (waitpid(pid, &status, 0) == -1)
337 if (errno != EINTR)
338 fatal("do_cmd2: waitpid: %s", strerror(errno));
339 return 0;
340}
341
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000343 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344 char *buf;
345} BUF;
346
Damien Miller95def091999-11-25 00:26:21 +1100347BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100348void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100349int okname(char *);
350void run_err(const char *,...);
351void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100354uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355int errs, remin, remout;
356int pflag, iamremote, iamrecursive, targetshouldbedirectory;
357
358#define CMDNEEDS 64
359char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
360
Damien Miller95def091999-11-25 00:26:21 +1100361int response(void);
362void rsource(char *, struct stat *);
363void sink(int, char *[]);
364void source(int, char *[]);
365void tolocal(int, char *[]);
366void toremote(char *, int, char *[]);
367void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
369int
Damien Miller65d1f572003-06-04 22:51:08 +1000370main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000372 int ch, fflag, tflag, status, n;
Damien Miller65e42f82010-09-24 22:15:11 +1000373 char *targ, **newargv;
374 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375 extern char *optarg;
376 extern int optind;
377
Darren Tuckerce321d82005-10-03 18:11:24 +1000378 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
379 sanitise_stdfd();
380
Damien Miller3d1a9f42006-07-10 22:19:53 +1000381 /* Copy argv, because we modify it */
382 newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv));
383 for (n = 0; n < argc; n++)
384 newargv[n] = xstrdup(argv[n]);
385 argv = newargv;
386
Damien Miller59d3d5b2003-08-22 09:34:41 +1000387 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000388
Damien Miller3eec6b72006-01-31 21:49:27 +1100389 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100390 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
391 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100392 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000393 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100394 addargs(&args, "-oForwardAgent=no");
395 addargs(&args, "-oPermitLocalCommand=no");
396 addargs(&args, "-oClearAllForwardings=yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100397
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398 fflag = tflag = 0;
Damien Millerf1211432011-01-06 22:40:30 +1100399 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100400 switch (ch) {
401 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100402 case '1':
403 case '2':
Damien Miller34132e52000-01-14 15:45:46 +1100404 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100405 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100406 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000407 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100408 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100409 break;
Damien Millerf1211432011-01-06 22:40:30 +1100410 case '3':
411 throughlocal = 1;
412 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100413 case 'o':
414 case 'c':
415 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000416 case 'F':
Damien Miller87dc0a42010-12-01 12:03:19 +1100417 addargs(&remote_remote_args, "-%c", ch);
418 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100419 addargs(&args, "-%c", ch);
420 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100421 break;
422 case 'P':
Damien Miller87dc0a42010-12-01 12:03:19 +1100423 addargs(&remote_remote_args, "-p");
424 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100425 addargs(&args, "-p");
426 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100427 break;
428 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100429 addargs(&remote_remote_args, "-oBatchmode=yes");
430 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100431 break;
Damien Millerff74d742003-01-24 11:36:58 +1100432 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000433 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
434 &errstr);
435 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100436 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000437 limit_kbps *= 1024; /* kbps */
438 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100439 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440 case 'p':
441 pflag = 1;
442 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 case 'r':
444 iamrecursive = 1;
445 break;
Damien Millerad833b32000-08-23 10:46:23 +1000446 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100447 ssh_program = xstrdup(optarg);
448 break;
449 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000450 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100451 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100452 verbose_mode = 1;
453 break;
454 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100455 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100456 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100457 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000458 break;
459
Damien Miller95def091999-11-25 00:26:21 +1100460 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461 case 'd':
462 targetshouldbedirectory = 1;
463 break;
Damien Miller95def091999-11-25 00:26:21 +1100464 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465 iamremote = 1;
466 fflag = 1;
467 break;
Damien Miller95def091999-11-25 00:26:21 +1100468 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000469 iamremote = 1;
470 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000471#ifdef HAVE_CYGWIN
472 setmode(0, O_BINARY);
473#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 default:
476 usage();
477 }
478 argc -= optind;
479 argv += optind;
480
481 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000482 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100484 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000485 showprogress = 0;
486
487 remin = STDIN_FILENO;
488 remout = STDOUT_FILENO;
489
Kevin Stevesef4eea92001-02-05 12:42:17 +0000490 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100491 /* Follow "protocol", send data. */
492 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493 source(argc, argv);
494 exit(errs != 0);
495 }
Damien Miller95def091999-11-25 00:26:21 +1100496 if (tflag) {
497 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498 sink(argc, argv);
499 exit(errs != 0);
500 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501 if (argc < 2)
502 usage();
503 if (argc > 2)
504 targetshouldbedirectory = 1;
505
506 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000507 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000509 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
510 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000511 iamrecursive ? " -r" : "", pflag ? " -p" : "",
512 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000513
Damien Miller95def091999-11-25 00:26:21 +1100514 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515
516 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
517 toremote(targ, argc, argv);
518 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519 if (targetshouldbedirectory)
520 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100521 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000523 /*
524 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100525 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000526 */
527 if (do_cmd_pid != -1 && errs == 0) {
528 if (remin != -1)
529 (void) close(remin);
530 if (remout != -1)
531 (void) close(remout);
532 if (waitpid(do_cmd_pid, &status, 0) == -1)
533 errs = 1;
534 else {
535 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
536 errs = 1;
537 }
538 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539 exit(errs != 0);
540}
541
Damien Miller65e42f82010-09-24 22:15:11 +1000542/* Callback from atomicio6 to update progress meter and limit bandwidth */
543static int
544scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000545{
Damien Miller65e42f82010-09-24 22:15:11 +1000546 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000547
Damien Miller65e42f82010-09-24 22:15:11 +1000548 *cnt += s;
549 if (limit_kbps > 0)
550 bandwidth_limit(&bwlimit, s);
551 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000552}
553
Damien Miller044bd2a2013-07-18 16:09:25 +1000554static int
555do_times(int fd, int verb, const struct stat *sb)
556{
557 /* strlen(2^64) == 20; strlen(10^6) == 7 */
558 char buf[(20 + 7 + 2) * 2 + 2];
559
560 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
561 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
562 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
563 if (verb) {
564 fprintf(stderr, "File mtime %lld atime %lld\n",
565 (long long)sb->st_mtime, (long long)sb->st_atime);
566 fprintf(stderr, "Sending file timestamps: %s", buf);
567 }
568 (void) atomicio(vwrite, fd, buf, strlen(buf));
569 return (response());
570}
571
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000572void
Damien Miller65d1f572003-06-04 22:51:08 +1000573toremote(char *targ, int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000574{
Damien Miller3dae15c2005-04-03 10:16:11 +1000575 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
Damien Miller3eec6b72006-01-31 21:49:27 +1100576 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000577 int i;
Damien Miller87dc0a42010-12-01 12:03:19 +1100578 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100579
580 memset(&alist, '\0', sizeof(alist));
581 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582
583 *targ++ = 0;
584 if (*targ == 0)
585 targ = ".";
586
Damien Miller4942de52005-04-03 10:16:39 +1000587 arg = xstrdup(argv[argc - 1]);
Damien Miller3dae15c2005-04-03 10:16:11 +1000588 if ((thost = strrchr(arg, '@'))) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000589 /* user@host */
590 *thost++ = 0;
Damien Miller3dae15c2005-04-03 10:16:11 +1000591 tuser = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000592 if (*tuser == '\0')
593 tuser = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000594 } else {
Damien Miller3dae15c2005-04-03 10:16:11 +1000595 thost = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596 tuser = NULL;
597 }
598
Damien Miller3eec6b72006-01-31 21:49:27 +1100599 if (tuser != NULL && !okname(tuser)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000600 free(arg);
Damien Miller3eec6b72006-01-31 21:49:27 +1100601 return;
602 }
603
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000604 for (i = 0; i < argc - 1; i++) {
605 src = colon(argv[i]);
Damien Millerf1211432011-01-06 22:40:30 +1100606 if (src && throughlocal) { /* extended remote to remote */
607 *src++ = 0;
608 if (*src == 0)
609 src = ".";
610 host = strrchr(argv[i], '@');
611 if (host) {
612 *host++ = 0;
613 host = cleanhostname(host);
614 suser = argv[i];
615 if (*suser == '\0')
616 suser = pwd->pw_name;
617 else if (!okname(suser))
618 continue;
619 } else {
620 host = cleanhostname(argv[i]);
621 suser = NULL;
622 }
Damien Miller0603d982011-09-22 21:38:00 +1000623 xasprintf(&bp, "%s -f %s%s", cmd,
624 *src == '-' ? "-- " : "", src);
Damien Millerf1211432011-01-06 22:40:30 +1100625 if (do_cmd(host, suser, bp, &remin, &remout) < 0)
626 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000627 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100628 host = cleanhostname(thost);
Damien Miller0603d982011-09-22 21:38:00 +1000629 xasprintf(&bp, "%s -t %s%s", cmd,
630 *targ == '-' ? "-- " : "", targ);
Damien Millerf1211432011-01-06 22:40:30 +1100631 if (do_cmd2(host, tuser, bp, remin, remout) < 0)
632 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000633 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100634 (void) close(remin);
635 (void) close(remout);
636 remin = remout = -1;
637 } else if (src) { /* standard remote to remote */
Damien Miller3eec6b72006-01-31 21:49:27 +1100638 freeargs(&alist);
639 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100640 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100641 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100642 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100643 for (j = 0; j < remote_remote_args.num; j++) {
644 addargs(&alist, "%s",
645 remote_remote_args.list[j]);
646 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000647 *src++ = 0;
648 if (*src == 0)
649 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000650 host = strrchr(argv[i], '@');
Damien Miller3eec6b72006-01-31 21:49:27 +1100651
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000652 if (host) {
653 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100654 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655 suser = argv[i];
656 if (*suser == '\0')
657 suser = pwd->pw_name;
Damien Miller3eec6b72006-01-31 21:49:27 +1100658 else if (!okname(suser))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659 continue;
Damien Miller3eec6b72006-01-31 21:49:27 +1100660 addargs(&alist, "-l");
661 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100662 } else {
663 host = cleanhostname(argv[i]);
Damien Miller34132e52000-01-14 15:45:46 +1100664 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100665 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100666 addargs(&alist, "%s", host);
667 addargs(&alist, "%s", cmd);
668 addargs(&alist, "%s", src);
669 addargs(&alist, "%s%s%s:%s",
670 tuser ? tuser : "", tuser ? "@" : "",
671 thost, targ);
672 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000673 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100674 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000676 xasprintf(&bp, "%s -t %s%s", cmd,
677 *targ == '-' ? "-- " : "", targ);
Damien Miller34132e52000-01-14 15:45:46 +1100678 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000679 if (do_cmd(host, tuser, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000680 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100681 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682 if (response() < 0)
683 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000684 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685 }
Damien Miller95def091999-11-25 00:26:21 +1100686 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000687 }
688 }
Darren Tuckera627d422013-06-02 07:31:17 +1000689 free(arg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000690}
691
692void
Damien Miller65d1f572003-06-04 22:51:08 +1000693tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694{
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695 char *bp, *host, *src, *suser;
Damien Miller3eec6b72006-01-31 21:49:27 +1100696 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000697 int i;
Damien Miller3eec6b72006-01-31 21:49:27 +1100698
699 memset(&alist, '\0', sizeof(alist));
700 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000701
702 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100703 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +1100704 freeargs(&alist);
705 addargs(&alist, "%s", _PATH_CP);
706 if (iamrecursive)
707 addargs(&alist, "-r");
708 if (pflag)
709 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100710 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100711 addargs(&alist, "%s", argv[i]);
712 addargs(&alist, "%s", argv[argc-1]);
713 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000715 continue;
716 }
717 *src++ = 0;
718 if (*src == 0)
719 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000720 if ((host = strrchr(argv[i], '@')) == NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721 host = argv[i];
722 suser = NULL;
723 } else {
724 *host++ = 0;
725 suser = argv[i];
726 if (*suser == '\0')
727 suser = pwd->pw_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 }
Damien Miller34132e52000-01-14 15:45:46 +1100729 host = cleanhostname(host);
Damien Miller0603d982011-09-22 21:38:00 +1000730 xasprintf(&bp, "%s -f %s%s",
731 cmd, *src == '-' ? "-- " : "", src);
Damien Miller07aa1322006-04-23 12:04:27 +1000732 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000733 free(bp);
Damien Miller95def091999-11-25 00:26:21 +1100734 ++errs;
735 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736 }
Darren Tuckera627d422013-06-02 07:31:17 +1000737 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000738 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100739 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000740 remin = remout = -1;
741 }
742}
743
744void
Damien Miller65d1f572003-06-04 22:51:08 +1000745source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000746{
747 struct stat stb;
748 static BUF buffer;
749 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000750 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +1000751 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +1000752 int fd = -1, haderr, indx;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000753 char *last, *name, buf[2048], encname[PATH_MAX];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000754 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000755
756 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100757 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000758 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000759 len = strlen(name);
760 while (len > 1 && name[len-1] == '/')
761 name[--len] = '\0';
Darren Tucker43ce9022007-06-12 23:41:06 +1000762 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +1000764 if (strchr(name, '\n') != NULL) {
765 strnvis(encname, name, sizeof(encname), VIS_NL);
766 name = encname;
767 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000768 if (fstat(fd, &stb) < 0) {
769syserr: run_err("%s: %s", name, strerror(errno));
770 goto next;
771 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000772 if (stb.st_size < 0) {
773 run_err("%s: %s", name, "Negative file size");
774 goto next;
775 }
Darren Tucker43ce9022007-06-12 23:41:06 +1000776 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777 switch (stb.st_mode & S_IFMT) {
778 case S_IFREG:
779 break;
780 case S_IFDIR:
781 if (iamrecursive) {
782 rsource(name, &stb);
783 goto next;
784 }
785 /* FALLTHROUGH */
786 default:
787 run_err("%s: not a regular file", name);
788 goto next;
789 }
790 if ((last = strrchr(name, '/')) == NULL)
791 last = name;
792 else
793 ++last;
794 curfile = last;
795 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000796 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797 goto next;
798 }
799#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +0000800 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000801 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +1100802 (long long)stb.st_size, last);
Damien Miller95def091999-11-25 00:26:21 +1100803 if (verbose_mode) {
804 fprintf(stderr, "Sending file modes: %s", buf);
Damien Miller95def091999-11-25 00:26:21 +1100805 }
Darren Tucker9f63f222003-07-03 13:46:56 +1000806 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807 if (response() < 0)
808 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +1000809 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +1100810next: if (fd != -1) {
811 (void) close(fd);
812 fd = -1;
813 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814 continue;
815 }
Damien Miller62d57f62003-01-10 21:43:24 +1100816 if (showprogress)
817 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +1000818 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
820 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000821 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822 amt = stb.st_size - i;
823 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +1000824 if ((nr = atomicio(read, fd,
825 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +1000826 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +1000827 memset(bp->buf + nr, 0, amt - nr);
828 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000830 /* Keep writing after error to retain sync */
831 if (haderr) {
832 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +1000833 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +1000834 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000835 }
Damien Miller65e42f82010-09-24 22:15:11 +1000836 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +1000837 &statbytes) != amt)
838 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000839 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000840 unset_nonblock(remout);
Damien Miller95def091999-11-25 00:26:21 +1100841 if (showprogress)
Damien Miller62d57f62003-01-10 21:43:24 +1100842 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843
Darren Tucker33f86bc2005-11-22 19:38:06 +1100844 if (fd != -1) {
845 if (close(fd) < 0 && !haderr)
846 haderr = errno;
847 fd = -1;
848 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +1000850 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851 else
852 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100853 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854 }
855}
856
857void
Damien Miller65d1f572003-06-04 22:51:08 +1000858rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859{
860 DIR *dirp;
861 struct dirent *dp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000862 char *last, *vect[1], path[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863
864 if (!(dirp = opendir(name))) {
865 run_err("%s: %s", name, strerror(errno));
866 return;
867 }
868 last = strrchr(name, '/');
869 if (last == 0)
870 last = name;
871 else
872 last++;
873 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000874 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 closedir(dirp);
876 return;
877 }
878 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000879 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000880 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100881 if (verbose_mode)
882 fprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +1000883 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884 if (response() < 0) {
885 closedir(dirp);
886 return;
887 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000888 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889 if (dp->d_ino == 0)
890 continue;
891 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
892 continue;
893 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
894 run_err("%s/%s: name too long", name, dp->d_name);
895 continue;
896 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000897 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000898 vect[0] = path;
899 source(1, vect);
900 }
Damien Miller95def091999-11-25 00:26:21 +1100901 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +1000902 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100903 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904}
905
906void
Damien Miller65d1f572003-06-04 22:51:08 +1000907sink(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908{
909 static BUF buffer;
910 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100911 enum {
912 YES, NO, DISPLAYED
913 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +1000915 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +1000916 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +1000917 int amt, exists, first, ofd;
918 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +1100919 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +1000920 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +1100921 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000922 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100923 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000924
Ben Lindstromf719a202001-04-14 23:14:22 +0000925#define atime tv[0]
926#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +1100927#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928
929 setimes = targisdir = 0;
930 mask = umask(0);
931 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100932 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933 if (argc != 1) {
934 run_err("ambiguous target");
935 exit(1);
936 }
937 targ = *argv;
938 if (targetshouldbedirectory)
939 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100940
Darren Tucker9f63f222003-07-03 13:46:56 +1000941 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
943 targisdir = 1;
944 for (first = 1;; first = 0) {
945 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +1000946 if (atomicio(read, remin, cp, 1) != 1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947 return;
948 if (*cp++ == '\n')
949 SCREWUP("unexpected <newline>");
950 do {
Damien Millere247cc42000-05-07 12:03:14 +1000951 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952 SCREWUP("lost connection");
953 *cp++ = ch;
954 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
955 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +1000956 if (verbose_mode)
957 fprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958
959 if (buf[0] == '\01' || buf[0] == '\02') {
960 if (iamremote == 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000961 (void) atomicio(vwrite, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000962 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963 if (buf[0] == '\02')
964 exit(1);
965 ++errs;
966 continue;
967 }
968 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +1000969 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970 return;
971 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972 if (ch == '\n')
973 *--cp = 0;
974
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975 cp = buf;
976 if (*cp == 'T') {
977 setimes++;
978 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +1000979 if (!isdigit((unsigned char)*cp))
980 SCREWUP("mtime.sec not present");
981 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +0000982 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983 SCREWUP("mtime.sec not delimited");
Damien Millerbc35d922013-07-18 16:11:25 +1000984 if ((time_t)ull < 0 ||
985 (unsigned long long)(time_t)ull != ull)
Damien Miller044bd2a2013-07-18 16:09:25 +1000986 setimes = 0; /* out of range */
987 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +0000988 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +1000989 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
990 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +1000992 if (!isdigit((unsigned char)*cp))
993 SCREWUP("atime.sec not present");
994 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +0000995 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996 SCREWUP("atime.sec not delimited");
Damien Millerbc35d922013-07-18 16:11:25 +1000997 if ((time_t)ull < 0 ||
998 (unsigned long long)(time_t)ull != ull)
Damien Miller044bd2a2013-07-18 16:09:25 +1000999 setimes = 0; /* out of range */
1000 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001001 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001002 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1003 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001005 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006 continue;
1007 }
1008 if (*cp != 'C' && *cp != 'D') {
1009 /*
1010 * Check for the case "rcp remote:foo\* local:bar".
1011 * In this case, the line "No match." can be returned
1012 * by the shell before the rcp command on the remote is
1013 * executed so the ^Aerror_message convention isn't
1014 * followed.
1015 */
1016 if (first) {
1017 run_err("%s", cp);
1018 exit(1);
1019 }
1020 SCREWUP("expected control record");
1021 }
1022 mode = 0;
1023 for (++cp; cp < buf + 5; cp++) {
1024 if (*cp < '0' || *cp > '7')
1025 SCREWUP("bad mode");
1026 mode = (mode << 3) | (*cp - '0');
1027 }
1028 if (*cp++ != ' ')
1029 SCREWUP("mode not delimited");
1030
Damien Millere0016732013-11-21 13:56:49 +11001031 for (size = 0; isdigit((unsigned char)*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001032 size = size * 10 + (*cp++ - '0');
1033 if (*cp++ != ' ')
1034 SCREWUP("size not delimited");
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001035 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
1036 run_err("error: unexpected filename: %s", cp);
1037 exit(1);
1038 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001039 if (targisdir) {
1040 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001041 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001042 size_t need;
1043
1044 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001045 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001046 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001047 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001048 cursize = need;
1049 }
1050 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001051 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001052 np = namebuf;
1053 } else
1054 np = targ;
1055 curfile = cp;
1056 exists = stat(np, &stb) == 0;
1057 if (buf[0] == 'D') {
1058 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001059 if (!iamrecursive)
1060 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061 if (exists) {
1062 if (!S_ISDIR(stb.st_mode)) {
1063 errno = ENOTDIR;
1064 goto bad;
1065 }
1066 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001067 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068 } else {
Damien Miller95def091999-11-25 00:26:21 +11001069 /* Handle copying from a read-only
1070 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071 mod_flag = 1;
1072 if (mkdir(np, mode | S_IRWXU) < 0)
1073 goto bad;
1074 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001075 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076 sink(1, vect);
1077 if (setimes) {
1078 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +11001079 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001080 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001081 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001082 }
1083 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001084 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001085 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086 continue;
1087 }
1088 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001089 mode |= S_IWUSR;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +00001090 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091bad: run_err("%s: %s", np, strerror(errno));
1092 continue;
1093 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001094 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001095 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001096 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097 continue;
1098 }
1099 cp = bp->buf;
1100 wrerr = NO;
1101
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001103 if (showprogress)
1104 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001105 set_nonblock(remin);
1106 for (count = i = 0; i < size; i += bp->cnt) {
1107 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108 if (i + amt > size)
1109 amt = size - i;
1110 count += amt;
1111 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001112 j = atomicio6(read, remin, cp, amt,
1113 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001114 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001115 run_err("%s", j != EPIPE ?
1116 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001117 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118 exit(1);
1119 }
1120 amt -= j;
1121 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001123
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124 if (count == bp->cnt) {
1125 /* Keep reading so we stay sync'd up. */
1126 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001127 if (atomicio(vwrite, ofd, bp->buf,
1128 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001130 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131 }
1132 }
1133 count = 0;
1134 cp = bp->buf;
1135 }
1136 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001137 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138 if (showprogress)
Damien Miller62d57f62003-01-10 21:43:24 +11001139 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001141 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001143 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144 }
Darren Tucker067263e2007-06-25 18:32:33 +10001145 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1146 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147 run_err("%s: truncate: %s", np, strerror(errno));
1148 wrerr = DISPLAYED;
1149 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001150 if (pflag) {
1151 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001152#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001153 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001154#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001155 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001156#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001158 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001159 wrerr = DISPLAYED;
1160 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161 } else {
1162 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001163#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001164 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001165#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001166 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001167#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001169 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001170 wrerr = DISPLAYED;
1171 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172 }
Damien Millerbe484b52000-07-15 14:14:16 +10001173 if (close(ofd) == -1) {
1174 wrerr = YES;
1175 wrerrno = errno;
1176 }
Damien Miller95def091999-11-25 00:26:21 +11001177 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178 if (setimes && wrerr == NO) {
1179 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001180 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001182 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 wrerr = DISPLAYED;
1184 }
1185 }
Damien Miller95def091999-11-25 00:26:21 +11001186 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001187 case YES:
1188 run_err("%s: %s", np, strerror(wrerrno));
1189 break;
1190 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001191 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 break;
1193 case DISPLAYED:
1194 break;
1195 }
1196 }
1197screwup:
1198 run_err("protocol error: %s", why);
1199 exit(1);
1200}
1201
1202int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001203response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204{
1205 char ch, *cp, resp, rbuf[2048];
1206
Damien Millere247cc42000-05-07 12:03:14 +10001207 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001208 lostconn(0);
1209
1210 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001211 switch (resp) {
1212 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213 return (0);
1214 default:
1215 *cp++ = resp;
1216 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001217 case 1: /* error, followed by error msg */
1218 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219 do {
Damien Millere247cc42000-05-07 12:03:14 +10001220 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221 lostconn(0);
1222 *cp++ = ch;
1223 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1224
1225 if (!iamremote)
Darren Tucker9f63f222003-07-03 13:46:56 +10001226 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227 ++errs;
1228 if (resp == 1)
1229 return (-1);
1230 exit(1);
1231 }
1232 /* NOTREACHED */
1233}
1234
1235void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001236usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001238 (void) fprintf(stderr,
Damien Miller907998d2011-01-06 22:41:21 +11001239 "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
Darren Tucker1f203942003-10-15 15:50:42 +10001240 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
Damien Miller647d97b2007-08-08 14:29:58 +10001241 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242 exit(1);
1243}
1244
1245void
Damien Miller95def091999-11-25 00:26:21 +11001246run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247{
1248 static FILE *fp;
1249 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250
1251 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001252 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1253 (void) fprintf(fp, "%c", 0x01);
1254 (void) fprintf(fp, "scp: ");
1255 va_start(ap, fmt);
1256 (void) vfprintf(fp, fmt, ap);
1257 va_end(ap);
1258 (void) fprintf(fp, "\n");
1259 (void) fflush(fp);
1260 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261
Damien Miller95def091999-11-25 00:26:21 +11001262 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001263 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001264 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001265 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001266 fprintf(stderr, "\n");
1267 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268}
1269
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270void
Damien Miller65d1f572003-06-04 22:51:08 +10001271verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272{
1273 struct stat stb;
1274
1275 if (!stat(cp, &stb)) {
1276 if (S_ISDIR(stb.st_mode))
1277 return;
1278 errno = ENOTDIR;
1279 }
1280 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001281 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001282}
1283
1284int
Damien Miller65d1f572003-06-04 22:51:08 +10001285okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286{
1287 int c;
1288 char *cp;
1289
1290 cp = cp0;
1291 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001292 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293 if (c & 0200)
1294 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001295 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001296 switch (c) {
1297 case '\'':
1298 case '"':
1299 case '`':
1300 case ' ':
1301 case '#':
1302 goto bad;
1303 default:
1304 break;
1305 }
1306 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307 } while (*++cp);
1308 return (1);
1309
Damien Miller98c7ad62000-03-09 21:27:49 +11001310bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311 return (0);
1312}
1313
1314BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001315allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316{
1317 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001318#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319 struct stat stb;
1320
1321 if (fstat(fd, &stb) < 0) {
1322 run_err("fstat: %s", strerror(errno));
1323 return (0);
1324 }
Ben Lindstrom418e0782002-12-23 02:22:09 +00001325 size = roundup(stb.st_blksize, blksize);
1326 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001327 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001328#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001329 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001330#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331 if (bp->cnt >= size)
1332 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001333 if (bp->buf == NULL)
1334 bp->buf = xmalloc(size);
1335 else
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001336 bp->buf = xreallocarray(bp->buf, 1, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001337 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338 bp->cnt = size;
1339 return (bp);
1340}
1341
1342void
Damien Miller65d1f572003-06-04 22:51:08 +10001343lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001344{
1345 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001346 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001347 if (signo)
1348 _exit(1);
1349 else
1350 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351}