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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
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.
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.
23 static int match_hostname(char *host
, char *tok
)
27 return wildmatch(tok
, host
);
30 static int match_binary(char *b1
, char *b2
, char *mask
, int addrlen
)
34 for (i
= 0; i
< addrlen
; i
++) {
35 if ((b1
[i
] ^ b2
[i
]) & mask
[i
])
42 static void make_mask(char *mask
, int plen
, int addrlen
)
50 memset(mask
, 0xff, w
);
52 mask
[w
] = 0xff & (0xff<<(8-b
));
54 memset(mask
+w
+1, 0, addrlen
-w
-1);
59 static int match_address(char *addr
, char *tok
)
62 struct addrinfo hints
, *resa
, *rest
;
72 char *a
= NULL
, *t
= NULL
;
85 /* Fail quietly if tok is a hostname (not an address) */
86 if (strspn(tok
, ".0123456789") != len
88 && strchr(tok
, ':') == NULL
96 memset(&hints
, 0, sizeof(hints
));
97 hints
.ai_family
= PF_UNSPEC
;
98 hints
.ai_socktype
= SOCK_STREAM
;
100 hints
.ai_flags
= AI_NUMERICHOST
;
103 if (getaddrinfo(addr
, NULL
, &hints
, &resa
) != 0) {
109 gai
= getaddrinfo(tok
, NULL
, &hints
, &rest
);
113 rprintf(FLOG
, "error matching address %s: %s\n",
114 tok
, gai_strerror(gai
));
119 if (rest
->ai_family
!= resa
->ai_family
) {
124 switch(resa
->ai_family
) {
126 a
= (char *)&((struct sockaddr_in
*)resa
->ai_addr
)->sin_addr
;
127 t
= (char *)&((struct sockaddr_in
*)rest
->ai_addr
)->sin_addr
;
135 struct sockaddr_in6
*sin6a
, *sin6t
;
137 sin6a
= (struct sockaddr_in6
*)resa
->ai_addr
;
138 sin6t
= (struct sockaddr_in6
*)rest
->ai_addr
;
140 a
= (char *)&sin6a
->sin6_addr
;
141 t
= (char *)&sin6t
->sin6_addr
;
145 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
146 if (sin6t
->sin6_scope_id
&&
147 sin6a
->sin6_scope_id
!= sin6t
->sin6_scope_id
) {
157 rprintf(FLOG
, "unknown family %u\n", rest
->ai_family
);
164 if (inet_pton(resa
->ai_addr
->sa_family
, p
, mask
) <= 0) {
172 bits
= strtol(p
, &ep
, 10);
174 rprintf(FLOG
, "malformed mask in %s\n", tok
);
179 for (pp
= (unsigned char *)p
; *pp
; pp
++) {
180 if (!isascii(*pp
) || !isdigit(*pp
)) {
181 rprintf(FLOG
, "malformed mask in %s\n", tok
);
192 if (bits
< 0 || bits
> (addrlen
<< 3)) {
193 rprintf(FLOG
, "malformed mask in %s\n", tok
);
203 make_mask(mask
, bits
, addrlen
);
205 ret
= match_binary(a
, t
, mask
, addrlen
);
213 static int access_match(char *list
, char *addr
, char *host
)
216 char *list2
= strdup(list
);
219 out_of_memory("access_match");
225 for (tok
= strtok(list2
, " ,\t"); tok
; tok
= strtok(NULL
, " ,\t")) {
226 if (match_hostname(host
, tok
) || match_address(addr
, tok
)) {
236 int allow_access(char *addr
, char *host
, char *allow_list
, char *deny_list
)
238 if (allow_list
&& !*allow_list
)
240 if (deny_list
&& !*deny_list
)
243 /* If we match an allow-list item, we always allow access. */
245 if (access_match(allow_list
, addr
, host
))
247 /* For an allow-list w/o a deny-list, disallow non-matches. */
252 /* If we match a deny-list item (and got past any allow-list
253 * items), we always disallow access. */
254 if (deny_list
&& access_match(deny_list
, addr
, host
))
257 /* Allow all other access. */