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 extern int DEBUGLEVEL
;
15 /* Delimiters for lists of daemons or clients. */
16 static char *sep
= ", \t";
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
);
32 if (net
== INADDR_NONE
||
33 (mask
= interpret_addr(slash
+ 1)) == INADDR_NONE
) {
34 DEBUG(0,("access: bad net/mask access control: %s\n", tok
));
37 return ((addr
& mask
) == net
);
40 /* string_match - match string against token */
41 static int string_match(char *tok
,char *s
, char *invalid_char
)
49 /* Return True if a token has the magic value "ALL". Return
50 * FAIL if the token is "FAIL". If the token starts with a "."
51 * (domain name), return True if it matches the last fields of
52 * the string. If the token has the magic value "LOCAL",
53 * return True if the string does not contain a "."
54 * character. If the token ends on a "." (network number),
55 * return True if it matches the first fields of the
56 * string. If the token begins with a "@" (netgroup name),
57 * return True if the string is a (host) member of the
58 * netgroup. Return True if the token fully matches the
59 * string. If the token is a netnumber/netmask pair, return
60 * True if the address is a member of the specified subnet. */
62 if (tok
[0] == '.') { /* domain: match last fields */
63 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
64 && strcasecmp(tok
, s
+ str_len
- tok_len
) == 0)
66 } else if (tok
[0] == '@') { /* netgroup: look it up */
68 static char *mydomain
= NULL
;
69 char *hostname
= NULL
;
70 BOOL netgroup_ok
= False
;
72 if (!mydomain
) yp_get_default_domain(&mydomain
);
75 DEBUG(0,("Unable to get default yp domain.\n"));
78 if (!(hostname
= strdup(s
))) {
79 DEBUG(1,("out of memory for strdup!\n"));
83 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
85 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
89 BOOLSTR(netgroup_ok
)));
93 if (netgroup_ok
) return(True
);
95 DEBUG(0,("access: netgroup support is not configured\n"));
98 } else if (strcasecmp(tok
, "ALL") == 0) { /* all: match any */
100 } else if (strcasecmp(tok
, "FAIL") == 0) { /* fail: match any */
102 } else if (strcasecmp(tok
, "LOCAL") == 0) { /* local: no dots */
103 if (strchr(s
, '.') == 0 && strcasecmp(s
, "unknown") != 0)
105 } else if (!strcasecmp(tok
, s
)) { /* match host name or address */
107 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
108 if (strncmp(tok
, s
, tok_len
) == 0)
110 } else if ((cut
= strchr(tok
, '/')) != 0) { /* netnumber/netmask */
111 if (isdigit((int)s
[0]) && masked_match(tok
, cut
, s
))
113 } else if (strchr(tok
, '*') != 0) {
115 } else if (strchr(tok
, '?') != 0) {
122 /* client_match - match host name and address against token */
123 static int client_match(char *tok
,char *item
)
125 char **client
= (char **)item
;
127 char invalid_char
= '\0';
130 * Try to match the address first. If that fails, try to match the host
134 if ((match
= string_match(tok
, client
[1], &invalid_char
)) == 0) {
136 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
137 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
139 if (client
[0][0] != 0)
140 match
= string_match(tok
, client
[0], &invalid_char
);
143 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
144 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
150 /* list_match - match an item against a list of tokens with exceptions */
151 /* (All modifications are marked with the initials "jkf") */
152 static int list_match(char *list
,char *item
, int (*match_fn
)(char *, char *))
155 char *listcopy
; /* jkf */
159 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
160 * overwriting the list given as its first parameter.
163 /* jkf -- can get called recursively with NULL list */
164 listcopy
= (list
== 0) ? (char *)0 : strdup(list
);
167 * Process tokens one at a time. We have exhausted all possible matches
168 * when we reach an "EXCEPT" token or the end of the list. If we do find
169 * a match, look for an "EXCEPT" list and recurse to determine whether
170 * the match is affected by any exceptions.
173 for (tok
= strtok(listcopy
, sep
); tok
; tok
= strtok(NULL
, sep
)) {
174 if (strcasecmp(tok
, "EXCEPT") == 0) /* EXCEPT: give up */
176 if ((match
= (*match_fn
) (tok
, item
))) /* True or FAIL */
179 /* Process exceptions to True or FAIL matches. */
181 if (match
!= False
) {
182 while ((tok
= strtok((char *) 0, sep
)) && strcasecmp(tok
, "EXCEPT"))
184 if (tok
== 0 || list_match((char *) 0, item
, match_fn
) == False
) {
185 if (listcopy
!= 0) free(listcopy
); /* jkf */
190 if (listcopy
!= 0) free(listcopy
); /* jkf */
195 /* return true if access should be allowed */
196 BOOL
allow_access(char *deny_list
,char *allow_list
,
197 char *cname
,char *caddr
)
204 /* if it is loopback then always allow unless specifically denied */
205 if (strcmp(caddr
, "127.0.0.1") == 0) {
207 list_match(deny_list
,(char *)client
,client_match
)) {
213 /* if theres no deny list and no allow list then allow access */
214 if ((!deny_list
|| *deny_list
== 0) &&
215 (!allow_list
|| *allow_list
== 0)) {
219 /* if there is an allow list but no deny list then allow only hosts
221 if (!deny_list
|| *deny_list
== 0)
222 return(list_match(allow_list
,(char *)client
,client_match
));
224 /* if theres a deny list but no allow list then allow
225 all hosts not on the deny list */
226 if (!allow_list
|| *allow_list
== 0)
227 return(!list_match(deny_list
,(char *)client
,client_match
));
229 /* if there are both type of list then allow all hosts on the
231 if (list_match(allow_list
,(char *)client
,client_match
))
234 /* if there are both type of list and it's not on the allow then
235 allow it if its not on the deny */
236 if (list_match(deny_list
,(char *)client
,client_match
))
242 /* return true if the char* contains ip addrs only. Used to avoid
243 gethostbyaddr() calls */
244 static BOOL
only_ipaddrs_in_list(const char* list
)
250 listcopy
= strdup(list
);
252 for (tok
= strtok(listcopy
, sep
); tok
; tok
= strtok(NULL
, sep
))
254 /* factor out the special strings */
255 if (!strcasecmp(tok
, "ALL") || !strcasecmp(tok
, "FAIL") ||
256 !strcasecmp(tok
, "EXCEPT"))
261 if (!is_ipaddress(tok
))
265 * if we failed, make sure that it was not because the token
266 * was a network/netmask pair. Only network/netmask pairs
269 if ((p
=strchr(tok
, '/')) == NULL
)
272 DEBUG(3,("only_ipaddrs_in_list: list [%s] has non-ip address %s\n", list
, p
));
284 /* return true if access should be allowed to a service for a socket */
285 BOOL
check_access(int sock
, char *allow_list
, char *deny_list
)
288 BOOL only_ip
= False
;
290 if (deny_list
) deny_list
= strdup(deny_list
);
291 if (allow_list
) allow_list
= strdup(allow_list
);
293 if ((!deny_list
|| *deny_list
==0) && (!allow_list
|| *allow_list
==0))
300 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
301 if (only_ipaddrs_in_list(allow_list
) && only_ipaddrs_in_list(deny_list
))
304 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
305 ret
= allow_access(deny_list
,allow_list
, "", get_socket_addr(sock
));
309 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
310 ret
= allow_access(deny_list
,allow_list
, get_socket_name(sock
),
311 get_socket_addr(sock
));
316 DEBUG(2,("Allowed connection from %s (%s)\n",
317 only_ip
? "" : get_socket_name(sock
),
318 get_socket_addr(sock
)));
322 DEBUG(0,("Denied connection from %s (%s)\n",
323 only_ip
? "" : get_socket_name(sock
),
324 get_socket_addr(sock
)));
328 if (deny_list
) free(deny_list
);
329 if (allow_list
) free(allow_list
);