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