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