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