No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / telnet / telnetd / utility.c
blob964bb0538a3e2971e402cc3a9a5a5e30a726ef2b
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("$Heimdal: utility.c 15844 2005-08-08 13:36:16Z lha $"
38 "$NetBSD$");
41 * utility functions performing io related tasks
45 * ttloop
47 * A small subroutine to flush the network output buffer, get some
48 * data from the network, and pass it through the telnet state
49 * machine. We also flush the pty input buffer (by dropping its data)
50 * if it becomes too full.
52 * return 0 if OK or 1 if interrupted by a signal.
55 int
56 ttloop(void)
58 DIAG(TD_REPORT, {
59 output_data("td: ttloop\r\n");
60 });
61 if (nfrontp-nbackp)
62 netflush();
63 ncc = read(net, netibuf, sizeof netibuf);
64 if (ncc < 0) {
65 if (errno == EINTR)
66 return 1;
67 syslog(LOG_INFO, "ttloop: read: %m\n");
68 exit(1);
69 } else if (ncc == 0) {
70 syslog(LOG_INFO, "ttloop: peer died\n");
71 exit(1);
73 DIAG(TD_REPORT, {
74 output_data("td: ttloop read %d chars\r\n", ncc);
75 });
76 netip = netibuf;
77 telrcv(); /* state machine */
78 if (ncc > 0) {
79 pfrontp = pbackp = ptyobuf;
80 telrcv();
82 return 0;
83 } /* end of ttloop */
86 * Check a descriptor to see if out of band data exists on it.
88 int
89 stilloob(int s)
91 static struct timeval timeout = { 0 };
92 fd_set excepts;
93 int value;
95 if (s >= FD_SETSIZE)
96 fatal(ourpty, "fd too large");
98 do {
99 FD_ZERO(&excepts);
100 FD_SET(s, &excepts);
101 value = select(s+1, 0, 0, &excepts, &timeout);
102 } while ((value == -1) && (errno == EINTR));
104 if (value < 0) {
105 fatalperror(ourpty, "select");
107 if (FD_ISSET(s, &excepts)) {
108 return 1;
109 } else {
110 return 0;
114 void
115 ptyflush(void)
117 int n;
119 if ((n = pfrontp - pbackp) > 0) {
120 DIAG((TD_REPORT | TD_PTYDATA), {
121 output_data("td: ptyflush %d chars\r\n", n);
123 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
124 n = write(ourpty, pbackp, n);
126 if (n < 0) {
127 if (errno == EWOULDBLOCK || errno == EINTR)
128 return;
129 cleanup(0);
131 pbackp += n;
132 if (pbackp == pfrontp)
133 pbackp = pfrontp = ptyobuf;
137 * nextitem()
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")
144 * character.
146 char *
147 nextitem(char *current)
149 if ((*current&0xff) != IAC) {
150 return current+1;
152 switch (*(current+1)&0xff) {
153 case DO:
154 case DONT:
155 case WILL:
156 case WONT:
157 return current+3;
158 case SB:{
159 /* loop forever looking for the SE */
160 char *look = current+2;
162 for (;;) {
163 if ((*look++&0xff) == IAC) {
164 if ((*look++&0xff) == SE) {
165 return look;
170 default:
171 return current+2;
177 * netclear()
179 * We are about to do a TELNET SYNCH operation. Clear
180 * the path to the network.
182 * Things are a bit tricky since we may have sent the first
183 * byte or so of a previous TELNET command into the network.
184 * So, we have to scan the network buffer from the beginning
185 * until we are up to where we want to be.
187 * A side effect of what we do, just to keep things
188 * simple, is to clear the urgent data pointer. The principal
189 * caller should be setting the urgent data pointer AFTER calling
190 * us in any case.
192 void
193 netclear(void)
195 char *thisitem, *next;
196 char *good;
197 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
198 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
200 #ifdef ENCRYPTION
201 thisitem = nclearto > netobuf ? nclearto : netobuf;
202 #else
203 thisitem = netobuf;
204 #endif
206 while ((next = nextitem(thisitem)) <= nbackp) {
207 thisitem = next;
210 /* Now, thisitem is first before/at boundary. */
212 #ifdef ENCRYPTION
213 good = nclearto > netobuf ? nclearto : netobuf;
214 #else
215 good = netobuf; /* where the good bytes go */
216 #endif
218 while (nfrontp > thisitem) {
219 if (wewant(thisitem)) {
220 int length;
222 next = thisitem;
223 do {
224 next = nextitem(next);
225 } while (wewant(next) && (nfrontp > next));
226 length = next-thisitem;
227 memmove(good, thisitem, length);
228 good += length;
229 thisitem = next;
230 } else {
231 thisitem = nextitem(thisitem);
235 nbackp = netobuf;
236 nfrontp = good; /* next byte to be sent */
237 neturg = 0;
238 } /* end of netclear */
240 extern int not42;
243 * netflush
244 * Send as much data as possible to the network,
245 * handling requests for urgent data.
247 void
248 netflush(void)
250 int n;
252 if ((n = nfrontp - nbackp) > 0) {
253 DIAG(TD_REPORT,
254 { n += output_data("td: netflush %d chars\r\n", n);
256 #ifdef ENCRYPTION
257 if (encrypt_output) {
258 char *s = nclearto ? nclearto : nbackp;
259 if (nfrontp - s > 0) {
260 (*encrypt_output)((unsigned char *)s, nfrontp-s);
261 nclearto = nfrontp;
264 #endif
266 * if no urgent data, or if the other side appears to be an
267 * old 4.2 client (and thus unable to survive TCP urgent data),
268 * write the entire buffer in non-OOB mode.
270 #if 1 /* remove this to make it work between solaris 2.6 and linux */
271 if ((neturg == 0) || (not42 == 0)) {
272 #endif
273 n = write(net, nbackp, n); /* normal write */
274 #if 1 /* remove this to make it work between solaris 2.6 and linux */
275 } else {
276 n = neturg - nbackp;
278 * In 4.2 (and 4.3) systems, there is some question about
279 * what byte in a sendOOB operation is the "OOB" data.
280 * To make ourselves compatible, we only send ONE byte
281 * out of band, the one WE THINK should be OOB (though
282 * we really have more the TCP philosophy of urgent data
283 * rather than the Unix philosophy of OOB data).
285 if (n > 1) {
286 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
287 } else {
288 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
291 #endif
293 if (n < 0) {
294 if (errno == EWOULDBLOCK || errno == EINTR)
295 return;
296 cleanup(0);
298 nbackp += n;
299 #ifdef ENCRYPTION
300 if (nbackp > nclearto)
301 nclearto = 0;
302 #endif
303 if (nbackp >= neturg) {
304 neturg = 0;
306 if (nbackp == nfrontp) {
307 nbackp = nfrontp = netobuf;
308 #ifdef ENCRYPTION
309 nclearto = 0;
310 #endif
312 return;
317 * writenet
319 * Just a handy little function to write a bit of raw data to the net.
320 * It will force a transmit of the buffer if necessary
322 * arguments
323 * ptr - A pointer to a character string to write
324 * len - How many bytes to write
326 void
327 writenet(const void *ptr, size_t len)
329 /* flush buffer if no room for new data) */
330 while ((&netobuf[BUFSIZ] - nfrontp) < len) {
331 /* if this fails, don't worry, buffer is a little big */
332 netflush();
334 if ((&netobuf[BUFSIZ] - nfrontp) < len)
335 abort();
337 memmove(nfrontp, ptr, len);
338 nfrontp += len;
343 * miscellaneous functions doing a variety of little jobs follow ...
347 void fatal(int f, char *msg)
349 char buf[BUFSIZ];
351 snprintf(buf, sizeof(buf), "telnetd: %s.\r\n", msg);
352 #ifdef ENCRYPTION
353 if (encrypt_output) {
355 * Better turn off encryption first....
356 * Hope it flushes...
358 encrypt_send_end();
359 netflush();
361 #endif
362 write(f, buf, (int)strlen(buf));
363 sleep(1); /*XXX*/
364 exit(1);
367 void
368 fatalperror_errno(int f, const char *msg, int error)
370 char buf[BUFSIZ];
372 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(error));
373 fatal(f, buf);
376 void
377 fatalperror(int f, const char *msg)
379 fatalperror_errno(f, msg, errno);
382 char editedhost[32];
384 void edithost(char *pat, char *host)
386 char *res = editedhost;
388 if (!pat)
389 pat = "";
390 while (*pat) {
391 switch (*pat) {
393 case '#':
394 if (*host)
395 host++;
396 break;
398 case '@':
399 if (*host)
400 *res++ = *host++;
401 break;
403 default:
404 *res++ = *pat;
405 break;
407 if (res == &editedhost[sizeof editedhost - 1]) {
408 *res = '\0';
409 return;
411 pat++;
413 if (*host)
414 strlcpy (res, host,
415 sizeof editedhost - (res - editedhost));
416 else
417 *res = '\0';
418 editedhost[sizeof editedhost - 1] = '\0';
421 static char *putlocation;
423 void
424 putstr(char *s)
427 while (*s)
428 putchr(*s++);
431 void
432 putchr(int cc)
434 *putlocation++ = cc;
437 static char fmtstr[] = { "%l:%M%P on %A, %d %B %Y" };
439 void putf(char *cp, char *where)
441 #ifdef HAVE_UNAME
442 struct utsname name;
443 #endif
444 char *slash;
445 time_t t;
446 char db[100];
448 /* if we don't have uname, set these to sensible values */
449 char *sysname = "Unix",
450 *machine = "",
451 *release = "",
452 *version = "";
454 #ifdef HAVE_UNAME
455 uname(&name);
456 sysname=name.sysname;
457 machine=name.machine;
458 release=name.release;
459 version=name.version;
460 #endif
462 putlocation = where;
464 while (*cp) {
465 if (*cp != '%') {
466 putchr(*cp++);
467 continue;
469 switch (*++cp) {
471 case 't':
472 slash = strchr(line+1, '/');
473 if (slash == (char *) 0)
474 putstr(line);
475 else
476 putstr(&slash[1]);
477 break;
479 case 'h':
480 putstr(editedhost);
481 break;
483 case 's':
484 putstr(sysname);
485 break;
487 case 'm':
488 putstr(machine);
489 break;
491 case 'r':
492 putstr(release);
493 break;
495 case 'v':
496 putstr(version);
497 break;
499 case 'd':
500 time(&t);
501 strftime(db, sizeof(db), fmtstr, localtime(&t));
502 putstr(db);
503 break;
505 case '%':
506 putchr('%');
507 break;
509 cp++;
513 #ifdef DIAGNOSTICS
515 * Print telnet options and commands in plain text, if possible.
517 void
518 printoption(char *fmt, int option)
520 if (TELOPT_OK(option))
521 output_data("%s %s\r\n",
522 fmt,
523 TELOPT(option));
524 else if (TELCMD_OK(option))
525 output_data("%s %s\r\n",
526 fmt,
527 TELCMD(option));
528 else
529 output_data("%s %d\r\n",
530 fmt,
531 option);
532 return;
535 void
536 printsub(int direction, unsigned char *pointer, int length)
537 /* '<' or '>' */
538 /* where suboption data sits */
539 /* length of suboption data */
541 int i = 0;
542 unsigned char buf[512];
544 if (!(diagnostic & TD_OPTIONS))
545 return;
547 if (direction) {
548 output_data("td: %s suboption ",
549 direction == '<' ? "recv" : "send");
550 if (length >= 3) {
551 int j;
553 i = pointer[length-2];
554 j = pointer[length-1];
556 if (i != IAC || j != SE) {
557 output_data("(terminated by ");
558 if (TELOPT_OK(i))
559 output_data("%s ",
560 TELOPT(i));
561 else if (TELCMD_OK(i))
562 output_data("%s ",
563 TELCMD(i));
564 else
565 output_data("%d ",
567 if (TELOPT_OK(j))
568 output_data("%s",
569 TELOPT(j));
570 else if (TELCMD_OK(j))
571 output_data("%s",
572 TELCMD(j));
573 else
574 output_data("%d",
576 output_data(", not IAC SE!) ");
579 length -= 2;
581 if (length < 1) {
582 output_data("(Empty suboption??\?)");
583 return;
585 switch (pointer[0]) {
586 case TELOPT_TTYPE:
587 output_data("TERMINAL-TYPE ");
588 switch (pointer[1]) {
589 case TELQUAL_IS:
590 output_data("IS \"%.*s\"",
591 length-2,
592 (char *)pointer+2);
593 break;
594 case TELQUAL_SEND:
595 output_data("SEND");
596 break;
597 default:
598 output_data("- unknown qualifier %d (0x%x).",
599 pointer[1], pointer[1]);
601 break;
602 case TELOPT_TSPEED:
603 output_data("TERMINAL-SPEED");
604 if (length < 2) {
605 output_data(" (empty suboption??\?)");
606 break;
608 switch (pointer[1]) {
609 case TELQUAL_IS:
610 output_data(" IS %.*s", length-2, (char *)pointer+2);
611 break;
612 default:
613 if (pointer[1] == 1)
614 output_data(" SEND");
615 else
616 output_data(" %d (unknown)", pointer[1]);
617 for (i = 2; i < length; i++) {
618 output_data(" ?%d?", pointer[i]);
620 break;
622 break;
624 case TELOPT_LFLOW:
625 output_data("TOGGLE-FLOW-CONTROL");
626 if (length < 2) {
627 output_data(" (empty suboption??\?)");
628 break;
630 switch (pointer[1]) {
631 case LFLOW_OFF:
632 output_data(" OFF");
633 break;
634 case LFLOW_ON:
635 output_data(" ON");
636 break;
637 case LFLOW_RESTART_ANY:
638 output_data(" RESTART-ANY");
639 break;
640 case LFLOW_RESTART_XON:
641 output_data(" RESTART-XON");
642 break;
643 default:
644 output_data(" %d (unknown)",
645 pointer[1]);
647 for (i = 2; i < length; i++) {
648 output_data(" ?%d?",
649 pointer[i]);
651 break;
653 case TELOPT_NAWS:
654 output_data("NAWS");
655 if (length < 2) {
656 output_data(" (empty suboption??\?)");
657 break;
659 if (length == 2) {
660 output_data(" ?%d?",
661 pointer[1]);
662 break;
664 output_data(" %u %u(%u)",
665 pointer[1],
666 pointer[2],
667 (((unsigned int)pointer[1])<<8) + pointer[2]);
668 if (length == 4) {
669 output_data(" ?%d?",
670 pointer[3]);
671 break;
673 output_data(" %u %u(%u)",
674 pointer[3],
675 pointer[4],
676 (((unsigned int)pointer[3])<<8) + pointer[4]);
677 for (i = 5; i < length; i++) {
678 output_data(" ?%d?",
679 pointer[i]);
681 break;
683 case TELOPT_LINEMODE:
684 output_data("LINEMODE ");
685 if (length < 2) {
686 output_data(" (empty suboption??\?)");
687 break;
689 switch (pointer[1]) {
690 case WILL:
691 output_data("WILL ");
692 goto common;
693 case WONT:
694 output_data("WONT ");
695 goto common;
696 case DO:
697 output_data("DO ");
698 goto common;
699 case DONT:
700 output_data("DONT ");
701 common:
702 if (length < 3) {
703 output_data("(no option??\?)");
704 break;
706 switch (pointer[2]) {
707 case LM_FORWARDMASK:
708 output_data("Forward Mask");
709 for (i = 3; i < length; i++) {
710 output_data(" %x", pointer[i]);
712 break;
713 default:
714 output_data("%d (unknown)",
715 pointer[2]);
716 for (i = 3; i < length; i++) {
717 output_data(" %d",
718 pointer[i]);
720 break;
722 break;
724 case LM_SLC:
725 output_data("SLC");
726 for (i = 2; i < length - 2; i += 3) {
727 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
728 output_data(" %s",
729 SLC_NAME(pointer[i+SLC_FUNC]));
730 else
731 output_data(" %d",
732 pointer[i+SLC_FUNC]);
733 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
734 case SLC_NOSUPPORT:
735 output_data(" NOSUPPORT");
736 break;
737 case SLC_CANTCHANGE:
738 output_data(" CANTCHANGE");
739 break;
740 case SLC_VARIABLE:
741 output_data(" VARIABLE");
742 break;
743 case SLC_DEFAULT:
744 output_data(" DEFAULT");
745 break;
747 output_data("%s%s%s",
748 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
749 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
750 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
751 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
752 SLC_FLUSHOUT| SLC_LEVELBITS)) {
753 output_data("(0x%x)",
754 pointer[i+SLC_FLAGS]);
756 output_data(" %d;",
757 pointer[i+SLC_VALUE]);
758 if ((pointer[i+SLC_VALUE] == IAC) &&
759 (pointer[i+SLC_VALUE+1] == IAC))
760 i++;
762 for (; i < length; i++) {
763 output_data(" ?%d?",
764 pointer[i]);
766 break;
768 case LM_MODE:
769 output_data("MODE ");
770 if (length < 3) {
771 output_data("(no mode??\?)");
772 break;
775 char tbuf[32];
776 snprintf(tbuf,
777 sizeof(tbuf),
778 "%s%s%s%s%s",
779 pointer[2]&MODE_EDIT ? "|EDIT" : "",
780 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
781 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
782 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
783 pointer[2]&MODE_ACK ? "|ACK" : "");
784 output_data("%s",
785 tbuf[1] ? &tbuf[1] : "0");
787 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
788 output_data(" (0x%x)",
789 pointer[2]);
791 for (i = 3; i < length; i++) {
792 output_data(" ?0x%x?",
793 pointer[i]);
795 break;
796 default:
797 output_data("%d (unknown)",
798 pointer[1]);
799 for (i = 2; i < length; i++) {
800 output_data(" %d", pointer[i]);
803 break;
805 case TELOPT_STATUS: {
806 char *cp;
807 int j, k;
809 output_data("STATUS");
811 switch (pointer[1]) {
812 default:
813 if (pointer[1] == TELQUAL_SEND)
814 output_data(" SEND");
815 else
816 output_data(" %d (unknown)",
817 pointer[1]);
818 for (i = 2; i < length; i++) {
819 output_data(" ?%d?",
820 pointer[i]);
822 break;
823 case TELQUAL_IS:
824 output_data(" IS\r\n");
826 for (i = 2; i < length; i++) {
827 switch(pointer[i]) {
828 case DO: cp = "DO"; goto common2;
829 case DONT: cp = "DONT"; goto common2;
830 case WILL: cp = "WILL"; goto common2;
831 case WONT: cp = "WONT"; goto common2;
832 common2:
833 i++;
834 if (TELOPT_OK(pointer[i]))
835 output_data(" %s %s",
837 TELOPT(pointer[i]));
838 else
839 output_data(" %s %d",
841 pointer[i]);
843 output_data("\r\n");
844 break;
846 case SB:
847 output_data(" SB ");
848 i++;
849 j = k = i;
850 while (j < length) {
851 if (pointer[j] == SE) {
852 if (j+1 == length)
853 break;
854 if (pointer[j+1] == SE)
855 j++;
856 else
857 break;
859 pointer[k++] = pointer[j++];
861 printsub(0, &pointer[i], k - i);
862 if (i < length) {
863 output_data(" SE");
864 i = j;
865 } else
866 i = j - 1;
868 output_data("\r\n");
870 break;
872 default:
873 output_data(" %d",
874 pointer[i]);
875 break;
878 break;
880 break;
883 case TELOPT_XDISPLOC:
884 output_data("X-DISPLAY-LOCATION ");
885 switch (pointer[1]) {
886 case TELQUAL_IS:
887 output_data("IS \"%.*s\"",
888 length-2,
889 (char *)pointer+2);
890 break;
891 case TELQUAL_SEND:
892 output_data("SEND");
893 break;
894 default:
895 output_data("- unknown qualifier %d (0x%x).",
896 pointer[1], pointer[1]);
898 break;
900 case TELOPT_NEW_ENVIRON:
901 output_data("NEW-ENVIRON ");
902 goto env_common1;
903 case TELOPT_OLD_ENVIRON:
904 output_data("OLD-ENVIRON");
905 env_common1:
906 switch (pointer[1]) {
907 case TELQUAL_IS:
908 output_data("IS ");
909 goto env_common;
910 case TELQUAL_SEND:
911 output_data("SEND ");
912 goto env_common;
913 case TELQUAL_INFO:
914 output_data("INFO ");
915 env_common:
917 int noquote = 2;
918 for (i = 2; i < length; i++ ) {
919 switch (pointer[i]) {
920 case NEW_ENV_VAR:
921 output_data("\" VAR " + noquote);
922 noquote = 2;
923 break;
925 case NEW_ENV_VALUE:
926 output_data("\" VALUE " + noquote);
927 noquote = 2;
928 break;
930 case ENV_ESC:
931 output_data("\" ESC " + noquote);
932 noquote = 2;
933 break;
935 case ENV_USERVAR:
936 output_data("\" USERVAR " + noquote);
937 noquote = 2;
938 break;
940 default:
941 if (isprint(pointer[i]) && pointer[i] != '"') {
942 if (noquote) {
943 output_data ("\"");
944 noquote = 0;
946 output_data ("%c", pointer[i]);
947 } else {
948 output_data("\" %03o " + noquote,
949 pointer[i]);
950 noquote = 2;
952 break;
955 if (!noquote)
956 output_data ("\"");
957 break;
960 break;
962 #ifdef AUTHENTICATION
963 case TELOPT_AUTHENTICATION:
964 output_data("AUTHENTICATION");
966 if (length < 2) {
967 output_data(" (empty suboption??\?)");
968 break;
970 switch (pointer[1]) {
971 case TELQUAL_REPLY:
972 case TELQUAL_IS:
973 output_data(" %s ",
974 (pointer[1] == TELQUAL_IS) ?
975 "IS" : "REPLY");
976 if (AUTHTYPE_NAME_OK(pointer[2]))
977 output_data("%s ",
978 AUTHTYPE_NAME(pointer[2]));
979 else
980 output_data("%d ",
981 pointer[2]);
982 if (length < 3) {
983 output_data("(partial suboption??\?)");
984 break;
986 output_data("%s|%s",
987 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
988 "CLIENT" : "SERVER",
989 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
990 "MUTUAL" : "ONE-WAY");
992 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
993 output_data("%s",
994 buf);
995 break;
997 case TELQUAL_SEND:
998 i = 2;
999 output_data(" SEND ");
1000 while (i < length) {
1001 if (AUTHTYPE_NAME_OK(pointer[i]))
1002 output_data("%s ",
1003 AUTHTYPE_NAME(pointer[i]));
1004 else
1005 output_data("%d ",
1006 pointer[i]);
1007 if (++i >= length) {
1008 output_data("(partial suboption??\?)");
1009 break;
1011 output_data("%s|%s ",
1012 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1013 "CLIENT" : "SERVER",
1014 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1015 "MUTUAL" : "ONE-WAY");
1016 ++i;
1018 break;
1020 case TELQUAL_NAME:
1021 i = 2;
1022 output_data(" NAME \"%.*s\"",
1023 length - 2,
1024 pointer);
1025 break;
1027 default:
1028 for (i = 2; i < length; i++) {
1029 output_data(" ?%d?",
1030 pointer[i]);
1032 break;
1034 break;
1035 #endif
1037 #ifdef ENCRYPTION
1038 case TELOPT_ENCRYPT:
1039 output_data("ENCRYPT");
1040 if (length < 2) {
1041 output_data(" (empty suboption?)");
1042 break;
1044 switch (pointer[1]) {
1045 case ENCRYPT_START:
1046 output_data(" START");
1047 break;
1049 case ENCRYPT_END:
1050 output_data(" END");
1051 break;
1053 case ENCRYPT_REQSTART:
1054 output_data(" REQUEST-START");
1055 break;
1057 case ENCRYPT_REQEND:
1058 output_data(" REQUEST-END");
1059 break;
1061 case ENCRYPT_IS:
1062 case ENCRYPT_REPLY:
1063 output_data(" %s ",
1064 (pointer[1] == ENCRYPT_IS) ?
1065 "IS" : "REPLY");
1066 if (length < 3) {
1067 output_data(" (partial suboption?)");
1068 break;
1070 if (ENCTYPE_NAME_OK(pointer[2]))
1071 output_data("%s ",
1072 ENCTYPE_NAME(pointer[2]));
1073 else
1074 output_data(" %d (unknown)",
1075 pointer[2]);
1077 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1078 output_data("%s",
1079 buf);
1080 break;
1082 case ENCRYPT_SUPPORT:
1083 i = 2;
1084 output_data(" SUPPORT ");
1085 while (i < length) {
1086 if (ENCTYPE_NAME_OK(pointer[i]))
1087 output_data("%s ",
1088 ENCTYPE_NAME(pointer[i]));
1089 else
1090 output_data("%d ",
1091 pointer[i]);
1092 i++;
1094 break;
1096 case ENCRYPT_ENC_KEYID:
1097 output_data(" ENC_KEYID %d", pointer[1]);
1098 goto encommon;
1100 case ENCRYPT_DEC_KEYID:
1101 output_data(" DEC_KEYID %d", pointer[1]);
1102 goto encommon;
1104 default:
1105 output_data(" %d (unknown)", pointer[1]);
1106 encommon:
1107 for (i = 2; i < length; i++) {
1108 output_data(" %d", pointer[i]);
1110 break;
1112 break;
1113 #endif
1115 default:
1116 if (TELOPT_OK(pointer[0]))
1117 output_data("%s (unknown)",
1118 TELOPT(pointer[0]));
1119 else
1120 output_data("%d (unknown)",
1121 pointer[i]);
1122 for (i = 1; i < length; i++) {
1123 output_data(" %d", pointer[i]);
1125 break;
1127 output_data("\r\n");
1131 * Dump a data buffer in hex and ascii to the output data stream.
1133 void
1134 printdata(char *tag, char *ptr, int cnt)
1136 int i;
1137 char xbuf[30];
1139 while (cnt) {
1140 /* flush net output buffer if no room for new data) */
1141 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1142 netflush();
1145 /* add a line of output */
1146 output_data("%s: ", tag);
1147 for (i = 0; i < 20 && cnt; i++) {
1148 output_data("%02x", *ptr);
1149 if (isprint((unsigned char)*ptr)) {
1150 xbuf[i] = *ptr;
1151 } else {
1152 xbuf[i] = '.';
1154 if (i % 2) {
1155 output_data(" ");
1157 cnt--;
1158 ptr++;
1160 xbuf[i] = '\0';
1161 output_data(" %s\r\n", xbuf);
1164 #endif /* DIAGNOSTICS */