Annotation of src/usr.bin/ssh/scp.c, Revision 1.128
1.1 deraadt 1: /*
1.38 deraadt 2: * scp - secure remote copy. This is basically patched BSD rcp which
3: * uses ssh to do the data transfer (instead of using rcmd).
1.27 markus 4: *
1.38 deraadt 5: * NOTE: This version should NOT be suid root. (This uses ssh to
6: * do the transfer and ssh has the necessary privileges.)
1.27 markus 7: *
1.38 deraadt 8: * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
9: *
10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
1.39 markus 15: */
1.38 deraadt 16: /*
1.60 deraadt 17: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18: * Copyright (c) 1999 Aaron Campbell. All rights reserved.
1.27 markus 19: *
1.38 deraadt 20: * Redistribution and use in source and binary forms, with or without
21: * modification, are permitted provided that the following conditions
22: * are met:
23: * 1. Redistributions of source code must retain the above copyright
24: * notice, this list of conditions and the following disclaimer.
25: * 2. Redistributions in binary form must reproduce the above copyright
26: * notice, this list of conditions and the following disclaimer in the
27: * documentation and/or other materials provided with the distribution.
1.27 markus 28: *
1.38 deraadt 29: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39: */
1.1 deraadt 40:
41: /*
1.35 deraadt 42: * Parts from:
43: *
1.1 deraadt 44: * Copyright (c) 1983, 1990, 1992, 1993, 1995
45: * The Regents of the University of California. All rights reserved.
46: *
47: * Redistribution and use in source and binary forms, with or without
48: * modification, are permitted provided that the following conditions
49: * are met:
50: * 1. Redistributions of source code must retain the above copyright
51: * notice, this list of conditions and the following disclaimer.
52: * 2. Redistributions in binary form must reproduce the above copyright
53: * notice, this list of conditions and the following disclaimer in the
54: * documentation and/or other materials provided with the distribution.
1.103 millert 55: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 56: * may be used to endorse or promote products derived from this software
57: * without specific prior written permission.
58: *
59: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69: * SUCH DAMAGE.
70: *
71: */
72:
73: #include "includes.h"
1.128 ! reyk 74: RCSID("$OpenBSD: scp.c,v 1.127 2005/11/12 18:38:15 deraadt Exp $");
1.1 deraadt 75:
76: #include "xmalloc.h"
1.51 markus 77: #include "atomicio.h"
1.50 markus 78: #include "pathnames.h"
1.51 markus 79: #include "log.h"
1.69 mouring 80: #include "misc.h"
1.97 fgsch 81: #include "progressmeter.h"
1.1 deraadt 82:
1.35 deraadt 83: int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
1.11 aaron 84:
1.99 markus 85: void bwlimit(int);
86:
1.70 mouring 87: /* Struct for addargs */
88: arglist args;
1.41 markus 89:
1.99 markus 90: /* Bandwidth limit */
1.113 djm 91: off_t limit_rate = 0;
1.99 markus 92:
1.11 aaron 93: /* Name of current file being transferred. */
94: char *curfile;
1.4 aaron 95:
1.1 deraadt 96: /* This is set to non-zero to enable verbose mode. */
1.17 markus 97: int verbose_mode = 0;
1.1 deraadt 98:
1.6 aaron 99: /* This is set to zero if the progressmeter is not desired. */
100: int showprogress = 1;
101:
1.34 deraadt 102: /* This is the program to execute for the secured connection. ("ssh" or -S) */
1.50 markus 103: char *ssh_program = _PATH_SSH_PROGRAM;
1.34 deraadt 104:
1.92 markus 105: /* This is used to store the pid of ssh_program */
1.105 djm 106: pid_t do_cmd_pid = -1;
107:
108: static void
109: killchild(int signo)
110: {
1.119 dtucker 111: if (do_cmd_pid > 1) {
1.123 avsm 112: kill(do_cmd_pid, signo ? signo : SIGTERM);
1.119 dtucker 113: waitpid(do_cmd_pid, NULL, 0);
114: }
1.105 djm 115:
1.123 avsm 116: if (signo)
117: _exit(1);
118: exit(1);
1.105 djm 119: }
1.92 markus 120:
1.20 markus 121: /*
122: * This function executes the given command as the specified user on the
123: * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
124: * assigns the input and output file descriptors on success.
125: */
1.1 deraadt 126:
1.27 markus 127: int
1.35 deraadt 128: do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
1.1 deraadt 129: {
1.18 markus 130: int pin[2], pout[2], reserved[2];
131:
132: if (verbose_mode)
1.78 deraadt 133: fprintf(stderr,
134: "Executing: program %s host %s, user %s, command %s\n",
135: ssh_program, host,
136: remuser ? remuser : "(unspecified)", cmd);
1.18 markus 137:
1.20 markus 138: /*
139: * Reserve two descriptors so that the real pipes won't get
140: * descriptors 0 and 1 because that will screw up dup2 below.
141: */
1.18 markus 142: pipe(reserved);
143:
144: /* Create a socket pair for communicating with ssh. */
145: if (pipe(pin) < 0)
146: fatal("pipe: %s", strerror(errno));
147: if (pipe(pout) < 0)
148: fatal("pipe: %s", strerror(errno));
149:
150: /* Free the reserved descriptors. */
151: close(reserved[0]);
152: close(reserved[1]);
153:
1.106 nino 154: /* Fork a child to execute the command on the remote host using ssh. */
1.92 markus 155: do_cmd_pid = fork();
156: if (do_cmd_pid == 0) {
1.18 markus 157: /* Child. */
158: close(pin[1]);
159: close(pout[0]);
160: dup2(pin[0], 0);
161: dup2(pout[1], 1);
162: close(pin[0]);
163: close(pout[1]);
164:
1.41 markus 165: args.list[0] = ssh_program;
166: if (remuser != NULL)
1.70 mouring 167: addargs(&args, "-l%s", remuser);
168: addargs(&args, "%s", host);
169: addargs(&args, "%s", cmd);
1.18 markus 170:
1.41 markus 171: execvp(ssh_program, args.list);
1.34 deraadt 172: perror(ssh_program);
1.18 markus 173: exit(1);
1.92 markus 174: } else if (do_cmd_pid == -1) {
175: fatal("fork: %s", strerror(errno));
1.18 markus 176: }
177: /* Parent. Close the other side, and return the local side. */
178: close(pin[0]);
179: *fdout = pin[1];
180: close(pout[1]);
181: *fdin = pout[0];
1.105 djm 182: signal(SIGTERM, killchild);
183: signal(SIGINT, killchild);
184: signal(SIGHUP, killchild);
1.18 markus 185: return 0;
1.1 deraadt 186: }
187:
188: typedef struct {
1.124 djm 189: size_t cnt;
1.1 deraadt 190: char *buf;
191: } BUF;
192:
1.18 markus 193: BUF *allocbuf(BUF *, int, int);
194: void lostconn(int);
195: void nospace(void);
196: int okname(char *);
197: void run_err(const char *,...);
198: void verifydir(char *);
1.1 deraadt 199:
200: struct passwd *pwd;
1.18 markus 201: uid_t userid;
1.1 deraadt 202: int errs, remin, remout;
203: int pflag, iamremote, iamrecursive, targetshouldbedirectory;
204:
205: #define CMDNEEDS 64
206: char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
207:
1.18 markus 208: int response(void);
209: void rsource(char *, struct stat *);
210: void sink(int, char *[]);
211: void source(int, char *[]);
212: void tolocal(int, char *[]);
213: void toremote(char *, int, char *[]);
214: void usage(void);
1.1 deraadt 215:
216: int
1.104 djm 217: main(int argc, char **argv)
1.1 deraadt 218: {
1.92 markus 219: int ch, fflag, tflag, status;
1.99 markus 220: double speed;
221: char *targ, *endp;
1.1 deraadt 222: extern char *optarg;
223: extern int optind;
1.126 djm 224:
225: /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
226: sanitise_stdfd();
1.1 deraadt 227:
1.41 markus 228: args.list = NULL;
1.91 deraadt 229: addargs(&args, "ssh"); /* overwritten with ssh_program */
1.70 mouring 230: addargs(&args, "-x");
1.82 stevesk 231: addargs(&args, "-oForwardAgent no");
1.128 ! reyk 232: addargs(&args, "-oPermitLocalCommand no");
1.84 stevesk 233: addargs(&args, "-oClearAllForwardings yes");
1.41 markus 234:
1.1 deraadt 235: fflag = tflag = 0;
1.100 markus 236: while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
1.18 markus 237: switch (ch) {
238: /* User-visible flags. */
1.100 markus 239: case '1':
240: case '2':
1.23 markus 241: case '4':
1.41 markus 242: case '6':
243: case 'C':
1.70 mouring 244: addargs(&args, "-%c", ch);
1.41 markus 245: break;
246: case 'o':
247: case 'c':
248: case 'i':
1.83 stevesk 249: case 'F':
1.70 mouring 250: addargs(&args, "-%c%s", ch, optarg);
1.23 markus 251: break;
1.41 markus 252: case 'P':
1.70 mouring 253: addargs(&args, "-p%s", optarg);
1.41 markus 254: break;
255: case 'B':
1.70 mouring 256: addargs(&args, "-oBatchmode yes");
1.23 markus 257: break;
1.99 markus 258: case 'l':
259: speed = strtod(optarg, &endp);
260: if (speed <= 0 || *endp != '\0')
261: usage();
1.113 djm 262: limit_rate = speed * 1024;
1.99 markus 263: break;
1.1 deraadt 264: case 'p':
265: pflag = 1;
266: break;
267: case 'r':
268: iamrecursive = 1;
269: break;
1.34 deraadt 270: case 'S':
1.41 markus 271: ssh_program = xstrdup(optarg);
272: break;
273: case 'v':
1.72 markus 274: addargs(&args, "-v");
1.41 markus 275: verbose_mode = 1;
276: break;
277: case 'q':
1.111 dtucker 278: addargs(&args, "-q");
1.41 markus 279: showprogress = 0;
1.34 deraadt 280: break;
281:
1.18 markus 282: /* Server options. */
1.1 deraadt 283: case 'd':
284: targetshouldbedirectory = 1;
285: break;
1.18 markus 286: case 'f': /* "from" */
1.1 deraadt 287: iamremote = 1;
288: fflag = 1;
289: break;
1.18 markus 290: case 't': /* "to" */
1.1 deraadt 291: iamremote = 1;
292: tflag = 1;
293: break;
294: default:
295: usage();
296: }
297: argc -= optind;
298: argv += optind;
299:
300: if ((pwd = getpwuid(userid = getuid())) == NULL)
1.108 deraadt 301: fatal("unknown user %u", (u_int) userid);
1.1 deraadt 302:
1.18 markus 303: if (!isatty(STDERR_FILENO))
1.7 deraadt 304: showprogress = 0;
305:
1.1 deraadt 306: remin = STDIN_FILENO;
307: remout = STDOUT_FILENO;
308:
1.52 stevesk 309: if (fflag) {
1.18 markus 310: /* Follow "protocol", send data. */
311: (void) response();
1.1 deraadt 312: source(argc, argv);
313: exit(errs != 0);
314: }
1.18 markus 315: if (tflag) {
316: /* Receive data. */
1.1 deraadt 317: sink(argc, argv);
318: exit(errs != 0);
319: }
320: if (argc < 2)
321: usage();
322: if (argc > 2)
323: targetshouldbedirectory = 1;
324:
325: remin = remout = -1;
1.92 markus 326: do_cmd_pid = -1;
1.1 deraadt 327: /* Command to be executed on remote system using "ssh". */
1.55 deraadt 328: (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
329: verbose_mode ? " -v" : "",
1.35 deraadt 330: iamrecursive ? " -r" : "", pflag ? " -p" : "",
331: targetshouldbedirectory ? " -d" : "");
1.1 deraadt 332:
1.18 markus 333: (void) signal(SIGPIPE, lostconn);
1.1 deraadt 334:
335: if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
336: toremote(targ, argc, argv);
337: else {
1.18 markus 338: tolocal(argc, argv); /* Dest is local host. */
1.1 deraadt 339: if (targetshouldbedirectory)
340: verifydir(argv[argc - 1]);
1.92 markus 341: }
342: /*
343: * Finally check the exit status of the ssh process, if one was forked
344: * and no error has occured yet
345: */
346: if (do_cmd_pid != -1 && errs == 0) {
347: if (remin != -1)
348: (void) close(remin);
349: if (remout != -1)
350: (void) close(remout);
351: if (waitpid(do_cmd_pid, &status, 0) == -1)
352: errs = 1;
353: else {
354: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
355: errs = 1;
356: }
1.1 deraadt 357: }
358: exit(errs != 0);
359: }
360:
361: void
1.104 djm 362: toremote(char *targ, int argc, char **argv)
1.1 deraadt 363: {
364: int i, len;
1.120 deraadt 365: char *bp, *host, *src, *suser, *thost, *tuser, *arg;
1.1 deraadt 366:
367: *targ++ = 0;
368: if (*targ == 0)
369: targ = ".";
370:
1.121 djm 371: arg = xstrdup(argv[argc - 1]);
1.120 deraadt 372: if ((thost = strrchr(arg, '@'))) {
1.1 deraadt 373: /* user@host */
374: *thost++ = 0;
1.120 deraadt 375: tuser = arg;
1.1 deraadt 376: if (*tuser == '\0')
377: tuser = NULL;
378: } else {
1.120 deraadt 379: thost = arg;
1.1 deraadt 380: tuser = NULL;
381: }
382:
383: for (i = 0; i < argc - 1; i++) {
384: src = colon(argv[i]);
1.18 markus 385: if (src) { /* remote to remote */
1.84 stevesk 386: static char *ssh_options =
1.89 markus 387: "-x -o'ClearAllForwardings yes'";
1.1 deraadt 388: *src++ = 0;
389: if (*src == 0)
390: src = ".";
1.94 markus 391: host = strrchr(argv[i], '@');
1.34 deraadt 392: len = strlen(ssh_program) + strlen(argv[i]) +
1.35 deraadt 393: strlen(src) + (tuser ? strlen(tuser) : 0) +
1.84 stevesk 394: strlen(thost) + strlen(targ) +
395: strlen(ssh_options) + CMDNEEDS + 20;
1.18 markus 396: bp = xmalloc(len);
1.1 deraadt 397: if (host) {
398: *host++ = 0;
1.23 markus 399: host = cleanhostname(host);
1.1 deraadt 400: suser = argv[i];
401: if (*suser == '\0')
402: suser = pwd->pw_name;
1.102 markus 403: else if (!okname(suser)) {
404: xfree(bp);
1.1 deraadt 405: continue;
1.102 markus 406: }
407: if (tuser && !okname(tuser)) {
408: xfree(bp);
1.101 markus 409: continue;
1.102 markus 410: }
1.55 deraadt 411: snprintf(bp, len,
1.84 stevesk 412: "%s%s %s -n "
1.55 deraadt 413: "-l %s %s %s %s '%s%s%s:%s'",
1.44 deraadt 414: ssh_program, verbose_mode ? " -v" : "",
1.84 stevesk 415: ssh_options, suser, host, cmd, src,
1.44 deraadt 416: tuser ? tuser : "", tuser ? "@" : "",
417: thost, targ);
1.23 markus 418: } else {
419: host = cleanhostname(argv[i]);
1.55 deraadt 420: snprintf(bp, len,
1.84 stevesk 421: "exec %s%s %s -n %s "
1.55 deraadt 422: "%s %s '%s%s%s:%s'",
1.44 deraadt 423: ssh_program, verbose_mode ? " -v" : "",
1.84 stevesk 424: ssh_options, host, cmd, src,
1.44 deraadt 425: tuser ? tuser : "", tuser ? "@" : "",
426: thost, targ);
1.23 markus 427: }
1.18 markus 428: if (verbose_mode)
429: fprintf(stderr, "Executing: %s\n", bp);
1.109 markus 430: if (system(bp) != 0)
431: errs = 1;
1.18 markus 432: (void) xfree(bp);
433: } else { /* local to remote */
1.1 deraadt 434: if (remin == -1) {
435: len = strlen(targ) + CMDNEEDS + 20;
1.18 markus 436: bp = xmalloc(len);
1.55 deraadt 437: (void) snprintf(bp, len, "%s -t %s", cmd, targ);
1.23 markus 438: host = cleanhostname(thost);
1.35 deraadt 439: if (do_cmd(host, tuser, bp, &remin,
440: &remout, argc) < 0)
1.18 markus 441: exit(1);
1.1 deraadt 442: if (response() < 0)
443: exit(1);
1.18 markus 444: (void) xfree(bp);
1.1 deraadt 445: }
1.18 markus 446: source(1, argv + i);
1.1 deraadt 447: }
448: }
449: }
450:
451: void
1.104 djm 452: tolocal(int argc, char **argv)
1.1 deraadt 453: {
454: int i, len;
455: char *bp, *host, *src, *suser;
456:
457: for (i = 0; i < argc - 1; i++) {
1.18 markus 458: if (!(src = colon(argv[i]))) { /* Local to local. */
1.1 deraadt 459: len = strlen(_PATH_CP) + strlen(argv[i]) +
1.35 deraadt 460: strlen(argv[argc - 1]) + 20;
1.1 deraadt 461: bp = xmalloc(len);
1.55 deraadt 462: (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
1.35 deraadt 463: iamrecursive ? " -r" : "", pflag ? " -p" : "",
464: argv[i], argv[argc - 1]);
1.18 markus 465: if (verbose_mode)
466: fprintf(stderr, "Executing: %s\n", bp);
1.1 deraadt 467: if (system(bp))
468: ++errs;
1.18 markus 469: (void) xfree(bp);
1.1 deraadt 470: continue;
471: }
472: *src++ = 0;
473: if (*src == 0)
474: src = ".";
1.94 markus 475: if ((host = strrchr(argv[i], '@')) == NULL) {
1.1 deraadt 476: host = argv[i];
477: suser = NULL;
478: } else {
479: *host++ = 0;
480: suser = argv[i];
481: if (*suser == '\0')
482: suser = pwd->pw_name;
483: }
1.23 markus 484: host = cleanhostname(host);
1.1 deraadt 485: len = strlen(src) + CMDNEEDS + 20;
1.18 markus 486: bp = xmalloc(len);
1.55 deraadt 487: (void) snprintf(bp, len, "%s -f %s", cmd, src);
1.35 deraadt 488: if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
1.18 markus 489: (void) xfree(bp);
490: ++errs;
491: continue;
1.1 deraadt 492: }
1.18 markus 493: xfree(bp);
1.1 deraadt 494: sink(1, argv + argc - 1);
1.18 markus 495: (void) close(remin);
1.1 deraadt 496: remin = remout = -1;
497: }
498: }
499:
500: void
1.104 djm 501: source(int argc, char **argv)
1.1 deraadt 502: {
503: struct stat stb;
504: static BUF buffer;
505: BUF *bp;
1.122 avsm 506: off_t i, amt, statbytes;
507: size_t result;
1.125 dtucker 508: int fd = -1, haderr, indx;
1.1 deraadt 509: char *last, *name, buf[2048];
1.65 deraadt 510: int len;
1.1 deraadt 511:
512: for (indx = 0; indx < argc; ++indx) {
1.18 markus 513: name = argv[indx];
1.11 aaron 514: statbytes = 0;
1.65 deraadt 515: len = strlen(name);
516: while (len > 1 && name[len-1] == '/')
517: name[--len] = '\0';
1.85 markus 518: if (strchr(name, '\n') != NULL) {
519: run_err("%s: skipping, filename contains a newline",
520: name);
521: goto next;
522: }
1.1 deraadt 523: if ((fd = open(name, O_RDONLY, 0)) < 0)
524: goto syserr;
525: if (fstat(fd, &stb) < 0) {
526: syserr: run_err("%s: %s", name, strerror(errno));
527: goto next;
528: }
529: switch (stb.st_mode & S_IFMT) {
530: case S_IFREG:
531: break;
532: case S_IFDIR:
533: if (iamrecursive) {
534: rsource(name, &stb);
535: goto next;
536: }
537: /* FALLTHROUGH */
538: default:
539: run_err("%s: not a regular file", name);
540: goto next;
541: }
542: if ((last = strrchr(name, '/')) == NULL)
543: last = name;
544: else
545: ++last;
1.11 aaron 546: curfile = last;
1.1 deraadt 547: if (pflag) {
548: /*
549: * Make it compatible with possible future
550: * versions expecting microseconds.
551: */
1.55 deraadt 552: (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
1.47 markus 553: (u_long) stb.st_mtime,
554: (u_long) stb.st_atime);
1.107 deraadt 555: (void) atomicio(vwrite, remout, buf, strlen(buf));
1.1 deraadt 556: if (response() < 0)
557: goto next;
558: }
559: #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1.61 markus 560: snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1.47 markus 561: (u_int) (stb.st_mode & FILEMODEMASK),
1.62 markus 562: (long long)stb.st_size, last);
1.18 markus 563: if (verbose_mode) {
564: fprintf(stderr, "Sending file modes: %s", buf);
565: }
1.107 deraadt 566: (void) atomicio(vwrite, remout, buf, strlen(buf));
1.1 deraadt 567: if (response() < 0)
568: goto next;
569: if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
1.127 deraadt 570: next: if (fd != -1) {
571: (void) close(fd);
572: fd = -1;
573: }
1.1 deraadt 574: continue;
575: }
1.97 fgsch 576: if (showprogress)
577: start_progress_meter(curfile, stb.st_size, &statbytes);
1.1 deraadt 578: /* Keep writing after an error so that we stay sync'd up. */
579: for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
580: amt = bp->cnt;
581: if (i + amt > stb.st_size)
582: amt = stb.st_size - i;
583: if (!haderr) {
1.30 deraadt 584: result = atomicio(read, fd, bp->buf, amt);
1.1 deraadt 585: if (result != amt)
1.122 avsm 586: haderr = errno;
1.1 deraadt 587: }
588: if (haderr)
1.107 deraadt 589: (void) atomicio(vwrite, remout, bp->buf, amt);
1.1 deraadt 590: else {
1.107 deraadt 591: result = atomicio(vwrite, remout, bp->buf, amt);
1.1 deraadt 592: if (result != amt)
1.122 avsm 593: haderr = errno;
1.4 aaron 594: statbytes += result;
1.1 deraadt 595: }
1.113 djm 596: if (limit_rate)
1.99 markus 597: bwlimit(amt);
1.1 deraadt 598: }
1.18 markus 599: if (showprogress)
1.97 fgsch 600: stop_progress_meter();
1.4 aaron 601:
1.127 deraadt 602: if (fd != -1) {
603: if (close(fd) < 0 && !haderr)
604: haderr = errno;
605: fd = -1;
606: }
1.1 deraadt 607: if (!haderr)
1.107 deraadt 608: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 609: else
610: run_err("%s: %s", name, strerror(haderr));
1.18 markus 611: (void) response();
1.1 deraadt 612: }
613: }
614:
615: void
1.104 djm 616: rsource(char *name, struct stat *statp)
1.1 deraadt 617: {
618: DIR *dirp;
619: struct dirent *dp;
620: char *last, *vect[1], path[1100];
621:
622: if (!(dirp = opendir(name))) {
623: run_err("%s: %s", name, strerror(errno));
624: return;
625: }
626: last = strrchr(name, '/');
627: if (last == 0)
628: last = name;
629: else
630: last++;
631: if (pflag) {
1.55 deraadt 632: (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
1.47 markus 633: (u_long) statp->st_mtime,
634: (u_long) statp->st_atime);
1.107 deraadt 635: (void) atomicio(vwrite, remout, path, strlen(path));
1.1 deraadt 636: if (response() < 0) {
637: closedir(dirp);
638: return;
639: }
640: }
1.55 deraadt 641: (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1.47 markus 642: (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1.18 markus 643: if (verbose_mode)
644: fprintf(stderr, "Entering directory: %s", path);
1.107 deraadt 645: (void) atomicio(vwrite, remout, path, strlen(path));
1.1 deraadt 646: if (response() < 0) {
647: closedir(dirp);
648: return;
649: }
1.63 stevesk 650: while ((dp = readdir(dirp)) != NULL) {
1.1 deraadt 651: if (dp->d_ino == 0)
652: continue;
653: if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
654: continue;
655: if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
656: run_err("%s/%s: name too long", name, dp->d_name);
657: continue;
658: }
1.55 deraadt 659: (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1.1 deraadt 660: vect[0] = path;
661: source(1, vect);
662: }
1.18 markus 663: (void) closedir(dirp);
1.107 deraadt 664: (void) atomicio(vwrite, remout, "E\n", 2);
1.18 markus 665: (void) response();
1.1 deraadt 666: }
667:
668: void
1.99 markus 669: bwlimit(int amount)
670: {
671: static struct timeval bwstart, bwend;
672: static int lamt, thresh = 16384;
1.115 avsm 673: u_int64_t waitlen;
1.99 markus 674: struct timespec ts, rm;
675:
676: if (!timerisset(&bwstart)) {
677: gettimeofday(&bwstart, NULL);
678: return;
679: }
680:
681: lamt += amount;
682: if (lamt < thresh)
683: return;
684:
685: gettimeofday(&bwend, NULL);
686: timersub(&bwend, &bwstart, &bwend);
687: if (!timerisset(&bwend))
688: return;
689:
690: lamt *= 8;
1.115 avsm 691: waitlen = (double)1000000L * lamt / limit_rate;
1.99 markus 692:
1.115 avsm 693: bwstart.tv_sec = waitlen / 1000000L;
694: bwstart.tv_usec = waitlen % 1000000L;
1.99 markus 695:
696: if (timercmp(&bwstart, &bwend, >)) {
697: timersub(&bwstart, &bwend, &bwend);
698:
699: /* Adjust the wait time */
700: if (bwend.tv_sec) {
701: thresh /= 2;
702: if (thresh < 2048)
703: thresh = 2048;
704: } else if (bwend.tv_usec < 100) {
705: thresh *= 2;
706: if (thresh > 32768)
707: thresh = 32768;
708: }
709:
710: TIMEVAL_TO_TIMESPEC(&bwend, &ts);
711: while (nanosleep(&ts, &rm) == -1) {
712: if (errno != EINTR)
713: break;
714: ts = rm;
715: }
716: }
717:
718: lamt = 0;
719: gettimeofday(&bwstart, NULL);
720: }
721:
722: void
1.104 djm 723: sink(int argc, char **argv)
1.1 deraadt 724: {
725: static BUF buffer;
726: struct stat stb;
1.18 markus 727: enum {
728: YES, NO, DISPLAYED
729: } wrerr;
1.1 deraadt 730: BUF *bp;
1.122 avsm 731: off_t i;
1.124 djm 732: size_t j, count;
733: int amt, exists, first, mask, mode, ofd, omode;
1.97 fgsch 734: off_t size, statbytes;
1.36 deraadt 735: int setimes, targisdir, wrerrno = 0;
1.1 deraadt 736: char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
1.40 markus 737: struct timeval tv[2];
1.1 deraadt 738:
1.66 stevesk 739: #define atime tv[0]
740: #define mtime tv[1]
1.118 deraadt 741: #define SCREWUP(str) { why = str; goto screwup; }
1.1 deraadt 742:
743: setimes = targisdir = 0;
744: mask = umask(0);
745: if (!pflag)
1.18 markus 746: (void) umask(mask);
1.1 deraadt 747: if (argc != 1) {
748: run_err("ambiguous target");
749: exit(1);
750: }
751: targ = *argv;
752: if (targetshouldbedirectory)
753: verifydir(targ);
1.18 markus 754:
1.107 deraadt 755: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 756: if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
757: targisdir = 1;
758: for (first = 1;; first = 0) {
759: cp = buf;
1.122 avsm 760: if (atomicio(read, remin, cp, 1) != 1)
1.1 deraadt 761: return;
762: if (*cp++ == '\n')
763: SCREWUP("unexpected <newline>");
764: do {
1.30 deraadt 765: if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1 deraadt 766: SCREWUP("lost connection");
767: *cp++ = ch;
768: } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
769: *cp = 0;
1.114 markus 770: if (verbose_mode)
771: fprintf(stderr, "Sink: %s", buf);
1.1 deraadt 772:
773: if (buf[0] == '\01' || buf[0] == '\02') {
774: if (iamremote == 0)
1.107 deraadt 775: (void) atomicio(vwrite, STDERR_FILENO,
1.44 deraadt 776: buf + 1, strlen(buf + 1));
1.1 deraadt 777: if (buf[0] == '\02')
778: exit(1);
779: ++errs;
780: continue;
781: }
782: if (buf[0] == 'E') {
1.107 deraadt 783: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 784: return;
785: }
786: if (ch == '\n')
787: *--cp = 0;
788:
789: cp = buf;
790: if (*cp == 'T') {
791: setimes++;
792: cp++;
1.66 stevesk 793: mtime.tv_sec = strtol(cp, &cp, 10);
794: if (!cp || *cp++ != ' ')
1.1 deraadt 795: SCREWUP("mtime.sec not delimited");
1.66 stevesk 796: mtime.tv_usec = strtol(cp, &cp, 10);
797: if (!cp || *cp++ != ' ')
1.1 deraadt 798: SCREWUP("mtime.usec not delimited");
1.66 stevesk 799: atime.tv_sec = strtol(cp, &cp, 10);
800: if (!cp || *cp++ != ' ')
1.1 deraadt 801: SCREWUP("atime.sec not delimited");
1.66 stevesk 802: atime.tv_usec = strtol(cp, &cp, 10);
803: if (!cp || *cp++ != '\0')
1.1 deraadt 804: SCREWUP("atime.usec not delimited");
1.107 deraadt 805: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 806: continue;
807: }
808: if (*cp != 'C' && *cp != 'D') {
809: /*
810: * Check for the case "rcp remote:foo\* local:bar".
811: * In this case, the line "No match." can be returned
812: * by the shell before the rcp command on the remote is
813: * executed so the ^Aerror_message convention isn't
814: * followed.
815: */
816: if (first) {
817: run_err("%s", cp);
818: exit(1);
819: }
820: SCREWUP("expected control record");
821: }
822: mode = 0;
823: for (++cp; cp < buf + 5; cp++) {
824: if (*cp < '0' || *cp > '7')
825: SCREWUP("bad mode");
826: mode = (mode << 3) | (*cp - '0');
827: }
828: if (*cp++ != ' ')
829: SCREWUP("mode not delimited");
830:
1.63 stevesk 831: for (size = 0; isdigit(*cp);)
1.1 deraadt 832: size = size * 10 + (*cp++ - '0');
833: if (*cp++ != ' ')
834: SCREWUP("size not delimited");
1.114 markus 835: if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
836: run_err("error: unexpected filename: %s", cp);
837: exit(1);
838: }
1.1 deraadt 839: if (targisdir) {
840: static char *namebuf;
1.124 djm 841: static size_t cursize;
1.1 deraadt 842: size_t need;
843:
844: need = strlen(targ) + strlen(cp) + 250;
1.55 deraadt 845: if (need > cursize) {
1.58 danh 846: if (namebuf)
847: xfree(namebuf);
1.18 markus 848: namebuf = xmalloc(need);
1.55 deraadt 849: cursize = need;
850: }
851: (void) snprintf(namebuf, need, "%s%s%s", targ,
1.88 mouring 852: strcmp(targ, "/") ? "/" : "", cp);
1.1 deraadt 853: np = namebuf;
854: } else
855: np = targ;
1.12 aaron 856: curfile = cp;
1.1 deraadt 857: exists = stat(np, &stb) == 0;
858: if (buf[0] == 'D') {
859: int mod_flag = pflag;
1.114 markus 860: if (!iamrecursive)
861: SCREWUP("received directory without -r");
1.1 deraadt 862: if (exists) {
863: if (!S_ISDIR(stb.st_mode)) {
864: errno = ENOTDIR;
865: goto bad;
866: }
867: if (pflag)
1.18 markus 868: (void) chmod(np, mode);
1.1 deraadt 869: } else {
1.18 markus 870: /* Handle copying from a read-only
871: directory */
1.1 deraadt 872: mod_flag = 1;
873: if (mkdir(np, mode | S_IRWXU) < 0)
874: goto bad;
875: }
1.58 danh 876: vect[0] = xstrdup(np);
1.1 deraadt 877: sink(1, vect);
878: if (setimes) {
879: setimes = 0;
1.59 deraadt 880: if (utimes(vect[0], tv) < 0)
1.18 markus 881: run_err("%s: set times: %s",
1.59 deraadt 882: vect[0], strerror(errno));
1.1 deraadt 883: }
884: if (mod_flag)
1.59 deraadt 885: (void) chmod(vect[0], mode);
886: if (vect[0])
887: xfree(vect[0]);
1.1 deraadt 888: continue;
889: }
890: omode = mode;
891: mode |= S_IWRITE;
1.71 markus 892: if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.1 deraadt 893: bad: run_err("%s: %s", np, strerror(errno));
894: continue;
895: }
1.107 deraadt 896: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 897: if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
1.18 markus 898: (void) close(ofd);
1.1 deraadt 899: continue;
900: }
901: cp = bp->buf;
902: wrerr = NO;
1.7 deraadt 903:
1.12 aaron 904: statbytes = 0;
1.97 fgsch 905: if (showprogress)
906: start_progress_meter(curfile, size, &statbytes);
1.1 deraadt 907: for (count = i = 0; i < size; i += 4096) {
908: amt = 4096;
909: if (i + amt > size)
910: amt = size - i;
911: count += amt;
912: do {
1.117 avsm 913: j = atomicio(read, remin, cp, amt);
1.122 avsm 914: if (j == 0) {
1.1 deraadt 915: run_err("%s", j ? strerror(errno) :
1.63 stevesk 916: "dropped connection");
1.1 deraadt 917: exit(1);
918: }
919: amt -= j;
920: cp += j;
1.18 markus 921: statbytes += j;
1.1 deraadt 922: } while (amt > 0);
1.112 djm 923:
1.113 djm 924: if (limit_rate)
1.99 markus 925: bwlimit(4096);
926:
1.1 deraadt 927: if (count == bp->cnt) {
928: /* Keep reading so we stay sync'd up. */
929: if (wrerr == NO) {
1.122 avsm 930: if (atomicio(vwrite, ofd, bp->buf,
931: count) != count) {
1.1 deraadt 932: wrerr = YES;
1.122 avsm 933: wrerrno = errno;
1.1 deraadt 934: }
935: }
936: count = 0;
937: cp = bp->buf;
938: }
939: }
1.7 deraadt 940: if (showprogress)
1.97 fgsch 941: stop_progress_meter();
1.1 deraadt 942: if (count != 0 && wrerr == NO &&
1.122 avsm 943: atomicio(vwrite, ofd, bp->buf, count) != count) {
1.1 deraadt 944: wrerr = YES;
1.122 avsm 945: wrerrno = errno;
1.1 deraadt 946: }
1.98 djm 947: if (wrerr == NO && ftruncate(ofd, size) != 0) {
1.1 deraadt 948: run_err("%s: truncate: %s", np, strerror(errno));
949: wrerr = DISPLAYED;
950: }
951: if (pflag) {
952: if (exists || omode != mode)
1.116 dtucker 953: if (fchmod(ofd, omode)) {
1.1 deraadt 954: run_err("%s: set mode: %s",
1.63 stevesk 955: np, strerror(errno));
1.116 dtucker 956: wrerr = DISPLAYED;
957: }
1.1 deraadt 958: } else {
959: if (!exists && omode != mode)
1.116 dtucker 960: if (fchmod(ofd, omode & ~mask)) {
1.1 deraadt 961: run_err("%s: set mode: %s",
1.63 stevesk 962: np, strerror(errno));
1.116 dtucker 963: wrerr = DISPLAYED;
964: }
1.1 deraadt 965: }
1.33 provos 966: if (close(ofd) == -1) {
967: wrerr = YES;
968: wrerrno = errno;
969: }
1.18 markus 970: (void) response();
1.1 deraadt 971: if (setimes && wrerr == NO) {
972: setimes = 0;
1.40 markus 973: if (utimes(np, tv) < 0) {
1.1 deraadt 974: run_err("%s: set times: %s",
1.63 stevesk 975: np, strerror(errno));
1.1 deraadt 976: wrerr = DISPLAYED;
977: }
978: }
1.18 markus 979: switch (wrerr) {
1.1 deraadt 980: case YES:
981: run_err("%s: %s", np, strerror(wrerrno));
982: break;
983: case NO:
1.107 deraadt 984: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 985: break;
986: case DISPLAYED:
987: break;
988: }
989: }
990: screwup:
991: run_err("protocol error: %s", why);
992: exit(1);
993: }
994:
995: int
1.86 itojun 996: response(void)
1.1 deraadt 997: {
998: char ch, *cp, resp, rbuf[2048];
999:
1.30 deraadt 1000: if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1.1 deraadt 1001: lostconn(0);
1002:
1003: cp = rbuf;
1.18 markus 1004: switch (resp) {
1005: case 0: /* ok */
1.1 deraadt 1006: return (0);
1007: default:
1008: *cp++ = resp;
1009: /* FALLTHROUGH */
1.18 markus 1010: case 1: /* error, followed by error msg */
1011: case 2: /* fatal error, "" */
1.1 deraadt 1012: do {
1.30 deraadt 1013: if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1 deraadt 1014: lostconn(0);
1015: *cp++ = ch;
1016: } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1017:
1018: if (!iamremote)
1.107 deraadt 1019: (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1.1 deraadt 1020: ++errs;
1021: if (resp == 1)
1022: return (-1);
1023: exit(1);
1024: }
1025: /* NOTREACHED */
1026: }
1027:
1028: void
1.86 itojun 1029: usage(void)
1.1 deraadt 1030: {
1.83 stevesk 1031: (void) fprintf(stderr,
1.110 jmc 1032: "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1033: " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1.90 deraadt 1034: " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1.1 deraadt 1035: exit(1);
1036: }
1037:
1038: void
1.18 markus 1039: run_err(const char *fmt,...)
1.1 deraadt 1040: {
1041: static FILE *fp;
1042: va_list ap;
1043:
1044: ++errs;
1045: if (fp == NULL && !(fp = fdopen(remout, "w")))
1046: return;
1.18 markus 1047: (void) fprintf(fp, "%c", 0x01);
1048: (void) fprintf(fp, "scp: ");
1.73 markus 1049: va_start(ap, fmt);
1.18 markus 1050: (void) vfprintf(fp, fmt, ap);
1.73 markus 1051: va_end(ap);
1.18 markus 1052: (void) fprintf(fp, "\n");
1053: (void) fflush(fp);
1054:
1055: if (!iamremote) {
1.73 markus 1056: va_start(ap, fmt);
1.18 markus 1057: vfprintf(stderr, fmt, ap);
1.73 markus 1058: va_end(ap);
1.18 markus 1059: fprintf(stderr, "\n");
1060: }
1.1 deraadt 1061: }
1062:
1063: void
1.104 djm 1064: verifydir(char *cp)
1.1 deraadt 1065: {
1066: struct stat stb;
1067:
1068: if (!stat(cp, &stb)) {
1069: if (S_ISDIR(stb.st_mode))
1070: return;
1071: errno = ENOTDIR;
1072: }
1073: run_err("%s: %s", cp, strerror(errno));
1.123 avsm 1074: killchild(0);
1.1 deraadt 1075: }
1076:
1077: int
1.104 djm 1078: okname(char *cp0)
1.1 deraadt 1079: {
1080: int c;
1081: char *cp;
1082:
1083: cp = cp0;
1084: do {
1.75 deraadt 1085: c = (int)*cp;
1.1 deraadt 1086: if (c & 0200)
1087: goto bad;
1.101 markus 1088: if (!isalpha(c) && !isdigit(c)) {
1089: switch (c) {
1090: case '\'':
1091: case '"':
1092: case '`':
1093: case ' ':
1094: case '#':
1095: goto bad;
1096: default:
1097: break;
1098: }
1099: }
1.1 deraadt 1100: } while (*++cp);
1101: return (1);
1102:
1.25 markus 1103: bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1.1 deraadt 1104: return (0);
1105: }
1106:
1107: BUF *
1.104 djm 1108: allocbuf(BUF *bp, int fd, int blksize)
1.1 deraadt 1109: {
1110: size_t size;
1111: struct stat stb;
1112:
1113: if (fstat(fd, &stb) < 0) {
1114: run_err("fstat: %s", strerror(errno));
1115: return (0);
1116: }
1.95 markus 1117: size = roundup(stb.st_blksize, blksize);
1118: if (size == 0)
1.18 markus 1119: size = blksize;
1.1 deraadt 1120: if (bp->cnt >= size)
1121: return (bp);
1.18 markus 1122: if (bp->buf == NULL)
1123: bp->buf = xmalloc(size);
1124: else
1125: bp->buf = xrealloc(bp->buf, size);
1.81 markus 1126: memset(bp->buf, 0, size);
1.1 deraadt 1127: bp->cnt = size;
1128: return (bp);
1129: }
1130:
1131: void
1.104 djm 1132: lostconn(int signo)
1.1 deraadt 1133: {
1134: if (!iamremote)
1.74 markus 1135: write(STDERR_FILENO, "lost connection\n", 16);
1136: if (signo)
1137: _exit(1);
1138: else
1139: exit(1);
1.4 aaron 1140: }