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