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 /* masked_match - match address against netnumber/netmask */
16 static int masked_match(char *tok, char *slash, char *s)
22 if ((addr = interpret_addr(s)) == INADDR_NONE)
25 net = interpret_addr(tok);
27 if (net == INADDR_NONE ||
28 (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
29 DEBUG(0,("access: bad net/mask access control: %s\n", tok));
32 return ((addr & mask) == net);
35 /* string_match - match string against token */
36 static int string_match(char *tok,char *s, char *invalid_char)
44 /* Return True if a token has the magic value "ALL". Return
45 * FAIL if the token is "FAIL". If the token starts with a "."
46 * (domain name), return True if it matches the last fields of
47 * the string. If the token has the magic value "LOCAL",
48 * return True if the string does not contain a "."
49 * character. If the token ends on a "." (network number),
50 * return True if it matches the first fields of the
51 * string. If the token begins with a "@" (netgroup name),
52 * return True if the string is a (host) member of the
53 * netgroup. Return True if the token fully matches the
54 * string. If the token is a netnumber/netmask pair, return
55 * True if the address is a member of the specified subnet.
58 if (tok[0] == '.') { /* domain: match last fields */
59 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
60 && strcasecmp(tok, s + str_len - tok_len) == 0)
62 } else if (tok[0] == '@') { /* netgroup: look it up */
64 static char *mydomain = NULL;
65 char *hostname = NULL;
66 BOOL netgroup_ok = False;
68 if (!mydomain) yp_get_default_domain(&mydomain);
71 DEBUG(0,("Unable to get default yp domain.\n"));
74 if (!(hostname = strdup(s))) {
75 DEBUG(1,("out of memory for strdup!\n"));
79 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
81 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
85 BOOLSTR(netgroup_ok)));
89 if (netgroup_ok) return(True);
91 DEBUG(0,("access: netgroup support is not configured\n"));
94 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
96 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
98 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
99 if (strchr_m(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
101 } else if (!strcasecmp(tok, s)) { /* match host name or address */
103 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
104 if (strncmp(tok, s, tok_len) == 0)
106 } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
107 if (isdigit((int)s[0]) && masked_match(tok, cut, s))
109 } else if (strchr_m(tok, '*') != 0) {
111 } else if (strchr_m(tok, '?') != 0) {
118 /* client_match - match host name and address against token */
119 static int client_match(char *tok,char *item)
121 char **client = (char **)item;
123 char invalid_char = '\0';
126 * Try to match the address first. If that fails, try to match the host
130 if ((match = string_match(tok, client[1], &invalid_char)) == 0) {
132 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
133 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
135 if (client[0][0] != 0)
136 match = string_match(tok, client[0], &invalid_char);
139 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
140 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
146 /* list_match - match an item against a list of tokens with exceptions */
147 static int list_match(char **list,char *item, int (*match_fn)(char *, char *))
151 if (!list) return False;
154 * Process tokens one at a time. We have exhausted all possible matches
155 * when we reach an "EXCEPT" token or the end of the list. If we do find
156 * a match, look for an "EXCEPT" list and recurse to determine whether
157 * the match is affected by any exceptions.
160 for (; *list ; list++) {
161 if (strcasecmp(*list, "EXCEPT") == 0) /* EXCEPT: give up */
163 if ((match = (*match_fn) (*list, item))) /* True or FAIL */
166 /* Process exceptions to True or FAIL matches. */
168 if (match != False) {
169 while (*list && strcasecmp(*list, "EXCEPT"))
172 for (; *list; list++) {
173 if ((*match_fn) (*list, item)) /* Exception Found */
182 /* return true if access should be allowed */
183 BOOL allow_access(char **deny_list,char **allow_list,
184 char *cname,char *caddr)
191 /* if it is loopback then always allow unless specifically denied */
192 if (strcmp(caddr, "127.0.0.1") == 0) {
194 * If 127.0.0.1 matches both allow and deny then allow.
195 * Patch from Steve Langasek vorlon@netexpress.net.
198 list_match(deny_list,(char *)client,client_match) &&
200 !list_match(allow_list,(char *)client, client_match))) {
206 /* if theres no deny list and no allow list then allow access */
207 if ((!deny_list || *deny_list == 0) &&
208 (!allow_list || *allow_list == 0)) {
212 /* if there is an allow list but no deny list then allow only hosts
214 if (!deny_list || *deny_list == 0)
215 return(list_match(allow_list,(char *)client,client_match));
217 /* if theres a deny list but no allow list then allow
218 all hosts not on the deny list */
219 if (!allow_list || *allow_list == 0)
220 return(!list_match(deny_list,(char *)client,client_match));
222 /* if there are both types of list then allow all hosts on the
224 if (list_match(allow_list,(char *)client,client_match))
227 /* if there are both types of list and it's not on the allow then
228 allow it if its not on the deny */
229 if (list_match(deny_list,(char *)client,client_match))
235 /* return true if the char* contains ip addrs only. Used to avoid
236 gethostbyaddr() calls */
237 static BOOL only_ipaddrs_in_list(char** list)
241 if (!list) return True;
243 for (; *list ; list++)
245 /* factor out the special strings */
246 if (!strcasecmp(*list, "ALL") || !strcasecmp(*list, "FAIL") ||
247 !strcasecmp(*list, "EXCEPT"))
252 if (!is_ipaddress(*list))
256 * if we failed, make sure that it was not because the token
257 * was a network/netmask pair. Only network/netmask pairs
260 if ((p=strchr_m(*list, '/')) == NULL)
263 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list));
272 /* return true if access should be allowed to a service for a socket */
273 BOOL check_access(int sock, char **allow_list, char **deny_list)
276 BOOL only_ip = False;
278 if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0))
285 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
286 if (only_ipaddrs_in_list(allow_list) && only_ipaddrs_in_list(deny_list))
289 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
290 ret = allow_access(deny_list,allow_list, "", get_socket_addr(sock));
294 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
295 ret = allow_access(deny_list,allow_list, get_socket_name(sock),
296 get_socket_addr(sock));
301 DEBUG(2,("Allowed connection from %s (%s)\n",
302 only_ip ? "" : get_socket_name(sock),
303 get_socket_addr(sock)));
307 DEBUG(0,("Denied connection from %s (%s)\n",
308 only_ip ? "" : get_socket_name(sock),
309 get_socket_addr(sock)));