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