blob: b6afce37c8f43c2abd0c4916baabc2ccffe7dd72 [file] [log] [blame]
Andrew Tridgell56c473b1998-05-13 08:03:47 +00001/*
Wayne Davison0f78b812006-04-25 20:23:34 +00002 * Routines to authenticate access to a daemon (hosts allow/deny).
3 *
4 * Copyright (C) 1998 Andrew Tridgell
Wayne Davisonc3b553a2022-01-15 17:21:01 -08005 * Copyright (C) 2004-2022 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00006 *
7 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00008 * 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.
Wayne Davison0f78b812006-04-25 20:23:34 +000011 *
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 *
Wayne Davisone7c67062006-04-25 23:51:12 +000017 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000018 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000019 */
Andrew Tridgell56c473b1998-05-13 08:03:47 +000020
21#include "rsync.h"
Wayne Davison11eb67e2020-06-25 19:59:19 -070022#include "ifuncs.h"
Wayne Davisonec3833c2021-02-01 16:31:28 -080023#ifdef HAVE_NETGROUP_H
24#include <netgroup.h>
25#endif
Andrew Tridgell56c473b1998-05-13 08:03:47 +000026
Wayne Davisonbf4170a2011-01-03 18:59:08 -080027static int allow_forward_dns;
28
29extern const char undetermined_hostname[];
30
31static int match_hostname(const char **host_ptr, const char *addr, const char *tok)
Andrew Tridgell56c473b1998-05-13 08:03:47 +000032{
Wayne Davisonbf4170a2011-01-03 18:59:08 -080033 struct hostent *hp;
34 unsigned int i;
35 const char *host = *host_ptr;
36
Wayne Davison36142822005-01-15 20:06:48 +000037 if (!host || !*host)
38 return 0;
Wayne Davisonbf4170a2011-01-03 18:59:08 -080039
Wayne Davison2f130492020-07-12 19:15:50 -070040#ifdef HAVE_INNETGR
41 if (*tok == '@' && tok[1])
42 return innetgr(tok + 1, host, NULL, NULL);
43#endif
44
Wayne Davisonbf4170a2011-01-03 18:59:08 -080045 /* 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 return 0;
59
60 for (i = 0; hp->h_addr_list[i] != NULL; i++) {
61 if (strcmp(addr, inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]))) == 0) {
62 /* If reverse lookups are off, we'll use the conf-specified
63 * hostname in preference to UNDETERMINED. */
Wayne Davison11eb67e2020-06-25 19:59:19 -070064 if (host == undetermined_hostname)
65 *host_ptr = strdup(tok);
Wayne Davisonbf4170a2011-01-03 18:59:08 -080066 return 1;
67 }
68 }
69
70 return 0;
Andrew Tridgell56c473b1998-05-13 08:03:47 +000071}
72
Wayne Davisondf694f72009-01-15 00:14:51 -080073static int match_binary(const char *b1, const char *b2, const char *mask, int addrlen)
David Dykstrabc2b4962003-01-09 21:14:10 +000074{
75 int i;
76
Wayne Davison36142822005-01-15 20:06:48 +000077 for (i = 0; i < addrlen; i++) {
78 if ((b1[i] ^ b2[i]) & mask[i])
David Dykstrabc2b4962003-01-09 21:14:10 +000079 return 0;
David Dykstrabc2b4962003-01-09 21:14:10 +000080 }
81
82 return 1;
83}
84
Wayne Davison36142822005-01-15 20:06:48 +000085static void make_mask(char *mask, int plen, int addrlen)
86{
David Dykstrabc2b4962003-01-09 21:14:10 +000087 int w, b;
88
89 w = plen >> 3;
90 b = plen & 0x7;
91
92 if (w)
93 memset(mask, 0xff, w);
David Dykstra7bc82182003-01-20 13:46:28 +000094 if (w < addrlen)
95 mask[w] = 0xff & (0xff<<(8-b));
David Dykstrabc2b4962003-01-09 21:14:10 +000096 if (w+1 < addrlen)
97 memset(mask+w+1, 0, addrlen-w-1);
98
99 return;
100}
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000101
Wayne Davisondf694f72009-01-15 00:14:51 -0800102static int match_address(const char *addr, const char *tok)
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000103{
104 char *p;
David Dykstrabc2b4962003-01-09 21:14:10 +0000105 struct addrinfo hints, *resa, *rest;
106 int gai;
107 int ret = 0;
108 int addrlen = 0;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000109#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000110 long int bits;
111#else
112 int bits;
113#endif
114 char mask[16];
115 char *a = NULL, *t = NULL;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000116
Wayne Davison36142822005-01-15 20:06:48 +0000117 if (!addr || !*addr)
118 return 0;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000119
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000120 p = strchr(tok,'/');
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800121 if (p)
Wayne Davison32f60a62003-07-07 18:25:01 +0000122 *p = '\0';
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000123
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800124 /* Fail quietly if tok is a hostname, not an address. */
125 if (tok[strspn(tok, ".0123456789")] && strchr(tok, ':') == NULL) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000126 if (p)
127 *p = '/';
128 return 0;
129 }
Wayne Davison70a60512003-07-05 07:39:57 +0000130
David Dykstrabc2b4962003-01-09 21:14:10 +0000131 memset(&hints, 0, sizeof(hints));
132 hints.ai_family = PF_UNSPEC;
133 hints.ai_socktype = SOCK_STREAM;
David Dykstra8d2aad42003-01-09 21:30:24 +0000134#ifdef AI_NUMERICHOST
David Dykstrabc2b4962003-01-09 21:14:10 +0000135 hints.ai_flags = AI_NUMERICHOST;
David Dykstra8d2aad42003-01-09 21:30:24 +0000136#endif
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000137
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000138 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
139 if (p)
140 *p = '/';
141 return 0;
142 }
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000143
David Dykstrabc2b4962003-01-09 21:14:10 +0000144 gai = getaddrinfo(tok, NULL, &hints, &rest);
145 if (p)
146 *p++ = '/';
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000147 if (gai != 0) {
148 rprintf(FLOG, "error matching address %s: %s\n",
149 tok, gai_strerror(gai));
David Dykstrabc2b4962003-01-09 21:14:10 +0000150 freeaddrinfo(resa);
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000151 return 0;
152 }
153
David Dykstrabc2b4962003-01-09 21:14:10 +0000154 if (rest->ai_family != resa->ai_family) {
155 ret = 0;
156 goto out;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000157 }
158
David Dykstrabc2b4962003-01-09 21:14:10 +0000159 switch(resa->ai_family) {
160 case PF_INET:
161 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
162 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
163 addrlen = 4;
164
165 break;
166
Wayne Davison4f5b0752005-02-14 00:53:43 +0000167#ifdef INET6
Wayne Davisone63ff702020-06-13 18:19:12 -0700168 case PF_INET6: {
David Dykstrabc2b4962003-01-09 21:14:10 +0000169 struct sockaddr_in6 *sin6a, *sin6t;
170
171 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
172 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
173
174 a = (char *)&sin6a->sin6_addr;
175 t = (char *)&sin6t->sin6_addr;
176
David Dykstra7bc82182003-01-20 13:46:28 +0000177 addrlen = 16;
178
Wayne Davison4f5b0752005-02-14 00:53:43 +0000179#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
Wayne Davisone63ff702020-06-13 18:19:12 -0700180 if (sin6t->sin6_scope_id && sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
David Dykstrabc2b4962003-01-09 21:14:10 +0000181 ret = 0;
182 goto out;
183 }
184#endif
185
David Dykstrabc2b4962003-01-09 21:14:10 +0000186 break;
Wayne Davisone63ff702020-06-13 18:19:12 -0700187 }
David Dykstrabc2b4962003-01-09 21:14:10 +0000188#endif
189 default:
Wayne Davisone63ff702020-06-13 18:19:12 -0700190 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
191 ret = 0;
192 goto out;
David Dykstrabc2b4962003-01-09 21:14:10 +0000193 }
194
195 bits = -1;
196 if (p) {
197 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
Wayne Davison4f5b0752005-02-14 00:53:43 +0000198#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000199 char *ep = NULL;
200#else
201 unsigned char *pp;
202#endif
203
Wayne Davison4f5b0752005-02-14 00:53:43 +0000204#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000205 bits = strtol(p, &ep, 10);
206 if (!*p || *ep) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000207 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000208 ret = 0;
209 goto out;
210 }
211#else
212 for (pp = (unsigned char *)p; *pp; pp++) {
213 if (!isascii(*pp) || !isdigit(*pp)) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000214 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000215 ret = 0;
216 goto out;
217 }
218 }
219 bits = atoi(p);
220#endif
221 if (bits == 0) {
222 ret = 1;
223 goto out;
224 }
225 if (bits < 0 || bits > (addrlen << 3)) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000226 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000227 ret = 0;
228 goto out;
229 }
230 }
231 } else {
232 bits = 128;
233 }
234
235 if (bits >= 0)
236 make_mask(mask, bits, addrlen);
237
238 ret = match_binary(a, t, mask, addrlen);
239
Wayne Davison2997e9f2005-11-10 16:42:46 +0000240 out:
David Dykstrabc2b4962003-01-09 21:14:10 +0000241 freeaddrinfo(resa);
242 freeaddrinfo(rest);
243 return ret;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000244}
245
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800246static int access_match(const char *list, const char *addr, const char **host_ptr)
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000247{
248 char *tok;
249 char *list2 = strdup(list);
250
Andrew Tridgell5a96ee01998-05-14 04:31:03 +0000251 strlower(list2);
Andrew Tridgell5a96ee01998-05-14 04:31:03 +0000252
Wayne Davison36142822005-01-15 20:06:48 +0000253 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800254 if (match_hostname(host_ptr, addr, tok) || match_address(addr, tok)) {
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000255 free(list2);
256 return 1;
257 }
258 }
259
260 free(list2);
261 return 0;
262}
263
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800264int allow_access(const char *addr, const char **host_ptr, int i)
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000265{
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800266 const char *allow_list = lp_hosts_allow(i);
267 const char *deny_list = lp_hosts_deny(i);
268
Wayne Davison36142822005-01-15 20:06:48 +0000269 if (allow_list && !*allow_list)
270 allow_list = NULL;
271 if (deny_list && !*deny_list)
272 deny_list = NULL;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000273
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800274 allow_forward_dns = lp_forward_lookup(i);
275
Wayne Davison36142822005-01-15 20:06:48 +0000276 /* If we match an allow-list item, we always allow access. */
277 if (allow_list) {
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800278 if (access_match(allow_list, addr, host_ptr))
Wayne Davison36142822005-01-15 20:06:48 +0000279 return 1;
280 /* For an allow-list w/o a deny-list, disallow non-matches. */
281 if (!deny_list)
282 return 0;
283 }
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000284
Wayne Davison36142822005-01-15 20:06:48 +0000285 /* If we match a deny-list item (and got past any allow-list
286 * items), we always disallow access. */
Wayne Davisonbf4170a2011-01-03 18:59:08 -0800287 if (deny_list && access_match(deny_list, addr, host_ptr))
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000288 return 0;
289
Wayne Davison36142822005-01-15 20:06:48 +0000290 /* Allow all other access. */
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000291 return 1;
292}