]> The Tcpdump Group git mirrors - tcpdump/blob - print-telnet.c
64b45299641089ea57e201c3993bb03bc966f8fe
[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.17 2001-06-26 15:19:41 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 #include <stdio.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <string.h>
68
69 #include "interface.h"
70 #include "addrtoname.h"
71
72 #define TELCMDS
73 #define TELOPTS
74 #include "telnet.h"
75
76 /* normal */
77 static const char *cmds[] = {
78 "IS", "SEND", "INFO",
79 };
80
81 /* 37: Authentication */
82 static const char *authcmd[] = {
83 "IS", "SEND", "REPLY", "NAME",
84 };
85 static const char *authtype[] = {
86 "NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK",
87 "SRP", "RSA", "SSL", NULL, NULL,
88 "LOKI", "SSA", "KEA_SJ", "KEA_SJ_INTEG", "DSS",
89 "NTLM",
90 };
91
92 /* 38: Encryption */
93 static const char *enccmd[] = {
94 "IS", "SUPPORT", "REPLY", "START", "END",
95 "REQUEST-START", "REQUEST-END", "END_KEYID", "DEC_KEYID",
96 };
97 static const char *enctype[] = {
98 "NULL", "DES_CFB64", "DES_OFB64", "DES3_CFB64", "DES3_OFB64",
99 NULL, "CAST5_40_CFB64", "CAST5_40_OFB64", "CAST128_CFB64", "CAST128_OFB64",
100 };
101
102 #define STR_OR_ID(x, tab) \
103 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
104
105 static char *
106 numstr(int x)
107 {
108 static char buf[20];
109
110 snprintf(buf, sizeof(buf), "%#x", x);
111 return buf;
112 }
113
114 /* sp points to IAB byte */
115 static int
116 telnet_parse(const u_char *sp, u_int length, int print)
117 {
118 int i, c, x;
119 const u_char *osp, *p;
120 #define PEEK(c, sp, length) \
121 do { \
122 if (length < 1) \
123 goto trunc; \
124 c = *sp; \
125 } while (0)
126 #define FETCH(c, sp, length) \
127 do { \
128 PEEK((c), (sp), (length)); \
129 sp++; \
130 length--; \
131 } while (0)
132
133 osp = sp;
134
135 FETCH(c, sp, length);
136 if (c != IAC)
137 goto trunc;
138 FETCH(c, sp, length);
139 if (c == IAC) { /* <IAC><IAC>! */
140 if (print)
141 printf("IAC IAC");
142 goto done;
143 }
144
145 i = c - TELCMD_FIRST;
146 if (i < 0 || i > IAC - TELCMD_FIRST)
147 goto trunc;
148
149 switch (c) {
150 case DONT:
151 case DO:
152 case WONT:
153 case WILL:
154 case SB:
155 /* DONT/DO/WONT/WILL x */
156 FETCH(x, sp, length);
157 if (x >= 0 && x < NTELOPTS) {
158 if (print)
159 (void)printf("%s %s", telcmds[i], telopts[x]);
160 } else {
161 if (print)
162 (void)printf("%s %#x", telcmds[i], x);
163 }
164 if (c != SB)
165 break;
166 /* IAC SB .... IAC SE */
167 p = sp;
168 while (length > p + 1 - sp) {
169 if (p[0] == IAC && p[1] == SE)
170 break;
171 p++;
172 }
173 if (*p != IAC)
174 goto trunc;
175
176 switch (x) {
177 case TELOPT_AUTHENTICATION:
178 if (p <= sp)
179 break;
180 FETCH(c, sp, length);
181 if (print)
182 (void)printf(" %s", STR_OR_ID(c, authcmd));
183 if (p <= sp)
184 break;
185 FETCH(c, sp, length);
186 if (print)
187 (void)printf(" %s", STR_OR_ID(c, authtype));
188 break;
189 case TELOPT_ENCRYPT:
190 if (p <= sp)
191 break;
192 FETCH(c, sp, length);
193 if (print)
194 (void)printf(" %s", STR_OR_ID(c, enccmd));
195 if (p <= sp)
196 break;
197 FETCH(c, sp, length);
198 if (print)
199 (void)printf(" %s", STR_OR_ID(c, enctype));
200 break;
201 default:
202 if (p <= sp)
203 break;
204 FETCH(c, sp, length);
205 if (print)
206 (void)printf(" %s", STR_OR_ID(c, cmds));
207 break;
208 }
209 while (p > sp) {
210 FETCH(x, sp, length);
211 if (print)
212 (void)printf(" %#x", x);
213 }
214 /* terminating IAC SE */
215 if (print)
216 (void)printf(" SE");
217 sp += 2;
218 length -= 2;
219 break;
220 default:
221 if (print)
222 (void)printf("%s", telcmds[i]);
223 goto done;
224 }
225
226 done:
227 return sp - osp;
228
229 trunc:
230 return -1;
231 #undef PEEK
232 #undef FETCH
233 }
234
235 void
236 telnet_print(const u_char *sp, u_int length)
237 {
238 int first = 1;
239 const u_char *osp;
240 int l;
241
242 osp = sp;
243
244 while (length > 0 && *sp == IAC) {
245 l = telnet_parse(sp, length, 0);
246 if (l < 0)
247 break;
248
249 /*
250 * now print it
251 */
252 if (Xflag && 2 < vflag) {
253 if (first)
254 printf("\nTelnet:");
255 hex_print_with_offset(sp, l, sp - osp);
256 if (l > 8)
257 printf("\n\t\t\t\t");
258 else
259 printf("%*s\t", (8 - l) * 3, "");
260 } else
261 printf("%s", (first) ? " [telnet " : ", ");
262
263 (void)telnet_parse(sp, length, 1);
264 first = 0;
265
266 sp += l;
267 length -= l;
268 }
269 if (!first) {
270 if (Xflag && 2 < vflag)
271 printf("\n");
272 else
273 printf("]");
274 }
275 }