[BACK]Return to netcat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / nc

Annotation of src/usr.bin/nc/netcat.c, Revision 1.145

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