Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / packet-tacacs.c
1 /* packet-tacacs.c
2  * Routines for cisco tacacs/xtacacs/tacacs+ packet dissection
3  * Copyright 2001, Paul Ionescu <paul@acorp.ro>
4  *
5  * $Id: packet-tacacs.c,v 1.22 2002/08/02 23:36:03 jmayer Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from old packet-tacacs.c
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28
29 /* rfc-1492 for tacacs and xtacacs 
30  * draft-grant-tacacs-00.txt for tacacs+ (tacplus)
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <stdio.h>
38
39 #include <string.h>
40 #include <glib.h>
41 #include <epan/packet.h>
42
43 static int proto_tacacs = -1;
44 static int hf_tacacs_version = -1;
45 static int hf_tacacs_type = -1;
46 static int hf_tacacs_nonce = -1;
47 static int hf_tacacs_userlen = -1;
48 static int hf_tacacs_passlen = -1;
49 static int hf_tacacs_response = -1;
50 static int hf_tacacs_reason = -1;
51 static int hf_tacacs_result1 = -1;
52 static int hf_tacacs_destaddr = -1;
53 static int hf_tacacs_destport = -1;
54 static int hf_tacacs_line = -1;
55 static int hf_tacacs_result2 = -1;
56 static int hf_tacacs_result3 = -1;
57
58 static gint ett_tacacs = -1;
59
60 #define VERSION_TACACS  0x00
61 #define VERSION_XTACACS 0x80
62
63 static const value_string tacacs_version_vals[] = {
64         { VERSION_TACACS,  "TACACS" },
65         { VERSION_XTACACS, "XTACACS" },
66         { 0,               NULL }
67 };
68
69 #define TACACS_LOGIN            1
70 #define TACACS_RESPONSE         2
71 #define TACACS_CHANGE           3
72 #define TACACS_FOLLOW           4
73 #define TACACS_CONNECT          5
74 #define TACACS_SUPERUSER        6
75 #define TACACS_LOGOUT           7
76 #define TACACS_RELOAD           8
77 #define TACACS_SLIP_ON          9
78 #define TACACS_SLIP_OFF         10
79 #define TACACS_SLIP_ADDR        11
80 static const value_string tacacs_type_vals[] = {
81         { TACACS_LOGIN,     "Login" },
82         { TACACS_RESPONSE,  "Response" },
83         { TACACS_CHANGE,    "Change" },
84         { TACACS_FOLLOW,    "Follow" },
85         { TACACS_CONNECT,   "Connect" },
86         { TACACS_SUPERUSER, "Superuser" },
87         { TACACS_LOGOUT,    "Logout" },
88         { TACACS_RELOAD,    "Reload" },
89         { TACACS_SLIP_ON,   "SLIP on" },
90         { TACACS_SLIP_OFF,  "SLIP off" },
91         { TACACS_SLIP_ADDR, "SLIP Addr" },
92         { 0,                NULL }};    
93
94 static const value_string tacacs_reason_vals[] = {
95         { 0  , "none" },
96         { 1  , "expiring" },
97         { 2  , "password" },
98         { 3  , "denied" },
99         { 4  , "quit" },
100         { 5  , "idle" },
101         { 6  , "drop" },
102         { 7  , "bad" },
103         { 0  , NULL }
104 };
105
106 static const value_string tacacs_resp_vals[] = {
107         { 0  , "this is not a response" },
108         { 1  , "accepted" },
109         { 2  , "rejected" },
110         { 0  , NULL }
111 };
112
113 #define TAC_PLUS_AUTHEN 1
114 #define TAC_PLUS_AUTHOR 2
115 #define TAC_PLUS_ACCT   3
116
117 #define UDP_PORT_TACACS 49
118 #define TCP_PORT_TACACS 49
119
120 static void
121 dissect_tacacs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
122 {
123         proto_tree      *tacacs_tree;
124         proto_item      *ti;
125         guint8          txt_buff[256],version,type,userlen,passlen;
126
127         if (check_col(pinfo->cinfo, COL_PROTOCOL))
128                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TACACS");
129         if (check_col(pinfo->cinfo, COL_INFO))
130                 col_clear(pinfo->cinfo, COL_INFO);
131
132         version = tvb_get_guint8(tvb,0);
133         if (version != 0) {
134                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
135                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "XTACACS");
136         }
137
138         type = tvb_get_guint8(tvb,1);
139         if (check_col(pinfo->cinfo, COL_INFO))
140                 col_add_str(pinfo->cinfo, COL_INFO,
141                     val_to_str(type, tacacs_type_vals, "Unknown (0x%02x)"));
142
143         if (tree) 
144         {
145                 ti = proto_tree_add_protocol_format(tree, proto_tacacs,
146                  tvb, 0, -1, version==0?"TACACS":"XTACACS");
147                 tacacs_tree = proto_item_add_subtree(ti, ett_tacacs);
148
149                 proto_tree_add_uint(tacacs_tree, hf_tacacs_version, tvb, 0, 1,
150                     version);
151                 proto_tree_add_uint(tacacs_tree, hf_tacacs_type, tvb, 1, 1,
152                     type);
153                 proto_tree_add_item(tacacs_tree, hf_tacacs_nonce, tvb, 2, 2,
154                     FALSE);
155
156         if (version==0)
157             {
158             if (type!=TACACS_RESPONSE)
159                 {
160                 userlen=tvb_get_guint8(tvb,4);
161                 proto_tree_add_uint(tacacs_tree, hf_tacacs_userlen, tvb, 4, 1,
162                     userlen);
163                 passlen=tvb_get_guint8(tvb,5);
164                 proto_tree_add_uint(tacacs_tree, hf_tacacs_passlen, tvb, 5, 1,
165                     passlen);
166                 tvb_get_nstringz0(tvb,6,userlen,txt_buff);
167                 proto_tree_add_text(tacacs_tree, tvb, 6, userlen,         "Username: %s",txt_buff);
168                 tvb_get_nstringz0(tvb,6+userlen,passlen,txt_buff);
169                 proto_tree_add_text(tacacs_tree, tvb, 6+userlen, passlen, "Password: %s",txt_buff);
170                 }
171             else
172                 {
173                 proto_tree_add_item(tacacs_tree, hf_tacacs_response, tvb, 4, 1,
174                     FALSE);
175                 proto_tree_add_item(tacacs_tree, hf_tacacs_reason, tvb, 5, 1,
176                     FALSE);
177                 }
178             }
179         else
180             {
181             userlen=tvb_get_guint8(tvb,4);
182             proto_tree_add_uint(tacacs_tree, hf_tacacs_userlen, tvb, 4, 1,
183                 userlen);
184             passlen=tvb_get_guint8(tvb,5);
185             proto_tree_add_uint(tacacs_tree, hf_tacacs_passlen, tvb, 5, 1,
186                 passlen);
187             proto_tree_add_item(tacacs_tree, hf_tacacs_response, tvb, 6, 1,
188                 FALSE);
189             proto_tree_add_item(tacacs_tree, hf_tacacs_reason, tvb, 7, 1,
190                 FALSE);
191             proto_tree_add_item(tacacs_tree, hf_tacacs_result1, tvb, 8, 4,
192                 FALSE);
193             proto_tree_add_item(tacacs_tree, hf_tacacs_destaddr, tvb, 12, 4,
194                 FALSE);
195             proto_tree_add_item(tacacs_tree, hf_tacacs_destport, tvb, 16, 2,
196                 FALSE);
197             proto_tree_add_item(tacacs_tree, hf_tacacs_line, tvb, 18, 2,
198                 FALSE);
199             proto_tree_add_item(tacacs_tree, hf_tacacs_result2, tvb, 20, 4,
200                 FALSE);
201             proto_tree_add_item(tacacs_tree, hf_tacacs_result3, tvb, 24, 2,
202                 FALSE);
203             if (type!=TACACS_RESPONSE)
204                 {
205                 tvb_get_nstringz0(tvb,26,userlen,txt_buff);
206                 proto_tree_add_text(tacacs_tree, tvb, 26, userlen,  "Username: %s",txt_buff);
207                 tvb_get_nstringz0(tvb,26+userlen,passlen,txt_buff);
208                 proto_tree_add_text(tacacs_tree, tvb, 26+userlen, passlen, "Password; %s",txt_buff);
209                 }
210             }
211         }
212 }
213
214 void
215 proto_register_tacacs(void)
216 {
217         static hf_register_info hf[] = {
218           { &hf_tacacs_version,
219             { "Version",           "tacacs.version",
220               FT_UINT8, BASE_HEX, VALS(tacacs_version_vals), 0x0,
221               "Version", HFILL }},
222           { &hf_tacacs_type,
223             { "Type",              "tacacs.type",
224               FT_UINT8, BASE_DEC, VALS(tacacs_type_vals), 0x0,
225               "Type", HFILL }},
226           { &hf_tacacs_nonce,
227             { "Nonce",             "tacacs.nonce",
228               FT_UINT16, BASE_HEX, NULL, 0x0,
229               "Nonce", HFILL }},
230           { &hf_tacacs_userlen,
231             { "Username length",   "tacacs.userlen",
232               FT_UINT8, BASE_DEC, NULL, 0x0,
233               "Username length", HFILL }},
234           { &hf_tacacs_passlen,
235             { "Password length",   "tacacs.passlen",
236               FT_UINT8, BASE_DEC, NULL, 0x0,
237               "Password length", HFILL }},
238           { &hf_tacacs_response,
239             { "Response",          "tacacs.response",
240               FT_UINT8, BASE_DEC, VALS(tacacs_resp_vals), 0x0,
241               "Response", HFILL }},
242           { &hf_tacacs_reason,
243             { "Reason",            "tacacs.reason",
244               FT_UINT8, BASE_DEC, VALS(tacacs_reason_vals), 0x0,
245               "Reason", HFILL }},
246           { &hf_tacacs_result1,
247             { "Result 1",          "tacacs.result1",
248               FT_UINT32, BASE_HEX, NULL, 0x0,
249               "Result 1", HFILL }},
250           { &hf_tacacs_destaddr,
251             { "Destination address", "tacacs.destaddr",
252               FT_IPv4, BASE_NONE, NULL, 0x0,
253               "Destination address", HFILL }},
254           { &hf_tacacs_destport,
255             { "Destination port",  "tacacs.destport",
256               FT_UINT16, BASE_DEC, NULL, 0x0,
257               "Destination port", HFILL }},
258           { &hf_tacacs_line,
259             { "Line",              "tacacs.line",
260               FT_UINT16, BASE_DEC, NULL, 0x0,
261               "Line", HFILL }},
262           { &hf_tacacs_result2,
263             { "Result 2",          "tacacs.result2",
264               FT_UINT32, BASE_HEX, NULL, 0x0,
265               "Result 2", HFILL }},
266           { &hf_tacacs_result3,
267             { "Result 3",          "tacacs.result3",
268               FT_UINT16, BASE_HEX, NULL, 0x0,
269               "Result 3", HFILL }},
270         };
271
272         static gint *ett[] = {
273                 &ett_tacacs,
274         };
275         proto_tacacs = proto_register_protocol("TACACS", "TACACS", "tacacs");
276         proto_register_field_array(proto_tacacs, hf, array_length(hf));
277         proto_register_subtree_array(ett, array_length(ett));
278 }
279
280 void
281 proto_reg_handoff_tacacs(void)
282 {
283         dissector_handle_t tacacs_handle;
284
285         tacacs_handle = create_dissector_handle(dissect_tacacs, proto_tacacs);
286         dissector_add("udp.port", UDP_PORT_TACACS, tacacs_handle);
287 }
288
289 static int proto_tacplus = -1;
290 static int hf_tacplus_response = -1;
291 static int hf_tacplus_request = -1;
292 static int hf_tacplus_majvers = -1;
293 static int hf_tacplus_minvers = -1;
294 static int hf_tacplus_type = -1;
295 static int hf_tacplus_seqno = -1;
296 static int hf_tacplus_flags = -1;
297 static int hf_tacplus_flags_payload_type = -1;
298 static int hf_tacplus_flags_connection_type = -1;
299 static int hf_tacplus_session_id = -1;
300 static int hf_tacplus_packet_len = -1;
301
302 static gint ett_tacplus = -1;
303 static gint ett_tacplus_flags = -1;
304
305 static const value_string tacplus_type_vals[] = {
306         { TAC_PLUS_AUTHEN  , "Authentication" },
307         { TAC_PLUS_AUTHOR  , "Authorization" },
308         { TAC_PLUS_ACCT    , "Accounting" },
309         { 0 , NULL }};
310
311 #define FLAGS_UNENCRYPTED       0x01
312
313 static const true_false_string payload_type = {
314   "Unencrypted",
315   "Encrypted"
316 };
317
318 #define FLAGS_SINGLE            0x04
319
320 static const true_false_string connection_type = {
321   "Single",
322   "Multiple"
323 };
324
325 static void
326 dissect_tacplus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
327 {
328         proto_tree      *tacplus_tree;
329         proto_item      *ti;
330         guint8          version,flags;
331         proto_tree      *flags_tree;
332         proto_item      *tf;
333         guint32         len;
334         gboolean        request=(pinfo->match_port == pinfo->destport);
335
336         if (check_col(pinfo->cinfo, COL_PROTOCOL))
337                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TACACS+");
338
339         if (check_col(pinfo->cinfo, COL_INFO))
340         {
341                 col_set_str(pinfo->cinfo, COL_INFO,
342                         request ? "Request" : "Response");        
343         }
344
345         if (tree) 
346         {
347                 ti = proto_tree_add_protocol_format(tree, proto_tacplus,
348                  tvb, 0, -1, "TACACS+");
349
350                 tacplus_tree = proto_item_add_subtree(ti, ett_tacplus);
351                 if (pinfo->match_port == pinfo->destport)
352                 {
353                         proto_tree_add_boolean_hidden(tacplus_tree,
354                             hf_tacplus_request, tvb, 0, 0, TRUE);
355                 }
356                 else
357                 {
358                         proto_tree_add_boolean_hidden(tacplus_tree,
359                             hf_tacplus_response, tvb, 0, 0, TRUE);
360                 }
361                 version = tvb_get_guint8(tvb,0);
362                 proto_tree_add_uint_format(tacplus_tree, hf_tacplus_majvers, tvb, 0, 1,
363                     version,
364                     "Major version: %s",
365                     (version&0xf0)==0xc0?"TACACS+":"Unknown Version");
366                 proto_tree_add_uint(tacplus_tree, hf_tacplus_minvers, tvb, 0, 1,
367                     version&0xf);
368                 proto_tree_add_item(tacplus_tree, hf_tacplus_type, tvb, 1, 1,
369                     FALSE);
370                 proto_tree_add_item(tacplus_tree, hf_tacplus_seqno, tvb, 2, 1,
371                     FALSE);
372                 flags = tvb_get_guint8(tvb,3);
373                 tf = proto_tree_add_uint_format(tacplus_tree, hf_tacplus_flags,
374                     tvb, 3, 1, flags,
375                     "Flags: %s, %s (0x%02x)",
376                     (flags&FLAGS_UNENCRYPTED) ? "Unencrypted payload" :
377                                                 "Encrypted payload",
378                     (flags&FLAGS_SINGLE) ? "Single connection" :
379                                            "Multiple Connections",
380                     flags);
381                 flags_tree = proto_item_add_subtree(tf, ett_tacplus_flags);
382                 proto_tree_add_boolean(flags_tree, hf_tacplus_flags_payload_type,
383                     tvb, 3, 1, flags);
384                 proto_tree_add_boolean(flags_tree, hf_tacplus_flags_connection_type,
385                     tvb, 3, 1, flags);
386                 proto_tree_add_item(tacplus_tree, hf_tacplus_session_id, tvb, 4, 4,
387                     FALSE);
388                 len = tvb_get_ntohl(tvb,8);
389                 proto_tree_add_uint(tacplus_tree, hf_tacplus_packet_len, tvb, 8, 4,
390                     len);
391
392                 if (flags&FLAGS_UNENCRYPTED)
393                         proto_tree_add_text(tacplus_tree, tvb, 12, len, "Payload");
394                 else
395                         proto_tree_add_text(tacplus_tree, tvb, 12, len, "Encrypted payload");
396         }
397 }
398
399 void
400 proto_register_tacplus(void)
401 {
402         static hf_register_info hf[] = {
403           { &hf_tacplus_response,
404             { "Response",           "tacplus.response",
405               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
406               "TRUE if TACACS+ response", HFILL }},
407           { &hf_tacplus_request,
408             { "Request",            "tacplus.request",
409               FT_BOOLEAN, BASE_NONE, NULL, 0x0,
410               "TRUE if TACACS+ request", HFILL }},
411           { &hf_tacplus_majvers,
412             { "Major version",      "tacplus.majvers",
413               FT_UINT8, BASE_DEC, NULL, 0x0,
414               "Major version number", HFILL }},
415           { &hf_tacplus_minvers,
416             { "Minor version",      "tacplus.minvers",
417               FT_UINT8, BASE_DEC, NULL, 0x0,
418               "Minor version number", HFILL }},
419           { &hf_tacplus_type,
420             { "Type",               "tacplus.type",
421               FT_UINT8, BASE_DEC, VALS(tacplus_type_vals), 0x0,
422               "Type", HFILL }},
423           { &hf_tacplus_seqno,
424             { "Sequence number",    "tacplus.seqno",
425               FT_UINT8, BASE_DEC, NULL, 0x0,
426               "Sequence number", HFILL }},
427           { &hf_tacplus_flags,
428             { "Flags",              "tacplus.flags",
429               FT_UINT8, BASE_HEX, NULL, 0x0,
430               "Flags", HFILL }},
431           { &hf_tacplus_flags_payload_type,
432             { "Payload type",       "tacplus.flags.payload_type",
433               FT_BOOLEAN, 8, TFS(&payload_type), FLAGS_UNENCRYPTED,
434               "Payload type (unencrypted or encrypted)", HFILL }},
435           { &hf_tacplus_flags_connection_type,
436             { "Connection type",    "tacplus.flags.connection_type",
437               FT_BOOLEAN, 8, TFS(&connection_type), FLAGS_SINGLE,
438               "Connection type (single or multiple)", HFILL }},
439           { &hf_tacplus_session_id,
440             { "Session ID",         "tacplus.session_id",
441               FT_UINT32, BASE_DEC, NULL, 0x0,
442               "Session ID", HFILL }},
443           { &hf_tacplus_packet_len,
444             { "Packet length",      "tacplus.packet_len",
445               FT_UINT32, BASE_DEC, NULL, 0x0,
446               "Packet length", HFILL }}
447         };
448
449         static gint *ett[] = {
450                 &ett_tacplus,
451                 &ett_tacplus_flags,
452         };
453         proto_tacplus = proto_register_protocol("TACACS+", "TACACS+", "tacplus");
454         proto_register_field_array(proto_tacplus, hf, array_length(hf));
455         proto_register_subtree_array(ett, array_length(ett));
456 }
457
458 void
459 proto_reg_handoff_tacplus(void)
460 {
461         dissector_handle_t tacplus_handle;
462
463         tacplus_handle = create_dissector_handle(dissect_tacplus,
464             proto_tacplus);
465         dissector_add("tcp.port", TCP_PORT_TACACS, tacplus_handle);
466 }