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