blob: b4db851980ba68a93d119c0c42d17d31f3b1866c [file] [log] [blame]
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001/* $OpenBSD: scp.c,v 1.187 2016/09/12 01:22:38 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>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000097#include <limits.h>
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +000098#include <locale.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100099#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +1100100#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +1000101#include <stdarg.h>
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"
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000117#include "utf8.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000119extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000120
Damien Millerc77cadb2007-10-26 15:39:15 +1000121#define COPY_BUFLEN 16384
122
Damien Miller07aa1322006-04-23 12:04:27 +1000123int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
Damien Millerf1211432011-01-06 22:40:30 +1100124int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000125
Ben Lindstrom387c4722001-05-08 20:27:25 +0000126/* Struct for addargs */
127arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100128arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100129
Damien Millerff74d742003-01-24 11:36:58 +1100130/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000131long long limit_kbps = 0;
132struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100133
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134/* Name of current file being transferred. */
135char *curfile;
136
137/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100138int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140/* This is set to zero if the progressmeter is not desired. */
141int showprogress = 1;
142
Damien Millerf1211432011-01-06 22:40:30 +1100143/*
144 * This is set to non-zero if remote-remote copy should be piped
145 * through this process.
146 */
147int throughlocal = 0;
148
Damien Millerad833b32000-08-23 10:46:23 +1000149/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100150char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000151
Ben Lindstrom007eb912002-11-09 15:54:08 +0000152/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000153pid_t do_cmd_pid = -1;
154
155static void
156killchild(int signo)
157{
Darren Tuckerba66df82005-01-24 21:57:40 +1100158 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000159 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100160 waitpid(do_cmd_pid, NULL, 0);
161 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000162
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000163 if (signo)
164 _exit(1);
165 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000166}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000167
Damien Miller0979b402010-07-02 13:37:33 +1000168static void
169suspchild(int signo)
170{
171 int status;
172
173 if (do_cmd_pid > 1) {
174 kill(do_cmd_pid, signo);
175 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
176 errno == EINTR)
177 ;
178 kill(getpid(), SIGSTOP);
179 }
180}
181
Damien Miller3eec6b72006-01-31 21:49:27 +1100182static int
183do_local_cmd(arglist *a)
184{
185 u_int i;
186 int status;
187 pid_t pid;
188
189 if (a->num == 0)
190 fatal("do_local_cmd: no arguments");
191
192 if (verbose_mode) {
193 fprintf(stderr, "Executing:");
194 for (i = 0; i < a->num; i++)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000195 fmprintf(stderr, " %s", a->list[i]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100196 fprintf(stderr, "\n");
197 }
198 if ((pid = fork()) == -1)
199 fatal("do_local_cmd: fork: %s", strerror(errno));
200
201 if (pid == 0) {
202 execvp(a->list[0], a->list);
203 perror(a->list[0]);
204 exit(1);
205 }
206
207 do_cmd_pid = pid;
208 signal(SIGTERM, killchild);
209 signal(SIGINT, killchild);
210 signal(SIGHUP, killchild);
211
212 while (waitpid(pid, &status, 0) == -1)
213 if (errno != EINTR)
214 fatal("do_local_cmd: waitpid: %s", strerror(errno));
215
216 do_cmd_pid = -1;
217
218 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
219 return (-1);
220
221 return (0);
222}
223
Damien Miller5428f641999-11-25 11:54:57 +1100224/*
225 * This function executes the given command as the specified user on the
226 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
227 * assigns the input and output file descriptors on success.
228 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229
Damien Miller4af51302000-04-16 11:18:38 +1000230int
Damien Miller07aa1322006-04-23 12:04:27 +1000231do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232{
Damien Miller95def091999-11-25 00:26:21 +1100233 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234
Damien Miller95def091999-11-25 00:26:21 +1100235 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000236 fmprintf(stderr,
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000237 "Executing: program %s host %s, user %s, command %s\n",
238 ssh_program, host,
239 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
Damien Miller5428f641999-11-25 11:54:57 +1100241 /*
242 * Reserve two descriptors so that the real pipes won't get
243 * descriptors 0 and 1 because that will screw up dup2 below.
244 */
Damien Miller40b59852006-06-13 13:00:25 +1000245 if (pipe(reserved) < 0)
246 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller95def091999-11-25 00:26:21 +1100248 /* Create a socket pair for communicating with ssh. */
249 if (pipe(pin) < 0)
250 fatal("pipe: %s", strerror(errno));
251 if (pipe(pout) < 0)
252 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 /* Free the reserved descriptors. */
255 close(reserved[0]);
256 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller0979b402010-07-02 13:37:33 +1000258 signal(SIGTSTP, suspchild);
259 signal(SIGTTIN, suspchild);
260 signal(SIGTTOU, suspchild);
261
Damien Millerb2cdcb52003-06-18 20:26:34 +1000262 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000263 do_cmd_pid = fork();
264 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100265 /* Child. */
266 close(pin[1]);
267 close(pout[0]);
268 dup2(pin[0], 0);
269 dup2(pout[1], 1);
270 close(pin[0]);
271 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Damien Miller3eec6b72006-01-31 21:49:27 +1100273 replacearg(&args, 0, "%s", ssh_program);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100274 if (remuser != NULL) {
275 addargs(&args, "-l");
276 addargs(&args, "%s", remuser);
277 }
278 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000279 addargs(&args, "%s", host);
280 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100281
Damien Miller874d77b2000-10-14 16:23:11 +1100282 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000283 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100284 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000285 } else if (do_cmd_pid == -1) {
286 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287 }
Damien Miller95def091999-11-25 00:26:21 +1100288 /* Parent. Close the other side, and return the local side. */
289 close(pin[0]);
290 *fdout = pin[1];
291 close(pout[1]);
292 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000293 signal(SIGTERM, killchild);
294 signal(SIGINT, killchild);
295 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100296 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Damien Millerf1211432011-01-06 22:40:30 +1100299/*
300 * This functions executes a command simlar to do_cmd(), but expects the
301 * input and output descriptors to be setup by a previous call to do_cmd().
302 * This way the input and output of two commands can be connected.
303 */
304int
305do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout)
306{
307 pid_t pid;
308 int status;
309
310 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000311 fmprintf(stderr,
Damien Millerf1211432011-01-06 22:40:30 +1100312 "Executing: 2nd program %s host %s, user %s, command %s\n",
313 ssh_program, host,
314 remuser ? remuser : "(unspecified)", cmd);
315
316 /* Fork a child to execute the command on the remote host using ssh. */
317 pid = fork();
318 if (pid == 0) {
319 dup2(fdin, 0);
320 dup2(fdout, 1);
321
322 replacearg(&args, 0, "%s", ssh_program);
323 if (remuser != NULL) {
324 addargs(&args, "-l");
325 addargs(&args, "%s", remuser);
326 }
327 addargs(&args, "--");
328 addargs(&args, "%s", host);
329 addargs(&args, "%s", cmd);
330
331 execvp(ssh_program, args.list);
332 perror(ssh_program);
333 exit(1);
334 } else if (pid == -1) {
335 fatal("fork: %s", strerror(errno));
336 }
337 while (waitpid(pid, &status, 0) == -1)
338 if (errno != EINTR)
339 fatal("do_cmd2: waitpid: %s", strerror(errno));
340 return 0;
341}
342
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000344 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345 char *buf;
346} BUF;
347
Damien Miller95def091999-11-25 00:26:21 +1100348BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100349void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100350int okname(char *);
351void run_err(const char *,...);
352void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100355uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356int errs, remin, remout;
357int pflag, iamremote, iamrecursive, targetshouldbedirectory;
358
359#define CMDNEEDS 64
360char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
361
Damien Miller95def091999-11-25 00:26:21 +1100362int response(void);
363void rsource(char *, struct stat *);
364void sink(int, char *[]);
365void source(int, char *[]);
366void tolocal(int, char *[]);
367void toremote(char *, int, char *[]);
368void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369
370int
Damien Miller65d1f572003-06-04 22:51:08 +1000371main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000373 int ch, fflag, tflag, status, n;
Damien Miller65e42f82010-09-24 22:15:11 +1000374 char *targ, **newargv;
375 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376 extern char *optarg;
377 extern int optind;
378
Darren Tuckerce321d82005-10-03 18:11:24 +1000379 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
380 sanitise_stdfd();
381
Damien Millerdda78a02016-12-12 13:57:10 +1100382 msetlocale();
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000383
Damien Miller3d1a9f42006-07-10 22:19:53 +1000384 /* Copy argv, because we modify it */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000385 newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
Damien Miller3d1a9f42006-07-10 22:19:53 +1000386 for (n = 0; n < argc; n++)
387 newargv[n] = xstrdup(argv[n]);
388 argv = newargv;
389
Damien Miller59d3d5b2003-08-22 09:34:41 +1000390 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000391
Damien Miller3eec6b72006-01-31 21:49:27 +1100392 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100393 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
394 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100395 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000396 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100397 addargs(&args, "-oForwardAgent=no");
398 addargs(&args, "-oPermitLocalCommand=no");
399 addargs(&args, "-oClearAllForwardings=yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100400
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401 fflag = tflag = 0;
Damien Millerf1211432011-01-06 22:40:30 +1100402 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100403 switch (ch) {
404 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100405 case '1':
406 case '2':
Damien Miller34132e52000-01-14 15:45:46 +1100407 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100408 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100409 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000410 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100411 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100412 break;
Damien Millerf1211432011-01-06 22:40:30 +1100413 case '3':
414 throughlocal = 1;
415 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100416 case 'o':
417 case 'c':
418 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000419 case 'F':
Damien Miller87dc0a42010-12-01 12:03:19 +1100420 addargs(&remote_remote_args, "-%c", ch);
421 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100422 addargs(&args, "-%c", ch);
423 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100424 break;
425 case 'P':
Damien Miller87dc0a42010-12-01 12:03:19 +1100426 addargs(&remote_remote_args, "-p");
427 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100428 addargs(&args, "-p");
429 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100430 break;
431 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100432 addargs(&remote_remote_args, "-oBatchmode=yes");
433 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100434 break;
Damien Millerff74d742003-01-24 11:36:58 +1100435 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000436 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
437 &errstr);
438 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100439 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000440 limit_kbps *= 1024; /* kbps */
441 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100442 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000443 case 'p':
444 pflag = 1;
445 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446 case 'r':
447 iamrecursive = 1;
448 break;
Damien Millerad833b32000-08-23 10:46:23 +1000449 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100450 ssh_program = xstrdup(optarg);
451 break;
452 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000453 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100454 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100455 verbose_mode = 1;
456 break;
457 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100458 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100459 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100460 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000461 break;
462
Damien Miller95def091999-11-25 00:26:21 +1100463 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 case 'd':
465 targetshouldbedirectory = 1;
466 break;
Damien Miller95def091999-11-25 00:26:21 +1100467 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468 iamremote = 1;
469 fflag = 1;
470 break;
Damien Miller95def091999-11-25 00:26:21 +1100471 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000472 iamremote = 1;
473 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000474#ifdef HAVE_CYGWIN
475 setmode(0, O_BINARY);
476#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000478 default:
479 usage();
480 }
481 argc -= optind;
482 argv += optind;
483
484 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000485 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100487 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000488 showprogress = 0;
489
deraadt@openbsd.org9080bd02015-11-27 00:49:31 +0000490 if (pflag) {
491 /* Cannot pledge: -p allows setuid/setgid files... */
492 } else {
493 if (pledge("stdio rpath wpath cpath fattr tty proc exec",
494 NULL) == -1) {
495 perror("pledge");
496 exit(1);
497 }
498 }
499
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 remin = STDIN_FILENO;
501 remout = STDOUT_FILENO;
502
Kevin Stevesef4eea92001-02-05 12:42:17 +0000503 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100504 /* Follow "protocol", send data. */
505 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506 source(argc, argv);
507 exit(errs != 0);
508 }
Damien Miller95def091999-11-25 00:26:21 +1100509 if (tflag) {
510 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511 sink(argc, argv);
512 exit(errs != 0);
513 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514 if (argc < 2)
515 usage();
516 if (argc > 2)
517 targetshouldbedirectory = 1;
518
519 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000520 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000522 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
523 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000524 iamrecursive ? " -r" : "", pflag ? " -p" : "",
525 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Damien Miller95def091999-11-25 00:26:21 +1100527 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528
529 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
530 toremote(targ, argc, argv);
531 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532 if (targetshouldbedirectory)
533 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100534 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000535 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000536 /*
537 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100538 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000539 */
540 if (do_cmd_pid != -1 && errs == 0) {
541 if (remin != -1)
542 (void) close(remin);
543 if (remout != -1)
544 (void) close(remout);
545 if (waitpid(do_cmd_pid, &status, 0) == -1)
546 errs = 1;
547 else {
548 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
549 errs = 1;
550 }
551 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000552 exit(errs != 0);
553}
554
Damien Miller65e42f82010-09-24 22:15:11 +1000555/* Callback from atomicio6 to update progress meter and limit bandwidth */
556static int
557scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000558{
Damien Miller65e42f82010-09-24 22:15:11 +1000559 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000560
Damien Miller65e42f82010-09-24 22:15:11 +1000561 *cnt += s;
562 if (limit_kbps > 0)
563 bandwidth_limit(&bwlimit, s);
564 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000565}
566
Damien Miller044bd2a2013-07-18 16:09:25 +1000567static int
568do_times(int fd, int verb, const struct stat *sb)
569{
570 /* strlen(2^64) == 20; strlen(10^6) == 7 */
571 char buf[(20 + 7 + 2) * 2 + 2];
572
573 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
574 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
575 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
576 if (verb) {
577 fprintf(stderr, "File mtime %lld atime %lld\n",
578 (long long)sb->st_mtime, (long long)sb->st_atime);
579 fprintf(stderr, "Sending file timestamps: %s", buf);
580 }
581 (void) atomicio(vwrite, fd, buf, strlen(buf));
582 return (response());
583}
584
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000585void
Damien Miller65d1f572003-06-04 22:51:08 +1000586toremote(char *targ, int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587{
Damien Miller3dae15c2005-04-03 10:16:11 +1000588 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
Damien Miller3eec6b72006-01-31 21:49:27 +1100589 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000590 int i;
Damien Miller87dc0a42010-12-01 12:03:19 +1100591 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100592
593 memset(&alist, '\0', sizeof(alist));
594 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595
596 *targ++ = 0;
597 if (*targ == 0)
598 targ = ".";
599
Damien Miller4942de52005-04-03 10:16:39 +1000600 arg = xstrdup(argv[argc - 1]);
Damien Miller3dae15c2005-04-03 10:16:11 +1000601 if ((thost = strrchr(arg, '@'))) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000602 /* user@host */
603 *thost++ = 0;
Damien Miller3dae15c2005-04-03 10:16:11 +1000604 tuser = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000605 if (*tuser == '\0')
606 tuser = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000607 } else {
Damien Miller3dae15c2005-04-03 10:16:11 +1000608 thost = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000609 tuser = NULL;
610 }
611
Damien Miller3eec6b72006-01-31 21:49:27 +1100612 if (tuser != NULL && !okname(tuser)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000613 free(arg);
Damien Miller3eec6b72006-01-31 21:49:27 +1100614 return;
615 }
616
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000617 for (i = 0; i < argc - 1; i++) {
618 src = colon(argv[i]);
Damien Millerf1211432011-01-06 22:40:30 +1100619 if (src && throughlocal) { /* extended remote to remote */
620 *src++ = 0;
621 if (*src == 0)
622 src = ".";
623 host = strrchr(argv[i], '@');
624 if (host) {
625 *host++ = 0;
626 host = cleanhostname(host);
627 suser = argv[i];
628 if (*suser == '\0')
629 suser = pwd->pw_name;
630 else if (!okname(suser))
631 continue;
632 } else {
633 host = cleanhostname(argv[i]);
634 suser = NULL;
635 }
Damien Miller0603d982011-09-22 21:38:00 +1000636 xasprintf(&bp, "%s -f %s%s", cmd,
637 *src == '-' ? "-- " : "", src);
Damien Millerf1211432011-01-06 22:40:30 +1100638 if (do_cmd(host, suser, bp, &remin, &remout) < 0)
639 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000640 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100641 host = cleanhostname(thost);
Damien Miller0603d982011-09-22 21:38:00 +1000642 xasprintf(&bp, "%s -t %s%s", cmd,
643 *targ == '-' ? "-- " : "", targ);
Damien Millerf1211432011-01-06 22:40:30 +1100644 if (do_cmd2(host, tuser, bp, remin, remout) < 0)
645 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000646 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100647 (void) close(remin);
648 (void) close(remout);
649 remin = remout = -1;
650 } else if (src) { /* standard remote to remote */
Damien Miller3eec6b72006-01-31 21:49:27 +1100651 freeargs(&alist);
652 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100653 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100654 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100655 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100656 for (j = 0; j < remote_remote_args.num; j++) {
657 addargs(&alist, "%s",
658 remote_remote_args.list[j]);
659 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000660 *src++ = 0;
661 if (*src == 0)
662 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000663 host = strrchr(argv[i], '@');
Damien Miller3eec6b72006-01-31 21:49:27 +1100664
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000665 if (host) {
666 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100667 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668 suser = argv[i];
669 if (*suser == '\0')
670 suser = pwd->pw_name;
Damien Miller3eec6b72006-01-31 21:49:27 +1100671 else if (!okname(suser))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672 continue;
Damien Miller3eec6b72006-01-31 21:49:27 +1100673 addargs(&alist, "-l");
674 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100675 } else {
676 host = cleanhostname(argv[i]);
Damien Miller34132e52000-01-14 15:45:46 +1100677 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100678 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100679 addargs(&alist, "%s", host);
680 addargs(&alist, "%s", cmd);
681 addargs(&alist, "%s", src);
682 addargs(&alist, "%s%s%s:%s",
683 tuser ? tuser : "", tuser ? "@" : "",
684 thost, targ);
685 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000686 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100687 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000689 xasprintf(&bp, "%s -t %s%s", cmd,
690 *targ == '-' ? "-- " : "", targ);
Damien Miller34132e52000-01-14 15:45:46 +1100691 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000692 if (do_cmd(host, tuser, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000693 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100694 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695 if (response() < 0)
696 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000697 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698 }
Damien Miller95def091999-11-25 00:26:21 +1100699 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700 }
701 }
Darren Tuckera627d422013-06-02 07:31:17 +1000702 free(arg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000703}
704
705void
Damien Miller65d1f572003-06-04 22:51:08 +1000706tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707{
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708 char *bp, *host, *src, *suser;
Damien Miller3eec6b72006-01-31 21:49:27 +1100709 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000710 int i;
Damien Miller3eec6b72006-01-31 21:49:27 +1100711
712 memset(&alist, '\0', sizeof(alist));
713 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714
715 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100716 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +1100717 freeargs(&alist);
718 addargs(&alist, "%s", _PATH_CP);
719 if (iamrecursive)
720 addargs(&alist, "-r");
721 if (pflag)
722 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100723 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100724 addargs(&alist, "%s", argv[i]);
725 addargs(&alist, "%s", argv[argc-1]);
726 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000728 continue;
729 }
730 *src++ = 0;
731 if (*src == 0)
732 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000733 if ((host = strrchr(argv[i], '@')) == NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000734 host = argv[i];
735 suser = NULL;
736 } else {
737 *host++ = 0;
738 suser = argv[i];
739 if (*suser == '\0')
740 suser = pwd->pw_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741 }
Damien Miller34132e52000-01-14 15:45:46 +1100742 host = cleanhostname(host);
Damien Miller0603d982011-09-22 21:38:00 +1000743 xasprintf(&bp, "%s -f %s%s",
744 cmd, *src == '-' ? "-- " : "", src);
Damien Miller07aa1322006-04-23 12:04:27 +1000745 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000746 free(bp);
Damien Miller95def091999-11-25 00:26:21 +1100747 ++errs;
748 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749 }
Darren Tuckera627d422013-06-02 07:31:17 +1000750 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000751 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100752 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000753 remin = remout = -1;
754 }
755}
756
757void
Damien Miller65d1f572003-06-04 22:51:08 +1000758source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000759{
760 struct stat stb;
761 static BUF buffer;
762 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000763 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +1000764 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +1000765 int fd = -1, haderr, indx;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000766 char *last, *name, buf[2048], encname[PATH_MAX];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000767 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000768
769 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100770 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000772 len = strlen(name);
773 while (len > 1 && name[len-1] == '/')
774 name[--len] = '\0';
Darren Tucker43ce9022007-06-12 23:41:06 +1000775 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +1000777 if (strchr(name, '\n') != NULL) {
778 strnvis(encname, name, sizeof(encname), VIS_NL);
779 name = encname;
780 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781 if (fstat(fd, &stb) < 0) {
782syserr: run_err("%s: %s", name, strerror(errno));
783 goto next;
784 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000785 if (stb.st_size < 0) {
786 run_err("%s: %s", name, "Negative file size");
787 goto next;
788 }
Darren Tucker43ce9022007-06-12 23:41:06 +1000789 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790 switch (stb.st_mode & S_IFMT) {
791 case S_IFREG:
792 break;
793 case S_IFDIR:
794 if (iamrecursive) {
795 rsource(name, &stb);
796 goto next;
797 }
798 /* FALLTHROUGH */
799 default:
800 run_err("%s: not a regular file", name);
801 goto next;
802 }
803 if ((last = strrchr(name, '/')) == NULL)
804 last = name;
805 else
806 ++last;
807 curfile = last;
808 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000809 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810 goto next;
811 }
812#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +0000813 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000814 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +1100815 (long long)stb.st_size, last);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000816 if (verbose_mode)
817 fmprintf(stderr, "Sending file modes: %s", buf);
Darren Tucker9f63f222003-07-03 13:46:56 +1000818 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819 if (response() < 0)
820 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +1000821 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +1100822next: if (fd != -1) {
823 (void) close(fd);
824 fd = -1;
825 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826 continue;
827 }
Damien Miller62d57f62003-01-10 21:43:24 +1100828 if (showprogress)
829 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +1000830 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
832 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000833 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834 amt = stb.st_size - i;
835 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +1000836 if ((nr = atomicio(read, fd,
837 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +1000838 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +1000839 memset(bp->buf + nr, 0, amt - nr);
840 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000842 /* Keep writing after error to retain sync */
843 if (haderr) {
844 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +1000845 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +1000846 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000847 }
Damien Miller65e42f82010-09-24 22:15:11 +1000848 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +1000849 &statbytes) != amt)
850 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000851 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000852 unset_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853
Darren Tucker33f86bc2005-11-22 19:38:06 +1100854 if (fd != -1) {
855 if (close(fd) < 0 && !haderr)
856 haderr = errno;
857 fd = -1;
858 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +1000860 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861 else
862 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100863 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +0000864 if (showprogress)
865 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866 }
867}
868
869void
Damien Miller65d1f572003-06-04 22:51:08 +1000870rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871{
872 DIR *dirp;
873 struct dirent *dp;
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000874 char *last, *vect[1], path[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875
876 if (!(dirp = opendir(name))) {
877 run_err("%s: %s", name, strerror(errno));
878 return;
879 }
880 last = strrchr(name, '/');
mmcc@openbsd.orge92c4992015-10-16 17:07:24 +0000881 if (last == NULL)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 last = name;
883 else
884 last++;
885 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000886 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000887 closedir(dirp);
888 return;
889 }
890 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000891 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000892 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100893 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000894 fmprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +1000895 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896 if (response() < 0) {
897 closedir(dirp);
898 return;
899 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000900 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901 if (dp->d_ino == 0)
902 continue;
903 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
904 continue;
905 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
906 run_err("%s/%s: name too long", name, dp->d_name);
907 continue;
908 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000909 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910 vect[0] = path;
911 source(1, vect);
912 }
Damien Miller95def091999-11-25 00:26:21 +1100913 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +1000914 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100915 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000916}
917
918void
Damien Miller65d1f572003-06-04 22:51:08 +1000919sink(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920{
921 static BUF buffer;
922 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100923 enum {
924 YES, NO, DISPLAYED
925 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000926 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +1000927 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +1000928 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +1000929 int amt, exists, first, ofd;
930 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +1100931 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +1000932 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +1100933 int setimes, targisdir, wrerrno = 0;
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000934 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100935 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000936
Ben Lindstromf719a202001-04-14 23:14:22 +0000937#define atime tv[0]
938#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +1100939#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000940
941 setimes = targisdir = 0;
942 mask = umask(0);
943 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100944 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945 if (argc != 1) {
946 run_err("ambiguous target");
947 exit(1);
948 }
949 targ = *argv;
950 if (targetshouldbedirectory)
951 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100952
Darren Tucker9f63f222003-07-03 13:46:56 +1000953 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000954 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
955 targisdir = 1;
956 for (first = 1;; first = 0) {
957 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +1000958 if (atomicio(read, remin, cp, 1) != 1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959 return;
960 if (*cp++ == '\n')
961 SCREWUP("unexpected <newline>");
962 do {
Damien Millere247cc42000-05-07 12:03:14 +1000963 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964 SCREWUP("lost connection");
965 *cp++ = ch;
966 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
967 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +1000968 if (verbose_mode)
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000969 fmprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
971 if (buf[0] == '\01' || buf[0] == '\02') {
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000972 if (iamremote == 0) {
973 (void) snmprintf(visbuf, sizeof(visbuf),
974 NULL, "%s", buf + 1);
Darren Tucker9f63f222003-07-03 13:46:56 +1000975 (void) atomicio(vwrite, STDERR_FILENO,
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +0000976 visbuf, strlen(visbuf));
977 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978 if (buf[0] == '\02')
979 exit(1);
980 ++errs;
981 continue;
982 }
983 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +1000984 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000985 return;
986 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987 if (ch == '\n')
988 *--cp = 0;
989
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990 cp = buf;
991 if (*cp == 'T') {
992 setimes++;
993 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +1000994 if (!isdigit((unsigned char)*cp))
995 SCREWUP("mtime.sec not present");
996 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +0000997 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000998 SCREWUP("mtime.sec not delimited");
Damien Millerbc35d922013-07-18 16:11:25 +1000999 if ((time_t)ull < 0 ||
1000 (unsigned long long)(time_t)ull != ull)
Damien Miller044bd2a2013-07-18 16:09:25 +10001001 setimes = 0; /* out of range */
1002 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001003 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001004 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1005 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +10001007 if (!isdigit((unsigned char)*cp))
1008 SCREWUP("atime.sec not present");
1009 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +00001010 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011 SCREWUP("atime.sec not delimited");
Damien Millerbc35d922013-07-18 16:11:25 +10001012 if ((time_t)ull < 0 ||
1013 (unsigned long long)(time_t)ull != ull)
Damien Miller044bd2a2013-07-18 16:09:25 +10001014 setimes = 0; /* out of range */
1015 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001016 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001017 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1018 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001020 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021 continue;
1022 }
1023 if (*cp != 'C' && *cp != 'D') {
1024 /*
1025 * Check for the case "rcp remote:foo\* local:bar".
1026 * In this case, the line "No match." can be returned
1027 * by the shell before the rcp command on the remote is
1028 * executed so the ^Aerror_message convention isn't
1029 * followed.
1030 */
1031 if (first) {
1032 run_err("%s", cp);
1033 exit(1);
1034 }
1035 SCREWUP("expected control record");
1036 }
1037 mode = 0;
1038 for (++cp; cp < buf + 5; cp++) {
1039 if (*cp < '0' || *cp > '7')
1040 SCREWUP("bad mode");
1041 mode = (mode << 3) | (*cp - '0');
1042 }
1043 if (*cp++ != ' ')
1044 SCREWUP("mode not delimited");
1045
Damien Millere0016732013-11-21 13:56:49 +11001046 for (size = 0; isdigit((unsigned char)*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001047 size = size * 10 + (*cp++ - '0');
1048 if (*cp++ != ' ')
1049 SCREWUP("size not delimited");
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001050 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
1051 run_err("error: unexpected filename: %s", cp);
1052 exit(1);
1053 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001054 if (targisdir) {
1055 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001056 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057 size_t need;
1058
1059 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001060 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001061 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001062 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001063 cursize = need;
1064 }
1065 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001066 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067 np = namebuf;
1068 } else
1069 np = targ;
1070 curfile = cp;
1071 exists = stat(np, &stb) == 0;
1072 if (buf[0] == 'D') {
1073 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001074 if (!iamrecursive)
1075 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001076 if (exists) {
1077 if (!S_ISDIR(stb.st_mode)) {
1078 errno = ENOTDIR;
1079 goto bad;
1080 }
1081 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001082 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001083 } else {
Damien Miller95def091999-11-25 00:26:21 +11001084 /* Handle copying from a read-only
1085 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086 mod_flag = 1;
1087 if (mkdir(np, mode | S_IRWXU) < 0)
1088 goto bad;
1089 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001090 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091 sink(1, vect);
1092 if (setimes) {
1093 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +11001094 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001095 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001096 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001097 }
1098 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001099 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001100 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101 continue;
1102 }
1103 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001104 mode |= S_IWUSR;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +00001105 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106bad: run_err("%s: %s", np, strerror(errno));
1107 continue;
1108 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001109 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001110 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001111 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112 continue;
1113 }
1114 cp = bp->buf;
1115 wrerr = NO;
1116
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001118 if (showprogress)
1119 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001120 set_nonblock(remin);
1121 for (count = i = 0; i < size; i += bp->cnt) {
1122 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123 if (i + amt > size)
1124 amt = size - i;
1125 count += amt;
1126 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001127 j = atomicio6(read, remin, cp, amt,
1128 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001129 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001130 run_err("%s", j != EPIPE ?
1131 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001132 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133 exit(1);
1134 }
1135 amt -= j;
1136 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001138
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139 if (count == bp->cnt) {
1140 /* Keep reading so we stay sync'd up. */
1141 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001142 if (atomicio(vwrite, ofd, bp->buf,
1143 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001145 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146 }
1147 }
1148 count = 0;
1149 cp = bp->buf;
1150 }
1151 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001152 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001154 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001156 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 }
Darren Tucker067263e2007-06-25 18:32:33 +10001158 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1159 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 run_err("%s: truncate: %s", np, strerror(errno));
1161 wrerr = DISPLAYED;
1162 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163 if (pflag) {
1164 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001165#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001166 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001167#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001168 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001169#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001170 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001171 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001172 wrerr = DISPLAYED;
1173 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174 } else {
1175 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001176#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001177 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001178#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001179 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001180#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001181 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001182 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001183 wrerr = DISPLAYED;
1184 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001185 }
Damien Millerbe484b52000-07-15 14:14:16 +10001186 if (close(ofd) == -1) {
1187 wrerr = YES;
1188 wrerrno = errno;
1189 }
Damien Miller95def091999-11-25 00:26:21 +11001190 (void) response();
dtucker@openbsd.org8ef04d72016-03-02 22:43:52 +00001191 if (showprogress)
1192 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 if (setimes && wrerr == NO) {
1194 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001195 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001197 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198 wrerr = DISPLAYED;
1199 }
1200 }
Damien Miller95def091999-11-25 00:26:21 +11001201 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202 case YES:
1203 run_err("%s: %s", np, strerror(wrerrno));
1204 break;
1205 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001206 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207 break;
1208 case DISPLAYED:
1209 break;
1210 }
1211 }
1212screwup:
1213 run_err("protocol error: %s", why);
1214 exit(1);
1215}
1216
1217int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001218response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219{
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001220 char ch, *cp, resp, rbuf[2048], visbuf[2048];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001221
Damien Millere247cc42000-05-07 12:03:14 +10001222 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223 lostconn(0);
1224
1225 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001226 switch (resp) {
1227 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228 return (0);
1229 default:
1230 *cp++ = resp;
1231 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001232 case 1: /* error, followed by error msg */
1233 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001234 do {
Damien Millere247cc42000-05-07 12:03:14 +10001235 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236 lostconn(0);
1237 *cp++ = ch;
1238 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1239
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001240 if (!iamremote) {
1241 cp[-1] = '\0';
1242 (void) snmprintf(visbuf, sizeof(visbuf),
1243 NULL, "%s\n", rbuf);
1244 (void) atomicio(vwrite, STDERR_FILENO,
1245 visbuf, strlen(visbuf));
1246 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247 ++errs;
1248 if (resp == 1)
1249 return (-1);
1250 exit(1);
1251 }
1252 /* NOTREACHED */
1253}
1254
1255void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001256usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001258 (void) fprintf(stderr,
Damien Miller907998d2011-01-06 22:41:21 +11001259 "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
Darren Tucker1f203942003-10-15 15:50:42 +10001260 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
Damien Miller647d97b2007-08-08 14:29:58 +10001261 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262 exit(1);
1263}
1264
1265void
Damien Miller95def091999-11-25 00:26:21 +11001266run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267{
1268 static FILE *fp;
1269 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270
1271 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001272 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1273 (void) fprintf(fp, "%c", 0x01);
1274 (void) fprintf(fp, "scp: ");
1275 va_start(ap, fmt);
1276 (void) vfprintf(fp, fmt, ap);
1277 va_end(ap);
1278 (void) fprintf(fp, "\n");
1279 (void) fflush(fp);
1280 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281
Damien Miller95def091999-11-25 00:26:21 +11001282 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001283 va_start(ap, fmt);
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001284 vfmprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001285 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001286 fprintf(stderr, "\n");
1287 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001288}
1289
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001290void
Damien Miller65d1f572003-06-04 22:51:08 +10001291verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292{
1293 struct stat stb;
1294
1295 if (!stat(cp, &stb)) {
1296 if (S_ISDIR(stb.st_mode))
1297 return;
1298 errno = ENOTDIR;
1299 }
1300 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001301 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302}
1303
1304int
Damien Miller65d1f572003-06-04 22:51:08 +10001305okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001306{
1307 int c;
1308 char *cp;
1309
1310 cp = cp0;
1311 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001312 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001313 if (c & 0200)
1314 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001315 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001316 switch (c) {
1317 case '\'':
1318 case '"':
1319 case '`':
1320 case ' ':
1321 case '#':
1322 goto bad;
1323 default:
1324 break;
1325 }
1326 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327 } while (*++cp);
1328 return (1);
1329
schwarze@openbsd.org0e059cd2016-05-25 23:48:45 +00001330bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331 return (0);
1332}
1333
1334BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001335allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336{
1337 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001338#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001339 struct stat stb;
1340
1341 if (fstat(fd, &stb) < 0) {
1342 run_err("fstat: %s", strerror(errno));
1343 return (0);
1344 }
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001345 size = ROUNDUP(stb.st_blksize, blksize);
Ben Lindstrom418e0782002-12-23 02:22:09 +00001346 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001347 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001348#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001349 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001350#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351 if (bp->cnt >= size)
1352 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001353 if (bp->buf == NULL)
1354 bp->buf = xmalloc(size);
1355 else
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001356 bp->buf = xreallocarray(bp->buf, 1, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001357 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358 bp->cnt = size;
1359 return (bp);
1360}
1361
1362void
Damien Miller65d1f572003-06-04 22:51:08 +10001363lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364{
1365 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001366 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001367 if (signo)
1368 _exit(1);
1369 else
1370 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001371}