• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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[] _U_ =
54      "@(#) $Header: /tcpdump/master/tcpdump/print-telnet.c,v 1.24 2003/12/29 11:05:10 hannes 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 *
numstr(int x)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
telnet_parse(const u_char * sp,u_int length,int print)110 telnet_parse(const u_char *sp, u_int length, int print)
111 {
112 	int i, x;
113 	u_int c;
114 	const u_char *osp, *p;
115 #define FETCH(c, sp, length) \
116 	do { \
117 		if (length < 1) \
118 			goto pktend; \
119 		TCHECK(*sp); \
120 		c = *sp++; \
121 		length--; \
122 	} while (0)
123 
124 	osp = sp;
125 
126 	FETCH(c, sp, length);
127 	if (c != IAC)
128 		goto pktend;
129 	FETCH(c, sp, length);
130 	if (c == IAC) {		/* <IAC><IAC>! */
131 		if (print)
132 			printf("IAC IAC");
133 		goto done;
134 	}
135 
136 	i = c - TELCMD_FIRST;
137 	if (i < 0 || i > IAC - TELCMD_FIRST)
138 		goto pktend;
139 
140 	switch (c) {
141 	case DONT:
142 	case DO:
143 	case WONT:
144 	case WILL:
145 	case SB:
146 		/* DONT/DO/WONT/WILL x */
147 		FETCH(x, sp, length);
148 		if (x >= 0 && x < NTELOPTS) {
149 			if (print)
150 				(void)printf("%s %s", telcmds[i], telopts[x]);
151 		} else {
152 			if (print)
153 				(void)printf("%s %#x", telcmds[i], x);
154 		}
155 		if (c != SB)
156 			break;
157 		/* IAC SB .... IAC SE */
158 		p = sp;
159 		while (length > (u_int)(p + 1 - sp)) {
160 			if (p[0] == IAC && p[1] == SE)
161 				break;
162 			p++;
163 		}
164 		if (*p != IAC)
165 			goto pktend;
166 
167 		switch (x) {
168 		case TELOPT_AUTHENTICATION:
169 			if (p <= sp)
170 				break;
171 			FETCH(c, sp, length);
172 			if (print)
173 				(void)printf(" %s", STR_OR_ID(c, authcmd));
174 			if (p <= sp)
175 				break;
176 			FETCH(c, sp, length);
177 			if (print)
178 				(void)printf(" %s", STR_OR_ID(c, authtype));
179 			break;
180 		case TELOPT_ENCRYPT:
181 			if (p <= sp)
182 				break;
183 			FETCH(c, sp, length);
184 			if (print)
185 				(void)printf(" %s", STR_OR_ID(c, enccmd));
186 			if (p <= sp)
187 				break;
188 			FETCH(c, sp, length);
189 			if (print)
190 				(void)printf(" %s", STR_OR_ID(c, enctype));
191 			break;
192 		default:
193 			if (p <= sp)
194 				break;
195 			FETCH(c, sp, length);
196 			if (print)
197 				(void)printf(" %s", STR_OR_ID(c, cmds));
198 			break;
199 		}
200 		while (p > sp) {
201 			FETCH(x, sp, length);
202 			if (print)
203 				(void)printf(" %#x", x);
204 		}
205 		/* terminating IAC SE */
206 		if (print)
207 			(void)printf(" SE");
208 		sp += 2;
209 		length -= 2;
210 		break;
211 	default:
212 		if (print)
213 			(void)printf("%s", telcmds[i]);
214 		goto done;
215 	}
216 
217 done:
218 	return sp - osp;
219 
220 trunc:
221 	(void)printf("[|telnet]");
222 pktend:
223 	return -1;
224 #undef FETCH
225 }
226 
227 void
telnet_print(const u_char * sp,u_int length)228 telnet_print(const u_char *sp, u_int length)
229 {
230 	int first = 1;
231 	const u_char *osp;
232 	int l;
233 
234 	osp = sp;
235 
236 	while (length > 0 && *sp == IAC) {
237 		l = telnet_parse(sp, length, 0);
238 		if (l < 0)
239 			break;
240 
241 		/*
242 		 * now print it
243 		 */
244 		if (Xflag && 2 < vflag) {
245 			if (first)
246 				printf("\nTelnet:");
247 			hex_print_with_offset("\n", sp, l, sp - osp);
248 			if (l > 8)
249 				printf("\n\t\t\t\t");
250 			else
251 				printf("%*s\t", (8 - l) * 3, "");
252 		} else
253 			printf("%s", (first) ? " [telnet " : ", ");
254 
255 		(void)telnet_parse(sp, length, 1);
256 		first = 0;
257 
258 		sp += l;
259 		length -= l;
260 	}
261 	if (!first) {
262 		if (Xflag && 2 < vflag)
263 			printf("\n");
264 		else
265 			printf("]");
266 	}
267 }
268