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