blob: 0dd8cb772aa81d11a2e13e4f918bb536388a5372 [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 Davisonba2133d2007-02-04 14:54:58 +00005 * Copyright (C) 2004-2007 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00006 *
7 * This program is free software; you can redistribute it and/or modify
Wayne Davison4fd842f2007-07-07 05:33:14 +00008 * it under the terms of the GNU General Public License version 3 as
Wayne Davisonba2133d2007-02-04 14:54:58 +00009 * published by the Free Software Foundation.
Wayne Davison0f78b812006-04-25 20:23:34 +000010 *
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 *
Wayne Davisone7c67062006-04-25 23:51:12 +000016 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000017 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000018 */
Andrew Tridgell56c473b1998-05-13 08:03:47 +000019
20#include "rsync.h"
21
Andrew Tridgell56c473b1998-05-13 08:03:47 +000022static int match_hostname(char *host, char *tok)
23{
Wayne Davison36142822005-01-15 20:06:48 +000024 if (!host || !*host)
25 return 0;
Wayne Davisonfe332032003-07-30 06:12:27 +000026 return wildmatch(tok, host);
Andrew Tridgell56c473b1998-05-13 08:03:47 +000027}
28
David Dykstrabc2b4962003-01-09 21:14:10 +000029static int match_binary(char *b1, char *b2, char *mask, int addrlen)
30{
31 int i;
32
Wayne Davison36142822005-01-15 20:06:48 +000033 for (i = 0; i < addrlen; i++) {
34 if ((b1[i] ^ b2[i]) & mask[i])
David Dykstrabc2b4962003-01-09 21:14:10 +000035 return 0;
David Dykstrabc2b4962003-01-09 21:14:10 +000036 }
37
38 return 1;
39}
40
Wayne Davison36142822005-01-15 20:06:48 +000041static void make_mask(char *mask, int plen, int addrlen)
42{
David Dykstrabc2b4962003-01-09 21:14:10 +000043 int w, b;
44
45 w = plen >> 3;
46 b = plen & 0x7;
47
48 if (w)
49 memset(mask, 0xff, w);
David Dykstra7bc82182003-01-20 13:46:28 +000050 if (w < addrlen)
51 mask[w] = 0xff & (0xff<<(8-b));
David Dykstrabc2b4962003-01-09 21:14:10 +000052 if (w+1 < addrlen)
53 memset(mask+w+1, 0, addrlen-w-1);
54
55 return;
56}
Andrew Tridgell56c473b1998-05-13 08:03:47 +000057
58static int match_address(char *addr, char *tok)
59{
60 char *p;
David Dykstrabc2b4962003-01-09 21:14:10 +000061 struct addrinfo hints, *resa, *rest;
62 int gai;
63 int ret = 0;
64 int addrlen = 0;
Wayne Davison4f5b0752005-02-14 00:53:43 +000065#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +000066 long int bits;
67#else
68 int bits;
69#endif
70 char mask[16];
71 char *a = NULL, *t = NULL;
Wayne Davison32f60a62003-07-07 18:25:01 +000072 unsigned int len;
Andrew Tridgell56c473b1998-05-13 08:03:47 +000073
Wayne Davison36142822005-01-15 20:06:48 +000074 if (!addr || !*addr)
75 return 0;
Andrew Tridgell56c473b1998-05-13 08:03:47 +000076
Andrew Tridgell56c473b1998-05-13 08:03:47 +000077 p = strchr(tok,'/');
Wayne Davison32f60a62003-07-07 18:25:01 +000078 if (p) {
79 *p = '\0';
80 len = p - tok;
Wayne Davison36142822005-01-15 20:06:48 +000081 } else
Wayne Davison32f60a62003-07-07 18:25:01 +000082 len = strlen(tok);
Andrew Tridgell56c473b1998-05-13 08:03:47 +000083
Wayne Davison32f60a62003-07-07 18:25:01 +000084 /* Fail quietly if tok is a hostname (not an address) */
Wayne Davisonb49d3812003-07-07 19:37:58 +000085 if (strspn(tok, ".0123456789") != len
Wayne Davison4f5b0752005-02-14 00:53:43 +000086#ifdef INET6
Wayne Davisonbe7cf822004-09-24 16:50:07 +000087 && strchr(tok, ':') == NULL
Wayne Davison32f60a62003-07-07 18:25:01 +000088#endif
Wayne Davisonbe7cf822004-09-24 16:50:07 +000089 ) {
90 if (p)
91 *p = '/';
92 return 0;
93 }
Wayne Davison70a60512003-07-05 07:39:57 +000094
David Dykstrabc2b4962003-01-09 21:14:10 +000095 memset(&hints, 0, sizeof(hints));
96 hints.ai_family = PF_UNSPEC;
97 hints.ai_socktype = SOCK_STREAM;
David Dykstra8d2aad42003-01-09 21:30:24 +000098#ifdef AI_NUMERICHOST
David Dykstrabc2b4962003-01-09 21:14:10 +000099 hints.ai_flags = AI_NUMERICHOST;
David Dykstra8d2aad42003-01-09 21:30:24 +0000100#endif
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000101
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000102 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
103 if (p)
104 *p = '/';
105 return 0;
106 }
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000107
David Dykstrabc2b4962003-01-09 21:14:10 +0000108 gai = getaddrinfo(tok, NULL, &hints, &rest);
109 if (p)
110 *p++ = '/';
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000111 if (gai != 0) {
112 rprintf(FLOG, "error matching address %s: %s\n",
113 tok, gai_strerror(gai));
David Dykstrabc2b4962003-01-09 21:14:10 +0000114 freeaddrinfo(resa);
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000115 return 0;
116 }
117
David Dykstrabc2b4962003-01-09 21:14:10 +0000118 if (rest->ai_family != resa->ai_family) {
119 ret = 0;
120 goto out;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000121 }
122
David Dykstrabc2b4962003-01-09 21:14:10 +0000123 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
Wayne Davison4f5b0752005-02-14 00:53:43 +0000131#ifdef INET6
David Dykstrabc2b4962003-01-09 21:14:10 +0000132 case PF_INET6:
133 {
134 struct sockaddr_in6 *sin6a, *sin6t;
135
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
David Dykstra7bc82182003-01-20 13:46:28 +0000142 addrlen = 16;
143
Wayne Davison4f5b0752005-02-14 00:53:43 +0000144#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
David Dykstrabc2b4962003-01-09 21:14:10 +0000145 if (sin6t->sin6_scope_id &&
146 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
147 ret = 0;
148 goto out;
149 }
150#endif
151
David Dykstrabc2b4962003-01-09 21:14:10 +0000152 break;
153 }
154#endif
155 default:
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000156 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
David Dykstrabc2b4962003-01-09 21:14:10 +0000157 ret = 0;
158 goto out;
159 }
160
161 bits = -1;
162 if (p) {
163 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
Wayne Davison4f5b0752005-02-14 00:53:43 +0000164#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000165 char *ep = NULL;
166#else
167 unsigned char *pp;
168#endif
169
Wayne Davison4f5b0752005-02-14 00:53:43 +0000170#ifdef HAVE_STRTOL
David Dykstrabc2b4962003-01-09 21:14:10 +0000171 bits = strtol(p, &ep, 10);
172 if (!*p || *ep) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000173 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000174 ret = 0;
175 goto out;
176 }
177#else
178 for (pp = (unsigned char *)p; *pp; pp++) {
179 if (!isascii(*pp) || !isdigit(*pp)) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000180 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000181 ret = 0;
182 goto out;
183 }
184 }
185 bits = atoi(p);
186#endif
187 if (bits == 0) {
188 ret = 1;
189 goto out;
190 }
191 if (bits < 0 || bits > (addrlen << 3)) {
Wayne Davisonbe7cf822004-09-24 16:50:07 +0000192 rprintf(FLOG, "malformed mask in %s\n", tok);
David Dykstrabc2b4962003-01-09 21:14:10 +0000193 ret = 0;
194 goto out;
195 }
196 }
197 } else {
198 bits = 128;
199 }
200
201 if (bits >= 0)
202 make_mask(mask, bits, addrlen);
203
204 ret = match_binary(a, t, mask, addrlen);
205
Wayne Davison2997e9f2005-11-10 16:42:46 +0000206 out:
David Dykstrabc2b4962003-01-09 21:14:10 +0000207 freeaddrinfo(resa);
208 freeaddrinfo(rest);
209 return ret;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000210}
211
212static int access_match(char *list, char *addr, char *host)
213{
214 char *tok;
215 char *list2 = strdup(list);
216
Wayne Davison36142822005-01-15 20:06:48 +0000217 if (!list2)
218 out_of_memory("access_match");
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000219
Andrew Tridgell5a96ee01998-05-14 04:31:03 +0000220 strlower(list2);
Wayne Davison36142822005-01-15 20:06:48 +0000221 if (host)
222 strlower(host);
Andrew Tridgell5a96ee01998-05-14 04:31:03 +0000223
Wayne Davison36142822005-01-15 20:06:48 +0000224 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000225 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{
Wayne Davison36142822005-01-15 20:06:48 +0000237 if (allow_list && !*allow_list)
238 allow_list = NULL;
239 if (deny_list && !*deny_list)
240 deny_list = NULL;
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000241
Wayne Davison36142822005-01-15 20:06:48 +0000242 /* 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 }
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000250
Wayne Davison36142822005-01-15 20:06:48 +0000251 /* 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))
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000254 return 0;
255
Wayne Davison36142822005-01-15 20:06:48 +0000256 /* Allow all other access. */
Andrew Tridgell56c473b1998-05-13 08:03:47 +0000257 return 1;
258}