Annotation of src/usr.bin/ssh/scp.c, Revision 1.185
1.185 ! dtucker 1: /* $OpenBSD: scp.c,v 1.184 2015/11/27 00:49:31 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;
1.184 deraadt 465:
466: if (pflag) {
467: /* Cannot pledge: -p allows setuid/setgid files... */
468: } else {
469: if (pledge("stdio rpath wpath cpath fattr tty proc exec",
470: NULL) == -1) {
471: perror("pledge");
472: exit(1);
473: }
474: }
1.7 deraadt 475:
1.1 deraadt 476: remin = STDIN_FILENO;
477: remout = STDOUT_FILENO;
478:
1.52 stevesk 479: if (fflag) {
1.18 markus 480: /* Follow "protocol", send data. */
481: (void) response();
1.1 deraadt 482: source(argc, argv);
483: exit(errs != 0);
484: }
1.18 markus 485: if (tflag) {
486: /* Receive data. */
1.1 deraadt 487: sink(argc, argv);
488: exit(errs != 0);
489: }
490: if (argc < 2)
491: usage();
492: if (argc > 2)
493: targetshouldbedirectory = 1;
494:
495: remin = remout = -1;
1.92 markus 496: do_cmd_pid = -1;
1.1 deraadt 497: /* Command to be executed on remote system using "ssh". */
1.55 deraadt 498: (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
499: verbose_mode ? " -v" : "",
1.35 deraadt 500: iamrecursive ? " -r" : "", pflag ? " -p" : "",
501: targetshouldbedirectory ? " -d" : "");
1.1 deraadt 502:
1.18 markus 503: (void) signal(SIGPIPE, lostconn);
1.1 deraadt 504:
505: if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
506: toremote(targ, argc, argv);
507: else {
508: if (targetshouldbedirectory)
509: verifydir(argv[argc - 1]);
1.130 djm 510: tolocal(argc, argv); /* Dest is local host. */
1.92 markus 511: }
512: /*
513: * Finally check the exit status of the ssh process, if one was forked
1.164 stevesk 514: * and no error has occurred yet
1.92 markus 515: */
516: if (do_cmd_pid != -1 && errs == 0) {
517: if (remin != -1)
518: (void) close(remin);
519: if (remout != -1)
520: (void) close(remout);
521: if (waitpid(do_cmd_pid, &status, 0) == -1)
522: errs = 1;
523: else {
524: if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
525: errs = 1;
526: }
1.1 deraadt 527: }
528: exit(errs != 0);
529: }
530:
1.167 djm 531: /* Callback from atomicio6 to update progress meter and limit bandwidth */
532: static int
533: scpio(void *_cnt, size_t s)
1.161 djm 534: {
1.167 djm 535: off_t *cnt = (off_t *)_cnt;
536:
537: *cnt += s;
538: if (limit_kbps > 0)
539: bandwidth_limit(&bwlimit, s);
540: return 0;
1.161 djm 541: }
542:
1.176 guenther 543: static int
544: do_times(int fd, int verb, const struct stat *sb)
545: {
546: /* strlen(2^64) == 20; strlen(10^6) == 7 */
547: char buf[(20 + 7 + 2) * 2 + 2];
548:
549: (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
550: (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
551: (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
552: if (verb) {
553: fprintf(stderr, "File mtime %lld atime %lld\n",
554: (long long)sb->st_mtime, (long long)sb->st_atime);
555: fprintf(stderr, "Sending file timestamps: %s", buf);
556: }
557: (void) atomicio(vwrite, fd, buf, strlen(buf));
558: return (response());
559: }
560:
1.1 deraadt 561: void
1.104 djm 562: toremote(char *targ, int argc, char **argv)
1.1 deraadt 563: {
1.120 deraadt 564: char *bp, *host, *src, *suser, *thost, *tuser, *arg;
1.129 djm 565: arglist alist;
1.140 deraadt 566: int i;
1.168 djm 567: u_int j;
1.129 djm 568:
569: memset(&alist, '\0', sizeof(alist));
570: alist.list = NULL;
1.1 deraadt 571:
572: *targ++ = 0;
573: if (*targ == 0)
574: targ = ".";
575:
1.121 djm 576: arg = xstrdup(argv[argc - 1]);
1.120 deraadt 577: if ((thost = strrchr(arg, '@'))) {
1.1 deraadt 578: /* user@host */
579: *thost++ = 0;
1.120 deraadt 580: tuser = arg;
1.1 deraadt 581: if (*tuser == '\0')
582: tuser = NULL;
583: } else {
1.120 deraadt 584: thost = arg;
1.1 deraadt 585: tuser = NULL;
586: }
587:
1.129 djm 588: if (tuser != NULL && !okname(tuser)) {
1.173 djm 589: free(arg);
1.129 djm 590: return;
591: }
592:
1.1 deraadt 593: for (i = 0; i < argc - 1; i++) {
594: src = colon(argv[i]);
1.169 markus 595: if (src && throughlocal) { /* extended remote to remote */
596: *src++ = 0;
597: if (*src == 0)
598: src = ".";
599: host = strrchr(argv[i], '@');
600: if (host) {
601: *host++ = 0;
602: host = cleanhostname(host);
603: suser = argv[i];
604: if (*suser == '\0')
605: suser = pwd->pw_name;
606: else if (!okname(suser))
607: continue;
608: } else {
609: host = cleanhostname(argv[i]);
610: suser = NULL;
611: }
1.171 djm 612: xasprintf(&bp, "%s -f %s%s", cmd,
613: *src == '-' ? "-- " : "", src);
1.169 markus 614: if (do_cmd(host, suser, bp, &remin, &remout) < 0)
615: exit(1);
1.173 djm 616: free(bp);
1.169 markus 617: host = cleanhostname(thost);
1.171 djm 618: xasprintf(&bp, "%s -t %s%s", cmd,
619: *targ == '-' ? "-- " : "", targ);
1.169 markus 620: if (do_cmd2(host, tuser, bp, remin, remout) < 0)
621: exit(1);
1.173 djm 622: free(bp);
1.169 markus 623: (void) close(remin);
624: (void) close(remout);
625: remin = remout = -1;
626: } else if (src) { /* standard remote to remote */
1.129 djm 627: freeargs(&alist);
628: addargs(&alist, "%s", ssh_program);
629: addargs(&alist, "-x");
1.168 djm 630: addargs(&alist, "-oClearAllForwardings=yes");
1.129 djm 631: addargs(&alist, "-n");
1.168 djm 632: for (j = 0; j < remote_remote_args.num; j++) {
633: addargs(&alist, "%s",
634: remote_remote_args.list[j]);
635: }
1.1 deraadt 636: *src++ = 0;
637: if (*src == 0)
638: src = ".";
1.94 markus 639: host = strrchr(argv[i], '@');
1.129 djm 640:
1.1 deraadt 641: if (host) {
642: *host++ = 0;
1.23 markus 643: host = cleanhostname(host);
1.1 deraadt 644: suser = argv[i];
645: if (*suser == '\0')
646: suser = pwd->pw_name;
1.129 djm 647: else if (!okname(suser))
1.101 markus 648: continue;
1.129 djm 649: addargs(&alist, "-l");
650: addargs(&alist, "%s", suser);
1.23 markus 651: } else {
652: host = cleanhostname(argv[i]);
653: }
1.165 guenther 654: addargs(&alist, "--");
1.129 djm 655: addargs(&alist, "%s", host);
656: addargs(&alist, "%s", cmd);
657: addargs(&alist, "%s", src);
658: addargs(&alist, "%s%s%s:%s",
659: tuser ? tuser : "", tuser ? "@" : "",
660: thost, targ);
661: if (do_local_cmd(&alist) != 0)
1.109 markus 662: errs = 1;
1.18 markus 663: } else { /* local to remote */
1.1 deraadt 664: if (remin == -1) {
1.171 djm 665: xasprintf(&bp, "%s -t %s%s", cmd,
666: *targ == '-' ? "-- " : "", targ);
1.23 markus 667: host = cleanhostname(thost);
1.35 deraadt 668: if (do_cmd(host, tuser, bp, &remin,
1.140 deraadt 669: &remout) < 0)
1.18 markus 670: exit(1);
1.1 deraadt 671: if (response() < 0)
672: exit(1);
1.173 djm 673: free(bp);
1.1 deraadt 674: }
1.18 markus 675: source(1, argv + i);
1.1 deraadt 676: }
677: }
1.173 djm 678: free(arg);
1.1 deraadt 679: }
680:
681: void
1.104 djm 682: tolocal(int argc, char **argv)
1.1 deraadt 683: {
684: char *bp, *host, *src, *suser;
1.129 djm 685: arglist alist;
1.140 deraadt 686: int i;
1.129 djm 687:
688: memset(&alist, '\0', sizeof(alist));
689: alist.list = NULL;
1.1 deraadt 690:
691: for (i = 0; i < argc - 1; i++) {
1.18 markus 692: if (!(src = colon(argv[i]))) { /* Local to local. */
1.129 djm 693: freeargs(&alist);
694: addargs(&alist, "%s", _PATH_CP);
695: if (iamrecursive)
696: addargs(&alist, "-r");
697: if (pflag)
698: addargs(&alist, "-p");
1.165 guenther 699: addargs(&alist, "--");
1.129 djm 700: addargs(&alist, "%s", argv[i]);
701: addargs(&alist, "%s", argv[argc-1]);
702: if (do_local_cmd(&alist))
1.1 deraadt 703: ++errs;
704: continue;
705: }
706: *src++ = 0;
707: if (*src == 0)
708: src = ".";
1.94 markus 709: if ((host = strrchr(argv[i], '@')) == NULL) {
1.1 deraadt 710: host = argv[i];
711: suser = NULL;
712: } else {
713: *host++ = 0;
714: suser = argv[i];
715: if (*suser == '\0')
716: suser = pwd->pw_name;
717: }
1.23 markus 718: host = cleanhostname(host);
1.171 djm 719: xasprintf(&bp, "%s -f %s%s",
720: cmd, *src == '-' ? "-- " : "", src);
1.140 deraadt 721: if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
1.173 djm 722: free(bp);
1.18 markus 723: ++errs;
724: continue;
1.1 deraadt 725: }
1.173 djm 726: free(bp);
1.1 deraadt 727: sink(1, argv + argc - 1);
1.18 markus 728: (void) close(remin);
1.1 deraadt 729: remin = remout = -1;
730: }
731: }
732:
733: void
1.104 djm 734: source(int argc, char **argv)
1.1 deraadt 735: {
736: struct stat stb;
737: static BUF buffer;
738: BUF *bp;
1.163 dtucker 739: off_t i, statbytes;
1.180 djm 740: size_t amt, nr;
1.125 dtucker 741: int fd = -1, haderr, indx;
1.181 deraadt 742: char *last, *name, buf[2048], encname[PATH_MAX];
1.65 deraadt 743: int len;
1.1 deraadt 744:
745: for (indx = 0; indx < argc; ++indx) {
1.18 markus 746: name = argv[indx];
1.11 aaron 747: statbytes = 0;
1.65 deraadt 748: len = strlen(name);
749: while (len > 1 && name[len-1] == '/')
750: name[--len] = '\0';
1.158 dtucker 751: if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
752: goto syserr;
1.85 markus 753: if (strchr(name, '\n') != NULL) {
1.158 dtucker 754: strnvis(encname, name, sizeof(encname), VIS_NL);
755: name = encname;
1.85 markus 756: }
1.1 deraadt 757: if (fstat(fd, &stb) < 0) {
758: syserr: run_err("%s: %s", name, strerror(errno));
759: goto next;
760: }
1.163 dtucker 761: if (stb.st_size < 0) {
762: run_err("%s: %s", name, "Negative file size");
763: goto next;
764: }
1.157 djm 765: unset_nonblock(fd);
1.1 deraadt 766: switch (stb.st_mode & S_IFMT) {
767: case S_IFREG:
768: break;
769: case S_IFDIR:
770: if (iamrecursive) {
771: rsource(name, &stb);
772: goto next;
773: }
774: /* FALLTHROUGH */
775: default:
776: run_err("%s: not a regular file", name);
777: goto next;
778: }
779: if ((last = strrchr(name, '/')) == NULL)
780: last = name;
781: else
782: ++last;
1.11 aaron 783: curfile = last;
1.1 deraadt 784: if (pflag) {
1.176 guenther 785: if (do_times(remout, verbose_mode, &stb) < 0)
1.1 deraadt 786: goto next;
787: }
788: #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1.61 markus 789: snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1.47 markus 790: (u_int) (stb.st_mode & FILEMODEMASK),
1.62 markus 791: (long long)stb.st_size, last);
1.18 markus 792: if (verbose_mode) {
793: fprintf(stderr, "Sending file modes: %s", buf);
794: }
1.107 deraadt 795: (void) atomicio(vwrite, remout, buf, strlen(buf));
1.1 deraadt 796: if (response() < 0)
797: goto next;
1.161 djm 798: if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
1.127 deraadt 799: next: if (fd != -1) {
800: (void) close(fd);
801: fd = -1;
802: }
1.1 deraadt 803: continue;
804: }
1.97 fgsch 805: if (showprogress)
806: start_progress_meter(curfile, stb.st_size, &statbytes);
1.161 djm 807: set_nonblock(remout);
1.1 deraadt 808: for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
809: amt = bp->cnt;
1.163 dtucker 810: if (i + (off_t)amt > stb.st_size)
1.1 deraadt 811: amt = stb.st_size - i;
812: if (!haderr) {
1.180 djm 813: if ((nr = atomicio(read, fd,
814: bp->buf, amt)) != amt) {
1.122 avsm 815: haderr = errno;
1.180 djm 816: memset(bp->buf + nr, 0, amt - nr);
817: }
1.1 deraadt 818: }
1.161 djm 819: /* Keep writing after error to retain sync */
820: if (haderr) {
821: (void)atomicio(vwrite, remout, bp->buf, amt);
1.180 djm 822: memset(bp->buf, 0, amt);
1.161 djm 823: continue;
1.1 deraadt 824: }
1.167 djm 825: if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
1.161 djm 826: &statbytes) != amt)
827: haderr = errno;
1.1 deraadt 828: }
1.161 djm 829: unset_nonblock(remout);
1.4 aaron 830:
1.127 deraadt 831: if (fd != -1) {
832: if (close(fd) < 0 && !haderr)
833: haderr = errno;
834: fd = -1;
835: }
1.1 deraadt 836: if (!haderr)
1.107 deraadt 837: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 838: else
839: run_err("%s: %s", name, strerror(haderr));
1.18 markus 840: (void) response();
1.185 ! dtucker 841: if (showprogress)
! 842: stop_progress_meter();
1.1 deraadt 843: }
844: }
845:
846: void
1.104 djm 847: rsource(char *name, struct stat *statp)
1.1 deraadt 848: {
849: DIR *dirp;
850: struct dirent *dp;
1.181 deraadt 851: char *last, *vect[1], path[PATH_MAX];
1.1 deraadt 852:
853: if (!(dirp = opendir(name))) {
854: run_err("%s: %s", name, strerror(errno));
855: return;
856: }
857: last = strrchr(name, '/');
1.183 mmcc 858: if (last == NULL)
1.1 deraadt 859: last = name;
860: else
861: last++;
862: if (pflag) {
1.176 guenther 863: if (do_times(remout, verbose_mode, statp) < 0) {
1.1 deraadt 864: closedir(dirp);
865: return;
866: }
867: }
1.55 deraadt 868: (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1.47 markus 869: (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1.18 markus 870: if (verbose_mode)
871: fprintf(stderr, "Entering directory: %s", path);
1.107 deraadt 872: (void) atomicio(vwrite, remout, path, strlen(path));
1.1 deraadt 873: if (response() < 0) {
874: closedir(dirp);
875: return;
876: }
1.63 stevesk 877: while ((dp = readdir(dirp)) != NULL) {
1.1 deraadt 878: if (dp->d_ino == 0)
879: continue;
880: if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
881: continue;
882: if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
883: run_err("%s/%s: name too long", name, dp->d_name);
884: continue;
885: }
1.55 deraadt 886: (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1.1 deraadt 887: vect[0] = path;
888: source(1, vect);
889: }
1.18 markus 890: (void) closedir(dirp);
1.107 deraadt 891: (void) atomicio(vwrite, remout, "E\n", 2);
1.18 markus 892: (void) response();
1.1 deraadt 893: }
894:
895: void
1.104 djm 896: sink(int argc, char **argv)
1.1 deraadt 897: {
898: static BUF buffer;
899: struct stat stb;
1.18 markus 900: enum {
901: YES, NO, DISPLAYED
902: } wrerr;
1.1 deraadt 903: BUF *bp;
1.122 avsm 904: off_t i;
1.124 djm 905: size_t j, count;
1.140 deraadt 906: int amt, exists, first, ofd;
907: mode_t mode, omode, mask;
1.97 fgsch 908: off_t size, statbytes;
1.176 guenther 909: unsigned long long ull;
1.36 deraadt 910: int setimes, targisdir, wrerrno = 0;
1.1 deraadt 911: char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
1.40 markus 912: struct timeval tv[2];
1.1 deraadt 913:
1.66 stevesk 914: #define atime tv[0]
915: #define mtime tv[1]
1.118 deraadt 916: #define SCREWUP(str) { why = str; goto screwup; }
1.1 deraadt 917:
918: setimes = targisdir = 0;
919: mask = umask(0);
920: if (!pflag)
1.18 markus 921: (void) umask(mask);
1.1 deraadt 922: if (argc != 1) {
923: run_err("ambiguous target");
924: exit(1);
925: }
926: targ = *argv;
927: if (targetshouldbedirectory)
928: verifydir(targ);
1.18 markus 929:
1.107 deraadt 930: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 931: if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
932: targisdir = 1;
933: for (first = 1;; first = 0) {
934: cp = buf;
1.122 avsm 935: if (atomicio(read, remin, cp, 1) != 1)
1.1 deraadt 936: return;
937: if (*cp++ == '\n')
938: SCREWUP("unexpected <newline>");
939: do {
1.30 deraadt 940: if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1 deraadt 941: SCREWUP("lost connection");
942: *cp++ = ch;
943: } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
944: *cp = 0;
1.114 markus 945: if (verbose_mode)
946: fprintf(stderr, "Sink: %s", buf);
1.1 deraadt 947:
948: if (buf[0] == '\01' || buf[0] == '\02') {
949: if (iamremote == 0)
1.107 deraadt 950: (void) atomicio(vwrite, STDERR_FILENO,
1.44 deraadt 951: buf + 1, strlen(buf + 1));
1.1 deraadt 952: if (buf[0] == '\02')
953: exit(1);
954: ++errs;
955: continue;
956: }
957: if (buf[0] == 'E') {
1.107 deraadt 958: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 959: return;
960: }
961: if (ch == '\n')
962: *--cp = 0;
963:
964: cp = buf;
965: if (*cp == 'T') {
966: setimes++;
967: cp++;
1.176 guenther 968: if (!isdigit((unsigned char)*cp))
969: SCREWUP("mtime.sec not present");
970: ull = strtoull(cp, &cp, 10);
1.66 stevesk 971: if (!cp || *cp++ != ' ')
1.1 deraadt 972: SCREWUP("mtime.sec not delimited");
1.178 djm 973: if ((time_t)ull < 0 ||
974: (unsigned long long)(time_t)ull != ull)
1.176 guenther 975: setimes = 0; /* out of range */
976: mtime.tv_sec = ull;
1.66 stevesk 977: mtime.tv_usec = strtol(cp, &cp, 10);
1.176 guenther 978: if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
979: mtime.tv_usec > 999999)
1.1 deraadt 980: SCREWUP("mtime.usec not delimited");
1.176 guenther 981: if (!isdigit((unsigned char)*cp))
982: SCREWUP("atime.sec not present");
983: ull = strtoull(cp, &cp, 10);
1.66 stevesk 984: if (!cp || *cp++ != ' ')
1.1 deraadt 985: SCREWUP("atime.sec not delimited");
1.178 djm 986: if ((time_t)ull < 0 ||
987: (unsigned long long)(time_t)ull != ull)
1.176 guenther 988: setimes = 0; /* out of range */
989: atime.tv_sec = ull;
1.66 stevesk 990: atime.tv_usec = strtol(cp, &cp, 10);
1.176 guenther 991: if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
992: atime.tv_usec > 999999)
1.1 deraadt 993: SCREWUP("atime.usec not delimited");
1.107 deraadt 994: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 995: continue;
996: }
997: if (*cp != 'C' && *cp != 'D') {
998: /*
999: * Check for the case "rcp remote:foo\* local:bar".
1000: * In this case, the line "No match." can be returned
1001: * by the shell before the rcp command on the remote is
1002: * executed so the ^Aerror_message convention isn't
1003: * followed.
1004: */
1005: if (first) {
1006: run_err("%s", cp);
1007: exit(1);
1008: }
1009: SCREWUP("expected control record");
1010: }
1011: mode = 0;
1012: for (++cp; cp < buf + 5; cp++) {
1013: if (*cp < '0' || *cp > '7')
1014: SCREWUP("bad mode");
1015: mode = (mode << 3) | (*cp - '0');
1016: }
1017: if (*cp++ != ' ')
1018: SCREWUP("mode not delimited");
1019:
1.179 deraadt 1020: for (size = 0; isdigit((unsigned char)*cp);)
1.1 deraadt 1021: size = size * 10 + (*cp++ - '0');
1022: if (*cp++ != ' ')
1023: SCREWUP("size not delimited");
1.114 markus 1024: if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
1025: run_err("error: unexpected filename: %s", cp);
1026: exit(1);
1027: }
1.1 deraadt 1028: if (targisdir) {
1029: static char *namebuf;
1.124 djm 1030: static size_t cursize;
1.1 deraadt 1031: size_t need;
1032:
1033: need = strlen(targ) + strlen(cp) + 250;
1.55 deraadt 1034: if (need > cursize) {
1.173 djm 1035: free(namebuf);
1.18 markus 1036: namebuf = xmalloc(need);
1.55 deraadt 1037: cursize = need;
1038: }
1039: (void) snprintf(namebuf, need, "%s%s%s", targ,
1.88 mouring 1040: strcmp(targ, "/") ? "/" : "", cp);
1.1 deraadt 1041: np = namebuf;
1042: } else
1043: np = targ;
1.12 aaron 1044: curfile = cp;
1.1 deraadt 1045: exists = stat(np, &stb) == 0;
1046: if (buf[0] == 'D') {
1047: int mod_flag = pflag;
1.114 markus 1048: if (!iamrecursive)
1049: SCREWUP("received directory without -r");
1.1 deraadt 1050: if (exists) {
1051: if (!S_ISDIR(stb.st_mode)) {
1052: errno = ENOTDIR;
1053: goto bad;
1054: }
1055: if (pflag)
1.18 markus 1056: (void) chmod(np, mode);
1.1 deraadt 1057: } else {
1.18 markus 1058: /* Handle copying from a read-only
1059: directory */
1.1 deraadt 1060: mod_flag = 1;
1061: if (mkdir(np, mode | S_IRWXU) < 0)
1062: goto bad;
1063: }
1.58 danh 1064: vect[0] = xstrdup(np);
1.1 deraadt 1065: sink(1, vect);
1066: if (setimes) {
1067: setimes = 0;
1.59 deraadt 1068: if (utimes(vect[0], tv) < 0)
1.18 markus 1069: run_err("%s: set times: %s",
1.59 deraadt 1070: vect[0], strerror(errno));
1.1 deraadt 1071: }
1072: if (mod_flag)
1.59 deraadt 1073: (void) chmod(vect[0], mode);
1.173 djm 1074: free(vect[0]);
1.1 deraadt 1075: continue;
1076: }
1077: omode = mode;
1.174 dtucker 1078: mode |= S_IWUSR;
1.71 markus 1079: if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.1 deraadt 1080: bad: run_err("%s: %s", np, strerror(errno));
1081: continue;
1082: }
1.107 deraadt 1083: (void) atomicio(vwrite, remout, "", 1);
1.161 djm 1084: if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
1.18 markus 1085: (void) close(ofd);
1.1 deraadt 1086: continue;
1087: }
1088: cp = bp->buf;
1089: wrerr = NO;
1.7 deraadt 1090:
1.12 aaron 1091: statbytes = 0;
1.97 fgsch 1092: if (showprogress)
1093: start_progress_meter(curfile, size, &statbytes);
1.161 djm 1094: set_nonblock(remin);
1095: for (count = i = 0; i < size; i += bp->cnt) {
1096: amt = bp->cnt;
1.1 deraadt 1097: if (i + amt > size)
1098: amt = size - i;
1099: count += amt;
1100: do {
1.167 djm 1101: j = atomicio6(read, remin, cp, amt,
1102: scpio, &statbytes);
1.122 avsm 1103: if (j == 0) {
1.161 djm 1104: run_err("%s", j != EPIPE ?
1105: strerror(errno) :
1.63 stevesk 1106: "dropped connection");
1.1 deraadt 1107: exit(1);
1108: }
1109: amt -= j;
1110: cp += j;
1111: } while (amt > 0);
1.112 djm 1112:
1.1 deraadt 1113: if (count == bp->cnt) {
1114: /* Keep reading so we stay sync'd up. */
1115: if (wrerr == NO) {
1.122 avsm 1116: if (atomicio(vwrite, ofd, bp->buf,
1117: count) != count) {
1.1 deraadt 1118: wrerr = YES;
1.122 avsm 1119: wrerrno = errno;
1.1 deraadt 1120: }
1121: }
1122: count = 0;
1123: cp = bp->buf;
1124: }
1125: }
1.161 djm 1126: unset_nonblock(remin);
1.1 deraadt 1127: if (count != 0 && wrerr == NO &&
1.122 avsm 1128: atomicio(vwrite, ofd, bp->buf, count) != count) {
1.1 deraadt 1129: wrerr = YES;
1.122 avsm 1130: wrerrno = errno;
1.1 deraadt 1131: }
1.159 djm 1132: if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1133: ftruncate(ofd, size) != 0) {
1.1 deraadt 1134: run_err("%s: truncate: %s", np, strerror(errno));
1135: wrerr = DISPLAYED;
1136: }
1137: if (pflag) {
1138: if (exists || omode != mode)
1.116 dtucker 1139: if (fchmod(ofd, omode)) {
1.1 deraadt 1140: run_err("%s: set mode: %s",
1.63 stevesk 1141: np, strerror(errno));
1.116 dtucker 1142: wrerr = DISPLAYED;
1143: }
1.1 deraadt 1144: } else {
1145: if (!exists && omode != mode)
1.116 dtucker 1146: if (fchmod(ofd, omode & ~mask)) {
1.1 deraadt 1147: run_err("%s: set mode: %s",
1.63 stevesk 1148: np, strerror(errno));
1.116 dtucker 1149: wrerr = DISPLAYED;
1150: }
1.1 deraadt 1151: }
1.33 provos 1152: if (close(ofd) == -1) {
1153: wrerr = YES;
1154: wrerrno = errno;
1155: }
1.18 markus 1156: (void) response();
1.185 ! dtucker 1157: if (showprogress)
! 1158: stop_progress_meter();
1.1 deraadt 1159: if (setimes && wrerr == NO) {
1160: setimes = 0;
1.40 markus 1161: if (utimes(np, tv) < 0) {
1.1 deraadt 1162: run_err("%s: set times: %s",
1.63 stevesk 1163: np, strerror(errno));
1.1 deraadt 1164: wrerr = DISPLAYED;
1165: }
1166: }
1.18 markus 1167: switch (wrerr) {
1.1 deraadt 1168: case YES:
1169: run_err("%s: %s", np, strerror(wrerrno));
1170: break;
1171: case NO:
1.107 deraadt 1172: (void) atomicio(vwrite, remout, "", 1);
1.1 deraadt 1173: break;
1174: case DISPLAYED:
1175: break;
1176: }
1177: }
1178: screwup:
1179: run_err("protocol error: %s", why);
1180: exit(1);
1181: }
1182:
1183: int
1.86 itojun 1184: response(void)
1.1 deraadt 1185: {
1186: char ch, *cp, resp, rbuf[2048];
1187:
1.30 deraadt 1188: if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1.1 deraadt 1189: lostconn(0);
1190:
1191: cp = rbuf;
1.18 markus 1192: switch (resp) {
1193: case 0: /* ok */
1.1 deraadt 1194: return (0);
1195: default:
1196: *cp++ = resp;
1197: /* FALLTHROUGH */
1.18 markus 1198: case 1: /* error, followed by error msg */
1199: case 2: /* fatal error, "" */
1.1 deraadt 1200: do {
1.30 deraadt 1201: if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1 deraadt 1202: lostconn(0);
1203: *cp++ = ch;
1204: } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1205:
1206: if (!iamremote)
1.107 deraadt 1207: (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1.1 deraadt 1208: ++errs;
1209: if (resp == 1)
1210: return (-1);
1211: exit(1);
1212: }
1213: /* NOTREACHED */
1214: }
1215:
1216: void
1.86 itojun 1217: usage(void)
1.1 deraadt 1218: {
1.83 stevesk 1219: (void) fprintf(stderr,
1.170 jmc 1220: "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1.110 jmc 1221: " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1.160 sobrado 1222: " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1.1 deraadt 1223: exit(1);
1224: }
1225:
1226: void
1.18 markus 1227: run_err(const char *fmt,...)
1.1 deraadt 1228: {
1229: static FILE *fp;
1230: va_list ap;
1231:
1232: ++errs;
1.136 biorn 1233: if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1234: (void) fprintf(fp, "%c", 0x01);
1235: (void) fprintf(fp, "scp: ");
1236: va_start(ap, fmt);
1237: (void) vfprintf(fp, fmt, ap);
1238: va_end(ap);
1239: (void) fprintf(fp, "\n");
1240: (void) fflush(fp);
1241: }
1.18 markus 1242:
1243: if (!iamremote) {
1.73 markus 1244: va_start(ap, fmt);
1.18 markus 1245: vfprintf(stderr, fmt, ap);
1.73 markus 1246: va_end(ap);
1.18 markus 1247: fprintf(stderr, "\n");
1248: }
1.1 deraadt 1249: }
1250:
1251: void
1.104 djm 1252: verifydir(char *cp)
1.1 deraadt 1253: {
1254: struct stat stb;
1255:
1256: if (!stat(cp, &stb)) {
1257: if (S_ISDIR(stb.st_mode))
1258: return;
1259: errno = ENOTDIR;
1260: }
1261: run_err("%s: %s", cp, strerror(errno));
1.123 avsm 1262: killchild(0);
1.1 deraadt 1263: }
1264:
1265: int
1.104 djm 1266: okname(char *cp0)
1.1 deraadt 1267: {
1268: int c;
1269: char *cp;
1270:
1271: cp = cp0;
1272: do {
1.75 deraadt 1273: c = (int)*cp;
1.1 deraadt 1274: if (c & 0200)
1275: goto bad;
1.179 deraadt 1276: if (!isalpha(c) && !isdigit((unsigned char)c)) {
1.101 markus 1277: switch (c) {
1278: case '\'':
1279: case '"':
1280: case '`':
1281: case ' ':
1282: case '#':
1283: goto bad;
1284: default:
1285: break;
1286: }
1287: }
1.1 deraadt 1288: } while (*++cp);
1289: return (1);
1290:
1.25 markus 1291: bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1.1 deraadt 1292: return (0);
1293: }
1294:
1295: BUF *
1.104 djm 1296: allocbuf(BUF *bp, int fd, int blksize)
1.1 deraadt 1297: {
1298: size_t size;
1299: struct stat stb;
1300:
1301: if (fstat(fd, &stb) < 0) {
1302: run_err("fstat: %s", strerror(errno));
1303: return (0);
1304: }
1.95 markus 1305: size = roundup(stb.st_blksize, blksize);
1306: if (size == 0)
1.18 markus 1307: size = blksize;
1.1 deraadt 1308: if (bp->cnt >= size)
1309: return (bp);
1.18 markus 1310: if (bp->buf == NULL)
1311: bp->buf = xmalloc(size);
1312: else
1.182 deraadt 1313: bp->buf = xreallocarray(bp->buf, 1, size);
1.81 markus 1314: memset(bp->buf, 0, size);
1.1 deraadt 1315: bp->cnt = size;
1316: return (bp);
1317: }
1318:
1319: void
1.104 djm 1320: lostconn(int signo)
1.1 deraadt 1321: {
1322: if (!iamremote)
1.172 dtucker 1323: (void)write(STDERR_FILENO, "lost connection\n", 16);
1.74 markus 1324: if (signo)
1325: _exit(1);
1326: else
1327: exit(1);
1.4 aaron 1328: }