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