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
15 #define ALLONES ((uint32)0xFFFFFFFF)
17 /* masked_match - match address against netnumber/netmask */
18 static BOOL
masked_match(const char *tok
, const char *slash
, const char *s
)
25 if ((addr
= interpret_addr(s
)) == INADDR_NONE
)
28 fstrcpy(tok_cpy
, tok
);
29 tok_cpy
[PTR_DIFF(slash
,tok
)] = '\0';
30 net
= interpret_addr(tok_cpy
);
31 tok_cpy
[PTR_DIFF(slash
,tok
)] = '/';
33 if (strlen(slash
+ 1) > 2) {
34 mask
= interpret_addr(slash
+ 1);
36 mask
= (uint32
)((ALLONES
>> atoi(slash
+ 1)) ^ ALLONES
);
37 /* convert to network byte order */
41 if (net
== INADDR_NONE
|| mask
== INADDR_NONE
) {
42 DEBUG(0,("access: bad net/mask access control: %s\n", tok
));
46 return ((addr
& mask
) == net
);
49 /* string_match - match string against token */
50 static BOOL
string_match(const char *tok
,const char *s
, char *invalid_char
)
58 /* Return True if a token has the magic value "ALL". Return
59 * FAIL if the token is "FAIL". If the token starts with a "."
60 * (domain name), return True if it matches the last fields of
61 * the string. If the token has the magic value "LOCAL",
62 * return True if the string does not contain a "."
63 * character. If the token ends on a "." (network number),
64 * return True if it matches the first fields of the
65 * string. If the token begins with a "@" (netgroup name),
66 * return True if the string is a (host) member of the
67 * netgroup. Return True if the token fully matches the
68 * string. If the token is a netnumber/netmask pair, return
69 * True if the address is a member of the specified subnet.
72 if (tok
[0] == '.') { /* domain: match last fields */
73 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
74 && strequal(tok
, s
+ str_len
- tok_len
))
76 } else if (tok
[0] == '@') { /* netgroup: look it up */
78 static char *mydomain
= NULL
;
79 char *hostname
= NULL
;
80 BOOL netgroup_ok
= False
;
83 yp_get_default_domain(&mydomain
);
86 DEBUG(0,("Unable to get default yp domain.\n"));
89 if (!(hostname
= strdup(s
))) {
90 DEBUG(1,("out of memory for strdup!\n"));
94 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
96 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
100 BOOLSTR(netgroup_ok
)));
107 DEBUG(0,("access: netgroup support is not configured\n"));
110 } else if (strequal(tok
, "ALL")) { /* all: match any */
112 } else if (strequal(tok
, "FAIL")) { /* fail: match any */
114 } else if (strequal(tok
, "LOCAL")) { /* local: no dots */
115 if (strchr_m(s
, '.') == 0 && !strequal(s
, "unknown"))
117 } else if (strequal(tok
, s
)) { /* match host name or address */
119 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
120 if (strncmp(tok
, s
, tok_len
) == 0)
122 } else if ((cut
= strchr_m(tok
, '/')) != 0) { /* netnumber/netmask */
123 if (isdigit((int)s
[0]) && masked_match(tok
, cut
, s
))
125 } else if (strchr_m(tok
, '*') != 0) {
127 } else if (strchr_m(tok
, '?') != 0) {
133 /* client_match - match host name and address against token */
134 static BOOL
client_match(const char *tok
, const char *item
)
136 const char **client
= (const char **)item
;
138 char invalid_char
= '\0';
141 * Try to match the address first. If that fails, try to match the host
145 if ((match
= string_match(tok
, client
[1], &invalid_char
)) == 0) {
147 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
148 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
150 if (client
[0][0] != 0)
151 match
= string_match(tok
, client
[0], &invalid_char
);
154 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
155 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
161 /* list_match - match an item against a list of tokens with exceptions */
162 static BOOL
list_match(const char **list
,const char *item
,
163 BOOL (*match_fn
)(const char *, const char *))
171 * Process tokens one at a time. We have exhausted all possible matches
172 * when we reach an "EXCEPT" token or the end of the list. If we do find
173 * a match, look for an "EXCEPT" list and recurse to determine whether
174 * the match is affected by any exceptions.
177 for (; *list
; list
++) {
178 if (strequal(*list
, "EXCEPT")) /* EXCEPT: give up */
180 if ((match
= (*match_fn
) (*list
, item
))) /* True or FAIL */
183 /* Process exceptions to True or FAIL matches. */
185 if (match
!= False
) {
186 while (*list
&& !strequal(*list
, "EXCEPT"))
189 for (; *list
; list
++) {
190 if ((*match_fn
) (*list
, item
)) /* Exception Found */
198 /* return true if access should be allowed */
199 static BOOL
allow_access_internal(const char **deny_list
,const char **allow_list
,
200 const char *cname
, const char *caddr
)
202 const char *client
[2];
207 /* if it is loopback then always allow unless specifically denied */
208 if (strcmp(caddr
, "127.0.0.1") == 0) {
210 * If 127.0.0.1 matches both allow and deny then allow.
211 * Patch from Steve Langasek vorlon@netexpress.net.
214 list_match(deny_list
,(const char *)client
,client_match
) &&
216 !list_match(allow_list
,(const char *)client
, client_match
))) {
222 /* if theres no deny list and no allow list then allow access */
223 if ((!deny_list
|| *deny_list
== 0) &&
224 (!allow_list
|| *allow_list
== 0)) {
228 /* if there is an allow list but no deny list then allow only hosts
230 if (!deny_list
|| *deny_list
== 0)
231 return(list_match(allow_list
,(const char *)client
,client_match
));
233 /* if theres a deny list but no allow list then allow
234 all hosts not on the deny list */
235 if (!allow_list
|| *allow_list
== 0)
236 return(!list_match(deny_list
,(const char *)client
,client_match
));
238 /* if there are both types of list then allow all hosts on the
240 if (list_match(allow_list
,(const char *)client
,client_match
))
243 /* if there are both types of list and it's not on the allow then
244 allow it if its not on the deny */
245 if (list_match(deny_list
,(const char *)client
,client_match
))
251 /* return true if access should be allowed */
252 BOOL
allow_access(const char **deny_list
, const char **allow_list
,
253 const char *cname
, const char *caddr
)
256 char *nc_cname
= smb_xstrdup(cname
);
257 char *nc_caddr
= smb_xstrdup(caddr
);
259 ret
= allow_access_internal(deny_list
, allow_list
, nc_cname
, nc_caddr
);
266 /* return true if the char* contains ip addrs only. Used to avoid
267 gethostbyaddr() calls */
269 static BOOL
only_ipaddrs_in_list(const char** list
)
276 for (; *list
; list
++) {
277 /* factor out the special strings */
278 if (strequal(*list
, "ALL") || strequal(*list
, "FAIL") ||
279 strequal(*list
, "EXCEPT")) {
283 if (!is_ipaddress(*list
)) {
285 * if we failed, make sure that it was not because the token
286 * was a network/netmask pair. Only network/netmask pairs
289 if ((strchr_m(*list
, '/')) == NULL
) {
291 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list
));
300 /* return true if access should be allowed to a service for a socket */
301 BOOL
check_access(int sock
, const char **allow_list
, const char **deny_list
)
304 BOOL only_ip
= False
;
306 if ((!deny_list
|| *deny_list
==0) && (!allow_list
|| *allow_list
==0))
310 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
311 if (only_ipaddrs_in_list(allow_list
) && only_ipaddrs_in_list(deny_list
)) {
313 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
314 ret
= allow_access(deny_list
,allow_list
, "", get_peer_addr(sock
));
316 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
317 ret
= allow_access(deny_list
,allow_list
, get_peer_name(sock
,True
),
318 get_peer_addr(sock
));
322 DEBUG(2,("Allowed connection from %s (%s)\n",
323 only_ip
? "" : get_peer_name(sock
,True
),
324 get_peer_addr(sock
)));
326 DEBUG(0,("Denied connection from %s (%s)\n",
327 only_ip
? "" : get_peer_name(sock
,True
),
328 get_peer_addr(sock
)));