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