1 /* $NetBSD: main.c,v 1.18 2003/06/18 20:51:00 christos 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.18 2003/06/18 20:51:00 christos 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(void) 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(int argc, char *argv[]) 142 { 143 extern char *optarg; 144 extern int optind; 145 int ch; 146 char *user; 147 #ifdef FORWARD 148 extern int forward_flags; 149 #endif /* FORWARD */ 150 151 tninit(); /* Clear out things */ 152 153 TerminalSaveState(); 154 155 if ((prompt = strrchr(argv[0], '/')) != NULL) 156 ++prompt; 157 else 158 prompt = argv[0]; 159 160 user = NULL; 161 162 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE; 163 autologin = -1; 164 165 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 166 #define IPSECOPT "P:" 167 #else 168 #define IPSECOPT 169 #endif 170 while ((ch = getopt(argc, argv, "468EKLNS:X:acde:fFk:l:n:rt:x" 171 IPSECOPT)) != -1) { 172 #undef IPSECOPT 173 switch(ch) { 174 case '4': 175 family = AF_INET; 176 break; 177 case '6': 178 family = AF_INET6; 179 break; 180 case '8': 181 eight = 3; /* binary output and input */ 182 break; 183 case 'E': 184 rlogin = escape = _POSIX_VDISABLE; 185 break; 186 case 'K': 187 #ifdef AUTHENTICATION 188 autologin = 0; 189 #endif 190 break; 191 case 'L': 192 eight |= 2; /* binary output only */ 193 break; 194 case 'N': 195 doaddrlookup = 0; 196 break; 197 case 'S': 198 { 199 #ifdef HAS_GETTOS 200 extern int tos; 201 202 if ((tos = parsetos(optarg, "tcp")) < 0) 203 fprintf(stderr, "%s%s%s%s\n", 204 prompt, ": Bad TOS argument '", 205 optarg, 206 "; will try to use default TOS"); 207 #else 208 fprintf(stderr, 209 "%s: Warning: -S ignored, no parsetos() support.\n", 210 prompt); 211 #endif 212 } 213 break; 214 case 'X': 215 #ifdef AUTHENTICATION 216 auth_disable_name(optarg); 217 #endif 218 break; 219 case 'a': 220 autologin = 1; 221 break; 222 case 'c': 223 skiprc = 1; 224 break; 225 case 'd': 226 debug = 1; 227 break; 228 case 'e': 229 set_escape_char(optarg); 230 break; 231 case 'f': 232 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD) 233 if (forward_flags & OPTS_FORWARD_CREDS) { 234 fprintf(stderr, 235 "%s: Only one of -f and -F allowed.\n", 236 prompt); 237 usage(); 238 } 239 forward_flags |= OPTS_FORWARD_CREDS; 240 #else 241 fprintf(stderr, 242 "%s: Warning: -f ignored, no Kerberos V5 support.\n", 243 prompt); 244 #endif 245 break; 246 case 'F': 247 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD) 248 if (forward_flags & OPTS_FORWARD_CREDS) { 249 fprintf(stderr, 250 "%s: Only one of -f and -F allowed.\n", 251 prompt); 252 usage(); 253 } 254 forward_flags |= OPTS_FORWARD_CREDS; 255 forward_flags |= OPTS_FORWARDABLE_CREDS; 256 #else 257 fprintf(stderr, 258 "%s: Warning: -F ignored, no Kerberos V5 support.\n", 259 prompt); 260 #endif 261 break; 262 case 'k': 263 #if defined(AUTHENTICATION) && defined(KRB4) 264 { 265 extern char *dest_realm, dst_realm_buf[], dst_realm_sz; 266 dest_realm = dst_realm_buf; 267 (void)strncpy(dest_realm, optarg, dst_realm_sz); 268 } 269 #else 270 fprintf(stderr, 271 "%s: Warning: -k ignored, no Kerberos V4 support.\n", 272 prompt); 273 #endif 274 break; 275 case 'l': 276 if(autologin == 0) { 277 autologin = -1; 278 } 279 user = optarg; 280 break; 281 case 'n': 282 #if defined(TN3270) && defined(unix) 283 /* distinguish between "-n oasynch" and "-noasynch" */ 284 if (argv[optind - 1][0] == '-' && argv[optind - 1][1] 285 == 'n' && argv[optind - 1][2] == 'o') { 286 if (!strcmp(optarg, "oasynch")) { 287 noasynchtty = 1; 288 noasynchnet = 1; 289 } else if (!strcmp(optarg, "oasynchtty")) 290 noasynchtty = 1; 291 else if (!strcmp(optarg, "oasynchnet")) 292 noasynchnet = 1; 293 } else 294 #endif /* defined(TN3270) && defined(unix) */ 295 SetNetTrace(optarg); 296 break; 297 case 'r': 298 rlogin = '~'; 299 break; 300 case 't': 301 #if defined(TN3270) && defined(unix) 302 (void)strlcpy(tline, optarg, sizeof(tline)); 303 transcom = tline; 304 #else 305 fprintf(stderr, 306 "%s: Warning: -t ignored, no TN3270 support.\n", 307 prompt); 308 #endif 309 break; 310 case 'x': 311 #ifdef ENCRYPTION 312 encrypt_auto(1); 313 decrypt_auto(1); 314 #else /* ENCRYPTION */ 315 fprintf(stderr, 316 "%s: Warning: -x ignored, no ENCRYPT support.\n", 317 prompt); 318 #endif /* ENCRYPTION */ 319 break; 320 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 321 case 'P': 322 if (!strncmp("in", optarg, 2)) 323 ipsec_policy_in = strdup(optarg); 324 else if (!strncmp("out", optarg, 3)) 325 ipsec_policy_out = strdup(optarg); 326 else 327 usage(); 328 break; 329 #endif 330 case '?': 331 default: 332 usage(); 333 /* NOTREACHED */ 334 } 335 } 336 337 if (autologin == -1) { /* esc@magic.fi; force */ 338 #if defined(AUTHENTICATION) 339 autologin = 1; 340 #endif 341 #if defined(ENCRYPTION) 342 encrypt_auto(1); 343 decrypt_auto(1); 344 #endif 345 } 346 347 if (autologin == -1) 348 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1; 349 350 argc -= optind; 351 argv += optind; 352 353 if (argc) { 354 char *args[7], **argp = args; 355 #ifdef __GNUC__ 356 (void) &argp; /* avoid longjmp clobbering */ 357 #endif 358 359 if (argc > 2) 360 usage(); 361 *argp++ = prompt; 362 if (user) { 363 *argp++ = "-l"; 364 *argp++ = user; 365 } 366 *argp++ = argv[0]; /* host */ 367 if (argc > 1) 368 *argp++ = argv[1]; /* port */ 369 *argp = 0; 370 371 if (setjmp(toplevel) != 0) 372 Exit(0); 373 if (tn(argp - args, args) == 1) 374 return (0); 375 else 376 return (1); 377 } 378 (void)setjmp(toplevel); 379 for (;;) { 380 #ifdef TN3270 381 if (shell_active) 382 shell_continue(); 383 else 384 #endif 385 command(1, 0, 0); 386 } 387 } 388