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