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