1 /* $OpenBSD: main.c,v 1.36 2015/12/06 12:00:16 tobias Exp $ */ 2 /* $NetBSD: main.c,v 1.5 1996/02/28 21:04:05 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include "telnet_locl.h" 34 35 #include <sys/socket.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <unistd.h> 39 40 int family = AF_UNSPEC; 41 int rtableid = -1; 42 43 /* 44 * Initialize variables. 45 */ 46 void 47 tninit(void) 48 { 49 init_terminal(); 50 51 init_network(); 52 53 init_telnet(); 54 55 init_sys(); 56 } 57 58 static __dead void 59 usage(void) 60 { 61 extern char *__progname; 62 63 (void)fprintf(stderr, 64 "usage: %s [-4678acDEKLr] [-b hostalias] [-e escapechar] " 65 "[-l user]\n" 66 "\t[-n tracefile] [-V rtable] [host [port]]\n", 67 __progname); 68 69 exit(1); 70 } 71 72 /* 73 * main. Parse arguments, invoke the protocol or command parser. 74 */ 75 76 int 77 main(int argc, char *argv[]) 78 { 79 int ch; 80 extern char *__progname; 81 char *user, *alias; 82 const char *errstr; 83 84 tninit(); /* Clear out things */ 85 86 TerminalSaveState(); 87 88 prompt = __progname; 89 90 user = alias = NULL; 91 92 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE; 93 94 autologin = -1; 95 96 while ((ch = getopt(argc, argv, "4678ab:cDEe:KLl:n:rV:")) 97 != -1) { 98 switch(ch) { 99 case '4': 100 family = AF_INET; 101 break; 102 case '6': 103 family = AF_INET6; 104 break; 105 case '7': 106 eight = 0; 107 break; 108 case '8': 109 eight = 3; /* binary output and input */ 110 break; 111 case 'a': 112 autologin = 1; 113 break; 114 case 'b': 115 alias = optarg; 116 break; 117 case 'c': 118 skiprc = 1; 119 break; 120 case 'D': { 121 /* sometimes we don't want a mangled display */ 122 char *p; 123 if((p = getenv("DISPLAY"))) 124 env_define("DISPLAY", (unsigned char*)p); 125 break; 126 } 127 case 'E': 128 rlogin = escape = _POSIX_VDISABLE; 129 break; 130 case 'e': 131 set_escape_char(optarg); 132 break; 133 case 'K': 134 autologin = 0; 135 break; 136 case 'L': 137 eight |= 2; /* binary output only */ 138 break; 139 case 'l': 140 autologin = -1; 141 user = optarg; 142 break; 143 case 'n': 144 SetNetTrace(optarg); 145 break; 146 case 'r': 147 rlogin = '~'; 148 break; 149 case 'V': 150 rtableid = (int)strtonum(optarg, 0, 151 RT_TABLEID_MAX, &errstr); 152 if (errstr) { 153 fprintf(stderr, "%s: Warning: " 154 "-V ignored, rtable %s: %s\n", 155 prompt, errstr, optarg); 156 } 157 break; 158 case '?': 159 default: 160 usage(); 161 } 162 } 163 164 if (rtableid >= 0) 165 if (setrtable(rtableid) == -1) { 166 perror("setrtable"); 167 exit(1); 168 } 169 170 if (pledge("stdio rpath wpath getpw dns inet tty", NULL) == -1) { 171 perror("pledge"); 172 exit(1); 173 } 174 175 if (autologin == -1) 176 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1; 177 178 argc -= optind; 179 argv += optind; 180 181 if (argc) { 182 char *args[8], **argp = args; 183 184 if (argc > 2) 185 usage(); 186 *argp++ = prompt; 187 if (user) { 188 *argp++ = "-l"; 189 *argp++ = user; 190 } 191 if (alias) { 192 *argp++ = "-b"; 193 *argp++ = alias; 194 } 195 *argp++ = argv[0]; /* host */ 196 if (argc > 1) 197 *argp++ = argv[1]; /* port */ 198 *argp = NULL; 199 200 if (setjmp(toplevel) != 0) 201 Exit(0); 202 if (tn(argp - args, args) == 1) 203 return (0); 204 else 205 return (1); 206 } 207 (void)setjmp(toplevel); 208 for (;;) { 209 command(1, NULL, 0); 210 } 211 return 0; 212 } 213