blob: 3d9cdf19550ad309e797933e54b1a6bde04ba373 [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>
6 * Copyright (C) 2002, 2003, 2004 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
Wayne Davisone7c67062006-04-25 23:51:12 +000018 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
Wayne Davison0f78b812006-04-25 20:23:34 +000021 */
22
23/*
Martin Pool0cd2f402002-01-25 02:13:04 +000024 * This file is now converted to use the new-style getaddrinfo()
25 * interface, which supports IPv6 but is also supported on recent
26 * IPv4-only machines. On systems that don't have that interface, we
27 * emulate it using the KAME implementation.
Wayne Davison0f78b812006-04-25 20:23:34 +000028 */
Martin Pool0cd2f402002-01-25 02:13:04 +000029
30#include "rsync.h"
31
32static const char default_name[] = "UNKNOWN";
David Dykstra542ad672002-08-02 15:39:43 +000033extern int am_server;
Martin Pool0cd2f402002-01-25 02:13:04 +000034
35
36/**
Wayne Davisonc1e72172004-04-01 20:53:39 +000037 * Return the IP addr of the client as a string
Martin Pool0cd2f402002-01-25 02:13:04 +000038 **/
39char *client_addr(int fd)
40{
Martin Pool0cd2f402002-01-25 02:13:04 +000041 static char addr_buf[100];
42 static int initialised;
Wayne Davisonb2ef4f62004-06-03 17:20:21 +000043 struct sockaddr_storage ss;
44 socklen_t length = sizeof ss;
45 char *ssh_info, *p;
Martin Pool0cd2f402002-01-25 02:13:04 +000046
Wayne Davison4cfa6152004-04-01 20:56:50 +000047 if (initialised)
48 return addr_buf;
Martin Pool0cd2f402002-01-25 02:13:04 +000049
50 initialised = 1;
51
Wayne Davison706c7532004-04-01 21:39:35 +000052 if (am_server) { /* daemon over --rsh mode */
Wayne Davison55410362006-10-13 23:17:27 +000053 strlcpy(addr_buf, "0.0.0.0", sizeof addr_buf);
Wayne Davisonb2ef4f62004-06-03 17:20:21 +000054 if ((ssh_info = getenv("SSH_CONNECTION")) != NULL
55 || (ssh_info = getenv("SSH_CLIENT")) != NULL
56 || (ssh_info = getenv("SSH2_CLIENT")) != NULL) {
57 strlcpy(addr_buf, ssh_info, sizeof addr_buf);
58 /* Truncate the value to just the IP address. */
59 if ((p = strchr(addr_buf, ' ')) != NULL)
60 *p = '\0';
David Dykstra542ad672002-08-02 15:39:43 +000061 }
Wayne Davison3b5f6212002-08-02 17:11:39 +000062 } else {
David Dykstra09021ea2002-08-01 19:17:00 +000063 client_sockaddr(fd, &ss, &length);
Wayne Davison3b5f6212002-08-02 17:11:39 +000064 getnameinfo((struct sockaddr *)&ss, length,
65 addr_buf, sizeof addr_buf, NULL, 0, NI_NUMERICHOST);
66 }
David Dykstra09021ea2002-08-01 19:17:00 +000067
Martin Pool0cd2f402002-01-25 02:13:04 +000068 return addr_buf;
69}
70
71
72static int get_sockaddr_family(const struct sockaddr_storage *ss)
73{
74 return ((struct sockaddr *) ss)->sa_family;
75}
76
77
78/**
79 * Return the DNS name of the client.
80 *
81 * The name is statically cached so that repeated lookups are quick,
82 * so there is a limit of one lookup per customer.
83 *
84 * If anything goes wrong, including the name->addr->name check, then
85 * we just use "UNKNOWN", so you can use that value in hosts allow
86 * lines.
Martin Pool6c92af22002-04-03 02:33:42 +000087 *
88 * After translation from sockaddr to name we do a forward lookup to
89 * make sure nobody is spoofing PTR records.
Martin Pool0cd2f402002-01-25 02:13:04 +000090 **/
91char *client_name(int fd)
92{
Martin Pool0cd2f402002-01-25 02:13:04 +000093 static char name_buf[100];
94 static char port_buf[100];
95 static int initialised;
Wayne Davison706c7532004-04-01 21:39:35 +000096 struct sockaddr_storage ss;
David Dykstra1e736b82002-08-02 15:05:03 +000097 socklen_t ss_len;
Martin Pool0cd2f402002-01-25 02:13:04 +000098
Wayne Davison4cfa6152004-04-01 20:56:50 +000099 if (initialised)
100 return name_buf;
Martin Pool0cd2f402002-01-25 02:13:04 +0000101
Wayne Davison55410362006-10-13 23:17:27 +0000102 strlcpy(name_buf, default_name, sizeof name_buf);
Martin Pool0cd2f402002-01-25 02:13:04 +0000103 initialised = 1;
104
Wayne Davison706c7532004-04-01 21:39:35 +0000105 memset(&ss, 0, sizeof ss);
106
Wayne Davison2b284ee2004-04-01 21:08:24 +0000107 if (am_server) { /* daemon over --rsh mode */
David Dykstra1e736b82002-08-02 15:05:03 +0000108 char *addr = client_addr(fd);
Wayne Davison706c7532004-04-01 21:39:35 +0000109 struct addrinfo hint, *answer;
110 int err;
111
112 memset(&hint, 0, sizeof hint);
113
Wayne Davison6a6d2112004-04-30 16:10:45 +0000114#ifdef AI_NUMERICHOST
Wayne Davison706c7532004-04-01 21:39:35 +0000115 hint.ai_flags = AI_NUMERICHOST;
Wayne Davison6a6d2112004-04-30 16:10:45 +0000116#endif
Wayne Davison706c7532004-04-01 21:39:35 +0000117 hint.ai_socktype = SOCK_STREAM;
118
119 if ((err = getaddrinfo(addr, NULL, &hint, &answer)) != 0) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000120 rprintf(FLOG, "malformed address %s: %s\n",
Wayne Davison706c7532004-04-01 21:39:35 +0000121 addr, gai_strerror(err));
122 return name_buf;
123 }
124
125 switch (answer->ai_family) {
126 case AF_INET:
127 ss_len = sizeof (struct sockaddr_in);
128 memcpy(&ss, answer->ai_addr, ss_len);
129 break;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000130#ifdef INET6
Wayne Davison706c7532004-04-01 21:39:35 +0000131 case AF_INET6:
132 ss_len = sizeof (struct sockaddr_in6);
133 memcpy(&ss, answer->ai_addr, ss_len);
134 break;
David Dykstra1e736b82002-08-02 15:05:03 +0000135#endif
Wayne Davison55410362006-10-13 23:17:27 +0000136 default:
137 exit_cleanup(RERR_SOCKETIO);
David Dykstra1e736b82002-08-02 15:05:03 +0000138 }
Wayne Davison706c7532004-04-01 21:39:35 +0000139 freeaddrinfo(answer);
David Dykstra09021ea2002-08-01 19:17:00 +0000140 } else {
David Dykstra1e736b82002-08-02 15:05:03 +0000141 ss_len = sizeof ss;
David Dykstra09021ea2002-08-01 19:17:00 +0000142 client_sockaddr(fd, &ss, &ss_len);
David Dykstra09021ea2002-08-01 19:17:00 +0000143 }
Martin Pool0cd2f402002-01-25 02:13:04 +0000144
Wayne Davisonfde045c2004-09-24 16:39:41 +0000145 if (lookup_name(fd, &ss, ss_len, name_buf, sizeof name_buf,
146 port_buf, sizeof port_buf) == 0)
Wayne Davison55410362006-10-13 23:17:27 +0000147 check_name(fd, &ss, name_buf, sizeof name_buf);
David Dykstra1e736b82002-08-02 15:05:03 +0000148
Martin Pool0cd2f402002-01-25 02:13:04 +0000149 return name_buf;
150}
151
152
153
154/**
Martin Poolaf32f692002-01-25 02:15:58 +0000155 * Get the sockaddr for the client.
156 *
157 * If it comes in as an ipv4 address mapped into IPv6 format then we
158 * convert it back to a regular IPv4.
Martin Pool0cd2f402002-01-25 02:13:04 +0000159 **/
160void client_sockaddr(int fd,
161 struct sockaddr_storage *ss,
162 socklen_t *ss_len)
163{
Wayne Davisonc1e72172004-04-01 20:53:39 +0000164 memset(ss, 0, sizeof *ss);
David Dykstrabc2b4962003-01-09 21:14:10 +0000165
Martin Pool0cd2f402002-01-25 02:13:04 +0000166 if (getpeername(fd, (struct sockaddr *) ss, ss_len)) {
167 /* FIXME: Can we really not continue? */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000168 rsyserr(FLOG, errno, "getpeername on fd%d failed", fd);
Martin Pool0cd2f402002-01-25 02:13:04 +0000169 exit_cleanup(RERR_SOCKETIO);
170 }
171
Wayne Davison4f5b0752005-02-14 00:53:43 +0000172#ifdef INET6
Wayne Davison2b284ee2004-04-01 21:08:24 +0000173 if (get_sockaddr_family(ss) == AF_INET6 &&
Martin Pool0cd2f402002-01-25 02:13:04 +0000174 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)ss)->sin6_addr)) {
175 /* OK, so ss is in the IPv6 family, but it is really
176 * an IPv4 address: something like
177 * "::ffff:10.130.1.2". If we use it as-is, then the
178 * reverse lookup might fail or perhaps something else
179 * bad might happen. So instead we convert it to an
180 * equivalent address in the IPv4 address family. */
181 struct sockaddr_in6 sin6;
182 struct sockaddr_in *sin;
183
Wayne Davisonc1e72172004-04-01 20:53:39 +0000184 memcpy(&sin6, ss, sizeof sin6);
Martin Pool0cd2f402002-01-25 02:13:04 +0000185 sin = (struct sockaddr_in *)ss;
Wayne Davisonc1e72172004-04-01 20:53:39 +0000186 memset(sin, 0, sizeof *sin);
Martin Pool0cd2f402002-01-25 02:13:04 +0000187 sin->sin_family = AF_INET;
Wayne Davisonc1e72172004-04-01 20:53:39 +0000188 *ss_len = sizeof (struct sockaddr_in);
Wayne Davison4f5b0752005-02-14 00:53:43 +0000189#ifdef HAVE_SOCKADDR_IN_LEN
Martin Pool0cd2f402002-01-25 02:13:04 +0000190 sin->sin_len = *ss_len;
191#endif
192 sin->sin_port = sin6.sin6_port;
193
194 /* There is a macro to extract the mapped part
195 * (IN6_V4MAPPED_TO_SINADDR ?), but it does not seem
196 * to be present in the Linux headers. */
197 memcpy(&sin->sin_addr, &sin6.sin6_addr.s6_addr[12],
Wayne Davisonc1e72172004-04-01 20:53:39 +0000198 sizeof sin->sin_addr);
Wayne Davison2b284ee2004-04-01 21:08:24 +0000199 }
Martin Pool0cd2f402002-01-25 02:13:04 +0000200#endif
201}
202
203
204/**
205 * Look up a name from @p ss into @p name_buf.
Martin Pool6c92af22002-04-03 02:33:42 +0000206 *
207 * @param fd file descriptor for client socket.
Martin Pool0cd2f402002-01-25 02:13:04 +0000208 **/
209int lookup_name(int fd, const struct sockaddr_storage *ss,
210 socklen_t ss_len,
Wayne Davison55410362006-10-13 23:17:27 +0000211 char *name_buf, size_t name_buf_size,
212 char *port_buf, size_t port_buf_size)
Martin Pool0cd2f402002-01-25 02:13:04 +0000213{
214 int name_err;
Wayne Davison4cfa6152004-04-01 20:56:50 +0000215
Martin Pool0cd2f402002-01-25 02:13:04 +0000216 /* reverse lookup */
217 name_err = getnameinfo((struct sockaddr *) ss, ss_len,
Wayne Davison55410362006-10-13 23:17:27 +0000218 name_buf, name_buf_size,
219 port_buf, port_buf_size,
Martin Pool0cd2f402002-01-25 02:13:04 +0000220 NI_NAMEREQD | NI_NUMERICSERV);
221 if (name_err != 0) {
Wayne Davison55410362006-10-13 23:17:27 +0000222 strlcpy(name_buf, default_name, name_buf_size);
Wayne Davisonfde045c2004-09-24 16:39:41 +0000223 rprintf(FLOG, "name lookup failed for %s: %s\n",
224 client_addr(fd), gai_strerror(name_err));
Martin Pool0cd2f402002-01-25 02:13:04 +0000225 return name_err;
226 }
227
228 return 0;
229}
230
231
232
Martin Poolaf32f692002-01-25 02:15:58 +0000233/**
Martin Pool974f27e2002-01-25 02:29:53 +0000234 * Compare an addrinfo from the resolver to a sockinfo.
235 *
236 * Like strcmp, returns 0 for identical.
237 **/
238int compare_addrinfo_sockaddr(const struct addrinfo *ai,
239 const struct sockaddr_storage *ss)
240{
241 int ss_family = get_sockaddr_family(ss);
242 const char fn[] = "compare_addrinfo_sockaddr";
Wayne Davisonc1e72172004-04-01 20:53:39 +0000243
Martin Pool974f27e2002-01-25 02:29:53 +0000244 if (ai->ai_family != ss_family) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000245 rprintf(FLOG, "%s: response family %d != %d\n",
Martin Pool974f27e2002-01-25 02:29:53 +0000246 fn, ai->ai_family, ss_family);
247 return 1;
248 }
249
Martin Pool39e01d22002-01-25 02:43:35 +0000250 /* The comparison method depends on the particular AF. */
251 if (ss_family == AF_INET) {
252 const struct sockaddr_in *sin1, *sin2;
253
254 sin1 = (const struct sockaddr_in *) ss;
255 sin2 = (const struct sockaddr_in *) ai->ai_addr;
Wayne Davison4cfa6152004-04-01 20:56:50 +0000256
Martin Pool6780f722002-01-25 02:45:09 +0000257 return memcmp(&sin1->sin_addr, &sin2->sin_addr,
258 sizeof sin1->sin_addr);
Martin Pool39e01d22002-01-25 02:43:35 +0000259 }
Wayne Davison2b284ee2004-04-01 21:08:24 +0000260
Wayne Davison4f5b0752005-02-14 00:53:43 +0000261#ifdef INET6
Wayne Davison2b284ee2004-04-01 21:08:24 +0000262 if (ss_family == AF_INET6) {
Martin Pool6780f722002-01-25 02:45:09 +0000263 const struct sockaddr_in6 *sin1, *sin2;
264
265 sin1 = (const struct sockaddr_in6 *) ss;
266 sin2 = (const struct sockaddr_in6 *) ai->ai_addr;
David Dykstrabc2b4962003-01-09 21:14:10 +0000267
Wayne Davisonc1e72172004-04-01 20:53:39 +0000268 if (ai->ai_addrlen < sizeof (struct sockaddr_in6)) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000269 rprintf(FLOG, "%s: too short sockaddr_in6; length=%d\n",
David Dykstrabc2b4962003-01-09 21:14:10 +0000270 fn, ai->ai_addrlen);
271 return 1;
272 }
273
274 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
275 sizeof sin1->sin6_addr))
276 return 1;
277
Wayne Davison4f5b0752005-02-14 00:53:43 +0000278#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
David Dykstrabc2b4962003-01-09 21:14:10 +0000279 if (sin1->sin6_scope_id != sin2->sin6_scope_id)
280 return 1;
281#endif
282 return 0;
Martin Pool974f27e2002-01-25 02:29:53 +0000283 }
Martin Pool39e01d22002-01-25 02:43:35 +0000284#endif /* INET6 */
Wayne Davison2b284ee2004-04-01 21:08:24 +0000285
286 /* don't know */
287 return 1;
Martin Pool974f27e2002-01-25 02:29:53 +0000288}
289
290
291/**
Martin Poolaf32f692002-01-25 02:15:58 +0000292 * Do a forward lookup on @p name_buf and make sure it corresponds to
293 * @p ss -- otherwise we may be being spoofed. If we suspect we are,
294 * then we don't abort the connection but just emit a warning, and
295 * change @p name_buf to be "UNKNOWN".
Martin Pool6c92af22002-04-03 02:33:42 +0000296 *
297 * We don't do anything with the service when checking the name,
298 * because it doesn't seem that it could be spoofed in any way, and
299 * getaddrinfo on random service names seems to cause problems on AIX.
Martin Poolaf32f692002-01-25 02:15:58 +0000300 **/
Martin Pool0cd2f402002-01-25 02:13:04 +0000301int check_name(int fd,
302 const struct sockaddr_storage *ss,
Wayne Davison55410362006-10-13 23:17:27 +0000303 char *name_buf, size_t name_buf_size)
Martin Pool0cd2f402002-01-25 02:13:04 +0000304{
305 struct addrinfo hints, *res, *res0;
306 int error;
307 int ss_family = get_sockaddr_family(ss);
308
Wayne Davisonc1e72172004-04-01 20:53:39 +0000309 memset(&hints, 0, sizeof hints);
Martin Pool39e01d22002-01-25 02:43:35 +0000310 hints.ai_family = ss_family;
311 hints.ai_flags = AI_CANONNAME;
Martin Pool0cd2f402002-01-25 02:13:04 +0000312 hints.ai_socktype = SOCK_STREAM;
Martin Pool6c92af22002-04-03 02:33:42 +0000313 error = getaddrinfo(name_buf, NULL, &hints, &res0);
Martin Pool0cd2f402002-01-25 02:13:04 +0000314 if (error) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000315 rprintf(FLOG, "forward name lookup for %s failed: %s\n",
Martin Poolaf32f692002-01-25 02:15:58 +0000316 name_buf, gai_strerror(error));
Wayne Davison55410362006-10-13 23:17:27 +0000317 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool0cd2f402002-01-25 02:13:04 +0000318 return error;
319 }
320
Martin Pool974f27e2002-01-25 02:29:53 +0000321 /* Given all these results, we expect that one of them will be
322 * the same as ss. The comparison is a bit complicated. */
Martin Pool0cd2f402002-01-25 02:13:04 +0000323 for (res = res0; res; res = res->ai_next) {
Martin Pool974f27e2002-01-25 02:29:53 +0000324 if (!compare_addrinfo_sockaddr(res, ss))
325 break; /* OK, identical */
Martin Pool0cd2f402002-01-25 02:13:04 +0000326 }
327
328 if (!res0) {
329 /* We hit the end of the list without finding an
330 * address that was the same as ss. */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000331 rprintf(FLOG, "no known address for \"%s\": "
332 "spoofed address?\n", name_buf);
Wayne Davison55410362006-10-13 23:17:27 +0000333 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool974f27e2002-01-25 02:29:53 +0000334 } else if (res == NULL) {
Martin Pool0cd2f402002-01-25 02:13:04 +0000335 /* We hit the end of the list without finding an
336 * address that was the same as ss. */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000337 rprintf(FLOG, "%s is not a known address for \"%s\": "
338 "spoofed address?\n", client_addr(fd), name_buf);
Wayne Davison55410362006-10-13 23:17:27 +0000339 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool0cd2f402002-01-25 02:13:04 +0000340 }
341
342 freeaddrinfo(res0);
343 return 0;
344}