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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)utilities.c 8.3 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/utilities.c,v 1.2.8.2 2002/04/13 10:59:08 markm Exp $
36 #include <arpa/telnet.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
55 #include <libtelnet/auth.h>
58 #include <libtelnet/encrypt.h>
61 FILE *NetTrace
= NULL
; /* Not in bss, since needs to stay */
67 * Upcase (in place) the argument.
71 upcase(char *argument
)
75 while ((c
= *argument
) != 0) {
77 *argument
= toupper(c
);
86 * Compensate for differences in 4.2 and 4.3 systems.
90 SetSockOpt(int fd
, int level
, int option
, int yesno
)
92 return setsockopt(fd
, level
, option
,
93 (char *)&yesno
, sizeof yesno
);
97 * The following are routines used to print out debugging information.
100 unsigned char NetTraceFile
[256] = "(standard output)";
103 SetNetTrace(char *file
)
105 if (NetTrace
&& NetTrace
!= stdout
)
107 if (file
&& (strcmp(file
, "-") != 0)) {
108 NetTrace
= fopen(file
, "w");
110 strcpy((char *)NetTraceFile
, file
);
113 fprintf(stderr
, "Cannot open %s.\n", file
);
116 strcpy((char *)NetTraceFile
, "(standard output)");
120 Dump(char direction
, unsigned char *buffer
, int length
)
122 # define BYTES_PER_LINE 32
123 # define min(x,y) ((x<y)? x:y)
124 unsigned char *pThis
;
131 fprintf(NetTrace
, "%c 0x%x\t", direction
, offset
);
134 buffer
= buffer
+ min(length
, BYTES_PER_LINE
/2);
135 while (pThis
< buffer
) {
136 fprintf(NetTrace
, "%c%.2x",
137 (((*pThis
)&0xff) == 0xff) ? '*' : ' ',
141 length
-= BYTES_PER_LINE
/2;
142 offset
+= BYTES_PER_LINE
/2;
144 buffer
= buffer
+ min(length
, BYTES_PER_LINE
);
145 while (pThis
< buffer
) {
146 fprintf(NetTrace
, "%.2x", (*pThis
)&0xff);
149 length
-= BYTES_PER_LINE
;
150 offset
+= BYTES_PER_LINE
;
152 if (NetTrace
== stdout
) {
153 fprintf(NetTrace
, "\r\n");
155 fprintf(NetTrace
, "\n");
161 /* find next unique line */
168 printoption(const char *direction
, int cmd
, int option
)
173 if (TELCMD_OK(option
))
174 fprintf(NetTrace
, "%s IAC %s", direction
, TELCMD(option
));
176 fprintf(NetTrace
, "%s IAC %d", direction
, option
);
179 fmt
= (cmd
== WILL
) ? "WILL" : (cmd
== WONT
) ? "WONT" :
180 (cmd
== DO
) ? "DO" : (cmd
== DONT
) ? "DONT" : 0;
182 fprintf(NetTrace
, "%s %s ", direction
, fmt
);
183 if (TELOPT_OK(option
))
184 fprintf(NetTrace
, "%s", TELOPT(option
));
185 else if (option
== TELOPT_EXOPL
)
186 fprintf(NetTrace
, "EXOPL");
188 fprintf(NetTrace
, "%d", option
);
190 fprintf(NetTrace
, "%s %d %d", direction
, cmd
, option
);
192 if (NetTrace
== stdout
) {
193 fprintf(NetTrace
, "\r\n");
196 fprintf(NetTrace
, "\n");
205 extern char will_wont_resp
[], do_dont_resp
[];
207 for (i
= 0; i
< 256; i
++) {
208 if (do_dont_resp
[i
]) {
210 printf("resp DO_DONT %s: %d\n", TELOPT(i
), do_dont_resp
[i
]);
211 else if (TELCMD_OK(i
))
212 printf("resp DO_DONT %s: %d\n", TELCMD(i
), do_dont_resp
[i
]);
214 printf("resp DO_DONT %d: %d\n", i
,
216 if (my_want_state_is_do(i
)) {
218 printf("want DO %s\n", TELOPT(i
));
219 else if (TELCMD_OK(i
))
220 printf("want DO %s\n", TELCMD(i
));
222 printf("want DO %d\n", i
);
225 printf("want DONT %s\n", TELOPT(i
));
226 else if (TELCMD_OK(i
))
227 printf("want DONT %s\n", TELCMD(i
));
229 printf("want DONT %d\n", i
);
232 if (my_state_is_do(i
)) {
234 printf(" DO %s\n", TELOPT(i
));
235 else if (TELCMD_OK(i
))
236 printf(" DO %s\n", TELCMD(i
));
238 printf(" DO %d\n", i
);
241 if (will_wont_resp
[i
]) {
243 printf("resp WILL_WONT %s: %d\n", TELOPT(i
), will_wont_resp
[i
]);
244 else if (TELCMD_OK(i
))
245 printf("resp WILL_WONT %s: %d\n", TELCMD(i
), will_wont_resp
[i
]);
247 printf("resp WILL_WONT %d: %d\n",
248 i
, will_wont_resp
[i
]);
249 if (my_want_state_is_will(i
)) {
251 printf("want WILL %s\n", TELOPT(i
));
252 else if (TELCMD_OK(i
))
253 printf("want WILL %s\n", TELCMD(i
));
255 printf("want WILL %d\n", i
);
258 printf("want WONT %s\n", TELOPT(i
));
259 else if (TELCMD_OK(i
))
260 printf("want WONT %s\n", TELCMD(i
));
262 printf("want WONT %d\n", i
);
265 if (my_state_is_will(i
)) {
267 printf(" WILL %s\n", TELOPT(i
));
268 else if (TELCMD_OK(i
))
269 printf(" WILL %s\n", TELCMD(i
));
271 printf(" WILL %d\n", i
);
279 printsub(char direction
, unsigned char *pointer
, int length
)
282 #ifdef AUTHENTICATION
285 extern int want_status_response
;
287 if (showoptions
|| direction
== 0 ||
288 (want_status_response
&& (pointer
[0] == TELOPT_STATUS
))) {
290 fprintf(NetTrace
, "%s IAC SB ",
291 (direction
== '<')? "RCVD":"SENT");
295 i
= pointer
[length
-2];
296 j
= pointer
[length
-1];
298 if (i
!= IAC
|| j
!= SE
) {
299 fprintf(NetTrace
, "(terminated by ");
301 fprintf(NetTrace
, "%s ", TELOPT(i
));
302 else if (TELCMD_OK(i
))
303 fprintf(NetTrace
, "%s ", TELCMD(i
));
305 fprintf(NetTrace
, "%d ", i
);
307 fprintf(NetTrace
, "%s", TELOPT(j
));
308 else if (TELCMD_OK(j
))
309 fprintf(NetTrace
, "%s", TELCMD(j
));
311 fprintf(NetTrace
, "%d", j
);
312 fprintf(NetTrace
, ", not IAC SE!) ");
318 fprintf(NetTrace
, "(Empty suboption??\?)");
319 if (NetTrace
== stdout
)
323 switch (pointer
[0]) {
325 fprintf(NetTrace
, "TERMINAL-TYPE ");
326 switch (pointer
[1]) {
328 fprintf(NetTrace
, "IS \"%.*s\"", length
-2, (char *)pointer
+2);
331 fprintf(NetTrace
, "SEND");
335 "- unknown qualifier %d (0x%x).",
336 pointer
[1], pointer
[1]);
340 fprintf(NetTrace
, "TERMINAL-SPEED");
342 fprintf(NetTrace
, " (empty suboption??\?)");
345 switch (pointer
[1]) {
347 fprintf(NetTrace
, " IS ");
348 fprintf(NetTrace
, "%.*s", length
-2, (char *)pointer
+2);
352 fprintf(NetTrace
, " SEND");
354 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
355 for (i
= 2; i
< length
; i
++)
356 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
362 fprintf(NetTrace
, "TOGGLE-FLOW-CONTROL");
364 fprintf(NetTrace
, " (empty suboption??\?)");
367 switch (pointer
[1]) {
369 fprintf(NetTrace
, " OFF"); break;
371 fprintf(NetTrace
, " ON"); break;
372 case LFLOW_RESTART_ANY
:
373 fprintf(NetTrace
, " RESTART-ANY"); break;
374 case LFLOW_RESTART_XON
:
375 fprintf(NetTrace
, " RESTART-XON"); break;
377 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
379 for (i
= 2; i
< length
; i
++)
380 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
384 fprintf(NetTrace
, "NAWS");
386 fprintf(NetTrace
, " (empty suboption??\?)");
390 fprintf(NetTrace
, " ?%d?", pointer
[1]);
393 fprintf(NetTrace
, " %d %d (%d)",
394 pointer
[1], pointer
[2],
395 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
397 fprintf(NetTrace
, " ?%d?", pointer
[3]);
400 fprintf(NetTrace
, " %d %d (%d)",
401 pointer
[3], pointer
[4],
402 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
403 for (i
= 5; i
< length
; i
++)
404 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
407 #ifdef AUTHENTICATION
408 case TELOPT_AUTHENTICATION
:
409 fprintf(NetTrace
, "AUTHENTICATION");
411 fprintf(NetTrace
, " (empty suboption??\?)");
414 switch (pointer
[1]) {
417 fprintf(NetTrace
, " %s ", (pointer
[1] == TELQUAL_IS
) ?
419 if (AUTHTYPE_NAME_OK(pointer
[2]))
420 fprintf(NetTrace
, "%s ", AUTHTYPE_NAME(pointer
[2]));
422 fprintf(NetTrace
, "%d ", pointer
[2]);
424 fprintf(NetTrace
, "(partial suboption??\?)");
427 fprintf(NetTrace
, "%s|%s",
428 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
430 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
431 "MUTUAL" : "ONE-WAY");
433 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
434 fprintf(NetTrace
, "%s", buf
);
439 fprintf(NetTrace
, " SEND ");
441 if (AUTHTYPE_NAME_OK(pointer
[i
]))
442 fprintf(NetTrace
, "%s ", AUTHTYPE_NAME(pointer
[i
]));
444 fprintf(NetTrace
, "%d ", pointer
[i
]);
446 fprintf(NetTrace
, "(partial suboption??\?)");
449 fprintf(NetTrace
, "%s|%s ",
450 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
452 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
453 "MUTUAL" : "ONE-WAY");
460 fprintf(NetTrace
, " NAME \"");
462 putc(pointer
[i
++], NetTrace
);
467 for (i
= 2; i
< length
; i
++)
468 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
476 fprintf(NetTrace
, "ENCRYPT");
478 fprintf(NetTrace
, " (empty suboption??\?)");
481 switch (pointer
[1]) {
483 fprintf(NetTrace
, " START");
487 fprintf(NetTrace
, " END");
490 case ENCRYPT_REQSTART
:
491 fprintf(NetTrace
, " REQUEST-START");
495 fprintf(NetTrace
, " REQUEST-END");
500 fprintf(NetTrace
, " %s ", (pointer
[1] == ENCRYPT_IS
) ?
503 fprintf(NetTrace
, " (partial suboption??\?)");
506 if (ENCTYPE_NAME_OK(pointer
[2]))
507 fprintf(NetTrace
, "%s ", ENCTYPE_NAME(pointer
[2]));
509 fprintf(NetTrace
, " %d (unknown)", pointer
[2]);
511 encrypt_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
512 fprintf(NetTrace
, "%s", buf
);
515 case ENCRYPT_SUPPORT
:
517 fprintf(NetTrace
, " SUPPORT ");
519 if (ENCTYPE_NAME_OK(pointer
[i
]))
520 fprintf(NetTrace
, "%s ", ENCTYPE_NAME(pointer
[i
]));
522 fprintf(NetTrace
, "%d ", pointer
[i
]);
527 case ENCRYPT_ENC_KEYID
:
528 fprintf(NetTrace
, " ENC_KEYID ");
531 case ENCRYPT_DEC_KEYID
:
532 fprintf(NetTrace
, " DEC_KEYID ");
536 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
538 for (i
= 2; i
< length
; i
++)
539 fprintf(NetTrace
, " %d", pointer
[i
]);
543 #endif /* ENCRYPTION */
545 case TELOPT_LINEMODE
:
546 fprintf(NetTrace
, "LINEMODE ");
548 fprintf(NetTrace
, " (empty suboption??\?)");
551 switch (pointer
[1]) {
553 fprintf(NetTrace
, "WILL ");
556 fprintf(NetTrace
, "WONT ");
559 fprintf(NetTrace
, "DO ");
562 fprintf(NetTrace
, "DONT ");
565 fprintf(NetTrace
, "(no option??\?)");
568 switch (pointer
[2]) {
570 fprintf(NetTrace
, "Forward Mask");
571 for (i
= 3; i
< length
; i
++)
572 fprintf(NetTrace
, " %x", pointer
[i
]);
575 fprintf(NetTrace
, "%d (unknown)", pointer
[2]);
576 for (i
= 3; i
< length
; i
++)
577 fprintf(NetTrace
, " %d", pointer
[i
]);
583 fprintf(NetTrace
, "SLC");
584 for (i
= 2; i
< length
- 2; i
+= 3) {
585 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
586 fprintf(NetTrace
, " %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
588 fprintf(NetTrace
, " %d", pointer
[i
+SLC_FUNC
]);
589 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
591 fprintf(NetTrace
, " NOSUPPORT"); break;
593 fprintf(NetTrace
, " CANTCHANGE"); break;
595 fprintf(NetTrace
, " VARIABLE"); break;
597 fprintf(NetTrace
, " DEFAULT"); break;
599 fprintf(NetTrace
, "%s%s%s",
600 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
601 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
602 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
603 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
604 SLC_FLUSHOUT
| SLC_LEVELBITS
))
605 fprintf(NetTrace
, "(0x%x)", pointer
[i
+SLC_FLAGS
]);
606 fprintf(NetTrace
, " %d;", pointer
[i
+SLC_VALUE
]);
607 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
608 (pointer
[i
+SLC_VALUE
+1] == IAC
))
611 for (; i
< length
; i
++)
612 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
616 fprintf(NetTrace
, "MODE ");
618 fprintf(NetTrace
, "(no mode??\?)");
623 sprintf(tbuf
, "%s%s%s%s%s",
624 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
625 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
626 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
627 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
628 pointer
[2]&MODE_ACK
? "|ACK" : "");
629 fprintf(NetTrace
, "%s", tbuf
[1] ? &tbuf
[1] : "0");
631 if (pointer
[2]&~(MODE_MASK
))
632 fprintf(NetTrace
, " (0x%x)", pointer
[2]);
633 for (i
= 3; i
< length
; i
++)
634 fprintf(NetTrace
, " ?0x%x?", pointer
[i
]);
637 fprintf(NetTrace
, "%d (unknown)", pointer
[1]);
638 for (i
= 2; i
< length
; i
++)
639 fprintf(NetTrace
, " %d", pointer
[i
]);
643 case TELOPT_STATUS
: {
647 fprintf(NetTrace
, "STATUS");
649 switch (pointer
[1]) {
651 if (pointer
[1] == TELQUAL_SEND
)
652 fprintf(NetTrace
, " SEND");
654 fprintf(NetTrace
, " %d (unknown)", pointer
[1]);
655 for (i
= 2; i
< length
; i
++)
656 fprintf(NetTrace
, " ?%d?", pointer
[i
]);
659 if (--want_status_response
< 0)
660 want_status_response
= 0;
661 if (NetTrace
== stdout
)
662 fprintf(NetTrace
, " IS\r\n");
664 fprintf(NetTrace
, " IS\n");
666 for (i
= 2; i
< length
; i
++) {
668 case DO
: cp
= "DO"; goto common2
;
669 case DONT
: cp
= "DONT"; goto common2
;
670 case WILL
: cp
= "WILL"; goto common2
;
671 case WONT
: cp
= "WONT"; goto common2
;
674 if (TELOPT_OK((int)pointer
[i
]))
675 fprintf(NetTrace
, " %s %s", cp
, TELOPT(pointer
[i
]));
677 fprintf(NetTrace
, " %s %d", cp
, pointer
[i
]);
679 if (NetTrace
== stdout
)
680 fprintf(NetTrace
, "\r\n");
682 fprintf(NetTrace
, "\n");
686 fprintf(NetTrace
, " SB ");
690 if (pointer
[j
] == SE
) {
693 if (pointer
[j
+1] == SE
)
698 pointer
[k
++] = pointer
[j
++];
700 printsub(0, &pointer
[i
], k
- i
);
702 fprintf(NetTrace
, " SE");
707 if (NetTrace
== stdout
)
708 fprintf(NetTrace
, "\r\n");
710 fprintf(NetTrace
, "\n");
715 fprintf(NetTrace
, " %d", pointer
[i
]);
724 case TELOPT_XDISPLOC
:
725 fprintf(NetTrace
, "X-DISPLAY-LOCATION ");
726 switch (pointer
[1]) {
728 fprintf(NetTrace
, "IS \"%.*s\"", length
-2, (char *)pointer
+2);
731 fprintf(NetTrace
, "SEND");
734 fprintf(NetTrace
, "- unknown qualifier %d (0x%x).",
735 pointer
[1], pointer
[1]);
739 case TELOPT_NEW_ENVIRON
:
740 fprintf(NetTrace
, "NEW-ENVIRON ");
743 case TELOPT_OLD_ENVIRON
:
744 fprintf(NetTrace
, "OLD-ENVIRON");
747 switch (pointer
[1]) {
749 fprintf(NetTrace
, "IS ");
752 fprintf(NetTrace
, "SEND ");
755 fprintf(NetTrace
, "INFO ");
759 #if defined(ENV_HACK) && defined(OLD_ENVIRON)
760 extern int old_env_var
, old_env_value
;
762 for (i
= 2; i
< length
; i
++ ) {
763 switch (pointer
[i
]) {
766 /* case NEW_ENV_OVAR: */
767 if (pointer
[0] == TELOPT_OLD_ENVIRON
) {
769 if (old_env_var
== OLD_ENV_VALUE
)
770 fprintf(NetTrace
, "\" (VALUE) " + noquote
);
773 fprintf(NetTrace
, "\" VAR " + noquote
);
775 #endif /* OLD_ENVIRON */
776 fprintf(NetTrace
, "%s", "\" VALUE " + noquote
);
782 /* case OLD_ENV_VALUE: */
783 if (pointer
[0] == TELOPT_OLD_ENVIRON
) {
785 if (old_env_value
== OLD_ENV_VAR
)
786 fprintf(NetTrace
, "\" (VAR) " + noquote
);
789 fprintf(NetTrace
, "\" VALUE " + noquote
);
791 #endif /* OLD_ENVIRON */
792 fprintf(NetTrace
, "%s", "\" VAR " + noquote
);
797 fprintf(NetTrace
, "%s", "\" ESC " + noquote
);
802 fprintf(NetTrace
, "%s", "\" USERVAR " + noquote
);
807 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
812 putc(pointer
[i
], NetTrace
);
814 fprintf(NetTrace
, "\" %03o " + noquote
,
829 if (TELOPT_OK(pointer
[0]))
830 fprintf(NetTrace
, "%s (unknown)", TELOPT(pointer
[0]));
832 fprintf(NetTrace
, "%d (unknown)", pointer
[0]);
833 for (i
= 1; i
< length
; i
++)
834 fprintf(NetTrace
, " %d", pointer
[i
]);
838 if (NetTrace
== stdout
)
839 fprintf(NetTrace
, "\r\n");
841 fprintf(NetTrace
, "\n");
843 if (NetTrace
== stdout
)
848 /* EmptyTerminal - called to make sure that the terminal buffer is empty.
849 * Note that we consider the buffer to run all the
850 * way to the kernel (thus the select).
860 if (TTYBYTES() == 0) {
862 (void) select(tout
+1, NULL
, &o
, NULL
, NULL
); /* wait for TTLOWAT */
867 (void) select(tout
+1, NULL
, &o
, NULL
, NULL
); /* wait for TTLOWAT */
877 (void)telrcv(); /* Process any incoming data */
879 } while (ring_full_count(&netiring
)); /* While there is any */
884 EmptyTerminal(); /* Flush the path to the tty */
896 ExitString(const char *string
, int returnCode
)
899 fwrite(string
, 1, strlen(string
), stderr
);