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