blob: 1ec3b7087685efe269e5aa420d08092caaea0fae [file] [log] [blame]
Damien Miller1845fe62014-07-02 15:29:01 +10001/* $OpenBSD: scp.c,v 1.180 2014/06/24 02:21:01 djm 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>
Damien Miller9f2abc42006-07-10 20:53:08 +100098#include <pwd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110099#include <signal.h>
Darren Tucker5d196262006-07-12 22:15:16 +1000100#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +1000101#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +1000102#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +1000103#include <string.h>
Damien Miller5598b4f2006-07-24 14:09:40 +1000104#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +1000105#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +1100106#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Darren Tuckerbed63112007-06-13 00:02:07 +1000107#include <vis.h>
108#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000111#include "atomicio.h"
112#include "pathnames.h"
113#include "log.h"
Kevin Steves93c17d92001-02-18 03:55:16 +0000114#include "misc.h"
Damien Miller62d57f62003-01-10 21:43:24 +1100115#include "progressmeter.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000117extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000118
Damien Millerc77cadb2007-10-26 15:39:15 +1000119#define COPY_BUFLEN 16384
120
Damien Miller07aa1322006-04-23 12:04:27 +1000121int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
Damien Millerf1211432011-01-06 22:40:30 +1100122int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout);
Damien Miller07aa1322006-04-23 12:04:27 +1000123
Ben Lindstrom387c4722001-05-08 20:27:25 +0000124/* Struct for addargs */
125arglist args;
Damien Miller87dc0a42010-12-01 12:03:19 +1100126arglist remote_remote_args;
Damien Miller874d77b2000-10-14 16:23:11 +1100127
Damien Millerff74d742003-01-24 11:36:58 +1100128/* Bandwidth limit */
Damien Miller65e42f82010-09-24 22:15:11 +1000129long long limit_kbps = 0;
130struct bwlimit bwlimit;
Damien Millerff74d742003-01-24 11:36:58 +1100131
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132/* Name of current file being transferred. */
133char *curfile;
134
135/* This is set to non-zero to enable verbose mode. */
Damien Miller95def091999-11-25 00:26:21 +1100136int verbose_mode = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* This is set to zero if the progressmeter is not desired. */
139int showprogress = 1;
140
Damien Millerf1211432011-01-06 22:40:30 +1100141/*
142 * This is set to non-zero if remote-remote copy should be piped
143 * through this process.
144 */
145int throughlocal = 0;
146
Damien Millerad833b32000-08-23 10:46:23 +1000147/* This is the program to execute for the secured connection. ("ssh" or -S) */
Darren Tuckere5662302007-12-02 22:48:40 +1100148char *ssh_program = _PATH_SSH_PROGRAM;
Damien Millerad833b32000-08-23 10:46:23 +1000149
Ben Lindstrom007eb912002-11-09 15:54:08 +0000150/* This is used to store the pid of ssh_program */
Damien Millerb69aaa82003-06-04 22:51:24 +1000151pid_t do_cmd_pid = -1;
152
153static void
154killchild(int signo)
155{
Darren Tuckerba66df82005-01-24 21:57:40 +1100156 if (do_cmd_pid > 1) {
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000157 kill(do_cmd_pid, signo ? signo : SIGTERM);
Darren Tuckerba66df82005-01-24 21:57:40 +1100158 waitpid(do_cmd_pid, NULL, 0);
159 }
Damien Millerb69aaa82003-06-04 22:51:24 +1000160
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +1000161 if (signo)
162 _exit(1);
163 exit(1);
Damien Millerb69aaa82003-06-04 22:51:24 +1000164}
Ben Lindstrom007eb912002-11-09 15:54:08 +0000165
Damien Miller0979b402010-07-02 13:37:33 +1000166static void
167suspchild(int signo)
168{
169 int status;
170
171 if (do_cmd_pid > 1) {
172 kill(do_cmd_pid, signo);
173 while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
174 errno == EINTR)
175 ;
176 kill(getpid(), SIGSTOP);
177 }
178}
179
Damien Miller3eec6b72006-01-31 21:49:27 +1100180static int
181do_local_cmd(arglist *a)
182{
183 u_int i;
184 int status;
185 pid_t pid;
186
187 if (a->num == 0)
188 fatal("do_local_cmd: no arguments");
189
190 if (verbose_mode) {
191 fprintf(stderr, "Executing:");
192 for (i = 0; i < a->num; i++)
193 fprintf(stderr, " %s", a->list[i]);
194 fprintf(stderr, "\n");
195 }
196 if ((pid = fork()) == -1)
197 fatal("do_local_cmd: fork: %s", strerror(errno));
198
199 if (pid == 0) {
200 execvp(a->list[0], a->list);
201 perror(a->list[0]);
202 exit(1);
203 }
204
205 do_cmd_pid = pid;
206 signal(SIGTERM, killchild);
207 signal(SIGINT, killchild);
208 signal(SIGHUP, killchild);
209
210 while (waitpid(pid, &status, 0) == -1)
211 if (errno != EINTR)
212 fatal("do_local_cmd: waitpid: %s", strerror(errno));
213
214 do_cmd_pid = -1;
215
216 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
217 return (-1);
218
219 return (0);
220}
221
Damien Miller5428f641999-11-25 11:54:57 +1100222/*
223 * This function executes the given command as the specified user on the
224 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
225 * assigns the input and output file descriptors on success.
226 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227
Damien Miller4af51302000-04-16 11:18:38 +1000228int
Damien Miller07aa1322006-04-23 12:04:27 +1000229do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230{
Damien Miller95def091999-11-25 00:26:21 +1100231 int pin[2], pout[2], reserved[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232
Damien Miller95def091999-11-25 00:26:21 +1100233 if (verbose_mode)
Ben Lindstrom2772a3f2001-08-06 21:17:12 +0000234 fprintf(stderr,
235 "Executing: program %s host %s, user %s, command %s\n",
236 ssh_program, host,
237 remuser ? remuser : "(unspecified)", cmd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Damien Miller5428f641999-11-25 11:54:57 +1100239 /*
240 * Reserve two descriptors so that the real pipes won't get
241 * descriptors 0 and 1 because that will screw up dup2 below.
242 */
Damien Miller40b59852006-06-13 13:00:25 +1000243 if (pipe(reserved) < 0)
244 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller95def091999-11-25 00:26:21 +1100246 /* Create a socket pair for communicating with ssh. */
247 if (pipe(pin) < 0)
248 fatal("pipe: %s", strerror(errno));
249 if (pipe(pout) < 0)
250 fatal("pipe: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller95def091999-11-25 00:26:21 +1100252 /* Free the reserved descriptors. */
253 close(reserved[0]);
254 close(reserved[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255
Damien Miller0979b402010-07-02 13:37:33 +1000256 signal(SIGTSTP, suspchild);
257 signal(SIGTTIN, suspchild);
258 signal(SIGTTOU, suspchild);
259
Damien Millerb2cdcb52003-06-18 20:26:34 +1000260 /* Fork a child to execute the command on the remote host using ssh. */
Ben Lindstrom007eb912002-11-09 15:54:08 +0000261 do_cmd_pid = fork();
262 if (do_cmd_pid == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100263 /* Child. */
264 close(pin[1]);
265 close(pout[0]);
266 dup2(pin[0], 0);
267 dup2(pout[1], 1);
268 close(pin[0]);
269 close(pout[1]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Miller3eec6b72006-01-31 21:49:27 +1100271 replacearg(&args, 0, "%s", ssh_program);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100272 if (remuser != NULL) {
273 addargs(&args, "-l");
274 addargs(&args, "%s", remuser);
275 }
276 addargs(&args, "--");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000277 addargs(&args, "%s", host);
278 addargs(&args, "%s", cmd);
Damien Miller95def091999-11-25 00:26:21 +1100279
Damien Miller874d77b2000-10-14 16:23:11 +1100280 execvp(ssh_program, args.list);
Damien Millerad833b32000-08-23 10:46:23 +1000281 perror(ssh_program);
Damien Miller95def091999-11-25 00:26:21 +1100282 exit(1);
Ben Lindstrom007eb912002-11-09 15:54:08 +0000283 } else if (do_cmd_pid == -1) {
284 fatal("fork: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285 }
Damien Miller95def091999-11-25 00:26:21 +1100286 /* Parent. Close the other side, and return the local side. */
287 close(pin[0]);
288 *fdout = pin[1];
289 close(pout[1]);
290 *fdin = pout[0];
Damien Millerb69aaa82003-06-04 22:51:24 +1000291 signal(SIGTERM, killchild);
292 signal(SIGINT, killchild);
293 signal(SIGHUP, killchild);
Damien Miller95def091999-11-25 00:26:21 +1100294 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295}
296
Damien Millerf1211432011-01-06 22:40:30 +1100297/*
298 * This functions executes a command simlar to do_cmd(), but expects the
299 * input and output descriptors to be setup by a previous call to do_cmd().
300 * This way the input and output of two commands can be connected.
301 */
302int
303do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout)
304{
305 pid_t pid;
306 int status;
307
308 if (verbose_mode)
309 fprintf(stderr,
310 "Executing: 2nd program %s host %s, user %s, command %s\n",
311 ssh_program, host,
312 remuser ? remuser : "(unspecified)", cmd);
313
314 /* Fork a child to execute the command on the remote host using ssh. */
315 pid = fork();
316 if (pid == 0) {
317 dup2(fdin, 0);
318 dup2(fdout, 1);
319
320 replacearg(&args, 0, "%s", ssh_program);
321 if (remuser != NULL) {
322 addargs(&args, "-l");
323 addargs(&args, "%s", remuser);
324 }
325 addargs(&args, "--");
326 addargs(&args, "%s", host);
327 addargs(&args, "%s", cmd);
328
329 execvp(ssh_program, args.list);
330 perror(ssh_program);
331 exit(1);
332 } else if (pid == -1) {
333 fatal("fork: %s", strerror(errno));
334 }
335 while (waitpid(pid, &status, 0) == -1)
336 if (errno != EINTR)
337 fatal("do_cmd2: waitpid: %s", strerror(errno));
338 return 0;
339}
340
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341typedef struct {
Damien Millereccb9de2005-06-17 12:59:34 +1000342 size_t cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343 char *buf;
344} BUF;
345
Damien Miller95def091999-11-25 00:26:21 +1100346BUF *allocbuf(BUF *, int, int);
Damien Miller95def091999-11-25 00:26:21 +1100347void lostconn(int);
Damien Miller95def091999-11-25 00:26:21 +1100348int okname(char *);
349void run_err(const char *,...);
350void verifydir(char *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352struct passwd *pwd;
Damien Miller95def091999-11-25 00:26:21 +1100353uid_t userid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354int errs, remin, remout;
355int pflag, iamremote, iamrecursive, targetshouldbedirectory;
356
357#define CMDNEEDS 64
358char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
359
Damien Miller95def091999-11-25 00:26:21 +1100360int response(void);
361void rsource(char *, struct stat *);
362void sink(int, char *[]);
363void source(int, char *[]);
364void tolocal(int, char *[]);
365void toremote(char *, int, char *[]);
366void usage(void);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
368int
Damien Miller65d1f572003-06-04 22:51:08 +1000369main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370{
Damien Miller3d1a9f42006-07-10 22:19:53 +1000371 int ch, fflag, tflag, status, n;
Damien Miller65e42f82010-09-24 22:15:11 +1000372 char *targ, **newargv;
373 const char *errstr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374 extern char *optarg;
375 extern int optind;
376
Darren Tuckerce321d82005-10-03 18:11:24 +1000377 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
378 sanitise_stdfd();
379
Damien Miller3d1a9f42006-07-10 22:19:53 +1000380 /* Copy argv, because we modify it */
381 newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv));
382 for (n = 0; n < argc; n++)
383 newargv[n] = xstrdup(argv[n]);
384 argv = newargv;
385
Damien Miller59d3d5b2003-08-22 09:34:41 +1000386 __progname = ssh_get_progname(argv[0]);
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000387
Damien Miller3eec6b72006-01-31 21:49:27 +1100388 memset(&args, '\0', sizeof(args));
Damien Miller87dc0a42010-12-01 12:03:19 +1100389 memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
390 args.list = remote_remote_args.list = NULL;
Damien Miller3eec6b72006-01-31 21:49:27 +1100391 addargs(&args, "%s", ssh_program);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000392 addargs(&args, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100393 addargs(&args, "-oForwardAgent=no");
394 addargs(&args, "-oPermitLocalCommand=no");
395 addargs(&args, "-oClearAllForwardings=yes");
Damien Miller874d77b2000-10-14 16:23:11 +1100396
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397 fflag = tflag = 0;
Damien Millerf1211432011-01-06 22:40:30 +1100398 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
Damien Miller95def091999-11-25 00:26:21 +1100399 switch (ch) {
400 /* User-visible flags. */
Damien Miller8e121472003-01-24 11:37:38 +1100401 case '1':
402 case '2':
Damien Miller34132e52000-01-14 15:45:46 +1100403 case '4':
Damien Miller34132e52000-01-14 15:45:46 +1100404 case '6':
Damien Miller874d77b2000-10-14 16:23:11 +1100405 case 'C':
Ben Lindstrom387c4722001-05-08 20:27:25 +0000406 addargs(&args, "-%c", ch);
Damien Miller87dc0a42010-12-01 12:03:19 +1100407 addargs(&remote_remote_args, "-%c", ch);
Damien Miller874d77b2000-10-14 16:23:11 +1100408 break;
Damien Millerf1211432011-01-06 22:40:30 +1100409 case '3':
410 throughlocal = 1;
411 break;
Damien Miller874d77b2000-10-14 16:23:11 +1100412 case 'o':
413 case 'c':
414 case 'i':
Ben Lindstrom1e243242001-09-18 05:38:44 +0000415 case 'F':
Damien Miller87dc0a42010-12-01 12:03:19 +1100416 addargs(&remote_remote_args, "-%c", ch);
417 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100418 addargs(&args, "-%c", ch);
419 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100420 break;
421 case 'P':
Damien Miller87dc0a42010-12-01 12:03:19 +1100422 addargs(&remote_remote_args, "-p");
423 addargs(&remote_remote_args, "%s", optarg);
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100424 addargs(&args, "-p");
425 addargs(&args, "%s", optarg);
Damien Miller874d77b2000-10-14 16:23:11 +1100426 break;
427 case 'B':
Damien Miller87dc0a42010-12-01 12:03:19 +1100428 addargs(&remote_remote_args, "-oBatchmode=yes");
429 addargs(&args, "-oBatchmode=yes");
Damien Miller34132e52000-01-14 15:45:46 +1100430 break;
Damien Millerff74d742003-01-24 11:36:58 +1100431 case 'l':
Damien Miller65e42f82010-09-24 22:15:11 +1000432 limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
433 &errstr);
434 if (errstr != NULL)
Damien Millerff74d742003-01-24 11:36:58 +1100435 usage();
Damien Miller65e42f82010-09-24 22:15:11 +1000436 limit_kbps *= 1024; /* kbps */
437 bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
Damien Millerff74d742003-01-24 11:36:58 +1100438 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439 case 'p':
440 pflag = 1;
441 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442 case 'r':
443 iamrecursive = 1;
444 break;
Damien Millerad833b32000-08-23 10:46:23 +1000445 case 'S':
Damien Miller874d77b2000-10-14 16:23:11 +1100446 ssh_program = xstrdup(optarg);
447 break;
448 case 'v':
Ben Lindstrom9cc94642001-06-09 01:15:11 +0000449 addargs(&args, "-v");
Damien Miller87dc0a42010-12-01 12:03:19 +1100450 addargs(&remote_remote_args, "-v");
Damien Miller874d77b2000-10-14 16:23:11 +1100451 verbose_mode = 1;
452 break;
453 case 'q':
Damien Millerc1f27922003-11-17 21:19:05 +1100454 addargs(&args, "-q");
Damien Miller87dc0a42010-12-01 12:03:19 +1100455 addargs(&remote_remote_args, "-q");
Damien Miller874d77b2000-10-14 16:23:11 +1100456 showprogress = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000457 break;
458
Damien Miller95def091999-11-25 00:26:21 +1100459 /* Server options. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 case 'd':
461 targetshouldbedirectory = 1;
462 break;
Damien Miller95def091999-11-25 00:26:21 +1100463 case 'f': /* "from" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464 iamremote = 1;
465 fflag = 1;
466 break;
Damien Miller95def091999-11-25 00:26:21 +1100467 case 't': /* "to" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468 iamremote = 1;
469 tflag = 1;
Damien Miller402b3312001-04-14 00:28:42 +1000470#ifdef HAVE_CYGWIN
471 setmode(0, O_BINARY);
472#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474 default:
475 usage();
476 }
477 argc -= optind;
478 argv += optind;
479
480 if ((pwd = getpwuid(userid = getuid())) == NULL)
Darren Tucker03a29ba2003-07-19 20:07:45 +1000481 fatal("unknown user %u", (u_int) userid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000482
Darren Tucker6ec2fbe2007-02-19 22:14:11 +1100483 if (!isatty(STDOUT_FILENO))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484 showprogress = 0;
485
486 remin = STDIN_FILENO;
487 remout = STDOUT_FILENO;
488
Kevin Stevesef4eea92001-02-05 12:42:17 +0000489 if (fflag) {
Damien Miller95def091999-11-25 00:26:21 +1100490 /* Follow "protocol", send data. */
491 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492 source(argc, argv);
493 exit(errs != 0);
494 }
Damien Miller95def091999-11-25 00:26:21 +1100495 if (tflag) {
496 /* Receive data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497 sink(argc, argv);
498 exit(errs != 0);
499 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500 if (argc < 2)
501 usage();
502 if (argc > 2)
503 targetshouldbedirectory = 1;
504
505 remin = remout = -1;
Ben Lindstrom007eb912002-11-09 15:54:08 +0000506 do_cmd_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507 /* Command to be executed on remote system using "ssh". */
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000508 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
509 verbose_mode ? " -v" : "",
Damien Millerad833b32000-08-23 10:46:23 +1000510 iamrecursive ? " -r" : "", pflag ? " -p" : "",
511 targetshouldbedirectory ? " -d" : "");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller95def091999-11-25 00:26:21 +1100513 (void) signal(SIGPIPE, lostconn);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
515 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
516 toremote(targ, argc, argv);
517 else {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518 if (targetshouldbedirectory)
519 verifydir(argv[argc - 1]);
Damien Miller923f1ce2006-01-31 22:11:37 +1100520 tolocal(argc, argv); /* Dest is local host. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521 }
Ben Lindstrom007eb912002-11-09 15:54:08 +0000522 /*
523 * Finally check the exit status of the ssh process, if one was forked
Damien Millerb4acb472008-11-03 19:23:45 +1100524 * and no error has occurred yet
Ben Lindstrom007eb912002-11-09 15:54:08 +0000525 */
526 if (do_cmd_pid != -1 && errs == 0) {
527 if (remin != -1)
528 (void) close(remin);
529 if (remout != -1)
530 (void) close(remout);
531 if (waitpid(do_cmd_pid, &status, 0) == -1)
532 errs = 1;
533 else {
534 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
535 errs = 1;
536 }
537 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538 exit(errs != 0);
539}
540
Damien Miller65e42f82010-09-24 22:15:11 +1000541/* Callback from atomicio6 to update progress meter and limit bandwidth */
542static int
543scpio(void *_cnt, size_t s)
Damien Millerc77cadb2007-10-26 15:39:15 +1000544{
Damien Miller65e42f82010-09-24 22:15:11 +1000545 off_t *cnt = (off_t *)_cnt;
Damien Millerc77cadb2007-10-26 15:39:15 +1000546
Damien Miller65e42f82010-09-24 22:15:11 +1000547 *cnt += s;
548 if (limit_kbps > 0)
549 bandwidth_limit(&bwlimit, s);
550 return 0;
Damien Millerc77cadb2007-10-26 15:39:15 +1000551}
552
Damien Miller044bd2a2013-07-18 16:09:25 +1000553static int
554do_times(int fd, int verb, const struct stat *sb)
555{
556 /* strlen(2^64) == 20; strlen(10^6) == 7 */
557 char buf[(20 + 7 + 2) * 2 + 2];
558
559 (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
560 (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
561 (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
562 if (verb) {
563 fprintf(stderr, "File mtime %lld atime %lld\n",
564 (long long)sb->st_mtime, (long long)sb->st_atime);
565 fprintf(stderr, "Sending file timestamps: %s", buf);
566 }
567 (void) atomicio(vwrite, fd, buf, strlen(buf));
568 return (response());
569}
570
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000571void
Damien Miller65d1f572003-06-04 22:51:08 +1000572toremote(char *targ, int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000573{
Damien Miller3dae15c2005-04-03 10:16:11 +1000574 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
Damien Miller3eec6b72006-01-31 21:49:27 +1100575 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000576 int i;
Damien Miller87dc0a42010-12-01 12:03:19 +1100577 u_int j;
Damien Miller3eec6b72006-01-31 21:49:27 +1100578
579 memset(&alist, '\0', sizeof(alist));
580 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000581
582 *targ++ = 0;
583 if (*targ == 0)
584 targ = ".";
585
Damien Miller4942de52005-04-03 10:16:39 +1000586 arg = xstrdup(argv[argc - 1]);
Damien Miller3dae15c2005-04-03 10:16:11 +1000587 if ((thost = strrchr(arg, '@'))) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000588 /* user@host */
589 *thost++ = 0;
Damien Miller3dae15c2005-04-03 10:16:11 +1000590 tuser = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591 if (*tuser == '\0')
592 tuser = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000593 } else {
Damien Miller3dae15c2005-04-03 10:16:11 +1000594 thost = arg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000595 tuser = NULL;
596 }
597
Damien Miller3eec6b72006-01-31 21:49:27 +1100598 if (tuser != NULL && !okname(tuser)) {
Darren Tuckera627d422013-06-02 07:31:17 +1000599 free(arg);
Damien Miller3eec6b72006-01-31 21:49:27 +1100600 return;
601 }
602
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000603 for (i = 0; i < argc - 1; i++) {
604 src = colon(argv[i]);
Damien Millerf1211432011-01-06 22:40:30 +1100605 if (src && throughlocal) { /* extended remote to remote */
606 *src++ = 0;
607 if (*src == 0)
608 src = ".";
609 host = strrchr(argv[i], '@');
610 if (host) {
611 *host++ = 0;
612 host = cleanhostname(host);
613 suser = argv[i];
614 if (*suser == '\0')
615 suser = pwd->pw_name;
616 else if (!okname(suser))
617 continue;
618 } else {
619 host = cleanhostname(argv[i]);
620 suser = NULL;
621 }
Damien Miller0603d982011-09-22 21:38:00 +1000622 xasprintf(&bp, "%s -f %s%s", cmd,
623 *src == '-' ? "-- " : "", src);
Damien Millerf1211432011-01-06 22:40:30 +1100624 if (do_cmd(host, suser, bp, &remin, &remout) < 0)
625 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000626 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100627 host = cleanhostname(thost);
Damien Miller0603d982011-09-22 21:38:00 +1000628 xasprintf(&bp, "%s -t %s%s", cmd,
629 *targ == '-' ? "-- " : "", targ);
Damien Millerf1211432011-01-06 22:40:30 +1100630 if (do_cmd2(host, tuser, bp, remin, remout) < 0)
631 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000632 free(bp);
Damien Millerf1211432011-01-06 22:40:30 +1100633 (void) close(remin);
634 (void) close(remout);
635 remin = remout = -1;
636 } else if (src) { /* standard remote to remote */
Damien Miller3eec6b72006-01-31 21:49:27 +1100637 freeargs(&alist);
638 addargs(&alist, "%s", ssh_program);
Damien Miller3eec6b72006-01-31 21:49:27 +1100639 addargs(&alist, "-x");
Damien Miller87dc0a42010-12-01 12:03:19 +1100640 addargs(&alist, "-oClearAllForwardings=yes");
Damien Miller3eec6b72006-01-31 21:49:27 +1100641 addargs(&alist, "-n");
Damien Miller87dc0a42010-12-01 12:03:19 +1100642 for (j = 0; j < remote_remote_args.num; j++) {
643 addargs(&alist, "%s",
644 remote_remote_args.list[j]);
645 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000646 *src++ = 0;
647 if (*src == 0)
648 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000649 host = strrchr(argv[i], '@');
Damien Miller3eec6b72006-01-31 21:49:27 +1100650
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000651 if (host) {
652 *host++ = 0;
Damien Miller34132e52000-01-14 15:45:46 +1100653 host = cleanhostname(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000654 suser = argv[i];
655 if (*suser == '\0')
656 suser = pwd->pw_name;
Damien Miller3eec6b72006-01-31 21:49:27 +1100657 else if (!okname(suser))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658 continue;
Damien Miller3eec6b72006-01-31 21:49:27 +1100659 addargs(&alist, "-l");
660 addargs(&alist, "%s", suser);
Damien Miller34132e52000-01-14 15:45:46 +1100661 } else {
662 host = cleanhostname(argv[i]);
Damien Miller34132e52000-01-14 15:45:46 +1100663 }
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100664 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100665 addargs(&alist, "%s", host);
666 addargs(&alist, "%s", cmd);
667 addargs(&alist, "%s", src);
668 addargs(&alist, "%s%s%s:%s",
669 tuser ? tuser : "", tuser ? "@" : "",
670 thost, targ);
671 if (do_local_cmd(&alist) != 0)
Darren Tucker8654d162003-09-22 21:14:55 +1000672 errs = 1;
Damien Miller95def091999-11-25 00:26:21 +1100673 } else { /* local to remote */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000674 if (remin == -1) {
Damien Miller0603d982011-09-22 21:38:00 +1000675 xasprintf(&bp, "%s -t %s%s", cmd,
676 *targ == '-' ? "-- " : "", targ);
Damien Miller34132e52000-01-14 15:45:46 +1100677 host = cleanhostname(thost);
Damien Millerad833b32000-08-23 10:46:23 +1000678 if (do_cmd(host, tuser, bp, &remin,
Damien Miller07aa1322006-04-23 12:04:27 +1000679 &remout) < 0)
Damien Miller95def091999-11-25 00:26:21 +1100680 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681 if (response() < 0)
682 exit(1);
Darren Tuckera627d422013-06-02 07:31:17 +1000683 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000684 }
Damien Miller95def091999-11-25 00:26:21 +1100685 source(1, argv + i);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686 }
687 }
Darren Tuckera627d422013-06-02 07:31:17 +1000688 free(arg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689}
690
691void
Damien Miller65d1f572003-06-04 22:51:08 +1000692tolocal(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693{
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694 char *bp, *host, *src, *suser;
Damien Miller3eec6b72006-01-31 21:49:27 +1100695 arglist alist;
Damien Miller07aa1322006-04-23 12:04:27 +1000696 int i;
Damien Miller3eec6b72006-01-31 21:49:27 +1100697
698 memset(&alist, '\0', sizeof(alist));
699 alist.list = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000700
701 for (i = 0; i < argc - 1; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100702 if (!(src = colon(argv[i]))) { /* Local to local. */
Damien Miller3eec6b72006-01-31 21:49:27 +1100703 freeargs(&alist);
704 addargs(&alist, "%s", _PATH_CP);
705 if (iamrecursive)
706 addargs(&alist, "-r");
707 if (pflag)
708 addargs(&alist, "-p");
Darren Tuckerb8c884a2010-01-08 18:53:43 +1100709 addargs(&alist, "--");
Damien Miller3eec6b72006-01-31 21:49:27 +1100710 addargs(&alist, "%s", argv[i]);
711 addargs(&alist, "%s", argv[argc-1]);
712 if (do_local_cmd(&alist))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713 ++errs;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714 continue;
715 }
716 *src++ = 0;
717 if (*src == 0)
718 src = ".";
Ben Lindstromc276c122002-12-23 02:14:51 +0000719 if ((host = strrchr(argv[i], '@')) == NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720 host = argv[i];
721 suser = NULL;
722 } else {
723 *host++ = 0;
724 suser = argv[i];
725 if (*suser == '\0')
726 suser = pwd->pw_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000727 }
Damien Miller34132e52000-01-14 15:45:46 +1100728 host = cleanhostname(host);
Damien Miller0603d982011-09-22 21:38:00 +1000729 xasprintf(&bp, "%s -f %s%s",
730 cmd, *src == '-' ? "-- " : "", src);
Damien Miller07aa1322006-04-23 12:04:27 +1000731 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000732 free(bp);
Damien Miller95def091999-11-25 00:26:21 +1100733 ++errs;
734 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000735 }
Darren Tuckera627d422013-06-02 07:31:17 +1000736 free(bp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000737 sink(1, argv + argc - 1);
Damien Miller95def091999-11-25 00:26:21 +1100738 (void) close(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000739 remin = remout = -1;
740 }
741}
742
743void
Damien Miller65d1f572003-06-04 22:51:08 +1000744source(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745{
746 struct stat stb;
747 static BUF buffer;
748 BUF *bp;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000749 off_t i, statbytes;
Damien Miller1845fe62014-07-02 15:29:01 +1000750 size_t amt, nr;
Darren Tucker40858532005-08-02 17:07:07 +1000751 int fd = -1, haderr, indx;
Darren Tuckerbed63112007-06-13 00:02:07 +1000752 char *last, *name, buf[2048], encname[MAXPATHLEN];
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000753 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754
755 for (indx = 0; indx < argc; ++indx) {
Damien Miller95def091999-11-25 00:26:21 +1100756 name = argv[indx];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000757 statbytes = 0;
Ben Lindstromd47cf4d2001-04-07 01:14:38 +0000758 len = strlen(name);
759 while (len > 1 && name[len-1] == '/')
760 name[--len] = '\0';
Darren Tucker43ce9022007-06-12 23:41:06 +1000761 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000762 goto syserr;
Darren Tuckerbed63112007-06-13 00:02:07 +1000763 if (strchr(name, '\n') != NULL) {
764 strnvis(encname, name, sizeof(encname), VIS_NL);
765 name = encname;
766 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000767 if (fstat(fd, &stb) < 0) {
768syserr: run_err("%s: %s", name, strerror(errno));
769 goto next;
770 }
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000771 if (stb.st_size < 0) {
772 run_err("%s: %s", name, "Negative file size");
773 goto next;
774 }
Darren Tucker43ce9022007-06-12 23:41:06 +1000775 unset_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000776 switch (stb.st_mode & S_IFMT) {
777 case S_IFREG:
778 break;
779 case S_IFDIR:
780 if (iamrecursive) {
781 rsource(name, &stb);
782 goto next;
783 }
784 /* FALLTHROUGH */
785 default:
786 run_err("%s: not a regular file", name);
787 goto next;
788 }
789 if ((last = strrchr(name, '/')) == NULL)
790 last = name;
791 else
792 ++last;
793 curfile = last;
794 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000795 if (do_times(remout, verbose_mode, &stb) < 0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796 goto next;
797 }
798#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
Ben Lindstroma4c57662001-03-17 00:10:20 +0000799 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000800 (u_int) (stb.st_mode & FILEMODEMASK),
Darren Tuckere0be3042005-11-25 14:44:55 +1100801 (long long)stb.st_size, last);
Damien Miller95def091999-11-25 00:26:21 +1100802 if (verbose_mode) {
803 fprintf(stderr, "Sending file modes: %s", buf);
Damien Miller95def091999-11-25 00:26:21 +1100804 }
Darren Tucker9f63f222003-07-03 13:46:56 +1000805 (void) atomicio(vwrite, remout, buf, strlen(buf));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806 if (response() < 0)
807 goto next;
Damien Millerc77cadb2007-10-26 15:39:15 +1000808 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
Darren Tucker33f86bc2005-11-22 19:38:06 +1100809next: if (fd != -1) {
810 (void) close(fd);
811 fd = -1;
812 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813 continue;
814 }
Damien Miller62d57f62003-01-10 21:43:24 +1100815 if (showprogress)
816 start_progress_meter(curfile, stb.st_size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +1000817 set_nonblock(remout);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
819 amt = bp->cnt;
Darren Tucker03ccc9b2008-06-14 09:02:25 +1000820 if (i + (off_t)amt > stb.st_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821 amt = stb.st_size - i;
822 if (!haderr) {
Damien Miller1845fe62014-07-02 15:29:01 +1000823 if ((nr = atomicio(read, fd,
824 bp->buf, amt)) != amt) {
Damien Millerb253cc42005-05-26 12:23:44 +1000825 haderr = errno;
Damien Miller1845fe62014-07-02 15:29:01 +1000826 memset(bp->buf + nr, 0, amt - nr);
827 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000829 /* Keep writing after error to retain sync */
830 if (haderr) {
831 (void)atomicio(vwrite, remout, bp->buf, amt);
Damien Miller1845fe62014-07-02 15:29:01 +1000832 memset(bp->buf, 0, amt);
Damien Millerc77cadb2007-10-26 15:39:15 +1000833 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000834 }
Damien Miller65e42f82010-09-24 22:15:11 +1000835 if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
Damien Millerc77cadb2007-10-26 15:39:15 +1000836 &statbytes) != amt)
837 haderr = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838 }
Damien Millerc77cadb2007-10-26 15:39:15 +1000839 unset_nonblock(remout);
Damien Miller95def091999-11-25 00:26:21 +1100840 if (showprogress)
Damien Miller62d57f62003-01-10 21:43:24 +1100841 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842
Darren Tucker33f86bc2005-11-22 19:38:06 +1100843 if (fd != -1) {
844 if (close(fd) < 0 && !haderr)
845 haderr = errno;
846 fd = -1;
847 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848 if (!haderr)
Darren Tucker9f63f222003-07-03 13:46:56 +1000849 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000850 else
851 run_err("%s: %s", name, strerror(haderr));
Damien Miller95def091999-11-25 00:26:21 +1100852 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000853 }
854}
855
856void
Damien Miller65d1f572003-06-04 22:51:08 +1000857rsource(char *name, struct stat *statp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000858{
859 DIR *dirp;
860 struct dirent *dp;
Darren Tucker194454d2013-06-06 08:16:04 +1000861 char *last, *vect[1], path[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000862
863 if (!(dirp = opendir(name))) {
864 run_err("%s: %s", name, strerror(errno));
865 return;
866 }
867 last = strrchr(name, '/');
868 if (last == 0)
869 last = name;
870 else
871 last++;
872 if (pflag) {
Damien Miller044bd2a2013-07-18 16:09:25 +1000873 if (do_times(remout, verbose_mode, statp) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874 closedir(dirp);
875 return;
876 }
877 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000878 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
Ben Lindstrom46c16222000-12-22 01:43:59 +0000879 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
Damien Miller95def091999-11-25 00:26:21 +1100880 if (verbose_mode)
881 fprintf(stderr, "Entering directory: %s", path);
Darren Tucker9f63f222003-07-03 13:46:56 +1000882 (void) atomicio(vwrite, remout, path, strlen(path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883 if (response() < 0) {
884 closedir(dirp);
885 return;
886 }
Ben Lindstrombd472262001-03-29 00:39:55 +0000887 while ((dp = readdir(dirp)) != NULL) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888 if (dp->d_ino == 0)
889 continue;
890 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
891 continue;
892 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
893 run_err("%s/%s: name too long", name, dp->d_name);
894 continue;
895 }
Ben Lindstromf6b7b092001-02-09 01:23:39 +0000896 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897 vect[0] = path;
898 source(1, vect);
899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 (void) closedir(dirp);
Darren Tucker9f63f222003-07-03 13:46:56 +1000901 (void) atomicio(vwrite, remout, "E\n", 2);
Damien Miller95def091999-11-25 00:26:21 +1100902 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903}
904
905void
Damien Miller65d1f572003-06-04 22:51:08 +1000906sink(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907{
908 static BUF buffer;
909 struct stat stb;
Damien Miller95def091999-11-25 00:26:21 +1100910 enum {
911 YES, NO, DISPLAYED
912 } wrerr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913 BUF *bp;
Damien Millerb253cc42005-05-26 12:23:44 +1000914 off_t i;
Damien Millereccb9de2005-06-17 12:59:34 +1000915 size_t j, count;
Damien Miller07aa1322006-04-23 12:04:27 +1000916 int amt, exists, first, ofd;
917 mode_t mode, omode, mask;
Damien Miller62d57f62003-01-10 21:43:24 +1100918 off_t size, statbytes;
Damien Miller044bd2a2013-07-18 16:09:25 +1000919 unsigned long long ull;
Damien Millercaf6dd62000-08-29 11:33:50 +1100920 int setimes, targisdir, wrerrno = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000921 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
Damien Miller62cee002000-09-23 17:15:56 +1100922 struct timeval tv[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000923
Ben Lindstromf719a202001-04-14 23:14:22 +0000924#define atime tv[0]
925#define mtime tv[1]
Darren Tuckerf30e1ac2004-11-05 20:10:02 +1100926#define SCREWUP(str) { why = str; goto screwup; }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000927
928 setimes = targisdir = 0;
929 mask = umask(0);
930 if (!pflag)
Damien Miller95def091999-11-25 00:26:21 +1100931 (void) umask(mask);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000932 if (argc != 1) {
933 run_err("ambiguous target");
934 exit(1);
935 }
936 targ = *argv;
937 if (targetshouldbedirectory)
938 verifydir(targ);
Damien Miller95def091999-11-25 00:26:21 +1100939
Darren Tucker9f63f222003-07-03 13:46:56 +1000940 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
942 targisdir = 1;
943 for (first = 1;; first = 0) {
944 cp = buf;
Damien Millerb253cc42005-05-26 12:23:44 +1000945 if (atomicio(read, remin, cp, 1) != 1)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946 return;
947 if (*cp++ == '\n')
948 SCREWUP("unexpected <newline>");
949 do {
Damien Millere247cc42000-05-07 12:03:14 +1000950 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951 SCREWUP("lost connection");
952 *cp++ = ch;
953 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
954 *cp = 0;
Darren Tuckerbddc2b02004-04-19 23:50:16 +1000955 if (verbose_mode)
956 fprintf(stderr, "Sink: %s", buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
958 if (buf[0] == '\01' || buf[0] == '\02') {
959 if (iamremote == 0)
Darren Tucker9f63f222003-07-03 13:46:56 +1000960 (void) atomicio(vwrite, STDERR_FILENO,
Kevin Steves7d00ba42000-12-15 23:03:10 +0000961 buf + 1, strlen(buf + 1));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962 if (buf[0] == '\02')
963 exit(1);
964 ++errs;
965 continue;
966 }
967 if (buf[0] == 'E') {
Darren Tucker9f63f222003-07-03 13:46:56 +1000968 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000969 return;
970 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971 if (ch == '\n')
972 *--cp = 0;
973
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974 cp = buf;
975 if (*cp == 'T') {
976 setimes++;
977 cp++;
Damien Miller044bd2a2013-07-18 16:09:25 +1000978 if (!isdigit((unsigned char)*cp))
979 SCREWUP("mtime.sec not present");
980 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +0000981 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982 SCREWUP("mtime.sec not delimited");
Damien Millerbc35d922013-07-18 16:11:25 +1000983 if ((time_t)ull < 0 ||
984 (unsigned long long)(time_t)ull != ull)
Damien Miller044bd2a2013-07-18 16:09:25 +1000985 setimes = 0; /* out of range */
986 mtime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +0000987 mtime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +1000988 if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
989 mtime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990 SCREWUP("mtime.usec not delimited");
Damien Miller044bd2a2013-07-18 16:09:25 +1000991 if (!isdigit((unsigned char)*cp))
992 SCREWUP("atime.sec not present");
993 ull = strtoull(cp, &cp, 10);
Ben Lindstromf719a202001-04-14 23:14:22 +0000994 if (!cp || *cp++ != ' ')
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995 SCREWUP("atime.sec not delimited");
Damien Millerbc35d922013-07-18 16:11:25 +1000996 if ((time_t)ull < 0 ||
997 (unsigned long long)(time_t)ull != ull)
Damien Miller044bd2a2013-07-18 16:09:25 +1000998 setimes = 0; /* out of range */
999 atime.tv_sec = ull;
Ben Lindstromf719a202001-04-14 23:14:22 +00001000 atime.tv_usec = strtol(cp, &cp, 10);
Damien Miller044bd2a2013-07-18 16:09:25 +10001001 if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1002 atime.tv_usec > 999999)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001003 SCREWUP("atime.usec not delimited");
Darren Tucker9f63f222003-07-03 13:46:56 +10001004 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005 continue;
1006 }
1007 if (*cp != 'C' && *cp != 'D') {
1008 /*
1009 * Check for the case "rcp remote:foo\* local:bar".
1010 * In this case, the line "No match." can be returned
1011 * by the shell before the rcp command on the remote is
1012 * executed so the ^Aerror_message convention isn't
1013 * followed.
1014 */
1015 if (first) {
1016 run_err("%s", cp);
1017 exit(1);
1018 }
1019 SCREWUP("expected control record");
1020 }
1021 mode = 0;
1022 for (++cp; cp < buf + 5; cp++) {
1023 if (*cp < '0' || *cp > '7')
1024 SCREWUP("bad mode");
1025 mode = (mode << 3) | (*cp - '0');
1026 }
1027 if (*cp++ != ' ')
1028 SCREWUP("mode not delimited");
1029
Damien Millere0016732013-11-21 13:56:49 +11001030 for (size = 0; isdigit((unsigned char)*cp);)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031 size = size * 10 + (*cp++ - '0');
1032 if (*cp++ != ' ')
1033 SCREWUP("size not delimited");
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001034 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
1035 run_err("error: unexpected filename: %s", cp);
1036 exit(1);
1037 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001038 if (targisdir) {
1039 static char *namebuf;
Damien Millereccb9de2005-06-17 12:59:34 +10001040 static size_t cursize;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041 size_t need;
1042
1043 need = strlen(targ) + strlen(cp) + 250;
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001044 if (need > cursize) {
Darren Tuckera627d422013-06-02 07:31:17 +10001045 free(namebuf);
Damien Miller95def091999-11-25 00:26:21 +11001046 namebuf = xmalloc(need);
Ben Lindstromf6b7b092001-02-09 01:23:39 +00001047 cursize = need;
1048 }
1049 (void) snprintf(namebuf, need, "%s%s%s", targ,
Ben Lindstromde3895d2002-04-06 18:29:59 +00001050 strcmp(targ, "/") ? "/" : "", cp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051 np = namebuf;
1052 } else
1053 np = targ;
1054 curfile = cp;
1055 exists = stat(np, &stb) == 0;
1056 if (buf[0] == 'D') {
1057 int mod_flag = pflag;
Darren Tuckerbddc2b02004-04-19 23:50:16 +10001058 if (!iamrecursive)
1059 SCREWUP("received directory without -r");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060 if (exists) {
1061 if (!S_ISDIR(stb.st_mode)) {
1062 errno = ENOTDIR;
1063 goto bad;
1064 }
1065 if (pflag)
Damien Miller95def091999-11-25 00:26:21 +11001066 (void) chmod(np, mode);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067 } else {
Damien Miller95def091999-11-25 00:26:21 +11001068 /* Handle copying from a read-only
1069 directory */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070 mod_flag = 1;
1071 if (mkdir(np, mode | S_IRWXU) < 0)
1072 goto bad;
1073 }
Ben Lindstrom550bc542001-02-10 21:50:00 +00001074 vect[0] = xstrdup(np);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001075 sink(1, vect);
1076 if (setimes) {
1077 setimes = 0;
Damien Miller225736c2001-02-19 21:51:08 +11001078 if (utimes(vect[0], tv) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001079 run_err("%s: set times: %s",
Damien Miller225736c2001-02-19 21:51:08 +11001080 vect[0], strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001081 }
1082 if (mod_flag)
Damien Miller225736c2001-02-19 21:51:08 +11001083 (void) chmod(vect[0], mode);
Darren Tuckera627d422013-06-02 07:31:17 +10001084 free(vect[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001085 continue;
1086 }
1087 omode = mode;
Darren Tucker3750fce2013-06-02 07:52:21 +10001088 mode |= S_IWUSR;
Ben Lindstrom7bad55b2001-06-05 19:31:41 +00001089 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090bad: run_err("%s: %s", np, strerror(errno));
1091 continue;
1092 }
Darren Tucker9f63f222003-07-03 13:46:56 +10001093 (void) atomicio(vwrite, remout, "", 1);
Damien Millerc77cadb2007-10-26 15:39:15 +10001094 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001095 (void) close(ofd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001096 continue;
1097 }
1098 cp = bp->buf;
1099 wrerr = NO;
1100
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001101 statbytes = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001102 if (showprogress)
1103 start_progress_meter(curfile, size, &statbytes);
Damien Millerc77cadb2007-10-26 15:39:15 +10001104 set_nonblock(remin);
1105 for (count = i = 0; i < size; i += bp->cnt) {
1106 amt = bp->cnt;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001107 if (i + amt > size)
1108 amt = size - i;
1109 count += amt;
1110 do {
Damien Miller65e42f82010-09-24 22:15:11 +10001111 j = atomicio6(read, remin, cp, amt,
1112 scpio, &statbytes);
Damien Millerb253cc42005-05-26 12:23:44 +10001113 if (j == 0) {
Damien Millerc77cadb2007-10-26 15:39:15 +10001114 run_err("%s", j != EPIPE ?
1115 strerror(errno) :
Ben Lindstrombd472262001-03-29 00:39:55 +00001116 "dropped connection");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001117 exit(1);
1118 }
1119 amt -= j;
1120 cp += j;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121 } while (amt > 0);
Damien Miller787b2ec2003-11-21 23:56:47 +11001122
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001123 if (count == bp->cnt) {
1124 /* Keep reading so we stay sync'd up. */
1125 if (wrerr == NO) {
Damien Millerb253cc42005-05-26 12:23:44 +10001126 if (atomicio(vwrite, ofd, bp->buf,
1127 count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001129 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130 }
1131 }
1132 count = 0;
1133 cp = bp->buf;
1134 }
1135 }
Damien Millerc77cadb2007-10-26 15:39:15 +10001136 unset_nonblock(remin);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137 if (showprogress)
Damien Miller62d57f62003-01-10 21:43:24 +11001138 stop_progress_meter();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001139 if (count != 0 && wrerr == NO &&
Damien Millerb253cc42005-05-26 12:23:44 +10001140 atomicio(vwrite, ofd, bp->buf, count) != count) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141 wrerr = YES;
Damien Millerb253cc42005-05-26 12:23:44 +10001142 wrerrno = errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143 }
Darren Tucker067263e2007-06-25 18:32:33 +10001144 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1145 ftruncate(ofd, size) != 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146 run_err("%s: truncate: %s", np, strerror(errno));
1147 wrerr = DISPLAYED;
1148 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149 if (pflag) {
1150 if (exists || omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001151#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001152 if (fchmod(ofd, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001153#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001154 if (chmod(np, omode)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001155#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001157 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001158 wrerr = DISPLAYED;
1159 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160 } else {
1161 if (!exists && omode != mode)
Damien Miller78315eb2000-09-29 23:01:36 +11001162#ifdef HAVE_FCHMOD
Darren Tuckere1f17052004-07-08 23:11:44 +10001163 if (fchmod(ofd, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001164#else /* HAVE_FCHMOD */
Darren Tuckere1f17052004-07-08 23:11:44 +10001165 if (chmod(np, omode & ~mask)) {
Damien Miller78315eb2000-09-29 23:01:36 +11001166#endif /* HAVE_FCHMOD */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167 run_err("%s: set mode: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001168 np, strerror(errno));
Darren Tuckere1f17052004-07-08 23:11:44 +10001169 wrerr = DISPLAYED;
1170 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171 }
Damien Millerbe484b52000-07-15 14:14:16 +10001172 if (close(ofd) == -1) {
1173 wrerr = YES;
1174 wrerrno = errno;
1175 }
Damien Miller95def091999-11-25 00:26:21 +11001176 (void) response();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177 if (setimes && wrerr == NO) {
1178 setimes = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001179 if (utimes(np, tv) < 0) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180 run_err("%s: set times: %s",
Ben Lindstrombd472262001-03-29 00:39:55 +00001181 np, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182 wrerr = DISPLAYED;
1183 }
1184 }
Damien Miller95def091999-11-25 00:26:21 +11001185 switch (wrerr) {
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186 case YES:
1187 run_err("%s: %s", np, strerror(wrerrno));
1188 break;
1189 case NO:
Darren Tucker9f63f222003-07-03 13:46:56 +10001190 (void) atomicio(vwrite, remout, "", 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191 break;
1192 case DISPLAYED:
1193 break;
1194 }
1195 }
1196screwup:
1197 run_err("protocol error: %s", why);
1198 exit(1);
1199}
1200
1201int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001202response(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203{
1204 char ch, *cp, resp, rbuf[2048];
1205
Damien Millere247cc42000-05-07 12:03:14 +10001206 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207 lostconn(0);
1208
1209 cp = rbuf;
Damien Miller95def091999-11-25 00:26:21 +11001210 switch (resp) {
1211 case 0: /* ok */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212 return (0);
1213 default:
1214 *cp++ = resp;
1215 /* FALLTHROUGH */
Damien Miller95def091999-11-25 00:26:21 +11001216 case 1: /* error, followed by error msg */
1217 case 2: /* fatal error, "" */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218 do {
Damien Millere247cc42000-05-07 12:03:14 +10001219 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220 lostconn(0);
1221 *cp++ = ch;
1222 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1223
1224 if (!iamremote)
Darren Tucker9f63f222003-07-03 13:46:56 +10001225 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226 ++errs;
1227 if (resp == 1)
1228 return (-1);
1229 exit(1);
1230 }
1231 /* NOTREACHED */
1232}
1233
1234void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001235usage(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236{
Ben Lindstrom1e243242001-09-18 05:38:44 +00001237 (void) fprintf(stderr,
Damien Miller907998d2011-01-06 22:41:21 +11001238 "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
Darren Tucker1f203942003-10-15 15:50:42 +10001239 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
Damien Miller647d97b2007-08-08 14:29:58 +10001240 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241 exit(1);
1242}
1243
1244void
Damien Miller95def091999-11-25 00:26:21 +11001245run_err(const char *fmt,...)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
1247 static FILE *fp;
1248 va_list ap;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
1250 ++errs;
Damien Miller745570c2006-03-26 13:49:43 +11001251 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1252 (void) fprintf(fp, "%c", 0x01);
1253 (void) fprintf(fp, "scp: ");
1254 va_start(ap, fmt);
1255 (void) vfprintf(fp, fmt, ap);
1256 va_end(ap);
1257 (void) fprintf(fp, "\n");
1258 (void) fflush(fp);
1259 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260
Damien Miller95def091999-11-25 00:26:21 +11001261 if (!iamremote) {
Damien Miller03783f01999-12-31 09:16:40 +11001262 va_start(ap, fmt);
Damien Miller95def091999-11-25 00:26:21 +11001263 vfprintf(stderr, fmt, ap);
Damien Miller03783f01999-12-31 09:16:40 +11001264 va_end(ap);
Damien Miller95def091999-11-25 00:26:21 +11001265 fprintf(stderr, "\n");
1266 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267}
1268
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269void
Damien Miller65d1f572003-06-04 22:51:08 +10001270verifydir(char *cp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271{
1272 struct stat stb;
1273
1274 if (!stat(cp, &stb)) {
1275 if (S_ISDIR(stb.st_mode))
1276 return;
1277 errno = ENOTDIR;
1278 }
1279 run_err("%s: %s", cp, strerror(errno));
Darren Tuckerfc4f2dd2005-06-01 23:01:12 +10001280 killchild(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281}
1282
1283int
Damien Miller65d1f572003-06-04 22:51:08 +10001284okname(char *cp0)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285{
1286 int c;
1287 char *cp;
1288
1289 cp = cp0;
1290 do {
Ben Lindstrom7d5ed3a2001-06-25 04:28:30 +00001291 c = (int)*cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292 if (c & 0200)
1293 goto bad;
Damien Millere0016732013-11-21 13:56:49 +11001294 if (!isalpha(c) && !isdigit((unsigned char)c)) {
Damien Millereeeeb352003-02-24 11:52:58 +11001295 switch (c) {
1296 case '\'':
1297 case '"':
1298 case '`':
1299 case ' ':
1300 case '#':
1301 goto bad;
1302 default:
1303 break;
1304 }
1305 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001306 } while (*++cp);
1307 return (1);
1308
Damien Miller98c7ad62000-03-09 21:27:49 +11001309bad: fprintf(stderr, "%s: invalid user name\n", cp0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310 return (0);
1311}
1312
1313BUF *
Damien Miller65d1f572003-06-04 22:51:08 +10001314allocbuf(BUF *bp, int fd, int blksize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315{
1316 size_t size;
Tim Rice13aae5e2001-10-21 17:53:58 -07001317#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318 struct stat stb;
1319
1320 if (fstat(fd, &stb) < 0) {
1321 run_err("fstat: %s", strerror(errno));
1322 return (0);
1323 }
Ben Lindstrom418e0782002-12-23 02:22:09 +00001324 size = roundup(stb.st_blksize, blksize);
1325 if (size == 0)
Damien Miller95def091999-11-25 00:26:21 +11001326 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001327#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Kevin Stevesef4eea92001-02-05 12:42:17 +00001328 size = blksize;
Tim Rice13aae5e2001-10-21 17:53:58 -07001329#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001330 if (bp->cnt >= size)
1331 return (bp);
Damien Miller95def091999-11-25 00:26:21 +11001332 if (bp->buf == NULL)
1333 bp->buf = xmalloc(size);
1334 else
Damien Miller36812092006-03-26 14:22:47 +11001335 bp->buf = xrealloc(bp->buf, 1, size);
Ben Lindstrom5fccbc22001-09-12 17:49:48 +00001336 memset(bp->buf, 0, size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337 bp->cnt = size;
1338 return (bp);
1339}
1340
1341void
Damien Miller65d1f572003-06-04 22:51:08 +10001342lostconn(int signo)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343{
1344 if (!iamremote)
Darren Tuckerdbee3082013-05-16 20:32:29 +10001345 (void)write(STDERR_FILENO, "lost connection\n", 16);
Ben Lindstrom738f51e2001-06-21 03:08:58 +00001346 if (signo)
1347 _exit(1);
1348 else
1349 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001350}