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