blob: 89f1256e00a0e849a0850287cb25ef73a91cc693 [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
5 * Copyright (C) 2004, 2005 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 2 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
Andrew Tridgell56c473b1998-05-13 08:03:47 +000021
22#include "rsync.h"
23
Andrew Tridgell56c473b1998-05-13 08:03:47 +000024static int match_hostname(char *host, char *tok)
25{
Wayne Davison36142822005-01-15 20:06:48 +000026 if (!host || !*host)
27 return 0;
Wayne Davisonfe332032003-07-30 06:12:27 +000028 return wildmatch(tok, host);
Andrew Tridgell56c473b1998-05-13 08:03:47 +000029}
30
David Dykstrabc2b4962003-01-09 21:14:10 +000031static int match_binary(char *b1, char *b2, char *mask, int addrlen)
32{
33 int i;
34
Wayne Davison36142822005-01-15 20:06:48 +000035 for (i = 0; i < addrlen; i++) {
36 if ((b1[i] ^ b2[i]) & mask[i])
David Dykstrabc2b4962003-01-09 21:14:10 +000037 return 0;
David Dykstrabc2b4962003-01-09 21:14:10 +000038 }
39
40 return 1;
41}
42
Wayne Davison36142822005-01-15 20:06:48 +000043static void make_mask(char *mask, int plen, int addrlen)
44{
David Dykstrabc2b4962003-01-09 21:14:10 +000045 int w, b;
46
47 w = plen >> 3;
48 b = plen & 0x7;
49
50 if (w)
51 memset(mask, 0xff, w);
David Dykstra7bc82182003-01-20 13:46:28 +000052 if (w < addrlen)
53 mask[w] = 0xff & (0xff<<(8-b));
David Dykstrabc2b4962003-01-09 21:14:10 +000054 if (w+1 < addrlen)
55 memset(mask+w+1, 0, addrlen-w-1);
56
57 return;
58}
Andrew Tridgell56c473b1998-05-13 08:03:47 +000059
60static int match_address(char *addr, char *tok)
61{
62 char *p;
David Dykstrabc2b4962003-01-09 21:14:10 +000063 struct addrinfo hints, *resa, *rest;
64 int gai;
65 int ret = 0;
66 int addrlen = 0;
Wayne Davison4f5b0752005-02-14 00:53:43 +000067#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +000068 long int bits;
69#else
70 int bits;
71#endif
72 char mask[16];
73 char *a = NULL, *t = NULL;
Wayne Davison32f60a62003-07-07 18:25:01 +000074 unsigned int len;
Andrew Tridgell56c473b1998-05-13 08:03:47 +000075
Wayne Davison36142822005-01-15 20:06:48 +000076 if (!addr || !*addr)
77 return 0;
Andrew Tridgell56c473b1998-05-13 08:03:47 +000078
Andrew Tridgell56c473b1998-05-13 08:03:47 +000079 p = strchr(tok,'/');
Wayne Davison32f60a62003-07-07 18:25:01 +000080 if (p) {
81 *p = '\0';
82 len = p - tok;
Wayne Davison36142822005-01-15 20:06:48 +000083 } else
Wayne Davison32f60a62003-07-07 18:25:01 +000084 len = strlen(tok);
Andrew Tridgell56c473b1998-05-13 08:03:47 +000085
Wayne Davison32f60a62003-07-07 18:25:01 +000086 /* Fail quietly if tok is a hostname (not an address) */
Wayne Davisonb49d3812003-07-07 19:37:58 +000087 if (strspn(tok, ".0123456789") != len
Wayne Davison4f5b0752005-02-14 00:53:43 +000088#ifdef INET6
Wayne Davisonbe7cf822004-09-24 16:50:07 +000089 && strchr(tok, ':') == NULL
Wayne Davison32f60a62003-07-07 18:25:01 +000090#endif
Wayne Davisonbe7cf822004-09-24 16:50:07 +000091 ) {
92 if (p)
93 *p = '/';
94 return 0;
95 }
Wayne Davison70a60512003-07-05 07:39:57 +000096
David Dykstrabc2b4962003-01-09 21:14:10 +000097 memset(&hints, 0, sizeof(hints));
98 hints.ai_family = PF_UNSPEC;
99 hints.ai_socktype = SOCK_STREAM;
David Dykstra8d2aad42003-01-09 21:30:24 +0000100#ifdef AI_NUMERICHOST
David Dykstrabc2b4962003-01-09 21:14:10 +0000101 hints.ai_flags = AI_NUMERICHOST;
David Dykstra8d2aad42003-01-09 21:30:24 +0000102#endif
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000103
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000104 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
105 if (p)
106 *p = '/';
107 return 0;
108 }
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000109
David Dykstrabc2b4962003-01-09 21:14:10 +0000110 gai = getaddrinfo(tok, NULL, &hints, &rest);
111 if (p)
112 *p++ = '/';
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000113 if (gai != 0) {
114 rprintf(FLOG, "error matching address %s: %s\n",
115 tok, gai_strerror(gai));
David Dykstrabc2b4962003-01-09 21:14:10 +0000116 freeaddrinfo(resa);
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000117 return 0;
118 }
119
David Dykstrabc2b4962003-01-09 21:14:10 +0000120 if (rest->ai_family != resa->ai_family) {
121 ret = 0;
122 goto out;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000123 }
124
David Dykstrabc2b4962003-01-09 21:14:10 +0000125 switch(resa->ai_family) {
126 case PF_INET:
127 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
128 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
129 addrlen = 4;
130
131 break;
132
Wayne Davison4f5b0752005-02-14 00:53:43 +0000133#ifdef INET6
David Dykstrabc2b4962003-01-09 21:14:10 +0000134 case PF_INET6:
135 {
136 struct sockaddr_in6 *sin6a, *sin6t;
137
138 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
139 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
140
141 a = (char *)&sin6a->sin6_addr;
142 t = (char *)&sin6t->sin6_addr;
143
David Dykstra7bc82182003-01-20 13:46:28 +0000144 addrlen = 16;
145
Wayne Davison4f5b0752005-02-14 00:53:43 +0000146#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
David Dykstrabc2b4962003-01-09 21:14:10 +0000147 if (sin6t->sin6_scope_id &&
148 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
149 ret = 0;
150 goto out;
151 }
152#endif
153
David Dykstrabc2b4962003-01-09 21:14:10 +0000154 break;
155 }
156#endif
157 default:
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000158 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
David Dykstrabc2b4962003-01-09 21:14:10 +0000159 ret = 0;
160 goto out;
161 }
162
163 bits = -1;
164 if (p) {
165 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
Wayne Davison4f5b0752005-02-14 00:53:43 +0000166#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000167 char *ep = NULL;
168#else
169 unsigned char *pp;
170#endif
171
Wayne Davison4f5b0752005-02-14 00:53:43 +0000172#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000173 bits = strtol(p, &ep, 10);
174 if (!*p || *ep) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000175 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000176 ret = 0;
177 goto out;
178 }
179#else
180 for (pp = (unsigned char *)p; *pp; pp++) {
181 if (!isascii(*pp) || !isdigit(*pp)) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000182 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000183 ret = 0;
184 goto out;
185 }
186 }
187 bits = atoi(p);
188#endif
189 if (bits == 0) {
190 ret = 1;
191 goto out;
192 }
193 if (bits < 0 || bits > (addrlen << 3)) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000194 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000195 ret = 0;
196 goto out;
197 }
198 }
199 } else {
200 bits = 128;
201 }
202
203 if (bits >= 0)
204 make_mask(mask, bits, addrlen);
205
206 ret = match_binary(a, t, mask, addrlen);
207
Wayne Davison2997e9f2005-11-10 16:42:46 +0000208 out:
David Dykstrabc2b4962003-01-09 21:14:10 +0000209 freeaddrinfo(resa);
210 freeaddrinfo(rest);
211 return ret;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000212}
213
214static int access_match(char *list, char *addr, char *host)
215{
216 char *tok;
217 char *list2 = strdup(list);
218
Wayne Davison36142822005-01-15 20:06:48 +0000219 if (!list2)
220 out_of_memory("access_match");
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000221
Andrew Tridgell5a96ee01998-05-14 04:31:03 +0000222 strlower(list2);
Wayne Davison36142822005-01-15 20:06:48 +0000223 if (host)
224 strlower(host);
Andrew Tridgell5a96ee01998-05-14 04:31:03 +0000225
Wayne Davison36142822005-01-15 20:06:48 +0000226 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000227 if (match_hostname(host, tok) || match_address(addr, tok)) {
228 free(list2);
229 return 1;
230 }
231 }
232
233 free(list2);
234 return 0;
235}
236
237int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
238{
Wayne Davison36142822005-01-15 20:06:48 +0000239 if (allow_list && !*allow_list)
240 allow_list = NULL;
241 if (deny_list && !*deny_list)
242 deny_list = NULL;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000243
Wayne Davison36142822005-01-15 20:06:48 +0000244 /* If we match an allow-list item, we always allow access. */
245 if (allow_list) {
246 if (access_match(allow_list, addr, host))
247 return 1;
248 /* For an allow-list w/o a deny-list, disallow non-matches. */
249 if (!deny_list)
250 return 0;
251 }
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000252
Wayne Davison36142822005-01-15 20:06:48 +0000253 /* If we match a deny-list item (and got past any allow-list
254 * items), we always disallow access. */
255 if (deny_list && access_match(deny_list, addr, host))
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000256 return 0;
257
Wayne Davison36142822005-01-15 20:06:48 +0000258 /* Allow all other access. */
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000259 return 1;
260}