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 int masked_match(char *tok
, char *slash
, char *s
)
24 if ((addr
= interpret_addr(s
)) == INADDR_NONE
)
27 net
= interpret_addr(tok
);
30 if (strlen(slash
+ 1) > 2) {
31 mask
= interpret_addr(slash
+ 1);
33 mask
= (uint32
)((ALLONES
<< atoi(slash
+ 1)) ^ ALLONES
);
36 if (net
== INADDR_NONE
|| mask
== INADDR_NONE
) {
37 DEBUG(0,("access: bad net/mask access control: %s\n", tok
));
40 return ((addr
& mask
) == net
);
43 /* string_match - match string against token */
44 static int string_match(char *tok
,char *s
, char *invalid_char
)
52 /* Return True if a token has the magic value "ALL". Return
53 * FAIL if the token is "FAIL". If the token starts with a "."
54 * (domain name), return True if it matches the last fields of
55 * the string. If the token has the magic value "LOCAL",
56 * return True if the string does not contain a "."
57 * character. If the token ends on a "." (network number),
58 * return True if it matches the first fields of the
59 * string. If the token begins with a "@" (netgroup name),
60 * return True if the string is a (host) member of the
61 * netgroup. Return True if the token fully matches the
62 * string. If the token is a netnumber/netmask pair, return
63 * True if the address is a member of the specified subnet.
66 if (tok
[0] == '.') { /* domain: match last fields */
67 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
68 && strcasecmp(tok
, s
+ str_len
- tok_len
) == 0)
70 } else if (tok
[0] == '@') { /* netgroup: look it up */
72 static char *mydomain
= NULL
;
73 char *hostname
= NULL
;
74 BOOL netgroup_ok
= False
;
76 if (!mydomain
) yp_get_default_domain(&mydomain
);
79 DEBUG(0,("Unable to get default yp domain.\n"));
82 if (!(hostname
= strdup(s
))) {
83 DEBUG(1,("out of memory for strdup!\n"));
87 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
89 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
93 BOOLSTR(netgroup_ok
)));
97 if (netgroup_ok
) return(True
);
99 DEBUG(0,("access: netgroup support is not configured\n"));
102 } else if (strcasecmp(tok
, "ALL") == 0) { /* all: match any */
104 } else if (strcasecmp(tok
, "FAIL") == 0) { /* fail: match any */
106 } else if (strcasecmp(tok
, "LOCAL") == 0) { /* local: no dots */
107 if (strchr_m(s
, '.') == 0 && strcasecmp(s
, "unknown") != 0)
109 } else if (!strcasecmp(tok
, s
)) { /* match host name or address */
111 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
112 if (strncmp(tok
, s
, tok_len
) == 0)
114 } else if ((cut
= strchr_m(tok
, '/')) != 0) { /* netnumber/netmask */
115 if (isdigit((int)s
[0]) && masked_match(tok
, cut
, s
))
117 } else if (strchr_m(tok
, '*') != 0) {
119 } else if (strchr_m(tok
, '?') != 0) {
126 /* client_match - match host name and address against token */
127 static int client_match(char *tok
,char *item
)
129 char **client
= (char **)item
;
131 char invalid_char
= '\0';
134 * Try to match the address first. If that fails, try to match the host
138 if ((match
= string_match(tok
, client
[1], &invalid_char
)) == 0) {
140 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
141 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
143 if (client
[0][0] != 0)
144 match
= string_match(tok
, client
[0], &invalid_char
);
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
));
154 /* list_match - match an item against a list of tokens with exceptions */
155 static int list_match(char **list
,char *item
, int (*match_fn
)(char *, char *))
159 if (!list
) return False
;
162 * Process tokens one at a time. We have exhausted all possible matches
163 * when we reach an "EXCEPT" token or the end of the list. If we do find
164 * a match, look for an "EXCEPT" list and recurse to determine whether
165 * the match is affected by any exceptions.
168 for (; *list
; list
++) {
169 if (strcasecmp(*list
, "EXCEPT") == 0) /* EXCEPT: give up */
171 if ((match
= (*match_fn
) (*list
, item
))) /* True or FAIL */
174 /* Process exceptions to True or FAIL matches. */
176 if (match
!= False
) {
177 while (*list
&& strcasecmp(*list
, "EXCEPT"))
180 for (; *list
; list
++) {
181 if ((*match_fn
) (*list
, item
)) /* Exception Found */
190 /* return true if access should be allowed */
191 static BOOL
allow_access_internal(char **deny_list
,char **allow_list
,
192 char *cname
,char *caddr
)
199 /* if it is loopback then always allow unless specifically denied */
200 if (strcmp(caddr
, "127.0.0.1") == 0) {
202 * If 127.0.0.1 matches both allow and deny then allow.
203 * Patch from Steve Langasek vorlon@netexpress.net.
206 list_match(deny_list
,(char *)client
,client_match
) &&
208 !list_match(allow_list
,(char *)client
, client_match
))) {
214 /* if theres no deny list and no allow list then allow access */
215 if ((!deny_list
|| *deny_list
== 0) &&
216 (!allow_list
|| *allow_list
== 0)) {
220 /* if there is an allow list but no deny list then allow only hosts
222 if (!deny_list
|| *deny_list
== 0)
223 return(list_match(allow_list
,(char *)client
,client_match
));
225 /* if theres a deny list but no allow list then allow
226 all hosts not on the deny list */
227 if (!allow_list
|| *allow_list
== 0)
228 return(!list_match(deny_list
,(char *)client
,client_match
));
230 /* if there are both types of list then allow all hosts on the
232 if (list_match(allow_list
,(char *)client
,client_match
))
235 /* if there are both types of list and it's not on the allow then
236 allow it if its not on the deny */
237 if (list_match(deny_list
,(char *)client
,client_match
))
243 /* return true if access should be allowed */
244 BOOL
allow_access(char **deny_list
,char **allow_list
,
245 const char *cname
, const char *caddr
)
249 char *nc_cname
= smb_xstrdup(cname
);
250 char *nc_caddr
= smb_xstrdup(caddr
);
252 ret
= allow_access_internal(deny_list
, allow_list
, nc_cname
, nc_caddr
);
259 /* return true if the char* contains ip addrs only. Used to avoid
260 gethostbyaddr() calls */
261 static BOOL
only_ipaddrs_in_list(char** list
)
265 if (!list
) return True
;
267 for (; *list
; list
++)
269 /* factor out the special strings */
270 if (!strcasecmp(*list
, "ALL") || !strcasecmp(*list
, "FAIL") ||
271 !strcasecmp(*list
, "EXCEPT"))
276 if (!is_ipaddress(*list
))
280 * if we failed, make sure that it was not because the token
281 * was a network/netmask pair. Only network/netmask pairs
284 if ((p
=strchr_m(*list
, '/')) == NULL
)
287 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list
));
296 /* return true if access should be allowed to a service for a socket */
297 BOOL
check_access(int sock
, char **allow_list
, char **deny_list
)
300 BOOL only_ip
= False
;
302 if ((!deny_list
|| *deny_list
==0) && (!allow_list
|| *allow_list
==0))
309 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
310 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_socket_addr(sock
));
318 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
319 ret
= allow_access(deny_list
,allow_list
, get_socket_name(sock
),
320 get_socket_addr(sock
));
325 DEBUG(2,("Allowed connection from %s (%s)\n",
326 only_ip
? "" : get_socket_name(sock
),
327 get_socket_addr(sock
)));
331 DEBUG(0,("Denied connection from %s (%s)\n",
332 only_ip
? "" : get_socket_name(sock
),
333 get_socket_addr(sock
)));