fix
[heimdal.git] / appl / telnet / telnetd / utility.c
blob4bae20554930c2fe1dbc23efabb8bf2028adce24
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
34 #define PRINTOPTIONS
35 #include "telnetd.h"
37 RCSID("$Id$");
40 * utility functions performing io related tasks
44 * ttloop
46 * A small subroutine to flush the network output buffer, get some
47 * data from the network, and pass it through the telnet state
48 * machine. We also flush the pty input buffer (by dropping its data)
49 * if it becomes too full.
52 void
53 ttloop(void)
55 void netflush(void);
57 DIAG(TD_REPORT, {
58 output_data("td: ttloop\r\n");
59 });
60 if (nfrontp-nbackp)
61 netflush();
62 ncc = read(net, netibuf, sizeof netibuf);
63 if (ncc < 0) {
64 syslog(LOG_INFO, "ttloop: read: %m\n");
65 exit(1);
66 } else if (ncc == 0) {
67 syslog(LOG_INFO, "ttloop: peer died: %m\n");
68 exit(1);
70 DIAG(TD_REPORT, {
71 output_data("td: ttloop read %d chars\r\n", ncc);
72 });
73 netip = netibuf;
74 telrcv(); /* state machine */
75 if (ncc > 0) {
76 pfrontp = pbackp = ptyobuf;
77 telrcv();
79 } /* end of ttloop */
82 * Check a descriptor to see if out of band data exists on it.
84 int
85 stilloob(int s)
87 static struct timeval timeout = { 0 };
88 fd_set excepts;
89 int value;
91 do {
92 FD_ZERO(&excepts);
93 FD_SET(s, &excepts);
94 value = select(s+1, 0, 0, &excepts, &timeout);
95 } while ((value == -1) && (errno == EINTR));
97 if (value < 0) {
98 fatalperror(ourpty, "select");
100 if (FD_ISSET(s, &excepts)) {
101 return 1;
102 } else {
103 return 0;
107 void
108 ptyflush(void)
110 int n;
112 if ((n = pfrontp - pbackp) > 0) {
113 DIAG((TD_REPORT | TD_PTYDATA), {
114 output_data("td: ptyflush %d chars\r\n", n);
116 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
117 n = write(ourpty, pbackp, n);
119 if (n < 0) {
120 if (errno == EWOULDBLOCK || errno == EINTR)
121 return;
122 cleanup(0);
124 pbackp += n;
125 if (pbackp == pfrontp)
126 pbackp = pfrontp = ptyobuf;
130 * nextitem()
132 * Return the address of the next "item" in the TELNET data
133 * stream. This will be the address of the next character if
134 * the current address is a user data character, or it will
135 * be the address of the character following the TELNET command
136 * if the current address is a TELNET IAC ("I Am a Command")
137 * character.
139 char *
140 nextitem(char *current)
142 if ((*current&0xff) != IAC) {
143 return current+1;
145 switch (*(current+1)&0xff) {
146 case DO:
147 case DONT:
148 case WILL:
149 case WONT:
150 return current+3;
151 case SB:{
152 /* loop forever looking for the SE */
153 char *look = current+2;
155 for (;;) {
156 if ((*look++&0xff) == IAC) {
157 if ((*look++&0xff) == SE) {
158 return look;
163 default:
164 return current+2;
170 * netclear()
172 * We are about to do a TELNET SYNCH operation. Clear
173 * the path to the network.
175 * Things are a bit tricky since we may have sent the first
176 * byte or so of a previous TELNET command into the network.
177 * So, we have to scan the network buffer from the beginning
178 * until we are up to where we want to be.
180 * A side effect of what we do, just to keep things
181 * simple, is to clear the urgent data pointer. The principal
182 * caller should be setting the urgent data pointer AFTER calling
183 * us in any case.
185 void
186 netclear(void)
188 char *thisitem, *next;
189 char *good;
190 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
191 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
193 #ifdef ENCRYPTION
194 thisitem = nclearto > netobuf ? nclearto : netobuf;
195 #else
196 thisitem = netobuf;
197 #endif
199 while ((next = nextitem(thisitem)) <= nbackp) {
200 thisitem = next;
203 /* Now, thisitem is first before/at boundary. */
205 #ifdef ENCRYPTION
206 good = nclearto > netobuf ? nclearto : netobuf;
207 #else
208 good = netobuf; /* where the good bytes go */
209 #endif
211 while (nfrontp > thisitem) {
212 if (wewant(thisitem)) {
213 int length;
215 next = thisitem;
216 do {
217 next = nextitem(next);
218 } while (wewant(next) && (nfrontp > next));
219 length = next-thisitem;
220 memmove(good, thisitem, length);
221 good += length;
222 thisitem = next;
223 } else {
224 thisitem = nextitem(thisitem);
228 nbackp = netobuf;
229 nfrontp = good; /* next byte to be sent */
230 neturg = 0;
231 } /* end of netclear */
234 * netflush
235 * Send as much data as possible to the network,
236 * handling requests for urgent data.
238 void
239 netflush(void)
241 int n;
242 extern int not42;
244 if ((n = nfrontp - nbackp) > 0) {
245 DIAG(TD_REPORT,
246 { n += output_data("td: netflush %d chars\r\n", n);
248 #ifdef ENCRYPTION
249 if (encrypt_output) {
250 char *s = nclearto ? nclearto : nbackp;
251 if (nfrontp - s > 0) {
252 (*encrypt_output)((unsigned char *)s, nfrontp-s);
253 nclearto = nfrontp;
256 #endif
258 * if no urgent data, or if the other side appears to be an
259 * old 4.2 client (and thus unable to survive TCP urgent data),
260 * write the entire buffer in non-OOB mode.
262 #if 1 /* remove this to make it work between solaris 2.6 and linux */
263 if ((neturg == 0) || (not42 == 0)) {
264 #endif
265 n = write(net, nbackp, n); /* normal write */
266 #if 1 /* remove this to make it work between solaris 2.6 and linux */
267 } else {
268 n = neturg - nbackp;
270 * In 4.2 (and 4.3) systems, there is some question about
271 * what byte in a sendOOB operation is the "OOB" data.
272 * To make ourselves compatible, we only send ONE byte
273 * out of band, the one WE THINK should be OOB (though
274 * we really have more the TCP philosophy of urgent data
275 * rather than the Unix philosophy of OOB data).
277 if (n > 1) {
278 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
279 } else {
280 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
283 #endif
285 if (n < 0) {
286 if (errno == EWOULDBLOCK || errno == EINTR)
287 return;
288 cleanup(0);
290 nbackp += n;
291 #ifdef ENCRYPTION
292 if (nbackp > nclearto)
293 nclearto = 0;
294 #endif
295 if (nbackp >= neturg) {
296 neturg = 0;
298 if (nbackp == nfrontp) {
299 nbackp = nfrontp = netobuf;
300 #ifdef ENCRYPTION
301 nclearto = 0;
302 #endif
304 return;
309 * writenet
311 * Just a handy little function to write a bit of raw data to the net.
312 * It will force a transmit of the buffer if necessary
314 * arguments
315 * ptr - A pointer to a character string to write
316 * len - How many bytes to write
318 void
319 writenet(unsigned char *ptr, int len)
321 /* flush buffer if no room for new data) */
322 while ((&netobuf[BUFSIZ] - nfrontp) < len) {
323 /* if this fails, don't worry, buffer is a little big */
324 netflush();
327 memmove(nfrontp, ptr, len);
328 nfrontp += len;
333 * miscellaneous functions doing a variety of little jobs follow ...
337 void fatal(int f, char *msg)
339 char buf[BUFSIZ];
341 snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
342 #ifdef ENCRYPTION
343 if (encrypt_output) {
345 * Better turn off encryption first....
346 * Hope it flushes...
348 encrypt_send_end();
349 netflush();
351 #endif
352 write(f, buf, (int)strlen(buf));
353 sleep(1); /*XXX*/
354 exit(1);
357 void
358 fatalperror(int f, const char *msg)
360 char buf[BUFSIZ];
362 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
363 fatal(f, buf);
366 char editedhost[32];
368 void edithost(char *pat, char *host)
370 char *res = editedhost;
372 if (!pat)
373 pat = "";
374 while (*pat) {
375 switch (*pat) {
377 case '#':
378 if (*host)
379 host++;
380 break;
382 case '@':
383 if (*host)
384 *res++ = *host++;
385 break;
387 default:
388 *res++ = *pat;
389 break;
391 if (res == &editedhost[sizeof editedhost - 1]) {
392 *res = '\0';
393 return;
395 pat++;
397 if (*host)
398 strcpy_truncate (res, host,
399 sizeof editedhost - (res - editedhost));
400 else
401 *res = '\0';
402 editedhost[sizeof editedhost - 1] = '\0';
405 static char *putlocation;
407 void
408 putstr(char *s)
411 while (*s)
412 putchr(*s++);
415 void
416 putchr(int cc)
418 *putlocation++ = cc;
422 * This is split on two lines so that SCCS will not see the M
423 * between two % signs and expand it...
425 static char fmtstr[] = { "%l:%M" "%P on %A, %d %B %Y" };
427 void putf(char *cp, char *where)
429 #ifdef HAVE_UNAME
430 struct utsname name;
431 #endif
432 char *slash;
433 time_t t;
434 char db[100];
436 /* if we don't have uname, set these to sensible values */
437 char *sysname = "Unix",
438 *machine = "",
439 *release = "",
440 *version = "";
442 #ifdef HAVE_UNAME
443 uname(&name);
444 sysname=name.sysname;
445 machine=name.machine;
446 release=name.release;
447 version=name.version;
448 #endif
450 putlocation = where;
452 while (*cp) {
453 if (*cp != '%') {
454 putchr(*cp++);
455 continue;
457 switch (*++cp) {
459 case 't':
460 #ifdef STREAMSPTY
461 /* names are like /dev/pts/2 -- we want pts/2 */
462 slash = strchr(line+1, '/');
463 #else
464 slash = strrchr(line, '/');
465 #endif
466 if (slash == (char *) 0)
467 putstr(line);
468 else
469 putstr(&slash[1]);
470 break;
472 case 'h':
473 putstr(editedhost);
474 break;
476 case 's':
477 putstr(sysname);
478 break;
480 case 'm':
481 putstr(machine);
482 break;
484 case 'r':
485 putstr(release);
486 break;
488 case 'v':
489 putstr(version);
490 break;
492 case 'd':
493 time(&t);
494 strftime(db, sizeof(db), fmtstr, localtime(&t));
495 putstr(db);
496 break;
498 case '%':
499 putchr('%');
500 break;
502 cp++;
506 #ifdef DIAGNOSTICS
508 * Print telnet options and commands in plain text, if possible.
510 void
511 printoption(char *fmt, int option)
513 if (TELOPT_OK(option))
514 output_data("%s %s\r\n",
515 fmt,
516 TELOPT(option));
517 else if (TELCMD_OK(option))
518 output_data("%s %s\r\n",
519 fmt,
520 TELCMD(option));
521 else
522 output_data("%s %d\r\n",
523 fmt,
524 option);
525 return;
528 void
529 printsub(int direction, unsigned char *pointer, int length)
530 /* '<' or '>' */
531 /* where suboption data sits */
532 /* length of suboption data */
534 int i = 0;
535 unsigned char buf[512];
537 if (!(diagnostic & TD_OPTIONS))
538 return;
540 if (direction) {
541 output_data("td: %s suboption ",
542 direction == '<' ? "recv" : "send");
543 if (length >= 3) {
544 int j;
546 i = pointer[length-2];
547 j = pointer[length-1];
549 if (i != IAC || j != SE) {
550 output_data("(terminated by ");
551 if (TELOPT_OK(i))
552 output_data("%s ",
553 TELOPT(i));
554 else if (TELCMD_OK(i))
555 output_data("%s ",
556 TELCMD(i));
557 else
558 output_data("%d ",
560 if (TELOPT_OK(j))
561 output_data("%s",
562 TELOPT(j));
563 else if (TELCMD_OK(j))
564 output_data("%s",
565 TELCMD(j));
566 else
567 output_data("%d",
569 output_data(", not IAC SE!) ");
572 length -= 2;
574 if (length < 1) {
575 output_data("(Empty suboption??\?)");
576 return;
578 switch (pointer[0]) {
579 case TELOPT_TTYPE:
580 output_data("TERMINAL-TYPE ");
581 switch (pointer[1]) {
582 case TELQUAL_IS:
583 output_data("IS \"%.*s\"",
584 length-2,
585 (char *)pointer+2);
586 break;
587 case TELQUAL_SEND:
588 output_data("SEND");
589 break;
590 default:
591 output_data("- unknown qualifier %d (0x%x).",
592 pointer[1], pointer[1]);
594 break;
595 case TELOPT_TSPEED:
596 output_data("TERMINAL-SPEED");
597 if (length < 2) {
598 output_data(" (empty suboption??\?)");
599 break;
601 switch (pointer[1]) {
602 case TELQUAL_IS:
603 output_data(" IS %.*s", length-2, (char *)pointer+2);
604 break;
605 default:
606 if (pointer[1] == 1)
607 output_data(" SEND");
608 else
609 output_data(" %d (unknown)", pointer[1]);
610 for (i = 2; i < length; i++) {
611 output_data(" ?%d?", pointer[i]);
613 break;
615 break;
617 case TELOPT_LFLOW:
618 output_data("TOGGLE-FLOW-CONTROL");
619 if (length < 2) {
620 output_data(" (empty suboption??\?)");
621 break;
623 switch (pointer[1]) {
624 case LFLOW_OFF:
625 output_data(" OFF");
626 break;
627 case LFLOW_ON:
628 output_data(" ON");
629 break;
630 case LFLOW_RESTART_ANY:
631 output_data(" RESTART-ANY");
632 break;
633 case LFLOW_RESTART_XON:
634 output_data(" RESTART-XON");
635 break;
636 default:
637 output_data(" %d (unknown)",
638 pointer[1]);
640 for (i = 2; i < length; i++) {
641 output_data(" ?%d?",
642 pointer[i]);
644 break;
646 case TELOPT_NAWS:
647 output_data("NAWS");
648 if (length < 2) {
649 output_data(" (empty suboption??\?)");
650 break;
652 if (length == 2) {
653 output_data(" ?%d?",
654 pointer[1]);
655 break;
657 output_data(" %u %u(%u)",
658 pointer[1],
659 pointer[2],
660 (((unsigned int)pointer[1])<<8) + pointer[2]);
661 if (length == 4) {
662 output_data(" ?%d?",
663 pointer[3]);
664 break;
666 output_data(" %u %u(%u)",
667 pointer[3],
668 pointer[4],
669 (((unsigned int)pointer[3])<<8) + pointer[4]);
670 for (i = 5; i < length; i++) {
671 output_data(" ?%d?",
672 pointer[i]);
674 break;
676 case TELOPT_LINEMODE:
677 output_data("LINEMODE ");
678 if (length < 2) {
679 output_data(" (empty suboption??\?)");
680 break;
682 switch (pointer[1]) {
683 case WILL:
684 output_data("WILL ");
685 goto common;
686 case WONT:
687 output_data("WONT ");
688 goto common;
689 case DO:
690 output_data("DO ");
691 goto common;
692 case DONT:
693 output_data("DONT ");
694 common:
695 if (length < 3) {
696 output_data("(no option??\?)");
697 break;
699 switch (pointer[2]) {
700 case LM_FORWARDMASK:
701 output_data("Forward Mask");
702 for (i = 3; i < length; i++) {
703 output_data(" %x", pointer[i]);
705 break;
706 default:
707 output_data("%d (unknown)",
708 pointer[2]);
709 for (i = 3; i < length; i++) {
710 output_data(" %d",
711 pointer[i]);
713 break;
715 break;
717 case LM_SLC:
718 output_data("SLC");
719 for (i = 2; i < length - 2; i += 3) {
720 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
721 output_data(" %s",
722 SLC_NAME(pointer[i+SLC_FUNC]));
723 else
724 output_data(" %d",
725 pointer[i+SLC_FUNC]);
726 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
727 case SLC_NOSUPPORT:
728 output_data(" NOSUPPORT");
729 break;
730 case SLC_CANTCHANGE:
731 output_data(" CANTCHANGE");
732 break;
733 case SLC_VARIABLE:
734 output_data(" VARIABLE");
735 break;
736 case SLC_DEFAULT:
737 output_data(" DEFAULT");
738 break;
740 output_data("%s%s%s",
741 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
742 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
743 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
744 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
745 SLC_FLUSHOUT| SLC_LEVELBITS)) {
746 output_data("(0x%x)",
747 pointer[i+SLC_FLAGS]);
749 output_data(" %d;",
750 pointer[i+SLC_VALUE]);
751 if ((pointer[i+SLC_VALUE] == IAC) &&
752 (pointer[i+SLC_VALUE+1] == IAC))
753 i++;
755 for (; i < length; i++) {
756 output_data(" ?%d?",
757 pointer[i]);
759 break;
761 case LM_MODE:
762 output_data("MODE ");
763 if (length < 3) {
764 output_data("(no mode??\?)");
765 break;
768 char tbuf[32];
769 snprintf(tbuf,
770 sizeof(tbuf),
771 "%s%s%s%s%s",
772 pointer[2]&MODE_EDIT ? "|EDIT" : "",
773 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
774 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
775 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
776 pointer[2]&MODE_ACK ? "|ACK" : "");
777 output_data("%s",
778 tbuf[1] ? &tbuf[1] : "0");
780 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
781 output_data(" (0x%x)",
782 pointer[2]);
784 for (i = 3; i < length; i++) {
785 output_data(" ?0x%x?",
786 pointer[i]);
788 break;
789 default:
790 output_data("%d (unknown)",
791 pointer[1]);
792 for (i = 2; i < length; i++) {
793 output_data(" %d", pointer[i]);
796 break;
798 case TELOPT_STATUS: {
799 char *cp;
800 int j, k;
802 output_data("STATUS");
804 switch (pointer[1]) {
805 default:
806 if (pointer[1] == TELQUAL_SEND)
807 output_data(" SEND");
808 else
809 output_data(" %d (unknown)",
810 pointer[1]);
811 for (i = 2; i < length; i++) {
812 output_data(" ?%d?",
813 pointer[i]);
815 break;
816 case TELQUAL_IS:
817 output_data(" IS\r\n");
819 for (i = 2; i < length; i++) {
820 switch(pointer[i]) {
821 case DO: cp = "DO"; goto common2;
822 case DONT: cp = "DONT"; goto common2;
823 case WILL: cp = "WILL"; goto common2;
824 case WONT: cp = "WONT"; goto common2;
825 common2:
826 i++;
827 if (TELOPT_OK(pointer[i]))
828 output_data(" %s %s",
830 TELOPT(pointer[i]));
831 else
832 output_data(" %s %d",
834 pointer[i]);
836 output_data("\r\n");
837 break;
839 case SB:
840 output_data(" SB ");
841 i++;
842 j = k = i;
843 while (j < length) {
844 if (pointer[j] == SE) {
845 if (j+1 == length)
846 break;
847 if (pointer[j+1] == SE)
848 j++;
849 else
850 break;
852 pointer[k++] = pointer[j++];
854 printsub(0, &pointer[i], k - i);
855 if (i < length) {
856 output_data(" SE");
857 i = j;
858 } else
859 i = j - 1;
861 output_data("\r\n");
863 break;
865 default:
866 output_data(" %d",
867 pointer[i]);
868 break;
871 break;
873 break;
876 case TELOPT_XDISPLOC:
877 output_data("X-DISPLAY-LOCATION ");
878 switch (pointer[1]) {
879 case TELQUAL_IS:
880 output_data("IS \"%.*s\"",
881 length-2,
882 (char *)pointer+2);
883 break;
884 case TELQUAL_SEND:
885 output_data("SEND");
886 break;
887 default:
888 output_data("- unknown qualifier %d (0x%x).",
889 pointer[1], pointer[1]);
891 break;
893 case TELOPT_NEW_ENVIRON:
894 output_data("NEW-ENVIRON ");
895 goto env_common1;
896 case TELOPT_OLD_ENVIRON:
897 output_data("OLD-ENVIRON");
898 env_common1:
899 switch (pointer[1]) {
900 case TELQUAL_IS:
901 output_data("IS ");
902 goto env_common;
903 case TELQUAL_SEND:
904 output_data("SEND ");
905 goto env_common;
906 case TELQUAL_INFO:
907 output_data("INFO ");
908 env_common:
910 int noquote = 2;
911 for (i = 2; i < length; i++ ) {
912 switch (pointer[i]) {
913 case NEW_ENV_VAR:
914 output_data("\" VAR " + noquote);
915 noquote = 2;
916 break;
918 case NEW_ENV_VALUE:
919 output_data("\" VALUE " + noquote);
920 noquote = 2;
921 break;
923 case ENV_ESC:
924 output_data("\" ESC " + noquote);
925 noquote = 2;
926 break;
928 case ENV_USERVAR:
929 output_data("\" USERVAR " + noquote);
930 noquote = 2;
931 break;
933 default:
934 if (isprint(pointer[i]) && pointer[i] != '"') {
935 if (noquote) {
936 output_data ("\"");
937 noquote = 0;
939 output_data ("%c", pointer[i]);
940 } else {
941 output_data("\" %03o " + noquote,
942 pointer[i]);
943 noquote = 2;
945 break;
948 if (!noquote)
949 output_data ("\"");
950 break;
953 break;
955 #ifdef AUTHENTICATION
956 case TELOPT_AUTHENTICATION:
957 output_data("AUTHENTICATION");
959 if (length < 2) {
960 output_data(" (empty suboption??\?)");
961 break;
963 switch (pointer[1]) {
964 case TELQUAL_REPLY:
965 case TELQUAL_IS:
966 output_data(" %s ",
967 (pointer[1] == TELQUAL_IS) ?
968 "IS" : "REPLY");
969 if (AUTHTYPE_NAME_OK(pointer[2]))
970 output_data("%s ",
971 AUTHTYPE_NAME(pointer[2]));
972 else
973 output_data("%d ",
974 pointer[2]);
975 if (length < 3) {
976 output_data("(partial suboption??\?)");
977 break;
979 output_data("%s|%s",
980 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
981 "CLIENT" : "SERVER",
982 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
983 "MUTUAL" : "ONE-WAY");
985 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
986 output_data("%s",
987 buf);
988 break;
990 case TELQUAL_SEND:
991 i = 2;
992 output_data(" SEND ");
993 while (i < length) {
994 if (AUTHTYPE_NAME_OK(pointer[i]))
995 output_data("%s ",
996 AUTHTYPE_NAME(pointer[i]));
997 else
998 output_data("%d ",
999 pointer[i]);
1000 if (++i >= length) {
1001 output_data("(partial suboption??\?)");
1002 break;
1004 output_data("%s|%s ",
1005 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1006 "CLIENT" : "SERVER",
1007 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1008 "MUTUAL" : "ONE-WAY");
1009 ++i;
1011 break;
1013 case TELQUAL_NAME:
1014 i = 2;
1015 output_data(" NAME \"%.*s\"",
1016 length - 2,
1017 pointer);
1018 break;
1020 default:
1021 for (i = 2; i < length; i++) {
1022 output_data(" ?%d?",
1023 pointer[i]);
1025 break;
1027 break;
1028 #endif
1030 #ifdef ENCRYPTION
1031 case TELOPT_ENCRYPT:
1032 output_data("ENCRYPT");
1033 if (length < 2) {
1034 output_data(" (empty suboption?)");
1035 break;
1037 switch (pointer[1]) {
1038 case ENCRYPT_START:
1039 output_data(" START");
1040 break;
1042 case ENCRYPT_END:
1043 output_data(" END");
1044 break;
1046 case ENCRYPT_REQSTART:
1047 output_data(" REQUEST-START");
1048 break;
1050 case ENCRYPT_REQEND:
1051 output_data(" REQUEST-END");
1052 break;
1054 case ENCRYPT_IS:
1055 case ENCRYPT_REPLY:
1056 output_data(" %s ",
1057 (pointer[1] == ENCRYPT_IS) ?
1058 "IS" : "REPLY");
1059 if (length < 3) {
1060 output_data(" (partial suboption?)");
1061 break;
1063 if (ENCTYPE_NAME_OK(pointer[2]))
1064 output_data("%s ",
1065 ENCTYPE_NAME(pointer[2]));
1066 else
1067 output_data(" %d (unknown)",
1068 pointer[2]);
1070 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1071 output_data("%s",
1072 buf);
1073 break;
1075 case ENCRYPT_SUPPORT:
1076 i = 2;
1077 output_data(" SUPPORT ");
1078 while (i < length) {
1079 if (ENCTYPE_NAME_OK(pointer[i]))
1080 output_data("%s ",
1081 ENCTYPE_NAME(pointer[i]));
1082 else
1083 output_data("%d ",
1084 pointer[i]);
1085 i++;
1087 break;
1089 case ENCRYPT_ENC_KEYID:
1090 output_data(" ENC_KEYID %d", pointer[1]);
1091 goto encommon;
1093 case ENCRYPT_DEC_KEYID:
1094 output_data(" DEC_KEYID %d", pointer[1]);
1095 goto encommon;
1097 default:
1098 output_data(" %d (unknown)", pointer[1]);
1099 encommon:
1100 for (i = 2; i < length; i++) {
1101 output_data(" %d", pointer[i]);
1103 break;
1105 break;
1106 #endif
1108 default:
1109 if (TELOPT_OK(pointer[0]))
1110 output_data("%s (unknown)",
1111 TELOPT(pointer[0]));
1112 else
1113 output_data("%d (unknown)",
1114 pointer[i]);
1115 for (i = 1; i < length; i++) {
1116 output_data(" %d", pointer[i]);
1118 break;
1120 output_data("\r\n");
1124 * Dump a data buffer in hex and ascii to the output data stream.
1126 void
1127 printdata(char *tag, char *ptr, int cnt)
1129 int i;
1130 char xbuf[30];
1132 while (cnt) {
1133 /* flush net output buffer if no room for new data) */
1134 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1135 netflush();
1138 /* add a line of output */
1139 output_data("%s: ", tag);
1140 for (i = 0; i < 20 && cnt; i++) {
1141 output_data("%02x", *ptr);
1142 if (isprint(*ptr)) {
1143 xbuf[i] = *ptr;
1144 } else {
1145 xbuf[i] = '.';
1147 if (i % 2) {
1148 output_data(" ");
1150 cnt--;
1151 ptr++;
1153 xbuf[i] = '\0';
1154 output_data(" %s\r\n", xbuf);
1157 #endif /* DIAGNOSTICS */