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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)utility.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnetd/utility.c,v 1.5.2.4 2002/04/13 10:59:09 markm Exp $
33 #if defined(__FreeBSD__) || defined(__DragonFly__)
35 #include <sys/utsname.h>
42 #include <libtelnet/auth.h>
45 #include <libtelnet/encrypt.h>
49 * utility functions performing io related tasks
55 * A small subroutine to flush the network output buffer, get some data
56 * from the network, and pass it through the telnet state machine. We
57 * also flush the pty input buffer (by dropping its data) if it becomes
65 DIAG(TD_REPORT
, output_data("td: ttloop\r\n"));
66 if (nfrontp
- nbackp
> 0) {
69 ncc
= read(net
, netibuf
, sizeof netibuf
);
71 syslog(LOG_INFO
, "ttloop: read: %m");
73 } else if (ncc
== 0) {
74 syslog(LOG_INFO
, "ttloop: peer died: %m");
77 DIAG(TD_REPORT
, output_data("td: ttloop read %d chars\r\n", ncc
));
79 telrcv(); /* state machine */
81 pfrontp
= pbackp
= ptyobuf
;
87 * Check a descriptor to see if out of band data exists on it.
92 static struct timeval timeout
= { 0, 0 };
99 memset((char *)&timeout
, 0, sizeof timeout
);
100 value
= select(s
+1, NULL
, NULL
, &excepts
, &timeout
);
101 } while ((value
== -1) && (errno
== EINTR
));
104 fatalperror(pty
, "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
));
121 DIAG(TD_PTYDATA
, printdata("pd", pbackp
, n
));
122 n
= write(pty
, pbackp
, n
);
125 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
130 if (pbackp
== pfrontp
)
131 pbackp
= pfrontp
= ptyobuf
;
137 * Return the address of the next "item" in the TELNET data
138 * stream. This will be the address of the next character if
139 * the current address is a user data character, or it will
140 * be the address of the character following the TELNET command
141 * if the current address is a TELNET IAC ("I Am a Command")
145 nextitem(char *current
)
147 if ((*current
&0xff) != IAC
) {
150 switch (*(current
+1)&0xff) {
156 case SB
: /* loop forever looking for the SE */
158 char *look
= current
+2;
161 if ((*look
++&0xff) == IAC
) {
162 if ((*look
++&0xff) == SE
) {
171 } /* end of nextitem */
176 * We are about to do a TELNET SYNCH operation. Clear
177 * the path to the network.
179 * Things are a bit tricky since we may have sent the first
180 * byte or so of a previous TELNET command into the network.
181 * So, we have to scan the network buffer from the beginning
182 * until we are up to where we want to be.
184 * A side effect of what we do, just to keep things
185 * simple, is to clear the urgent data pointer. The principal
186 * caller should be setting the urgent data pointer AFTER calling
192 char *thisitem
, *next
;
194 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
195 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
198 thisitem
= nclearto
> netobuf
? nclearto
: netobuf
;
199 #else /* ENCRYPTION */
201 #endif /* ENCRYPTION */
203 while ((next
= nextitem(thisitem
)) <= nbackp
) {
207 /* Now, thisitem is first before/at boundary. */
210 good
= nclearto
> netobuf
? nclearto
: netobuf
;
211 #else /* ENCRYPTION */
212 good
= netobuf
; /* where the good bytes go */
213 #endif /* ENCRYPTION */
215 while (nfrontp
> thisitem
) {
216 if (wewant(thisitem
)) {
221 next
= nextitem(next
);
222 } while (wewant(next
) && (nfrontp
> next
));
223 length
= next
-thisitem
;
224 memmove(good
, thisitem
, length
);
228 thisitem
= nextitem(thisitem
);
233 nfrontp
= good
; /* next byte to be sent */
235 } /* end of netclear */
239 * Send as much data as possible to the network,
240 * handling requests for urgent data.
248 while ((n
= nfrontp
- nbackp
) > 0) {
250 /* XXX This causes output_data() to recurse and die */
252 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
);
263 #endif /* ENCRYPTION */
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 ((neturg
== 0) || (not42
== 0)) {
270 n
= write(net
, nbackp
, n
); /* normal write */
274 * In 4.2 (and 4.3) systems, there is some question about
275 * what byte in a sendOOB operation is the "OOB" data.
276 * To make ourselves compatible, we only send ONE byte
277 * out of band, the one WE THINK should be OOB (though
278 * we really have more the TCP philosophy of urgent data
279 * rather than the Unix philosophy of OOB data).
282 n
= send(net
, nbackp
, n
-1, 0); /* send URGENT all by itself */
284 n
= send(net
, nbackp
, n
, MSG_OOB
); /* URGENT data */
288 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
295 if (nbackp
> nclearto
)
297 #endif /* ENCRYPTION */
298 if (nbackp
>= neturg
) {
301 if (nbackp
== nfrontp
) {
302 nbackp
= nfrontp
= netobuf
;
305 #endif /* ENCRYPTION */
309 } /* end of netflush */
313 * miscellaneous functions doing a variety of little jobs follow ...
318 fatal(int f
, const char *msg
)
322 (void) snprintf(buf
, sizeof(buf
), "telnetd: %s.\r\n", msg
);
324 if (encrypt_output
) {
326 * Better turn off encryption first....
332 #endif /* ENCRYPTION */
333 (void) write(f
, buf
, (int)strlen(buf
));
339 fatalperror(int f
, const char *msg
)
343 (void) snprintf(buf
, sizeof(buf
), "%s: %s", msg
, strerror(errno
));
350 edithost(char *pat
, char *host
)
352 char *res
= editedhost
;
373 if (res
== &editedhost
[sizeof editedhost
- 1]) {
380 (void) strncpy(res
, host
,
381 sizeof editedhost
- (res
- editedhost
) -1);
384 editedhost
[sizeof editedhost
- 1] = '\0';
387 static char *putlocation
;
390 putstr(const char *s
)
403 #if defined(__FreeBSD__) || defined(__DragonFly__)
404 static char fmtstr
[] = { "%+" };
406 static char fmtstr
[] = { "%l:%M%P on %A, %d %B %Y" };
410 putf(char *cp
, char *where
)
415 #if defined(__FreeBSD__) || defined(__DragonFly__)
416 static struct utsname kerninfo
;
418 if (!*kerninfo
.sysname
)
429 } else if (*cp
!= '%') {
437 /* names are like /dev/pts/2 -- we want pts/2 */
438 slash
= strchr(line
+1, '/');
440 slash
= strrchr(line
, '/');
442 if (slash
== (char *) 0)
453 #if defined(__FreeBSD__) || defined(__DragonFly__)
454 setlocale(LC_TIME
, "");
457 (void)strftime(db
, sizeof(db
), fmtstr
, localtime(&t
));
461 #if defined(__FreeBSD__) || defined(__DragonFly__)
463 putstr(kerninfo
.sysname
);
467 putstr(kerninfo
.machine
);
471 putstr(kerninfo
.release
);
475 putstr(kerninfo
.version
);
489 * Print telnet options and commands in plain text, if possible.
492 printoption(const char *fmt
, int option
)
494 if (TELOPT_OK(option
))
495 output_data("%s %s\r\n", fmt
, TELOPT(option
));
496 else if (TELCMD_OK(option
))
497 output_data("%s %s\r\n", fmt
, TELCMD(option
));
499 output_data("%s %d\r\n", fmt
, option
);
504 printsub(char direction
, unsigned char *pointer
, int length
)
508 if (!(diagnostic
& TD_OPTIONS
))
512 output_data("td: %s suboption ",
513 direction
== '<' ? "recv" : "send");
517 i
= pointer
[length
-2];
518 j
= pointer
[length
-1];
520 if (i
!= IAC
|| j
!= SE
) {
521 output_data("(terminated by ");
523 output_data("%s ", TELOPT(i
));
524 else if (TELCMD_OK(i
))
525 output_data("%s ", TELCMD(i
));
527 output_data("%d ", i
);
529 output_data("%s", TELOPT(j
));
530 else if (TELCMD_OK(j
))
531 output_data("%s", TELCMD(j
));
533 output_data("%d", j
);
534 output_data(", not IAC SE!) ");
540 output_data("(Empty suboption??\?)");
543 switch (pointer
[0]) {
545 output_data("TERMINAL-TYPE ");
546 switch (pointer
[1]) {
548 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
555 "- unknown qualifier %d (0x%x).",
556 pointer
[1], pointer
[1]);
560 output_data("TERMINAL-SPEED");
562 output_data(" (empty suboption??\?)");
565 switch (pointer
[1]) {
567 output_data(" IS %.*s", length
-2, (char *)pointer
+2);
571 output_data(" SEND");
573 output_data(" %d (unknown)", pointer
[1]);
574 for (i
= 2; i
< length
; i
++) {
575 output_data(" ?%d?", pointer
[i
]);
582 output_data("TOGGLE-FLOW-CONTROL");
584 output_data(" (empty suboption??\?)");
587 switch (pointer
[1]) {
589 output_data(" OFF"); break;
591 output_data(" ON"); break;
592 case LFLOW_RESTART_ANY
:
593 output_data(" RESTART-ANY"); break;
594 case LFLOW_RESTART_XON
:
595 output_data(" RESTART-XON"); break;
597 output_data(" %d (unknown)", pointer
[1]);
599 for (i
= 2; i
< length
; i
++) {
600 output_data(" ?%d?", pointer
[i
]);
607 output_data(" (empty suboption??\?)");
611 output_data(" ?%d?", pointer
[1]);
614 output_data(" %d %d (%d)",
615 pointer
[1], pointer
[2],
616 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
618 output_data(" ?%d?", pointer
[3]);
621 output_data(" %d %d (%d)",
622 pointer
[3], pointer
[4],
623 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
624 for (i
= 5; i
< length
; i
++) {
625 output_data(" ?%d?", pointer
[i
]);
629 case TELOPT_LINEMODE
:
630 output_data("LINEMODE ");
632 output_data(" (empty suboption??\?)");
635 switch (pointer
[1]) {
637 output_data("WILL ");
640 output_data("WONT ");
646 output_data("DONT ");
649 output_data("(no option??\?)");
652 switch (pointer
[2]) {
654 output_data("Forward Mask");
655 for (i
= 3; i
< length
; i
++) {
656 output_data(" %x", pointer
[i
]);
660 output_data("%d (unknown)", pointer
[2]);
661 for (i
= 3; i
< length
; i
++) {
662 output_data(" %d", pointer
[i
]);
670 for (i
= 2; i
< length
- 2; i
+= 3) {
671 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
672 output_data(" %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
674 output_data(" %d", pointer
[i
+SLC_FUNC
]);
675 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
677 output_data(" NOSUPPORT"); break;
679 output_data(" CANTCHANGE"); break;
681 output_data(" VARIABLE"); break;
683 output_data(" DEFAULT"); break;
685 output_data("%s%s%s",
686 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
687 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
688 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
689 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
690 SLC_FLUSHOUT
| SLC_LEVELBITS
)) {
691 output_data("(0x%x)", pointer
[i
+SLC_FLAGS
]);
693 output_data(" %d;", pointer
[i
+SLC_VALUE
]);
694 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
695 (pointer
[i
+SLC_VALUE
+1] == IAC
))
698 for (; i
< length
; i
++) {
699 output_data(" ?%d?", pointer
[i
]);
704 output_data("MODE ");
706 output_data("(no mode??\?)");
711 sprintf(tbuf
, "%s%s%s%s%s",
712 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
713 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
714 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
715 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
716 pointer
[2]&MODE_ACK
? "|ACK" : "");
717 output_data("%s", tbuf
[1] ? &tbuf
[1] : "0");
719 if (pointer
[2]&~(MODE_EDIT
|MODE_TRAPSIG
|MODE_ACK
)) {
720 output_data(" (0x%x)", pointer
[2]);
722 for (i
= 3; i
< length
; i
++) {
723 output_data(" ?0x%x?", pointer
[i
]);
727 output_data("%d (unknown)", pointer
[1]);
728 for (i
= 2; i
< length
; i
++) {
729 output_data(" %d", pointer
[i
]);
734 case TELOPT_STATUS
: {
738 output_data("STATUS");
740 switch (pointer
[1]) {
742 if (pointer
[1] == TELQUAL_SEND
)
743 output_data(" SEND");
745 output_data(" %d (unknown)", pointer
[1]);
746 for (i
= 2; i
< length
; i
++) {
747 output_data(" ?%d?", pointer
[i
]);
751 output_data(" IS\r\n");
753 for (i
= 2; i
< length
; i
++) {
755 case DO
: cp
= "DO"; goto common2
;
756 case DONT
: cp
= "DONT"; goto common2
;
757 case WILL
: cp
= "WILL"; goto common2
;
758 case WONT
: cp
= "WONT"; goto common2
;
761 if (TELOPT_OK(pointer
[i
]))
762 output_data(" %s %s", cp
, TELOPT(pointer
[i
]));
764 output_data(" %s %d", cp
, pointer
[i
]);
774 if (pointer
[j
] == SE
) {
777 if (pointer
[j
+1] == SE
)
782 pointer
[k
++] = pointer
[j
++];
784 printsub(0, &pointer
[i
], k
- i
);
796 output_data(" %d", pointer
[i
]);
805 case TELOPT_XDISPLOC
:
806 output_data("X-DISPLAY-LOCATION ");
807 switch (pointer
[1]) {
809 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
815 output_data("- unknown qualifier %d (0x%x).",
816 pointer
[1], pointer
[1]);
820 case TELOPT_NEW_ENVIRON
:
821 output_data("NEW-ENVIRON ");
823 case TELOPT_OLD_ENVIRON
:
824 output_data("OLD-ENVIRON");
826 switch (pointer
[1]) {
831 output_data("SEND ");
834 output_data("INFO ");
838 for (i
= 2; i
< length
; i
++ ) {
839 switch (pointer
[i
]) {
841 output_data("%s", "\" VAR " + noquote
);
846 output_data("%s", "\" VALUE " + noquote
);
851 output_data("%s", "\" ESC " + noquote
);
856 output_data("%s", "\" USERVAR " + noquote
);
861 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
866 output_data("%c", pointer
[i
]);
868 output_data("\" %03o " + noquote
,
882 #ifdef AUTHENTICATION
883 case TELOPT_AUTHENTICATION
:
884 output_data("AUTHENTICATION");
887 output_data(" (empty suboption??\?)");
890 switch (pointer
[1]) {
893 output_data(" %s ", (pointer
[1] == TELQUAL_IS
) ?
895 if (AUTHTYPE_NAME_OK(pointer
[2]))
896 output_data("%s ", AUTHTYPE_NAME(pointer
[2]));
898 output_data("%d ", pointer
[2]);
900 output_data("(partial suboption??\?)");
904 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
906 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
907 "MUTUAL" : "ONE-WAY");
911 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
912 output_data("%s", buf
);
918 output_data(" SEND ");
920 if (AUTHTYPE_NAME_OK(pointer
[i
]))
921 output_data("%s ", AUTHTYPE_NAME(pointer
[i
]));
923 output_data("%d ", pointer
[i
]);
925 output_data("(partial suboption??\?)");
928 output_data("%s|%s ",
929 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
931 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
932 "MUTUAL" : "ONE-WAY");
938 output_data(" NAME \"%.*s\"", length
- 2, pointer
+ 2);
942 for (i
= 2; i
< length
; i
++) {
943 output_data(" ?%d?", pointer
[i
]);
952 output_data("ENCRYPT");
954 output_data(" (empty suboption??\?)");
957 switch (pointer
[1]) {
959 output_data(" START");
966 case ENCRYPT_REQSTART
:
967 output_data(" REQUEST-START");
971 output_data(" REQUEST-END");
976 output_data(" %s ", (pointer
[1] == ENCRYPT_IS
) ?
979 output_data(" (partial suboption??\?)");
982 if (ENCTYPE_NAME_OK(pointer
[2]))
983 output_data("%s ", ENCTYPE_NAME(pointer
[2]));
985 output_data(" %d (unknown)", pointer
[2]);
989 encrypt_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
990 output_data("%s", buf
);
994 case ENCRYPT_SUPPORT
:
996 output_data(" SUPPORT ");
998 if (ENCTYPE_NAME_OK(pointer
[i
]))
999 output_data("%s ", ENCTYPE_NAME(pointer
[i
]));
1001 output_data("%d ", pointer
[i
]);
1006 case ENCRYPT_ENC_KEYID
:
1007 output_data(" ENC_KEYID");
1010 case ENCRYPT_DEC_KEYID
:
1011 output_data(" DEC_KEYID");
1015 output_data(" %d (unknown)", pointer
[1]);
1017 for (i
= 2; i
< length
; i
++) {
1018 output_data(" %d", pointer
[i
]);
1023 #endif /* ENCRYPTION */
1026 if (TELOPT_OK(pointer
[0]))
1027 output_data("%s (unknown)", TELOPT(pointer
[0]));
1029 output_data("%d (unknown)", pointer
[i
]);
1030 for (i
= 1; i
< length
; i
++) {
1031 output_data(" %d", pointer
[i
]);
1035 output_data("\r\n");
1039 * Dump a data buffer in hex and ascii to the output data stream.
1042 printdata(const char *tag
, char *ptr
, int cnt
)
1048 /* flush net output buffer if no room for new data) */
1049 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 80) {
1053 /* add a line of output */
1054 output_data("%s: ", tag
);
1055 for (i
= 0; i
< 20 && cnt
; i
++) {
1056 output_data("%02x", *ptr
);
1057 if (isprint(*ptr
)) {
1069 output_data(" %s\r\n", xbuf
);
1072 #endif /* DIAGNOSTICS */