]>
git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/utility.c
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
36 static char sccsid
[] = "@(#)utility.c 8.2 (Berkeley) 12/15/93";
38 static const char rcsid
[] =
39 "$FreeBSD: src/libexec/telnetd/utility.c,v 1.15 2001/07/20 15:14:03 ru Exp $";
44 #include <sys/utsname.h>
50 * utility functions performing io related tasks
56 * A small subroutine to flush the network output buffer, get some data
57 * from the network, and pass it through the telnet state machine. We
58 * also flush the pty input buffer (by dropping its data) if it becomes
66 DIAG(TD_REPORT
, output_data("td: ttloop\r\n"));
67 if (nfrontp
- nbackp
> 0) {
70 ncc
= read(net
, netibuf
, sizeof netibuf
);
72 syslog(LOG_INFO
, "ttloop: read: %m");
74 } else if (ncc
== 0) {
75 syslog(LOG_INFO
, "ttloop: peer died: %m");
78 DIAG(TD_REPORT
, output_data("td: ttloop read %d chars\r\n", ncc
));
80 telrcv(); /* state machine */
82 pfrontp
= pbackp
= ptyobuf
;
88 * Check a descriptor to see if out of band data exists on it.
92 int s
; /* socket number */
94 static struct timeval timeout
= { 0 };
101 memset((char *)&timeout
, 0, sizeof timeout
);
102 value
= select(s
+1, (fd_set
*)0, (fd_set
*)0, &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")
150 if ((*current
&0xff) != IAC
) {
153 switch (*(current
+1)&0xff) {
159 case SB
: /* loop forever looking for the SE */
161 register char *look
= current
+2;
164 if ((*look
++&0xff) == IAC
) {
165 if ((*look
++&0xff) == SE
) {
174 } /* end of nextitem */
180 * We are about to do a TELNET SYNCH operation. Clear
181 * the path to the network.
183 * Things are a bit tricky since we may have sent the first
184 * byte or so of a previous TELNET command into the network.
185 * So, we have to scan the network buffer from the beginning
186 * until we are up to where we want to be.
188 * A side effect of what we do, just to keep things
189 * simple, is to clear the urgent data pointer. The principal
190 * caller should be setting the urgent data pointer AFTER calling
196 register char *thisitem
, *next
;
198 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
199 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
203 while ((next
= nextitem(thisitem
)) <= nbackp
) {
207 /* Now, thisitem is first before/at boundary. */
209 good
= netobuf
; /* where the good bytes go */
211 while (nfrontp
> thisitem
) {
212 if (wewant(thisitem
)) {
217 next
= nextitem(next
);
218 } while (wewant(next
) && (nfrontp
> next
));
219 length
= next
-thisitem
;
220 bcopy(thisitem
, good
, length
);
224 thisitem
= nextitem(thisitem
);
229 nfrontp
= good
; /* next byte to be sent */
231 } /* end of netclear */
235 * Send as much data as possible to the network,
236 * handling requests for urgent data.
244 while ((n
= nfrontp
- nbackp
) > 0) {
246 /* XXX This causes output_data() to recurse and die */
249 n
+= output_data("td: netflush %d chars\r\n", n
);
253 * if no urgent data, or if the other side appears to be an
254 * old 4.2 client (and thus unable to survive TCP urgent data),
255 * write the entire buffer in non-OOB mode.
257 if ((neturg
== 0) || (not42
== 0)) {
258 n
= write(net
, nbackp
, n
); /* normal write */
262 * In 4.2 (and 4.3) systems, there is some question about
263 * what byte in a sendOOB operation is the "OOB" data.
264 * To make ourselves compatible, we only send ONE byte
265 * out of band, the one WE THINK should be OOB (though
266 * we really have more the TCP philosophy of urgent data
267 * rather than the Unix philosophy of OOB data).
270 n
= send(net
, nbackp
, n
-1, 0); /* send URGENT all by itself */
272 n
= send(net
, nbackp
, n
, MSG_OOB
); /* URGENT data */
276 if (errno
== EWOULDBLOCK
|| errno
== EINTR
)
282 if (nbackp
>= neturg
) {
285 if (nbackp
== nfrontp
) {
286 nbackp
= nfrontp
= netobuf
;
290 } /* end of netflush */
294 * miscellaneous functions doing a variety of little jobs follow ...
305 (void) snprintf(buf
, sizeof(buf
), "telnetd: %s.\r\n", msg
);
306 (void) write(f
, buf
, (int)strlen(buf
));
316 char buf
[BUFSIZ
], *strerror();
318 (void) snprintf(buf
, sizeof(buf
), "%s: %s", msg
, strerror(errno
));
329 register char *res
= editedhost
;
350 if (res
== &editedhost
[sizeof editedhost
- 1]) {
357 (void) strncpy(res
, host
,
358 sizeof editedhost
- (res
- editedhost
) -1);
361 editedhost
[sizeof editedhost
- 1] = '\0';
364 static char *putlocation
;
383 static char fmtstr
[] = { "%+" };
386 * This is split on two lines so that SCCS will not see the M
387 * between two % signs and expand it...
389 static char fmtstr
[] = { "%l:%M\
390 %P on %A, %d %B %Y" };
402 extern char *index();
404 extern char *rindex();
407 static struct utsname kerninfo
;
409 if (!*kerninfo
.sysname
)
420 } else if (*cp
!= '%') {
428 /* names are like /dev/pts/2 -- we want pts/2 */
429 slash
= index(line
+1, '/');
431 slash
= rindex(line
, '/');
433 if (slash
== (char *) 0)
445 setlocale(LC_TIME
, "");
448 (void)strftime(db
, sizeof(db
), fmtstr
, localtime(&t
));
454 putstr(kerninfo
.sysname
);
458 putstr(kerninfo
.machine
);
462 putstr(kerninfo
.release
);
466 putstr(kerninfo
.version
);
480 * Print telnet options and commands in plain text, if possible.
483 printoption(fmt
, option
)
487 if (TELOPT_OK(option
))
488 output_data("%s %s\r\n", fmt
, TELOPT(option
));
489 else if (TELCMD_OK(option
))
490 output_data("%s %s\r\n", fmt
, TELCMD(option
));
492 output_data("%s %d\r\n", fmt
, option
);
497 printsub(direction
, pointer
, length
)
498 char direction
; /* '<' or '>' */
499 unsigned char *pointer
; /* where suboption data sits */
500 int length
; /* length of suboption data */
504 if (!(diagnostic
& TD_OPTIONS
))
508 output_data("td: %s suboption ",
509 direction
== '<' ? "recv" : "send");
513 i
= pointer
[length
-2];
514 j
= pointer
[length
-1];
516 if (i
!= IAC
|| j
!= SE
) {
517 output_data("(terminated by ");
519 output_data("%s ", TELOPT(i
));
520 else if (TELCMD_OK(i
))
521 output_data("%s ", TELCMD(i
));
523 output_data("%d ", i
);
525 output_data("%s", TELOPT(j
));
526 else if (TELCMD_OK(j
))
527 output_data("%s", TELCMD(j
));
529 output_data("%d", j
);
530 output_data(", not IAC SE!) ");
536 output_data("(Empty suboption??\?)");
539 switch (pointer
[0]) {
541 output_data("TERMINAL-TYPE ");
542 switch (pointer
[1]) {
544 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
551 "- unknown qualifier %d (0x%x).",
552 pointer
[1], pointer
[1]);
556 output_data("TERMINAL-SPEED");
558 output_data(" (empty suboption??\?)");
561 switch (pointer
[1]) {
563 output_data(" IS %.*s", length
-2, (char *)pointer
+2);
567 output_data(" SEND");
569 output_data(" %d (unknown)", pointer
[1]);
570 for (i
= 2; i
< length
; i
++) {
571 output_data(" ?%d?", pointer
[i
]);
578 output_data("TOGGLE-FLOW-CONTROL");
580 output_data(" (empty suboption??\?)");
583 switch (pointer
[1]) {
585 output_data(" OFF"); break;
587 output_data(" ON"); break;
588 case LFLOW_RESTART_ANY
:
589 output_data(" RESTART-ANY"); break;
590 case LFLOW_RESTART_XON
:
591 output_data(" RESTART-XON"); break;
593 output_data(" %d (unknown)", pointer
[1]);
595 for (i
= 2; i
< length
; i
++) {
596 output_data(" ?%d?", pointer
[i
]);
603 output_data(" (empty suboption??\?)");
607 output_data(" ?%d?", pointer
[1]);
610 output_data(" %d %d (%d)",
611 pointer
[1], pointer
[2],
612 (int)((((unsigned int)pointer
[1])<<8)|((unsigned int)pointer
[2])));
614 output_data(" ?%d?", pointer
[3]);
617 output_data(" %d %d (%d)",
618 pointer
[3], pointer
[4],
619 (int)((((unsigned int)pointer
[3])<<8)|((unsigned int)pointer
[4])));
620 for (i
= 5; i
< length
; i
++) {
621 output_data(" ?%d?", pointer
[i
]);
625 case TELOPT_LINEMODE
:
626 output_data("LINEMODE ");
628 output_data(" (empty suboption??\?)");
631 switch (pointer
[1]) {
633 output_data("WILL ");
636 output_data("WONT ");
642 output_data("DONT ");
645 output_data("(no option??\?)");
648 switch (pointer
[2]) {
650 output_data("Forward Mask");
651 for (i
= 3; i
< length
; i
++) {
652 output_data(" %x", pointer
[i
]);
656 output_data("%d (unknown)", pointer
[2]);
657 for (i
= 3; i
< length
; i
++) {
658 output_data(" %d", pointer
[i
]);
666 for (i
= 2; i
< length
- 2; i
+= 3) {
667 if (SLC_NAME_OK(pointer
[i
+SLC_FUNC
]))
668 output_data(" %s", SLC_NAME(pointer
[i
+SLC_FUNC
]));
670 output_data(" %d", pointer
[i
+SLC_FUNC
]);
671 switch (pointer
[i
+SLC_FLAGS
]&SLC_LEVELBITS
) {
673 output_data(" NOSUPPORT"); break;
675 output_data(" CANTCHANGE"); break;
677 output_data(" VARIABLE"); break;
679 output_data(" DEFAULT"); break;
681 output_data("%s%s%s",
682 pointer
[i
+SLC_FLAGS
]&SLC_ACK
? "|ACK" : "",
683 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHIN
? "|FLUSHIN" : "",
684 pointer
[i
+SLC_FLAGS
]&SLC_FLUSHOUT
? "|FLUSHOUT" : "");
685 if (pointer
[i
+SLC_FLAGS
]& ~(SLC_ACK
|SLC_FLUSHIN
|
686 SLC_FLUSHOUT
| SLC_LEVELBITS
)) {
687 output_data("(0x%x)", pointer
[i
+SLC_FLAGS
]);
689 output_data(" %d;", pointer
[i
+SLC_VALUE
]);
690 if ((pointer
[i
+SLC_VALUE
] == IAC
) &&
691 (pointer
[i
+SLC_VALUE
+1] == IAC
))
694 for (; i
< length
; i
++) {
695 output_data(" ?%d?", pointer
[i
]);
700 output_data("MODE ");
702 output_data("(no mode??\?)");
707 sprintf(tbuf
, "%s%s%s%s%s",
708 pointer
[2]&MODE_EDIT
? "|EDIT" : "",
709 pointer
[2]&MODE_TRAPSIG
? "|TRAPSIG" : "",
710 pointer
[2]&MODE_SOFT_TAB
? "|SOFT_TAB" : "",
711 pointer
[2]&MODE_LIT_ECHO
? "|LIT_ECHO" : "",
712 pointer
[2]&MODE_ACK
? "|ACK" : "");
713 output_data("%s", tbuf
[1] ? &tbuf
[1] : "0");
715 if (pointer
[2]&~(MODE_EDIT
|MODE_TRAPSIG
|MODE_ACK
)) {
716 output_data(" (0x%x)", pointer
[2]);
718 for (i
= 3; i
< length
; i
++) {
719 output_data(" ?0x%x?", pointer
[i
]);
723 output_data("%d (unknown)", pointer
[1]);
724 for (i
= 2; i
< length
; i
++) {
725 output_data(" %d", pointer
[i
]);
730 case TELOPT_STATUS
: {
734 output_data("STATUS");
736 switch (pointer
[1]) {
738 if (pointer
[1] == TELQUAL_SEND
)
739 output_data(" SEND");
741 output_data(" %d (unknown)", pointer
[1]);
742 for (i
= 2; i
< length
; i
++) {
743 output_data(" ?%d?", pointer
[i
]);
747 output_data(" IS\r\n");
749 for (i
= 2; i
< length
; i
++) {
751 case DO
: cp
= "DO"; goto common2
;
752 case DONT
: cp
= "DONT"; goto common2
;
753 case WILL
: cp
= "WILL"; goto common2
;
754 case WONT
: cp
= "WONT"; goto common2
;
757 if (TELOPT_OK(pointer
[i
]))
758 output_data(" %s %s", cp
, TELOPT(pointer
[i
]));
760 output_data(" %s %d", cp
, pointer
[i
]);
770 if (pointer
[j
] == SE
) {
773 if (pointer
[j
+1] == SE
)
778 pointer
[k
++] = pointer
[j
++];
780 printsub(0, &pointer
[i
], k
- i
);
792 output_data(" %d", pointer
[i
]);
801 case TELOPT_XDISPLOC
:
802 output_data("X-DISPLAY-LOCATION ");
803 switch (pointer
[1]) {
805 output_data("IS \"%.*s\"", length
-2, (char *)pointer
+2);
811 output_data("- unknown qualifier %d (0x%x).",
812 pointer
[1], pointer
[1]);
816 case TELOPT_NEW_ENVIRON
:
817 output_data("NEW-ENVIRON ");
819 case TELOPT_OLD_ENVIRON
:
820 output_data("OLD-ENVIRON");
822 switch (pointer
[1]) {
827 output_data("SEND ");
830 output_data("INFO ");
833 register int noquote
= 2;
834 for (i
= 2; i
< length
; i
++ ) {
835 switch (pointer
[i
]) {
837 output_data("\" VAR " + noquote
);
842 output_data("\" VALUE " + noquote
);
847 output_data("\" ESC " + noquote
);
852 output_data("\" USERVAR " + noquote
);
857 if (isprint(pointer
[i
]) && pointer
[i
] != '"') {
862 output_data("%c", pointer
[i
]);
864 output_data("\" %03o " + noquote
,
878 #if defined(AUTHENTICATION)
879 case TELOPT_AUTHENTICATION
:
880 output_data("AUTHENTICATION");
883 output_data(" (empty suboption??\?)");
886 switch (pointer
[1]) {
889 output_data(" %s ", (pointer
[1] == TELQUAL_IS
) ?
891 if (AUTHTYPE_NAME_OK(pointer
[2]))
892 output_data("%s ", AUTHTYPE_NAME(pointer
[2]));
894 output_data("%d ", pointer
[2]);
896 output_data("(partial suboption??\?)");
900 ((pointer
[3] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
902 ((pointer
[3] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
903 "MUTUAL" : "ONE-WAY");
907 auth_printsub(&pointer
[1], length
- 1, buf
, sizeof(buf
));
908 output_data("%s", buf
);
914 output_data(" SEND ");
916 if (AUTHTYPE_NAME_OK(pointer
[i
]))
917 output_data("%s ", AUTHTYPE_NAME(pointer
[i
]));
919 output_data("%d ", pointer
[i
]);
921 output_data("(partial suboption??\?)");
924 output_data("%s|%s ",
925 ((pointer
[i
] & AUTH_WHO_MASK
) == AUTH_WHO_CLIENT
) ?
927 ((pointer
[i
] & AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) ?
928 "MUTUAL" : "ONE-WAY");
934 output_data(" NAME \"%.*s\"", length
- 2, pointer
+ 2);
938 for (i
= 2; i
< length
; i
++) {
939 output_data(" ?%d?", pointer
[i
]);
948 if (TELOPT_OK(pointer
[0]))
949 output_data("%s (unknown)", TELOPT(pointer
[0]));
951 output_data("%d (unknown)", pointer
[i
]);
952 for (i
= 1; i
< length
; i
++) {
953 output_data(" %d", pointer
[i
]);
961 * Dump a data buffer in hex and ascii to the output data stream.
964 printdata(tag
, ptr
, cnt
)
973 /* flush net output buffer if no room for new data) */
974 if ((&netobuf
[BUFSIZ
] - nfrontp
) < 80) {
978 /* add a line of output */
979 output_data("%s: ", tag
);
980 for (i
= 0; i
< 20 && cnt
; i
++) {
981 output_data("%02x", *ptr
);
994 output_data(" %s\r\n", xbuf
);
997 #endif /* DIAGNOSTICS */