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 $
35 * $DragonFly: src/crypto/telnet/telnetd/utility.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
40 #include <sys/utsname.h>
47 #include <libtelnet/auth.h>
50 #include <libtelnet/encrypt.h>
54 * utility functions performing io related tasks
60 * A small subroutine to flush the network output buffer, get some data
61 * from the network, and pass it through the telnet state machine. We
62 * also flush the pty input buffer (by dropping its data) if it becomes
70 DIAG(TD_REPORT
, output_data("td: ttloop\r\n"));
71 if (nfrontp
- nbackp
> 0) {
74 ncc
= read(net
, netibuf
, sizeof netibuf
);
76 syslog(LOG_INFO
, "ttloop: read: %m");
78 } else if (ncc
== 0) {
79 syslog(LOG_INFO
, "ttloop: peer died: %m");
82 DIAG(TD_REPORT
, output_data("td: ttloop read %d chars\r\n", ncc
));
84 telrcv(); /* state machine */
86 pfrontp
= pbackp
= ptyobuf
;
92 * Check a descriptor to see if out of band data exists on it.
97 static struct timeval timeout
= { 0, 0 };
104 memset((char *)&timeout
, 0, sizeof timeout
);
105 value
= select(s
+1, NULL
, NULL
, &excepts
, &timeout
);
106 } while ((value
== -1) && (errno
== EINTR
));
109 fatalperror(pty
, "select");
111 if (FD_ISSET(s
, &excepts
)) {
123 if ((n
= pfrontp
- pbackp
) > 0) {
124 DIAG(TD_REPORT
| TD_PTYDATA
,
125 output_data("td: ptyflush %d chars\r\n", n
));
126 DIAG(TD_PTYDATA
, printdata("pd", pbackp
, n
));
127 n
= write(pty
, pbackp
, n
);
130 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
135 if (pbackp
== pfrontp
)
136 pbackp
= pfrontp
= ptyobuf
;
142 * Return the address of the next "item" in the TELNET data
143 * stream. This will be the address of the next character if
144 * the current address is a user data character, or it will
145 * be the address of the character following the TELNET command
146 * if the current address is a TELNET IAC ("I Am a Command")
150 nextitem(char *current
)
152 if ((*current
&0xff) != IAC
) {
155 switch (*(current
+1)&0xff) {
161 case SB
: /* loop forever looking for the SE */
163 char *look
= current
+2;
166 if ((*look
++&0xff) == IAC
) {
167 if ((*look
++&0xff) == SE
) {
176 } /* end of nextitem */
181 * We are about to do a TELNET SYNCH operation. Clear
182 * the path to the network.
184 * Things are a bit tricky since we may have sent the first
185 * byte or so of a previous TELNET command into the network.
186 * So, we have to scan the network buffer from the beginning
187 * until we are up to where we want to be.
189 * A side effect of what we do, just to keep things
190 * simple, is to clear the urgent data pointer. The principal
191 * caller should be setting the urgent data pointer AFTER calling
197 char *thisitem
, *next
;
199 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
200 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
203 thisitem
= nclearto
> netobuf
? nclearto
: netobuf
;
204 #else /* ENCRYPTION */
206 #endif /* ENCRYPTION */
208 while ((next
= nextitem(thisitem
)) <= nbackp
) {
212 /* Now, thisitem is first before/at boundary. */
215 good
= nclearto
> netobuf
? nclearto
: netobuf
;
216 #else /* ENCRYPTION */
217 good
= netobuf
; /* where the good bytes go */
218 #endif /* ENCRYPTION */
220 while (nfrontp
> thisitem
) {
221 if (wewant(thisitem
)) {
226 next
= nextitem(next
);
227 } while (wewant(next
) && (nfrontp
> next
));
228 length
= next
-thisitem
;
229 memmove(good
, thisitem
, length
);
233 thisitem
= nextitem(thisitem
);
238 nfrontp
= good
; /* next byte to be sent */
240 } /* end of netclear */
244 * Send as much data as possible to the network,
245 * handling requests for urgent data.
253 while ((n
= nfrontp
- nbackp
) > 0) {
255 /* XXX This causes output_data() to recurse and die */
257 n
+= output_data("td: netflush %d chars\r\n", n
);
261 if (encrypt_output
) {
262 char *s
= nclearto
? nclearto
: nbackp
;
263 if (nfrontp
- s
> 0) {
264 (*encrypt_output
)((unsigned char *)s
, nfrontp
-s
);
268 #endif /* ENCRYPTION */
270 * if no urgent data, or if the other side appears to be an
271 * old 4.2 client (and thus unable to survive TCP urgent data),
272 * write the entire buffer in non-OOB mode.
274 if ((neturg
== 0) || (not42
== 0)) {
275 n
= write(net
, nbackp
, n
); /* normal write */
279 * In 4.2 (and 4.3) systems, there is some question about
280 * what byte in a sendOOB operation is the "OOB" data.
281 * To make ourselves compatible, we only send ONE byte
282 * out of band, the one WE THINK should be OOB (though
283 * we really have more the TCP philosophy of urgent data
284 * rather than the Unix philosophy of OOB data).
287 n
= send(net
, nbackp
, n
-1, 0); /* send URGENT all by itself */
289 n
= send(net
, nbackp
, n
, MSG_OOB
); /* URGENT data */
293 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
300 if (nbackp
> nclearto
)
302 #endif /* ENCRYPTION */
303 if (nbackp
>= neturg
) {
306 if (nbackp
== nfrontp
) {
307 nbackp
= nfrontp
= netobuf
;
310 #endif /* ENCRYPTION */
314 } /* end of netflush */
318 * miscellaneous functions doing a variety of little jobs follow ...
323 fatal(int f
, const char *msg
)
327 (void) snprintf(buf
, sizeof(buf
), "telnetd: %s.\r\n", msg
);
329 if (encrypt_output
) {
331 * Better turn off encryption first....
337 #endif /* ENCRYPTION */
338 (void) write(f
, buf
, (int)strlen(buf
));
344 fatalperror(int f
, const char *msg
)
348 (void) snprintf(buf
, sizeof(buf
), "%s: %s", msg
, strerror(errno
));
355 edithost(char *pat
, char *host
)
357 char *res
= editedhost
;
378 if (res
== &editedhost
[sizeof editedhost
- 1]) {
385 (void) strncpy(res
, host
,
386 sizeof editedhost
- (res
- editedhost
) -1);
389 editedhost
[sizeof editedhost
- 1] = '\0';
392 static char *putlocation
;
395 putstr(const char *s
)
409 static char fmtstr
[] = { "%+" };
411 static char fmtstr
[] = { "%l:%M%P on %A, %d %B %Y" };
415 putf(char *cp
, char *where
)
421 static struct utsname kerninfo
;
423 if (!*kerninfo
.sysname
)
434 } else if (*cp
!= '%') {
442 /* names are like /dev/pts/2 -- we want pts/2 */
443 slash
= strchr(line
+1, '/');
445 slash
= strrchr(line
, '/');
447 if (slash
== (char *) 0)
459 setlocale(LC_TIME
, "");
462 (void)strftime(db
, sizeof(db
), fmtstr
, localtime(&t
));
468 putstr(kerninfo
.sysname
);
472 putstr(kerninfo
.machine
);
476 putstr(kerninfo
.release
);
480 putstr(kerninfo
.version
);
494 * Print telnet options and commands in plain text, if possible.
497 printoption(const char *fmt
, int option
)
499 if (TELOPT_OK(option
))
500 output_data("%s %s\r\n", fmt
, TELOPT(option
));
501 else if (TELCMD_OK(option
))
502 output_data("%s %s\r\n", fmt
, TELCMD(option
));
504 output_data("%s %d\r\n", fmt
, option
);
509 printsub(char direction
, unsigned char *pointer
, int length
)
513 if (!(diagnostic
& TD_OPTIONS
))
517 output_data("td: %s suboption ",
518 direction
== '<' ? "recv" : "send");
522 i
= pointer
[length
-2];
523 j
= pointer
[length
-1];
525 if (i
!= IAC
|| j
!= SE
) {
526 output_data("(terminated by ");
528 output_data("%s ", TELOPT(i
));
529 else if (TELCMD_OK(i
))
530 output_data("%s ", TELCMD(i
));
532 output_data("%d ", i
);
534 output_data("%s", TELOPT(j
));
535 else if (TELCMD_OK(j
))
536 output_data("%s", TELCMD(j
));
538 output_data("%d", j
);
539 output_data(", not IAC SE!) ");
545 output_data("(Empty suboption??\?)");
548 switch (pointer
[0]) {
550 output_data("TERMINAL-TYPE ");
551 switch (pointer
[1]) {
553 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
560 "- unknown qualifier %d (0x%x).",
561 pointer
[1], pointer
[1]);
565 output_data("TERMINAL-SPEED");
567 output_data(" (empty suboption??\?)");
570 switch (pointer
[1]) {
572 output_data(" IS %.*s", length
-2, (char *)pointer
+2);
576 output_data(" SEND");
578 output_data(" %d (unknown)", pointer
[1]);
579 for (i
= 2; i
< length
; i
++) {
580 output_data(" ?%d?", pointer
[i
]);
587 output_data("TOGGLE-FLOW-CONTROL");
589 output_data(" (empty suboption??\?)");
592 switch (pointer
[1]) {
594 output_data(" OFF"); break;
596 output_data(" ON"); break;
597 case LFLOW_RESTART_ANY
:
598 output_data(" RESTART-ANY"); break;
599 case LFLOW_RESTART_XON
:
600 output_data(" RESTART-XON"); break;
602 output_data(" %d (unknown)", pointer
[1]);
604 for (i
= 2; i
< length
; i
++) {
605 output_data(" ?%d?", pointer
[i
]);
612 output_data(" (empty suboption??\?)");
616 output_data(" ?%d?", pointer
[1]);
619 output_data(" %d %d (%d)",
620 pointer
[1], pointer
[2],
621 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
623 output_data(" ?%d?", pointer
[3]);
626 output_data(" %d %d (%d)",
627 pointer
[3], pointer
[4],
628 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
629 for (i
= 5; i
< length
; i
++) {
630 output_data(" ?%d?", pointer
[i
]);
634 case TELOPT_LINEMODE
:
635 output_data("LINEMODE ");
637 output_data(" (empty suboption??\?)");
640 switch (pointer
[1]) {
642 output_data("WILL ");
645 output_data("WONT ");
651 output_data("DONT ");
654 output_data("(no option??\?)");
657 switch (pointer
[2]) {
659 output_data("Forward Mask");
660 for (i
= 3; i
< length
; i
++) {
661 output_data(" %x", pointer
[i
]);
665 output_data("%d (unknown)", pointer
[2]);
666 for (i
= 3; i
< length
; i
++) {
667 output_data(" %d", pointer
[i
]);
675 for (i
= 2; i
< length
- 2; i
+= 3) {
676 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
677 output_data(" %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
679 output_data(" %d", pointer
[i
+SLC_FUNC
]);
680 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
682 output_data(" NOSUPPORT"); break;
684 output_data(" CANTCHANGE"); break;
686 output_data(" VARIABLE"); break;
688 output_data(" DEFAULT"); break;
690 output_data("%s%s%s",
691 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
692 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
693 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
694 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
695 SLC_FLUSHOUT
| SLC_LEVELBITS
)) {
696 output_data("(0x%x)", pointer
[i
+SLC_FLAGS
]);
698 output_data(" %d;", pointer
[i
+SLC_VALUE
]);
699 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
700 (pointer
[i
+SLC_VALUE
+1] == IAC
))
703 for (; i
< length
; i
++) {
704 output_data(" ?%d?", pointer
[i
]);
709 output_data("MODE ");
711 output_data("(no mode??\?)");
716 sprintf(tbuf
, "%s%s%s%s%s",
717 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
718 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
719 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
720 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
721 pointer
[2]&MODE_ACK
? "|ACK" : "");
722 output_data("%s", tbuf
[1] ? &tbuf
[1] : "0");
724 if (pointer
[2]&~(MODE_EDIT
|MODE_TRAPSIG
|MODE_ACK
)) {
725 output_data(" (0x%x)", pointer
[2]);
727 for (i
= 3; i
< length
; i
++) {
728 output_data(" ?0x%x?", pointer
[i
]);
732 output_data("%d (unknown)", pointer
[1]);
733 for (i
= 2; i
< length
; i
++) {
734 output_data(" %d", pointer
[i
]);
739 case TELOPT_STATUS
: {
743 output_data("STATUS");
745 switch (pointer
[1]) {
747 if (pointer
[1] == TELQUAL_SEND
)
748 output_data(" SEND");
750 output_data(" %d (unknown)", pointer
[1]);
751 for (i
= 2; i
< length
; i
++) {
752 output_data(" ?%d?", pointer
[i
]);
756 output_data(" IS\r\n");
758 for (i
= 2; i
< length
; i
++) {
760 case DO
: cp
= "DO"; goto common2
;
761 case DONT
: cp
= "DONT"; goto common2
;
762 case WILL
: cp
= "WILL"; goto common2
;
763 case WONT
: cp
= "WONT"; goto common2
;
766 if (TELOPT_OK(pointer
[i
]))
767 output_data(" %s %s", cp
, TELOPT(pointer
[i
]));
769 output_data(" %s %d", cp
, pointer
[i
]);
779 if (pointer
[j
] == SE
) {
782 if (pointer
[j
+1] == SE
)
787 pointer
[k
++] = pointer
[j
++];
789 printsub(0, &pointer
[i
], k
- i
);
801 output_data(" %d", pointer
[i
]);
810 case TELOPT_XDISPLOC
:
811 output_data("X-DISPLAY-LOCATION ");
812 switch (pointer
[1]) {
814 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
820 output_data("- unknown qualifier %d (0x%x).",
821 pointer
[1], pointer
[1]);
825 case TELOPT_NEW_ENVIRON
:
826 output_data("NEW-ENVIRON ");
828 case TELOPT_OLD_ENVIRON
:
829 output_data("OLD-ENVIRON");
831 switch (pointer
[1]) {
836 output_data("SEND ");
839 output_data("INFO ");
843 for (i
= 2; i
< length
; i
++ ) {
844 switch (pointer
[i
]) {
846 output_data("\" VAR " + noquote
);
851 output_data("\" VALUE " + noquote
);
856 output_data("\" ESC " + noquote
);
861 output_data("\" USERVAR " + noquote
);
866 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
871 output_data("%c", pointer
[i
]);
873 output_data("\" %03o " + noquote
,
887 #ifdef AUTHENTICATION
888 case TELOPT_AUTHENTICATION
:
889 output_data("AUTHENTICATION");
892 output_data(" (empty suboption??\?)");
895 switch (pointer
[1]) {
898 output_data(" %s ", (pointer
[1] == TELQUAL_IS
) ?
900 if (AUTHTYPE_NAME_OK(pointer
[2]))
901 output_data("%s ", AUTHTYPE_NAME(pointer
[2]));
903 output_data("%d ", pointer
[2]);
905 output_data("(partial suboption??\?)");
909 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
911 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
912 "MUTUAL" : "ONE-WAY");
916 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
917 output_data("%s", buf
);
923 output_data(" SEND ");
925 if (AUTHTYPE_NAME_OK(pointer
[i
]))
926 output_data("%s ", AUTHTYPE_NAME(pointer
[i
]));
928 output_data("%d ", pointer
[i
]);
930 output_data("(partial suboption??\?)");
933 output_data("%s|%s ",
934 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
936 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
937 "MUTUAL" : "ONE-WAY");
943 output_data(" NAME \"%.*s\"", length
- 2, pointer
+ 2);
947 for (i
= 2; i
< length
; i
++) {
948 output_data(" ?%d?", pointer
[i
]);
957 output_data("ENCRYPT");
959 output_data(" (empty suboption??\?)");
962 switch (pointer
[1]) {
964 output_data(" START");
971 case ENCRYPT_REQSTART
:
972 output_data(" REQUEST-START");
976 output_data(" REQUEST-END");
981 output_data(" %s ", (pointer
[1] == ENCRYPT_IS
) ?
984 output_data(" (partial suboption??\?)");
987 if (ENCTYPE_NAME_OK(pointer
[2]))
988 output_data("%s ", ENCTYPE_NAME(pointer
[2]));
990 output_data(" %d (unknown)", pointer
[2]);
994 encrypt_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
995 output_data("%s", buf
);
999 case ENCRYPT_SUPPORT
:
1001 output_data(" SUPPORT ");
1002 while (i
< length
) {
1003 if (ENCTYPE_NAME_OK(pointer
[i
]))
1004 output_data("%s ", ENCTYPE_NAME(pointer
[i
]));
1006 output_data("%d ", pointer
[i
]);
1011 case ENCRYPT_ENC_KEYID
:
1012 output_data(" ENC_KEYID");
1015 case ENCRYPT_DEC_KEYID
:
1016 output_data(" DEC_KEYID");
1020 output_data(" %d (unknown)", pointer
[1]);
1022 for (i
= 2; i
< length
; i
++) {
1023 output_data(" %d", pointer
[i
]);
1028 #endif /* ENCRYPTION */
1031 if (TELOPT_OK(pointer
[0]))
1032 output_data("%s (unknown)", TELOPT(pointer
[0]));
1034 output_data("%d (unknown)", pointer
[i
]);
1035 for (i
= 1; i
< length
; i
++) {
1036 output_data(" %d", pointer
[i
]);
1040 output_data("\r\n");
1044 * Dump a data buffer in hex and ascii to the output data stream.
1047 printdata(const char *tag
, char *ptr
, int cnt
)
1053 /* flush net output buffer if no room for new data) */
1054 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 80) {
1058 /* add a line of output */
1059 output_data("%s: ", tag
);
1060 for (i
= 0; i
< 20 && cnt
; i
++) {
1061 output_data("%02x", *ptr
);
1062 if (isprint(*ptr
)) {
1074 output_data(" %s\r\n", xbuf
);
1077 #endif /* DIAGNOSTICS */