dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / telnet / main.c
blob66dd54e5a8e51c5c736d218d9beef51a7e3c2af7
1 /*
2 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 #pragma ident "%Z%%M% %I% %E% SMI"
8 /*
9 * Copyright (c) 1988, 1990 Regents of the University of California.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
41 char copyright[] =
42 "@(#) Copyright (c) 1988, 1990 Regents of the University of California.\n"
43 " All rights reserved.\n";
45 static char sccsid[] = "@(#)main.c 5.5 (Berkeley) 12/18/92";
47 #include <string.h>
48 #include <sys/types.h>
50 #include "ring.h"
51 #include "externs.h"
52 #include "defines.h"
54 /* These values need to be the same as defined in libtelnet/kerberos5.c */
55 /* Either define them in both places, or put in some common header file. */
56 #define OPTS_FORWARD_CREDS 0x00000002
57 #define OPTS_FORWARDABLE_CREDS 0x00000001
60 * This flag is incremented, if any of the
61 * Kerberos command line options are used.
63 int krb5auth_flag = 0;
66 * Initialize variables.
68 int
69 tninit()
71 init_terminal();
73 init_network();
75 if (init_telnet() == 0)
76 return (0);
78 init_sys();
80 return (1);
83 #if defined(USE_TOS)
84 #define TELNET_OPTIONS "8EKLS:X:acde:fFk:l:n:rt:x"
85 #else
86 #define TELNET_OPTIONS "8EKLX:acde:fFk:l:n:rt:x"
87 #endif /* USE_TOS */
89 static void
90 usage()
92 (void) fprintf(stderr, "Usage: %s %s\n",
93 prompt,
94 " [-8] [-E] [-K] [-L] [-a] [-c] [-d] [-f/-F] [-r] [-x]"
95 "\n\t[-e char] [-k realm] [-l user] [-n tracefile] [-X atype]"
96 "\n\t[host-name [port]]");
97 exit(1);
101 * main. Parse arguments, invoke the protocol or command parser.
106 main(int argc, char *argv[])
108 int ch;
109 char *user;
110 extern boolean_t auth_enable_encrypt;
111 extern int forward_flags;
113 /* Clear out things */
114 if (tninit() == 0)
115 return (EXIT_FAILURE);
117 if (!isatty(fileno(stdin))) {
118 setbuf(stdin, NULL);
120 if (!isatty(fileno(stdout))) {
121 setbuf(stdout, NULL);
124 TerminalSaveState();
126 if (prompt = strrchr(argv[0], '/'))
127 ++prompt;
128 else
129 prompt = argv[0];
131 user = NULL;
133 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
134 autologin = -1;
136 while ((ch = getopt(argc, argv, TELNET_OPTIONS)) != EOF) {
137 switch (ch) {
138 case 'K':
139 autologin_set = 1;
140 autologin = 0;
141 krb5auth_flag++;
142 break;
143 case 'X':
144 auth_disable_name(optarg);
145 krb5auth_flag++;
146 break;
147 case 'a':
148 autologin_set = 1;
149 autologin = 1;
150 krb5auth_flag++;
151 break;
152 case 'f':
153 if (forward_flags & OPTS_FORWARD_CREDS) {
154 (void) fprintf(stderr, gettext(
155 "%s: Only one of -f "
156 "and -F allowed.\n"), prompt);
157 usage();
159 forward_flags |= OPTS_FORWARD_CREDS;
160 forward_flag_set = 1;
161 krb5auth_flag++;
162 break;
163 case 'F':
164 if (forward_flags & OPTS_FORWARD_CREDS) {
165 (void) fprintf(stderr, gettext(
166 "%s: Only one of -f "
167 "and -F allowed.\n"), prompt);
168 usage();
170 forward_flags |= OPTS_FORWARD_CREDS;
171 forward_flags |= OPTS_FORWARDABLE_CREDS;
172 forwardable_flag_set = 1;
173 forward_flag = 1;
174 krb5auth_flag++;
175 break;
176 case 'k':
177 set_krb5_realm(optarg);
178 krb5auth_flag++;
179 break;
180 case 'x':
181 if (krb5_privacy_allowed()) {
182 encrypt_auto(1);
183 decrypt_auto(1);
184 wantencryption = B_TRUE;
185 autologin = 1;
186 autologin_set = 1;
187 auth_enable_encrypt = B_TRUE;
188 encrypt_flag_set = 1;
189 krb5auth_flag++;
190 } else {
191 (void) fprintf(stderr, gettext(
192 "%s: Encryption not supported.\n"),
193 prompt);
194 exit(1);
196 break;
198 /* begin common options */
199 case '8':
200 eight = 3; /* binary output and input */
201 break;
202 case 'E':
203 escape_valid = B_FALSE;
204 rlogin = escape = _POSIX_VDISABLE;
205 break;
206 case 'L':
207 eight |= 2; /* binary output only */
208 break;
209 #if USE_TOS
210 case 'S':
211 (void) fprintf(stderr,
212 "%s: Warning: -S ignored, no parsetos() support.\n",
213 prompt);
214 break;
215 #endif /* USE_TOS */
216 case 'c':
217 skiprc = 1;
218 break;
219 case 'd':
220 debug = 1;
221 break;
222 case 'e':
223 escape_valid = B_TRUE;
224 set_escape_char(optarg);
225 break;
226 case 'l':
227 autologin_set = 1;
228 autologin = 1;
229 user = optarg;
230 break;
231 case 'n':
232 SetNetTrace(optarg);
233 break;
234 case 'r':
235 rlogin = '~';
236 break;
237 case 't':
238 (void) fprintf(stderr,
239 "%s: Warning: -t ignored, no TN3270 support.\n",
240 prompt);
241 break;
242 default:
243 usage();
244 /* NOTREACHED */
247 if (autologin == -1)
248 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
250 argc -= optind;
251 argv += optind;
253 if (argc) {
254 char *args[7], **argp = args;
256 if (argc > 2)
257 usage();
258 *argp++ = prompt;
259 if (user) {
260 *argp++ = "-l";
261 *argp++ = user;
263 *argp++ = argv[0]; /* host */
264 if (argc > 1)
265 *argp++ = argv[1]; /* port */
266 *argp = 0;
268 if (setjmp(toplevel) != 0)
269 Exit(EXIT_SUCCESS);
270 if (tn(argp - args, args) == 1)
271 return (EXIT_SUCCESS);
272 else
273 return (EXIT_FAILURE);
275 (void) setjmp(toplevel);
276 for (;;) {
277 command(1, 0, 0);