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