Home | History | Annotate | Line # | Download | only in telnet
main.c revision 1.16
      1 /*	$NetBSD: main.c,v 1.16 2002/06/14 00:30:56 wiz 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.16 2002/06/14 00:30:56 wiz Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 
     52 #include <unistd.h>
     53 
     54 #include "ring.h"
     55 #include "externs.h"
     56 #include "defines.h"
     57 #ifdef AUTHENTICATION
     58 #include <libtelnet/auth.h>
     59 #endif
     60 #ifdef ENCRYPTION
     61 #include <libtelnet/encrypt.h>
     62 #endif
     63 
     64 /* These values need to be the same as defined in libtelnet/kerberos5.c */
     65 /* Either define them in both places, or put in some common header file. */
     66 #define OPTS_FORWARD_CREDS	0x00000002
     67 #define OPTS_FORWARDABLE_CREDS	0x00000001
     68 
     69 #if 0
     70 #define FORWARD
     71 #endif
     72 
     73 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
     74 char *ipsec_policy_in = NULL;
     75 char *ipsec_policy_out = NULL;
     76 #endif
     77 
     78 int main(int, char *[]);
     79 
     80 /*
     81  * Initialize variables.
     82  */
     83     void
     84 tninit()
     85 {
     86     init_terminal();
     87 
     88     init_network();
     89 
     90     init_telnet();
     91 
     92     init_sys();
     93 
     94 #if defined(TN3270)
     95     init_3270();
     96 #endif
     97 }
     98 
     99 	void
    100 usage()
    101 {
    102 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
    103 	    prompt,
    104 #ifdef	AUTHENTICATION
    105 	    "[-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c] [-d]",
    106 	    "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
    107 #else
    108 	    "[-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
    109 	    "\n\t[-n tracefile]",
    110 #endif
    111 #if defined(TN3270) && defined(unix)
    112 # ifdef AUTHENTICATION
    113 	    "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
    114 # else
    115 	    "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
    116 # endif
    117 #else
    118 	    "[-r] ",
    119 #endif
    120 #ifdef	ENCRYPTION
    121 	    "[-x] "
    122 #endif
    123 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
    124 	    "[-P policy] [host-name [port]]"
    125 #else
    126 	    "[host-name [port]]"
    127 #endif
    128 	);
    129 	exit(1);
    130 }
    131 
    132 /*
    133  * main.  Parse arguments, invoke the protocol or command parser.
    134  */
    135 
    136 
    137 int
    138 main(argc, argv)
    139 	int argc;
    140 	char *argv[];
    141 {
    142 	extern char *optarg;
    143 	extern int optind;
    144 	int ch;
    145 	char *user;
    146 #ifdef	FORWARD
    147 	extern int forward_flags;
    148 #endif	/* FORWARD */
    149 
    150 	tninit();		/* Clear out things */
    151 
    152 	TerminalSaveState();
    153 
    154 	if ((prompt = strrchr(argv[0], '/')) != NULL)
    155 		++prompt;
    156 	else
    157 		prompt = argv[0];
    158 
    159 	user = NULL;
    160 
    161 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
    162 	autologin = -1;
    163 
    164 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
    165 #define IPSECOPT	"P:"
    166 #else
    167 #define IPSECOPT
    168 #endif
    169 	while ((ch = getopt(argc, argv, "8EKLNS:X:acde:fFk:l:n:rt:x"
    170 			IPSECOPT)) != -1) {
    171 #undef IPSECOPT
    172 		switch(ch) {
    173 		case '8':
    174 			eight = 3;	/* binary output and input */
    175 			break;
    176 		case 'E':
    177 			rlogin = escape = _POSIX_VDISABLE;
    178 			break;
    179 		case 'K':
    180 #ifdef	AUTHENTICATION
    181 			autologin = 0;
    182 #endif
    183 			break;
    184 		case 'L':
    185 			eight |= 2;	/* binary output only */
    186 			break;
    187 		case 'N':
    188 			doaddrlookup = 0;
    189 			break;
    190 		case 'S':
    191 		    {
    192 #ifdef	HAS_GETTOS
    193 			extern int tos;
    194 
    195 			if ((tos = parsetos(optarg, "tcp")) < 0)
    196 				fprintf(stderr, "%s%s%s%s\n",
    197 					prompt, ": Bad TOS argument '",
    198 					optarg,
    199 					"; will try to use default TOS");
    200 #else
    201 			fprintf(stderr,
    202 			   "%s: Warning: -S ignored, no parsetos() support.\n",
    203 								prompt);
    204 #endif
    205 		    }
    206 			break;
    207 		case 'X':
    208 #ifdef	AUTHENTICATION
    209 			auth_disable_name(optarg);
    210 #endif
    211 			break;
    212 		case 'a':
    213 			autologin = 1;
    214 			break;
    215 		case 'c':
    216 			skiprc = 1;
    217 			break;
    218 		case 'd':
    219 			debug = 1;
    220 			break;
    221 		case 'e':
    222 			set_escape_char(optarg);
    223 			break;
    224 		case 'f':
    225 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
    226 			if (forward_flags & OPTS_FORWARD_CREDS) {
    227 			    fprintf(stderr,
    228 				    "%s: Only one of -f and -F allowed.\n",
    229 				    prompt);
    230 			    usage();
    231 			}
    232 			forward_flags |= OPTS_FORWARD_CREDS;
    233 #else
    234 			fprintf(stderr,
    235 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
    236 				prompt);
    237 #endif
    238 			break;
    239 		case 'F':
    240 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
    241 			if (forward_flags & OPTS_FORWARD_CREDS) {
    242 			    fprintf(stderr,
    243 				    "%s: Only one of -f and -F allowed.\n",
    244 				    prompt);
    245 			    usage();
    246 			}
    247 			forward_flags |= OPTS_FORWARD_CREDS;
    248 			forward_flags |= OPTS_FORWARDABLE_CREDS;
    249 #else
    250 			fprintf(stderr,
    251 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
    252 				prompt);
    253 #endif
    254 			break;
    255 		case 'k':
    256 #if defined(AUTHENTICATION) && defined(KRB4)
    257 		    {
    258 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
    259 			dest_realm = dst_realm_buf;
    260 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
    261 		    }
    262 #else
    263 			fprintf(stderr,
    264 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
    265 								prompt);
    266 #endif
    267 			break;
    268 		case 'l':
    269 			if(autologin == 0) {
    270 				autologin = -1;
    271 			}
    272 			user = optarg;
    273 			break;
    274 		case 'n':
    275 #if defined(TN3270) && defined(unix)
    276 			/* distinguish between "-n oasynch" and "-noasynch" */
    277 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
    278 			    == 'n' && argv[optind - 1][2] == 'o') {
    279 				if (!strcmp(optarg, "oasynch")) {
    280 					noasynchtty = 1;
    281 					noasynchnet = 1;
    282 				} else if (!strcmp(optarg, "oasynchtty"))
    283 					noasynchtty = 1;
    284 				else if (!strcmp(optarg, "oasynchnet"))
    285 					noasynchnet = 1;
    286 			} else
    287 #endif	/* defined(TN3270) && defined(unix) */
    288 				SetNetTrace(optarg);
    289 			break;
    290 		case 'r':
    291 			rlogin = '~';
    292 			break;
    293 		case 't':
    294 #if defined(TN3270) && defined(unix)
    295 			(void)strlcpy(tline, optarg, sizeof(tline));
    296 			transcom = tline;
    297 #else
    298 			fprintf(stderr,
    299 			   "%s: Warning: -t ignored, no TN3270 support.\n",
    300 								prompt);
    301 #endif
    302 			break;
    303 		case 'x':
    304 #ifdef	ENCRYPTION
    305 			encrypt_auto(1);
    306 			decrypt_auto(1);
    307 #else	/* ENCRYPTION */
    308 			fprintf(stderr,
    309 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
    310 								prompt);
    311 #endif	/* ENCRYPTION */
    312 			break;
    313 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
    314 		case 'P':
    315 			if (!strncmp("in", optarg, 2))
    316 				ipsec_policy_in = strdup(optarg);
    317 			else if (!strncmp("out", optarg, 3))
    318 				ipsec_policy_out = strdup(optarg);
    319 			else
    320 				usage();
    321 			break;
    322 #endif
    323 		case '?':
    324 		default:
    325 			usage();
    326 			/* NOTREACHED */
    327 		}
    328 	}
    329 
    330 	if (autologin == -1) {		/* esc (at) magic.fi; force  */
    331 #if defined(AUTHENTICATION)
    332 		autologin = 1;
    333 #endif
    334 #if defined(ENCRYPTION)
    335 		encrypt_auto(1);
    336 		decrypt_auto(1);
    337 #endif
    338 	}
    339 
    340 	if (autologin == -1)
    341 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
    342 
    343 	argc -= optind;
    344 	argv += optind;
    345 
    346 	if (argc) {
    347 		char *args[7], **argp = args;
    348 #ifdef __GNUC__
    349 		(void) &argp;	/* avoid longjmp clobbering */
    350 #endif
    351 
    352 		if (argc > 2)
    353 			usage();
    354 		*argp++ = prompt;
    355 		if (user) {
    356 			*argp++ = "-l";
    357 			*argp++ = user;
    358 		}
    359 		*argp++ = argv[0];		/* host */
    360 		if (argc > 1)
    361 			*argp++ = argv[1];	/* port */
    362 		*argp = 0;
    363 
    364 		if (setjmp(toplevel) != 0)
    365 			Exit(0);
    366 		if (tn(argp - args, args) == 1)
    367 			return (0);
    368 		else
    369 			return (1);
    370 	}
    371 	(void)setjmp(toplevel);
    372 	for (;;) {
    373 #ifdef TN3270
    374 		if (shell_active)
    375 			shell_continue();
    376 		else
    377 #endif
    378 			command(1, 0, 0);
    379 	}
    380 }
    381