Daemon supports forward-DNS lookups for simple hostnames
[rsync/rsync.git] / access.c
CommitLineData
56c473b7 1/*
0f78b815
WD
2 * Routines to authenticate access to a daemon (hosts allow/deny).
3 *
4 * Copyright (C) 1998 Andrew Tridgell
b3bf9b9d 5 * Copyright (C) 2004-2009 Wayne Davison
0f78b815
WD
6 *
7 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
0f78b815
WD
11 *
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 *
e7c67065 17 * You should have received a copy of the GNU General Public License along
4fd842f9 18 * with this program; if not, visit the http://fsf.org website.
0f78b815 19 */
56c473b7
AT
20
21#include "rsync.h"
22
bf4170ad
WD
23static int allow_forward_dns;
24
25extern const char undetermined_hostname[];
26
27static int match_hostname(const char **host_ptr, const char *addr, const char *tok)
56c473b7 28{
bf4170ad
WD
29 struct hostent *hp;
30 unsigned int i;
31 const char *host = *host_ptr;
32
36142821
WD
33 if (!host || !*host)
34 return 0;
bf4170ad
WD
35
36 /* First check if the reverse-DNS-determined hostname matches. */
37 if (iwildmatch(tok, host))
38 return 1;
39
40 if (!allow_forward_dns)
41 return 0;
42
43 /* Fail quietly if tok is an address or wildcarded entry, not a simple hostname. */
44 if (!tok[strspn(tok, ".0123456789")] || tok[strcspn(tok, ":/*?[")])
45 return 0;
46
47 /* Now try forward-DNS on the token (config-specified hostname) and see if the IP matches. */
48 if (!(hp = gethostbyname(tok)))
49 return 0;
50
51 for (i = 0; hp->h_addr_list[i] != NULL; i++) {
52 if (strcmp(addr, inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]))) == 0) {
53 /* If reverse lookups are off, we'll use the conf-specified
54 * hostname in preference to UNDETERMINED. */
55 if (host == undetermined_hostname) {
56 if (!(*host_ptr = strdup(tok)))
57 *host_ptr = undetermined_hostname;
58 }
59 return 1;
60 }
61 }
62
63 return 0;
56c473b7
AT
64}
65
df694f72 66static int match_binary(const char *b1, const char *b2, const char *mask, int addrlen)
bc2b4963
DD
67{
68 int i;
69
36142821
WD
70 for (i = 0; i < addrlen; i++) {
71 if ((b1[i] ^ b2[i]) & mask[i])
bc2b4963 72 return 0;
bc2b4963
DD
73 }
74
75 return 1;
76}
77
36142821
WD
78static void make_mask(char *mask, int plen, int addrlen)
79{
bc2b4963
DD
80 int w, b;
81
82 w = plen >> 3;
83 b = plen & 0x7;
84
85 if (w)
86 memset(mask, 0xff, w);
7bc8218d
DD
87 if (w < addrlen)
88 mask[w] = 0xff & (0xff<<(8-b));
bc2b4963
DD
89 if (w+1 < addrlen)
90 memset(mask+w+1, 0, addrlen-w-1);
91
92 return;
93}
56c473b7 94
df694f72 95static int match_address(const char *addr, const char *tok)
56c473b7
AT
96{
97 char *p;
bc2b4963
DD
98 struct addrinfo hints, *resa, *rest;
99 int gai;
100 int ret = 0;
101 int addrlen = 0;
4f5b0756 102#ifdef HAVE_STRTOL
bc2b4963
DD
103 long int bits;
104#else
105 int bits;
106#endif
107 char mask[16];
108 char *a = NULL, *t = NULL;
56c473b7 109
36142821
WD
110 if (!addr || !*addr)
111 return 0;
56c473b7 112
56c473b7 113 p = strchr(tok,'/');
bf4170ad 114 if (p)
32f60a6e 115 *p = '\0';
56c473b7 116
bf4170ad
WD
117 /* Fail quietly if tok is a hostname, not an address. */
118 if (tok[strspn(tok, ".0123456789")] && strchr(tok, ':') == NULL) {
be7cf822
WD
119 if (p)
120 *p = '/';
121 return 0;
122 }
70a6051c 123
bc2b4963
DD
124 memset(&hints, 0, sizeof(hints));
125 hints.ai_family = PF_UNSPEC;
126 hints.ai_socktype = SOCK_STREAM;
8d2aad49 127#ifdef AI_NUMERICHOST
bc2b4963 128 hints.ai_flags = AI_NUMERICHOST;
8d2aad49 129#endif
56c473b7 130
be7cf822
WD
131 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
132 if (p)
133 *p = '/';
134 return 0;
135 }
56c473b7 136
bc2b4963
DD
137 gai = getaddrinfo(tok, NULL, &hints, &rest);
138 if (p)
139 *p++ = '/';
be7cf822
WD
140 if (gai != 0) {
141 rprintf(FLOG, "error matching address %s: %s\n",
142 tok, gai_strerror(gai));
bc2b4963 143 freeaddrinfo(resa);
56c473b7
AT
144 return 0;
145 }
146
bc2b4963
DD
147 if (rest->ai_family != resa->ai_family) {
148 ret = 0;
149 goto out;
150 }
151
152 switch(resa->ai_family) {
153 case PF_INET:
154 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
155 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
156 addrlen = 4;
157
158 break;
159
4f5b0756 160#ifdef INET6
bc2b4963
DD
161 case PF_INET6:
162 {
163 struct sockaddr_in6 *sin6a, *sin6t;
56c473b7 164
bc2b4963
DD
165 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
166 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
167
168 a = (char *)&sin6a->sin6_addr;
169 t = (char *)&sin6t->sin6_addr;
170
7bc8218d
DD
171 addrlen = 16;
172
4f5b0756 173#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
bc2b4963
DD
174 if (sin6t->sin6_scope_id &&
175 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
176 ret = 0;
177 goto out;
178 }
179#endif
180
bc2b4963
DD
181 break;
182 }
183#endif
184 default:
be7cf822 185 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
bc2b4963
DD
186 ret = 0;
187 goto out;
188 }
189
190 bits = -1;
56c473b7 191 if (p) {
bc2b4963 192 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
4f5b0756 193#ifdef HAVE_STRTOL
bc2b4963
DD
194 char *ep = NULL;
195#else
196 unsigned char *pp;
197#endif
198
4f5b0756 199#ifdef HAVE_STRTOL
bc2b4963
DD
200 bits = strtol(p, &ep, 10);
201 if (!*p || *ep) {
be7cf822 202 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
203 ret = 0;
204 goto out;
205 }
206#else
207 for (pp = (unsigned char *)p; *pp; pp++) {
208 if (!isascii(*pp) || !isdigit(*pp)) {
be7cf822 209 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
210 ret = 0;
211 goto out;
212 }
56c473b7 213 }
bc2b4963
DD
214 bits = atoi(p);
215#endif
216 if (bits == 0) {
217 ret = 1;
218 goto out;
219 }
220 if (bits < 0 || bits > (addrlen << 3)) {
be7cf822 221 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
222 ret = 0;
223 goto out;
56c473b7 224 }
56c473b7 225 }
bc2b4963
DD
226 } else {
227 bits = 128;
56c473b7
AT
228 }
229
bc2b4963
DD
230 if (bits >= 0)
231 make_mask(mask, bits, addrlen);
232
233 ret = match_binary(a, t, mask, addrlen);
234
2997e9f7 235 out:
bc2b4963
DD
236 freeaddrinfo(resa);
237 freeaddrinfo(rest);
238 return ret;
56c473b7
AT
239}
240
bf4170ad 241static int access_match(const char *list, const char *addr, const char **host_ptr)
56c473b7
AT
242{
243 char *tok;
244 char *list2 = strdup(list);
245
36142821
WD
246 if (!list2)
247 out_of_memory("access_match");
56c473b7 248
5a96ee05 249 strlower(list2);
5a96ee05 250
36142821 251 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
bf4170ad 252 if (match_hostname(host_ptr, addr, tok) || match_address(addr, tok)) {
56c473b7
AT
253 free(list2);
254 return 1;
255 }
256 }
257
258 free(list2);
259 return 0;
260}
261
bf4170ad 262int allow_access(const char *addr, const char **host_ptr, int i)
56c473b7 263{
bf4170ad
WD
264 const char *allow_list = lp_hosts_allow(i);
265 const char *deny_list = lp_hosts_deny(i);
266
36142821
WD
267 if (allow_list && !*allow_list)
268 allow_list = NULL;
269 if (deny_list && !*deny_list)
270 deny_list = NULL;
271
bf4170ad
WD
272 allow_forward_dns = lp_forward_lookup(i);
273
36142821
WD
274 /* If we match an allow-list item, we always allow access. */
275 if (allow_list) {
bf4170ad 276 if (access_match(allow_list, addr, host_ptr))
36142821
WD
277 return 1;
278 /* For an allow-list w/o a deny-list, disallow non-matches. */
279 if (!deny_list)
280 return 0;
281 }
282
283 /* If we match a deny-list item (and got past any allow-list
284 * items), we always disallow access. */
bf4170ad 285 if (deny_list && access_match(deny_list, addr, host_ptr))
56c473b7
AT
286 return 0;
287
36142821 288 /* Allow all other access. */
56c473b7
AT
289 return 1;
290}