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