121739Sdist /* 262010Sbostic * Copyright (c) 1985, 1989, 1993 362010Sbostic * The Regents of the University of California. All rights reserved. 433737Sbostic * 542665Sbostic * %sccs.include.redist.c% 621739Sdist */ 721739Sdist 810296Ssam #ifndef lint 9*66670Spendry static char sccsid[] = "@(#)ftp.c 8.2 (Berkeley) 04/02/94"; 1033737Sbostic #endif /* not lint */ 1110296Ssam 1236940Skarels #include <sys/param.h> 1310296Ssam #include <sys/stat.h> 1410296Ssam #include <sys/ioctl.h> 1510296Ssam #include <sys/socket.h> 1613614Ssam #include <sys/time.h> 1736935Skarels #include <sys/file.h> 1810296Ssam 1910296Ssam #include <netinet/in.h> 2044340Skarels #include <netinet/in_systm.h> 2144340Skarels #include <netinet/ip.h> 22*66670Spendry #include <arpa/inet.h> 2312397Ssam #include <arpa/ftp.h> 2426048Sminshall #include <arpa/telnet.h> 2510296Ssam 26*66670Spendry #include <ctype.h> 27*66670Spendry #include <err.h> 28*66670Spendry #include <errno.h> 29*66670Spendry #include <fcntl.h> 3010296Ssam #include <netdb.h> 3126048Sminshall #include <pwd.h> 32*66670Spendry #include <signal.h> 33*66670Spendry #include <stdio.h> 3454192Sbostic #include <stdlib.h> 3554192Sbostic #include <string.h> 36*66670Spendry #include <unistd.h> 37*66670Spendry #include <varargs.h> 3810296Ssam 3936940Skarels #include "ftp_var.h" 4036940Skarels 41*66670Spendry extern int h_errno; 42*66670Spendry 4310296Ssam struct sockaddr_in hisctladdr; 4410296Ssam struct sockaddr_in data_addr; 4510296Ssam int data = -1; 4626048Sminshall int abrtflag = 0; 47*66670Spendry jmp_buf ptabort; 48*66670Spendry int ptabflg; 4926048Sminshall int ptflag = 0; 5010296Ssam struct sockaddr_in myctladdr; 5137225Skarels off_t restart_point = 0; 5210296Ssam 5338202Srick 5410296Ssam FILE *cin, *cout; 5510296Ssam 5625904Skarels char * 5710296Ssam hookup(host, port) 5810296Ssam char *host; 5910296Ssam int port; 6010296Ssam { 61*66670Spendry struct hostent *hp = 0; 6244340Skarels int s, len, tos; 6325904Skarels static char hostnamebuf[80]; 6410296Ssam 65*66670Spendry memset((char *)&hisctladdr, 0, sizeof (hisctladdr)); 6625904Skarels hisctladdr.sin_addr.s_addr = inet_addr(host); 6725904Skarels if (hisctladdr.sin_addr.s_addr != -1) { 6825904Skarels hisctladdr.sin_family = AF_INET; 6936940Skarels (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf)); 7036940Skarels } else { 7125100Sbloom hp = gethostbyname(host); 7225904Skarels if (hp == NULL) { 73*66670Spendry warnx("%s: %s", host, hstrerror(h_errno)); 7426048Sminshall code = -1; 75*66670Spendry return ((char *) 0); 7625904Skarels } 7725904Skarels hisctladdr.sin_family = hp->h_addrtype; 78*66670Spendry memmove((caddr_t)&hisctladdr.sin_addr, 79*66670Spendry hp->h_addr_list[0], hp->h_length); 8036940Skarels (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf)); 8110296Ssam } 8225904Skarels hostname = hostnamebuf; 8325904Skarels s = socket(hisctladdr.sin_family, SOCK_STREAM, 0); 8410296Ssam if (s < 0) { 85*66670Spendry warn("socket"); 8626048Sminshall code = -1; 8710296Ssam return (0); 8810296Ssam } 8910296Ssam hisctladdr.sin_port = port; 9038133Srick while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) { 9125904Skarels if (hp && hp->h_addr_list[1]) { 9225904Skarels int oerrno = errno; 93*66670Spendry char *ia; 9425904Skarels 95*66670Spendry ia = inet_ntoa(hisctladdr.sin_addr); 9625904Skarels errno = oerrno; 97*66670Spendry warn("connect to address %s", ia); 9825904Skarels hp->h_addr_list++; 99*66670Spendry memmove((caddr_t)&hisctladdr.sin_addr, 100*66670Spendry hp->h_addr_list[0], hp->h_length); 10126496Sminshall fprintf(stdout, "Trying %s...\n", 10225904Skarels inet_ntoa(hisctladdr.sin_addr)); 10326813Skarels (void) close(s); 10426813Skarels s = socket(hisctladdr.sin_family, SOCK_STREAM, 0); 10526813Skarels if (s < 0) { 106*66670Spendry warn("socket"); 10726813Skarels code = -1; 10826813Skarels return (0); 10926813Skarels } 11025904Skarels continue; 11125904Skarels } 112*66670Spendry warn("connect"); 11326048Sminshall code = -1; 11410296Ssam goto bad; 11510296Ssam } 11611627Ssam len = sizeof (myctladdr); 11738133Srick if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) { 118*66670Spendry warn("getsockname"); 11926048Sminshall code = -1; 12010296Ssam goto bad; 12110296Ssam } 12244340Skarels #ifdef IP_TOS 12344340Skarels tos = IPTOS_LOWDELAY; 12444340Skarels if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 125*66670Spendry warn("setsockopt TOS (ignored)"); 12644340Skarels #endif 12710296Ssam cin = fdopen(s, "r"); 12810296Ssam cout = fdopen(s, "w"); 12911219Ssam if (cin == NULL || cout == NULL) { 130*66670Spendry warnx("fdopen failed."); 13110296Ssam if (cin) 13226496Sminshall (void) fclose(cin); 13310296Ssam if (cout) 13426496Sminshall (void) fclose(cout); 13526048Sminshall code = -1; 13610296Ssam goto bad; 13710296Ssam } 13810296Ssam if (verbose) 13926067Sminshall printf("Connected to %s.\n", hostname); 14027687Sminshall if (getreply(0) > 2) { /* read startup message from server */ 14126048Sminshall if (cin) 14226496Sminshall (void) fclose(cin); 14326048Sminshall if (cout) 14426496Sminshall (void) fclose(cout); 14526048Sminshall code = -1; 14626048Sminshall goto bad; 14726048Sminshall } 14827687Sminshall #ifdef SO_OOBINLINE 14927687Sminshall { 15027687Sminshall int on = 1; 15126048Sminshall 15240193Sbostic if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)) 15327687Sminshall < 0 && debug) { 154*66670Spendry warn("setsockopt"); 15527687Sminshall } 15627687Sminshall } 15738133Srick #endif /* SO_OOBINLINE */ 15826048Sminshall 15925904Skarels return (hostname); 16010296Ssam bad: 16126496Sminshall (void) close(s); 16225904Skarels return ((char *)0); 16310296Ssam } 16410296Ssam 165*66670Spendry int 16625904Skarels login(host) 16725904Skarels char *host; 16810296Ssam { 16926048Sminshall char tmp[80]; 170*66670Spendry char *user, *pass, *acct; 17126048Sminshall int n, aflag = 0; 17210296Ssam 17326048Sminshall user = pass = acct = 0; 17426048Sminshall if (ruserpass(host, &user, &pass, &acct) < 0) { 17526048Sminshall code = -1; 176*66670Spendry return (0); 17726048Sminshall } 17837458Skarels while (user == NULL) { 17926048Sminshall char *myname = getlogin(); 18026048Sminshall 18126048Sminshall if (myname == NULL) { 18226048Sminshall struct passwd *pp = getpwuid(getuid()); 18326048Sminshall 18426448Slepreau if (pp != NULL) 18526048Sminshall myname = pp->pw_name; 18626048Sminshall } 18737458Skarels if (myname) 18837458Skarels printf("Name (%s:%s): ", host, myname); 18937458Skarels else 19037458Skarels printf("Name (%s): ", host); 19126048Sminshall (void) fgets(tmp, sizeof(tmp) - 1, stdin); 19226048Sminshall tmp[strlen(tmp) - 1] = '\0'; 19326448Slepreau if (*tmp == '\0') 19426048Sminshall user = myname; 19526448Slepreau else 19626048Sminshall user = tmp; 19726048Sminshall } 19810296Ssam n = command("USER %s", user); 19926048Sminshall if (n == CONTINUE) { 20026448Slepreau if (pass == NULL) 20135659Sbostic pass = getpass("Password:"); 20210296Ssam n = command("PASS %s", pass); 20326048Sminshall } 20410296Ssam if (n == CONTINUE) { 20526048Sminshall aflag++; 20635659Sbostic acct = getpass("Account:"); 20710296Ssam n = command("ACCT %s", acct); 20810296Ssam } 20910296Ssam if (n != COMPLETE) { 210*66670Spendry warnx("Login failed."); 21110296Ssam return (0); 21210296Ssam } 21326448Slepreau if (!aflag && acct != NULL) 21426048Sminshall (void) command("ACCT %s", acct); 21526448Slepreau if (proxy) 216*66670Spendry return (1); 21726048Sminshall for (n = 0; n < macnum; ++n) { 21826048Sminshall if (!strcmp("init", macros[n].mac_name)) { 21926496Sminshall (void) strcpy(line, "$init"); 22026048Sminshall makeargv(); 22126048Sminshall domacro(margc, margv); 22226048Sminshall break; 22326048Sminshall } 22426048Sminshall } 22510296Ssam return (1); 22610296Ssam } 22710296Ssam 22840193Sbostic void 22926048Sminshall cmdabort() 23026048Sminshall { 23126048Sminshall 23226048Sminshall printf("\n"); 23326048Sminshall (void) fflush(stdout); 23426048Sminshall abrtflag++; 23526448Slepreau if (ptflag) 23626048Sminshall longjmp(ptabort,1); 23726048Sminshall } 23826048Sminshall 23938133Srick /*VARARGS*/ 240*66670Spendry int 24138133Srick command(va_alist) 24238133Srick va_dcl 24338133Srick { 24438133Srick va_list ap; 24510296Ssam char *fmt; 24638133Srick int r; 24740193Sbostic sig_t oldintr; 24810296Ssam 24926048Sminshall abrtflag = 0; 25010296Ssam if (debug) { 25110296Ssam printf("---> "); 25238133Srick va_start(ap); 25338133Srick fmt = va_arg(ap, char *); 25438133Srick if (strncmp("PASS ", fmt, 5) == 0) 25538133Srick printf("PASS XXXX"); 25638133Srick else 25738133Srick vfprintf(stdout, fmt, ap); 25838133Srick va_end(ap); 25910296Ssam printf("\n"); 26010296Ssam (void) fflush(stdout); 26110296Ssam } 26211219Ssam if (cout == NULL) { 263*66670Spendry warn("No control connection for command"); 26426048Sminshall code = -1; 26511219Ssam return (0); 26611219Ssam } 26738133Srick oldintr = signal(SIGINT, cmdabort); 26838133Srick va_start(ap); 26938133Srick fmt = va_arg(ap, char *); 27038133Srick vfprintf(cout, fmt, ap); 27138133Srick va_end(ap); 27210296Ssam fprintf(cout, "\r\n"); 27310296Ssam (void) fflush(cout); 27426048Sminshall cpend = 1; 27526048Sminshall r = getreply(!strcmp(fmt, "QUIT")); 27626448Slepreau if (abrtflag && oldintr != SIG_IGN) 27746828Sbostic (*oldintr)(SIGINT); 27826048Sminshall (void) signal(SIGINT, oldintr); 279*66670Spendry return (r); 28010296Ssam } 28110296Ssam 28237229Skarels char reply_string[BUFSIZ]; /* last line of previous reply */ 28336935Skarels 284*66670Spendry int 28510296Ssam getreply(expecteof) 28610296Ssam int expecteof; 28710296Ssam { 288*66670Spendry int c, n; 289*66670Spendry int dig; 29038133Srick int originalcode = 0, continuation = 0; 29140193Sbostic sig_t oldintr; 29226048Sminshall int pflag = 0; 293*66670Spendry char *cp, *pt = pasv; 29410296Ssam 29538133Srick oldintr = signal(SIGINT, cmdabort); 29610296Ssam for (;;) { 29710296Ssam dig = n = code = 0; 29837229Skarels cp = reply_string; 29910296Ssam while ((c = getc(cin)) != '\n') { 30027687Sminshall if (c == IAC) { /* handle telnet commands */ 30127687Sminshall switch (c = getc(cin)) { 30227687Sminshall case WILL: 30327687Sminshall case WONT: 30427687Sminshall c = getc(cin); 30538133Srick fprintf(cout, "%c%c%c", IAC, DONT, c); 30627687Sminshall (void) fflush(cout); 30727687Sminshall break; 30827687Sminshall case DO: 30927687Sminshall case DONT: 31027687Sminshall c = getc(cin); 31138133Srick fprintf(cout, "%c%c%c", IAC, WONT, c); 31227687Sminshall (void) fflush(cout); 31327687Sminshall break; 31427687Sminshall default: 31527687Sminshall break; 31627687Sminshall } 31727687Sminshall continue; 31827687Sminshall } 31910296Ssam dig++; 32010296Ssam if (c == EOF) { 32126048Sminshall if (expecteof) { 32226048Sminshall (void) signal(SIGINT,oldintr); 32326048Sminshall code = 221; 32410296Ssam return (0); 32526048Sminshall } 32610296Ssam lostpeer(); 32726048Sminshall if (verbose) { 32826048Sminshall printf("421 Service not available, remote server has closed connection\n"); 32926048Sminshall (void) fflush(stdout); 33026048Sminshall } 33133772Scsvsj code = 421; 332*66670Spendry return (4); 33310296Ssam } 33426048Sminshall if (c != '\r' && (verbose > 0 || 33526048Sminshall (verbose > -1 && n == '5' && dig > 4))) { 33626448Slepreau if (proxflag && 33726448Slepreau (dig == 1 || dig == 5 && verbose == 0)) 33826048Sminshall printf("%s:",hostname); 33926496Sminshall (void) putchar(c); 34026048Sminshall } 34110296Ssam if (dig < 4 && isdigit(c)) 34210296Ssam code = code * 10 + (c - '0'); 34326448Slepreau if (!pflag && code == 227) 34426048Sminshall pflag = 1; 34526448Slepreau if (dig > 4 && pflag == 1 && isdigit(c)) 34626048Sminshall pflag = 2; 34726048Sminshall if (pflag == 2) { 34826448Slepreau if (c != '\r' && c != ')') 34926048Sminshall *pt++ = c; 35026048Sminshall else { 35126048Sminshall *pt = '\0'; 35226048Sminshall pflag = 3; 35326048Sminshall } 35426048Sminshall } 35526048Sminshall if (dig == 4 && c == '-') { 35626448Slepreau if (continuation) 35726048Sminshall code = 0; 35810296Ssam continuation++; 35926048Sminshall } 36010296Ssam if (n == 0) 36110296Ssam n = c; 36237229Skarels if (cp < &reply_string[sizeof(reply_string) - 1]) 36337229Skarels *cp++ = c; 36410296Ssam } 36526048Sminshall if (verbose > 0 || verbose > -1 && n == '5') { 36626496Sminshall (void) putchar(c); 36711346Ssam (void) fflush (stdout); 36811346Ssam } 36910296Ssam if (continuation && code != originalcode) { 37010296Ssam if (originalcode == 0) 37110296Ssam originalcode = code; 37210296Ssam continue; 37310296Ssam } 37436935Skarels *cp = '\0'; 37526448Slepreau if (n != '1') 37626048Sminshall cpend = 0; 37726048Sminshall (void) signal(SIGINT,oldintr); 37826448Slepreau if (code == 421 || originalcode == 421) 37926048Sminshall lostpeer(); 38026448Slepreau if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN) 38146828Sbostic (*oldintr)(SIGINT); 38225907Smckusick return (n - '0'); 38310296Ssam } 38410296Ssam } 38510296Ssam 386*66670Spendry int 38726048Sminshall empty(mask, sec) 38827687Sminshall struct fd_set *mask; 38926048Sminshall int sec; 39026048Sminshall { 39126048Sminshall struct timeval t; 39226048Sminshall 39326048Sminshall t.tv_sec = (long) sec; 39426048Sminshall t.tv_usec = 0; 395*66670Spendry return (select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t)); 39626048Sminshall } 39726048Sminshall 39810296Ssam jmp_buf sendabort; 39910296Ssam 40040193Sbostic void 40110296Ssam abortsend() 40210296Ssam { 40310296Ssam 40426048Sminshall mflag = 0; 40526048Sminshall abrtflag = 0; 40638133Srick printf("\nsend aborted\nwaiting for remote to finish abort\n"); 40726048Sminshall (void) fflush(stdout); 40810296Ssam longjmp(sendabort, 1); 40910296Ssam } 41010296Ssam 41136940Skarels #define HASHBYTES 1024 41236940Skarels 413*66670Spendry void 41437225Skarels sendrequest(cmd, local, remote, printnames) 41510296Ssam char *cmd, *local, *remote; 41637225Skarels int printnames; 41710296Ssam { 41840193Sbostic struct stat st; 41940193Sbostic struct timeval start, stop; 420*66670Spendry int c, d; 42135659Sbostic FILE *fin, *dout = 0, *popen(); 422*66670Spendry int (*closefunc) __P((FILE *)); 42340193Sbostic sig_t oldintr, oldintp; 42436940Skarels long bytes = 0, hashbytes = HASHBYTES; 42540193Sbostic char *lmode, buf[BUFSIZ], *bufp; 42610296Ssam 42737225Skarels if (verbose && printnames) { 42837225Skarels if (local && *local != '-') 42937225Skarels printf("local: %s ", local); 43037225Skarels if (remote) 43137225Skarels printf("remote: %s\n", remote); 43237225Skarels } 43326048Sminshall if (proxy) { 43426048Sminshall proxtrans(cmd, local, remote); 43526048Sminshall return; 43626048Sminshall } 43738033Skarels if (curtype != type) 43838033Skarels changetype(type, 0); 43910296Ssam closefunc = NULL; 44026048Sminshall oldintr = NULL; 44126048Sminshall oldintp = NULL; 44240193Sbostic lmode = "w"; 44326048Sminshall if (setjmp(sendabort)) { 44426048Sminshall while (cpend) { 44526048Sminshall (void) getreply(0); 44626048Sminshall } 44726048Sminshall if (data >= 0) { 44826048Sminshall (void) close(data); 44926048Sminshall data = -1; 45026048Sminshall } 45126448Slepreau if (oldintr) 45226048Sminshall (void) signal(SIGINT,oldintr); 45326448Slepreau if (oldintp) 45426048Sminshall (void) signal(SIGPIPE,oldintp); 45526048Sminshall code = -1; 45626048Sminshall return; 45726048Sminshall } 45810296Ssam oldintr = signal(SIGINT, abortsend); 45910296Ssam if (strcmp(local, "-") == 0) 46010296Ssam fin = stdin; 46110296Ssam else if (*local == '|') { 46226048Sminshall oldintp = signal(SIGPIPE,SIG_IGN); 46335659Sbostic fin = popen(local + 1, "r"); 46410296Ssam if (fin == NULL) { 465*66670Spendry warn("%s", local + 1); 46626048Sminshall (void) signal(SIGINT, oldintr); 46726048Sminshall (void) signal(SIGPIPE, oldintp); 46826048Sminshall code = -1; 46926048Sminshall return; 47010296Ssam } 47135659Sbostic closefunc = pclose; 47210296Ssam } else { 47310296Ssam fin = fopen(local, "r"); 47410296Ssam if (fin == NULL) { 475*66670Spendry warn("local: %s", local); 47626048Sminshall (void) signal(SIGINT, oldintr); 47726048Sminshall code = -1; 47826048Sminshall return; 47910296Ssam } 48010296Ssam closefunc = fclose; 48110296Ssam if (fstat(fileno(fin), &st) < 0 || 48210296Ssam (st.st_mode&S_IFMT) != S_IFREG) { 48326496Sminshall fprintf(stdout, "%s: not a plain file.\n", local); 48426048Sminshall (void) signal(SIGINT, oldintr); 48536935Skarels fclose(fin); 48626048Sminshall code = -1; 48726048Sminshall return; 48810296Ssam } 48910296Ssam } 49026048Sminshall if (initconn()) { 49126048Sminshall (void) signal(SIGINT, oldintr); 49226448Slepreau if (oldintp) 49326048Sminshall (void) signal(SIGPIPE, oldintp); 49426048Sminshall code = -1; 49536935Skarels if (closefunc != NULL) 49636935Skarels (*closefunc)(fin); 49726048Sminshall return; 49826048Sminshall } 49926448Slepreau if (setjmp(sendabort)) 50026048Sminshall goto abort; 50136935Skarels 50237225Skarels if (restart_point && 50337225Skarels (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) { 50437225Skarels if (fseek(fin, (long) restart_point, 0) < 0) { 505*66670Spendry warn("local: %s", local); 50637225Skarels restart_point = 0; 50737225Skarels if (closefunc != NULL) 50837225Skarels (*closefunc)(fin); 50937225Skarels return; 51037225Skarels } 51137225Skarels if (command("REST %ld", (long) restart_point) 51237225Skarels != CONTINUE) { 51337225Skarels restart_point = 0; 51437225Skarels if (closefunc != NULL) 51537225Skarels (*closefunc)(fin); 51637225Skarels return; 51737225Skarels } 51837225Skarels restart_point = 0; 51940193Sbostic lmode = "r+w"; 52037225Skarels } 52110296Ssam if (remote) { 52226048Sminshall if (command("%s %s", cmd, remote) != PRELIM) { 52326048Sminshall (void) signal(SIGINT, oldintr); 52426448Slepreau if (oldintp) 52526048Sminshall (void) signal(SIGPIPE, oldintp); 52636935Skarels if (closefunc != NULL) 52736935Skarels (*closefunc)(fin); 52826048Sminshall return; 52926048Sminshall } 53010296Ssam } else 53126048Sminshall if (command("%s", cmd) != PRELIM) { 53226048Sminshall (void) signal(SIGINT, oldintr); 53326448Slepreau if (oldintp) 53426048Sminshall (void) signal(SIGPIPE, oldintp); 53536935Skarels if (closefunc != NULL) 53636935Skarels (*closefunc)(fin); 53726048Sminshall return; 53826048Sminshall } 53940193Sbostic dout = dataconn(lmode); 54026448Slepreau if (dout == NULL) 54126048Sminshall goto abort; 54226496Sminshall (void) gettimeofday(&start, (struct timezone *)0); 54336935Skarels oldintp = signal(SIGPIPE, SIG_IGN); 54442278Skarels switch (curtype) { 54511219Ssam 54611219Ssam case TYPE_I: 54711219Ssam case TYPE_L: 54811346Ssam errno = d = 0; 54936942Skarels while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) { 55011219Ssam bytes += c; 55136942Skarels for (bufp = buf; c > 0; c -= d, bufp += d) 55236942Skarels if ((d = write(fileno(dout), bufp, c)) <= 0) 55336942Skarels break; 55411651Ssam if (hash) { 55536940Skarels while (bytes >= hashbytes) { 55636940Skarels (void) putchar('#'); 55736940Skarels hashbytes += HASHBYTES; 55836940Skarels } 55926496Sminshall (void) fflush(stdout); 56011651Ssam } 56111219Ssam } 56213213Ssam if (hash && bytes > 0) { 56336940Skarels if (bytes < HASHBYTES) 56436940Skarels (void) putchar('#'); 56526496Sminshall (void) putchar('\n'); 56626496Sminshall (void) fflush(stdout); 56711651Ssam } 56811219Ssam if (c < 0) 569*66670Spendry warn("local: %s", local); 57048507Srick if (d < 0) { 57148507Srick if (errno != EPIPE) 572*66670Spendry warn("netout"); 57336935Skarels bytes = -1; 57436935Skarels } 57511219Ssam break; 57611219Ssam 57711219Ssam case TYPE_A: 57811219Ssam while ((c = getc(fin)) != EOF) { 57911219Ssam if (c == '\n') { 58011651Ssam while (hash && (bytes >= hashbytes)) { 58126496Sminshall (void) putchar('#'); 58226496Sminshall (void) fflush(stdout); 58336940Skarels hashbytes += HASHBYTES; 58411651Ssam } 58511219Ssam if (ferror(dout)) 58611219Ssam break; 58726496Sminshall (void) putc('\r', dout); 58811219Ssam bytes++; 58911219Ssam } 59026496Sminshall (void) putc(c, dout); 59111219Ssam bytes++; 59226048Sminshall /* if (c == '\r') { */ 593*66670Spendry /* (void) putc('\0', dout); // this violates rfc */ 59426048Sminshall /* bytes++; */ 59526048Sminshall /* } */ 59611219Ssam } 59711651Ssam if (hash) { 59813213Ssam if (bytes < hashbytes) 59926496Sminshall (void) putchar('#'); 60026496Sminshall (void) putchar('\n'); 60126496Sminshall (void) fflush(stdout); 60211651Ssam } 60311219Ssam if (ferror(fin)) 604*66670Spendry warn("local: %s", local); 60536935Skarels if (ferror(dout)) { 60636935Skarels if (errno != EPIPE) 607*66670Spendry warn("netout"); 60836935Skarels bytes = -1; 60936935Skarels } 61011219Ssam break; 61110296Ssam } 61226496Sminshall (void) gettimeofday(&stop, (struct timezone *)0); 61310296Ssam if (closefunc != NULL) 61426048Sminshall (*closefunc)(fin); 61510296Ssam (void) fclose(dout); 61626048Sminshall (void) getreply(0); 61726048Sminshall (void) signal(SIGINT, oldintr); 61836935Skarels if (oldintp) 61936935Skarels (void) signal(SIGPIPE, oldintp); 62035699Sbostic if (bytes > 0) 62137225Skarels ptransfer("sent", bytes, &start, &stop); 62210296Ssam return; 62326048Sminshall abort: 62426496Sminshall (void) gettimeofday(&stop, (struct timezone *)0); 62526048Sminshall (void) signal(SIGINT, oldintr); 62626448Slepreau if (oldintp) 62726048Sminshall (void) signal(SIGPIPE, oldintp); 62826048Sminshall if (!cpend) { 62926048Sminshall code = -1; 63026048Sminshall return; 63126048Sminshall } 63226048Sminshall if (data >= 0) { 63326048Sminshall (void) close(data); 63426048Sminshall data = -1; 63526048Sminshall } 63626448Slepreau if (dout) 63726048Sminshall (void) fclose(dout); 63826048Sminshall (void) getreply(0); 63926048Sminshall code = -1; 64010296Ssam if (closefunc != NULL && fin != NULL) 64126048Sminshall (*closefunc)(fin); 64235699Sbostic if (bytes > 0) 64337225Skarels ptransfer("sent", bytes, &start, &stop); 64410296Ssam } 64510296Ssam 64610296Ssam jmp_buf recvabort; 64710296Ssam 64840193Sbostic void 64910296Ssam abortrecv() 65010296Ssam { 65110296Ssam 65226048Sminshall mflag = 0; 65326048Sminshall abrtflag = 0; 65438133Srick printf("\nreceive aborted\nwaiting for remote to finish abort\n"); 65526048Sminshall (void) fflush(stdout); 65610296Ssam longjmp(recvabort, 1); 65710296Ssam } 65810296Ssam 659*66670Spendry void 66040193Sbostic recvrequest(cmd, local, remote, lmode, printnames) 66140193Sbostic char *cmd, *local, *remote, *lmode; 662*66670Spendry int printnames; 66310296Ssam { 664*66670Spendry FILE *fout, *din = 0; 665*66670Spendry int (*closefunc) __P((FILE *)); 66640193Sbostic sig_t oldintr, oldintp; 667*66670Spendry int c, d, is_retr, tcrflag, bare_lfs = 0; 66838133Srick static int bufsize; 66936944Skarels static char *buf; 67036940Skarels long bytes = 0, hashbytes = HASHBYTES; 67110296Ssam struct timeval start, stop; 67236940Skarels struct stat st; 67310296Ssam 67436935Skarels is_retr = strcmp(cmd, "RETR") == 0; 67537225Skarels if (is_retr && verbose && printnames) { 67637225Skarels if (local && *local != '-') 67737225Skarels printf("local: %s ", local); 67837225Skarels if (remote) 67937225Skarels printf("remote: %s\n", remote); 68037225Skarels } 68136935Skarels if (proxy && is_retr) { 68226048Sminshall proxtrans(cmd, local, remote); 68326048Sminshall return; 68426048Sminshall } 68510296Ssam closefunc = NULL; 68626048Sminshall oldintr = NULL; 68726048Sminshall oldintp = NULL; 68836935Skarels tcrflag = !crflag && is_retr; 68926048Sminshall if (setjmp(recvabort)) { 69026048Sminshall while (cpend) { 69126048Sminshall (void) getreply(0); 69226048Sminshall } 69326048Sminshall if (data >= 0) { 69426048Sminshall (void) close(data); 69526048Sminshall data = -1; 69626048Sminshall } 69726448Slepreau if (oldintr) 69826048Sminshall (void) signal(SIGINT, oldintr); 69926048Sminshall code = -1; 70026048Sminshall return; 70126048Sminshall } 70210296Ssam oldintr = signal(SIGINT, abortrecv); 70326048Sminshall if (strcmp(local, "-") && *local != '|') { 70410296Ssam if (access(local, 2) < 0) { 705*66670Spendry char *dir = strrchr(local, '/'); 70610296Ssam 70726048Sminshall if (errno != ENOENT && errno != EACCES) { 708*66670Spendry warn("local: %s", local); 70926048Sminshall (void) signal(SIGINT, oldintr); 71026048Sminshall code = -1; 71126048Sminshall return; 71210296Ssam } 71326048Sminshall if (dir != NULL) 71426048Sminshall *dir = 0; 71526048Sminshall d = access(dir ? local : ".", 2); 71626048Sminshall if (dir != NULL) 71726048Sminshall *dir = '/'; 71826048Sminshall if (d < 0) { 719*66670Spendry warn("local: %s", local); 72026048Sminshall (void) signal(SIGINT, oldintr); 72126048Sminshall code = -1; 72226048Sminshall return; 72326048Sminshall } 72426048Sminshall if (!runique && errno == EACCES && 72536935Skarels chmod(local, 0600) < 0) { 726*66670Spendry warn("local: %s", local); 72726048Sminshall (void) signal(SIGINT, oldintr); 72838202Srick (void) signal(SIGINT, oldintr); 72926048Sminshall code = -1; 73026048Sminshall return; 73126048Sminshall } 73226048Sminshall if (runique && errno == EACCES && 73326048Sminshall (local = gunique(local)) == NULL) { 73426048Sminshall (void) signal(SIGINT, oldintr); 73526048Sminshall code = -1; 73626048Sminshall return; 73726048Sminshall } 73810296Ssam } 73926048Sminshall else if (runique && (local = gunique(local)) == NULL) { 74026048Sminshall (void) signal(SIGINT, oldintr); 74126048Sminshall code = -1; 74226048Sminshall return; 74326048Sminshall } 74426048Sminshall } 74538033Skarels if (!is_retr) { 74638033Skarels if (curtype != TYPE_A) 74738033Skarels changetype(TYPE_A, 0); 74838033Skarels } else if (curtype != type) 74938033Skarels changetype(type, 0); 75026048Sminshall if (initconn()) { 75126048Sminshall (void) signal(SIGINT, oldintr); 75226048Sminshall code = -1; 75326048Sminshall return; 75426048Sminshall } 75526448Slepreau if (setjmp(recvabort)) 75626048Sminshall goto abort; 75738033Skarels if (is_retr && restart_point && 75838033Skarels command("REST %ld", (long) restart_point) != CONTINUE) 75938033Skarels return; 76010296Ssam if (remote) { 76126048Sminshall if (command("%s %s", cmd, remote) != PRELIM) { 76226048Sminshall (void) signal(SIGINT, oldintr); 76326048Sminshall return; 76426048Sminshall } 76526048Sminshall } else { 76626048Sminshall if (command("%s", cmd) != PRELIM) { 76726048Sminshall (void) signal(SIGINT, oldintr); 76826048Sminshall return; 76926048Sminshall } 77026048Sminshall } 77126048Sminshall din = dataconn("r"); 77226048Sminshall if (din == NULL) 77326048Sminshall goto abort; 77426448Slepreau if (strcmp(local, "-") == 0) 77510296Ssam fout = stdout; 77610296Ssam else if (*local == '|') { 77726048Sminshall oldintp = signal(SIGPIPE, SIG_IGN); 77835659Sbostic fout = popen(local + 1, "w"); 77926048Sminshall if (fout == NULL) { 780*66670Spendry warn("%s", local+1); 78126048Sminshall goto abort; 78226048Sminshall } 78335659Sbostic closefunc = pclose; 78436940Skarels } else { 78540193Sbostic fout = fopen(local, lmode); 78626048Sminshall if (fout == NULL) { 787*66670Spendry warn("local: %s", local); 78826048Sminshall goto abort; 78926048Sminshall } 79010296Ssam closefunc = fclose; 79110296Ssam } 79236940Skarels if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0) 79336940Skarels st.st_blksize = BUFSIZ; 79436940Skarels if (st.st_blksize > bufsize) { 79536940Skarels if (buf) 79636940Skarels (void) free(buf); 79738133Srick buf = malloc((unsigned)st.st_blksize); 79836940Skarels if (buf == NULL) { 799*66670Spendry warn("malloc"); 80036944Skarels bufsize = 0; 80136940Skarels goto abort; 80236940Skarels } 80336940Skarels bufsize = st.st_blksize; 80436940Skarels } 80526496Sminshall (void) gettimeofday(&start, (struct timezone *)0); 80638033Skarels switch (curtype) { 80711219Ssam 80811219Ssam case TYPE_I: 80911219Ssam case TYPE_L: 81037225Skarels if (restart_point && 811*66670Spendry lseek(fileno(fout), restart_point, L_SET) < 0) { 812*66670Spendry warn("local: %s", local); 81337225Skarels if (closefunc != NULL) 81437225Skarels (*closefunc)(fout); 81537225Skarels return; 81637225Skarels } 81711346Ssam errno = d = 0; 81836940Skarels while ((c = read(fileno(din), buf, bufsize)) > 0) { 81936944Skarels if ((d = write(fileno(fout), buf, c)) != c) 82011219Ssam break; 82111219Ssam bytes += c; 82211651Ssam if (hash) { 82336940Skarels while (bytes >= hashbytes) { 82436940Skarels (void) putchar('#'); 82536940Skarels hashbytes += HASHBYTES; 82636940Skarels } 82726496Sminshall (void) fflush(stdout); 82811651Ssam } 82911219Ssam } 83013213Ssam if (hash && bytes > 0) { 83136940Skarels if (bytes < HASHBYTES) 83236940Skarels (void) putchar('#'); 83326496Sminshall (void) putchar('\n'); 83426496Sminshall (void) fflush(stdout); 83511651Ssam } 83636935Skarels if (c < 0) { 83736935Skarels if (errno != EPIPE) 838*66670Spendry warn("netin"); 83936935Skarels bytes = -1; 84036935Skarels } 84136942Skarels if (d < c) { 84236942Skarels if (d < 0) 843*66670Spendry warn("local: %s", local); 84436942Skarels else 845*66670Spendry warnx("%s: short write", local); 84636942Skarels } 84711219Ssam break; 84811219Ssam 84911219Ssam case TYPE_A: 85037225Skarels if (restart_point) { 851*66670Spendry int i, n, ch; 85237225Skarels 85337225Skarels if (fseek(fout, 0L, L_SET) < 0) 85437225Skarels goto done; 85537225Skarels n = restart_point; 85640193Sbostic for (i = 0; i++ < n;) { 85740193Sbostic if ((ch = getc(fout)) == EOF) 85837225Skarels goto done; 85940193Sbostic if (ch == '\n') 86037225Skarels i++; 86137225Skarels } 86237225Skarels if (fseek(fout, 0L, L_INCR) < 0) { 86337225Skarels done: 864*66670Spendry warn("local: %s", local); 86537225Skarels if (closefunc != NULL) 86637225Skarels (*closefunc)(fout); 86737225Skarels return; 86837225Skarels } 86937225Skarels } 87011219Ssam while ((c = getc(din)) != EOF) { 87138133Srick if (c == '\n') 87238133Srick bare_lfs++; 87327749Sminshall while (c == '\r') { 87411651Ssam while (hash && (bytes >= hashbytes)) { 87526496Sminshall (void) putchar('#'); 87626496Sminshall (void) fflush(stdout); 87736940Skarels hashbytes += HASHBYTES; 87811651Ssam } 87910296Ssam bytes++; 88026048Sminshall if ((c = getc(din)) != '\n' || tcrflag) { 88136940Skarels if (ferror(fout)) 88236940Skarels goto break2; 88336940Skarels (void) putc('\r', fout); 88436942Skarels if (c == '\0') { 88536942Skarels bytes++; 88636940Skarels goto contin2; 88736942Skarels } 88836942Skarels if (c == EOF) 88936942Skarels goto contin2; 89011219Ssam } 89111219Ssam } 89236940Skarels (void) putc(c, fout); 89311219Ssam bytes++; 89436940Skarels contin2: ; 89510296Ssam } 89636940Skarels break2: 89738133Srick if (bare_lfs) { 89838133Srick printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs); 89938133Srick printf("File may not have transferred correctly.\n"); 90038133Srick } 90111651Ssam if (hash) { 90213213Ssam if (bytes < hashbytes) 90326496Sminshall (void) putchar('#'); 90426496Sminshall (void) putchar('\n'); 90526496Sminshall (void) fflush(stdout); 90611651Ssam } 90736944Skarels if (ferror(din)) { 90836935Skarels if (errno != EPIPE) 909*66670Spendry warn("netin"); 91036935Skarels bytes = -1; 91136935Skarels } 91236940Skarels if (ferror(fout)) 913*66670Spendry warn("local: %s", local); 91411219Ssam break; 91510296Ssam } 91626448Slepreau if (closefunc != NULL) 91726048Sminshall (*closefunc)(fout); 91826496Sminshall (void) signal(SIGINT, oldintr); 91926448Slepreau if (oldintp) 92026048Sminshall (void) signal(SIGPIPE, oldintp); 92126496Sminshall (void) gettimeofday(&stop, (struct timezone *)0); 92210296Ssam (void) fclose(din); 92326048Sminshall (void) getreply(0); 92436935Skarels if (bytes > 0 && is_retr) 92537225Skarels ptransfer("received", bytes, &start, &stop); 92626048Sminshall return; 92726048Sminshall abort: 92826048Sminshall 92927687Sminshall /* abort using RFC959 recommended IP,SYNC sequence */ 93026048Sminshall 93126496Sminshall (void) gettimeofday(&stop, (struct timezone *)0); 93226448Slepreau if (oldintp) 93326048Sminshall (void) signal(SIGPIPE, oldintr); 93438133Srick (void) signal(SIGINT, SIG_IGN); 93526048Sminshall if (!cpend) { 93626048Sminshall code = -1; 93738133Srick (void) signal(SIGINT, oldintr); 93826048Sminshall return; 93926048Sminshall } 94026048Sminshall 94138133Srick abort_remote(din); 94226048Sminshall code = -1; 94326048Sminshall if (data >= 0) { 94426048Sminshall (void) close(data); 94526048Sminshall data = -1; 94626048Sminshall } 94726448Slepreau if (closefunc != NULL && fout != NULL) 94826048Sminshall (*closefunc)(fout); 94926448Slepreau if (din) 95026048Sminshall (void) fclose(din); 95135699Sbostic if (bytes > 0) 95237225Skarels ptransfer("received", bytes, &start, &stop); 95338133Srick (void) signal(SIGINT, oldintr); 95410296Ssam } 95510296Ssam 95610296Ssam /* 95740193Sbostic * Need to start a listen on the data channel before we send the command, 95840193Sbostic * otherwise the server's connect may fail. 95910296Ssam */ 960*66670Spendry int 96110296Ssam initconn() 96210296Ssam { 963*66670Spendry char *p, *a; 96426048Sminshall int result, len, tmpno = 0; 96526993Skarels int on = 1; 96610296Ssam 96711651Ssam noport: 96810296Ssam data_addr = myctladdr; 96911651Ssam if (sendport) 97011651Ssam data_addr.sin_port = 0; /* let system pick one */ 97111651Ssam if (data != -1) 97238133Srick (void) close(data); 97318287Sralph data = socket(AF_INET, SOCK_STREAM, 0); 97410296Ssam if (data < 0) { 975*66670Spendry warn("socket"); 97626448Slepreau if (tmpno) 97726048Sminshall sendport = 1; 97810296Ssam return (1); 97910296Ssam } 98012397Ssam if (!sendport) 98127687Sminshall if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) { 982*66670Spendry warn("setsockopt (reuse address)"); 98312397Ssam goto bad; 98412397Ssam } 98526496Sminshall if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) { 986*66670Spendry warn("bind"); 98710296Ssam goto bad; 98810296Ssam } 98910296Ssam if (options & SO_DEBUG && 99027687Sminshall setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0) 991*66670Spendry warn("setsockopt (ignored)"); 99211627Ssam len = sizeof (data_addr); 99338133Srick if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) { 994*66670Spendry warn("getsockname"); 99510296Ssam goto bad; 99610296Ssam } 99726448Slepreau if (listen(data, 1) < 0) 998*66670Spendry warn("listen"); 99911651Ssam if (sendport) { 100011651Ssam a = (char *)&data_addr.sin_addr; 100111651Ssam p = (char *)&data_addr.sin_port; 100210296Ssam #define UC(b) (((int)b)&0xff) 100311651Ssam result = 100411651Ssam command("PORT %d,%d,%d,%d,%d,%d", 100511651Ssam UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 100611651Ssam UC(p[0]), UC(p[1])); 100711651Ssam if (result == ERROR && sendport == -1) { 100811651Ssam sendport = 0; 100926048Sminshall tmpno = 1; 101011651Ssam goto noport; 101111651Ssam } 101211651Ssam return (result != COMPLETE); 101311651Ssam } 101426448Slepreau if (tmpno) 101526048Sminshall sendport = 1; 101644340Skarels #ifdef IP_TOS 101744340Skarels on = IPTOS_THROUGHPUT; 101844340Skarels if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) 1019*66670Spendry warn("setsockopt TOS (ignored)"); 102044340Skarels #endif 102111651Ssam return (0); 102210296Ssam bad: 102310296Ssam (void) close(data), data = -1; 102426448Slepreau if (tmpno) 102526048Sminshall sendport = 1; 102610296Ssam return (1); 102710296Ssam } 102810296Ssam 102910296Ssam FILE * 103040193Sbostic dataconn(lmode) 103140193Sbostic char *lmode; 103210296Ssam { 103310296Ssam struct sockaddr_in from; 103444340Skarels int s, fromlen = sizeof (from), tos; 103510296Ssam 103626496Sminshall s = accept(data, (struct sockaddr *) &from, &fromlen); 103710296Ssam if (s < 0) { 1038*66670Spendry warn("accept"); 103910296Ssam (void) close(data), data = -1; 104010296Ssam return (NULL); 104110296Ssam } 104210296Ssam (void) close(data); 104310296Ssam data = s; 104444340Skarels #ifdef IP_TOS 104544340Skarels tos = IPTOS_THROUGHPUT; 104644340Skarels if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0) 1047*66670Spendry warn("setsockopt TOS (ignored)"); 104844340Skarels #endif 104940193Sbostic return (fdopen(data, lmode)); 105010296Ssam } 105110296Ssam 1052*66670Spendry void 105337225Skarels ptransfer(direction, bytes, t0, t1) 105437225Skarels char *direction; 105511651Ssam long bytes; 105610296Ssam struct timeval *t0, *t1; 105710296Ssam { 105810296Ssam struct timeval td; 105916437Sleres float s, bs; 106010296Ssam 106135699Sbostic if (verbose) { 106235699Sbostic tvsub(&td, t1, t0); 106335699Sbostic s = td.tv_sec + (td.tv_usec / 1000000.); 106410296Ssam #define nz(x) ((x) == 0 ? 1 : (x)) 106535699Sbostic bs = bytes / nz(s); 106635699Sbostic printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n", 106735699Sbostic bytes, direction, s, bs / 1024.); 106835699Sbostic } 106910296Ssam } 107010296Ssam 1071*66670Spendry /* 1072*66670Spendry void 1073*66670Spendry tvadd(tsum, t0) 107410296Ssam struct timeval *tsum, *t0; 107510296Ssam { 107610296Ssam 107710296Ssam tsum->tv_sec += t0->tv_sec; 107810296Ssam tsum->tv_usec += t0->tv_usec; 107910296Ssam if (tsum->tv_usec > 1000000) 108010296Ssam tsum->tv_sec++, tsum->tv_usec -= 1000000; 1081*66670Spendry } 1082*66670Spendry */ 108310296Ssam 1084*66670Spendry void 108510296Ssam tvsub(tdiff, t1, t0) 108610296Ssam struct timeval *tdiff, *t1, *t0; 108710296Ssam { 108810296Ssam 108910296Ssam tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 109010296Ssam tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 109110296Ssam if (tdiff->tv_usec < 0) 109210296Ssam tdiff->tv_sec--, tdiff->tv_usec += 1000000; 109310296Ssam } 109426048Sminshall 109540193Sbostic void 109626048Sminshall psabort() 109726048Sminshall { 109826048Sminshall 109926048Sminshall abrtflag++; 110026048Sminshall } 110126048Sminshall 1102*66670Spendry void 110326048Sminshall pswitch(flag) 110426048Sminshall int flag; 110526048Sminshall { 110640193Sbostic sig_t oldintr; 110726048Sminshall static struct comvars { 110826048Sminshall int connect; 110928469Skarels char name[MAXHOSTNAMELEN]; 111026048Sminshall struct sockaddr_in mctl; 111126048Sminshall struct sockaddr_in hctl; 111226048Sminshall FILE *in; 111326048Sminshall FILE *out; 111426048Sminshall int tpe; 111538033Skarels int curtpe; 111626048Sminshall int cpnd; 111726048Sminshall int sunqe; 111826048Sminshall int runqe; 111926048Sminshall int mcse; 112026048Sminshall int ntflg; 112126048Sminshall char nti[17]; 112226048Sminshall char nto[17]; 112326048Sminshall int mapflg; 112426048Sminshall char mi[MAXPATHLEN]; 112526048Sminshall char mo[MAXPATHLEN]; 112638033Skarels } proxstruct, tmpstruct; 112726048Sminshall struct comvars *ip, *op; 112826048Sminshall 112926048Sminshall abrtflag = 0; 113026048Sminshall oldintr = signal(SIGINT, psabort); 113126048Sminshall if (flag) { 113226448Slepreau if (proxy) 113326048Sminshall return; 113426048Sminshall ip = &tmpstruct; 113526048Sminshall op = &proxstruct; 113626048Sminshall proxy++; 113738033Skarels } else { 113826448Slepreau if (!proxy) 113926048Sminshall return; 114026048Sminshall ip = &proxstruct; 114126048Sminshall op = &tmpstruct; 114226048Sminshall proxy = 0; 114326048Sminshall } 114426048Sminshall ip->connect = connected; 114526048Sminshall connected = op->connect; 114628469Skarels if (hostname) { 114728469Skarels (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1); 114828469Skarels ip->name[strlen(ip->name)] = '\0'; 114928469Skarels } else 115028469Skarels ip->name[0] = 0; 115126048Sminshall hostname = op->name; 115226048Sminshall ip->hctl = hisctladdr; 115326048Sminshall hisctladdr = op->hctl; 115426048Sminshall ip->mctl = myctladdr; 115526048Sminshall myctladdr = op->mctl; 115626048Sminshall ip->in = cin; 115726048Sminshall cin = op->in; 115826048Sminshall ip->out = cout; 115926048Sminshall cout = op->out; 116026048Sminshall ip->tpe = type; 116126048Sminshall type = op->tpe; 116238033Skarels ip->curtpe = curtype; 116338033Skarels curtype = op->curtpe; 116426048Sminshall ip->cpnd = cpend; 116526048Sminshall cpend = op->cpnd; 116626048Sminshall ip->sunqe = sunique; 116726048Sminshall sunique = op->sunqe; 116826048Sminshall ip->runqe = runique; 116926048Sminshall runique = op->runqe; 117026048Sminshall ip->mcse = mcase; 117126048Sminshall mcase = op->mcse; 117226048Sminshall ip->ntflg = ntflag; 117326048Sminshall ntflag = op->ntflg; 117426496Sminshall (void) strncpy(ip->nti, ntin, 16); 117526048Sminshall (ip->nti)[strlen(ip->nti)] = '\0'; 117626496Sminshall (void) strcpy(ntin, op->nti); 117726496Sminshall (void) strncpy(ip->nto, ntout, 16); 117826048Sminshall (ip->nto)[strlen(ip->nto)] = '\0'; 117926496Sminshall (void) strcpy(ntout, op->nto); 118026048Sminshall ip->mapflg = mapflag; 118126048Sminshall mapflag = op->mapflg; 118226496Sminshall (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1); 118326048Sminshall (ip->mi)[strlen(ip->mi)] = '\0'; 118426496Sminshall (void) strcpy(mapin, op->mi); 118526496Sminshall (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1); 118626048Sminshall (ip->mo)[strlen(ip->mo)] = '\0'; 118726496Sminshall (void) strcpy(mapout, op->mo); 118826048Sminshall (void) signal(SIGINT, oldintr); 118926048Sminshall if (abrtflag) { 119026048Sminshall abrtflag = 0; 119146828Sbostic (*oldintr)(SIGINT); 119226448Slepreau } 119326048Sminshall } 119426048Sminshall 119540193Sbostic void 119626048Sminshall abortpt() 119726048Sminshall { 1198*66670Spendry 119926048Sminshall printf("\n"); 120026496Sminshall (void) fflush(stdout); 120126048Sminshall ptabflg++; 120226048Sminshall mflag = 0; 120326048Sminshall abrtflag = 0; 120426048Sminshall longjmp(ptabort, 1); 120526048Sminshall } 120626048Sminshall 1207*66670Spendry void 120826048Sminshall proxtrans(cmd, local, remote) 120926048Sminshall char *cmd, *local, *remote; 121026048Sminshall { 121140193Sbostic sig_t oldintr; 121238133Srick int secndflag = 0, prox_type, nfnd; 121326048Sminshall char *cmd2; 121426496Sminshall struct fd_set mask; 121526048Sminshall 121626448Slepreau if (strcmp(cmd, "RETR")) 121726048Sminshall cmd2 = "RETR"; 121826448Slepreau else 121926048Sminshall cmd2 = runique ? "STOU" : "STOR"; 122038033Skarels if ((prox_type = type) == 0) { 122138033Skarels if (unix_server && unix_proxy) 122238033Skarels prox_type = TYPE_I; 122338033Skarels else 122438033Skarels prox_type = TYPE_A; 122538033Skarels } 122638033Skarels if (curtype != prox_type) 122738033Skarels changetype(prox_type, 1); 122826048Sminshall if (command("PASV") != COMPLETE) { 122938033Skarels printf("proxy server does not support third party transfers.\n"); 123026048Sminshall return; 123126048Sminshall } 123226048Sminshall pswitch(0); 123326048Sminshall if (!connected) { 123426048Sminshall printf("No primary connection\n"); 123526048Sminshall pswitch(1); 123626048Sminshall code = -1; 123726048Sminshall return; 123826048Sminshall } 123938033Skarels if (curtype != prox_type) 124038033Skarels changetype(prox_type, 1); 124126048Sminshall if (command("PORT %s", pasv) != COMPLETE) { 124226048Sminshall pswitch(1); 124326048Sminshall return; 124426048Sminshall } 124526448Slepreau if (setjmp(ptabort)) 124626048Sminshall goto abort; 124726048Sminshall oldintr = signal(SIGINT, abortpt); 124826048Sminshall if (command("%s %s", cmd, remote) != PRELIM) { 124926048Sminshall (void) signal(SIGINT, oldintr); 125026048Sminshall pswitch(1); 125126048Sminshall return; 125226048Sminshall } 125326048Sminshall sleep(2); 125426048Sminshall pswitch(1); 125526048Sminshall secndflag++; 125626448Slepreau if (command("%s %s", cmd2, local) != PRELIM) 125726048Sminshall goto abort; 125826048Sminshall ptflag++; 125926048Sminshall (void) getreply(0); 126026048Sminshall pswitch(0); 126126048Sminshall (void) getreply(0); 126226048Sminshall (void) signal(SIGINT, oldintr); 126326048Sminshall pswitch(1); 126426048Sminshall ptflag = 0; 126526048Sminshall printf("local: %s remote: %s\n", local, remote); 126626048Sminshall return; 126726048Sminshall abort: 126826048Sminshall (void) signal(SIGINT, SIG_IGN); 126926048Sminshall ptflag = 0; 127026448Slepreau if (strcmp(cmd, "RETR") && !proxy) 127126048Sminshall pswitch(1); 127226448Slepreau else if (!strcmp(cmd, "RETR") && proxy) 127326048Sminshall pswitch(0); 127426048Sminshall if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */ 127526048Sminshall if (command("%s %s", cmd2, local) != PRELIM) { 127626048Sminshall pswitch(0); 127738133Srick if (cpend) 127838133Srick abort_remote((FILE *) NULL); 127926048Sminshall } 128026048Sminshall pswitch(1); 128126448Slepreau if (ptabflg) 128226048Sminshall code = -1; 128326048Sminshall (void) signal(SIGINT, oldintr); 128426048Sminshall return; 128526048Sminshall } 128638133Srick if (cpend) 128738133Srick abort_remote((FILE *) NULL); 128826048Sminshall pswitch(!proxy); 128926048Sminshall if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */ 129026048Sminshall if (command("%s %s", cmd2, local) != PRELIM) { 129126048Sminshall pswitch(0); 129238133Srick if (cpend) 129338133Srick abort_remote((FILE *) NULL); 129426048Sminshall pswitch(1); 129526448Slepreau if (ptabflg) 129626048Sminshall code = -1; 129726048Sminshall (void) signal(SIGINT, oldintr); 129826048Sminshall return; 129926048Sminshall } 130026048Sminshall } 130138133Srick if (cpend) 130238133Srick abort_remote((FILE *) NULL); 130326048Sminshall pswitch(!proxy); 130426048Sminshall if (cpend) { 130527687Sminshall FD_ZERO(&mask); 130626496Sminshall FD_SET(fileno(cin), &mask); 130738133Srick if ((nfnd = empty(&mask, 10)) <= 0) { 130827687Sminshall if (nfnd < 0) { 1309*66670Spendry warn("abort"); 131027687Sminshall } 131126448Slepreau if (ptabflg) 131226048Sminshall code = -1; 131326048Sminshall lostpeer(); 131426048Sminshall } 131526048Sminshall (void) getreply(0); 131626048Sminshall (void) getreply(0); 131726048Sminshall } 131826448Slepreau if (proxy) 131926048Sminshall pswitch(0); 132026048Sminshall pswitch(1); 132126448Slepreau if (ptabflg) 132226048Sminshall code = -1; 132326048Sminshall (void) signal(SIGINT, oldintr); 132426048Sminshall } 132526048Sminshall 1326*66670Spendry void 1327*66670Spendry reset(argc, argv) 1328*66670Spendry int argc; 1329*66670Spendry char *argv[]; 133026048Sminshall { 133126496Sminshall struct fd_set mask; 133226496Sminshall int nfnd = 1; 133326048Sminshall 133427687Sminshall FD_ZERO(&mask); 133530946Scsvsj while (nfnd > 0) { 133626496Sminshall FD_SET(fileno(cin), &mask); 133727687Sminshall if ((nfnd = empty(&mask,0)) < 0) { 1338*66670Spendry warn("reset"); 133926048Sminshall code = -1; 134026048Sminshall lostpeer(); 134126048Sminshall } 134227687Sminshall else if (nfnd) { 134326048Sminshall (void) getreply(0); 134426496Sminshall } 134526048Sminshall } 134626048Sminshall } 134726048Sminshall 134826048Sminshall char * 134926048Sminshall gunique(local) 135026048Sminshall char *local; 135126048Sminshall { 135226048Sminshall static char new[MAXPATHLEN]; 1353*66670Spendry char *cp = strrchr(local, '/'); 135426048Sminshall int d, count=0; 135526048Sminshall char ext = '1'; 135626048Sminshall 135726448Slepreau if (cp) 135826048Sminshall *cp = '\0'; 135926048Sminshall d = access(cp ? local : ".", 2); 136026448Slepreau if (cp) 136126048Sminshall *cp = '/'; 136226048Sminshall if (d < 0) { 1363*66670Spendry warn("local: %s", local); 1364*66670Spendry return ((char *) 0); 136526048Sminshall } 136626048Sminshall (void) strcpy(new, local); 136726048Sminshall cp = new + strlen(new); 136826048Sminshall *cp++ = '.'; 136926048Sminshall while (!d) { 137026048Sminshall if (++count == 100) { 137126048Sminshall printf("runique: can't find unique file name.\n"); 1372*66670Spendry return ((char *) 0); 137326048Sminshall } 137426048Sminshall *cp++ = ext; 137526048Sminshall *cp = '\0'; 137626448Slepreau if (ext == '9') 137726048Sminshall ext = '0'; 137826448Slepreau else 137926048Sminshall ext++; 138026448Slepreau if ((d = access(new, 0)) < 0) 138126048Sminshall break; 138226448Slepreau if (ext != '0') 138326048Sminshall cp--; 138426448Slepreau else if (*(cp - 2) == '.') 138526048Sminshall *(cp - 1) = '1'; 138626048Sminshall else { 138726048Sminshall *(cp - 2) = *(cp - 2) + 1; 138826048Sminshall cp--; 138926048Sminshall } 139026048Sminshall } 1391*66670Spendry return (new); 139226048Sminshall } 139338133Srick 1394*66670Spendry void 139538133Srick abort_remote(din) 1396*66670Spendry FILE *din; 139738133Srick { 139838133Srick char buf[BUFSIZ]; 139938133Srick int nfnd; 140038133Srick struct fd_set mask; 140138133Srick 140238133Srick /* 140338133Srick * send IAC in urgent mode instead of DM because 4.3BSD places oob mark 140438133Srick * after urgent byte rather than before as is protocol now 140538133Srick */ 140638133Srick sprintf(buf, "%c%c%c", IAC, IP, IAC); 140738133Srick if (send(fileno(cout), buf, 3, MSG_OOB) != 3) 1408*66670Spendry warn("abort"); 140938133Srick fprintf(cout,"%cABOR\r\n", DM); 141038133Srick (void) fflush(cout); 141138133Srick FD_ZERO(&mask); 141238133Srick FD_SET(fileno(cin), &mask); 141338133Srick if (din) { 141438133Srick FD_SET(fileno(din), &mask); 141538133Srick } 141638133Srick if ((nfnd = empty(&mask, 10)) <= 0) { 141738133Srick if (nfnd < 0) { 1418*66670Spendry warn("abort"); 141938133Srick } 142038133Srick if (ptabflg) 142138133Srick code = -1; 142238133Srick lostpeer(); 142338133Srick } 142438133Srick if (din && FD_ISSET(fileno(din), &mask)) { 142538133Srick while (read(fileno(din), buf, BUFSIZ) > 0) 142638133Srick /* LOOP */; 142738133Srick } 142838133Srick if (getreply(0) == ERROR && code == 552) { 142938133Srick /* 552 needed for nic style abort */ 143038133Srick (void) getreply(0); 143138133Srick } 143238133Srick (void) getreply(0); 143338133Srick } 1434