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