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

Annotation of src/usr.bin/ssh/canohost.c, Revision 1.70

1.70    ! dtucker     1: /* $OpenBSD: canohost.c,v 1.69 2013/11/20 20:54:10 deraadt Exp $ */
1.1       deraadt     2: /*
1.7       deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Functions for returning the canonical host name of the remote site.
1.12      markus      7:  *
1.14      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.7       deraadt    13:  */
1.1       deraadt    14:
1.55      stevesk    15: #include <sys/types.h>
1.54      stevesk    16: #include <sys/socket.h>
                     17:
                     18: #include <netinet/in.h>
1.49      stevesk    19:
1.56      stevesk    20: #include <errno.h>
1.57      stevesk    21: #include <netdb.h>
1.60      stevesk    22: #include <stdio.h>
1.59      stevesk    23: #include <stdlib.h>
1.58      stevesk    24: #include <string.h>
1.61      deraadt    25: #include <stdarg.h>
1.66      dtucker    26: #include <unistd.h>
1.1       deraadt    27:
1.61      deraadt    28: #include "xmalloc.h"
1.1       deraadt    29: #include "packet.h"
1.18      markus     30: #include "log.h"
1.21      itojun     31: #include "canohost.h"
1.62      dtucker    32: #include "misc.h"
1.1       deraadt    33:
1.27      itojun     34: static void check_ip_options(int, char *);
1.65      andreas    35: static char *canonical_host_ip = NULL;
                     36: static int cached_port = -1;
1.20      markus     37:
1.8       markus     38: /*
                     39:  * Return the canonical name of the host at the other end of the socket. The
1.67      djm        40:  * caller should free the returned string.
1.8       markus     41:  */
1.1       deraadt    42:
1.27      itojun     43: static char *
1.40      avsm       44: get_remote_hostname(int sock, int use_dns)
1.1       deraadt    45: {
1.10      markus     46:        struct sockaddr_storage from;
                     47:        socklen_t fromlen;
                     48:        struct addrinfo hints, *ai, *aitop;
1.20      markus     49:        char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
1.6       markus     50:
                     51:        /* Get IP address of client. */
                     52:        fromlen = sizeof(from);
                     53:        memset(&from, 0, sizeof(from));
1.40      avsm       54:        if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
1.6       markus     55:                debug("getpeername failed: %.100s", strerror(errno));
1.38      markus     56:                cleanup_exit(255);
1.6       markus     57:        }
1.20      markus     58:
1.10      markus     59:        if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
1.29      deraadt    60:            NULL, 0, NI_NUMERICHOST) != 0)
1.10      markus     61:                fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
1.45      dtucker    62:
                     63:        if (from.ss_family == AF_INET)
                     64:                check_ip_options(sock, ntop);
1.32      itojun     65:
1.37      markus     66:        if (!use_dns)
                     67:                return xstrdup(ntop);
1.10      markus     68:
1.26      markus     69:        debug3("Trying to reverse map address %.100s.", ntop);
1.6       markus     70:        /* Map the IP address to a host name. */
1.10      markus     71:        if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
1.29      deraadt    72:            NULL, 0, NI_NAMEREQD) != 0) {
1.20      markus     73:                /* Host name not found.  Use ip address. */
                     74:                return xstrdup(ntop);
1.6       markus     75:        }
                     76:
1.37      markus     77:        /*
                     78:         * if reverse lookup result looks like a numeric hostname,
                     79:         * someone is trying to trick us by PTR record like following:
                     80:         *      1.1.1.10.in-addr.arpa.  IN PTR  2.3.4.5
                     81:         */
                     82:        memset(&hints, 0, sizeof(hints));
                     83:        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
                     84:        hints.ai_flags = AI_NUMERICHOST;
1.63      dtucker    85:        if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
1.37      markus     86:                logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
                     87:                    name, ntop);
                     88:                freeaddrinfo(ai);
                     89:                return xstrdup(ntop);
                     90:        }
                     91:
1.68      djm        92:        /* Names are stores in lowercase. */
                     93:        lowercase(name);
                     94:
1.8       markus     95:        /*
1.20      markus     96:         * Map it back to an IP address and check that the given
                     97:         * address actually is an address of this host.  This is
                     98:         * necessary because anyone with access to a name server can
                     99:         * define arbitrary names for an IP address. Mapping from
                    100:         * name to IP address can be trusted better (but can still be
                    101:         * fooled if the intruder has access to the name server of
                    102:         * the domain).
1.8       markus    103:         */
1.20      markus    104:        memset(&hints, 0, sizeof(hints));
                    105:        hints.ai_family = from.ss_family;
                    106:        hints.ai_socktype = SOCK_STREAM;
                    107:        if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
1.36      itojun    108:                logit("reverse mapping checking getaddrinfo for %.700s "
1.50      djm       109:                    "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
1.20      markus    110:                return xstrdup(ntop);
                    111:        }
                    112:        /* Look for the address from the list of addresses. */
                    113:        for (ai = aitop; ai; ai = ai->ai_next) {
                    114:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
                    115:                    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
                    116:                    (strcmp(ntop, ntop2) == 0))
                    117:                                break;
                    118:        }
                    119:        freeaddrinfo(aitop);
                    120:        /* If we reached the end of the list, the address was not there. */
                    121:        if (!ai) {
                    122:                /* Address not found for the host name. */
1.36      itojun    123:                logit("Address %.100s maps to %.600s, but this does not "
1.48      stevesk   124:                    "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
1.20      markus    125:                    ntop, name);
                    126:                return xstrdup(ntop);
                    127:        }
                    128:        return xstrdup(name);
                    129: }
1.6       markus    130:
1.20      markus    131: /*
                    132:  * If IP options are supported, make sure there are none (log and
                    133:  * disconnect them if any are found).  Basically we are worried about
                    134:  * source routing; it can be used to pretend you are somebody
                    135:  * (ip-address) you are not. That itself may be "almost acceptable"
                    136:  * under certain circumstances, but rhosts autentication is useless
                    137:  * if source routing is accepted. Notice also that if we just dropped
                    138:  * source routing here, the other side could use IP spoofing to do
                    139:  * rest of the interaction and could still bypass security.  So we
                    140:  * exit here if we detect any IP options.
                    141:  */
                    142: /* IPv4 only */
1.27      itojun    143: static void
1.40      avsm      144: check_ip_options(int sock, char *ipaddr)
1.20      markus    145: {
1.22      markus    146:        u_char options[200];
                    147:        char text[sizeof(options) * 3 + 1];
1.70    ! dtucker   148:        socklen_t option_size, i;
1.44      djm       149:        int ipproto;
1.20      markus    150:        struct protoent *ip;
                    151:
                    152:        if ((ip = getprotobyname("ip")) != NULL)
                    153:                ipproto = ip->p_proto;
                    154:        else
                    155:                ipproto = IPPROTO_IP;
                    156:        option_size = sizeof(options);
1.40      avsm      157:        if (getsockopt(sock, ipproto, IP_OPTIONS, options,
1.20      markus    158:            &option_size) >= 0 && option_size != 0) {
1.22      markus    159:                text[0] = '\0';
                    160:                for (i = 0; i < option_size; i++)
                    161:                        snprintf(text + i*3, sizeof(text) - i*3,
                    162:                            " %2.2x", options[i]);
1.46      dtucker   163:                fatal("Connection from %.100s with IP options:%.800s",
1.20      markus    164:                    ipaddr, text);
1.6       markus    165:        }
1.1       deraadt   166: }
                    167:
1.8       markus    168: /*
                    169:  * Return the canonical name of the host in the other side of the current
                    170:  * connection.  The host name is cached, so it is efficient to call this
                    171:  * several times.
                    172:  */
1.1       deraadt   173:
1.6       markus    174: const char *
1.37      markus    175: get_canonical_hostname(int use_dns)
1.1       deraadt   176: {
1.47      dtucker   177:        char *host;
1.10      markus    178:        static char *canonical_host_name = NULL;
1.47      dtucker   179:        static char *remote_ip = NULL;
1.10      markus    180:
1.20      markus    181:        /* Check if we have previously retrieved name with same option. */
1.47      dtucker   182:        if (use_dns && canonical_host_name != NULL)
                    183:                return canonical_host_name;
                    184:        if (!use_dns && remote_ip != NULL)
                    185:                return remote_ip;
1.6       markus    186:
                    187:        /* Get the real hostname if socket; otherwise return UNKNOWN. */
1.10      markus    188:        if (packet_connection_is_on_socket())
1.47      dtucker   189:                host = get_remote_hostname(packet_get_connection_in(), use_dns);
1.6       markus    190:        else
1.47      dtucker   191:                host = "UNKNOWN";
1.1       deraadt   192:
1.47      dtucker   193:        if (use_dns)
                    194:                canonical_host_name = host;
                    195:        else
                    196:                remote_ip = host;
                    197:        return host;
1.1       deraadt   198: }
                    199:
1.8       markus    200: /*
1.35      stevesk   201:  * Returns the local/remote IP-address/hostname of socket as a string.
                    202:  * The returned string must be freed.
1.8       markus    203:  */
1.27      itojun    204: static char *
1.40      avsm      205: get_socket_address(int sock, int remote, int flags)
1.1       deraadt   206: {
1.25      markus    207:        struct sockaddr_storage addr;
                    208:        socklen_t addrlen;
1.10      markus    209:        char ntop[NI_MAXHOST];
1.42      djm       210:        int r;
1.1       deraadt   211:
1.6       markus    212:        /* Get IP address of client. */
1.25      markus    213:        addrlen = sizeof(addr);
                    214:        memset(&addr, 0, sizeof(addr));
                    215:
                    216:        if (remote) {
1.40      avsm      217:                if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
1.34      stevesk   218:                    < 0)
1.25      markus    219:                        return NULL;
                    220:        } else {
1.40      avsm      221:                if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
1.34      stevesk   222:                    < 0)
1.25      markus    223:                        return NULL;
                    224:        }
                    225:        /* Get the address in ascii. */
1.42      djm       226:        if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
                    227:            sizeof(ntop), NULL, 0, flags)) != 0) {
                    228:                error("get_socket_address: getnameinfo %d failed: %s", flags,
1.62      dtucker   229:                    ssh_gai_strerror(r));
1.19      markus    230:                return NULL;
                    231:        }
                    232:        return xstrdup(ntop);
1.25      markus    233: }
                    234:
                    235: char *
1.40      avsm      236: get_peer_ipaddr(int sock)
1.25      markus    237: {
1.34      stevesk   238:        char *p;
                    239:
1.40      avsm      240:        if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
1.34      stevesk   241:                return p;
                    242:        return xstrdup("UNKNOWN");
1.25      markus    243: }
                    244:
                    245: char *
1.40      avsm      246: get_local_ipaddr(int sock)
1.25      markus    247: {
1.34      stevesk   248:        char *p;
                    249:
1.40      avsm      250:        if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
1.34      stevesk   251:                return p;
                    252:        return xstrdup("UNKNOWN");
1.25      markus    253: }
                    254:
                    255: char *
1.66      dtucker   256: get_local_name(int fd)
1.25      markus    257: {
1.66      dtucker   258:        char *host, myname[NI_MAXHOST];
                    259:
                    260:        /* Assume we were passed a socket */
                    261:        if ((host = get_socket_address(fd, 0, NI_NAMEREQD)) != NULL)
                    262:                return host;
                    263:
                    264:        /* Handle the case where we were passed a pipe */
                    265:        if (gethostname(myname, sizeof(myname)) == -1) {
                    266:                verbose("get_local_name: gethostname: %s", strerror(errno));
                    267:        } else {
                    268:                host = xstrdup(myname);
                    269:        }
                    270:
                    271:        return host;
1.19      markus    272: }
                    273:
1.65      andreas   274: void
                    275: clear_cached_addr(void)
                    276: {
1.67      djm       277:        free(canonical_host_ip);
                    278:        canonical_host_ip = NULL;
1.65      andreas   279:        cached_port = -1;
                    280: }
                    281:
1.19      markus    282: /*
                    283:  * Returns the IP-address of the remote host as a string.  The returned
                    284:  * string must not be freed.
                    285:  */
1.10      markus    286:
1.19      markus    287: const char *
1.28      itojun    288: get_remote_ipaddr(void)
1.19      markus    289: {
                    290:        /* Check whether we have cached the ipaddr. */
                    291:        if (canonical_host_ip == NULL) {
                    292:                if (packet_connection_is_on_socket()) {
                    293:                        canonical_host_ip =
                    294:                            get_peer_ipaddr(packet_get_connection_in());
                    295:                        if (canonical_host_ip == NULL)
1.38      markus    296:                                cleanup_exit(255);
1.19      markus    297:                } else {
                    298:                        /* If not on socket, return UNKNOWN. */
                    299:                        canonical_host_ip = xstrdup("UNKNOWN");
                    300:                }
                    301:        }
1.6       markus    302:        return canonical_host_ip;
1.24      stevesk   303: }
                    304:
                    305: const char *
1.37      markus    306: get_remote_name_or_ip(u_int utmp_len, int use_dns)
1.24      stevesk   307: {
                    308:        static const char *remote = "";
                    309:        if (utmp_len > 0)
1.37      markus    310:                remote = get_canonical_hostname(use_dns);
1.24      stevesk   311:        if (utmp_len == 0 || strlen(remote) > utmp_len)
                    312:                remote = get_remote_ipaddr();
                    313:        return remote;
1.1       deraadt   314: }
                    315:
1.10      markus    316: /* Returns the local/remote port for the socket. */
1.1       deraadt   317:
1.64      djm       318: int
1.10      markus    319: get_sock_port(int sock, int local)
1.1       deraadt   320: {
1.10      markus    321:        struct sockaddr_storage from;
                    322:        socklen_t fromlen;
                    323:        char strport[NI_MAXSERV];
1.42      djm       324:        int r;
1.1       deraadt   325:
1.6       markus    326:        /* Get IP address of client. */
                    327:        fromlen = sizeof(from);
                    328:        memset(&from, 0, sizeof(from));
1.10      markus    329:        if (local) {
                    330:                if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
                    331:                        error("getsockname failed: %.100s", strerror(errno));
                    332:                        return 0;
                    333:                }
                    334:        } else {
1.35      stevesk   335:                if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
1.10      markus    336:                        debug("getpeername failed: %.100s", strerror(errno));
1.43      markus    337:                        return -1;
1.10      markus    338:                }
1.6       markus    339:        }
                    340:        /* Return port number. */
1.42      djm       341:        if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
                    342:            strport, sizeof(strport), NI_NUMERICSERV)) != 0)
                    343:                fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
1.62      dtucker   344:                    ssh_gai_strerror(r));
1.10      markus    345:        return atoi(strport);
1.1       deraadt   346: }
                    347:
1.10      markus    348: /* Returns remote/local port number for the current connection. */
1.1       deraadt   349:
1.27      itojun    350: static int
1.10      markus    351: get_port(int local)
1.1       deraadt   352: {
1.8       markus    353:        /*
                    354:         * If the connection is not a socket, return 65535.  This is
                    355:         * intentionally chosen to be an unprivileged port number.
                    356:         */
1.10      markus    357:        if (!packet_connection_is_on_socket())
1.6       markus    358:                return 65535;
1.1       deraadt   359:
1.10      markus    360:        /* Get socket and return the port number. */
                    361:        return get_sock_port(packet_get_connection_in(), local);
                    362: }
                    363:
1.12      markus    364: int
1.10      markus    365: get_peer_port(int sock)
                    366: {
                    367:        return get_sock_port(sock, 0);
                    368: }
                    369:
1.12      markus    370: int
1.28      itojun    371: get_remote_port(void)
1.10      markus    372: {
1.41      djm       373:        /* Cache to avoid getpeername() on a dead connection */
1.65      andreas   374:        if (cached_port == -1)
                    375:                cached_port = get_port(0);
1.41      djm       376:
1.65      andreas   377:        return cached_port;
1.10      markus    378: }
1.1       deraadt   379:
1.10      markus    380: int
1.28      itojun    381: get_local_port(void)
1.10      markus    382: {
                    383:        return get_port(1);
1.1       deraadt   384: }