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

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