]> andersk Git - openssh.git/blob - canohost.c
- OpenBSD CVS Updates:
[openssh.git] / canohost.c
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("$OpenBSD: canohost.c,v 1.13 2000/06/20 01:39:39 markus Exp $");
18
19 #include "packet.h"
20 #include "xmalloc.h"
21 #include "ssh.h"
22
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  */
27
28 char *
29 get_remote_hostname(int socket)
30 {
31         struct sockaddr_storage from;
32         int i;
33         socklen_t fromlen;
34         struct addrinfo hints, *ai, *aitop;
35         char name[MAXHOSTNAMELEN];
36         char ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
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         }
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
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");
72
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';
78                 /*
79                  * Convert it to all lowercase (which is expected by the rest
80                  * of this software).
81                  */
82                 for (i = 0; name[i]; i++)
83                         if (isupper(name[i]))
84                                 name[i] = tolower(name[i]);
85
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                  */
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);
101                         goto check_ip_options;
102                 }
103                 /* Look for the address from the list of addresses. */
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) {
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!",
115                             ntop, name);
116                         strlcpy(name, ntop, sizeof name);
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. */
122                 strlcpy(name, ntop, sizeof name);
123                 log("Could not reverse map address %.100s.", name);
124         }
125
126 check_ip_options:
127
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         /* IP options -- IPv4 only */
140         if (from.ss_family == AF_INET) {
141                 unsigned char options[200], *ucp;
142                 char text[1024], *cp;
143                 socklen_t option_size;
144                 int ipproto;
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,
153                     &option_size) >= 0 && option_size != 0) {
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",
159                             ntop, text);
160                         packet_disconnect("Connection from %.100s with IP options:%.800s",
161                                           ntop, text);
162                 }
163         }
164
165         return xstrdup(name);
166 }
167
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  */
173
174 const char *
175 get_canonical_hostname()
176 {
177         static char *canonical_host_name = NULL;
178
179         /* Check if we have previously retrieved this same name. */
180         if (canonical_host_name != NULL)
181                 return canonical_host_name;
182
183         /* Get the real hostname if socket; otherwise return UNKNOWN. */
184         if (packet_connection_is_on_socket())
185                 canonical_host_name = get_remote_hostname(packet_get_connection_in());
186         else
187                 canonical_host_name = xstrdup("UNKNOWN");
188
189         return canonical_host_name;
190 }
191
192 /*
193  * Returns the IP-address of the remote host as a string.  The returned
194  * string must not be freed.
195  */
196
197 const char *
198 get_remote_ipaddr()
199 {
200         static char *canonical_host_ip = NULL;
201         struct sockaddr_storage from;
202         socklen_t fromlen;
203         int socket;
204         char ntop[NI_MAXHOST];
205
206         /* Check whether we have chached the name. */
207         if (canonical_host_ip != NULL)
208                 return canonical_host_ip;
209
210         /* If not a socket, return UNKNOWN. */
211         if (!packet_connection_is_on_socket()) {
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. */
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);
231
232         /* Return ip address string. */
233         return canonical_host_ip;
234 }
235
236 /* Returns the local/remote port for the socket. */
237
238 int
239 get_sock_port(int sock, int local)
240 {
241         struct sockaddr_storage from;
242         socklen_t fromlen;
243         char strport[NI_MAXSERV];
244
245         /* Get IP address of client. */
246         fromlen = sizeof(from);
247         memset(&from, 0, sizeof(from));
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                 }
258         }
259         /* Return port number. */
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);
264 }
265
266 /* Returns remote/local port number for the current connection. */
267
268 int
269 get_port(int local)
270 {
271         /*
272          * If the connection is not a socket, return 65535.  This is
273          * intentionally chosen to be an unprivileged port number.
274          */
275         if (!packet_connection_is_on_socket())
276                 return 65535;
277
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 }
287
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);
298 }
This page took 0.057651 seconds and 5 git commands to generate.