2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
41 #include <sys/param.h>
42 #if defined(CRAY) || defined(sysV88)
43 #include <sys/types.h>
47 #include <sys/types.h>
48 #endif /* defined(unix) */
49 #include <sys/socket.h>
50 #include <netinet/in.h>
64 #include <arpa/telnet.h>
74 #if defined(AUTHENTICATION)
75 #include <libtelnet/auth.h>
77 #if defined(ENCRYPTION)
78 #include <libtelnet/encrypt.h>
81 #if !defined(CRAY) && !defined(sysV88)
82 #include <netinet/in_systm.h>
83 # if (defined(vax) || defined(tahoe) || defined(hp300)) && !defined(ultrix)
84 # include <machine/endian.h>
86 #endif /* !defined(CRAY) && !defined(sysV88) */
87 #include <netinet/ip.h>
88 #include <netinet/ip6.h>
91 #ifndef MAXHOSTNAMELEN
92 #define MAXHOSTNAMELEN 64
95 #if defined(IPPROTO_IP) && defined(IP_TOS)
97 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
100 static char _hostname[MAXHOSTNAMELEN];
102 extern char *getenv();
104 extern int isprefix();
105 extern char **genget();
106 extern int Ambiguous();
108 static int help(int argc, char *argv[]);
110 static void cmdrc(char *m1, char *m2);
111 static int switch_af(struct addrinfo **aip);
116 char *name; /* command name */
117 char *help; /* help string (NULL for no help) */
118 int (*handler)(); /* routine which executes command */
119 int needconnect; /* Do we need to be connected to execute? */
122 static char line[256];
123 static char saveline[256];
125 static char *margv[20];
128 #include <sys/wait.h>
129 #define PATH_SKEY "/usr/bin/key"
131 skey_calc(argc, argv)
138 printf("%s sequence challenge\n", argv[0]);
144 execv(PATH_SKEY, argv);
150 (void) wait(&status);
151 if (WIFEXITED(status))
152 return (WEXITSTATUS(status));
161 register char *cp, *cp2, c;
162 register char **argp = margv;
166 if (*cp == '!') { /* Special case shell escape */
167 strcpy(saveline, line); /* save for shell command */
168 *argp++ = "!"; /* No room in string to get this */
173 register int inquote = 0;
180 for (cp2 = cp; c != '\0'; c = *++cp) {
188 if ((c = *++cp) == '\0')
190 } else if (c == '"') {
193 } else if (c == '\'') {
196 } else if (isspace(c))
210 * Make a character string into a number.
212 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
226 c = b | 0x40; /* DEL */
239 * Construct a control character sequence
240 * for a special character.
248 * The only way I could get the Sun 3.5 compiler
250 * if ((unsigned int)c >= 0x80)
251 * was to assign "c" to an unsigned int variable...
254 register unsigned int uic = (unsigned int)c;
258 if (c == (cc_t)_POSIX_VDISABLE) {
263 buf[1] = ((c>>6)&07) + '0';
264 buf[2] = ((c>>3)&07) + '0';
265 buf[3] = (c&07) + '0';
267 } else if (uic >= 0x20) {
281 * The following are data structures and routines for
282 * the "send" command.
287 char *name; /* How user refers to it (case independent) */
288 char *help; /* Help information (0 ==> no help) */
289 int needconnect; /* Need to be connected */
290 int narg; /* Number of arguments */
291 int (*handler)(); /* Routine to perform (for special ops) */
292 int nbyte; /* Number of bytes to send this command */
293 int what; /* Character to be sent (<0 ==> special) */
300 send_docmd P((char *)),
301 send_dontcmd P((char *)),
302 send_willcmd P((char *)),
303 send_wontcmd P((char *));
305 static struct sendlist Sendlist[] = {
306 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
307 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
308 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
309 { "break", 0, 1, 0, 0, 2, BREAK },
310 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
311 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
312 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
313 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
314 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
315 { "intp", 0, 1, 0, 0, 2, IP },
316 { "interrupt", 0, 1, 0, 0, 2, IP },
317 { "intr", 0, 1, 0, 0, 2, IP },
318 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
319 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
320 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
321 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
322 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
323 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
324 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
325 { "?", "Display send options", 0, 0, send_help, 0, 0 },
326 { "help", 0, 0, 0, send_help, 0, 0 },
327 { "do", 0, 0, 1, send_docmd, 3, 0 },
328 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
329 { "will", 0, 0, 1, send_willcmd, 3, 0 },
330 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
334 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
335 sizeof(struct sendlist)))
342 int count; /* how many bytes we are going to need to send */
344 struct sendlist *s; /* pointer to current command */
349 printf("need at least one argument for 'send' command\n");
350 printf("'send ?' for help\n");
354 * First, validate all the send arguments.
355 * In addition, we see how much space we are going to need, and
356 * whether or not we will be doing a "SYNCH" operation (which
357 * flushes the network queue).
360 for (i = 1; i < argc; i++) {
361 s = GETSEND(argv[i]);
363 printf("Unknown send argument '%s'\n'send ?' for help.\n",
366 } else if (Ambiguous(s)) {
367 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
371 if (i + s->narg >= argc) {
373 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
374 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
378 if (s->handler == send_help) {
384 needconnect += s->needconnect;
386 if (!connected && needconnect) {
387 printf("?Need to be connected first.\n");
388 printf("'send ?' for help\n");
391 /* Now, do we have enough room? */
392 if (NETROOM() < count) {
393 printf("There is not enough room in the buffer TO the network\n");
394 printf("to process your request. Nothing will be done.\n");
395 printf("('send synch' will throw away most data in the network\n");
396 printf("buffer, if this might help.)\n");
399 /* OK, they are all OK, now go through again and actually send */
401 for (i = 1; i < argc; i++) {
402 if ((s = GETSEND(argv[i])) == 0) {
403 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
409 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
410 (s->narg > 1) ? argv[i+2] : 0);
413 NET2ADD(IAC, s->what);
414 printoption("SENT", IAC, s->what);
417 return (count == success);
431 return(send_tncmd(send_do, "do", name));
438 return(send_tncmd(send_dont, "dont", name));
444 return(send_tncmd(send_will, "will", name));
450 return(send_tncmd(send_wont, "wont", name));
454 send_tncmd(func, cmd, name)
459 extern char *telopts[];
460 register int val = 0;
462 if (isprefix(name, "help") || isprefix(name, "?")) {
463 register int col, len;
465 printf("Usage: send %s <value|option>\n", cmd);
466 printf("\"value\" must be from 0 to 255\n");
467 printf("Valid options are:\n\t");
470 for (cpp = telopts; *cpp; cpp++) {
471 len = strlen(*cpp) + 3;
472 if (col + len > 65) {
476 printf(" \"%s\"", *cpp);
482 cpp = (char **)genget(name, telopts, sizeof(char *));
483 if (Ambiguous(cpp)) {
484 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
491 register char *cp = name;
493 while (*cp >= '0' && *cp <= '9') {
499 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
502 } else if (val < 0 || val > 255) {
503 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
509 printf("?Need to be connected first.\n");
519 struct sendlist *s; /* pointer to current command */
520 for (s = Sendlist; s->name; s++) {
522 printf("%-15s %s\n", s->name, s->help);
528 * The following are the routines and data structures referred
529 * to by the arguments to the "toggle" command.
544 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
545 perror("setsockopt (SO_DEBUG)");
549 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
550 perror("setsockopt (SO_DEBUG)");
552 printf("Cannot turn off socket debugging\n");
562 printf("Will send carriage returns as telnet <CR><LF>.\n");
564 printf("Will send carriage returns as telnet <CR><NUL>.\n");
575 donebinarytoggle = 1;
580 if (my_want_state_is_will(TELOPT_BINARY) &&
581 my_want_state_is_do(TELOPT_BINARY)) {
583 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
584 my_want_state_is_dont(TELOPT_BINARY)) {
587 val = binmode ? 0 : 1;
591 if (my_want_state_is_will(TELOPT_BINARY) &&
592 my_want_state_is_do(TELOPT_BINARY)) {
593 printf("Already operating in binary mode with remote host.\n");
595 printf("Negotiating binary mode with remote host.\n");
599 if (my_want_state_is_wont(TELOPT_BINARY) &&
600 my_want_state_is_dont(TELOPT_BINARY)) {
601 printf("Already in network ascii mode with remote host.\n");
603 printf("Negotiating network ascii mode with remote host.\n");
614 donebinarytoggle = 1;
617 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
620 if (my_want_state_is_do(TELOPT_BINARY)) {
621 printf("Already receiving in binary mode.\n");
623 printf("Negotiating binary mode on input.\n");
627 if (my_want_state_is_dont(TELOPT_BINARY)) {
628 printf("Already receiving in network ascii mode.\n");
630 printf("Negotiating network ascii mode on input.\n");
641 donebinarytoggle = 1;
644 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
647 if (my_want_state_is_will(TELOPT_BINARY)) {
648 printf("Already transmitting in binary mode.\n");
650 printf("Negotiating binary mode on output.\n");
654 if (my_want_state_is_wont(TELOPT_BINARY)) {
655 printf("Already transmitting in network ascii mode.\n");
657 printf("Negotiating network ascii mode on output.\n");
665 static int togglehelp P((void));
666 #if defined(AUTHENTICATION)
667 extern int auth_togdebug P((int));
670 extern int EncryptAutoEnc P((int));
671 extern int EncryptAutoDec P((int));
672 extern int EncryptDebug P((int));
673 extern int EncryptVerbose P((int));
674 #endif /* ENCRYPTION */
677 char *name; /* name of toggle */
678 char *help; /* help message */
679 int (*handler)(); /* routine to do actual setting */
681 char *actionexplanation;
684 static struct togglelist Togglelist[] = {
686 "flushing of output when sending interrupt characters",
689 "flush output when sending interrupt characters" },
691 "automatic sending of interrupt characters in urgent mode",
694 "send interrupt characters in urgent mode" },
695 #if defined(AUTHENTICATION)
697 "automatic sending of login and/or authentication info",
700 "send login name and/or authentication information" },
702 "Toggle authentication debugging",
705 "print authentication debugging information" },
709 "automatic encryption of data stream",
712 "automatically encrypt output" },
714 "automatic decryption of data stream",
717 "automatically decrypt input" },
719 "Toggle verbose encryption output",
722 "print verbose encryption output" },
724 "Toggle encryption debugging",
727 "print encryption debugging information" },
728 #endif /* ENCRYPTION */
730 "don't read ~/.telnetrc file",
733 "skip reading of ~/.telnetrc file" },
735 "sending and receiving of binary data",
740 "receiving of binary data",
745 "sending of binary data",
750 "sending carriage returns as telnet <CR><LF>",
755 "mapping of received carriage returns",
758 "map carriage return on output" },
760 "local recognition of certain control characters",
763 "recognize certain control characters" },
764 { " ", "", 0 }, /* empty line */
765 #if defined(unix) && defined(TN3270)
767 "(debugging) toggle tracing of API transactions",
770 "trace API transactions" },
772 "(debugging) toggle printing of hexadecimal curses data",
775 "print hexadecimal representation of curses data" },
776 #endif /* defined(unix) && defined(TN3270) */
781 "turn on socket level debugging" },
783 "printing of hexadecimal network data (debugging)",
786 "print hexadecimal representation of network traffic" },
788 "output of \"netdata\" to user readable format (debugging)",
791 "print user readable output for \"netdata\"" },
793 "viewing of options processing (debugging)",
796 "show option processing" },
799 "(debugging) toggle printing of hexadecimal terminal data",
802 "print hexadecimal representation of terminal traffic" },
803 #endif /* defined(unix) */
816 struct togglelist *c;
818 for (c = Togglelist; c->name; c++) {
821 printf("%-15s toggle %s\n", c->name, c->help);
827 printf("%-15s %s\n", "?", "display help information");
835 struct togglelist *c;
837 for (c = Togglelist; c->name; c++) {
840 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
848 #define GETTOGGLE(name) (struct togglelist *) \
849 genget(name, (char **) Togglelist, sizeof(struct togglelist))
858 struct togglelist *c;
862 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
871 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
875 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
880 *c->variable = !*c->variable; /* invert it */
881 if (c->actionexplanation) {
882 printf("%s %s.\n", *c->variable? "Will" : "Won't",
883 c->actionexplanation);
887 retval &= (*c->handler)(-1);
895 * The following perform the "set" command.
899 struct termio new_tc = { 0 };
903 char *name; /* name */
904 char *help; /* help information */
906 cc_t *charp; /* where it is located at */
909 static struct setlist Setlist[] = {
910 #ifdef KLUDGELINEMODE
911 { "echo", "character to toggle local echoing on/off", 0, &echoc },
913 { "escape", "character to escape back to telnet command mode", 0, &escape },
914 { "rlogin", "rlogin escape character", 0, &rlogin },
915 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
917 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
918 { "flushoutput", "character to cause an Abort Output", 0, termFlushCharp },
919 { "interrupt", "character to cause an Interrupt Process", 0, termIntCharp },
920 { "quit", "character to cause an Abort process", 0, termQuitCharp },
921 { "eof", "character to cause an EOF ", 0, termEofCharp },
923 { " ", "The following are for local editing in linemode", 0, 0 },
924 { "erase", "character to use to erase a character", 0, termEraseCharp },
925 { "kill", "character to use to erase a line", 0, termKillCharp },
926 { "lnext", "character to use for literal next", 0, termLiteralNextCharp },
927 { "susp", "character to cause a Suspend Process", 0, termSuspCharp },
928 { "reprint", "character to use for line reprint", 0, termRprntCharp },
929 { "worderase", "character to use to erase a word", 0, termWerasCharp },
930 { "start", "character to use for XON", 0, termStartCharp },
931 { "stop", "character to use for XOFF", 0, termStopCharp },
932 { "forw1", "alternate end of line character", 0, termForw1Charp },
933 { "forw2", "alternate end of line character", 0, termForw2Charp },
934 { "ayt", "alternate AYT character", 0, termAytCharp },
938 #if defined(CRAY) && !defined(__STDC__)
939 /* Work around compiler bug in pcc 4.1.5 */
943 #ifndef KLUDGELINEMODE
948 Setlist[N+0].charp = &termFlushChar;
949 Setlist[N+1].charp = &termIntChar;
950 Setlist[N+2].charp = &termQuitChar;
951 Setlist[N+3].charp = &termEofChar;
952 Setlist[N+6].charp = &termEraseChar;
953 Setlist[N+7].charp = &termKillChar;
954 Setlist[N+8].charp = &termLiteralNextChar;
955 Setlist[N+9].charp = &termSuspChar;
956 Setlist[N+10].charp = &termRprntChar;
957 Setlist[N+11].charp = &termWerasChar;
958 Setlist[N+12].charp = &termStartChar;
959 Setlist[N+13].charp = &termStopChar;
960 Setlist[N+14].charp = &termForw1Char;
961 Setlist[N+15].charp = &termForw2Char;
962 Setlist[N+16].charp = &termAytChar;
965 #endif /* defined(CRAY) && !defined(__STDC__) */
967 static struct setlist *
971 return (struct setlist *)
972 genget(name, (char **) Setlist, sizeof(struct setlist));
979 if (rlogin != _POSIX_VDISABLE) {
980 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
981 printf("Telnet rlogin escape character is '%s'.\n",
984 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
985 printf("Telnet escape character is '%s'.\n", control(escape));
996 struct togglelist *c;
998 if (argc < 2 || argc > 3) {
999 printf("Format is 'set Name Value'\n'set ?' for help.\n");
1002 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
1003 for (ct = Setlist; ct->name; ct++)
1004 printf("%-15s %s\n", ct->name, ct->help);
1007 printf("%-15s %s\n", "?", "display help information");
1011 ct = getset(argv[1]);
1013 c = GETTOGGLE(argv[1]);
1015 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
1018 } else if (Ambiguous(c)) {
1019 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
1024 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
1026 else if (strcmp("off", argv[2]) == 0)
1029 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
1032 if (c->actionexplanation) {
1033 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1034 c->actionexplanation);
1039 } else if (argc != 3) {
1040 printf("Format is 'set Name Value'\n'set ?' for help.\n");
1042 } else if (Ambiguous(ct)) {
1043 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
1046 } else if (ct->handler) {
1047 (*ct->handler)(argv[2]);
1048 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
1050 if (strcmp("off", argv[2])) {
1051 value = special(argv[2]);
1053 value = _POSIX_VDISABLE;
1055 *(ct->charp) = (cc_t)value;
1056 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1063 unsetcmd(argc, argv)
1068 struct togglelist *c;
1069 register char *name;
1073 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1076 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1077 for (ct = Setlist; ct->name; ct++)
1078 printf("%-15s %s\n", ct->name, ct->help);
1081 printf("%-15s %s\n", "?", "display help information");
1091 c = GETTOGGLE(name);
1093 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1096 } else if (Ambiguous(c)) {
1097 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1103 if (c->actionexplanation) {
1104 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1105 c->actionexplanation);
1110 } else if (Ambiguous(ct)) {
1111 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1114 } else if (ct->handler) {
1116 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1118 *(ct->charp) = _POSIX_VDISABLE;
1119 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1126 * The following are the data structures and routines for the
1129 #ifdef KLUDGELINEMODE
1130 extern int kludgelinemode;
1136 send_wont(TELOPT_LINEMODE, 1);
1137 send_dont(TELOPT_SGA, 1);
1138 send_dont(TELOPT_ECHO, 1);
1146 #ifdef KLUDGELINEMODE
1148 send_dont(TELOPT_SGA, 1);
1150 send_will(TELOPT_LINEMODE, 1);
1151 send_dont(TELOPT_ECHO, 1);
1158 #ifdef KLUDGELINEMODE
1160 send_do(TELOPT_SGA, 1);
1163 send_wont(TELOPT_LINEMODE, 1);
1164 send_do(TELOPT_ECHO, 1);
1173 extern int linemode;
1175 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1176 printf("?Need to have LINEMODE option enabled first.\n");
1177 printf("'mode ?' for help.\n");
1182 c = (linemode | bit);
1184 c = (linemode & ~bit);
1192 return dolmmode(bit, 1);
1198 return dolmmode(bit, 0);
1202 char *name; /* command name */
1203 char *help; /* help string */
1204 int (*handler)(); /* routine which executes command */
1205 int needconnect; /* Do we need to be connected to execute? */
1209 extern int modehelp();
1211 static struct modelist ModeList[] = {
1212 { "character", "Disable LINEMODE option", docharmode, 1 },
1213 #ifdef KLUDGELINEMODE
1214 { "", "(or disable obsolete line-by-line mode)", 0 },
1216 { "line", "Enable LINEMODE option", dolinemode, 1 },
1217 #ifdef KLUDGELINEMODE
1218 { "", "(or enable obsolete line-by-line mode)", 0 },
1221 { "", "These require the LINEMODE option to be enabled", 0 },
1222 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1223 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1224 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1225 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1226 { "+edit", 0, setmod, 1, MODE_EDIT },
1227 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1228 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1229 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1230 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1231 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1232 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1233 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1234 { "help", 0, modehelp, 0 },
1235 #ifdef KLUDGELINEMODE
1236 { "kludgeline", 0, dokludgemode, 1 },
1239 { "?", "Print help information", modehelp, 0 },
1247 struct modelist *mt;
1249 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1250 for (mt = ModeList; mt->name; mt++) {
1253 printf("%-15s %s\n", mt->name, mt->help);
1261 #define GETMODECMD(name) (struct modelist *) \
1262 genget(name, (char **) ModeList, sizeof(struct modelist))
1269 struct modelist *mt;
1272 printf("'mode' command requires an argument\n");
1273 printf("'mode ?' for help.\n");
1274 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1275 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1276 } else if (Ambiguous(mt)) {
1277 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1278 } else if (mt->needconnect && !connected) {
1279 printf("?Need to be connected first.\n");
1280 printf("'mode ?' for help.\n");
1281 } else if (mt->handler) {
1282 return (*mt->handler)(mt->arg1);
1288 * The following data structures and routines implement the
1289 * "display" command.
1297 struct togglelist *tl;
1300 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1301 if (*tl->variable) { \
1306 printf(" %s.\n", tl->actionexplanation); \
1309 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1310 if (sl->handler == 0) \
1311 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1313 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1317 for (tl = Togglelist; tl->name; tl++) {
1321 for (sl = Setlist; sl->name; sl++) {
1327 for (i = 1; i < argc; i++) {
1328 sl = getset(argv[i]);
1329 tl = GETTOGGLE(argv[i]);
1330 if (Ambiguous(sl) || Ambiguous(tl)) {
1331 printf("?Ambiguous argument '%s'.\n", argv[i]);
1333 } else if (!sl && !tl) {
1334 printf("?Unknown argument '%s'.\n", argv[i]);
1346 /*@*/optionstatus();
1349 #endif /* ENCRYPTION */
1356 * The following are the data structures, and many of the routines,
1357 * relating to command processing.
1361 * Set the escape character.
1364 setescape(argc, argv)
1372 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1373 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1377 printf("new escape character: ");
1378 (void) fgets(buf, sizeof(buf), stdin);
1384 printf("Escape character is '%s'.\n", control(escape));
1386 (void) fflush(stdout);
1395 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1396 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1397 (void) fflush(stdout);
1408 long oldrows, oldcols, newrows, newcols, err;
1410 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1411 (void) kill(0, SIGTSTP);
1413 * If we didn't get the window size before the SUSPEND, but we
1414 * can get them now (?), then send the NAWS to make sure that
1415 * we are set up for the right window size.
1417 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1418 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1422 /* reget parameters in case they were changed */
1423 TerminalSaveState();
1426 printf("Suspend is not supported. Try the '!' command instead\n");
1431 #if !defined(TN3270)
1438 long oldrows, oldcols, newrows, newcols, err;
1442 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1445 perror("Fork failed\n");
1451 * Fire up the shell in the child.
1453 register char *shellp, *shellname;
1454 extern char *strrchr();
1456 shellp = getenv("SHELL");
1459 if ((shellname = strrchr(shellp, '/')) == 0)
1464 execl(shellp, shellname, "-c", &saveline[1], 0);
1466 execl(shellp, shellname, 0);
1471 (void)wait((int *)0); /* Wait for the shell to complete */
1473 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1474 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1481 #else /* !defined(TN3270) */
1483 #endif /* !defined(TN3270) */
1488 int argc; /* Number of arguments */
1489 char *argv[]; /* arguments */
1491 extern int resettermname;
1494 (void) shutdown(net, 2);
1495 printf("Connection closed.\n");
1496 (void) NetClose(net);
1499 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1500 auth_encrypt_connect(connected);
1501 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
1505 SetIn3270(); /* Get out of 3270 mode */
1506 #endif /* defined(TN3270) */
1508 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1509 longjmp(toplevel, 1);
1512 return 1; /* Keep lint, etc., happy */
1519 (void) call(bye, "bye", "fromquit", 0);
1528 send_do(TELOPT_LOGOUT, 1);
1545 static void slc_help();
1547 struct slclist SlcList[] = {
1548 { "export", "Use local special character definitions",
1549 slc_mode_export, 0 },
1550 { "import", "Use remote special character definitions",
1551 slc_mode_import, 1 },
1552 { "check", "Verify remote special character definitions",
1553 slc_mode_import, 0 },
1554 { "help", 0, slc_help, 0 },
1555 { "?", "Print help information", slc_help, 0 },
1564 for (c = SlcList; c->name; c++) {
1567 printf("%-15s %s\n", c->name, c->help);
1574 static struct slclist *
1578 return (struct slclist *)
1579 genget(name, (char **) SlcList, sizeof(struct slclist));
1591 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1594 c = getslc(argv[1]);
1596 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1601 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1605 (*c->handler)(c->arg);
1611 * The ENVIRON command.
1621 extern struct env_lst *
1622 env_define P((unsigned char *, unsigned char *));
1624 env_undefine P((unsigned char *)),
1625 env_export P((unsigned char *)),
1626 env_unexport P((unsigned char *)),
1627 env_send P((unsigned char *)),
1628 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1629 env_varval P((unsigned char *)),
1635 struct envlist EnvList[] = {
1636 { "define", "Define an environment variable",
1637 (void (*)())env_define, 2 },
1638 { "undefine", "Undefine an environment variable",
1640 { "export", "Mark an environment variable for automatic export",
1642 { "unexport", "Don't mark an environment variable for automatic export",
1644 { "send", "Send an environment variable", env_send, 1 },
1645 { "list", "List the current environment variables",
1647 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1648 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1651 { "help", 0, env_help, 0 },
1652 { "?", "Print help information", env_help, 0 },
1661 for (c = EnvList; c->name; c++) {
1664 printf("%-15s %s\n", c->name, c->help);
1671 static struct envlist *
1675 return (struct envlist *)
1676 genget(name, (char **) EnvList, sizeof(struct envlist));
1688 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1691 c = getenvcmd(argv[1]);
1693 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1698 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1702 if (c->narg + 2 != argc) {
1704 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1705 c->narg < argc + 2 ? "only " : "",
1706 c->narg, c->narg == 1 ? "" : "s", c->name);
1709 (*c->handler)(argv[2], argv[3]);
1714 struct env_lst *next; /* pointer to next structure */
1715 struct env_lst *prev; /* pointer to previous structure */
1716 unsigned char *var; /* pointer to variable name */
1717 unsigned char *value; /* pointer to variable value */
1718 int export; /* 1 -> export with default list of variables */
1719 int welldefined; /* A well defined variable */
1722 struct env_lst envlisthead;
1728 register struct env_lst *ep;
1730 for (ep = envlisthead.next; ep; ep = ep->next) {
1731 if (strcmp((char *)ep->var, (char *)var) == 0)
1740 extern char **environ;
1741 register char **epp, *cp;
1742 register struct env_lst *ep;
1743 extern char *strchr();
1745 for (epp = environ; *epp; epp++) {
1746 if ((cp = strchr(*epp, '='))) {
1748 ep = env_define((unsigned char *)*epp,
1749 (unsigned char *)cp+1);
1755 * Special case for DISPLAY variable. If it is ":0.0" or
1756 * "unix:0.0", we have to get rid of "unix" and insert our
1759 if ((ep = env_find("DISPLAY"))
1760 && ((*ep->value == ':')
1761 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1763 char *cp2 = strchr((char *)ep->value, ':');
1765 gethostname(hbuf, 256);
1767 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1768 sprintf((char *)cp, "%s%s", hbuf, cp2);
1770 ep->value = (unsigned char *)cp;
1773 * If USER is not defined, but LOGNAME is, then add
1774 * USER with the value from LOGNAME. By default, we
1775 * don't export the USER variable.
1777 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1778 env_define((unsigned char *)"USER", ep->value);
1779 env_unexport((unsigned char *)"USER");
1781 env_export((unsigned char *)"DISPLAY");
1782 env_export((unsigned char *)"PRINTER");
1786 env_define(var, value)
1787 unsigned char *var, *value;
1789 register struct env_lst *ep;
1791 if ((ep = env_find(var))) {
1797 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1798 ep->next = envlisthead.next;
1799 envlisthead.next = ep;
1800 ep->prev = &envlisthead;
1802 ep->next->prev = ep;
1804 ep->welldefined = opt_welldefined(var);
1806 ep->var = (unsigned char *)strdup((char *)var);
1807 ep->value = (unsigned char *)strdup((char *)value);
1815 register struct env_lst *ep;
1817 if ((ep = env_find(var))) {
1818 ep->prev->next = ep->next;
1820 ep->next->prev = ep->prev;
1833 register struct env_lst *ep;
1835 if ((ep = env_find(var)))
1843 register struct env_lst *ep;
1845 if ((ep = env_find(var)))
1853 register struct env_lst *ep;
1855 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1857 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1861 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1867 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1871 env_opt_start_info();
1872 env_opt_add(ep->var);
1879 register struct env_lst *ep;
1881 for (ep = envlisthead.next; ep; ep = ep->next) {
1882 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1883 ep->var, ep->value);
1888 env_default(init, welldefined)
1891 static struct env_lst *nep = NULL;
1898 while ((nep = nep->next)) {
1899 if (nep->export && (nep->welldefined == welldefined))
1910 register struct env_lst *ep;
1912 if ((ep = env_find(var)))
1917 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1920 unsigned char *what;
1922 extern int old_env_var, old_env_value, env_auto;
1923 int len = strlen((char *)what);
1928 if (strncasecmp((char *)what, "status", len) == 0) {
1930 printf("%s%s", "VAR and VALUE are/will be ",
1931 "determined automatically\n");
1932 if (old_env_var == OLD_ENV_VAR)
1933 printf("VAR and VALUE set to correct definitions\n");
1935 printf("VAR and VALUE definitions are reversed\n");
1936 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1938 old_env_var = OLD_ENV_VALUE;
1939 old_env_value = OLD_ENV_VAR;
1940 } else if (strncasecmp((char *)what, "right", len) == 0) {
1942 old_env_var = OLD_ENV_VAR;
1943 old_env_value = OLD_ENV_VALUE;
1944 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1946 old_env_var = OLD_ENV_VALUE;
1947 old_env_value = OLD_ENV_VAR;
1950 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1955 #if defined(AUTHENTICATION)
1957 * The AUTHENTICATE command.
1968 auth_enable P((char *)),
1969 auth_disable P((char *)),
1970 auth_status P((void));
1972 auth_help P((void));
1974 struct authlist AuthList[] = {
1975 { "status", "Display current status of authentication information",
1977 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1979 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1981 { "help", 0, auth_help, 0 },
1982 { "?", "Print help information", auth_help, 0 },
1991 for (c = AuthList; c->name; c++) {
1994 printf("%-15s %s\n", c->name, c->help);
2003 auth_cmd(argc, argv)
2011 "Need an argument to 'auth' command. 'auth ?' for help.\n");
2015 c = (struct authlist *)
2016 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
2018 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
2023 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
2027 if (c->narg + 2 != argc) {
2029 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
2030 c->narg < argc + 2 ? "only " : "",
2031 c->narg, c->narg == 1 ? "" : "s", c->name);
2034 return((*c->handler)(argv[2], argv[3]));
2040 * The ENCRYPT command.
2043 struct encryptlist {
2053 EncryptEnable P((char *, char *)),
2054 EncryptDisable P((char *, char *)),
2055 EncryptType P((char *, char *)),
2056 EncryptStart P((char *)),
2057 EncryptStartInput P((void)),
2058 EncryptStartOutput P((void)),
2059 EncryptStop P((char *)),
2060 EncryptStopInput P((void)),
2061 EncryptStopOutput P((void)),
2062 EncryptStatus P((void));
2064 EncryptHelp P((void));
2066 struct encryptlist EncryptList[] = {
2067 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
2068 EncryptEnable, 1, 1, 2 },
2069 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
2070 EncryptDisable, 0, 1, 2 },
2071 { "type", "Set encryption type. ('encrypt type ?' for more)",
2072 EncryptType, 0, 1, 1 },
2073 { "start", "Start encryption. ('encrypt start ?' for more)",
2074 EncryptStart, 1, 0, 1 },
2075 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
2076 EncryptStop, 1, 0, 1 },
2077 { "input", "Start encrypting the input stream",
2078 EncryptStartInput, 1, 0, 0 },
2079 { "-input", "Stop encrypting the input stream",
2080 EncryptStopInput, 1, 0, 0 },
2081 { "output", "Start encrypting the output stream",
2082 EncryptStartOutput, 1, 0, 0 },
2083 { "-output", "Stop encrypting the output stream",
2084 EncryptStopOutput, 1, 0, 0 },
2086 { "status", "Display current status of authentication information",
2087 EncryptStatus, 0, 0, 0 },
2088 { "help", 0, EncryptHelp, 0, 0, 0 },
2089 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
2096 struct encryptlist *c;
2098 for (c = EncryptList; c->name; c++) {
2101 printf("%-15s %s\n", c->name, c->help);
2110 encrypt_cmd(argc, argv)
2114 struct encryptlist *c;
2118 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2122 c = (struct encryptlist *)
2123 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2125 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2130 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2135 if (argc < c->minarg || argc > c->maxarg) {
2136 if (c->minarg == c->maxarg) {
2137 fprintf(stderr, "Need %s%d argument%s ",
2138 c->minarg < argc ? "only " : "", c->minarg,
2139 c->minarg == 1 ? "" : "s");
2141 fprintf(stderr, "Need %s%d-%d arguments ",
2142 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2144 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2148 if (c->needconnect && !connected) {
2149 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2150 printf("?Need to be connected first.\n");
2154 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2155 argc > 1 ? argv[3] : 0,
2156 argc > 2 ? argv[4] : 0));
2158 #endif /* ENCRYPTION */
2160 #if defined(unix) && defined(TN3270)
2169 res = fcntl(fd, F_GETOWN, 0);
2176 printf("\tOwner is %d.\n", res);
2180 res = fcntl(fd, F_GETFL, 0);
2188 printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
2191 #endif /* defined(unix) && defined(TN3270) */
2194 * Print status about the connection.
2203 printf("Connected to %s.\n", hostname);
2204 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2205 int mode = getconnmode();
2207 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2208 printf("Operating with LINEMODE option\n");
2209 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2210 printf("%s catching of signals\n",
2211 (mode&MODE_TRAPSIG) ? "Local" : "No");
2213 #ifdef KLUDGELINEMODE
2214 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2215 printf("Operating in obsolete linemode\n");
2218 printf("Operating in single character mode\n");
2220 printf("Catching signals locally\n");
2222 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2223 if (my_want_state_is_will(TELOPT_LFLOW))
2224 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2227 #endif /* ENCRYPTION */
2230 printf("No connection.\n");
2232 # if !defined(TN3270)
2233 printf("Escape character is '%s'.\n", control(escape));
2234 (void) fflush(stdout);
2235 # else /* !defined(TN3270) */
2236 if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
2237 printf("Escape character is '%s'.\n", control(escape));
2240 if ((argc >= 2) && !strcmp(argv[1], "everything")) {
2241 printf("SIGIO received %d time%s.\n",
2242 sigiocount, (sigiocount == 1)? "":"s");
2244 printf("Process ID %d, process group %d.\n",
2245 getpid(), getpgrp(getpid()));
2246 printf("Terminal input:\n");
2248 printf("Terminal output:\n");
2250 printf("Network socket:\n");
2254 if (In3270 && transcom) {
2255 printf("Transparent mode command is '%s'.\n", transcom);
2257 # endif /* defined(unix) */
2258 (void) fflush(stdout);
2262 # endif /* defined(TN3270) */
2268 * Function that gets called when SIGINFO is received.
2273 (void) call(status, "status", "notmuch", 0);
2279 struct sockaddr *sa;
2282 static char addrbuf[INET6_ADDRSTRLEN];
2284 switch (sa->sa_family) {
2286 addr = &((struct sockaddr_in *)sa)->sin_addr;
2290 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
2296 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
2300 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2302 setpolicy(net, res, policy)
2304 struct addrinfo *res;
2314 buf = ipsec_set_policy(policy, strlen(policy));
2316 printf("%s\n", ipsec_strerror());
2319 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
2320 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
2321 if (setsockopt(net, level, optname, buf, ipsec_get_policylen(buf)) < 0){
2322 perror("setsockopt");
2332 * When an Address Family related error happend, check if retry with
2333 * another AF is possible or not.
2334 * Return 1, if retry with another af is OK. Else, return 0.
2338 struct addrinfo **aip;
2341 struct addrinfo *ai;
2344 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
2347 while (ai != NULL && ai->ai_family != nextaf);
2361 char *srp = 0, *strrchr();
2363 int sourceroute(), srlen;
2364 int srcroute = 0, result;
2365 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2366 char *src_addr = NULL;
2367 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
2368 int error = 0, af_error = 0;
2371 printf("?Already connected to %s\n", hostname);
2376 (void) strcpy(line, "open ");
2378 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2386 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2388 if (strcmp(*argv, "-l") == 0) {
2396 if (strcmp(*argv, "-a") == 0) {
2401 if (strcmp(*argv, "-s") == 0) {
2420 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
2427 if (src_addr != NULL) {
2428 memset(&hints, 0, sizeof(hints));
2429 hints.ai_flags = AI_NUMERICHOST;
2430 hints.ai_family = family;
2431 hints.ai_socktype = SOCK_STREAM;
2432 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2433 if (error == EAI_NONAME) {
2435 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2438 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
2439 if (error == EAI_SYSTEM)
2440 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2446 if (hostp[0] == '@' || hostp[0] == '!') {
2449 family == AF_INET6 ||
2451 (hostname = strrchr(hostp, ':')) == NULL)
2452 hostname = strrchr(hostp, '@');
2460 } else if (*portp == '-') {
2466 memset(&hints, 0, sizeof(hints));
2467 hints.ai_flags = AI_NUMERICHOST;
2468 hints.ai_family = family;
2469 hints.ai_socktype = SOCK_STREAM;
2470 error = getaddrinfo(hostname, portp, &hints, &res);
2475 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2476 _hostname, sizeof(_hostname) - 1, NULL, 0,
2479 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2480 _hostname[sizeof(_hostname)-1] = '\0';
2481 hostname = _hostname;
2482 } else if (error == EAI_NONAME) {
2483 hints.ai_flags = AI_CANONNAME;
2484 error = getaddrinfo(hostname, portp, &hints, &res);
2486 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2487 if (error == EAI_SYSTEM)
2488 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2493 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2494 else if (res->ai_canonname != NULL)
2495 strcpy(_hostname, res->ai_canonname);
2497 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2498 _hostname[sizeof(_hostname)-1] = '\0';
2499 hostname = _hostname;
2500 } else if (error != 0) {
2501 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2502 if (error == EAI_SYSTEM)
2503 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2509 if (srcroute != 0) {
2510 static char hostbuf[BUFSIZ];
2512 if (af_error == 0) { /* save intermediate hostnames for retry */
2513 strncpy(hostbuf, hostp, BUFSIZ - 1);
2514 hostbuf[BUFSIZ - 1] = '\0';
2518 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2521 if (family == AF_UNSPEC && af_error == 0 &&
2522 switch_af(&res) == 1) {
2529 } else if (result == -1) {
2530 printf("Bad source route option: %s\n", hostp);
2536 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2537 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2541 if (family == AF_UNSPEC && af_error == 0 &&
2542 switch_af(&res) == 1) {
2547 perror("telnet: socket");
2550 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2551 perror("setsockopt (source route)");
2552 #if defined(IPPROTO_IP) && defined(IP_TOS)
2553 if (res->ai_family == PF_INET) {
2554 # if defined(HAS_GETTOS)
2556 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2560 tos = 020; /* Low Delay bit */
2562 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2563 (char *)&tos, sizeof(int)) < 0)
2564 && (errno != ENOPROTOOPT))
2565 perror("telnet: setsockopt (IP_TOS) (ignored)");
2567 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2569 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2570 perror("setsockopt (SO_DEBUG)");
2573 if (src_addr != NULL) {
2574 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2575 if (src_res->ai_family == res->ai_family)
2577 if (src_res == NULL)
2579 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2581 if (family == AF_UNSPEC && af_error == 0 &&
2582 switch_af(&res) == 1) {
2584 (void) NetClose(net);
2589 (void) NetClose(net);
2593 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2594 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2595 (void) NetClose(net);
2598 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2599 (void) NetClose(net);
2604 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2605 struct addrinfo *next;
2607 next = res->ai_next;
2608 /* If already an af failed, only try same af. */
2610 while (next != NULL && next->ai_family != res->ai_family)
2611 next = next->ai_next;
2612 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2615 (void) NetClose(net);
2618 warnx("Unable to connect to remote host");
2619 (void) NetClose(net);
2623 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2624 auth_encrypt_connect(connected);
2625 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2626 } while (connected == 0);
2628 if (src_res0 != NULL)
2629 freeaddrinfo(src_res0);
2630 cmdrc(hostp, hostname);
2631 if (autologin && user == NULL) {
2634 user = getenv("USER");
2636 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2637 if ((pw = getpwuid(getuid())))
2644 env_define((unsigned char *)"USER", (unsigned char *)user);
2645 env_export((unsigned char *)"USER");
2647 (void) call(status, "status", "notmuch", 0);
2648 if (setjmp(peerdied) == 0)
2650 (void) NetClose(net);
2651 ExitString("Connection closed by foreign host.\n",1);
2656 if (src_res0 != NULL)
2657 freeaddrinfo(src_res0);
2661 #define HELPINDENT (sizeof ("connect"))
2664 openhelp[] = "connect to a site",
2665 closehelp[] = "close current connection",
2666 logouthelp[] = "forcibly logout remote user and close the connection",
2667 quithelp[] = "exit telnet",
2668 statushelp[] = "print status information",
2669 helphelp[] = "print help information",
2670 sendhelp[] = "transmit special characters ('send ?' for more)",
2671 sethelp[] = "set operating parameters ('set ?' for more)",
2672 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2673 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2674 slchelp[] = "change state of special charaters ('slc ?' for more)",
2675 displayhelp[] = "display operating parameters",
2676 #if defined(TN3270) && defined(unix)
2677 transcomhelp[] = "specify Unix command for transparent mode pipe",
2678 #endif /* defined(TN3270) && defined(unix) */
2679 #if defined(AUTHENTICATION)
2680 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2683 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2684 #endif /* ENCRYPTION */
2686 zhelp[] = "suspend telnet",
2687 #endif /* defined(unix) */
2689 skeyhelp[] = "compute response to s/key challenge",
2691 shellhelp[] = "invoke a subshell",
2692 envhelp[] = "change environment variables ('environ ?' for more)",
2693 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2695 static Command cmdtab[] = {
2696 { "close", closehelp, bye, 1 },
2697 { "logout", logouthelp, logout, 1 },
2698 { "display", displayhelp, display, 0 },
2699 { "mode", modestring, modecmd, 0 },
2700 { "open", openhelp, tn, 0 },
2701 { "quit", quithelp, quit, 0 },
2702 { "send", sendhelp, sendcmd, 0 },
2703 { "set", sethelp, setcmd, 0 },
2704 { "unset", unsethelp, unsetcmd, 0 },
2705 { "status", statushelp, status, 0 },
2706 { "toggle", togglestring, toggle, 0 },
2707 { "slc", slchelp, slccmd, 0 },
2708 #if defined(TN3270) && defined(unix)
2709 { "transcom", transcomhelp, settranscom, 0 },
2710 #endif /* defined(TN3270) && defined(unix) */
2711 #if defined(AUTHENTICATION)
2712 { "auth", authhelp, auth_cmd, 0 },
2715 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2716 #endif /* ENCRYPTION */
2718 { "z", zhelp, suspend, 0 },
2719 #endif /* defined(unix) */
2721 { "!", shellhelp, shell, 1 },
2723 { "!", shellhelp, shell, 0 },
2725 { "environ", envhelp, env_cmd, 0 },
2726 { "?", helphelp, help, 0 },
2728 { "skey", skeyhelp, skey_calc, 0 },
2733 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2734 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2736 static Command cmdtab2[] = {
2737 { "help", 0, help, 0 },
2738 { "escape", escapehelp, setescape, 0 },
2739 { "crmod", crmodhelp, togcrmod, 0 },
2745 * Call routine with argc, argv set from args (terminated by 0).
2754 typedef int (*intrtn_t)();
2760 routine = (va_arg(ap, intrtn_t));
2761 while ((args[argno++] = va_arg(ap, char *)) != 0) {
2765 return (*routine)(argno-1, args);
2775 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2777 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2781 command(top, tbuf, cnt)
2786 register Command *c;
2793 (void) signal(SIGINT, SIG_DFL);
2794 (void) signal(SIGQUIT, SIG_DFL);
2795 #endif /* defined(unix) */
2798 if (rlogin == _POSIX_VDISABLE)
2799 printf("%s> ", prompt);
2803 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2806 if (cp == line || *--cp != '\n' || cp == line)
2809 if (rlogin == _POSIX_VDISABLE)
2810 printf("%s\n", line);
2813 if (rlogin != _POSIX_VDISABLE)
2814 printf("%s> ", prompt);
2815 if (fgets(line, sizeof(line), stdin) == NULL) {
2816 if (feof(stdin) || ferror(stdin)) {
2826 if (margv[0] == 0) {
2829 c = getcmd(margv[0]);
2831 printf("?Ambiguous command\n");
2835 printf("?Invalid command\n");
2838 if (c->needconnect && !connected) {
2839 printf("?Need to be connected first.\n");
2842 if ((*c->handler)(margc, margv)) {
2848 longjmp(toplevel, 1);
2852 if (shell_active == 0) {
2855 #else /* defined(TN3270) */
2857 #endif /* defined(TN3270) */
2869 register Command *c;
2872 printf("Commands may be abbreviated. Commands are:\n\n");
2873 for (c = cmdtab; c->name; c++)
2875 printf("%-*s\t%s\n", HELPINDENT, c->name,
2879 else while (--argc > 0) {
2884 printf("?Ambiguous help command %s\n", arg);
2885 else if (c == (Command *)0)
2886 printf("?Invalid help command %s\n", arg);
2888 printf("%s\n", c->help);
2893 static char *rcname = 0;
2894 static char rcbuf[128];
2900 register Command *c;
2903 int l1 = strlen(m1);
2904 int l2 = strlen(m2);
2914 rcname = getenv("HOME");
2915 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2916 strcpy(rcbuf, rcname);
2919 strcat(rcbuf, "/.telnetrc");
2923 if ((rcfile = fopen(rcname, "r")) == 0) {
2928 if (fgets(line, sizeof(line), rcfile) == NULL)
2935 if (!isspace(line[0]))
2938 if (gotmachine == 0) {
2939 if (isspace(line[0]))
2941 if (strncasecmp(line, m1, l1) == 0)
2942 strncpy(line, &line[l1], sizeof(line) - l1);
2943 else if (strncasecmp(line, m2, l2) == 0)
2944 strncpy(line, &line[l2], sizeof(line) - l2);
2945 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2946 strncpy(line, &line[7], sizeof(line) - 7);
2949 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2956 c = getcmd(margv[0]);
2958 printf("?Ambiguous command: %s\n", margv[0]);
2962 printf("?Invalid command: %s\n", margv[0]);
2966 * This should never happen...
2968 if (c->needconnect && !connected) {
2969 printf("?Need to be connected first for %s.\n", margv[0]);
2972 (*c->handler)(margc, margv);
2978 * Source route is handed in as
2979 * [!]@hop1@hop2...[@|:]dst
2980 * If the leading ! is present, it is a
2981 * strict source route, otherwise it is
2982 * assmed to be a loose source route.
2984 * We fill in the source route option as
2985 * hop1,hop2,hop3...dest
2986 * and return a pointer to hop1, which will
2987 * be the address to connect() to.
2991 * res: ponter to addrinfo structure which contains sockaddr to
2992 * the host to connect to.
2994 * arg: pointer to route list to decipher
2996 * cpp: If *cpp is not equal to NULL, this is a
2997 * pointer to a pointer to a character array
2998 * that should be filled in with the option.
3000 * lenp: pointer to an integer that contains the
3001 * length of *cpp if *cpp != NULL.
3003 * protop: pointer to an integer that should be filled in with
3004 * appropriate protocol for setsockopt, as socket
3007 * optp: pointer to an integer that should be filled in with
3008 * appropriate option for setsockopt, as socket protocol
3013 * If the return value is 1, then all operations are
3014 * successful. If the
3015 * return value is -1, there was a syntax error in the
3016 * option, either unknown characters, or too many hosts.
3017 * If the return value is 0, one of the hostnames in the
3018 * path is unknown, and *cpp is set to point to the bad
3021 * *cpp: If *cpp was equal to NULL, it will be filled
3022 * in with a pointer to our static area that has
3023 * the option filled in. This will be 32bit aligned.
3025 * *lenp: This will be filled in with how long the option
3026 * pointed to by *cpp is.
3028 * *protop: This will be filled in with appropriate protocol for
3029 * setsockopt, as socket protocol family.
3031 * *optp: This will be filled in with appropriate option for
3032 * setsockopt, as socket protocol family.
3035 sourceroute(ai, arg, cpp, lenp, protop, optp)
3036 struct addrinfo *ai;
3043 static char buf[1024]; /*XXX*/
3044 struct cmsghdr *cmsg;
3048 char *cp, *cp2, *lsrp, *ep;
3050 struct sockaddr_in *sin;
3051 struct sockaddr_in6 *sin6;
3052 struct addrinfo hints, *res;
3057 * Verify the arguments, and make sure we have
3058 * at least 7 bytes for the option.
3060 if (cpp == NULL || lenp == NULL)
3063 switch (res->ai_family) {
3070 if (*lenp < CMSG_SPACE(sizeof(struct ip6_rthdr) +
3071 sizeof(struct in6_addr)))
3078 * Decide whether we have a buffer passed to us,
3079 * or if we need to use our own static buffer.
3085 *cpp = lsrp = ALIGN(buf);
3092 if (ai->ai_family == AF_INET6) {
3093 cmsg = inet6_rthdr_init(*cpp, IPV6_RTHDR_TYPE_0);
3096 *protop = IPPROTO_IPV6;
3097 *optp = IPV6_PKTOPTIONS;
3102 * Next, decide whether we have a loose source
3103 * route or a strict source route, and fill in
3104 * the begining of the option.
3109 *lsrp++ = IPOPT_SSRR;
3111 *lsrp++ = IPOPT_LSRR;
3115 ipopt.io_type = IPOPT_SSRR;
3117 ipopt.io_type = IPOPT_LSRR;
3124 lsrp++; /* skip over length, we'll fill it in later */
3127 *protop = IPPROTO_IP;
3132 memset(&hints, 0, sizeof(hints));
3133 hints.ai_family = ai->ai_family;
3134 hints.ai_socktype = SOCK_STREAM;
3138 ai->ai_family != AF_INET6 &&
3142 else for (cp2 = cp; (c = *cp2); cp2++) {
3147 } else if (c == '@') {
3151 ai->ai_family != AF_INET6 &&
3162 hints.ai_flags = AI_NUMERICHOST;
3163 error = getaddrinfo(cp, NULL, &hints, &res);
3164 if (error == EAI_NONAME) {
3166 error = getaddrinfo(cp, NULL, &hints, &res);
3169 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
3170 if (error == EAI_SYSTEM)
3171 fprintf(stderr, "%s: %s\n", cp,
3177 if (res->ai_family == AF_INET6) {
3178 sin6 = (struct sockaddr_in6 *)res->ai_addr;
3179 inet6_rthdr_add(cmsg, &sin6->sin6_addr,
3184 sin = (struct sockaddr_in *)res->ai_addr;
3185 memcpy(lsrp, (char *)&sin->sin_addr, 4);
3193 * Check to make sure there is space for next address
3196 if (res->ai_family == AF_INET6) {
3197 if (((char *)CMSG_DATA(cmsg) +
3198 sizeof(struct ip6_rthdr) +
3199 ((inet6_rthdr_segments(cmsg) + 1) *
3200 sizeof(struct in6_addr))) > ep)
3209 if (res->ai_family == AF_INET6) {
3210 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
3211 *lenp = cmsg->cmsg_len;
3216 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
3221 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
3222 *lenp = lsrp - *cpp;
3224 ipopt.io_len = lsrp - *cpp;
3225 if (ipopt.io_len <= 5) { /* Is 3 better ? */
3230 *lenp = sizeof(ipopt);
3231 *cpp = (char *) &ipopt;