2 This module is an adaption of code from the tcpd-1.4 package written
3 by Wietse Venema, Eindhoven University of Technology, The Netherlands.
5 The code is used here with permission.
7 The code has been considerably changed from the original. Bug reports
8 should be sent to samba@samba.org
10 Updated for IPv6 by Jeremy Allison (C) 2007.
18 /* masked_match - match address against netnumber/netmask */
19 static bool masked_match(const char *tok
, const char *slash
, const char *s
)
21 struct sockaddr_storage ss_mask
;
22 struct sockaddr_storage ss_tok
;
23 struct sockaddr_storage ss_host
;
24 char *tok_copy
= NULL
;
26 if (!interpret_string_addr(&ss_host
, s
, 0)) {
31 /* IPv6 address - remove braces. */
32 tok_copy
= SMB_STRDUP(tok
+1);
36 /* Remove the terminating ']' */
37 tok_copy
[PTR_DIFF(slash
,tok
)-1] = '\0';
39 tok_copy
= SMB_STRDUP(tok
);
43 /* Remove the terminating '/' */
44 tok_copy
[PTR_DIFF(slash
,tok
)] = '\0';
47 if (!interpret_string_addr(&ss_tok
, tok_copy
, 0)) {
54 if (strlen(slash
+ 1) > 2) {
55 if (!interpret_string_addr(&ss_mask
, slash
+1, 0)) {
60 unsigned long val
= strtoul(slash
+1, &endp
, 0);
61 if (slash
+1 == endp
|| (endp
&& *endp
!= '\0')) {
64 if (!make_netmask(&ss_mask
, &ss_tok
, val
)) {
69 return same_net((struct sockaddr
*)&ss_host
, (struct sockaddr
*)&ss_tok
, (struct sockaddr
*)&ss_mask
);
72 /* string_match - match string s against token tok */
73 static bool string_match(const char *tok
,const char *s
)
79 /* Return true if a token has the magic value "ALL". Return
80 * true if the token is "FAIL". If the token starts with a "."
81 * (domain name), return true if it matches the last fields of
82 * the string. If the token has the magic value "LOCAL",
83 * return true if the string does not contain a "."
84 * character. If the token ends on a "." (network number),
85 * return true if it matches the first fields of the
86 * string. If the token begins with a "@" (netgroup name),
87 * return true if the string is a (host) member of the
88 * netgroup. Return true if the token fully matches the
89 * string. If the token is a netnumber/netmask pair, return
90 * true if the address is a member of the specified subnet.
93 if (tok
[0] == '.') { /* domain: match last fields */
94 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
95 && strequal(tok
, s
+ str_len
- tok_len
)) {
98 } else if (tok
[0] == '@') { /* netgroup: look it up */
101 char *mydomain
= NULL
;
102 char *hostname
= NULL
;
103 bool netgroup_ok
= false;
106 NULL
, SINGLETON_CACHE
,
107 data_blob_string_const_null("yp_default_domain"),
110 SMB_ASSERT(tmp
.length
> 0);
111 mydomain
= (tmp
.data
[0] == '\0')
112 ? NULL
: (char *)tmp
.data
;
115 yp_get_default_domain(&mydomain
);
118 NULL
, SINGLETON_CACHE
,
119 data_blob_string_const_null("yp_default_domain"),
120 data_blob_string_const_null(mydomain
?mydomain
:""));
124 DEBUG(0,("Unable to get default yp domain. "
125 "Try without it.\n"));
127 if (!(hostname
= SMB_STRDUP(s
))) {
128 DEBUG(1,("out of memory for strdup!\n"));
132 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
134 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
136 mydomain
?mydomain
:"(ANY)",
138 BOOLSTR(netgroup_ok
)));
145 DEBUG(0,("access: netgroup support is not configured\n"));
148 } else if (strequal(tok
, "ALL")) { /* all: match any */
150 } else if (strequal(tok
, "FAIL")) { /* fail: match any */
152 } else if (strequal(tok
, "LOCAL")) { /* local: no dots */
153 if (strchr_m(s
, '.') == 0 && !strequal(s
, "unknown")) {
156 } else if (strequal(tok
, s
)) { /* match host name or address */
158 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
159 if (strncmp(tok
, s
, tok_len
) == 0) {
162 } else if ((cut
= strchr_m(tok
, '/')) != 0) { /* netnumber/netmask */
163 if ((isdigit(s
[0]) && strchr_m(tok
, '.') != NULL
) ||
164 (tok
[0] == '[' && cut
> tok
&& cut
[-1] == ']') ||
165 ((isxdigit(s
[0]) || s
[0] == ':') &&
166 strchr_m(tok
, ':') != NULL
)) {
168 * [IPv6:addr]/netmask or IPv6:addr/netmask */
169 return masked_match(tok
, cut
, s
);
171 } else if (strchr_m(tok
, '*') != 0 || strchr_m(tok
, '?')) {
172 return unix_wild_match(tok
, s
);
177 /* client_match - match host name and address against token */
178 bool client_match(const char *tok
, const void *item
)
180 const char **client
= (const char **)item
;
181 const char *tok_addr
= tok
;
182 const char *cli_addr
= client
[ADDR_INDEX
];
185 * tok and client[ADDR_INDEX] can be an IPv4 mapped to IPv6,
186 * we try and match the IPv4 part of address only.
187 * Bug #5311 and #7383.
190 if (strnequal(tok_addr
, "::ffff:",7)) {
194 if (strnequal(cli_addr
,"::ffff:",7)) {
199 * Try to match the address first. If that fails, try to match the host
203 if (string_match(tok_addr
, cli_addr
)) {
207 if (client
[NAME_INDEX
][0] != 0) {
208 if (string_match(tok
, client
[NAME_INDEX
])) {
216 /* list_match - match an item against a list of tokens with exceptions */
217 bool list_match(const char **list
,const void *item
,
218 bool (*match_fn
)(const char *, const void *))
227 * Process tokens one at a time. We have exhausted all possible matches
228 * when we reach an "EXCEPT" token or the end of the list. If we do find
229 * a match, look for an "EXCEPT" list and recurse to determine whether
230 * the match is affected by any exceptions.
233 for (; *list
; list
++) {
234 if (strequal(*list
, "EXCEPT")) {
235 /* EXCEPT: give up */
238 if ((match
= (*match_fn
) (*list
, item
))) {
243 /* Process exceptions to true or FAIL matches. */
245 if (match
!= false) {
246 while (*list
&& !strequal(*list
, "EXCEPT")) {
250 for (; *list
; list
++) {
251 if ((*match_fn
) (*list
, item
)) {
252 /* Exception Found */
261 /* return true if access should be allowed */
262 static bool allow_access_internal(const char **deny_list
,
263 const char **allow_list
,
267 const char *client
[2];
269 client
[NAME_INDEX
] = cname
;
270 client
[ADDR_INDEX
] = caddr
;
272 /* if it is loopback then always allow unless specifically denied */
273 if (strcmp(caddr
, "127.0.0.1") == 0 || strcmp(caddr
, "::1") == 0) {
275 * If 127.0.0.1 matches both allow and deny then allow.
276 * Patch from Steve Langasek vorlon@netexpress.net.
279 list_match(deny_list
,client
,client_match
) &&
281 !list_match(allow_list
,client
, client_match
))) {
287 /* if theres no deny list and no allow list then allow access */
288 if ((!deny_list
|| *deny_list
== 0) &&
289 (!allow_list
|| *allow_list
== 0)) {
293 /* if there is an allow list but no deny list then allow only hosts
295 if (!deny_list
|| *deny_list
== 0) {
296 return(list_match(allow_list
,client
,client_match
));
299 /* if theres a deny list but no allow list then allow
300 all hosts not on the deny list */
301 if (!allow_list
|| *allow_list
== 0) {
302 return(!list_match(deny_list
,client
,client_match
));
305 /* if there are both types of list then allow all hosts on the
307 if (list_match(allow_list
,(const char *)client
,client_match
)) {
311 /* if there are both types of list and it's not on the allow then
312 allow it if its not on the deny */
313 if (list_match(deny_list
,(const char *)client
,client_match
)) {
320 /* return true if access should be allowed */
321 bool allow_access(const char **deny_list
,
322 const char **allow_list
,
327 char *nc_cname
= smb_xstrdup(cname
);
328 char *nc_caddr
= smb_xstrdup(caddr
);
330 ret
= allow_access_internal(deny_list
, allow_list
, nc_cname
, nc_caddr
);
337 /* return true if the char* contains ip addrs only. Used to avoid
340 static bool only_ipaddrs_in_list(const char **list
)
348 for (; *list
; list
++) {
349 /* factor out the special strings */
350 if (strequal(*list
, "ALL") || strequal(*list
, "FAIL") ||
351 strequal(*list
, "EXCEPT")) {
355 if (!is_ipaddress(*list
)) {
357 * If we failed, make sure that it was not because
358 * the token was a network/netmask pair. Only
359 * network/netmask pairs have a '/' in them.
361 if ((strchr_m(*list
, '/')) == NULL
) {
363 DEBUG(3,("only_ipaddrs_in_list: list has "
364 "non-ip address (%s)\n",
374 /* return true if access should be allowed to a service for a socket */
375 bool check_access(int sock
, const char **allow_list
, const char **deny_list
)
378 bool only_ip
= false;
380 if ((!deny_list
|| *deny_list
==0) && (!allow_list
|| *allow_list
==0))
384 char addr
[INET6_ADDRSTRLEN
];
386 /* Bypass name resolution calls if the lists
387 * only contain IP addrs */
388 if (only_ipaddrs_in_list(allow_list
) &&
389 only_ipaddrs_in_list(deny_list
)) {
391 DEBUG (3, ("check_access: no hostnames "
392 "in host allow/deny list.\n"));
393 ret
= allow_access(deny_list
,
396 get_peer_addr(sock
,addr
,sizeof(addr
)));
398 DEBUG (3, ("check_access: hostnames in "
399 "host allow/deny list.\n"));
400 ret
= allow_access(deny_list
,
402 get_peer_name(sock
,true),
403 get_peer_addr(sock
,addr
,sizeof(addr
)));
407 DEBUG(2,("Allowed connection from %s (%s)\n",
408 only_ip
? "" : get_peer_name(sock
,true),
409 get_peer_addr(sock
,addr
,sizeof(addr
))));
411 DEBUG(0,("Denied connection from %s (%s)\n",
412 only_ip
? "" : get_peer_name(sock
,true),
413 get_peer_addr(sock
,addr
,sizeof(addr
))));