]> The Tcpdump Group git mirrors - tcpdump/blob - print-telnet.c
improve ENCRYPT and AUTHENTICATION telnet negotiation printing.
[tcpdump] / print-telnet.c
1 /* $NetBSD: print-telnet.c,v 1.2 1999/10/11 12:40:12 sjg Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon J. Gerraty.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * @(#)Copyright (c) 1994, Simon J. Gerraty.
40 *
41 * This is free software. It comes with NO WARRANTY.
42 * Permission to use, modify and distribute this source code
43 * is granted subject to the following conditions.
44 * 1/ that the above copyright notice and this notice
45 * are preserved in all copies.
46 */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #ifndef lint
53 static const char rcsid[] =
54 "@(#) $Header: /tcpdump/master/tcpdump/print-telnet.c,v 1.16 2001-06-26 03:01:10 itojun Exp $";
55 #endif
56
57 #include <sys/param.h>
58 #include <sys/time.h>
59 #include <sys/types.h>
60 #include <ctype.h>
61
62 #include <netinet/in.h>
63
64 #define TELCMDS
65 #define TELOPTS
66 #include <arpa/telnet.h>
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <unistd.h>
71 #include <string.h>
72
73 #include "interface.h"
74 #include "addrtoname.h"
75
76 #ifndef TELCMD_FIRST
77 # define TELCMD_FIRST SE
78 #endif
79
80 /* normal */
81 static const char *cmds[] = {
82 "IS", "SEND", "INFO",
83 };
84
85 /* 37: Authentication */
86 static const char *authcmd[] = {
87 "IS", "SEND", "REPLY", "NAME",
88 };
89 static const char *authtype[] = {
90 "NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK",
91 "SRP", "RSA", "SSL", NULL, NULL,
92 "LOKI", "SSA", "KEA_SJ", "KEA_SJ_INTEG", "DSS",
93 "NTLM",
94 };
95
96 /* 38: Encryption */
97 static const char *enccmd[] = {
98 "IS", "SUPPORT", "REPLY", "START", "END",
99 "REQUEST-START", "REQUEST-END", "END_KEYID", "DEC_KEYID",
100 };
101 static const char *enctype[] = {
102 "NULL", "DES_CFB64", "DES_OFB64", "DES3_CFB64", "DES3_OFB64",
103 NULL, "CAST5_40_CFB64", "CAST5_40_OFB64", "CAST128_CFB64", "CAST128_OFB64",
104 };
105
106 #define STR_OR_ID(x, tab) \
107 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
108
109 static char *
110 numstr(int x)
111 {
112 static char buf[20];
113
114 snprintf(buf, sizeof(buf), "%#x", x);
115 return buf;
116 }
117
118 /* sp points to IAB byte */
119 static int
120 telnet_parse(const u_char *sp, u_int length, int print)
121 {
122 int i, c, x;
123 const u_char *osp, *p;
124 #define PEEK(c, sp, length) \
125 do { \
126 if (length < 1) \
127 goto trunc; \
128 c = *sp; \
129 } while (0)
130 #define FETCH(c, sp, length) \
131 do { \
132 PEEK((c), (sp), (length)); \
133 sp++; \
134 length--; \
135 } while (0)
136
137 osp = sp;
138
139 FETCH(c, sp, length);
140 if (c != IAC)
141 goto trunc;
142 FETCH(c, sp, length);
143 if (c == IAC) { /* <IAC><IAC>! */
144 if (print)
145 printf("IAC IAC");
146 goto done;
147 }
148
149 i = c - TELCMD_FIRST;
150 if (i < 0 || i > IAC - TELCMD_FIRST)
151 goto trunc;
152
153 switch (c) {
154 case DONT:
155 case DO:
156 case WONT:
157 case WILL:
158 case SB:
159 /* DONT/DO/WONT/WILL x */
160 FETCH(x, sp, length);
161 if (x >= 0 && x < NTELOPTS) {
162 if (print)
163 (void)printf("%s %s", telcmds[i], telopts[x]);
164 } else {
165 if (print)
166 (void)printf("%s %#x", telcmds[i], x);
167 }
168 if (c != SB)
169 break;
170 /* IAC SB .... IAC SE */
171 p = sp;
172 while (length > p + 1 - sp) {
173 if (p[0] == IAC && p[1] == SE)
174 break;
175 p++;
176 }
177 if (*p != IAC)
178 goto trunc;
179
180 switch (x) {
181 case TELOPT_AUTHENTICATION:
182 if (p <= sp)
183 break;
184 FETCH(c, sp, length);
185 if (print)
186 (void)printf(" %s", STR_OR_ID(c, authcmd));
187 if (p <= sp)
188 break;
189 FETCH(c, sp, length);
190 if (print)
191 (void)printf(" %s", STR_OR_ID(c, authtype));
192 break;
193 case TELOPT_ENCRYPT:
194 if (p <= sp)
195 break;
196 FETCH(c, sp, length);
197 if (print)
198 (void)printf(" %s", STR_OR_ID(c, enccmd));
199 if (p <= sp)
200 break;
201 FETCH(c, sp, length);
202 if (print)
203 (void)printf(" %s", STR_OR_ID(c, enctype));
204 break;
205 default:
206 if (p <= sp)
207 break;
208 FETCH(c, sp, length);
209 if (print)
210 (void)printf(" %s", STR_OR_ID(c, cmds));
211 break;
212 }
213 while (p > sp) {
214 FETCH(x, sp, length);
215 if (print)
216 (void)printf(" %#x", x);
217 }
218 /* terminating IAC SE */
219 if (print)
220 (void)printf(" SE");
221 sp += 2;
222 length -= 2;
223 break;
224 default:
225 if (print)
226 (void)printf("%s", telcmds[i]);
227 goto done;
228 }
229
230 done:
231 return sp - osp;
232
233 trunc:
234 return -1;
235 #undef PEEK
236 #undef FETCH
237 }
238
239 void
240 telnet_print(const u_char *sp, u_int length)
241 {
242 int first = 1;
243 const u_char *osp;
244 int l;
245
246 osp = sp;
247
248 while (length > 0 && *sp == IAC) {
249 l = telnet_parse(sp, length, 0);
250 if (l < 0)
251 break;
252
253 /*
254 * now print it
255 */
256 if (Xflag && 2 < vflag) {
257 if (first)
258 printf("\nTelnet:");
259 hex_print_with_offset(sp, l, sp - osp);
260 if (l > 8)
261 printf("\n\t\t\t\t");
262 else
263 printf("%*s\t", (8 - l) * 3, "");
264 } else
265 printf("%s", (first) ? " [telnet " : ", ");
266
267 (void)telnet_parse(sp, length, 1);
268 first = 0;
269
270 sp += l;
271 length -= l;
272 }
273 if (!first) {
274 if (Xflag && 2 < vflag)
275 printf("\n");
276 else
277 printf("]");
278 }
279 }