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