Annotation of src/usr.bin/ssh/canohost.c, Revision 1.1
1.1 ! deraadt 1: /*
! 2:
! 3: canohost.c
! 4:
! 5: Author: Tatu Ylonen <ylo@cs.hut.fi>
! 6:
! 7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
! 8: All rights reserved
! 9:
! 10: Created: Sun Jul 2 17:52:22 1995 ylo
! 11:
! 12: Functions for returning the canonical host name of the remote site.
! 13:
! 14: */
! 15:
! 16: #include "includes.h"
! 17: RCSID("$Id: canohost.c,v 1.3 1999/05/11 19:27:15 bg Exp $");
! 18:
! 19: #include "packet.h"
! 20: #include "xmalloc.h"
! 21: #include "ssh.h"
! 22:
! 23: /* Return the canonical name of the host at the other end of the socket.
! 24: The caller should free the returned string with xfree. */
! 25:
! 26: char *get_remote_hostname(int socket)
! 27: {
! 28: struct sockaddr_in from;
! 29: int fromlen, i;
! 30: struct hostent *hp;
! 31: char name[512];
! 32:
! 33: /* Get IP address of client. */
! 34: fromlen = sizeof(from);
! 35: memset(&from, 0, sizeof(from));
! 36: if (getpeername(socket, (struct sockaddr *)&from, &fromlen) < 0)
! 37: {
! 38: error("getpeername failed: %.100s", strerror(errno));
! 39: strcpy(name, "UNKNOWN");
! 40: goto check_ip_options;
! 41: }
! 42:
! 43: /* Map the IP address to a host name. */
! 44: hp = gethostbyaddr((char *)&from.sin_addr, sizeof(struct in_addr),
! 45: from.sin_family);
! 46: if (hp)
! 47: {
! 48: /* Got host name, find canonic host name. */
! 49: if (strchr(hp->h_name, '.') != 0)
! 50: strncpy(name, hp->h_name, sizeof(name));
! 51: else if (hp->h_aliases != 0
! 52: && hp->h_aliases[0] != 0
! 53: && strchr(hp->h_aliases[0], '.') != 0)
! 54: strncpy(name, hp->h_aliases[0], sizeof(name));
! 55: else
! 56: strncpy(name, hp->h_name, sizeof(name));
! 57: name[sizeof(name) - 1] = '\0';
! 58:
! 59: /* Convert it to all lowercase (which is expected by the rest of this
! 60: software). */
! 61: for (i = 0; name[i]; i++)
! 62: if (isupper(name[i]))
! 63: name[i] = tolower(name[i]);
! 64:
! 65: /* Map it back to an IP address and check that the given address actually
! 66: is an address of this host. This is necessary because anyone with
! 67: access to a name server can define arbitrary names for an IP address.
! 68: Mapping from name to IP address can be trusted better (but can still
! 69: be fooled if the intruder has access to the name server of the
! 70: domain). */
! 71: hp = gethostbyname(name);
! 72: if (!hp)
! 73: {
! 74: log("reverse mapping checking gethostbyname for %.700s failed - POSSIBLE BREAKIN ATTEMPT!", name);
! 75: strcpy(name, inet_ntoa(from.sin_addr));
! 76: goto check_ip_options;
! 77: }
! 78: /* Look for the address from the list of addresses. */
! 79: for (i = 0; hp->h_addr_list[i]; i++)
! 80: if (memcmp(hp->h_addr_list[i], &from.sin_addr, sizeof(from.sin_addr))
! 81: == 0)
! 82: break;
! 83: /* If we reached the end of the list, the address was not there. */
! 84: if (!hp->h_addr_list[i])
! 85: {
! 86: /* Address not found for the host name. */
! 87: log("Address %.100s maps to %.600s, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!",
! 88: inet_ntoa(from.sin_addr), name);
! 89: strcpy(name, inet_ntoa(from.sin_addr));
! 90: goto check_ip_options;
! 91: }
! 92: /* Address was found for the host name. We accept the host name. */
! 93: }
! 94: else
! 95: {
! 96: /* Host name not found. Use ascii representation of the address. */
! 97: strcpy(name, inet_ntoa(from.sin_addr));
! 98: log("Could not reverse map address %.100s.", name);
! 99: }
! 100:
! 101: check_ip_options:
! 102:
! 103: #ifdef IP_OPTIONS
! 104: /* If IP options are supported, make sure there are none (log and clear
! 105: them if any are found). Basically we are worried about source routing;
! 106: it can be used to pretend you are somebody (ip-address) you are not.
! 107: That itself may be "almost acceptable" under certain circumstances,
! 108: but rhosts autentication is useless if source routing is accepted.
! 109: Notice also that if we just dropped source routing here, the other
! 110: side could use IP spoofing to do rest of the interaction and could still
! 111: bypass security. So we exit here if we detect any IP options. */
! 112: {
! 113: unsigned char options[200], *ucp;
! 114: char text[1024], *cp;
! 115: int option_size, ipproto;
! 116: struct protoent *ip;
! 117:
! 118: if ((ip = getprotobyname("ip")) != NULL)
! 119: ipproto = ip->p_proto;
! 120: else
! 121: ipproto = IPPROTO_IP;
! 122: option_size = sizeof(options);
! 123: if (getsockopt(0, ipproto, IP_OPTIONS, (char *)options,
! 124: &option_size) >= 0 && option_size != 0)
! 125: {
! 126: cp = text;
! 127: /* Note: "text" buffer must be at least 3x as big as options. */
! 128: for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3)
! 129: sprintf(cp, " %2.2x", *ucp);
! 130: log("Connection from %.100s with IP options:%.800s",
! 131: inet_ntoa(from.sin_addr), text);
! 132: packet_disconnect("Connection from %.100s with IP options:%.800s",
! 133: inet_ntoa(from.sin_addr), text);
! 134: }
! 135: }
! 136: #endif
! 137:
! 138: return xstrdup(name);
! 139: }
! 140:
! 141: static char *canonical_host_name = NULL;
! 142: static char *canonical_host_ip = NULL;
! 143:
! 144: /* Return the canonical name of the host in the other side of the current
! 145: connection. The host name is cached, so it is efficient to call this
! 146: several times. */
! 147:
! 148: const char *get_canonical_hostname()
! 149: {
! 150: /* Check if we have previously retrieved this same name. */
! 151: if (canonical_host_name != NULL)
! 152: return canonical_host_name;
! 153:
! 154: /* Get the real hostname if socket; otherwise return UNKNOWN. */
! 155: if (packet_get_connection_in() == packet_get_connection_out())
! 156: canonical_host_name = get_remote_hostname(packet_get_connection_in());
! 157: else
! 158: canonical_host_name = xstrdup("UNKNOWN");
! 159:
! 160: return canonical_host_name;
! 161: }
! 162:
! 163: /* Returns the IP-address of the remote host as a string. The returned
! 164: string need not be freed. */
! 165:
! 166: const char *get_remote_ipaddr()
! 167: {
! 168: struct sockaddr_in from;
! 169: int fromlen, socket;
! 170:
! 171: /* Check if we have previously retrieved this same name. */
! 172: if (canonical_host_ip != NULL)
! 173: return canonical_host_ip;
! 174:
! 175: /* If not a socket, return UNKNOWN. */
! 176: if (packet_get_connection_in() != packet_get_connection_out())
! 177: {
! 178: canonical_host_ip = xstrdup("UNKNOWN");
! 179: return canonical_host_ip;
! 180: }
! 181:
! 182: /* Get client socket. */
! 183: socket = packet_get_connection_in();
! 184:
! 185: /* Get IP address of client. */
! 186: fromlen = sizeof(from);
! 187: memset(&from, 0, sizeof(from));
! 188: if (getpeername(socket, (struct sockaddr *)&from, &fromlen) < 0)
! 189: {
! 190: error("getpeername failed: %.100s", strerror(errno));
! 191: return NULL;
! 192: }
! 193:
! 194: /* Get the IP address in ascii. */
! 195: canonical_host_ip = xstrdup(inet_ntoa(from.sin_addr));
! 196:
! 197: /* Return ip address string. */
! 198: return canonical_host_ip;
! 199: }
! 200:
! 201: /* Returns the port of the peer of the socket. */
! 202:
! 203: int get_peer_port(int sock)
! 204: {
! 205: struct sockaddr_in from;
! 206: int fromlen;
! 207:
! 208: /* Get IP address of client. */
! 209: fromlen = sizeof(from);
! 210: memset(&from, 0, sizeof(from));
! 211: if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0)
! 212: {
! 213: error("getpeername failed: %.100s", strerror(errno));
! 214: return 0;
! 215: }
! 216:
! 217: /* Return port number. */
! 218: return ntohs(from.sin_port);
! 219: }
! 220:
! 221: /* Returns the port number of the remote host. */
! 222:
! 223: int get_remote_port()
! 224: {
! 225: int socket;
! 226:
! 227: /* If the connection is not a socket, return 65535. This is intentionally
! 228: chosen to be an unprivileged port number. */
! 229: if (packet_get_connection_in() != packet_get_connection_out())
! 230: return 65535;
! 231:
! 232: /* Get client socket. */
! 233: socket = packet_get_connection_in();
! 234:
! 235: /* Get and return the peer port number. */
! 236: return get_peer_port(socket);
! 237: }