blob: 935d2d61f85c143aaf45d48431c531e066669020 [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 Davison7e1a9c42013-01-19 11:05:53 -08006 * Copyright (C) 2002-2013 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00007 *
8 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
Wayne Davison0f78b812006-04-25 20:23:34 +000012 *
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
Wayne Davison4fd842f2007-07-07 05:33:14 +000019 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000020 */
21
22/*
Martin Pool0cd2f402002-01-25 02:13:04 +000023 * This file is now converted to use the new-style getaddrinfo()
24 * interface, which supports IPv6 but is also supported on recent
25 * IPv4-only machines. On systems that don't have that interface, we
26 * emulate it using the KAME implementation.
Wayne Davison0f78b812006-04-25 20:23:34 +000027 */
Martin Pool0cd2f402002-01-25 02:13:04 +000028
29#include "rsync.h"
30
31static const char default_name[] = "UNKNOWN";
David Dykstra542ad672002-08-02 15:39:43 +000032extern int am_server;
Martin Pool0cd2f402002-01-25 02:13:04 +000033
34
35/**
Wayne Davisonc1e72172004-04-01 20:53:39 +000036 * Return the IP addr of the client as a string
Martin Pool0cd2f402002-01-25 02:13:04 +000037 **/
38char *client_addr(int fd)
39{
Martin Pool0cd2f402002-01-25 02:13:04 +000040 static char addr_buf[100];
41 static int initialised;
Wayne Davisonb2ef4f62004-06-03 17:20:21 +000042 struct sockaddr_storage ss;
43 socklen_t length = sizeof ss;
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 Davison70d4a942013-06-15 14:40:52 -070051 char *env_str;
Wayne Davison55410362006-10-13 23:17:27 +000052 strlcpy(addr_buf, "0.0.0.0", sizeof addr_buf);
Wayne Davison99c95202013-07-12 15:24:58 -070053 if ((env_str = getenv("REMOTE_HOST")) != NULL
54 || (env_str = getenv("SSH_CONNECTION")) != NULL
Wayne Davison70d4a942013-06-15 14:40:52 -070055 || (env_str = getenv("SSH_CLIENT")) != NULL
Wayne Davison99c95202013-07-12 15:24:58 -070056 || (env_str = getenv("SSH2_CLIENT")) != NULL) {
Wayne Davison70d4a942013-06-15 14:40:52 -070057 char *p;
58 strlcpy(addr_buf, env_str, sizeof addr_buf);
Wayne Davisonb2ef4f62004-06-03 17:20:21 +000059 /* Truncate the value to just the IP address. */
60 if ((p = strchr(addr_buf, ' ')) != NULL)
61 *p = '\0';
David Dykstra542ad672002-08-02 15:39:43 +000062 }
Wayne Davison3b5f6212002-08-02 17:11:39 +000063 } else {
David Dykstra09021ea2002-08-01 19:17:00 +000064 client_sockaddr(fd, &ss, &length);
Wayne Davison3b5f6212002-08-02 17:11:39 +000065 getnameinfo((struct sockaddr *)&ss, length,
66 addr_buf, sizeof addr_buf, NULL, 0, NI_NUMERICHOST);
67 }
David Dykstra09021ea2002-08-01 19:17:00 +000068
Martin Pool0cd2f402002-01-25 02:13:04 +000069 return addr_buf;
70}
71
72
73static int get_sockaddr_family(const struct sockaddr_storage *ss)
74{
75 return ((struct sockaddr *) ss)->sa_family;
76}
77
78
79/**
80 * Return the DNS name of the client.
81 *
82 * The name is statically cached so that repeated lookups are quick,
83 * so there is a limit of one lookup per customer.
84 *
85 * If anything goes wrong, including the name->addr->name check, then
86 * we just use "UNKNOWN", so you can use that value in hosts allow
87 * lines.
Martin Pool6c92af22002-04-03 02:33:42 +000088 *
89 * After translation from sockaddr to name we do a forward lookup to
90 * make sure nobody is spoofing PTR records.
Martin Pool0cd2f402002-01-25 02:13:04 +000091 **/
92char *client_name(int fd)
93{
Martin Pool0cd2f402002-01-25 02:13:04 +000094 static char name_buf[100];
95 static char port_buf[100];
96 static int initialised;
Wayne Davison706c7532004-04-01 21:39:35 +000097 struct sockaddr_storage ss;
David Dykstra1e736b82002-08-02 15:05:03 +000098 socklen_t ss_len;
Martin Pool0cd2f402002-01-25 02:13:04 +000099
Wayne Davison4cfa6152004-04-01 20:56:50 +0000100 if (initialised)
101 return name_buf;
Martin Pool0cd2f402002-01-25 02:13:04 +0000102
Wayne Davison55410362006-10-13 23:17:27 +0000103 strlcpy(name_buf, default_name, sizeof name_buf);
Martin Pool0cd2f402002-01-25 02:13:04 +0000104 initialised = 1;
105
Wayne Davison706c7532004-04-01 21:39:35 +0000106 memset(&ss, 0, sizeof ss);
107
Wayne Davison2b284ee2004-04-01 21:08:24 +0000108 if (am_server) { /* daemon over --rsh mode */
David Dykstra1e736b82002-08-02 15:05:03 +0000109 char *addr = client_addr(fd);
Wayne Davison706c7532004-04-01 21:39:35 +0000110 struct addrinfo hint, *answer;
111 int err;
112
Wayne Davisonea0f0372008-10-11 11:00:51 -0700113 if (strcmp(addr, "0.0.0.0") == 0)
114 return name_buf;
115
Wayne Davison706c7532004-04-01 21:39:35 +0000116 memset(&hint, 0, sizeof hint);
117
Wayne Davison6a6d2112004-04-30 16:10:45 +0000118#ifdef AI_NUMERICHOST
Wayne Davison706c7532004-04-01 21:39:35 +0000119 hint.ai_flags = AI_NUMERICHOST;
Wayne Davison6a6d2112004-04-30 16:10:45 +0000120#endif
Wayne Davison706c7532004-04-01 21:39:35 +0000121 hint.ai_socktype = SOCK_STREAM;
122
123 if ((err = getaddrinfo(addr, NULL, &hint, &answer)) != 0) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000124 rprintf(FLOG, "malformed address %s: %s\n",
Wayne Davison706c7532004-04-01 21:39:35 +0000125 addr, gai_strerror(err));
126 return name_buf;
127 }
128
129 switch (answer->ai_family) {
130 case AF_INET:
131 ss_len = sizeof (struct sockaddr_in);
132 memcpy(&ss, answer->ai_addr, ss_len);
133 break;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000134#ifdef INET6
Wayne Davison706c7532004-04-01 21:39:35 +0000135 case AF_INET6:
136 ss_len = sizeof (struct sockaddr_in6);
137 memcpy(&ss, answer->ai_addr, ss_len);
138 break;
David Dykstra1e736b82002-08-02 15:05:03 +0000139#endif
Wayne Davison55410362006-10-13 23:17:27 +0000140 default:
141 exit_cleanup(RERR_SOCKETIO);
David Dykstra1e736b82002-08-02 15:05:03 +0000142 }
Wayne Davison706c7532004-04-01 21:39:35 +0000143 freeaddrinfo(answer);
David Dykstra09021ea2002-08-01 19:17:00 +0000144 } else {
David Dykstra1e736b82002-08-02 15:05:03 +0000145 ss_len = sizeof ss;
David Dykstra09021ea2002-08-01 19:17:00 +0000146 client_sockaddr(fd, &ss, &ss_len);
David Dykstra09021ea2002-08-01 19:17:00 +0000147 }
Martin Pool0cd2f402002-01-25 02:13:04 +0000148
Wayne Davisonfde045c2004-09-24 16:39:41 +0000149 if (lookup_name(fd, &ss, ss_len, name_buf, sizeof name_buf,
150 port_buf, sizeof port_buf) == 0)
Wayne Davison55410362006-10-13 23:17:27 +0000151 check_name(fd, &ss, name_buf, sizeof name_buf);
David Dykstra1e736b82002-08-02 15:05:03 +0000152
Martin Pool0cd2f402002-01-25 02:13:04 +0000153 return name_buf;
154}
155
156
157
158/**
Martin Poolaf32f692002-01-25 02:15:58 +0000159 * Get the sockaddr for the client.
160 *
161 * If it comes in as an ipv4 address mapped into IPv6 format then we
162 * convert it back to a regular IPv4.
Martin Pool0cd2f402002-01-25 02:13:04 +0000163 **/
164void client_sockaddr(int fd,
165 struct sockaddr_storage *ss,
166 socklen_t *ss_len)
167{
Wayne Davisonc1e72172004-04-01 20:53:39 +0000168 memset(ss, 0, sizeof *ss);
David Dykstrabc2b4962003-01-09 21:14:10 +0000169
Martin Pool0cd2f402002-01-25 02:13:04 +0000170 if (getpeername(fd, (struct sockaddr *) ss, ss_len)) {
171 /* FIXME: Can we really not continue? */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000172 rsyserr(FLOG, errno, "getpeername on fd%d failed", fd);
Martin Pool0cd2f402002-01-25 02:13:04 +0000173 exit_cleanup(RERR_SOCKETIO);
174 }
175
Wayne Davison4f5b0752005-02-14 00:53:43 +0000176#ifdef INET6
Wayne Davison2b284ee2004-04-01 21:08:24 +0000177 if (get_sockaddr_family(ss) == AF_INET6 &&
Martin Pool0cd2f402002-01-25 02:13:04 +0000178 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)ss)->sin6_addr)) {
179 /* OK, so ss is in the IPv6 family, but it is really
180 * an IPv4 address: something like
181 * "::ffff:10.130.1.2". If we use it as-is, then the
182 * reverse lookup might fail or perhaps something else
183 * bad might happen. So instead we convert it to an
184 * equivalent address in the IPv4 address family. */
185 struct sockaddr_in6 sin6;
186 struct sockaddr_in *sin;
187
Wayne Davisonc1e72172004-04-01 20:53:39 +0000188 memcpy(&sin6, ss, sizeof sin6);
Martin Pool0cd2f402002-01-25 02:13:04 +0000189 sin = (struct sockaddr_in *)ss;
Wayne Davisonc1e72172004-04-01 20:53:39 +0000190 memset(sin, 0, sizeof *sin);
Martin Pool0cd2f402002-01-25 02:13:04 +0000191 sin->sin_family = AF_INET;
Wayne Davisonc1e72172004-04-01 20:53:39 +0000192 *ss_len = sizeof (struct sockaddr_in);
Wayne Davison4f5b0752005-02-14 00:53:43 +0000193#ifdef HAVE_SOCKADDR_IN_LEN
Martin Pool0cd2f402002-01-25 02:13:04 +0000194 sin->sin_len = *ss_len;
195#endif
196 sin->sin_port = sin6.sin6_port;
197
198 /* There is a macro to extract the mapped part
199 * (IN6_V4MAPPED_TO_SINADDR ?), but it does not seem
200 * to be present in the Linux headers. */
201 memcpy(&sin->sin_addr, &sin6.sin6_addr.s6_addr[12],
Wayne Davisonc1e72172004-04-01 20:53:39 +0000202 sizeof sin->sin_addr);
Wayne Davison2b284ee2004-04-01 21:08:24 +0000203 }
Martin Pool0cd2f402002-01-25 02:13:04 +0000204#endif
205}
206
207
208/**
209 * Look up a name from @p ss into @p name_buf.
Martin Pool6c92af22002-04-03 02:33:42 +0000210 *
211 * @param fd file descriptor for client socket.
Martin Pool0cd2f402002-01-25 02:13:04 +0000212 **/
213int lookup_name(int fd, const struct sockaddr_storage *ss,
214 socklen_t ss_len,
Wayne Davison55410362006-10-13 23:17:27 +0000215 char *name_buf, size_t name_buf_size,
216 char *port_buf, size_t port_buf_size)
Martin Pool0cd2f402002-01-25 02:13:04 +0000217{
218 int name_err;
Wayne Davison4cfa6152004-04-01 20:56:50 +0000219
Martin Pool0cd2f402002-01-25 02:13:04 +0000220 /* reverse lookup */
221 name_err = getnameinfo((struct sockaddr *) ss, ss_len,
Wayne Davison55410362006-10-13 23:17:27 +0000222 name_buf, name_buf_size,
223 port_buf, port_buf_size,
Martin Pool0cd2f402002-01-25 02:13:04 +0000224 NI_NAMEREQD | NI_NUMERICSERV);
225 if (name_err != 0) {
Wayne Davison55410362006-10-13 23:17:27 +0000226 strlcpy(name_buf, default_name, name_buf_size);
Wayne Davisonfde045c2004-09-24 16:39:41 +0000227 rprintf(FLOG, "name lookup failed for %s: %s\n",
228 client_addr(fd), gai_strerror(name_err));
Martin Pool0cd2f402002-01-25 02:13:04 +0000229 return name_err;
230 }
231
232 return 0;
233}
234
235
236
Martin Poolaf32f692002-01-25 02:15:58 +0000237/**
Martin Pool974f27e2002-01-25 02:29:53 +0000238 * Compare an addrinfo from the resolver to a sockinfo.
239 *
240 * Like strcmp, returns 0 for identical.
241 **/
242int compare_addrinfo_sockaddr(const struct addrinfo *ai,
243 const struct sockaddr_storage *ss)
244{
245 int ss_family = get_sockaddr_family(ss);
246 const char fn[] = "compare_addrinfo_sockaddr";
Wayne Davisonc1e72172004-04-01 20:53:39 +0000247
Martin Pool974f27e2002-01-25 02:29:53 +0000248 if (ai->ai_family != ss_family) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000249 rprintf(FLOG, "%s: response family %d != %d\n",
Martin Pool974f27e2002-01-25 02:29:53 +0000250 fn, ai->ai_family, ss_family);
251 return 1;
252 }
253
Martin Pool39e01d22002-01-25 02:43:35 +0000254 /* The comparison method depends on the particular AF. */
255 if (ss_family == AF_INET) {
256 const struct sockaddr_in *sin1, *sin2;
257
258 sin1 = (const struct sockaddr_in *) ss;
259 sin2 = (const struct sockaddr_in *) ai->ai_addr;
Wayne Davison4cfa6152004-04-01 20:56:50 +0000260
Martin Pool6780f722002-01-25 02:45:09 +0000261 return memcmp(&sin1->sin_addr, &sin2->sin_addr,
262 sizeof sin1->sin_addr);
Martin Pool39e01d22002-01-25 02:43:35 +0000263 }
Wayne Davison2b284ee2004-04-01 21:08:24 +0000264
Wayne Davison4f5b0752005-02-14 00:53:43 +0000265#ifdef INET6
Wayne Davison2b284ee2004-04-01 21:08:24 +0000266 if (ss_family == AF_INET6) {
Martin Pool6780f722002-01-25 02:45:09 +0000267 const struct sockaddr_in6 *sin1, *sin2;
268
269 sin1 = (const struct sockaddr_in6 *) ss;
270 sin2 = (const struct sockaddr_in6 *) ai->ai_addr;
David Dykstrabc2b4962003-01-09 21:14:10 +0000271
Wayne Davisonc1e72172004-04-01 20:53:39 +0000272 if (ai->ai_addrlen < sizeof (struct sockaddr_in6)) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000273 rprintf(FLOG, "%s: too short sockaddr_in6; length=%d\n",
Wayne Davisonf5870612008-02-22 15:15:32 -0800274 fn, (int)ai->ai_addrlen);
David Dykstrabc2b4962003-01-09 21:14:10 +0000275 return 1;
276 }
277
278 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
279 sizeof sin1->sin6_addr))
280 return 1;
281
Wayne Davison4f5b0752005-02-14 00:53:43 +0000282#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
David Dykstrabc2b4962003-01-09 21:14:10 +0000283 if (sin1->sin6_scope_id != sin2->sin6_scope_id)
284 return 1;
285#endif
286 return 0;
Martin Pool974f27e2002-01-25 02:29:53 +0000287 }
Martin Pool39e01d22002-01-25 02:43:35 +0000288#endif /* INET6 */
Wayne Davison2b284ee2004-04-01 21:08:24 +0000289
290 /* don't know */
291 return 1;
Martin Pool974f27e2002-01-25 02:29:53 +0000292}
293
294
295/**
Martin Poolaf32f692002-01-25 02:15:58 +0000296 * Do a forward lookup on @p name_buf and make sure it corresponds to
297 * @p ss -- otherwise we may be being spoofed. If we suspect we are,
298 * then we don't abort the connection but just emit a warning, and
299 * change @p name_buf to be "UNKNOWN".
Martin Pool6c92af22002-04-03 02:33:42 +0000300 *
301 * We don't do anything with the service when checking the name,
302 * because it doesn't seem that it could be spoofed in any way, and
303 * getaddrinfo on random service names seems to cause problems on AIX.
Martin Poolaf32f692002-01-25 02:15:58 +0000304 **/
Martin Pool0cd2f402002-01-25 02:13:04 +0000305int check_name(int fd,
306 const struct sockaddr_storage *ss,
Wayne Davison55410362006-10-13 23:17:27 +0000307 char *name_buf, size_t name_buf_size)
Martin Pool0cd2f402002-01-25 02:13:04 +0000308{
309 struct addrinfo hints, *res, *res0;
310 int error;
311 int ss_family = get_sockaddr_family(ss);
312
Wayne Davisonc1e72172004-04-01 20:53:39 +0000313 memset(&hints, 0, sizeof hints);
Martin Pool39e01d22002-01-25 02:43:35 +0000314 hints.ai_family = ss_family;
315 hints.ai_flags = AI_CANONNAME;
Martin Pool0cd2f402002-01-25 02:13:04 +0000316 hints.ai_socktype = SOCK_STREAM;
Martin Pool6c92af22002-04-03 02:33:42 +0000317 error = getaddrinfo(name_buf, NULL, &hints, &res0);
Martin Pool0cd2f402002-01-25 02:13:04 +0000318 if (error) {
Wayne Davisonfde045c2004-09-24 16:39:41 +0000319 rprintf(FLOG, "forward name lookup for %s failed: %s\n",
Martin Poolaf32f692002-01-25 02:15:58 +0000320 name_buf, gai_strerror(error));
Wayne Davison55410362006-10-13 23:17:27 +0000321 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool0cd2f402002-01-25 02:13:04 +0000322 return error;
323 }
324
Martin Pool974f27e2002-01-25 02:29:53 +0000325 /* Given all these results, we expect that one of them will be
326 * the same as ss. The comparison is a bit complicated. */
Martin Pool0cd2f402002-01-25 02:13:04 +0000327 for (res = res0; res; res = res->ai_next) {
Martin Pool974f27e2002-01-25 02:29:53 +0000328 if (!compare_addrinfo_sockaddr(res, ss))
329 break; /* OK, identical */
Martin Pool0cd2f402002-01-25 02:13:04 +0000330 }
331
332 if (!res0) {
333 /* 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, "no known address for \"%s\": "
336 "spoofed address?\n", name_buf);
Wayne Davison55410362006-10-13 23:17:27 +0000337 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool974f27e2002-01-25 02:29:53 +0000338 } else if (res == NULL) {
Martin Pool0cd2f402002-01-25 02:13:04 +0000339 /* We hit the end of the list without finding an
340 * address that was the same as ss. */
Wayne Davisonfde045c2004-09-24 16:39:41 +0000341 rprintf(FLOG, "%s is not a known address for \"%s\": "
342 "spoofed address?\n", client_addr(fd), name_buf);
Wayne Davison55410362006-10-13 23:17:27 +0000343 strlcpy(name_buf, default_name, name_buf_size);
Martin Pool0cd2f402002-01-25 02:13:04 +0000344 }
345
346 freeaddrinfo(res0);
347 return 0;
348}