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