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