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

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