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.27 2001/09/03 05:54:17 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.
58 output_data("td: ttloop\r\n");
62 ncc = read(net, netibuf, sizeof netibuf);
66 syslog(LOG_INFO, "ttloop: read: %m\n");
68 } else if (ncc == 0) {
69 syslog(LOG_INFO, "ttloop: peer died\n");
73 output_data("td: ttloop read %d chars\r\n", ncc);
76 telrcv(); /* state machine */
78 pfrontp = pbackp = ptyobuf;
85 * Check a descriptor to see if out of band data exists on it.
90 static struct timeval timeout = { 0 };
95 fatal(ourpty, "fd too large");
100 value = select(s+1, 0, 0, &excepts, &timeout);
101 } while ((value == -1) && (errno == EINTR));
104 fatalperror(ourpty, "select");
106 if (FD_ISSET(s, &excepts)) {
118 if ((n = pfrontp - pbackp) > 0) {
119 DIAG((TD_REPORT | TD_PTYDATA), {
120 output_data("td: ptyflush %d chars\r\n", n);
122 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
123 n = write(ourpty, pbackp, n);
126 if (errno == EWOULDBLOCK || errno == EINTR)
131 if (pbackp == pfrontp)
132 pbackp = pfrontp = ptyobuf;
138 * Return the address of the next "item" in the TELNET data
139 * stream. This will be the address of the next character if
140 * the current address is a user data character, or it will
141 * be the address of the character following the TELNET command
142 * if the current address is a TELNET IAC ("I Am a Command")
146 nextitem(char *current)
148 if ((*current&0xff) != IAC) {
151 switch (*(current+1)&0xff) {
158 /* loop forever looking for the SE */
159 char *look = current+2;
162 if ((*look++&0xff) == IAC) {
163 if ((*look++&0xff) == SE) {
178 * We are about to do a TELNET SYNCH operation. Clear
179 * the path to the network.
181 * Things are a bit tricky since we may have sent the first
182 * byte or so of a previous TELNET command into the network.
183 * So, we have to scan the network buffer from the beginning
184 * until we are up to where we want to be.
186 * A side effect of what we do, just to keep things
187 * simple, is to clear the urgent data pointer. The principal
188 * caller should be setting the urgent data pointer AFTER calling
194 char *thisitem, *next;
196 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
197 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
200 thisitem = nclearto > netobuf ? nclearto : netobuf;
205 while ((next = nextitem(thisitem)) <= nbackp) {
209 /* Now, thisitem is first before/at boundary. */
212 good = nclearto > netobuf ? nclearto : netobuf;
214 good = netobuf; /* where the good bytes go */
217 while (nfrontp > thisitem) {
218 if (wewant(thisitem)) {
223 next = nextitem(next);
224 } while (wewant(next) && (nfrontp > next));
225 length = next-thisitem;
226 memmove(good, thisitem, length);
230 thisitem = nextitem(thisitem);
235 nfrontp = good; /* next byte to be sent */
237 } /* end of netclear */
243 * Send as much data as possible to the network,
244 * handling requests for urgent data.
251 if ((n = nfrontp - nbackp) > 0) {
253 { n += output_data("td: netflush %d chars\r\n", n);
256 if (encrypt_output) {
257 char *s = nclearto ? nclearto : nbackp;
258 if (nfrontp - s > 0) {
259 (*encrypt_output)((unsigned char *)s, nfrontp-s);
265 * if no urgent data, or if the other side appears to be an
266 * old 4.2 client (and thus unable to survive TCP urgent data),
267 * write the entire buffer in non-OOB mode.
269 #if 1 /* remove this to make it work between solaris 2.6 and linux */
270 if ((neturg == 0) || (not42 == 0)) {
272 n = write(net, nbackp, n); /* normal write */
273 #if 1 /* remove this to make it work between solaris 2.6 and linux */
277 * In 4.2 (and 4.3) systems, there is some question about
278 * what byte in a sendOOB operation is the "OOB" data.
279 * To make ourselves compatible, we only send ONE byte
280 * out of band, the one WE THINK should be OOB (though
281 * we really have more the TCP philosophy of urgent data
282 * rather than the Unix philosophy of OOB data).
285 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
287 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
293 if (errno == EWOULDBLOCK || errno == EINTR)
299 if (nbackp > nclearto)
302 if (nbackp >= neturg) {
305 if (nbackp == nfrontp) {
306 nbackp = nfrontp = netobuf;
318 * Just a handy little function to write a bit of raw data to the net.
319 * It will force a transmit of the buffer if necessary
322 * ptr - A pointer to a character string to write
323 * len - How many bytes to write
326 writenet(unsigned char *ptr, int len)
328 /* flush buffer if no room for new data) */
329 while ((&netobuf[BUFSIZ] - nfrontp) < len) {
330 /* if this fails, don't worry, buffer is a little big */
334 memmove(nfrontp, ptr, len);
340 * miscellaneous functions doing a variety of little jobs follow ...
344 void fatal(int f, char *msg)
348 snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
350 if (encrypt_output) {
352 * Better turn off encryption first....
359 write(f, buf, (int)strlen(buf));
365 fatalperror_errno(int f, const char *msg, int error)
369 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(error));
374 fatalperror(int f, const char *msg)
376 fatalperror_errno(f, msg, errno);
381 void edithost(char *pat, char *host)
383 char *res = editedhost;
404 if (res == &editedhost[sizeof editedhost - 1]) {
412 sizeof editedhost - (res - editedhost));
415 editedhost[sizeof editedhost - 1] = '\0';
418 static char *putlocation;
435 * This is split on two lines so that SCCS will not see the M
436 * between two % signs and expand it...
438 static char fmtstr[] = { "%l:%M" "%P on %A, %d %B %Y" };
440 void putf(char *cp, char *where)
449 /* if we don't have uname, set these to sensible values */
450 char *sysname = "Unix",
457 sysname=name.sysname;
458 machine=name.machine;
459 release=name.release;
460 version=name.version;
474 /* names are like /dev/pts/2 -- we want pts/2 */
475 slash = strchr(line+1, '/');
477 slash = strrchr(line, '/');
479 if (slash == (char *) 0)
507 strftime(db, sizeof(db), fmtstr, localtime(&t));
521 * Print telnet options and commands in plain text, if possible.
524 printoption(char *fmt, int option)
526 if (TELOPT_OK(option))
527 output_data("%s %s\r\n",
530 else if (TELCMD_OK(option))
531 output_data("%s %s\r\n",
535 output_data("%s %d\r\n",
542 printsub(int direction, unsigned char *pointer, int length)
544 /* where suboption data sits */
545 /* length of suboption data */
548 unsigned char buf[512];
550 if (!(diagnostic & TD_OPTIONS))
554 output_data("td: %s suboption ",
555 direction == '<' ? "recv" : "send");
559 i = pointer[length-2];
560 j = pointer[length-1];
562 if (i != IAC || j != SE) {
563 output_data("(terminated by ");
567 else if (TELCMD_OK(i))
576 else if (TELCMD_OK(j))
582 output_data(", not IAC SE!) ");
588 output_data("(Empty suboption??\?)");
591 switch (pointer[0]) {
593 output_data("TERMINAL-TYPE ");
594 switch (pointer[1]) {
596 output_data("IS \"%.*s\"",
604 output_data("- unknown qualifier %d (0x%x).",
605 pointer[1], pointer[1]);
609 output_data("TERMINAL-SPEED");
611 output_data(" (empty suboption??\?)");
614 switch (pointer[1]) {
616 output_data(" IS %.*s", length-2, (char *)pointer+2);
620 output_data(" SEND");
622 output_data(" %d (unknown)", pointer[1]);
623 for (i = 2; i < length; i++) {
624 output_data(" ?%d?", pointer[i]);
631 output_data("TOGGLE-FLOW-CONTROL");
633 output_data(" (empty suboption??\?)");
636 switch (pointer[1]) {
643 case LFLOW_RESTART_ANY:
644 output_data(" RESTART-ANY");
646 case LFLOW_RESTART_XON:
647 output_data(" RESTART-XON");
650 output_data(" %d (unknown)",
653 for (i = 2; i < length; i++) {
662 output_data(" (empty suboption??\?)");
670 output_data(" %u %u(%u)",
673 (((unsigned int)pointer[1])<<8) + pointer[2]);
679 output_data(" %u %u(%u)",
682 (((unsigned int)pointer[3])<<8) + pointer[4]);
683 for (i = 5; i < length; i++) {
689 case TELOPT_LINEMODE:
690 output_data("LINEMODE ");
692 output_data(" (empty suboption??\?)");
695 switch (pointer[1]) {
697 output_data("WILL ");
700 output_data("WONT ");
706 output_data("DONT ");
709 output_data("(no option??\?)");
712 switch (pointer[2]) {
714 output_data("Forward Mask");
715 for (i = 3; i < length; i++) {
716 output_data(" %x", pointer[i]);
720 output_data("%d (unknown)",
722 for (i = 3; i < length; i++) {
732 for (i = 2; i < length - 2; i += 3) {
733 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
735 SLC_NAME(pointer[i+SLC_FUNC]));
738 pointer[i+SLC_FUNC]);
739 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
741 output_data(" NOSUPPORT");
744 output_data(" CANTCHANGE");
747 output_data(" VARIABLE");
750 output_data(" DEFAULT");
753 output_data("%s%s%s",
754 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
755 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
756 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
757 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
758 SLC_FLUSHOUT| SLC_LEVELBITS)) {
759 output_data("(0x%x)",
760 pointer[i+SLC_FLAGS]);
763 pointer[i+SLC_VALUE]);
764 if ((pointer[i+SLC_VALUE] == IAC) &&
765 (pointer[i+SLC_VALUE+1] == IAC))
768 for (; i < length; i++) {
775 output_data("MODE ");
777 output_data("(no mode??\?)");
785 pointer[2]&MODE_EDIT ? "|EDIT" : "",
786 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
787 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
788 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
789 pointer[2]&MODE_ACK ? "|ACK" : "");
791 tbuf[1] ? &tbuf[1] : "0");
793 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
794 output_data(" (0x%x)",
797 for (i = 3; i < length; i++) {
798 output_data(" ?0x%x?",
803 output_data("%d (unknown)",
805 for (i = 2; i < length; i++) {
806 output_data(" %d", pointer[i]);
811 case TELOPT_STATUS: {
815 output_data("STATUS");
817 switch (pointer[1]) {
819 if (pointer[1] == TELQUAL_SEND)
820 output_data(" SEND");
822 output_data(" %d (unknown)",
824 for (i = 2; i < length; i++) {
830 output_data(" IS\r\n");
832 for (i = 2; i < length; i++) {
834 case DO: cp = "DO"; goto common2;
835 case DONT: cp = "DONT"; goto common2;
836 case WILL: cp = "WILL"; goto common2;
837 case WONT: cp = "WONT"; goto common2;
840 if (TELOPT_OK(pointer[i]))
841 output_data(" %s %s",
845 output_data(" %s %d",
857 if (pointer[j] == SE) {
860 if (pointer[j+1] == SE)
865 pointer[k++] = pointer[j++];
867 printsub(0, &pointer[i], k - i);
889 case TELOPT_XDISPLOC:
890 output_data("X-DISPLAY-LOCATION ");
891 switch (pointer[1]) {
893 output_data("IS \"%.*s\"",
901 output_data("- unknown qualifier %d (0x%x).",
902 pointer[1], pointer[1]);
906 case TELOPT_NEW_ENVIRON:
907 output_data("NEW-ENVIRON ");
909 case TELOPT_OLD_ENVIRON:
910 output_data("OLD-ENVIRON");
912 switch (pointer[1]) {
917 output_data("SEND ");
920 output_data("INFO ");
924 for (i = 2; i < length; i++ ) {
925 switch (pointer[i]) {
927 output_data("\" VAR " + noquote);
932 output_data("\" VALUE " + noquote);
937 output_data("\" ESC " + noquote);
942 output_data("\" USERVAR " + noquote);
947 if (isprint(pointer[i]) && pointer[i] != '"') {
952 output_data ("%c", pointer[i]);
954 output_data("\" %03o " + noquote,
968 #ifdef AUTHENTICATION
969 case TELOPT_AUTHENTICATION:
970 output_data("AUTHENTICATION");
973 output_data(" (empty suboption??\?)");
976 switch (pointer[1]) {
980 (pointer[1] == TELQUAL_IS) ?
982 if (AUTHTYPE_NAME_OK(pointer[2]))
984 AUTHTYPE_NAME(pointer[2]));
989 output_data("(partial suboption??\?)");
993 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
995 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
996 "MUTUAL" : "ONE-WAY");
998 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1005 output_data(" SEND ");
1006 while (i < length) {
1007 if (AUTHTYPE_NAME_OK(pointer[i]))
1009 AUTHTYPE_NAME(pointer[i]));
1013 if (++i >= length) {
1014 output_data("(partial suboption??\?)");
1017 output_data("%s|%s ",
1018 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1019 "CLIENT" : "SERVER",
1020 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1021 "MUTUAL" : "ONE-WAY");
1028 output_data(" NAME \"%.*s\"",
1034 for (i = 2; i < length; i++) {
1035 output_data(" ?%d?",
1044 case TELOPT_ENCRYPT:
1045 output_data("ENCRYPT");
1047 output_data(" (empty suboption?)");
1050 switch (pointer[1]) {
1052 output_data(" START");
1056 output_data(" END");
1059 case ENCRYPT_REQSTART:
1060 output_data(" REQUEST-START");
1063 case ENCRYPT_REQEND:
1064 output_data(" REQUEST-END");
1070 (pointer[1] == ENCRYPT_IS) ?
1073 output_data(" (partial suboption?)");
1076 if (ENCTYPE_NAME_OK(pointer[2]))
1078 ENCTYPE_NAME(pointer[2]));
1080 output_data(" %d (unknown)",
1083 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1088 case ENCRYPT_SUPPORT:
1090 output_data(" SUPPORT ");
1091 while (i < length) {
1092 if (ENCTYPE_NAME_OK(pointer[i]))
1094 ENCTYPE_NAME(pointer[i]));
1102 case ENCRYPT_ENC_KEYID:
1103 output_data(" ENC_KEYID %d", pointer[1]);
1106 case ENCRYPT_DEC_KEYID:
1107 output_data(" DEC_KEYID %d", pointer[1]);
1111 output_data(" %d (unknown)", pointer[1]);
1113 for (i = 2; i < length; i++) {
1114 output_data(" %d", pointer[i]);
1122 if (TELOPT_OK(pointer[0]))
1123 output_data("%s (unknown)",
1124 TELOPT(pointer[0]));
1126 output_data("%d (unknown)",
1128 for (i = 1; i < length; i++) {
1129 output_data(" %d", pointer[i]);
1133 output_data("\r\n");
1137 * Dump a data buffer in hex and ascii to the output data stream.
1140 printdata(char *tag, char *ptr, int cnt)
1146 /* flush net output buffer if no room for new data) */
1147 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1151 /* add a line of output */
1152 output_data("%s: ", tag);
1153 for (i = 0; i < 20 && cnt; i++) {
1154 output_data("%02x", *ptr);
1155 if (isprint((unsigned char)*ptr)) {
1167 output_data(" %s\r\n", xbuf);
1170 #endif /* DIAGNOSTICS */