Debian Codegraph @

access.c

raw symbols SCIP
1 /*
2 * Routines to authenticate access to a daemon (hosts allow/deny).
3 *
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2004-2022 Wayne Davison
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
19 */
20
21 #include "rsync.h"
22 #include "ifuncs.h"
23 #ifdef HAVE_NETGROUP_H
24 #include <netgroup.h>
25 #endif
26
27 static int allow_forward_dns;
28
29 extern const char undetermined_hostname[];
30
31 static int match_hostname(const char **host_ptr, const char *addr, const char *tok, int deny)
32 {
33 struct hostent *hp;
34 unsigned int i;
35 const char *host = *host_ptr;
36
37 if (!host || !*host)
38 return 0;
39
40 #ifdef HAVE_INNETGR
41 if (*tok == '@' && tok[1])
42 return innetgr(tok + 1, host, NULL, NULL);
43 #endif
44
45 /* First check if the reverse-DNS-determined hostname matches. */
46 if (iwildmatch(tok, host))
47 return 1;
48
49 if (!allow_forward_dns)
50 return 0;
51
52 /* Fail quietly if tok is an address or wildcarded entry, not a simple hostname. */
53 if (!tok[strspn(tok, ".0123456789")] || tok[strcspn(tok, ":/*?[")])
54 return 0;
55
56 /* Now try forward-DNS on the token (config-specified hostname) and see if the IP matches. */
57 if (!(hp = gethostbyname(tok))) {
58 /* A deny-list hostname token we cannot resolve must fail CLOSED:
59 * we can't prove the peer isn't the denied host, so treat the
60 * unresolvable token as a match (deny). Allow-list tokens keep
61 * failing as a non-match. Sibling of CVE-2026-43617, which fixed
62 * only the reverse-lookup path. */
63 return deny;
64 }
65
66 for (i = 0; hp->h_addr_list[i] != NULL; i++) {
67 if (strcmp(addr, inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]))) == 0) {
68 /* If reverse lookups are off, we'll use the conf-specified
69 * hostname in preference to UNDETERMINED. */
70 if (host == undetermined_hostname)
71 *host_ptr = strdup(tok);
72 return 1;
73 }
74 }
75
76 return 0;
77 }
78
79 static int match_binary(const char *b1, const char *b2, const char *mask, int addrlen)
80 {
81 int i;
82
83 for (i = 0; i < addrlen; i++) {
84 if ((b1[i] ^ b2[i]) & mask[i])
85 return 0;
86 }
87
88 return 1;
89 }
90
91 static void make_mask(char *mask, int plen, int addrlen)
92 {
93 int w, b;
94
95 w = plen >> 3;
96 b = plen & 0x7;
97
98 if (w)
99 memset(mask, 0xff, w);
100 if (w < addrlen)
101 mask[w] = 0xff & (0xff<<(8-b));
102 if (w+1 < addrlen)
103 memset(mask+w+1, 0, addrlen-w-1);
104
105 return;
106 }
107
108 static int match_address(const char *addr, char *tok)
109 {
110 char *p;
111 struct addrinfo hints, *resa, *rest;
112 int gai;
113 int ret = 0;
114 int addrlen = 0;
115 #ifdef HAVE_STRTOL
116 long int bits;
117 #else
118 int bits;
119 #endif
120 char mask[16];
121 char *a = NULL, *t = NULL;
122
123 if (!addr || !*addr)
124 return 0;
125
126 p = strchr(tok,'/');
127 if (p)
128 *p = '\0';
129
130 /* Fail quietly if tok is a hostname, not an address. */
131 if (tok[strspn(tok, ".0123456789")] && strchr(tok, ':') == NULL) {
132 if (p)
133 *p = '/';
134 return 0;
135 }
136
137 memset(&hints, 0, sizeof(hints));
138 hints.ai_family = PF_UNSPEC;
139 hints.ai_socktype = SOCK_STREAM;
140 #ifdef AI_NUMERICHOST
141 hints.ai_flags = AI_NUMERICHOST;
142 #endif
143
144 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
145 if (p)
146 *p = '/';
147 return 0;
148 }
149
150 gai = getaddrinfo(tok, NULL, &hints, &rest);
151 if (p)
152 *p++ = '/';
153 if (gai != 0) {
154 rprintf(FLOG, "error matching address %s: %s\n",
155 tok, gai_strerror(gai));
156 freeaddrinfo(resa);
157 return 0;
158 }
159
160 if (rest->ai_family != resa->ai_family) {
161 ret = 0;
162 goto out;
163 }
164
165 switch(resa->ai_family) {
166 case PF_INET:
167 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
168 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
169 addrlen = 4;
170
171 break;
172
173 #ifdef INET6
174 case PF_INET6: {
175 struct sockaddr_in6 *sin6a, *sin6t;
176
177 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
178 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
179
180 a = (char *)&sin6a->sin6_addr;
181 t = (char *)&sin6t->sin6_addr;
182
183 addrlen = 16;
184
185 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
186 if (sin6t->sin6_scope_id && sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
187 ret = 0;
188 goto out;
189 }
190 #endif
191
192 break;
193 }
194 #endif
195 default:
196 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
197 ret = 0;
198 goto out;
199 }
200
201 bits = -1;
202 if (p) {
203 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
204 #ifdef HAVE_STRTOL
205 char *ep = NULL;
206 #else
207 unsigned char *pp;
208 #endif
209
210 #ifdef HAVE_STRTOL
211 bits = strtol(p, &ep, 10);
212 if (!*p || *ep) {
213 rprintf(FLOG, "malformed mask in %s\n", tok);
214 ret = 0;
215 goto out;
216 }
217 #else
218 for (pp = (unsigned char *)p; *pp; pp++) {
219 if (!isascii(*pp) || !isdigit(*pp)) {
220 rprintf(FLOG, "malformed mask in %s\n", tok);
221 ret = 0;
222 goto out;
223 }
224 }
225 bits = atoi(p);
226 #endif
227 if (bits == 0) {
228 ret = 1;
229 goto out;
230 }
231 if (bits < 0 || bits > (addrlen << 3)) {
232 rprintf(FLOG, "malformed mask in %s\n", tok);
233 ret = 0;
234 goto out;
235 }
236 }
237 } else {
238 bits = 128;
239 }
240
241 if (bits >= 0)
242 make_mask(mask, bits, addrlen);
243
244 ret = match_binary(a, t, mask, addrlen);
245
246 out:
247 freeaddrinfo(resa);
248 freeaddrinfo(rest);
249 return ret;
250 }
251
252 static int access_match(const char *list, const char *addr, const char **host_ptr, int deny)
253 {
254 char *tok;
255 char *list2 = strdup(list);
256
257 strlower(list2);
258
259 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
260 if (match_hostname(host_ptr, addr, tok, deny) || match_address(addr, tok)) {
261 free(list2);
262 return 1;
263 }
264 }
265
266 free(list2);
267 return 0;
268 }
269
270 int allow_access(const char *addr, const char **host_ptr, int i)
271 {
272 const char *allow_list = lp_hosts_allow(i);
273 const char *deny_list = lp_hosts_deny(i);
274
275 if (allow_list && !*allow_list)
276 allow_list = NULL;
277 if (deny_list && !*deny_list)
278 deny_list = NULL;
279
280 allow_forward_dns = lp_forward_lookup(i);
281
282 /* If we match an allow-list item, we always allow access. */
283 if (allow_list) {
284 if (access_match(allow_list, addr, host_ptr, 0))
285 return 1;
286 /* For an allow-list w/o a deny-list, disallow non-matches. */
287 if (!deny_list)
288 return 0;
289 }
290
291 /* If we match a deny-list item (and got past any allow-list
292 * items), we always disallow access. */
293 if (deny_list && access_match(deny_list, addr, host_ptr, 1))
294 return 0;
295
296 /* Allow all other access. */
297 return 1;
298 }
299
300 int allow_proxy_protocol_peer(const char *list, const char *addr, const char **host_ptr)
301 {
302 if (!list || !*list)
303 return 0;
304 allow_forward_dns = 0;
305 return access_match(list, addr, host_ptr, 0);
306 }