123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <netdissect-stdinc.h>
- #include <stdio.h>
- #include "netdissect.h"
- static const char tstr[] = " [|telnet]";
- #define TELCMDS
- #define TELOPTS
- #define IAC 255
- #define DONT 254
- #define DO 253
- #define WONT 252
- #define WILL 251
- #define SB 250
- #define GA 249
- #define EL 248
- #define EC 247
- #define AYT 246
- #define AO 245
- #define IP 244
- #define BREAK 243
- #define DM 242
- #define NOP 241
- #define SE 240
- #define EOR 239
- #define ABORT 238
- #define SUSP 237
- #define xEOF 236
- #define SYNCH 242
- #ifdef TELCMDS
- static const char *telcmds[] = {
- "EOF", "SUSP", "ABORT", "EOR",
- "SE", "NOP", "DMARK", "BRK", "IP", "AO", "AYT", "EC",
- "EL", "GA", "SB", "WILL", "WONT", "DO", "DONT", "IAC", 0,
- };
- #else
- extern char *telcmds[];
- #endif
- #define TELCMD_FIRST xEOF
- #define TELCMD_LAST IAC
- #define TELCMD_OK(x) ((unsigned int)(x) <= TELCMD_LAST && \
- (unsigned int)(x) >= TELCMD_FIRST)
- #define TELCMD(x) telcmds[(x)-TELCMD_FIRST]
- #define TELOPT_BINARY 0
- #define TELOPT_ECHO 1
- #define TELOPT_RCP 2
- #define TELOPT_SGA 3
- #define TELOPT_NAMS 4
- #define TELOPT_STATUS 5
- #define TELOPT_TM 6
- #define TELOPT_RCTE 7
- #define TELOPT_NAOL 8
- #define TELOPT_NAOP 9
- #define TELOPT_NAOCRD 10
- #define TELOPT_NAOHTS 11
- #define TELOPT_NAOHTD 12
- #define TELOPT_NAOFFD 13
- #define TELOPT_NAOVTS 14
- #define TELOPT_NAOVTD 15
- #define TELOPT_NAOLFD 16
- #define TELOPT_XASCII 17
- #define TELOPT_LOGOUT 18
- #define TELOPT_BM 19
- #define TELOPT_DET 20
- #define TELOPT_SUPDUP 21
- #define TELOPT_SUPDUPOUTPUT 22
- #define TELOPT_SNDLOC 23
- #define TELOPT_TTYPE 24
- #define TELOPT_EOR 25
- #define TELOPT_TUID 26
- #define TELOPT_OUTMRK 27
- #define TELOPT_TTYLOC 28
- #define TELOPT_3270REGIME 29
- #define TELOPT_X3PAD 30
- #define TELOPT_NAWS 31
- #define TELOPT_TSPEED 32
- #define TELOPT_LFLOW 33
- #define TELOPT_LINEMODE 34
- #define TELOPT_XDISPLOC 35
- #define TELOPT_OLD_ENVIRON 36
- #define TELOPT_AUTHENTICATION 37
- #define TELOPT_ENCRYPT 38
- #define TELOPT_NEW_ENVIRON 39
- #define TELOPT_EXOPL 255
- #define NTELOPTS (1+TELOPT_NEW_ENVIRON)
- #ifdef TELOPTS
- static const char *telopts[NTELOPTS+1] = {
- "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME",
- "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
- "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS",
- "NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO",
- "DATA ENTRY TERMINAL", "SUPDUP", "SUPDUP OUTPUT",
- "SEND LOCATION", "TERMINAL TYPE", "END OF RECORD",
- "TACACS UID", "OUTPUT MARKING", "TTYLOC",
- "3270 REGIME", "X.3 PAD", "NAWS", "TSPEED", "LFLOW",
- "LINEMODE", "XDISPLOC", "OLD-ENVIRON", "AUTHENTICATION",
- "ENCRYPT", "NEW-ENVIRON",
- 0,
- };
- #define TELOPT_FIRST TELOPT_BINARY
- #define TELOPT_LAST TELOPT_NEW_ENVIRON
- #define TELOPT_OK(x) ((unsigned int)(x) <= TELOPT_LAST)
- #define TELOPT(x) telopts[(x)-TELOPT_FIRST]
- #endif
- #define TELQUAL_IS 0
- #define TELQUAL_SEND 1
- #define TELQUAL_INFO 2
- #define TELQUAL_REPLY 2
- #define TELQUAL_NAME 3
- #define LFLOW_OFF 0
- #define LFLOW_ON 1
- #define LFLOW_RESTART_ANY 2
- #define LFLOW_RESTART_XON 3
- #define LM_MODE 1
- #define LM_FORWARDMASK 2
- #define LM_SLC 3
- #define MODE_EDIT 0x01
- #define MODE_TRAPSIG 0x02
- #define MODE_ACK 0x04
- #define MODE_SOFT_TAB 0x08
- #define MODE_LIT_ECHO 0x10
- #define MODE_MASK 0x1f
- #define SLC_SYNCH 1
- #define SLC_BRK 2
- #define SLC_IP 3
- #define SLC_AO 4
- #define SLC_AYT 5
- #define SLC_EOR 6
- #define SLC_ABORT 7
- #define SLC_EOF 8
- #define SLC_SUSP 9
- #define SLC_EC 10
- #define SLC_EL 11
- #define SLC_EW 12
- #define SLC_RP 13
- #define SLC_LNEXT 14
- #define SLC_XON 15
- #define SLC_XOFF 16
- #define SLC_FORW1 17
- #define SLC_FORW2 18
- #define SLC_MCL 19
- #define SLC_MCR 20
- #define SLC_MCWL 21
- #define SLC_MCWR 22
- #define SLC_MCBOL 23
- #define SLC_MCEOL 24
- #define SLC_INSRT 25
- #define SLC_OVER 26
- #define SLC_ECR 27
- #define SLC_EWR 28
- #define SLC_EBOL 29
- #define SLC_EEOL 30
- #define NSLC 30
- #define SLC_NAMELIST "0", "SYNCH", "BRK", "IP", "AO", "AYT", "EOR", \
- "ABORT", "EOF", "SUSP", "EC", "EL", "EW", "RP", \
- "LNEXT", "XON", "XOFF", "FORW1", "FORW2", \
- "MCL", "MCR", "MCWL", "MCWR", "MCBOL", \
- "MCEOL", "INSRT", "OVER", "ECR", "EWR", \
- "EBOL", "EEOL", \
- 0,
- #ifdef SLC_NAMES
- const char *slc_names[] = {
- SLC_NAMELIST
- };
- #else
- extern char *slc_names[];
- #define SLC_NAMES SLC_NAMELIST
- #endif
- #define SLC_NAME_OK(x) ((unsigned int)(x) <= NSLC)
- #define SLC_NAME(x) slc_names[x]
- #define SLC_NOSUPPORT 0
- #define SLC_CANTCHANGE 1
- #define SLC_VARIABLE 2
- #define SLC_DEFAULT 3
- #define SLC_LEVELBITS 0x03
- #define SLC_FUNC 0
- #define SLC_FLAGS 1
- #define SLC_VALUE 2
- #define SLC_ACK 0x80
- #define SLC_FLUSHIN 0x40
- #define SLC_FLUSHOUT 0x20
- #define OLD_ENV_VAR 1
- #define OLD_ENV_VALUE 0
- #define NEW_ENV_VAR 0
- #define NEW_ENV_VALUE 1
- #define ENV_ESC 2
- #define ENV_USERVAR 3
- #define AUTH_WHO_CLIENT 0
- #define AUTH_WHO_SERVER 1
- #define AUTH_WHO_MASK 1
- #define AUTHTYPE_NULL 0
- #define AUTHTYPE_KERBEROS_V4 1
- #define AUTHTYPE_KERBEROS_V5 2
- #define AUTHTYPE_SPX 3
- #define AUTHTYPE_MINK 4
- #define AUTHTYPE_CNT 5
- #define AUTHTYPE_TEST 99
- #ifdef AUTH_NAMES
- const char *authtype_names[] = {
- "NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK", 0,
- };
- #else
- extern char *authtype_names[];
- #endif
- #define AUTHTYPE_NAME_OK(x) ((unsigned int)(x) < AUTHTYPE_CNT)
- #define AUTHTYPE_NAME(x) authtype_names[x]
- #define ENCRYPT_IS 0
- #define ENCRYPT_SUPPORT 1
- #define ENCRYPT_REPLY 2
- #define ENCRYPT_START 3
- #define ENCRYPT_END 4
- #define ENCRYPT_REQSTART 5
- #define ENCRYPT_REQEND 6
- #define ENCRYPT_ENC_KEYID 7
- #define ENCRYPT_DEC_KEYID 8
- #define ENCRYPT_CNT 9
- #define ENCTYPE_ANY 0
- #define ENCTYPE_DES_CFB64 1
- #define ENCTYPE_DES_OFB64 2
- #define ENCTYPE_CNT 3
- #ifdef ENCRYPT_NAMES
- const char *encrypt_names[] = {
- "IS", "SUPPORT", "REPLY", "START", "END",
- "REQUEST-START", "REQUEST-END", "ENC-KEYID", "DEC-KEYID",
- 0,
- };
- const char *enctype_names[] = {
- "ANY", "DES_CFB64", "DES_OFB64", 0,
- };
- #else
- extern char *encrypt_names[];
- extern char *enctype_names[];
- #endif
- #define ENCRYPT_NAME_OK(x) ((unsigned int)(x) < ENCRYPT_CNT)
- #define ENCRYPT_NAME(x) encrypt_names[x]
- #define ENCTYPE_NAME_OK(x) ((unsigned int)(x) < ENCTYPE_CNT)
- #define ENCTYPE_NAME(x) enctype_names[x]
- static const char *cmds[] = {
- "IS", "SEND", "INFO",
- };
- static const char *authcmd[] = {
- "IS", "SEND", "REPLY", "NAME",
- };
- static const char *authtype[] = {
- "NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK",
- "SRP", "RSA", "SSL", NULL, NULL,
- "LOKI", "SSA", "KEA_SJ", "KEA_SJ_INTEG", "DSS",
- "NTLM",
- };
- static const char *enccmd[] = {
- "IS", "SUPPORT", "REPLY", "START", "END",
- "REQUEST-START", "REQUEST-END", "END_KEYID", "DEC_KEYID",
- };
- static const char *enctype[] = {
- "NULL", "DES_CFB64", "DES_OFB64", "DES3_CFB64", "DES3_OFB64",
- NULL, "CAST5_40_CFB64", "CAST5_40_OFB64", "CAST128_CFB64", "CAST128_OFB64",
- };
- #define STR_OR_ID(x, tab) \
- (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
- static char *
- numstr(int x)
- {
- static char buf[20];
- snprintf(buf, sizeof(buf), "%#x", x);
- return buf;
- }
- static int
- telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
- {
- int i, x;
- u_int c;
- const u_char *osp, *p;
- #define FETCH(c, sp, length) \
- do { \
- if (length < 1) \
- goto pktend; \
- ND_TCHECK(*sp); \
- c = *sp++; \
- length--; \
- } while (0)
- osp = sp;
- FETCH(c, sp, length);
- if (c != IAC)
- goto pktend;
- FETCH(c, sp, length);
- if (c == IAC) {
- if (print)
- ND_PRINT((ndo, "IAC IAC"));
- goto done;
- }
- i = c - TELCMD_FIRST;
- if (i < 0 || i > IAC - TELCMD_FIRST)
- goto pktend;
- switch (c) {
- case DONT:
- case DO:
- case WONT:
- case WILL:
- case SB:
-
- FETCH(x, sp, length);
- if (x >= 0 && x < NTELOPTS) {
- if (print)
- ND_PRINT((ndo, "%s %s", telcmds[i], telopts[x]));
- } else {
- if (print)
- ND_PRINT((ndo, "%s %#x", telcmds[i], x));
- }
- if (c != SB)
- break;
-
- p = sp;
- while (length > (u_int)(p + 1 - sp)) {
- ND_TCHECK2(*p, 2);
- if (p[0] == IAC && p[1] == SE)
- break;
- p++;
- }
- ND_TCHECK(*p);
- if (*p != IAC)
- goto pktend;
- switch (x) {
- case TELOPT_AUTHENTICATION:
- if (p <= sp)
- break;
- FETCH(c, sp, length);
- if (print)
- ND_PRINT((ndo, " %s", STR_OR_ID(c, authcmd)));
- if (p <= sp)
- break;
- FETCH(c, sp, length);
- if (print)
- ND_PRINT((ndo, " %s", STR_OR_ID(c, authtype)));
- break;
- case TELOPT_ENCRYPT:
- if (p <= sp)
- break;
- FETCH(c, sp, length);
- if (print)
- ND_PRINT((ndo, " %s", STR_OR_ID(c, enccmd)));
- if (p <= sp)
- break;
- FETCH(c, sp, length);
- if (print)
- ND_PRINT((ndo, " %s", STR_OR_ID(c, enctype)));
- break;
- default:
- if (p <= sp)
- break;
- FETCH(c, sp, length);
- if (print)
- ND_PRINT((ndo, " %s", STR_OR_ID(c, cmds)));
- break;
- }
- while (p > sp) {
- FETCH(x, sp, length);
- if (print)
- ND_PRINT((ndo, " %#x", x));
- }
-
- if (print)
- ND_PRINT((ndo, " SE"));
- sp += 2;
- break;
- default:
- if (print)
- ND_PRINT((ndo, "%s", telcmds[i]));
- goto done;
- }
- done:
- return sp - osp;
- trunc:
- ND_PRINT((ndo, "%s", tstr));
- pktend:
- return -1;
- #undef FETCH
- }
- void
- telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
- {
- int first = 1;
- const u_char *osp;
- int l;
- osp = sp;
- ND_TCHECK(*sp);
- while (length > 0 && *sp == IAC) {
-
- l = telnet_parse(ndo, sp, length, 0);
- if (l < 0)
- break;
-
- if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) {
- if (first)
- ND_PRINT((ndo, "\nTelnet:"));
- hex_print_with_offset(ndo, "\n", sp, l, sp - osp);
- if (l > 8)
- ND_PRINT((ndo, "\n\t\t\t\t"));
- else
- ND_PRINT((ndo, "%*s\t", (8 - l) * 3, ""));
- } else
- ND_PRINT((ndo, "%s", (first) ? " [telnet " : ", "));
- (void)telnet_parse(ndo, sp, length, 1);
- first = 0;
- sp += l;
- length -= l;
- ND_TCHECK(*sp);
- }
- if (!first) {
- if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag)
- ND_PRINT((ndo, "\n"));
- else
- ND_PRINT((ndo, "]"));
- }
- return;
- trunc:
- ND_PRINT((ndo, "%s", tstr));
- }
|