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/libexec/telnetd/utility.c,v 1.13.2.4 2002/04/13 11:07:12 markm Exp $
35 * $DragonFly: src/libexec/telnetd/utility.c,v 1.3 2004/02/13 03:49:50 dillon Exp $
38 #if defined(__DragonFly__) || defined(__FreeBSD__)
40 #include <sys/utsname.h>
48 * utility functions performing io related tasks
54 * A small subroutine to flush the network output buffer, get some data
55 * from the network, and pass it through the telnet state machine. We
56 * also flush the pty input buffer (by dropping its data) if it becomes
64 DIAG(TD_REPORT, output_data("td: ttloop\r\n"));
65 if (nfrontp - nbackp > 0) {
68 ncc = read(net, netibuf, sizeof netibuf);
70 syslog(LOG_INFO, "ttloop: read: %m");
72 } else if (ncc == 0) {
73 syslog(LOG_INFO, "ttloop: peer died: %m");
76 DIAG(TD_REPORT, output_data("td: ttloop read %d chars\r\n", ncc));
78 telrcv(); /* state machine */
80 pfrontp = pbackp = ptyobuf;
86 * Check a descriptor to see if out of band data exists on it.
91 static struct timeval timeout = { 0, 0 };
98 memset((char *)&timeout, 0, sizeof timeout);
99 value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
100 } while ((value == -1) && (errno == EINTR));
103 fatalperror(pty, "select");
105 if (FD_ISSET(s, &excepts)) {
117 if ((n = pfrontp - pbackp) > 0) {
118 DIAG(TD_REPORT | TD_PTYDATA,
119 output_data("td: ptyflush %d chars\r\n", n));
120 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
121 n = write(pty, pbackp, n);
124 if (errno == EWOULDBLOCK || errno == EINTR)
129 if (pbackp == pfrontp)
130 pbackp = pfrontp = ptyobuf;
136 * Return the address of the next "item" in the TELNET data
137 * stream. This will be the address of the next character if
138 * the current address is a user data character, or it will
139 * be the address of the character following the TELNET command
140 * if the current address is a TELNET IAC ("I Am a Command")
144 nextitem(char *current)
146 if ((*current&0xff) != IAC) {
149 switch (*(current+1)&0xff) {
155 case SB: /* loop forever looking for the SE */
157 char *look = current+2;
160 if ((*look++&0xff) == IAC) {
161 if ((*look++&0xff) == SE) {
170 } /* end of nextitem */
175 * We are about to do a TELNET SYNCH operation. Clear
176 * the path to the network.
178 * Things are a bit tricky since we may have sent the first
179 * byte or so of a previous TELNET command into the network.
180 * So, we have to scan the network buffer from the beginning
181 * until we are up to where we want to be.
183 * A side effect of what we do, just to keep things
184 * simple, is to clear the urgent data pointer. The principal
185 * caller should be setting the urgent data pointer AFTER calling
191 char *thisitem, *next;
193 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
194 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
198 while ((next = nextitem(thisitem)) <= nbackp) {
202 /* Now, thisitem is first before/at boundary. */
204 good = netobuf; /* where the good bytes go */
206 while (nfrontp > thisitem) {
207 if (wewant(thisitem)) {
212 next = nextitem(next);
213 } while (wewant(next) && (nfrontp > next));
214 length = next-thisitem;
215 memmove(good, thisitem, length);
219 thisitem = nextitem(thisitem);
224 nfrontp = good; /* next byte to be sent */
226 } /* end of netclear */
230 * Send as much data as possible to the network,
231 * handling requests for urgent data.
239 while ((n = nfrontp - nbackp) > 0) {
241 /* XXX This causes output_data() to recurse and die */
243 n += output_data("td: netflush %d chars\r\n", n);
247 * if no urgent data, or if the other side appears to be an
248 * old 4.2 client (and thus unable to survive TCP urgent data),
249 * write the entire buffer in non-OOB mode.
251 if ((neturg == 0) || (not42 == 0)) {
252 n = write(net, nbackp, n); /* normal write */
256 * In 4.2 (and 4.3) systems, there is some question about
257 * what byte in a sendOOB operation is the "OOB" data.
258 * To make ourselves compatible, we only send ONE byte
259 * out of band, the one WE THINK should be OOB (though
260 * we really have more the TCP philosophy of urgent data
261 * rather than the Unix philosophy of OOB data).
264 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
266 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
270 if (errno == EWOULDBLOCK || errno == EINTR)
276 if (nbackp >= neturg) {
279 if (nbackp == nfrontp) {
280 nbackp = nfrontp = netobuf;
284 } /* end of netflush */
288 * miscellaneous functions doing a variety of little jobs follow ...
293 fatal(int f, const char *msg)
297 (void) snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
298 (void) write(f, buf, (int)strlen(buf));
304 fatalperror(int f, const char *msg)
308 (void) snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
315 edithost(char *pat, char *host)
317 char *res = editedhost;
338 if (res == &editedhost[sizeof editedhost - 1]) {
345 (void) strncpy(res, host,
346 sizeof editedhost - (res - editedhost) -1);
349 editedhost[sizeof editedhost - 1] = '\0';
352 static char *putlocation;
355 putstr(const char *s)
368 #if defined(__DragonFly__) || defined(__FreeBSD__)
369 static char fmtstr[] = { "%+" };
371 static char fmtstr[] = { "%l:%M%P on %A, %d %B %Y" };
375 putf(char *cp, char *where)
380 #if defined(__DragonFly__) || defined(__FreeBSD__)
381 static struct utsname kerninfo;
383 if (!*kerninfo.sysname)
394 } else if (*cp != '%') {
402 /* names are like /dev/pts/2 -- we want pts/2 */
403 slash = strchr(line+1, '/');
405 slash = strrchr(line, '/');
407 if (slash == (char *) 0)
418 #if defined(__DragonFly__) || defined(__FreeBSD__)
419 setlocale(LC_TIME, "");
422 (void)strftime(db, sizeof(db), fmtstr, localtime(&t));
426 #if defined(__DragonFly__) || defined(__FreeBSD__)
428 putstr(kerninfo.sysname);
432 putstr(kerninfo.machine);
436 putstr(kerninfo.release);
440 putstr(kerninfo.version);
454 * Print telnet options and commands in plain text, if possible.
457 printoption(const char *fmt, int option)
459 if (TELOPT_OK(option))
460 output_data("%s %s\r\n", fmt, TELOPT(option));
461 else if (TELCMD_OK(option))
462 output_data("%s %s\r\n", fmt, TELCMD(option));
464 output_data("%s %d\r\n", fmt, option);
469 printsub(char direction, unsigned char *pointer, int length)
473 if (!(diagnostic & TD_OPTIONS))
477 output_data("td: %s suboption ",
478 direction == '<' ? "recv" : "send");
482 i = pointer[length-2];
483 j = pointer[length-1];
485 if (i != IAC || j != SE) {
486 output_data("(terminated by ");
488 output_data("%s ", TELOPT(i));
489 else if (TELCMD_OK(i))
490 output_data("%s ", TELCMD(i));
492 output_data("%d ", i);
494 output_data("%s", TELOPT(j));
495 else if (TELCMD_OK(j))
496 output_data("%s", TELCMD(j));
498 output_data("%d", j);
499 output_data(", not IAC SE!) ");
505 output_data("(Empty suboption??\?)");
508 switch (pointer[0]) {
510 output_data("TERMINAL-TYPE ");
511 switch (pointer[1]) {
513 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
520 "- unknown qualifier %d (0x%x).",
521 pointer[1], pointer[1]);
525 output_data("TERMINAL-SPEED");
527 output_data(" (empty suboption??\?)");
530 switch (pointer[1]) {
532 output_data(" IS %.*s", length-2, (char *)pointer+2);
536 output_data(" SEND");
538 output_data(" %d (unknown)", pointer[1]);
539 for (i = 2; i < length; i++) {
540 output_data(" ?%d?", pointer[i]);
547 output_data("TOGGLE-FLOW-CONTROL");
549 output_data(" (empty suboption??\?)");
552 switch (pointer[1]) {
554 output_data(" OFF"); break;
556 output_data(" ON"); break;
557 case LFLOW_RESTART_ANY:
558 output_data(" RESTART-ANY"); break;
559 case LFLOW_RESTART_XON:
560 output_data(" RESTART-XON"); break;
562 output_data(" %d (unknown)", pointer[1]);
564 for (i = 2; i < length; i++) {
565 output_data(" ?%d?", pointer[i]);
572 output_data(" (empty suboption??\?)");
576 output_data(" ?%d?", pointer[1]);
579 output_data(" %d %d (%d)",
580 pointer[1], pointer[2],
581 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
583 output_data(" ?%d?", pointer[3]);
586 output_data(" %d %d (%d)",
587 pointer[3], pointer[4],
588 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
589 for (i = 5; i < length; i++) {
590 output_data(" ?%d?", pointer[i]);
594 case TELOPT_LINEMODE:
595 output_data("LINEMODE ");
597 output_data(" (empty suboption??\?)");
600 switch (pointer[1]) {
602 output_data("WILL ");
605 output_data("WONT ");
611 output_data("DONT ");
614 output_data("(no option??\?)");
617 switch (pointer[2]) {
619 output_data("Forward Mask");
620 for (i = 3; i < length; i++) {
621 output_data(" %x", pointer[i]);
625 output_data("%d (unknown)", pointer[2]);
626 for (i = 3; i < length; i++) {
627 output_data(" %d", pointer[i]);
635 for (i = 2; i < length - 2; i += 3) {
636 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
637 output_data(" %s", SLC_NAME(pointer[i+SLC_FUNC]));
639 output_data(" %d", pointer[i+SLC_FUNC]);
640 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
642 output_data(" NOSUPPORT"); break;
644 output_data(" CANTCHANGE"); break;
646 output_data(" VARIABLE"); break;
648 output_data(" DEFAULT"); break;
650 output_data("%s%s%s",
651 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
652 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
653 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
654 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
655 SLC_FLUSHOUT| SLC_LEVELBITS)) {
656 output_data("(0x%x)", pointer[i+SLC_FLAGS]);
658 output_data(" %d;", pointer[i+SLC_VALUE]);
659 if ((pointer[i+SLC_VALUE] == IAC) &&
660 (pointer[i+SLC_VALUE+1] == IAC))
663 for (; i < length; i++) {
664 output_data(" ?%d?", pointer[i]);
669 output_data("MODE ");
671 output_data("(no mode??\?)");
676 sprintf(tbuf, "%s%s%s%s%s",
677 pointer[2]&MODE_EDIT ? "|EDIT" : "",
678 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
679 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
680 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
681 pointer[2]&MODE_ACK ? "|ACK" : "");
682 output_data("%s", tbuf[1] ? &tbuf[1] : "0");
684 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
685 output_data(" (0x%x)", pointer[2]);
687 for (i = 3; i < length; i++) {
688 output_data(" ?0x%x?", pointer[i]);
692 output_data("%d (unknown)", pointer[1]);
693 for (i = 2; i < length; i++) {
694 output_data(" %d", pointer[i]);
699 case TELOPT_STATUS: {
703 output_data("STATUS");
705 switch (pointer[1]) {
707 if (pointer[1] == TELQUAL_SEND)
708 output_data(" SEND");
710 output_data(" %d (unknown)", pointer[1]);
711 for (i = 2; i < length; i++) {
712 output_data(" ?%d?", pointer[i]);
716 output_data(" IS\r\n");
718 for (i = 2; i < length; i++) {
720 case DO: cp = "DO"; goto common2;
721 case DONT: cp = "DONT"; goto common2;
722 case WILL: cp = "WILL"; goto common2;
723 case WONT: cp = "WONT"; goto common2;
726 if (TELOPT_OK(pointer[i]))
727 output_data(" %s %s", cp, TELOPT(pointer[i]));
729 output_data(" %s %d", cp, pointer[i]);
739 if (pointer[j] == SE) {
742 if (pointer[j+1] == SE)
747 pointer[k++] = pointer[j++];
749 printsub(0, &pointer[i], k - i);
761 output_data(" %d", pointer[i]);
770 case TELOPT_XDISPLOC:
771 output_data("X-DISPLAY-LOCATION ");
772 switch (pointer[1]) {
774 output_data("IS \"%.*s\"", length-2, (char *)pointer+2);
780 output_data("- unknown qualifier %d (0x%x).",
781 pointer[1], pointer[1]);
785 case TELOPT_NEW_ENVIRON:
786 output_data("NEW-ENVIRON ");
788 case TELOPT_OLD_ENVIRON:
789 output_data("OLD-ENVIRON");
791 switch (pointer[1]) {
796 output_data("SEND ");
799 output_data("INFO ");
803 for (i = 2; i < length; i++ ) {
804 switch (pointer[i]) {
806 output_data("\" VAR " + noquote);
811 output_data("\" VALUE " + noquote);
816 output_data("\" ESC " + noquote);
821 output_data("\" USERVAR " + noquote);
826 if (isprint(pointer[i]) && pointer[i] != '"') {
831 output_data("%c", pointer[i]);
833 output_data("\" %03o " + noquote,
850 if (TELOPT_OK(pointer[0]))
851 output_data("%s (unknown)", TELOPT(pointer[0]));
853 output_data("%d (unknown)", pointer[i]);
854 for (i = 1; i < length; i++) {
855 output_data(" %d", pointer[i]);
863 * Dump a data buffer in hex and ascii to the output data stream.
866 printdata(const char *tag, char *ptr, int cnt)
872 /* flush net output buffer if no room for new data) */
873 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
877 /* add a line of output */
878 output_data("%s: ", tag);
879 for (i = 0; i < 20 && cnt; i++) {
880 output_data("%02x", *ptr);
893 output_data(" %s\r\n", xbuf );
896 #endif /* DIAGNOSTICS */