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

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