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
13 /* Delimiters for lists of daemons or clients. */
14 static const char *sep
= ", \t";
18 #define ALLONES ((uint32)0xFFFFFFFF)
20 /* masked_match - match address against netnumber/netmask */
21 static int masked_match(char *tok
, char *slash
, char *s
)
27 if ((addr
= interpret_addr(s
)) == INADDR_NONE
)
30 net
= interpret_addr(tok
);
33 if (strlen(slash
+ 1) > 2) {
34 mask
= interpret_addr(slash
+ 1);
36 mask
= (uint32
)((ALLONES
>> atoi(slash
+ 1)) ^ ALLONES
);
39 if (net
== INADDR_NONE
|| mask
== INADDR_NONE
) {
40 DEBUG(0,("access: bad net/mask access control: %s\n", tok
));
43 return ((addr
& mask
) == net
);
46 /* string_match - match string against token */
47 static int string_match(char *tok
,char *s
, char *invalid_char
)
55 /* Return True if a token has the magic value "ALL". Return
56 * FAIL if the token is "FAIL". If the token starts with a "."
57 * (domain name), return True if it matches the last fields of
58 * the string. If the token has the magic value "LOCAL",
59 * return True if the string does not contain a "."
60 * character. If the token ends on a "." (network number),
61 * return True if it matches the first fields of the
62 * string. If the token begins with a "@" (netgroup name),
63 * return True if the string is a (host) member of the
64 * netgroup. Return True if the token fully matches the
65 * string. If the token is a netnumber/netmask pair, return
66 * True if the address is a member of the specified subnet. */
68 if (tok
[0] == '.') { /* domain: match last fields */
69 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
70 && strcasecmp(tok
, s
+ str_len
- tok_len
) == 0)
72 } else if (tok
[0] == '@') { /* netgroup: look it up */
74 static char *mydomain
= NULL
;
75 char *hostname
= NULL
;
76 BOOL netgroup_ok
= False
;
78 if (!mydomain
) yp_get_default_domain(&mydomain
);
81 DEBUG(0,("Unable to get default yp domain.\n"));
84 if (!(hostname
= strdup(s
))) {
85 DEBUG(1,("out of memory for strdup!\n"));
89 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
91 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
95 BOOLSTR(netgroup_ok
)));
99 if (netgroup_ok
) return(True
);
101 DEBUG(0,("access: netgroup support is not configured\n"));
104 } else if (strcasecmp(tok
, "ALL") == 0) { /* all: match any */
106 } else if (strcasecmp(tok
, "FAIL") == 0) { /* fail: match any */
108 } else if (strcasecmp(tok
, "LOCAL") == 0) { /* local: no dots */
109 if (strchr(s
, '.') == 0 && strcasecmp(s
, "unknown") != 0)
111 } else if (!strcasecmp(tok
, s
)) { /* match host name or address */
113 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
114 if (strncmp(tok
, s
, tok_len
) == 0)
116 } else if ((cut
= strchr(tok
, '/')) != 0) { /* netnumber/netmask */
117 if (isdigit((int)s
[0]) && masked_match(tok
, cut
, s
))
119 } else if (strchr(tok
, '*') != 0) {
121 } else if (strchr(tok
, '?') != 0) {
128 /* client_match - match host name and address against token */
129 static int client_match(char *tok
,char *item
)
131 char **client
= (char **)item
;
133 char invalid_char
= '\0';
136 * Try to match the address first. If that fails, try to match the host
140 if ((match
= string_match(tok
, client
[1], &invalid_char
)) == 0) {
142 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
143 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
145 if (client
[0][0] != 0)
146 match
= string_match(tok
, client
[0], &invalid_char
);
149 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
150 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
156 /* list_match - match an item against a list of tokens with exceptions */
157 /* (All modifications are marked with the initials "jkf") */
158 static int list_match(char *list
,char *item
, int (*match_fn
)(char *, char *))
161 char *listcopy
; /* jkf */
165 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
166 * overwriting the list given as its first parameter.
169 /* jkf -- can get called recursively with NULL list */
170 listcopy
= (list
== 0) ? (char *)0 : strdup(list
);
173 * Process tokens one at a time. We have exhausted all possible matches
174 * when we reach an "EXCEPT" token or the end of the list. If we do find
175 * a match, look for an "EXCEPT" list and recurse to determine whether
176 * the match is affected by any exceptions.
179 for (tok
= strtok(listcopy
, sep
); tok
; tok
= strtok(NULL
, sep
)) {
180 if (strcasecmp(tok
, "EXCEPT") == 0) /* EXCEPT: give up */
182 if ((match
= (*match_fn
) (tok
, item
))) /* True or FAIL */
185 /* Process exceptions to True or FAIL matches. */
187 if (match
!= False
) {
188 while ((tok
= strtok((char *) 0, sep
)) && strcasecmp(tok
, "EXCEPT"))
190 if (tok
== 0 || list_match((char *) 0, item
, match_fn
) == False
) {
191 SAFE_FREE(listcopy
); /* jkf */
196 SAFE_FREE(listcopy
); /* jkf */
201 /* return true if access should be allowed */
202 BOOL
allow_access(char *deny_list
,char *allow_list
,
203 char *cname
,char *caddr
)
210 /* if it is loopback then always allow unless specifically denied */
211 if (strcmp(caddr
, "127.0.0.1") == 0) {
213 * If 127.0.0.1 matches both allow and deny then allow.
214 * Patch from Steve Langasek vorlon@netexpress.net.
217 list_match(deny_list
,(char *)client
,client_match
) &&
219 !list_match(allow_list
,(char *)client
, client_match
))) {
225 /* if theres no deny list and no allow list then allow access */
226 if ((!deny_list
|| *deny_list
== 0) &&
227 (!allow_list
|| *allow_list
== 0)) {
231 /* if there is an allow list but no deny list then allow only hosts
233 if (!deny_list
|| *deny_list
== 0)
234 return(list_match(allow_list
,(char *)client
,client_match
));
236 /* if theres a deny list but no allow list then allow
237 all hosts not on the deny list */
238 if (!allow_list
|| *allow_list
== 0)
239 return(!list_match(deny_list
,(char *)client
,client_match
));
241 /* if there are both type of list then allow all hosts on the
243 if (list_match(allow_list
,(char *)client
,client_match
))
246 /* if there are both type of list and it's not on the allow then
247 allow it if its not on the deny */
248 if (list_match(deny_list
,(char *)client
,client_match
))
254 /* return true if the char* contains ip addrs only. Used to avoid
255 gethostbyaddr() calls */
256 static BOOL
only_ipaddrs_in_list(const char* list
)
262 listcopy
= strdup(list
);
264 for (tok
= strtok(listcopy
, sep
); tok
; tok
= strtok(NULL
, sep
))
266 /* factor out the special strings */
267 if (!strcasecmp(tok
, "ALL") || !strcasecmp(tok
, "FAIL") ||
268 !strcasecmp(tok
, "EXCEPT"))
273 if (!is_ipaddress(tok
))
276 * if we failed, make sure that it was not because the token
277 * was a network/netmask pair. Only network/netmask pairs
280 if (strchr(tok
, '/') == NULL
)
283 DEBUG(3,("only_ipaddrs_in_list: list [%s] has non-ip address %s\n", list
, tok
));
294 /* return true if access should be allowed to a service for a socket */
295 BOOL
check_access(int sock
, char *allow_list
, char *deny_list
)
298 BOOL only_ip
= False
;
302 DEBUG(10,("check_access: allow = %s, deny = %s\n",
303 allow_list
? allow_list
: "NULL",
304 deny_list
? deny_list
: "NULL"));
307 deny
= strdup(deny_list
);
309 allow
= strdup(allow_list
);
311 if ((!deny
|| *deny
==0) && (!allow
|| *allow
==0))
315 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
316 if (only_ipaddrs_in_list(allow
) && only_ipaddrs_in_list(deny
)) {
318 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
319 ret
= allow_access(deny
,allow
, "", get_socket_addr(sock
));
321 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
322 ret
= allow_access(deny
,allow
, get_socket_name(sock
),
323 get_socket_addr(sock
));
327 DEBUG(2,("Allowed connection from %s (%s)\n",
328 only_ip
? "" : get_socket_name(sock
),
329 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
)));