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