]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 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 | */ | |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
17 | RCSID("$Id$"); | |
18 | ||
19 | #include "packet.h" | |
20 | #include "xmalloc.h" | |
21 | #include "ssh.h" | |
22 | ||
aa3378df | 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 | */ | |
8efc0c15 | 27 | |
5260325f | 28 | char * |
29 | get_remote_hostname(int socket) | |
8efc0c15 | 30 | { |
48e671d5 | 31 | struct sockaddr_storage from; |
32 | int i; | |
33 | socklen_t fromlen; | |
34 | struct addrinfo hints, *ai, *aitop; | |
5260325f | 35 | char name[MAXHOSTNAMELEN]; |
48e671d5 | 36 | char ntop[NI_MAXHOST], ntop2[NI_MAXHOST]; |
5260325f | 37 | |
38 | /* Get IP address of client. */ | |
39 | fromlen = sizeof(from); | |
40 | memset(&from, 0, sizeof(from)); | |
41 | if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) { | |
42 | debug("getpeername failed: %.100s", strerror(errno)); | |
43 | fatal_cleanup(); | |
44 | } | |
80faa19f | 45 | |
46 | #ifdef IPV4_IN_IPV6 | |
47 | if (from.ss_family == AF_INET6) { | |
48 | struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from; | |
49 | ||
50 | /* Detect IPv4 in IPv6 mapped address and convert it to */ | |
51 | /* plain (AF_INET) IPv4 address */ | |
52 | if (IN6_IS_ADDR_V4MAPPED(&from6->sin6_addr)) { | |
53 | struct sockaddr_in *from4 = (struct sockaddr_in *)&from; | |
54 | struct in_addr addr; | |
55 | u_int16_t port; | |
56 | ||
57 | memcpy(&addr, ((char *)&from6->sin6_addr) + 12, sizeof(addr)); | |
58 | port = from6->sin6_port; | |
59 | ||
60 | memset(&from, 0, sizeof(from)); | |
61 | ||
62 | from4->sin_family = AF_INET; | |
63 | memcpy(&from4->sin_addr, &addr, sizeof(addr)); | |
64 | from4->sin_port = port; | |
65 | } | |
66 | } | |
67 | #endif | |
68 | ||
48e671d5 | 69 | if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), |
70 | NULL, 0, NI_NUMERICHOST) != 0) | |
71 | fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); | |
5260325f | 72 | |
48e671d5 | 73 | /* Map the IP address to a host name. */ |
74 | if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), | |
75 | NULL, 0, NI_NAMEREQD) == 0) { | |
76 | /* Got host name. */ | |
77 | name[sizeof(name) - 1] = '\0'; | |
aa3378df | 78 | /* |
79 | * Convert it to all lowercase (which is expected by the rest | |
80 | * of this software). | |
81 | */ | |
5260325f | 82 | for (i = 0; name[i]; i++) |
83 | if (isupper(name[i])) | |
84 | name[i] = tolower(name[i]); | |
85 | ||
aa3378df | 86 | /* |
87 | * Map it back to an IP address and check that the given | |
88 | * address actually is an address of this host. This is | |
89 | * necessary because anyone with access to a name server can | |
90 | * define arbitrary names for an IP address. Mapping from | |
91 | * name to IP address can be trusted better (but can still be | |
92 | * fooled if the intruder has access to the name server of | |
93 | * the domain). | |
94 | */ | |
48e671d5 | 95 | memset(&hints, 0, sizeof(hints)); |
96 | hints.ai_family = from.ss_family; | |
97 | hints.ai_socktype = SOCK_STREAM; | |
98 | if (getaddrinfo(name, NULL, &hints, &aitop) != 0) { | |
99 | log("reverse mapping checking getaddrinfo for %.700s failed - POSSIBLE BREAKIN ATTEMPT!", name); | |
100 | strlcpy(name, ntop, sizeof name); | |
5260325f | 101 | goto check_ip_options; |
102 | } | |
103 | /* Look for the address from the list of addresses. */ | |
48e671d5 | 104 | for (ai = aitop; ai; ai = ai->ai_next) { |
105 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2, | |
106 | sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 && | |
107 | (strcmp(ntop, ntop2) == 0)) | |
108 | break; | |
109 | } | |
110 | freeaddrinfo(aitop); | |
111 | /* If we reached the end of the list, the address was not there. */ | |
112 | if (!ai) { | |
5260325f | 113 | /* Address not found for the host name. */ |
114 | log("Address %.100s maps to %.600s, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!", | |
48e671d5 | 115 | ntop, name); |
116 | strlcpy(name, ntop, sizeof name); | |
5260325f | 117 | goto check_ip_options; |
118 | } | |
119 | /* Address was found for the host name. We accept the host name. */ | |
120 | } else { | |
121 | /* Host name not found. Use ascii representation of the address. */ | |
48e671d5 | 122 | strlcpy(name, ntop, sizeof name); |
5260325f | 123 | log("Could not reverse map address %.100s.", name); |
8efc0c15 | 124 | } |
5260325f | 125 | |
126 | check_ip_options: | |
127 | ||
aa3378df | 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 | */ | |
48e671d5 | 139 | /* IP options -- IPv4 only */ |
140 | if (from.ss_family == AF_INET) { | |
5260325f | 141 | unsigned char options[200], *ucp; |
142 | char text[1024], *cp; | |
48e671d5 | 143 | socklen_t option_size; |
144 | int ipproto; | |
5260325f | 145 | struct protoent *ip; |
146 | ||
147 | if ((ip = getprotobyname("ip")) != NULL) | |
148 | ipproto = ip->p_proto; | |
149 | else | |
150 | ipproto = IPPROTO_IP; | |
151 | option_size = sizeof(options); | |
152 | if (getsockopt(0, ipproto, IP_OPTIONS, (char *) options, | |
48e671d5 | 153 | &option_size) >= 0 && option_size != 0) { |
5260325f | 154 | cp = text; |
155 | /* Note: "text" buffer must be at least 3x as big as options. */ | |
156 | for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3) | |
157 | sprintf(cp, " %2.2x", *ucp); | |
158 | log("Connection from %.100s with IP options:%.800s", | |
48e671d5 | 159 | ntop, text); |
5260325f | 160 | packet_disconnect("Connection from %.100s with IP options:%.800s", |
48e671d5 | 161 | ntop, text); |
5260325f | 162 | } |
8efc0c15 | 163 | } |
5260325f | 164 | |
165 | return xstrdup(name); | |
8efc0c15 | 166 | } |
167 | ||
aa3378df | 168 | /* |
169 | * Return the canonical name of the host in the other side of the current | |
170 | * connection. The host name is cached, so it is efficient to call this | |
171 | * several times. | |
172 | */ | |
8efc0c15 | 173 | |
5260325f | 174 | const char * |
175 | get_canonical_hostname() | |
8efc0c15 | 176 | { |
48e671d5 | 177 | static char *canonical_host_name = NULL; |
178 | ||
5260325f | 179 | /* Check if we have previously retrieved this same name. */ |
180 | if (canonical_host_name != NULL) | |
181 | return canonical_host_name; | |
8efc0c15 | 182 | |
5260325f | 183 | /* Get the real hostname if socket; otherwise return UNKNOWN. */ |
48e671d5 | 184 | if (packet_connection_is_on_socket()) |
5260325f | 185 | canonical_host_name = get_remote_hostname(packet_get_connection_in()); |
186 | else | |
187 | canonical_host_name = xstrdup("UNKNOWN"); | |
8efc0c15 | 188 | |
5260325f | 189 | return canonical_host_name; |
8efc0c15 | 190 | } |
191 | ||
aa3378df | 192 | /* |
193 | * Returns the IP-address of the remote host as a string. The returned | |
48e671d5 | 194 | * string must not be freed. |
aa3378df | 195 | */ |
8efc0c15 | 196 | |
5260325f | 197 | const char * |
198 | get_remote_ipaddr() | |
8efc0c15 | 199 | { |
48e671d5 | 200 | static char *canonical_host_ip = NULL; |
201 | struct sockaddr_storage from; | |
202 | socklen_t fromlen; | |
203 | int socket; | |
204 | char ntop[NI_MAXHOST]; | |
5260325f | 205 | |
aa3378df | 206 | /* Check whether we have chached the name. */ |
5260325f | 207 | if (canonical_host_ip != NULL) |
208 | return canonical_host_ip; | |
209 | ||
210 | /* If not a socket, return UNKNOWN. */ | |
48e671d5 | 211 | if (!packet_connection_is_on_socket()) { |
5260325f | 212 | canonical_host_ip = xstrdup("UNKNOWN"); |
213 | return canonical_host_ip; | |
214 | } | |
215 | /* Get client socket. */ | |
216 | socket = packet_get_connection_in(); | |
217 | ||
218 | /* Get IP address of client. */ | |
219 | fromlen = sizeof(from); | |
220 | memset(&from, 0, sizeof(from)); | |
221 | if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) { | |
222 | debug("getpeername failed: %.100s", strerror(errno)); | |
223 | fatal_cleanup(); | |
224 | } | |
225 | /* Get the IP address in ascii. */ | |
48e671d5 | 226 | if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), |
227 | NULL, 0, NI_NUMERICHOST) != 0) | |
228 | fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); | |
229 | ||
230 | canonical_host_ip = xstrdup(ntop); | |
5260325f | 231 | |
232 | /* Return ip address string. */ | |
233 | return canonical_host_ip; | |
8efc0c15 | 234 | } |
235 | ||
48e671d5 | 236 | /* Returns the local/remote port for the socket. */ |
8efc0c15 | 237 | |
48e671d5 | 238 | int |
239 | get_sock_port(int sock, int local) | |
8efc0c15 | 240 | { |
48e671d5 | 241 | struct sockaddr_storage from; |
242 | socklen_t fromlen; | |
243 | char strport[NI_MAXSERV]; | |
5260325f | 244 | |
245 | /* Get IP address of client. */ | |
246 | fromlen = sizeof(from); | |
247 | memset(&from, 0, sizeof(from)); | |
48e671d5 | 248 | if (local) { |
249 | if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) { | |
250 | error("getsockname failed: %.100s", strerror(errno)); | |
251 | return 0; | |
252 | } | |
253 | } else { | |
254 | if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) { | |
255 | debug("getpeername failed: %.100s", strerror(errno)); | |
256 | fatal_cleanup(); | |
257 | } | |
5260325f | 258 | } |
259 | /* Return port number. */ | |
48e671d5 | 260 | if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0, |
261 | strport, sizeof(strport), NI_NUMERICSERV) != 0) | |
262 | fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed"); | |
263 | return atoi(strport); | |
8efc0c15 | 264 | } |
265 | ||
48e671d5 | 266 | /* Returns remote/local port number for the current connection. */ |
8efc0c15 | 267 | |
5260325f | 268 | int |
48e671d5 | 269 | get_port(int local) |
8efc0c15 | 270 | { |
aa3378df | 271 | /* |
272 | * If the connection is not a socket, return 65535. This is | |
273 | * intentionally chosen to be an unprivileged port number. | |
274 | */ | |
48e671d5 | 275 | if (!packet_connection_is_on_socket()) |
5260325f | 276 | return 65535; |
8efc0c15 | 277 | |
48e671d5 | 278 | /* Get socket and return the port number. */ |
279 | return get_sock_port(packet_get_connection_in(), local); | |
280 | } | |
281 | ||
282 | int | |
283 | get_peer_port(int sock) | |
284 | { | |
285 | return get_sock_port(sock, 0); | |
286 | } | |
8efc0c15 | 287 | |
48e671d5 | 288 | int |
289 | get_remote_port() | |
290 | { | |
291 | return get_port(0); | |
292 | } | |
293 | ||
294 | int | |
295 | get_local_port() | |
296 | { | |
297 | return get_port(1); | |
8efc0c15 | 298 | } |