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

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