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