Fix some aclocal warnings during autogen.sh
[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.54 2003/10/08 11:33:19 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 static int hf_ftp_pasv_ip = -1 ;
52 static int hf_ftp_pasv_port = -1;
53 static int hf_ftp_pasv_nat = -1;
54 static int hf_ftp_active_ip = -1;
55 static int hf_ftp_active_port = -1;
56 static int hf_ftp_active_nat = -1;
57
58 static gint ett_ftp = -1;
59 static gint ett_ftp_reqresp = -1;
60 static gint ett_ftp_data = -1;
61
62 static dissector_handle_t ftpdata_handle;
63
64 #define TCP_PORT_FTPDATA                20
65 #define TCP_PORT_FTP                    21
66
67 static const value_string response_table[] = {
68         { 110, "Restart marker reply" },
69         { 120, "Service ready in nnn minutes" },
70         { 125, "Data connection already open; transfer starting" },
71         { 150, "File status okay; about to open data connection" },
72         { 200, "Command okay" },
73         { 202, "Command not implemented, superfluous at this site" },
74         { 211, "System status, or system help reply" },
75         { 212, "Directory status" },
76         { 213, "File status" },
77         { 214, "Help message" },
78         { 215, "NAME system type" },
79         { 220, "Service ready for new user" },
80         { 221, "Service closing control connection" },
81         { 225, "Data connection open; no transfer in progress" },
82         { 226, "Closing data connection" },
83         { 227, "Entering Passive Mode" },
84         { 230, "User logged in, proceed" },
85         { 250, "Requested file action okay, completed" },
86         { 257, "PATHNAME created" },
87         { 331, "User name okay, need password" },
88         { 332, "Need account for login" },
89         { 350, "Requested file action pending further information" },
90         { 421, "Service not available, closing control connection" },
91         { 425, "Can't open data connection" },
92         { 426, "Connection closed; transfer aborted" },
93         { 450, "Requested file action not taken" },
94         { 451, "Requested action aborted: local error in processing" },
95         { 452, "Requested action not taken. Insufficient storage space in system" },
96         { 500, "Syntax error, command unrecognized" },
97         { 501, "Syntax error in parameters or arguments" },
98         { 502, "Command not implemented" },
99         { 503, "Bad sequence of commands" },
100         { 504, "Command not implemented for that parameter" },
101         { 530, "Not logged in" },
102         { 532, "Need account for storing files" },
103         { 550, "Requested action not taken: File unavailable" },
104         { 551, "Requested action aborted: page type unknown" },
105         { 552, "Requested file action aborted: Exceeded storage allocation" },
106         { 553, "Requested action not taken: File name not allowed" },
107         { 0,   NULL }
108 };
109                 
110 static void
111 dissect_ftpdata(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
112
113 /*
114  * Parse the address and port information in a PORT command or in the
115  * response to a PASV command.  Return TRUE if we found an address and
116  * port, and supply the address and port; return FALSE if we didn't find
117  * them.
118  *
119  * We ignore the IP address in the reply, and use the address from which
120  * the request came.
121  *
122  * XXX - are there cases where they differ?  What if the FTP server is
123  * behind a NAT box, so that the address it puts into the reply isn't
124  * the address at which you should contact it?  Do all NAT boxes detect
125  * FTP PASV replies and rewrite the address?  (I suspect not.)
126  *
127  * RFC 959 doesn't say much about the syntax of the 227 reply.
128  *
129  * A proposal from Dan Bernstein at
130  *
131  *      http://cr.yp.to/ftp/retr.html
132  *
133  * "recommend[s] that clients use the following strategy to parse the
134  * response line: look for the first digit after the initial space; look
135  * for the fourth comma after that digit; read two (possibly negative)
136  * integers, separated by a comma; the TCP port number is p1*256+p2, where
137  * p1 is the first integer modulo 256 and p2 is the second integer modulo
138  * 256."
139  *
140  * wget 1.5.3 looks for a digit, although it doesn't handle negative
141  * integers.
142  *
143  * The FTP code in the source of the cURL library, at
144  *
145  *      http://curl.haxx.se/lxr/source/lib/ftp.c
146  *
147  * says that cURL "now scans for a sequence of six comma-separated numbers
148  * and will take them as IP+port indicators"; it loops, doing "sscanf"s
149  * looking for six numbers separated by commas, stepping the start pointer
150  * in the scanf one character at a time - i.e., it tries rather exhaustively.
151  *
152  * An optimization would be to scan for a digit, and start there, and if
153  * the scanf doesn't find six values, scan for the next digit and try
154  * again; this will probably succeed on the first try.
155  *
156  * The cURL code also says that "found reply-strings include":
157  *
158  *      "227 Entering Passive Mode (127,0,0,1,4,51)"
159  *      "227 Data transfer will passively listen to 127,0,0,1,4,51"
160  *      "227 Entering passive mode. 127,0,0,1,4,51"
161  *
162  * so it appears that you can't assume there are parentheses around
163  * the address and port number.
164  */
165 static gboolean
166 parse_port_pasv(const guchar *line, int linelen, guint32 *ftp_ip,
167     guint16 *ftp_port)
168 {
169         char *args;
170         char *p;
171         guchar c;
172         int i;
173         int address[4], port[2];
174         gboolean ret = FALSE;
175
176         /*
177          * Copy the rest of the line into a null-terminated buffer.
178          */
179         args = g_malloc(linelen + 1);
180         memcpy(args, line, linelen);
181         args[linelen] = '\0';
182         p = args;
183
184         for (;;) {
185                 /*
186                  * Look for a digit.
187                  */
188                 while ((c = *p) != '\0' && !isdigit(c))
189                         p++;
190
191                 if (*p == '\0') {
192                         /*
193                          * We ran out of text without finding anything.
194                          */
195                         break;
196                 }
197
198                 /*
199                  * See if we have six numbers.
200                  */
201                 i = sscanf(p, "%d,%d,%d,%d,%d,%d",
202                     &address[0], &address[1], &address[2], &address[3],
203                     &port[0], &port[1]);
204                 if (i == 6) {
205                         /*
206                          * We have a winner!
207                          */
208                         *ftp_port = ((port[0] & 0xFF)<<8) | (port[1] & 0xFF);
209                         *ftp_ip = g_htonl((address[0] << 24) | (address[1] <<16) | (address[2] <<8) | address[3]);
210                         ret = TRUE;
211                         break;
212                 }
213
214                 /*
215                  * Well, that didn't work.  Skip the first number we found,
216                  * and keep trying.
217                  */
218                 while ((c = *p) != '\0' && isdigit(c))
219                         p++;
220         }
221
222         g_free(args);
223         return ret;
224 }
225
226 static void
227 dissect_ftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
228 {
229         gboolean        is_request;
230         proto_tree      *ftp_tree = NULL;
231         proto_tree      *reqresp_tree = NULL;
232         proto_item      *ti;
233         gint            offset = 0;
234         const guchar    *line;
235         guint32         code;
236         gchar           code_str[4];
237         gboolean        is_port_request = FALSE;
238         gboolean        is_pasv_response = FALSE;
239         gint            next_offset;
240         int             linelen;
241         int             tokenlen;
242         const guchar    *next_token;
243         guint32         pasv_ip;
244         guint32         ftp_ip;
245         guint16         ftp_port;
246         address         ftp_ip_address;
247         gboolean        ftp_nat;
248         conversation_t  *conversation;
249
250         ftp_ip_address = pinfo->src;
251
252         if (pinfo->match_port == pinfo->destport)
253                 is_request = TRUE;
254         else
255                 is_request = FALSE;
256
257         if (check_col(pinfo->cinfo, COL_PROTOCOL))
258                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FTP");
259
260         /*
261          * Find the end of the first line.
262          *
263          * Note that "tvb_find_line_end()" will return a value that is
264          * not longer than what's in the buffer, so the "tvb_get_ptr()"
265          * call won't throw an exception.
266          */
267         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
268         line = tvb_get_ptr(tvb, offset, linelen);
269
270         if (check_col(pinfo->cinfo, COL_INFO)) {
271                 /*
272                  * Put the first line from the buffer into the summary
273                  * (but leave out the line terminator).
274                  */
275                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
276                     is_request ? "Request" : "Response",
277                     format_text(line, linelen));
278         }
279
280         if (tree) {
281                 ti = proto_tree_add_item(tree, proto_ftp, tvb, offset, -1,
282                     FALSE);
283                 ftp_tree = proto_item_add_subtree(ti, ett_ftp);
284
285                 if (is_request) {
286                         proto_tree_add_boolean_hidden(ftp_tree,
287                             hf_ftp_request, tvb, 0, 0, TRUE);
288                         proto_tree_add_boolean_hidden(ftp_tree,
289                             hf_ftp_response, tvb, 0, 0, FALSE);
290                 } else {
291                         proto_tree_add_boolean_hidden(ftp_tree,
292                             hf_ftp_request, tvb, 0, 0, FALSE);
293                         proto_tree_add_boolean_hidden(ftp_tree,
294                             hf_ftp_response, tvb, 0, 0, TRUE);
295                 }
296
297                 /*
298                  * Put the line into the protocol tree.
299                  */
300                 ti = proto_tree_add_text(ftp_tree, tvb, offset,
301                     next_offset - offset, "%s",
302                     tvb_format_text(tvb, offset, next_offset - offset));
303                 reqresp_tree = proto_item_add_subtree(ti, ett_ftp_reqresp);
304         }
305
306         if (is_request) {
307                 /*
308                  * Extract the first token, and, if there is a first
309                  * token, add it as the request.
310                  */
311                 tokenlen = get_token_len(line, line + linelen, &next_token);
312                 if (tokenlen != 0) {
313                         if (tree) {
314                                 proto_tree_add_item(reqresp_tree,
315                                     hf_ftp_request_command, tvb, offset,
316                                     tokenlen, FALSE);
317                         }
318                         if (strncmp(line, "PORT", tokenlen) == 0)
319                                 is_port_request = TRUE;
320                 }
321         } else {
322                 /*
323                  * This is a response; the response code is 3 digits,
324                  * followed by a space or hyphen, possibly followed by
325                  * text.
326                  *
327                  * If the line doesn't start with 3 digits, it's part of
328                  * a continuation.
329                  *
330                  * XXX - keep track of state in the first pass, and
331                  * treat non-continuation lines not beginning with digits
332                  * as errors?
333                  */
334                 if (linelen >= 3 && isdigit(line[0]) && isdigit(line[1])
335                     && isdigit(line[2])) {
336                         /*
337                          * One-line reply, or first or last line
338                          * of a multi-line reply.
339                          */
340                         tvb_get_nstringz0(tvb, offset, sizeof(code_str), code_str);
341                         code = strtoul(code_str, NULL, 10);
342                                 
343                         if (tree) {
344                                 proto_tree_add_uint(reqresp_tree,
345                                     hf_ftp_response_code, tvb, offset, 3, code);
346                         }
347
348                         /*
349                          * See if it's a passive-mode response.
350                          *
351                          * XXX - check for "229" responses to EPSV
352                          * commands, to handle IPv6, as per RFC 2428?
353                          *
354                          * XXX - does anybody do FOOBAR, as per RFC
355                          * 1639, or has that been supplanted by RFC 2428?
356                          */
357                         if (code == 227)
358                                 is_pasv_response = TRUE;
359
360                         /*
361                          * Skip the 3 digits and, if present, the
362                          * space or hyphen.
363                          */
364                         if (linelen >= 4)
365                                 next_token = line + 4;
366                         else
367                                 next_token = line + linelen;
368                 } else {
369                         /*
370                          * Line doesn't start with 3 digits; assume it's
371                          * a line in the middle of a multi-line reply.
372                          */
373                         next_token = line;
374                 }
375         }
376         offset += next_token - line;
377         linelen -= next_token - line;
378         line = next_token;
379
380         if (tree) {
381                 /*
382                  * Add the rest of the first line as request or
383                  * reply data.
384                  */
385                 if (linelen != 0) {
386                         if (is_request) {
387                                 proto_tree_add_item(reqresp_tree,
388                                     hf_ftp_request_arg, tvb, offset,
389                                     linelen, FALSE);
390                         } else {
391                                 proto_tree_add_item(reqresp_tree,
392                                     hf_ftp_response_arg, tvb, offset,
393                                     linelen, FALSE);
394                         }
395                 }
396                 offset = next_offset;
397         }
398
399         /*
400          * If this is a PORT request or a PASV response, handle it.
401          */
402         if (is_port_request) {
403                 if (parse_port_pasv(line, linelen, &ftp_ip,
404                     &ftp_port)) {
405                         if (tree) {
406                                 proto_tree_add_ipv4(reqresp_tree,
407                                     hf_ftp_active_ip, tvb, 0, 0,
408                                     ftp_ip);
409                                 proto_tree_add_uint(reqresp_tree,
410                                     hf_ftp_active_port, tvb, 0, 0,
411                                     ftp_port);
412                         }
413                         SET_ADDRESS(&ftp_ip_address, AT_IPv4, 4,
414                             (const guint8 *)&ftp_ip);
415                         ftp_nat = !ADDRESSES_EQUAL(&pinfo->src,
416                             &ftp_ip_address);
417                         if (ftp_nat) {
418                                 if (tree) {
419                                         proto_tree_add_boolean(
420                                             reqresp_tree,
421                                             hf_ftp_active_nat, tvb,
422                                             0, 0, ftp_nat);
423                                 }
424                         }
425                 }
426         }
427
428         if (is_pasv_response) {
429                 if (linelen != 0) {
430                         /*
431                          * This frame contains a PASV response; set up a
432                          * conversation for the data.
433                          */
434                         if (parse_port_pasv(line, linelen, &pasv_ip,
435                             &ftp_port)) {
436                                 if (tree) {
437                                         proto_tree_add_ipv4(reqresp_tree,
438                                             hf_ftp_pasv_ip, tvb, 0, 0, pasv_ip);
439                                         proto_tree_add_uint(reqresp_tree,
440                                             hf_ftp_pasv_port, tvb, 0, 0,
441                                             ftp_port);
442                                 }
443                                 SET_ADDRESS(&ftp_ip_address, AT_IPv4, 4,
444                                     (const guint8 *)&pasv_ip);
445                                 ftp_nat = !ADDRESSES_EQUAL(&pinfo->src,
446                                     &ftp_ip_address);
447                                 if (ftp_nat) {
448                                         if (tree) {
449                                                 proto_tree_add_boolean(reqresp_tree,
450                                                     hf_ftp_pasv_nat, tvb, 0, 0,
451                                                     ftp_nat);
452                                         }
453                                 }
454
455                                 /*
456                                  * We use "ftp_ip_address", so that if
457                                  * we're NAT'd we look for the un-NAT'd
458                                  * connection.
459                                  *
460                                  * XXX - should this call to
461                                  * "find_conversation()" just use
462                                  * "ftp_ip_address" and "server_port", and
463                                  * wildcard everything else?
464                                  */
465                                 conversation = find_conversation(&ftp_ip_address,
466                                     &pinfo->dst, PT_TCP, ftp_port, 0,
467                                     NO_PORT_B);
468                                 if (conversation == NULL) {
469                                         /*
470                                          * XXX - should this call to
471                                          * "conversation_new()" just use
472                                          * "ftp_ip_address" and "server_port",
473                                          * and wildcard everything else?
474                                          *
475                                          * XXX - what if we did find a
476                                          * conversation?  As we create it
477                                          * only on the first pass through
478                                          * the packets, if we find one, it's
479                                          * presumably an unrelated conversation.
480                                          * Should we remove the old one from
481                                          * the hash table and put this one in
482                                          * its place?  Can the conversation
483                                          * code handle conversations not in
484                                          * the hash table?  Or should we
485                                          * make conversations support
486                                          * start and end frames, as circuits
487                                          * do, and treat this as an indication
488                                          * that one conversation was closed
489                                          * and a new one was opened?
490                                          */
491                                         conversation = conversation_new(
492                                             &ftp_ip_address, &pinfo->dst,
493                                             PT_TCP, ftp_port, 0, NO_PORT2);
494                                         conversation_set_dissector(conversation,
495                                             ftpdata_handle);
496                                 }
497                         }
498                 }
499         }
500
501         if (tree) {
502                 /*
503                  * Show the rest of the request or response as text,
504                  * a line at a time.
505                  * XXX - only if there's a continuation indicator?
506                  */
507                 while (tvb_offset_exists(tvb, offset)) {
508                         /*
509                          * Find the end of the line.
510                          */
511                         linelen = tvb_find_line_end(tvb, offset, -1,
512                             &next_offset, FALSE);
513
514                         /*
515                          * Put this line.
516                          */
517                         proto_tree_add_text(ftp_tree, tvb, offset,
518                             next_offset - offset, "%s",
519                             tvb_format_text(tvb, offset, next_offset - offset));
520                         offset = next_offset;
521                 }
522         }
523 }
524
525 static void
526 dissect_ftpdata(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
527 {
528         proto_tree      *ti, *ftp_data_tree;
529         int             data_length;
530
531         if (check_col(pinfo->cinfo, COL_PROTOCOL))
532                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "FTP-DATA");
533
534         if (check_col(pinfo->cinfo, COL_INFO)) {
535                 col_add_fstr(pinfo->cinfo, COL_INFO, "FTP Data: %u bytes",
536                     tvb_reported_length(tvb));
537         }
538
539         if (tree) {
540                 data_length = tvb_length(tvb);
541
542                 ti = proto_tree_add_item(tree, proto_ftp_data, tvb, 0, -1,
543                     FALSE);
544                 ftp_data_tree = proto_item_add_subtree(ti, ett_ftp_data);
545
546                 /*
547                  * XXX - if this is binary data, it'll produce
548                  * a *really* long line.
549                  */
550                 proto_tree_add_text(ftp_data_tree, tvb, 0, data_length,
551                     "FTP Data: %s", tvb_format_text(tvb, 0, data_length));
552         }
553 }
554
555 void
556 proto_register_ftp(void)
557 {
558     static hf_register_info hf[] = {
559     { &hf_ftp_response,
560       { "Response",           "ftp.response",
561         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
562         "TRUE if FTP response", HFILL }},
563
564     { &hf_ftp_request,
565       { "Request",            "ftp.request",
566         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
567         "TRUE if FTP request", HFILL }},
568
569     { &hf_ftp_request_command,
570       { "Request command",    "ftp.request.command",
571         FT_STRING,  BASE_NONE, NULL, 0x0,
572         "", HFILL }},
573
574     { &hf_ftp_request_arg,
575       { "Request arg",        "ftp.request.arg",
576         FT_STRING,  BASE_NONE, NULL, 0x0,
577         "", HFILL }},
578
579     { &hf_ftp_response_code,
580       { "Response code",      "ftp.response.code",
581         FT_UINT32,   BASE_DEC, VALS(response_table), 0x0,
582         "", HFILL }},
583
584     { &hf_ftp_response_arg,
585       { "Response arg",      "ftp.response.arg",
586         FT_STRING,  BASE_NONE, NULL, 0x0,
587         "", HFILL }},
588
589     { &hf_ftp_pasv_ip,
590       { "Passive IP address", "ftp.passive.ip",
591         FT_IPv4, BASE_NONE, NULL,0x0, 
592         "Passive IP address (check NAT)", HFILL}},
593
594     { &hf_ftp_pasv_port,
595       { "Passive port", "ftp.passive.port",
596        FT_UINT16, BASE_DEC, NULL,0x0,
597         "Passive FTP server port", HFILL }},
598
599     { &hf_ftp_pasv_nat,
600       {"Passive IP NAT", "ftp.passive.nat",
601         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
602         "NAT is active SIP and passive IP different", HFILL }},
603
604     { &hf_ftp_active_ip,
605       { "Active IP address", "ftp.active.cip",
606         FT_IPv4, BASE_NONE, NULL, 0x0,
607         "Active FTP client IP address", HFILL }},
608
609     { &hf_ftp_active_port,
610       {"Active port", "ftp.active.port",
611         FT_UINT16, BASE_DEC, NULL, 0x0,
612         "Active FTP client port", HFILL }},
613
614     { &hf_ftp_active_nat,
615       { "Active IP NAT", "ftp.active.nat",
616         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
617         "NAT is active", HFILL}}
618
619   };
620   static gint *ett[] = {
621     &ett_ftp,
622     &ett_ftp_reqresp,
623     &ett_ftp_data,
624   };
625
626   proto_ftp = proto_register_protocol("File Transfer Protocol (FTP)", "FTP",
627                                       "ftp");
628   proto_ftp_data = proto_register_protocol("FTP Data", "FTP-DATA", "ftp-data");
629   proto_register_field_array(proto_ftp, hf, array_length(hf));
630   proto_register_subtree_array(ett, array_length(ett));
631
632   ftpdata_handle = create_dissector_handle(dissect_ftpdata, proto_ftp_data);
633 }
634
635 void
636 proto_reg_handoff_ftp(void)
637 {
638   dissector_handle_t ftpdata_handle, ftp_handle;
639
640   ftpdata_handle = create_dissector_handle(dissect_ftpdata, proto_ftp_data);
641   dissector_add("tcp.port", TCP_PORT_FTPDATA, ftpdata_handle);
642   ftp_handle = create_dissector_handle(dissect_ftp, proto_ftp);
643   dissector_add("tcp.port", TCP_PORT_FTP, ftp_handle);
644 }