2 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
20 hosts allow/deny code for rsync
27 static int match_hostname(char *host, char *tok)
29 if (!host || !*host) return 0;
30 return wildmatch(tok, host);
33 static int match_binary(char *b1, char *b2, char *mask, int addrlen)
37 for (i=0; i<addrlen; i++) {
38 if ((b1[i]^b2[i])&mask[i]) {
46 static void make_mask(char *mask, int plen, int addrlen) {
53 memset(mask, 0xff, w);
55 mask[w] = 0xff & (0xff<<(8-b));
57 memset(mask+w+1, 0, addrlen-w-1);
62 static int match_address(char *addr, char *tok)
65 struct addrinfo hints, *resa, *rest;
75 char *a = NULL, *t = NULL;
78 if (!addr || !*addr) return 0;
88 /* Fail quietly if tok is a hostname (not an address) */
89 if (strspn(tok, ".0123456789") != len
91 && strchr(tok, ':') == NULL
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;
106 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
112 gai = getaddrinfo(tok, NULL, &hints, &rest);
116 rprintf(FLOG, "error matching address %s: %s\n",
117 tok, gai_strerror(gai));
122 if (rest->ai_family != resa->ai_family) {
127 switch(resa->ai_family) {
129 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
130 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
138 struct sockaddr_in6 *sin6a, *sin6t;
140 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
141 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
143 a = (char *)&sin6a->sin6_addr;
144 t = (char *)&sin6t->sin6_addr;
148 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
149 if (sin6t->sin6_scope_id &&
150 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
160 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
167 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
175 bits = strtol(p, &ep, 10);
177 rprintf(FLOG, "malformed mask in %s\n", tok);
182 for (pp = (unsigned char *)p; *pp; pp++) {
183 if (!isascii(*pp) || !isdigit(*pp)) {
184 rprintf(FLOG, "malformed mask in %s\n", tok);
195 if (bits < 0 || bits > (addrlen << 3)) {
196 rprintf(FLOG, "malformed mask in %s\n", tok);
206 make_mask(mask, bits, addrlen);
208 ret = match_binary(a, t, mask, addrlen);
216 static int access_match(char *list, char *addr, char *host)
219 char *list2 = strdup(list);
221 if (!list2) out_of_memory("access_match");
224 if (host) strlower(host);
226 for (tok=strtok(list2," ,\t"); tok; tok=strtok(NULL," ,\t")) {
227 if (match_hostname(host, tok) || match_address(addr, tok)) {
237 int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
239 /* if theres no deny list and no allow list then allow access */
240 if ((!deny_list || !*deny_list) && (!allow_list || !*allow_list))
243 /* if there is an allow list but no deny list then allow only hosts
245 if (!deny_list || !*deny_list)
246 return(access_match(allow_list, addr, host));
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));
253 /* if there are both type of list then allow all hosts on the
255 if (access_match(allow_list,addr,host))
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))