Home | History | Annotate | Line # | Download | only in dist
print-telnet.c revision 1.2.12.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.1  christos #ifdef HAVE_CONFIG_H
     42       1.1  christos #include "config.h"
     43       1.1  christos #endif
     44       1.1  christos 
     45       1.2  christos #include <sys/cdefs.h>
     46       1.1  christos #ifndef lint
     47       1.2  christos #if 0
     48       1.1  christos static const char rcsid[] _U_ =
     49  1.2.12.1       tls      "@(#) Header: /tcpdump/master/tcpdump/print-telnet.c,v 1.24 2003-12-29 11:05:10 hannes Exp ";
     50       1.2  christos #else
     51       1.2  christos __RCSID("$NetBSD: print-telnet.c,v 1.2.12.1 2013/06/23 06:28:29 tls Exp $");
     52       1.2  christos #endif
     53       1.1  christos #endif
     54       1.1  christos 
     55       1.1  christos #include <tcpdump-stdinc.h>
     56       1.1  christos 
     57       1.1  christos #include <stdio.h>
     58       1.1  christos #include <stdlib.h>
     59       1.1  christos #include <string.h>
     60       1.1  christos 
     61       1.1  christos #include "interface.h"
     62       1.1  christos #include "addrtoname.h"
     63       1.1  christos 
     64       1.1  christos #define TELCMDS
     65       1.1  christos #define TELOPTS
     66       1.1  christos #include "telnet.h"
     67       1.1  christos 
     68       1.1  christos /* normal */
     69       1.1  christos static const char *cmds[] = {
     70       1.1  christos 	"IS", "SEND", "INFO",
     71       1.1  christos };
     72       1.1  christos 
     73       1.1  christos /* 37: Authentication */
     74       1.1  christos static const char *authcmd[] = {
     75       1.1  christos 	"IS", "SEND", "REPLY", "NAME",
     76       1.1  christos };
     77       1.1  christos static const char *authtype[] = {
     78       1.1  christos 	"NULL", "KERBEROS_V4", "KERBEROS_V5", "SPX", "MINK",
     79       1.1  christos 	"SRP", "RSA", "SSL", NULL, NULL,
     80       1.1  christos 	"LOKI", "SSA", "KEA_SJ", "KEA_SJ_INTEG", "DSS",
     81       1.1  christos 	"NTLM",
     82       1.1  christos };
     83       1.1  christos 
     84       1.1  christos /* 38: Encryption */
     85       1.1  christos static const char *enccmd[] = {
     86       1.1  christos 	"IS", "SUPPORT", "REPLY", "START", "END",
     87       1.1  christos 	"REQUEST-START", "REQUEST-END", "END_KEYID", "DEC_KEYID",
     88       1.1  christos };
     89       1.1  christos static const char *enctype[] = {
     90       1.1  christos 	"NULL", "DES_CFB64", "DES_OFB64", "DES3_CFB64", "DES3_OFB64",
     91       1.1  christos 	NULL, "CAST5_40_CFB64", "CAST5_40_OFB64", "CAST128_CFB64", "CAST128_OFB64",
     92       1.1  christos };
     93       1.1  christos 
     94       1.1  christos #define STR_OR_ID(x, tab) \
     95       1.1  christos 	(((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
     96       1.1  christos 
     97       1.1  christos static char *
     98       1.1  christos numstr(int x)
     99       1.1  christos {
    100       1.1  christos 	static char buf[20];
    101       1.1  christos 
    102       1.1  christos 	snprintf(buf, sizeof(buf), "%#x", x);
    103       1.1  christos 	return buf;
    104       1.1  christos }
    105       1.1  christos 
    106       1.1  christos /* sp points to IAC byte */
    107       1.1  christos static int
    108       1.1  christos telnet_parse(const u_char *sp, u_int length, int print)
    109       1.1  christos {
    110       1.1  christos 	int i, x;
    111       1.1  christos 	u_int c;
    112       1.1  christos 	const u_char *osp, *p;
    113       1.1  christos #define FETCH(c, sp, length) \
    114       1.1  christos 	do { \
    115       1.1  christos 		if (length < 1) \
    116       1.1  christos 			goto pktend; \
    117       1.1  christos 		TCHECK(*sp); \
    118       1.1  christos 		c = *sp++; \
    119       1.1  christos 		length--; \
    120       1.1  christos 	} while (0)
    121       1.1  christos 
    122       1.1  christos 	osp = sp;
    123       1.1  christos 
    124       1.1  christos 	FETCH(c, sp, length);
    125       1.1  christos 	if (c != IAC)
    126       1.1  christos 		goto pktend;
    127       1.1  christos 	FETCH(c, sp, length);
    128       1.1  christos 	if (c == IAC) {		/* <IAC><IAC>! */
    129       1.1  christos 		if (print)
    130       1.1  christos 			printf("IAC IAC");
    131       1.1  christos 		goto done;
    132       1.1  christos 	}
    133       1.1  christos 
    134       1.1  christos 	i = c - TELCMD_FIRST;
    135       1.1  christos 	if (i < 0 || i > IAC - TELCMD_FIRST)
    136       1.1  christos 		goto pktend;
    137       1.1  christos 
    138       1.1  christos 	switch (c) {
    139       1.1  christos 	case DONT:
    140       1.1  christos 	case DO:
    141       1.1  christos 	case WONT:
    142       1.1  christos 	case WILL:
    143       1.1  christos 	case SB:
    144       1.1  christos 		/* DONT/DO/WONT/WILL x */
    145       1.1  christos 		FETCH(x, sp, length);
    146       1.1  christos 		if (x >= 0 && x < NTELOPTS) {
    147       1.1  christos 			if (print)
    148       1.1  christos 				(void)printf("%s %s", telcmds[i], telopts[x]);
    149       1.1  christos 		} else {
    150       1.1  christos 			if (print)
    151       1.1  christos 				(void)printf("%s %#x", telcmds[i], x);
    152       1.1  christos 		}
    153       1.1  christos 		if (c != SB)
    154       1.1  christos 			break;
    155       1.1  christos 		/* IAC SB .... IAC SE */
    156       1.1  christos 		p = sp;
    157       1.1  christos 		while (length > (u_int)(p + 1 - sp)) {
    158       1.1  christos 			if (p[0] == IAC && p[1] == SE)
    159       1.1  christos 				break;
    160       1.1  christos 			p++;
    161       1.1  christos 		}
    162       1.1  christos 		if (*p != IAC)
    163       1.1  christos 			goto pktend;
    164       1.1  christos 
    165       1.1  christos 		switch (x) {
    166       1.1  christos 		case TELOPT_AUTHENTICATION:
    167       1.1  christos 			if (p <= sp)
    168       1.1  christos 				break;
    169       1.1  christos 			FETCH(c, sp, length);
    170       1.1  christos 			if (print)
    171       1.1  christos 				(void)printf(" %s", STR_OR_ID(c, authcmd));
    172       1.1  christos 			if (p <= sp)
    173       1.1  christos 				break;
    174       1.1  christos 			FETCH(c, sp, length);
    175       1.1  christos 			if (print)
    176       1.1  christos 				(void)printf(" %s", STR_OR_ID(c, authtype));
    177       1.1  christos 			break;
    178       1.1  christos 		case TELOPT_ENCRYPT:
    179       1.1  christos 			if (p <= sp)
    180       1.1  christos 				break;
    181       1.1  christos 			FETCH(c, sp, length);
    182       1.1  christos 			if (print)
    183       1.1  christos 				(void)printf(" %s", STR_OR_ID(c, enccmd));
    184       1.1  christos 			if (p <= sp)
    185       1.1  christos 				break;
    186       1.1  christos 			FETCH(c, sp, length);
    187       1.1  christos 			if (print)
    188       1.1  christos 				(void)printf(" %s", STR_OR_ID(c, enctype));
    189       1.1  christos 			break;
    190       1.1  christos 		default:
    191       1.1  christos 			if (p <= sp)
    192       1.1  christos 				break;
    193       1.1  christos 			FETCH(c, sp, length);
    194       1.1  christos 			if (print)
    195       1.1  christos 				(void)printf(" %s", STR_OR_ID(c, cmds));
    196       1.1  christos 			break;
    197       1.1  christos 		}
    198       1.1  christos 		while (p > sp) {
    199       1.1  christos 			FETCH(x, sp, length);
    200       1.1  christos 			if (print)
    201       1.1  christos 				(void)printf(" %#x", x);
    202       1.1  christos 		}
    203       1.1  christos 		/* terminating IAC SE */
    204       1.1  christos 		if (print)
    205       1.1  christos 			(void)printf(" SE");
    206       1.1  christos 		sp += 2;
    207       1.1  christos 		length -= 2;
    208       1.1  christos 		break;
    209       1.1  christos 	default:
    210       1.1  christos 		if (print)
    211       1.1  christos 			(void)printf("%s", telcmds[i]);
    212       1.1  christos 		goto done;
    213       1.1  christos 	}
    214       1.1  christos 
    215       1.1  christos done:
    216       1.1  christos 	return sp - osp;
    217       1.1  christos 
    218       1.1  christos trunc:
    219       1.1  christos 	(void)printf("[|telnet]");
    220       1.1  christos pktend:
    221       1.1  christos 	return -1;
    222       1.1  christos #undef FETCH
    223       1.1  christos }
    224       1.1  christos 
    225       1.1  christos void
    226       1.1  christos telnet_print(const u_char *sp, u_int length)
    227       1.1  christos {
    228       1.1  christos 	int first = 1;
    229       1.1  christos 	const u_char *osp;
    230       1.1  christos 	int l;
    231       1.1  christos 
    232       1.1  christos 	osp = sp;
    233       1.1  christos 
    234       1.1  christos 	while (length > 0 && *sp == IAC) {
    235       1.1  christos 		l = telnet_parse(sp, length, 0);
    236       1.1  christos 		if (l < 0)
    237       1.1  christos 			break;
    238       1.1  christos 
    239       1.1  christos 		/*
    240       1.1  christos 		 * now print it
    241       1.1  christos 		 */
    242       1.1  christos 		if (Xflag && 2 < vflag) {
    243       1.1  christos 			if (first)
    244       1.1  christos 				printf("\nTelnet:");
    245       1.1  christos 			hex_print_with_offset("\n", sp, l, sp - osp);
    246       1.1  christos 			if (l > 8)
    247       1.1  christos 				printf("\n\t\t\t\t");
    248       1.1  christos 			else
    249       1.1  christos 				printf("%*s\t", (8 - l) * 3, "");
    250       1.1  christos 		} else
    251       1.1  christos 			printf("%s", (first) ? " [telnet " : ", ");
    252       1.1  christos 
    253       1.1  christos 		(void)telnet_parse(sp, length, 1);
    254       1.1  christos 		first = 0;
    255       1.1  christos 
    256       1.1  christos 		sp += l;
    257       1.1  christos 		length -= l;
    258       1.1  christos 	}
    259       1.1  christos 	if (!first) {
    260       1.1  christos 		if (Xflag && 2 < vflag)
    261       1.1  christos 			printf("\n");
    262       1.1  christos 		else
    263       1.1  christos 			printf("]");
    264       1.1  christos 	}
    265       1.1  christos }
    266