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>
51 * utility functions performing io related tasks
57 * A small subroutine to flush the network output buffer, get some data
58 * from the network, and pass it through the telnet state machine. We
59 * also flush the pty input buffer (by dropping its data) if it becomes
67 DIAG(TD_REPORT
, output_data("td: ttloop\r\n"));
68 if (nfrontp
- nbackp
> 0) {
71 ncc
= read(net
, netibuf
, sizeof netibuf
);
73 syslog(LOG_INFO
, "ttloop: read: %m");
75 } else if (ncc
== 0) {
76 syslog(LOG_INFO
, "ttloop: peer died: %m");
79 DIAG(TD_REPORT
, output_data("td: ttloop read %d chars\r\n", ncc
));
81 telrcv(); /* state machine */
83 pfrontp
= pbackp
= ptyobuf
;
89 * Check a descriptor to see if out of band data exists on it.
94 static struct timeval timeout
= { 0, 0 };
101 memset((char *)&timeout
, 0, sizeof timeout
);
102 value
= select(s
+1, NULL
, NULL
, &excepts
, &timeout
);
103 } while ((value
== -1) && (errno
== EINTR
));
106 fatalperror(pty
, "select");
108 if (FD_ISSET(s
, &excepts
)) {
120 if ((n
= pfrontp
- pbackp
) > 0) {
121 DIAG(TD_REPORT
| TD_PTYDATA
,
122 output_data("td: ptyflush %d chars\r\n", n
));
123 DIAG(TD_PTYDATA
, printdata("pd", pbackp
, n
));
124 n
= write(pty
, 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) {
158 case SB
: /* loop forever looking for the SE */
160 char *look
= current
+2;
163 if ((*look
++&0xff) == IAC
) {
164 if ((*look
++&0xff) == SE
) {
173 } /* end of nextitem */
178 * We are about to do a TELNET SYNCH operation. Clear
179 * the path to the network.
181 * Things are a bit tricky since we may have sent the first
182 * byte or so of a previous TELNET command into the network.
183 * So, we have to scan the network buffer from the beginning
184 * until we are up to where we want to be.
186 * A side effect of what we do, just to keep things
187 * simple, is to clear the urgent data pointer. The principal
188 * caller should be setting the urgent data pointer AFTER calling
194 char *thisitem
, *next
;
196 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
197 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
200 thisitem
= nclearto
> netobuf
? nclearto
: netobuf
;
201 #else /* ENCRYPTION */
203 #endif /* ENCRYPTION */
205 while ((next
= nextitem(thisitem
)) <= nbackp
) {
209 /* Now, thisitem is first before/at boundary. */
212 good
= nclearto
> netobuf
? nclearto
: netobuf
;
213 #else /* ENCRYPTION */
214 good
= netobuf
; /* where the good bytes go */
215 #endif /* ENCRYPTION */
217 while (nfrontp
> thisitem
) {
218 if (wewant(thisitem
)) {
223 next
= nextitem(next
);
224 } while (wewant(next
) && (nfrontp
> next
));
225 length
= next
-thisitem
;
226 memmove(good
, thisitem
, length
);
230 thisitem
= nextitem(thisitem
);
235 nfrontp
= good
; /* next byte to be sent */
237 } /* end of netclear */
241 * Send as much data as possible to the network,
242 * handling requests for urgent data.
250 while ((n
= nfrontp
- nbackp
) > 0) {
252 /* XXX This causes output_data() to recurse and die */
254 n
+= output_data("td: netflush %d chars\r\n", n
);
258 if (encrypt_output
) {
259 char *s
= nclearto
? nclearto
: nbackp
;
260 if (nfrontp
- s
> 0) {
261 (*encrypt_output
)((unsigned char *)s
, nfrontp
-s
);
265 #endif /* ENCRYPTION */
267 * if no urgent data, or if the other side appears to be an
268 * old 4.2 client (and thus unable to survive TCP urgent data),
269 * write the entire buffer in non-OOB mode.
271 if ((neturg
== 0) || (not42
== 0)) {
272 n
= write(net
, nbackp
, n
); /* normal write */
276 * In 4.2 (and 4.3) systems, there is some question about
277 * what byte in a sendOOB operation is the "OOB" data.
278 * To make ourselves compatible, we only send ONE byte
279 * out of band, the one WE THINK should be OOB (though
280 * we really have more the TCP philosophy of urgent data
281 * rather than the Unix philosophy of OOB data).
284 n
= send(net
, nbackp
, n
-1, 0); /* send URGENT all by itself */
286 n
= send(net
, nbackp
, n
, MSG_OOB
); /* URGENT data */
290 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
297 if (nbackp
> nclearto
)
299 #endif /* ENCRYPTION */
300 if (nbackp
>= neturg
) {
303 if (nbackp
== nfrontp
) {
304 nbackp
= nfrontp
= netobuf
;
307 #endif /* ENCRYPTION */
311 } /* end of netflush */
315 * miscellaneous functions doing a variety of little jobs follow ...
320 fatalmsg(int f
, const char *msg
)
324 (void) snprintf(buf
, sizeof(buf
), "telnetd: %s.\r\n", msg
);
326 if (encrypt_output
) {
328 * Better turn off encryption first....
334 #endif /* ENCRYPTION */
335 (void) write(f
, buf
, (int)strlen(buf
));
341 fatalperror(int f
, const char *msg
)
345 (void) snprintf(buf
, sizeof(buf
), "%s: %s", msg
, strerror(errno
));
352 edithost(char *pat
, char *host
)
354 char *res
= editedhost
;
375 if (res
== &editedhost
[sizeof editedhost
- 1]) {
382 (void) strncpy(res
, host
,
383 sizeof editedhost
- (res
- editedhost
) -1);
386 editedhost
[sizeof editedhost
- 1] = '\0';
389 static char *putlocation
;
392 putstr(const char *s
)
405 #if defined(__FreeBSD__) || defined(__DragonFly__)
406 static char fmtstr
[] = { "%+" };
408 static char fmtstr
[] = { "%l:%M%P on %A, %d %B %Y" };
412 putf(char *cp
, char *where
)
417 #if defined(__FreeBSD__) || defined(__DragonFly__)
418 static struct utsname kerninfo
;
420 if (!*kerninfo
.sysname
)
431 } else if (*cp
!= '%') {
439 /* names are like /dev/pts/2 -- we want pts/2 */
440 slash
= strchr(line
+1, '/');
442 slash
= strrchr(line
, '/');
444 if (slash
== (char *) 0)
455 #if defined(__FreeBSD__) || defined(__DragonFly__)
456 setlocale(LC_TIME
, "");
459 (void)strftime(db
, sizeof(db
), fmtstr
, localtime(&t
));
463 #if defined(__FreeBSD__) || defined(__DragonFly__)
465 putstr(kerninfo
.sysname
);
469 putstr(kerninfo
.machine
);
473 putstr(kerninfo
.release
);
477 putstr(kerninfo
.version
);
491 * Print telnet options and commands in plain text, if possible.
494 printoption(const char *fmt
, int option
)
496 if (TELOPT_OK(option
))
497 output_data("%s %s\r\n", fmt
, TELOPT(option
));
498 else if (TELCMD_OK(option
))
499 output_data("%s %s\r\n", fmt
, TELCMD(option
));
501 output_data("%s %d\r\n", fmt
, option
);
506 printsub(char direction
, unsigned char *pointer
, int length
)
510 if (!(diagnostic
& TD_OPTIONS
))
514 output_data("td: %s suboption ",
515 direction
== '<' ? "recv" : "send");
519 i
= pointer
[length
-2];
520 j
= pointer
[length
-1];
522 if (i
!= IAC
|| j
!= SE
) {
523 output_data("(terminated by ");
525 output_data("%s ", TELOPT(i
));
526 else if (TELCMD_OK(i
))
527 output_data("%s ", TELCMD(i
));
529 output_data("%d ", i
);
531 output_data("%s", TELOPT(j
));
532 else if (TELCMD_OK(j
))
533 output_data("%s", TELCMD(j
));
535 output_data("%d", j
);
536 output_data(", not IAC SE!) ");
542 output_data("(Empty suboption??\?)");
545 switch (pointer
[0]) {
547 output_data("TERMINAL-TYPE ");
548 switch (pointer
[1]) {
550 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
557 "- unknown qualifier %d (0x%x).",
558 pointer
[1], pointer
[1]);
562 output_data("TERMINAL-SPEED");
564 output_data(" (empty suboption??\?)");
567 switch (pointer
[1]) {
569 output_data(" IS %.*s", length
-2, (char *)pointer
+2);
573 output_data(" SEND");
575 output_data(" %d (unknown)", pointer
[1]);
576 for (i
= 2; i
< length
; i
++) {
577 output_data(" ?%d?", pointer
[i
]);
584 output_data("TOGGLE-FLOW-CONTROL");
586 output_data(" (empty suboption??\?)");
589 switch (pointer
[1]) {
591 output_data(" OFF"); break;
593 output_data(" ON"); break;
594 case LFLOW_RESTART_ANY
:
595 output_data(" RESTART-ANY"); break;
596 case LFLOW_RESTART_XON
:
597 output_data(" RESTART-XON"); break;
599 output_data(" %d (unknown)", pointer
[1]);
601 for (i
= 2; i
< length
; i
++) {
602 output_data(" ?%d?", pointer
[i
]);
609 output_data(" (empty suboption??\?)");
613 output_data(" ?%d?", pointer
[1]);
616 output_data(" %d %d (%d)",
617 pointer
[1], pointer
[2],
618 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
620 output_data(" ?%d?", pointer
[3]);
623 output_data(" %d %d (%d)",
624 pointer
[3], pointer
[4],
625 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
626 for (i
= 5; i
< length
; i
++) {
627 output_data(" ?%d?", pointer
[i
]);
631 case TELOPT_LINEMODE
:
632 output_data("LINEMODE ");
634 output_data(" (empty suboption??\?)");
637 switch (pointer
[1]) {
639 output_data("WILL ");
642 output_data("WONT ");
648 output_data("DONT ");
651 output_data("(no option??\?)");
654 switch (pointer
[2]) {
656 output_data("Forward Mask");
657 for (i
= 3; i
< length
; i
++) {
658 output_data(" %x", pointer
[i
]);
662 output_data("%d (unknown)", pointer
[2]);
663 for (i
= 3; i
< length
; i
++) {
664 output_data(" %d", pointer
[i
]);
672 for (i
= 2; i
< length
- 2; i
+= 3) {
673 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
674 output_data(" %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
676 output_data(" %d", pointer
[i
+SLC_FUNC
]);
677 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
679 output_data(" NOSUPPORT"); break;
681 output_data(" CANTCHANGE"); break;
683 output_data(" VARIABLE"); break;
685 output_data(" DEFAULT"); break;
687 output_data("%s%s%s",
688 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
689 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
690 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
691 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
692 SLC_FLUSHOUT
| SLC_LEVELBITS
)) {
693 output_data("(0x%x)", pointer
[i
+SLC_FLAGS
]);
695 output_data(" %d;", pointer
[i
+SLC_VALUE
]);
696 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
697 (pointer
[i
+SLC_VALUE
+1] == IAC
))
700 for (; i
< length
; i
++) {
701 output_data(" ?%d?", pointer
[i
]);
706 output_data("MODE ");
708 output_data("(no mode??\?)");
713 sprintf(tbuf
, "%s%s%s%s%s",
714 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
715 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
716 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
717 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
718 pointer
[2]&MODE_ACK
? "|ACK" : "");
719 output_data("%s", tbuf
[1] ? &tbuf
[1] : "0");
721 if (pointer
[2]&~(MODE_EDIT
|MODE_TRAPSIG
|MODE_ACK
)) {
722 output_data(" (0x%x)", pointer
[2]);
724 for (i
= 3; i
< length
; i
++) {
725 output_data(" ?0x%x?", pointer
[i
]);
729 output_data("%d (unknown)", pointer
[1]);
730 for (i
= 2; i
< length
; i
++) {
731 output_data(" %d", pointer
[i
]);
736 case TELOPT_STATUS
: {
740 output_data("STATUS");
742 switch (pointer
[1]) {
744 if (pointer
[1] == TELQUAL_SEND
)
745 output_data(" SEND");
747 output_data(" %d (unknown)", pointer
[1]);
748 for (i
= 2; i
< length
; i
++) {
749 output_data(" ?%d?", pointer
[i
]);
753 output_data(" IS\r\n");
755 for (i
= 2; i
< length
; i
++) {
757 case DO
: cp
= "DO"; goto common2
;
758 case DONT
: cp
= "DONT"; goto common2
;
759 case WILL
: cp
= "WILL"; goto common2
;
760 case WONT
: cp
= "WONT"; goto common2
;
763 if (TELOPT_OK(pointer
[i
]))
764 output_data(" %s %s", cp
, TELOPT(pointer
[i
]));
766 output_data(" %s %d", cp
, pointer
[i
]);
776 if (pointer
[j
] == SE
) {
779 if (pointer
[j
+1] == SE
)
784 pointer
[k
++] = pointer
[j
++];
786 printsub(0, &pointer
[i
], k
- i
);
798 output_data(" %d", pointer
[i
]);
807 case TELOPT_XDISPLOC
:
808 output_data("X-DISPLAY-LOCATION ");
809 switch (pointer
[1]) {
811 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
817 output_data("- unknown qualifier %d (0x%x).",
818 pointer
[1], pointer
[1]);
822 case TELOPT_NEW_ENVIRON
:
823 output_data("NEW-ENVIRON ");
825 case TELOPT_OLD_ENVIRON
:
826 output_data("OLD-ENVIRON");
828 switch (pointer
[1]) {
833 output_data("SEND ");
836 output_data("INFO ");
840 for (i
= 2; i
< length
; i
++ ) {
841 switch (pointer
[i
]) {
843 output_data("%s", "\" VAR " + noquote
);
848 output_data("%s", "\" VALUE " + noquote
);
853 output_data("%s", "\" ESC " + noquote
);
858 output_data("%s", "\" USERVAR " + noquote
);
863 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
868 output_data("%c", pointer
[i
]);
870 output_data("\" %03o " + noquote
,
884 #ifdef AUTHENTICATION
885 case TELOPT_AUTHENTICATION
:
886 output_data("AUTHENTICATION");
889 output_data(" (empty suboption??\?)");
892 switch (pointer
[1]) {
895 output_data(" %s ", (pointer
[1] == TELQUAL_IS
) ?
897 if (AUTHTYPE_NAME_OK(pointer
[2]))
898 output_data("%s ", AUTHTYPE_NAME(pointer
[2]));
900 output_data("%d ", pointer
[2]);
902 output_data("(partial suboption??\?)");
906 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
908 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
909 "MUTUAL" : "ONE-WAY");
913 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
914 output_data("%s", buf
);
920 output_data(" SEND ");
922 if (AUTHTYPE_NAME_OK(pointer
[i
]))
923 output_data("%s ", AUTHTYPE_NAME(pointer
[i
]));
925 output_data("%d ", pointer
[i
]);
927 output_data("(partial suboption??\?)");
930 output_data("%s|%s ",
931 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
933 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
934 "MUTUAL" : "ONE-WAY");
940 output_data(" NAME \"%.*s\"", length
- 2, pointer
+ 2);
944 for (i
= 2; i
< length
; i
++) {
945 output_data(" ?%d?", pointer
[i
]);
954 output_data("ENCRYPT");
956 output_data(" (empty suboption??\?)");
959 switch (pointer
[1]) {
961 output_data(" START");
968 case ENCRYPT_REQSTART
:
969 output_data(" REQUEST-START");
973 output_data(" REQUEST-END");
978 output_data(" %s ", (pointer
[1] == ENCRYPT_IS
) ?
981 output_data(" (partial suboption??\?)");
984 if (ENCTYPE_NAME_OK(pointer
[2]))
985 output_data("%s ", ENCTYPE_NAME(pointer
[2]));
987 output_data(" %d (unknown)", pointer
[2]);
991 encrypt_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
992 output_data("%s", buf
);
996 case ENCRYPT_SUPPORT
:
998 output_data(" SUPPORT ");
1000 if (ENCTYPE_NAME_OK(pointer
[i
]))
1001 output_data("%s ", ENCTYPE_NAME(pointer
[i
]));
1003 output_data("%d ", pointer
[i
]);
1008 case ENCRYPT_ENC_KEYID
:
1009 output_data(" ENC_KEYID");
1012 case ENCRYPT_DEC_KEYID
:
1013 output_data(" DEC_KEYID");
1017 output_data(" %d (unknown)", pointer
[1]);
1019 for (i
= 2; i
< length
; i
++) {
1020 output_data(" %d", pointer
[i
]);
1025 #endif /* ENCRYPTION */
1028 if (TELOPT_OK(pointer
[0]))
1029 output_data("%s (unknown)", TELOPT(pointer
[0]));
1031 output_data("%d (unknown)", pointer
[i
]);
1032 for (i
= 1; i
< length
; i
++) {
1033 output_data(" %d", pointer
[i
]);
1037 output_data("\r\n");
1041 * Dump a data buffer in hex and ascii to the output data stream.
1044 printdata(const char *tag
, char *ptr
, int cnt
)
1050 /* flush net output buffer if no room for new data) */
1051 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 80) {
1055 /* add a line of output */
1056 output_data("%s: ", tag
);
1057 for (i
= 0; i
< 20 && cnt
; i
++) {
1058 output_data("%02x", *ptr
);
1059 if (isprint(*ptr
)) {
1071 output_data(" %s\r\n", xbuf
);
1074 #endif /* DIAGNOSTICS */