Simplified one "if" that checks if one of the --FOO-dest options
[rsync/rsync.git] / access.c
... / ...
CommitLineData
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 wildmatch(tok, host);
31}
32
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);
54 if (w < addrlen)
55 mask[w] = 0xff & (0xff<<(8-b));
56 if (w+1 < addrlen)
57 memset(mask+w+1, 0, addrlen-w-1);
58
59 return;
60}
61
62static int match_address(char *addr, char *tok)
63{
64 char *p;
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;
76 unsigned int len;
77
78 if (!addr || !*addr) return 0;
79
80 p = strchr(tok,'/');
81 if (p) {
82 *p = '\0';
83 len = p - tok;
84 }
85 else
86 len = strlen(tok);
87
88 /* Fail quietly if tok is a hostname (not an address) */
89 if (strspn(tok, ".0123456789") != len
90#ifdef INET6
91 && strchr(tok, ':') == NULL
92#endif
93 ) {
94 if (p)
95 *p = '/';
96 return 0;
97 }
98
99 memset(&hints, 0, sizeof(hints));
100 hints.ai_family = PF_UNSPEC;
101 hints.ai_socktype = SOCK_STREAM;
102#ifdef AI_NUMERICHOST
103 hints.ai_flags = AI_NUMERICHOST;
104#endif
105
106 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
107 if (p)
108 *p = '/';
109 return 0;
110 }
111
112 gai = getaddrinfo(tok, NULL, &hints, &rest);
113 if (p)
114 *p++ = '/';
115 if (gai != 0) {
116 rprintf(FLOG, "error matching address %s: %s\n",
117 tok, gai_strerror(gai));
118 freeaddrinfo(resa);
119 return 0;
120 }
121
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;
139
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
146 addrlen = 16;
147
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
156 break;
157 }
158#endif
159 default:
160 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
161 ret = 0;
162 goto out;
163 }
164
165 bits = -1;
166 if (p) {
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) {
177 rprintf(FLOG, "malformed mask in %s\n", tok);
178 ret = 0;
179 goto out;
180 }
181#else
182 for (pp = (unsigned char *)p; *pp; pp++) {
183 if (!isascii(*pp) || !isdigit(*pp)) {
184 rprintf(FLOG, "malformed mask in %s\n", tok);
185 ret = 0;
186 goto out;
187 }
188 }
189 bits = atoi(p);
190#endif
191 if (bits == 0) {
192 ret = 1;
193 goto out;
194 }
195 if (bits < 0 || bits > (addrlen << 3)) {
196 rprintf(FLOG, "malformed mask in %s\n", tok);
197 ret = 0;
198 goto out;
199 }
200 }
201 } else {
202 bits = 128;
203 }
204
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;
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
223 strlower(list2);
224 if (host) strlower(host);
225
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}