scp.c revision 1.27 1 1.27 christos /* $NetBSD: scp.c,v 1.27 2020/05/29 12:14:49 christos Exp $ */
2 1.26 christos /* $OpenBSD: scp.c,v 1.210 2020/05/06 20:57:38 djm Exp $ */
3 1.1 christos /*
4 1.1 christos * scp - secure remote copy. This is basically patched BSD rcp which
5 1.1 christos * uses ssh to do the data transfer (instead of using rcmd).
6 1.1 christos *
7 1.1 christos * NOTE: This version should NOT be suid root. (This uses ssh to
8 1.1 christos * do the transfer and ssh has the necessary privileges.)
9 1.1 christos *
10 1.1 christos * 1995 Timo Rinne <tri (at) iki.fi>, Tatu Ylonen <ylo (at) cs.hut.fi>
11 1.1 christos *
12 1.1 christos * As far as I am concerned, the code I have written for this software
13 1.1 christos * can be used freely for any purpose. Any derived versions of this
14 1.1 christos * software must be clearly marked as such, and if the derived work is
15 1.1 christos * incompatible with the protocol description in the RFC file, it must be
16 1.1 christos * called by a name other than "ssh" or "Secure Shell".
17 1.1 christos */
18 1.1 christos /*
19 1.1 christos * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 1.1 christos * Copyright (c) 1999 Aaron Campbell. All rights reserved.
21 1.1 christos *
22 1.1 christos * Redistribution and use in source and binary forms, with or without
23 1.1 christos * modification, are permitted provided that the following conditions
24 1.1 christos * are met:
25 1.1 christos * 1. Redistributions of source code must retain the above copyright
26 1.1 christos * notice, this list of conditions and the following disclaimer.
27 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
28 1.1 christos * notice, this list of conditions and the following disclaimer in the
29 1.1 christos * documentation and/or other materials provided with the distribution.
30 1.1 christos *
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.1 christos */
42 1.1 christos
43 1.1 christos /*
44 1.1 christos * Parts from:
45 1.1 christos *
46 1.1 christos * Copyright (c) 1983, 1990, 1992, 1993, 1995
47 1.1 christos * The Regents of the University of California. All rights reserved.
48 1.1 christos *
49 1.1 christos * Redistribution and use in source and binary forms, with or without
50 1.1 christos * modification, are permitted provided that the following conditions
51 1.1 christos * are met:
52 1.1 christos * 1. Redistributions of source code must retain the above copyright
53 1.1 christos * notice, this list of conditions and the following disclaimer.
54 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
55 1.1 christos * notice, this list of conditions and the following disclaimer in the
56 1.1 christos * documentation and/or other materials provided with the distribution.
57 1.1 christos * 3. Neither the name of the University nor the names of its contributors
58 1.1 christos * may be used to endorse or promote products derived from this software
59 1.1 christos * without specific prior written permission.
60 1.1 christos *
61 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.1 christos * SUCH DAMAGE.
72 1.1 christos *
73 1.1 christos */
74 1.1 christos
75 1.2 christos #include "includes.h"
76 1.27 christos __RCSID("$NetBSD: scp.c,v 1.27 2020/05/29 12:14:49 christos Exp $");
77 1.15 christos
78 1.11 christos #include <sys/param.h> /* roundup MAX */
79 1.1 christos #include <sys/types.h>
80 1.1 christos #include <sys/poll.h>
81 1.1 christos #include <sys/wait.h>
82 1.1 christos #include <sys/stat.h>
83 1.1 christos #include <sys/time.h>
84 1.1 christos #include <sys/uio.h>
85 1.1 christos
86 1.1 christos #include <ctype.h>
87 1.1 christos #include <dirent.h>
88 1.1 christos #include <errno.h>
89 1.1 christos #include <fcntl.h>
90 1.23 christos #include <fnmatch.h>
91 1.14 christos #include <locale.h>
92 1.1 christos #include <pwd.h>
93 1.1 christos #include <signal.h>
94 1.1 christos #include <stdarg.h>
95 1.17 christos #include <stdint.h>
96 1.1 christos #include <stdio.h>
97 1.1 christos #include <stdlib.h>
98 1.1 christos #include <string.h>
99 1.1 christos #include <time.h>
100 1.1 christos #include <unistd.h>
101 1.11 christos #include <limits.h>
102 1.1 christos #include <vis.h>
103 1.1 christos
104 1.1 christos #include "xmalloc.h"
105 1.18 christos #include "ssh.h"
106 1.1 christos #include "atomicio.h"
107 1.1 christos #include "pathnames.h"
108 1.1 christos #include "log.h"
109 1.1 christos #include "misc.h"
110 1.1 christos #include "progressmeter.h"
111 1.14 christos #include "utf8.h"
112 1.1 christos
113 1.1 christos #define COPY_BUFLEN 16384
114 1.1 christos
115 1.18 christos int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
116 1.18 christos int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
117 1.1 christos
118 1.5 christos static char empty[] = "";
119 1.1 christos
120 1.1 christos /* Struct for addargs */
121 1.1 christos arglist args;
122 1.5 christos arglist remote_remote_args;
123 1.1 christos
124 1.1 christos /* Bandwidth limit */
125 1.5 christos long long limit_kbps = 0;
126 1.5 christos struct bwlimit bwlimit;
127 1.1 christos
128 1.1 christos /* Name of current file being transferred. */
129 1.1 christos char *curfile;
130 1.1 christos
131 1.1 christos /* This is set to non-zero to enable verbose mode. */
132 1.1 christos int verbose_mode = 0;
133 1.1 christos
134 1.1 christos /* This is set to zero if the progressmeter is not desired. */
135 1.1 christos int showprogress = 1;
136 1.1 christos
137 1.5 christos /*
138 1.5 christos * This is set to non-zero if remote-remote copy should be piped
139 1.5 christos * through this process.
140 1.5 christos */
141 1.5 christos int throughlocal = 0;
142 1.5 christos
143 1.18 christos /* Non-standard port to use for the ssh connection or -1. */
144 1.18 christos int sshport = -1;
145 1.18 christos
146 1.1 christos /* This is the program to execute for the secured connection. ("ssh" or -S) */
147 1.2 christos #ifdef RESCUEDIR
148 1.5 christos const char *ssh_program = RESCUEDIR "/ssh";
149 1.2 christos #else
150 1.5 christos const char *ssh_program = _PATH_SSH_PROGRAM;
151 1.2 christos #endif
152 1.1 christos
153 1.1 christos /* This is used to store the pid of ssh_program */
154 1.1 christos pid_t do_cmd_pid = -1;
155 1.1 christos
156 1.6 joerg __dead static void
157 1.1 christos killchild(int signo)
158 1.1 christos {
159 1.1 christos if (do_cmd_pid > 1) {
160 1.1 christos kill(do_cmd_pid, signo ? signo : SIGTERM);
161 1.1 christos waitpid(do_cmd_pid, NULL, 0);
162 1.1 christos }
163 1.1 christos
164 1.1 christos if (signo)
165 1.1 christos _exit(1);
166 1.1 christos exit(1);
167 1.1 christos }
168 1.1 christos
169 1.4 adam static void
170 1.4 adam suspchild(int signo)
171 1.4 adam {
172 1.4 adam int status;
173 1.4 adam
174 1.4 adam if (do_cmd_pid > 1) {
175 1.4 adam kill(do_cmd_pid, signo);
176 1.4 adam while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
177 1.4 adam errno == EINTR)
178 1.4 adam ;
179 1.4 adam kill(getpid(), SIGSTOP);
180 1.4 adam }
181 1.4 adam }
182 1.4 adam
183 1.1 christos static int
184 1.1 christos do_local_cmd(arglist *a)
185 1.1 christos {
186 1.1 christos u_int i;
187 1.1 christos int status;
188 1.1 christos pid_t pid;
189 1.1 christos
190 1.1 christos if (a->num == 0)
191 1.1 christos fatal("do_local_cmd: no arguments");
192 1.1 christos
193 1.1 christos if (verbose_mode) {
194 1.1 christos fprintf(stderr, "Executing:");
195 1.1 christos for (i = 0; i < a->num; i++)
196 1.14 christos fmprintf(stderr, " %s", a->list[i]);
197 1.1 christos fprintf(stderr, "\n");
198 1.1 christos }
199 1.1 christos if ((pid = fork()) == -1)
200 1.1 christos fatal("do_local_cmd: fork: %s", strerror(errno));
201 1.1 christos
202 1.1 christos if (pid == 0) {
203 1.1 christos execvp(a->list[0], a->list);
204 1.1 christos perror(a->list[0]);
205 1.1 christos exit(1);
206 1.1 christos }
207 1.1 christos
208 1.1 christos do_cmd_pid = pid;
209 1.25 christos ssh_signal(SIGTERM, killchild);
210 1.25 christos ssh_signal(SIGINT, killchild);
211 1.25 christos ssh_signal(SIGHUP, killchild);
212 1.1 christos
213 1.1 christos while (waitpid(pid, &status, 0) == -1)
214 1.1 christos if (errno != EINTR)
215 1.1 christos fatal("do_local_cmd: waitpid: %s", strerror(errno));
216 1.1 christos
217 1.1 christos do_cmd_pid = -1;
218 1.1 christos
219 1.1 christos if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
220 1.1 christos return (-1);
221 1.1 christos
222 1.1 christos return (0);
223 1.1 christos }
224 1.1 christos
225 1.1 christos /*
226 1.1 christos * This function executes the given command as the specified user on the
227 1.1 christos * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
228 1.1 christos * assigns the input and output file descriptors on success.
229 1.1 christos */
230 1.1 christos
231 1.1 christos int
232 1.18 christos do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
233 1.1 christos {
234 1.1 christos int pin[2], pout[2], reserved[2];
235 1.1 christos
236 1.1 christos if (verbose_mode)
237 1.14 christos fmprintf(stderr,
238 1.1 christos "Executing: program %s host %s, user %s, command %s\n",
239 1.1 christos ssh_program, host,
240 1.1 christos remuser ? remuser : "(unspecified)", cmd);
241 1.1 christos
242 1.18 christos if (port == -1)
243 1.18 christos port = sshport;
244 1.18 christos
245 1.1 christos /*
246 1.1 christos * Reserve two descriptors so that the real pipes won't get
247 1.1 christos * descriptors 0 and 1 because that will screw up dup2 below.
248 1.1 christos */
249 1.24 christos if (pipe(reserved) == -1)
250 1.1 christos fatal("pipe: %s", strerror(errno));
251 1.1 christos
252 1.1 christos /* Create a socket pair for communicating with ssh. */
253 1.24 christos if (pipe(pin) == -1)
254 1.1 christos fatal("pipe: %s", strerror(errno));
255 1.24 christos if (pipe(pout) == -1)
256 1.1 christos fatal("pipe: %s", strerror(errno));
257 1.1 christos
258 1.1 christos /* Free the reserved descriptors. */
259 1.1 christos close(reserved[0]);
260 1.1 christos close(reserved[1]);
261 1.1 christos
262 1.25 christos ssh_signal(SIGTSTP, suspchild);
263 1.25 christos ssh_signal(SIGTTIN, suspchild);
264 1.25 christos ssh_signal(SIGTTOU, suspchild);
265 1.4 adam
266 1.1 christos /* Fork a child to execute the command on the remote host using ssh. */
267 1.1 christos do_cmd_pid = fork();
268 1.1 christos if (do_cmd_pid == 0) {
269 1.1 christos /* Child. */
270 1.1 christos close(pin[1]);
271 1.1 christos close(pout[0]);
272 1.1 christos dup2(pin[0], 0);
273 1.1 christos dup2(pout[1], 1);
274 1.1 christos close(pin[0]);
275 1.1 christos close(pout[1]);
276 1.1 christos
277 1.1 christos replacearg(&args, 0, "%s", ssh_program);
278 1.18 christos if (port != -1) {
279 1.18 christos addargs(&args, "-p");
280 1.18 christos addargs(&args, "%d", port);
281 1.18 christos }
282 1.4 adam if (remuser != NULL) {
283 1.4 adam addargs(&args, "-l");
284 1.4 adam addargs(&args, "%s", remuser);
285 1.4 adam }
286 1.4 adam addargs(&args, "--");
287 1.1 christos addargs(&args, "%s", host);
288 1.1 christos addargs(&args, "%s", cmd);
289 1.1 christos
290 1.1 christos execvp(ssh_program, args.list);
291 1.1 christos perror(ssh_program);
292 1.1 christos exit(1);
293 1.1 christos } else if (do_cmd_pid == -1) {
294 1.1 christos fatal("fork: %s", strerror(errno));
295 1.1 christos }
296 1.1 christos /* Parent. Close the other side, and return the local side. */
297 1.1 christos close(pin[0]);
298 1.1 christos *fdout = pin[1];
299 1.1 christos close(pout[1]);
300 1.1 christos *fdin = pout[0];
301 1.25 christos ssh_signal(SIGTERM, killchild);
302 1.25 christos ssh_signal(SIGINT, killchild);
303 1.25 christos ssh_signal(SIGHUP, killchild);
304 1.1 christos return 0;
305 1.1 christos }
306 1.1 christos
307 1.5 christos /*
308 1.20 christos * This function executes a command similar to do_cmd(), but expects the
309 1.5 christos * input and output descriptors to be setup by a previous call to do_cmd().
310 1.5 christos * This way the input and output of two commands can be connected.
311 1.5 christos */
312 1.5 christos int
313 1.18 christos do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
314 1.5 christos {
315 1.5 christos pid_t pid;
316 1.5 christos int status;
317 1.5 christos
318 1.5 christos if (verbose_mode)
319 1.14 christos fmprintf(stderr,
320 1.5 christos "Executing: 2nd program %s host %s, user %s, command %s\n",
321 1.5 christos ssh_program, host,
322 1.5 christos remuser ? remuser : "(unspecified)", cmd);
323 1.5 christos
324 1.18 christos if (port == -1)
325 1.18 christos port = sshport;
326 1.18 christos
327 1.5 christos /* Fork a child to execute the command on the remote host using ssh. */
328 1.5 christos pid = fork();
329 1.5 christos if (pid == 0) {
330 1.5 christos dup2(fdin, 0);
331 1.5 christos dup2(fdout, 1);
332 1.5 christos
333 1.5 christos replacearg(&args, 0, "%s", ssh_program);
334 1.18 christos if (port != -1) {
335 1.18 christos addargs(&args, "-p");
336 1.18 christos addargs(&args, "%d", port);
337 1.18 christos }
338 1.5 christos if (remuser != NULL) {
339 1.5 christos addargs(&args, "-l");
340 1.5 christos addargs(&args, "%s", remuser);
341 1.5 christos }
342 1.26 christos addargs(&args, "-oBatchMode=yes");
343 1.5 christos addargs(&args, "--");
344 1.5 christos addargs(&args, "%s", host);
345 1.5 christos addargs(&args, "%s", cmd);
346 1.5 christos
347 1.5 christos execvp(ssh_program, args.list);
348 1.5 christos perror(ssh_program);
349 1.5 christos exit(1);
350 1.5 christos } else if (pid == -1) {
351 1.5 christos fatal("fork: %s", strerror(errno));
352 1.5 christos }
353 1.5 christos while (waitpid(pid, &status, 0) == -1)
354 1.5 christos if (errno != EINTR)
355 1.5 christos fatal("do_cmd2: waitpid: %s", strerror(errno));
356 1.5 christos return 0;
357 1.5 christos }
358 1.5 christos
359 1.1 christos typedef struct {
360 1.1 christos size_t cnt;
361 1.1 christos char *buf;
362 1.1 christos } BUF;
363 1.1 christos
364 1.1 christos BUF *allocbuf(BUF *, int, int);
365 1.6 joerg __dead static void lostconn(int);
366 1.1 christos int okname(char *);
367 1.8 joerg void run_err(const char *,...) __printflike(1, 2);
368 1.26 christos void run_err(const char *,...) __printflike(1, 2);
369 1.27 christos int note_err(const char *,...) __printflike(1, 2);
370 1.1 christos void verifydir(char *);
371 1.1 christos
372 1.1 christos struct passwd *pwd;
373 1.1 christos uid_t userid;
374 1.1 christos int errs, remin, remout;
375 1.23 christos int Tflag, pflag, iamremote, iamrecursive, targetshouldbedirectory;
376 1.1 christos
377 1.1 christos #define CMDNEEDS 64
378 1.1 christos char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
379 1.1 christos
380 1.1 christos int response(void);
381 1.1 christos void rsource(char *, struct stat *);
382 1.23 christos void sink(int, char *[], const char *);
383 1.1 christos void source(int, char *[]);
384 1.6 joerg static void tolocal(int, char *[]);
385 1.18 christos static void toremote(int, char *[]);
386 1.6 joerg __dead static void usage(void);
387 1.1 christos
388 1.1 christos int
389 1.1 christos main(int argc, char **argv)
390 1.1 christos {
391 1.1 christos int ch, fflag, tflag, status, n;
392 1.18 christos char **newargv;
393 1.5 christos const char *errstr;
394 1.1 christos extern char *optarg;
395 1.1 christos extern int optind;
396 1.1 christos
397 1.1 christos /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
398 1.1 christos sanitise_stdfd();
399 1.1 christos
400 1.14 christos setlocale(LC_CTYPE, "");
401 1.14 christos
402 1.1 christos /* Copy argv, because we modify it */
403 1.15 christos newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
404 1.1 christos for (n = 0; n < argc; n++)
405 1.1 christos newargv[n] = xstrdup(argv[n]);
406 1.1 christos argv = newargv;
407 1.1 christos
408 1.1 christos memset(&args, '\0', sizeof(args));
409 1.5 christos memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
410 1.5 christos args.list = remote_remote_args.list = NULL;
411 1.1 christos addargs(&args, "%s", ssh_program);
412 1.1 christos addargs(&args, "-x");
413 1.5 christos addargs(&args, "-oForwardAgent=no");
414 1.5 christos addargs(&args, "-oPermitLocalCommand=no");
415 1.5 christos addargs(&args, "-oClearAllForwardings=yes");
416 1.18 christos addargs(&args, "-oRemoteCommand=none");
417 1.18 christos addargs(&args, "-oRequestTTY=no");
418 1.1 christos
419 1.23 christos fflag = Tflag = tflag = 0;
420 1.23 christos while ((ch = getopt(argc, argv,
421 1.23 christos "dfl:prtTvBCc:i:P:q12346S:o:F:J:")) != -1) {
422 1.1 christos switch (ch) {
423 1.1 christos /* User-visible flags. */
424 1.1 christos case '1':
425 1.17 christos fatal("SSH protocol v.1 is no longer supported");
426 1.17 christos break;
427 1.1 christos case '2':
428 1.17 christos /* Ignored */
429 1.17 christos break;
430 1.1 christos case '4':
431 1.1 christos case '6':
432 1.1 christos case 'C':
433 1.1 christos addargs(&args, "-%c", ch);
434 1.5 christos addargs(&remote_remote_args, "-%c", ch);
435 1.5 christos break;
436 1.5 christos case '3':
437 1.5 christos throughlocal = 1;
438 1.1 christos break;
439 1.1 christos case 'o':
440 1.1 christos case 'c':
441 1.1 christos case 'i':
442 1.1 christos case 'F':
443 1.23 christos case 'J':
444 1.5 christos addargs(&remote_remote_args, "-%c", ch);
445 1.5 christos addargs(&remote_remote_args, "%s", optarg);
446 1.4 adam addargs(&args, "-%c", ch);
447 1.4 adam addargs(&args, "%s", optarg);
448 1.1 christos break;
449 1.1 christos case 'P':
450 1.18 christos sshport = a2port(optarg);
451 1.18 christos if (sshport <= 0)
452 1.18 christos fatal("bad port \"%s\"\n", optarg);
453 1.1 christos break;
454 1.1 christos case 'B':
455 1.5 christos addargs(&remote_remote_args, "-oBatchmode=yes");
456 1.5 christos addargs(&args, "-oBatchmode=yes");
457 1.1 christos break;
458 1.1 christos case 'l':
459 1.5 christos limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
460 1.5 christos &errstr);
461 1.5 christos if (errstr != NULL)
462 1.1 christos usage();
463 1.5 christos limit_kbps *= 1024; /* kbps */
464 1.5 christos bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
465 1.1 christos break;
466 1.1 christos case 'p':
467 1.1 christos pflag = 1;
468 1.1 christos break;
469 1.1 christos case 'r':
470 1.1 christos iamrecursive = 1;
471 1.1 christos break;
472 1.1 christos case 'S':
473 1.1 christos ssh_program = xstrdup(optarg);
474 1.1 christos break;
475 1.1 christos case 'v':
476 1.1 christos addargs(&args, "-v");
477 1.5 christos addargs(&remote_remote_args, "-v");
478 1.1 christos verbose_mode = 1;
479 1.1 christos break;
480 1.1 christos case 'q':
481 1.1 christos addargs(&args, "-q");
482 1.5 christos addargs(&remote_remote_args, "-q");
483 1.1 christos showprogress = 0;
484 1.1 christos break;
485 1.1 christos
486 1.1 christos /* Server options. */
487 1.1 christos case 'd':
488 1.1 christos targetshouldbedirectory = 1;
489 1.1 christos break;
490 1.1 christos case 'f': /* "from" */
491 1.1 christos iamremote = 1;
492 1.1 christos fflag = 1;
493 1.1 christos break;
494 1.1 christos case 't': /* "to" */
495 1.1 christos iamremote = 1;
496 1.1 christos tflag = 1;
497 1.1 christos break;
498 1.23 christos case 'T':
499 1.23 christos Tflag = 1;
500 1.23 christos break;
501 1.1 christos default:
502 1.1 christos usage();
503 1.1 christos }
504 1.23 christos }
505 1.1 christos argc -= optind;
506 1.1 christos argv += optind;
507 1.1 christos
508 1.1 christos if ((pwd = getpwuid(userid = getuid())) == NULL)
509 1.1 christos fatal("unknown user %u", (u_int) userid);
510 1.1 christos
511 1.1 christos if (!isatty(STDOUT_FILENO))
512 1.1 christos showprogress = 0;
513 1.1 christos
514 1.13 christos if (pflag) {
515 1.13 christos /* Cannot pledge: -p allows setuid/setgid files... */
516 1.13 christos } else {
517 1.13 christos #ifdef __OpenBSD__
518 1.13 christos if (pledge("stdio rpath wpath cpath fattr tty proc exec",
519 1.13 christos NULL) == -1) {
520 1.13 christos perror("pledge");
521 1.13 christos exit(1);
522 1.13 christos }
523 1.13 christos #endif
524 1.13 christos }
525 1.13 christos
526 1.1 christos remin = STDIN_FILENO;
527 1.1 christos remout = STDOUT_FILENO;
528 1.1 christos
529 1.1 christos if (fflag) {
530 1.1 christos /* Follow "protocol", send data. */
531 1.1 christos (void) response();
532 1.1 christos source(argc, argv);
533 1.1 christos exit(errs != 0);
534 1.1 christos }
535 1.1 christos if (tflag) {
536 1.1 christos /* Receive data. */
537 1.23 christos sink(argc, argv, NULL);
538 1.1 christos exit(errs != 0);
539 1.1 christos }
540 1.1 christos if (argc < 2)
541 1.1 christos usage();
542 1.1 christos if (argc > 2)
543 1.1 christos targetshouldbedirectory = 1;
544 1.1 christos
545 1.1 christos remin = remout = -1;
546 1.1 christos do_cmd_pid = -1;
547 1.1 christos /* Command to be executed on remote system using "ssh". */
548 1.1 christos (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
549 1.1 christos verbose_mode ? " -v" : "",
550 1.1 christos iamrecursive ? " -r" : "", pflag ? " -p" : "",
551 1.1 christos targetshouldbedirectory ? " -d" : "");
552 1.1 christos
553 1.25 christos (void) ssh_signal(SIGPIPE, lostconn);
554 1.1 christos
555 1.18 christos if (colon(argv[argc - 1])) /* Dest is remote host. */
556 1.18 christos toremote(argc, argv);
557 1.1 christos else {
558 1.1 christos if (targetshouldbedirectory)
559 1.1 christos verifydir(argv[argc - 1]);
560 1.1 christos tolocal(argc, argv); /* Dest is local host. */
561 1.1 christos }
562 1.1 christos /*
563 1.1 christos * Finally check the exit status of the ssh process, if one was forked
564 1.1 christos * and no error has occurred yet
565 1.1 christos */
566 1.1 christos if (do_cmd_pid != -1 && errs == 0) {
567 1.1 christos if (remin != -1)
568 1.1 christos (void) close(remin);
569 1.1 christos if (remout != -1)
570 1.1 christos (void) close(remout);
571 1.1 christos if (waitpid(do_cmd_pid, &status, 0) == -1)
572 1.1 christos errs = 1;
573 1.1 christos else {
574 1.1 christos if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
575 1.1 christos errs = 1;
576 1.1 christos }
577 1.1 christos }
578 1.1 christos exit(errs != 0);
579 1.1 christos }
580 1.1 christos
581 1.5 christos /* Callback from atomicio6 to update progress meter and limit bandwidth */
582 1.5 christos static int
583 1.5 christos scpio(void *_cnt, size_t s)
584 1.1 christos {
585 1.5 christos off_t *cnt = (off_t *)_cnt;
586 1.5 christos
587 1.5 christos *cnt += s;
588 1.23 christos refresh_progress_meter(0);
589 1.5 christos if (limit_kbps > 0)
590 1.5 christos bandwidth_limit(&bwlimit, s);
591 1.5 christos return 0;
592 1.1 christos }
593 1.1 christos
594 1.9 christos static int
595 1.9 christos do_times(int fd, int verb, const struct stat *sb)
596 1.9 christos {
597 1.9 christos /* strlen(2^64) == 20; strlen(10^6) == 7 */
598 1.9 christos char buf[(20 + 7 + 2) * 2 + 2];
599 1.9 christos
600 1.9 christos (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
601 1.9 christos (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
602 1.9 christos (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
603 1.9 christos if (verb) {
604 1.9 christos fprintf(stderr, "File mtime %lld atime %lld\n",
605 1.9 christos (long long)sb->st_mtime, (long long)sb->st_atime);
606 1.9 christos fprintf(stderr, "Sending file timestamps: %s", buf);
607 1.9 christos }
608 1.9 christos (void) atomicio(vwrite, fd, buf, strlen(buf));
609 1.9 christos return (response());
610 1.9 christos }
611 1.9 christos
612 1.18 christos static int
613 1.18 christos parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
614 1.19 christos const char **pathp)
615 1.18 christos {
616 1.18 christos int r;
617 1.18 christos
618 1.18 christos r = parse_uri("scp", uri, userp, hostp, portp, pathp);
619 1.18 christos if (r == 0 && *pathp == NULL)
620 1.18 christos *pathp = xstrdup(".");
621 1.18 christos return r;
622 1.18 christos }
623 1.18 christos
624 1.23 christos /* Appends a string to an array; returns 0 on success, -1 on alloc failure */
625 1.23 christos static int
626 1.23 christos append(char *cp, char ***ap, size_t *np)
627 1.23 christos {
628 1.23 christos char **tmp;
629 1.23 christos
630 1.23 christos if ((tmp = reallocarray(*ap, *np + 1, sizeof(*tmp))) == NULL)
631 1.23 christos return -1;
632 1.23 christos tmp[(*np)] = cp;
633 1.23 christos (*np)++;
634 1.23 christos *ap = tmp;
635 1.23 christos return 0;
636 1.23 christos }
637 1.23 christos
638 1.23 christos /*
639 1.23 christos * Finds the start and end of the first brace pair in the pattern.
640 1.23 christos * returns 0 on success or -1 for invalid patterns.
641 1.23 christos */
642 1.23 christos static int
643 1.23 christos find_brace(const char *pattern, int *startp, int *endp)
644 1.23 christos {
645 1.23 christos int i;
646 1.23 christos int in_bracket, brace_level;
647 1.23 christos
648 1.23 christos *startp = *endp = -1;
649 1.23 christos in_bracket = brace_level = 0;
650 1.23 christos for (i = 0; i < INT_MAX && *endp < 0 && pattern[i] != '\0'; i++) {
651 1.23 christos switch (pattern[i]) {
652 1.23 christos case '\\':
653 1.23 christos /* skip next character */
654 1.23 christos if (pattern[i + 1] != '\0')
655 1.23 christos i++;
656 1.23 christos break;
657 1.23 christos case '[':
658 1.23 christos in_bracket = 1;
659 1.23 christos break;
660 1.23 christos case ']':
661 1.23 christos in_bracket = 0;
662 1.23 christos break;
663 1.23 christos case '{':
664 1.23 christos if (in_bracket)
665 1.23 christos break;
666 1.23 christos if (pattern[i + 1] == '}') {
667 1.23 christos /* Protect a single {}, for find(1), like csh */
668 1.23 christos i++; /* skip */
669 1.23 christos break;
670 1.23 christos }
671 1.23 christos if (*startp == -1)
672 1.23 christos *startp = i;
673 1.23 christos brace_level++;
674 1.23 christos break;
675 1.23 christos case '}':
676 1.23 christos if (in_bracket)
677 1.23 christos break;
678 1.23 christos if (*startp < 0) {
679 1.23 christos /* Unbalanced brace */
680 1.23 christos return -1;
681 1.23 christos }
682 1.23 christos if (--brace_level <= 0)
683 1.23 christos *endp = i;
684 1.23 christos break;
685 1.23 christos }
686 1.23 christos }
687 1.23 christos /* unbalanced brackets/braces */
688 1.23 christos if (*endp < 0 && (*startp >= 0 || in_bracket))
689 1.23 christos return -1;
690 1.23 christos return 0;
691 1.23 christos }
692 1.23 christos
693 1.23 christos /*
694 1.23 christos * Assembles and records a successfully-expanded pattern, returns -1 on
695 1.23 christos * alloc failure.
696 1.23 christos */
697 1.23 christos static int
698 1.23 christos emit_expansion(const char *pattern, int brace_start, int brace_end,
699 1.23 christos int sel_start, int sel_end, char ***patternsp, size_t *npatternsp)
700 1.23 christos {
701 1.23 christos char *cp;
702 1.23 christos int o = 0, tail_len = strlen(pattern + brace_end + 1);
703 1.23 christos
704 1.23 christos if ((cp = malloc(brace_start + (sel_end - sel_start) +
705 1.23 christos tail_len + 1)) == NULL)
706 1.23 christos return -1;
707 1.23 christos
708 1.23 christos /* Pattern before initial brace */
709 1.23 christos if (brace_start > 0) {
710 1.23 christos memcpy(cp, pattern, brace_start);
711 1.23 christos o = brace_start;
712 1.23 christos }
713 1.23 christos /* Current braced selection */
714 1.23 christos if (sel_end - sel_start > 0) {
715 1.23 christos memcpy(cp + o, pattern + sel_start,
716 1.23 christos sel_end - sel_start);
717 1.23 christos o += sel_end - sel_start;
718 1.23 christos }
719 1.23 christos /* Remainder of pattern after closing brace */
720 1.23 christos if (tail_len > 0) {
721 1.23 christos memcpy(cp + o, pattern + brace_end + 1, tail_len);
722 1.23 christos o += tail_len;
723 1.23 christos }
724 1.23 christos cp[o] = '\0';
725 1.23 christos if (append(cp, patternsp, npatternsp) != 0) {
726 1.23 christos free(cp);
727 1.23 christos return -1;
728 1.23 christos }
729 1.23 christos return 0;
730 1.23 christos }
731 1.23 christos
732 1.23 christos /*
733 1.23 christos * Expand the first encountered brace in pattern, appending the expanded
734 1.23 christos * patterns it yielded to the *patternsp array.
735 1.23 christos *
736 1.23 christos * Returns 0 on success or -1 on allocation failure.
737 1.23 christos *
738 1.23 christos * Signals whether expansion was performed via *expanded and whether
739 1.23 christos * pattern was invalid via *invalid.
740 1.23 christos */
741 1.23 christos static int
742 1.23 christos brace_expand_one(const char *pattern, char ***patternsp, size_t *npatternsp,
743 1.23 christos int *expanded, int *invalid)
744 1.23 christos {
745 1.23 christos int i;
746 1.23 christos int in_bracket, brace_start, brace_end, brace_level;
747 1.23 christos int sel_start, sel_end;
748 1.23 christos
749 1.23 christos *invalid = *expanded = 0;
750 1.23 christos
751 1.23 christos if (find_brace(pattern, &brace_start, &brace_end) != 0) {
752 1.23 christos *invalid = 1;
753 1.23 christos return 0;
754 1.23 christos } else if (brace_start == -1)
755 1.23 christos return 0;
756 1.23 christos
757 1.23 christos in_bracket = brace_level = 0;
758 1.23 christos for (i = sel_start = brace_start + 1; i < brace_end; i++) {
759 1.23 christos switch (pattern[i]) {
760 1.23 christos case '{':
761 1.23 christos if (in_bracket)
762 1.23 christos break;
763 1.23 christos brace_level++;
764 1.23 christos break;
765 1.23 christos case '}':
766 1.23 christos if (in_bracket)
767 1.23 christos break;
768 1.23 christos brace_level--;
769 1.23 christos break;
770 1.23 christos case '[':
771 1.23 christos in_bracket = 1;
772 1.23 christos break;
773 1.23 christos case ']':
774 1.23 christos in_bracket = 0;
775 1.23 christos break;
776 1.23 christos case '\\':
777 1.23 christos if (i < brace_end - 1)
778 1.23 christos i++; /* skip */
779 1.23 christos break;
780 1.23 christos }
781 1.23 christos if (pattern[i] == ',' || i == brace_end - 1) {
782 1.23 christos if (in_bracket || brace_level > 0)
783 1.23 christos continue;
784 1.23 christos /* End of a selection, emit an expanded pattern */
785 1.23 christos
786 1.23 christos /* Adjust end index for last selection */
787 1.23 christos sel_end = (i == brace_end - 1) ? brace_end : i;
788 1.23 christos if (emit_expansion(pattern, brace_start, brace_end,
789 1.23 christos sel_start, sel_end, patternsp, npatternsp) != 0)
790 1.23 christos return -1;
791 1.23 christos /* move on to the next selection */
792 1.23 christos sel_start = i + 1;
793 1.23 christos continue;
794 1.23 christos }
795 1.23 christos }
796 1.23 christos if (in_bracket || brace_level > 0) {
797 1.23 christos *invalid = 1;
798 1.23 christos return 0;
799 1.23 christos }
800 1.23 christos /* success */
801 1.23 christos *expanded = 1;
802 1.23 christos return 0;
803 1.23 christos }
804 1.23 christos
805 1.23 christos /* Expand braces from pattern. Returns 0 on success, -1 on failure */
806 1.23 christos static int
807 1.23 christos brace_expand(const char *pattern, char ***patternsp, size_t *npatternsp)
808 1.23 christos {
809 1.23 christos char *cp, *cp2, **active = NULL, **done = NULL;
810 1.23 christos size_t i, nactive = 0, ndone = 0;
811 1.23 christos int ret = -1, invalid = 0, expanded = 0;
812 1.23 christos
813 1.23 christos *patternsp = NULL;
814 1.23 christos *npatternsp = 0;
815 1.23 christos
816 1.23 christos /* Start the worklist with the original pattern */
817 1.23 christos if ((cp = strdup(pattern)) == NULL)
818 1.23 christos return -1;
819 1.23 christos if (append(cp, &active, &nactive) != 0) {
820 1.23 christos free(cp);
821 1.23 christos return -1;
822 1.23 christos }
823 1.23 christos while (nactive > 0) {
824 1.23 christos cp = active[nactive - 1];
825 1.23 christos nactive--;
826 1.23 christos if (brace_expand_one(cp, &active, &nactive,
827 1.23 christos &expanded, &invalid) == -1) {
828 1.23 christos free(cp);
829 1.23 christos goto fail;
830 1.23 christos }
831 1.23 christos if (invalid)
832 1.23 christos fatal("%s: invalid brace pattern \"%s\"", __func__, cp);
833 1.23 christos if (expanded) {
834 1.23 christos /*
835 1.23 christos * Current entry expanded to new entries on the
836 1.23 christos * active list; discard the progenitor pattern.
837 1.23 christos */
838 1.23 christos free(cp);
839 1.23 christos continue;
840 1.23 christos }
841 1.23 christos /*
842 1.23 christos * Pattern did not expand; append the finename component to
843 1.23 christos * the completed list
844 1.23 christos */
845 1.23 christos if ((cp2 = strrchr(cp, '/')) != NULL)
846 1.23 christos *cp2++ = '\0';
847 1.23 christos else
848 1.23 christos cp2 = cp;
849 1.23 christos if (append(xstrdup(cp2), &done, &ndone) != 0) {
850 1.23 christos free(cp);
851 1.23 christos goto fail;
852 1.23 christos }
853 1.23 christos free(cp);
854 1.23 christos }
855 1.23 christos /* success */
856 1.23 christos *patternsp = done;
857 1.23 christos *npatternsp = ndone;
858 1.23 christos done = NULL;
859 1.23 christos ndone = 0;
860 1.23 christos ret = 0;
861 1.23 christos fail:
862 1.23 christos for (i = 0; i < nactive; i++)
863 1.23 christos free(active[i]);
864 1.23 christos free(active);
865 1.23 christos for (i = 0; i < ndone; i++)
866 1.23 christos free(done[i]);
867 1.23 christos free(done);
868 1.23 christos return ret;
869 1.23 christos }
870 1.23 christos
871 1.9 christos void
872 1.18 christos toremote(int argc, char **argv)
873 1.1 christos {
874 1.19 christos char *suser = NULL, *host = NULL;
875 1.19 christos const char *src = NULL;
876 1.19 christos char *bp, *tuser, *thost;
877 1.19 christos const char *targ;
878 1.18 christos int sport = -1, tport = -1;
879 1.1 christos arglist alist;
880 1.18 christos int i, r;
881 1.5 christos u_int j;
882 1.1 christos
883 1.1 christos memset(&alist, '\0', sizeof(alist));
884 1.1 christos alist.list = NULL;
885 1.1 christos
886 1.18 christos /* Parse target */
887 1.18 christos r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ);
888 1.18 christos if (r == -1) {
889 1.18 christos fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]);
890 1.18 christos ++errs;
891 1.18 christos goto out;
892 1.18 christos }
893 1.18 christos if (r != 0) {
894 1.18 christos if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
895 1.18 christos &targ) == -1) {
896 1.18 christos fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
897 1.18 christos ++errs;
898 1.18 christos goto out;
899 1.18 christos }
900 1.1 christos }
901 1.1 christos if (tuser != NULL && !okname(tuser)) {
902 1.18 christos ++errs;
903 1.18 christos goto out;
904 1.1 christos }
905 1.1 christos
906 1.18 christos /* Parse source files */
907 1.1 christos for (i = 0; i < argc - 1; i++) {
908 1.18 christos free(suser);
909 1.18 christos free(host);
910 1.19 christos free(__UNCONST(src));
911 1.18 christos r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
912 1.18 christos if (r == -1) {
913 1.18 christos fmprintf(stderr, "%s: invalid uri\n", argv[i]);
914 1.18 christos ++errs;
915 1.18 christos continue;
916 1.18 christos }
917 1.18 christos if (r != 0) {
918 1.18 christos parse_user_host_path(argv[i], &suser, &host, &src);
919 1.18 christos }
920 1.18 christos if (suser != NULL && !okname(suser)) {
921 1.18 christos ++errs;
922 1.18 christos continue;
923 1.18 christos }
924 1.18 christos if (host && throughlocal) { /* extended remote to remote */
925 1.7 christos xasprintf(&bp, "%s -f %s%s", cmd,
926 1.7 christos *src == '-' ? "-- " : "", src);
927 1.18 christos if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
928 1.5 christos exit(1);
929 1.9 christos free(bp);
930 1.7 christos xasprintf(&bp, "%s -t %s%s", cmd,
931 1.7 christos *targ == '-' ? "-- " : "", targ);
932 1.18 christos if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
933 1.5 christos exit(1);
934 1.9 christos free(bp);
935 1.5 christos (void) close(remin);
936 1.5 christos (void) close(remout);
937 1.5 christos remin = remout = -1;
938 1.18 christos } else if (host) { /* standard remote to remote */
939 1.18 christos if (tport != -1 && tport != SSH_DEFAULT_PORT) {
940 1.18 christos /* This would require the remote support URIs */
941 1.18 christos fatal("target port not supported with two "
942 1.18 christos "remote hosts without the -3 option");
943 1.18 christos }
944 1.18 christos
945 1.1 christos freeargs(&alist);
946 1.1 christos addargs(&alist, "%s", ssh_program);
947 1.1 christos addargs(&alist, "-x");
948 1.5 christos addargs(&alist, "-oClearAllForwardings=yes");
949 1.1 christos addargs(&alist, "-n");
950 1.5 christos for (j = 0; j < remote_remote_args.num; j++) {
951 1.5 christos addargs(&alist, "%s",
952 1.5 christos remote_remote_args.list[j]);
953 1.5 christos }
954 1.18 christos if (sport != -1) {
955 1.18 christos addargs(&alist, "-p");
956 1.18 christos addargs(&alist, "%d", sport);
957 1.18 christos }
958 1.18 christos if (suser) {
959 1.1 christos addargs(&alist, "-l");
960 1.1 christos addargs(&alist, "%s", suser);
961 1.1 christos }
962 1.4 adam addargs(&alist, "--");
963 1.1 christos addargs(&alist, "%s", host);
964 1.1 christos addargs(&alist, "%s", cmd);
965 1.1 christos addargs(&alist, "%s", src);
966 1.1 christos addargs(&alist, "%s%s%s:%s",
967 1.1 christos tuser ? tuser : "", tuser ? "@" : "",
968 1.1 christos thost, targ);
969 1.1 christos if (do_local_cmd(&alist) != 0)
970 1.1 christos errs = 1;
971 1.1 christos } else { /* local to remote */
972 1.1 christos if (remin == -1) {
973 1.7 christos xasprintf(&bp, "%s -t %s%s", cmd,
974 1.7 christos *targ == '-' ? "-- " : "", targ);
975 1.18 christos if (do_cmd(thost, tuser, tport, bp, &remin,
976 1.1 christos &remout) < 0)
977 1.1 christos exit(1);
978 1.1 christos if (response() < 0)
979 1.1 christos exit(1);
980 1.9 christos free(bp);
981 1.1 christos }
982 1.1 christos source(1, argv + i);
983 1.1 christos }
984 1.1 christos }
985 1.18 christos out:
986 1.18 christos free(tuser);
987 1.18 christos free(thost);
988 1.19 christos free(__UNCONST(targ));
989 1.18 christos free(suser);
990 1.18 christos free(host);
991 1.19 christos free(__UNCONST(src));
992 1.1 christos }
993 1.1 christos
994 1.6 joerg static void
995 1.1 christos tolocal(int argc, char **argv)
996 1.1 christos {
997 1.19 christos char *bp, *host = NULL, *suser = NULL;
998 1.19 christos const char *src = NULL;
999 1.1 christos arglist alist;
1000 1.18 christos int i, r, sport = -1;
1001 1.1 christos
1002 1.1 christos memset(&alist, '\0', sizeof(alist));
1003 1.1 christos alist.list = NULL;
1004 1.1 christos
1005 1.1 christos for (i = 0; i < argc - 1; i++) {
1006 1.18 christos free(suser);
1007 1.18 christos free(host);
1008 1.19 christos free(__UNCONST(src));
1009 1.18 christos r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
1010 1.18 christos if (r == -1) {
1011 1.18 christos fmprintf(stderr, "%s: invalid uri\n", argv[i]);
1012 1.18 christos ++errs;
1013 1.18 christos continue;
1014 1.18 christos }
1015 1.18 christos if (r != 0)
1016 1.18 christos parse_user_host_path(argv[i], &suser, &host, &src);
1017 1.18 christos if (suser != NULL && !okname(suser)) {
1018 1.18 christos ++errs;
1019 1.18 christos continue;
1020 1.18 christos }
1021 1.18 christos if (!host) { /* Local to local. */
1022 1.1 christos freeargs(&alist);
1023 1.1 christos addargs(&alist, "%s", _PATH_CP);
1024 1.1 christos if (iamrecursive)
1025 1.1 christos addargs(&alist, "-r");
1026 1.1 christos if (pflag)
1027 1.1 christos addargs(&alist, "-p");
1028 1.4 adam addargs(&alist, "--");
1029 1.1 christos addargs(&alist, "%s", argv[i]);
1030 1.1 christos addargs(&alist, "%s", argv[argc-1]);
1031 1.1 christos if (do_local_cmd(&alist))
1032 1.1 christos ++errs;
1033 1.1 christos continue;
1034 1.1 christos }
1035 1.18 christos /* Remote to local. */
1036 1.7 christos xasprintf(&bp, "%s -f %s%s",
1037 1.7 christos cmd, *src == '-' ? "-- " : "", src);
1038 1.18 christos if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
1039 1.9 christos free(bp);
1040 1.1 christos ++errs;
1041 1.1 christos continue;
1042 1.1 christos }
1043 1.9 christos free(bp);
1044 1.23 christos sink(1, argv + argc - 1, src);
1045 1.1 christos (void) close(remin);
1046 1.1 christos remin = remout = -1;
1047 1.1 christos }
1048 1.18 christos free(suser);
1049 1.18 christos free(host);
1050 1.19 christos free(__UNCONST(src));
1051 1.1 christos }
1052 1.1 christos
1053 1.1 christos void
1054 1.1 christos source(int argc, char **argv)
1055 1.1 christos {
1056 1.1 christos struct stat stb;
1057 1.1 christos static BUF buffer;
1058 1.1 christos BUF *bp;
1059 1.1 christos off_t i, statbytes;
1060 1.10 christos size_t amt, nr;
1061 1.1 christos int fd = -1, haderr, indx;
1062 1.24 christos char *last, *name, buf[PATH_MAX + 128], encname[PATH_MAX];
1063 1.1 christos int len;
1064 1.1 christos
1065 1.1 christos for (indx = 0; indx < argc; ++indx) {
1066 1.2 christos fd = -1;
1067 1.1 christos name = argv[indx];
1068 1.1 christos statbytes = 0;
1069 1.1 christos len = strlen(name);
1070 1.1 christos while (len > 1 && name[len-1] == '/')
1071 1.1 christos name[--len] = '\0';
1072 1.24 christos if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) == -1)
1073 1.1 christos goto syserr;
1074 1.1 christos if (strchr(name, '\n') != NULL) {
1075 1.3 stacktic strvisx(encname, name, len, VIS_NL);
1076 1.1 christos name = encname;
1077 1.1 christos }
1078 1.24 christos if (fstat(fd, &stb) == -1) {
1079 1.1 christos syserr: run_err("%s: %s", name, strerror(errno));
1080 1.1 christos goto next;
1081 1.1 christos }
1082 1.1 christos if (stb.st_size < 0) {
1083 1.1 christos run_err("%s: %s", name, "Negative file size");
1084 1.1 christos goto next;
1085 1.1 christos }
1086 1.1 christos unset_nonblock(fd);
1087 1.1 christos switch (stb.st_mode & S_IFMT) {
1088 1.1 christos case S_IFREG:
1089 1.1 christos break;
1090 1.1 christos case S_IFDIR:
1091 1.1 christos if (iamrecursive) {
1092 1.1 christos rsource(name, &stb);
1093 1.1 christos goto next;
1094 1.1 christos }
1095 1.1 christos /* FALLTHROUGH */
1096 1.1 christos default:
1097 1.1 christos run_err("%s: not a regular file", name);
1098 1.1 christos goto next;
1099 1.1 christos }
1100 1.1 christos if ((last = strrchr(name, '/')) == NULL)
1101 1.1 christos last = name;
1102 1.1 christos else
1103 1.1 christos ++last;
1104 1.1 christos curfile = last;
1105 1.1 christos if (pflag) {
1106 1.9 christos if (do_times(remout, verbose_mode, &stb) < 0)
1107 1.1 christos goto next;
1108 1.1 christos }
1109 1.1 christos #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1110 1.1 christos snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1111 1.1 christos (u_int) (stb.st_mode & FILEMODEMASK),
1112 1.1 christos (long long)stb.st_size, last);
1113 1.14 christos if (verbose_mode)
1114 1.14 christos fmprintf(stderr, "Sending file modes: %s", buf);
1115 1.1 christos (void) atomicio(vwrite, remout, buf, strlen(buf));
1116 1.1 christos if (response() < 0)
1117 1.1 christos goto next;
1118 1.1 christos if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
1119 1.1 christos next: if (fd != -1) {
1120 1.1 christos (void) close(fd);
1121 1.1 christos fd = -1;
1122 1.1 christos }
1123 1.1 christos continue;
1124 1.1 christos }
1125 1.1 christos if (showprogress)
1126 1.1 christos start_progress_meter(curfile, stb.st_size, &statbytes);
1127 1.1 christos set_nonblock(remout);
1128 1.1 christos for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
1129 1.1 christos amt = bp->cnt;
1130 1.1 christos if (i + (off_t)amt > stb.st_size)
1131 1.1 christos amt = stb.st_size - i;
1132 1.1 christos if (!haderr) {
1133 1.10 christos if ((nr = atomicio(read, fd,
1134 1.10 christos bp->buf, amt)) != amt) {
1135 1.1 christos haderr = errno;
1136 1.10 christos memset(bp->buf + nr, 0, amt - nr);
1137 1.10 christos }
1138 1.1 christos }
1139 1.1 christos /* Keep writing after error to retain sync */
1140 1.1 christos if (haderr) {
1141 1.1 christos (void)atomicio(vwrite, remout, bp->buf, amt);
1142 1.10 christos memset(bp->buf, 0, amt);
1143 1.1 christos continue;
1144 1.1 christos }
1145 1.5 christos if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
1146 1.1 christos &statbytes) != amt)
1147 1.1 christos haderr = errno;
1148 1.1 christos }
1149 1.1 christos unset_nonblock(remout);
1150 1.1 christos
1151 1.1 christos if (fd != -1) {
1152 1.24 christos if (close(fd) == -1 && !haderr)
1153 1.1 christos haderr = errno;
1154 1.1 christos fd = -1;
1155 1.1 christos }
1156 1.1 christos if (!haderr)
1157 1.5 christos (void) atomicio(vwrite, remout, empty, 1);
1158 1.1 christos else
1159 1.1 christos run_err("%s: %s", name, strerror(haderr));
1160 1.1 christos (void) response();
1161 1.14 christos if (showprogress)
1162 1.14 christos stop_progress_meter();
1163 1.1 christos }
1164 1.1 christos }
1165 1.1 christos
1166 1.1 christos void
1167 1.1 christos rsource(char *name, struct stat *statp)
1168 1.1 christos {
1169 1.1 christos DIR *dirp;
1170 1.1 christos struct dirent *dp;
1171 1.22 mrg char *last, *vect[1], path[PATH_MAX + 20];
1172 1.1 christos
1173 1.1 christos if (!(dirp = opendir(name))) {
1174 1.1 christos run_err("%s: %s", name, strerror(errno));
1175 1.1 christos return;
1176 1.1 christos }
1177 1.1 christos last = strrchr(name, '/');
1178 1.13 christos if (last == NULL)
1179 1.1 christos last = name;
1180 1.1 christos else
1181 1.1 christos last++;
1182 1.1 christos if (pflag) {
1183 1.9 christos if (do_times(remout, verbose_mode, statp) < 0) {
1184 1.1 christos closedir(dirp);
1185 1.1 christos return;
1186 1.1 christos }
1187 1.1 christos }
1188 1.1 christos (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1189 1.1 christos (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1190 1.1 christos if (verbose_mode)
1191 1.14 christos fmprintf(stderr, "Entering directory: %s", path);
1192 1.1 christos (void) atomicio(vwrite, remout, path, strlen(path));
1193 1.1 christos if (response() < 0) {
1194 1.1 christos closedir(dirp);
1195 1.1 christos return;
1196 1.1 christos }
1197 1.1 christos while ((dp = readdir(dirp)) != NULL) {
1198 1.1 christos if (dp->d_ino == 0)
1199 1.1 christos continue;
1200 1.1 christos if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1201 1.1 christos continue;
1202 1.1 christos if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
1203 1.1 christos run_err("%s/%s: name too long", name, dp->d_name);
1204 1.1 christos continue;
1205 1.1 christos }
1206 1.1 christos (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1207 1.1 christos vect[0] = path;
1208 1.1 christos source(1, vect);
1209 1.1 christos }
1210 1.1 christos (void) closedir(dirp);
1211 1.5 christos (void) atomicio(vwrite, remout, __UNCONST("E\n"), 2);
1212 1.1 christos (void) response();
1213 1.1 christos }
1214 1.1 christos
1215 1.17 christos #define TYPE_OVERFLOW(type, val) \
1216 1.17 christos ((sizeof(type) == 4 && (val) > INT32_MAX) || \
1217 1.17 christos (sizeof(type) == 8 && (val) > INT64_MAX) || \
1218 1.17 christos (sizeof(type) != 4 && sizeof(type) != 8))
1219 1.17 christos
1220 1.1 christos void
1221 1.23 christos sink(int argc, char **argv, const char *src)
1222 1.1 christos {
1223 1.1 christos static BUF buffer;
1224 1.1 christos struct stat stb;
1225 1.1 christos BUF *bp;
1226 1.1 christos off_t i;
1227 1.1 christos size_t j, count;
1228 1.1 christos int amt, exists, first, ofd;
1229 1.1 christos mode_t mode, omode, mask;
1230 1.1 christos off_t size, statbytes;
1231 1.9 christos unsigned long long ull;
1232 1.26 christos int setimes, targisdir, wrerr;
1233 1.14 christos char ch, *cp, *np, *targ, *vect[1], buf[2048], visbuf[2048];
1234 1.5 christos const char *why;
1235 1.23 christos char **patterns = NULL;
1236 1.23 christos size_t n, npatterns = 0;
1237 1.1 christos struct timeval tv[2];
1238 1.1 christos
1239 1.1 christos #define atime tv[0]
1240 1.1 christos #define mtime tv[1]
1241 1.1 christos #define SCREWUP(str) { why = str; goto screwup; }
1242 1.1 christos
1243 1.17 christos if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
1244 1.17 christos SCREWUP("Unexpected off_t/time_t size");
1245 1.17 christos
1246 1.1 christos setimes = targisdir = 0;
1247 1.1 christos mask = umask(0);
1248 1.1 christos if (!pflag)
1249 1.1 christos (void) umask(mask);
1250 1.1 christos if (argc != 1) {
1251 1.1 christos run_err("ambiguous target");
1252 1.1 christos exit(1);
1253 1.1 christos }
1254 1.1 christos targ = *argv;
1255 1.1 christos if (targetshouldbedirectory)
1256 1.1 christos verifydir(targ);
1257 1.1 christos
1258 1.5 christos (void) atomicio(vwrite, remout, empty, 1);
1259 1.1 christos if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
1260 1.1 christos targisdir = 1;
1261 1.23 christos if (src != NULL && !iamrecursive && !Tflag) {
1262 1.23 christos /*
1263 1.23 christos * Prepare to try to restrict incoming filenames to match
1264 1.23 christos * the requested destination file glob.
1265 1.23 christos */
1266 1.23 christos if (brace_expand(src, &patterns, &npatterns) != 0)
1267 1.23 christos fatal("%s: could not expand pattern", __func__);
1268 1.23 christos }
1269 1.1 christos for (first = 1;; first = 0) {
1270 1.1 christos cp = buf;
1271 1.1 christos if (atomicio(read, remin, cp, 1) != 1)
1272 1.23 christos goto done;
1273 1.1 christos if (*cp++ == '\n')
1274 1.1 christos SCREWUP("unexpected <newline>");
1275 1.1 christos do {
1276 1.1 christos if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1277 1.1 christos SCREWUP("lost connection");
1278 1.1 christos *cp++ = ch;
1279 1.1 christos } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
1280 1.1 christos *cp = 0;
1281 1.1 christos if (verbose_mode)
1282 1.14 christos fmprintf(stderr, "Sink: %s", buf);
1283 1.1 christos
1284 1.1 christos if (buf[0] == '\01' || buf[0] == '\02') {
1285 1.14 christos if (iamremote == 0) {
1286 1.14 christos (void) snmprintf(visbuf, sizeof(visbuf),
1287 1.14 christos NULL, "%s", buf + 1);
1288 1.1 christos (void) atomicio(vwrite, STDERR_FILENO,
1289 1.14 christos visbuf, strlen(visbuf));
1290 1.14 christos }
1291 1.1 christos if (buf[0] == '\02')
1292 1.1 christos exit(1);
1293 1.1 christos ++errs;
1294 1.1 christos continue;
1295 1.1 christos }
1296 1.1 christos if (buf[0] == 'E') {
1297 1.23 christos (void) atomicio(vwrite, remout, __UNCONST(""), 1);
1298 1.23 christos goto done;
1299 1.1 christos }
1300 1.1 christos if (ch == '\n')
1301 1.1 christos *--cp = 0;
1302 1.1 christos
1303 1.1 christos cp = buf;
1304 1.1 christos if (*cp == 'T') {
1305 1.1 christos setimes++;
1306 1.1 christos cp++;
1307 1.9 christos if (!isdigit((unsigned char)*cp))
1308 1.9 christos SCREWUP("mtime.sec not present");
1309 1.9 christos ull = strtoull(cp, &cp, 10);
1310 1.1 christos if (!cp || *cp++ != ' ')
1311 1.1 christos SCREWUP("mtime.sec not delimited");
1312 1.17 christos if (TYPE_OVERFLOW(time_t, ull))
1313 1.9 christos setimes = 0; /* out of range */
1314 1.9 christos mtime.tv_sec = ull;
1315 1.1 christos mtime.tv_usec = strtol(cp, &cp, 10);
1316 1.9 christos if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1317 1.9 christos mtime.tv_usec > 999999)
1318 1.1 christos SCREWUP("mtime.usec not delimited");
1319 1.9 christos if (!isdigit((unsigned char)*cp))
1320 1.9 christos SCREWUP("atime.sec not present");
1321 1.9 christos ull = strtoull(cp, &cp, 10);
1322 1.1 christos if (!cp || *cp++ != ' ')
1323 1.1 christos SCREWUP("atime.sec not delimited");
1324 1.17 christos if (TYPE_OVERFLOW(time_t, ull))
1325 1.9 christos setimes = 0; /* out of range */
1326 1.9 christos atime.tv_sec = ull;
1327 1.1 christos atime.tv_usec = strtol(cp, &cp, 10);
1328 1.9 christos if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1329 1.9 christos atime.tv_usec > 999999)
1330 1.1 christos SCREWUP("atime.usec not delimited");
1331 1.5 christos (void) atomicio(vwrite, remout, empty, 1);
1332 1.1 christos continue;
1333 1.1 christos }
1334 1.1 christos if (*cp != 'C' && *cp != 'D') {
1335 1.1 christos /*
1336 1.1 christos * Check for the case "rcp remote:foo\* local:bar".
1337 1.1 christos * In this case, the line "No match." can be returned
1338 1.1 christos * by the shell before the rcp command on the remote is
1339 1.1 christos * executed so the ^Aerror_message convention isn't
1340 1.1 christos * followed.
1341 1.1 christos */
1342 1.1 christos if (first) {
1343 1.1 christos run_err("%s", cp);
1344 1.1 christos exit(1);
1345 1.1 christos }
1346 1.1 christos SCREWUP("expected control record");
1347 1.1 christos }
1348 1.1 christos mode = 0;
1349 1.1 christos for (++cp; cp < buf + 5; cp++) {
1350 1.1 christos if (*cp < '0' || *cp > '7')
1351 1.1 christos SCREWUP("bad mode");
1352 1.1 christos mode = (mode << 3) | (*cp - '0');
1353 1.1 christos }
1354 1.20 christos if (!pflag)
1355 1.20 christos mode &= ~mask;
1356 1.1 christos if (*cp++ != ' ')
1357 1.1 christos SCREWUP("mode not delimited");
1358 1.1 christos
1359 1.17 christos if (!isdigit((unsigned char)*cp))
1360 1.17 christos SCREWUP("size not present");
1361 1.17 christos ull = strtoull(cp, &cp, 10);
1362 1.17 christos if (!cp || *cp++ != ' ')
1363 1.1 christos SCREWUP("size not delimited");
1364 1.17 christos if (TYPE_OVERFLOW(off_t, ull))
1365 1.17 christos SCREWUP("size out of range");
1366 1.17 christos size = (off_t)ull;
1367 1.17 christos
1368 1.23 christos if (*cp == '\0' || strchr(cp, '/') != NULL ||
1369 1.23 christos strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0) {
1370 1.1 christos run_err("error: unexpected filename: %s", cp);
1371 1.1 christos exit(1);
1372 1.1 christos }
1373 1.23 christos if (npatterns > 0) {
1374 1.23 christos for (n = 0; n < npatterns; n++) {
1375 1.23 christos if (fnmatch(patterns[n], cp, 0) == 0)
1376 1.23 christos break;
1377 1.23 christos }
1378 1.23 christos if (n >= npatterns)
1379 1.23 christos SCREWUP("filename does not match request");
1380 1.23 christos }
1381 1.1 christos if (targisdir) {
1382 1.1 christos static char *namebuf;
1383 1.1 christos static size_t cursize;
1384 1.1 christos size_t need;
1385 1.1 christos
1386 1.1 christos need = strlen(targ) + strlen(cp) + 250;
1387 1.1 christos if (need > cursize) {
1388 1.9 christos free(namebuf);
1389 1.1 christos namebuf = xmalloc(need);
1390 1.1 christos cursize = need;
1391 1.1 christos }
1392 1.1 christos (void) snprintf(namebuf, need, "%s%s%s", targ,
1393 1.1 christos strcmp(targ, "/") ? "/" : "", cp);
1394 1.1 christos np = namebuf;
1395 1.1 christos } else
1396 1.1 christos np = targ;
1397 1.1 christos curfile = cp;
1398 1.1 christos exists = stat(np, &stb) == 0;
1399 1.1 christos if (buf[0] == 'D') {
1400 1.1 christos int mod_flag = pflag;
1401 1.1 christos if (!iamrecursive)
1402 1.1 christos SCREWUP("received directory without -r");
1403 1.1 christos if (exists) {
1404 1.1 christos if (!S_ISDIR(stb.st_mode)) {
1405 1.1 christos errno = ENOTDIR;
1406 1.1 christos goto bad;
1407 1.1 christos }
1408 1.1 christos if (pflag)
1409 1.1 christos (void) chmod(np, mode);
1410 1.1 christos } else {
1411 1.1 christos /* Handle copying from a read-only
1412 1.1 christos directory */
1413 1.1 christos mod_flag = 1;
1414 1.24 christos if (mkdir(np, mode | S_IRWXU) == -1)
1415 1.1 christos goto bad;
1416 1.1 christos }
1417 1.1 christos vect[0] = xstrdup(np);
1418 1.23 christos sink(1, vect, src);
1419 1.1 christos if (setimes) {
1420 1.1 christos setimes = 0;
1421 1.26 christos (void) utimes(vect[0], tv);
1422 1.1 christos }
1423 1.1 christos if (mod_flag)
1424 1.1 christos (void) chmod(vect[0], mode);
1425 1.9 christos free(vect[0]);
1426 1.1 christos continue;
1427 1.1 christos }
1428 1.1 christos omode = mode;
1429 1.9 christos mode |= S_IWUSR;
1430 1.24 christos if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) == -1) {
1431 1.1 christos bad: run_err("%s: %s", np, strerror(errno));
1432 1.1 christos continue;
1433 1.1 christos }
1434 1.5 christos (void) atomicio(vwrite, remout, empty, 1);
1435 1.1 christos if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
1436 1.1 christos (void) close(ofd);
1437 1.1 christos continue;
1438 1.1 christos }
1439 1.1 christos cp = bp->buf;
1440 1.26 christos wrerr = 0;
1441 1.1 christos
1442 1.26 christos /*
1443 1.26 christos * NB. do not use run_err() unless immediately followed by
1444 1.26 christos * exit() below as it may send a spurious reply that might
1445 1.26 christos * desyncronise us from the peer. Use note_err() instead.
1446 1.26 christos */
1447 1.1 christos statbytes = 0;
1448 1.1 christos if (showprogress)
1449 1.1 christos start_progress_meter(curfile, size, &statbytes);
1450 1.1 christos set_nonblock(remin);
1451 1.1 christos for (count = i = 0; i < size; i += bp->cnt) {
1452 1.1 christos amt = bp->cnt;
1453 1.1 christos if (i + amt > size)
1454 1.1 christos amt = size - i;
1455 1.1 christos count += amt;
1456 1.1 christos do {
1457 1.5 christos j = atomicio6(read, remin, cp, amt,
1458 1.5 christos scpio, &statbytes);
1459 1.1 christos if (j == 0) {
1460 1.1 christos run_err("%s", j != EPIPE ?
1461 1.1 christos strerror(errno) :
1462 1.1 christos "dropped connection");
1463 1.1 christos exit(1);
1464 1.1 christos }
1465 1.1 christos amt -= j;
1466 1.1 christos cp += j;
1467 1.1 christos } while (amt > 0);
1468 1.1 christos
1469 1.1 christos if (count == bp->cnt) {
1470 1.1 christos /* Keep reading so we stay sync'd up. */
1471 1.26 christos if (!wrerr) {
1472 1.1 christos if (atomicio(vwrite, ofd, bp->buf,
1473 1.1 christos count) != count) {
1474 1.26 christos note_err("%s: %s", np,
1475 1.26 christos strerror(errno));
1476 1.26 christos wrerr = 1;
1477 1.1 christos }
1478 1.1 christos }
1479 1.1 christos count = 0;
1480 1.1 christos cp = bp->buf;
1481 1.1 christos }
1482 1.1 christos }
1483 1.1 christos unset_nonblock(remin);
1484 1.26 christos if (count != 0 && !wrerr &&
1485 1.1 christos atomicio(vwrite, ofd, bp->buf, count) != count) {
1486 1.26 christos note_err("%s: %s", np, strerror(errno));
1487 1.26 christos wrerr = 1;
1488 1.1 christos }
1489 1.26 christos if (!wrerr && (!exists || S_ISREG(stb.st_mode)) &&
1490 1.26 christos ftruncate(ofd, size) != 0)
1491 1.26 christos note_err("%s: truncate: %s", np, strerror(errno));
1492 1.1 christos if (pflag) {
1493 1.1 christos if (exists || omode != mode)
1494 1.1 christos if (fchmod(ofd, omode)) {
1495 1.26 christos note_err("%s: set mode: %s",
1496 1.1 christos np, strerror(errno));
1497 1.1 christos }
1498 1.1 christos } else {
1499 1.1 christos if (!exists && omode != mode)
1500 1.1 christos if (fchmod(ofd, omode & ~mask)) {
1501 1.26 christos note_err("%s: set mode: %s",
1502 1.1 christos np, strerror(errno));
1503 1.1 christos }
1504 1.1 christos }
1505 1.26 christos if (close(ofd) == -1)
1506 1.27 christos note_err("%s: close: %s", np, strerror(errno));
1507 1.1 christos (void) response();
1508 1.14 christos if (showprogress)
1509 1.14 christos stop_progress_meter();
1510 1.26 christos if (setimes && !wrerr) {
1511 1.1 christos setimes = 0;
1512 1.24 christos if (utimes(np, tv) == -1) {
1513 1.26 christos note_err("%s: set times: %s",
1514 1.1 christos np, strerror(errno));
1515 1.1 christos }
1516 1.1 christos }
1517 1.26 christos /* If no error was noted then signal success for this file */
1518 1.26 christos if (note_err(NULL) == 0)
1519 1.26 christos (void) atomicio(vwrite, remout, __UNCONST(""), 1);
1520 1.1 christos }
1521 1.23 christos done:
1522 1.23 christos for (n = 0; n < npatterns; n++)
1523 1.23 christos free(patterns[n]);
1524 1.23 christos free(patterns);
1525 1.23 christos return;
1526 1.1 christos screwup:
1527 1.23 christos for (n = 0; n < npatterns; n++)
1528 1.23 christos free(patterns[n]);
1529 1.23 christos free(patterns);
1530 1.1 christos run_err("protocol error: %s", why);
1531 1.1 christos exit(1);
1532 1.1 christos }
1533 1.1 christos
1534 1.1 christos int
1535 1.1 christos response(void)
1536 1.1 christos {
1537 1.14 christos char ch, *cp, resp, rbuf[2048], visbuf[2048];
1538 1.1 christos
1539 1.1 christos if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1540 1.1 christos lostconn(0);
1541 1.1 christos
1542 1.1 christos cp = rbuf;
1543 1.1 christos switch (resp) {
1544 1.1 christos case 0: /* ok */
1545 1.1 christos return (0);
1546 1.1 christos default:
1547 1.1 christos *cp++ = resp;
1548 1.1 christos /* FALLTHROUGH */
1549 1.1 christos case 1: /* error, followed by error msg */
1550 1.1 christos case 2: /* fatal error, "" */
1551 1.1 christos do {
1552 1.1 christos if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1553 1.1 christos lostconn(0);
1554 1.1 christos *cp++ = ch;
1555 1.1 christos } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1556 1.1 christos
1557 1.14 christos if (!iamremote) {
1558 1.14 christos cp[-1] = '\0';
1559 1.14 christos (void) snmprintf(visbuf, sizeof(visbuf),
1560 1.14 christos NULL, "%s\n", rbuf);
1561 1.14 christos (void) atomicio(vwrite, STDERR_FILENO,
1562 1.14 christos visbuf, strlen(visbuf));
1563 1.14 christos }
1564 1.1 christos ++errs;
1565 1.1 christos if (resp == 1)
1566 1.1 christos return (-1);
1567 1.1 christos exit(1);
1568 1.1 christos }
1569 1.1 christos /* NOTREACHED */
1570 1.1 christos }
1571 1.1 christos
1572 1.1 christos void
1573 1.1 christos usage(void)
1574 1.1 christos {
1575 1.1 christos (void) fprintf(stderr,
1576 1.23 christos "usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1577 1.23 christos " [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
1578 1.23 christos " [-S program] source ... target\n");
1579 1.1 christos exit(1);
1580 1.1 christos }
1581 1.1 christos
1582 1.1 christos void
1583 1.1 christos run_err(const char *fmt,...)
1584 1.1 christos {
1585 1.1 christos static FILE *fp;
1586 1.1 christos va_list ap;
1587 1.1 christos
1588 1.1 christos ++errs;
1589 1.1 christos if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1590 1.1 christos (void) fprintf(fp, "%c", 0x01);
1591 1.1 christos (void) fprintf(fp, "scp: ");
1592 1.1 christos va_start(ap, fmt);
1593 1.1 christos (void) vfprintf(fp, fmt, ap);
1594 1.1 christos va_end(ap);
1595 1.1 christos (void) fprintf(fp, "\n");
1596 1.1 christos (void) fflush(fp);
1597 1.1 christos }
1598 1.1 christos
1599 1.1 christos if (!iamremote) {
1600 1.1 christos va_start(ap, fmt);
1601 1.14 christos vfmprintf(stderr, fmt, ap);
1602 1.1 christos va_end(ap);
1603 1.1 christos fprintf(stderr, "\n");
1604 1.1 christos }
1605 1.1 christos }
1606 1.1 christos
1607 1.26 christos /*
1608 1.26 christos * Notes a sink error for sending at the end of a file transfer. Returns 0 if
1609 1.26 christos * no error has been noted or -1 otherwise. Use note_err(NULL) to flush
1610 1.26 christos * any active error at the end of the transfer.
1611 1.26 christos */
1612 1.26 christos int
1613 1.26 christos note_err(const char *fmt, ...)
1614 1.26 christos {
1615 1.26 christos static char *emsg;
1616 1.26 christos va_list ap;
1617 1.26 christos
1618 1.26 christos /* Replay any previously-noted error */
1619 1.26 christos if (fmt == NULL) {
1620 1.26 christos if (emsg == NULL)
1621 1.26 christos return 0;
1622 1.26 christos run_err("%s", emsg);
1623 1.26 christos free(emsg);
1624 1.26 christos emsg = NULL;
1625 1.26 christos return -1;
1626 1.26 christos }
1627 1.26 christos
1628 1.26 christos errs++;
1629 1.26 christos /* Prefer first-noted error */
1630 1.26 christos if (emsg != NULL)
1631 1.26 christos return -1;
1632 1.26 christos
1633 1.26 christos va_start(ap, fmt);
1634 1.26 christos vasnmprintf(&emsg, INT_MAX, NULL, fmt, ap);
1635 1.26 christos va_end(ap);
1636 1.26 christos return -1;
1637 1.26 christos }
1638 1.26 christos
1639 1.1 christos void
1640 1.1 christos verifydir(char *cp)
1641 1.1 christos {
1642 1.1 christos struct stat stb;
1643 1.1 christos
1644 1.1 christos if (!stat(cp, &stb)) {
1645 1.1 christos if (S_ISDIR(stb.st_mode))
1646 1.1 christos return;
1647 1.1 christos errno = ENOTDIR;
1648 1.1 christos }
1649 1.1 christos run_err("%s: %s", cp, strerror(errno));
1650 1.1 christos killchild(0);
1651 1.1 christos }
1652 1.1 christos
1653 1.1 christos int
1654 1.1 christos okname(char *cp0)
1655 1.1 christos {
1656 1.1 christos int c;
1657 1.1 christos char *cp;
1658 1.1 christos
1659 1.1 christos cp = cp0;
1660 1.1 christos do {
1661 1.1 christos c = (int)*cp;
1662 1.1 christos if (c & 0200)
1663 1.1 christos goto bad;
1664 1.10 christos if (!isalpha(c) && !isdigit((unsigned char)c)) {
1665 1.1 christos switch (c) {
1666 1.1 christos case '\'':
1667 1.1 christos case '"':
1668 1.1 christos case '`':
1669 1.1 christos case ' ':
1670 1.1 christos case '#':
1671 1.1 christos goto bad;
1672 1.1 christos default:
1673 1.1 christos break;
1674 1.1 christos }
1675 1.1 christos }
1676 1.1 christos } while (*++cp);
1677 1.1 christos return (1);
1678 1.1 christos
1679 1.14 christos bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
1680 1.1 christos return (0);
1681 1.1 christos }
1682 1.1 christos
1683 1.1 christos BUF *
1684 1.1 christos allocbuf(BUF *bp, int fd, int blksize)
1685 1.1 christos {
1686 1.1 christos size_t size;
1687 1.1 christos struct stat stb;
1688 1.1 christos
1689 1.24 christos if (fstat(fd, &stb) == -1) {
1690 1.1 christos run_err("fstat: %s", strerror(errno));
1691 1.1 christos return (0);
1692 1.1 christos }
1693 1.15 christos size = ROUNDUP(stb.st_blksize, blksize);
1694 1.1 christos if (size == 0)
1695 1.1 christos size = blksize;
1696 1.1 christos if (bp->cnt >= size)
1697 1.1 christos return (bp);
1698 1.17 christos bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
1699 1.1 christos bp->cnt = size;
1700 1.1 christos return (bp);
1701 1.1 christos }
1702 1.1 christos
1703 1.6 joerg static void
1704 1.1 christos lostconn(int signo)
1705 1.1 christos {
1706 1.1 christos if (!iamremote)
1707 1.9 christos (void)write(STDERR_FILENO, "lost connection\n", 16);
1708 1.1 christos if (signo)
1709 1.1 christos _exit(1);
1710 1.1 christos else
1711 1.1 christos exit(1);
1712 1.1 christos }
1713