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
33 * @(#)utility.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnetd/utility.c,v 1.5.2.4 2002/04/13 10:59:09 markm Exp $
39 #include <sys/utsname.h>
46 #include <libtelnet/auth.h>
49 #include <libtelnet/encrypt.h>
53 * utility functions performing io related tasks
59 * A small subroutine to flush the network output buffer, get some data
60 * from the network, and pass it through the telnet state machine. We
61 * also flush the pty input buffer (by dropping its data) if it becomes
69 DIAG(TD_REPORT, output_data("td: ttloop\r\n"));
70 if (nfrontp - nbackp > 0) {
73 ncc = read(net, netibuf, sizeof netibuf);
75 syslog(LOG_INFO, "ttloop: read: %m");
77 } else if (ncc == 0) {
78 syslog(LOG_INFO, "ttloop: peer died: %m");
81 DIAG(TD_REPORT, output_data("td: ttloop read %d chars\r\n", ncc));
83 telrcv(); /* state machine */
85 pfrontp = pbackp = ptyobuf;
91 * Check a descriptor to see if out of band data exists on it.
96 static struct timeval timeout = { 0, 0 };
103 memset((char *)&timeout, 0, sizeof timeout);
104 value = select(s+1, NULL, NULL, &excepts, &timeout);
105 } while ((value == -1) && (errno == EINTR));
108 fatalperror(pty, "select");
110 if (FD_ISSET(s, &excepts)) {
122 if ((n = pfrontp - pbackp) > 0) {
123 DIAG(TD_REPORT | TD_PTYDATA,
124 output_data("td: ptyflush %d chars\r\n", n));
125 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
126 n = write(pty, pbackp, n);
129 if (errno == EWOULDBLOCK || errno == EINTR)
134 if (pbackp == pfrontp)
135 pbackp = pfrontp = ptyobuf;
141 * Return the address of the next "item" in the TELNET data
142 * stream. This will be the address of the next character if
143 * the current address is a user data character, or it will
144 * be the address of the character following the TELNET command
145 * if the current address is a TELNET IAC ("I Am a Command")
149 nextitem(char *current)
151 if ((*current&0xff) != IAC) {
154 switch (*(current+1)&0xff) {
160 case SB: /* loop forever looking for the SE */
162 char *look = current+2;
165 if ((*look++&0xff) == IAC) {
166 if ((*look++&0xff) == SE) {
175 } /* end of nextitem */
180 * We are about to do a TELNET SYNCH operation. Clear
181 * the path to the network.
183 * Things are a bit tricky since we may have sent the first
184 * byte or so of a previous TELNET command into the network.
185 * So, we have to scan the network buffer from the beginning
186 * until we are up to where we want to be.
188 * A side effect of what we do, just to keep things
189 * simple, is to clear the urgent data pointer. The principal
190 * caller should be setting the urgent data pointer AFTER calling
196 char *thisitem, *next;
198 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
199 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
202 thisitem = nclearto > netobuf ? nclearto : netobuf;
203 #else /* ENCRYPTION */
205 #endif /* ENCRYPTION */
207 while ((next = nextitem(thisitem)) <= nbackp) {
211 /* Now, thisitem is first before/at boundary. */
214 good = nclearto > netobuf ? nclearto : netobuf;
215 #else /* ENCRYPTION */
216 good = netobuf; /* where the good bytes go */
217 #endif /* ENCRYPTION */
219 while (nfrontp > thisitem) {
220 if (wewant(thisitem)) {
225 next = nextitem(next);
226 } while (wewant(next) && (nfrontp > next));
227 length = next-thisitem;
228 memmove(good, thisitem, length);
232 thisitem = nextitem(thisitem);
237 nfrontp = good; /* next byte to be sent */
239 } /* end of netclear */
243 * Send as much data as possible to the network,
244 * handling requests for urgent data.
252 while ((n = nfrontp - nbackp) > 0) {
254 /* XXX This causes output_data() to recurse and die */
256 n += output_data("td: netflush %d chars\r\n", n);
260 if (encrypt_output) {
261 char *s = nclearto ? nclearto : nbackp;
262 if (nfrontp - s > 0) {
263 (*encrypt_output)((unsigned char *)s, nfrontp-s);
267 #endif /* ENCRYPTION */
269 * if no urgent data, or if the other side appears to be an
270 * old 4.2 client (and thus unable to survive TCP urgent data),
271 * write the entire buffer in non-OOB mode.
273 if ((neturg == 0) || (not42 == 0)) {
274 n = write(net, nbackp, n); /* normal write */
278 * In 4.2 (and 4.3) systems, there is some question about
279 * what byte in a sendOOB operation is the "OOB" data.
280 * To make ourselves compatible, we only send ONE byte
281 * out of band, the one WE THINK should be OOB (though
282 * we really have more the TCP philosophy of urgent data
283 * rather than the Unix philosophy of OOB data).
286 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
288 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
292 if (errno == EWOULDBLOCK || errno == EINTR)
299 if (nbackp > nclearto)
301 #endif /* ENCRYPTION */
302 if (nbackp >= neturg) {
305 if (nbackp == nfrontp) {
306 nbackp = nfrontp = netobuf;
309 #endif /* ENCRYPTION */
313 } /* end of netflush */
317 * miscellaneous functions doing a variety of little jobs follow ...
322 fatal(int f, const char *msg)
326 (void) snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
328 if (encrypt_output) {
330 * Better turn off encryption first....
336 #endif /* ENCRYPTION */
337 (void) write(f, buf, (int)strlen(buf));
343 fatalperror(int f, const char *msg)
347 (void) snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
354 edithost(char *pat, char *host)
356 char *res = editedhost;
377 if (res == &editedhost[sizeof editedhost - 1]) {
384 (void) strncpy(res, host,
385 sizeof editedhost - (res - editedhost) -1);
388 editedhost[sizeof editedhost - 1] = '\0';
391 static char *putlocation;
394 putstr(const char *s)
408 static char fmtstr[] = { "%+" };
410 static char fmtstr[] = { "%l:%M%P on %A, %d %B %Y" };
414 putf(char *cp, char *where)
420 static struct utsname kerninfo;
422 if (!*kerninfo.sysname)
433 } else if (*cp != '%') {
441 /* names are like /dev/pts/2 -- we want pts/2 */
442 slash = strchr(line+1, '/');
444 slash = strrchr(line, '/');
446 if (slash == (char *) 0)
458 setlocale(LC_TIME, "");
461 (void)strftime(db, sizeof(db), fmtstr, localtime(&t));
467 putstr(kerninfo.sysname);
471 putstr(kerninfo.machine);
475 putstr(kerninfo.release);
479 putstr(kerninfo.version);
493 * Print telnet options and commands in plain text, if possible.
496 printoption(const char *fmt, int option)
498 if (TELOPT_OK(option))
499 output_data("%s %s\r\n", fmt, TELOPT(option));
500 else if (TELCMD_OK(option))
501 output_data("%s %s\r\n", fmt, TELCMD(option));
503 output_data("%s %d\r\n", fmt, option);
508 printsub(char direction, unsigned char *pointer, int length)
512 if (!(diagnostic & TD_OPTIONS))
516 output_data("td: %s suboption ",
517 direction == '<' ? "recv" : "send");
521 i = pointer[length-2];
522 j = pointer[length-1];
524 if (i != IAC || j != SE) {
525 output_data("(terminated by ");
527 output_data("%s ", TELOPT(i));
528 else if (TELCMD_OK(i))
529 output_data("%s ", TELCMD(i));
531 output_data("%d ", i);
533 output_data("%s", TELOPT(j));
534 else if (TELCMD_OK(j))
535 output_data("%s", TELCMD(j));
537 output_data("%d", j);
538 output_data(", not IAC SE!) ");
544 output_data("(Empty suboption??\?)");
547 switch (pointer[0]) {
549 output_data("TERMINAL-TYPE ");
550 switch (pointer[1]) {
552 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
559 "- unknown qualifier %d (0x%x).",
560 pointer[1], pointer[1]);
564 output_data("TERMINAL-SPEED");
566 output_data(" (empty suboption??\?)");
569 switch (pointer[1]) {
571 output_data(" IS %.*s", length-2, (char *)pointer+2);
575 output_data(" SEND");
577 output_data(" %d (unknown)", pointer[1]);
578 for (i = 2; i < length; i++) {
579 output_data(" ?%d?", pointer[i]);
586 output_data("TOGGLE-FLOW-CONTROL");
588 output_data(" (empty suboption??\?)");
591 switch (pointer[1]) {
593 output_data(" OFF"); break;
595 output_data(" ON"); break;
596 case LFLOW_RESTART_ANY:
597 output_data(" RESTART-ANY"); break;
598 case LFLOW_RESTART_XON:
599 output_data(" RESTART-XON"); break;
601 output_data(" %d (unknown)", pointer[1]);
603 for (i = 2; i < length; i++) {
604 output_data(" ?%d?", pointer[i]);
611 output_data(" (empty suboption??\?)");
615 output_data(" ?%d?", pointer[1]);
618 output_data(" %d %d (%d)",
619 pointer[1], pointer[2],
620 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
622 output_data(" ?%d?", pointer[3]);
625 output_data(" %d %d (%d)",
626 pointer[3], pointer[4],
627 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
628 for (i = 5; i < length; i++) {
629 output_data(" ?%d?", pointer[i]);
633 case TELOPT_LINEMODE:
634 output_data("LINEMODE ");
636 output_data(" (empty suboption??\?)");
639 switch (pointer[1]) {
641 output_data("WILL ");
644 output_data("WONT ");
650 output_data("DONT ");
653 output_data("(no option??\?)");
656 switch (pointer[2]) {
658 output_data("Forward Mask");
659 for (i = 3; i < length; i++) {
660 output_data(" %x", pointer[i]);
664 output_data("%d (unknown)", pointer[2]);
665 for (i = 3; i < length; i++) {
666 output_data(" %d", pointer[i]);
674 for (i = 2; i < length - 2; i += 3) {
675 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
676 output_data(" %s", SLC_NAME(pointer[i+SLC_FUNC]));
678 output_data(" %d", pointer[i+SLC_FUNC]);
679 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
681 output_data(" NOSUPPORT"); break;
683 output_data(" CANTCHANGE"); break;
685 output_data(" VARIABLE"); break;
687 output_data(" DEFAULT"); break;
689 output_data("%s%s%s",
690 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
691 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
692 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
693 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
694 SLC_FLUSHOUT| SLC_LEVELBITS)) {
695 output_data("(0x%x)", pointer[i+SLC_FLAGS]);
697 output_data(" %d;", pointer[i+SLC_VALUE]);
698 if ((pointer[i+SLC_VALUE] == IAC) &&
699 (pointer[i+SLC_VALUE+1] == IAC))
702 for (; i < length; i++) {
703 output_data(" ?%d?", pointer[i]);
708 output_data("MODE ");
710 output_data("(no mode??\?)");
715 sprintf(tbuf, "%s%s%s%s%s",
716 pointer[2]&MODE_EDIT ? "|EDIT" : "",
717 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
718 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
719 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
720 pointer[2]&MODE_ACK ? "|ACK" : "");
721 output_data("%s", tbuf[1] ? &tbuf[1] : "0");
723 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
724 output_data(" (0x%x)", pointer[2]);
726 for (i = 3; i < length; i++) {
727 output_data(" ?0x%x?", pointer[i]);
731 output_data("%d (unknown)", pointer[1]);
732 for (i = 2; i < length; i++) {
733 output_data(" %d", pointer[i]);
738 case TELOPT_STATUS: {
742 output_data("STATUS");
744 switch (pointer[1]) {
746 if (pointer[1] == TELQUAL_SEND)
747 output_data(" SEND");
749 output_data(" %d (unknown)", pointer[1]);
750 for (i = 2; i < length; i++) {
751 output_data(" ?%d?", pointer[i]);
755 output_data(" IS\r\n");
757 for (i = 2; i < length; i++) {
759 case DO: cp = "DO"; goto common2;
760 case DONT: cp = "DONT"; goto common2;
761 case WILL: cp = "WILL"; goto common2;
762 case WONT: cp = "WONT"; goto common2;
765 if (TELOPT_OK(pointer[i]))
766 output_data(" %s %s", cp, TELOPT(pointer[i]));
768 output_data(" %s %d", cp, pointer[i]);
778 if (pointer[j] == SE) {
781 if (pointer[j+1] == SE)
786 pointer[k++] = pointer[j++];
788 printsub(0, &pointer[i], k - i);
800 output_data(" %d", pointer[i]);
809 case TELOPT_XDISPLOC:
810 output_data("X-DISPLAY-LOCATION ");
811 switch (pointer[1]) {
813 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
819 output_data("- unknown qualifier %d (0x%x).",
820 pointer[1], pointer[1]);
824 case TELOPT_NEW_ENVIRON:
825 output_data("NEW-ENVIRON ");
827 case TELOPT_OLD_ENVIRON:
828 output_data("OLD-ENVIRON");
830 switch (pointer[1]) {
835 output_data("SEND ");
838 output_data("INFO ");
842 for (i = 2; i < length; i++ ) {
843 switch (pointer[i]) {
845 output_data("\" VAR " + noquote);
850 output_data("\" VALUE " + noquote);
855 output_data("\" ESC " + noquote);
860 output_data("\" USERVAR " + noquote);
865 if (isprint(pointer[i]) && pointer[i] != '"') {
870 output_data("%c", pointer[i]);
872 output_data("\" %03o " + noquote,
886 #ifdef AUTHENTICATION
887 case TELOPT_AUTHENTICATION:
888 output_data("AUTHENTICATION");
891 output_data(" (empty suboption??\?)");
894 switch (pointer[1]) {
897 output_data(" %s ", (pointer[1] == TELQUAL_IS) ?
899 if (AUTHTYPE_NAME_OK(pointer[2]))
900 output_data("%s ", AUTHTYPE_NAME(pointer[2]));
902 output_data("%d ", pointer[2]);
904 output_data("(partial suboption??\?)");
908 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
910 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
911 "MUTUAL" : "ONE-WAY");
915 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
916 output_data("%s", buf);
922 output_data(" SEND ");
924 if (AUTHTYPE_NAME_OK(pointer[i]))
925 output_data("%s ", AUTHTYPE_NAME(pointer[i]));
927 output_data("%d ", pointer[i]);
929 output_data("(partial suboption??\?)");
932 output_data("%s|%s ",
933 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
935 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
936 "MUTUAL" : "ONE-WAY");
942 output_data(" NAME \"%.*s\"", length - 2, pointer + 2);
946 for (i = 2; i < length; i++) {
947 output_data(" ?%d?", pointer[i]);
956 output_data("ENCRYPT");
958 output_data(" (empty suboption??\?)");
961 switch (pointer[1]) {
963 output_data(" START");
970 case ENCRYPT_REQSTART:
971 output_data(" REQUEST-START");
975 output_data(" REQUEST-END");
980 output_data(" %s ", (pointer[1] == ENCRYPT_IS) ?
983 output_data(" (partial suboption??\?)");
986 if (ENCTYPE_NAME_OK(pointer[2]))
987 output_data("%s ", ENCTYPE_NAME(pointer[2]));
989 output_data(" %d (unknown)", pointer[2]);
993 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
994 output_data("%s", buf);
998 case ENCRYPT_SUPPORT:
1000 output_data(" SUPPORT ");
1001 while (i < length) {
1002 if (ENCTYPE_NAME_OK(pointer[i]))
1003 output_data("%s ", ENCTYPE_NAME(pointer[i]));
1005 output_data("%d ", pointer[i]);
1010 case ENCRYPT_ENC_KEYID:
1011 output_data(" ENC_KEYID");
1014 case ENCRYPT_DEC_KEYID:
1015 output_data(" DEC_KEYID");
1019 output_data(" %d (unknown)", pointer[1]);
1021 for (i = 2; i < length; i++) {
1022 output_data(" %d", pointer[i]);
1027 #endif /* ENCRYPTION */
1030 if (TELOPT_OK(pointer[0]))
1031 output_data("%s (unknown)", TELOPT(pointer[0]));
1033 output_data("%d (unknown)", pointer[i]);
1034 for (i = 1; i < length; i++) {
1035 output_data(" %d", pointer[i]);
1039 output_data("\r\n");
1043 * Dump a data buffer in hex and ascii to the output data stream.
1046 printdata(const char *tag, char *ptr, int cnt)
1052 /* flush net output buffer if no room for new data) */
1053 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1057 /* add a line of output */
1058 output_data("%s: ", tag);
1059 for (i = 0; i < 20 && cnt; i++) {
1060 output_data("%02x", *ptr);
1061 if (isprint(*ptr)) {
1073 output_data(" %s\r\n", xbuf );
1076 #endif /* DIAGNOSTICS */