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
37 RCSID("$Id: utility.c,v 1.22.2.1 2000/10/10 13:12:34 assar Exp $");
40 * utility functions performing io related tasks
46 * A small subroutine to flush the network output buffer, get some
47 * data from the network, and pass it through the telnet state
48 * machine. We also flush the pty input buffer (by dropping its data)
49 * if it becomes too full.
51 * return 0 if OK or 1 if interrupted by a signal.
60 output_data("td: ttloop\r\n");
64 ncc = read(net, netibuf, sizeof netibuf);
68 syslog(LOG_INFO, "ttloop: read: %m\n");
70 } else if (ncc == 0) {
71 syslog(LOG_INFO, "ttloop: peer died\n");
75 output_data("td: ttloop read %d chars\r\n", ncc);
78 telrcv(); /* state machine */
80 pfrontp = pbackp = ptyobuf;
87 * Check a descriptor to see if out of band data exists on it.
92 static struct timeval timeout = { 0 };
97 fatal(ourpty, "fd too large");
102 value = select(s+1, 0, 0, &excepts, &timeout);
103 } while ((value == -1) && (errno == EINTR));
106 fatalperror(ourpty, "select");
108 if (FD_ISSET(s, &excepts)) {
120 if ((n = pfrontp - pbackp) > 0) {
121 DIAG((TD_REPORT | TD_PTYDATA), {
122 output_data("td: ptyflush %d chars\r\n", n);
124 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
125 n = write(ourpty, pbackp, n);
128 if (errno == EWOULDBLOCK || errno == EINTR)
133 if (pbackp == pfrontp)
134 pbackp = pfrontp = ptyobuf;
140 * Return the address of the next "item" in the TELNET data
141 * stream. This will be the address of the next character if
142 * the current address is a user data character, or it will
143 * be the address of the character following the TELNET command
144 * if the current address is a TELNET IAC ("I Am a Command")
148 nextitem(char *current)
150 if ((*current&0xff) != IAC) {
153 switch (*(current+1)&0xff) {
160 /* loop forever looking for the SE */
161 char *look = current+2;
164 if ((*look++&0xff) == IAC) {
165 if ((*look++&0xff) == SE) {
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;
207 while ((next = nextitem(thisitem)) <= nbackp) {
211 /* Now, thisitem is first before/at boundary. */
214 good = nclearto > netobuf ? nclearto : netobuf;
216 good = netobuf; /* where the good bytes go */
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 if ((n = nfrontp - nbackp) > 0) {
254 { n += output_data("td: netflush %d chars\r\n", n);
257 if (encrypt_output) {
258 char *s = nclearto ? nclearto : nbackp;
259 if (nfrontp - s > 0) {
260 (*encrypt_output)((unsigned char *)s, nfrontp-s);
266 * if no urgent data, or if the other side appears to be an
267 * old 4.2 client (and thus unable to survive TCP urgent data),
268 * write the entire buffer in non-OOB mode.
270 #if 1 /* remove this to make it work between solaris 2.6 and linux */
271 if ((neturg == 0) || (not42 == 0)) {
273 n = write(net, nbackp, n); /* normal write */
274 #if 1 /* remove this to make it work between solaris 2.6 and linux */
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 */
294 if (errno == EWOULDBLOCK || errno == EINTR)
300 if (nbackp > nclearto)
303 if (nbackp >= neturg) {
306 if (nbackp == nfrontp) {
307 nbackp = nfrontp = netobuf;
319 * Just a handy little function to write a bit of raw data to the net.
320 * It will force a transmit of the buffer if necessary
323 * ptr - A pointer to a character string to write
324 * len - How many bytes to write
327 writenet(unsigned char *ptr, int len)
329 /* flush buffer if no room for new data) */
330 while ((&netobuf[BUFSIZ] - nfrontp) < len) {
331 /* if this fails, don't worry, buffer is a little big */
335 memmove(nfrontp, ptr, len);
341 * miscellaneous functions doing a variety of little jobs follow ...
345 void fatal(int f, char *msg)
349 snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
351 if (encrypt_output) {
353 * Better turn off encryption first....
360 write(f, buf, (int)strlen(buf));
366 fatalperror(int f, const char *msg)
370 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
376 void edithost(char *pat, char *host)
378 char *res = editedhost;
399 if (res == &editedhost[sizeof editedhost - 1]) {
407 sizeof editedhost - (res - editedhost));
410 editedhost[sizeof editedhost - 1] = '\0';
413 static char *putlocation;
430 * This is split on two lines so that SCCS will not see the M
431 * between two % signs and expand it...
433 static char fmtstr[] = { "%l:%M" "%P on %A, %d %B %Y" };
435 void putf(char *cp, char *where)
444 /* if we don't have uname, set these to sensible values */
445 char *sysname = "Unix",
452 sysname=name.sysname;
453 machine=name.machine;
454 release=name.release;
455 version=name.version;
469 /* names are like /dev/pts/2 -- we want pts/2 */
470 slash = strchr(line+1, '/');
472 slash = strrchr(line, '/');
474 if (slash == (char *) 0)
502 strftime(db, sizeof(db), fmtstr, localtime(&t));
516 * Print telnet options and commands in plain text, if possible.
519 printoption(char *fmt, int option)
521 if (TELOPT_OK(option))
522 output_data("%s %s\r\n",
525 else if (TELCMD_OK(option))
526 output_data("%s %s\r\n",
530 output_data("%s %d\r\n",
537 printsub(int direction, unsigned char *pointer, int length)
539 /* where suboption data sits */
540 /* length of suboption data */
543 unsigned char buf[512];
545 if (!(diagnostic & TD_OPTIONS))
549 output_data("td: %s suboption ",
550 direction == '<' ? "recv" : "send");
554 i = pointer[length-2];
555 j = pointer[length-1];
557 if (i != IAC || j != SE) {
558 output_data("(terminated by ");
562 else if (TELCMD_OK(i))
571 else if (TELCMD_OK(j))
577 output_data(", not IAC SE!) ");
583 output_data("(Empty suboption??\?)");
586 switch (pointer[0]) {
588 output_data("TERMINAL-TYPE ");
589 switch (pointer[1]) {
591 output_data("IS \"%.*s\"",
599 output_data("- unknown qualifier %d (0x%x).",
600 pointer[1], pointer[1]);
604 output_data("TERMINAL-SPEED");
606 output_data(" (empty suboption??\?)");
609 switch (pointer[1]) {
611 output_data(" IS %.*s", length-2, (char *)pointer+2);
615 output_data(" SEND");
617 output_data(" %d (unknown)", pointer[1]);
618 for (i = 2; i < length; i++) {
619 output_data(" ?%d?", pointer[i]);
626 output_data("TOGGLE-FLOW-CONTROL");
628 output_data(" (empty suboption??\?)");
631 switch (pointer[1]) {
638 case LFLOW_RESTART_ANY:
639 output_data(" RESTART-ANY");
641 case LFLOW_RESTART_XON:
642 output_data(" RESTART-XON");
645 output_data(" %d (unknown)",
648 for (i = 2; i < length; i++) {
657 output_data(" (empty suboption??\?)");
665 output_data(" %u %u(%u)",
668 (((unsigned int)pointer[1])<<8) + pointer[2]);
674 output_data(" %u %u(%u)",
677 (((unsigned int)pointer[3])<<8) + pointer[4]);
678 for (i = 5; i < length; i++) {
684 case TELOPT_LINEMODE:
685 output_data("LINEMODE ");
687 output_data(" (empty suboption??\?)");
690 switch (pointer[1]) {
692 output_data("WILL ");
695 output_data("WONT ");
701 output_data("DONT ");
704 output_data("(no option??\?)");
707 switch (pointer[2]) {
709 output_data("Forward Mask");
710 for (i = 3; i < length; i++) {
711 output_data(" %x", pointer[i]);
715 output_data("%d (unknown)",
717 for (i = 3; i < length; i++) {
727 for (i = 2; i < length - 2; i += 3) {
728 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
730 SLC_NAME(pointer[i+SLC_FUNC]));
733 pointer[i+SLC_FUNC]);
734 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
736 output_data(" NOSUPPORT");
739 output_data(" CANTCHANGE");
742 output_data(" VARIABLE");
745 output_data(" DEFAULT");
748 output_data("%s%s%s",
749 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
750 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
751 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
752 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
753 SLC_FLUSHOUT| SLC_LEVELBITS)) {
754 output_data("(0x%x)",
755 pointer[i+SLC_FLAGS]);
758 pointer[i+SLC_VALUE]);
759 if ((pointer[i+SLC_VALUE] == IAC) &&
760 (pointer[i+SLC_VALUE+1] == IAC))
763 for (; i < length; i++) {
770 output_data("MODE ");
772 output_data("(no mode??\?)");
780 pointer[2]&MODE_EDIT ? "|EDIT" : "",
781 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
782 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
783 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
784 pointer[2]&MODE_ACK ? "|ACK" : "");
786 tbuf[1] ? &tbuf[1] : "0");
788 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
789 output_data(" (0x%x)",
792 for (i = 3; i < length; i++) {
793 output_data(" ?0x%x?",
798 output_data("%d (unknown)",
800 for (i = 2; i < length; i++) {
801 output_data(" %d", pointer[i]);
806 case TELOPT_STATUS: {
810 output_data("STATUS");
812 switch (pointer[1]) {
814 if (pointer[1] == TELQUAL_SEND)
815 output_data(" SEND");
817 output_data(" %d (unknown)",
819 for (i = 2; i < length; i++) {
825 output_data(" IS\r\n");
827 for (i = 2; i < length; i++) {
829 case DO: cp = "DO"; goto common2;
830 case DONT: cp = "DONT"; goto common2;
831 case WILL: cp = "WILL"; goto common2;
832 case WONT: cp = "WONT"; goto common2;
835 if (TELOPT_OK(pointer[i]))
836 output_data(" %s %s",
840 output_data(" %s %d",
852 if (pointer[j] == SE) {
855 if (pointer[j+1] == SE)
860 pointer[k++] = pointer[j++];
862 printsub(0, &pointer[i], k - i);
884 case TELOPT_XDISPLOC:
885 output_data("X-DISPLAY-LOCATION ");
886 switch (pointer[1]) {
888 output_data("IS \"%.*s\"",
896 output_data("- unknown qualifier %d (0x%x).",
897 pointer[1], pointer[1]);
901 case TELOPT_NEW_ENVIRON:
902 output_data("NEW-ENVIRON ");
904 case TELOPT_OLD_ENVIRON:
905 output_data("OLD-ENVIRON");
907 switch (pointer[1]) {
912 output_data("SEND ");
915 output_data("INFO ");
919 for (i = 2; i < length; i++ ) {
920 switch (pointer[i]) {
922 output_data("\" VAR " + noquote);
927 output_data("\" VALUE " + noquote);
932 output_data("\" ESC " + noquote);
937 output_data("\" USERVAR " + noquote);
942 if (isprint(pointer[i]) && pointer[i] != '"') {
947 output_data ("%c", pointer[i]);
949 output_data("\" %03o " + noquote,
963 #ifdef AUTHENTICATION
964 case TELOPT_AUTHENTICATION:
965 output_data("AUTHENTICATION");
968 output_data(" (empty suboption??\?)");
971 switch (pointer[1]) {
975 (pointer[1] == TELQUAL_IS) ?
977 if (AUTHTYPE_NAME_OK(pointer[2]))
979 AUTHTYPE_NAME(pointer[2]));
984 output_data("(partial suboption??\?)");
988 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
990 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
991 "MUTUAL" : "ONE-WAY");
993 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1000 output_data(" SEND ");
1001 while (i < length) {
1002 if (AUTHTYPE_NAME_OK(pointer[i]))
1004 AUTHTYPE_NAME(pointer[i]));
1008 if (++i >= length) {
1009 output_data("(partial suboption??\?)");
1012 output_data("%s|%s ",
1013 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1014 "CLIENT" : "SERVER",
1015 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1016 "MUTUAL" : "ONE-WAY");
1023 output_data(" NAME \"%.*s\"",
1029 for (i = 2; i < length; i++) {
1030 output_data(" ?%d?",
1039 case TELOPT_ENCRYPT:
1040 output_data("ENCRYPT");
1042 output_data(" (empty suboption?)");
1045 switch (pointer[1]) {
1047 output_data(" START");
1051 output_data(" END");
1054 case ENCRYPT_REQSTART:
1055 output_data(" REQUEST-START");
1058 case ENCRYPT_REQEND:
1059 output_data(" REQUEST-END");
1065 (pointer[1] == ENCRYPT_IS) ?
1068 output_data(" (partial suboption?)");
1071 if (ENCTYPE_NAME_OK(pointer[2]))
1073 ENCTYPE_NAME(pointer[2]));
1075 output_data(" %d (unknown)",
1078 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1083 case ENCRYPT_SUPPORT:
1085 output_data(" SUPPORT ");
1086 while (i < length) {
1087 if (ENCTYPE_NAME_OK(pointer[i]))
1089 ENCTYPE_NAME(pointer[i]));
1097 case ENCRYPT_ENC_KEYID:
1098 output_data(" ENC_KEYID %d", pointer[1]);
1101 case ENCRYPT_DEC_KEYID:
1102 output_data(" DEC_KEYID %d", pointer[1]);
1106 output_data(" %d (unknown)", pointer[1]);
1108 for (i = 2; i < length; i++) {
1109 output_data(" %d", pointer[i]);
1117 if (TELOPT_OK(pointer[0]))
1118 output_data("%s (unknown)",
1119 TELOPT(pointer[0]));
1121 output_data("%d (unknown)",
1123 for (i = 1; i < length; i++) {
1124 output_data(" %d", pointer[i]);
1128 output_data("\r\n");
1132 * Dump a data buffer in hex and ascii to the output data stream.
1135 printdata(char *tag, char *ptr, int cnt)
1141 /* flush net output buffer if no room for new data) */
1142 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1146 /* add a line of output */
1147 output_data("%s: ", tag);
1148 for (i = 0; i < 20 && cnt; i++) {
1149 output_data("%02x", *ptr);
1150 if (isprint(*ptr)) {
1162 output_data(" %s\r\n", xbuf);
1165 #endif /* DIAGNOSTICS */