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