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