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 $
35 * $DragonFly: src/crypto/telnet/telnet/utilities.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
41 #include <arpa/telnet.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
60 #include <libtelnet/auth.h>
63 #include <libtelnet/encrypt.h>
66 FILE *NetTrace = 0; /* Not in bss, since needs to stay */
72 * Upcase (in place) the argument.
76 upcase(char *argument)
80 while ((c = *argument) != 0) {
82 *argument = toupper(c);
91 * Compensate for differences in 4.2 and 4.3 systems.
95 SetSockOpt(int fd, int level, int option, int yesno)
97 return setsockopt(fd, level, option,
98 (char *)&yesno, sizeof yesno);
102 * The following are routines used to print out debugging information.
105 unsigned char NetTraceFile[256] = "(standard output)";
108 SetNetTrace(char *file)
110 if (NetTrace && NetTrace != stdout)
112 if (file && (strcmp(file, "-") != 0)) {
113 NetTrace = fopen(file, "w");
115 strcpy((char *)NetTraceFile, file);
118 fprintf(stderr, "Cannot open %s.\n", file);
121 strcpy((char *)NetTraceFile, "(standard output)");
125 Dump(char direction, unsigned char *buffer, int length)
127 # define BYTES_PER_LINE 32
128 # define min(x,y) ((x<y)? x:y)
129 unsigned char *pThis;
136 fprintf(NetTrace, "%c 0x%x\t", direction, offset);
139 buffer = buffer + min(length, BYTES_PER_LINE/2);
140 while (pThis < buffer) {
141 fprintf(NetTrace, "%c%.2x",
142 (((*pThis)&0xff) == 0xff) ? '*' : ' ',
146 length -= BYTES_PER_LINE/2;
147 offset += BYTES_PER_LINE/2;
149 buffer = buffer + min(length, BYTES_PER_LINE);
150 while (pThis < buffer) {
151 fprintf(NetTrace, "%.2x", (*pThis)&0xff);
154 length -= BYTES_PER_LINE;
155 offset += BYTES_PER_LINE;
157 if (NetTrace == stdout) {
158 fprintf(NetTrace, "\r\n");
160 fprintf(NetTrace, "\n");
166 /* find next unique line */
173 printoption(const char *direction, int cmd, int option)
178 if (TELCMD_OK(option))
179 fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
181 fprintf(NetTrace, "%s IAC %d", direction, option);
184 fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
185 (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
187 fprintf(NetTrace, "%s %s ", direction, fmt);
188 if (TELOPT_OK(option))
189 fprintf(NetTrace, "%s", TELOPT(option));
190 else if (option == TELOPT_EXOPL)
191 fprintf(NetTrace, "EXOPL");
193 fprintf(NetTrace, "%d", option);
195 fprintf(NetTrace, "%s %d %d", direction, cmd, option);
197 if (NetTrace == stdout) {
198 fprintf(NetTrace, "\r\n");
201 fprintf(NetTrace, "\n");
210 extern char will_wont_resp[], do_dont_resp[];
212 for (i = 0; i < 256; i++) {
213 if (do_dont_resp[i]) {
215 printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
216 else if (TELCMD_OK(i))
217 printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
219 printf("resp DO_DONT %d: %d\n", i,
221 if (my_want_state_is_do(i)) {
223 printf("want DO %s\n", TELOPT(i));
224 else if (TELCMD_OK(i))
225 printf("want DO %s\n", TELCMD(i));
227 printf("want DO %d\n", i);
230 printf("want DONT %s\n", TELOPT(i));
231 else if (TELCMD_OK(i))
232 printf("want DONT %s\n", TELCMD(i));
234 printf("want DONT %d\n", i);
237 if (my_state_is_do(i)) {
239 printf(" DO %s\n", TELOPT(i));
240 else if (TELCMD_OK(i))
241 printf(" DO %s\n", TELCMD(i));
243 printf(" DO %d\n", i);
246 if (will_wont_resp[i]) {
248 printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
249 else if (TELCMD_OK(i))
250 printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
252 printf("resp WILL_WONT %d: %d\n",
253 i, will_wont_resp[i]);
254 if (my_want_state_is_will(i)) {
256 printf("want WILL %s\n", TELOPT(i));
257 else if (TELCMD_OK(i))
258 printf("want WILL %s\n", TELCMD(i));
260 printf("want WILL %d\n", i);
263 printf("want WONT %s\n", TELOPT(i));
264 else if (TELCMD_OK(i))
265 printf("want WONT %s\n", TELCMD(i));
267 printf("want WONT %d\n", i);
270 if (my_state_is_will(i)) {
272 printf(" WILL %s\n", TELOPT(i));
273 else if (TELCMD_OK(i))
274 printf(" WILL %s\n", TELCMD(i));
276 printf(" WILL %d\n", i);
284 printsub(char direction, unsigned char *pointer, int length)
287 #ifdef AUTHENTICATION
290 extern int want_status_response;
292 if (showoptions || direction == 0 ||
293 (want_status_response && (pointer[0] == TELOPT_STATUS))) {
295 fprintf(NetTrace, "%s IAC SB ",
296 (direction == '<')? "RCVD":"SENT");
300 i = pointer[length-2];
301 j = pointer[length-1];
303 if (i != IAC || j != SE) {
304 fprintf(NetTrace, "(terminated by ");
306 fprintf(NetTrace, "%s ", TELOPT(i));
307 else if (TELCMD_OK(i))
308 fprintf(NetTrace, "%s ", TELCMD(i));
310 fprintf(NetTrace, "%d ", i);
312 fprintf(NetTrace, "%s", TELOPT(j));
313 else if (TELCMD_OK(j))
314 fprintf(NetTrace, "%s", TELCMD(j));
316 fprintf(NetTrace, "%d", j);
317 fprintf(NetTrace, ", not IAC SE!) ");
323 fprintf(NetTrace, "(Empty suboption??\?)");
324 if (NetTrace == stdout)
328 switch (pointer[0]) {
330 fprintf(NetTrace, "TERMINAL-TYPE ");
331 switch (pointer[1]) {
333 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
336 fprintf(NetTrace, "SEND");
340 "- unknown qualifier %d (0x%x).",
341 pointer[1], pointer[1]);
345 fprintf(NetTrace, "TERMINAL-SPEED");
347 fprintf(NetTrace, " (empty suboption??\?)");
350 switch (pointer[1]) {
352 fprintf(NetTrace, " IS ");
353 fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
357 fprintf(NetTrace, " SEND");
359 fprintf(NetTrace, " %d (unknown)", pointer[1]);
360 for (i = 2; i < length; i++)
361 fprintf(NetTrace, " ?%d?", pointer[i]);
367 fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
369 fprintf(NetTrace, " (empty suboption??\?)");
372 switch (pointer[1]) {
374 fprintf(NetTrace, " OFF"); break;
376 fprintf(NetTrace, " ON"); break;
377 case LFLOW_RESTART_ANY:
378 fprintf(NetTrace, " RESTART-ANY"); break;
379 case LFLOW_RESTART_XON:
380 fprintf(NetTrace, " RESTART-XON"); break;
382 fprintf(NetTrace, " %d (unknown)", pointer[1]);
384 for (i = 2; i < length; i++)
385 fprintf(NetTrace, " ?%d?", pointer[i]);
389 fprintf(NetTrace, "NAWS");
391 fprintf(NetTrace, " (empty suboption??\?)");
395 fprintf(NetTrace, " ?%d?", pointer[1]);
398 fprintf(NetTrace, " %d %d (%d)",
399 pointer[1], pointer[2],
400 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
402 fprintf(NetTrace, " ?%d?", pointer[3]);
405 fprintf(NetTrace, " %d %d (%d)",
406 pointer[3], pointer[4],
407 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
408 for (i = 5; i < length; i++)
409 fprintf(NetTrace, " ?%d?", pointer[i]);
412 #ifdef AUTHENTICATION
413 case TELOPT_AUTHENTICATION:
414 fprintf(NetTrace, "AUTHENTICATION");
416 fprintf(NetTrace, " (empty suboption??\?)");
419 switch (pointer[1]) {
422 fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
424 if (AUTHTYPE_NAME_OK(pointer[2]))
425 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
427 fprintf(NetTrace, "%d ", pointer[2]);
429 fprintf(NetTrace, "(partial suboption??\?)");
432 fprintf(NetTrace, "%s|%s",
433 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
435 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
436 "MUTUAL" : "ONE-WAY");
438 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
439 fprintf(NetTrace, "%s", buf);
444 fprintf(NetTrace, " SEND ");
446 if (AUTHTYPE_NAME_OK(pointer[i]))
447 fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
449 fprintf(NetTrace, "%d ", pointer[i]);
451 fprintf(NetTrace, "(partial suboption??\?)");
454 fprintf(NetTrace, "%s|%s ",
455 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
457 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
458 "MUTUAL" : "ONE-WAY");
465 fprintf(NetTrace, " NAME \"");
467 putc(pointer[i++], NetTrace);
472 for (i = 2; i < length; i++)
473 fprintf(NetTrace, " ?%d?", pointer[i]);
481 fprintf(NetTrace, "ENCRYPT");
483 fprintf(NetTrace, " (empty suboption??\?)");
486 switch (pointer[1]) {
488 fprintf(NetTrace, " START");
492 fprintf(NetTrace, " END");
495 case ENCRYPT_REQSTART:
496 fprintf(NetTrace, " REQUEST-START");
500 fprintf(NetTrace, " REQUEST-END");
505 fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
508 fprintf(NetTrace, " (partial suboption??\?)");
511 if (ENCTYPE_NAME_OK(pointer[2]))
512 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
514 fprintf(NetTrace, " %d (unknown)", pointer[2]);
516 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
517 fprintf(NetTrace, "%s", buf);
520 case ENCRYPT_SUPPORT:
522 fprintf(NetTrace, " SUPPORT ");
524 if (ENCTYPE_NAME_OK(pointer[i]))
525 fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
527 fprintf(NetTrace, "%d ", pointer[i]);
532 case ENCRYPT_ENC_KEYID:
533 fprintf(NetTrace, " ENC_KEYID ");
536 case ENCRYPT_DEC_KEYID:
537 fprintf(NetTrace, " DEC_KEYID ");
541 fprintf(NetTrace, " %d (unknown)", pointer[1]);
543 for (i = 2; i < length; i++)
544 fprintf(NetTrace, " %d", pointer[i]);
548 #endif /* ENCRYPTION */
550 case TELOPT_LINEMODE:
551 fprintf(NetTrace, "LINEMODE ");
553 fprintf(NetTrace, " (empty suboption??\?)");
556 switch (pointer[1]) {
558 fprintf(NetTrace, "WILL ");
561 fprintf(NetTrace, "WONT ");
564 fprintf(NetTrace, "DO ");
567 fprintf(NetTrace, "DONT ");
570 fprintf(NetTrace, "(no option??\?)");
573 switch (pointer[2]) {
575 fprintf(NetTrace, "Forward Mask");
576 for (i = 3; i < length; i++)
577 fprintf(NetTrace, " %x", pointer[i]);
580 fprintf(NetTrace, "%d (unknown)", pointer[2]);
581 for (i = 3; i < length; i++)
582 fprintf(NetTrace, " %d", pointer[i]);
588 fprintf(NetTrace, "SLC");
589 for (i = 2; i < length - 2; i += 3) {
590 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
591 fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
593 fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
594 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
596 fprintf(NetTrace, " NOSUPPORT"); break;
598 fprintf(NetTrace, " CANTCHANGE"); break;
600 fprintf(NetTrace, " VARIABLE"); break;
602 fprintf(NetTrace, " DEFAULT"); break;
604 fprintf(NetTrace, "%s%s%s",
605 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
606 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
607 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
608 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
609 SLC_FLUSHOUT| SLC_LEVELBITS))
610 fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
611 fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
612 if ((pointer[i+SLC_VALUE] == IAC) &&
613 (pointer[i+SLC_VALUE+1] == IAC))
616 for (; i < length; i++)
617 fprintf(NetTrace, " ?%d?", pointer[i]);
621 fprintf(NetTrace, "MODE ");
623 fprintf(NetTrace, "(no mode??\?)");
628 sprintf(tbuf, "%s%s%s%s%s",
629 pointer[2]&MODE_EDIT ? "|EDIT" : "",
630 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
631 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
632 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
633 pointer[2]&MODE_ACK ? "|ACK" : "");
634 fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
636 if (pointer[2]&~(MODE_MASK))
637 fprintf(NetTrace, " (0x%x)", pointer[2]);
638 for (i = 3; i < length; i++)
639 fprintf(NetTrace, " ?0x%x?", pointer[i]);
642 fprintf(NetTrace, "%d (unknown)", pointer[1]);
643 for (i = 2; i < length; i++)
644 fprintf(NetTrace, " %d", pointer[i]);
648 case TELOPT_STATUS: {
652 fprintf(NetTrace, "STATUS");
654 switch (pointer[1]) {
656 if (pointer[1] == TELQUAL_SEND)
657 fprintf(NetTrace, " SEND");
659 fprintf(NetTrace, " %d (unknown)", pointer[1]);
660 for (i = 2; i < length; i++)
661 fprintf(NetTrace, " ?%d?", pointer[i]);
664 if (--want_status_response < 0)
665 want_status_response = 0;
666 if (NetTrace == stdout)
667 fprintf(NetTrace, " IS\r\n");
669 fprintf(NetTrace, " IS\n");
671 for (i = 2; i < length; i++) {
673 case DO: cp = "DO"; goto common2;
674 case DONT: cp = "DONT"; goto common2;
675 case WILL: cp = "WILL"; goto common2;
676 case WONT: cp = "WONT"; goto common2;
679 if (TELOPT_OK((int)pointer[i]))
680 fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
682 fprintf(NetTrace, " %s %d", cp, pointer[i]);
684 if (NetTrace == stdout)
685 fprintf(NetTrace, "\r\n");
687 fprintf(NetTrace, "\n");
691 fprintf(NetTrace, " SB ");
695 if (pointer[j] == SE) {
698 if (pointer[j+1] == SE)
703 pointer[k++] = pointer[j++];
705 printsub(0, &pointer[i], k - i);
707 fprintf(NetTrace, " SE");
712 if (NetTrace == stdout)
713 fprintf(NetTrace, "\r\n");
715 fprintf(NetTrace, "\n");
720 fprintf(NetTrace, " %d", pointer[i]);
729 case TELOPT_XDISPLOC:
730 fprintf(NetTrace, "X-DISPLAY-LOCATION ");
731 switch (pointer[1]) {
733 fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
736 fprintf(NetTrace, "SEND");
739 fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
740 pointer[1], pointer[1]);
744 case TELOPT_NEW_ENVIRON:
745 fprintf(NetTrace, "NEW-ENVIRON ");
748 case TELOPT_OLD_ENVIRON:
749 fprintf(NetTrace, "OLD-ENVIRON");
752 switch (pointer[1]) {
754 fprintf(NetTrace, "IS ");
757 fprintf(NetTrace, "SEND ");
760 fprintf(NetTrace, "INFO ");
764 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
765 extern int old_env_var, old_env_value;
767 for (i = 2; i < length; i++ ) {
768 switch (pointer[i]) {
771 /* case NEW_ENV_OVAR: */
772 if (pointer[0] == TELOPT_OLD_ENVIRON) {
774 if (old_env_var == OLD_ENV_VALUE)
775 fprintf(NetTrace, "\" (VALUE) " + noquote);
778 fprintf(NetTrace, "\" VAR " + noquote);
780 #endif /* OLD_ENVIRON */
781 fprintf(NetTrace, "\" VALUE " + noquote);
787 /* case OLD_ENV_VALUE: */
788 if (pointer[0] == TELOPT_OLD_ENVIRON) {
790 if (old_env_value == OLD_ENV_VAR)
791 fprintf(NetTrace, "\" (VAR) " + noquote);
794 fprintf(NetTrace, "\" VALUE " + noquote);
796 #endif /* OLD_ENVIRON */
797 fprintf(NetTrace, "\" VAR " + noquote);
802 fprintf(NetTrace, "\" ESC " + noquote);
807 fprintf(NetTrace, "\" USERVAR " + noquote);
812 if (isprint(pointer[i]) && pointer[i] != '"') {
817 putc(pointer[i], NetTrace);
819 fprintf(NetTrace, "\" %03o " + noquote,
834 if (TELOPT_OK(pointer[0]))
835 fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
837 fprintf(NetTrace, "%d (unknown)", pointer[0]);
838 for (i = 1; i < length; i++)
839 fprintf(NetTrace, " %d", pointer[i]);
843 if (NetTrace == stdout)
844 fprintf(NetTrace, "\r\n");
846 fprintf(NetTrace, "\n");
848 if (NetTrace == stdout)
853 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
854 * Note that we consider the buffer to run all the
855 * way to the kernel (thus the select).
865 if (TTYBYTES() == 0) {
867 (void) select(tout+1, NULL, &o, NULL, NULL); /* wait for TTLOWAT */
872 (void) select(tout+1, NULL, &o, NULL, NULL); /* wait for TTLOWAT */
882 (void)telrcv(); /* Process any incoming data */
884 } while (ring_full_count(&netiring)); /* While there is any */
889 EmptyTerminal(); /* Flush the path to the tty */
901 ExitString(const char *string, int returnCode)
904 fwrite(string, 1, strlen(string), stderr);