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