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

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.20    ! markus     15: RCSID("$OpenBSD: canohost.c,v 1.19 2001/01/29 19:42:33 markus Exp $");
1.1       deraadt    16:
                     17: #include "packet.h"
                     18: #include "xmalloc.h"
1.18      markus     19: #include "log.h"
1.1       deraadt    20:
1.20    ! markus     21: void   check_ip_options(int socket, char *ipaddr);
        !            22:
1.8       markus     23: /*
                     24:  * Return the canonical name of the host at the other end of the socket. The
                     25:  * caller should free the returned string with xfree.
                     26:  */
1.1       deraadt    27:
1.6       markus     28: char *
1.20    ! markus     29: get_remote_hostname(int socket, int reverse_mapping_check)
1.1       deraadt    30: {
1.10      markus     31:        struct sockaddr_storage from;
                     32:        int i;
                     33:        socklen_t fromlen;
                     34:        struct addrinfo hints, *ai, *aitop;
1.20    ! markus     35:        char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
1.6       markus     36:
                     37:        /* Get IP address of client. */
                     38:        fromlen = sizeof(from);
                     39:        memset(&from, 0, sizeof(from));
1.20    ! markus     40:        if (getpeername(socket, (struct sockaddr *) &from, &fromlen) < 0) {
1.6       markus     41:                debug("getpeername failed: %.100s", strerror(errno));
                     42:                fatal_cleanup();
                     43:        }
1.20    ! markus     44:        if (from.ss_family == AF_INET)
        !            45:                check_ip_options(socket, ntop);
        !            46:
1.10      markus     47:        if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
                     48:             NULL, 0, NI_NUMERICHOST) != 0)
                     49:                fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
                     50:
1.6       markus     51:        /* Map the IP address to a host name. */
1.10      markus     52:        if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
1.20    ! markus     53:             NULL, 0, NI_NAMEREQD) != 0) {
        !            54:                /* Host name not found.  Use ip address. */
        !            55:                log("Could not reverse map address %.100s.", ntop);
        !            56:                return xstrdup(ntop);
1.6       markus     57:        }
                     58:
1.20    ! markus     59:        /* Got host name. */
        !            60:        name[sizeof(name) - 1] = '\0';
        !            61:        /*
        !            62:         * Convert it to all lowercase (which is expected by the rest
        !            63:         * of this software).
        !            64:         */
        !            65:        for (i = 0; name[i]; i++)
        !            66:                if (isupper(name[i]))
        !            67:                        name[i] = tolower(name[i]);
1.6       markus     68:
1.20    ! markus     69:        if (!reverse_mapping_check)
        !            70:                return xstrdup(name);
1.8       markus     71:        /*
1.20    ! markus     72:         * Map it back to an IP address and check that the given
        !            73:         * address actually is an address of this host.  This is
        !            74:         * necessary because anyone with access to a name server can
        !            75:         * define arbitrary names for an IP address. Mapping from
        !            76:         * name to IP address can be trusted better (but can still be
        !            77:         * fooled if the intruder has access to the name server of
        !            78:         * the domain).
1.8       markus     79:         */
1.20    ! markus     80:        memset(&hints, 0, sizeof(hints));
        !            81:        hints.ai_family = from.ss_family;
        !            82:        hints.ai_socktype = SOCK_STREAM;
        !            83:        if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
        !            84:                log("reverse mapping checking getaddrinfo for %.700s "
        !            85:                    "failed - POSSIBLE BREAKIN ATTEMPT!", name);
        !            86:                return xstrdup(ntop);
        !            87:        }
        !            88:        /* Look for the address from the list of addresses. */
        !            89:        for (ai = aitop; ai; ai = ai->ai_next) {
        !            90:                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
        !            91:                    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
        !            92:                    (strcmp(ntop, ntop2) == 0))
        !            93:                                break;
        !            94:        }
        !            95:        freeaddrinfo(aitop);
        !            96:        /* If we reached the end of the list, the address was not there. */
        !            97:        if (!ai) {
        !            98:                /* Address not found for the host name. */
        !            99:                log("Address %.100s maps to %.600s, but this does not "
        !           100:                    "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
        !           101:                    ntop, name);
        !           102:                return xstrdup(ntop);
        !           103:        }
        !           104:        return xstrdup(name);
        !           105: }
1.6       markus    106:
1.20    ! markus    107: /*
        !           108:  * If IP options are supported, make sure there are none (log and
        !           109:  * disconnect them if any are found).  Basically we are worried about
        !           110:  * source routing; it can be used to pretend you are somebody
        !           111:  * (ip-address) you are not. That itself may be "almost acceptable"
        !           112:  * under certain circumstances, but rhosts autentication is useless
        !           113:  * if source routing is accepted. Notice also that if we just dropped
        !           114:  * source routing here, the other side could use IP spoofing to do
        !           115:  * rest of the interaction and could still bypass security.  So we
        !           116:  * exit here if we detect any IP options.
        !           117:  */
        !           118: /* IPv4 only */
        !           119: void
        !           120: check_ip_options(int socket, char *ipaddr)
        !           121: {
        !           122:        u_char options[200], *ucp;
        !           123:        char text[1024], *cp;
        !           124:        socklen_t option_size;
        !           125:        int ipproto;
        !           126:        struct protoent *ip;
        !           127:
        !           128:        if ((ip = getprotobyname("ip")) != NULL)
        !           129:                ipproto = ip->p_proto;
        !           130:        else
        !           131:                ipproto = IPPROTO_IP;
        !           132:        option_size = sizeof(options);
        !           133:        if (getsockopt(socket, ipproto, IP_OPTIONS, (void *)options,
        !           134:            &option_size) >= 0 && option_size != 0) {
        !           135:                cp = text;
        !           136:                /* Note: "text" buffer must be at least 3x as big as options. */
        !           137:                for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3)
        !           138:                        sprintf(cp, " %2.2x", *ucp);
        !           139:                log("Connection from %.100s with IP options:%.800s",
        !           140:                    ipaddr, text);
        !           141:                packet_disconnect("Connection from %.100s with IP options:%.800s",
        !           142:                    ipaddr, text);
1.6       markus    143:        }
1.1       deraadt   144: }
                    145:
1.8       markus    146: /*
                    147:  * Return the canonical name of the host in the other side of the current
                    148:  * connection.  The host name is cached, so it is efficient to call this
                    149:  * several times.
                    150:  */
1.1       deraadt   151:
1.6       markus    152: const char *
1.20    ! markus    153: get_canonical_hostname(int reverse_mapping_check)
1.1       deraadt   154: {
1.10      markus    155:        static char *canonical_host_name = NULL;
1.20    ! markus    156:        static int reverse_mapping_checked = 0;
1.10      markus    157:
1.20    ! markus    158:        /* Check if we have previously retrieved name with same option. */
        !           159:        if (canonical_host_name != NULL) {
        !           160:                if (reverse_mapping_checked != reverse_mapping_check)
        !           161:                        xfree(canonical_host_name);
        !           162:                else
        !           163:                        return canonical_host_name;
        !           164:        }
1.6       markus    165:
                    166:        /* Get the real hostname if socket; otherwise return UNKNOWN. */
1.10      markus    167:        if (packet_connection_is_on_socket())
1.20    ! markus    168:                canonical_host_name = get_remote_hostname(
        !           169:                    packet_get_connection_in(), reverse_mapping_check);
1.6       markus    170:        else
                    171:                canonical_host_name = xstrdup("UNKNOWN");
1.1       deraadt   172:
1.20    ! markus    173:        reverse_mapping_checked = reverse_mapping_check;
1.6       markus    174:        return canonical_host_name;
1.1       deraadt   175: }
                    176:
1.8       markus    177: /*
1.19      markus    178:  * Returns the remote IP-address of socket as a string.  The returned
                    179:  * string must be freed.
1.8       markus    180:  */
1.1       deraadt   181:
1.19      markus    182: char *
                    183: get_peer_ipaddr(int socket)
1.1       deraadt   184: {
1.10      markus    185:        struct sockaddr_storage from;
                    186:        socklen_t fromlen;
                    187:        char ntop[NI_MAXHOST];
1.1       deraadt   188:
1.6       markus    189:        /* Get IP address of client. */
                    190:        fromlen = sizeof(from);
                    191:        memset(&from, 0, sizeof(from));
                    192:        if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) {
1.19      markus    193:                debug("get_peer_ipaddr: getpeername failed: %.100s", strerror(errno));
                    194:                return NULL;
1.6       markus    195:        }
                    196:        /* Get the IP address in ascii. */
1.10      markus    197:        if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
1.19      markus    198:             NULL, 0, NI_NUMERICHOST) != 0) {
                    199:                error("get_peer_ipaddr: getnameinfo NI_NUMERICHOST failed");
                    200:                return NULL;
                    201:        }
                    202:        return xstrdup(ntop);
                    203: }
                    204:
                    205: /*
                    206:  * Returns the IP-address of the remote host as a string.  The returned
                    207:  * string must not be freed.
                    208:  */
1.10      markus    209:
1.19      markus    210: const char *
                    211: get_remote_ipaddr()
                    212: {
                    213:        static char *canonical_host_ip = NULL;
1.1       deraadt   214:
1.19      markus    215:        /* Check whether we have cached the ipaddr. */
                    216:        if (canonical_host_ip == NULL) {
                    217:                if (packet_connection_is_on_socket()) {
                    218:                        canonical_host_ip =
                    219:                            get_peer_ipaddr(packet_get_connection_in());
                    220:                        if (canonical_host_ip == NULL)
                    221:                                fatal_cleanup();
                    222:                } else {
                    223:                        /* If not on socket, return UNKNOWN. */
                    224:                        canonical_host_ip = xstrdup("UNKNOWN");
                    225:                }
                    226:        }
1.6       markus    227:        return canonical_host_ip;
1.1       deraadt   228: }
                    229:
1.10      markus    230: /* Returns the local/remote port for the socket. */
1.1       deraadt   231:
1.10      markus    232: int
                    233: get_sock_port(int sock, int local)
1.1       deraadt   234: {
1.10      markus    235:        struct sockaddr_storage from;
                    236:        socklen_t fromlen;
                    237:        char strport[NI_MAXSERV];
1.1       deraadt   238:
1.6       markus    239:        /* Get IP address of client. */
                    240:        fromlen = sizeof(from);
                    241:        memset(&from, 0, sizeof(from));
1.10      markus    242:        if (local) {
                    243:                if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
                    244:                        error("getsockname failed: %.100s", strerror(errno));
                    245:                        return 0;
                    246:                }
                    247:        } else {
                    248:                if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
                    249:                        debug("getpeername failed: %.100s", strerror(errno));
                    250:                        fatal_cleanup();
                    251:                }
1.6       markus    252:        }
                    253:        /* Return port number. */
1.10      markus    254:        if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
                    255:             strport, sizeof(strport), NI_NUMERICSERV) != 0)
                    256:                fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
                    257:        return atoi(strport);
1.1       deraadt   258: }
                    259:
1.10      markus    260: /* Returns remote/local port number for the current connection. */
1.1       deraadt   261:
1.12      markus    262: int
1.10      markus    263: get_port(int local)
1.1       deraadt   264: {
1.8       markus    265:        /*
                    266:         * If the connection is not a socket, return 65535.  This is
                    267:         * intentionally chosen to be an unprivileged port number.
                    268:         */
1.10      markus    269:        if (!packet_connection_is_on_socket())
1.6       markus    270:                return 65535;
1.1       deraadt   271:
1.10      markus    272:        /* Get socket and return the port number. */
                    273:        return get_sock_port(packet_get_connection_in(), local);
                    274: }
                    275:
1.12      markus    276: int
1.10      markus    277: get_peer_port(int sock)
                    278: {
                    279:        return get_sock_port(sock, 0);
                    280: }
                    281:
1.12      markus    282: int
1.10      markus    283: get_remote_port()
                    284: {
                    285:        return get_port(0);
                    286: }
1.1       deraadt   287:
1.10      markus    288: int
                    289: get_local_port()
                    290: {
                    291:        return get_port(1);
1.1       deraadt   292: }