xref: /dflybsd-src/usr.bin/telnet/main.c (revision 0402ebbc7d4b6f34d02791995169d25c4aec3b15)
1 /*
2  * Copyright (c) 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)main.c	8.3 (Berkeley) 5/30/95
34  * $FreeBSD: src/usr.bin/telnet/main.c,v 1.10.2.5 2002/04/13 11:07:13 markm Exp $
35  * $DragonFly: src/usr.bin/telnet/main.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
36  */
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "ring.h"
45 #include "externs.h"
46 #include "defines.h"
47 
48 
49 /* These values need to be the same as defined in libtelnet/kerberos5.c */
50 /* Either define them in both places, or put in some common header file. */
51 #define OPTS_FORWARD_CREDS	0x00000002
52 #define OPTS_FORWARDABLE_CREDS	0x00000001
53 
54 #if 0
55 #define FORWARD
56 #endif
57 
58 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
59 char *ipsec_policy_in = NULL;
60 char *ipsec_policy_out = NULL;
61 #endif
62 
63 int family = AF_UNSPEC;
64 
65 /*
66  * Initialize variables.
67  */
68 void
69 tninit(void)
70 {
71     init_terminal();
72 
73     init_network();
74 
75     init_telnet();
76 
77     init_sys();
78 }
79 
80 static void
81 usage(void)
82 {
83 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
84 	    prompt,
85 	    "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-c] [-d]",
86 	    "\n\t[-e char] [-l user] [-n tracefile] ",
87 	    "[-r] [-s src_addr] [-u] ",
88 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
89 	    "[-P policy] "
90 #endif
91 	    "[host-name [port]]"
92 	);
93 	exit(1);
94 }
95 
96 /*
97  * main.  Parse arguments, invoke the protocol or command parser.
98  */
99 
100 int
101 main(int argc, char *argv[])
102 {
103 	int ch;
104 	char *user;
105 	char *src_addr = NULL;
106 #ifdef	FORWARD
107 	extern int forward_flags;
108 #endif	/* FORWARD */
109 
110 	tninit();		/* Clear out things */
111 
112 	TerminalSaveState();
113 
114 	if ((prompt = strrchr(argv[0], '/')))
115 		++prompt;
116 	else
117 		prompt = argv[0];
118 
119 	user = NULL;
120 
121 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
122 	autologin = -1;
123 
124 
125 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
126 #define IPSECOPT	"P:"
127 #else
128 #define IPSECOPT
129 #endif
130 	while ((ch = getopt(argc, argv,
131 			    "468EKLNS:X:acde:fFk:l:n:rs:t:uxy" IPSECOPT)) != -1)
132 #undef IPSECOPT
133 	{
134 		switch(ch) {
135 		case '4':
136 			family = AF_INET;
137 			break;
138 #ifdef INET6
139 		case '6':
140 			family = AF_INET6;
141 			break;
142 #endif
143 		case '8':
144 			eight = 3;	/* binary output and input */
145 			break;
146 		case 'E':
147 			rlogin = escape = _POSIX_VDISABLE;
148 			break;
149 		case 'K':
150 			break;
151 		case 'L':
152 			eight |= 2;	/* binary output only */
153 			break;
154 		case 'N':
155 			doaddrlookup = 0;
156 			break;
157 		case 'S':
158 		    {
159 #ifdef	HAS_GETTOS
160 			extern int tos;
161 
162 			if ((tos = parsetos(optarg, "tcp")) < 0)
163 				fprintf(stderr, "%s%s%s%s\n",
164 					prompt, ": Bad TOS argument '",
165 					optarg,
166 					"; will try to use default TOS");
167 #else
168 			fprintf(stderr,
169 			   "%s: Warning: -S ignored, no parsetos() support.\n",
170 								prompt);
171 #endif
172 		    }
173 			break;
174 		case 'X':
175 			break;
176 		case 'a':
177 			autologin = 1;
178 			break;
179 		case 'c':
180 			skiprc = 1;
181 			break;
182 		case 'd':
183 			debug = 1;
184 			break;
185 		case 'e':
186 			set_escape_char(optarg);
187 			break;
188 		case 'f':
189 			fprintf(stderr,
190 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
191 				prompt);
192 			break;
193 		case 'F':
194 			fprintf(stderr,
195 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
196 				prompt);
197 			break;
198 		case 'k':
199 			fprintf(stderr,
200 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
201 								prompt);
202 			break;
203 		case 'l':
204 			autologin = 1;
205 			user = optarg;
206 			break;
207 		case 'n':
208 				SetNetTrace(optarg);
209 			break;
210 		case 'r':
211 			rlogin = '~';
212 			break;
213 		case 's':
214 			src_addr = optarg;
215 			break;
216 		case 'u':
217 			family = AF_UNIX;
218 			break;
219 		case 'x':
220 			fprintf(stderr,
221 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
222 								prompt);
223 			break;
224 		case 'y':
225 			fprintf(stderr,
226 			    "%s: Warning: -y ignored, no ENCRYPT support.\n",
227 								prompt);
228 			break;
229 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
230 		case 'P':
231 			if (!strncmp("in", optarg, 2))
232 				ipsec_policy_in = strdup(optarg);
233 			else if (!strncmp("out", optarg, 3))
234 				ipsec_policy_out = strdup(optarg);
235 			else
236 				usage();
237 			break;
238 #endif
239 		case '?':
240 		default:
241 			usage();
242 			/* NOTREACHED */
243 		}
244 	}
245 	if (autologin == -1)
246 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
247 
248 	argc -= optind;
249 	argv += optind;
250 
251 	if (argc) {
252 		char *args[9], **argp = args;
253 
254 		if (argc > 2)
255 			usage();
256 		*argp++ = prompt;
257 		if (user) {
258 			*argp++ = strdup("-l");
259 			*argp++ = user;
260 		}
261 		if (src_addr) {
262 			*argp++ = strdup("-s");
263 			*argp++ = src_addr;
264 		}
265 		*argp++ = argv[0];		/* host */
266 		if (argc > 1)
267 			*argp++ = argv[1];	/* port */
268 		*argp = 0;
269 
270 		if (setjmp(toplevel) != 0)
271 			Exit(0);
272 		if (tn(argp - args, args) == 1)
273 			return (0);
274 		else
275 			return (1);
276 	}
277 	(void)setjmp(toplevel);
278 	for (;;) {
279 			command(1, 0, 0);
280 	}
281 	return 0;
282 }
283