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