Switching to GPL 3.
[rsync/rsync.git] / access.c
CommitLineData
56c473b7 1/*
0f78b815
WD
2 * Routines to authenticate access to a daemon (hosts allow/deny).
3 *
4 * Copyright (C) 1998 Andrew Tridgell
ba2133d6 5 * Copyright (C) 2004-2007 Wayne Davison
0f78b815
WD
6 *
7 * This program is free software; you can redistribute it and/or modify
4fd842f9 8 * it under the terms of the GNU General Public License version 3 as
ba2133d6 9 * published by the Free Software Foundation.
0f78b815
WD
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
e7c67065 16 * You should have received a copy of the GNU General Public License along
4fd842f9 17 * with this program; if not, visit the http://fsf.org website.
0f78b815 18 */
56c473b7
AT
19
20#include "rsync.h"
21
56c473b7
AT
22static int match_hostname(char *host, char *tok)
23{
36142821
WD
24 if (!host || !*host)
25 return 0;
fe332038 26 return wildmatch(tok, host);
56c473b7
AT
27}
28
bc2b4963
DD
29static int match_binary(char *b1, char *b2, char *mask, int addrlen)
30{
31 int i;
32
36142821
WD
33 for (i = 0; i < addrlen; i++) {
34 if ((b1[i] ^ b2[i]) & mask[i])
bc2b4963 35 return 0;
bc2b4963
DD
36 }
37
38 return 1;
39}
40
36142821
WD
41static void make_mask(char *mask, int plen, int addrlen)
42{
bc2b4963
DD
43 int w, b;
44
45 w = plen >> 3;
46 b = plen & 0x7;
47
48 if (w)
49 memset(mask, 0xff, w);
7bc8218d
DD
50 if (w < addrlen)
51 mask[w] = 0xff & (0xff<<(8-b));
bc2b4963
DD
52 if (w+1 < addrlen)
53 memset(mask+w+1, 0, addrlen-w-1);
54
55 return;
56}
56c473b7
AT
57
58static int match_address(char *addr, char *tok)
59{
60 char *p;
bc2b4963
DD
61 struct addrinfo hints, *resa, *rest;
62 int gai;
63 int ret = 0;
64 int addrlen = 0;
4f5b0756 65#ifdef HAVE_STRTOL
bc2b4963
DD
66 long int bits;
67#else
68 int bits;
69#endif
70 char mask[16];
71 char *a = NULL, *t = NULL;
32f60a6e 72 unsigned int len;
56c473b7 73
36142821
WD
74 if (!addr || !*addr)
75 return 0;
56c473b7 76
56c473b7 77 p = strchr(tok,'/');
32f60a6e
WD
78 if (p) {
79 *p = '\0';
80 len = p - tok;
36142821 81 } else
32f60a6e 82 len = strlen(tok);
56c473b7 83
32f60a6e 84 /* Fail quietly if tok is a hostname (not an address) */
b49d381d 85 if (strspn(tok, ".0123456789") != len
4f5b0756 86#ifdef INET6
be7cf822 87 && strchr(tok, ':') == NULL
32f60a6e 88#endif
be7cf822
WD
89 ) {
90 if (p)
91 *p = '/';
92 return 0;
93 }
70a6051c 94
bc2b4963
DD
95 memset(&hints, 0, sizeof(hints));
96 hints.ai_family = PF_UNSPEC;
97 hints.ai_socktype = SOCK_STREAM;
8d2aad49 98#ifdef AI_NUMERICHOST
bc2b4963 99 hints.ai_flags = AI_NUMERICHOST;
8d2aad49 100#endif
56c473b7 101
be7cf822
WD
102 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
103 if (p)
104 *p = '/';
105 return 0;
106 }
56c473b7 107
bc2b4963
DD
108 gai = getaddrinfo(tok, NULL, &hints, &rest);
109 if (p)
110 *p++ = '/';
be7cf822
WD
111 if (gai != 0) {
112 rprintf(FLOG, "error matching address %s: %s\n",
113 tok, gai_strerror(gai));
bc2b4963 114 freeaddrinfo(resa);
56c473b7
AT
115 return 0;
116 }
117
bc2b4963
DD
118 if (rest->ai_family != resa->ai_family) {
119 ret = 0;
120 goto out;
121 }
122
123 switch(resa->ai_family) {
124 case PF_INET:
125 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
126 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
127 addrlen = 4;
128
129 break;
130
4f5b0756 131#ifdef INET6
bc2b4963
DD
132 case PF_INET6:
133 {
134 struct sockaddr_in6 *sin6a, *sin6t;
56c473b7 135
bc2b4963
DD
136 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
137 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
138
139 a = (char *)&sin6a->sin6_addr;
140 t = (char *)&sin6t->sin6_addr;
141
7bc8218d
DD
142 addrlen = 16;
143
4f5b0756 144#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
bc2b4963
DD
145 if (sin6t->sin6_scope_id &&
146 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
147 ret = 0;
148 goto out;
149 }
150#endif
151
bc2b4963
DD
152 break;
153 }
154#endif
155 default:
be7cf822 156 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
bc2b4963
DD
157 ret = 0;
158 goto out;
159 }
160
161 bits = -1;
56c473b7 162 if (p) {
bc2b4963 163 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
4f5b0756 164#ifdef HAVE_STRTOL
bc2b4963
DD
165 char *ep = NULL;
166#else
167 unsigned char *pp;
168#endif
169
4f5b0756 170#ifdef HAVE_STRTOL
bc2b4963
DD
171 bits = strtol(p, &ep, 10);
172 if (!*p || *ep) {
be7cf822 173 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
174 ret = 0;
175 goto out;
176 }
177#else
178 for (pp = (unsigned char *)p; *pp; pp++) {
179 if (!isascii(*pp) || !isdigit(*pp)) {
be7cf822 180 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
181 ret = 0;
182 goto out;
183 }
56c473b7 184 }
bc2b4963
DD
185 bits = atoi(p);
186#endif
187 if (bits == 0) {
188 ret = 1;
189 goto out;
190 }
191 if (bits < 0 || bits > (addrlen << 3)) {
be7cf822 192 rprintf(FLOG, "malformed mask in %s\n", tok);
bc2b4963
DD
193 ret = 0;
194 goto out;
56c473b7 195 }
56c473b7 196 }
bc2b4963
DD
197 } else {
198 bits = 128;
56c473b7
AT
199 }
200
bc2b4963
DD
201 if (bits >= 0)
202 make_mask(mask, bits, addrlen);
203
204 ret = match_binary(a, t, mask, addrlen);
205
2997e9f7 206 out:
bc2b4963
DD
207 freeaddrinfo(resa);
208 freeaddrinfo(rest);
209 return ret;
56c473b7
AT
210}
211
212static int access_match(char *list, char *addr, char *host)
213{
214 char *tok;
215 char *list2 = strdup(list);
216
36142821
WD
217 if (!list2)
218 out_of_memory("access_match");
56c473b7 219
5a96ee05 220 strlower(list2);
36142821
WD
221 if (host)
222 strlower(host);
5a96ee05 223
36142821 224 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
56c473b7
AT
225 if (match_hostname(host, tok) || match_address(addr, tok)) {
226 free(list2);
227 return 1;
228 }
229 }
230
231 free(list2);
232 return 0;
233}
234
235int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
236{
36142821
WD
237 if (allow_list && !*allow_list)
238 allow_list = NULL;
239 if (deny_list && !*deny_list)
240 deny_list = NULL;
241
242 /* If we match an allow-list item, we always allow access. */
243 if (allow_list) {
244 if (access_match(allow_list, addr, host))
245 return 1;
246 /* For an allow-list w/o a deny-list, disallow non-matches. */
247 if (!deny_list)
248 return 0;
249 }
250
251 /* If we match a deny-list item (and got past any allow-list
252 * items), we always disallow access. */
253 if (deny_list && access_match(deny_list, addr, host))
56c473b7
AT
254 return 0;
255
36142821 256 /* Allow all other access. */
56c473b7
AT
257 return 1;
258}