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