remove incorrect bound check. the check can fail on legal packet, at the
[obnox/wireshark/wip.git] / packet-tacacs.c
1 /* packet-tacacs.c
2  * Routines for cisco tacacs/tacplus/AAA packet dissection
3  *
4  * $Id: packet-tacacs.c,v 1.8 2000/11/19 08:54:09 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-tftp.c
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44
45 static int proto_tacacs = -1;
46 static int hf_tacacs_request = -1;
47 static int hf_tacacs_response = -1;
48 static int hf_tacacs_version = -1;
49
50 static gint ett_tacacs = -1;
51
52 #define UDP_PORT_TACACS 49
53 #define TCP_PORT_TACACS 49
54
55 static void
56 dissect_tacacs(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
57 {
58         proto_tree      *tacacs_tree, *ti;
59
60         OLD_CHECK_DISPLAY_AS_DATA(proto_tacacs, pd, offset, fd, tree);
61
62         if (check_col(fd, COL_PROTOCOL))
63                 col_set_str(fd, COL_PROTOCOL, "TACACS");
64
65         if (check_col(fd, COL_INFO))
66         {
67                 col_add_fstr(fd, COL_INFO, "%s", 
68                         (pi.match_port == pi.destport) ? "Request" : "Response");         
69         }
70
71         if (tree) 
72         {
73                 ti = proto_tree_add_item(tree, proto_tacacs, NullTVB, offset, END_OF_FRAME, FALSE);
74                 tacacs_tree = proto_item_add_subtree(ti, ett_tacacs);
75
76                 proto_tree_add_string(tacacs_tree, hf_tacacs_version, NullTVB, 0, 0, "XTacacs");
77
78                 if (pi.match_port == pi.destport)
79                 {
80                         proto_tree_add_boolean_hidden(tacacs_tree, hf_tacacs_request, NullTVB,
81                                                    offset, END_OF_FRAME, TRUE);
82                         proto_tree_add_text(tacacs_tree, NullTVB, offset, 
83                                 END_OF_FRAME, "Request: <opaque data>" );
84                 }
85                 else
86                 {
87                         proto_tree_add_boolean_hidden(tacacs_tree, hf_tacacs_response, NullTVB,
88                                                    offset, END_OF_FRAME, TRUE);
89                         proto_tree_add_text(tacacs_tree, NullTVB, offset, 
90                                 END_OF_FRAME, "Response: <opaque data>");
91                 }
92         }
93 }
94
95 static void
96 dissect_tacplus(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
97 {
98         proto_tree      *tacacs_tree, *ti;
99
100         if (check_col(fd, COL_PROTOCOL))
101         col_set_str(fd, COL_PROTOCOL, "TACACS");
102
103         if (check_col(fd, COL_INFO))
104         {
105                 col_add_fstr(fd, COL_INFO, "%s", 
106                         (pi.match_port == pi.destport) ? "Request" : "Response");         
107         }
108
109         if (tree) 
110         {
111                 ti = proto_tree_add_item(tree, proto_tacacs, NullTVB, offset, END_OF_FRAME, FALSE);
112                 tacacs_tree = proto_item_add_subtree(ti, ett_tacacs);
113
114                 proto_tree_add_string(tacacs_tree, hf_tacacs_version, NullTVB, 0, 0, "Tacacs+");
115
116                 if (pi.match_port == pi.destport)
117                 {
118                         proto_tree_add_boolean_hidden(tacacs_tree, hf_tacacs_request, NullTVB,
119                                                    offset, END_OF_FRAME, TRUE);
120                         proto_tree_add_text(tacacs_tree, NullTVB, offset, 
121                                 END_OF_FRAME, "Request: <opaque data>" );
122                 }
123                 else
124                 {
125                         proto_tree_add_boolean_hidden(tacacs_tree, hf_tacacs_response, NullTVB,
126                                                    offset, END_OF_FRAME, TRUE);
127                         proto_tree_add_text(tacacs_tree, NullTVB, offset, 
128                                 END_OF_FRAME, "Response: <opaque data>");
129                 }
130         }
131 }
132
133 void
134 proto_register_tacacs(void)
135 {
136         static hf_register_info hf[] = {
137           { &hf_tacacs_version,
138             { "Tacacs Version",           "tacacs.version",
139               FT_STRING, BASE_NONE, NULL, 0x0,
140               "xtacacs or tacplus" }},
141           { &hf_tacacs_response,
142             { "Response",           "tacacs.response",
143               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
144               "TRUE if TACACS response" }},
145           { &hf_tacacs_request,
146             { "Request",            "tacacs.request",
147               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
148               "TRUE if TACACS request" }}
149         };
150
151         static gint *ett[] = {
152                 &ett_tacacs,
153         };
154         proto_tacacs = proto_register_protocol("TACACS", "tacacs");
155         proto_register_field_array(proto_tacacs, hf, array_length(hf));
156         proto_register_subtree_array(ett, array_length(ett));
157 }
158
159 void
160 proto_reg_handoff_tacacs(void)
161 {
162         old_dissector_add("udp.port", UDP_PORT_TACACS, dissect_tacacs);
163         old_dissector_add("tcp.port", TCP_PORT_TACACS, dissect_tacplus);
164 }