2 * Copyright (c) 1989, 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
36 static const char sccsid[] = "@(#)utility.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
44 #include <sys/utsname.h>
51 #include <libtelnet/auth.h>
54 #include <libtelnet/encrypt.h>
58 * utility functions performing io related tasks
64 * A small subroutine to flush the network output buffer, get some data
65 * from the network, and pass it through the telnet state machine. We
66 * also flush the pty input buffer (by dropping its data) if it becomes
74 DIAG(TD_REPORT, output_data("td: ttloop\r\n"));
75 if (nfrontp - nbackp > 0) {
78 ncc = read(net, netibuf, sizeof netibuf);
80 syslog(LOG_INFO, "ttloop: read: %m");
82 } else if (ncc == 0) {
83 syslog(LOG_INFO, "ttloop: peer died: %m");
86 DIAG(TD_REPORT, output_data("td: ttloop read %d chars\r\n", ncc));
88 telrcv(); /* state machine */
90 pfrontp = pbackp = ptyobuf;
96 * Check a descriptor to see if out of band data exists on it.
101 static struct timeval timeout = { 0, 0 };
108 memset((char *)&timeout, 0, sizeof timeout);
109 value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
110 } while ((value == -1) && (errno == EINTR));
113 fatalperror(pty, "select");
115 if (FD_ISSET(s, &excepts)) {
127 if ((n = pfrontp - pbackp) > 0) {
128 DIAG(TD_REPORT | TD_PTYDATA,
129 output_data("td: ptyflush %d chars\r\n", n));
130 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
131 n = write(pty, pbackp, n);
134 if (errno == EWOULDBLOCK || errno == EINTR)
139 if (pbackp == pfrontp)
140 pbackp = pfrontp = ptyobuf;
146 * Return the address of the next "item" in the TELNET data
147 * stream. This will be the address of the next character if
148 * the current address is a user data character, or it will
149 * be the address of the character following the TELNET command
150 * if the current address is a TELNET IAC ("I Am a Command")
154 nextitem(char *current)
156 if ((*current&0xff) != IAC) {
159 switch (*(current+1)&0xff) {
165 case SB: /* loop forever looking for the SE */
167 char *look = current+2;
170 if ((*look++&0xff) == IAC) {
171 if ((*look++&0xff) == SE) {
180 } /* end of nextitem */
185 * We are about to do a TELNET SYNCH operation. Clear
186 * the path to the network.
188 * Things are a bit tricky since we may have sent the first
189 * byte or so of a previous TELNET command into the network.
190 * So, we have to scan the network buffer from the beginning
191 * until we are up to where we want to be.
193 * A side effect of what we do, just to keep things
194 * simple, is to clear the urgent data pointer. The principal
195 * caller should be setting the urgent data pointer AFTER calling
201 char *thisitem, *next;
203 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
204 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
207 thisitem = nclearto > netobuf ? nclearto : netobuf;
208 #else /* ENCRYPTION */
210 #endif /* ENCRYPTION */
212 while ((next = nextitem(thisitem)) <= nbackp) {
216 /* Now, thisitem is first before/at boundary. */
219 good = nclearto > netobuf ? nclearto : netobuf;
220 #else /* ENCRYPTION */
221 good = netobuf; /* where the good bytes go */
222 #endif /* ENCRYPTION */
224 while (nfrontp > thisitem) {
225 if (wewant(thisitem)) {
230 next = nextitem(next);
231 } while (wewant(next) && (nfrontp > next));
232 length = next-thisitem;
233 memmove(good, thisitem, length);
237 thisitem = nextitem(thisitem);
242 nfrontp = good; /* next byte to be sent */
244 } /* end of netclear */
248 * Send as much data as possible to the network,
249 * handling requests for urgent data.
257 while ((n = nfrontp - nbackp) > 0) {
259 /* XXX This causes output_data() to recurse and die */
261 n += output_data("td: netflush %d chars\r\n", n);
265 if (encrypt_output) {
266 char *s = nclearto ? nclearto : nbackp;
267 if (nfrontp - s > 0) {
268 (*encrypt_output)((unsigned char *)s, nfrontp-s);
272 #endif /* ENCRYPTION */
274 * if no urgent data, or if the other side appears to be an
275 * old 4.2 client (and thus unable to survive TCP urgent data),
276 * write the entire buffer in non-OOB mode.
278 if ((neturg == 0) || (not42 == 0)) {
279 n = write(net, nbackp, n); /* normal write */
283 * In 4.2 (and 4.3) systems, there is some question about
284 * what byte in a sendOOB operation is the "OOB" data.
285 * To make ourselves compatible, we only send ONE byte
286 * out of band, the one WE THINK should be OOB (though
287 * we really have more the TCP philosophy of urgent data
288 * rather than the Unix philosophy of OOB data).
291 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
293 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
297 if (errno == EWOULDBLOCK || errno == EINTR)
304 if (nbackp > nclearto)
306 #endif /* ENCRYPTION */
307 if (nbackp >= neturg) {
310 if (nbackp == nfrontp) {
311 nbackp = nfrontp = netobuf;
314 #endif /* ENCRYPTION */
318 } /* end of netflush */
322 * miscellaneous functions doing a variety of little jobs follow ...
327 fatal(int f, const char *msg)
331 (void) snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
333 if (encrypt_output) {
335 * Better turn off encryption first....
341 #endif /* ENCRYPTION */
342 (void) write(f, buf, (int)strlen(buf));
348 fatalperror(int f, const char *msg)
352 (void) snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
359 edithost(char *pat, char *host)
361 char *res = editedhost;
382 if (res == &editedhost[sizeof editedhost - 1]) {
389 (void) strncpy(res, host,
390 sizeof editedhost - (res - editedhost) -1);
393 editedhost[sizeof editedhost - 1] = '\0';
396 static char *putlocation;
399 putstr(const char *s)
413 static char fmtstr[] = { "%+" };
415 static char fmtstr[] = { "%l:%M%P on %A, %d %B %Y" };
419 putf(char *cp, char *where)
425 static struct utsname kerninfo;
427 if (!*kerninfo.sysname)
438 } else if (*cp != '%') {
446 /* names are like /dev/pts/2 -- we want pts/2 */
447 slash = strchr(line+1, '/');
449 slash = strrchr(line, '/');
451 if (slash == (char *) 0)
463 setlocale(LC_TIME, "");
466 (void)strftime(db, sizeof(db), fmtstr, localtime(&t));
472 putstr(kerninfo.sysname);
476 putstr(kerninfo.machine);
480 putstr(kerninfo.release);
484 putstr(kerninfo.version);
498 * Print telnet options and commands in plain text, if possible.
501 printoption(const char *fmt, int option)
503 if (TELOPT_OK(option))
504 output_data("%s %s\r\n", fmt, TELOPT(option));
505 else if (TELCMD_OK(option))
506 output_data("%s %s\r\n", fmt, TELCMD(option));
508 output_data("%s %d\r\n", fmt, option);
513 printsub(char direction, unsigned char *pointer, int length)
517 if (!(diagnostic & TD_OPTIONS))
521 output_data("td: %s suboption ",
522 direction == '<' ? "recv" : "send");
526 i = pointer[length-2];
527 j = pointer[length-1];
529 if (i != IAC || j != SE) {
530 output_data("(terminated by ");
532 output_data("%s ", TELOPT(i));
533 else if (TELCMD_OK(i))
534 output_data("%s ", TELCMD(i));
536 output_data("%d ", i);
538 output_data("%s", TELOPT(j));
539 else if (TELCMD_OK(j))
540 output_data("%s", TELCMD(j));
542 output_data("%d", j);
543 output_data(", not IAC SE!) ");
549 output_data("(Empty suboption??\?)");
552 switch (pointer[0]) {
554 output_data("TERMINAL-TYPE ");
555 switch (pointer[1]) {
557 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
564 "- unknown qualifier %d (0x%x).",
565 pointer[1], pointer[1]);
569 output_data("TERMINAL-SPEED");
571 output_data(" (empty suboption??\?)");
574 switch (pointer[1]) {
576 output_data(" IS %.*s", length-2, (char *)pointer+2);
580 output_data(" SEND");
582 output_data(" %d (unknown)", pointer[1]);
583 for (i = 2; i < length; i++) {
584 output_data(" ?%d?", pointer[i]);
591 output_data("TOGGLE-FLOW-CONTROL");
593 output_data(" (empty suboption??\?)");
596 switch (pointer[1]) {
598 output_data(" OFF"); break;
600 output_data(" ON"); break;
601 case LFLOW_RESTART_ANY:
602 output_data(" RESTART-ANY"); break;
603 case LFLOW_RESTART_XON:
604 output_data(" RESTART-XON"); break;
606 output_data(" %d (unknown)", pointer[1]);
608 for (i = 2; i < length; i++) {
609 output_data(" ?%d?", pointer[i]);
616 output_data(" (empty suboption??\?)");
620 output_data(" ?%d?", pointer[1]);
623 output_data(" %d %d (%d)",
624 pointer[1], pointer[2],
625 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
627 output_data(" ?%d?", pointer[3]);
630 output_data(" %d %d (%d)",
631 pointer[3], pointer[4],
632 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
633 for (i = 5; i < length; i++) {
634 output_data(" ?%d?", pointer[i]);
638 case TELOPT_LINEMODE:
639 output_data("LINEMODE ");
641 output_data(" (empty suboption??\?)");
644 switch (pointer[1]) {
646 output_data("WILL ");
649 output_data("WONT ");
655 output_data("DONT ");
658 output_data("(no option??\?)");
661 switch (pointer[2]) {
663 output_data("Forward Mask");
664 for (i = 3; i < length; i++) {
665 output_data(" %x", pointer[i]);
669 output_data("%d (unknown)", pointer[2]);
670 for (i = 3; i < length; i++) {
671 output_data(" %d", pointer[i]);
679 for (i = 2; i < length - 2; i += 3) {
680 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
681 output_data(" %s", SLC_NAME(pointer[i+SLC_FUNC]));
683 output_data(" %d", pointer[i+SLC_FUNC]);
684 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
686 output_data(" NOSUPPORT"); break;
688 output_data(" CANTCHANGE"); break;
690 output_data(" VARIABLE"); break;
692 output_data(" DEFAULT"); break;
694 output_data("%s%s%s",
695 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
696 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
697 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
698 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
699 SLC_FLUSHOUT| SLC_LEVELBITS)) {
700 output_data("(0x%x)", pointer[i+SLC_FLAGS]);
702 output_data(" %d;", pointer[i+SLC_VALUE]);
703 if ((pointer[i+SLC_VALUE] == IAC) &&
704 (pointer[i+SLC_VALUE+1] == IAC))
707 for (; i < length; i++) {
708 output_data(" ?%d?", pointer[i]);
713 output_data("MODE ");
715 output_data("(no mode??\?)");
720 sprintf(tbuf, "%s%s%s%s%s",
721 pointer[2]&MODE_EDIT ? "|EDIT" : "",
722 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
723 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
724 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
725 pointer[2]&MODE_ACK ? "|ACK" : "");
726 output_data("%s", tbuf[1] ? &tbuf[1] : "0");
728 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
729 output_data(" (0x%x)", pointer[2]);
731 for (i = 3; i < length; i++) {
732 output_data(" ?0x%x?", pointer[i]);
736 output_data("%d (unknown)", pointer[1]);
737 for (i = 2; i < length; i++) {
738 output_data(" %d", pointer[i]);
743 case TELOPT_STATUS: {
747 output_data("STATUS");
749 switch (pointer[1]) {
751 if (pointer[1] == TELQUAL_SEND)
752 output_data(" SEND");
754 output_data(" %d (unknown)", pointer[1]);
755 for (i = 2; i < length; i++) {
756 output_data(" ?%d?", pointer[i]);
760 output_data(" IS\r\n");
762 for (i = 2; i < length; i++) {
764 case DO: cp = "DO"; goto common2;
765 case DONT: cp = "DONT"; goto common2;
766 case WILL: cp = "WILL"; goto common2;
767 case WONT: cp = "WONT"; goto common2;
770 if (TELOPT_OK(pointer[i]))
771 output_data(" %s %s", cp, TELOPT(pointer[i]));
773 output_data(" %s %d", cp, pointer[i]);
783 if (pointer[j] == SE) {
786 if (pointer[j+1] == SE)
791 pointer[k++] = pointer[j++];
793 printsub(0, &pointer[i], k - i);
805 output_data(" %d", pointer[i]);
814 case TELOPT_XDISPLOC:
815 output_data("X-DISPLAY-LOCATION ");
816 switch (pointer[1]) {
818 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
824 output_data("- unknown qualifier %d (0x%x).",
825 pointer[1], pointer[1]);
829 case TELOPT_NEW_ENVIRON:
830 output_data("NEW-ENVIRON ");
832 case TELOPT_OLD_ENVIRON:
833 output_data("OLD-ENVIRON");
835 switch (pointer[1]) {
840 output_data("SEND ");
843 output_data("INFO ");
847 for (i = 2; i < length; i++ ) {
848 switch (pointer[i]) {
850 output_data("%s", "\" VAR " + noquote);
855 output_data("%s", "\" VALUE " + noquote);
860 output_data("%s", "\" ESC " + noquote);
865 output_data("%s", "\" USERVAR " + noquote);
870 if (isprint(pointer[i]) && pointer[i] != '"') {
875 output_data("%c", pointer[i]);
877 output_data("\" %03o " + noquote,
891 #ifdef AUTHENTICATION
892 case TELOPT_AUTHENTICATION:
893 output_data("AUTHENTICATION");
896 output_data(" (empty suboption??\?)");
899 switch (pointer[1]) {
902 output_data(" %s ", (pointer[1] == TELQUAL_IS) ?
904 if (AUTHTYPE_NAME_OK(pointer[2]))
905 output_data("%s ", AUTHTYPE_NAME(pointer[2]));
907 output_data("%d ", pointer[2]);
909 output_data("(partial suboption??\?)");
913 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
915 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
916 "MUTUAL" : "ONE-WAY");
920 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
921 output_data("%s", buf);
927 output_data(" SEND ");
929 if (AUTHTYPE_NAME_OK(pointer[i]))
930 output_data("%s ", AUTHTYPE_NAME(pointer[i]));
932 output_data("%d ", pointer[i]);
934 output_data("(partial suboption??\?)");
937 output_data("%s|%s ",
938 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
940 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
941 "MUTUAL" : "ONE-WAY");
947 output_data(" NAME \"%.*s\"", length - 2, pointer + 2);
951 for (i = 2; i < length; i++) {
952 output_data(" ?%d?", pointer[i]);
961 output_data("ENCRYPT");
963 output_data(" (empty suboption??\?)");
966 switch (pointer[1]) {
968 output_data(" START");
975 case ENCRYPT_REQSTART:
976 output_data(" REQUEST-START");
980 output_data(" REQUEST-END");
985 output_data(" %s ", (pointer[1] == ENCRYPT_IS) ?
988 output_data(" (partial suboption??\?)");
991 if (ENCTYPE_NAME_OK(pointer[2]))
992 output_data("%s ", ENCTYPE_NAME(pointer[2]));
994 output_data(" %d (unknown)", pointer[2]);
998 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
999 output_data("%s", buf);
1003 case ENCRYPT_SUPPORT:
1005 output_data(" SUPPORT ");
1006 while (i < length) {
1007 if (ENCTYPE_NAME_OK(pointer[i]))
1008 output_data("%s ", ENCTYPE_NAME(pointer[i]));
1010 output_data("%d ", pointer[i]);
1015 case ENCRYPT_ENC_KEYID:
1016 output_data(" ENC_KEYID");
1019 case ENCRYPT_DEC_KEYID:
1020 output_data(" DEC_KEYID");
1024 output_data(" %d (unknown)", pointer[1]);
1026 for (i = 2; i < length; i++) {
1027 output_data(" %d", pointer[i]);
1032 #endif /* ENCRYPTION */
1035 if (TELOPT_OK(pointer[0]))
1036 output_data("%s (unknown)", TELOPT(pointer[0]));
1038 output_data("%d (unknown)", pointer[i]);
1039 for (i = 1; i < length; i++) {
1040 output_data(" %d", pointer[i]);
1044 output_data("\r\n");
1048 * Dump a data buffer in hex and ascii to the output data stream.
1051 printdata(const char *tag, char *ptr, int cnt)
1057 /* flush net output buffer if no room for new data) */
1058 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1062 /* add a line of output */
1063 output_data("%s: ", tag);
1064 for (i = 0; i < 20 && cnt; i++) {
1065 output_data("%02x", *ptr);
1066 if (isprint(*ptr)) {
1078 output_data(" %s\r\n", xbuf );
1081 #endif /* DIAGNOSTICS */