1 /* $OpenBSD: commands.c,v 1.48 2005/02/27 15:46:42 otto Exp $ */
2 /* $NetBSD: commands.c,v 1.14 1996/03/24 22:03:48 jtk Exp $ */
3
4 /*
5 * Copyright (c) 1988, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "telnet_locl.h"
34 #include <err.h>
35
36 __RCSID("$MirOS: src/usr.bin/telnet/commands.c,v 1.3 2007/08/24 14:20:12 tg Exp $");
37
38 #if defined(IPPROTO_IP) && defined(IP_TOS)
39 int tos = -1;
40 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
41
42 char *hostname;
43 static char _hostname[MAXHOSTNAMELEN];
44
45 typedef int (*intrtn_t)(int, char**);
46 static int call(intrtn_t, ...);
47
48 typedef struct {
49 char *name; /* command name */
50 char *help; /* help string (NULL for no help) */
51 int (*handler)(); /* routine which executes command */
52 int needconnect; /* Do we need to be connected to execute? */
53 } Command;
54
55 static char line[256];
56 static char saveline[256];
57 static int margc;
58 static char *margv[20];
59
60 #include <sys/wait.h>
61 #define PATH_SKEY "/usr/bin/skey"
62 int
skey_calc(argc,argv)63 skey_calc(argc, argv)
64 int argc;
65 char **argv;
66 {
67 int status;
68
69 if(argc != 3) {
70 printf("usage: %s sequence challenge\n", argv[0]);
71 return 0;
72 }
73
74 switch(fork()) {
75 case 0:
76 execv(PATH_SKEY, argv);
77 exit (1);
78 case -1:
79 err(1, "fork");
80 break;
81 default:
82 (void) wait(&status);
83 if (WIFEXITED(status))
84 return (WEXITSTATUS(status));
85 return (0);
86 }
87 }
88
89
90
91 static void
makeargv()92 makeargv()
93 {
94 char *cp, *cp2, c;
95 char **argp = margv;
96
97 margc = 0;
98 cp = line;
99 if (*cp == '!') { /* Special case shell escape */
100 strlcpy(saveline, line, sizeof(saveline)); /* save for shell command */
101 *argp++ = "!"; /* No room in string to get this */
102 margc++;
103 cp++;
104 }
105 while ((c = *cp)) {
106 int inquote = 0;
107 while (isspace(c))
108 c = *++cp;
109 if (c == '\0')
110 break;
111 *argp++ = cp;
112 margc += 1;
113 for (cp2 = cp; c != '\0'; c = *++cp) {
114 if (inquote) {
115 if (c == inquote) {
116 inquote = 0;
117 continue;
118 }
119 } else {
120 if (c == '\\') {
121 if ((c = *++cp) == '\0')
122 break;
123 } else if (c == '"') {
124 inquote = '"';
125 continue;
126 } else if (c == '\'') {
127 inquote = '\'';
128 continue;
129 } else if (isspace(c))
130 break;
131 }
132 *cp2++ = c;
133 }
134 *cp2 = '\0';
135 if (c == '\0')
136 break;
137 cp++;
138 }
139 *argp++ = 0;
140 }
141
142 /*
143 * Make a character string into a number.
144 *
145 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
146 */
147
148 static char
special(s)149 special(s)
150 char *s;
151 {
152 char c;
153 char b;
154
155 switch (*s) {
156 case '^':
157 b = *++s;
158 if (b == '?') {
159 c = b | 0x40; /* DEL */
160 } else {
161 c = b & 0x1f;
162 }
163 break;
164 default:
165 c = *s;
166 break;
167 }
168 return c;
169 }
170
171 /*
172 * Construct a control character sequence
173 * for a special character.
174 */
175 static char *
control(c)176 control(c)
177 cc_t c;
178 {
179 static char buf[5];
180 /*
181 * The only way I could get the Sun 3.5 compiler
182 * to shut up about
183 * if ((unsigned int)c >= 0x80)
184 * was to assign "c" to an unsigned int variable...
185 * Arggg....
186 */
187 unsigned int uic = (unsigned int)c;
188
189 if (uic == 0x7f)
190 return ("^?");
191 if (c == (cc_t)_POSIX_VDISABLE) {
192 return "off";
193 }
194 if (uic >= 0x80) {
195 buf[0] = '\\';
196 buf[1] = ((c>>6)&07) + '0';
197 buf[2] = ((c>>3)&07) + '0';
198 buf[3] = (c&07) + '0';
199 buf[4] = 0;
200 } else if (uic >= 0x20) {
201 buf[0] = c;
202 buf[1] = 0;
203 } else {
204 buf[0] = '^';
205 buf[1] = '@'+c;
206 buf[2] = 0;
207 }
208 return (buf);
209 }
210
211
212
213 /*
214 * The following are data structures and routines for
215 * the "send" command.
216 *
217 */
218
219 struct sendlist {
220 char *name; /* How user refers to it (case independent) */
221 char *help; /* Help information (0 ==> no help) */
222 int needconnect; /* Need to be connected */
223 int narg; /* Number of arguments */
224 int (*handler)(); /* Routine to perform (for special ops) */
225 int nbyte; /* Number of bytes to send this command */
226 int what; /* Character to be sent (<0 ==> special) */
227 };
228
229
230 static int
231 send_esc(void),
232 send_help(void),
233 send_docmd(char *),
234 send_dontcmd(char *),
235 send_willcmd(char *),
236 send_wontcmd(char *);
237
238 static struct sendlist Sendlist[] = {
239 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
240 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
241 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
242 { "break", 0, 1, 0, 0, 2, BREAK },
243 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
244 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
245 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
246 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
247 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
248 { "intp", 0, 1, 0, 0, 2, IP },
249 { "interrupt", 0, 1, 0, 0, 2, IP },
250 { "intr", 0, 1, 0, 0, 2, IP },
251 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
252 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
253 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
254 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
255 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
256 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
257 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
258 { "?", "Display send options", 0, 0, send_help, 0, 0 },
259 { "help", 0, 0, 0, send_help, 0, 0 },
260 { "do", 0, 0, 1, send_docmd, 3, 0 },
261 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
262 { "will", 0, 0, 1, send_willcmd, 3, 0 },
263 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
264 { 0 }
265 };
266
267 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
268 sizeof(struct sendlist)))
269
270 static int
sendcmd(argc,argv)271 sendcmd(argc, argv)
272 int argc;
273 char **argv;
274 {
275 int count; /* how many bytes we are going to need to send */
276 int i;
277 struct sendlist *s; /* pointer to current command */
278 int success = 0;
279 int needconnect = 0;
280
281 if (argc < 2) {
282 printf("need at least one argument for 'send' command\r\n");
283 printf("'send ?' for help\r\n");
284 return 0;
285 }
286 /*
287 * First, validate all the send arguments.
288 * In addition, we see how much space we are going to need, and
289 * whether or not we will be doing a "SYNCH" operation (which
290 * flushes the network queue).
291 */
292 count = 0;
293 for (i = 1; i < argc; i++) {
294 s = GETSEND(argv[i]);
295 if (s == 0) {
296 printf("Unknown send argument '%s'\r\n'send ?' for help.\r\n",
297 argv[i]);
298 return 0;
299 } else if (Ambiguous(s)) {
300 printf("Ambiguous send argument '%s'\r\n'send ?' for help.\r\n",
301 argv[i]);
302 return 0;
303 }
304 if (i + s->narg >= argc) {
305 fprintf(stderr,
306 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\r\n",
307 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
308 return 0;
309 }
310 count += s->nbyte;
311 if (s->handler == send_help) {
312 send_help();
313 return 0;
314 }
315
316 i += s->narg;
317 needconnect += s->needconnect;
318 }
319 if (!connected && needconnect) {
320 printf("?Need to be connected first.\r\n");
321 printf("'send ?' for help\r\n");
322 return 0;
323 }
324 /* Now, do we have enough room? */
325 if (NETROOM() < count) {
326 printf("There is not enough room in the buffer TO the network\r\n");
327 printf("to process your request. Nothing will be done.\r\n");
328 printf("('send synch' will throw away most data in the network\r\n");
329 printf("buffer, if this might help.)\r\n");
330 return 0;
331 }
332 /* OK, they are all OK, now go through again and actually send */
333 count = 0;
334 for (i = 1; i < argc; i++) {
335 if ((s = GETSEND(argv[i])) == 0) {
336 fprintf(stderr, "Telnet 'send' error - argument disappeared!\r\n");
337 (void) quit();
338 /*NOTREACHED*/
339 }
340 if (s->handler) {
341 count++;
342 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
343 (s->narg > 1) ? argv[i+2] : 0);
344 i += s->narg;
345 } else {
346 NET2ADD(IAC, s->what);
347 printoption("SENT", IAC, s->what);
348 }
349 }
350 return (count == success);
351 }
352
353 static int
354 send_tncmd(void (*func)(), char *cmd, char *name);
355
356 static int
send_esc()357 send_esc()
358 {
359 NETADD(escape);
360 return 1;
361 }
362
363 static int
send_docmd(name)364 send_docmd(name)
365 char *name;
366 {
367 return(send_tncmd(send_do, "do", name));
368 }
369
370 static int
send_dontcmd(name)371 send_dontcmd(name)
372 char *name;
373 {
374 return(send_tncmd(send_dont, "dont", name));
375 }
376 static int
send_willcmd(name)377 send_willcmd(name)
378 char *name;
379 {
380 return(send_tncmd(send_will, "will", name));
381 }
382 static int
send_wontcmd(name)383 send_wontcmd(name)
384 char *name;
385 {
386 return(send_tncmd(send_wont, "wont", name));
387 }
388
389 int
390 send_tncmd(func, cmd, name)
391 void (*func)();
392 char *cmd, *name;
393 {
394 char **cpp;
395 extern char *telopts[];
396 int val = 0;
397
398 if (isprefix(name, "help") || isprefix(name, "?")) {
399 int col, len;
400
401 printf("Usage: send %s <value|option>\r\n", cmd);
402 printf("\"value\" must be from 0 to 255\r\n");
403 printf("Valid options are:\r\n\t");
404
405 col = 8;
406 for (cpp = telopts; *cpp; cpp++) {
407 len = strlen(*cpp) + 3;
408 if (col + len > 65) {
409 printf("\r\n\t");
410 col = 8;
411 }
412 printf(" \"%s\"", *cpp);
413 col += len;
414 }
415 printf("\r\n");
416 return 0;
417 }
418 cpp = (char **)genget(name, telopts, sizeof(char *));
419 if (Ambiguous(cpp)) {
420 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\r\n",
421 name, cmd);
422 return 0;
423 }
424 if (cpp) {
425 val = cpp - telopts;
426 } else {
427 char *cp = name;
428
429 while (*cp >= '0' && *cp <= '9') {
430 val *= 10;
431 val += *cp - '0';
432 cp++;
433 }
434 if (*cp != 0) {
435 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\r\n",
436 name, cmd);
437 return 0;
438 } else if (val < 0 || val > 255) {
439 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\r\n",
440 name, cmd);
441 return 0;
442 }
443 }
444 if (!connected) {
445 printf("?Need to be connected first.\r\n");
446 return 0;
447 }
448 (*func)(val, 1);
449 return 1;
450 }
451
452 static int
send_help()453 send_help()
454 {
455 struct sendlist *s; /* pointer to current command */
456 for (s = Sendlist; s->name; s++) {
457 if (s->help)
458 printf("%-15s %s\r\n", s->name, s->help);
459 }
460 return(0);
461 }
462
463 /*
464 * The following are the routines and data structures referred
465 * to by the arguments to the "toggle" command.
466 */
467
468 static int
lclchars()469 lclchars()
470 {
471 donelclchars = 1;
472 return 1;
473 }
474
475 static int
togdebug()476 togdebug()
477 {
478 #ifndef NOT43
479 if (net > 0 &&
480 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
481 perror("setsockopt (SO_DEBUG)");
482 }
483 #else /* NOT43 */
484 if (debug) {
485 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
486 perror("setsockopt (SO_DEBUG)");
487 } else
488 printf("Cannot turn off socket debugging\r\n");
489 #endif /* NOT43 */
490 return 1;
491 }
492
493
494 static int
togcrlf()495 togcrlf()
496 {
497 if (crlf) {
498 printf("Will send carriage returns as telnet <CR><LF>.\r\n");
499 } else {
500 printf("Will send carriage returns as telnet <CR><NUL>.\r\n");
501 }
502 return 1;
503 }
504
505 int binmode;
506
507 static int
togbinary(val)508 togbinary(val)
509 int val;
510 {
511 donebinarytoggle = 1;
512
513 if (val >= 0) {
514 binmode = val;
515 } else {
516 if (my_want_state_is_will(TELOPT_BINARY) &&
517 my_want_state_is_do(TELOPT_BINARY)) {
518 binmode = 1;
519 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
520 my_want_state_is_dont(TELOPT_BINARY)) {
521 binmode = 0;
522 }
523 val = binmode ? 0 : 1;
524 }
525
526 if (val == 1) {
527 if (my_want_state_is_will(TELOPT_BINARY) &&
528 my_want_state_is_do(TELOPT_BINARY)) {
529 printf("Already operating in binary mode with remote host.\r\n");
530 } else {
531 printf("Negotiating binary mode with remote host.\r\n");
532 tel_enter_binary(3);
533 }
534 } else {
535 if (my_want_state_is_wont(TELOPT_BINARY) &&
536 my_want_state_is_dont(TELOPT_BINARY)) {
537 printf("Already in network ascii mode with remote host.\r\n");
538 } else {
539 printf("Negotiating network ascii mode with remote host.\r\n");
540 tel_leave_binary(3);
541 }
542 }
543 return 1;
544 }
545
546 static int
togrbinary(val)547 togrbinary(val)
548 int val;
549 {
550 donebinarytoggle = 1;
551
552 if (val == -1)
553 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
554
555 if (val == 1) {
556 if (my_want_state_is_do(TELOPT_BINARY)) {
557 printf("Already receiving in binary mode.\r\n");
558 } else {
559 printf("Negotiating binary mode on input.\r\n");
560 tel_enter_binary(1);
561 }
562 } else {
563 if (my_want_state_is_dont(TELOPT_BINARY)) {
564 printf("Already receiving in network ascii mode.\r\n");
565 } else {
566 printf("Negotiating network ascii mode on input.\r\n");
567 tel_leave_binary(1);
568 }
569 }
570 return 1;
571 }
572
573 static int
togxbinary(val)574 togxbinary(val)
575 int val;
576 {
577 donebinarytoggle = 1;
578
579 if (val == -1)
580 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
581
582 if (val == 1) {
583 if (my_want_state_is_will(TELOPT_BINARY)) {
584 printf("Already transmitting in binary mode.\r\n");
585 } else {
586 printf("Negotiating binary mode on output.\r\n");
587 tel_enter_binary(2);
588 }
589 } else {
590 if (my_want_state_is_wont(TELOPT_BINARY)) {
591 printf("Already transmitting in network ascii mode.\r\n");
592 } else {
593 printf("Negotiating network ascii mode on output.\r\n");
594 tel_leave_binary(2);
595 }
596 }
597 return 1;
598 }
599
600
601 static int togglehelp(void);
602 #if defined(AUTHENTICATION)
603 extern int auth_togdebug(int);
604 #endif
605 #if defined(ENCRYPTION)
606 extern int EncryptAutoEnc(int);
607 extern int EncryptAutoDec(int);
608 extern int EncryptDebug(int);
609 extern int EncryptVerbose(int);
610 #endif
611
612
613 struct togglelist {
614 char *name; /* name of toggle */
615 char *help; /* help message */
616 int (*handler)(); /* routine to do actual setting */
617 int *variable;
618 char *actionexplanation;
619 int needconnect; /* Need to be connected */
620 };
621
622 static struct togglelist Togglelist[] = {
623 { "autoflush",
624 "flushing of output when sending interrupt characters",
625 0,
626 &autoflush,
627 "flush output when sending interrupt characters" },
628 { "autosynch",
629 "automatic sending of interrupt characters in urgent mode",
630 0,
631 &autosynch,
632 "send interrupt characters in urgent mode" },
633 #if defined(AUTHENTICATION)
634 { "autologin",
635 "automatic sending of login and/or authentication info",
636 0,
637 &autologin,
638 "send login name and/or authentication information" },
639 { "authdebug",
640 "Toggle authentication debugging",
641 auth_togdebug,
642 0,
643 "print authentication debugging information" },
644 #endif
645 #if defined(ENCRYPTION)
646 { "autoencrypt",
647 "automatic encryption of data stream",
648 EncryptAutoEnc,
649 0,
650 "automatically encrypt output" },
651 { "autodecrypt",
652 "automatic decryption of data stream",
653 EncryptAutoDec,
654 0,
655 "automatically decrypt input" },
656 { "verbose_encrypt",
657 "Toggle verbose encryption output",
658 EncryptVerbose,
659 0,
660 "print verbose encryption output" },
661 { "encdebug",
662 "Toggle encryption debugging",
663 EncryptDebug,
664 0,
665 "print encryption debugging information" },
666 #endif
667 { "skiprc",
668 "don't read ~/.telnetrc file",
669 0,
670 &skiprc,
671 "skip reading of ~/.telnetrc file" },
672 { "binary",
673 "sending and receiving of binary data",
674 togbinary,
675 0,
676 0 },
677 { "inbinary",
678 "receiving of binary data",
679 togrbinary,
680 0,
681 0 },
682 { "outbinary",
683 "sending of binary data",
684 togxbinary,
685 0,
686 0 },
687 { "crlf",
688 "sending carriage returns as telnet <CR><LF>",
689 togcrlf,
690 &crlf,
691 0 },
692 { "crmod",
693 "mapping of received carriage returns",
694 0,
695 &crmod,
696 "map carriage return on output" },
697 { "localchars",
698 "local recognition of certain control characters",
699 lclchars,
700 &localchars,
701 "recognize certain control characters" },
702 { " ", "", 0, 0 }, /* empty line */
703 #if defined(unix) && defined(TN3270)
704 { "apitrace",
705 "(debugging) toggle tracing of API transactions",
706 0,
707 &apitrace,
708 "trace API transactions", 0 },
709 { "cursesdata",
710 "(debugging) toggle printing of hexadecimal curses data",
711 0,
712 &cursesdata,
713 "print hexadecimal representation of curses data", 0 },
714 #endif /* defined(unix) && defined(TN3270) */
715 { "debug",
716 "debugging",
717 togdebug,
718 &debug,
719 "turn on socket level debugging" },
720 { "netdata",
721 "printing of hexadecimal network data (debugging)",
722 0,
723 &netdata,
724 "print hexadecimal representation of network traffic" },
725 { "prettydump",
726 "output of \"netdata\" to user readable format (debugging)",
727 0,
728 &prettydump,
729 "print user readable output for \"netdata\"" },
730 { "options",
731 "viewing of options processing (debugging)",
732 0,
733 &showoptions,
734 "show option processing" },
735 #if defined(unix)
736 { "termdata",
737 "(debugging) toggle printing of hexadecimal terminal data",
738 0,
739 &termdata,
740 "print hexadecimal representation of terminal traffic" },
741 #endif /* defined(unix) */
742 { "?",
743 0,
744 togglehelp },
745 { "help",
746 0,
747 togglehelp },
748 { 0 }
749 };
750
751 static int
togglehelp()752 togglehelp()
753 {
754 struct togglelist *c;
755
756 for (c = Togglelist; c->name; c++) {
757 if (c->help) {
758 if (*c->help)
759 printf("%-15s toggle %s\r\n", c->name, c->help);
760 else
761 printf("\r\n");
762 }
763 }
764 printf("\r\n");
765 printf("%-15s %s\r\n", "?", "display help information");
766 return 0;
767 }
768
769 static void
settogglehelp(set)770 settogglehelp(set)
771 int set;
772 {
773 struct togglelist *c;
774
775 for (c = Togglelist; c->name; c++) {
776 if (c->help) {
777 if (*c->help)
778 printf("%-15s %s %s\r\n", c->name, set ? "enable" : "disable",
779 c->help);
780 else
781 printf("\r\n");
782 }
783 }
784 }
785
786 #define GETTOGGLE(name) (struct togglelist *) \
787 genget(name, (char **) Togglelist, sizeof(struct togglelist))
788
789 static int
toggle(argc,argv)790 toggle(argc, argv)
791 int argc;
792 char *argv[];
793 {
794 int retval = 1;
795 char *name;
796 struct togglelist *c;
797
798 if (argc < 2) {
799 fprintf(stderr,
800 "Need an argument to 'toggle' command. 'toggle ?' for help.\r\n");
801 return 0;
802 }
803 argc--;
804 argv++;
805 while (argc--) {
806 name = *argv++;
807 c = GETTOGGLE(name);
808 if (Ambiguous(c)) {
809 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\r\n",
810 name);
811 return 0;
812 } else if (c == 0) {
813 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\r\n",
814 name);
815 return 0;
816 } else if (!connected && c->needconnect) {
817 printf("?Need to be connected first.\r\n");
818 printf("'send ?' for help\r\n");
819 return 0;
820 } else {
821 if (c->variable) {
822 *c->variable = !*c->variable; /* invert it */
823 if (c->actionexplanation) {
824 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
825 c->actionexplanation);
826 }
827 }
828 if (c->handler) {
829 retval &= (*c->handler)(-1);
830 }
831 }
832 }
833 return retval;
834 }
835
836 /*
837 * The following perform the "set" command.
838 */
839
840 #ifdef USE_TERMIO
841 struct termios new_tc = { 0 };
842 #endif
843
844 struct setlist {
845 char *name; /* name */
846 char *help; /* help information */
847 void (*handler)();
848 cc_t *charp; /* where it is located at */
849 };
850
851 static struct setlist Setlist[] = {
852 #ifdef KLUDGELINEMODE
853 { "echo", "character to toggle local echoing on/off", 0, &echoc },
854 #endif
855 { "escape", "character to escape back to telnet command mode", 0, &escape },
856 { "rlogin", "rlogin escape character", 0, &rlogin },
857 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
858 { " ", "" },
859 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
860 { "flushoutput", "character to cause an Abort Output", 0, &termFlushChar },
861 { "interrupt", "character to cause an Interrupt Process", 0, &termIntChar },
862 { "quit", "character to cause an Abort process", 0, &termQuitChar },
863 { "eof", "character to cause an EOF ", 0, &termEofChar },
864 { " ", "" },
865 { " ", "The following are for local editing in linemode", 0, 0 },
866 { "erase", "character to use to erase a character", 0, &termEraseChar },
867 { "kill", "character to use to erase a line", 0, &termKillChar },
868 { "lnext", "character to use for literal next", 0, &termLiteralNextChar },
869 { "susp", "character to cause a Suspend Process", 0, &termSuspChar },
870 { "reprint", "character to use for line reprint", 0, &termRprntChar },
871 { "worderase", "character to use to erase a word", 0, &termWerasChar },
872 { "start", "character to use for XON", 0, &termStartChar },
873 { "stop", "character to use for XOFF", 0, &termStopChar },
874 { "forw1", "alternate end of line character", 0, &termForw1Char },
875 { "forw2", "alternate end of line character", 0, &termForw2Char },
876 { "ayt", "alternate AYT character", 0, &termAytChar },
877 { 0 }
878 };
879
880 static struct setlist *
getset(name)881 getset(name)
882 char *name;
883 {
884 return (struct setlist *)
885 genget(name, (char **) Setlist, sizeof(struct setlist));
886 }
887
888 void
set_escape_char(s)889 set_escape_char(s)
890 char *s;
891 {
892 if (rlogin != _POSIX_VDISABLE) {
893 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
894 printf("Telnet rlogin escape character is '%s'.\r\n",
895 control(rlogin));
896 } else {
897 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
898 printf("Telnet escape character is '%s'.\r\n", control(escape));
899 }
900 }
901
902 static int
setcmd(argc,argv)903 setcmd(argc, argv)
904 int argc;
905 char *argv[];
906 {
907 int value;
908 struct setlist *ct;
909 struct togglelist *c;
910
911 if (argc < 2 || argc > 3) {
912 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
913 return 0;
914 }
915 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
916 for (ct = Setlist; ct->name; ct++)
917 printf("%-15s %s\r\n", ct->name, ct->help);
918 printf("\r\n");
919 settogglehelp(1);
920 printf("%-15s %s\r\n", "?", "display help information");
921 return 0;
922 }
923
924 ct = getset(argv[1]);
925 if (ct == 0) {
926 c = GETTOGGLE(argv[1]);
927 if (c == 0) {
928 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\r\n",
929 argv[1]);
930 return 0;
931 } else if (Ambiguous(c)) {
932 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
933 argv[1]);
934 return 0;
935 } else if (!connected && c->needconnect) {
936 printf("?Need to be connected first.\r\n");
937 printf("'send ?' for help\r\n");
938 return 0;
939 }
940
941 if (c->variable) {
942 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
943 *c->variable = 1;
944 else if (strcmp("off", argv[2]) == 0)
945 *c->variable = 0;
946 else {
947 printf("Format is 'set togglename [on|off]'\r\n'set ?' for help.\r\n");
948 return 0;
949 }
950 if (c->actionexplanation) {
951 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
952 c->actionexplanation);
953 }
954 }
955 if (c->handler)
956 (*c->handler)(1);
957 } else if (argc != 3) {
958 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
959 return 0;
960 } else if (Ambiguous(ct)) {
961 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
962 argv[1]);
963 return 0;
964 } else if (ct->handler) {
965 (*ct->handler)(argv[2]);
966 printf("%s set to \"%s\".\r\n", ct->name, (char *)ct->charp);
967 } else {
968 if (strcmp("off", argv[2])) {
969 value = special(argv[2]);
970 } else {
971 value = _POSIX_VDISABLE;
972 }
973 *(ct->charp) = (cc_t)value;
974 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
975 }
976 slc_check();
977 return 1;
978 }
979
980 static int
unsetcmd(argc,argv)981 unsetcmd(argc, argv)
982 int argc;
983 char *argv[];
984 {
985 struct setlist *ct;
986 struct togglelist *c;
987 char *name;
988
989 if (argc < 2) {
990 fprintf(stderr,
991 "Need an argument to 'unset' command. 'unset ?' for help.\r\n");
992 return 0;
993 }
994 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
995 for (ct = Setlist; ct->name; ct++)
996 printf("%-15s %s\r\n", ct->name, ct->help);
997 printf("\r\n");
998 settogglehelp(0);
999 printf("%-15s %s\r\n", "?", "display help information");
1000 return 0;
1001 }
1002
1003 argc--;
1004 argv++;
1005 while (argc--) {
1006 name = *argv++;
1007 ct = getset(name);
1008 if (ct == 0) {
1009 c = GETTOGGLE(name);
1010 if (c == 0) {
1011 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\r\n",
1012 name);
1013 return 0;
1014 } else if (Ambiguous(c)) {
1015 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
1016 name);
1017 return 0;
1018 }
1019 if (c->variable) {
1020 *c->variable = 0;
1021 if (c->actionexplanation) {
1022 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1023 c->actionexplanation);
1024 }
1025 }
1026 if (c->handler)
1027 (*c->handler)(0);
1028 } else if (Ambiguous(ct)) {
1029 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
1030 name);
1031 return 0;
1032 } else if (ct->handler) {
1033 (*ct->handler)(0);
1034 printf("%s reset to \"%s\".\r\n", ct->name, (char *)ct->charp);
1035 } else {
1036 *(ct->charp) = _POSIX_VDISABLE;
1037 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
1038 }
1039 }
1040 return 1;
1041 }
1042
1043 /*
1044 * The following are the data structures and routines for the
1045 * 'mode' command.
1046 */
1047 #ifdef KLUDGELINEMODE
1048 extern int kludgelinemode;
1049
1050 static int
dokludgemode()1051 dokludgemode()
1052 {
1053 kludgelinemode = 1;
1054 send_wont(TELOPT_LINEMODE, 1);
1055 send_dont(TELOPT_SGA, 1);
1056 send_dont(TELOPT_ECHO, 1);
1057 return 1;
1058 }
1059 #endif
1060
1061 static int
dolinemode()1062 dolinemode()
1063 {
1064 #ifdef KLUDGELINEMODE
1065 if (kludgelinemode)
1066 send_dont(TELOPT_SGA, 1);
1067 #endif
1068 send_will(TELOPT_LINEMODE, 1);
1069 send_dont(TELOPT_ECHO, 1);
1070 return 1;
1071 }
1072
1073 static int
docharmode()1074 docharmode()
1075 {
1076 #ifdef KLUDGELINEMODE
1077 if (kludgelinemode)
1078 send_do(TELOPT_SGA, 1);
1079 else
1080 #endif
1081 send_wont(TELOPT_LINEMODE, 1);
1082 send_do(TELOPT_ECHO, 1);
1083 return 1;
1084 }
1085
1086 static int
dolmmode(bit,on)1087 dolmmode(bit, on)
1088 int bit, on;
1089 {
1090 unsigned char c;
1091 extern int linemode;
1092
1093 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1094 printf("?Need to have LINEMODE option enabled first.\r\n");
1095 printf("'mode ?' for help.\r\n");
1096 return 0;
1097 }
1098
1099 if (on)
1100 c = (linemode | bit);
1101 else
1102 c = (linemode & ~bit);
1103 lm_mode(&c, 1, 1);
1104 return 1;
1105 }
1106
1107 int
tn_setmode(int bit)1108 tn_setmode(int bit)
1109 {
1110 return dolmmode(bit, 1);
1111 }
1112
1113 int
tn_clearmode(int bit)1114 tn_clearmode(int bit)
1115 {
1116 return dolmmode(bit, 0);
1117 }
1118
1119 struct modelist {
1120 char *name; /* command name */
1121 char *help; /* help string */
1122 int (*handler)(); /* routine which executes command */
1123 int needconnect; /* Do we need to be connected to execute? */
1124 int arg1;
1125 };
1126
1127 static int modehelp(void);
1128
1129 static struct modelist ModeList[] = {
1130 { "character", "Disable LINEMODE option", docharmode, 1 },
1131 #ifdef KLUDGELINEMODE
1132 { "", "(or disable obsolete line-by-line mode)", 0 },
1133 #endif
1134 { "line", "Enable LINEMODE option", dolinemode, 1 },
1135 #ifdef KLUDGELINEMODE
1136 { "", "(or enable obsolete line-by-line mode)", 0 },
1137 #endif
1138 { "", "", 0 },
1139 { "", "These require the LINEMODE option to be enabled", 0 },
1140 { "isig", "Enable signal trapping", tn_setmode, 1, MODE_TRAPSIG },
1141 { "+isig", 0, tn_setmode, 1, MODE_TRAPSIG },
1142 { "-isig", "Disable signal trapping", tn_clearmode, 1, MODE_TRAPSIG },
1143 { "edit", "Enable character editing", tn_setmode, 1, MODE_EDIT },
1144 { "+edit", 0, tn_setmode, 1, MODE_EDIT },
1145 { "-edit", "Disable character editing", tn_clearmode, 1, MODE_EDIT },
1146 { "softtabs", "Enable tab expansion", tn_setmode, 1, MODE_SOFT_TAB },
1147 { "+softtabs", 0, tn_setmode, 1, MODE_SOFT_TAB },
1148 { "-softtabs", "Disable character editing", tn_clearmode, 1, MODE_SOFT_TAB },
1149 { "litecho", "Enable literal character echo", tn_setmode, 1, MODE_LIT_ECHO },
1150 { "+litecho", 0, tn_setmode, 1, MODE_LIT_ECHO },
1151 { "-litecho", "Disable literal character echo", tn_clearmode, 1, MODE_LIT_ECHO },
1152 { "help", 0, modehelp, 0 },
1153 #ifdef KLUDGELINEMODE
1154 { "kludgeline", 0, dokludgemode, 1 },
1155 #endif
1156 { "", "", 0 },
1157 { "?", "Print help information", modehelp, 0 },
1158 { 0 },
1159 };
1160
1161
1162 static int
modehelp()1163 modehelp()
1164 {
1165 struct modelist *mt;
1166
1167 printf("format is: 'mode Mode', where 'Mode' is one of:\r\n\r\n");
1168 for (mt = ModeList; mt->name; mt++) {
1169 if (mt->help) {
1170 if (*mt->help)
1171 printf("%-15s %s\r\n", mt->name, mt->help);
1172 else
1173 printf("\r\n");
1174 }
1175 }
1176 return 0;
1177 }
1178
1179 #define GETMODECMD(name) (struct modelist *) \
1180 genget(name, (char **) ModeList, sizeof(struct modelist))
1181
1182 static int
modecmd(argc,argv)1183 modecmd(argc, argv)
1184 int argc;
1185 char *argv[];
1186 {
1187 struct modelist *mt;
1188
1189 if (argc != 2) {
1190 printf("'mode' command requires an argument\r\n");
1191 printf("'mode ?' for help.\r\n");
1192 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1193 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\r\n", argv[1]);
1194 } else if (Ambiguous(mt)) {
1195 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\r\n", argv[1]);
1196 } else if (mt->needconnect && !connected) {
1197 printf("?Need to be connected first.\r\n");
1198 printf("'mode ?' for help.\r\n");
1199 } else if (mt->handler) {
1200 return (*mt->handler)(mt->arg1);
1201 }
1202 return 0;
1203 }
1204
1205 /*
1206 * The following data structures and routines implement the
1207 * "display" command.
1208 */
1209
1210 static int
display(argc,argv)1211 display(argc, argv)
1212 int argc;
1213 char *argv[];
1214 {
1215 struct togglelist *tl;
1216 struct setlist *sl;
1217
1218 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1219 if (*tl->variable) { \
1220 printf("will"); \
1221 } else { \
1222 printf("won't"); \
1223 } \
1224 printf(" %s.\r\n", tl->actionexplanation); \
1225 }
1226
1227 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1228 if (sl->handler == 0) \
1229 printf("%-15s [%s]\r\n", sl->name, control(*sl->charp)); \
1230 else \
1231 printf("%-15s \"%s\"\r\n", sl->name, (char *)sl->charp); \
1232 }
1233
1234 if (argc == 1) {
1235 for (tl = Togglelist; tl->name; tl++) {
1236 dotog(tl);
1237 }
1238 printf("\r\n");
1239 for (sl = Setlist; sl->name; sl++) {
1240 doset(sl);
1241 }
1242 } else {
1243 int i;
1244
1245 for (i = 1; i < argc; i++) {
1246 sl = getset(argv[i]);
1247 tl = GETTOGGLE(argv[i]);
1248 if (Ambiguous(sl) || Ambiguous(tl)) {
1249 printf("?Ambiguous argument '%s'.\r\n", argv[i]);
1250 return 0;
1251 } else if (!sl && !tl) {
1252 printf("?Unknown argument '%s'.\r\n", argv[i]);
1253 return 0;
1254 } else {
1255 if (tl) {
1256 dotog(tl);
1257 }
1258 if (sl) {
1259 doset(sl);
1260 }
1261 }
1262 }
1263 }
1264 /*@*/optionstatus();
1265 #if defined(ENCRYPTION)
1266 EncryptStatus();
1267 #endif
1268 return 1;
1269 #undef doset
1270 #undef dotog
1271 }
1272
1273 /*
1274 * The following are the data structures, and many of the routines,
1275 * relating to command processing.
1276 */
1277
1278 /*
1279 * Set the escape character.
1280 */
1281 static int
setescape(argc,argv)1282 setescape(argc, argv)
1283 int argc;
1284 char *argv[];
1285 {
1286 char *arg;
1287 char buf[50];
1288
1289 printf(
1290 "Deprecated usage - please use 'set escape%s%s' in the future.\r\n",
1291 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1292 if (argc > 2)
1293 arg = argv[1];
1294 else {
1295 printf("new escape character: ");
1296 (void) fgets(buf, sizeof(buf), stdin);
1297 arg = buf;
1298 }
1299 if (arg[0] != '\0')
1300 escape = arg[0];
1301 if (!In3270) {
1302 printf("Escape character is '%s'.\r\n", control(escape));
1303 }
1304 (void) fflush(stdout);
1305 return 1;
1306 }
1307
1308 /*VARARGS*/
1309 static int
togcrmod()1310 togcrmod()
1311 {
1312 crmod = !crmod;
1313 printf("Deprecated usage - please use 'toggle crmod' in the future.\r\n");
1314 printf("%s map carriage return on output.\r\n", crmod ? "Will" : "Won't");
1315 (void) fflush(stdout);
1316 return 1;
1317 }
1318
1319 /*VARARGS*/
1320 int
telnetsuspend()1321 telnetsuspend()
1322 {
1323 #ifdef SIGTSTP
1324 setcommandmode();
1325 {
1326 long oldrows, oldcols, newrows, newcols, err;
1327
1328 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1329 (void) kill(0, SIGTSTP);
1330 /*
1331 * If we didn't get the window size before the SUSPEND, but we
1332 * can get them now (?), then send the NAWS to make sure that
1333 * we are set up for the right window size.
1334 */
1335 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1336 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1337 sendnaws();
1338 }
1339 }
1340 /* reget parameters in case they were changed */
1341 TerminalSaveState();
1342 setconnmode(0);
1343 #else
1344 printf("Suspend is not supported. Try the '!' command instead\r\n");
1345 #endif
1346 return 1;
1347 }
1348
1349 #if !defined(TN3270)
1350 /*ARGSUSED*/
1351 int
shell(argc,argv)1352 shell(argc, argv)
1353 int argc;
1354 char *argv[];
1355 {
1356 long oldrows, oldcols, newrows, newcols, err;
1357
1358 setcommandmode();
1359
1360 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1361 switch(vfork()) {
1362 case -1:
1363 perror("Fork failed\r\n");
1364 break;
1365
1366 case 0:
1367 {
1368 /*
1369 * Fire up the shell in the child.
1370 */
1371 char *shellp, *shellname;
1372
1373 shellp = getenv("SHELL");
1374 if (shellp == NULL)
1375 shellp = "/bin/sh";
1376 if ((shellname = strrchr(shellp, '/')) == 0)
1377 shellname = shellp;
1378 else
1379 shellname++;
1380 if (argc > 1)
1381 execl(shellp, shellname, "-c", &saveline[1], (char *)NULL);
1382 else
1383 execl(shellp, shellname, (char *)NULL);
1384 perror("Execl");
1385 _exit(1);
1386 }
1387 default:
1388 (void)wait((int *)0); /* Wait for the shell to complete */
1389
1390 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1391 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1392 sendnaws();
1393 }
1394 break;
1395 }
1396 return 1;
1397 }
1398 #else /* !defined(TN3270) */
1399 extern int shell();
1400 #endif /* !defined(TN3270) */
1401
1402 /*VARARGS*/
1403 static int
bye(argc,argv)1404 bye(argc, argv)
1405 int argc; /* Number of arguments */
1406 char *argv[]; /* arguments */
1407 {
1408 extern int resettermname;
1409
1410 if (connected) {
1411 (void) shutdown(net, 2);
1412 printf("Connection closed.\r\n");
1413 (void) NetClose(net);
1414 connected = 0;
1415 resettermname = 1;
1416 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1417 auth_encrypt_connect(connected);
1418 #endif /* defined(AUTHENTICATION) */
1419 /* reset options */
1420 tninit();
1421 #if defined(TN3270)
1422 SetIn3270(); /* Get out of 3270 mode */
1423 #endif /* defined(TN3270) */
1424 }
1425 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1426 longjmp(toplevel, 1);
1427 /* NOTREACHED */
1428 }
1429 return 0; /* NOTREACHED */
1430 }
1431
1432 /*VARARGS*/
1433 int
quit()1434 quit()
1435 {
1436 (void) call(bye, "bye", "fromquit", 0);
1437 Exit(0);
1438 return 0; /*NOTREACHED*/
1439 }
1440
1441 /*VARARGS*/
1442 static int
logout()1443 logout()
1444 {
1445 send_do(TELOPT_LOGOUT, 1);
1446 (void) netflush();
1447 return 1;
1448 }
1449
1450
1451 /*
1452 * The SLC command.
1453 */
1454
1455 struct slclist {
1456 char *name;
1457 char *help;
1458 void (*handler)();
1459 int arg;
1460 };
1461
1462 static void slc_help();
1463
1464 struct slclist SlcList[] = {
1465 { "export", "Use local special character definitions",
1466 slc_mode_export, 0 },
1467 { "import", "Use remote special character definitions",
1468 slc_mode_import, 1 },
1469 { "check", "Verify remote special character definitions",
1470 slc_mode_import, 0 },
1471 { "help", 0, slc_help, 0 },
1472 { "?", "Print help information", slc_help, 0 },
1473 { 0 },
1474 };
1475
1476 static void
slc_help()1477 slc_help()
1478 {
1479 struct slclist *c;
1480
1481 for (c = SlcList; c->name; c++) {
1482 if (c->help) {
1483 if (*c->help)
1484 printf("%-15s %s\r\n", c->name, c->help);
1485 else
1486 printf("\r\n");
1487 }
1488 }
1489 }
1490
1491 static struct slclist *
getslc(name)1492 getslc(name)
1493 char *name;
1494 {
1495 return (struct slclist *)
1496 genget(name, (char **) SlcList, sizeof(struct slclist));
1497 }
1498
1499 static int
slccmd(argc,argv)1500 slccmd(argc, argv)
1501 int argc;
1502 char *argv[];
1503 {
1504 struct slclist *c;
1505
1506 if (argc != 2) {
1507 fprintf(stderr,
1508 "Need an argument to 'slc' command. 'slc ?' for help.\r\n");
1509 return 0;
1510 }
1511 c = getslc(argv[1]);
1512 if (c == 0) {
1513 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\r\n",
1514 argv[1]);
1515 return 0;
1516 }
1517 if (Ambiguous(c)) {
1518 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\r\n",
1519 argv[1]);
1520 return 0;
1521 }
1522 (*c->handler)(c->arg);
1523 slcstate();
1524 return 1;
1525 }
1526
1527 /*
1528 * The ENVIRON command.
1529 */
1530
1531 struct envlist {
1532 char *name;
1533 char *help;
1534 void (*handler)();
1535 int narg;
1536 };
1537
1538 static void env_help(void);
1539
1540 struct envlist EnvList[] = {
1541 { "define", "Define an environment variable",
1542 (void (*)())env_define, 2 },
1543 { "undefine", "Undefine an environment variable",
1544 env_undefine, 1 },
1545 { "export", "Mark an environment variable for automatic export",
1546 env_export, 1 },
1547 { "unexport", "Don't mark an environment variable for automatic export",
1548 env_unexport, 1 },
1549 { "send", "Send an environment variable", env_send, 1 },
1550 { "list", "List the current environment variables",
1551 env_list, 0 },
1552 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1553 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1554 env_varval, 1 },
1555 #endif
1556 { "help", 0, env_help, 0 },
1557 { "?", "Print help information", env_help, 0 },
1558 { 0 },
1559 };
1560
1561 static void
env_help()1562 env_help()
1563 {
1564 struct envlist *c;
1565
1566 for (c = EnvList; c->name; c++) {
1567 if (c->help) {
1568 if (*c->help)
1569 printf("%-15s %s\r\n", c->name, c->help);
1570 else
1571 printf("\r\n");
1572 }
1573 }
1574 }
1575
1576 static struct envlist *
getenvcmd(name)1577 getenvcmd(name)
1578 char *name;
1579 {
1580 return (struct envlist *)
1581 genget(name, (char **) EnvList, sizeof(struct envlist));
1582 }
1583
1584 int
env_cmd(argc,argv)1585 env_cmd(argc, argv)
1586 int argc;
1587 char *argv[];
1588 {
1589 struct envlist *c;
1590
1591 if (argc < 2) {
1592 fprintf(stderr,
1593 "Need an argument to 'environ' command. 'environ ?' for help.\r\n");
1594 return 0;
1595 }
1596 c = getenvcmd(argv[1]);
1597 if (c == 0) {
1598 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\r\n",
1599 argv[1]);
1600 return 0;
1601 }
1602 if (Ambiguous(c)) {
1603 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\r\n",
1604 argv[1]);
1605 return 0;
1606 }
1607 if (c->narg + 2 != argc) {
1608 fprintf(stderr,
1609 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\r\n",
1610 c->narg < argc + 2 ? "only " : "",
1611 c->narg, c->narg == 1 ? "" : "s", c->name);
1612 return 0;
1613 }
1614 (*c->handler)(argv[2], argv[3]);
1615 return 1;
1616 }
1617
1618 struct env_lst {
1619 struct env_lst *next; /* pointer to next structure */
1620 struct env_lst *prev; /* pointer to previous structure */
1621 unsigned char *var; /* pointer to variable name */
1622 unsigned char *value; /* pointer to variable value */
1623 int export; /* 1 -> export with default list of variables */
1624 int welldefined; /* A well defined variable */
1625 };
1626
1627 struct env_lst envlisthead;
1628
1629 struct env_lst *
env_find(var)1630 env_find(var)
1631 unsigned char *var;
1632 {
1633 struct env_lst *ep;
1634
1635 for (ep = envlisthead.next; ep; ep = ep->next) {
1636 if (strcmp((char *)ep->var, (char *)var) == 0)
1637 return(ep);
1638 }
1639 return(NULL);
1640 }
1641
1642 void
env_init()1643 env_init()
1644 {
1645 extern char **environ;
1646 char **epp, *cp;
1647 struct env_lst *ep;
1648
1649 for (epp = environ; *epp; epp++) {
1650 if ((cp = strchr(*epp, '='))) {
1651 *cp = '\0';
1652 ep = env_define((unsigned char *)*epp,
1653 (unsigned char *)cp+1);
1654 ep->export = 0;
1655 *cp = '=';
1656 }
1657 }
1658 /*
1659 * Special case for DISPLAY variable. If it is ":0.0" or
1660 * "unix:0.0", we have to get rid of "unix" and insert our
1661 * hostname.
1662 */
1663 if ((ep = env_find("DISPLAY"))
1664 && ((*ep->value == ':')
1665 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1666 char hbuf[MAXHOSTNAMELEN];
1667 char *cp2 = strchr((char *)ep->value, ':');
1668
1669 gethostname(hbuf, sizeof hbuf);
1670
1671 /* If this is not the full name, try to get it via DNS */
1672 if (strchr(hbuf, '.') == 0) {
1673 struct hostent *he = gethostbyname(hbuf);
1674 if (he != 0)
1675 strncpy(hbuf, he->h_name, sizeof hbuf-1);
1676 hbuf[sizeof hbuf-1] = '\0';
1677 }
1678
1679 if (asprintf (&cp, "%s%s", hbuf, cp2) == -1)
1680 err(1, "asprintf");
1681
1682 free(ep->value);
1683 ep->value = (unsigned char *)cp;
1684 }
1685 /*
1686 * If USER is not defined, but LOGNAME is, then add
1687 * USER with the value from LOGNAME. By default, we
1688 * don't export the USER variable.
1689 */
1690 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1691 env_define((unsigned char *)"USER", ep->value);
1692 env_unexport((unsigned char *)"USER");
1693 }
1694 env_export((unsigned char *)"DISPLAY");
1695 env_export((unsigned char *)"PRINTER");
1696 env_export((unsigned char *)"XAUTHORITY");
1697 }
1698
1699 struct env_lst *
env_define(var,value)1700 env_define(var, value)
1701 unsigned char *var, *value;
1702 {
1703 struct env_lst *ep;
1704
1705 if ((ep = env_find(var))) {
1706 if (ep->var)
1707 free(ep->var);
1708 if (ep->value)
1709 free(ep->value);
1710 } else {
1711 if ((ep = malloc(sizeof(struct env_lst))) == NULL)
1712 err(1, "malloc");
1713 ep->next = envlisthead.next;
1714 envlisthead.next = ep;
1715 ep->prev = &envlisthead;
1716 if (ep->next)
1717 ep->next->prev = ep;
1718 }
1719 ep->welldefined = opt_welldefined((char *)var);
1720 ep->export = 1;
1721 if ((ep->var = strdup((char *)var)) == NULL)
1722 err(1, "strdup");
1723 if ((ep->value = strdup((char *)value)) == NULL)
1724 err(1, "strdup");
1725 return(ep);
1726 }
1727
1728 void
env_undefine(var)1729 env_undefine(var)
1730 unsigned char *var;
1731 {
1732 struct env_lst *ep;
1733
1734 if ((ep = env_find(var))) {
1735 ep->prev->next = ep->next;
1736 if (ep->next)
1737 ep->next->prev = ep->prev;
1738 if (ep->var)
1739 free(ep->var);
1740 if (ep->value)
1741 free(ep->value);
1742 free(ep);
1743 }
1744 }
1745
1746 void
env_export(var)1747 env_export(var)
1748 unsigned char *var;
1749 {
1750 struct env_lst *ep;
1751
1752 if ((ep = env_find(var)))
1753 ep->export = 1;
1754 }
1755
1756 void
env_unexport(var)1757 env_unexport(var)
1758 unsigned char *var;
1759 {
1760 struct env_lst *ep;
1761
1762 if ((ep = env_find(var)) != NULL)
1763 ep->export = 0;
1764 }
1765
1766 void
env_send(var)1767 env_send(var)
1768 unsigned char *var;
1769 {
1770 struct env_lst *ep;
1771
1772 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1773 #ifdef OLD_ENVIRON
1774 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1775 #endif
1776 ) {
1777 fprintf(stderr,
1778 "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1779 var);
1780 return;
1781 }
1782 ep = env_find(var);
1783 if (ep == 0) {
1784 fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1785 var);
1786 return;
1787 }
1788 env_opt_start_info();
1789 env_opt_add(ep->var);
1790 env_opt_end(0);
1791 }
1792
1793 void
env_list()1794 env_list()
1795 {
1796 struct env_lst *ep;
1797
1798 for (ep = envlisthead.next; ep; ep = ep->next) {
1799 printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1800 ep->var, ep->value);
1801 }
1802 }
1803
1804 unsigned char *
env_default(int init,int welldefined)1805 env_default(int init, int welldefined)
1806 {
1807 static struct env_lst *nep = NULL;
1808
1809 if (init) {
1810 nep = &envlisthead;
1811 return NULL;
1812 }
1813 if (nep) {
1814 while ((nep = nep->next)) {
1815 if (nep->export && (nep->welldefined == welldefined))
1816 return(nep->var);
1817 }
1818 }
1819 return(NULL);
1820 }
1821
1822 unsigned char *
env_getvalue(var,exported_only)1823 env_getvalue(var, exported_only)
1824 unsigned char *var;
1825 int exported_only;
1826 {
1827 struct env_lst *ep;
1828
1829 if ((ep = env_find(var)) && (!exported_only || ep->export))
1830 return(ep->value);
1831 return(NULL);
1832 }
1833
1834 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1835 void
env_varval(what)1836 env_varval(what)
1837 unsigned char *what;
1838 {
1839 extern int old_env_var, old_env_value, env_auto;
1840 int len = strlen((char *)what);
1841
1842 if (len == 0)
1843 goto unknown;
1844
1845 if (strncasecmp((char *)what, "status", len) == 0) {
1846 if (env_auto)
1847 printf("%s%s", "VAR and VALUE are/will be ",
1848 "determined automatically\r\n");
1849 if (old_env_var == OLD_ENV_VAR)
1850 printf("VAR and VALUE set to correct definitions\r\n");
1851 else
1852 printf("VAR and VALUE definitions are reversed\r\n");
1853 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1854 env_auto = 1;
1855 old_env_var = OLD_ENV_VALUE;
1856 old_env_value = OLD_ENV_VAR;
1857 } else if (strncasecmp((char *)what, "right", len) == 0) {
1858 env_auto = 0;
1859 old_env_var = OLD_ENV_VAR;
1860 old_env_value = OLD_ENV_VALUE;
1861 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1862 env_auto = 0;
1863 old_env_var = OLD_ENV_VALUE;
1864 old_env_value = OLD_ENV_VAR;
1865 } else {
1866 unknown:
1867 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\r\n");
1868 }
1869 }
1870 #endif
1871
1872 #if defined(AUTHENTICATION)
1873 /*
1874 * The AUTHENTICATE command.
1875 */
1876
1877 struct authlist {
1878 char *name;
1879 char *help;
1880 int (*handler)();
1881 int narg;
1882 };
1883
1884 static int
1885 auth_help(void);
1886
1887 struct authlist AuthList[] = {
1888 { "status", "Display current status of authentication information",
1889 auth_status, 0 },
1890 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1891 auth_disable, 1 },
1892 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1893 auth_enable, 1 },
1894 { "help", 0, auth_help, 0 },
1895 { "?", "Print help information", auth_help, 0 },
1896 { 0 },
1897 };
1898
1899 static int
auth_help()1900 auth_help()
1901 {
1902 struct authlist *c;
1903
1904 for (c = AuthList; c->name; c++) {
1905 if (c->help) {
1906 if (*c->help)
1907 printf("%-15s %s\r\n", c->name, c->help);
1908 else
1909 printf("\r\n");
1910 }
1911 }
1912 return 0;
1913 }
1914
1915 int
auth_cmd(argc,argv)1916 auth_cmd(argc, argv)
1917 int argc;
1918 char *argv[];
1919 {
1920 struct authlist *c;
1921
1922 if (argc < 2) {
1923 fprintf(stderr,
1924 "Need an argument to 'auth' command. 'auth ?' for help.\r\n");
1925 return 0;
1926 }
1927
1928 c = (struct authlist *)
1929 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1930 if (c == 0) {
1931 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1932 argv[1]);
1933 return 0;
1934 }
1935 if (Ambiguous(c)) {
1936 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1937 argv[1]);
1938 return 0;
1939 }
1940 if (c->narg + 2 != argc) {
1941 fprintf(stderr,
1942 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\r\n",
1943 c->narg < argc + 2 ? "only " : "",
1944 c->narg, c->narg == 1 ? "" : "s", c->name);
1945 return 0;
1946 }
1947 return((*c->handler)(argv[2], argv[3]));
1948 }
1949 #endif
1950
1951 #if defined(ENCRYPTION)
1952 /*
1953 * The ENCRYPT command.
1954 */
1955
1956 struct encryptlist {
1957 char *name;
1958 char *help;
1959 int (*handler)();
1960 int needconnect;
1961 int minarg;
1962 int maxarg;
1963 };
1964
1965 static int
1966 EncryptHelp (void);
1967
1968 struct encryptlist EncryptList[] = {
1969 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1970 EncryptEnable, 1, 1, 2 },
1971 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1972 EncryptDisable, 0, 1, 2 },
1973 { "type", "Set encryption type. ('encrypt type ?' for more)",
1974 EncryptType, 0, 1, 1 },
1975 { "start", "Start encryption. ('encrypt start ?' for more)",
1976 EncryptStart, 1, 0, 1 },
1977 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1978 EncryptStop, 1, 0, 1 },
1979 { "input", "Start encrypting the input stream",
1980 EncryptStartInput, 1, 0, 0 },
1981 { "-input", "Stop encrypting the input stream",
1982 EncryptStopInput, 1, 0, 0 },
1983 { "output", "Start encrypting the output stream",
1984 EncryptStartOutput, 1, 0, 0 },
1985 { "-output", "Stop encrypting the output stream",
1986 EncryptStopOutput, 1, 0, 0 },
1987
1988 { "status", "Display current status of authentication information",
1989 EncryptStatus, 0, 0, 0 },
1990 { "help", 0, EncryptHelp, 0, 0, 0 },
1991 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
1992 { 0 },
1993 };
1994
1995 static int
EncryptHelp()1996 EncryptHelp()
1997 {
1998 struct encryptlist *c;
1999
2000 for (c = EncryptList; c->name; c++) {
2001 if (c->help) {
2002 if (*c->help)
2003 printf("%-15s %s\r\n", c->name, c->help);
2004 else
2005 printf("\r\n");
2006 }
2007 }
2008 return 0;
2009 }
2010
2011 static int
encrypt_cmd(int argc,char ** argv)2012 encrypt_cmd(int argc, char **argv)
2013 {
2014 struct encryptlist *c;
2015
2016 if (argc < 2) {
2017 fprintf(stderr, "Need at least one argument for 'encrypt' command.\n");
2018 fprintf(stderr, "('encrypt ?' for help)\n");
2019 return 0;
2020 }
2021
2022 c = (struct encryptlist *)
2023 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2024 if (c == 0) {
2025 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
2026 argv[1]);
2027 return 0;
2028 }
2029 if (Ambiguous(c)) {
2030 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
2031 argv[1]);
2032 return 0;
2033 }
2034 argc -= 2;
2035 if (argc < c->minarg || argc > c->maxarg) {
2036 if (c->minarg == c->maxarg) {
2037 fprintf(stderr, "Need %s%d argument%s ",
2038 c->minarg < argc ? "only " : "", c->minarg,
2039 c->minarg == 1 ? "" : "s");
2040 } else {
2041 fprintf(stderr, "Need %s%d-%d arguments ",
2042 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2043 }
2044 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\r\n",
2045 c->name);
2046 return 0;
2047 }
2048 if (c->needconnect && !connected) {
2049 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2050 printf("?Need to be connected first.\r\n");
2051 return 0;
2052 }
2053 }
2054 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2055 argc > 1 ? argv[3] : 0,
2056 argc > 2 ? argv[4] : 0));
2057 }
2058 #endif
2059
2060 #if defined(unix) && defined(TN3270)
2061 static void
filestuff(fd)2062 filestuff(fd)
2063 int fd;
2064 {
2065 int res;
2066
2067 #ifdef F_GETOWN
2068 setconnmode(0);
2069 res = fcntl(fd, F_GETOWN, 0);
2070 setcommandmode();
2071
2072 if (res == -1) {
2073 perror("fcntl");
2074 return;
2075 }
2076 printf("\tOwner is %d.\r\n", res);
2077 #endif
2078
2079 setconnmode(0);
2080 res = fcntl(fd, F_GETFL, 0);
2081 setcommandmode();
2082
2083 if (res == -1) {
2084 perror("fcntl");
2085 return;
2086 }
2087 #ifdef notdef
2088 printf("\tFlags are 0x%x: %s\r\n", res, decodeflags(res));
2089 #endif
2090 }
2091 #endif /* defined(unix) && defined(TN3270) */
2092
2093 /*
2094 * Print status about the connection.
2095 */
2096 /*ARGSUSED*/
2097 static int
status(argc,argv)2098 status(argc, argv)
2099 int argc;
2100 char *argv[];
2101 {
2102 if (connected) {
2103 printf("Connected to %s.\r\n", hostname);
2104 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2105 int mode = getconnmode();
2106
2107 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2108 printf("Operating with LINEMODE option\r\n");
2109 printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
2110 printf("%s catching of signals\r\n",
2111 (mode&MODE_TRAPSIG) ? "Local" : "No");
2112 slcstate();
2113 #ifdef KLUDGELINEMODE
2114 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2115 printf("Operating in obsolete linemode\r\n");
2116 #endif
2117 } else {
2118 printf("Operating in single character mode\r\n");
2119 if (localchars)
2120 printf("Catching signals locally\r\n");
2121 }
2122 printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2123 if (my_want_state_is_will(TELOPT_LFLOW))
2124 printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
2125 #if defined(ENCRYPTION)
2126 encrypt_display();
2127 #endif
2128 }
2129 } else {
2130 printf("No connection.\r\n");
2131 }
2132 # if !defined(TN3270)
2133 printf("Escape character is '%s'.\r\n", control(escape));
2134 (void) fflush(stdout);
2135 # else /* !defined(TN3270) */
2136 if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2137 printf("Escape character is '%s'.\r\n", control(escape));
2138 }
2139 # if defined(unix)
2140 if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2141 printf("SIGIO received %d time%s.\r\n",
2142 sigiocount, (sigiocount == 1)? "":"s");
2143 if (In3270) {
2144 printf("Process ID %ld, process group %ld.\r\n",
2145 (long)getpid(), (long)getpgrp());
2146 printf("Terminal input:\r\n");
2147 filestuff(tin);
2148 printf("Terminal output:\r\n");
2149 filestuff(tout);
2150 printf("Network socket:\r\n");
2151 filestuff(net);
2152 }
2153 }
2154 if (In3270 && transcom) {
2155 printf("Transparent mode command is '%s'.\r\n", transcom);
2156 }
2157 # endif /* defined(unix) */
2158 (void) fflush(stdout);
2159 if (In3270) {
2160 return 0;
2161 }
2162 # endif /* defined(TN3270) */
2163 fflush(stdout);
2164 return 1;
2165 }
2166
2167 #ifdef SIGINFO
2168 /*
2169 * Function that gets called when SIGINFO is received.
2170 */
2171 void
ayt_status()2172 ayt_status()
2173 {
2174 (void) call(status, "status", "notmuch", 0);
2175 }
2176 #endif
2177
2178 static Command *getcmd(char *name);
2179
2180 static void
cmdrc(char * m1,char * m2)2181 cmdrc(char *m1, char *m2)
2182 {
2183 static char rcname[128];
2184 Command *c;
2185 FILE *rcfile;
2186 int gotmachine = 0;
2187 int l1 = strlen(m1);
2188 int l2 = strlen(m2);
2189 char m1save[MAXHOSTNAMELEN];
2190
2191 if (skiprc)
2192 return;
2193
2194 strlcpy(m1save, m1, sizeof(m1save));
2195 m1 = m1save;
2196
2197 if (rcname[0] == 0) {
2198 char *home = getenv("HOME");
2199
2200 if (home == NULL || *home == '\0')
2201 return;
2202 snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
2203 home ? home : "");
2204 }
2205
2206 if ((rcfile = fopen(rcname, "r")) == 0) {
2207 return;
2208 }
2209
2210 for (;;) {
2211 if (fgets(line, sizeof(line), rcfile) == NULL)
2212 break;
2213 if (line[0] == 0)
2214 break;
2215 if (line[0] == '#')
2216 continue;
2217 if (gotmachine) {
2218 if (!isspace(line[0]))
2219 gotmachine = 0;
2220 }
2221 if (gotmachine == 0) {
2222 if (isspace(line[0]))
2223 continue;
2224 if (strncasecmp(line, m1, l1) == 0)
2225 strncpy(line, &line[l1], sizeof(line) - l1);
2226 else if (strncasecmp(line, m2, l2) == 0)
2227 strncpy(line, &line[l2], sizeof(line) - l2);
2228 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2229 strncpy(line, &line[7], sizeof(line) - 7);
2230 else
2231 continue;
2232 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2233 continue;
2234 gotmachine = 1;
2235 }
2236 makeargv();
2237 if (margv[0] == 0)
2238 continue;
2239 c = getcmd(margv[0]);
2240 if (Ambiguous(c)) {
2241 printf("?Ambiguous command: %s\r\n", margv[0]);
2242 continue;
2243 }
2244 if (c == 0) {
2245 printf("?Invalid command: %s\r\n", margv[0]);
2246 continue;
2247 }
2248 /*
2249 * This should never happen...
2250 */
2251 if (c->needconnect && !connected) {
2252 printf("?Need to be connected first for %s.\r\n", margv[0]);
2253 continue;
2254 }
2255 (*c->handler)(margc, margv);
2256 }
2257 fclose(rcfile);
2258 }
2259
2260
2261 int
tn(argc,argv)2262 tn(argc, argv)
2263 int argc;
2264 char *argv[];
2265 {
2266 struct addrinfo hints, *res, *res0;
2267 int error;
2268 struct sockaddr_in sin;
2269 unsigned long temp;
2270 extern char *inet_ntoa();
2271 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2272 char *srp = 0;
2273 int srlen;
2274 #endif
2275 char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
2276 int retry;
2277 const int niflags = NI_NUMERICHOST;
2278
2279 /* clear the socket address prior to use */
2280 memset((char *)&sin, 0, sizeof(sin));
2281
2282 if (connected) {
2283 printf("?Already connected to %s\r\n", hostname);
2284 return 0;
2285 }
2286 if (argc < 2) {
2287 strlcpy(line, "open ", sizeof(line));
2288 printf("(to) ");
2289 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2290 makeargv();
2291 argc = margc;
2292 argv = margv;
2293 }
2294 cmd = *argv;
2295 --argc; ++argv;
2296 while (argc) {
2297 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2298 goto usage;
2299 if (strcmp(*argv, "-l") == 0) {
2300 --argc; ++argv;
2301 if (argc == 0)
2302 goto usage;
2303 if ((user = strdup(*argv++)) == NULL)
2304 err(1, "strdup");
2305 --argc;
2306 continue;
2307 }
2308 if (strcmp(*argv, "-b") == 0) {
2309 --argc; ++argv;
2310 if (argc == 0)
2311 goto usage;
2312 aliasp = *argv++;
2313 --argc;
2314 continue;
2315 }
2316 if (strcmp(*argv, "-a") == 0) {
2317 --argc; ++argv;
2318 autologin = 1;
2319 continue;
2320 }
2321 if (hostp == 0) {
2322 hostp = *argv++;
2323 --argc;
2324 continue;
2325 }
2326 if (portp == 0) {
2327 portp = *argv++;
2328 --argc;
2329 continue;
2330 }
2331 usage:
2332 printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
2333 return 0;
2334 }
2335 if (hostp == 0)
2336 goto usage;
2337
2338 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2339 if (hostp[0] == '@' || hostp[0] == '!') {
2340 if ((hostname = strrchr(hostp, ':')) == NULL)
2341 hostname = strrchr(hostp, '@');
2342 hostname++;
2343 srp = 0;
2344 temp = sourceroute(hostp, &srp, &srlen);
2345 if (temp == 0) {
2346 herror(srp);
2347 return 0;
2348 } else if (temp == -1) {
2349 printf("Bad source route option: %s\r\n", hostp);
2350 return 0;
2351 } else {
2352 abort();
2353 }
2354 } else
2355 #endif
2356 {
2357 hostname = hostp;
2358 memset(&hints, 0, sizeof(hints));
2359 hints.ai_family = family;
2360 hints.ai_socktype = SOCK_STREAM;
2361 hints.ai_flags = AI_CANONNAME;
2362 if (portp == NULL) {
2363 portp = "telnet";
2364 telnetport = 1;
2365 } else if (*portp == '-') {
2366 portp++;
2367 telnetport = 1;
2368 } else
2369 telnetport = 0;
2370 h_errno = 0;
2371 error = getaddrinfo(hostp, portp, &hints, &res0);
2372 if (error) {
2373 if (error == EAI_SERVICE)
2374 warnx("%s: bad port", portp);
2375 else
2376 warnx("%s: %s", hostp, gai_strerror(error));
2377 if (h_errno)
2378 herror(hostp);
2379 return 0;
2380 }
2381 }
2382
2383 net = -1;
2384 retry = 0;
2385 for (res = res0; res; res = res->ai_next) {
2386 if (1 /* retry */) {
2387 char hbuf[NI_MAXHOST];
2388
2389 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
2390 NULL, 0, niflags) != 0) {
2391 strlcpy(hbuf, "(invalid)", sizeof(hbuf));
2392 }
2393 printf("Trying %s...\r\n", hbuf);
2394 }
2395 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2396 if (net < 0)
2397 continue;
2398
2399 if (aliasp) {
2400 struct addrinfo ahints, *ares;
2401
2402 memset(&ahints, 0, sizeof(ahints));
2403 ahints.ai_family = family;
2404 ahints.ai_socktype = SOCK_STREAM;
2405 ahints.ai_flags = AI_PASSIVE;
2406 error = getaddrinfo(aliasp, "0", &ahints, &ares);
2407 if (error) {
2408 warn("%s: %s", aliasp, gai_strerror(error));
2409 close(net);
2410 continue;
2411 }
2412 if (bind(net, ares->ai_addr, ares->ai_addrlen) < 0) {
2413 perror(aliasp);
2414 (void) close(net); /* dump descriptor */
2415 freeaddrinfo(ares);
2416 continue;
2417 }
2418 freeaddrinfo(ares);
2419 }
2420 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2421 if (srp && res->ai_family == AF_INET
2422 && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
2423 perror("setsockopt (IP_OPTIONS)");
2424 #endif
2425 #if defined(IPPROTO_IP) && defined(IP_TOS)
2426 if (res->ai_family == AF_INET) {
2427 # if defined(HAS_GETTOS)
2428 struct tosent *tp;
2429 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2430 tos = tp->t_tos;
2431 # endif
2432 if (tos < 0)
2433 tos = IPTOS_LOWDELAY; /* Low Delay bit */
2434 if (tos
2435 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2436 (void *)&tos, sizeof(int)) < 0)
2437 && (errno != ENOPROTOOPT))
2438 perror("telnet: setsockopt (IP_TOS) (ignored)");
2439 }
2440 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2441
2442 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2443 perror("setsockopt (SO_DEBUG)");
2444 }
2445
2446 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2447 char hbuf[NI_MAXHOST];
2448
2449 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
2450 NULL, 0, niflags) != 0) {
2451 strlcpy(hbuf, "(invalid)", sizeof(hbuf));
2452 }
2453 fprintf(stderr, "telnet: connect to address %s: %s\n", hbuf,
2454 strerror(errno));
2455
2456 close(net);
2457 net = -1;
2458 retry++;
2459 continue;
2460 }
2461
2462 connected++;
2463 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2464 auth_encrypt_connect(connected);
2465 #endif /* defined(AUTHENTICATION) */
2466 break;
2467 }
2468 freeaddrinfo(res0);
2469 if (net < 0) {
2470 return 0;
2471 }
2472 cmdrc(hostp, hostname);
2473 if (autologin && user == NULL) {
2474 struct passwd *pw;
2475
2476 user = getenv("USER");
2477 if (user == NULL ||
2478 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2479 if ((pw = getpwuid(getuid())) != NULL)
2480 user = pw->pw_name;
2481 else
2482 user = NULL;
2483 }
2484 }
2485 if (user) {
2486 env_define((unsigned char *)"USER", (unsigned char *)user);
2487 env_export((unsigned char *)"USER");
2488 }
2489 (void) call(status, "status", "notmuch", 0);
2490 if (setjmp(peerdied) == 0)
2491 telnet(user);
2492 (void) NetClose(net);
2493 ExitString("Connection closed by foreign host.\r\n",1);
2494 /*NOTREACHED*/
2495 return 0;
2496 }
2497
2498 #define HELPINDENT (sizeof ("connect"))
2499
2500 static char
2501 openhelp[] = "connect to a site",
2502 closehelp[] = "close current connection",
2503 logouthelp[] = "forcibly logout remote user and close the connection",
2504 quithelp[] = "exit telnet",
2505 statushelp[] = "print status information",
2506 helphelp[] = "print help information",
2507 sendhelp[] = "transmit special characters ('send ?' for more)",
2508 sethelp[] = "set operating parameters ('set ?' for more)",
2509 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2510 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2511 slchelp[] = "change state of special charaters ('slc ?' for more)",
2512 displayhelp[] = "display operating parameters",
2513 #if defined(TN3270) && defined(unix)
2514 transcomhelp[] = "specify Unix command for transparent mode pipe",
2515 #endif /* defined(TN3270) && defined(unix) */
2516 #if defined(AUTHENTICATION)
2517 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2518 #endif
2519 #if defined(ENCRYPTION)
2520 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2521 #endif
2522 zhelp[] = "suspend telnet",
2523 skeyhelp[] = "compute response to s/key challenge",
2524 shellhelp[] = "invoke a subshell",
2525 envhelp[] = "change environment variables ('environ ?' for more)",
2526 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2527
2528 static int help(int, char**);
2529
2530 static Command cmdtab[] = {
2531 { "close", closehelp, bye, 1 },
2532 { "logout", logouthelp, logout, 1 },
2533 { "display", displayhelp, display, 0 },
2534 { "mode", modestring, modecmd, 0 },
2535 { "open", openhelp, tn, 0 },
2536 { "quit", quithelp, quit, 0 },
2537 { "send", sendhelp, sendcmd, 0 },
2538 { "set", sethelp, setcmd, 0 },
2539 { "unset", unsethelp, unsetcmd, 0 },
2540 { "status", statushelp, status, 0 },
2541 { "toggle", togglestring, toggle, 0 },
2542 { "slc", slchelp, slccmd, 0 },
2543 #if defined(TN3270) && defined(unix)
2544 { "transcom", transcomhelp, settranscom, 0 },
2545 #endif /* defined(TN3270) && defined(unix) */
2546 #if defined(AUTHENTICATION)
2547 { "auth", authhelp, auth_cmd, 0 },
2548 #endif
2549 #if defined(ENCRYPTION)
2550 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2551 #endif
2552
2553 { "z", zhelp, telnetsuspend, 0 },
2554 #if defined(TN3270)
2555 { "!", shellhelp, shell, 1 },
2556 #else
2557 { "!", shellhelp, shell, 0 },
2558 #endif
2559 { "environ", envhelp, env_cmd, 0 },
2560 { "?", helphelp, help, 0 },
2561 { "skey", skeyhelp, skey_calc, 0 },
2562 { 0, 0, 0, 0 }
2563 };
2564
2565 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2566 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2567
2568 static Command cmdtab2[] = {
2569 { "help", 0, help, 0 },
2570 { "escape", escapehelp, setescape, 0 },
2571 { "crmod", crmodhelp, togcrmod, 0 },
2572 { 0, 0, 0, 0 }
2573 };
2574
2575
2576 /*
2577 * Call routine with argc, argv set from args (terminated by 0).
2578 */
2579
2580 /*VARARGS1*/
2581 static int
call(intrtn_t routine,...)2582 call(intrtn_t routine, ...)
2583 {
2584 va_list ap;
2585 char *args[100];
2586 int argno = 0;
2587
2588 va_start(ap, routine);
2589 while ((args[argno++] = va_arg(ap, char *)) != 0);
2590 va_end(ap);
2591 return (*routine)(argno-1, args);
2592 }
2593
2594
2595 static Command *
getcmd(name)2596 getcmd(name)
2597 char *name;
2598 {
2599 Command *cm;
2600
2601 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2602 return cm;
2603 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2604 }
2605
2606 void
command(top,tbuf,cnt)2607 command(top, tbuf, cnt)
2608 int top;
2609 char *tbuf;
2610 int cnt;
2611 {
2612 Command *c;
2613
2614 setcommandmode();
2615 if (!top) {
2616 putchar('\n');
2617 #if defined(unix)
2618 } else {
2619 (void) signal(SIGINT, SIG_DFL);
2620 (void) signal(SIGQUIT, SIG_DFL);
2621 #endif /* defined(unix) */
2622 }
2623 for (;;) {
2624 if (rlogin == _POSIX_VDISABLE)
2625 printf("%s> ", prompt);
2626 if (tbuf) {
2627 char *cp;
2628 cp = line;
2629 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2630 cnt--;
2631 tbuf = 0;
2632 if (cp == line || *--cp != '\n' || cp == line)
2633 goto getline;
2634 *cp = '\0';
2635 if (rlogin == _POSIX_VDISABLE)
2636 printf("%s\r\n", line);
2637 } else {
2638 getline:
2639 if (rlogin != _POSIX_VDISABLE)
2640 printf("%s> ", prompt);
2641 if (fgets(line, sizeof(line), stdin) == NULL) {
2642 if (feof(stdin) || ferror(stdin)) {
2643 (void) quit();
2644 /*NOTREACHED*/
2645 }
2646 break;
2647 }
2648 }
2649 if (line[0] == 0)
2650 break;
2651 makeargv();
2652 if (margv[0] == 0) {
2653 break;
2654 }
2655 c = getcmd(margv[0]);
2656 if (Ambiguous(c)) {
2657 printf("?Ambiguous command\r\n");
2658 continue;
2659 }
2660 if (c == 0) {
2661 printf("?Invalid command\r\n");
2662 continue;
2663 }
2664 if (c->needconnect && !connected) {
2665 printf("?Need to be connected first.\r\n");
2666 continue;
2667 }
2668 if ((*c->handler)(margc, margv)) {
2669 break;
2670 }
2671 }
2672 if (!top) {
2673 if (!connected) {
2674 longjmp(toplevel, 1);
2675 /*NOTREACHED*/
2676 }
2677 #if defined(TN3270)
2678 if (shell_active == 0) {
2679 setconnmode(0);
2680 }
2681 #else /* defined(TN3270) */
2682 setconnmode(0);
2683 #endif /* defined(TN3270) */
2684 }
2685 }
2686
2687 /*
2688 * Help command.
2689 */
2690 static int
help(argc,argv)2691 help(argc, argv)
2692 int argc;
2693 char *argv[];
2694 {
2695 Command *c;
2696
2697 if (argc == 1) {
2698 printf("Commands may be abbreviated. Commands are:\r\n\r\n");
2699 for (c = cmdtab; c->name; c++)
2700 if (c->help) {
2701 printf("%-*s\t%s\r\n", (int)HELPINDENT, c->name,
2702 c->help);
2703 }
2704 return 0;
2705 }
2706 while (--argc > 0) {
2707 char *arg;
2708 arg = *++argv;
2709 c = getcmd(arg);
2710 if (Ambiguous(c))
2711 printf("?Ambiguous help command %s\r\n", arg);
2712 else if (c == (Command *)0)
2713 printf("?Invalid help command %s\r\n", arg);
2714 else
2715 printf("%s\r\n", c->help);
2716 }
2717 return 0;
2718 }
2719
2720 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2721
2722 /*
2723 * Source route is handed in as
2724 * [!]@hop1@hop2...[@|:]dst
2725 * If the leading ! is present, it is a
2726 * strict source route, otherwise it is
2727 * assmed to be a loose source route.
2728 *
2729 * We fill in the source route option as
2730 * hop1,hop2,hop3...dest
2731 * and return a pointer to hop1, which will
2732 * be the address to connect() to.
2733 *
2734 * Arguments:
2735 * arg: pointer to route list to decipher
2736 *
2737 * cpp: If *cpp is not equal to NULL, this is a
2738 * pointer to a pointer to a character array
2739 * that should be filled in with the option.
2740 *
2741 * lenp: pointer to an integer that contains the
2742 * length of *cpp if *cpp != NULL.
2743 *
2744 * Return values:
2745 *
2746 * Returns the address of the host to connect to. If the
2747 * return value is -1, there was a syntax error in the
2748 * option, either unknown characters, or too many hosts.
2749 * If the return value is 0, one of the hostnames in the
2750 * path is unknown, and *cpp is set to point to the bad
2751 * hostname.
2752 *
2753 * *cpp: If *cpp was equal to NULL, it will be filled
2754 * in with a pointer to our static area that has
2755 * the option filled in. This will be 32bit aligned.
2756 *
2757 * *lenp: This will be filled in with how long the option
2758 * pointed to by *cpp is.
2759 *
2760 */
2761 unsigned long
sourceroute(arg,cpp,lenp)2762 sourceroute(arg, cpp, lenp)
2763 char *arg;
2764 char **cpp;
2765 int *lenp;
2766 {
2767 static char lsr[44];
2768 #ifdef sysV88
2769 static IOPTN ipopt;
2770 #endif
2771 char *cp, *cp2, *lsrp, *lsrep;
2772 int tmp;
2773 struct in_addr sin_addr;
2774 struct hostent *host = 0;
2775 char c;
2776
2777 /*
2778 * Verify the arguments, and make sure we have
2779 * at least 7 bytes for the option.
2780 */
2781 if (cpp == NULL || lenp == NULL)
2782 return((unsigned long)-1);
2783 if (*cpp != NULL && *lenp < 7)
2784 return((unsigned long)-1);
2785 /*
2786 * Decide whether we have a buffer passed to us,
2787 * or if we need to use our own static buffer.
2788 */
2789 if (*cpp) {
2790 lsrp = *cpp;
2791 lsrep = lsrp + *lenp;
2792 } else {
2793 *cpp = lsrp = lsr;
2794 lsrep = lsrp + 44;
2795 }
2796
2797 cp = arg;
2798
2799 /*
2800 * Next, decide whether we have a loose source
2801 * route or a strict source route, and fill in
2802 * the begining of the option.
2803 */
2804 #ifndef sysV88
2805 if (*cp == '!') {
2806 cp++;
2807 *lsrp++ = IPOPT_SSRR;
2808 } else
2809 *lsrp++ = IPOPT_LSRR;
2810 #else
2811 if (*cp == '!') {
2812 cp++;
2813 ipopt.io_type = IPOPT_SSRR;
2814 } else
2815 ipopt.io_type = IPOPT_LSRR;
2816 #endif
2817
2818 if (*cp != '@')
2819 return((unsigned long)-1);
2820
2821 #ifndef sysV88
2822 lsrp++; /* skip over length, we'll fill it in later */
2823 *lsrp++ = 4;
2824 #endif
2825
2826 cp++;
2827
2828 sin_addr.s_addr = 0;
2829
2830 for (c = 0;;) {
2831 if (c == ':')
2832 cp2 = 0;
2833 else for (cp2 = cp; (c = *cp2); cp2++) {
2834 if (c == ',') {
2835 *cp2++ = '\0';
2836 if (*cp2 == '@')
2837 cp2++;
2838 } else if (c == '@') {
2839 *cp2++ = '\0';
2840 } else if (c == ':') {
2841 *cp2++ = '\0';
2842 } else
2843 continue;
2844 break;
2845 }
2846 if (!c)
2847 cp2 = 0;
2848
2849 if ((tmp = inet_addr(cp)) != -1) {
2850 sin_addr.s_addr = tmp;
2851 } else if ((host = gethostbyname(cp))) {
2852 #if defined(h_addr)
2853 memmove((caddr_t)&sin_addr,
2854 host->h_addr_list[0],
2855 sizeof(sin_addr));
2856 #else
2857 memmove((caddr_t)&sin_addr, host->h_addr,
2858 sizeof(sin_addr));
2859 #endif
2860 } else {
2861 *cpp = cp;
2862 return(0);
2863 }
2864 memmove(lsrp, (char *)&sin_addr, 4);
2865 lsrp += 4;
2866 if (cp2)
2867 cp = cp2;
2868 else
2869 break;
2870 /*
2871 * Check to make sure there is space for next address
2872 */
2873 if (lsrp + 4 > lsrep)
2874 return((unsigned long)-1);
2875 }
2876 #ifndef sysV88
2877 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2878 *cpp = 0;
2879 *lenp = 0;
2880 return((unsigned long)-1);
2881 }
2882 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2883 *lenp = lsrp - *cpp;
2884 #else
2885 ipopt.io_len = lsrp - *cpp;
2886 if (ipopt.io_len <= 5) { /* Is 3 better ? */
2887 *cpp = 0;
2888 *lenp = 0;
2889 return((unsigned long)-1);
2890 }
2891 *lenp = sizeof(ipopt);
2892 *cpp = (char *) &ipopt;
2893 #endif
2894 return(sin_addr.s_addr);
2895 }
2896 #endif
2897