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