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