- Make sure that match_address() always restores the "tok" string,
[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;
fe332038 30 return wildmatch(tok, host);
56c473b7
AT
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 88 /* Fail quietly if tok is a hostname (not an address) */
b49d381d 89 if (strspn(tok, ".0123456789") != len
32f60a6e 90#ifdef INET6
be7cf822 91 && strchr(tok, ':') == NULL
32f60a6e 92#endif
be7cf822
WD
93 ) {
94 if (p)
95 *p = '/';
96 return 0;
97 }
70a6051c 98
bc2b4963
DD
99 memset(&hints, 0, sizeof(hints));
100 hints.ai_family = PF_UNSPEC;
101 hints.ai_socktype = SOCK_STREAM;
8d2aad49 102#ifdef AI_NUMERICHOST
bc2b4963 103 hints.ai_flags = AI_NUMERICHOST;
8d2aad49 104#endif
56c473b7 105
be7cf822
WD
106 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
107 if (p)
108 *p = '/';
109 return 0;
110 }
56c473b7 111
bc2b4963
DD
112 gai = getaddrinfo(tok, NULL, &hints, &rest);
113 if (p)
114 *p++ = '/';
be7cf822
WD
115 if (gai != 0) {
116 rprintf(FLOG, "error matching address %s: %s\n",
117 tok, gai_strerror(gai));
bc2b4963 118 freeaddrinfo(resa);
56c473b7
AT
119 return 0;
120 }
121
bc2b4963
DD
122 if (rest->ai_family != resa->ai_family) {
123 ret = 0;
124 goto out;
125 }
126
127 switch(resa->ai_family) {
128 case PF_INET:
129 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
130 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
131 addrlen = 4;
132
133 break;
134
135#ifdef INET6
136 case PF_INET6:
137 {
138 struct sockaddr_in6 *sin6a, *sin6t;
56c473b7 139
bc2b4963
DD
140 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
141 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
142
143 a = (char *)&sin6a->sin6_addr;
144 t = (char *)&sin6t->sin6_addr;
145
7bc8218d
DD
146 addrlen = 16;
147
bc2b4963
DD
148#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
149 if (sin6t->sin6_scope_id &&
150 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
151 ret = 0;
152 goto out;
153 }
154#endif
155
bc2b4963
DD
156 break;
157 }
158#endif
159 default:
be7cf822 160 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
bc2b4963
DD
161 ret = 0;
162 goto out;
163 }
164
165 bits = -1;
56c473b7 166 if (p) {
bc2b4963
DD
167 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
168#ifdef HAVE_STRTOL
169 char *ep = NULL;
170#else
171 unsigned char *pp;
172#endif
173
174#ifdef HAVE_STRTOL
175 bits = strtol(p, &ep, 10);
176 if (!*p || *ep) {
be7cf822 177 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
178 ret = 0;
179 goto out;
180 }
181#else
182 for (pp = (unsigned char *)p; *pp; pp++) {
183 if (!isascii(*pp) || !isdigit(*pp)) {
be7cf822 184 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
185 ret = 0;
186 goto out;
187 }
56c473b7 188 }
bc2b4963
DD
189 bits = atoi(p);
190#endif
191 if (bits == 0) {
192 ret = 1;
193 goto out;
194 }
195 if (bits < 0 || bits > (addrlen << 3)) {
be7cf822 196 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
197 ret = 0;
198 goto out;
56c473b7 199 }
56c473b7 200 }
bc2b4963
DD
201 } else {
202 bits = 128;
56c473b7
AT
203 }
204
bc2b4963
DD
205 if (bits >= 0)
206 make_mask(mask, bits, addrlen);
207
208 ret = match_binary(a, t, mask, addrlen);
209
210out:
211 freeaddrinfo(resa);
212 freeaddrinfo(rest);
213 return ret;
56c473b7
AT
214}
215
216static int access_match(char *list, char *addr, char *host)
217{
218 char *tok;
219 char *list2 = strdup(list);
220
221 if (!list2) out_of_memory("access_match");
222
5a96ee05
AT
223 strlower(list2);
224 if (host) strlower(host);
225
56c473b7
AT
226 for (tok=strtok(list2," ,\t"); tok; tok=strtok(NULL," ,\t")) {
227 if (match_hostname(host, tok) || match_address(addr, tok)) {
228 free(list2);
229 return 1;
230 }
231 }
232
233 free(list2);
234 return 0;
235}
236
237int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
238{
239 /* if theres no deny list and no allow list then allow access */
240 if ((!deny_list || !*deny_list) && (!allow_list || !*allow_list))
241 return 1;
242
243 /* if there is an allow list but no deny list then allow only hosts
244 on the allow list */
245 if (!deny_list || !*deny_list)
246 return(access_match(allow_list, addr, host));
247
248 /* if theres a deny list but no allow list then allow
249 all hosts not on the deny list */
250 if (!allow_list || !*allow_list)
251 return(!access_match(deny_list,addr,host));
252
253 /* if there are both type of list then allow all hosts on the
254 allow list */
255 if (access_match(allow_list,addr,host))
256 return 1;
257
258 /* if there are both type of list and it's not on the allow then
259 allow it if its not on the deny */
260 if (access_match(deny_list,addr,host))
261 return 0;
262
263 return 1;
264}