[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.142

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