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
33 * @(#)utilities.c 8.3 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnet/utilities.c,v 1.2.8.2 2002/04/13 10:59:08 markm Exp $
40 #include <arpa/telnet.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
59 #include <libtelnet/auth.h>
62 #include <libtelnet/encrypt.h>
65 FILE *NetTrace = NULL; /* Not in bss, since needs to stay */
71 * Upcase (in place) the argument.
75 upcase(char *argument)
79 while ((c = *argument) != 0) {
81 *argument = toupper(c);
90 * Compensate for differences in 4.2 and 4.3 systems.
94 SetSockOpt(int fd, int level, int option, int yesno)
96 return setsockopt(fd, level, option,
97 (char *)&yesno, sizeof yesno);
101 * The following are routines used to print out debugging information.
104 unsigned char NetTraceFile[256] = "(standard output)";
107 SetNetTrace(char *file)
109 if (NetTrace && NetTrace != stdout)
111 if (file && (strcmp(file, "-") != 0)) {
112 NetTrace = fopen(file, "w");
114 strcpy((char *)NetTraceFile, file);
117 fprintf(stderr, "Cannot open %s.\n", file);
120 strcpy((char *)NetTraceFile, "(standard output)");
124 Dump(char direction, unsigned char *buffer, int length)
126 # define BYTES_PER_LINE 32
127 # define min(x,y) ((x<y)? x:y)
128 unsigned char *pThis;
135 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
138 buffer = buffer + min(length, BYTES_PER_LINE/2);
139 while (pThis < buffer) {
140 fprintf(NetTrace, "%c%.2x",
141 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
145 length -= BYTES_PER_LINE/2;
146 offset += BYTES_PER_LINE/2;
148 buffer = buffer + min(length, BYTES_PER_LINE);
149 while (pThis < buffer) {
150 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
153 length -= BYTES_PER_LINE;
154 offset += BYTES_PER_LINE;
156 if (NetTrace == stdout) {
157 fprintf(NetTrace, "\r\n");
159 fprintf(NetTrace, "\n");
165 /* find next unique line */
172 printoption(const char *direction, int cmd, int option)
177 if (TELCMD_OK(option))
178 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
180 fprintf(NetTrace, "%s IAC %d", direction, option);
183 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
184 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
186 fprintf(NetTrace, "%s %s ", direction, fmt);
187 if (TELOPT_OK(option))
188 fprintf(NetTrace, "%s", TELOPT(option));
189 else if (option == TELOPT_EXOPL)
190 fprintf(NetTrace, "EXOPL");
192 fprintf(NetTrace, "%d", option);
194 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
196 if (NetTrace == stdout) {
197 fprintf(NetTrace, "\r\n");
200 fprintf(NetTrace, "\n");
209 extern char will_wont_resp[], do_dont_resp[];
211 for (i = 0; i < 256; i++) {
212 if (do_dont_resp[i]) {
214 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
215 else if (TELCMD_OK(i))
216 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
218 printf("resp DO_DONT %d: %d\n", i,
220 if (my_want_state_is_do(i)) {
222 printf("want DO %s\n", TELOPT(i));
223 else if (TELCMD_OK(i))
224 printf("want DO %s\n", TELCMD(i));
226 printf("want DO %d\n", i);
229 printf("want DONT %s\n", TELOPT(i));
230 else if (TELCMD_OK(i))
231 printf("want DONT %s\n", TELCMD(i));
233 printf("want DONT %d\n", i);
236 if (my_state_is_do(i)) {
238 printf(" DO %s\n", TELOPT(i));
239 else if (TELCMD_OK(i))
240 printf(" DO %s\n", TELCMD(i));
242 printf(" DO %d\n", i);
245 if (will_wont_resp[i]) {
247 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
248 else if (TELCMD_OK(i))
249 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
251 printf("resp WILL_WONT %d: %d\n",
252 i, will_wont_resp[i]);
253 if (my_want_state_is_will(i)) {
255 printf("want WILL %s\n", TELOPT(i));
256 else if (TELCMD_OK(i))
257 printf("want WILL %s\n", TELCMD(i));
259 printf("want WILL %d\n", i);
262 printf("want WONT %s\n", TELOPT(i));
263 else if (TELCMD_OK(i))
264 printf("want WONT %s\n", TELCMD(i));
266 printf("want WONT %d\n", i);
269 if (my_state_is_will(i)) {
271 printf(" WILL %s\n", TELOPT(i));
272 else if (TELCMD_OK(i))
273 printf(" WILL %s\n", TELCMD(i));
275 printf(" WILL %d\n", i);
283 printsub(char direction, unsigned char *pointer, int length)
286 #ifdef AUTHENTICATION
289 extern int want_status_response;
291 if (showoptions || direction == 0 ||
292 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
294 fprintf(NetTrace, "%s IAC SB ",
295 (direction == '<')? "RCVD":"SENT");
299 i = pointer[length-2];
300 j = pointer[length-1];
302 if (i != IAC || j != SE) {
303 fprintf(NetTrace, "(terminated by ");
305 fprintf(NetTrace, "%s ", TELOPT(i));
306 else if (TELCMD_OK(i))
307 fprintf(NetTrace, "%s ", TELCMD(i));
309 fprintf(NetTrace, "%d ", i);
311 fprintf(NetTrace, "%s", TELOPT(j));
312 else if (TELCMD_OK(j))
313 fprintf(NetTrace, "%s", TELCMD(j));
315 fprintf(NetTrace, "%d", j);
316 fprintf(NetTrace, ", not IAC SE!) ");
322 fprintf(NetTrace, "(Empty suboption??\?)");
323 if (NetTrace == stdout)
327 switch (pointer[0]) {
329 fprintf(NetTrace, "TERMINAL-TYPE ");
330 switch (pointer[1]) {
332 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
335 fprintf(NetTrace, "SEND");
339 "- unknown qualifier %d (0x%x).",
340 pointer[1], pointer[1]);
344 fprintf(NetTrace, "TERMINAL-SPEED");
346 fprintf(NetTrace, " (empty suboption??\?)");
349 switch (pointer[1]) {
351 fprintf(NetTrace, " IS ");
352 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
356 fprintf(NetTrace, " SEND");
358 fprintf(NetTrace, " %d (unknown)", pointer[1]);
359 for (i = 2; i < length; i++)
360 fprintf(NetTrace, " ?%d?", pointer[i]);
366 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
368 fprintf(NetTrace, " (empty suboption??\?)");
371 switch (pointer[1]) {
373 fprintf(NetTrace, " OFF"); break;
375 fprintf(NetTrace, " ON"); break;
376 case LFLOW_RESTART_ANY:
377 fprintf(NetTrace, " RESTART-ANY"); break;
378 case LFLOW_RESTART_XON:
379 fprintf(NetTrace, " RESTART-XON"); break;
381 fprintf(NetTrace, " %d (unknown)", pointer[1]);
383 for (i = 2; i < length; i++)
384 fprintf(NetTrace, " ?%d?", pointer[i]);
388 fprintf(NetTrace, "NAWS");
390 fprintf(NetTrace, " (empty suboption??\?)");
394 fprintf(NetTrace, " ?%d?", pointer[1]);
397 fprintf(NetTrace, " %d %d (%d)",
398 pointer[1], pointer[2],
399 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
401 fprintf(NetTrace, " ?%d?", pointer[3]);
404 fprintf(NetTrace, " %d %d (%d)",
405 pointer[3], pointer[4],
406 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
407 for (i = 5; i < length; i++)
408 fprintf(NetTrace, " ?%d?", pointer[i]);
411 #ifdef AUTHENTICATION
412 case TELOPT_AUTHENTICATION:
413 fprintf(NetTrace, "AUTHENTICATION");
415 fprintf(NetTrace, " (empty suboption??\?)");
418 switch (pointer[1]) {
421 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
423 if (AUTHTYPE_NAME_OK(pointer[2]))
424 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
426 fprintf(NetTrace, "%d ", pointer[2]);
428 fprintf(NetTrace, "(partial suboption??\?)");
431 fprintf(NetTrace, "%s|%s",
432 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
434 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
435 "MUTUAL" : "ONE-WAY");
437 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
438 fprintf(NetTrace, "%s", buf);
443 fprintf(NetTrace, " SEND ");
445 if (AUTHTYPE_NAME_OK(pointer[i]))
446 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
448 fprintf(NetTrace, "%d ", pointer[i]);
450 fprintf(NetTrace, "(partial suboption??\?)");
453 fprintf(NetTrace, "%s|%s ",
454 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
456 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
457 "MUTUAL" : "ONE-WAY");
464 fprintf(NetTrace, " NAME \"");
466 putc(pointer[i++], NetTrace);
471 for (i = 2; i < length; i++)
472 fprintf(NetTrace, " ?%d?", pointer[i]);
480 fprintf(NetTrace, "ENCRYPT");
482 fprintf(NetTrace, " (empty suboption??\?)");
485 switch (pointer[1]) {
487 fprintf(NetTrace, " START");
491 fprintf(NetTrace, " END");
494 case ENCRYPT_REQSTART:
495 fprintf(NetTrace, " REQUEST-START");
499 fprintf(NetTrace, " REQUEST-END");
504 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
507 fprintf(NetTrace, " (partial suboption??\?)");
510 if (ENCTYPE_NAME_OK(pointer[2]))
511 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
513 fprintf(NetTrace, " %d (unknown)", pointer[2]);
515 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
516 fprintf(NetTrace, "%s", buf);
519 case ENCRYPT_SUPPORT:
521 fprintf(NetTrace, " SUPPORT ");
523 if (ENCTYPE_NAME_OK(pointer[i]))
524 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
526 fprintf(NetTrace, "%d ", pointer[i]);
531 case ENCRYPT_ENC_KEYID:
532 fprintf(NetTrace, " ENC_KEYID ");
535 case ENCRYPT_DEC_KEYID:
536 fprintf(NetTrace, " DEC_KEYID ");
540 fprintf(NetTrace, " %d (unknown)", pointer[1]);
542 for (i = 2; i < length; i++)
543 fprintf(NetTrace, " %d", pointer[i]);
547 #endif /* ENCRYPTION */
549 case TELOPT_LINEMODE:
550 fprintf(NetTrace, "LINEMODE ");
552 fprintf(NetTrace, " (empty suboption??\?)");
555 switch (pointer[1]) {
557 fprintf(NetTrace, "WILL ");
560 fprintf(NetTrace, "WONT ");
563 fprintf(NetTrace, "DO ");
566 fprintf(NetTrace, "DONT ");
569 fprintf(NetTrace, "(no option??\?)");
572 switch (pointer[2]) {
574 fprintf(NetTrace, "Forward Mask");
575 for (i = 3; i < length; i++)
576 fprintf(NetTrace, " %x", pointer[i]);
579 fprintf(NetTrace, "%d (unknown)", pointer[2]);
580 for (i = 3; i < length; i++)
581 fprintf(NetTrace, " %d", pointer[i]);
587 fprintf(NetTrace, "SLC");
588 for (i = 2; i < length - 2; i += 3) {
589 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
590 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
592 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
593 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
595 fprintf(NetTrace, " NOSUPPORT"); break;
597 fprintf(NetTrace, " CANTCHANGE"); break;
599 fprintf(NetTrace, " VARIABLE"); break;
601 fprintf(NetTrace, " DEFAULT"); break;
603 fprintf(NetTrace, "%s%s%s",
604 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
605 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
606 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
607 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
608 SLC_FLUSHOUT| SLC_LEVELBITS))
609 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
610 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
611 if ((pointer[i+SLC_VALUE] == IAC) &&
612 (pointer[i+SLC_VALUE+1] == IAC))
615 for (; i < length; i++)
616 fprintf(NetTrace, " ?%d?", pointer[i]);
620 fprintf(NetTrace, "MODE ");
622 fprintf(NetTrace, "(no mode??\?)");
627 sprintf(tbuf, "%s%s%s%s%s",
628 pointer[2]&MODE_EDIT ? "|EDIT" : "",
629 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
630 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
631 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
632 pointer[2]&MODE_ACK ? "|ACK" : "");
633 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
635 if (pointer[2]&~(MODE_MASK))
636 fprintf(NetTrace, " (0x%x)", pointer[2]);
637 for (i = 3; i < length; i++)
638 fprintf(NetTrace, " ?0x%x?", pointer[i]);
641 fprintf(NetTrace, "%d (unknown)", pointer[1]);
642 for (i = 2; i < length; i++)
643 fprintf(NetTrace, " %d", pointer[i]);
647 case TELOPT_STATUS: {
651 fprintf(NetTrace, "STATUS");
653 switch (pointer[1]) {
655 if (pointer[1] == TELQUAL_SEND)
656 fprintf(NetTrace, " SEND");
658 fprintf(NetTrace, " %d (unknown)", pointer[1]);
659 for (i = 2; i < length; i++)
660 fprintf(NetTrace, " ?%d?", pointer[i]);
663 if (--want_status_response < 0)
664 want_status_response = 0;
665 if (NetTrace == stdout)
666 fprintf(NetTrace, " IS\r\n");
668 fprintf(NetTrace, " IS\n");
670 for (i = 2; i < length; i++) {
672 case DO: cp = "DO"; goto common2;
673 case DONT: cp = "DONT"; goto common2;
674 case WILL: cp = "WILL"; goto common2;
675 case WONT: cp = "WONT"; goto common2;
678 if (TELOPT_OK((int)pointer[i]))
679 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
681 fprintf(NetTrace, " %s %d", cp, pointer[i]);
683 if (NetTrace == stdout)
684 fprintf(NetTrace, "\r\n");
686 fprintf(NetTrace, "\n");
690 fprintf(NetTrace, " SB ");
694 if (pointer[j] == SE) {
697 if (pointer[j+1] == SE)
702 pointer[k++] = pointer[j++];
704 printsub(0, &pointer[i], k - i);
706 fprintf(NetTrace, " SE");
711 if (NetTrace == stdout)
712 fprintf(NetTrace, "\r\n");
714 fprintf(NetTrace, "\n");
719 fprintf(NetTrace, " %d", pointer[i]);
728 case TELOPT_XDISPLOC:
729 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
730 switch (pointer[1]) {
732 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
735 fprintf(NetTrace, "SEND");
738 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
739 pointer[1], pointer[1]);
743 case TELOPT_NEW_ENVIRON:
744 fprintf(NetTrace, "NEW-ENVIRON ");
747 case TELOPT_OLD_ENVIRON:
748 fprintf(NetTrace, "OLD-ENVIRON");
751 switch (pointer[1]) {
753 fprintf(NetTrace, "IS ");
756 fprintf(NetTrace, "SEND ");
759 fprintf(NetTrace, "INFO ");
763 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
764 extern int old_env_var, old_env_value;
766 for (i = 2; i < length; i++ ) {
767 switch (pointer[i]) {
770 /* case NEW_ENV_OVAR: */
771 if (pointer[0] == TELOPT_OLD_ENVIRON) {
773 if (old_env_var == OLD_ENV_VALUE)
774 fprintf(NetTrace, "\" (VALUE) " + noquote);
777 fprintf(NetTrace, "\" VAR " + noquote);
779 #endif /* OLD_ENVIRON */
780 fprintf(NetTrace, "\" VALUE " + noquote);
786 /* case OLD_ENV_VALUE: */
787 if (pointer[0] == TELOPT_OLD_ENVIRON) {
789 if (old_env_value == OLD_ENV_VAR)
790 fprintf(NetTrace, "\" (VAR) " + noquote);
793 fprintf(NetTrace, "\" VALUE " + noquote);
795 #endif /* OLD_ENVIRON */
796 fprintf(NetTrace, "\" VAR " + noquote);
801 fprintf(NetTrace, "\" ESC " + noquote);
806 fprintf(NetTrace, "\" USERVAR " + noquote);
811 if (isprint(pointer[i]) && pointer[i] != '"') {
816 putc(pointer[i], NetTrace);
818 fprintf(NetTrace, "\" %03o " + noquote,
833 if (TELOPT_OK(pointer[0]))
834 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
836 fprintf(NetTrace, "%d (unknown)", pointer[0]);
837 for (i = 1; i < length; i++)
838 fprintf(NetTrace, " %d", pointer[i]);
842 if (NetTrace == stdout)
843 fprintf(NetTrace, "\r\n");
845 fprintf(NetTrace, "\n");
847 if (NetTrace == stdout)
852 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
853 * Note that we consider the buffer to run all the
854 * way to the kernel (thus the select).
864 if (TTYBYTES() == 0) {
866 (void) select(tout+1, NULL, &o, NULL, NULL); /* wait for TTLOWAT */
871 (void) select(tout+1, NULL, &o, NULL, NULL); /* wait for TTLOWAT */
881 (void)telrcv(); /* Process any incoming data */
883 } while (ring_full_count(&netiring)); /* While there is any */
888 EmptyTerminal(); /* Flush the path to the tty */
900 ExitString(const char *string, int returnCode)
903 fwrite(string, 1, strlen(string), stderr);