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