Annotation of src/usr.bin/ssh/scp.c, Revision 1.199
1.199 ! tb 1: /* $OpenBSD: scp.c,v 1.198 2018/11/16 03:03:10 djm Exp $ */
1.1 deraadt 2: /*
1.38 deraadt 3: * scp - secure remote copy. This is basically patched BSD rcp which
4: * uses ssh to do the data transfer (instead of using rcmd).
1.27 markus 5: *
1.38 deraadt 6: * NOTE: This version should NOT be suid root. (This uses ssh to
7: * do the transfer and ssh has the necessary privileges.)
1.27 markus 8: *
1.38 deraadt 9: * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
10: *
11: * 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".
1.39 markus 16: */
1.38 deraadt 17: /*
1.60 deraadt 18: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19: * Copyright (c) 1999 Aaron Campbell. All rights reserved.
1.27 markus 20: *
1.38 deraadt 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.
1.27 markus 29: *
1.38 deraadt 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: */
1.1 deraadt 41:
42: /*
1.35 deraadt 43: * Parts from:
44: *
1.1 deraadt 45: * 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.
1.103 millert 56: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 57: * 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: *
72: */
73:
1.132 stevesk 74: #include <sys/types.h>
1.161 djm 75: #include <sys/poll.h>
1.132 stevesk 76: #include <sys/wait.h>
1.134 stevesk 77: #include <sys/stat.h>
1.151 stevesk 78: #include <sys/time.h>
1.155 deraadt 79: #include <sys/uio.h>
1.131 stevesk 80:
1.135 stevesk 81: #include <ctype.h>
1.131 stevesk 82: #include <dirent.h>
1.147 stevesk 83: #include <errno.h>
1.144 stevesk 84: #include <fcntl.h>
1.186 schwarze 85: #include <locale.h>
1.143 stevesk 86: #include <pwd.h>
1.133 stevesk 87: #include <signal.h>
1.146 stevesk 88: #include <stdarg.h>
1.189 millert 89: #include <stdint.h>
1.154 stevesk 90: #include <stdio.h>
1.153 stevesk 91: #include <stdlib.h>
1.150 stevesk 92: #include <string.h>
1.149 stevesk 93: #include <time.h>
1.148 stevesk 94: #include <unistd.h>
1.181 deraadt 95: #include <limits.h>
1.158 dtucker 96: #include <vis.h>
1.1 deraadt 97:
98: #include "xmalloc.h"
1.193 millert 99: #include "ssh.h"
1.51 markus 100: #include "atomicio.h"
1.50 markus 101: #include "pathnames.h"
1.51 markus 102: #include "log.h"
1.69 mouring 103: #include "misc.h"
1.97 fgsch 104: #include "progressmeter.h"
1.186 schwarze 105: #include "utf8.h"
1.1 deraadt 106:
1.161 djm 107: #define COPY_BUFLEN 16384
108:
1.193 millert 109: int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
110: int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
1.11 aaron 111:
1.70 mouring 112: /* Struct for addargs */
113: arglist args;
1.168 djm 114: arglist remote_remote_args;
1.41 markus 115:
1.99 markus 116: /* Bandwidth limit */
1.167 djm 117: long long limit_kbps = 0;
118: struct bwlimit bwlimit;
1.99 markus 119:
1.11 aaron 120: /* Name of current file being transferred. */
121: char *curfile;
1.4 aaron 122:
1.1 deraadt 123: /* This is set to non-zero to enable verbose mode. */
1.17 markus 124: int verbose_mode = 0;
1.1 deraadt 125:
1.6 aaron 126: /* This is set to zero if the progressmeter is not desired. */
127: int showprogress = 1;
128:
1.169 markus 129: /*
130: * This is set to non-zero if remote-remote copy should be piped
131: * through this process.
132: */
133: int throughlocal = 0;
134:
1.193 millert 135: /* Non-standard port to use for the ssh connection or -1. */
136: int sshport = -1;
137:
1.34 deraadt 138: /* This is the program to execute for the secured connection. ("ssh" or -S) */
1.50 markus 139: char *ssh_program = _PATH_SSH_PROGRAM;
1.34 deraadt 140:
1.92 markus 141: /* This is used to store the pid of ssh_program */
1.105 djm 142: pid_t do_cmd_pid = -1;
143:
144: static void
145: killchild(int signo)
146: {
1.119 dtucker 147: if (do_cmd_pid > 1) {
1.123 avsm 148: kill(do_cmd_pid, signo ? signo : SIGTERM);
1.119 dtucker 149: waitpid(do_cmd_pid, NULL, 0);
150: }
1.105 djm 151:
1.123 avsm 152: if (signo)
153: _exit(1);
154: exit(1);
1.105 djm 155: }
1.92 markus 156:
1.166 millert 157: static void
158: suspchild(int signo)
159: {
160: int status;
161:
162: if (do_cmd_pid > 1) {
163: kill(do_cmd_pid, signo);
164: while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
165: errno == EINTR)
166: ;
167: kill(getpid(), SIGSTOP);
168: }
169: }
170:
1.129 djm 171: static int
172: do_local_cmd(arglist *a)
173: {
174: u_int i;
175: int status;
176: pid_t pid;
177:
178: if (a->num == 0)
179: fatal("do_local_cmd: no arguments");
180:
181: if (verbose_mode) {
182: fprintf(stderr, "Executing:");
183: for (i = 0; i < a->num; i++)
1.186 schwarze 184: fmprintf(stderr, " %s", a->list[i]);
1.129 djm 185: fprintf(stderr, "\n");
186: }
187: if ((pid = fork()) == -1)
188: fatal("do_local_cmd: fork: %s", strerror(errno));
189:
190: if (pid == 0) {
191: execvp(a->list[0], a->list);
192: perror(a->list[0]);
193: exit(1);
194: }
195:
196: do_cmd_pid = pid;
197: signal(SIGTERM, killchild);
198: signal(SIGINT, killchild);
199: signal(SIGHUP, killchild);
200:
201: while (waitpid(pid, &status, 0) == -1)
202: if (errno != EINTR)
203: fatal("do_local_cmd: waitpid: %s", strerror(errno));
204:
205: do_cmd_pid = -1;
206:
207: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
208: return (-1);
209:
210: return (0);
211: }
212:
1.20 markus 213: /*
214: * This function executes the given command as the specified user on the
215: * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
216: * assigns the input and output file descriptors on success.
217: */
1.1 deraadt 218:
1.27 markus 219: int
1.193 millert 220: do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
1.1 deraadt 221: {
1.18 markus 222: int pin[2], pout[2], reserved[2];
223:
224: if (verbose_mode)
1.186 schwarze 225: fmprintf(stderr,
1.78 deraadt 226: "Executing: program %s host %s, user %s, command %s\n",
227: ssh_program, host,
228: remuser ? remuser : "(unspecified)", cmd);
1.18 markus 229:
1.193 millert 230: if (port == -1)
231: port = sshport;
232:
1.20 markus 233: /*
234: * Reserve two descriptors so that the real pipes won't get
235: * descriptors 0 and 1 because that will screw up dup2 below.
236: */
1.142 markus 237: if (pipe(reserved) < 0)
238: fatal("pipe: %s", strerror(errno));
1.18 markus 239:
240: /* Create a socket pair for communicating with ssh. */
241: if (pipe(pin) < 0)
242: fatal("pipe: %s", strerror(errno));
243: if (pipe(pout) < 0)
244: fatal("pipe: %s", strerror(errno));
245:
246: /* Free the reserved descriptors. */
247: close(reserved[0]);
248: close(reserved[1]);
1.166 millert 249:
250: signal(SIGTSTP, suspchild);
251: signal(SIGTTIN, suspchild);
252: signal(SIGTTOU, suspchild);
1.18 markus 253:
1.106 nino 254: /* Fork a child to execute the command on the remote host using ssh. */
1.92 markus 255: do_cmd_pid = fork();
256: if (do_cmd_pid == 0) {
1.18 markus 257: /* Child. */
258: close(pin[1]);
259: close(pout[0]);
260: dup2(pin[0], 0);
261: dup2(pout[1], 1);
262: close(pin[0]);
263: close(pout[1]);
264:
1.129 djm 265: replacearg(&args, 0, "%s", ssh_program);
1.193 millert 266: if (port != -1) {
267: addargs(&args, "-p");
268: addargs(&args, "%d", port);
269: }
1.165 guenther 270: if (remuser != NULL) {
271: addargs(&args, "-l");
272: addargs(&args, "%s", remuser);
273: }
274: addargs(&args, "--");
1.70 mouring 275: addargs(&args, "%s", host);
276: addargs(&args, "%s", cmd);
1.18 markus 277:
1.41 markus 278: execvp(ssh_program, args.list);
1.34 deraadt 279: perror(ssh_program);
1.18 markus 280: exit(1);
1.92 markus 281: } else if (do_cmd_pid == -1) {
282: fatal("fork: %s", strerror(errno));
1.18 markus 283: }
284: /* Parent. Close the other side, and return the local side. */
285: close(pin[0]);
286: *fdout = pin[1];
287: close(pout[1]);
288: *fdin = pout[0];
1.105 djm 289: signal(SIGTERM, killchild);
290: signal(SIGINT, killchild);
291: signal(SIGHUP, killchild);
1.18 markus 292: return 0;
1.1 deraadt 293: }
294:
1.169 markus 295: /*
1.196 djm 296: * This function executes a command similar to do_cmd(), but expects the
1.169 markus 297: * input and output descriptors to be setup by a previous call to do_cmd().
298: * This way the input and output of two commands can be connected.
299: */
300: int
1.193 millert 301: do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
1.169 markus 302: {
303: pid_t pid;
304: int status;
305:
306: if (verbose_mode)
1.186 schwarze 307: fmprintf(stderr,
1.169 markus 308: "Executing: 2nd program %s host %s, user %s, command %s\n",
309: ssh_program, host,
310: remuser ? remuser : "(unspecified)", cmd);
311:
1.193 millert 312: if (port == -1)
313: port = sshport;
314:
1.169 markus 315: /* Fork a child to execute the command on the remote host using ssh. */
316: pid = fork();
317: if (pid == 0) {
318: dup2(fdin, 0);
319: dup2(fdout, 1);
320:
321: replacearg(&args, 0, "%s", ssh_program);
1.193 millert 322: if (port != -1) {
323: addargs(&args, "-p");
324: addargs(&args, "%d", port);
325: }
1.169 markus 326: if (remuser != NULL) {
327: addargs(&args, "-l");
328: addargs(&args, "%s", remuser);
329: }
330: addargs(&args, "--");
331: addargs(&args, "%s", host);
332: addargs(&args, "%s", cmd);
333:
334: execvp(ssh_program, args.list);
335: perror(ssh_program);
336: exit(1);
337: } else if (pid == -1) {
338: fatal("fork: %s", strerror(errno));
339: }
340: while (waitpid(pid, &status, 0) == -1)
341: if (errno != EINTR)
342: fatal("do_cmd2: waitpid: %s", strerror(errno));
343: return 0;
344: }
345:
1.1 deraadt 346: typedef struct {
1.124 djm 347: size_t cnt;
1.1 deraadt 348: char *buf;
349: } BUF;
350:
1.18 markus 351: BUF *allocbuf(BUF *, int, int);
352: void lostconn(int);
353: int okname(char *);
354: void run_err(const char *,...);
355: void verifydir(char *);
1.1 deraadt 356:
357: struct passwd *pwd;
1.18 markus 358: uid_t userid;
1.1 deraadt 359: int errs, remin, remout;
360: int pflag, iamremote, iamrecursive, targetshouldbedirectory;
361:
362: #define CMDNEEDS 64
363: char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
364:
1.18 markus 365: int response(void);
366: void rsource(char *, struct stat *);
367: void sink(int, char *[]);
368: void source(int, char *[]);
369: void tolocal(int, char *[]);
1.193 millert 370: void toremote(int, char *[]);
1.18 markus 371: void usage(void);
1.1 deraadt 372:
373: int
1.104 djm 374: main(int argc, char **argv)
1.1 deraadt 375: {
1.145 djm 376: int ch, fflag, tflag, status, n;
1.193 millert 377: char **newargv;
1.167 djm 378: const char *errstr;
1.1 deraadt 379: extern char *optarg;
380: extern int optind;
1.126 djm 381:
382: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
383: sanitise_stdfd();
1.145 djm 384:
1.186 schwarze 385: setlocale(LC_CTYPE, "");
386:
1.145 djm 387: /* Copy argv, because we modify it */
1.187 deraadt 388: newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
1.145 djm 389: for (n = 0; n < argc; n++)
390: newargv[n] = xstrdup(argv[n]);
391: argv = newargv;
1.1 deraadt 392:
1.129 djm 393: memset(&args, '\0', sizeof(args));
1.168 djm 394: memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
395: args.list = remote_remote_args.list = NULL;
1.129 djm 396: addargs(&args, "%s", ssh_program);
1.70 mouring 397: addargs(&args, "-x");
1.168 djm 398: addargs(&args, "-oForwardAgent=no");
399: addargs(&args, "-oPermitLocalCommand=no");
400: addargs(&args, "-oClearAllForwardings=yes");
1.195 djm 401: addargs(&args, "-oRemoteCommand=none");
402: addargs(&args, "-oRequestTTY=no");
1.41 markus 403:
1.1 deraadt 404: fflag = tflag = 0;
1.199 ! tb 405: while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:J:")) != -1)
1.18 markus 406: switch (ch) {
407: /* User-visible flags. */
1.100 markus 408: case '1':
1.190 djm 409: fatal("SSH protocol v.1 is no longer supported");
410: break;
1.100 markus 411: case '2':
1.190 djm 412: /* Ignored */
413: break;
1.23 markus 414: case '4':
1.41 markus 415: case '6':
416: case 'C':
1.70 mouring 417: addargs(&args, "-%c", ch);
1.168 djm 418: addargs(&remote_remote_args, "-%c", ch);
1.41 markus 419: break;
1.169 markus 420: case '3':
421: throughlocal = 1;
422: break;
1.41 markus 423: case 'o':
424: case 'c':
425: case 'i':
1.83 stevesk 426: case 'F':
1.199 ! tb 427: case 'J':
1.168 djm 428: addargs(&remote_remote_args, "-%c", ch);
429: addargs(&remote_remote_args, "%s", optarg);
1.165 guenther 430: addargs(&args, "-%c", ch);
431: addargs(&args, "%s", optarg);
1.23 markus 432: break;
1.41 markus 433: case 'P':
1.193 millert 434: sshport = a2port(optarg);
435: if (sshport <= 0)
436: fatal("bad port \"%s\"\n", optarg);
1.41 markus 437: break;
438: case 'B':
1.168 djm 439: addargs(&remote_remote_args, "-oBatchmode=yes");
440: addargs(&args, "-oBatchmode=yes");
1.23 markus 441: break;
1.99 markus 442: case 'l':
1.167 djm 443: limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
444: &errstr);
445: if (errstr != NULL)
1.99 markus 446: usage();
1.167 djm 447: limit_kbps *= 1024; /* kbps */
448: bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
1.99 markus 449: break;
1.1 deraadt 450: case 'p':
451: pflag = 1;
452: break;
453: case 'r':
454: iamrecursive = 1;
455: break;
1.34 deraadt 456: case 'S':
1.41 markus 457: ssh_program = xstrdup(optarg);
458: break;
459: case 'v':
1.72 markus 460: addargs(&args, "-v");
1.168 djm 461: addargs(&remote_remote_args, "-v");
1.41 markus 462: verbose_mode = 1;
463: break;
464: case 'q':
1.111 dtucker 465: addargs(&args, "-q");
1.168 djm 466: addargs(&remote_remote_args, "-q");
1.41 markus 467: showprogress = 0;
1.34 deraadt 468: break;
469:
1.18 markus 470: /* Server options. */
1.1 deraadt 471: case 'd':
472: targetshouldbedirectory = 1;
473: break;
1.18 markus 474: case 'f': /* "from" */
1.1 deraadt 475: iamremote = 1;
476: fflag = 1;
477: break;
1.18 markus 478: case 't': /* "to" */
1.1 deraadt 479: iamremote = 1;
480: tflag = 1;
481: break;
482: default:
483: usage();
484: }
485: argc -= optind;
486: argv += optind;
487:
488: if ((pwd = getpwuid(userid = getuid())) == NULL)
1.108 deraadt 489: fatal("unknown user %u", (u_int) userid);
1.1 deraadt 490:
1.156 djm 491: if (!isatty(STDOUT_FILENO))
1.7 deraadt 492: showprogress = 0;
1.184 deraadt 493:
494: if (pflag) {
495: /* Cannot pledge: -p allows setuid/setgid files... */
496: } else {
497: if (pledge("stdio rpath wpath cpath fattr tty proc exec",
498: NULL) == -1) {
499: perror("pledge");
500: exit(1);
501: }
502: }
1.7 deraadt 503:
1.1 deraadt 504: remin = STDIN_FILENO;
505: remout = STDOUT_FILENO;
506:
1.52 stevesk 507: if (fflag) {
1.18 markus 508: /* Follow "protocol", send data. */
509: (void) response();
1.1 deraadt 510: source(argc, argv);
511: exit(errs != 0);
512: }
1.18 markus 513: if (tflag) {
514: /* Receive data. */
1.1 deraadt 515: sink(argc, argv);
516: exit(errs != 0);
517: }
518: if (argc < 2)
519: usage();
520: if (argc > 2)
521: targetshouldbedirectory = 1;
522:
523: remin = remout = -1;
1.92 markus 524: do_cmd_pid = -1;
1.1 deraadt 525: /* Command to be executed on remote system using "ssh". */
1.55 deraadt 526: (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
527: verbose_mode ? " -v" : "",
1.35 deraadt 528: iamrecursive ? " -r" : "", pflag ? " -p" : "",
529: targetshouldbedirectory ? " -d" : "");
1.1 deraadt 530:
1.18 markus 531: (void) signal(SIGPIPE, lostconn);
1.1 deraadt 532:
1.193 millert 533: if (colon(argv[argc - 1])) /* Dest is remote host. */
534: toremote(argc, argv);
1.1 deraadt 535: else {
536: if (targetshouldbedirectory)
537: verifydir(argv[argc - 1]);
1.130 djm 538: tolocal(argc, argv); /* Dest is local host. */
1.92 markus 539: }
540: /*
541: * Finally check the exit status of the ssh process, if one was forked
1.164 stevesk 542: * and no error has occurred yet
1.92 markus 543: */
544: if (do_cmd_pid != -1 && errs == 0) {
545: if (remin != -1)
546: (void) close(remin);
547: if (remout != -1)
548: (void) close(remout);
549: if (waitpid(do_cmd_pid, &status, 0) == -1)
550: errs = 1;
551: else {
552: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
553: errs = 1;
554: }
1.1 deraadt 555: }
556: exit(errs != 0);
557: }
558:
1.167 djm 559: /* Callback from atomicio6 to update progress meter and limit bandwidth */
560: static int
561: scpio(void *_cnt, size_t s)
1.161 djm 562: {
1.167 djm 563: off_t *cnt = (off_t *)_cnt;
564:
565: *cnt += s;
566: if (limit_kbps > 0)
567: bandwidth_limit(&bwlimit, s);
568: return 0;
1.161 djm 569: }
570:
1.176 guenther 571: static int
572: do_times(int fd, int verb, const struct stat *sb)
573: {
574: /* strlen(2^64) == 20; strlen(10^6) == 7 */
575: char buf[(20 + 7 + 2) * 2 + 2];
576:
577: (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
578: (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
579: (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
580: if (verb) {
581: fprintf(stderr, "File mtime %lld atime %lld\n",
582: (long long)sb->st_mtime, (long long)sb->st_atime);
583: fprintf(stderr, "Sending file timestamps: %s", buf);
584: }
585: (void) atomicio(vwrite, fd, buf, strlen(buf));
586: return (response());
587: }
588:
1.194 millert 589: static int
590: parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
591: char **pathp)
592: {
593: int r;
594:
595: r = parse_uri("scp", uri, userp, hostp, portp, pathp);
596: if (r == 0 && *pathp == NULL)
597: *pathp = xstrdup(".");
598: return r;
599: }
600:
1.1 deraadt 601: void
1.193 millert 602: toremote(int argc, char **argv)
1.1 deraadt 603: {
1.193 millert 604: char *suser = NULL, *host = NULL, *src = NULL;
605: char *bp, *tuser, *thost, *targ;
606: int sport = -1, tport = -1;
1.129 djm 607: arglist alist;
1.193 millert 608: int i, r;
1.168 djm 609: u_int j;
1.129 djm 610:
611: memset(&alist, '\0', sizeof(alist));
612: alist.list = NULL;
1.1 deraadt 613:
1.193 millert 614: /* Parse target */
1.194 millert 615: r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ);
616: if (r == -1) {
617: fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]);
618: ++errs;
619: goto out;
620: }
1.193 millert 621: if (r != 0) {
622: if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
1.194 millert 623: &targ) == -1) {
624: fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
625: ++errs;
1.193 millert 626: goto out;
1.194 millert 627: }
1.129 djm 628: }
1.194 millert 629: if (tuser != NULL && !okname(tuser)) {
630: ++errs;
1.193 millert 631: goto out;
1.194 millert 632: }
1.129 djm 633:
1.193 millert 634: /* Parse source files */
1.1 deraadt 635: for (i = 0; i < argc - 1; i++) {
1.193 millert 636: free(suser);
637: free(host);
638: free(src);
1.194 millert 639: r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
640: if (r == -1) {
641: fmprintf(stderr, "%s: invalid uri\n", argv[i]);
642: ++errs;
643: continue;
644: }
645: if (r != 0) {
1.193 millert 646: parse_user_host_path(argv[i], &suser, &host, &src);
1.194 millert 647: }
1.193 millert 648: if (suser != NULL && !okname(suser)) {
649: ++errs;
650: continue;
651: }
652: if (host && throughlocal) { /* extended remote to remote */
1.171 djm 653: xasprintf(&bp, "%s -f %s%s", cmd,
654: *src == '-' ? "-- " : "", src);
1.193 millert 655: if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
1.169 markus 656: exit(1);
1.173 djm 657: free(bp);
1.171 djm 658: xasprintf(&bp, "%s -t %s%s", cmd,
659: *targ == '-' ? "-- " : "", targ);
1.193 millert 660: if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
1.169 markus 661: exit(1);
1.173 djm 662: free(bp);
1.169 markus 663: (void) close(remin);
664: (void) close(remout);
665: remin = remout = -1;
1.193 millert 666: } else if (host) { /* standard remote to remote */
667: if (tport != -1 && tport != SSH_DEFAULT_PORT) {
668: /* This would require the remote support URIs */
669: fatal("target port not supported with two "
670: "remote hosts without the -3 option");
671: }
672:
1.129 djm 673: freeargs(&alist);
674: addargs(&alist, "%s", ssh_program);
675: addargs(&alist, "-x");
1.168 djm 676: addargs(&alist, "-oClearAllForwardings=yes");
1.129 djm 677: addargs(&alist, "-n");
1.168 djm 678: for (j = 0; j < remote_remote_args.num; j++) {
679: addargs(&alist, "%s",
680: remote_remote_args.list[j]);
681: }
1.193 millert 682:
683: if (sport != -1) {
684: addargs(&alist, "-p");
685: addargs(&alist, "%d", sport);
686: }
687: if (suser) {
1.129 djm 688: addargs(&alist, "-l");
689: addargs(&alist, "%s", suser);
1.23 markus 690: }
1.165 guenther 691: addargs(&alist, "--");
1.129 djm 692: addargs(&alist, "%s", host);
693: addargs(&alist, "%s", cmd);
694: addargs(&alist, "%s", src);
695: addargs(&alist, "%s%s%s:%s",
696: tuser ? tuser : "", tuser ? "@" : "",
697: thost, targ);
698: if (do_local_cmd(&alist) != 0)
1.109 markus 699: errs = 1;
1.18 markus 700: } else { /* local to remote */
1.1 deraadt 701: if (remin == -1) {
1.171 djm 702: xasprintf(&bp, "%s -t %s%s", cmd,
703: *targ == '-' ? "-- " : "", targ);
1.193 millert 704: if (do_cmd(thost, tuser, tport, bp, &remin,
1.140 deraadt 705: &remout) < 0)
1.18 markus 706: exit(1);
1.1 deraadt 707: if (response() < 0)
708: exit(1);
1.173 djm 709: free(bp);
1.1 deraadt 710: }
1.18 markus 711: source(1, argv + i);
1.1 deraadt 712: }
713: }
1.193 millert 714: out:
715: free(tuser);
716: free(thost);
717: free(targ);
718: free(suser);
719: free(host);
720: free(src);
1.1 deraadt 721: }
722:
723: void
1.104 djm 724: tolocal(int argc, char **argv)
1.1 deraadt 725: {
1.193 millert 726: char *bp, *host = NULL, *src = NULL, *suser = NULL;
1.129 djm 727: arglist alist;
1.193 millert 728: int i, r, sport = -1;
1.129 djm 729:
730: memset(&alist, '\0', sizeof(alist));
731: alist.list = NULL;
1.1 deraadt 732:
733: for (i = 0; i < argc - 1; i++) {
1.193 millert 734: free(suser);
735: free(host);
736: free(src);
1.194 millert 737: r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
1.193 millert 738: if (r == -1) {
1.194 millert 739: fmprintf(stderr, "%s: invalid uri\n", argv[i]);
1.193 millert 740: ++errs;
741: continue;
742: }
743: if (r != 0)
744: parse_user_host_path(argv[i], &suser, &host, &src);
745: if (suser != NULL && !okname(suser)) {
746: ++errs;
747: continue;
748: }
749: if (!host) { /* Local to local. */
1.129 djm 750: freeargs(&alist);
751: addargs(&alist, "%s", _PATH_CP);
752: if (iamrecursive)
753: addargs(&alist, "-r");
754: if (pflag)
755: addargs(&alist, "-p");
1.165 guenther 756: addargs(&alist, "--");
1.129 djm 757: addargs(&alist, "%s", argv[i]);
758: addargs(&alist, "%s", argv[argc-1]);
759: if (do_local_cmd(&alist))
1.1 deraadt 760: ++errs;
761: continue;
762: }
1.193 millert 763: /* Remote to local. */
1.171 djm 764: xasprintf(&bp, "%s -f %s%s",
765: cmd, *src == '-' ? "-- " : "", src);
1.193 millert 766: if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
1.173 djm 767: free(bp);
1.18 markus 768: ++errs;
769: continue;
1.1 deraadt 770: }
1.173 djm 771: free(bp);
1.1 deraadt 772: sink(1, argv + argc - 1);
1.18 markus 773: (void) close(remin);
1.1 deraadt 774: remin = remout = -1;
775: }
1.193 millert 776: free(suser);
777: free(host);
778: free(src);
1.1 deraadt 779: }
780:
781: void
1.104 djm 782: source(int argc, char **argv)
1.1 deraadt 783: {
784: struct stat stb;
785: static BUF buffer;
786: BUF *bp;
1.163 dtucker 787: off_t i, statbytes;
1.180 djm 788: size_t amt, nr;
1.125 dtucker 789: int fd = -1, haderr, indx;
1.181 deraadt 790: char *last, *name, buf[2048], encname[PATH_MAX];
1.65 deraadt 791: int len;
1.1 deraadt 792:
793: for (indx = 0; indx < argc; ++indx) {
1.18 markus 794: name = argv[indx];
1.11 aaron 795: statbytes = 0;
1.65 deraadt 796: len = strlen(name);
797: while (len > 1 && name[len-1] == '/')
798: name[--len] = '\0';
1.158 dtucker 799: if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
800: goto syserr;
1.85 markus 801: if (strchr(name, '\n') != NULL) {
1.158 dtucker 802: strnvis(encname, name, sizeof(encname), VIS_NL);
803: name = encname;
1.85 markus 804: }
1.1 deraadt 805: if (fstat(fd, &stb) < 0) {
806: syserr: run_err("%s: %s", name, strerror(errno));
807: goto next;
808: }
1.163 dtucker 809: if (stb.st_size < 0) {
810: run_err("%s: %s", name, "Negative file size");
811: goto next;
812: }
1.157 djm 813: unset_nonblock(fd);
1.1 deraadt 814: switch (stb.st_mode & S_IFMT) {
815: case S_IFREG:
816: break;
817: case S_IFDIR:
818: if (iamrecursive) {
819: rsource(name, &stb);
820: goto next;
821: }
822: /* FALLTHROUGH */
823: default:
824: run_err("%s: not a regular file", name);
825: goto next;
826: }
827: if ((last = strrchr(name, '/')) == NULL)
828: last = name;
829: else
830: ++last;
1.11 aaron 831: curfile = last;
1.1 deraadt 832: if (pflag) {
1.176 guenther 833: if (do_times(remout, verbose_mode, &stb) < 0)
1.1 deraadt 834: goto next;
835: }
836: #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1.61 markus 837: snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1.47 markus 838: (u_int) (stb.st_mode & FILEMODEMASK),
1.62 markus 839: (long long)stb.st_size, last);
1.186 schwarze 840: if (verbose_mode)
841: fmprintf(stderr, "Sending file modes: %s", buf);
1.107 deraadt 842: (void) atomicio(vwrite, remout, buf, strlen(buf));
1.1 deraadt 843: if (response() < 0)
844: goto next;
1.161 djm 845: if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
1.127 deraadt 846: next: if (fd != -1) {
847: (void) close(fd);
848: fd = -1;
849: }
1.1 deraadt 850: continue;
851: }
1.97 fgsch 852: if (showprogress)
853: start_progress_meter(curfile, stb.st_size, &statbytes);
1.161 djm 854: set_nonblock(remout);
1.1 deraadt 855: for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
856: amt = bp->cnt;
1.163 dtucker 857: if (i + (off_t)amt > stb.st_size)
1.1 deraadt 858: amt = stb.st_size - i;
859: if (!haderr) {
1.180 djm 860: if ((nr = atomicio(read, fd,
861: bp->buf, amt)) != amt) {
1.122 avsm 862: haderr = errno;
1.180 djm 863: memset(bp->buf + nr, 0, amt - nr);
864: }
1.1 deraadt 865: }
1.161 djm 866: /* Keep writing after error to retain sync */
867: if (haderr) {
868: (void)atomicio(vwrite, remout, bp->buf, amt);
1.180 djm 869: memset(bp->buf, 0, amt);
1.161 djm 870: continue;
1.1 deraadt 871: }
1.167 djm 872: if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
1.161 djm 873: &statbytes) != amt)
874: haderr = errno;
1.1 deraadt 875: }
1.161 djm 876: unset_nonblock(remout);
1.4 aaron 877:
1.127 deraadt 878: if (fd != -1) {
879: if (close(fd) < 0 && !haderr)
880: haderr = errno;
881: fd = -1;
882: }
1.1 deraadt 883: if (!haderr)
1.107 deraadt 884: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 885: else
886: run_err("%s: %s", name, strerror(haderr));
1.18 markus 887: (void) response();
1.185 dtucker 888: if (showprogress)
889: stop_progress_meter();
1.1 deraadt 890: }
891: }
892:
893: void
1.104 djm 894: rsource(char *name, struct stat *statp)
1.1 deraadt 895: {
896: DIR *dirp;
897: struct dirent *dp;
1.181 deraadt 898: char *last, *vect[1], path[PATH_MAX];
1.1 deraadt 899:
900: if (!(dirp = opendir(name))) {
901: run_err("%s: %s", name, strerror(errno));
902: return;
903: }
904: last = strrchr(name, '/');
1.183 mmcc 905: if (last == NULL)
1.1 deraadt 906: last = name;
907: else
908: last++;
909: if (pflag) {
1.176 guenther 910: if (do_times(remout, verbose_mode, statp) < 0) {
1.1 deraadt 911: closedir(dirp);
912: return;
913: }
914: }
1.55 deraadt 915: (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1.47 markus 916: (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1.18 markus 917: if (verbose_mode)
1.186 schwarze 918: fmprintf(stderr, "Entering directory: %s", path);
1.107 deraadt 919: (void) atomicio(vwrite, remout, path, strlen(path));
1.1 deraadt 920: if (response() < 0) {
921: closedir(dirp);
922: return;
923: }
1.63 stevesk 924: while ((dp = readdir(dirp)) != NULL) {
1.1 deraadt 925: if (dp->d_ino == 0)
926: continue;
927: if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
928: continue;
929: if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
930: run_err("%s/%s: name too long", name, dp->d_name);
931: continue;
932: }
1.55 deraadt 933: (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1.1 deraadt 934: vect[0] = path;
935: source(1, vect);
936: }
1.18 markus 937: (void) closedir(dirp);
1.107 deraadt 938: (void) atomicio(vwrite, remout, "E\n", 2);
1.18 markus 939: (void) response();
1.1 deraadt 940: }
941:
1.189 millert 942: #define TYPE_OVERFLOW(type, val) \
943: ((sizeof(type) == 4 && (val) > INT32_MAX) || \
944: (sizeof(type) == 8 && (val) > INT64_MAX) || \
945: (sizeof(type) != 4 && sizeof(type) != 8))
946:
1.1 deraadt 947: void
1.104 djm 948: sink(int argc, char **argv)
1.1 deraadt 949: {
950: static BUF buffer;
951: struct stat stb;
1.18 markus 952: enum {
953: YES, NO, DISPLAYED
954: } wrerr;
1.1 deraadt 955: BUF *bp;
1.122 avsm 956: off_t i;
1.124 djm 957: size_t j, count;
1.140 deraadt 958: int amt, exists, first, ofd;
959: mode_t mode, omode, mask;
1.97 fgsch 960: off_t size, statbytes;
1.176 guenther 961: unsigned long long ull;
1.36 deraadt 962: int setimes, targisdir, wrerrno = 0;
1.186 schwarze 963: char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
1.40 markus 964: struct timeval tv[2];
1.1 deraadt 965:
1.66 stevesk 966: #define atime tv[0]
967: #define mtime tv[1]
1.118 deraadt 968: #define SCREWUP(str) { why = str; goto screwup; }
1.1 deraadt 969:
1.189 millert 970: if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
971: SCREWUP("Unexpected off_t/time_t size");
972:
1.1 deraadt 973: setimes = targisdir = 0;
974: mask = umask(0);
975: if (!pflag)
1.18 markus 976: (void) umask(mask);
1.1 deraadt 977: if (argc != 1) {
978: run_err("ambiguous target");
979: exit(1);
980: }
981: targ = *argv;
982: if (targetshouldbedirectory)
983: verifydir(targ);
1.18 markus 984:
1.107 deraadt 985: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 986: if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
987: targisdir = 1;
988: for (first = 1;; first = 0) {
989: cp = buf;
1.122 avsm 990: if (atomicio(read, remin, cp, 1) != 1)
1.1 deraadt 991: return;
992: if (*cp++ == '\n')
993: SCREWUP("unexpected <newline>");
994: do {
1.30 deraadt 995: if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1 deraadt 996: SCREWUP("lost connection");
997: *cp++ = ch;
998: } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
999: *cp = 0;
1.114 markus 1000: if (verbose_mode)
1.186 schwarze 1001: fmprintf(stderr, "Sink: %s", buf);
1.1 deraadt 1002:
1003: if (buf[0] == '\01' || buf[0] == '\02') {
1.186 schwarze 1004: if (iamremote == 0) {
1005: (void) snmprintf(visbuf, sizeof(visbuf),
1006: NULL, "%s", buf + 1);
1.107 deraadt 1007: (void) atomicio(vwrite, STDERR_FILENO,
1.186 schwarze 1008: visbuf, strlen(visbuf));
1009: }
1.1 deraadt 1010: if (buf[0] == '\02')
1011: exit(1);
1012: ++errs;
1013: continue;
1014: }
1015: if (buf[0] == 'E') {
1.107 deraadt 1016: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 1017: return;
1018: }
1019: if (ch == '\n')
1020: *--cp = 0;
1021:
1022: cp = buf;
1023: if (*cp == 'T') {
1024: setimes++;
1025: cp++;
1.176 guenther 1026: if (!isdigit((unsigned char)*cp))
1027: SCREWUP("mtime.sec not present");
1028: ull = strtoull(cp, &cp, 10);
1.66 stevesk 1029: if (!cp || *cp++ != ' ')
1.1 deraadt 1030: SCREWUP("mtime.sec not delimited");
1.189 millert 1031: if (TYPE_OVERFLOW(time_t, ull))
1.176 guenther 1032: setimes = 0; /* out of range */
1033: mtime.tv_sec = ull;
1.66 stevesk 1034: mtime.tv_usec = strtol(cp, &cp, 10);
1.176 guenther 1035: if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
1036: mtime.tv_usec > 999999)
1.1 deraadt 1037: SCREWUP("mtime.usec not delimited");
1.176 guenther 1038: if (!isdigit((unsigned char)*cp))
1039: SCREWUP("atime.sec not present");
1040: ull = strtoull(cp, &cp, 10);
1.66 stevesk 1041: if (!cp || *cp++ != ' ')
1.1 deraadt 1042: SCREWUP("atime.sec not delimited");
1.189 millert 1043: if (TYPE_OVERFLOW(time_t, ull))
1.176 guenther 1044: setimes = 0; /* out of range */
1045: atime.tv_sec = ull;
1.66 stevesk 1046: atime.tv_usec = strtol(cp, &cp, 10);
1.176 guenther 1047: if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
1048: atime.tv_usec > 999999)
1.1 deraadt 1049: SCREWUP("atime.usec not delimited");
1.107 deraadt 1050: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 1051: continue;
1052: }
1053: if (*cp != 'C' && *cp != 'D') {
1054: /*
1055: * Check for the case "rcp remote:foo\* local:bar".
1056: * In this case, the line "No match." can be returned
1057: * by the shell before the rcp command on the remote is
1058: * executed so the ^Aerror_message convention isn't
1059: * followed.
1060: */
1061: if (first) {
1062: run_err("%s", cp);
1063: exit(1);
1064: }
1065: SCREWUP("expected control record");
1066: }
1067: mode = 0;
1068: for (++cp; cp < buf + 5; cp++) {
1069: if (*cp < '0' || *cp > '7')
1070: SCREWUP("bad mode");
1071: mode = (mode << 3) | (*cp - '0');
1072: }
1.197 dtucker 1073: if (!pflag)
1074: mode &= ~mask;
1.1 deraadt 1075: if (*cp++ != ' ')
1076: SCREWUP("mode not delimited");
1077:
1.188 millert 1078: if (!isdigit((unsigned char)*cp))
1079: SCREWUP("size not present");
1080: ull = strtoull(cp, &cp, 10);
1081: if (!cp || *cp++ != ' ')
1.1 deraadt 1082: SCREWUP("size not delimited");
1.189 millert 1083: if (TYPE_OVERFLOW(off_t, ull))
1.188 millert 1084: SCREWUP("size out of range");
1085: size = (off_t)ull;
1086:
1.198 djm 1087: if (*cp == '\0' || strchr(cp, '/') != NULL ||
1088: strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0) {
1.114 markus 1089: run_err("error: unexpected filename: %s", cp);
1090: exit(1);
1091: }
1.1 deraadt 1092: if (targisdir) {
1093: static char *namebuf;
1.124 djm 1094: static size_t cursize;
1.1 deraadt 1095: size_t need;
1096:
1097: need = strlen(targ) + strlen(cp) + 250;
1.55 deraadt 1098: if (need > cursize) {
1.173 djm 1099: free(namebuf);
1.18 markus 1100: namebuf = xmalloc(need);
1.55 deraadt 1101: cursize = need;
1102: }
1103: (void) snprintf(namebuf, need, "%s%s%s", targ,
1.88 mouring 1104: strcmp(targ, "/") ? "/" : "", cp);
1.1 deraadt 1105: np = namebuf;
1106: } else
1107: np = targ;
1.12 aaron 1108: curfile = cp;
1.1 deraadt 1109: exists = stat(np, &stb) == 0;
1110: if (buf[0] == 'D') {
1111: int mod_flag = pflag;
1.114 markus 1112: if (!iamrecursive)
1113: SCREWUP("received directory without -r");
1.1 deraadt 1114: if (exists) {
1115: if (!S_ISDIR(stb.st_mode)) {
1116: errno = ENOTDIR;
1117: goto bad;
1118: }
1119: if (pflag)
1.18 markus 1120: (void) chmod(np, mode);
1.1 deraadt 1121: } else {
1.18 markus 1122: /* Handle copying from a read-only
1123: directory */
1.1 deraadt 1124: mod_flag = 1;
1125: if (mkdir(np, mode | S_IRWXU) < 0)
1126: goto bad;
1127: }
1.58 danh 1128: vect[0] = xstrdup(np);
1.1 deraadt 1129: sink(1, vect);
1130: if (setimes) {
1131: setimes = 0;
1.59 deraadt 1132: if (utimes(vect[0], tv) < 0)
1.18 markus 1133: run_err("%s: set times: %s",
1.59 deraadt 1134: vect[0], strerror(errno));
1.1 deraadt 1135: }
1136: if (mod_flag)
1.59 deraadt 1137: (void) chmod(vect[0], mode);
1.173 djm 1138: free(vect[0]);
1.1 deraadt 1139: continue;
1140: }
1141: omode = mode;
1.174 dtucker 1142: mode |= S_IWUSR;
1.71 markus 1143: if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.1 deraadt 1144: bad: run_err("%s: %s", np, strerror(errno));
1145: continue;
1146: }
1.107 deraadt 1147: (void) atomicio(vwrite, remout, "", 1);
1.161 djm 1148: if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
1.18 markus 1149: (void) close(ofd);
1.1 deraadt 1150: continue;
1151: }
1152: cp = bp->buf;
1153: wrerr = NO;
1.7 deraadt 1154:
1.12 aaron 1155: statbytes = 0;
1.97 fgsch 1156: if (showprogress)
1157: start_progress_meter(curfile, size, &statbytes);
1.161 djm 1158: set_nonblock(remin);
1159: for (count = i = 0; i < size; i += bp->cnt) {
1160: amt = bp->cnt;
1.1 deraadt 1161: if (i + amt > size)
1162: amt = size - i;
1163: count += amt;
1164: do {
1.167 djm 1165: j = atomicio6(read, remin, cp, amt,
1166: scpio, &statbytes);
1.122 avsm 1167: if (j == 0) {
1.161 djm 1168: run_err("%s", j != EPIPE ?
1169: strerror(errno) :
1.63 stevesk 1170: "dropped connection");
1.1 deraadt 1171: exit(1);
1172: }
1173: amt -= j;
1174: cp += j;
1175: } while (amt > 0);
1.112 djm 1176:
1.1 deraadt 1177: if (count == bp->cnt) {
1178: /* Keep reading so we stay sync'd up. */
1179: if (wrerr == NO) {
1.122 avsm 1180: if (atomicio(vwrite, ofd, bp->buf,
1181: count) != count) {
1.1 deraadt 1182: wrerr = YES;
1.122 avsm 1183: wrerrno = errno;
1.1 deraadt 1184: }
1185: }
1186: count = 0;
1187: cp = bp->buf;
1188: }
1189: }
1.161 djm 1190: unset_nonblock(remin);
1.1 deraadt 1191: if (count != 0 && wrerr == NO &&
1.122 avsm 1192: atomicio(vwrite, ofd, bp->buf, count) != count) {
1.1 deraadt 1193: wrerr = YES;
1.122 avsm 1194: wrerrno = errno;
1.1 deraadt 1195: }
1.159 djm 1196: if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1197: ftruncate(ofd, size) != 0) {
1.1 deraadt 1198: run_err("%s: truncate: %s", np, strerror(errno));
1199: wrerr = DISPLAYED;
1200: }
1201: if (pflag) {
1202: if (exists || omode != mode)
1.116 dtucker 1203: if (fchmod(ofd, omode)) {
1.1 deraadt 1204: run_err("%s: set mode: %s",
1.63 stevesk 1205: np, strerror(errno));
1.116 dtucker 1206: wrerr = DISPLAYED;
1207: }
1.1 deraadt 1208: } else {
1209: if (!exists && omode != mode)
1.116 dtucker 1210: if (fchmod(ofd, omode & ~mask)) {
1.1 deraadt 1211: run_err("%s: set mode: %s",
1.63 stevesk 1212: np, strerror(errno));
1.116 dtucker 1213: wrerr = DISPLAYED;
1214: }
1.1 deraadt 1215: }
1.33 provos 1216: if (close(ofd) == -1) {
1217: wrerr = YES;
1218: wrerrno = errno;
1219: }
1.18 markus 1220: (void) response();
1.185 dtucker 1221: if (showprogress)
1222: stop_progress_meter();
1.1 deraadt 1223: if (setimes && wrerr == NO) {
1224: setimes = 0;
1.40 markus 1225: if (utimes(np, tv) < 0) {
1.1 deraadt 1226: run_err("%s: set times: %s",
1.63 stevesk 1227: np, strerror(errno));
1.1 deraadt 1228: wrerr = DISPLAYED;
1229: }
1230: }
1.18 markus 1231: switch (wrerr) {
1.1 deraadt 1232: case YES:
1233: run_err("%s: %s", np, strerror(wrerrno));
1234: break;
1235: case NO:
1.107 deraadt 1236: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 1237: break;
1238: case DISPLAYED:
1239: break;
1240: }
1241: }
1242: screwup:
1243: run_err("protocol error: %s", why);
1244: exit(1);
1245: }
1246:
1247: int
1.86 itojun 1248: response(void)
1.1 deraadt 1249: {
1.186 schwarze 1250: char ch, *cp, resp, rbuf[2048], visbuf[2048];
1.1 deraadt 1251:
1.30 deraadt 1252: if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1.1 deraadt 1253: lostconn(0);
1254:
1255: cp = rbuf;
1.18 markus 1256: switch (resp) {
1257: case 0: /* ok */
1.1 deraadt 1258: return (0);
1259: default:
1260: *cp++ = resp;
1261: /* FALLTHROUGH */
1.18 markus 1262: case 1: /* error, followed by error msg */
1263: case 2: /* fatal error, "" */
1.1 deraadt 1264: do {
1.30 deraadt 1265: if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1 deraadt 1266: lostconn(0);
1267: *cp++ = ch;
1268: } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1269:
1.186 schwarze 1270: if (!iamremote) {
1271: cp[-1] = '\0';
1272: (void) snmprintf(visbuf, sizeof(visbuf),
1273: NULL, "%s\n", rbuf);
1274: (void) atomicio(vwrite, STDERR_FILENO,
1275: visbuf, strlen(visbuf));
1276: }
1.1 deraadt 1277: ++errs;
1278: if (resp == 1)
1279: return (-1);
1280: exit(1);
1281: }
1282: /* NOTREACHED */
1283: }
1284:
1285: void
1.86 itojun 1286: usage(void)
1.1 deraadt 1287: {
1.83 stevesk 1288: (void) fprintf(stderr,
1.191 jmc 1289: "usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1.199 ! tb 1290: " [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
! 1291: " [-S program] source ... target\n");
1.1 deraadt 1292: exit(1);
1293: }
1294:
1295: void
1.18 markus 1296: run_err(const char *fmt,...)
1.1 deraadt 1297: {
1298: static FILE *fp;
1299: va_list ap;
1300:
1301: ++errs;
1.136 biorn 1302: if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1303: (void) fprintf(fp, "%c", 0x01);
1304: (void) fprintf(fp, "scp: ");
1305: va_start(ap, fmt);
1306: (void) vfprintf(fp, fmt, ap);
1307: va_end(ap);
1308: (void) fprintf(fp, "\n");
1309: (void) fflush(fp);
1310: }
1.18 markus 1311:
1312: if (!iamremote) {
1.73 markus 1313: va_start(ap, fmt);
1.186 schwarze 1314: vfmprintf(stderr, fmt, ap);
1.73 markus 1315: va_end(ap);
1.18 markus 1316: fprintf(stderr, "\n");
1317: }
1.1 deraadt 1318: }
1319:
1320: void
1.104 djm 1321: verifydir(char *cp)
1.1 deraadt 1322: {
1323: struct stat stb;
1324:
1325: if (!stat(cp, &stb)) {
1326: if (S_ISDIR(stb.st_mode))
1327: return;
1328: errno = ENOTDIR;
1329: }
1330: run_err("%s: %s", cp, strerror(errno));
1.123 avsm 1331: killchild(0);
1.1 deraadt 1332: }
1333:
1334: int
1.104 djm 1335: okname(char *cp0)
1.1 deraadt 1336: {
1337: int c;
1338: char *cp;
1339:
1340: cp = cp0;
1341: do {
1.75 deraadt 1342: c = (int)*cp;
1.1 deraadt 1343: if (c & 0200)
1344: goto bad;
1.179 deraadt 1345: if (!isalpha(c) && !isdigit((unsigned char)c)) {
1.101 markus 1346: switch (c) {
1347: case '\'':
1348: case '"':
1349: case '`':
1350: case ' ':
1351: case '#':
1352: goto bad;
1353: default:
1354: break;
1355: }
1356: }
1.1 deraadt 1357: } while (*++cp);
1358: return (1);
1359:
1.186 schwarze 1360: bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
1.1 deraadt 1361: return (0);
1362: }
1363:
1364: BUF *
1.104 djm 1365: allocbuf(BUF *bp, int fd, int blksize)
1.1 deraadt 1366: {
1367: size_t size;
1368: struct stat stb;
1369:
1370: if (fstat(fd, &stb) < 0) {
1371: run_err("fstat: %s", strerror(errno));
1372: return (0);
1373: }
1.187 deraadt 1374: size = ROUNDUP(stb.st_blksize, blksize);
1.95 markus 1375: if (size == 0)
1.18 markus 1376: size = blksize;
1.1 deraadt 1377: if (bp->cnt >= size)
1378: return (bp);
1.192 deraadt 1379: bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
1.1 deraadt 1380: bp->cnt = size;
1381: return (bp);
1382: }
1383:
1384: void
1.104 djm 1385: lostconn(int signo)
1.1 deraadt 1386: {
1387: if (!iamremote)
1.172 dtucker 1388: (void)write(STDERR_FILENO, "lost connection\n", 16);
1.74 markus 1389: if (signo)
1390: _exit(1);
1391: else
1392: exit(1);
1.4 aaron 1393: }