Improved deficiencies in the hostname-vs-address check (reintroduced in the
[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{
29 if (!host || !*host) return 0;
30 return (fnmatch(tok, host, 0) == 0);
31}
32
bc2b4963
DD
33static int match_binary(char *b1, char *b2, char *mask, int addrlen)
34{
35 int i;
36
37 for (i=0; i<addrlen; i++) {
38 if ((b1[i]^b2[i])&mask[i]) {
39 return 0;
40 }
41 }
42
43 return 1;
44}
45
46static void make_mask(char *mask, int plen, int addrlen) {
47 int w, b;
48
49 w = plen >> 3;
50 b = plen & 0x7;
51
52 if (w)
53 memset(mask, 0xff, w);
7bc8218d
DD
54 if (w < addrlen)
55 mask[w] = 0xff & (0xff<<(8-b));
bc2b4963
DD
56 if (w+1 < addrlen)
57 memset(mask+w+1, 0, addrlen-w-1);
58
59 return;
60}
56c473b7
AT
61
62static int match_address(char *addr, char *tok)
63{
64 char *p;
bc2b4963
DD
65 struct addrinfo hints, *resa, *rest;
66 int gai;
67 int ret = 0;
68 int addrlen = 0;
69#ifdef HAVE_STRTOL
70 long int bits;
71#else
72 int bits;
73#endif
74 char mask[16];
75 char *a = NULL, *t = NULL;
32f60a6e 76 unsigned int len;
56c473b7
AT
77
78 if (!addr || !*addr) return 0;
79
56c473b7 80 p = strchr(tok,'/');
32f60a6e
WD
81 if (p) {
82 *p = '\0';
83 len = p - tok;
84 }
85 else
86 len = strlen(tok);
56c473b7 87
32f60a6e
WD
88 /* Fail quietly if tok is a hostname (not an address) */
89 if (strspn(tok, "./0123456789") != len
90#ifdef INET6
91 && strspn(tok, "/0123456789:ABCDEFabcdef") != len
92 && !strchr(tok, '%')
93#endif
94 ) return 0;
70a6051c 95
bc2b4963
DD
96 memset(&hints, 0, sizeof(hints));
97 hints.ai_family = PF_UNSPEC;
98 hints.ai_socktype = SOCK_STREAM;
8d2aad49 99#ifdef AI_NUMERICHOST
bc2b4963 100 hints.ai_flags = AI_NUMERICHOST;
8d2aad49 101#endif
56c473b7 102
bc2b4963
DD
103 gai = getaddrinfo(addr, NULL, &hints, &resa);
104 if (gai) return 0;
56c473b7 105
bc2b4963
DD
106 gai = getaddrinfo(tok, NULL, &hints, &rest);
107 if (p)
108 *p++ = '/';
109 if (gai) {
32f60a6e
WD
110 rprintf(FERROR,
111 "error matching address %s: %s\n",
112 tok,
113 gai_strerror(gai));
bc2b4963 114 freeaddrinfo(resa);
56c473b7
AT
115 return 0;
116 }
117
bc2b4963
DD
118 if (rest->ai_family != resa->ai_family) {
119 ret = 0;
120 goto out;
121 }
122
123 switch(resa->ai_family) {
124 case PF_INET:
125 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
126 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
127 addrlen = 4;
128
129 break;
130
131#ifdef INET6
132 case PF_INET6:
133 {
134 struct sockaddr_in6 *sin6a, *sin6t;
56c473b7 135
bc2b4963
DD
136 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
137 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
138
139 a = (char *)&sin6a->sin6_addr;
140 t = (char *)&sin6t->sin6_addr;
141
7bc8218d
DD
142 addrlen = 16;
143
bc2b4963
DD
144#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
145 if (sin6t->sin6_scope_id &&
146 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
147 ret = 0;
148 goto out;
149 }
150#endif
151
bc2b4963
DD
152 break;
153 }
154#endif
155 default:
156 rprintf(FERROR,"unknown family %u\n", rest->ai_family);
157 ret = 0;
158 goto out;
159 }
160
161 bits = -1;
56c473b7 162 if (p) {
bc2b4963
DD
163 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
164#ifdef HAVE_STRTOL
165 char *ep = NULL;
166#else
167 unsigned char *pp;
168#endif
169
170#ifdef HAVE_STRTOL
171 bits = strtol(p, &ep, 10);
172 if (!*p || *ep) {
56c473b7 173 rprintf(FERROR,"malformed mask in %s\n", tok);
bc2b4963
DD
174 ret = 0;
175 goto out;
176 }
177#else
178 for (pp = (unsigned char *)p; *pp; pp++) {
179 if (!isascii(*pp) || !isdigit(*pp)) {
180 rprintf(FERROR,"malformed mask in %s\n", tok);
181 ret = 0;
182 goto out;
183 }
56c473b7 184 }
bc2b4963
DD
185 bits = atoi(p);
186#endif
187 if (bits == 0) {
188 ret = 1;
189 goto out;
190 }
191 if (bits < 0 || bits > (addrlen << 3)) {
56c473b7 192 rprintf(FERROR,"malformed mask in %s\n", tok);
bc2b4963
DD
193 ret = 0;
194 goto out;
56c473b7 195 }
56c473b7 196 }
bc2b4963
DD
197 } else {
198 bits = 128;
56c473b7
AT
199 }
200
bc2b4963
DD
201 if (bits >= 0)
202 make_mask(mask, bits, addrlen);
203
204 ret = match_binary(a, t, mask, addrlen);
205
206out:
207 freeaddrinfo(resa);
208 freeaddrinfo(rest);
209 return ret;
56c473b7
AT
210}
211
212static int access_match(char *list, char *addr, char *host)
213{
214 char *tok;
215 char *list2 = strdup(list);
216
217 if (!list2) out_of_memory("access_match");
218
5a96ee05
AT
219 strlower(list2);
220 if (host) strlower(host);
221
56c473b7
AT
222 for (tok=strtok(list2," ,\t"); tok; tok=strtok(NULL," ,\t")) {
223 if (match_hostname(host, tok) || match_address(addr, tok)) {
224 free(list2);
225 return 1;
226 }
227 }
228
229 free(list2);
230 return 0;
231}
232
233int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
234{
235 /* if theres no deny list and no allow list then allow access */
236 if ((!deny_list || !*deny_list) && (!allow_list || !*allow_list))
237 return 1;
238
239 /* if there is an allow list but no deny list then allow only hosts
240 on the allow list */
241 if (!deny_list || !*deny_list)
242 return(access_match(allow_list, addr, host));
243
244 /* if theres a deny list but no allow list then allow
245 all hosts not on the deny list */
246 if (!allow_list || !*allow_list)
247 return(!access_match(deny_list,addr,host));
248
249 /* if there are both type of list then allow all hosts on the
250 allow list */
251 if (access_match(allow_list,addr,host))
252 return 1;
253
254 /* if there are both type of list and it's not on the allow then
255 allow it if its not on the deny */
256 if (access_match(deny_list,addr,host))
257 return 0;
258
259 return 1;
260}