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 debug("getpeername failed: %.100s", strerror(errno));
42 /* Map the IP address to a host name. */
43 hp = gethostbyaddr((char *)&from.sin_addr, sizeof(struct in_addr),
47 /* Got host name, find canonic host name. */
48 if (strchr(hp->h_name, '.') != 0)
49 strlcpy(name, hp->h_name, sizeof(name));
50 else if (hp->h_aliases != 0
51 && hp->h_aliases[0] != 0
52 && strchr(hp->h_aliases[0], '.') != 0)
53 strlcpy(name, hp->h_aliases[0], sizeof(name));
55 strlcpy(name, hp->h_name, sizeof(name));
57 /* Convert it to all lowercase (which is expected by the rest of this
59 for (i = 0; name[i]; i++)
61 name[i] = tolower(name[i]);
63 /* Map it back to an IP address and check that the given address actually
64 is an address of this host. This is necessary because anyone with
65 access to a name server can define arbitrary names for an IP address.
66 Mapping from name to IP address can be trusted better (but can still
67 be fooled if the intruder has access to the name server of the
69 hp = gethostbyname(name);
72 log("reverse mapping checking gethostbyname for %.700s failed - POSSIBLE BREAKIN ATTEMPT!", name);
73 strlcpy(name, inet_ntoa(from.sin_addr), sizeof name);
74 goto check_ip_options;
76 /* Look for the address from the list of addresses. */
77 for (i = 0; hp->h_addr_list[i]; i++)
78 if (memcmp(hp->h_addr_list[i], &from.sin_addr, sizeof(from.sin_addr))
81 /* If we reached the end of the list, the address was not there. */
82 if (!hp->h_addr_list[i])
84 /* Address not found for the host name. */
85 log("Address %.100s maps to %.600s, but this does not map back to the address - POSSIBLE BREAKIN ATTEMPT!",
86 inet_ntoa(from.sin_addr), name);
87 strlcpy(name, inet_ntoa(from.sin_addr), sizeof name);
88 goto check_ip_options;
90 /* Address was found for the host name. We accept the host name. */
94 /* Host name not found. Use ascii representation of the address. */
95 strlcpy(name, inet_ntoa(from.sin_addr), sizeof name);
96 log("Could not reverse map address %.100s.", name);
101 /* If IP options are supported, make sure there are none (log and disconnect
102 them if any are found). Basically we are worried about source routing;
103 it can be used to pretend you are somebody (ip-address) you are not.
104 That itself may be "almost acceptable" under certain circumstances,
105 but rhosts autentication is useless if source routing is accepted.
106 Notice also that if we just dropped source routing here, the other
107 side could use IP spoofing to do rest of the interaction and could still
108 bypass security. So we exit here if we detect any IP options. */
110 unsigned char options[200], *ucp;
111 char text[1024], *cp;
112 int option_size, ipproto;
115 if ((ip = getprotobyname("ip")) != NULL)
116 ipproto = ip->p_proto;
118 ipproto = IPPROTO_IP;
119 option_size = sizeof(options);
120 if (getsockopt(0, ipproto, IP_OPTIONS, (char *)options,
121 &option_size) >= 0 && option_size != 0)
124 /* Note: "text" buffer must be at least 3x as big as options. */
125 for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3)
126 sprintf(cp, " %2.2x", *ucp);
127 log("Connection from %.100s with IP options:%.800s",
128 inet_ntoa(from.sin_addr), text);
129 packet_disconnect("Connection from %.100s with IP options:%.800s",
130 inet_ntoa(from.sin_addr), text);
134 return xstrdup(name);
137 static char *canonical_host_name = NULL;
138 static char *canonical_host_ip = NULL;
140 /* Return the canonical name of the host in the other side of the current
141 connection. The host name is cached, so it is efficient to call this
144 const char *get_canonical_hostname()
146 /* Check if we have previously retrieved this same name. */
147 if (canonical_host_name != NULL)
148 return canonical_host_name;
150 /* Get the real hostname if socket; otherwise return UNKNOWN. */
151 if (packet_get_connection_in() == packet_get_connection_out())
152 canonical_host_name = get_remote_hostname(packet_get_connection_in());
154 canonical_host_name = xstrdup("UNKNOWN");
156 return canonical_host_name;
159 /* Returns the IP-address of the remote host as a string. The returned
160 string need not be freed. */
162 const char *get_remote_ipaddr()
164 struct sockaddr_in from;
167 /* Check if we have previously retrieved this same name. */
168 if (canonical_host_ip != NULL)
169 return canonical_host_ip;
171 /* If not a socket, return UNKNOWN. */
172 if (packet_get_connection_in() != packet_get_connection_out())
174 canonical_host_ip = xstrdup("UNKNOWN");
175 return canonical_host_ip;
178 /* Get client socket. */
179 socket = packet_get_connection_in();
181 /* Get IP address of client. */
182 fromlen = sizeof(from);
183 memset(&from, 0, sizeof(from));
184 if (getpeername(socket, (struct sockaddr *)&from, &fromlen) < 0)
186 debug("getpeername failed: %.100s", strerror(errno));
190 /* Get the IP address in ascii. */
191 canonical_host_ip = xstrdup(inet_ntoa(from.sin_addr));
193 /* Return ip address string. */
194 return canonical_host_ip;
197 /* Returns the port of the peer of the socket. */
199 int get_peer_port(int sock)
201 struct sockaddr_in from;
204 /* Get IP address of client. */
205 fromlen = sizeof(from);
206 memset(&from, 0, sizeof(from));
207 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0)
209 debug("getpeername failed: %.100s", strerror(errno));
213 /* Return port number. */
214 return ntohs(from.sin_port);
217 /* Returns the port number of the remote host. */
219 int get_remote_port()
223 /* If the connection is not a socket, return 65535. This is intentionally
224 chosen to be an unprivileged port number. */
225 if (packet_get_connection_in() != packet_get_connection_out())
228 /* Get client socket. */
229 socket = packet_get_connection_in();
231 /* Get and return the peer port number. */
232 return get_peer_port(socket);