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