• 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 #define NETDISSECT_REWORKED
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #include <tcpdump-stdinc.h>
54 
55 #include <stdio.h>
56 
57 #include "interface.h"
58 
59 #define TELCMDS
60 #define TELOPTS
61 
62 /*	NetBSD: telnet.h,v 1.9 2001/06/11 01:50:50 wiz Exp 	*/
63 
64 /*
65  * Definitions for the TELNET protocol.
66  */
67 #define	IAC	255		/* interpret as command: */
68 #define	DONT	254		/* you are not to use option */
69 #define	DO	253		/* please, you use option */
70 #define	WONT	252		/* I won't use option */
71 #define	WILL	251		/* I will use option */
72 #define	SB	250		/* interpret as subnegotiation */
73 #define	GA	249		/* you may reverse the line */
74 #define	EL	248		/* erase the current line */
75 #define	EC	247		/* erase the current character */
76 #define	AYT	246		/* are you there */
77 #define	AO	245		/* abort output--but let prog finish */
78 #define	IP	244		/* interrupt process--permanently */
79 #define	BREAK	243		/* break */
80 #define	DM	242		/* data mark--for connect. cleaning */
81 #define	NOP	241		/* nop */
82 #define	SE	240		/* end sub negotiation */
83 #define EOR     239             /* end of record (transparent mode) */
84 #define	ABORT	238		/* Abort process */
85 #define	SUSP	237		/* Suspend process */
86 #define	xEOF	236		/* End of file: EOF is already used... */
87 
88 #define SYNCH	242		/* for telfunc calls */
89 
90 #ifdef TELCMDS
91 const char *telcmds[] = {
92 	"EOF", "SUSP", "ABORT", "EOR",
93 	"SE", "NOP", "DMARK", "BRK", "IP", "AO", "AYT", "EC",
94 	"EL", "GA", "SB", "WILL", "WONT", "DO", "DONT", "IAC", 0,
95 };
96 #else
97 extern char *telcmds[];
98 #endif
99 
100 #define	TELCMD_FIRST	xEOF
101 #define	TELCMD_LAST	IAC
102 #define	TELCMD_OK(x)	((unsigned int)(x) <= TELCMD_LAST && \
103 			 (unsigned int)(x) >= TELCMD_FIRST)
104 #define	TELCMD(x)	telcmds[(x)-TELCMD_FIRST]
105 
106 /* telnet options */
107 #define TELOPT_BINARY	0	/* 8-bit data path */
108 #define TELOPT_ECHO	1	/* echo */
109 #define	TELOPT_RCP	2	/* prepare to reconnect */
110 #define	TELOPT_SGA	3	/* suppress go ahead */
111 #define	TELOPT_NAMS	4	/* approximate message size */
112 #define	TELOPT_STATUS	5	/* give status */
113 #define	TELOPT_TM	6	/* timing mark */
114 #define	TELOPT_RCTE	7	/* remote controlled transmission and echo */
115 #define TELOPT_NAOL 	8	/* negotiate about output line width */
116 #define TELOPT_NAOP 	9	/* negotiate about output page size */
117 #define TELOPT_NAOCRD	10	/* negotiate about CR disposition */
118 #define TELOPT_NAOHTS	11	/* negotiate about horizontal tabstops */
119 #define TELOPT_NAOHTD	12	/* negotiate about horizontal tab disposition */
120 #define TELOPT_NAOFFD	13	/* negotiate about formfeed disposition */
121 #define TELOPT_NAOVTS	14	/* negotiate about vertical tab stops */
122 #define TELOPT_NAOVTD	15	/* negotiate about vertical tab disposition */
123 #define TELOPT_NAOLFD	16	/* negotiate about output LF disposition */
124 #define TELOPT_XASCII	17	/* extended ascic character set */
125 #define	TELOPT_LOGOUT	18	/* force logout */
126 #define	TELOPT_BM	19	/* byte macro */
127 #define	TELOPT_DET	20	/* data entry terminal */
128 #define	TELOPT_SUPDUP	21	/* supdup protocol */
129 #define	TELOPT_SUPDUPOUTPUT 22	/* supdup output */
130 #define	TELOPT_SNDLOC	23	/* send location */
131 #define	TELOPT_TTYPE	24	/* terminal type */
132 #define	TELOPT_EOR	25	/* end or record */
133 #define	TELOPT_TUID	26	/* TACACS user identification */
134 #define	TELOPT_OUTMRK	27	/* output marking */
135 #define	TELOPT_TTYLOC	28	/* terminal location number */
136 #define	TELOPT_3270REGIME 29	/* 3270 regime */
137 #define	TELOPT_X3PAD	30	/* X.3 PAD */
138 #define	TELOPT_NAWS	31	/* window size */
139 #define	TELOPT_TSPEED	32	/* terminal speed */
140 #define	TELOPT_LFLOW	33	/* remote flow control */
141 #define TELOPT_LINEMODE	34	/* Linemode option */
142 #define TELOPT_XDISPLOC	35	/* X Display Location */
143 #define TELOPT_OLD_ENVIRON 36	/* Old - Environment variables */
144 #define	TELOPT_AUTHENTICATION 37/* Authenticate */
145 #define	TELOPT_ENCRYPT	38	/* Encryption option */
146 #define TELOPT_NEW_ENVIRON 39	/* New - Environment variables */
147 #define	TELOPT_EXOPL	255	/* extended-options-list */
148 
149 
150 #define	NTELOPTS	(1+TELOPT_NEW_ENVIRON)
151 #ifdef TELOPTS
152 const char *telopts[NTELOPTS+1] = {
153 	"BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME",
154 	"STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
155 	"NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS",
156 	"NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO",
157 	"DATA ENTRY TERMINAL", "SUPDUP", "SUPDUP OUTPUT",
158 	"SEND LOCATION", "TERMINAL TYPE", "END OF RECORD",
159 	"TACACS UID", "OUTPUT MARKING", "TTYLOC",
160 	"3270 REGIME", "X.3 PAD", "NAWS", "TSPEED", "LFLOW",
161 	"LINEMODE", "XDISPLOC", "OLD-ENVIRON", "AUTHENTICATION",
162 	"ENCRYPT", "NEW-ENVIRON",
163 	0,
164 };
165 #define	TELOPT_FIRST	TELOPT_BINARY
166 #define	TELOPT_LAST	TELOPT_NEW_ENVIRON
167 #define	TELOPT_OK(x)	((unsigned int)(x) <= TELOPT_LAST)
168 #define	TELOPT(x)	telopts[(x)-TELOPT_FIRST]
169 #endif
170 
171 /* sub-option qualifiers */
172 #define	TELQUAL_IS	0	/* option is... */
173 #define	TELQUAL_SEND	1	/* send option */
174 #define	TELQUAL_INFO	2	/* ENVIRON: informational version of IS */
175 #define	TELQUAL_REPLY	2	/* AUTHENTICATION: client version of IS */
176 #define	TELQUAL_NAME	3	/* AUTHENTICATION: client version of IS */
177 
178 #define	LFLOW_OFF		0	/* Disable remote flow control */
179 #define	LFLOW_ON		1	/* Enable remote flow control */
180 #define	LFLOW_RESTART_ANY	2	/* Restart output on any char */
181 #define	LFLOW_RESTART_XON	3	/* Restart output only on XON */
182 
183 /*
184  * LINEMODE suboptions
185  */
186 
187 #define	LM_MODE		1
188 #define	LM_FORWARDMASK	2
189 #define	LM_SLC		3
190 
191 #define	MODE_EDIT	0x01
192 #define	MODE_TRAPSIG	0x02
193 #define	MODE_ACK	0x04
194 #define MODE_SOFT_TAB	0x08
195 #define MODE_LIT_ECHO	0x10
196 
197 #define	MODE_MASK	0x1f
198 
199 #define	SLC_SYNCH	1
200 #define	SLC_BRK		2
201 #define	SLC_IP		3
202 #define	SLC_AO		4
203 #define	SLC_AYT		5
204 #define	SLC_EOR		6
205 #define	SLC_ABORT	7
206 #define	SLC_EOF		8
207 #define	SLC_SUSP	9
208 #define	SLC_EC		10
209 #define	SLC_EL		11
210 #define	SLC_EW		12
211 #define	SLC_RP		13
212 #define	SLC_LNEXT	14
213 #define	SLC_XON		15
214 #define	SLC_XOFF	16
215 #define	SLC_FORW1	17
216 #define	SLC_FORW2	18
217 #define	SLC_MCL         19
218 #define	SLC_MCR         20
219 #define	SLC_MCWL        21
220 #define	SLC_MCWR        22
221 #define	SLC_MCBOL       23
222 #define	SLC_MCEOL       24
223 #define	SLC_INSRT       25
224 #define	SLC_OVER        26
225 #define	SLC_ECR         27
226 #define	SLC_EWR         28
227 #define	SLC_EBOL        29
228 #define	SLC_EEOL        30
229 
230 #define	NSLC		30
231 
232 /*
233  * For backwards compatibility, we define SLC_NAMES to be the
234  * list of names if SLC_NAMES is not defined.
235  */
236 #define	SLC_NAMELIST	"0", "SYNCH", "BRK", "IP", "AO", "AYT", "EOR",	\
237 			"ABORT", "EOF", "SUSP", "EC", "EL", "EW", "RP",	\
238 			"LNEXT", "XON", "XOFF", "FORW1", "FORW2",	\
239 			"MCL", "MCR", "MCWL", "MCWR", "MCBOL",		\
240 			"MCEOL", "INSRT", "OVER", "ECR", "EWR",		\
241 			"EBOL", "EEOL",					\
242 			0,
243 
244 #ifdef	SLC_NAMES
245 const char *slc_names[] = {
246 	SLC_NAMELIST
247 };
248 #else
249 extern char *slc_names[];
250 #define	SLC_NAMES SLC_NAMELIST
251 #endif
252 
253 #define	SLC_NAME_OK(x)	((unsigned int)(x) <= NSLC)
254 #define SLC_NAME(x)	slc_names[x]
255 
256 #define	SLC_NOSUPPORT	0
257 #define	SLC_CANTCHANGE	1
258 #define	SLC_VARIABLE	2
259 #define	SLC_DEFAULT	3
260 #define	SLC_LEVELBITS	0x03
261 
262 #define	SLC_FUNC	0
263 #define	SLC_FLAGS	1
264 #define	SLC_VALUE	2
265 
266 #define	SLC_ACK		0x80
267 #define	SLC_FLUSHIN	0x40
268 #define	SLC_FLUSHOUT	0x20
269 
270 #define	OLD_ENV_VAR	1
271 #define	OLD_ENV_VALUE	0
272 #define	NEW_ENV_VAR	0
273 #define	NEW_ENV_VALUE	1
274 #define	ENV_ESC		2
275 #define ENV_USERVAR	3
276 
277 /*
278  * AUTHENTICATION suboptions
279  */
280 
281 /*
282  * Who is authenticating who ...
283  */
284 #define	AUTH_WHO_CLIENT		0	/* Client authenticating server */
285 #define	AUTH_WHO_SERVER		1	/* Server authenticating client */
286 #define	AUTH_WHO_MASK		1
287 
288 #define	AUTHTYPE_NULL		0
289 #define	AUTHTYPE_KERBEROS_V4	1
290 #define	AUTHTYPE_KERBEROS_V5	2
291 #define	AUTHTYPE_SPX		3
292 #define	AUTHTYPE_MINK		4
293 #define	AUTHTYPE_CNT		5
294 
295 #define	AUTHTYPE_TEST		99
296 
297 #ifdef	AUTH_NAMES
298 const char *authtype_names[] = {
299 	"NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK", 0,
300 };
301 #else
302 extern char *authtype_names[];
303 #endif
304 
305 #define	AUTHTYPE_NAME_OK(x)	((unsigned int)(x) < AUTHTYPE_CNT)
306 #define	AUTHTYPE_NAME(x)	authtype_names[x]
307 
308 /*
309  * ENCRYPTion suboptions
310  */
311 #define	ENCRYPT_IS		0	/* I pick encryption type ... */
312 #define	ENCRYPT_SUPPORT		1	/* I support encryption types ... */
313 #define	ENCRYPT_REPLY		2	/* Initial setup response */
314 #define	ENCRYPT_START		3	/* Am starting to send encrypted */
315 #define	ENCRYPT_END		4	/* Am ending encrypted */
316 #define	ENCRYPT_REQSTART	5	/* Request you start encrypting */
317 #define	ENCRYPT_REQEND		6	/* Request you send encrypting */
318 #define	ENCRYPT_ENC_KEYID	7
319 #define	ENCRYPT_DEC_KEYID	8
320 #define	ENCRYPT_CNT		9
321 
322 #define	ENCTYPE_ANY		0
323 #define	ENCTYPE_DES_CFB64	1
324 #define	ENCTYPE_DES_OFB64	2
325 #define	ENCTYPE_CNT		3
326 
327 #ifdef	ENCRYPT_NAMES
328 const char *encrypt_names[] = {
329 	"IS", "SUPPORT", "REPLY", "START", "END",
330 	"REQUEST-START", "REQUEST-END", "ENC-KEYID", "DEC-KEYID",
331 	0,
332 };
333 const char *enctype_names[] = {
334 	"ANY", "DES_CFB64",  "DES_OFB64",  0,
335 };
336 #else
337 extern char *encrypt_names[];
338 extern char *enctype_names[];
339 #endif
340 
341 #define	ENCRYPT_NAME_OK(x)	((unsigned int)(x) < ENCRYPT_CNT)
342 #define	ENCRYPT_NAME(x)		encrypt_names[x]
343 
344 #define	ENCTYPE_NAME_OK(x)	((unsigned int)(x) < ENCTYPE_CNT)
345 #define	ENCTYPE_NAME(x)		enctype_names[x]
346 
347 /* normal */
348 static const char *cmds[] = {
349 	"IS", "SEND", "INFO",
350 };
351 
352 /* 37: Authentication */
353 static const char *authcmd[] = {
354 	"IS", "SEND", "REPLY", "NAME",
355 };
356 static const char *authtype[] = {
357 	"NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK",
358 	"SRP", "RSA", "SSL", NULL, NULL,
359 	"LOKI", "SSA", "KEA_SJ", "KEA_SJ_INTEG", "DSS",
360 	"NTLM",
361 };
362 
363 /* 38: Encryption */
364 static const char *enccmd[] = {
365 	"IS", "SUPPORT", "REPLY", "START", "END",
366 	"REQUEST-START", "REQUEST-END", "END_KEYID", "DEC_KEYID",
367 };
368 static const char *enctype[] = {
369 	"NULL", "DES_CFB64", "DES_OFB64", "DES3_CFB64", "DES3_OFB64",
370 	NULL, "CAST5_40_CFB64", "CAST5_40_OFB64", "CAST128_CFB64", "CAST128_OFB64",
371 };
372 
373 #define STR_OR_ID(x, tab) \
374 	(((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
375 
376 static char *
numstr(int x)377 numstr(int x)
378 {
379 	static char buf[20];
380 
381 	snprintf(buf, sizeof(buf), "%#x", x);
382 	return buf;
383 }
384 
385 /* sp points to IAC byte */
386 static int
telnet_parse(netdissect_options * ndo,const u_char * sp,u_int length,int print)387 telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
388 {
389 	int i, x;
390 	u_int c;
391 	const u_char *osp, *p;
392 #define FETCH(c, sp, length) \
393 	do { \
394 		if (length < 1) \
395 			goto pktend; \
396 		ND_TCHECK(*sp); \
397 		c = *sp++; \
398 		length--; \
399 	} while (0)
400 
401 	osp = sp;
402 
403 	FETCH(c, sp, length);
404 	if (c != IAC)
405 		goto pktend;
406 	FETCH(c, sp, length);
407 	if (c == IAC) {		/* <IAC><IAC>! */
408 		if (print)
409 			ND_PRINT((ndo, "IAC IAC"));
410 		goto done;
411 	}
412 
413 	i = c - TELCMD_FIRST;
414 	if (i < 0 || i > IAC - TELCMD_FIRST)
415 		goto pktend;
416 
417 	switch (c) {
418 	case DONT:
419 	case DO:
420 	case WONT:
421 	case WILL:
422 	case SB:
423 		/* DONT/DO/WONT/WILL x */
424 		FETCH(x, sp, length);
425 		if (x >= 0 && x < NTELOPTS) {
426 			if (print)
427 				ND_PRINT((ndo, "%s %s", telcmds[i], telopts[x]));
428 		} else {
429 			if (print)
430 				ND_PRINT((ndo, "%s %#x", telcmds[i], x));
431 		}
432 		if (c != SB)
433 			break;
434 		/* IAC SB .... IAC SE */
435 		p = sp;
436 		while (length > (u_int)(p + 1 - sp)) {
437 			if (p[0] == IAC && p[1] == SE)
438 				break;
439 			p++;
440 		}
441 		if (*p != IAC)
442 			goto pktend;
443 
444 		switch (x) {
445 		case TELOPT_AUTHENTICATION:
446 			if (p <= sp)
447 				break;
448 			FETCH(c, sp, length);
449 			if (print)
450 				ND_PRINT((ndo, " %s", STR_OR_ID(c, authcmd)));
451 			if (p <= sp)
452 				break;
453 			FETCH(c, sp, length);
454 			if (print)
455 				ND_PRINT((ndo, " %s", STR_OR_ID(c, authtype)));
456 			break;
457 		case TELOPT_ENCRYPT:
458 			if (p <= sp)
459 				break;
460 			FETCH(c, sp, length);
461 			if (print)
462 				ND_PRINT((ndo, " %s", STR_OR_ID(c, enccmd)));
463 			if (p <= sp)
464 				break;
465 			FETCH(c, sp, length);
466 			if (print)
467 				ND_PRINT((ndo, " %s", STR_OR_ID(c, enctype)));
468 			break;
469 		default:
470 			if (p <= sp)
471 				break;
472 			FETCH(c, sp, length);
473 			if (print)
474 				ND_PRINT((ndo, " %s", STR_OR_ID(c, cmds)));
475 			break;
476 		}
477 		while (p > sp) {
478 			FETCH(x, sp, length);
479 			if (print)
480 				ND_PRINT((ndo, " %#x", x));
481 		}
482 		/* terminating IAC SE */
483 		if (print)
484 			ND_PRINT((ndo, " SE"));
485 		sp += 2;
486 		break;
487 	default:
488 		if (print)
489 			ND_PRINT((ndo, "%s", telcmds[i]));
490 		goto done;
491 	}
492 
493 done:
494 	return sp - osp;
495 
496 trunc:
497 	ND_PRINT((ndo, "[|telnet]"));
498 pktend:
499 	return -1;
500 #undef FETCH
501 }
502 
503 void
telnet_print(netdissect_options * ndo,const u_char * sp,u_int length)504 telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
505 {
506 	int first = 1;
507 	const u_char *osp;
508 	int l;
509 
510 	osp = sp;
511 
512 	while (length > 0 && *sp == IAC) {
513 		/*
514 		 * Parse the Telnet command without printing it,
515 		 * to determine its length.
516 		 */
517 		l = telnet_parse(ndo, sp, length, 0);
518 		if (l < 0)
519 			break;
520 
521 		/*
522 		 * now print it
523 		 */
524 		if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) {
525 			if (first)
526 				ND_PRINT((ndo, "\nTelnet:"));
527 			hex_print_with_offset(ndo, "\n", sp, l, sp - osp);
528 			if (l > 8)
529 				ND_PRINT((ndo, "\n\t\t\t\t"));
530 			else
531 				ND_PRINT((ndo, "%*s\t", (8 - l) * 3, ""));
532 		} else
533 			ND_PRINT((ndo, "%s", (first) ? " [telnet " : ", "));
534 
535 		(void)telnet_parse(ndo, sp, length, 1);
536 		first = 0;
537 
538 		sp += l;
539 		length -= l;
540 	}
541 	if (!first) {
542 		if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag)
543 			ND_PRINT((ndo, "\n"));
544 		else
545 			ND_PRINT((ndo, "]"));
546 	}
547 }
548