1 *** ftp-gw.c.orig Sun Jun 22 16:27:42 1997
2 --- ftp-gw.c Sun Jun 22 17:02:16 1997
7 static char RcsId[] = "Header: /devel/CVS/IP-Filter/FWTK/ftp-gw.diff,v 2.1 1999/08/04 17:30:30 darrenr Exp";
10 + * Patches for IP Filter NAT extensions written by Darren Reed, 7/7/96
11 + * darrenr@cyber.com.au
13 + static char vIpFilter[] = "v3.1.11";
20 #include <sys/signal.h>
21 #include <sys/ioctl.h>
22 #include <sys/errno.h>
25 extern char *sys_errlist[];
28 #include <arpa/telnet.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
35 extern char *rindex();
43 + #include "ip_compat.h"
52 static int cmd_noop();
53 static int cmd_abor();
54 static int cmd_passthru();
55 + static int nat_destination();
56 + static int connectdest();
57 static void saveline();
58 static void flushsaved();
59 static void trap_sigurg();
63 if(say(0,"220-Proxy first requires authentication"))
65 ! sprintf(xuf,"220 %s FTP proxy (Version %s) ready.",huf,FWTK_VERSION_MINOR);
71 if(say(0,"220-Proxy first requires authentication"))
73 ! sprintf(xuf,"220-%s FTP proxy (Version %s) ready.",huf,FWTK_VERSION_MINOR);
76 ! sprintf(xuf,"220-%s TIS ftp-gw with IP Filter %s NAT extensions",huf,vIpFilter);
93 static char narg[] = "501 Missing or extra username";
94 static char noad[] = "501 Use user@site to connect via proxy";
102 short port = FTPPORT;
104 /* kludgy but effective. if authorizing everything call auth instead */
109 return(sayn(0,noad,sizeof(noad)));
112 + if((rfd == -1) && (x = connectdest(dest,port)))
114 + sprintf(buf,"USER %s",user);
117 + x = getresp(rfd,buf,sizeof(buf),1);
120 + return(say(0,buf));
124 + connectdest(dest,port)
141 ! sprintf(buf,"521 %s: %s",dest,ebuf);
144 sprintf(buf,"----GATEWAY CONNECTED TO %s----",dest);
151 ! sprintf(buf,"521 %s,%d: %s",dest,ntohs(port),ebuf);
155 sprintf(buf,"----GATEWAY CONNECTED TO %s----",dest);
164 ! sprintf(buf,"USER %s",user);
167 ! x = getresp(rfd,buf,sizeof(buf),1);
170 ! return(say(0,buf));
192 + nat_destination(fd)
195 + struct sockaddr_in laddr, faddr;
196 + struct natlookup natlookup;
200 + bzero((char *)&laddr, sizeof(laddr));
201 + bzero((char *)&faddr, sizeof(faddr));
202 + slen = sizeof(laddr);
203 + if(getsockname(fd,(struct sockaddr *)&laddr,&slen) < 0) {
204 + perror("getsockname");
207 + slen = sizeof(faddr);
208 + if(getpeername(fd,(struct sockaddr *)&faddr,&slen) < 0) {
209 + perror("getsockname");
213 + natlookup.nl_inport = laddr.sin_port;
214 + natlookup.nl_outport = faddr.sin_port;
215 + natlookup.nl_inip = laddr.sin_addr;
216 + natlookup.nl_outip = faddr.sin_addr;
217 + natlookup.nl_flags = IPN_TCP;
218 + if((natfd = open(IPL_NAT, O_RDONLY)) < 0) {
222 + if(ioctl(natfd, SIOCGNATL, &natlookup) == -1) {
223 + syslog(LOG_ERR, "SIOCGNATL failed: %m\n");
225 + if(say(0,"220 Ready"))
230 + return connectdest(inet_ntoa(natlookup.nl_realip),
231 + ntohs(natlookup.nl_realport));