summaryrefslogtreecommitdiff
path: root/canohost.c
diff options
context:
space:
mode:
Diffstat (limited to 'canohost.c')
-rw-r--r--canohost.c52
1 files changed, 20 insertions, 32 deletions
diff --git a/canohost.c b/canohost.c
index 438175f76..941db23b6 100644
--- a/canohost.c
+++ b/canohost.c
@@ -12,7 +12,7 @@
12 */ 12 */
13 13
14#include "includes.h" 14#include "includes.h"
15RCSID("$OpenBSD: canohost.c,v 1.37 2003/06/02 09:17:34 markus Exp $"); 15RCSID("$OpenBSD: canohost.c,v 1.35 2002/11/26 02:38:54 stevesk Exp $");
16 16
17#include "packet.h" 17#include "packet.h"
18#include "xmalloc.h" 18#include "xmalloc.h"
@@ -27,7 +27,7 @@ static void check_ip_options(int, char *);
27 */ 27 */
28 28
29static char * 29static char *
30get_remote_hostname(int socket, int use_dns) 30get_remote_hostname(int socket, int verify_reverse_mapping)
31{ 31{
32 struct sockaddr_storage from; 32 struct sockaddr_storage from;
33 int i; 33 int i;
@@ -72,9 +72,6 @@ get_remote_hostname(int socket, int use_dns)
72 NULL, 0, NI_NUMERICHOST) != 0) 72 NULL, 0, NI_NUMERICHOST) != 0)
73 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); 73 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
74 74
75 if (!use_dns)
76 return xstrdup(ntop);
77
78 if (from.ss_family == AF_INET) 75 if (from.ss_family == AF_INET)
79 check_ip_options(socket, ntop); 76 check_ip_options(socket, ntop);
80 77
@@ -83,24 +80,14 @@ get_remote_hostname(int socket, int use_dns)
83 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), 80 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
84 NULL, 0, NI_NAMEREQD) != 0) { 81 NULL, 0, NI_NAMEREQD) != 0) {
85 /* Host name not found. Use ip address. */ 82 /* Host name not found. Use ip address. */
83#if 0
84 log("Could not reverse map address %.100s.", ntop);
85#endif
86 return xstrdup(ntop); 86 return xstrdup(ntop);
87 } 87 }
88 88
89 /* 89 /* Got host name. */
90 * if reverse lookup result looks like a numeric hostname, 90 name[sizeof(name) - 1] = '\0';
91 * someone is trying to trick us by PTR record like following:
92 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
93 */
94 memset(&hints, 0, sizeof(hints));
95 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
96 hints.ai_flags = AI_NUMERICHOST;
97 if (getaddrinfo(name, "0", &hints, &ai) == 0) {
98 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
99 name, ntop);
100 freeaddrinfo(ai);
101 return xstrdup(ntop);
102 }
103
104 /* 91 /*
105 * Convert it to all lowercase (which is expected by the rest 92 * Convert it to all lowercase (which is expected by the rest
106 * of this software). 93 * of this software).
@@ -108,6 +95,9 @@ get_remote_hostname(int socket, int use_dns)
108 for (i = 0; name[i]; i++) 95 for (i = 0; name[i]; i++)
109 if (isupper(name[i])) 96 if (isupper(name[i]))
110 name[i] = tolower(name[i]); 97 name[i] = tolower(name[i]);
98
99 if (!verify_reverse_mapping)
100 return xstrdup(name);
111 /* 101 /*
112 * Map it back to an IP address and check that the given 102 * Map it back to an IP address and check that the given
113 * address actually is an address of this host. This is 103 * address actually is an address of this host. This is
@@ -121,7 +111,7 @@ get_remote_hostname(int socket, int use_dns)
121 hints.ai_family = from.ss_family; 111 hints.ai_family = from.ss_family;
122 hints.ai_socktype = SOCK_STREAM; 112 hints.ai_socktype = SOCK_STREAM;
123 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) { 113 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
124 logit("reverse mapping checking getaddrinfo for %.700s " 114 log("reverse mapping checking getaddrinfo for %.700s "
125 "failed - POSSIBLE BREAKIN ATTEMPT!", name); 115 "failed - POSSIBLE BREAKIN ATTEMPT!", name);
126 return xstrdup(ntop); 116 return xstrdup(ntop);
127 } 117 }
@@ -136,7 +126,7 @@ get_remote_hostname(int socket, int use_dns)
136 /* If we reached the end of the list, the address was not there. */ 126 /* If we reached the end of the list, the address was not there. */
137 if (!ai) { 127 if (!ai) {
138 /* Address not found for the host name. */ 128 /* Address not found for the host name. */
139 logit("Address %.100s maps to %.600s, but this does not " 129 log("Address %.100s maps to %.600s, but this does not "
140 "map back to the address - POSSIBLE BREAKIN ATTEMPT!", 130 "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
141 ntop, name); 131 ntop, name);
142 return xstrdup(ntop); 132 return xstrdup(ntop);
@@ -159,7 +149,6 @@ get_remote_hostname(int socket, int use_dns)
159static void 149static void
160check_ip_options(int socket, char *ipaddr) 150check_ip_options(int socket, char *ipaddr)
161{ 151{
162#ifdef IP_OPTIONS
163 u_char options[200]; 152 u_char options[200];
164 char text[sizeof(options) * 3 + 1]; 153 char text[sizeof(options) * 3 + 1];
165 socklen_t option_size; 154 socklen_t option_size;
@@ -177,12 +166,11 @@ check_ip_options(int socket, char *ipaddr)
177 for (i = 0; i < option_size; i++) 166 for (i = 0; i < option_size; i++)
178 snprintf(text + i*3, sizeof(text) - i*3, 167 snprintf(text + i*3, sizeof(text) - i*3,
179 " %2.2x", options[i]); 168 " %2.2x", options[i]);
180 logit("Connection from %.100s with IP options:%.800s", 169 log("Connection from %.100s with IP options:%.800s",
181 ipaddr, text); 170 ipaddr, text);
182 packet_disconnect("Connection from %.100s with IP options:%.800s", 171 packet_disconnect("Connection from %.100s with IP options:%.800s",
183 ipaddr, text); 172 ipaddr, text);
184 } 173 }
185#endif /* IP_OPTIONS */
186} 174}
187 175
188/* 176/*
@@ -192,14 +180,14 @@ check_ip_options(int socket, char *ipaddr)
192 */ 180 */
193 181
194const char * 182const char *
195get_canonical_hostname(int use_dns) 183get_canonical_hostname(int verify_reverse_mapping)
196{ 184{
197 static char *canonical_host_name = NULL; 185 static char *canonical_host_name = NULL;
198 static int use_dns_done = 0; 186 static int verify_reverse_mapping_done = 0;
199 187
200 /* Check if we have previously retrieved name with same option. */ 188 /* Check if we have previously retrieved name with same option. */
201 if (canonical_host_name != NULL) { 189 if (canonical_host_name != NULL) {
202 if (use_dns_done != use_dns) 190 if (verify_reverse_mapping_done != verify_reverse_mapping)
203 xfree(canonical_host_name); 191 xfree(canonical_host_name);
204 else 192 else
205 return canonical_host_name; 193 return canonical_host_name;
@@ -208,11 +196,11 @@ get_canonical_hostname(int use_dns)
208 /* Get the real hostname if socket; otherwise return UNKNOWN. */ 196 /* Get the real hostname if socket; otherwise return UNKNOWN. */
209 if (packet_connection_is_on_socket()) 197 if (packet_connection_is_on_socket())
210 canonical_host_name = get_remote_hostname( 198 canonical_host_name = get_remote_hostname(
211 packet_get_connection_in(), use_dns); 199 packet_get_connection_in(), verify_reverse_mapping);
212 else 200 else
213 canonical_host_name = xstrdup("UNKNOWN"); 201 canonical_host_name = xstrdup("UNKNOWN");
214 202
215 use_dns_done = use_dns; 203 verify_reverse_mapping_done = verify_reverse_mapping;
216 return canonical_host_name; 204 return canonical_host_name;
217} 205}
218 206
@@ -306,11 +294,11 @@ get_remote_ipaddr(void)
306} 294}
307 295
308const char * 296const char *
309get_remote_name_or_ip(u_int utmp_len, int use_dns) 297get_remote_name_or_ip(u_int utmp_len, int verify_reverse_mapping)
310{ 298{
311 static const char *remote = ""; 299 static const char *remote = "";
312 if (utmp_len > 0) 300 if (utmp_len > 0)
313 remote = get_canonical_hostname(use_dns); 301 remote = get_canonical_hostname(verify_reverse_mapping);
314 if (utmp_len == 0 || strlen(remote) > utmp_len) 302 if (utmp_len == 0 || strlen(remote) > utmp_len)
315 remote = get_remote_ipaddr(); 303 remote = get_remote_ipaddr();
316 return remote; 304 return remote;