1 /* $NetBSD: utility.c,v 1.30 2006/10/07 18:26:40 elad Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)utility.c 8.4 (Berkeley) 5/30/95";
37 __RCSID("$NetBSD: utility.c,v 1.30 2006/10/07 18:26:40 elad Exp $");
41 #include <sys/utsname.h>
46 char *nextitem(char *);
52 * utility functions performing io related tasks
58 * A small subroutine to flush the network output buffer, get some data
59 * from the network, and pass it through the telnet state machine. We
60 * also flush the pty input buffer (by dropping its data) if it becomes
68 DIAG(TD_REPORT
, {output_data("td: ttloop\r\n");});
69 if (nfrontp
- nbackp
) {
72 ncc
= read(net
, netibuf
, sizeof netibuf
);
74 syslog(LOG_ERR
, "ttloop: read: %m");
76 } else if (ncc
== 0) {
77 syslog(LOG_INFO
, "ttloop: unexpected EOF from peer");
80 DIAG(TD_REPORT
, {output_data("td: ttloop read %d chars\r\n", ncc
);});
82 telrcv(); /* state machine */
84 pfrontp
= pbackp
= ptyobuf
;
90 * Check a descriptor to see if out of band data exists on it.
93 stilloob(int s
/* socket number */)
99 set
[0].events
= POLLPRI
;
101 value
= poll(set
, 1, 0);
102 } while ((value
== -1) && (errno
== EINTR
));
105 fatalperror(pty
, "poll");
107 if (set
[0].revents
& POLLPRI
) {
119 if ((n
= pfrontp
- pbackp
) > 0) {
120 DIAG((TD_REPORT
| TD_PTYDATA
),
121 { output_data("td: ptyflush %d chars\r\n", n
); });
122 DIAG(TD_PTYDATA
, printdata("pd", pbackp
, n
));
123 n
= write(pty
, pbackp
, n
);
126 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
131 if (pbackp
== pfrontp
)
132 pbackp
= pfrontp
= ptyobuf
;
138 * Return the address of the next "item" in the TELNET data
139 * stream. This will be the address of the next character if
140 * the current address is a user data character, or it will
141 * be the address of the character following the TELNET command
142 * if the current address is a TELNET IAC ("I Am a Command")
146 nextitem(char *current
)
148 if ((*current
&0xff) != IAC
) {
151 switch (*(current
+1)&0xff) {
157 case SB
: /* loop forever looking for the SE */
159 char *look
= current
+2;
162 if ((*look
++&0xff) == IAC
) {
163 if ((*look
++&0xff) == SE
) {
172 } /* 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.
249 if ((n
= nfrontp
- nbackp
) > 0) {
251 { output_data("td: netflush %d chars\r\n", n
);
252 n
= nfrontp
- nbackp
; /* re-compute count */
255 if (encrypt_output
) {
256 char *s
= nclearto
? nclearto
: nbackp
;
257 if (nfrontp
- s
> 0) {
258 (*encrypt_output
)((unsigned char *)s
, nfrontp
- s
);
262 #endif /* ENCRYPTION */
264 * if no urgent data, or if the other side appears to be an
265 * old 4.2 client (and thus unable to survive TCP urgent data),
266 * write the entire buffer in non-OOB mode.
268 if ((neturg
== 0) || (not42
== 0)) {
269 n
= write(net
, nbackp
, n
); /* normal write */
273 * In 4.2 (and 4.3) systems, there is some question about
274 * what byte in a sendOOB operation is the "OOB" data.
275 * To make ourselves compatible, we only send ONE byte
276 * out of band, the one WE THINK should be OOB (though
277 * we really have more the TCP philosophy of urgent data
278 * rather than the Unix philosophy of OOB data).
281 n
= send(net
, nbackp
, n
-1, 0); /* send URGENT all by itself */
283 n
= send(net
, nbackp
, n
, MSG_OOB
); /* URGENT data */
288 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
294 if (nbackp
> nclearto
)
296 #endif /* ENCRYPTION */
297 if (nbackp
>= neturg
) {
300 if (nbackp
== nfrontp
) {
301 nbackp
= nfrontp
= netobuf
;
304 #endif /* ENCRYPTION */
307 } /* end of netflush */
313 * Just a handy little function to write a bit of raw data to the net.
314 * It will force a transmit of the buffer if necessary
317 * ptr - A pointer to a character string to write
318 * len - How many bytes to write
321 writenet(unsigned char *ptr
, int len
)
323 /* flush buffer if no room for new data) */
324 if ((&netobuf
[BUFSIZ
] - nfrontp
) < len
) {
325 /* if this fails, don't worry, buffer is a little big */
329 memmove(nfrontp
, ptr
, len
);
332 } /* end of writenet */
336 * miscellaneous functions doing a variety of little jobs follow ...
339 fatal(int f
, const char *msg
)
343 (void)snprintf(buf
, sizeof buf
, "telnetd: %s.\r\n", msg
);
345 if (encrypt_output
) {
347 * Better turn off encryption first....
353 #endif /* ENCRYPTION */
354 (void)write(f
, buf
, (int)strlen(buf
));
366 (void)snprintf(buf
, sizeof buf
, "%s: %s", msg
, strerror(errno
));
370 char editedhost
[MAXHOSTNAMELEN
];
373 edithost(char *pat
, char *host
)
375 char *res
= editedhost
;
396 if (res
== &editedhost
[sizeof editedhost
- 1]) {
403 (void) strncpy(res
, host
,
404 sizeof editedhost
- (res
- editedhost
) -1);
407 editedhost
[sizeof editedhost
- 1] = '\0';
410 static char *putlocation
;
427 * This is split on two lines so that SCCS will not see the M
428 * between two % signs and expand it...
430 static char fmtstr
[] = { "%l:%M\
431 %p on %A, %d %B %Y" };
434 putf(char *cp
, char *where
)
439 struct utsname utsinfo
;
453 if ((slash
= strstr(line
, "/pts/")) == NULL
)
454 slash
= strrchr(line
, '/');
455 if (slash
== (char *) 0)
467 (void)strftime(db
, sizeof(db
), fmtstr
, localtime(&t
));
476 putstr(utsinfo
.sysname
);
480 putstr(utsinfo
.machine
);
484 putstr(utsinfo
.release
);
488 putstr(utsinfo
.version
);
494 return (putlocation
);
499 * Print telnet options and commands in plain text, if possible.
502 printoption(const char *fmt
, int option
)
504 if (TELOPT_OK(option
))
505 output_data("%s %s\r\n", fmt
, TELOPT(option
));
506 else if (TELCMD_OK(option
))
507 output_data("%s %s\r\n", fmt
, TELCMD(option
));
509 output_data("%s %d\r\n", fmt
, option
);
515 int direction
, /* '<' or '>' */
516 unsigned char *pointer
, /* where suboption data sits */
517 int length
) /* length of suboption data */
520 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
524 if (!(diagnostic
& TD_OPTIONS
))
528 output_data("td: %s suboption ",
529 direction
== '<' ? "recv" : "send");
533 i
= pointer
[length
- 2];
534 j
= pointer
[length
- 1];
536 if (i
!= IAC
|| j
!= SE
) {
537 output_data("(terminated by ");
539 output_data("%s ", TELOPT(i
));
540 else if (TELCMD_OK(i
))
541 output_data("%s ", TELCMD(i
));
543 output_data("%d ", i
);
545 output_data("%s", TELOPT(j
));
546 else if (TELCMD_OK(j
))
547 output_data("%s", TELCMD(j
));
549 output_data("%d", j
);
550 output_data(", not IAC SE!) ");
556 output_data("(Empty suboption??\?)");
559 switch (pointer
[0]) {
561 output_data("TERMINAL-TYPE ");
562 switch (pointer
[1]) {
564 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
570 output_data("- unknown qualifier %d (0x%x).",
571 pointer
[1], pointer
[1]);
575 output_data("TERMINAL-SPEED");
577 output_data(" (empty suboption??\?)");
580 switch (pointer
[1]) {
582 output_data(" IS %.*s", length
-2, (char *)pointer
+2);
586 output_data(" SEND");
588 output_data(" %d (unknown)", pointer
[1]);
589 for (i
= 2; i
< length
; i
++) {
590 output_data(" ?%d?", pointer
[i
]);
597 output_data("TOGGLE-FLOW-CONTROL");
599 output_data(" (empty suboption??\?)");
602 switch (pointer
[1]) {
604 output_data(" OFF"); break;
606 output_data(" ON"); break;
607 case LFLOW_RESTART_ANY
:
608 output_data(" RESTART-ANY"); break;
609 case LFLOW_RESTART_XON
:
610 output_data(" RESTART-XON"); break;
612 output_data(" %d (unknown)", pointer
[1]);
614 for (i
= 2; i
< length
; i
++)
615 output_data(" ?%d?", pointer
[i
]);
621 output_data(" (empty suboption??\?)");
625 output_data(" ?%d?", pointer
[1]);
628 output_data(" %d %d (%d)",
629 pointer
[1], pointer
[2],
630 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
632 output_data(" ?%d?", pointer
[3]);
635 output_data(" %d %d (%d)", pointer
[3], pointer
[4],
636 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
637 for (i
= 5; i
< length
; i
++) {
638 output_data(" ?%d?", pointer
[i
]);
642 case TELOPT_LINEMODE
:
643 output_data("LINEMODE ");
645 output_data(" (empty suboption??\?)");
648 switch (pointer
[1]) {
650 output_data("WILL ");
653 output_data("WONT ");
659 output_data("DONT ");
662 output_data("(no option??\?)");
665 switch (pointer
[2]) {
667 output_data("Forward Mask");
668 for (i
= 3; i
< length
; i
++)
669 output_data(" %x", pointer
[i
]);
672 output_data("%d (unknown)", pointer
[2]);
673 for (i
= 3; i
< length
; i
++)
674 output_data(" %d", pointer
[i
]);
681 for (i
= 2; i
< length
- 2; i
+= 3) {
682 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
683 output_data(" %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
685 output_data(" %d", pointer
[i
+SLC_FUNC
]);
686 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
688 output_data(" NOSUPPORT"); break;
690 output_data(" CANTCHANGE"); break;
692 output_data(" VARIABLE"); break;
694 output_data(" DEFAULT"); break;
696 output_data("%s%s%s",
697 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
698 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
699 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
700 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
701 SLC_FLUSHOUT
| SLC_LEVELBITS
)) {
702 output_data("(0x%x)", pointer
[i
+SLC_FLAGS
]);
704 output_data(" %d;", pointer
[i
+SLC_VALUE
]);
705 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
706 (pointer
[i
+SLC_VALUE
+1] == IAC
))
709 for (; i
< length
; i
++)
710 output_data(" ?%d?", pointer
[i
]);
714 output_data("MODE ");
716 output_data("(no mode??\?)");
722 (void)snprintf(tbuf
, sizeof tbuf
, "%s%s%s%s%s",
723 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
724 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
725 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
726 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
727 pointer
[2]&MODE_ACK
? "|ACK" : "");
728 output_data("%s", tbuf
[1] ? &tbuf
[1] : "0");
730 if (pointer
[2]&~(MODE_EDIT
|MODE_TRAPSIG
|MODE_ACK
))
731 output_data(" (0x%x)", pointer
[2]);
732 for (i
= 3; i
< length
; i
++)
733 output_data(" ?0x%x?", pointer
[i
]);
736 output_data("%d (unknown)", pointer
[1]);
737 for (i
= 2; i
< length
; i
++)
738 output_data(" %d", pointer
[i
]);
742 case TELOPT_STATUS
: {
746 output_data("STATUS");
748 switch (pointer
[1]) {
750 if (pointer
[1] == TELQUAL_SEND
)
751 output_data(" SEND");
753 output_data(" %d (unknown)", pointer
[1]);
754 for (i
= 2; i
< length
; i
++)
755 output_data(" ?%d?", pointer
[i
]);
758 output_data(" IS\r\n");
760 for (i
= 2; i
< length
; i
++) {
762 case DO
: cp
= "DO"; goto common2
;
763 case DONT
: cp
= "DONT"; goto common2
;
764 case WILL
: cp
= "WILL"; goto common2
;
765 case WONT
: cp
= "WONT"; goto common2
;
768 if (TELOPT_OK(pointer
[i
]))
769 output_data(" %s %s", cp
, TELOPT(pointer
[i
]));
771 output_data(" %s %d", cp
, pointer
[i
]);
781 if (pointer
[j
] == SE
) {
784 if (pointer
[j
+1] == SE
)
789 pointer
[k
++] = pointer
[j
++];
791 printsub(0, &pointer
[i
], k
- i
);
803 output_data(" %d", pointer
[i
]);
812 case TELOPT_XDISPLOC
:
813 output_data("X-DISPLAY-LOCATION ");
814 switch (pointer
[1]) {
816 output_data("IS \"%.*s\"", length
- 2, (char *)pointer
+ 2);
822 output_data("- unknown qualifier %d (0x%x).",
823 pointer
[1], pointer
[1]);
827 case TELOPT_NEW_ENVIRON
:
828 output_data("NEW-ENVIRON ");
830 case TELOPT_OLD_ENVIRON
:
831 output_data("OLD-ENVIRON");
833 switch (pointer
[1]) {
838 output_data("SEND ");
841 output_data("INFO ");
845 for (i
= 2; i
< length
; i
++ ) {
846 switch (pointer
[i
]) {
848 output_data("%s", "\" VAR " + noquote
);
853 output_data("%s", "\" VALUE " + noquote
);
858 output_data("%s", "\" ESC " + noquote
);
863 output_data("%s", "\" USERVAR " + noquote
);
868 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
873 output_data("%c", pointer
[i
]);
875 output_data("\" %03o " + noquote
, pointer
[i
]);
888 #ifdef AUTHENTICATION
889 case TELOPT_AUTHENTICATION
:
890 output_data("AUTHENTICATION");
893 output_data(" (empty suboption??\?)");
896 switch (pointer
[1]) {
899 output_data(" %s ", (pointer
[1] == TELQUAL_IS
) ?
901 if (AUTHTYPE_NAME_OK(pointer
[2]))
902 output_data("%s ", AUTHTYPE_NAME(pointer
[2]));
904 output_data("%d ", pointer
[2]);
906 output_data("(partial suboption??\?)");
910 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
912 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
913 "MUTUAL" : "ONE-WAY");
915 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
916 output_data("%s", buf
);
921 output_data(" SEND ");
923 if (AUTHTYPE_NAME_OK(pointer
[i
]))
924 output_data("%s ", AUTHTYPE_NAME(pointer
[i
]));
926 output_data("%d ", pointer
[i
]);
928 output_data("(partial suboption??\?)");
931 output_data("%s|%s ",
932 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
934 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
935 "MUTUAL" : "ONE-WAY");
942 output_data(" NAME \"");
944 if (isprint(pointer
[i
]))
945 output_data("%c", pointer
[i
++]);
947 output_data("\"%03o\"",pointer
[i
++]);
953 for (i
= 2; i
< length
; i
++)
954 output_data(" ?%d?", pointer
[i
]);
962 output_data("ENCRYPT");
964 output_data(" (empty suboption??\?)");
967 switch (pointer
[1]) {
969 output_data(" START");
976 case ENCRYPT_REQSTART
:
977 output_data(" REQUEST-START");
981 output_data(" REQUEST-END");
986 output_data(" %s ", (pointer
[1] == ENCRYPT_IS
) ?
989 output_data(" (partial suboption??\?)");
992 if (ENCTYPE_NAME_OK(pointer
[2]))
993 output_data("%s ", ENCTYPE_NAME(pointer
[2]));
995 output_data(" %d (unknown)", pointer
[2]);
997 encrypt_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
998 output_data("%s", buf
);
1001 case ENCRYPT_SUPPORT
:
1003 output_data(" SUPPORT ");
1004 while (i
< length
) {
1005 if (ENCTYPE_NAME_OK(pointer
[i
]))
1006 output_data("%s ", ENCTYPE_NAME(pointer
[i
]));
1008 output_data("%d ", pointer
[i
]);
1013 case ENCRYPT_ENC_KEYID
:
1014 output_data(" ENC_KEYID");
1017 case ENCRYPT_DEC_KEYID
:
1018 output_data(" DEC_KEYID");
1022 output_data(" %d (unknown)", pointer
[1]);
1024 for (i
= 2; i
< length
; i
++)
1025 output_data(" %d", pointer
[i
]);
1029 #endif /* ENCRYPTION */
1032 if (TELOPT_OK(pointer
[0]))
1033 output_data("%s (unknown)", TELOPT(pointer
[0]));
1035 output_data("%d (unknown)", pointer
[i
]);
1036 for (i
= 1; i
< length
; i
++)
1037 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(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((unsigned char)*ptr
)) {
1073 output_data(" %s\r\n", xbuf
);
1076 #endif /* DIAGNOSTICS */