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)
31 return wildmatch(tok, host);
34 static int match_binary(char *b1, char *b2, char *mask, int addrlen)
38 for (i = 0; i < addrlen; i++) {
39 if ((b1[i] ^ b2[i]) & mask[i])
46 static void make_mask(char *mask, int plen, int addrlen)
54 memset(mask, 0xff, w);
56 mask[w] = 0xff & (0xff<<(8-b));
58 memset(mask+w+1, 0, addrlen-w-1);
63 static int match_address(char *addr, char *tok)
66 struct addrinfo hints, *resa, *rest;
76 char *a = NULL, *t = NULL;
89 /* Fail quietly if tok is a hostname (not an address) */
90 if (strspn(tok, ".0123456789") != len
92 && strchr(tok, ':') == NULL
100 memset(&hints, 0, sizeof(hints));
101 hints.ai_family = PF_UNSPEC;
102 hints.ai_socktype = SOCK_STREAM;
103 #ifdef AI_NUMERICHOST
104 hints.ai_flags = AI_NUMERICHOST;
107 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
113 gai = getaddrinfo(tok, NULL, &hints, &rest);
117 rprintf(FLOG, "error matching address %s: %s\n",
118 tok, gai_strerror(gai));
123 if (rest->ai_family != resa->ai_family) {
128 switch(resa->ai_family) {
130 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
131 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
139 struct sockaddr_in6 *sin6a, *sin6t;
141 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
142 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
144 a = (char *)&sin6a->sin6_addr;
145 t = (char *)&sin6t->sin6_addr;
149 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
150 if (sin6t->sin6_scope_id &&
151 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
161 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
168 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
176 bits = strtol(p, &ep, 10);
178 rprintf(FLOG, "malformed mask in %s\n", tok);
183 for (pp = (unsigned char *)p; *pp; pp++) {
184 if (!isascii(*pp) || !isdigit(*pp)) {
185 rprintf(FLOG, "malformed mask in %s\n", tok);
196 if (bits < 0 || bits > (addrlen << 3)) {
197 rprintf(FLOG, "malformed mask in %s\n", tok);
207 make_mask(mask, bits, addrlen);
209 ret = match_binary(a, t, mask, addrlen);
217 static int access_match(char *list, char *addr, char *host)
220 char *list2 = strdup(list);
223 out_of_memory("access_match");
229 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
230 if (match_hostname(host, tok) || match_address(addr, tok)) {
240 int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
242 if (allow_list && !*allow_list)
244 if (deny_list && !*deny_list)
247 /* If we match an allow-list item, we always allow access. */
249 if (access_match(allow_list, addr, host))
251 /* For an allow-list w/o a deny-list, disallow non-matches. */
256 /* If we match a deny-list item (and got past any allow-list
257 * items), we always disallow access. */
258 if (deny_list && access_match(deny_list, addr, host))
261 /* Allow all other access. */