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