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.
15 #include "interfaces.h"
20 /* masked_match - match address against netnumber/netmask */
21 static bool masked_match(const char *tok
, const char *slash
, const char *s
)
23 struct sockaddr_storage ss_mask
;
24 struct sockaddr_storage ss_tok
;
25 struct sockaddr_storage ss_host
;
26 char *tok_copy
= NULL
;
28 if (!interpret_string_addr(&ss_host
, s
, 0)) {
33 /* IPv6 address - remove braces. */
34 tok_copy
= SMB_STRDUP(tok
+1);
38 /* Remove the terminating ']' */
39 tok_copy
[PTR_DIFF(slash
,tok
)-1] = '\0';
41 tok_copy
= SMB_STRDUP(tok
);
45 /* Remove the terminating '/' */
46 tok_copy
[PTR_DIFF(slash
,tok
)] = '\0';
49 if (!interpret_string_addr(&ss_tok
, tok_copy
, 0)) {
56 if (strlen(slash
+ 1) > 2) {
57 if (!interpret_string_addr(&ss_mask
, slash
+1, 0)) {
62 unsigned long val
= strtoul(slash
+1, &endp
, 0);
63 if (slash
+1 == endp
|| (endp
&& *endp
!= '\0')) {
66 if (!make_netmask(&ss_mask
, &ss_tok
, val
)) {
71 return same_net((struct sockaddr
*)(void *)&ss_host
,
72 (struct sockaddr
*)(void *)&ss_tok
,
73 (struct sockaddr
*)(void *)&ss_mask
);
76 /* string_match - match string s against token tok */
77 static bool string_match(const char *tok
,const char *s
)
83 /* Return true if a token has the magic value "ALL". Return
84 * true if the token is "FAIL". If the token starts with a "."
85 * (domain name), return true if it matches the last fields of
86 * the string. If the token has the magic value "LOCAL",
87 * return true if the string does not contain a "."
88 * character. If the token ends on a "." (network number),
89 * return true if it matches the first fields of the
90 * string. If the token begins with a "@" (netgroup name),
91 * return true if the string is a (host) member of the
92 * netgroup. Return true if the token fully matches the
93 * string. If the token is a netnumber/netmask pair, return
94 * true if the address is a member of the specified subnet.
97 if (tok
[0] == '.') { /* domain: match last fields */
98 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
99 && strequal(tok
, s
+ str_len
- tok_len
)) {
102 } else if (tok
[0] == '@') { /* netgroup: look it up */
105 char *mydomain
= NULL
;
106 char *hostname
= NULL
;
107 bool netgroup_ok
= false;
110 NULL
, SINGLETON_CACHE
,
111 data_blob_string_const_null("yp_default_domain"),
114 SMB_ASSERT(tmp
.length
> 0);
115 mydomain
= (tmp
.data
[0] == '\0')
116 ? NULL
: (char *)tmp
.data
;
119 yp_get_default_domain(&mydomain
);
122 NULL
, SINGLETON_CACHE
,
123 data_blob_string_const_null("yp_default_domain"),
124 data_blob_string_const_null(mydomain
?mydomain
:""));
128 DEBUG(0,("Unable to get default yp domain. "
129 "Try without it.\n"));
131 if (!(hostname
= SMB_STRDUP(s
))) {
132 DEBUG(1,("out of memory for strdup!\n"));
136 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
138 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
140 mydomain
?mydomain
:"(ANY)",
142 BOOLSTR(netgroup_ok
)));
149 DEBUG(0,("access: netgroup support is not configured\n"));
152 } else if (strequal(tok
, "ALL")) { /* all: match any */
154 } else if (strequal(tok
, "FAIL")) { /* fail: match any */
156 } else if (strequal(tok
, "LOCAL")) { /* local: no dots */
157 if (strchr_m(s
, '.') == 0 && !strequal(s
, "unknown")) {
160 } else if (strequal(tok
, s
)) { /* match host name or address */
162 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
163 if (strncmp(tok
, s
, tok_len
) == 0) {
166 } else if ((cut
= strchr_m(tok
, '/')) != 0) { /* netnumber/netmask */
167 if ((isdigit(s
[0]) && strchr_m(tok
, '.') != NULL
) ||
168 (tok
[0] == '[' && cut
> tok
&& cut
[-1] == ']') ||
169 ((isxdigit(s
[0]) || s
[0] == ':') &&
170 strchr_m(tok
, ':') != NULL
)) {
172 * [IPv6:addr]/netmask or IPv6:addr/netmask */
173 return masked_match(tok
, cut
, s
);
175 } else if (strchr_m(tok
, '*') != 0 || strchr_m(tok
, '?')) {
176 return unix_wild_match(tok
, s
);
181 /* client_match - match host name and address against token */
182 bool client_match(const char *tok
, const void *item
)
184 const char **client
= (const char **)item
;
185 const char *tok_addr
= tok
;
186 const char *cli_addr
= client
[ADDR_INDEX
];
189 * tok and client[ADDR_INDEX] can be an IPv4 mapped to IPv6,
190 * we try and match the IPv4 part of address only.
191 * Bug #5311 and #7383.
194 if (strnequal(tok_addr
, "::ffff:",7)) {
198 if (strnequal(cli_addr
,"::ffff:",7)) {
203 * Try to match the address first. If that fails, try to match the host
207 if (string_match(tok_addr
, cli_addr
)) {
211 if (client
[NAME_INDEX
][0] != 0) {
212 if (string_match(tok
, client
[NAME_INDEX
])) {
220 /* list_match - match an item against a list of tokens with exceptions */
221 bool list_match(const char **list
,const void *item
,
222 bool (*match_fn
)(const char *, const void *))
231 * Process tokens one at a time. We have exhausted all possible matches
232 * when we reach an "EXCEPT" token or the end of the list. If we do find
233 * a match, look for an "EXCEPT" list and recurse to determine whether
234 * the match is affected by any exceptions.
237 for (; *list
; list
++) {
238 if (strequal(*list
, "EXCEPT")) {
239 /* EXCEPT: give up */
242 if ((match
= (*match_fn
) (*list
, item
))) {
247 /* Process exceptions to true or FAIL matches. */
249 if (match
!= false) {
250 while (*list
&& !strequal(*list
, "EXCEPT")) {
254 for (; *list
; list
++) {
255 if ((*match_fn
) (*list
, item
)) {
256 /* Exception Found */
265 /* return true if access should be allowed */
266 static bool allow_access_internal(const char **deny_list
,
267 const char **allow_list
,
271 const char *client
[2];
273 client
[NAME_INDEX
] = cname
;
274 client
[ADDR_INDEX
] = caddr
;
276 /* if it is loopback then always allow unless specifically denied */
277 if (strcmp(caddr
, "127.0.0.1") == 0 || strcmp(caddr
, "::1") == 0) {
279 * If 127.0.0.1 matches both allow and deny then allow.
280 * Patch from Steve Langasek vorlon@netexpress.net.
283 list_match(deny_list
,client
,client_match
) &&
285 !list_match(allow_list
,client
, client_match
))) {
291 /* if theres no deny list and no allow list then allow access */
292 if ((!deny_list
|| *deny_list
== 0) &&
293 (!allow_list
|| *allow_list
== 0)) {
297 /* if there is an allow list but no deny list then allow only hosts
299 if (!deny_list
|| *deny_list
== 0) {
300 return(list_match(allow_list
,client
,client_match
));
303 /* if theres a deny list but no allow list then allow
304 all hosts not on the deny list */
305 if (!allow_list
|| *allow_list
== 0) {
306 return(!list_match(deny_list
,client
,client_match
));
309 /* if there are both types of list then allow all hosts on the
311 if (list_match(allow_list
,(const char *)client
,client_match
)) {
315 /* if there are both types of list and it's not on the allow then
316 allow it if its not on the deny */
317 if (list_match(deny_list
,(const char *)client
,client_match
)) {
324 /* return true if access should be allowed */
325 bool allow_access(const char **deny_list
,
326 const char **allow_list
,
331 char *nc_cname
= smb_xstrdup(cname
);
332 char *nc_caddr
= smb_xstrdup(caddr
);
334 ret
= allow_access_internal(deny_list
, allow_list
, nc_cname
, nc_caddr
);
337 ("%s connection from %s (%s)\n",
338 ret
? "Allowed" : "Denied", nc_cname
, nc_caddr
));