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

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