blob: d32b9d352d3c1f479486952fc1b16d6229b86520 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
Martin Pool0cd2f402002-01-25 02:13:04 +00002 * Functions for looking up the remote name or addr of a socket.
3 *
Wayne Davison0f78b812006-04-25 20:23:34 +00004 * Copyright (C) 1992-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
Wayne Davisonba2133d2007-02-04 14:54:58 +00006 * Copyright (C) 2002-2007 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00007 *
8 * This program is free software; you can redistribute it and/or modify
Wayne Davison4fd842f2007-07-07 05:33:14 +00009 * it under the terms of the GNU General Public License version 3 as
Wayne Davisonba2133d2007-02-04 14:54:58 +000010 * published by the Free Software Foundation.
Wayne Davison0f78b812006-04-25 20:23:34 +000011 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
Wayne Davisone7c67062006-04-25 23:51:12 +000017 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000018 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000019 */
20
21/*
Martin Pool0cd2f402002-01-25 02:13:04 +000022 * This file is now converted to use the new-style getaddrinfo()
23 * interface, which supports IPv6 but is also supported on recent
24 * IPv4-only machines. On systems that don't have that interface, we
25 * emulate it using the KAME implementation.
Wayne Davison0f78b812006-04-25 20:23:34 +000026 */
Martin Pool0cd2f402002-01-25 02:13:04 +000027
28#include "rsync.h"
29
30static const char default_name[] = "UNKNOWN";
David Dykstra542ad672002-08-02 15:39:43 +000031extern int am_server;
Martin Pool0cd2f402002-01-25 02:13:04 +000032
33
34/**
Wayne Davisonc1e72172004-04-01 20:53:39 +000035 * Return the IP addr of the client as a string
Martin Pool0cd2f402002-01-25 02:13:04 +000036 **/
37char *client_addr(int fd)
38{
Martin Pool0cd2f402002-01-25 02:13:04 +000039 static char addr_buf[100];
40 static int initialised;
Wayne Davisonb2ef4f62004-06-03 17:20:21 +000041 struct sockaddr_storage ss;
42 socklen_t length = sizeof ss;
43 char *ssh_info, *p;
Martin Pool0cd2f402002-01-25 02:13:04 +000044
Wayne Davison4cfa6152004-04-01 20:56:50 +000045 if (initialised)
46 return addr_buf;
Martin Pool0cd2f402002-01-25 02:13:04 +000047
48 initialised = 1;
49
Wayne Davison706c7532004-04-01 21:39:35 +000050 if (am_server) { /* daemon over --rsh mode */
Wayne Davison55410362006-10-13 23:17:27 +000051 strlcpy(addr_buf, "0.0.0.0", sizeof addr_buf);
Wayne Davisonb2ef4f62004-06-03 17:20:21 +000052 if ((ssh_info = getenv("SSH_CONNECTION")) != NULL
53 || (ssh_info = getenv("SSH_CLIENT")) != NULL
54 || (ssh_info = getenv("SSH2_CLIENT")) != NULL) {
55 strlcpy(addr_buf, ssh_info, sizeof addr_buf);
56 /* Truncate the value to just the IP address. */
57 if ((p = strchr(addr_buf, ' ')) != NULL)
58 *p = '\0';
David Dykstra542ad672002-08-02 15:39:43 +000059 }
Wayne Davison3b5f6212002-08-02 17:11:39 +000060 } else {
David Dykstra09021ea2002-08-01 19:17:00 +000061 client_sockaddr(fd, &ss, &length);
Wayne Davison3b5f6212002-08-02 17:11:39 +000062 getnameinfo((struct sockaddr *)&ss, length,
63 addr_buf, sizeof addr_buf, NULL, 0, NI_NUMERICHOST);
64 }
David Dykstra09021ea2002-08-01 19:17:00 +000065
Martin Pool0cd2f402002-01-25 02:13:04 +000066 return addr_buf;
67}
68
69
70static int get_sockaddr_family(const struct sockaddr_storage *ss)
71{
72 return ((struct sockaddr *) ss)->sa_family;
73}
74
75
76/**
77 * Return the DNS name of the client.
78 *
79 * The name is statically cached so that repeated lookups are quick,
80 * so there is a limit of one lookup per customer.
81 *
82 * If anything goes wrong, including the name->addr->name check, then
83 * we just use "UNKNOWN", so you can use that value in hosts allow
84 * lines.
Martin Pool6c92af22002-04-03 02:33:42 +000085 *
86 * After translation from sockaddr to name we do a forward lookup to
87 * make sure nobody is spoofing PTR records.
Martin Pool0cd2f402002-01-25 02:13:04 +000088 **/
89char *client_name(int fd)
90{
Martin Pool0cd2f402002-01-25 02:13:04 +000091 static char name_buf[100];
92 static char port_buf[100];
93 static int initialised;
Wayne Davison706c7532004-04-01 21:39:35 +000094 struct sockaddr_storage ss;
David Dykstra1e736b82002-08-02 15:05:03 +000095 socklen_t ss_len;
Martin Pool0cd2f402002-01-25 02:13:04 +000096
Wayne Davison4cfa6152004-04-01 20:56:50 +000097 if (initialised)
98 return name_buf;
Martin Pool0cd2f402002-01-25 02:13:04 +000099
Wayne Davison55410362006-10-13 23:17:27 +0000100 strlcpy(name_buf, default_name, sizeof name_buf);
Martin Pool0cd2f402002-01-25 02:13:04 +0000101 initialised = 1;
102
Wayne Davison706c7532004-04-01 21:39:35 +0000103 memset(&ss, 0, sizeof ss);
104
Wayne Davison2b284ee2004-04-01 21:08:24 +0000105 if (am_server) { /* daemon over --rsh mode */
David Dykstra1e736b82002-08-02 15:05:03 +0000106 char *addr = client_addr(fd);
Wayne Davison706c7532004-04-01 21:39:35 +0000107 struct addrinfo hint, *answer;
108 int err;
109
110 memset(&hint, 0, sizeof hint);
111
Wayne Davison6a6d2112004-04-30 16:10:45 +0000112#ifdef AI_NUMERICHOST
Wayne Davison706c7532004-04-01 21:39:35 +0000113 hint.ai_flags = AI_NUMERICHOST;
Wayne Davison6a6d2112004-04-30 16:10:45 +0000114#endif
Wayne Davison706c7532004-04-01 21:39:35 +0000115 hint.ai_socktype = SOCK_STREAM;
116
117 if ((err = getaddrinfo(addr, NULL, &hint, &answer)) != 0) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000118 rprintf(FLOG, "malformed address %s: %s\n",
Wayne Davison706c7532004-04-01 21:39:35 +0000119 addr, gai_strerror(err));
120 return name_buf;
121 }
122
123 switch (answer->ai_family) {
124 case AF_INET:
125 ss_len = sizeof (struct sockaddr_in);
126 memcpy(&ss, answer->ai_addr, ss_len);
127 break;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000128#ifdef INET6
Wayne Davison706c7532004-04-01 21:39:35 +0000129 case AF_INET6:
130 ss_len = sizeof (struct sockaddr_in6);
131 memcpy(&ss, answer->ai_addr, ss_len);
132 break;
David Dykstra1e736b82002-08-02 15:05:03 +0000133#endif
Wayne Davison55410362006-10-13 23:17:27 +0000134 default:
135 exit_cleanup(RERR_SOCKETIO);
David Dykstra1e736b82002-08-02 15:05:03 +0000136 }
Wayne Davison706c7532004-04-01 21:39:35 +0000137 freeaddrinfo(answer);
David Dykstra09021ea2002-08-01 19:17:00 +0000138 } else {
David Dykstra1e736b82002-08-02 15:05:03 +0000139 ss_len = sizeof ss;
David Dykstra09021ea2002-08-01 19:17:00 +0000140 client_sockaddr(fd, &ss, &ss_len);
David Dykstra09021ea2002-08-01 19:17:00 +0000141 }
Martin Pool0cd2f402002-01-25 02:13:04 +0000142
Wayne Davisonfde045c2004-09-24 16:39:41 +0000143 if (lookup_name(fd, &ss, ss_len, name_buf, sizeof name_buf,
144 port_buf, sizeof port_buf) == 0)
Wayne Davison55410362006-10-13 23:17:27 +0000145 check_name(fd, &ss, name_buf, sizeof name_buf);
David Dykstra1e736b82002-08-02 15:05:03 +0000146
Martin Pool0cd2f402002-01-25 02:13:04 +0000147 return name_buf;
148}
149
150
151
152/**
Martin Poolaf32f692002-01-25 02:15:58 +0000153 * Get the sockaddr for the client.
154 *
155 * If it comes in as an ipv4 address mapped into IPv6 format then we
156 * convert it back to a regular IPv4.
Martin Pool0cd2f402002-01-25 02:13:04 +0000157 **/
158void client_sockaddr(int fd,
159 struct sockaddr_storage *ss,
160 socklen_t *ss_len)
161{
Wayne Davisonc1e72172004-04-01 20:53:39 +0000162 memset(ss, 0, sizeof *ss);
David Dykstrabc2b4962003-01-09 21:14:10 +0000163
Martin Pool0cd2f402002-01-25 02:13:04 +0000164 if (getpeername(fd, (struct sockaddr *) ss, ss_len)) {
165 /* FIXME: Can we really not continue? */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000166 rsyserr(FLOG, errno, "getpeername on fd%d failed", fd);
Martin Pool0cd2f402002-01-25 02:13:04 +0000167 exit_cleanup(RERR_SOCKETIO);
168 }
169
Wayne Davison4f5b0752005-02-14 00:53:43 +0000170#ifdef INET6
Wayne Davison2b284ee2004-04-01 21:08:24 +0000171 if (get_sockaddr_family(ss) == AF_INET6 &&
Martin Pool0cd2f402002-01-25 02:13:04 +0000172 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)ss)->sin6_addr)) {
173 /* OK, so ss is in the IPv6 family, but it is really
174 * an IPv4 address: something like
175 * "::ffff:10.130.1.2". If we use it as-is, then the
176 * reverse lookup might fail or perhaps something else
177 * bad might happen. So instead we convert it to an
178 * equivalent address in the IPv4 address family. */
179 struct sockaddr_in6 sin6;
180 struct sockaddr_in *sin;
181
Wayne Davisonc1e72172004-04-01 20:53:39 +0000182 memcpy(&sin6, ss, sizeof sin6);
Martin Pool0cd2f402002-01-25 02:13:04 +0000183 sin = (struct sockaddr_in *)ss;
Wayne Davisonc1e72172004-04-01 20:53:39 +0000184 memset(sin, 0, sizeof *sin);
Martin Pool0cd2f402002-01-25 02:13:04 +0000185 sin->sin_family = AF_INET;
Wayne Davisonc1e72172004-04-01 20:53:39 +0000186 *ss_len = sizeof (struct sockaddr_in);
Wayne Davison4f5b0752005-02-14 00:53:43 +0000187#ifdef HAVE_SOCKADDR_IN_LEN
Martin Pool0cd2f402002-01-25 02:13:04 +0000188 sin->sin_len = *ss_len;
189#endif
190 sin->sin_port = sin6.sin6_port;
191
192 /* There is a macro to extract the mapped part
193 * (IN6_V4MAPPED_TO_SINADDR ?), but it does not seem
194 * to be present in the Linux headers. */
195 memcpy(&sin->sin_addr, &sin6.sin6_addr.s6_addr[12],
Wayne Davisonc1e72172004-04-01 20:53:39 +0000196 sizeof sin->sin_addr);
Wayne Davison2b284ee2004-04-01 21:08:24 +0000197 }
Martin Pool0cd2f402002-01-25 02:13:04 +0000198#endif
199}
200
201
202/**
203 * Look up a name from @p ss into @p name_buf.
Martin Pool6c92af22002-04-03 02:33:42 +0000204 *
205 * @param fd file descriptor for client socket.
Martin Pool0cd2f402002-01-25 02:13:04 +0000206 **/
207int lookup_name(int fd, const struct sockaddr_storage *ss,
208 socklen_t ss_len,
Wayne Davison55410362006-10-13 23:17:27 +0000209 char *name_buf, size_t name_buf_size,
210 char *port_buf, size_t port_buf_size)
Martin Pool0cd2f402002-01-25 02:13:04 +0000211{
212 int name_err;
Wayne Davison4cfa6152004-04-01 20:56:50 +0000213
Martin Pool0cd2f402002-01-25 02:13:04 +0000214 /* reverse lookup */
215 name_err = getnameinfo((struct sockaddr *) ss, ss_len,
Wayne Davison55410362006-10-13 23:17:27 +0000216 name_buf, name_buf_size,
217 port_buf, port_buf_size,
Martin Pool0cd2f402002-01-25 02:13:04 +0000218 NI_NAMEREQD | NI_NUMERICSERV);
219 if (name_err != 0) {
Wayne Davison55410362006-10-13 23:17:27 +0000220 strlcpy(name_buf, default_name, name_buf_size);
Wayne Davisonfde045c2004-09-24 16:39:41 +0000221 rprintf(FLOG, "name lookup failed for %s: %s\n",
222 client_addr(fd), gai_strerror(name_err));
Martin Pool0cd2f402002-01-25 02:13:04 +0000223 return name_err;
224 }
225
226 return 0;
227}
228
229
230
Martin Poolaf32f692002-01-25 02:15:58 +0000231/**
Martin Pool974f27e2002-01-25 02:29:53 +0000232 * Compare an addrinfo from the resolver to a sockinfo.
233 *
234 * Like strcmp, returns 0 for identical.
235 **/
236int compare_addrinfo_sockaddr(const struct addrinfo *ai,
237 const struct sockaddr_storage *ss)
238{
239 int ss_family = get_sockaddr_family(ss);
240 const char fn[] = "compare_addrinfo_sockaddr";
Wayne Davisonc1e72172004-04-01 20:53:39 +0000241
Martin Pool974f27e2002-01-25 02:29:53 +0000242 if (ai->ai_family != ss_family) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000243 rprintf(FLOG, "%s: response family %d != %d\n",
Martin Pool974f27e2002-01-25 02:29:53 +0000244 fn, ai->ai_family, ss_family);
245 return 1;
246 }
247
Martin Pool39e01d22002-01-25 02:43:35 +0000248 /* The comparison method depends on the particular AF. */
249 if (ss_family == AF_INET) {
250 const struct sockaddr_in *sin1, *sin2;
251
252 sin1 = (const struct sockaddr_in *) ss;
253 sin2 = (const struct sockaddr_in *) ai->ai_addr;
Wayne Davison4cfa6152004-04-01 20:56:50 +0000254
Martin Pool6780f722002-01-25 02:45:09 +0000255 return memcmp(&sin1->sin_addr, &sin2->sin_addr,
256 sizeof sin1->sin_addr);
Martin Pool39e01d22002-01-25 02:43:35 +0000257 }
Wayne Davison2b284ee2004-04-01 21:08:24 +0000258
Wayne Davison4f5b0752005-02-14 00:53:43 +0000259#ifdef INET6
Wayne Davison2b284ee2004-04-01 21:08:24 +0000260 if (ss_family == AF_INET6) {
Martin Pool6780f722002-01-25 02:45:09 +0000261 const struct sockaddr_in6 *sin1, *sin2;
262
263 sin1 = (const struct sockaddr_in6 *) ss;
264 sin2 = (const struct sockaddr_in6 *) ai->ai_addr;
David Dykstrabc2b4962003-01-09 21:14:10 +0000265
Wayne Davisonc1e72172004-04-01 20:53:39 +0000266 if (ai->ai_addrlen < sizeof (struct sockaddr_in6)) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000267 rprintf(FLOG, "%s: too short sockaddr_in6; length=%d\n",
David Dykstrabc2b4962003-01-09 21:14:10 +0000268 fn, ai->ai_addrlen);
269 return 1;
270 }
271
272 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
273 sizeof sin1->sin6_addr))
274 return 1;
275
Wayne Davison4f5b0752005-02-14 00:53:43 +0000276#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
David Dykstrabc2b4962003-01-09 21:14:10 +0000277 if (sin1->sin6_scope_id != sin2->sin6_scope_id)
278 return 1;
279#endif
280 return 0;
Martin Pool974f27e2002-01-25 02:29:53 +0000281 }
Martin Pool39e01d22002-01-25 02:43:35 +0000282#endif /* INET6 */
Wayne Davison2b284ee2004-04-01 21:08:24 +0000283
284 /* don't know */
285 return 1;
Martin Pool974f27e2002-01-25 02:29:53 +0000286}
287
288
289/**
Martin Poolaf32f692002-01-25 02:15:58 +0000290 * Do a forward lookup on @p name_buf and make sure it corresponds to
291 * @p ss -- otherwise we may be being spoofed. If we suspect we are,
292 * then we don't abort the connection but just emit a warning, and
293 * change @p name_buf to be "UNKNOWN".
Martin Pool6c92af22002-04-03 02:33:42 +0000294 *
295 * We don't do anything with the service when checking the name,
296 * because it doesn't seem that it could be spoofed in any way, and
297 * getaddrinfo on random service names seems to cause problems on AIX.
Martin Poolaf32f692002-01-25 02:15:58 +0000298 **/
Martin Pool0cd2f402002-01-25 02:13:04 +0000299int check_name(int fd,
300 const struct sockaddr_storage *ss,
Wayne Davison55410362006-10-13 23:17:27 +0000301 char *name_buf, size_t name_buf_size)
Martin Pool0cd2f402002-01-25 02:13:04 +0000302{
303 struct addrinfo hints, *res, *res0;
304 int error;
305 int ss_family = get_sockaddr_family(ss);
306
Wayne Davisonc1e72172004-04-01 20:53:39 +0000307 memset(&hints, 0, sizeof hints);
Martin Pool39e01d22002-01-25 02:43:35 +0000308 hints.ai_family = ss_family;
309 hints.ai_flags = AI_CANONNAME;
Martin Pool0cd2f402002-01-25 02:13:04 +0000310 hints.ai_socktype = SOCK_STREAM;
Martin Pool6c92af22002-04-03 02:33:42 +0000311 error = getaddrinfo(name_buf, NULL, &hints, &res0);
Martin Pool0cd2f402002-01-25 02:13:04 +0000312 if (error) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000313 rprintf(FLOG, "forward name lookup for %s failed: %s\n",
Martin Poolaf32f692002-01-25 02:15:58 +0000314 name_buf, gai_strerror(error));
Wayne Davison55410362006-10-13 23:17:27 +0000315 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool0cd2f402002-01-25 02:13:04 +0000316 return error;
317 }
318
Martin Pool974f27e2002-01-25 02:29:53 +0000319 /* Given all these results, we expect that one of them will be
320 * the same as ss. The comparison is a bit complicated. */
Martin Pool0cd2f402002-01-25 02:13:04 +0000321 for (res = res0; res; res = res->ai_next) {
Martin Pool974f27e2002-01-25 02:29:53 +0000322 if (!compare_addrinfo_sockaddr(res, ss))
323 break; /* OK, identical */
Martin Pool0cd2f402002-01-25 02:13:04 +0000324 }
325
326 if (!res0) {
327 /* We hit the end of the list without finding an
328 * address that was the same as ss. */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000329 rprintf(FLOG, "no known address for \"%s\": "
330 "spoofed address?\n", name_buf);
Wayne Davison55410362006-10-13 23:17:27 +0000331 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool974f27e2002-01-25 02:29:53 +0000332 } else if (res == NULL) {
Martin Pool0cd2f402002-01-25 02:13:04 +0000333 /* We hit the end of the list without finding an
334 * address that was the same as ss. */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000335 rprintf(FLOG, "%s is not a known address for \"%s\": "
336 "spoofed address?\n", client_addr(fd), name_buf);
Wayne Davison55410362006-10-13 23:17:27 +0000337 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool0cd2f402002-01-25 02:13:04 +0000338 }
339
340 freeaddrinfo(res0);
341 return 0;
342}