5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Sun Jul 2 17:52:22 1995 ylo
12 Functions for returning the canonical host name of the remote site.
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. */
26 char *get_remote_hostname(int socket)
28 struct sockaddr_in from;
31 char name[MAXHOSTNAMELEN];
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)
38 error("getpeername failed: %.100s", strerror(errno));
39 strlcpy(name, "UNKNOWN", sizeof name);
40 goto check_ip_options;
43 /* Map the IP address to a host name. */
44 hp = gethostbyaddr((char *)&from.sin_addr, sizeof(struct in_addr),
48 /* Got host name, find canonic host name. */
49 if (strchr(hp->h_name, '.') != 0)
50 strlcpy(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 strlcpy(name, hp->h_aliases[0], sizeof(name));
56 strlcpy(name, hp->h_name, sizeof(name));
58 /* Convert it to all lowercase (which is expected by the rest of this
60 for (i = 0; name[i]; i++)
62 name[i] = tolower(name[i]);
64 /* Map it back to an IP address and check that the given address actually
65 is an address of this host. This is necessary because anyone with
66 access to a name server can define arbitrary names for an IP address.
67 Mapping from name to IP address can be trusted better (but can still
68 be fooled if the intruder has access to the name server of the
70 hp = gethostbyname(name);
73 log("reverse mapping checking gethostbyname for %.700s failed - POSSIBLE BREAKIN ATTEMPT!", name);
74 strlcpy(name, inet_ntoa(from.sin_addr), sizeof name);
75 goto check_ip_options;
77 /* Look for the address from the list of addresses. */
78 for (i = 0; hp->h_addr_list[i]; i++)
79 if (memcmp(hp->h_addr_list[i], &from.sin_addr, sizeof(from.sin_addr))
82 /* If we reached the end of the list, the address was not there. */
83 if (!hp->h_addr_list[i])
85 /* Address not found for the host name. */
86 log("Address %.100s maps to %.600s, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!",
87 inet_ntoa(from.sin_addr), name);
88 strlcpy(name, inet_ntoa(from.sin_addr), sizeof name);
89 goto check_ip_options;
91 /* Address was found for the host name. We accept the host name. */
95 /* Host name not found. Use ascii representation of the address. */
96 strlcpy(name, inet_ntoa(from.sin_addr), sizeof name);
97 log("Could not reverse map address %.100s.", name);
102 /* If IP options are supported, make sure there are none (log and clear
103 them if any are found). Basically we are worried about source routing;
104 it can be used to pretend you are somebody (ip-address) you are not.
105 That itself may be "almost acceptable" under certain circumstances,
106 but rhosts autentication is useless if source routing is accepted.
107 Notice also that if we just dropped source routing here, the other
108 side could use IP spoofing to do rest of the interaction and could still
109 bypass security. So we exit here if we detect any IP options. */
111 unsigned char options[200], *ucp;
112 char text[1024], *cp;
113 int option_size, ipproto;
116 if ((ip = getprotobyname("ip")) != NULL)
117 ipproto = ip->p_proto;
119 ipproto = IPPROTO_IP;
120 option_size = sizeof(options);
121 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)options,
122 &option_size) >= 0 && option_size != 0)
125 /* Note: "text" buffer must be at least 3x as big as options. */
126 for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3)
127 sprintf(cp, " %2.2x", *ucp);
128 log("Connection from %.100s with IP options:%.800s",
129 inet_ntoa(from.sin_addr), text);
130 packet_disconnect("Connection from %.100s with IP options:%.800s",
131 inet_ntoa(from.sin_addr), text);
135 return xstrdup(name);
138 static char *canonical_host_name = NULL;
139 static char *canonical_host_ip = NULL;
141 /* Return the canonical name of the host in the other side of the current
142 connection. The host name is cached, so it is efficient to call this
145 const char *get_canonical_hostname()
147 /* Check if we have previously retrieved this same name. */
148 if (canonical_host_name != NULL)
149 return canonical_host_name;
151 /* Get the real hostname if socket; otherwise return UNKNOWN. */
152 if (packet_get_connection_in() == packet_get_connection_out())
153 canonical_host_name = get_remote_hostname(packet_get_connection_in());
155 canonical_host_name = xstrdup("UNKNOWN");
157 return canonical_host_name;
160 /* Returns the IP-address of the remote host as a string. The returned
161 string need not be freed. */
163 const char *get_remote_ipaddr()
165 struct sockaddr_in from;
168 /* Check if we have previously retrieved this same name. */
169 if (canonical_host_ip != NULL)
170 return canonical_host_ip;
172 /* If not a socket, return UNKNOWN. */
173 if (packet_get_connection_in() != packet_get_connection_out())
175 canonical_host_ip = xstrdup("UNKNOWN");
176 return canonical_host_ip;
179 /* Get client socket. */
180 socket = packet_get_connection_in();
182 /* Get IP address of client. */
183 fromlen = sizeof(from);
184 memset(&from, 0, sizeof(from));
185 if (getpeername(socket, (struct sockaddr *)&from, &fromlen) < 0)
187 error("getpeername failed: %.100s", strerror(errno));
191 /* Get the IP address in ascii. */
192 canonical_host_ip = xstrdup(inet_ntoa(from.sin_addr));
194 /* Return ip address string. */
195 return canonical_host_ip;
198 /* Returns the port of the peer of the socket. */
200 int get_peer_port(int sock)
202 struct sockaddr_in from;
205 /* Get IP address of client. */
206 fromlen = sizeof(from);
207 memset(&from, 0, sizeof(from));
208 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0)
210 error("getpeername failed: %.100s", strerror(errno));
214 /* Return port number. */
215 return ntohs(from.sin_port);
218 /* Returns the port number of the remote host. */
220 int get_remote_port()
224 /* If the connection is not a socket, return 65535. This is intentionally
225 chosen to be an unprivileged port number. */
226 if (packet_get_connection_in() != packet_get_connection_out())
229 /* Get client socket. */
230 socket = packet_get_connection_in();
232 /* Get and return the peer port number. */
233 return get_peer_port(socket);