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