2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/crypto/telnet/telnet/utilities.c,v 1.2.8.2 2002/04/13 10:59:08 markm Exp $");
39 static const char sccsid[] = "@(#)utilities.c 8.3 (Berkeley) 5/30/95";
45 #include <arpa/telnet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
63 #include <libtelnet/auth.h>
66 #include <libtelnet/encrypt.h>
69 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
75 * Upcase (in place) the argument.
79 upcase(char *argument)
83 while ((c = *argument) != 0) {
85 *argument = toupper(c);
94 * Compensate for differences in 4.2 and 4.3 systems.
98 SetSockOpt(int fd, int level, int option, int yesno)
100 return setsockopt(fd, level, option,
101 (char *)&yesno, sizeof yesno);
105 * The following are routines used to print out debugging information.
108 unsigned char NetTraceFile[256] = "(standard output)";
111 SetNetTrace(char *file)
113 if (NetTrace && NetTrace != stdout)
115 if (file && (strcmp(file, "-") != 0)) {
116 NetTrace = fopen(file, "w");
118 strcpy((char *)NetTraceFile, file);
121 fprintf(stderr, "Cannot open %s.\n", file);
124 strcpy((char *)NetTraceFile, "(standard output)");
128 Dump(char direction, unsigned char *buffer, int length)
130 # define BYTES_PER_LINE 32
131 # define min(x,y) ((x<y)? x:y)
132 unsigned char *pThis;
139 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
142 buffer = buffer + min(length, BYTES_PER_LINE/2);
143 while (pThis < buffer) {
144 fprintf(NetTrace, "%c%.2x",
145 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
149 length -= BYTES_PER_LINE/2;
150 offset += BYTES_PER_LINE/2;
152 buffer = buffer + min(length, BYTES_PER_LINE);
153 while (pThis < buffer) {
154 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
157 length -= BYTES_PER_LINE;
158 offset += BYTES_PER_LINE;
160 if (NetTrace == stdout) {
161 fprintf(NetTrace, "\r\n");
163 fprintf(NetTrace, "\n");
169 /* find next unique line */
176 printoption(const char *direction, int cmd, int option)
181 if (TELCMD_OK(option))
182 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
184 fprintf(NetTrace, "%s IAC %d", direction, option);
187 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
188 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
190 fprintf(NetTrace, "%s %s ", direction, fmt);
191 if (TELOPT_OK(option))
192 fprintf(NetTrace, "%s", TELOPT(option));
193 else if (option == TELOPT_EXOPL)
194 fprintf(NetTrace, "EXOPL");
196 fprintf(NetTrace, "%d", option);
198 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
200 if (NetTrace == stdout) {
201 fprintf(NetTrace, "\r\n");
204 fprintf(NetTrace, "\n");
213 extern char will_wont_resp[], do_dont_resp[];
215 for (i = 0; i < 256; i++) {
216 if (do_dont_resp[i]) {
218 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
219 else if (TELCMD_OK(i))
220 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
222 printf("resp DO_DONT %d: %d\n", i,
224 if (my_want_state_is_do(i)) {
226 printf("want DO %s\n", TELOPT(i));
227 else if (TELCMD_OK(i))
228 printf("want DO %s\n", TELCMD(i));
230 printf("want DO %d\n", i);
233 printf("want DONT %s\n", TELOPT(i));
234 else if (TELCMD_OK(i))
235 printf("want DONT %s\n", TELCMD(i));
237 printf("want DONT %d\n", i);
240 if (my_state_is_do(i)) {
242 printf(" DO %s\n", TELOPT(i));
243 else if (TELCMD_OK(i))
244 printf(" DO %s\n", TELCMD(i));
246 printf(" DO %d\n", i);
249 if (will_wont_resp[i]) {
251 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
252 else if (TELCMD_OK(i))
253 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
255 printf("resp WILL_WONT %d: %d\n",
256 i, will_wont_resp[i]);
257 if (my_want_state_is_will(i)) {
259 printf("want WILL %s\n", TELOPT(i));
260 else if (TELCMD_OK(i))
261 printf("want WILL %s\n", TELCMD(i));
263 printf("want WILL %d\n", i);
266 printf("want WONT %s\n", TELOPT(i));
267 else if (TELCMD_OK(i))
268 printf("want WONT %s\n", TELCMD(i));
270 printf("want WONT %d\n", i);
273 if (my_state_is_will(i)) {
275 printf(" WILL %s\n", TELOPT(i));
276 else if (TELCMD_OK(i))
277 printf(" WILL %s\n", TELCMD(i));
279 printf(" WILL %d\n", i);
287 printsub(char direction, unsigned char *pointer, int length)
290 #ifdef AUTHENTICATION
293 extern int want_status_response;
295 if (showoptions || direction == 0 ||
296 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
298 fprintf(NetTrace, "%s IAC SB ",
299 (direction == '<')? "RCVD":"SENT");
303 i = pointer[length-2];
304 j = pointer[length-1];
306 if (i != IAC || j != SE) {
307 fprintf(NetTrace, "(terminated by ");
309 fprintf(NetTrace, "%s ", TELOPT(i));
310 else if (TELCMD_OK(i))
311 fprintf(NetTrace, "%s ", TELCMD(i));
313 fprintf(NetTrace, "%d ", i);
315 fprintf(NetTrace, "%s", TELOPT(j));
316 else if (TELCMD_OK(j))
317 fprintf(NetTrace, "%s", TELCMD(j));
319 fprintf(NetTrace, "%d", j);
320 fprintf(NetTrace, ", not IAC SE!) ");
326 fprintf(NetTrace, "(Empty suboption??\?)");
327 if (NetTrace == stdout)
331 switch (pointer[0]) {
333 fprintf(NetTrace, "TERMINAL-TYPE ");
334 switch (pointer[1]) {
336 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
339 fprintf(NetTrace, "SEND");
343 "- unknown qualifier %d (0x%x).",
344 pointer[1], pointer[1]);
348 fprintf(NetTrace, "TERMINAL-SPEED");
350 fprintf(NetTrace, " (empty suboption??\?)");
353 switch (pointer[1]) {
355 fprintf(NetTrace, " IS ");
356 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
360 fprintf(NetTrace, " SEND");
362 fprintf(NetTrace, " %d (unknown)", pointer[1]);
363 for (i = 2; i < length; i++)
364 fprintf(NetTrace, " ?%d?", pointer[i]);
370 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
372 fprintf(NetTrace, " (empty suboption??\?)");
375 switch (pointer[1]) {
377 fprintf(NetTrace, " OFF"); break;
379 fprintf(NetTrace, " ON"); break;
380 case LFLOW_RESTART_ANY:
381 fprintf(NetTrace, " RESTART-ANY"); break;
382 case LFLOW_RESTART_XON:
383 fprintf(NetTrace, " RESTART-XON"); break;
385 fprintf(NetTrace, " %d (unknown)", pointer[1]);
387 for (i = 2; i < length; i++)
388 fprintf(NetTrace, " ?%d?", pointer[i]);
392 fprintf(NetTrace, "NAWS");
394 fprintf(NetTrace, " (empty suboption??\?)");
398 fprintf(NetTrace, " ?%d?", pointer[1]);
401 fprintf(NetTrace, " %d %d (%d)",
402 pointer[1], pointer[2],
403 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
405 fprintf(NetTrace, " ?%d?", pointer[3]);
408 fprintf(NetTrace, " %d %d (%d)",
409 pointer[3], pointer[4],
410 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
411 for (i = 5; i < length; i++)
412 fprintf(NetTrace, " ?%d?", pointer[i]);
415 #ifdef AUTHENTICATION
416 case TELOPT_AUTHENTICATION:
417 fprintf(NetTrace, "AUTHENTICATION");
419 fprintf(NetTrace, " (empty suboption??\?)");
422 switch (pointer[1]) {
425 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
427 if (AUTHTYPE_NAME_OK(pointer[2]))
428 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
430 fprintf(NetTrace, "%d ", pointer[2]);
432 fprintf(NetTrace, "(partial suboption??\?)");
435 fprintf(NetTrace, "%s|%s",
436 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
438 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
439 "MUTUAL" : "ONE-WAY");
441 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
442 fprintf(NetTrace, "%s", buf);
447 fprintf(NetTrace, " SEND ");
449 if (AUTHTYPE_NAME_OK(pointer[i]))
450 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
452 fprintf(NetTrace, "%d ", pointer[i]);
454 fprintf(NetTrace, "(partial suboption??\?)");
457 fprintf(NetTrace, "%s|%s ",
458 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
460 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
461 "MUTUAL" : "ONE-WAY");
468 fprintf(NetTrace, " NAME \"");
470 putc(pointer[i++], NetTrace);
475 for (i = 2; i < length; i++)
476 fprintf(NetTrace, " ?%d?", pointer[i]);
484 fprintf(NetTrace, "ENCRYPT");
486 fprintf(NetTrace, " (empty suboption??\?)");
489 switch (pointer[1]) {
491 fprintf(NetTrace, " START");
495 fprintf(NetTrace, " END");
498 case ENCRYPT_REQSTART:
499 fprintf(NetTrace, " REQUEST-START");
503 fprintf(NetTrace, " REQUEST-END");
508 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
511 fprintf(NetTrace, " (partial suboption??\?)");
514 if (ENCTYPE_NAME_OK(pointer[2]))
515 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
517 fprintf(NetTrace, " %d (unknown)", pointer[2]);
519 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
520 fprintf(NetTrace, "%s", buf);
523 case ENCRYPT_SUPPORT:
525 fprintf(NetTrace, " SUPPORT ");
527 if (ENCTYPE_NAME_OK(pointer[i]))
528 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
530 fprintf(NetTrace, "%d ", pointer[i]);
535 case ENCRYPT_ENC_KEYID:
536 fprintf(NetTrace, " ENC_KEYID ");
539 case ENCRYPT_DEC_KEYID:
540 fprintf(NetTrace, " DEC_KEYID ");
544 fprintf(NetTrace, " %d (unknown)", pointer[1]);
546 for (i = 2; i < length; i++)
547 fprintf(NetTrace, " %d", pointer[i]);
551 #endif /* ENCRYPTION */
553 case TELOPT_LINEMODE:
554 fprintf(NetTrace, "LINEMODE ");
556 fprintf(NetTrace, " (empty suboption??\?)");
559 switch (pointer[1]) {
561 fprintf(NetTrace, "WILL ");
564 fprintf(NetTrace, "WONT ");
567 fprintf(NetTrace, "DO ");
570 fprintf(NetTrace, "DONT ");
573 fprintf(NetTrace, "(no option??\?)");
576 switch (pointer[2]) {
578 fprintf(NetTrace, "Forward Mask");
579 for (i = 3; i < length; i++)
580 fprintf(NetTrace, " %x", pointer[i]);
583 fprintf(NetTrace, "%d (unknown)", pointer[2]);
584 for (i = 3; i < length; i++)
585 fprintf(NetTrace, " %d", pointer[i]);
591 fprintf(NetTrace, "SLC");
592 for (i = 2; i < length - 2; i += 3) {
593 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
594 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
596 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
597 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
599 fprintf(NetTrace, " NOSUPPORT"); break;
601 fprintf(NetTrace, " CANTCHANGE"); break;
603 fprintf(NetTrace, " VARIABLE"); break;
605 fprintf(NetTrace, " DEFAULT"); break;
607 fprintf(NetTrace, "%s%s%s",
608 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
609 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
610 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
611 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
612 SLC_FLUSHOUT| SLC_LEVELBITS))
613 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
614 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
615 if ((pointer[i+SLC_VALUE] == IAC) &&
616 (pointer[i+SLC_VALUE+1] == IAC))
619 for (; i < length; i++)
620 fprintf(NetTrace, " ?%d?", pointer[i]);
624 fprintf(NetTrace, "MODE ");
626 fprintf(NetTrace, "(no mode??\?)");
631 sprintf(tbuf, "%s%s%s%s%s",
632 pointer[2]&MODE_EDIT ? "|EDIT" : "",
633 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
634 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
635 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
636 pointer[2]&MODE_ACK ? "|ACK" : "");
637 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
639 if (pointer[2]&~(MODE_MASK))
640 fprintf(NetTrace, " (0x%x)", pointer[2]);
641 for (i = 3; i < length; i++)
642 fprintf(NetTrace, " ?0x%x?", pointer[i]);
645 fprintf(NetTrace, "%d (unknown)", pointer[1]);
646 for (i = 2; i < length; i++)
647 fprintf(NetTrace, " %d", pointer[i]);
651 case TELOPT_STATUS: {
655 fprintf(NetTrace, "STATUS");
657 switch (pointer[1]) {
659 if (pointer[1] == TELQUAL_SEND)
660 fprintf(NetTrace, " SEND");
662 fprintf(NetTrace, " %d (unknown)", pointer[1]);
663 for (i = 2; i < length; i++)
664 fprintf(NetTrace, " ?%d?", pointer[i]);
667 if (--want_status_response < 0)
668 want_status_response = 0;
669 if (NetTrace == stdout)
670 fprintf(NetTrace, " IS\r\n");
672 fprintf(NetTrace, " IS\n");
674 for (i = 2; i < length; i++) {
676 case DO: cp = "DO"; goto common2;
677 case DONT: cp = "DONT"; goto common2;
678 case WILL: cp = "WILL"; goto common2;
679 case WONT: cp = "WONT"; goto common2;
682 if (TELOPT_OK((int)pointer[i]))
683 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
685 fprintf(NetTrace, " %s %d", cp, pointer[i]);
687 if (NetTrace == stdout)
688 fprintf(NetTrace, "\r\n");
690 fprintf(NetTrace, "\n");
694 fprintf(NetTrace, " SB ");
698 if (pointer[j] == SE) {
701 if (pointer[j+1] == SE)
706 pointer[k++] = pointer[j++];
708 printsub(0, &pointer[i], k - i);
710 fprintf(NetTrace, " SE");
715 if (NetTrace == stdout)
716 fprintf(NetTrace, "\r\n");
718 fprintf(NetTrace, "\n");
723 fprintf(NetTrace, " %d", pointer[i]);
732 case TELOPT_XDISPLOC:
733 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
734 switch (pointer[1]) {
736 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
739 fprintf(NetTrace, "SEND");
742 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
743 pointer[1], pointer[1]);
747 case TELOPT_NEW_ENVIRON:
748 fprintf(NetTrace, "NEW-ENVIRON ");
751 case TELOPT_OLD_ENVIRON:
752 fprintf(NetTrace, "OLD-ENVIRON");
755 switch (pointer[1]) {
757 fprintf(NetTrace, "IS ");
760 fprintf(NetTrace, "SEND ");
763 fprintf(NetTrace, "INFO ");
767 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
768 extern int old_env_var, old_env_value;
770 for (i = 2; i < length; i++ ) {
771 switch (pointer[i]) {
774 /* case NEW_ENV_OVAR: */
775 if (pointer[0] == TELOPT_OLD_ENVIRON) {
777 if (old_env_var == OLD_ENV_VALUE)
778 fprintf(NetTrace, "\" (VALUE) " + noquote);
781 fprintf(NetTrace, "\" VAR " + noquote);
783 #endif /* OLD_ENVIRON */
784 fprintf(NetTrace, "\" VALUE " + noquote);
790 /* case OLD_ENV_VALUE: */
791 if (pointer[0] == TELOPT_OLD_ENVIRON) {
793 if (old_env_value == OLD_ENV_VAR)
794 fprintf(NetTrace, "\" (VAR) " + noquote);
797 fprintf(NetTrace, "\" VALUE " + noquote);
799 #endif /* OLD_ENVIRON */
800 fprintf(NetTrace, "\" VAR " + noquote);
805 fprintf(NetTrace, "\" ESC " + noquote);
810 fprintf(NetTrace, "\" USERVAR " + noquote);
815 if (isprint(pointer[i]) && pointer[i] != '"') {
820 putc(pointer[i], NetTrace);
822 fprintf(NetTrace, "\" %03o " + noquote,
837 if (TELOPT_OK(pointer[0]))
838 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
840 fprintf(NetTrace, "%d (unknown)", pointer[0]);
841 for (i = 1; i < length; i++)
842 fprintf(NetTrace, " %d", pointer[i]);
846 if (NetTrace == stdout)
847 fprintf(NetTrace, "\r\n");
849 fprintf(NetTrace, "\n");
851 if (NetTrace == stdout)
856 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
857 * Note that we consider the buffer to run all the
858 * way to the kernel (thus the select).
868 if (TTYBYTES() == 0) {
870 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
871 (struct timeval *) 0); /* wait for TTLOWAT */
876 (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0,
877 (struct timeval *) 0); /* wait for TTLOWAT */
887 (void)telrcv(); /* Process any incoming data */
889 } while (ring_full_count(&netiring)); /* While there is any */
894 EmptyTerminal(); /* Flush the path to the tty */
906 ExitString(const char *string, int returnCode)
909 fwrite(string, 1, strlen(string), stderr);