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