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