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