Simplified the logic in allow_access() and tweaked some whitespace.
[rsync/rsync.git] / access.c
CommitLineData
56c473b7
AT
1/*
2 Copyright (C) Andrew Tridgell 1998
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/*
20 hosts allow/deny code for rsync
21
22 */
23
24#include "rsync.h"
25
26
27static int match_hostname(char *host, char *tok)
28{
36142821
WD
29 if (!host || !*host)
30 return 0;
fe332038 31 return wildmatch(tok, host);
56c473b7
AT
32}
33
bc2b4963
DD
34static int match_binary(char *b1, char *b2, char *mask, int addrlen)
35{
36 int i;
37
36142821
WD
38 for (i = 0; i < addrlen; i++) {
39 if ((b1[i] ^ b2[i]) & mask[i])
bc2b4963 40 return 0;
bc2b4963
DD
41 }
42
43 return 1;
44}
45
36142821
WD
46static void make_mask(char *mask, int plen, int addrlen)
47{
bc2b4963
DD
48 int w, b;
49
50 w = plen >> 3;
51 b = plen & 0x7;
52
53 if (w)
54 memset(mask, 0xff, w);
7bc8218d
DD
55 if (w < addrlen)
56 mask[w] = 0xff & (0xff<<(8-b));
bc2b4963
DD
57 if (w+1 < addrlen)
58 memset(mask+w+1, 0, addrlen-w-1);
59
60 return;
61}
56c473b7
AT
62
63static int match_address(char *addr, char *tok)
64{
65 char *p;
bc2b4963
DD
66 struct addrinfo hints, *resa, *rest;
67 int gai;
68 int ret = 0;
69 int addrlen = 0;
70#ifdef HAVE_STRTOL
71 long int bits;
72#else
73 int bits;
74#endif
75 char mask[16];
76 char *a = NULL, *t = NULL;
32f60a6e 77 unsigned int len;
56c473b7 78
36142821
WD
79 if (!addr || !*addr)
80 return 0;
56c473b7 81
56c473b7 82 p = strchr(tok,'/');
32f60a6e
WD
83 if (p) {
84 *p = '\0';
85 len = p - tok;
36142821 86 } else
32f60a6e 87 len = strlen(tok);
56c473b7 88
32f60a6e 89 /* Fail quietly if tok is a hostname (not an address) */
b49d381d 90 if (strspn(tok, ".0123456789") != len
32f60a6e 91#ifdef INET6
be7cf822 92 && strchr(tok, ':') == NULL
32f60a6e 93#endif
be7cf822
WD
94 ) {
95 if (p)
96 *p = '/';
97 return 0;
98 }
70a6051c 99
bc2b4963
DD
100 memset(&hints, 0, sizeof(hints));
101 hints.ai_family = PF_UNSPEC;
102 hints.ai_socktype = SOCK_STREAM;
8d2aad49 103#ifdef AI_NUMERICHOST
bc2b4963 104 hints.ai_flags = AI_NUMERICHOST;
8d2aad49 105#endif
56c473b7 106
be7cf822
WD
107 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
108 if (p)
109 *p = '/';
110 return 0;
111 }
56c473b7 112
bc2b4963
DD
113 gai = getaddrinfo(tok, NULL, &hints, &rest);
114 if (p)
115 *p++ = '/';
be7cf822
WD
116 if (gai != 0) {
117 rprintf(FLOG, "error matching address %s: %s\n",
118 tok, gai_strerror(gai));
bc2b4963 119 freeaddrinfo(resa);
56c473b7
AT
120 return 0;
121 }
122
bc2b4963
DD
123 if (rest->ai_family != resa->ai_family) {
124 ret = 0;
125 goto out;
126 }
127
128 switch(resa->ai_family) {
129 case PF_INET:
130 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
131 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
132 addrlen = 4;
133
134 break;
135
136#ifdef INET6
137 case PF_INET6:
138 {
139 struct sockaddr_in6 *sin6a, *sin6t;
56c473b7 140
bc2b4963
DD
141 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
142 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
143
144 a = (char *)&sin6a->sin6_addr;
145 t = (char *)&sin6t->sin6_addr;
146
7bc8218d
DD
147 addrlen = 16;
148
bc2b4963
DD
149#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
150 if (sin6t->sin6_scope_id &&
151 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
152 ret = 0;
153 goto out;
154 }
155#endif
156
bc2b4963
DD
157 break;
158 }
159#endif
160 default:
be7cf822 161 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
bc2b4963
DD
162 ret = 0;
163 goto out;
164 }
165
166 bits = -1;
56c473b7 167 if (p) {
bc2b4963
DD
168 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
169#ifdef HAVE_STRTOL
170 char *ep = NULL;
171#else
172 unsigned char *pp;
173#endif
174
175#ifdef HAVE_STRTOL
176 bits = strtol(p, &ep, 10);
177 if (!*p || *ep) {
be7cf822 178 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
179 ret = 0;
180 goto out;
181 }
182#else
183 for (pp = (unsigned char *)p; *pp; pp++) {
184 if (!isascii(*pp) || !isdigit(*pp)) {
be7cf822 185 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
186 ret = 0;
187 goto out;
188 }
56c473b7 189 }
bc2b4963
DD
190 bits = atoi(p);
191#endif
192 if (bits == 0) {
193 ret = 1;
194 goto out;
195 }
196 if (bits < 0 || bits > (addrlen << 3)) {
be7cf822 197 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
198 ret = 0;
199 goto out;
56c473b7 200 }
56c473b7 201 }
bc2b4963
DD
202 } else {
203 bits = 128;
56c473b7
AT
204 }
205
bc2b4963
DD
206 if (bits >= 0)
207 make_mask(mask, bits, addrlen);
208
209 ret = match_binary(a, t, mask, addrlen);
210
211out:
212 freeaddrinfo(resa);
213 freeaddrinfo(rest);
214 return ret;
56c473b7
AT
215}
216
217static int access_match(char *list, char *addr, char *host)
218{
219 char *tok;
220 char *list2 = strdup(list);
221
36142821
WD
222 if (!list2)
223 out_of_memory("access_match");
56c473b7 224
5a96ee05 225 strlower(list2);
36142821
WD
226 if (host)
227 strlower(host);
5a96ee05 228
36142821 229 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
56c473b7
AT
230 if (match_hostname(host, tok) || match_address(addr, tok)) {
231 free(list2);
232 return 1;
233 }
234 }
235
236 free(list2);
237 return 0;
238}
239
240int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
241{
36142821
WD
242 if (allow_list && !*allow_list)
243 allow_list = NULL;
244 if (deny_list && !*deny_list)
245 deny_list = NULL;
246
247 /* If we match an allow-list item, we always allow access. */
248 if (allow_list) {
249 if (access_match(allow_list, addr, host))
250 return 1;
251 /* For an allow-list w/o a deny-list, disallow non-matches. */
252 if (!deny_list)
253 return 0;
254 }
255
256 /* If we match a deny-list item (and got past any allow-list
257 * items), we always disallow access. */
258 if (deny_list && access_match(deny_list, addr, host))
56c473b7
AT
259 return 0;
260
36142821 261 /* Allow all other access. */
56c473b7
AT
262 return 1;
263}