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