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