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