1 /* $NetBSD: main.c,v 1.17 2002/08/23 08:14:20 kanaoka Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 5/30/95"; 45 #else 46 __RCSID("$NetBSD: main.c,v 1.17 2002/08/23 08:14:20 kanaoka Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/types.h> 51 #include <sys/socket.h> 52 53 #include <unistd.h> 54 55 #include "ring.h" 56 #include "externs.h" 57 #include "defines.h" 58 #ifdef AUTHENTICATION 59 #include <libtelnet/auth.h> 60 #endif 61 #ifdef ENCRYPTION 62 #include <libtelnet/encrypt.h> 63 #endif 64 65 /* These values need to be the same as defined in libtelnet/kerberos5.c */ 66 /* Either define them in both places, or put in some common header file. */ 67 #define OPTS_FORWARD_CREDS 0x00000002 68 #define OPTS_FORWARDABLE_CREDS 0x00000001 69 70 #if 0 71 #define FORWARD 72 #endif 73 74 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 75 char *ipsec_policy_in = NULL; 76 char *ipsec_policy_out = NULL; 77 #endif 78 79 int family = AF_UNSPEC; 80 81 int main(int, char *[]); 82 83 /* 84 * Initialize variables. 85 */ 86 void 87 tninit() 88 { 89 init_terminal(); 90 91 init_network(); 92 93 init_telnet(); 94 95 init_sys(); 96 97 #if defined(TN3270) 98 init_3270(); 99 #endif 100 } 101 102 void 103 usage() 104 { 105 fprintf(stderr, "Usage: %s %s%s%s%s\n", 106 prompt, 107 #ifdef AUTHENTICATION 108 "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c] [-d]", 109 "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ", 110 #else 111 "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char] [-l user]", 112 "\n\t[-n tracefile]", 113 #endif 114 #if defined(TN3270) && defined(unix) 115 # ifdef AUTHENTICATION 116 "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ", 117 # else 118 "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]", 119 # endif 120 #else 121 "[-r] ", 122 #endif 123 #ifdef ENCRYPTION 124 "[-x] " 125 #endif 126 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 127 "[-P policy] [host-name [port]]" 128 #else 129 "[host-name [port]]" 130 #endif 131 ); 132 exit(1); 133 } 134 135 /* 136 * main. Parse arguments, invoke the protocol or command parser. 137 */ 138 139 140 int 141 main(argc, argv) 142 int argc; 143 char *argv[]; 144 { 145 extern char *optarg; 146 extern int optind; 147 int ch; 148 char *user; 149 #ifdef FORWARD 150 extern int forward_flags; 151 #endif /* FORWARD */ 152 153 tninit(); /* Clear out things */ 154 155 TerminalSaveState(); 156 157 if ((prompt = strrchr(argv[0], '/')) != NULL) 158 ++prompt; 159 else 160 prompt = argv[0]; 161 162 user = NULL; 163 164 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE; 165 autologin = -1; 166 167 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 168 #define IPSECOPT "P:" 169 #else 170 #define IPSECOPT 171 #endif 172 while ((ch = getopt(argc, argv, "468EKLNS:X:acde:fFk:l:n:rt:x" 173 IPSECOPT)) != -1) { 174 #undef IPSECOPT 175 switch(ch) { 176 case '4': 177 family = AF_INET; 178 break; 179 case '6': 180 family = AF_INET6; 181 break; 182 case '8': 183 eight = 3; /* binary output and input */ 184 break; 185 case 'E': 186 rlogin = escape = _POSIX_VDISABLE; 187 break; 188 case 'K': 189 #ifdef AUTHENTICATION 190 autologin = 0; 191 #endif 192 break; 193 case 'L': 194 eight |= 2; /* binary output only */ 195 break; 196 case 'N': 197 doaddrlookup = 0; 198 break; 199 case 'S': 200 { 201 #ifdef HAS_GETTOS 202 extern int tos; 203 204 if ((tos = parsetos(optarg, "tcp")) < 0) 205 fprintf(stderr, "%s%s%s%s\n", 206 prompt, ": Bad TOS argument '", 207 optarg, 208 "; will try to use default TOS"); 209 #else 210 fprintf(stderr, 211 "%s: Warning: -S ignored, no parsetos() support.\n", 212 prompt); 213 #endif 214 } 215 break; 216 case 'X': 217 #ifdef AUTHENTICATION 218 auth_disable_name(optarg); 219 #endif 220 break; 221 case 'a': 222 autologin = 1; 223 break; 224 case 'c': 225 skiprc = 1; 226 break; 227 case 'd': 228 debug = 1; 229 break; 230 case 'e': 231 set_escape_char(optarg); 232 break; 233 case 'f': 234 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD) 235 if (forward_flags & OPTS_FORWARD_CREDS) { 236 fprintf(stderr, 237 "%s: Only one of -f and -F allowed.\n", 238 prompt); 239 usage(); 240 } 241 forward_flags |= OPTS_FORWARD_CREDS; 242 #else 243 fprintf(stderr, 244 "%s: Warning: -f ignored, no Kerberos V5 support.\n", 245 prompt); 246 #endif 247 break; 248 case 'F': 249 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD) 250 if (forward_flags & OPTS_FORWARD_CREDS) { 251 fprintf(stderr, 252 "%s: Only one of -f and -F allowed.\n", 253 prompt); 254 usage(); 255 } 256 forward_flags |= OPTS_FORWARD_CREDS; 257 forward_flags |= OPTS_FORWARDABLE_CREDS; 258 #else 259 fprintf(stderr, 260 "%s: Warning: -F ignored, no Kerberos V5 support.\n", 261 prompt); 262 #endif 263 break; 264 case 'k': 265 #if defined(AUTHENTICATION) && defined(KRB4) 266 { 267 extern char *dest_realm, dst_realm_buf[], dst_realm_sz; 268 dest_realm = dst_realm_buf; 269 (void)strncpy(dest_realm, optarg, dst_realm_sz); 270 } 271 #else 272 fprintf(stderr, 273 "%s: Warning: -k ignored, no Kerberos V4 support.\n", 274 prompt); 275 #endif 276 break; 277 case 'l': 278 if(autologin == 0) { 279 autologin = -1; 280 } 281 user = optarg; 282 break; 283 case 'n': 284 #if defined(TN3270) && defined(unix) 285 /* distinguish between "-n oasynch" and "-noasynch" */ 286 if (argv[optind - 1][0] == '-' && argv[optind - 1][1] 287 == 'n' && argv[optind - 1][2] == 'o') { 288 if (!strcmp(optarg, "oasynch")) { 289 noasynchtty = 1; 290 noasynchnet = 1; 291 } else if (!strcmp(optarg, "oasynchtty")) 292 noasynchtty = 1; 293 else if (!strcmp(optarg, "oasynchnet")) 294 noasynchnet = 1; 295 } else 296 #endif /* defined(TN3270) && defined(unix) */ 297 SetNetTrace(optarg); 298 break; 299 case 'r': 300 rlogin = '~'; 301 break; 302 case 't': 303 #if defined(TN3270) && defined(unix) 304 (void)strlcpy(tline, optarg, sizeof(tline)); 305 transcom = tline; 306 #else 307 fprintf(stderr, 308 "%s: Warning: -t ignored, no TN3270 support.\n", 309 prompt); 310 #endif 311 break; 312 case 'x': 313 #ifdef ENCRYPTION 314 encrypt_auto(1); 315 decrypt_auto(1); 316 #else /* ENCRYPTION */ 317 fprintf(stderr, 318 "%s: Warning: -x ignored, no ENCRYPT support.\n", 319 prompt); 320 #endif /* ENCRYPTION */ 321 break; 322 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 323 case 'P': 324 if (!strncmp("in", optarg, 2)) 325 ipsec_policy_in = strdup(optarg); 326 else if (!strncmp("out", optarg, 3)) 327 ipsec_policy_out = strdup(optarg); 328 else 329 usage(); 330 break; 331 #endif 332 case '?': 333 default: 334 usage(); 335 /* NOTREACHED */ 336 } 337 } 338 339 if (autologin == -1) { /* esc@magic.fi; force */ 340 #if defined(AUTHENTICATION) 341 autologin = 1; 342 #endif 343 #if defined(ENCRYPTION) 344 encrypt_auto(1); 345 decrypt_auto(1); 346 #endif 347 } 348 349 if (autologin == -1) 350 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1; 351 352 argc -= optind; 353 argv += optind; 354 355 if (argc) { 356 char *args[7], **argp = args; 357 #ifdef __GNUC__ 358 (void) &argp; /* avoid longjmp clobbering */ 359 #endif 360 361 if (argc > 2) 362 usage(); 363 *argp++ = prompt; 364 if (user) { 365 *argp++ = "-l"; 366 *argp++ = user; 367 } 368 *argp++ = argv[0]; /* host */ 369 if (argc > 1) 370 *argp++ = argv[1]; /* port */ 371 *argp = 0; 372 373 if (setjmp(toplevel) != 0) 374 Exit(0); 375 if (tn(argp - args, args) == 1) 376 return (0); 377 else 378 return (1); 379 } 380 (void)setjmp(toplevel); 381 for (;;) { 382 #ifdef TN3270 383 if (shell_active) 384 shell_continue(); 385 else 386 #endif 387 command(1, 0, 0); 388 } 389 } 390