2 * Routines to authenticate access to a daemon (hosts allow/deny).
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2004-2007 Wayne Davison
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3 as
9 * published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, visit the http://fsf.org website.
22 static int match_hostname(char *host, char *tok)
26 return wildmatch(tok, host);
29 static int match_binary(char *b1, char *b2, char *mask, int addrlen)
33 for (i = 0; i < addrlen; i++) {
34 if ((b1[i] ^ b2[i]) & mask[i])
41 static void make_mask(char *mask, int plen, int addrlen)
49 memset(mask, 0xff, w);
51 mask[w] = 0xff & (0xff<<(8-b));
53 memset(mask+w+1, 0, addrlen-w-1);
58 static int match_address(char *addr, char *tok)
61 struct addrinfo hints, *resa, *rest;
71 char *a = NULL, *t = NULL;
84 /* Fail quietly if tok is a hostname (not an address) */
85 if (strspn(tok, ".0123456789") != len
87 && strchr(tok, ':') == NULL
95 memset(&hints, 0, sizeof(hints));
96 hints.ai_family = PF_UNSPEC;
97 hints.ai_socktype = SOCK_STREAM;
99 hints.ai_flags = AI_NUMERICHOST;
102 if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
108 gai = getaddrinfo(tok, NULL, &hints, &rest);
112 rprintf(FLOG, "error matching address %s: %s\n",
113 tok, gai_strerror(gai));
118 if (rest->ai_family != resa->ai_family) {
123 switch(resa->ai_family) {
125 a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
126 t = (char *)&((struct sockaddr_in *)rest->ai_addr)->sin_addr;
134 struct sockaddr_in6 *sin6a, *sin6t;
136 sin6a = (struct sockaddr_in6 *)resa->ai_addr;
137 sin6t = (struct sockaddr_in6 *)rest->ai_addr;
139 a = (char *)&sin6a->sin6_addr;
140 t = (char *)&sin6t->sin6_addr;
144 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
145 if (sin6t->sin6_scope_id &&
146 sin6a->sin6_scope_id != sin6t->sin6_scope_id) {
156 rprintf(FLOG, "unknown family %u\n", rest->ai_family);
163 if (inet_pton(resa->ai_addr->sa_family, p, mask) <= 0) {
171 bits = strtol(p, &ep, 10);
173 rprintf(FLOG, "malformed mask in %s\n", tok);
178 for (pp = (unsigned char *)p; *pp; pp++) {
179 if (!isascii(*pp) || !isdigit(*pp)) {
180 rprintf(FLOG, "malformed mask in %s\n", tok);
191 if (bits < 0 || bits > (addrlen << 3)) {
192 rprintf(FLOG, "malformed mask in %s\n", tok);
202 make_mask(mask, bits, addrlen);
204 ret = match_binary(a, t, mask, addrlen);
212 static int access_match(char *list, char *addr, char *host)
215 char *list2 = strdup(list);
218 out_of_memory("access_match");
224 for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
225 if (match_hostname(host, tok) || match_address(addr, tok)) {
235 int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
237 if (allow_list && !*allow_list)
239 if (deny_list && !*deny_list)
242 /* If we match an allow-list item, we always allow access. */
244 if (access_match(allow_list, addr, host))
246 /* For an allow-list w/o a deny-list, disallow non-matches. */
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))
256 /* Allow all other access. */