Fix client_name to work on when INET6 is enabled but using a 4-part IPv4
[rsync/rsync.git] / clientname.c
CommitLineData
0cd2f407
MP
1/* -*- c-file-style: "linux" -*-
2
3 rsync -- fast file replication program
4
5 Copyright (C) 1992-2001 by Andrew Tridgell <tridge@samba.org>
6 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/**
24 * @file clientname.c
25 *
26 * Functions for looking up the remote name or addr of a socket.
27 *
28 * This file is now converted to use the new-style getaddrinfo()
29 * interface, which supports IPv6 but is also supported on recent
30 * IPv4-only machines. On systems that don't have that interface, we
31 * emulate it using the KAME implementation.
32 **/
33
34#include "rsync.h"
35
36static const char default_name[] = "UNKNOWN";
37
38
39/**
40 * Return the IP addr of the client as a string
41 **/
42char *client_addr(int fd)
43{
44 struct sockaddr_storage ss;
45 socklen_t length = sizeof ss;
09021eab 46 char *ssh_client, *p;
0cd2f407
MP
47 static char addr_buf[100];
48 static int initialised;
49
50 if (initialised) return addr_buf;
51
52 initialised = 1;
53
a6d8c3f3 54 if ((ssh_client = getenv("SSH_CLIENT")) != NULL) {
09021eab
DD
55 strlcpy(addr_buf, ssh_client, sizeof(addr_buf));
56 /* truncate SSH_CLIENT to just IP address */
57 p = strchr(addr_buf, ' ');
58 if (p)
59 *p = '\0';
60 else
61 strlcpy(addr_buf, "0.0.0.0", sizeof("0.0.0.0"));
62 } else
63 client_sockaddr(fd, &ss, &length);
0cd2f407
MP
64
65 getnameinfo((struct sockaddr *)&ss, length,
09021eab
DD
66 addr_buf, sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
67
0cd2f407
MP
68 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.
6c92af20
MP
87 *
88 * After translation from sockaddr to name we do a forward lookup to
89 * make sure nobody is spoofing PTR records.
0cd2f407
MP
90 **/
91char *client_name(int fd)
92{
0cd2f407
MP
93 static char name_buf[100];
94 static char port_buf[100];
95 static int initialised;
1e736b8f
DD
96 struct sockaddr_storage *ssp;
97 socklen_t ss_len;
0cd2f407
MP
98
99 if (initialised) return name_buf;
100
101 strcpy(name_buf, default_name);
102 initialised = 1;
103
09021eab
DD
104 if (getenv("SSH_CLIENT") != NULL) {
105 /* Look up name of IP address given in $SSH_CLIENT */
1e736b8f 106 char *addr = client_addr(fd);
09021eab 107 struct sockaddr_in sin;
a6d8c3f3 108#ifdef INET6
1e736b8f
DD
109 int dots = 0;
110 char *p;
111 struct sockaddr_in6 sin6;
112
113 for (p = addr; *p && (dots < 3); p++) {
114 if (*p == '.')
115 dots++;
116 }
117 if (dots > 3) {
118 /* more than 4 parts to IP address, must be ipv6 */
119 ssp = (struct sockaddr_storage *) &sin6;
120 ss_len = sizeof sin6;
121 memset(ssp, 0, ss_len);
122 inet_pton(AF_INET6, addr, &sin6.sin6_addr.s6_addr);
123 sin6.sin6_family = AF_INET6;
124 } else
a6d8c3f3 125#endif
1e736b8f
DD
126 {
127 ssp = (struct sockaddr_storage *) &sin;
128 ss_len = sizeof sin;
129 memset(ssp, 0, ss_len);
130 inet_pton(AF_INET, addr, &sin.sin_addr.s_addr);
131 sin.sin_family = AF_INET;
132 }
09021eab 133
09021eab
DD
134 } else {
135 struct sockaddr_storage ss;
1e736b8f
DD
136
137 ss_len = sizeof ss;
138 ssp = &ss;
09021eab
DD
139
140 client_sockaddr(fd, &ss, &ss_len);
141
09021eab 142 }
0cd2f407 143
1e736b8f
DD
144 if (!lookup_name(fd, ssp, ss_len, name_buf, sizeof name_buf,
145 port_buf, sizeof port_buf))
146 check_name(fd, ssp, name_buf);
147
0cd2f407
MP
148 return name_buf;
149}
150
151
152
153/**
af32f69e
MP
154 * Get the sockaddr for the client.
155 *
156 * If it comes in as an ipv4 address mapped into IPv6 format then we
157 * convert it back to a regular IPv4.
0cd2f407
MP
158 **/
159void client_sockaddr(int fd,
160 struct sockaddr_storage *ss,
161 socklen_t *ss_len)
162{
163 if (getpeername(fd, (struct sockaddr *) ss, ss_len)) {
164 /* FIXME: Can we really not continue? */
165 rprintf(FERROR, RSYNC_NAME ": getpeername on fd%d failed: %s\n",
166 fd, strerror(errno));
167 exit_cleanup(RERR_SOCKETIO);
168 }
169
170#ifdef INET6
171 if (get_sockaddr_family(ss) == AF_INET6 &&
172 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
182 memcpy(&sin6, ss, sizeof(sin6));
183 sin = (struct sockaddr_in *)ss;
184 memset(sin, 0, sizeof(*sin));
185 sin->sin_family = AF_INET;
186 *ss_len = sizeof(struct sockaddr_in);
187#ifdef HAVE_SOCKADDR_LEN
188 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],
196 sizeof(sin->sin_addr));
197 }
198#endif
199}
200
201
202/**
203 * Look up a name from @p ss into @p name_buf.
6c92af20
MP
204 *
205 * @param fd file descriptor for client socket.
0cd2f407
MP
206 **/
207int lookup_name(int fd, const struct sockaddr_storage *ss,
208 socklen_t ss_len,
209 char *name_buf, size_t name_buf_len,
210 char *port_buf, size_t port_buf_len)
211{
212 int name_err;
213
214 /* reverse lookup */
215 name_err = getnameinfo((struct sockaddr *) ss, ss_len,
216 name_buf, name_buf_len,
217 port_buf, port_buf_len,
218 NI_NAMEREQD | NI_NUMERICSERV);
219 if (name_err != 0) {
220 strcpy(name_buf, default_name);
221 rprintf(FERROR, RSYNC_NAME ": name lookup failed for %s: %s\n",
222 client_addr(fd),
223 gai_strerror(name_err));
224 return name_err;
225 }
226
227 return 0;
228}
229
230
231
974f27e7
MP
232/**
233 * Compare an addrinfo from the resolver to a sockinfo.
234 *
235 * Like strcmp, returns 0 for identical.
236 **/
237int compare_addrinfo_sockaddr(const struct addrinfo *ai,
238 const struct sockaddr_storage *ss)
239{
240 int ss_family = get_sockaddr_family(ss);
241 const char fn[] = "compare_addrinfo_sockaddr";
242
243 if (ai->ai_family != ss_family) {
244 rprintf(FERROR,
245 "%s: response family %d != %d\n",
246 fn, ai->ai_family, ss_family);
247 return 1;
248 }
249
39e01d2d
MP
250 /* 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;
256
6780f720
MP
257 return memcmp(&sin1->sin_addr, &sin2->sin_addr,
258 sizeof sin1->sin_addr);
39e01d2d 259 }
974f27e7 260#ifdef INET6
39e01d2d 261 else if (ss_family == AF_INET6) {
6780f720
MP
262 const struct sockaddr_in6 *sin1, *sin2;
263
264 sin1 = (const struct sockaddr_in6 *) ss;
265 sin2 = (const struct sockaddr_in6 *) ai->ai_addr;
266
267 return memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
268 sizeof sin1->sin6_addr);
39e01d2d
MP
269 }
270#endif /* INET6 */
271 else {
272 /* don't know */
974f27e7
MP
273 return 1;
274 }
275}
276
277
af32f69e
MP
278/**
279 * Do a forward lookup on @p name_buf and make sure it corresponds to
280 * @p ss -- otherwise we may be being spoofed. If we suspect we are,
281 * then we don't abort the connection but just emit a warning, and
282 * change @p name_buf to be "UNKNOWN".
6c92af20
MP
283 *
284 * We don't do anything with the service when checking the name,
285 * because it doesn't seem that it could be spoofed in any way, and
286 * getaddrinfo on random service names seems to cause problems on AIX.
af32f69e 287 **/
0cd2f407
MP
288int check_name(int fd,
289 const struct sockaddr_storage *ss,
6c92af20 290 char *name_buf)
0cd2f407
MP
291{
292 struct addrinfo hints, *res, *res0;
293 int error;
294 int ss_family = get_sockaddr_family(ss);
295
296 memset(&hints, 0, sizeof(hints));
39e01d2d
MP
297 hints.ai_family = ss_family;
298 hints.ai_flags = AI_CANONNAME;
0cd2f407 299 hints.ai_socktype = SOCK_STREAM;
6c92af20 300 error = getaddrinfo(name_buf, NULL, &hints, &res0);
0cd2f407
MP
301 if (error) {
302 rprintf(FERROR,
af32f69e
MP
303 RSYNC_NAME ": forward name lookup for %s failed: %s\n",
304 name_buf, gai_strerror(error));
0cd2f407
MP
305 strcpy(name_buf, default_name);
306 return error;
307 }
308
309
974f27e7
MP
310 /* Given all these results, we expect that one of them will be
311 * the same as ss. The comparison is a bit complicated. */
0cd2f407 312 for (res = res0; res; res = res->ai_next) {
974f27e7
MP
313 if (!compare_addrinfo_sockaddr(res, ss))
314 break; /* OK, identical */
0cd2f407
MP
315 }
316
317 if (!res0) {
318 /* We hit the end of the list without finding an
319 * address that was the same as ss. */
320 rprintf(FERROR, RSYNC_NAME
321 ": no known address for \"%s\": "
322 "spoofed address?\n",
323 name_buf);
324 strcpy(name_buf, default_name);
974f27e7 325 } else if (res == NULL) {
0cd2f407
MP
326 /* We hit the end of the list without finding an
327 * address that was the same as ss. */
328 rprintf(FERROR, RSYNC_NAME
329 ": %s is not a known address for \"%s\": "
330 "spoofed address?\n",
331 client_addr(fd),
332 name_buf);
333 strcpy(name_buf, default_name);
334 }
335
336 freeaddrinfo(res0);
337 return 0;
338}
339