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";
38 static const char rcsid[] =
39 "$FreeBSD: src/libexec/telnetd/utility.c,v 1.13.2.4 2002/04/13 11:07:12 markm Exp $";
44 #include <sys/utsname.h>
52 * utility functions performing io related tasks
58 * A small subroutine to flush the network output buffer, get some data
59 * from the network, and pass it through the telnet state machine. We
60 * also flush the pty input buffer (by dropping its data) if it becomes
68 DIAG(TD_REPORT, output_data("td: ttloop\r\n"));
69 if (nfrontp - nbackp > 0) {
72 ncc = read(net, netibuf, sizeof netibuf);
74 syslog(LOG_INFO, "ttloop: read: %m");
76 } else if (ncc == 0) {
77 syslog(LOG_INFO, "ttloop: peer died: %m");
80 DIAG(TD_REPORT, output_data("td: ttloop read %d chars\r\n", ncc));
82 telrcv(); /* state machine */
84 pfrontp = pbackp = ptyobuf;
90 * Check a descriptor to see if out of band data exists on it.
95 static struct timeval timeout = { 0, 0 };
102 memset((char *)&timeout, 0, sizeof timeout);
103 value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
104 } while ((value == -1) && (errno == EINTR));
107 fatalperror(pty, "select");
109 if (FD_ISSET(s, &excepts)) {
121 if ((n = pfrontp - pbackp) > 0) {
122 DIAG(TD_REPORT | TD_PTYDATA,
123 output_data("td: ptyflush %d chars\r\n", n));
124 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
125 n = write(pty, 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) {
159 case SB: /* loop forever looking for the SE */
161 char *look = current+2;
164 if ((*look++&0xff) == IAC) {
165 if ((*look++&0xff) == SE) {
174 } /* end of nextitem */
179 * We are about to do a TELNET SYNCH operation. Clear
180 * the path to the network.
182 * Things are a bit tricky since we may have sent the first
183 * byte or so of a previous TELNET command into the network.
184 * So, we have to scan the network buffer from the beginning
185 * until we are up to where we want to be.
187 * A side effect of what we do, just to keep things
188 * simple, is to clear the urgent data pointer. The principal
189 * caller should be setting the urgent data pointer AFTER calling
195 char *thisitem, *next;
197 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
198 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
202 while ((next = nextitem(thisitem)) <= nbackp) {
206 /* Now, thisitem is first before/at boundary. */
208 good = netobuf; /* where the good bytes go */
210 while (nfrontp > thisitem) {
211 if (wewant(thisitem)) {
216 next = nextitem(next);
217 } while (wewant(next) && (nfrontp > next));
218 length = next-thisitem;
219 memmove(good, thisitem, length);
223 thisitem = nextitem(thisitem);
228 nfrontp = good; /* next byte to be sent */
230 } /* end of netclear */
234 * Send as much data as possible to the network,
235 * handling requests for urgent data.
243 while ((n = nfrontp - nbackp) > 0) {
245 /* XXX This causes output_data() to recurse and die */
247 n += output_data("td: netflush %d chars\r\n", n);
251 * if no urgent data, or if the other side appears to be an
252 * old 4.2 client (and thus unable to survive TCP urgent data),
253 * write the entire buffer in non-OOB mode.
255 if ((neturg == 0) || (not42 == 0)) {
256 n = write(net, nbackp, n); /* normal write */
260 * In 4.2 (and 4.3) systems, there is some question about
261 * what byte in a sendOOB operation is the "OOB" data.
262 * To make ourselves compatible, we only send ONE byte
263 * out of band, the one WE THINK should be OOB (though
264 * we really have more the TCP philosophy of urgent data
265 * rather than the Unix philosophy of OOB data).
268 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
270 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
274 if (errno == EWOULDBLOCK || errno == EINTR)
280 if (nbackp >= neturg) {
283 if (nbackp == nfrontp) {
284 nbackp = nfrontp = netobuf;
288 } /* end of netflush */
292 * miscellaneous functions doing a variety of little jobs follow ...
297 fatal(int f, const char *msg)
301 (void) snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
302 (void) write(f, buf, (int)strlen(buf));
308 fatalperror(int f, const char *msg)
312 (void) snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
319 edithost(char *pat, char *host)
321 char *res = editedhost;
342 if (res == &editedhost[sizeof editedhost - 1]) {
349 (void) strncpy(res, host,
350 sizeof editedhost - (res - editedhost) -1);
353 editedhost[sizeof editedhost - 1] = '\0';
356 static char *putlocation;
359 putstr(const char *s)
373 static char fmtstr[] = { "%+" };
375 static char fmtstr[] = { "%l:%M%P on %A, %d %B %Y" };
379 putf(char *cp, char *where)
385 static struct utsname kerninfo;
387 if (!*kerninfo.sysname)
398 } else if (*cp != '%') {
406 /* names are like /dev/pts/2 -- we want pts/2 */
407 slash = strchr(line+1, '/');
409 slash = strrchr(line, '/');
411 if (slash == (char *) 0)
423 setlocale(LC_TIME, "");
426 (void)strftime(db, sizeof(db), fmtstr, localtime(&t));
432 putstr(kerninfo.sysname);
436 putstr(kerninfo.machine);
440 putstr(kerninfo.release);
444 putstr(kerninfo.version);
458 * Print telnet options and commands in plain text, if possible.
461 printoption(const char *fmt, int option)
463 if (TELOPT_OK(option))
464 output_data("%s %s\r\n", fmt, TELOPT(option));
465 else if (TELCMD_OK(option))
466 output_data("%s %s\r\n", fmt, TELCMD(option));
468 output_data("%s %d\r\n", fmt, option);
473 printsub(char direction, unsigned char *pointer, int length)
477 if (!(diagnostic & TD_OPTIONS))
481 output_data("td: %s suboption ",
482 direction == '<' ? "recv" : "send");
486 i = pointer[length-2];
487 j = pointer[length-1];
489 if (i != IAC || j != SE) {
490 output_data("(terminated by ");
492 output_data("%s ", TELOPT(i));
493 else if (TELCMD_OK(i))
494 output_data("%s ", TELCMD(i));
496 output_data("%d ", i);
498 output_data("%s", TELOPT(j));
499 else if (TELCMD_OK(j))
500 output_data("%s", TELCMD(j));
502 output_data("%d", j);
503 output_data(", not IAC SE!) ");
509 output_data("(Empty suboption??\?)");
512 switch (pointer[0]) {
514 output_data("TERMINAL-TYPE ");
515 switch (pointer[1]) {
517 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
524 "- unknown qualifier %d (0x%x).",
525 pointer[1], pointer[1]);
529 output_data("TERMINAL-SPEED");
531 output_data(" (empty suboption??\?)");
534 switch (pointer[1]) {
536 output_data(" IS %.*s", length-2, (char *)pointer+2);
540 output_data(" SEND");
542 output_data(" %d (unknown)", pointer[1]);
543 for (i = 2; i < length; i++) {
544 output_data(" ?%d?", pointer[i]);
551 output_data("TOGGLE-FLOW-CONTROL");
553 output_data(" (empty suboption??\?)");
556 switch (pointer[1]) {
558 output_data(" OFF"); break;
560 output_data(" ON"); break;
561 case LFLOW_RESTART_ANY:
562 output_data(" RESTART-ANY"); break;
563 case LFLOW_RESTART_XON:
564 output_data(" RESTART-XON"); break;
566 output_data(" %d (unknown)", pointer[1]);
568 for (i = 2; i < length; i++) {
569 output_data(" ?%d?", pointer[i]);
576 output_data(" (empty suboption??\?)");
580 output_data(" ?%d?", pointer[1]);
583 output_data(" %d %d (%d)",
584 pointer[1], pointer[2],
585 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
587 output_data(" ?%d?", pointer[3]);
590 output_data(" %d %d (%d)",
591 pointer[3], pointer[4],
592 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
593 for (i = 5; i < length; i++) {
594 output_data(" ?%d?", pointer[i]);
598 case TELOPT_LINEMODE:
599 output_data("LINEMODE ");
601 output_data(" (empty suboption??\?)");
604 switch (pointer[1]) {
606 output_data("WILL ");
609 output_data("WONT ");
615 output_data("DONT ");
618 output_data("(no option??\?)");
621 switch (pointer[2]) {
623 output_data("Forward Mask");
624 for (i = 3; i < length; i++) {
625 output_data(" %x", pointer[i]);
629 output_data("%d (unknown)", pointer[2]);
630 for (i = 3; i < length; i++) {
631 output_data(" %d", pointer[i]);
639 for (i = 2; i < length - 2; i += 3) {
640 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
641 output_data(" %s", SLC_NAME(pointer[i+SLC_FUNC]));
643 output_data(" %d", pointer[i+SLC_FUNC]);
644 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
646 output_data(" NOSUPPORT"); break;
648 output_data(" CANTCHANGE"); break;
650 output_data(" VARIABLE"); break;
652 output_data(" DEFAULT"); break;
654 output_data("%s%s%s",
655 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
656 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
657 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
658 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
659 SLC_FLUSHOUT| SLC_LEVELBITS)) {
660 output_data("(0x%x)", pointer[i+SLC_FLAGS]);
662 output_data(" %d;", pointer[i+SLC_VALUE]);
663 if ((pointer[i+SLC_VALUE] == IAC) &&
664 (pointer[i+SLC_VALUE+1] == IAC))
667 for (; i < length; i++) {
668 output_data(" ?%d?", pointer[i]);
673 output_data("MODE ");
675 output_data("(no mode??\?)");
680 sprintf(tbuf, "%s%s%s%s%s",
681 pointer[2]&MODE_EDIT ? "|EDIT" : "",
682 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
683 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
684 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
685 pointer[2]&MODE_ACK ? "|ACK" : "");
686 output_data("%s", tbuf[1] ? &tbuf[1] : "0");
688 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
689 output_data(" (0x%x)", pointer[2]);
691 for (i = 3; i < length; i++) {
692 output_data(" ?0x%x?", pointer[i]);
696 output_data("%d (unknown)", pointer[1]);
697 for (i = 2; i < length; i++) {
698 output_data(" %d", pointer[i]);
703 case TELOPT_STATUS: {
707 output_data("STATUS");
709 switch (pointer[1]) {
711 if (pointer[1] == TELQUAL_SEND)
712 output_data(" SEND");
714 output_data(" %d (unknown)", pointer[1]);
715 for (i = 2; i < length; i++) {
716 output_data(" ?%d?", pointer[i]);
720 output_data(" IS\r\n");
722 for (i = 2; i < length; i++) {
724 case DO: cp = "DO"; goto common2;
725 case DONT: cp = "DONT"; goto common2;
726 case WILL: cp = "WILL"; goto common2;
727 case WONT: cp = "WONT"; goto common2;
730 if (TELOPT_OK(pointer[i]))
731 output_data(" %s %s", cp, TELOPT(pointer[i]));
733 output_data(" %s %d", cp, pointer[i]);
743 if (pointer[j] == SE) {
746 if (pointer[j+1] == SE)
751 pointer[k++] = pointer[j++];
753 printsub(0, &pointer[i], k - i);
765 output_data(" %d", pointer[i]);
774 case TELOPT_XDISPLOC:
775 output_data("X-DISPLAY-LOCATION ");
776 switch (pointer[1]) {
778 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
784 output_data("- unknown qualifier %d (0x%x).",
785 pointer[1], pointer[1]);
789 case TELOPT_NEW_ENVIRON:
790 output_data("NEW-ENVIRON ");
792 case TELOPT_OLD_ENVIRON:
793 output_data("OLD-ENVIRON");
795 switch (pointer[1]) {
800 output_data("SEND ");
803 output_data("INFO ");
807 for (i = 2; i < length; i++ ) {
808 switch (pointer[i]) {
810 output_data("\" VAR " + noquote);
815 output_data("\" VALUE " + noquote);
820 output_data("\" ESC " + noquote);
825 output_data("\" USERVAR " + noquote);
830 if (isprint(pointer[i]) && pointer[i] != '"') {
835 output_data("%c", pointer[i]);
837 output_data("\" %03o " + noquote,
854 if (TELOPT_OK(pointer[0]))
855 output_data("%s (unknown)", TELOPT(pointer[0]));
857 output_data("%d (unknown)", pointer[i]);
858 for (i = 1; i < length; i++) {
859 output_data(" %d", pointer[i]);
867 * Dump a data buffer in hex and ascii to the output data stream.
870 printdata(const char *tag, char *ptr, int cnt)
876 /* flush net output buffer if no room for new data) */
877 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
881 /* add a line of output */
882 output_data("%s: ", tag);
883 for (i = 0; i < 20 && cnt; i++) {
884 output_data("%02x", *ptr);
897 output_data(" %s\r\n", xbuf );
900 #endif /* DIAGNOSTICS */