X-Git-Url: http://andersk.mit.edu/gitweb/gssapi-openssh.git/blobdiff_plain/3f750d995dfe697ae3db8045685491a49b69effb..3ea093c40e14de71d426149c8561e0ad084db09e:/openssh/scp.c diff --git a/openssh/scp.c b/openssh/scp.c index 92cab27..d6005f8 100644 --- a/openssh/scp.c +++ b/openssh/scp.c @@ -1,3 +1,4 @@ +/* $OpenBSD: scp.c,v 1.155 2006/08/03 03:34:42 deraadt Exp $ */ /* * scp - secure remote copy. This is basically patched BSD rcp which * uses ssh to do the data transfer (instead of using rcmd). @@ -71,7 +72,30 @@ */ #include "includes.h" -RCSID("$OpenBSD: scp.c,v 1.130 2006/01/31 10:35:43 djm Exp $"); + +#include +#include +#ifdef HAVE_SYS_STAT_H +# include +#endif +#ifdef HAVE_SYS_TIME_H +# include +#endif +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "xmalloc.h" #include "atomicio.h" @@ -82,6 +106,8 @@ RCSID("$OpenBSD: scp.c,v 1.130 2006/01/31 10:35:43 djm Exp $"); extern char *__progname; +int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout); + void bwlimit(int); /* Struct for addargs */ @@ -167,7 +193,7 @@ do_local_cmd(arglist *a) */ int -do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) +do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) { int pin[2], pout[2], reserved[2]; @@ -181,7 +207,8 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) * Reserve two descriptors so that the real pipes won't get * descriptors 0 and 1 because that will screw up dup2 below. */ - pipe(reserved); + if (pipe(reserved) < 0) + fatal("pipe: %s", strerror(errno)); /* Create a socket pair for communicating with ssh. */ if (pipe(pin) < 0) @@ -234,7 +261,6 @@ typedef struct { BUF *allocbuf(BUF *, int, int); void lostconn(int); -void nospace(void); int okname(char *); void run_err(const char *,...); void verifydir(char *); @@ -258,15 +284,21 @@ void usage(void); int main(int argc, char **argv) { - int ch, fflag, tflag, status; + int ch, fflag, tflag, status, n; double speed; - char *targ, *endp; + char *targ, *endp, **newargv; extern char *optarg; extern int optind; /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ sanitise_stdfd(); + /* Copy argv, because we modify it */ + newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv)); + for (n = 0; n < argc; n++) + newargv[n] = xstrdup(argv[n]); + argv = newargv; + __progname = ssh_get_progname(argv[0]); memset(&args, '\0', sizeof(args)); @@ -278,7 +310,7 @@ main(int argc, char **argv) addargs(&args, "-oClearAllForwardings yes"); fflag = tflag = 0; - while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246zS:o:F:R:")) != -1) + while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1) switch (ch) { /* User-visible flags. */ case '1': @@ -286,9 +318,8 @@ main(int argc, char **argv) case '4': case '6': case 'C': - case 'z': - addargs(&args, "-%c", ch); - break; + addargs(&args, "-%c", ch); + break; case 'o': case 'c': case 'i': @@ -340,9 +371,6 @@ main(int argc, char **argv) setmode(0, O_BINARY); #endif break; - case 'R': - addargs(&args, "-r%s", optarg); - break; default: usage(); } @@ -413,9 +441,9 @@ main(int argc, char **argv) void toremote(char *targ, int argc, char **argv) { - int i, len; char *bp, *host, *src, *suser, *thost, *tuser, *arg; arglist alist; + int i; memset(&alist, '\0', sizeof(alist)); alist.list = NULL; @@ -480,12 +508,10 @@ toremote(char *targ, int argc, char **argv) errs = 1; } else { /* local to remote */ if (remin == -1) { - len = strlen(targ) + CMDNEEDS + 20; - bp = xmalloc(len); - (void) snprintf(bp, len, "%s -t %s", cmd, targ); + xasprintf(&bp, "%s -t %s", cmd, targ); host = cleanhostname(thost); if (do_cmd(host, tuser, bp, &remin, - &remout, argc) < 0) + &remout) < 0) exit(1); if (response() < 0) exit(1); @@ -494,14 +520,15 @@ toremote(char *targ, int argc, char **argv) source(1, argv + i); } } + xfree(arg); } void tolocal(int argc, char **argv) { - int i, len; char *bp, *host, *src, *suser; arglist alist; + int i; memset(&alist, '\0', sizeof(alist)); alist.list = NULL; @@ -533,10 +560,8 @@ tolocal(int argc, char **argv) suser = pwd->pw_name; } host = cleanhostname(host); - len = strlen(src) + CMDNEEDS + 20; - bp = xmalloc(len); - (void) snprintf(bp, len, "%s -f %s", cmd, src); - if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) { + xasprintf(&bp, "%s -f %s", cmd, src); + if (do_cmd(host, suser, bp, &remin, &remout) < 0) { (void) xfree(bp); ++errs; continue; @@ -617,10 +642,6 @@ syserr: run_err("%s: %s", name, strerror(errno)); (void) atomicio(vwrite, remout, buf, strlen(buf)); if (response() < 0) goto next; - /* this change decreases the number of read/write syscalls*/ - /* when scp acts as data source. this is the critical change*/ - /* buf can actually remain at 2k but increasing both to 16k*/ - /* seemed to make sense*/ if ((bp = allocbuf(&buffer, fd, sizeof(buf))) == NULL) { next: if (fd != -1) { (void) close(fd); @@ -785,7 +806,8 @@ sink(int argc, char **argv) BUF *bp; off_t i; size_t j, count; - int amt, exists, first, mask, mode, ofd, omode; + int amt, exists, first, ofd; + mode_t mode, omode, mask; off_t size, statbytes; int setimes, targisdir, wrerrno = 0; char ch, *cp, *np, *targ, *why, *vect[1], buf[16384]; @@ -1093,7 +1115,7 @@ usage(void) { (void) fprintf(stderr, "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n" - " [-l limit] [-o ssh_option] [-P port] [-R Receive buffer size (Kb)] [-S program]\n" + " [-l limit] [-o ssh_option] [-P port] [-S program]\n" " [[user@]host1:]file1 [...] [[user@]host2:]file2\n"); exit(1); } @@ -1105,15 +1127,15 @@ run_err(const char *fmt,...) va_list ap; ++errs; - if (fp == NULL && !(fp = fdopen(remout, "w"))) - return; - (void) fprintf(fp, "%c", 0x01); - (void) fprintf(fp, "scp: "); - va_start(ap, fmt); - (void) vfprintf(fp, fmt, ap); - va_end(ap); - (void) fprintf(fp, "\n"); - (void) fflush(fp); + if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) { + (void) fprintf(fp, "%c", 0x01); + (void) fprintf(fp, "scp: "); + va_start(ap, fmt); + (void) vfprintf(fp, fmt, ap); + va_end(ap); + (void) fprintf(fp, "\n"); + (void) fflush(fp); + } if (!iamremote) { va_start(ap, fmt); @@ -1189,7 +1211,7 @@ allocbuf(BUF *bp, int fd, int blksize) if (bp->buf == NULL) bp->buf = xmalloc(size); else - bp->buf = xrealloc(bp->buf, size); + bp->buf = xrealloc(bp->buf, 1, size); memset(bp->buf, 0, size); bp->cnt = size; return (bp);