Annotation of src/usr.bin/telnet/main.c, Revision 1.35
1.35 ! deraadt 1: /* $OpenBSD: main.c,v 1.34 2015/11/13 18:05:37 schwarze Exp $ */
1.2 niklas 2: /* $NetBSD: main.c,v 1.5 1996/02/28 21:04:05 thorpej Exp $ */
3:
1.1 deraadt 4: /*
1.2 niklas 5: * Copyright (c) 1988, 1990, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 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.
1.14 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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:
1.5 art 33: #include "telnet_locl.h"
1.1 deraadt 34:
1.26 guenther 35: #include <sys/socket.h>
36: #include <stdlib.h>
37: #include <string.h>
1.25 guenther 38: #include <unistd.h>
1.1 deraadt 39:
1.15 otto 40: int family = AF_UNSPEC;
1.22 sthen 41: int rtableid = -1;
1.15 otto 42:
1.1 deraadt 43: /*
44: * Initialize variables.
45: */
1.30 jsg 46: void
47: tninit(void)
1.1 deraadt 48: {
49: init_terminal();
50:
51: init_network();
1.2 niklas 52:
1.1 deraadt 53: init_telnet();
54:
55: init_sys();
56: }
57:
1.27 guenther 58: static __dead void
59: usage(void)
1.1 deraadt 60: {
1.16 jmc 61: extern char *__progname;
62:
63: (void)fprintf(stderr,
1.31 deraadt 64: "usage: %s [-4678acDEKLr] [-b hostalias] [-e escapechar] "
1.23 guenther 65: "[-l user]\n"
66: "\t[-n tracefile] [-V rtable] [host [port]]\n",
1.16 jmc 67: __progname);
68:
1.1 deraadt 69: exit(1);
70: }
71:
72: /*
73: * main. Parse arguments, invoke the protocol or command parser.
74: */
75:
1.30 jsg 76: int
77: main(int argc, char *argv[])
1.1 deraadt 78: {
79: int ch;
1.5 art 80: char *user, *alias;
1.17 claudio 81: const char *errstr;
1.33 deraadt 82:
1.1 deraadt 83: tninit(); /* Clear out things */
84:
85: TerminalSaveState();
86:
1.5 art 87: if ((prompt = strrchr(argv[0], '/')))
1.1 deraadt 88: ++prompt;
89: else
90: prompt = argv[0];
91:
1.3 niklas 92: user = alias = NULL;
1.1 deraadt 93:
94: rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
1.5 art 95:
1.1 deraadt 96: autologin = -1;
97:
1.31 deraadt 98: while ((ch = getopt(argc, argv, "4678ab:cDEe:KLl:n:rV:"))
1.15 otto 99: != -1) {
1.1 deraadt 100: switch(ch) {
1.15 otto 101: case '4':
102: family = AF_INET;
103: break;
104: case '6':
105: family = AF_INET6;
106: break;
1.23 guenther 107: case '7':
108: eight = 0;
109: break;
1.1 deraadt 110: case '8':
111: eight = 3; /* binary output and input */
112: break;
1.23 guenther 113: case 'a':
114: autologin = 1;
115: break;
116: case 'b':
117: alias = optarg;
118: break;
119: case 'c':
120: skiprc = 1;
1.5 art 121: break;
122: case 'D': {
123: /* sometimes we don't want a mangled display */
124: char *p;
125: if((p = getenv("DISPLAY")))
126: env_define("DISPLAY", (unsigned char*)p);
127: break;
128: }
1.1 deraadt 129: case 'E':
130: rlogin = escape = _POSIX_VDISABLE;
131: break;
1.23 guenther 132: case 'e':
133: set_escape_char(optarg);
134: break;
1.1 deraadt 135: case 'K':
136: autologin = 0;
137: break;
138: case 'L':
139: eight |= 2; /* binary output only */
140: break;
141: case 'l':
1.6 art 142: autologin = -1;
1.1 deraadt 143: user = optarg;
144: break;
145: case 'n':
1.23 guenther 146: SetNetTrace(optarg);
1.1 deraadt 147: break;
148: case 'r':
149: rlogin = '~';
150: break;
1.17 claudio 151: case 'V':
1.22 sthen 152: rtableid = (int)strtonum(optarg, 0,
1.17 claudio 153: RT_TABLEID_MAX, &errstr);
154: if (errstr) {
1.20 guenther 155: fprintf(stderr, "%s: Warning: "
156: "-V ignored, rtable %s: %s\n",
1.17 claudio 157: prompt, errstr, optarg);
158: }
1.1 deraadt 159: break;
160: case '?':
161: default:
162: usage();
163: }
164: }
1.32 deraadt 165:
166: if (rtableid >= 0)
167: if (setrtable(rtableid) == -1) {
168: perror("setrtable");
169: exit(1);
170: }
1.34 schwarze 171:
1.35 ! deraadt 172: if (pledge("stdio rpath wpath getpw dns inet tty", NULL) == -1) {
1.34 schwarze 173: perror("pledge");
174: exit(1);
175: }
1.5 art 176:
1.1 deraadt 177: if (autologin == -1)
178: autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
179:
180: argc -= optind;
181: argv += optind;
182:
183: if (argc) {
1.29 guenther 184: char *args[8], **argp = args;
1.1 deraadt 185:
186: if (argc > 2)
187: usage();
188: *argp++ = prompt;
189: if (user) {
190: *argp++ = "-l";
191: *argp++ = user;
1.3 niklas 192: }
193: if (alias) {
194: *argp++ = "-b";
195: *argp++ = alias;
1.1 deraadt 196: }
197: *argp++ = argv[0]; /* host */
198: if (argc > 1)
199: *argp++ = argv[1]; /* port */
1.28 jsg 200: *argp = NULL;
1.1 deraadt 201:
202: if (setjmp(toplevel) != 0)
203: Exit(0);
204: if (tn(argp - args, args) == 1)
205: return (0);
206: else
207: return (1);
208: }
209: (void)setjmp(toplevel);
210: for (;;) {
1.28 jsg 211: command(1, NULL, 0);
1.1 deraadt 212: }
1.7 art 213: return 0;
1.1 deraadt 214: }