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