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. */
57 if (tok[0] == '.') { /* domain: match last fields */
58 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
59 && strcasecmp(tok, s + str_len - tok_len) == 0)
61 } else if (tok[0] == '@') { /* netgroup: look it up */
63 static char *mydomain = NULL;
64 char *hostname = NULL;
65 BOOL netgroup_ok = False;
67 if (!mydomain) yp_get_default_domain(&mydomain);
70 DEBUG(0,("Unable to get default yp domain.\n"));
73 if (!(hostname = strdup(s))) {
74 DEBUG(1,("out of memory for strdup!\n"));
78 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
80 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
84 BOOLSTR(netgroup_ok)));
88 if (netgroup_ok) return(True);
90 DEBUG(0,("access: netgroup support is not configured\n"));
93 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
95 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
97 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
98 if (strchr_m(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
100 } else if (!strcasecmp(tok, s)) { /* match host name or address */
102 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
103 if (strncmp(tok, s, tok_len) == 0)
105 } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
106 if (isdigit((int)s[0]) && masked_match(tok, cut, s))
108 } else if (strchr_m(tok, '*') != 0) {
110 } else if (strchr_m(tok, '?') != 0) {
117 /* client_match - match host name and address against token */
118 static int client_match(char *tok,char *item)
120 char **client = (char **)item;
122 char invalid_char = '\0';
125 * Try to match the address first. If that fails, try to match the host
129 if ((match = string_match(tok, client[1], &invalid_char)) == 0) {
131 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
132 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
134 if (client[0][0] != 0)
135 match = string_match(tok, client[0], &invalid_char);
138 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
139 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
145 /* list_match - match an item against a list of tokens with exceptions */
146 static int list_match(char **list,char *item, int (*match_fn)(char *, char *))
150 if (!list) return False;
153 * Process tokens one at a time. We have exhausted all possible matches
154 * when we reach an "EXCEPT" token or the end of the list. If we do find
155 * a match, look for an "EXCEPT" list and recurse to determine whether
156 * the match is affected by any exceptions.
159 for (; *list ; list++) {
160 if (strcasecmp(*list, "EXCEPT") == 0) /* EXCEPT: give up */
162 if ((match = (*match_fn) (*list, item))) /* True or FAIL */
165 /* Process exceptions to True or FAIL matches. */
167 if (match != False) {
168 while (*list && strcasecmp(*list, "EXCEPT"))
171 for (; *list; list++) {
172 if ((*match_fn) (*list, item)) /* Exception Found */
181 /* return true if access should be allowed */
182 BOOL allow_access(char **deny_list,char **allow_list,
183 char *cname,char *caddr)
190 /* if it is loopback then always allow unless specifically denied */
191 if (strcmp(caddr, "127.0.0.1") == 0) {
193 list_match(deny_list,(char *)client,client_match)) {
199 /* if theres no deny list and no allow list then allow access */
200 if ((!deny_list || *deny_list == 0) &&
201 (!allow_list || *allow_list == 0)) {
205 /* if there is an allow list but no deny list then allow only hosts
207 if (!deny_list || *deny_list == 0)
208 return(list_match(allow_list,(char *)client,client_match));
210 /* if theres a deny list but no allow list then allow
211 all hosts not on the deny list */
212 if (!allow_list || *allow_list == 0)
213 return(!list_match(deny_list,(char *)client,client_match));
215 /* if there are both type of list then allow all hosts on the
217 if (list_match(allow_list,(char *)client,client_match))
220 /* if there are both type of list and it's not on the allow then
221 allow it if its not on the deny */
222 if (list_match(deny_list,(char *)client,client_match))
228 /* return true if the char* contains ip addrs only. Used to avoid
229 gethostbyaddr() calls */
230 static BOOL only_ipaddrs_in_list(char** list)
234 if (!list) return True;
236 for (; *list ; list++)
238 /* factor out the special strings */
239 if (!strcasecmp(*list, "ALL") || !strcasecmp(*list, "FAIL") ||
240 !strcasecmp(*list, "EXCEPT"))
245 if (!is_ipaddress(*list))
249 * if we failed, make sure that it was not because the token
250 * was a network/netmask pair. Only network/netmask pairs
253 if ((p=strchr_m(*list, '/')) == NULL)
256 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list));
265 /* return true if access should be allowed to a service for a socket */
266 BOOL check_access(int sock, char **allow_list, char **deny_list)
269 BOOL only_ip = False;
271 if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0))
278 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
279 if (only_ipaddrs_in_list(allow_list) && only_ipaddrs_in_list(deny_list))
282 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
283 ret = allow_access(deny_list,allow_list, "", get_socket_addr(sock));
287 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
288 ret = allow_access(deny_list,allow_list, get_socket_name(sock),
289 get_socket_addr(sock));
294 DEBUG(2,("Allowed connection from %s (%s)\n",
295 only_ip ? "" : get_socket_name(sock),
296 get_socket_addr(sock)));
300 DEBUG(0,("Denied connection from %s (%s)\n",
301 only_ip ? "" : get_socket_name(sock),
302 get_socket_addr(sock)));