utility.c revision 1.13 1 /* $NetBSD: utility.c,v 1.13 2000/01/13 13:11:32 ad Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)utility.c 8.4 (Berkeley) 5/30/95";
40 #else
41 __RCSID("$NetBSD: utility.c,v 1.13 2000/01/13 13:11:32 ad Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/utsname.h>
46 #define PRINTOPTIONS
47 #include "telnetd.h"
48
49 char *nextitem __P((char *));
50 void fatalperror __P((int, char *));
51 void edithost __P((char *, char *));
52 void putstr __P((char *));
53
54 /*
55 * utility functions performing io related tasks
56 */
57
58 /*
59 * ttloop
60 *
61 * A small subroutine to flush the network output buffer, get some data
62 * from the network, and pass it through the telnet state machine. We
63 * also flush the pty input buffer (by dropping its data) if it becomes
64 * too full.
65 */
66
67 void
68 ttloop()
69 {
70
71 DIAG(TD_REPORT, {sprintf(nfrontp, "td: ttloop\r\n");
72 nfrontp += strlen(nfrontp);});
73 if (nfrontp-nbackp) {
74 netflush();
75 }
76 ncc = read(net, netibuf, sizeof netibuf);
77 if (ncc < 0) {
78 syslog(LOG_INFO, "ttloop: read: %m\n");
79 exit(1);
80 } else if (ncc == 0) {
81 syslog(LOG_INFO, "ttloop: peer died: %m\n");
82 exit(1);
83 }
84 DIAG(TD_REPORT, {sprintf(nfrontp, "td: ttloop read %d chars\r\n", ncc);
85 nfrontp += strlen(nfrontp);});
86 netip = netibuf;
87 telrcv(); /* state machine */
88 if (ncc > 0) {
89 pfrontp = pbackp = ptyobuf;
90 telrcv();
91 }
92 } /* end of ttloop */
93
94 /*
95 * Check a descriptor to see if out of band data exists on it.
96 */
97 int
98 stilloob(s)
99 int s; /* socket number */
100 {
101 static struct timeval timeout = { 0 };
102 fd_set excepts;
103 int value;
104
105 do {
106 FD_ZERO(&excepts);
107 FD_SET(s, &excepts);
108 value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
109 } while ((value == -1) && (errno == EINTR));
110
111 if (value < 0) {
112 fatalperror(pty, "select");
113 }
114 if (FD_ISSET(s, &excepts)) {
115 return 1;
116 } else {
117 return 0;
118 }
119 }
120
121 void
122 ptyflush()
123 {
124 int n;
125
126 if ((n = pfrontp - pbackp) > 0) {
127 DIAG((TD_REPORT | TD_PTYDATA),
128 { sprintf(nfrontp, "td: ptyflush %d chars\r\n", n);
129 nfrontp += strlen(nfrontp); });
130 DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
131 n = write(pty, pbackp, n);
132 }
133 if (n < 0) {
134 if (errno == EWOULDBLOCK || errno == EINTR)
135 return;
136 cleanup(0);
137 }
138 pbackp += n;
139 if (pbackp == pfrontp)
140 pbackp = pfrontp = ptyobuf;
141 }
142
143 /*
144 * nextitem()
145 *
146 * Return the address of the next "item" in the TELNET data
147 * stream. This will be the address of the next character if
148 * the current address is a user data character, or it will
149 * be the address of the character following the TELNET command
150 * if the current address is a TELNET IAC ("I Am a Command")
151 * character.
152 */
153 char *
154 nextitem(current)
155 char *current;
156 {
157 if ((*current&0xff) != IAC) {
158 return current+1;
159 }
160 switch (*(current+1)&0xff) {
161 case DO:
162 case DONT:
163 case WILL:
164 case WONT:
165 return current+3;
166 case SB: /* loop forever looking for the SE */
167 {
168 register char *look = current+2;
169
170 for (;;) {
171 if ((*look++&0xff) == IAC) {
172 if ((*look++&0xff) == SE) {
173 return look;
174 }
175 }
176 }
177 }
178 default:
179 return current+2;
180 }
181 } /* end of nextitem */
182
183
184 /*
185 * netclear()
186 *
187 * We are about to do a TELNET SYNCH operation. Clear
188 * the path to the network.
189 *
190 * Things are a bit tricky since we may have sent the first
191 * byte or so of a previous TELNET command into the network.
192 * So, we have to scan the network buffer from the beginning
193 * until we are up to where we want to be.
194 *
195 * A side effect of what we do, just to keep things
196 * simple, is to clear the urgent data pointer. The principal
197 * caller should be setting the urgent data pointer AFTER calling
198 * us in any case.
199 */
200 void
201 netclear()
202 {
203 register char *thisitem, *next;
204 char *good;
205 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
206 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
207
208 thisitem = netobuf;
209
210 while ((next = nextitem(thisitem)) <= nbackp) {
211 thisitem = next;
212 }
213
214 /* Now, thisitem is first before/at boundary. */
215
216 good = netobuf; /* where the good bytes go */
217
218 while (nfrontp > thisitem) {
219 if (wewant(thisitem)) {
220 int length;
221
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);
232 }
233 }
234
235 nbackp = netobuf;
236 nfrontp = good; /* next byte to be sent */
237 neturg = 0;
238 } /* end of netclear */
239
240 /*
241 * netflush
242 * Send as much data as possible to the network,
243 * handling requests for urgent data.
244 */
245 void
246 netflush()
247 {
248 int n;
249 extern int not42;
250
251 if ((n = nfrontp - nbackp) > 0) {
252 DIAG(TD_REPORT,
253 { sprintf(nfrontp, "td: netflush %d chars\r\n", n);
254 n += strlen(nfrontp); /* get count first */
255 nfrontp += strlen(nfrontp); /* then move pointer */
256 });
257 /*
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.
261 */
262 if ((neturg == 0) || (not42 == 0)) {
263 n = write(net, nbackp, n); /* normal write */
264 } else {
265 n = neturg - nbackp;
266 /*
267 * In 4.2 (and 4.3) systems, there is some question about
268 * what byte in a sendOOB operation is the "OOB" data.
269 * To make ourselves compatible, we only send ONE byte
270 * out of band, the one WE THINK should be OOB (though
271 * we really have more the TCP philosophy of urgent data
272 * rather than the Unix philosophy of OOB data).
273 */
274 if (n > 1) {
275 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
276 } else {
277 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
278 }
279 }
280 }
281 if (n < 0) {
282 if (errno == EWOULDBLOCK || errno == EINTR)
283 return;
284 cleanup(0);
285 }
286 nbackp += n;
287 if (nbackp >= neturg) {
288 neturg = 0;
289 }
290 if (nbackp == nfrontp) {
291 nbackp = nfrontp = netobuf;
292 }
293 return;
294 } /* end of netflush */
295
296
297 /*
298 * writenet
299 *
300 * Just a handy little function to write a bit of raw data to the net.
301 * It will force a transmit of the buffer if necessary
302 *
303 * arguments
304 * ptr - A pointer to a character string to write
305 * len - How many bytes to write
306 */
307 void
308 writenet(ptr, len)
309 register unsigned char *ptr;
310 register int len;
311 {
312 /* flush buffer if no room for new data) */
313 if ((&netobuf[BUFSIZ] - nfrontp) < len) {
314 /* if this fails, don't worry, buffer is a little big */
315 netflush();
316 }
317
318 memmove(nfrontp, ptr, len);
319 nfrontp += len;
320
321 } /* end of writenet */
322
323
324 /*
325 * miscellaneous functions doing a variety of little jobs follow ...
326 */
327
328
329 void
330 fatal(f, msg)
331 int f;
332 char *msg;
333 {
334 char buf[BUFSIZ];
335
336 (void)snprintf(buf, sizeof buf, "telnetd: %s.\r\n", msg);
337 (void)write(f, buf, (int)strlen(buf));
338 sleep(1); /*XXX*/
339 exit(1);
340 }
341
342 void
343 fatalperror(f, msg)
344 int f;
345 char *msg;
346 {
347 char buf[BUFSIZ];
348
349 (void)snprintf(buf, sizeof buf, "%s: %s", msg, strerror(errno));
350 fatal(f, buf);
351 }
352
353 char editedhost[MAXHOSTNAMELEN];
354
355 void
356 edithost(pat, host)
357 register char *pat;
358 register char *host;
359 {
360 register char *res = editedhost;
361
362 if (!pat)
363 pat = "";
364 while (*pat) {
365 switch (*pat) {
366
367 case '#':
368 if (*host)
369 host++;
370 break;
371
372 case '@':
373 if (*host)
374 *res++ = *host++;
375 break;
376
377 default:
378 *res++ = *pat;
379 break;
380 }
381 if (res == &editedhost[sizeof editedhost - 1]) {
382 *res = '\0';
383 return;
384 }
385 pat++;
386 }
387 if (*host)
388 (void) strncpy(res, host,
389 sizeof editedhost - (res - editedhost) -1);
390 else
391 *res = '\0';
392 editedhost[sizeof editedhost - 1] = '\0';
393 }
394
395 static char *putlocation;
396
397 void
398 putstr(s)
399 register char *s;
400 {
401
402 while (*s)
403 putchr(*s++);
404 }
405
406 void
407 putchr(cc)
408 int cc;
409 {
410 *putlocation++ = cc;
411 }
412
413 /*
414 * This is split on two lines so that SCCS will not see the M
415 * between two % signs and expand it...
416 */
417 static char fmtstr[] = { "%l:%M\
418 %p on %A, %d %B %Y" };
419
420 char *
421 putf(cp, where)
422 register char *cp;
423 char *where;
424 {
425 char *slash;
426 time_t t;
427 char db[100];
428 struct utsname utsinfo;
429
430 uname(&utsinfo);
431
432 putlocation = where;
433
434 while (*cp) {
435 if (*cp != '%') {
436 putchr(*cp++);
437 continue;
438 }
439 switch (*++cp) {
440
441 case 't':
442 #ifdef STREAMSPTY
443 /* names are like /dev/pts/2 -- we want pts/2 */
444 slash = strchr(line+1, '/');
445 #else
446 slash = strrchr(line, '/');
447 #endif
448 if (slash == (char *) 0)
449 putstr(line);
450 else
451 putstr(&slash[1]);
452 break;
453
454 case 'h':
455 putstr(editedhost);
456 break;
457
458 case 'd':
459 (void)time(&t);
460 (void)strftime(db, sizeof(db), fmtstr, localtime(&t));
461 putstr(db);
462 break;
463
464 case '%':
465 putchr('%');
466 break;
467
468 case 's':
469 putstr(utsinfo.sysname);
470 break;
471
472 case 'm':
473 putstr(utsinfo.machine);
474 break;
475
476 case 'r':
477 putstr(utsinfo.release);
478 break;
479
480 case 'v':
481 puts(utsinfo.version);
482 break;
483 }
484 cp++;
485 }
486
487 return (putlocation);
488 }
489
490 #ifdef DIAGNOSTICS
491 /*
492 * Print telnet options and commands in plain text, if possible.
493 */
494 void
495 printoption(fmt, option)
496 register char *fmt;
497 register int option;
498 {
499 if (TELOPT_OK(option))
500 sprintf(nfrontp, "%s %s\r\n", fmt, TELOPT(option));
501 else if (TELCMD_OK(option))
502 sprintf(nfrontp, "%s %s\r\n", fmt, TELCMD(option));
503 else
504 sprintf(nfrontp, "%s %d\r\n", fmt, option);
505 nfrontp += strlen(nfrontp);
506 return;
507 }
508
509 void
510 printsub(direction, pointer, length)
511 char direction; /* '<' or '>' */
512 unsigned char *pointer; /* where suboption data sits */
513 int length; /* length of suboption data */
514 {
515 register int i = 0; /* XXX gcc */
516 #if defined(AUTHENTICATION)
517 char buf[512];
518 #endif
519
520 if (!(diagnostic & TD_OPTIONS))
521 return;
522
523 if (direction) {
524 sprintf(nfrontp, "td: %s suboption ",
525 direction == '<' ? "recv" : "send");
526 nfrontp += strlen(nfrontp);
527 if (length >= 3) {
528 register int j;
529
530 i = pointer[length-2];
531 j = pointer[length-1];
532
533 if (i != IAC || j != SE) {
534 sprintf(nfrontp, "(terminated by ");
535 nfrontp += strlen(nfrontp);
536 if (TELOPT_OK(i))
537 sprintf(nfrontp, "%s ", TELOPT(i));
538 else if (TELCMD_OK(i))
539 sprintf(nfrontp, "%s ", TELCMD(i));
540 else
541 sprintf(nfrontp, "%d ", i);
542 nfrontp += strlen(nfrontp);
543 if (TELOPT_OK(j))
544 sprintf(nfrontp, "%s", TELOPT(j));
545 else if (TELCMD_OK(j))
546 sprintf(nfrontp, "%s", TELCMD(j));
547 else
548 sprintf(nfrontp, "%d", j);
549 nfrontp += strlen(nfrontp);
550 sprintf(nfrontp, ", not IAC SE!) ");
551 nfrontp += strlen(nfrontp);
552 }
553 }
554 length -= 2;
555 }
556 if (length < 1) {
557 sprintf(nfrontp, "(Empty suboption??\?)");
558 nfrontp += strlen(nfrontp);
559 return;
560 }
561 switch (pointer[0]) {
562 case TELOPT_TTYPE:
563 sprintf(nfrontp, "TERMINAL-TYPE ");
564 nfrontp += strlen(nfrontp);
565 switch (pointer[1]) {
566 case TELQUAL_IS:
567 sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2);
568 break;
569 case TELQUAL_SEND:
570 sprintf(nfrontp, "SEND");
571 break;
572 default:
573 sprintf(nfrontp,
574 "- unknown qualifier %d (0x%x).",
575 pointer[1], pointer[1]);
576 }
577 nfrontp += strlen(nfrontp);
578 break;
579 case TELOPT_TSPEED:
580 sprintf(nfrontp, "TERMINAL-SPEED");
581 nfrontp += strlen(nfrontp);
582 if (length < 2) {
583 sprintf(nfrontp, " (empty suboption??\?)");
584 nfrontp += strlen(nfrontp);
585 break;
586 }
587 switch (pointer[1]) {
588 case TELQUAL_IS:
589 sprintf(nfrontp, " IS %.*s", length-2, (char *)pointer+2);
590 nfrontp += strlen(nfrontp);
591 break;
592 default:
593 if (pointer[1] == 1)
594 sprintf(nfrontp, " SEND");
595 else
596 sprintf(nfrontp, " %d (unknown)", pointer[1]);
597 nfrontp += strlen(nfrontp);
598 for (i = 2; i < length; i++) {
599 sprintf(nfrontp, " ?%d?", pointer[i]);
600 nfrontp += strlen(nfrontp);
601 }
602 break;
603 }
604 break;
605
606 case TELOPT_LFLOW:
607 sprintf(nfrontp, "TOGGLE-FLOW-CONTROL");
608 nfrontp += strlen(nfrontp);
609 if (length < 2) {
610 sprintf(nfrontp, " (empty suboption??\?)");
611 nfrontp += strlen(nfrontp);
612 break;
613 }
614 switch (pointer[1]) {
615 case LFLOW_OFF:
616 sprintf(nfrontp, " OFF"); break;
617 case LFLOW_ON:
618 sprintf(nfrontp, " ON"); break;
619 case LFLOW_RESTART_ANY:
620 sprintf(nfrontp, " RESTART-ANY"); break;
621 case LFLOW_RESTART_XON:
622 sprintf(nfrontp, " RESTART-XON"); break;
623 default:
624 sprintf(nfrontp, " %d (unknown)", pointer[1]);
625 }
626 nfrontp += strlen(nfrontp);
627 for (i = 2; i < length; i++) {
628 sprintf(nfrontp, " ?%d?", pointer[i]);
629 nfrontp += strlen(nfrontp);
630 }
631 break;
632
633 case TELOPT_NAWS:
634 sprintf(nfrontp, "NAWS");
635 nfrontp += strlen(nfrontp);
636 if (length < 2) {
637 sprintf(nfrontp, " (empty suboption??\?)");
638 nfrontp += strlen(nfrontp);
639 break;
640 }
641 if (length == 2) {
642 sprintf(nfrontp, " ?%d?", pointer[1]);
643 nfrontp += strlen(nfrontp);
644 break;
645 }
646 sprintf(nfrontp, " %d %d (%d)",
647 pointer[1], pointer[2],
648 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
649 nfrontp += strlen(nfrontp);
650 if (length == 4) {
651 sprintf(nfrontp, " ?%d?", pointer[3]);
652 nfrontp += strlen(nfrontp);
653 break;
654 }
655 sprintf(nfrontp, " %d %d (%d)",
656 pointer[3], pointer[4],
657 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
658 nfrontp += strlen(nfrontp);
659 for (i = 5; i < length; i++) {
660 sprintf(nfrontp, " ?%d?", pointer[i]);
661 nfrontp += strlen(nfrontp);
662 }
663 break;
664
665 case TELOPT_LINEMODE:
666 sprintf(nfrontp, "LINEMODE ");
667 nfrontp += strlen(nfrontp);
668 if (length < 2) {
669 sprintf(nfrontp, " (empty suboption??\?)");
670 nfrontp += strlen(nfrontp);
671 break;
672 }
673 switch (pointer[1]) {
674 case WILL:
675 sprintf(nfrontp, "WILL ");
676 goto common;
677 case WONT:
678 sprintf(nfrontp, "WONT ");
679 goto common;
680 case DO:
681 sprintf(nfrontp, "DO ");
682 goto common;
683 case DONT:
684 sprintf(nfrontp, "DONT ");
685 common:
686 nfrontp += strlen(nfrontp);
687 if (length < 3) {
688 sprintf(nfrontp, "(no option??\?)");
689 nfrontp += strlen(nfrontp);
690 break;
691 }
692 switch (pointer[2]) {
693 case LM_FORWARDMASK:
694 sprintf(nfrontp, "Forward Mask");
695 nfrontp += strlen(nfrontp);
696 for (i = 3; i < length; i++) {
697 sprintf(nfrontp, " %x", pointer[i]);
698 nfrontp += strlen(nfrontp);
699 }
700 break;
701 default:
702 sprintf(nfrontp, "%d (unknown)", pointer[2]);
703 nfrontp += strlen(nfrontp);
704 for (i = 3; i < length; i++) {
705 sprintf(nfrontp, " %d", pointer[i]);
706 nfrontp += strlen(nfrontp);
707 }
708 break;
709 }
710 break;
711
712 case LM_SLC:
713 sprintf(nfrontp, "SLC");
714 nfrontp += strlen(nfrontp);
715 for (i = 2; i < length - 2; i += 3) {
716 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
717 sprintf(nfrontp, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
718 else
719 sprintf(nfrontp, " %d", pointer[i+SLC_FUNC]);
720 nfrontp += strlen(nfrontp);
721 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
722 case SLC_NOSUPPORT:
723 sprintf(nfrontp, " NOSUPPORT"); break;
724 case SLC_CANTCHANGE:
725 sprintf(nfrontp, " CANTCHANGE"); break;
726 case SLC_VARIABLE:
727 sprintf(nfrontp, " VARIABLE"); break;
728 case SLC_DEFAULT:
729 sprintf(nfrontp, " DEFAULT"); break;
730 }
731 nfrontp += strlen(nfrontp);
732 sprintf(nfrontp, "%s%s%s",
733 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
734 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
735 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
736 nfrontp += strlen(nfrontp);
737 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
738 SLC_FLUSHOUT| SLC_LEVELBITS)) {
739 sprintf(nfrontp, "(0x%x)", pointer[i+SLC_FLAGS]);
740 nfrontp += strlen(nfrontp);
741 }
742 sprintf(nfrontp, " %d;", pointer[i+SLC_VALUE]);
743 nfrontp += strlen(nfrontp);
744 if ((pointer[i+SLC_VALUE] == IAC) &&
745 (pointer[i+SLC_VALUE+1] == IAC))
746 i++;
747 }
748 for (; i < length; i++) {
749 sprintf(nfrontp, " ?%d?", pointer[i]);
750 nfrontp += strlen(nfrontp);
751 }
752 break;
753
754 case LM_MODE:
755 sprintf(nfrontp, "MODE ");
756 nfrontp += strlen(nfrontp);
757 if (length < 3) {
758 sprintf(nfrontp, "(no mode??\?)");
759 nfrontp += strlen(nfrontp);
760 break;
761 }
762 {
763 char tbuf[32];
764
765 (void)snprintf(tbuf, sizeof tbuf, "%s%s%s%s%s",
766 pointer[2]&MODE_EDIT ? "|EDIT" : "",
767 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
768 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
769 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
770 pointer[2]&MODE_ACK ? "|ACK" : "");
771 sprintf(nfrontp, "%s", tbuf[1] ? &tbuf[1] : "0");
772 nfrontp += strlen(nfrontp);
773 }
774 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
775 sprintf(nfrontp, " (0x%x)", pointer[2]);
776 nfrontp += strlen(nfrontp);
777 }
778 for (i = 3; i < length; i++) {
779 sprintf(nfrontp, " ?0x%x?", pointer[i]);
780 nfrontp += strlen(nfrontp);
781 }
782 break;
783 default:
784 sprintf(nfrontp, "%d (unknown)", pointer[1]);
785 nfrontp += strlen(nfrontp);
786 for (i = 2; i < length; i++) {
787 sprintf(nfrontp, " %d", pointer[i]);
788 nfrontp += strlen(nfrontp);
789 }
790 }
791 break;
792
793 case TELOPT_STATUS: {
794 register char *cp;
795 register int j, k;
796
797 sprintf(nfrontp, "STATUS");
798 nfrontp += strlen(nfrontp);
799
800 switch (pointer[1]) {
801 default:
802 if (pointer[1] == TELQUAL_SEND)
803 sprintf(nfrontp, " SEND");
804 else
805 sprintf(nfrontp, " %d (unknown)", pointer[1]);
806 nfrontp += strlen(nfrontp);
807 for (i = 2; i < length; i++) {
808 sprintf(nfrontp, " ?%d?", pointer[i]);
809 nfrontp += strlen(nfrontp);
810 }
811 break;
812 case TELQUAL_IS:
813 sprintf(nfrontp, " IS\r\n");
814 nfrontp += strlen(nfrontp);
815
816 for (i = 2; i < length; i++) {
817 switch(pointer[i]) {
818 case DO: cp = "DO"; goto common2;
819 case DONT: cp = "DONT"; goto common2;
820 case WILL: cp = "WILL"; goto common2;
821 case WONT: cp = "WONT"; goto common2;
822 common2:
823 i++;
824 if (TELOPT_OK(pointer[i]))
825 sprintf(nfrontp, " %s %s", cp, TELOPT(pointer[i]));
826 else
827 sprintf(nfrontp, " %s %d", cp, pointer[i]);
828 nfrontp += strlen(nfrontp);
829
830 sprintf(nfrontp, "\r\n");
831 nfrontp += strlen(nfrontp);
832 break;
833
834 case SB:
835 sprintf(nfrontp, " SB ");
836 nfrontp += strlen(nfrontp);
837 i++;
838 j = k = i;
839 while (j < length) {
840 if (pointer[j] == SE) {
841 if (j+1 == length)
842 break;
843 if (pointer[j+1] == SE)
844 j++;
845 else
846 break;
847 }
848 pointer[k++] = pointer[j++];
849 }
850 printsub(0, &pointer[i], k - i);
851 if (i < length) {
852 sprintf(nfrontp, " SE");
853 nfrontp += strlen(nfrontp);
854 i = j;
855 } else
856 i = j - 1;
857
858 sprintf(nfrontp, "\r\n");
859 nfrontp += strlen(nfrontp);
860
861 break;
862
863 default:
864 sprintf(nfrontp, " %d", pointer[i]);
865 nfrontp += strlen(nfrontp);
866 break;
867 }
868 }
869 break;
870 }
871 break;
872 }
873
874 case TELOPT_XDISPLOC:
875 sprintf(nfrontp, "X-DISPLAY-LOCATION ");
876 nfrontp += strlen(nfrontp);
877 switch (pointer[1]) {
878 case TELQUAL_IS:
879 sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2);
880 break;
881 case TELQUAL_SEND:
882 sprintf(nfrontp, "SEND");
883 break;
884 default:
885 sprintf(nfrontp, "- unknown qualifier %d (0x%x).",
886 pointer[1], pointer[1]);
887 }
888 nfrontp += strlen(nfrontp);
889 break;
890
891 case TELOPT_NEW_ENVIRON:
892 sprintf(nfrontp, "NEW-ENVIRON ");
893 goto env_common1;
894 case TELOPT_OLD_ENVIRON:
895 sprintf(nfrontp, "OLD-ENVIRON");
896 env_common1:
897 nfrontp += strlen(nfrontp);
898 switch (pointer[1]) {
899 case TELQUAL_IS:
900 sprintf(nfrontp, "IS ");
901 goto env_common;
902 case TELQUAL_SEND:
903 sprintf(nfrontp, "SEND ");
904 goto env_common;
905 case TELQUAL_INFO:
906 sprintf(nfrontp, "INFO ");
907 env_common:
908 nfrontp += strlen(nfrontp);
909 {
910 register int noquote = 2;
911 for (i = 2; i < length; i++ ) {
912 switch (pointer[i]) {
913 case NEW_ENV_VAR:
914 sprintf(nfrontp, "\" VAR " + noquote);
915 nfrontp += strlen(nfrontp);
916 noquote = 2;
917 break;
918
919 case NEW_ENV_VALUE:
920 sprintf(nfrontp, "\" VALUE " + noquote);
921 nfrontp += strlen(nfrontp);
922 noquote = 2;
923 break;
924
925 case ENV_ESC:
926 sprintf(nfrontp, "\" ESC " + noquote);
927 nfrontp += strlen(nfrontp);
928 noquote = 2;
929 break;
930
931 case ENV_USERVAR:
932 sprintf(nfrontp, "\" USERVAR " + noquote);
933 nfrontp += strlen(nfrontp);
934 noquote = 2;
935 break;
936
937 default:
938 if (isprint(pointer[i]) && pointer[i] != '"') {
939 if (noquote) {
940 *nfrontp++ = '"';
941 noquote = 0;
942 }
943 *nfrontp++ = pointer[i];
944 } else {
945 sprintf(nfrontp, "\" %03o " + noquote,
946 pointer[i]);
947 nfrontp += strlen(nfrontp);
948 noquote = 2;
949 }
950 break;
951 }
952 }
953 if (!noquote)
954 *nfrontp++ = '"';
955 break;
956 }
957 }
958 break;
959
960 #if defined(AUTHENTICATION)
961 case TELOPT_AUTHENTICATION:
962 sprintf(nfrontp, "AUTHENTICATION");
963 nfrontp += strlen(nfrontp);
964
965 if (length < 2) {
966 sprintf(nfrontp, " (empty suboption??\?)");
967 nfrontp += strlen(nfrontp);
968 break;
969 }
970 switch (pointer[1]) {
971 case TELQUAL_REPLY:
972 case TELQUAL_IS:
973 sprintf(nfrontp, " %s ", (pointer[1] == TELQUAL_IS) ?
974 "IS" : "REPLY");
975 nfrontp += strlen(nfrontp);
976 if (AUTHTYPE_NAME_OK(pointer[2]))
977 sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[2]));
978 else
979 sprintf(nfrontp, "%d ", pointer[2]);
980 nfrontp += strlen(nfrontp);
981 if (length < 3) {
982 sprintf(nfrontp, "(partial suboption??\?)");
983 nfrontp += strlen(nfrontp);
984 break;
985 }
986 sprintf(nfrontp, "%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");
991 nfrontp += strlen(nfrontp);
992
993 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
994 sprintf(nfrontp, "%s", buf);
995 nfrontp += strlen(nfrontp);
996 break;
997
998 case TELQUAL_SEND:
999 i = 2;
1000 sprintf(nfrontp, " SEND ");
1001 nfrontp += strlen(nfrontp);
1002 while (i < length) {
1003 if (AUTHTYPE_NAME_OK(pointer[i]))
1004 sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[i]));
1005 else
1006 sprintf(nfrontp, "%d ", pointer[i]);
1007 nfrontp += strlen(nfrontp);
1008 if (++i >= length) {
1009 sprintf(nfrontp, "(partial suboption??\?)");
1010 nfrontp += strlen(nfrontp);
1011 break;
1012 }
1013 sprintf(nfrontp, "%s|%s ",
1014 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1015 "CLIENT" : "SERVER",
1016 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1017 "MUTUAL" : "ONE-WAY");
1018 nfrontp += strlen(nfrontp);
1019 ++i;
1020 }
1021 break;
1022
1023 case TELQUAL_NAME:
1024 i = 2;
1025 sprintf(nfrontp, " NAME \"");
1026 nfrontp += strlen(nfrontp);
1027 while (i < length)
1028 *nfrontp += pointer[i++];
1029 *nfrontp += '"';
1030 break;
1031
1032 default:
1033 for (i = 2; i < length; i++) {
1034 sprintf(nfrontp, " ?%d?", pointer[i]);
1035 nfrontp += strlen(nfrontp);
1036 }
1037 break;
1038 }
1039 break;
1040 #endif
1041
1042
1043 default:
1044 if (TELOPT_OK(pointer[0]))
1045 sprintf(nfrontp, "%s (unknown)", TELOPT(pointer[0]));
1046 else
1047 sprintf(nfrontp, "%d (unknown)", pointer[i]);
1048 nfrontp += strlen(nfrontp);
1049 for (i = 1; i < length; i++) {
1050 sprintf(nfrontp, " %d", pointer[i]);
1051 nfrontp += strlen(nfrontp);
1052 }
1053 break;
1054 }
1055 sprintf(nfrontp, "\r\n");
1056 nfrontp += strlen(nfrontp);
1057 }
1058
1059 /*
1060 * Dump a data buffer in hex and ascii to the output data stream.
1061 */
1062 void
1063 printdata(tag, ptr, cnt)
1064 register char *tag;
1065 register char *ptr;
1066 register int cnt;
1067 {
1068 register int i;
1069 char xbuf[30];
1070
1071 while (cnt) {
1072 /* flush net output buffer if no room for new data) */
1073 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1074 netflush();
1075 }
1076
1077 /* add a line of output */
1078 sprintf(nfrontp, "%s: ", tag);
1079 nfrontp += strlen(nfrontp);
1080 for (i = 0; i < 20 && cnt; i++) {
1081 sprintf(nfrontp, "%02x", *ptr);
1082 nfrontp += strlen(nfrontp);
1083 if (isprint(*ptr)) {
1084 xbuf[i] = *ptr;
1085 } else {
1086 xbuf[i] = '.';
1087 }
1088 if (i % 2) {
1089 *nfrontp = ' ';
1090 nfrontp++;
1091 }
1092 cnt--;
1093 ptr++;
1094 }
1095 xbuf[i] = '\0';
1096 sprintf(nfrontp, " %s\r\n", xbuf );
1097 nfrontp += strlen(nfrontp);
1098 }
1099 }
1100 #endif /* DIAGNOSTICS */
1101