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

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