Replace the types from sys/types.h and netinet/in.h by their glib.h
[obnox/wireshark/wip.git] / packet-ftp.c
1 /* packet-ftp.c
2  * Routines for ftp packet dissection
3  * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
4  * Copyright 2001, Juan Toledo <toledo@users.sourceforge.net> (Passive FTP)
5  * 
6  * $Id: packet-ftp.c,v 1.48 2002/08/02 23:35:49 jmayer Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from packet-pop.c
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36
37 #include <string.h>
38 #include <glib.h>
39 #include <epan/packet.h>
40 #include <epan/strutil.h>
41 #include <epan/conversation.h>
42
43 static int proto_ftp = -1;
44 static int proto_ftp_data = -1;
45 static int hf_ftp_response = -1;
46 static int hf_ftp_request = -1;
47 static int hf_ftp_request_command = -1;
48 static int hf_ftp_request_arg = -1;
49 static int hf_ftp_response_code = -1;
50 static int hf_ftp_response_arg = -1;
51
52 static gint ett_ftp = -1;
53 static gint ett_ftp_data = -1;
54
55 static dissector_handle_t ftpdata_handle;
56
57 #define TCP_PORT_FTPDATA                20
58 #define TCP_PORT_FTP                    21
59
60 static void
61 dissect_ftpdata(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
62
63 /*
64  * Handle a response to a PASV command.
65  *
66  * We ignore the IP address in the reply, and use the address from which
67  * the request came.
68  *
69  * XXX - are there cases where they differ?  What if the FTP server is
70  * behind a NAT box, so that the address it puts into the reply isn't
71  * the address at which you should contact it?  Do all NAT boxes detect
72  * FTP PASV replies and rewrite the address?  (I suspect not.)
73  *
74  * RFC 959 doesn't say much about the syntax of the 227 reply.
75  *
76  * A proposal from Dan Bernstein at
77  *
78  *      http://cr.yp.to/ftp/retr.html
79  *
80  * "recommend[s] that clients use the following strategy to parse the
81  * response line: look for the first digit after the initial space; look
82  * for the fourth comma after that digit; read two (possibly negative)
83  * integers, separated by a comma; the TCP port number is p1*256+p2, where
84  * p1 is the first integer modulo 256 and p2 is the second integer modulo
85  * 256."
86  *
87  * wget 1.5.3 looks for a digit, although it doesn't handle negative
88  * integers.
89  *
90  * The FTP code in the source of the cURL library, at
91  *
92  *      http://curl.haxx.se/lxr/source/lib/ftp.c
93  *
94  * says that cURL "now scans for a sequence of six comma-separated numbers
95  * and will take them as IP+port indicators"; it loops, doing "sscanf"s
96  * looking for six numbers separated by commas, stepping the start pointer
97  * in the scanf one character at a time - i.e., it tries rather exhaustively.
98  *
99  * An optimization would be to scan for a digit, and start there, and if
100  * the scanf doesn't find six values, scan for the next digit and try
101  * again; this will probably succeed on the first try.
102  *
103  * The cURL code also says that "found reply-strings include":
104  *
105  *      "227 Entering Passive Mode (127,0,0,1,4,51)"
106  *      "227 Data transfer will passively listen to 127,0,0,1,4,51"
107  *      "227 Entering passive mode. 127,0,0,1,4,51"
108  *
109  * so it appears that you can't assume there are parentheses around
110  * the address and port number.
111  */
112 static void
113 handle_pasv_response(const guchar *line, int linelen, packet_info *pinfo)
114 {
115         char *args;
116         char *p;
117         guchar c;
118         int i;
119         int address[4], port[2];
120         guint16 server_port;
121         conversation_t  *conversation;
122
123         /*
124          * Copy the rest of the line into a null-terminated buffer.
125          */
126         args = g_malloc(linelen + 1);
127         memcpy(args, line, linelen);
128         args[linelen] = '\0';
129         p = args;
130
131         for (;;) {
132                 /*
133                  * Look for a digit.
134                  */
135                 while ((c = *p) != '\0' && !isdigit(c))
136                         p++;
137
138                 if (*p == '\0') {
139                         /*
140                          * We ran out of text without finding anything.
141                          */
142                         break;
143                 }
144                         
145                 /*
146                  * See if we have six numbers.
147                  */
148                 i = sscanf(p, "%d,%d,%d,%d,%d,%d",
149                     &address[0], &address[1], &address[2], &address[3],
150                     &port[0], &port[1]);
151                 if (i == 6) {
152                         /*
153                          * We have a winner!
154                          * Set up a conversation, to be dissected as FTP data.
155                          */
156                         server_port = ((port[0] & 0xFF)<<8) | (port[1] & 0xFF);
157
158                         /*
159                          * XXX - should this call to "find_conversation()"
160                          * just use "pinfo->src" and "server_port", and
161                          * wildcard everything else?
162                          */
163                         conversation = find_conversation(&pinfo->src,
164                             &pinfo->dst, PT_TCP, server_port, 0, NO_PORT_B);
165                         if (conversation == NULL) {
166                                 /*
167                                  * XXX - should this call to
168                                  * "conversation_new()" just use "pinfo->src"
169                                  * and "server_port", and wildcard everything
170                                  * else?
171                                  *
172                                  * XXX - what if we did find a conversation?
173                                  * As we create it only on the first pass
174                                  * through the packets, if we find one, it's
175                                  * presumably an unrelated conversation.
176                                  * Should we remove the old one from the hash
177                                  * table and put this one in its place?
178                                  * Can the conversaton code handle
179                                  * conversations not in the hash table?
180                                  */
181                                 conversation = conversation_new(&pinfo->src,
182                                     &pinfo->dst, PT_TCP, server_port, 0,
183                                     NO_PORT2);
184                                 conversation_set_dissector(conversation,
185                                     ftpdata_handle);
186                         }
187                         break;
188                 }
189
190                 /*
191                  * Well, that didn't work.  Skip the first number we found,
192                  * and keep trying.
193                  */
194                 while ((c = *p) != '\0' && isdigit(c))
195                         p++;
196         }
197
198         g_free(args);
199 }       
200
201 static void
202 dissect_ftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
203 {
204         gboolean        is_request;
205         proto_tree      *ftp_tree = NULL;
206         proto_item      *ti;
207         gint            offset = 0;
208         const guchar    *line;
209         guint32         code;
210         gboolean        is_pasv_response = FALSE;
211         gint            next_offset;
212         int             linelen;
213         int             tokenlen;
214         const guchar    *next_token;
215
216         if (pinfo->match_port == pinfo->destport)
217                 is_request = TRUE;
218         else
219                 is_request = FALSE;
220
221         if (check_col(pinfo->cinfo, COL_PROTOCOL))
222                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FTP");
223
224         /*
225          * Find the end of the first line.
226          *
227          * Note that "tvb_find_line_end()" will return a value that is
228          * not longer than what's in the buffer, so the "tvb_get_ptr()"
229          * call won't throw an exception.
230          */
231         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
232         line = tvb_get_ptr(tvb, offset, linelen);
233
234         if (check_col(pinfo->cinfo, COL_INFO)) {
235                 /*
236                  * Put the first line from the buffer into the summary
237                  * (but leave out the line terminator).
238                  */
239                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
240                     is_request ? "Request" : "Response",
241                     format_text(line, linelen));
242         }
243    
244         if (tree) {
245                 ti = proto_tree_add_item(tree, proto_ftp, tvb, offset, -1,
246                     FALSE);
247                 ftp_tree = proto_item_add_subtree(ti, ett_ftp);
248
249                 if (is_request) {
250                         proto_tree_add_boolean_hidden(ftp_tree,
251                             hf_ftp_request, tvb, 0, 0, TRUE);
252                         proto_tree_add_boolean_hidden(ftp_tree,
253                             hf_ftp_response, tvb, 0, 0, FALSE);
254                 } else {
255                         proto_tree_add_boolean_hidden(ftp_tree,
256                             hf_ftp_request, tvb, 0, 0, FALSE);
257                         proto_tree_add_boolean_hidden(ftp_tree,
258                             hf_ftp_response, tvb, 0, 0, TRUE);
259                 }
260         }
261
262         if (is_request) {
263                 /*
264                  * Extract the first token, and, if there is a first
265                  * token, add it as the request.
266                  */
267                 tokenlen = get_token_len(line, line + linelen, &next_token);
268                 if (tokenlen != 0) {
269                         if (tree) {
270                                 proto_tree_add_item(ftp_tree,
271                                     hf_ftp_request_command, tvb, offset,
272                                     tokenlen, FALSE);
273                         }
274                 }
275         } else {
276                 /*
277                  * This is a response; the response code is 3 digits,
278                  * followed by a space or hyphen, possibly followed by
279                  * text.
280                  *
281                  * If the line doesn't start with 3 digits, it's part of
282                  * a continuation.
283                  *
284                  * XXX - keep track of state in the first pass, and
285                  * treat non-continuation lines not beginning with digits
286                  * as errors?
287                  */
288                 if (linelen >= 3 && isdigit(line[0]) && isdigit(line[1])
289                     && isdigit(line[2])) {
290                         /*
291                          * One-line reply, or first or last line
292                          * of a multi-line reply.
293                          */
294                         code = (line[0] - '0')*100 + (line[1] - '0')*10
295                             + (line[2] - '0');
296                         if (tree) {
297                                 proto_tree_add_uint(ftp_tree,
298                                     hf_ftp_response_code, tvb, offset, 3, code);
299
300                         }
301
302                         /*
303                          * See if it's a passive-mode response.
304                          *
305                          * XXX - check for "229" responses to EPSV
306                          * commands, to handle IPv6, as per RFC 2428?
307                          *
308                          * XXX - does anybody do FOOBAR, as per RFC
309                          * 1639, or has that been supplanted by RFC 2428?
310                          */
311                         if (code == 227)
312                                 is_pasv_response = TRUE;
313
314                         /*
315                          * Skip the 3 digits and, if present, the
316                          * space or hyphen.
317                          */
318                         if (linelen >= 4)
319                                 next_token = line + 4;
320                         else
321                                 next_token = line + linelen;
322                 } else {
323                         /*
324                          * Line doesn't start with 3 digits; assume it's
325                          * a line in the middle of a multi-line reply.
326                          */
327                         next_token = line;
328                 }
329         }
330         offset += next_token - line;
331         linelen -= next_token - line;
332         line = next_token;
333
334         /*
335          * If this is a PASV response, handle it if we haven't
336          * already processed this frame.
337          */
338         if (!pinfo->fd->flags.visited && is_pasv_response) {
339                 if (linelen != 0) {
340                         /*
341                          * We haven't processed this frame, and it contains
342                          * a PASV response; set up a conversation for the
343                          * data.
344                          */
345                         handle_pasv_response(line, linelen, pinfo);
346                 }
347         }
348
349         if (tree) {
350                 /*
351                  * Add the rest of the first line as request or
352                  * reply data.
353                  */
354                 if (linelen != 0) {
355                         if (is_request) {
356                                 proto_tree_add_item(ftp_tree,
357                                     hf_ftp_request_arg, tvb, offset,
358                                     linelen, FALSE);
359                         } else {
360                                 proto_tree_add_item(ftp_tree,
361                                     hf_ftp_response_arg, tvb, offset,
362                                     linelen, FALSE);
363                         }
364                 }
365                 offset = next_offset;
366
367                 /*
368                  * Show the rest of the request or response as text,
369                  * a line at a time.
370                  * XXX - only if there's a continuation indicator?
371                  */
372                 while (tvb_offset_exists(tvb, offset)) {
373                         /*
374                          * Find the end of the line.
375                          */
376                         linelen = tvb_find_line_end(tvb, offset, -1,
377                             &next_offset, FALSE);
378
379                         /*
380                          * Put this line.
381                          */
382                         proto_tree_add_text(ftp_tree, tvb, offset,
383                             next_offset - offset, "%s",
384                             tvb_format_text(tvb, offset, next_offset - offset));
385                         offset = next_offset;
386                 }
387         }
388 }
389
390 static void
391 dissect_ftpdata(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
392 {
393         proto_tree      *ti, *ftp_data_tree;
394         int             data_length;
395
396         if (check_col(pinfo->cinfo, COL_PROTOCOL))
397                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FTP-DATA");
398
399         if (check_col(pinfo->cinfo, COL_INFO)) {
400                 col_add_fstr(pinfo->cinfo, COL_INFO, "FTP Data: %u bytes",
401                     tvb_reported_length(tvb));
402         }
403
404         if (tree) {
405                 data_length = tvb_length(tvb);
406
407                 ti = proto_tree_add_item(tree, proto_ftp_data, tvb, 0, -1,
408                     FALSE);
409                 ftp_data_tree = proto_item_add_subtree(ti, ett_ftp_data);
410
411                 /*
412                  * XXX - if this is binary data, it'll produce
413                  * a *really* long line.
414                  */
415                 proto_tree_add_text(ftp_data_tree, tvb, 0, data_length,
416                     "FTP Data: %s", tvb_format_text(tvb, 0, data_length));
417         }
418 }
419
420 void
421 proto_register_ftp(void)
422 {
423     static hf_register_info hf[] = {
424     { &hf_ftp_response,
425       { "Response",           "ftp.response",
426         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
427         "TRUE if FTP response", HFILL }},
428
429     { &hf_ftp_request,
430       { "Request",            "ftp.request",
431         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
432         "TRUE if FTP request", HFILL }},
433
434     { &hf_ftp_request_command,
435       { "Request command",    "ftp.request.command",
436         FT_STRING,  BASE_NONE, NULL, 0x0,
437         "", HFILL }},
438
439     { &hf_ftp_request_arg,
440       { "Request arg",        "ftp.request.arg",
441         FT_STRING,  BASE_NONE, NULL, 0x0,
442         "", HFILL }},
443
444     { &hf_ftp_response_code,
445       { "Response code",      "ftp.response.code",
446         FT_UINT32,   BASE_DEC, NULL, 0x0,
447         "", HFILL }},
448
449     { &hf_ftp_response_arg,
450       { "Response arg",      "ftp.response.arg",
451         FT_STRING,  BASE_NONE, NULL, 0x0,
452         "", HFILL }}
453   };
454   static gint *ett[] = {
455     &ett_ftp,
456     &ett_ftp_data,
457   };
458
459   proto_ftp = proto_register_protocol("File Transfer Protocol (FTP)", "FTP",
460                                       "ftp");
461   proto_ftp_data = proto_register_protocol("FTP Data", "FTP-DATA", "ftp-data");
462   proto_register_field_array(proto_ftp, hf, array_length(hf));
463   proto_register_subtree_array(ett, array_length(ett));
464
465   ftpdata_handle = create_dissector_handle(dissect_ftpdata, proto_ftp_data);
466 }
467
468 void
469 proto_reg_handoff_ftp(void)
470 {
471   dissector_handle_t ftpdata_handle, ftp_handle;
472
473   ftpdata_handle = create_dissector_handle(dissect_ftpdata, proto_ftp_data);
474   dissector_add("tcp.port", TCP_PORT_FTPDATA, ftpdata_handle);
475   ftp_handle = create_dissector_handle(dissect_ftp, proto_ftp);
476   dissector_add("tcp.port", TCP_PORT_FTP, ftp_handle);
477 }