utility.c revision 1.17 1 /* $NetBSD: utility.c,v 1.17 2001/02/04 22:32:17 christos 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.17 2001/02/04 22:32:17 christos 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 putstr __P((char *));
51
52 extern int not42;
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_ERR, "ttloop: read: %m");
79 exit(1);
80 } else if (ncc == 0) {
81 syslog(LOG_ERR, "ttloop: peer died: %m");
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 #ifdef ENCRYPTION
209 thisitem = nclearto > netobuf ? nclearto : netobuf;
210 #else /* ENCRYPTION */
211 thisitem = netobuf;
212 #endif /* ENCRYPTION */
213
214 while ((next = nextitem(thisitem)) <= nbackp) {
215 thisitem = next;
216 }
217
218 /* Now, thisitem is first before/at boundary. */
219
220 #ifdef ENCRYPTION
221 good = nclearto > netobuf ? nclearto : netobuf;
222 #else /* ENCRYPTION */
223 good = netobuf; /* where the good bytes go */
224 #endif /* ENCRYPTION */
225
226 while (nfrontp > thisitem) {
227 if (wewant(thisitem)) {
228 int length;
229
230 next = thisitem;
231 do {
232 next = nextitem(next);
233 } while (wewant(next) && (nfrontp > next));
234 length = next-thisitem;
235 memmove(good, thisitem, length);
236 good += length;
237 thisitem = next;
238 } else {
239 thisitem = nextitem(thisitem);
240 }
241 }
242
243 nbackp = netobuf;
244 nfrontp = good; /* next byte to be sent */
245 neturg = 0;
246 } /* end of netclear */
247
248 /*
249 * netflush
250 * Send as much data as possible to the network,
251 * handling requests for urgent data.
252 */
253 void
254 netflush()
255 {
256 int n;
257
258 if ((n = nfrontp - nbackp) > 0) {
259 DIAG(TD_REPORT,
260 { sprintf(nfrontp, "td: netflush %d chars\r\n", n);
261 n += strlen(nfrontp); /* get count first */
262 nfrontp += strlen(nfrontp); /* then move pointer */
263 });
264 #ifdef ENCRYPTION
265 if (encrypt_output) {
266 char *s = nclearto ? nclearto : nbackp;
267 if (nfrontp - s > 0) {
268 (*encrypt_output)((unsigned char *)s, nfrontp-s);
269 nclearto = nfrontp;
270 }
271 }
272 #endif /* ENCRYPTION */
273 /*
274 * if no urgent data, or if the other side appears to be an
275 * old 4.2 client (and thus unable to survive TCP urgent data),
276 * write the entire buffer in non-OOB mode.
277 */
278 if ((neturg == 0) || (not42 == 0)) {
279 n = write(net, nbackp, n); /* normal write */
280 } else {
281 n = neturg - nbackp;
282 /*
283 * In 4.2 (and 4.3) systems, there is some question about
284 * what byte in a sendOOB operation is the "OOB" data.
285 * To make ourselves compatible, we only send ONE byte
286 * out of band, the one WE THINK should be OOB (though
287 * we really have more the TCP philosophy of urgent data
288 * rather than the Unix philosophy of OOB data).
289 */
290 if (n > 1) {
291 n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */
292 } else {
293 n = send(net, nbackp, n, MSG_OOB); /* URGENT data */
294 }
295 }
296 }
297 if (n < 0) {
298 if (errno == EWOULDBLOCK || errno == EINTR)
299 return;
300 cleanup(0);
301 }
302 nbackp += n;
303 #ifdef ENCRYPTION
304 if (nbackp > nclearto)
305 nclearto = 0;
306 #endif /* ENCRYPTION */
307 if (nbackp >= neturg) {
308 neturg = 0;
309 }
310 if (nbackp == nfrontp) {
311 nbackp = nfrontp = netobuf;
312 #ifdef ENCRYPTION
313 nclearto = 0;
314 #endif /* ENCRYPTION */
315 }
316 return;
317 } /* end of netflush */
318
319
320 /*
321 * writenet
322 *
323 * Just a handy little function to write a bit of raw data to the net.
324 * It will force a transmit of the buffer if necessary
325 *
326 * arguments
327 * ptr - A pointer to a character string to write
328 * len - How many bytes to write
329 */
330 void
331 writenet(ptr, len)
332 register unsigned char *ptr;
333 register int len;
334 {
335 /* flush buffer if no room for new data) */
336 if ((&netobuf[BUFSIZ] - nfrontp) < len) {
337 /* if this fails, don't worry, buffer is a little big */
338 netflush();
339 }
340
341 memmove(nfrontp, ptr, len);
342 nfrontp += len;
343
344 } /* end of writenet */
345
346
347 /*
348 * miscellaneous functions doing a variety of little jobs follow ...
349 */
350
351
352 void
353 fatal(f, msg)
354 int f;
355 char *msg;
356 {
357 char buf[BUFSIZ];
358
359 (void)snprintf(buf, sizeof buf, "telnetd: %s.\r\n", msg);
360 #ifdef ENCRYPTION
361 if (encrypt_output) {
362 /*
363 * Better turn off encryption first....
364 * Hope it flushes...
365 */
366 encrypt_send_end();
367 netflush();
368 }
369 #endif /* ENCRYPTION */
370 (void)write(f, buf, (int)strlen(buf));
371 sleep(1); /*XXX*/
372 exit(1);
373 }
374
375 void
376 fatalperror(f, msg)
377 int f;
378 char *msg;
379 {
380 char buf[BUFSIZ];
381
382 (void)snprintf(buf, sizeof buf, "%s: %s", msg, strerror(errno));
383 fatal(f, buf);
384 }
385
386 char editedhost[MAXHOSTNAMELEN];
387
388 void
389 edithost(pat, host)
390 register char *pat;
391 register char *host;
392 {
393 register char *res = editedhost;
394
395 if (!pat)
396 pat = "";
397 while (*pat) {
398 switch (*pat) {
399
400 case '#':
401 if (*host)
402 host++;
403 break;
404
405 case '@':
406 if (*host)
407 *res++ = *host++;
408 break;
409
410 default:
411 *res++ = *pat;
412 break;
413 }
414 if (res == &editedhost[sizeof editedhost - 1]) {
415 *res = '\0';
416 return;
417 }
418 pat++;
419 }
420 if (*host)
421 (void) strncpy(res, host,
422 sizeof editedhost - (res - editedhost) -1);
423 else
424 *res = '\0';
425 editedhost[sizeof editedhost - 1] = '\0';
426 }
427
428 static char *putlocation;
429
430 void
431 putstr(s)
432 register char *s;
433 {
434
435 while (*s)
436 putchr(*s++);
437 }
438
439 void
440 putchr(cc)
441 int cc;
442 {
443 *putlocation++ = cc;
444 }
445
446 /*
447 * This is split on two lines so that SCCS will not see the M
448 * between two % signs and expand it...
449 */
450 static char fmtstr[] = { "%l:%M\
451 %p on %A, %d %B %Y" };
452
453 char *
454 putf(cp, where)
455 register char *cp;
456 char *where;
457 {
458 char *slash;
459 time_t t;
460 char db[100];
461 struct utsname utsinfo;
462
463 uname(&utsinfo);
464
465 putlocation = where;
466
467 while (*cp) {
468 if (*cp != '%') {
469 putchr(*cp++);
470 continue;
471 }
472 switch (*++cp) {
473
474 case 't':
475 #ifdef STREAMSPTY
476 /* names are like /dev/pts/2 -- we want pts/2 */
477 slash = strchr(line+1, '/');
478 #else
479 slash = strrchr(line, '/');
480 #endif
481 if (slash == (char *) 0)
482 putstr(line);
483 else
484 putstr(&slash[1]);
485 break;
486
487 case 'h':
488 putstr(editedhost);
489 break;
490
491 case 'd':
492 (void)time(&t);
493 (void)strftime(db, sizeof(db), fmtstr, localtime(&t));
494 putstr(db);
495 break;
496
497 case '%':
498 putchr('%');
499 break;
500
501 case 's':
502 putstr(utsinfo.sysname);
503 break;
504
505 case 'm':
506 putstr(utsinfo.machine);
507 break;
508
509 case 'r':
510 putstr(utsinfo.release);
511 break;
512
513 case 'v':
514 puts(utsinfo.version);
515 break;
516 }
517 cp++;
518 }
519
520 return (putlocation);
521 }
522
523 #ifdef DIAGNOSTICS
524 /*
525 * Print telnet options and commands in plain text, if possible.
526 */
527 void
528 printoption(fmt, option)
529 register char *fmt;
530 register int option;
531 {
532 if (TELOPT_OK(option))
533 sprintf(nfrontp, "%s %s\r\n", fmt, TELOPT(option));
534 else if (TELCMD_OK(option))
535 sprintf(nfrontp, "%s %s\r\n", fmt, TELCMD(option));
536 else
537 sprintf(nfrontp, "%s %d\r\n", fmt, option);
538 nfrontp += strlen(nfrontp);
539 return;
540 }
541
542 void
543 printsub(direction, pointer, length)
544 char direction; /* '<' or '>' */
545 unsigned char *pointer; /* where suboption data sits */
546 int length; /* length of suboption data */
547 {
548 register int i = 0; /* XXX gcc */
549 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
550 char buf[512];
551 #endif
552
553 if (!(diagnostic & TD_OPTIONS))
554 return;
555
556 if (direction) {
557 sprintf(nfrontp, "td: %s suboption ",
558 direction == '<' ? "recv" : "send");
559 nfrontp += strlen(nfrontp);
560 if (length >= 3) {
561 register int j;
562
563 i = pointer[length-2];
564 j = pointer[length-1];
565
566 if (i != IAC || j != SE) {
567 sprintf(nfrontp, "(terminated by ");
568 nfrontp += strlen(nfrontp);
569 if (TELOPT_OK(i))
570 sprintf(nfrontp, "%s ", TELOPT(i));
571 else if (TELCMD_OK(i))
572 sprintf(nfrontp, "%s ", TELCMD(i));
573 else
574 sprintf(nfrontp, "%d ", i);
575 nfrontp += strlen(nfrontp);
576 if (TELOPT_OK(j))
577 sprintf(nfrontp, "%s", TELOPT(j));
578 else if (TELCMD_OK(j))
579 sprintf(nfrontp, "%s", TELCMD(j));
580 else
581 sprintf(nfrontp, "%d", j);
582 nfrontp += strlen(nfrontp);
583 sprintf(nfrontp, ", not IAC SE!) ");
584 nfrontp += strlen(nfrontp);
585 }
586 }
587 length -= 2;
588 }
589 if (length < 1) {
590 sprintf(nfrontp, "(Empty suboption??\?)");
591 nfrontp += strlen(nfrontp);
592 return;
593 }
594 switch (pointer[0]) {
595 case TELOPT_TTYPE:
596 sprintf(nfrontp, "TERMINAL-TYPE ");
597 nfrontp += strlen(nfrontp);
598 switch (pointer[1]) {
599 case TELQUAL_IS:
600 sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2);
601 break;
602 case TELQUAL_SEND:
603 sprintf(nfrontp, "SEND");
604 break;
605 default:
606 sprintf(nfrontp,
607 "- unknown qualifier %d (0x%x).",
608 pointer[1], pointer[1]);
609 }
610 nfrontp += strlen(nfrontp);
611 break;
612 case TELOPT_TSPEED:
613 sprintf(nfrontp, "TERMINAL-SPEED");
614 nfrontp += strlen(nfrontp);
615 if (length < 2) {
616 sprintf(nfrontp, " (empty suboption??\?)");
617 nfrontp += strlen(nfrontp);
618 break;
619 }
620 switch (pointer[1]) {
621 case TELQUAL_IS:
622 sprintf(nfrontp, " IS %.*s", length-2, (char *)pointer+2);
623 nfrontp += strlen(nfrontp);
624 break;
625 default:
626 if (pointer[1] == 1)
627 sprintf(nfrontp, " SEND");
628 else
629 sprintf(nfrontp, " %d (unknown)", pointer[1]);
630 nfrontp += strlen(nfrontp);
631 for (i = 2; i < length; i++) {
632 sprintf(nfrontp, " ?%d?", pointer[i]);
633 nfrontp += strlen(nfrontp);
634 }
635 break;
636 }
637 break;
638
639 case TELOPT_LFLOW:
640 sprintf(nfrontp, "TOGGLE-FLOW-CONTROL");
641 nfrontp += strlen(nfrontp);
642 if (length < 2) {
643 sprintf(nfrontp, " (empty suboption??\?)");
644 nfrontp += strlen(nfrontp);
645 break;
646 }
647 switch (pointer[1]) {
648 case LFLOW_OFF:
649 sprintf(nfrontp, " OFF"); break;
650 case LFLOW_ON:
651 sprintf(nfrontp, " ON"); break;
652 case LFLOW_RESTART_ANY:
653 sprintf(nfrontp, " RESTART-ANY"); break;
654 case LFLOW_RESTART_XON:
655 sprintf(nfrontp, " RESTART-XON"); break;
656 default:
657 sprintf(nfrontp, " %d (unknown)", pointer[1]);
658 }
659 nfrontp += strlen(nfrontp);
660 for (i = 2; i < length; i++) {
661 sprintf(nfrontp, " ?%d?", pointer[i]);
662 nfrontp += strlen(nfrontp);
663 }
664 break;
665
666 case TELOPT_NAWS:
667 sprintf(nfrontp, "NAWS");
668 nfrontp += strlen(nfrontp);
669 if (length < 2) {
670 sprintf(nfrontp, " (empty suboption??\?)");
671 nfrontp += strlen(nfrontp);
672 break;
673 }
674 if (length == 2) {
675 sprintf(nfrontp, " ?%d?", pointer[1]);
676 nfrontp += strlen(nfrontp);
677 break;
678 }
679 sprintf(nfrontp, " %d %d (%d)",
680 pointer[1], pointer[2],
681 (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
682 nfrontp += strlen(nfrontp);
683 if (length == 4) {
684 sprintf(nfrontp, " ?%d?", pointer[3]);
685 nfrontp += strlen(nfrontp);
686 break;
687 }
688 sprintf(nfrontp, " %d %d (%d)",
689 pointer[3], pointer[4],
690 (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
691 nfrontp += strlen(nfrontp);
692 for (i = 5; i < length; i++) {
693 sprintf(nfrontp, " ?%d?", pointer[i]);
694 nfrontp += strlen(nfrontp);
695 }
696 break;
697
698 case TELOPT_LINEMODE:
699 sprintf(nfrontp, "LINEMODE ");
700 nfrontp += strlen(nfrontp);
701 if (length < 2) {
702 sprintf(nfrontp, " (empty suboption??\?)");
703 nfrontp += strlen(nfrontp);
704 break;
705 }
706 switch (pointer[1]) {
707 case WILL:
708 sprintf(nfrontp, "WILL ");
709 goto common;
710 case WONT:
711 sprintf(nfrontp, "WONT ");
712 goto common;
713 case DO:
714 sprintf(nfrontp, "DO ");
715 goto common;
716 case DONT:
717 sprintf(nfrontp, "DONT ");
718 common:
719 nfrontp += strlen(nfrontp);
720 if (length < 3) {
721 sprintf(nfrontp, "(no option??\?)");
722 nfrontp += strlen(nfrontp);
723 break;
724 }
725 switch (pointer[2]) {
726 case LM_FORWARDMASK:
727 sprintf(nfrontp, "Forward Mask");
728 nfrontp += strlen(nfrontp);
729 for (i = 3; i < length; i++) {
730 sprintf(nfrontp, " %x", pointer[i]);
731 nfrontp += strlen(nfrontp);
732 }
733 break;
734 default:
735 sprintf(nfrontp, "%d (unknown)", pointer[2]);
736 nfrontp += strlen(nfrontp);
737 for (i = 3; i < length; i++) {
738 sprintf(nfrontp, " %d", pointer[i]);
739 nfrontp += strlen(nfrontp);
740 }
741 break;
742 }
743 break;
744
745 case LM_SLC:
746 sprintf(nfrontp, "SLC");
747 nfrontp += strlen(nfrontp);
748 for (i = 2; i < length - 2; i += 3) {
749 if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
750 sprintf(nfrontp, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
751 else
752 sprintf(nfrontp, " %d", pointer[i+SLC_FUNC]);
753 nfrontp += strlen(nfrontp);
754 switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
755 case SLC_NOSUPPORT:
756 sprintf(nfrontp, " NOSUPPORT"); break;
757 case SLC_CANTCHANGE:
758 sprintf(nfrontp, " CANTCHANGE"); break;
759 case SLC_VARIABLE:
760 sprintf(nfrontp, " VARIABLE"); break;
761 case SLC_DEFAULT:
762 sprintf(nfrontp, " DEFAULT"); break;
763 }
764 nfrontp += strlen(nfrontp);
765 sprintf(nfrontp, "%s%s%s",
766 pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
767 pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
768 pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
769 nfrontp += strlen(nfrontp);
770 if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
771 SLC_FLUSHOUT| SLC_LEVELBITS)) {
772 sprintf(nfrontp, "(0x%x)", pointer[i+SLC_FLAGS]);
773 nfrontp += strlen(nfrontp);
774 }
775 sprintf(nfrontp, " %d;", pointer[i+SLC_VALUE]);
776 nfrontp += strlen(nfrontp);
777 if ((pointer[i+SLC_VALUE] == IAC) &&
778 (pointer[i+SLC_VALUE+1] == IAC))
779 i++;
780 }
781 for (; i < length; i++) {
782 sprintf(nfrontp, " ?%d?", pointer[i]);
783 nfrontp += strlen(nfrontp);
784 }
785 break;
786
787 case LM_MODE:
788 sprintf(nfrontp, "MODE ");
789 nfrontp += strlen(nfrontp);
790 if (length < 3) {
791 sprintf(nfrontp, "(no mode??\?)");
792 nfrontp += strlen(nfrontp);
793 break;
794 }
795 {
796 char tbuf[32];
797
798 (void)snprintf(tbuf, sizeof tbuf, "%s%s%s%s%s",
799 pointer[2]&MODE_EDIT ? "|EDIT" : "",
800 pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
801 pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
802 pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
803 pointer[2]&MODE_ACK ? "|ACK" : "");
804 sprintf(nfrontp, "%s", tbuf[1] ? &tbuf[1] : "0");
805 nfrontp += strlen(nfrontp);
806 }
807 if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
808 sprintf(nfrontp, " (0x%x)", pointer[2]);
809 nfrontp += strlen(nfrontp);
810 }
811 for (i = 3; i < length; i++) {
812 sprintf(nfrontp, " ?0x%x?", pointer[i]);
813 nfrontp += strlen(nfrontp);
814 }
815 break;
816 default:
817 sprintf(nfrontp, "%d (unknown)", pointer[1]);
818 nfrontp += strlen(nfrontp);
819 for (i = 2; i < length; i++) {
820 sprintf(nfrontp, " %d", pointer[i]);
821 nfrontp += strlen(nfrontp);
822 }
823 }
824 break;
825
826 case TELOPT_STATUS: {
827 register char *cp;
828 register int j, k;
829
830 sprintf(nfrontp, "STATUS");
831 nfrontp += strlen(nfrontp);
832
833 switch (pointer[1]) {
834 default:
835 if (pointer[1] == TELQUAL_SEND)
836 sprintf(nfrontp, " SEND");
837 else
838 sprintf(nfrontp, " %d (unknown)", pointer[1]);
839 nfrontp += strlen(nfrontp);
840 for (i = 2; i < length; i++) {
841 sprintf(nfrontp, " ?%d?", pointer[i]);
842 nfrontp += strlen(nfrontp);
843 }
844 break;
845 case TELQUAL_IS:
846 sprintf(nfrontp, " IS\r\n");
847 nfrontp += strlen(nfrontp);
848
849 for (i = 2; i < length; i++) {
850 switch(pointer[i]) {
851 case DO: cp = "DO"; goto common2;
852 case DONT: cp = "DONT"; goto common2;
853 case WILL: cp = "WILL"; goto common2;
854 case WONT: cp = "WONT"; goto common2;
855 common2:
856 i++;
857 if (TELOPT_OK(pointer[i]))
858 sprintf(nfrontp, " %s %s", cp, TELOPT(pointer[i]));
859 else
860 sprintf(nfrontp, " %s %d", cp, pointer[i]);
861 nfrontp += strlen(nfrontp);
862
863 sprintf(nfrontp, "\r\n");
864 nfrontp += strlen(nfrontp);
865 break;
866
867 case SB:
868 sprintf(nfrontp, " SB ");
869 nfrontp += strlen(nfrontp);
870 i++;
871 j = k = i;
872 while (j < length) {
873 if (pointer[j] == SE) {
874 if (j+1 == length)
875 break;
876 if (pointer[j+1] == SE)
877 j++;
878 else
879 break;
880 }
881 pointer[k++] = pointer[j++];
882 }
883 printsub(0, &pointer[i], k - i);
884 if (i < length) {
885 sprintf(nfrontp, " SE");
886 nfrontp += strlen(nfrontp);
887 i = j;
888 } else
889 i = j - 1;
890
891 sprintf(nfrontp, "\r\n");
892 nfrontp += strlen(nfrontp);
893
894 break;
895
896 default:
897 sprintf(nfrontp, " %d", pointer[i]);
898 nfrontp += strlen(nfrontp);
899 break;
900 }
901 }
902 break;
903 }
904 break;
905 }
906
907 case TELOPT_XDISPLOC:
908 sprintf(nfrontp, "X-DISPLAY-LOCATION ");
909 nfrontp += strlen(nfrontp);
910 switch (pointer[1]) {
911 case TELQUAL_IS:
912 sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2);
913 break;
914 case TELQUAL_SEND:
915 sprintf(nfrontp, "SEND");
916 break;
917 default:
918 sprintf(nfrontp, "- unknown qualifier %d (0x%x).",
919 pointer[1], pointer[1]);
920 }
921 nfrontp += strlen(nfrontp);
922 break;
923
924 case TELOPT_NEW_ENVIRON:
925 sprintf(nfrontp, "NEW-ENVIRON ");
926 goto env_common1;
927 case TELOPT_OLD_ENVIRON:
928 sprintf(nfrontp, "OLD-ENVIRON");
929 env_common1:
930 nfrontp += strlen(nfrontp);
931 switch (pointer[1]) {
932 case TELQUAL_IS:
933 sprintf(nfrontp, "IS ");
934 goto env_common;
935 case TELQUAL_SEND:
936 sprintf(nfrontp, "SEND ");
937 goto env_common;
938 case TELQUAL_INFO:
939 sprintf(nfrontp, "INFO ");
940 env_common:
941 nfrontp += strlen(nfrontp);
942 {
943 register int noquote = 2;
944 for (i = 2; i < length; i++ ) {
945 switch (pointer[i]) {
946 case NEW_ENV_VAR:
947 sprintf(nfrontp, "%s", "\" VAR " + noquote);
948 nfrontp += strlen(nfrontp);
949 noquote = 2;
950 break;
951
952 case NEW_ENV_VALUE:
953 sprintf(nfrontp, "%s", "\" VALUE " + noquote);
954 nfrontp += strlen(nfrontp);
955 noquote = 2;
956 break;
957
958 case ENV_ESC:
959 sprintf(nfrontp, "%s", "\" ESC " + noquote);
960 nfrontp += strlen(nfrontp);
961 noquote = 2;
962 break;
963
964 case ENV_USERVAR:
965 sprintf(nfrontp, "%s", "\" USERVAR " + noquote);
966 nfrontp += strlen(nfrontp);
967 noquote = 2;
968 break;
969
970 default:
971 if (isprint(pointer[i]) && pointer[i] != '"') {
972 if (noquote) {
973 *nfrontp++ = '"';
974 noquote = 0;
975 }
976 *nfrontp++ = pointer[i];
977 } else {
978 sprintf(nfrontp, "\" %03o " + noquote,
979 pointer[i]);
980 nfrontp += strlen(nfrontp);
981 noquote = 2;
982 }
983 break;
984 }
985 }
986 if (!noquote)
987 *nfrontp++ = '"';
988 break;
989 }
990 }
991 break;
992
993 #if defined(AUTHENTICATION)
994 case TELOPT_AUTHENTICATION:
995 sprintf(nfrontp, "AUTHENTICATION");
996 nfrontp += strlen(nfrontp);
997
998 if (length < 2) {
999 sprintf(nfrontp, " (empty suboption??\?)");
1000 nfrontp += strlen(nfrontp);
1001 break;
1002 }
1003 switch (pointer[1]) {
1004 case TELQUAL_REPLY:
1005 case TELQUAL_IS:
1006 sprintf(nfrontp, " %s ", (pointer[1] == TELQUAL_IS) ?
1007 "IS" : "REPLY");
1008 nfrontp += strlen(nfrontp);
1009 if (AUTHTYPE_NAME_OK(pointer[2]))
1010 sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[2]));
1011 else
1012 sprintf(nfrontp, "%d ", pointer[2]);
1013 nfrontp += strlen(nfrontp);
1014 if (length < 3) {
1015 sprintf(nfrontp, "(partial suboption??\?)");
1016 nfrontp += strlen(nfrontp);
1017 break;
1018 }
1019 sprintf(nfrontp, "%s|%s",
1020 ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1021 "CLIENT" : "SERVER",
1022 ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1023 "MUTUAL" : "ONE-WAY");
1024 nfrontp += strlen(nfrontp);
1025
1026 auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1027 sprintf(nfrontp, "%s", buf);
1028 nfrontp += strlen(nfrontp);
1029 break;
1030
1031 case TELQUAL_SEND:
1032 i = 2;
1033 sprintf(nfrontp, " SEND ");
1034 nfrontp += strlen(nfrontp);
1035 while (i < length) {
1036 if (AUTHTYPE_NAME_OK(pointer[i]))
1037 sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[i]));
1038 else
1039 sprintf(nfrontp, "%d ", pointer[i]);
1040 nfrontp += strlen(nfrontp);
1041 if (++i >= length) {
1042 sprintf(nfrontp, "(partial suboption??\?)");
1043 nfrontp += strlen(nfrontp);
1044 break;
1045 }
1046 sprintf(nfrontp, "%s|%s ",
1047 ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
1048 "CLIENT" : "SERVER",
1049 ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
1050 "MUTUAL" : "ONE-WAY");
1051 nfrontp += strlen(nfrontp);
1052 ++i;
1053 }
1054 break;
1055
1056 case TELQUAL_NAME:
1057 i = 2;
1058 sprintf(nfrontp, " NAME \"");
1059 nfrontp += strlen(nfrontp);
1060 while (i < length) {
1061 if (isprint(pointer[i]))
1062 *nfrontp += pointer[i++];
1063 else {
1064 sprintf(nfrontp, "\"%03o\"",pointer[i++]);
1065 nfrontp += strlen(nfrontp);
1066 }
1067 }
1068 *nfrontp += '"';
1069 break;
1070
1071 default:
1072 for (i = 2; i < length; i++) {
1073 sprintf(nfrontp, " ?%d?", pointer[i]);
1074 nfrontp += strlen(nfrontp);
1075 }
1076 break;
1077 }
1078 break;
1079 #endif
1080
1081 #ifdef ENCRYPTION
1082 case TELOPT_ENCRYPT:
1083 sprintf(nfrontp, "ENCRYPT");
1084 nfrontp += strlen(nfrontp);
1085 if (length < 2) {
1086 sprintf(nfrontp, " (empty suboption??\?)");
1087 nfrontp += strlen(nfrontp);
1088 break;
1089 }
1090 switch (pointer[1]) {
1091 case ENCRYPT_START:
1092 sprintf(nfrontp, " START");
1093 nfrontp += strlen(nfrontp);
1094 break;
1095
1096 case ENCRYPT_END:
1097 sprintf(nfrontp, " END");
1098 nfrontp += strlen(nfrontp);
1099 break;
1100
1101 case ENCRYPT_REQSTART:
1102 sprintf(nfrontp, " REQUEST-START");
1103 nfrontp += strlen(nfrontp);
1104 break;
1105
1106 case ENCRYPT_REQEND:
1107 sprintf(nfrontp, " REQUEST-END");
1108 nfrontp += strlen(nfrontp);
1109 break;
1110
1111 case ENCRYPT_IS:
1112 case ENCRYPT_REPLY:
1113 sprintf(nfrontp, " %s ", (pointer[1] == ENCRYPT_IS) ?
1114 "IS" : "REPLY");
1115 nfrontp += strlen(nfrontp);
1116 if (length < 3) {
1117 sprintf(nfrontp, " (partial suboption??\?)");
1118 nfrontp += strlen(nfrontp);
1119 break;
1120 }
1121 if (ENCTYPE_NAME_OK(pointer[2]))
1122 sprintf(nfrontp, "%s ", ENCTYPE_NAME(pointer[2]));
1123 else
1124 sprintf(nfrontp, " %d (unknown)", pointer[2]);
1125 nfrontp += strlen(nfrontp);
1126
1127 encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
1128 sprintf(nfrontp, "%s", buf);
1129 nfrontp += strlen(nfrontp);
1130 break;
1131
1132 case ENCRYPT_SUPPORT:
1133 i = 2;
1134 sprintf(nfrontp, " SUPPORT ");
1135 nfrontp += strlen(nfrontp);
1136 while (i < length) {
1137 if (ENCTYPE_NAME_OK(pointer[i]))
1138 sprintf(nfrontp, "%s ",
1139 ENCTYPE_NAME(pointer[i]));
1140 else
1141 sprintf(nfrontp, "%d ", pointer[i]);
1142 nfrontp += strlen(nfrontp);
1143 i++;
1144 }
1145 break;
1146
1147 case ENCRYPT_ENC_KEYID:
1148 sprintf(nfrontp, " ENC_KEYID");
1149 nfrontp += strlen(nfrontp);
1150 goto encommon;
1151
1152 case ENCRYPT_DEC_KEYID:
1153 sprintf(nfrontp, " DEC_KEYID");
1154 nfrontp += strlen(nfrontp);
1155 goto encommon;
1156
1157 default:
1158 sprintf(nfrontp, " %d (unknown)", pointer[1]);
1159 nfrontp += strlen(nfrontp);
1160 encommon:
1161 for (i = 2; i < length; i++) {
1162 sprintf(nfrontp, " %d", pointer[i]);
1163 nfrontp += strlen(nfrontp);
1164 }
1165 break;
1166 }
1167 break;
1168 #endif /* ENCRYPTION */
1169
1170 default:
1171 if (TELOPT_OK(pointer[0]))
1172 sprintf(nfrontp, "%s (unknown)", TELOPT(pointer[0]));
1173 else
1174 sprintf(nfrontp, "%d (unknown)", pointer[i]);
1175 nfrontp += strlen(nfrontp);
1176 for (i = 1; i < length; i++) {
1177 sprintf(nfrontp, " %d", pointer[i]);
1178 nfrontp += strlen(nfrontp);
1179 }
1180 break;
1181 }
1182 sprintf(nfrontp, "\r\n");
1183 nfrontp += strlen(nfrontp);
1184 }
1185
1186 /*
1187 * Dump a data buffer in hex and ascii to the output data stream.
1188 */
1189 void
1190 printdata(tag, ptr, cnt)
1191 register char *tag;
1192 register char *ptr;
1193 register int cnt;
1194 {
1195 register int i;
1196 char xbuf[30];
1197
1198 while (cnt) {
1199 /* flush net output buffer if no room for new data) */
1200 if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
1201 netflush();
1202 }
1203
1204 /* add a line of output */
1205 sprintf(nfrontp, "%s: ", tag);
1206 nfrontp += strlen(nfrontp);
1207 for (i = 0; i < 20 && cnt; i++) {
1208 sprintf(nfrontp, "%02x", *ptr);
1209 nfrontp += strlen(nfrontp);
1210 if (isprint(*ptr)) {
1211 xbuf[i] = *ptr;
1212 } else {
1213 xbuf[i] = '.';
1214 }
1215 if (i % 2) {
1216 *nfrontp = ' ';
1217 nfrontp++;
1218 }
1219 cnt--;
1220 ptr++;
1221 }
1222 xbuf[i] = '\0';
1223 sprintf(nfrontp, " %s\r\n", xbuf );
1224 nfrontp += strlen(nfrontp);
1225 }
1226 }
1227 #endif /* DIAGNOSTICS */
1228