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("$Heimdal: utility.c 15844 2005-08-08 13:36:16Z lha $"
41 * utility functions performing io related tasks
47 * A small subroutine to flush the network output buffer, get some
48 * data from the network, and pass it through the telnet state
49 * machine. We also flush the pty input buffer (by dropping its data)
50 * if it becomes too full.
52 * return 0 if OK or 1 if interrupted by a signal.
59 output_data("td: ttloop\r\n");
63 ncc
= read(net
, netibuf
, sizeof netibuf
);
67 syslog(LOG_INFO
, "ttloop: read: %m\n");
69 } else if (ncc
== 0) {
70 syslog(LOG_INFO
, "ttloop: peer died\n");
74 output_data("td: ttloop read %d chars\r\n", ncc
);
77 telrcv(); /* state machine */
79 pfrontp
= pbackp
= ptyobuf
;
86 * Check a descriptor to see if out of band data exists on it.
91 static struct timeval timeout
= { 0 };
96 fatal(ourpty
, "fd too large");
101 value
= select(s
+1, 0, 0, &excepts
, &timeout
);
102 } while ((value
== -1) && (errno
== EINTR
));
105 fatalperror(ourpty
, "select");
107 if (FD_ISSET(s
, &excepts
)) {
119 if ((n
= pfrontp
- pbackp
) > 0) {
120 DIAG((TD_REPORT
| TD_PTYDATA
), {
121 output_data("td: ptyflush %d chars\r\n", n
);
123 DIAG(TD_PTYDATA
, printdata("pd", pbackp
, n
));
124 n
= write(ourpty
, pbackp
, n
);
127 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
132 if (pbackp
== pfrontp
)
133 pbackp
= pfrontp
= ptyobuf
;
139 * Return the address of the next "item" in the TELNET data
140 * stream. This will be the address of the next character if
141 * the current address is a user data character, or it will
142 * be the address of the character following the TELNET command
143 * if the current address is a TELNET IAC ("I Am a Command")
147 nextitem(char *current
)
149 if ((*current
&0xff) != IAC
) {
152 switch (*(current
+1)&0xff) {
159 /* loop forever looking for the SE */
160 char *look
= current
+2;
163 if ((*look
++&0xff) == IAC
) {
164 if ((*look
++&0xff) == SE
) {
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))
201 thisitem
= nclearto
> netobuf
? nclearto
: netobuf
;
206 while ((next
= nextitem(thisitem
)) <= nbackp
) {
210 /* Now, thisitem is first before/at boundary. */
213 good
= nclearto
> netobuf
? nclearto
: netobuf
;
215 good
= netobuf
; /* where the good bytes go */
218 while (nfrontp
> thisitem
) {
219 if (wewant(thisitem
)) {
224 next
= nextitem(next
);
225 } while (wewant(next
) && (nfrontp
> next
));
226 length
= next
-thisitem
;
227 memmove(good
, thisitem
, length
);
231 thisitem
= nextitem(thisitem
);
236 nfrontp
= good
; /* next byte to be sent */
238 } /* end of netclear */
244 * Send as much data as possible to the network,
245 * 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(const void *ptr
, size_t 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 */
334 if ((&netobuf
[BUFSIZ
] - nfrontp
) < len
)
337 memmove(nfrontp
, ptr
, len
);
343 * miscellaneous functions doing a variety of little jobs follow ...
347 void fatal(int f
, char *msg
)
351 snprintf(buf
, sizeof(buf
), "telnetd: %s.\r\n", msg
);
353 if (encrypt_output
) {
355 * Better turn off encryption first....
362 write(f
, buf
, (int)strlen(buf
));
368 fatalperror_errno(int f
, const char *msg
, int error
)
372 snprintf(buf
, sizeof(buf
), "%s: %s", msg
, strerror(error
));
377 fatalperror(int f
, const char *msg
)
379 fatalperror_errno(f
, msg
, errno
);
384 void edithost(char *pat
, char *host
)
386 char *res
= editedhost
;
407 if (res
== &editedhost
[sizeof editedhost
- 1]) {
415 sizeof editedhost
- (res
- editedhost
));
418 editedhost
[sizeof editedhost
- 1] = '\0';
421 static char *putlocation
;
437 static char fmtstr
[] = { "%l:%M%P on %A, %d %B %Y" };
439 void putf(char *cp
, char *where
)
448 /* if we don't have uname, set these to sensible values */
449 char *sysname
= "Unix",
456 sysname
=name
.sysname
;
457 machine
=name
.machine
;
458 release
=name
.release
;
459 version
=name
.version
;
472 slash
= strchr(line
+1, '/');
473 if (slash
== (char *) 0)
501 strftime(db
, sizeof(db
), fmtstr
, localtime(&t
));
515 * Print telnet options and commands in plain text, if possible.
518 printoption(char *fmt
, int option
)
520 if (TELOPT_OK(option
))
521 output_data("%s %s\r\n",
524 else if (TELCMD_OK(option
))
525 output_data("%s %s\r\n",
529 output_data("%s %d\r\n",
536 printsub(int direction
, unsigned char *pointer
, int length
)
538 /* where suboption data sits */
539 /* length of suboption data */
542 unsigned char buf
[512];
544 if (!(diagnostic
& TD_OPTIONS
))
548 output_data("td: %s suboption ",
549 direction
== '<' ? "recv" : "send");
553 i
= pointer
[length
-2];
554 j
= pointer
[length
-1];
556 if (i
!= IAC
|| j
!= SE
) {
557 output_data("(terminated by ");
561 else if (TELCMD_OK(i
))
570 else if (TELCMD_OK(j
))
576 output_data(", not IAC SE!) ");
582 output_data("(Empty suboption??\?)");
585 switch (pointer
[0]) {
587 output_data("TERMINAL-TYPE ");
588 switch (pointer
[1]) {
590 output_data("IS \"%.*s\"",
598 output_data("- unknown qualifier %d (0x%x).",
599 pointer
[1], pointer
[1]);
603 output_data("TERMINAL-SPEED");
605 output_data(" (empty suboption??\?)");
608 switch (pointer
[1]) {
610 output_data(" IS %.*s", length
-2, (char *)pointer
+2);
614 output_data(" SEND");
616 output_data(" %d (unknown)", pointer
[1]);
617 for (i
= 2; i
< length
; i
++) {
618 output_data(" ?%d?", pointer
[i
]);
625 output_data("TOGGLE-FLOW-CONTROL");
627 output_data(" (empty suboption??\?)");
630 switch (pointer
[1]) {
637 case LFLOW_RESTART_ANY
:
638 output_data(" RESTART-ANY");
640 case LFLOW_RESTART_XON
:
641 output_data(" RESTART-XON");
644 output_data(" %d (unknown)",
647 for (i
= 2; i
< length
; i
++) {
656 output_data(" (empty suboption??\?)");
664 output_data(" %u %u(%u)",
667 (((unsigned int)pointer
[1])<<8) + pointer
[2]);
673 output_data(" %u %u(%u)",
676 (((unsigned int)pointer
[3])<<8) + pointer
[4]);
677 for (i
= 5; i
< length
; i
++) {
683 case TELOPT_LINEMODE
:
684 output_data("LINEMODE ");
686 output_data(" (empty suboption??\?)");
689 switch (pointer
[1]) {
691 output_data("WILL ");
694 output_data("WONT ");
700 output_data("DONT ");
703 output_data("(no option??\?)");
706 switch (pointer
[2]) {
708 output_data("Forward Mask");
709 for (i
= 3; i
< length
; i
++) {
710 output_data(" %x", pointer
[i
]);
714 output_data("%d (unknown)",
716 for (i
= 3; i
< length
; i
++) {
726 for (i
= 2; i
< length
- 2; i
+= 3) {
727 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
729 SLC_NAME(pointer
[i
+SLC_FUNC
]));
732 pointer
[i
+SLC_FUNC
]);
733 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
735 output_data(" NOSUPPORT");
738 output_data(" CANTCHANGE");
741 output_data(" VARIABLE");
744 output_data(" DEFAULT");
747 output_data("%s%s%s",
748 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
749 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
750 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
751 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
752 SLC_FLUSHOUT
| SLC_LEVELBITS
)) {
753 output_data("(0x%x)",
754 pointer
[i
+SLC_FLAGS
]);
757 pointer
[i
+SLC_VALUE
]);
758 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
759 (pointer
[i
+SLC_VALUE
+1] == IAC
))
762 for (; i
< length
; i
++) {
769 output_data("MODE ");
771 output_data("(no mode??\?)");
779 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
780 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
781 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
782 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
783 pointer
[2]&MODE_ACK
? "|ACK" : "");
785 tbuf
[1] ? &tbuf
[1] : "0");
787 if (pointer
[2]&~(MODE_EDIT
|MODE_TRAPSIG
|MODE_ACK
)) {
788 output_data(" (0x%x)",
791 for (i
= 3; i
< length
; i
++) {
792 output_data(" ?0x%x?",
797 output_data("%d (unknown)",
799 for (i
= 2; i
< length
; i
++) {
800 output_data(" %d", pointer
[i
]);
805 case TELOPT_STATUS
: {
809 output_data("STATUS");
811 switch (pointer
[1]) {
813 if (pointer
[1] == TELQUAL_SEND
)
814 output_data(" SEND");
816 output_data(" %d (unknown)",
818 for (i
= 2; i
< length
; i
++) {
824 output_data(" IS\r\n");
826 for (i
= 2; i
< length
; i
++) {
828 case DO
: cp
= "DO"; goto common2
;
829 case DONT
: cp
= "DONT"; goto common2
;
830 case WILL
: cp
= "WILL"; goto common2
;
831 case WONT
: cp
= "WONT"; goto common2
;
834 if (TELOPT_OK(pointer
[i
]))
835 output_data(" %s %s",
839 output_data(" %s %d",
851 if (pointer
[j
] == SE
) {
854 if (pointer
[j
+1] == SE
)
859 pointer
[k
++] = pointer
[j
++];
861 printsub(0, &pointer
[i
], k
- i
);
883 case TELOPT_XDISPLOC
:
884 output_data("X-DISPLAY-LOCATION ");
885 switch (pointer
[1]) {
887 output_data("IS \"%.*s\"",
895 output_data("- unknown qualifier %d (0x%x).",
896 pointer
[1], pointer
[1]);
900 case TELOPT_NEW_ENVIRON
:
901 output_data("NEW-ENVIRON ");
903 case TELOPT_OLD_ENVIRON
:
904 output_data("OLD-ENVIRON");
906 switch (pointer
[1]) {
911 output_data("SEND ");
914 output_data("INFO ");
918 for (i
= 2; i
< length
; i
++ ) {
919 switch (pointer
[i
]) {
921 output_data("\" VAR " + noquote
);
926 output_data("\" VALUE " + noquote
);
931 output_data("\" ESC " + noquote
);
936 output_data("\" USERVAR " + noquote
);
941 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
946 output_data ("%c", pointer
[i
]);
948 output_data("\" %03o " + noquote
,
962 #ifdef AUTHENTICATION
963 case TELOPT_AUTHENTICATION
:
964 output_data("AUTHENTICATION");
967 output_data(" (empty suboption??\?)");
970 switch (pointer
[1]) {
974 (pointer
[1] == TELQUAL_IS
) ?
976 if (AUTHTYPE_NAME_OK(pointer
[2]))
978 AUTHTYPE_NAME(pointer
[2]));
983 output_data("(partial suboption??\?)");
987 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
989 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
990 "MUTUAL" : "ONE-WAY");
992 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
999 output_data(" SEND ");
1000 while (i
< length
) {
1001 if (AUTHTYPE_NAME_OK(pointer
[i
]))
1003 AUTHTYPE_NAME(pointer
[i
]));
1007 if (++i
>= length
) {
1008 output_data("(partial suboption??\?)");
1011 output_data("%s|%s ",
1012 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
1013 "CLIENT" : "SERVER",
1014 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
1015 "MUTUAL" : "ONE-WAY");
1022 output_data(" NAME \"%.*s\"",
1028 for (i
= 2; i
< length
; i
++) {
1029 output_data(" ?%d?",
1038 case TELOPT_ENCRYPT
:
1039 output_data("ENCRYPT");
1041 output_data(" (empty suboption?)");
1044 switch (pointer
[1]) {
1046 output_data(" START");
1050 output_data(" END");
1053 case ENCRYPT_REQSTART
:
1054 output_data(" REQUEST-START");
1057 case ENCRYPT_REQEND
:
1058 output_data(" REQUEST-END");
1064 (pointer
[1] == ENCRYPT_IS
) ?
1067 output_data(" (partial suboption?)");
1070 if (ENCTYPE_NAME_OK(pointer
[2]))
1072 ENCTYPE_NAME(pointer
[2]));
1074 output_data(" %d (unknown)",
1077 encrypt_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
1082 case ENCRYPT_SUPPORT
:
1084 output_data(" SUPPORT ");
1085 while (i
< length
) {
1086 if (ENCTYPE_NAME_OK(pointer
[i
]))
1088 ENCTYPE_NAME(pointer
[i
]));
1096 case ENCRYPT_ENC_KEYID
:
1097 output_data(" ENC_KEYID %d", pointer
[1]);
1100 case ENCRYPT_DEC_KEYID
:
1101 output_data(" DEC_KEYID %d", pointer
[1]);
1105 output_data(" %d (unknown)", pointer
[1]);
1107 for (i
= 2; i
< length
; i
++) {
1108 output_data(" %d", pointer
[i
]);
1116 if (TELOPT_OK(pointer
[0]))
1117 output_data("%s (unknown)",
1118 TELOPT(pointer
[0]));
1120 output_data("%d (unknown)",
1122 for (i
= 1; i
< length
; i
++) {
1123 output_data(" %d", pointer
[i
]);
1127 output_data("\r\n");
1131 * Dump a data buffer in hex and ascii to the output data stream.
1134 printdata(char *tag
, char *ptr
, int cnt
)
1140 /* flush net output buffer if no room for new data) */
1141 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 80) {
1145 /* add a line of output */
1146 output_data("%s: ", tag
);
1147 for (i
= 0; i
< 20 && cnt
; i
++) {
1148 output_data("%02x", *ptr
);
1149 if (isprint((unsigned char)*ptr
)) {
1161 output_data(" %s\r\n", xbuf
);
1164 #endif /* DIAGNOSTICS */