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

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