1 /* $NetBSD: main.c,v 1.33 2024/11/03 10:43:27 rillig 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.33 2024/11/03 10:43:27 rillig 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 /* 74 * Initialize variables. 75 */ 76 void 77 tninit(void) 78 { 79 init_terminal(); 80 81 init_network(); 82 83 init_telnet(); 84 85 init_sys(); 86 87 } 88 89 void 90 usage(void) 91 { 92 fprintf(stderr, "usage: %s %s%s%s%s\n", 93 prompt, 94 #ifdef AUTHENTICATION 95 "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c]", 96 "\n\t[-d] [-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ", 97 #else 98 "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char]", 99 "\n\t[-l user] [-n tracefile] ", 100 #endif 101 "[-r] ", 102 #ifdef ENCRYPTION 103 "[-x] " 104 #endif 105 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 106 "\n\t[-P policy] [host-name [port]]" 107 #else 108 "\n\t[host-name [port]]" 109 #endif 110 ); 111 exit(1); 112 } 113 114 /* 115 * main. Parse arguments, invoke the protocol or command parser. 116 */ 117 118 119 int 120 main(int argc, char *argv[]) 121 { 122 int ch; 123 char *user; 124 #ifdef FORWARD 125 extern int forward_flags; 126 #endif /* FORWARD */ 127 128 tninit(); /* Clear out things */ 129 130 TerminalSaveState(); 131 132 if ((prompt = strrchr(argv[0], '/')) != NULL) 133 ++prompt; 134 else 135 prompt = argv[0]; 136 137 user = NULL; 138 139 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE; 140 autologin = -1; 141 142 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 143 #define IPSECOPT "P:" 144 #else 145 #define IPSECOPT 146 #endif 147 while ((ch = getopt(argc, argv, "468EKLNS:X:acde:fFk:l:n:rt:x" 148 IPSECOPT)) != -1) { 149 #undef IPSECOPT 150 switch(ch) { 151 case '4': 152 family = AF_INET; 153 break; 154 case '6': 155 family = AF_INET6; 156 break; 157 case '8': 158 eight = 3; /* binary output and input */ 159 break; 160 case 'E': 161 rlogin = escape = _POSIX_VDISABLE; 162 break; 163 case 'K': 164 #ifdef AUTHENTICATION 165 autologin = 0; 166 #endif 167 break; 168 case 'L': 169 eight |= 2; /* binary output only */ 170 break; 171 case 'N': 172 doaddrlookup = 0; 173 break; 174 case 'S': 175 { 176 fprintf(stderr, 177 "%s: Warning: -S ignored, no parsetos() support.\n", 178 prompt); 179 } 180 break; 181 case 'X': 182 #ifdef AUTHENTICATION 183 auth_disable_name(optarg); 184 #endif 185 break; 186 case 'a': 187 autologin = 1; 188 break; 189 case 'c': 190 skiprc = 1; 191 break; 192 case 'd': 193 telnet_debug = 1; 194 break; 195 case 'e': 196 set_escape_char(optarg); 197 break; 198 case 'f': 199 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD) 200 if (forward_flags & OPTS_FORWARD_CREDS) { 201 fprintf(stderr, 202 "%s: Only one of -f and -F allowed.\n", 203 prompt); 204 usage(); 205 } 206 forward_flags |= OPTS_FORWARD_CREDS; 207 #else 208 fprintf(stderr, 209 "%s: Warning: -f ignored, no Kerberos V5 support.\n", 210 prompt); 211 #endif 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 forward_flags |= OPTS_FORWARDABLE_CREDS; 223 #else 224 fprintf(stderr, 225 "%s: Warning: -F ignored, no Kerberos V5 support.\n", 226 prompt); 227 #endif 228 break; 229 case 'k': 230 fprintf(stderr, 231 "%s: Warning: -k ignored, no Kerberos V4 support.\n", 232 prompt); 233 break; 234 case 'l': 235 if(autologin == 0) { 236 autologin = -1; 237 } 238 user = optarg; 239 break; 240 case 'n': 241 SetNetTrace(optarg); 242 break; 243 case 'r': 244 rlogin = '~'; 245 break; 246 case 't': 247 fprintf(stderr, 248 "%s: Warning: -t ignored, no TN3270 support.\n", 249 prompt); 250 break; 251 case 'x': 252 #ifdef ENCRYPTION 253 encrypt_auto(1); 254 decrypt_auto(1); 255 #else /* ENCRYPTION */ 256 fprintf(stderr, 257 "%s: Warning: -x ignored, no ENCRYPT support.\n", 258 prompt); 259 #endif /* ENCRYPTION */ 260 break; 261 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 262 case 'P': 263 if (!strncmp("in", optarg, 2)) 264 ipsec_policy_in = strdup(optarg); 265 else if (!strncmp("out", optarg, 3)) 266 ipsec_policy_out = strdup(optarg); 267 else 268 usage(); 269 break; 270 #endif 271 case '?': 272 default: 273 usage(); 274 /* NOTREACHED */ 275 } 276 } 277 278 if (autologin == -1) { /* esc@magic.fi; force */ 279 #if defined(AUTHENTICATION) 280 autologin = 1; 281 #endif 282 #if defined(ENCRYPTION) 283 encrypt_auto(1); 284 decrypt_auto(1); 285 #endif 286 } 287 288 if (autologin == -1) 289 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1; 290 291 argc -= optind; 292 argv += optind; 293 294 if (argc) { 295 static char ml[] = "-l"; 296 char *args[7]; 297 char ** volatile argp; /* avoid longjmp clobbering */ 298 299 argp = args; 300 if (argc > 2) 301 usage(); 302 *argp++ = prompt; 303 if (user) { 304 *argp++ = ml; 305 *argp++ = user; 306 } 307 *argp++ = argv[0]; /* host */ 308 if (argc > 1) 309 *argp++ = argv[1]; /* port */ 310 *argp = 0; 311 312 if (setjmp(toplevel) != 0) 313 Exit(0); 314 if (tn((int)(argp - args), args) == 1) 315 return (0); 316 else 317 return (1); 318 } 319 (void)setjmp(toplevel); 320 for (;;) { 321 command(1, 0, 0); 322 } 323 } 324