summaryrefslogtreecommitdiffstats
path: root/sys/netinet/libalias/alias_ftp.c
blob: 2b4c51ecb85b8abc680a92e4c7a18d7a55f93557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
    Alias_ftp.c performs special processing for FTP sessions under
    TCP.  Specifically, when a PORT command from the client side
    is sent, it is intercepted and modified.  The address is changed
    to the gateway machine and an aliasing port is used.

    For this routine to work, the PORT command must fit entirely
    into a single TCP packet.  This is typically the case, but exceptions
    can easily be envisioned under the actual specifications.

    Probably the most troubling aspect of the approach taken here is
    that the new PORT command will typically be a different length, and
    this causes a certain amount of bookkeeping to keep track of the
    changes of sequence and acknowledgment numbers, since the client
    machine is totally unaware of the modification to the TCP stream.

    This version also supports the EPRT command, which is functionally
    equivalent to the PORT command, but was designed to support both
    IPv4 and IPv6 addresses.  See RFC 2428 for specifications.


    This software is placed into the public domain with no restrictions
    on its distribution.

    Initial version:  August, 1996  (cjm)

    Version 1.6
         Brian Somers and Martin Renters identified an IP checksum
         error for modified IP packets.

    Version 1.7:  January 9, 1996 (cjm)
         Differential checksum computation for change
         in IP packet length.
   
    Version 2.1:  May, 1997 (cjm)
         Very minor changes to conform with
         local/global/function naming conventions
         within the packet aliasing module.

    See HISTORY file for record of revisions.

    $FreeBSD$
*/

/* Includes */
#include <ctype.h>
#include <stdio.h> 
#include <string.h>
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include "alias_local.h"

static int  ParseFtpPortCommand(char *, int, struct ip *, struct alias_link *, int);
static void ParseFtpEprtCommand(char *, int, struct ip *, struct alias_link *, int);
static void NewFtpPortCommand(struct ip *, struct alias_link *, struct in_addr, u_short, int, int);



void
AliasHandleFtpOut(
struct ip *pip,	  /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize  /* The maximum size this packet can grow to (including headers) */)
{
    int hlen, tlen, dlen;
    char *sptr;
    struct tcphdr *tc;
        
/* Calculate data length of TCP packet */
    tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
    hlen = (pip->ip_hl + tc->th_off) << 2;
    tlen = ntohs(pip->ip_len);
    dlen = tlen - hlen;

/* Return if data length is too long or too short */
    if (dlen<10 || dlen>80)
        return;

/* Place string pointer and beginning of data */
    sptr = (char *) pip;  
    sptr += hlen;

/* Parse through string using state diagram method */
    if (!ParseFtpPortCommand(sptr, dlen, pip, link, maxpacketsize))
	ParseFtpEprtCommand(sptr, dlen, pip, link, maxpacketsize);
}

static int
ParseFtpPortCommand(
char *sptr,
int dlen,
struct ip *pip,	  /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize  /* The maximum size this packet can grow to (including headers) */)
{
    struct in_addr true_addr;
    u_short true_port;
    char ch;
    int i, state;
    u_long a1, a2, a3, a4;
    u_short p1, p2; 

    a1=0; a2=0; a3=0; a4=0; p1=0; p2=0;
    state=-4;
    for (i=0; i<dlen; i++)
    {
	ch = sptr[i];
	switch (state)
	{
	case -4: if (ch == 'P') state++; else return 0; break;
	case -3: if (ch == 'O') state++; else return 0; break;
	case -2: if (ch == 'R') state++; else return 0; break;
	case -1: if (ch == 'T') state++; else return 0; break;

	case 0 :
	    if (isdigit(ch)) {a1=ch-'0'; state++;} break;
	case 1 :
	    if (isdigit(ch)) a1=10*a1+ch-'0'; else state++; break;
	case 2 :
	    if (isdigit(ch)) {a2=ch-'0'; state++;} break;
	case 3 :
	    if (isdigit(ch)) a2=10*a2+ch-'0'; else state++; break;
	case 4 :
	    if (isdigit(ch)) {a3=ch-'0'; state++;} break;
	case 5 :
	    if (isdigit(ch)) a3=10*a3+ch-'0'; else state++; break;
	case 6 :
	    if (isdigit(ch)) {a4=ch-'0'; state++;} break;
	case 7 :
	    if (isdigit(ch)) a4=10*a4+ch-'0'; else state++; break;
	case 8 :
	    if (isdigit(ch)) {p1=ch-'0'; state++;} break;
	case 9 :
	    if (isdigit(ch)) p1=10*p1+ch-'0'; else state++; break;
	case 10:
	    if (isdigit(ch)) {p2=ch-'0'; state++;} break;
	case 11:
	    if (isdigit(ch)) p2=10*p2+ch-'0'; break;
	}
    }

    if (state == 11)
    {
	true_port = htons((p1<<8) + p2);
	true_addr.s_addr = htonl((a1<<24) + (a2<<16) +(a3<<8) + a4);
	NewFtpPortCommand(pip, link, true_addr, true_port, maxpacketsize, 0);
	return 1;
    }
    else
	return 0;
}

static void
ParseFtpEprtCommand(
char *sptr,
int dlen,
struct ip *pip,	  /* IP packet to examine/patch */
struct alias_link *link, /* The link to go through (aliased port) */
int maxpacketsize  /* The maximum size this packet can grow to (including headers) */)
{
    struct in_addr true_addr;
    u_short true_port;
    char ch, delim;
    int i, state;
    u_long a1, a2, a3, a4;
    u_short pt;

    a1=0; a2=0; a3=0; a4=0; pt=0;
    delim='|';				/* XXX gcc -Wuninitialized */
    state=-4;
    for (i=0; i<dlen; i++)
    {
	ch = sptr[i];
	switch (state)
	{
	case -4: if (ch == 'E') state++; else return; break;
	case -3: if (ch == 'P') state++; else return; break;
	case -2: if (ch == 'R') state++; else return; break;
	case -1: if (ch == 'T') state++; else return; break;

	case 0 :
	    if (!isspace(ch)) {delim=ch; state++;} break;
	case 1 :
	    if (ch=='1') /* IPv4 address */ state++; else return; break;
	case 2 :
	    if (ch==delim) state++; else return; break;
	case 3 :
	    if (isdigit(ch)) {a1=ch-'0'; state++;} else return; break;
	case 4 :
	    if (isdigit(ch)) a1=10*a1+ch-'0';
	    else if (ch=='.') state++;
	    else return;
	    break;
	case 5 :
	    if (isdigit(ch)) {a2=ch-'0'; state++;} else return; break;
	case 6 :
	    if (isdigit(ch)) a2=10*a2+ch-'0';
	    else if (ch=='.') state++;
	    else return;
	    break;
	case 7:
	    if (isdigit(ch)) {a3=ch-'0'; state++;} else return; break;
	case 8 :
	    if (isdigit(ch)) a3=10*a3+ch-'0';
	    else if (ch=='.') state++;
	    else return;
	    break;
	case 9 :
	    if (isdigit(ch)) {a4=ch-'0'; state++;} else return; break;
	case 10:
	    if (isdigit(ch)) a4=10*a4+ch-'0';
	    else if (ch==delim) state++;
	    else return;
	    break;
	case 11:
	    if (isdigit(ch)) {pt=ch-'0'; state++;} else return; break;
	case 12:
	    if (isdigit(ch)) pt=10*pt+ch-'0';
	    else if (ch==delim) state++;
	    else return;
	    break;
	}
    }

    if (state == 13)
    {
	true_port = htons(pt);
	true_addr.s_addr = htonl((a1<<24) + (a2<<16) +(a3<<8) + a4);
	NewFtpPortCommand(pip, link, true_addr, true_port, maxpacketsize, 1);
    }
}

static void
NewFtpPortCommand(struct ip *pip,
                  struct alias_link *link,
                  struct in_addr true_addr,
                  u_short true_port,
                  int maxpacketsize,
                  int is_eprt)
{ 
    struct alias_link *ftp_link;

/* Establish link to address and port found in PORT command */
    ftp_link = FindUdpTcpOut(true_addr, GetDestAddress(link),
                             true_port, 0, IPPROTO_TCP);

    if (ftp_link != NULL)
    {
        int slen, hlen, tlen, dlen;
        struct tcphdr *tc;

#ifndef NO_FW_PUNCH
/* Punch hole in firewall */
        PunchFWHole(ftp_link);
#endif

/* Calculate data length of TCP packet */
        tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
        hlen = (pip->ip_hl + tc->th_off) << 2;
        tlen = ntohs(pip->ip_len);
        dlen = tlen - hlen;

/* Create new PORT command */
        {
            char stemp[80];
            char *sptr;
            u_short alias_port;
            u_char *ptr;
            int a1, a2, a3, a4, p1, p2; 
            struct in_addr alias_address;
        
/* Decompose alias address into quad format */
            alias_address = GetAliasAddress(link);
            ptr = (u_char *) &alias_address.s_addr;
            a1 = *ptr++; a2=*ptr++; a3=*ptr++; a4=*ptr;

	    alias_port = GetAliasPort(ftp_link);

	    if (is_eprt) {
/* Generate EPRT command string */
		sprintf(stemp, "EPRT |1|%d.%d.%d.%d|%d|\r\n",
			a1,a2,a3,a4,ntohs(alias_port));
	    } else {
/* Decompose alias port into pair format */
		ptr = (char *) &alias_port;
		p1 = *ptr++; p2=*ptr;

/* Generate PORT command string */
		sprintf(stemp, "PORT %d,%d,%d,%d,%d,%d\r\n",
			a1,a2,a3,a4,p1,p2);
	    }

/* Save string length for IP header modification */
            slen = strlen(stemp);

/* Copy into IP packet */
            sptr = (char *) pip; sptr += hlen;
            strncpy(sptr, stemp, maxpacketsize-hlen);
        }

/* Save information regarding modified seq and ack numbers */
        {
            int delta;

            SetAckModified(link);
            delta = GetDeltaSeqOut(pip, link);
            AddSeq(pip, link, delta+slen-dlen);
        }

/* Revise IP header */
        {
            u_short new_len;

            new_len = htons(hlen + slen);
            DifferentialChecksum(&pip->ip_sum,
                                 &new_len,
                                 &pip->ip_len,
                                 1);
            pip->ip_len = new_len;
        }

/* Compute TCP checksum for revised packet */
        tc->th_sum = 0;
        tc->th_sum = TcpChecksum(pip);
    }
    else
    {
#ifdef DEBUG
        fprintf(stderr,
        "PacketAlias/HandleFtpOut: Cannot allocate FTP data port\n");
#endif
    }
}
OpenPOWER on IntegriCloud