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
);
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 int string_match(char *tok
,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. */
71 if (tok
[0] == '.') { /* domain: match last fields */
72 if ((str_len
= strlen(s
)) > (tok_len
= strlen(tok
))
73 && strcasecmp(tok
, s
+ str_len
- tok_len
) == 0)
75 } else if (tok
[0] == '@') { /* netgroup: look it up */
77 static char *mydomain
= NULL
;
78 char *hostname
= NULL
;
79 BOOL netgroup_ok
= False
;
81 if (!mydomain
) yp_get_default_domain(&mydomain
);
84 DEBUG(0,("Unable to get default yp domain.\n"));
87 if (!(hostname
= strdup(s
))) {
88 DEBUG(1,("out of memory for strdup!\n"));
92 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
94 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
98 BOOLSTR(netgroup_ok
)));
102 if (netgroup_ok
) return(True
);
104 DEBUG(0,("access: netgroup support is not configured\n"));
107 } else if (strcasecmp(tok
, "ALL") == 0) { /* all: match any */
109 } else if (strcasecmp(tok
, "FAIL") == 0) { /* fail: match any */
111 } else if (strcasecmp(tok
, "LOCAL") == 0) { /* local: no dots */
112 if (strchr(s
, '.') == 0 && strcasecmp(s
, "unknown") != 0)
114 } else if (!strcasecmp(tok
, s
)) { /* match host name or address */
116 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
117 if (strncmp(tok
, s
, tok_len
) == 0)
119 } else if ((cut
= strchr(tok
, '/')) != 0) { /* netnumber/netmask */
120 if (isdigit((int)s
[0]) && masked_match(tok
, cut
, s
))
122 } else if (strchr(tok
, '*') != 0) {
124 } else if (strchr(tok
, '?') != 0) {
131 /* client_match - match host name and address against token */
132 static int client_match(char *tok
,char *item
)
134 char **client
= (char **)item
;
136 char invalid_char
= '\0';
139 * Try to match the address first. If that fails, try to match the host
143 if ((match
= string_match(tok
, client
[1], &invalid_char
)) == 0) {
145 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
146 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
148 if (client
[0][0] != 0)
149 match
= string_match(tok
, client
[0], &invalid_char
);
152 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
153 token '%s' in an allow/deny hosts line.\n", invalid_char
, tok
));
159 /* list_match - match an item against a list of tokens with exceptions */
160 /* (All modifications are marked with the initials "jkf") */
161 static int list_match(char *list
,char *item
, int (*match_fn
)(char *, char *))
164 char *listcopy
; /* jkf */
168 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
169 * overwriting the list given as its first parameter.
172 /* jkf -- can get called recursively with NULL list */
173 listcopy
= (list
== 0) ? (char *)0 : strdup(list
);
176 * Process tokens one at a time. We have exhausted all possible matches
177 * when we reach an "EXCEPT" token or the end of the list. If we do find
178 * a match, look for an "EXCEPT" list and recurse to determine whether
179 * the match is affected by any exceptions.
182 for (tok
= strtok(listcopy
, sep
); tok
; tok
= strtok(NULL
, sep
)) {
183 if (strcasecmp(tok
, "EXCEPT") == 0) /* EXCEPT: give up */
185 if ((match
= (*match_fn
) (tok
, item
))) /* True or FAIL */
188 /* Process exceptions to True or FAIL matches. */
190 if (match
!= False
) {
191 while ((tok
= strtok((char *) 0, sep
)) && strcasecmp(tok
, "EXCEPT"))
193 if (tok
== 0 || list_match((char *) 0, item
, match_fn
) == False
) {
194 SAFE_FREE(listcopy
); /* jkf */
199 SAFE_FREE(listcopy
); /* jkf */
204 /* return true if access should be allowed */
205 BOOL
allow_access(char *deny_list
,char *allow_list
,
206 char *cname
,char *caddr
)
213 /* if it is loopback then always allow unless specifically denied */
214 if (strcmp(caddr
, "127.0.0.1") == 0) {
216 * If 127.0.0.1 matches both allow and deny then allow.
217 * Patch from Steve Langasek vorlon@netexpress.net.
220 list_match(deny_list
,(char *)client
,client_match
) &&
222 !list_match(allow_list
,(char *)client
, client_match
))) {
228 /* if theres no deny list and no allow list then allow access */
229 if ((!deny_list
|| *deny_list
== 0) &&
230 (!allow_list
|| *allow_list
== 0)) {
234 /* if there is an allow list but no deny list then allow only hosts
236 if (!deny_list
|| *deny_list
== 0)
237 return(list_match(allow_list
,(char *)client
,client_match
));
239 /* if theres a deny list but no allow list then allow
240 all hosts not on the deny list */
241 if (!allow_list
|| *allow_list
== 0)
242 return(!list_match(deny_list
,(char *)client
,client_match
));
244 /* if there are both type of list then allow all hosts on the
246 if (list_match(allow_list
,(char *)client
,client_match
))
249 /* if there are both type of list and it's not on the allow then
250 allow it if its not on the deny */
251 if (list_match(deny_list
,(char *)client
,client_match
))
257 /* return true if the char* contains ip addrs only. Used to avoid
258 gethostbyaddr() calls */
259 static BOOL
only_ipaddrs_in_list(const char* list
)
265 listcopy
= strdup(list
);
267 for (tok
= strtok(listcopy
, sep
); tok
; tok
= strtok(NULL
, sep
))
269 /* factor out the special strings */
270 if (!strcasecmp(tok
, "ALL") || !strcasecmp(tok
, "FAIL") ||
271 !strcasecmp(tok
, "EXCEPT"))
276 if (!is_ipaddress(tok
))
279 * if we failed, make sure that it was not because the token
280 * was a network/netmask pair. Only network/netmask pairs
283 if (strchr(tok
, '/') == NULL
)
286 DEBUG(3,("only_ipaddrs_in_list: list [%s] has non-ip address %s\n", list
, tok
));
297 /* return true if access should be allowed to a service for a socket */
298 BOOL
check_access(int sock
, char *allow_list
, char *deny_list
)
301 BOOL only_ip
= False
;
305 DEBUG(10,("check_access: allow = %s, deny = %s\n",
306 allow_list
? allow_list
: "NULL",
307 deny_list
? deny_list
: "NULL"));
310 deny
= strdup(deny_list
);
312 allow
= strdup(allow_list
);
314 if ((!deny
|| *deny
==0) && (!allow
|| *allow
==0))
318 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
319 if (only_ipaddrs_in_list(allow
) && only_ipaddrs_in_list(deny
)) {
321 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
322 ret
= allow_access(deny
,allow
, "", get_socket_addr(sock
));
324 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
325 ret
= allow_access(deny
,allow
, get_socket_name(sock
),
326 get_socket_addr(sock
));
330 DEBUG(2,("Allowed connection from %s (%s)\n",
331 only_ip
? "" : get_socket_name(sock
),
332 get_socket_addr(sock
)));
334 DEBUG(0,("Denied connection from %s (%s)\n",
335 only_ip
? "" : get_socket_name(sock
),
336 get_socket_addr(sock
)));