netcat.c revision 1.1 1 1.1 christos /* $OpenBSD: netcat.c,v 1.172 2017/02/05 01:39:14 jca Exp $ */
2 1.1 christos /*
3 1.1 christos * Copyright (c) 2001 Eric Jackson <ericj (at) monkey.org>
4 1.1 christos * Copyright (c) 2015 Bob Beck. All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos *
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. The name of the author may not be used to endorse or promote products
16 1.1 christos * derived from this software without specific prior written permission.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 christos */
29 1.1 christos
30 1.1 christos /*
31 1.1 christos * Re-written nc(1) for OpenBSD. Original implementation by
32 1.1 christos * *Hobbit* <hobbit (at) avian.org>.
33 1.1 christos */
34 1.1 christos
35 1.1 christos #include <sys/types.h>
36 1.1 christos #include <sys/socket.h>
37 1.1 christos #include <sys/uio.h>
38 1.1 christos #include <sys/un.h>
39 1.1 christos
40 1.1 christos #include <netinet/in.h>
41 1.1 christos #include <netinet/tcp.h>
42 1.1 christos #include <netinet/ip.h>
43 1.1 christos #include <arpa/telnet.h>
44 1.1 christos
45 1.1 christos #include <err.h>
46 1.1 christos #include <errno.h>
47 1.1 christos #include <limits.h>
48 1.1 christos #include <netdb.h>
49 1.1 christos #include <poll.h>
50 1.1 christos #include <signal.h>
51 1.1 christos #include <stdarg.h>
52 1.1 christos #include <stdio.h>
53 1.1 christos #include <stdlib.h>
54 1.1 christos #include <string.h>
55 1.1 christos #include <time.h>
56 1.1 christos #include <unistd.h>
57 1.1 christos #include <tls.h>
58 1.1 christos #include "atomicio.h"
59 1.1 christos
60 1.1 christos #define PORT_MAX 65535
61 1.1 christos #define UNIX_DG_TMP_SOCKET_SIZE 19
62 1.1 christos
63 1.1 christos #define POLL_STDIN 0
64 1.1 christos #define POLL_NETOUT 1
65 1.1 christos #define POLL_NETIN 2
66 1.1 christos #define POLL_STDOUT 3
67 1.1 christos #define BUFSIZE 16384
68 1.1 christos #define DEFAULT_CA_FILE "/etc/ssl/cert.pem"
69 1.1 christos
70 1.1 christos #define TLS_ALL (1 << 1)
71 1.1 christos #define TLS_NOVERIFY (1 << 2)
72 1.1 christos #define TLS_NONAME (1 << 3)
73 1.1 christos #define TLS_CCERT (1 << 4)
74 1.1 christos #define TLS_MUSTSTAPLE (1 << 5)
75 1.1 christos
76 1.1 christos /* Command Line Options */
77 1.1 christos int dflag; /* detached, no stdin */
78 1.1 christos int Fflag; /* fdpass sock to stdout */
79 1.1 christos unsigned int iflag; /* Interval Flag */
80 1.1 christos int kflag; /* More than one connect */
81 1.1 christos int lflag; /* Bind to local port */
82 1.1 christos int Nflag; /* shutdown() network socket */
83 1.1 christos int nflag; /* Don't do name look up */
84 1.1 christos char *Pflag; /* Proxy username */
85 1.1 christos char *pflag; /* Localport flag */
86 1.1 christos int rflag; /* Random ports flag */
87 1.1 christos char *sflag; /* Source Address */
88 1.1 christos int tflag; /* Telnet Emulation */
89 1.1 christos int uflag; /* UDP - Default to TCP */
90 1.1 christos int vflag; /* Verbosity */
91 1.1 christos int xflag; /* Socks proxy */
92 1.1 christos int zflag; /* Port Scan Flag */
93 1.1 christos int Dflag; /* sodebug */
94 1.1 christos int Iflag; /* TCP receive buffer size */
95 1.1 christos int Oflag; /* TCP send buffer size */
96 1.1 christos int Sflag; /* TCP MD5 signature option */
97 1.1 christos int Tflag = -1; /* IP Type of Service */
98 1.1 christos int rtableid = -1;
99 1.1 christos
100 1.1 christos int usetls; /* use TLS */
101 1.1 christos char *Cflag; /* Public cert file */
102 1.1 christos char *Kflag; /* Private key file */
103 1.1 christos char *oflag; /* OCSP stapling file */
104 1.1 christos char *Rflag = DEFAULT_CA_FILE; /* Root CA file */
105 1.1 christos int tls_cachanged; /* Using non-default CA file */
106 1.1 christos int TLSopt; /* TLS options */
107 1.1 christos char *tls_expectname; /* required name in peer cert */
108 1.1 christos char *tls_expecthash; /* required hash of peer cert */
109 1.1 christos
110 1.1 christos int timeout = -1;
111 1.1 christos int family = AF_UNSPEC;
112 1.1 christos char *portlist[PORT_MAX+1];
113 1.1 christos char *unix_dg_tmp_socket;
114 1.1 christos int ttl = -1;
115 1.1 christos int minttl = -1;
116 1.1 christos
117 1.1 christos void atelnet(int, unsigned char *, unsigned int);
118 1.1 christos void build_ports(char *);
119 1.1 christos void help(void);
120 1.1 christos int local_listen(char *, char *, struct addrinfo);
121 1.1 christos void readwrite(int, struct tls *);
122 1.1 christos void fdpass(int nfd) __attribute__((noreturn));
123 1.1 christos int remote_connect(const char *, const char *, struct addrinfo);
124 1.1 christos int timeout_connect(int, const struct sockaddr *, socklen_t);
125 1.1 christos int socks_connect(const char *, const char *, struct addrinfo,
126 1.1 christos const char *, const char *, struct addrinfo, int, const char *);
127 1.1 christos int udptest(int);
128 1.1 christos int unix_bind(char *, int);
129 1.1 christos int unix_connect(char *);
130 1.1 christos int unix_listen(char *);
131 1.1 christos void set_common_sockopts(int, int);
132 1.1 christos int map_tos(char *, int *);
133 1.1 christos int map_tls(char *, int *);
134 1.1 christos void report_connect(const struct sockaddr *, socklen_t, char *);
135 1.1 christos void report_tls(struct tls *tls_ctx, char * host, char *tls_expectname);
136 1.1 christos void usage(int);
137 1.1 christos ssize_t drainbuf(int, unsigned char *, size_t *, struct tls *);
138 1.1 christos ssize_t fillbuf(int, unsigned char *, size_t *, struct tls *);
139 1.1 christos void tls_setup_client(struct tls *, int, char *);
140 1.1 christos struct tls *tls_setup_server(struct tls *, int, char *);
141 1.1 christos
142 1.1 christos int
143 1.1 christos main(int argc, char *argv[])
144 1.1 christos {
145 1.1 christos int ch, s = -1, ret, socksv;
146 1.1 christos char *host, *uport;
147 1.1 christos struct addrinfo hints;
148 1.1 christos struct servent *sv;
149 1.1 christos socklen_t len;
150 1.1 christos struct sockaddr_storage cliaddr;
151 1.1 christos char *proxy, *proxyport = NULL;
152 1.1 christos const char *errstr;
153 1.1 christos struct addrinfo proxyhints;
154 1.1 christos char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
155 1.1 christos struct tls_config *tls_cfg = NULL;
156 1.1 christos struct tls *tls_ctx = NULL;
157 1.1 christos
158 1.1 christos ret = 1;
159 1.1 christos socksv = 5;
160 1.1 christos host = NULL;
161 1.1 christos uport = NULL;
162 1.1 christos sv = NULL;
163 1.1 christos
164 1.1 christos signal(SIGPIPE, SIG_IGN);
165 1.1 christos
166 1.1 christos while ((ch = getopt(argc, argv,
167 1.1 christos "46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:R:rSs:T:tUuV:vw:X:x:z")) != -1) {
168 1.1 christos switch (ch) {
169 1.1 christos case '4':
170 1.1 christos family = AF_INET;
171 1.1 christos break;
172 1.1 christos case '6':
173 1.1 christos family = AF_INET6;
174 1.1 christos break;
175 1.1 christos case 'U':
176 1.1 christos family = AF_UNIX;
177 1.1 christos break;
178 1.1 christos case 'X':
179 1.1 christos if (strcasecmp(optarg, "connect") == 0)
180 1.1 christos socksv = -1; /* HTTP proxy CONNECT */
181 1.1 christos else if (strcmp(optarg, "4") == 0)
182 1.1 christos socksv = 4; /* SOCKS v.4 */
183 1.1 christos else if (strcmp(optarg, "5") == 0)
184 1.1 christos socksv = 5; /* SOCKS v.5 */
185 1.1 christos else
186 1.1 christos errx(1, "unsupported proxy protocol");
187 1.1 christos break;
188 1.1 christos case 'C':
189 1.1 christos Cflag = optarg;
190 1.1 christos break;
191 1.1 christos case 'c':
192 1.1 christos usetls = 1;
193 1.1 christos break;
194 1.1 christos case 'd':
195 1.1 christos dflag = 1;
196 1.1 christos break;
197 1.1 christos case 'e':
198 1.1 christos tls_expectname = optarg;
199 1.1 christos break;
200 1.1 christos case 'F':
201 1.1 christos Fflag = 1;
202 1.1 christos break;
203 1.1 christos case 'H':
204 1.1 christos tls_expecthash = optarg;
205 1.1 christos break;
206 1.1 christos case 'h':
207 1.1 christos help();
208 1.1 christos break;
209 1.1 christos case 'i':
210 1.1 christos iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
211 1.1 christos if (errstr)
212 1.1 christos errx(1, "interval %s: %s", errstr, optarg);
213 1.1 christos break;
214 1.1 christos case 'K':
215 1.1 christos Kflag = optarg;
216 1.1 christos break;
217 1.1 christos case 'k':
218 1.1 christos kflag = 1;
219 1.1 christos break;
220 1.1 christos case 'l':
221 1.1 christos lflag = 1;
222 1.1 christos break;
223 1.1 christos case 'M':
224 1.1 christos ttl = strtonum(optarg, 0, 255, &errstr);
225 1.1 christos if (errstr)
226 1.1 christos errx(1, "ttl is %s", errstr);
227 1.1 christos break;
228 1.1 christos case 'm':
229 1.1 christos minttl = strtonum(optarg, 0, 255, &errstr);
230 1.1 christos if (errstr)
231 1.1 christos errx(1, "minttl is %s", errstr);
232 1.1 christos break;
233 1.1 christos case 'N':
234 1.1 christos Nflag = 1;
235 1.1 christos break;
236 1.1 christos case 'n':
237 1.1 christos nflag = 1;
238 1.1 christos break;
239 1.1 christos case 'P':
240 1.1 christos Pflag = optarg;
241 1.1 christos break;
242 1.1 christos case 'p':
243 1.1 christos pflag = optarg;
244 1.1 christos break;
245 1.1 christos case 'R':
246 1.1 christos tls_cachanged = 1;
247 1.1 christos Rflag = optarg;
248 1.1 christos break;
249 1.1 christos case 'r':
250 1.1 christos rflag = 1;
251 1.1 christos break;
252 1.1 christos case 's':
253 1.1 christos sflag = optarg;
254 1.1 christos break;
255 1.1 christos case 't':
256 1.1 christos tflag = 1;
257 1.1 christos break;
258 1.1 christos case 'u':
259 1.1 christos uflag = 1;
260 1.1 christos break;
261 1.1 christos case 'V':
262 1.1 christos rtableid = (int)strtonum(optarg, 0,
263 1.1 christos RT_TABLEID_MAX, &errstr);
264 1.1 christos if (errstr)
265 1.1 christos errx(1, "rtable %s: %s", errstr, optarg);
266 1.1 christos break;
267 1.1 christos case 'v':
268 1.1 christos vflag = 1;
269 1.1 christos break;
270 1.1 christos case 'w':
271 1.1 christos timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
272 1.1 christos if (errstr)
273 1.1 christos errx(1, "timeout %s: %s", errstr, optarg);
274 1.1 christos timeout *= 1000;
275 1.1 christos break;
276 1.1 christos case 'x':
277 1.1 christos xflag = 1;
278 1.1 christos if ((proxy = strdup(optarg)) == NULL)
279 1.1 christos err(1, NULL);
280 1.1 christos break;
281 1.1 christos case 'z':
282 1.1 christos zflag = 1;
283 1.1 christos break;
284 1.1 christos case 'D':
285 1.1 christos Dflag = 1;
286 1.1 christos break;
287 1.1 christos case 'I':
288 1.1 christos Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
289 1.1 christos if (errstr != NULL)
290 1.1 christos errx(1, "TCP receive window %s: %s",
291 1.1 christos errstr, optarg);
292 1.1 christos break;
293 1.1 christos case 'O':
294 1.1 christos Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
295 1.1 christos if (errstr != NULL)
296 1.1 christos errx(1, "TCP send window %s: %s",
297 1.1 christos errstr, optarg);
298 1.1 christos break;
299 1.1 christos case 'o':
300 1.1 christos oflag = optarg;
301 1.1 christos break;
302 1.1 christos case 'S':
303 1.1 christos Sflag = 1;
304 1.1 christos break;
305 1.1 christos case 'T':
306 1.1 christos errstr = NULL;
307 1.1 christos errno = 0;
308 1.1 christos if (map_tos(optarg, &Tflag))
309 1.1 christos break;
310 1.1 christos if (map_tls(optarg, &TLSopt))
311 1.1 christos break;
312 1.1 christos if (strlen(optarg) > 1 && optarg[0] == '0' &&
313 1.1 christos optarg[1] == 'x')
314 1.1 christos Tflag = (int)strtol(optarg, NULL, 16);
315 1.1 christos else
316 1.1 christos Tflag = (int)strtonum(optarg, 0, 255,
317 1.1 christos &errstr);
318 1.1 christos if (Tflag < 0 || Tflag > 255 || errstr || errno)
319 1.1 christos errx(1, "illegal tos/tls value %s", optarg);
320 1.1 christos break;
321 1.1 christos default:
322 1.1 christos usage(1);
323 1.1 christos }
324 1.1 christos }
325 1.1 christos argc -= optind;
326 1.1 christos argv += optind;
327 1.1 christos
328 1.1 christos if (rtableid >= 0)
329 1.1 christos if (setrtable(rtableid) == -1)
330 1.1 christos err(1, "setrtable");
331 1.1 christos
332 1.1 christos if (family == AF_UNIX) {
333 1.1 christos if (pledge("stdio rpath wpath cpath tmppath unix", NULL) == -1)
334 1.1 christos err(1, "pledge");
335 1.1 christos } else if (Fflag) {
336 1.1 christos if (Pflag) {
337 1.1 christos if (pledge("stdio inet dns sendfd tty", NULL) == -1)
338 1.1 christos err(1, "pledge");
339 1.1 christos } else if (pledge("stdio inet dns sendfd", NULL) == -1)
340 1.1 christos err(1, "pledge");
341 1.1 christos } else if (Pflag) {
342 1.1 christos if (pledge("stdio inet dns tty", NULL) == -1)
343 1.1 christos err(1, "pledge");
344 1.1 christos } else if (usetls) {
345 1.1 christos if (pledge("stdio rpath inet dns", NULL) == -1)
346 1.1 christos err(1, "pledge");
347 1.1 christos } else if (pledge("stdio inet dns", NULL) == -1)
348 1.1 christos err(1, "pledge");
349 1.1 christos
350 1.1 christos /* Cruft to make sure options are clean, and used properly. */
351 1.1 christos if (argv[0] && !argv[1] && family == AF_UNIX) {
352 1.1 christos host = argv[0];
353 1.1 christos uport = NULL;
354 1.1 christos } else if (argv[0] && !argv[1]) {
355 1.1 christos if (!lflag)
356 1.1 christos usage(1);
357 1.1 christos uport = argv[0];
358 1.1 christos host = NULL;
359 1.1 christos } else if (argv[0] && argv[1]) {
360 1.1 christos host = argv[0];
361 1.1 christos uport = argv[1];
362 1.1 christos } else
363 1.1 christos usage(1);
364 1.1 christos
365 1.1 christos if (lflag && sflag)
366 1.1 christos errx(1, "cannot use -s and -l");
367 1.1 christos if (lflag && pflag)
368 1.1 christos errx(1, "cannot use -p and -l");
369 1.1 christos if (lflag && zflag)
370 1.1 christos errx(1, "cannot use -z and -l");
371 1.1 christos if (!lflag && kflag)
372 1.1 christos errx(1, "must use -l with -k");
373 1.1 christos if (uflag && usetls)
374 1.1 christos errx(1, "cannot use -c and -u");
375 1.1 christos if ((family == AF_UNIX) && usetls)
376 1.1 christos errx(1, "cannot use -c and -U");
377 1.1 christos if ((family == AF_UNIX) && Fflag)
378 1.1 christos errx(1, "cannot use -F and -U");
379 1.1 christos if (Fflag && usetls)
380 1.1 christos errx(1, "cannot use -c and -F");
381 1.1 christos if (TLSopt && !usetls)
382 1.1 christos errx(1, "you must specify -c to use TLS options");
383 1.1 christos if (Cflag && !usetls)
384 1.1 christos errx(1, "you must specify -c to use -C");
385 1.1 christos if (Kflag && !usetls)
386 1.1 christos errx(1, "you must specify -c to use -K");
387 1.1 christos if (oflag && !Cflag)
388 1.1 christos errx(1, "you must specify -C to use -o");
389 1.1 christos if (tls_cachanged && !usetls)
390 1.1 christos errx(1, "you must specify -c to use -R");
391 1.1 christos if (tls_expecthash && !usetls)
392 1.1 christos errx(1, "you must specify -c to use -H");
393 1.1 christos if (tls_expectname && !usetls)
394 1.1 christos errx(1, "you must specify -c to use -e");
395 1.1 christos
396 1.1 christos /* Get name of temporary socket for unix datagram client */
397 1.1 christos if ((family == AF_UNIX) && uflag && !lflag) {
398 1.1 christos if (sflag) {
399 1.1 christos unix_dg_tmp_socket = sflag;
400 1.1 christos } else {
401 1.1 christos strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
402 1.1 christos UNIX_DG_TMP_SOCKET_SIZE);
403 1.1 christos if (mktemp(unix_dg_tmp_socket_buf) == NULL)
404 1.1 christos err(1, "mktemp");
405 1.1 christos unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
406 1.1 christos }
407 1.1 christos }
408 1.1 christos
409 1.1 christos /* Initialize addrinfo structure. */
410 1.1 christos if (family != AF_UNIX) {
411 1.1 christos memset(&hints, 0, sizeof(struct addrinfo));
412 1.1 christos hints.ai_family = family;
413 1.1 christos hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
414 1.1 christos hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
415 1.1 christos if (nflag)
416 1.1 christos hints.ai_flags |= AI_NUMERICHOST;
417 1.1 christos }
418 1.1 christos
419 1.1 christos if (xflag) {
420 1.1 christos if (uflag)
421 1.1 christos errx(1, "no proxy support for UDP mode");
422 1.1 christos
423 1.1 christos if (lflag)
424 1.1 christos errx(1, "no proxy support for listen");
425 1.1 christos
426 1.1 christos if (family == AF_UNIX)
427 1.1 christos errx(1, "no proxy support for unix sockets");
428 1.1 christos
429 1.1 christos if (sflag)
430 1.1 christos errx(1, "no proxy support for local source address");
431 1.1 christos
432 1.1 christos if (*proxy == '[') {
433 1.1 christos ++proxy;
434 1.1 christos proxyport = strchr(proxy, ']');
435 1.1 christos if (proxyport == NULL)
436 1.1 christos errx(1, "missing closing bracket in proxy");
437 1.1 christos *proxyport++ = '\0';
438 1.1 christos if (*proxyport == '\0')
439 1.1 christos /* Use default proxy port. */
440 1.1 christos proxyport = NULL;
441 1.1 christos else {
442 1.1 christos if (*proxyport == ':')
443 1.1 christos ++proxyport;
444 1.1 christos else
445 1.1 christos errx(1, "garbage proxy port delimiter");
446 1.1 christos }
447 1.1 christos } else {
448 1.1 christos proxyport = strrchr(proxy, ':');
449 1.1 christos if (proxyport != NULL)
450 1.1 christos *proxyport++ = '\0';
451 1.1 christos }
452 1.1 christos
453 1.1 christos memset(&proxyhints, 0, sizeof(struct addrinfo));
454 1.1 christos proxyhints.ai_family = family;
455 1.1 christos proxyhints.ai_socktype = SOCK_STREAM;
456 1.1 christos proxyhints.ai_protocol = IPPROTO_TCP;
457 1.1 christos if (nflag)
458 1.1 christos proxyhints.ai_flags |= AI_NUMERICHOST;
459 1.1 christos }
460 1.1 christos
461 1.1 christos if (usetls) {
462 1.1 christos if (Pflag) {
463 1.1 christos if (pledge("stdio inet dns tty rpath", NULL) == -1)
464 1.1 christos err(1, "pledge");
465 1.1 christos } else if (pledge("stdio inet dns rpath", NULL) == -1)
466 1.1 christos err(1, "pledge");
467 1.1 christos
468 1.1 christos if (tls_init() == -1)
469 1.1 christos errx(1, "unable to initialize TLS");
470 1.1 christos if ((tls_cfg = tls_config_new()) == NULL)
471 1.1 christos errx(1, "unable to allocate TLS config");
472 1.1 christos if (Rflag && tls_config_set_ca_file(tls_cfg, Rflag) == -1)
473 1.1 christos errx(1, "%s", tls_config_error(tls_cfg));
474 1.1 christos if (Cflag && tls_config_set_cert_file(tls_cfg, Cflag) == -1)
475 1.1 christos errx(1, "%s", tls_config_error(tls_cfg));
476 1.1 christos if (Kflag && tls_config_set_key_file(tls_cfg, Kflag) == -1)
477 1.1 christos errx(1, "%s", tls_config_error(tls_cfg));
478 1.1 christos if (oflag && tls_config_set_ocsp_staple_file(tls_cfg, oflag) == -1)
479 1.1 christos errx(1, "%s", tls_config_error(tls_cfg));
480 1.1 christos if (TLSopt & TLS_ALL) {
481 1.1 christos if (tls_config_set_protocols(tls_cfg,
482 1.1 christos TLS_PROTOCOLS_ALL) != 0)
483 1.1 christos errx(1, "%s", tls_config_error(tls_cfg));
484 1.1 christos if (tls_config_set_ciphers(tls_cfg, "all") != 0)
485 1.1 christos errx(1, "%s", tls_config_error(tls_cfg));
486 1.1 christos }
487 1.1 christos if (!lflag && (TLSopt & TLS_CCERT))
488 1.1 christos errx(1, "clientcert is only valid with -l");
489 1.1 christos if (TLSopt & TLS_NONAME)
490 1.1 christos tls_config_insecure_noverifyname(tls_cfg);
491 1.1 christos if (TLSopt & TLS_NOVERIFY) {
492 1.1 christos if (tls_expecthash != NULL)
493 1.1 christos errx(1, "-H and -T noverify may not be used"
494 1.1 christos "together");
495 1.1 christos tls_config_insecure_noverifycert(tls_cfg);
496 1.1 christos }
497 1.1 christos if (TLSopt & TLS_MUSTSTAPLE)
498 1.1 christos tls_config_ocsp_require_stapling(tls_cfg);
499 1.1 christos
500 1.1 christos if (Pflag) {
501 1.1 christos if (pledge("stdio inet dns tty", NULL) == -1)
502 1.1 christos err(1, "pledge");
503 1.1 christos } else if (pledge("stdio inet dns", NULL) == -1)
504 1.1 christos err(1, "pledge");
505 1.1 christos }
506 1.1 christos if (lflag) {
507 1.1 christos struct tls *tls_cctx = NULL;
508 1.1 christos int connfd;
509 1.1 christos ret = 0;
510 1.1 christos
511 1.1 christos if (family == AF_UNIX) {
512 1.1 christos if (uflag)
513 1.1 christos s = unix_bind(host, 0);
514 1.1 christos else
515 1.1 christos s = unix_listen(host);
516 1.1 christos }
517 1.1 christos
518 1.1 christos if (usetls) {
519 1.1 christos tls_config_verify_client_optional(tls_cfg);
520 1.1 christos if ((tls_ctx = tls_server()) == NULL)
521 1.1 christos errx(1, "tls server creation failed");
522 1.1 christos if (tls_configure(tls_ctx, tls_cfg) == -1)
523 1.1 christos errx(1, "tls configuration failed (%s)",
524 1.1 christos tls_error(tls_ctx));
525 1.1 christos }
526 1.1 christos /* Allow only one connection at a time, but stay alive. */
527 1.1 christos for (;;) {
528 1.1 christos if (family != AF_UNIX)
529 1.1 christos s = local_listen(host, uport, hints);
530 1.1 christos if (s < 0)
531 1.1 christos err(1, NULL);
532 1.1 christos /*
533 1.1 christos * For UDP and -k, don't connect the socket, let it
534 1.1 christos * receive datagrams from multiple socket pairs.
535 1.1 christos */
536 1.1 christos if (uflag && kflag)
537 1.1 christos readwrite(s, NULL);
538 1.1 christos /*
539 1.1 christos * For UDP and not -k, we will use recvfrom() initially
540 1.1 christos * to wait for a caller, then use the regular functions
541 1.1 christos * to talk to the caller.
542 1.1 christos */
543 1.1 christos else if (uflag && !kflag) {
544 1.1 christos int rv, plen;
545 1.1 christos char buf[16384];
546 1.1 christos struct sockaddr_storage z;
547 1.1 christos
548 1.1 christos len = sizeof(z);
549 1.1 christos plen = 2048;
550 1.1 christos rv = recvfrom(s, buf, plen, MSG_PEEK,
551 1.1 christos (struct sockaddr *)&z, &len);
552 1.1 christos if (rv < 0)
553 1.1 christos err(1, "recvfrom");
554 1.1 christos
555 1.1 christos rv = connect(s, (struct sockaddr *)&z, len);
556 1.1 christos if (rv < 0)
557 1.1 christos err(1, "connect");
558 1.1 christos
559 1.1 christos if (vflag)
560 1.1 christos report_connect((struct sockaddr *)&z, len, NULL);
561 1.1 christos
562 1.1 christos readwrite(s, NULL);
563 1.1 christos } else {
564 1.1 christos len = sizeof(cliaddr);
565 1.1 christos connfd = accept4(s, (struct sockaddr *)&cliaddr,
566 1.1 christos &len, SOCK_NONBLOCK);
567 1.1 christos if (connfd == -1) {
568 1.1 christos /* For now, all errnos are fatal */
569 1.1 christos err(1, "accept");
570 1.1 christos }
571 1.1 christos if (vflag)
572 1.1 christos report_connect((struct sockaddr *)&cliaddr, len,
573 1.1 christos family == AF_UNIX ? host : NULL);
574 1.1 christos if ((usetls) &&
575 1.1 christos (tls_cctx = tls_setup_server(tls_ctx, connfd, host)))
576 1.1 christos readwrite(connfd, tls_cctx);
577 1.1 christos if (!usetls)
578 1.1 christos readwrite(connfd, NULL);
579 1.1 christos if (tls_cctx) {
580 1.1 christos int i;
581 1.1 christos
582 1.1 christos do {
583 1.1 christos i = tls_close(tls_cctx);
584 1.1 christos } while (i == TLS_WANT_POLLIN ||
585 1.1 christos i == TLS_WANT_POLLOUT);
586 1.1 christos tls_free(tls_cctx);
587 1.1 christos tls_cctx = NULL;
588 1.1 christos }
589 1.1 christos close(connfd);
590 1.1 christos }
591 1.1 christos if (family != AF_UNIX)
592 1.1 christos close(s);
593 1.1 christos else if (uflag) {
594 1.1 christos if (connect(s, NULL, 0) < 0)
595 1.1 christos err(1, "connect");
596 1.1 christos }
597 1.1 christos
598 1.1 christos if (!kflag)
599 1.1 christos break;
600 1.1 christos }
601 1.1 christos } else if (family == AF_UNIX) {
602 1.1 christos ret = 0;
603 1.1 christos
604 1.1 christos if ((s = unix_connect(host)) > 0 && !zflag) {
605 1.1 christos readwrite(s, NULL);
606 1.1 christos close(s);
607 1.1 christos } else
608 1.1 christos ret = 1;
609 1.1 christos
610 1.1 christos if (uflag)
611 1.1 christos unlink(unix_dg_tmp_socket);
612 1.1 christos exit(ret);
613 1.1 christos
614 1.1 christos } else {
615 1.1 christos int i = 0;
616 1.1 christos
617 1.1 christos /* Construct the portlist[] array. */
618 1.1 christos build_ports(uport);
619 1.1 christos
620 1.1 christos /* Cycle through portlist, connecting to each port. */
621 1.1 christos for (s = -1, i = 0; portlist[i] != NULL; i++) {
622 1.1 christos if (s != -1)
623 1.1 christos close(s);
624 1.1 christos
625 1.1 christos if (usetls) {
626 1.1 christos if ((tls_ctx = tls_client()) == NULL)
627 1.1 christos errx(1, "tls client creation failed");
628 1.1 christos if (tls_configure(tls_ctx, tls_cfg) == -1)
629 1.1 christos errx(1, "tls configuration failed (%s)",
630 1.1 christos tls_error(tls_ctx));
631 1.1 christos }
632 1.1 christos if (xflag)
633 1.1 christos s = socks_connect(host, portlist[i], hints,
634 1.1 christos proxy, proxyport, proxyhints, socksv,
635 1.1 christos Pflag);
636 1.1 christos else
637 1.1 christos s = remote_connect(host, portlist[i], hints);
638 1.1 christos
639 1.1 christos if (s == -1)
640 1.1 christos continue;
641 1.1 christos
642 1.1 christos ret = 0;
643 1.1 christos if (vflag || zflag) {
644 1.1 christos /* For UDP, make sure we are connected. */
645 1.1 christos if (uflag) {
646 1.1 christos if (udptest(s) == -1) {
647 1.1 christos ret = 1;
648 1.1 christos continue;
649 1.1 christos }
650 1.1 christos }
651 1.1 christos
652 1.1 christos /* Don't look up port if -n. */
653 1.1 christos if (nflag)
654 1.1 christos sv = NULL;
655 1.1 christos else {
656 1.1 christos sv = getservbyport(
657 1.1 christos ntohs(atoi(portlist[i])),
658 1.1 christos uflag ? "udp" : "tcp");
659 1.1 christos }
660 1.1 christos
661 1.1 christos fprintf(stderr,
662 1.1 christos "Connection to %s %s port [%s/%s] "
663 1.1 christos "succeeded!\n", host, portlist[i],
664 1.1 christos uflag ? "udp" : "tcp",
665 1.1 christos sv ? sv->s_name : "*");
666 1.1 christos }
667 1.1 christos if (Fflag)
668 1.1 christos fdpass(s);
669 1.1 christos else {
670 1.1 christos if (usetls)
671 1.1 christos tls_setup_client(tls_ctx, s, host);
672 1.1 christos if (!zflag)
673 1.1 christos readwrite(s, tls_ctx);
674 1.1 christos if (tls_ctx) {
675 1.1 christos int j;
676 1.1 christos
677 1.1 christos do {
678 1.1 christos j = tls_close(tls_ctx);
679 1.1 christos } while (j == TLS_WANT_POLLIN ||
680 1.1 christos j == TLS_WANT_POLLOUT);
681 1.1 christos tls_free(tls_ctx);
682 1.1 christos tls_ctx = NULL;
683 1.1 christos }
684 1.1 christos }
685 1.1 christos }
686 1.1 christos }
687 1.1 christos
688 1.1 christos if (s != -1)
689 1.1 christos close(s);
690 1.1 christos
691 1.1 christos tls_config_free(tls_cfg);
692 1.1 christos
693 1.1 christos exit(ret);
694 1.1 christos }
695 1.1 christos
696 1.1 christos /*
697 1.1 christos * unix_bind()
698 1.1 christos * Returns a unix socket bound to the given path
699 1.1 christos */
700 1.1 christos int
701 1.1 christos unix_bind(char *path, int flags)
702 1.1 christos {
703 1.1 christos struct sockaddr_un s_un;
704 1.1 christos int s, save_errno;
705 1.1 christos
706 1.1 christos /* Create unix domain socket. */
707 1.1 christos if ((s = socket(AF_UNIX, flags | (uflag ? SOCK_DGRAM : SOCK_STREAM),
708 1.1 christos 0)) < 0)
709 1.1 christos return (-1);
710 1.1 christos
711 1.1 christos memset(&s_un, 0, sizeof(struct sockaddr_un));
712 1.1 christos s_un.sun_family = AF_UNIX;
713 1.1 christos
714 1.1 christos if (strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path)) >=
715 1.1 christos sizeof(s_un.sun_path)) {
716 1.1 christos close(s);
717 1.1 christos errno = ENAMETOOLONG;
718 1.1 christos return (-1);
719 1.1 christos }
720 1.1 christos
721 1.1 christos if (bind(s, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) {
722 1.1 christos save_errno = errno;
723 1.1 christos close(s);
724 1.1 christos errno = save_errno;
725 1.1 christos return (-1);
726 1.1 christos }
727 1.1 christos return (s);
728 1.1 christos }
729 1.1 christos
730 1.1 christos void
731 1.1 christos tls_setup_client(struct tls *tls_ctx, int s, char *host)
732 1.1 christos {
733 1.1 christos int i;
734 1.1 christos
735 1.1 christos if (tls_connect_socket(tls_ctx, s,
736 1.1 christos tls_expectname ? tls_expectname : host) == -1) {
737 1.1 christos errx(1, "tls connection failed (%s)",
738 1.1 christos tls_error(tls_ctx));
739 1.1 christos }
740 1.1 christos do {
741 1.1 christos if ((i = tls_handshake(tls_ctx)) == -1)
742 1.1 christos errx(1, "tls handshake failed (%s)",
743 1.1 christos tls_error(tls_ctx));
744 1.1 christos } while (i == TLS_WANT_POLLIN || i == TLS_WANT_POLLOUT);
745 1.1 christos if (vflag)
746 1.1 christos report_tls(tls_ctx, host, tls_expectname);
747 1.1 christos if (tls_expecthash && tls_peer_cert_hash(tls_ctx) &&
748 1.1 christos strcmp(tls_expecthash, tls_peer_cert_hash(tls_ctx)) != 0)
749 1.1 christos errx(1, "peer certificate is not %s", tls_expecthash);
750 1.1 christos }
751 1.1 christos
752 1.1 christos struct tls *
753 1.1 christos tls_setup_server(struct tls *tls_ctx, int connfd, char *host)
754 1.1 christos {
755 1.1 christos struct tls *tls_cctx;
756 1.1 christos
757 1.1 christos if (tls_accept_socket(tls_ctx, &tls_cctx,
758 1.1 christos connfd) == -1) {
759 1.1 christos warnx("tls accept failed (%s)",
760 1.1 christos tls_error(tls_ctx));
761 1.1 christos tls_cctx = NULL;
762 1.1 christos } else {
763 1.1 christos int i;
764 1.1 christos
765 1.1 christos do {
766 1.1 christos if ((i = tls_handshake(tls_cctx)) == -1)
767 1.1 christos warnx("tls handshake failed (%s)",
768 1.1 christos tls_error(tls_cctx));
769 1.1 christos } while(i == TLS_WANT_POLLIN || i == TLS_WANT_POLLOUT);
770 1.1 christos }
771 1.1 christos if (tls_cctx) {
772 1.1 christos int gotcert = tls_peer_cert_provided(tls_cctx);
773 1.1 christos
774 1.1 christos if (vflag && gotcert)
775 1.1 christos report_tls(tls_cctx, host, tls_expectname);
776 1.1 christos if ((TLSopt & TLS_CCERT) && !gotcert)
777 1.1 christos warnx("No client certificate provided");
778 1.1 christos else if (gotcert && tls_peer_cert_hash(tls_ctx) && tls_expecthash &&
779 1.1 christos strcmp(tls_expecthash, tls_peer_cert_hash(tls_ctx)) != 0)
780 1.1 christos warnx("peer certificate is not %s", tls_expecthash);
781 1.1 christos else if (gotcert && tls_expectname &&
782 1.1 christos (!tls_peer_cert_contains_name(tls_cctx, tls_expectname)))
783 1.1 christos warnx("name (%s) not found in client cert",
784 1.1 christos tls_expectname);
785 1.1 christos else {
786 1.1 christos return tls_cctx;
787 1.1 christos }
788 1.1 christos }
789 1.1 christos return NULL;
790 1.1 christos }
791 1.1 christos
792 1.1 christos /*
793 1.1 christos * unix_connect()
794 1.1 christos * Returns a socket connected to a local unix socket. Returns -1 on failure.
795 1.1 christos */
796 1.1 christos int
797 1.1 christos unix_connect(char *path)
798 1.1 christos {
799 1.1 christos struct sockaddr_un s_un;
800 1.1 christos int s, save_errno;
801 1.1 christos
802 1.1 christos if (uflag) {
803 1.1 christos if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) < 0)
804 1.1 christos return (-1);
805 1.1 christos } else {
806 1.1 christos if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0)
807 1.1 christos return (-1);
808 1.1 christos }
809 1.1 christos
810 1.1 christos memset(&s_un, 0, sizeof(struct sockaddr_un));
811 1.1 christos s_un.sun_family = AF_UNIX;
812 1.1 christos
813 1.1 christos if (strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path)) >=
814 1.1 christos sizeof(s_un.sun_path)) {
815 1.1 christos close(s);
816 1.1 christos errno = ENAMETOOLONG;
817 1.1 christos return (-1);
818 1.1 christos }
819 1.1 christos if (connect(s, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) {
820 1.1 christos save_errno = errno;
821 1.1 christos close(s);
822 1.1 christos errno = save_errno;
823 1.1 christos return (-1);
824 1.1 christos }
825 1.1 christos return (s);
826 1.1 christos
827 1.1 christos }
828 1.1 christos
829 1.1 christos /*
830 1.1 christos * unix_listen()
831 1.1 christos * Create a unix domain socket, and listen on it.
832 1.1 christos */
833 1.1 christos int
834 1.1 christos unix_listen(char *path)
835 1.1 christos {
836 1.1 christos int s;
837 1.1 christos if ((s = unix_bind(path, 0)) < 0)
838 1.1 christos return (-1);
839 1.1 christos
840 1.1 christos if (listen(s, 5) < 0) {
841 1.1 christos close(s);
842 1.1 christos return (-1);
843 1.1 christos }
844 1.1 christos return (s);
845 1.1 christos }
846 1.1 christos
847 1.1 christos /*
848 1.1 christos * remote_connect()
849 1.1 christos * Returns a socket connected to a remote host. Properly binds to a local
850 1.1 christos * port or source address if needed. Returns -1 on failure.
851 1.1 christos */
852 1.1 christos int
853 1.1 christos remote_connect(const char *host, const char *port, struct addrinfo hints)
854 1.1 christos {
855 1.1 christos struct addrinfo *res, *res0;
856 1.1 christos int s = -1, error, on = 1, save_errno;
857 1.1 christos
858 1.1 christos if ((error = getaddrinfo(host, port, &hints, &res0)))
859 1.1 christos errx(1, "getaddrinfo: %s", gai_strerror(error));
860 1.1 christos
861 1.1 christos for (res = res0; res; res = res->ai_next) {
862 1.1 christos if ((s = socket(res->ai_family, res->ai_socktype |
863 1.1 christos SOCK_NONBLOCK, res->ai_protocol)) < 0)
864 1.1 christos continue;
865 1.1 christos
866 1.1 christos /* Bind to a local port or source address if specified. */
867 1.1 christos if (sflag || pflag) {
868 1.1 christos struct addrinfo ahints, *ares;
869 1.1 christos
870 1.1 christos /* try SO_BINDANY, but don't insist */
871 1.1 christos setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
872 1.1 christos memset(&ahints, 0, sizeof(struct addrinfo));
873 1.1 christos ahints.ai_family = res->ai_family;
874 1.1 christos ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
875 1.1 christos ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
876 1.1 christos ahints.ai_flags = AI_PASSIVE;
877 1.1 christos if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
878 1.1 christos errx(1, "getaddrinfo: %s", gai_strerror(error));
879 1.1 christos
880 1.1 christos if (bind(s, (struct sockaddr *)ares->ai_addr,
881 1.1 christos ares->ai_addrlen) < 0)
882 1.1 christos err(1, "bind failed");
883 1.1 christos freeaddrinfo(ares);
884 1.1 christos }
885 1.1 christos
886 1.1 christos set_common_sockopts(s, res->ai_family);
887 1.1 christos
888 1.1 christos if (timeout_connect(s, res->ai_addr, res->ai_addrlen) == 0)
889 1.1 christos break;
890 1.1 christos if (vflag)
891 1.1 christos warn("connect to %s port %s (%s) failed", host, port,
892 1.1 christos uflag ? "udp" : "tcp");
893 1.1 christos
894 1.1 christos save_errno = errno;
895 1.1 christos close(s);
896 1.1 christos errno = save_errno;
897 1.1 christos s = -1;
898 1.1 christos }
899 1.1 christos
900 1.1 christos freeaddrinfo(res0);
901 1.1 christos
902 1.1 christos return (s);
903 1.1 christos }
904 1.1 christos
905 1.1 christos int
906 1.1 christos timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
907 1.1 christos {
908 1.1 christos struct pollfd pfd;
909 1.1 christos socklen_t optlen;
910 1.1 christos int optval;
911 1.1 christos int ret;
912 1.1 christos
913 1.1 christos if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
914 1.1 christos pfd.fd = s;
915 1.1 christos pfd.events = POLLOUT;
916 1.1 christos if ((ret = poll(&pfd, 1, timeout)) == 1) {
917 1.1 christos optlen = sizeof(optval);
918 1.1 christos if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
919 1.1 christos &optval, &optlen)) == 0) {
920 1.1 christos errno = optval;
921 1.1 christos ret = optval == 0 ? 0 : -1;
922 1.1 christos }
923 1.1 christos } else if (ret == 0) {
924 1.1 christos errno = ETIMEDOUT;
925 1.1 christos ret = -1;
926 1.1 christos } else
927 1.1 christos err(1, "poll failed");
928 1.1 christos }
929 1.1 christos
930 1.1 christos return (ret);
931 1.1 christos }
932 1.1 christos
933 1.1 christos /*
934 1.1 christos * local_listen()
935 1.1 christos * Returns a socket listening on a local port, binds to specified source
936 1.1 christos * address. Returns -1 on failure.
937 1.1 christos */
938 1.1 christos int
939 1.1 christos local_listen(char *host, char *port, struct addrinfo hints)
940 1.1 christos {
941 1.1 christos struct addrinfo *res, *res0;
942 1.1 christos int s = -1, ret, x = 1, save_errno;
943 1.1 christos int error;
944 1.1 christos
945 1.1 christos /* Allow nodename to be null. */
946 1.1 christos hints.ai_flags |= AI_PASSIVE;
947 1.1 christos
948 1.1 christos /*
949 1.1 christos * In the case of binding to a wildcard address
950 1.1 christos * default to binding to an ipv4 address.
951 1.1 christos */
952 1.1 christos if (host == NULL && hints.ai_family == AF_UNSPEC)
953 1.1 christos hints.ai_family = AF_INET;
954 1.1 christos
955 1.1 christos if ((error = getaddrinfo(host, port, &hints, &res0)))
956 1.1 christos errx(1, "getaddrinfo: %s", gai_strerror(error));
957 1.1 christos
958 1.1 christos for (res = res0; res; res = res->ai_next) {
959 1.1 christos if ((s = socket(res->ai_family, res->ai_socktype,
960 1.1 christos res->ai_protocol)) < 0)
961 1.1 christos continue;
962 1.1 christos
963 1.1 christos ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
964 1.1 christos if (ret == -1)
965 1.1 christos err(1, NULL);
966 1.1 christos
967 1.1 christos set_common_sockopts(s, res->ai_family);
968 1.1 christos
969 1.1 christos if (bind(s, (struct sockaddr *)res->ai_addr,
970 1.1 christos res->ai_addrlen) == 0)
971 1.1 christos break;
972 1.1 christos
973 1.1 christos save_errno = errno;
974 1.1 christos close(s);
975 1.1 christos errno = save_errno;
976 1.1 christos s = -1;
977 1.1 christos }
978 1.1 christos
979 1.1 christos if (!uflag && s != -1) {
980 1.1 christos if (listen(s, 1) < 0)
981 1.1 christos err(1, "listen");
982 1.1 christos }
983 1.1 christos
984 1.1 christos freeaddrinfo(res0);
985 1.1 christos
986 1.1 christos return (s);
987 1.1 christos }
988 1.1 christos
989 1.1 christos /*
990 1.1 christos * readwrite()
991 1.1 christos * Loop that polls on the network file descriptor and stdin.
992 1.1 christos */
993 1.1 christos void
994 1.1 christos readwrite(int net_fd, struct tls *tls_ctx)
995 1.1 christos {
996 1.1 christos struct pollfd pfd[4];
997 1.1 christos int stdin_fd = STDIN_FILENO;
998 1.1 christos int stdout_fd = STDOUT_FILENO;
999 1.1 christos unsigned char netinbuf[BUFSIZE];
1000 1.1 christos size_t netinbufpos = 0;
1001 1.1 christos unsigned char stdinbuf[BUFSIZE];
1002 1.1 christos size_t stdinbufpos = 0;
1003 1.1 christos int n, num_fds;
1004 1.1 christos ssize_t ret;
1005 1.1 christos
1006 1.1 christos /* don't read from stdin if requested */
1007 1.1 christos if (dflag)
1008 1.1 christos stdin_fd = -1;
1009 1.1 christos
1010 1.1 christos /* stdin */
1011 1.1 christos pfd[POLL_STDIN].fd = stdin_fd;
1012 1.1 christos pfd[POLL_STDIN].events = POLLIN;
1013 1.1 christos
1014 1.1 christos /* network out */
1015 1.1 christos pfd[POLL_NETOUT].fd = net_fd;
1016 1.1 christos pfd[POLL_NETOUT].events = 0;
1017 1.1 christos
1018 1.1 christos /* network in */
1019 1.1 christos pfd[POLL_NETIN].fd = net_fd;
1020 1.1 christos pfd[POLL_NETIN].events = POLLIN;
1021 1.1 christos
1022 1.1 christos /* stdout */
1023 1.1 christos pfd[POLL_STDOUT].fd = stdout_fd;
1024 1.1 christos pfd[POLL_STDOUT].events = 0;
1025 1.1 christos
1026 1.1 christos while (1) {
1027 1.1 christos /* both inputs are gone, buffers are empty, we are done */
1028 1.1 christos if (pfd[POLL_STDIN].fd == -1 && pfd[POLL_NETIN].fd == -1 &&
1029 1.1 christos stdinbufpos == 0 && netinbufpos == 0) {
1030 1.1 christos close(net_fd);
1031 1.1 christos return;
1032 1.1 christos }
1033 1.1 christos /* both outputs are gone, we can't continue */
1034 1.1 christos if (pfd[POLL_NETOUT].fd == -1 && pfd[POLL_STDOUT].fd == -1) {
1035 1.1 christos close(net_fd);
1036 1.1 christos return;
1037 1.1 christos }
1038 1.1 christos /* listen and net in gone, queues empty, done */
1039 1.1 christos if (lflag && pfd[POLL_NETIN].fd == -1 &&
1040 1.1 christos stdinbufpos == 0 && netinbufpos == 0) {
1041 1.1 christos close(net_fd);
1042 1.1 christos return;
1043 1.1 christos }
1044 1.1 christos
1045 1.1 christos /* help says -i is for "wait between lines sent". We read and
1046 1.1 christos * write arbitrary amounts of data, and we don't want to start
1047 1.1 christos * scanning for newlines, so this is as good as it gets */
1048 1.1 christos if (iflag)
1049 1.1 christos sleep(iflag);
1050 1.1 christos
1051 1.1 christos /* poll */
1052 1.1 christos num_fds = poll(pfd, 4, timeout);
1053 1.1 christos
1054 1.1 christos /* treat poll errors */
1055 1.1 christos if (num_fds == -1) {
1056 1.1 christos close(net_fd);
1057 1.1 christos err(1, "polling error");
1058 1.1 christos }
1059 1.1 christos
1060 1.1 christos /* timeout happened */
1061 1.1 christos if (num_fds == 0)
1062 1.1 christos return;
1063 1.1 christos
1064 1.1 christos /* treat socket error conditions */
1065 1.1 christos for (n = 0; n < 4; n++) {
1066 1.1 christos if (pfd[n].revents & (POLLERR|POLLNVAL)) {
1067 1.1 christos pfd[n].fd = -1;
1068 1.1 christos }
1069 1.1 christos }
1070 1.1 christos /* reading is possible after HUP */
1071 1.1 christos if (pfd[POLL_STDIN].events & POLLIN &&
1072 1.1 christos pfd[POLL_STDIN].revents & POLLHUP &&
1073 1.1 christos !(pfd[POLL_STDIN].revents & POLLIN))
1074 1.1 christos pfd[POLL_STDIN].fd = -1;
1075 1.1 christos
1076 1.1 christos if (pfd[POLL_NETIN].events & POLLIN &&
1077 1.1 christos pfd[POLL_NETIN].revents & POLLHUP &&
1078 1.1 christos !(pfd[POLL_NETIN].revents & POLLIN))
1079 1.1 christos pfd[POLL_NETIN].fd = -1;
1080 1.1 christos
1081 1.1 christos if (pfd[POLL_NETOUT].revents & POLLHUP) {
1082 1.1 christos if (Nflag)
1083 1.1 christos shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
1084 1.1 christos pfd[POLL_NETOUT].fd = -1;
1085 1.1 christos }
1086 1.1 christos /* if HUP, stop watching stdout */
1087 1.1 christos if (pfd[POLL_STDOUT].revents & POLLHUP)
1088 1.1 christos pfd[POLL_STDOUT].fd = -1;
1089 1.1 christos /* if no net out, stop watching stdin */
1090 1.1 christos if (pfd[POLL_NETOUT].fd == -1)
1091 1.1 christos pfd[POLL_STDIN].fd = -1;
1092 1.1 christos /* if no stdout, stop watching net in */
1093 1.1 christos if (pfd[POLL_STDOUT].fd == -1) {
1094 1.1 christos if (pfd[POLL_NETIN].fd != -1)
1095 1.1 christos shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
1096 1.1 christos pfd[POLL_NETIN].fd = -1;
1097 1.1 christos }
1098 1.1 christos
1099 1.1 christos /* try to read from stdin */
1100 1.1 christos if (pfd[POLL_STDIN].revents & POLLIN && stdinbufpos < BUFSIZE) {
1101 1.1 christos ret = fillbuf(pfd[POLL_STDIN].fd, stdinbuf,
1102 1.1 christos &stdinbufpos, NULL);
1103 1.1 christos if (ret == TLS_WANT_POLLIN)
1104 1.1 christos pfd[POLL_STDIN].events = POLLIN;
1105 1.1 christos else if (ret == TLS_WANT_POLLOUT)
1106 1.1 christos pfd[POLL_STDIN].events = POLLOUT;
1107 1.1 christos else if (ret == 0 || ret == -1)
1108 1.1 christos pfd[POLL_STDIN].fd = -1;
1109 1.1 christos /* read something - poll net out */
1110 1.1 christos if (stdinbufpos > 0)
1111 1.1 christos pfd[POLL_NETOUT].events = POLLOUT;
1112 1.1 christos /* filled buffer - remove self from polling */
1113 1.1 christos if (stdinbufpos == BUFSIZE)
1114 1.1 christos pfd[POLL_STDIN].events = 0;
1115 1.1 christos }
1116 1.1 christos /* try to write to network */
1117 1.1 christos if (pfd[POLL_NETOUT].revents & POLLOUT && stdinbufpos > 0) {
1118 1.1 christos ret = drainbuf(pfd[POLL_NETOUT].fd, stdinbuf,
1119 1.1 christos &stdinbufpos, tls_ctx);
1120 1.1 christos if (ret == TLS_WANT_POLLIN)
1121 1.1 christos pfd[POLL_NETOUT].events = POLLIN;
1122 1.1 christos else if (ret == TLS_WANT_POLLOUT)
1123 1.1 christos pfd[POLL_NETOUT].events = POLLOUT;
1124 1.1 christos else if (ret == -1)
1125 1.1 christos pfd[POLL_NETOUT].fd = -1;
1126 1.1 christos /* buffer empty - remove self from polling */
1127 1.1 christos if (stdinbufpos == 0)
1128 1.1 christos pfd[POLL_NETOUT].events = 0;
1129 1.1 christos /* buffer no longer full - poll stdin again */
1130 1.1 christos if (stdinbufpos < BUFSIZE)
1131 1.1 christos pfd[POLL_STDIN].events = POLLIN;
1132 1.1 christos }
1133 1.1 christos /* try to read from network */
1134 1.1 christos if (pfd[POLL_NETIN].revents & POLLIN && netinbufpos < BUFSIZE) {
1135 1.1 christos ret = fillbuf(pfd[POLL_NETIN].fd, netinbuf,
1136 1.1 christos &netinbufpos, tls_ctx);
1137 1.1 christos if (ret == TLS_WANT_POLLIN)
1138 1.1 christos pfd[POLL_NETIN].events = POLLIN;
1139 1.1 christos else if (ret == TLS_WANT_POLLOUT)
1140 1.1 christos pfd[POLL_NETIN].events = POLLOUT;
1141 1.1 christos else if (ret == -1)
1142 1.1 christos pfd[POLL_NETIN].fd = -1;
1143 1.1 christos /* eof on net in - remove from pfd */
1144 1.1 christos if (ret == 0) {
1145 1.1 christos shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
1146 1.1 christos pfd[POLL_NETIN].fd = -1;
1147 1.1 christos }
1148 1.1 christos /* read something - poll stdout */
1149 1.1 christos if (netinbufpos > 0)
1150 1.1 christos pfd[POLL_STDOUT].events = POLLOUT;
1151 1.1 christos /* filled buffer - remove self from polling */
1152 1.1 christos if (netinbufpos == BUFSIZE)
1153 1.1 christos pfd[POLL_NETIN].events = 0;
1154 1.1 christos /* handle telnet */
1155 1.1 christos if (tflag)
1156 1.1 christos atelnet(pfd[POLL_NETIN].fd, netinbuf,
1157 1.1 christos netinbufpos);
1158 1.1 christos }
1159 1.1 christos /* try to write to stdout */
1160 1.1 christos if (pfd[POLL_STDOUT].revents & POLLOUT && netinbufpos > 0) {
1161 1.1 christos ret = drainbuf(pfd[POLL_STDOUT].fd, netinbuf,
1162 1.1 christos &netinbufpos, NULL);
1163 1.1 christos if (ret == TLS_WANT_POLLIN)
1164 1.1 christos pfd[POLL_STDOUT].events = POLLIN;
1165 1.1 christos else if (ret == TLS_WANT_POLLOUT)
1166 1.1 christos pfd[POLL_STDOUT].events = POLLOUT;
1167 1.1 christos else if (ret == -1)
1168 1.1 christos pfd[POLL_STDOUT].fd = -1;
1169 1.1 christos /* buffer empty - remove self from polling */
1170 1.1 christos if (netinbufpos == 0)
1171 1.1 christos pfd[POLL_STDOUT].events = 0;
1172 1.1 christos /* buffer no longer full - poll net in again */
1173 1.1 christos if (netinbufpos < BUFSIZE)
1174 1.1 christos pfd[POLL_NETIN].events = POLLIN;
1175 1.1 christos }
1176 1.1 christos
1177 1.1 christos /* stdin gone and queue empty? */
1178 1.1 christos if (pfd[POLL_STDIN].fd == -1 && stdinbufpos == 0) {
1179 1.1 christos if (pfd[POLL_NETOUT].fd != -1 && Nflag)
1180 1.1 christos shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
1181 1.1 christos pfd[POLL_NETOUT].fd = -1;
1182 1.1 christos }
1183 1.1 christos /* net in gone and queue empty? */
1184 1.1 christos if (pfd[POLL_NETIN].fd == -1 && netinbufpos == 0) {
1185 1.1 christos pfd[POLL_STDOUT].fd = -1;
1186 1.1 christos }
1187 1.1 christos }
1188 1.1 christos }
1189 1.1 christos
1190 1.1 christos ssize_t
1191 1.1 christos drainbuf(int fd, unsigned char *buf, size_t *bufpos, struct tls *tls)
1192 1.1 christos {
1193 1.1 christos ssize_t n;
1194 1.1 christos ssize_t adjust;
1195 1.1 christos
1196 1.1 christos if (tls)
1197 1.1 christos n = tls_write(tls, buf, *bufpos);
1198 1.1 christos else {
1199 1.1 christos n = write(fd, buf, *bufpos);
1200 1.1 christos /* don't treat EAGAIN, EINTR as error */
1201 1.1 christos if (n == -1 && (errno == EAGAIN || errno == EINTR))
1202 1.1 christos n = TLS_WANT_POLLOUT;
1203 1.1 christos }
1204 1.1 christos if (n <= 0)
1205 1.1 christos return n;
1206 1.1 christos /* adjust buffer */
1207 1.1 christos adjust = *bufpos - n;
1208 1.1 christos if (adjust > 0)
1209 1.1 christos memmove(buf, buf + n, adjust);
1210 1.1 christos *bufpos -= n;
1211 1.1 christos return n;
1212 1.1 christos }
1213 1.1 christos
1214 1.1 christos ssize_t
1215 1.1 christos fillbuf(int fd, unsigned char *buf, size_t *bufpos, struct tls *tls)
1216 1.1 christos {
1217 1.1 christos size_t num = BUFSIZE - *bufpos;
1218 1.1 christos ssize_t n;
1219 1.1 christos
1220 1.1 christos if (tls)
1221 1.1 christos n = tls_read(tls, buf + *bufpos, num);
1222 1.1 christos else {
1223 1.1 christos n = read(fd, buf + *bufpos, num);
1224 1.1 christos /* don't treat EAGAIN, EINTR as error */
1225 1.1 christos if (n == -1 && (errno == EAGAIN || errno == EINTR))
1226 1.1 christos n = TLS_WANT_POLLIN;
1227 1.1 christos }
1228 1.1 christos if (n <= 0)
1229 1.1 christos return n;
1230 1.1 christos *bufpos += n;
1231 1.1 christos return n;
1232 1.1 christos }
1233 1.1 christos
1234 1.1 christos /*
1235 1.1 christos * fdpass()
1236 1.1 christos * Pass the connected file descriptor to stdout and exit.
1237 1.1 christos */
1238 1.1 christos void
1239 1.1 christos fdpass(int nfd)
1240 1.1 christos {
1241 1.1 christos struct msghdr mh;
1242 1.1 christos union {
1243 1.1 christos struct cmsghdr hdr;
1244 1.1 christos char buf[CMSG_SPACE(sizeof(int))];
1245 1.1 christos } cmsgbuf;
1246 1.1 christos struct cmsghdr *cmsg;
1247 1.1 christos struct iovec iov;
1248 1.1 christos char c = '\0';
1249 1.1 christos ssize_t r;
1250 1.1 christos struct pollfd pfd;
1251 1.1 christos
1252 1.1 christos /* Avoid obvious stupidity */
1253 1.1 christos if (isatty(STDOUT_FILENO))
1254 1.1 christos errx(1, "Cannot pass file descriptor to tty");
1255 1.1 christos
1256 1.1 christos bzero(&mh, sizeof(mh));
1257 1.1 christos bzero(&cmsgbuf, sizeof(cmsgbuf));
1258 1.1 christos bzero(&iov, sizeof(iov));
1259 1.1 christos
1260 1.1 christos mh.msg_control = (caddr_t)&cmsgbuf.buf;
1261 1.1 christos mh.msg_controllen = sizeof(cmsgbuf.buf);
1262 1.1 christos cmsg = CMSG_FIRSTHDR(&mh);
1263 1.1 christos cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1264 1.1 christos cmsg->cmsg_level = SOL_SOCKET;
1265 1.1 christos cmsg->cmsg_type = SCM_RIGHTS;
1266 1.1 christos *(int *)CMSG_DATA(cmsg) = nfd;
1267 1.1 christos
1268 1.1 christos iov.iov_base = &c;
1269 1.1 christos iov.iov_len = 1;
1270 1.1 christos mh.msg_iov = &iov;
1271 1.1 christos mh.msg_iovlen = 1;
1272 1.1 christos
1273 1.1 christos bzero(&pfd, sizeof(pfd));
1274 1.1 christos pfd.fd = STDOUT_FILENO;
1275 1.1 christos pfd.events = POLLOUT;
1276 1.1 christos for (;;) {
1277 1.1 christos r = sendmsg(STDOUT_FILENO, &mh, 0);
1278 1.1 christos if (r == -1) {
1279 1.1 christos if (errno == EAGAIN || errno == EINTR) {
1280 1.1 christos if (poll(&pfd, 1, -1) == -1)
1281 1.1 christos err(1, "poll");
1282 1.1 christos continue;
1283 1.1 christos }
1284 1.1 christos err(1, "sendmsg");
1285 1.1 christos } else if (r != 1)
1286 1.1 christos errx(1, "sendmsg: unexpected return value %zd", r);
1287 1.1 christos else
1288 1.1 christos break;
1289 1.1 christos }
1290 1.1 christos exit(0);
1291 1.1 christos }
1292 1.1 christos
1293 1.1 christos /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1294 1.1 christos void
1295 1.1 christos atelnet(int nfd, unsigned char *buf, unsigned int size)
1296 1.1 christos {
1297 1.1 christos unsigned char *p, *end;
1298 1.1 christos unsigned char obuf[4];
1299 1.1 christos
1300 1.1 christos if (size < 3)
1301 1.1 christos return;
1302 1.1 christos end = buf + size - 2;
1303 1.1 christos
1304 1.1 christos for (p = buf; p < end; p++) {
1305 1.1 christos if (*p != IAC)
1306 1.1 christos continue;
1307 1.1 christos
1308 1.1 christos obuf[0] = IAC;
1309 1.1 christos p++;
1310 1.1 christos if ((*p == WILL) || (*p == WONT))
1311 1.1 christos obuf[1] = DONT;
1312 1.1 christos else if ((*p == DO) || (*p == DONT))
1313 1.1 christos obuf[1] = WONT;
1314 1.1 christos else
1315 1.1 christos continue;
1316 1.1 christos
1317 1.1 christos p++;
1318 1.1 christos obuf[2] = *p;
1319 1.1 christos if (atomicio(vwrite, nfd, obuf, 3) != 3)
1320 1.1 christos warn("Write Error!");
1321 1.1 christos }
1322 1.1 christos }
1323 1.1 christos
1324 1.1 christos
1325 1.1 christos int
1326 1.1 christos strtoport(char *portstr, int udp)
1327 1.1 christos {
1328 1.1 christos struct servent *entry;
1329 1.1 christos const char *errstr;
1330 1.1 christos char *proto;
1331 1.1 christos int port = -1;
1332 1.1 christos
1333 1.1 christos proto = udp ? "udp" : "tcp";
1334 1.1 christos
1335 1.1 christos port = strtonum(portstr, 1, PORT_MAX, &errstr);
1336 1.1 christos if (errstr == NULL)
1337 1.1 christos return port;
1338 1.1 christos if (errno != EINVAL)
1339 1.1 christos errx(1, "port number %s: %s", errstr, portstr);
1340 1.1 christos if ((entry = getservbyname(portstr, proto)) == NULL)
1341 1.1 christos errx(1, "service \"%s\" unknown", portstr);
1342 1.1 christos return ntohs(entry->s_port);
1343 1.1 christos }
1344 1.1 christos
1345 1.1 christos /*
1346 1.1 christos * build_ports()
1347 1.1 christos * Build an array of ports in portlist[], listing each port
1348 1.1 christos * that we should try to connect to.
1349 1.1 christos */
1350 1.1 christos void
1351 1.1 christos build_ports(char *p)
1352 1.1 christos {
1353 1.1 christos char *n;
1354 1.1 christos int hi, lo, cp;
1355 1.1 christos int x = 0;
1356 1.1 christos
1357 1.1 christos if ((n = strchr(p, '-')) != NULL) {
1358 1.1 christos *n = '\0';
1359 1.1 christos n++;
1360 1.1 christos
1361 1.1 christos /* Make sure the ports are in order: lowest->highest. */
1362 1.1 christos hi = strtoport(n, uflag);
1363 1.1 christos lo = strtoport(p, uflag);
1364 1.1 christos if (lo > hi) {
1365 1.1 christos cp = hi;
1366 1.1 christos hi = lo;
1367 1.1 christos lo = cp;
1368 1.1 christos }
1369 1.1 christos
1370 1.1 christos /*
1371 1.1 christos * Initialize portlist with a random permutation. Based on
1372 1.1 christos * Knuth, as in ip_randomid() in sys/netinet/ip_id.c.
1373 1.1 christos */
1374 1.1 christos if (rflag) {
1375 1.1 christos for (x = 0; x <= hi - lo; x++) {
1376 1.1 christos cp = arc4random_uniform(x + 1);
1377 1.1 christos portlist[x] = portlist[cp];
1378 1.1 christos if (asprintf(&portlist[cp], "%d", x + lo) < 0)
1379 1.1 christos err(1, "asprintf");
1380 1.1 christos }
1381 1.1 christos } else { /* Load ports sequentially. */
1382 1.1 christos for (cp = lo; cp <= hi; cp++) {
1383 1.1 christos if (asprintf(&portlist[x], "%d", cp) < 0)
1384 1.1 christos err(1, "asprintf");
1385 1.1 christos x++;
1386 1.1 christos }
1387 1.1 christos }
1388 1.1 christos } else {
1389 1.1 christos char *tmp;
1390 1.1 christos
1391 1.1 christos hi = strtoport(p, uflag);
1392 1.1 christos if (asprintf(&tmp, "%d", hi) != -1)
1393 1.1 christos portlist[0] = tmp;
1394 1.1 christos else
1395 1.1 christos err(1, NULL);
1396 1.1 christos }
1397 1.1 christos }
1398 1.1 christos
1399 1.1 christos /*
1400 1.1 christos * udptest()
1401 1.1 christos * Do a few writes to see if the UDP port is there.
1402 1.1 christos * Fails once PF state table is full.
1403 1.1 christos */
1404 1.1 christos int
1405 1.1 christos udptest(int s)
1406 1.1 christos {
1407 1.1 christos int i, ret;
1408 1.1 christos
1409 1.1 christos for (i = 0; i <= 3; i++) {
1410 1.1 christos if (write(s, "X", 1) == 1)
1411 1.1 christos ret = 1;
1412 1.1 christos else
1413 1.1 christos ret = -1;
1414 1.1 christos }
1415 1.1 christos return (ret);
1416 1.1 christos }
1417 1.1 christos
1418 1.1 christos void
1419 1.1 christos set_common_sockopts(int s, int af)
1420 1.1 christos {
1421 1.1 christos int x = 1;
1422 1.1 christos
1423 1.1 christos if (Sflag) {
1424 1.1 christos if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
1425 1.1 christos &x, sizeof(x)) == -1)
1426 1.1 christos err(1, NULL);
1427 1.1 christos }
1428 1.1 christos if (Dflag) {
1429 1.1 christos if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
1430 1.1 christos &x, sizeof(x)) == -1)
1431 1.1 christos err(1, NULL);
1432 1.1 christos }
1433 1.1 christos if (Tflag != -1) {
1434 1.1 christos if (af == AF_INET && setsockopt(s, IPPROTO_IP,
1435 1.1 christos IP_TOS, &Tflag, sizeof(Tflag)) == -1)
1436 1.1 christos err(1, "set IP ToS");
1437 1.1 christos
1438 1.1 christos else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
1439 1.1 christos IPV6_TCLASS, &Tflag, sizeof(Tflag)) == -1)
1440 1.1 christos err(1, "set IPv6 traffic class");
1441 1.1 christos }
1442 1.1 christos if (Iflag) {
1443 1.1 christos if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
1444 1.1 christos &Iflag, sizeof(Iflag)) == -1)
1445 1.1 christos err(1, "set TCP receive buffer size");
1446 1.1 christos }
1447 1.1 christos if (Oflag) {
1448 1.1 christos if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
1449 1.1 christos &Oflag, sizeof(Oflag)) == -1)
1450 1.1 christos err(1, "set TCP send buffer size");
1451 1.1 christos }
1452 1.1 christos
1453 1.1 christos if (ttl != -1) {
1454 1.1 christos if (af == AF_INET && setsockopt(s, IPPROTO_IP,
1455 1.1 christos IP_TTL, &ttl, sizeof(ttl)))
1456 1.1 christos err(1, "set IP TTL");
1457 1.1 christos
1458 1.1 christos else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
1459 1.1 christos IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)))
1460 1.1 christos err(1, "set IPv6 unicast hops");
1461 1.1 christos }
1462 1.1 christos
1463 1.1 christos if (minttl != -1) {
1464 1.1 christos if (af == AF_INET && setsockopt(s, IPPROTO_IP,
1465 1.1 christos IP_MINTTL, &minttl, sizeof(minttl)))
1466 1.1 christos err(1, "set IP min TTL");
1467 1.1 christos
1468 1.1 christos else if (af == AF_INET6 && setsockopt(s, IPPROTO_IPV6,
1469 1.1 christos IPV6_MINHOPCOUNT, &minttl, sizeof(minttl)))
1470 1.1 christos err(1, "set IPv6 min hop count");
1471 1.1 christos }
1472 1.1 christos }
1473 1.1 christos
1474 1.1 christos int
1475 1.1 christos map_tos(char *s, int *val)
1476 1.1 christos {
1477 1.1 christos /* DiffServ Codepoints and other TOS mappings */
1478 1.1 christos const struct toskeywords {
1479 1.1 christos const char *keyword;
1480 1.1 christos int val;
1481 1.1 christos } *t, toskeywords[] = {
1482 1.1 christos { "af11", IPTOS_DSCP_AF11 },
1483 1.1 christos { "af12", IPTOS_DSCP_AF12 },
1484 1.1 christos { "af13", IPTOS_DSCP_AF13 },
1485 1.1 christos { "af21", IPTOS_DSCP_AF21 },
1486 1.1 christos { "af22", IPTOS_DSCP_AF22 },
1487 1.1 christos { "af23", IPTOS_DSCP_AF23 },
1488 1.1 christos { "af31", IPTOS_DSCP_AF31 },
1489 1.1 christos { "af32", IPTOS_DSCP_AF32 },
1490 1.1 christos { "af33", IPTOS_DSCP_AF33 },
1491 1.1 christos { "af41", IPTOS_DSCP_AF41 },
1492 1.1 christos { "af42", IPTOS_DSCP_AF42 },
1493 1.1 christos { "af43", IPTOS_DSCP_AF43 },
1494 1.1 christos { "critical", IPTOS_PREC_CRITIC_ECP },
1495 1.1 christos { "cs0", IPTOS_DSCP_CS0 },
1496 1.1 christos { "cs1", IPTOS_DSCP_CS1 },
1497 1.1 christos { "cs2", IPTOS_DSCP_CS2 },
1498 1.1 christos { "cs3", IPTOS_DSCP_CS3 },
1499 1.1 christos { "cs4", IPTOS_DSCP_CS4 },
1500 1.1 christos { "cs5", IPTOS_DSCP_CS5 },
1501 1.1 christos { "cs6", IPTOS_DSCP_CS6 },
1502 1.1 christos { "cs7", IPTOS_DSCP_CS7 },
1503 1.1 christos { "ef", IPTOS_DSCP_EF },
1504 1.1 christos { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
1505 1.1 christos { "lowdelay", IPTOS_LOWDELAY },
1506 1.1 christos { "netcontrol", IPTOS_PREC_NETCONTROL },
1507 1.1 christos { "reliability", IPTOS_RELIABILITY },
1508 1.1 christos { "throughput", IPTOS_THROUGHPUT },
1509 1.1 christos { NULL, -1 },
1510 1.1 christos };
1511 1.1 christos
1512 1.1 christos for (t = toskeywords; t->keyword != NULL; t++) {
1513 1.1 christos if (strcmp(s, t->keyword) == 0) {
1514 1.1 christos *val = t->val;
1515 1.1 christos return (1);
1516 1.1 christos }
1517 1.1 christos }
1518 1.1 christos
1519 1.1 christos return (0);
1520 1.1 christos }
1521 1.1 christos
1522 1.1 christos int
1523 1.1 christos map_tls(char *s, int *val)
1524 1.1 christos {
1525 1.1 christos const struct tlskeywords {
1526 1.1 christos const char *keyword;
1527 1.1 christos int val;
1528 1.1 christos } *t, tlskeywords[] = {
1529 1.1 christos { "tlsall", TLS_ALL },
1530 1.1 christos { "noverify", TLS_NOVERIFY },
1531 1.1 christos { "noname", TLS_NONAME },
1532 1.1 christos { "clientcert", TLS_CCERT},
1533 1.1 christos { "muststaple", TLS_MUSTSTAPLE},
1534 1.1 christos { NULL, -1 },
1535 1.1 christos };
1536 1.1 christos
1537 1.1 christos for (t = tlskeywords; t->keyword != NULL; t++) {
1538 1.1 christos if (strcmp(s, t->keyword) == 0) {
1539 1.1 christos *val |= t->val;
1540 1.1 christos return (1);
1541 1.1 christos }
1542 1.1 christos }
1543 1.1 christos return (0);
1544 1.1 christos }
1545 1.1 christos
1546 1.1 christos void
1547 1.1 christos report_tls(struct tls * tls_ctx, char * host, char *tls_expectname)
1548 1.1 christos {
1549 1.1 christos time_t t;
1550 1.1 christos const char *ocsp_url;
1551 1.1 christos
1552 1.1 christos fprintf(stderr, "TLS handshake negotiated %s/%s with host %s\n",
1553 1.1 christos tls_conn_version(tls_ctx), tls_conn_cipher(tls_ctx), host);
1554 1.1 christos fprintf(stderr, "Peer name: %s\n",
1555 1.1 christos tls_expectname ? tls_expectname : host);
1556 1.1 christos if (tls_peer_cert_subject(tls_ctx))
1557 1.1 christos fprintf(stderr, "Subject: %s\n",
1558 1.1 christos tls_peer_cert_subject(tls_ctx));
1559 1.1 christos if (tls_peer_cert_issuer(tls_ctx))
1560 1.1 christos fprintf(stderr, "Issuer: %s\n",
1561 1.1 christos tls_peer_cert_issuer(tls_ctx));
1562 1.1 christos if ((t = tls_peer_cert_notbefore(tls_ctx)) != -1)
1563 1.1 christos fprintf(stderr, "Valid From: %s", ctime(&t));
1564 1.1 christos if ((t = tls_peer_cert_notafter(tls_ctx)) != -1)
1565 1.1 christos fprintf(stderr, "Valid Until: %s", ctime(&t));
1566 1.1 christos if (tls_peer_cert_hash(tls_ctx))
1567 1.1 christos fprintf(stderr, "Cert Hash: %s\n",
1568 1.1 christos tls_peer_cert_hash(tls_ctx));
1569 1.1 christos ocsp_url = tls_peer_ocsp_url(tls_ctx);
1570 1.1 christos if (ocsp_url != NULL)
1571 1.1 christos fprintf(stderr, "OCSP URL: %s\n", ocsp_url);
1572 1.1 christos switch (tls_peer_ocsp_response_status(tls_ctx)) {
1573 1.1 christos case TLS_OCSP_RESPONSE_SUCCESSFUL:
1574 1.1 christos fprintf(stderr, "OCSP Stapling: %s\n",
1575 1.1 christos tls_peer_ocsp_result(tls_ctx) == NULL ? "" :
1576 1.1 christos tls_peer_ocsp_result(tls_ctx));
1577 1.1 christos fprintf(stderr,
1578 1.1 christos " response_status=%d cert_status=%d crl_reason=%d\n",
1579 1.1 christos tls_peer_ocsp_response_status(tls_ctx),
1580 1.1 christos tls_peer_ocsp_cert_status(tls_ctx),
1581 1.1 christos tls_peer_ocsp_crl_reason(tls_ctx));
1582 1.1 christos t = tls_peer_ocsp_this_update(tls_ctx);
1583 1.1 christos fprintf(stderr, " this update: %s",
1584 1.1 christos t != -1 ? ctime(&t) : "\n");
1585 1.1 christos t = tls_peer_ocsp_next_update(tls_ctx);
1586 1.1 christos fprintf(stderr, " next update: %s",
1587 1.1 christos t != -1 ? ctime(&t) : "\n");
1588 1.1 christos t = tls_peer_ocsp_revocation_time(tls_ctx);
1589 1.1 christos fprintf(stderr, " revocation: %s",
1590 1.1 christos t != -1 ? ctime(&t) : "\n");
1591 1.1 christos break;
1592 1.1 christos case -1:
1593 1.1 christos break;
1594 1.1 christos default:
1595 1.1 christos fprintf(stderr, "OCSP Stapling: failure - response_status %d (%s)\n",
1596 1.1 christos tls_peer_ocsp_response_status(tls_ctx),
1597 1.1 christos tls_peer_ocsp_result(tls_ctx) == NULL ? "" :
1598 1.1 christos tls_peer_ocsp_result(tls_ctx));
1599 1.1 christos break;
1600 1.1 christos
1601 1.1 christos }
1602 1.1 christos }
1603 1.1 christos
1604 1.1 christos void
1605 1.1 christos report_connect(const struct sockaddr *sa, socklen_t salen, char *path)
1606 1.1 christos {
1607 1.1 christos char remote_host[NI_MAXHOST];
1608 1.1 christos char remote_port[NI_MAXSERV];
1609 1.1 christos int herr;
1610 1.1 christos int flags = NI_NUMERICSERV;
1611 1.1 christos
1612 1.1 christos if (path != NULL) {
1613 1.1 christos fprintf(stderr, "Connection on %s received!\n", path);
1614 1.1 christos return;
1615 1.1 christos }
1616 1.1 christos
1617 1.1 christos if (nflag)
1618 1.1 christos flags |= NI_NUMERICHOST;
1619 1.1 christos
1620 1.1 christos if ((herr = getnameinfo(sa, salen,
1621 1.1 christos remote_host, sizeof(remote_host),
1622 1.1 christos remote_port, sizeof(remote_port),
1623 1.1 christos flags)) != 0) {
1624 1.1 christos if (herr == EAI_SYSTEM)
1625 1.1 christos err(1, "getnameinfo");
1626 1.1 christos else
1627 1.1 christos errx(1, "getnameinfo: %s", gai_strerror(herr));
1628 1.1 christos }
1629 1.1 christos
1630 1.1 christos fprintf(stderr,
1631 1.1 christos "Connection from %s %s "
1632 1.1 christos "received!\n", remote_host, remote_port);
1633 1.1 christos }
1634 1.1 christos
1635 1.1 christos void
1636 1.1 christos help(void)
1637 1.1 christos {
1638 1.1 christos usage(0);
1639 1.1 christos fprintf(stderr, "\tCommand Summary:\n\
1640 1.1 christos \t-4 Use IPv4\n\
1641 1.1 christos \t-6 Use IPv6\n\
1642 1.1 christos \t-C certfile Public key file\n\
1643 1.1 christos \t-c Use TLS\n\
1644 1.1 christos \t-D Enable the debug socket option\n\
1645 1.1 christos \t-d Detach from stdin\n\
1646 1.1 christos \t-e name\t Required name in peer certificate\n\
1647 1.1 christos \t-F Pass socket fd\n\
1648 1.1 christos \t-H hash\t Hash string of peer certificate\n\
1649 1.1 christos \t-h This help text\n\
1650 1.1 christos \t-I length TCP receive buffer length\n\
1651 1.1 christos \t-i interval Delay interval for lines sent, ports scanned\n\
1652 1.1 christos \t-K keyfile Private key file\n\
1653 1.1 christos \t-k Keep inbound sockets open for multiple connects\n\
1654 1.1 christos \t-l Listen mode, for inbound connects\n\
1655 1.1 christos \t-M ttl Outgoing TTL / Hop Limit\n\
1656 1.1 christos \t-m minttl Minimum incoming TTL / Hop Limit\n\
1657 1.1 christos \t-N Shutdown the network socket after EOF on stdin\n\
1658 1.1 christos \t-n Suppress name/port resolutions\n\
1659 1.1 christos \t-O length TCP send buffer length\n\
1660 1.1 christos \t-o staplefile Staple file\n\
1661 1.1 christos \t-P proxyuser\tUsername for proxy authentication\n\
1662 1.1 christos \t-p port\t Specify local port for remote connects\n\
1663 1.1 christos \t-R CAfile CA bundle\n\
1664 1.1 christos \t-r Randomize remote ports\n\
1665 1.1 christos \t-S Enable the TCP MD5 signature option\n\
1666 1.1 christos \t-s source Local source address\n\
1667 1.1 christos \t-T keyword TOS value or TLS options\n\
1668 1.1 christos \t-t Answer TELNET negotiation\n\
1669 1.1 christos \t-U Use UNIX domain socket\n\
1670 1.1 christos \t-u UDP mode\n\
1671 1.1 christos \t-V rtable Specify alternate routing table\n\
1672 1.1 christos \t-v Verbose\n\
1673 1.1 christos \t-w timeout Timeout for connects and final net reads\n\
1674 1.1 christos \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1675 1.1 christos \t-x addr[:port]\tSpecify proxy address and port\n\
1676 1.1 christos \t-z Zero-I/O mode [used for scanning]\n\
1677 1.1 christos Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1678 1.1 christos exit(1);
1679 1.1 christos }
1680 1.1 christos
1681 1.1 christos void
1682 1.1 christos usage(int ret)
1683 1.1 christos {
1684 1.1 christos fprintf(stderr,
1685 1.1 christos "usage: nc [-46cDdFhklNnrStUuvz] [-C certfile] [-e name] "
1686 1.1 christos "[-H hash] [-I length]\n"
1687 1.1 christos "\t [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]\n"
1688 1.1 christos "\t [-o staplefile] [-P proxy_username] [-p source_port] "
1689 1.1 christos "[-R CAfile]\n"
1690 1.1 christos "\t [-s source] [-T keyword] [-V rtable] [-w timeout] "
1691 1.1 christos "[-X proxy_protocol]\n"
1692 1.1 christos "\t [-x proxy_address[:port]] [destination] [port]\n");
1693 1.1 christos if (ret)
1694 1.1 christos exit(1);
1695 1.1 christos }
1696