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