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;
17 /* masked_match - match address against netnumber/netmask */
18 static int masked_match(char *tok, char *slash, char *s)
24 if ((addr = interpret_addr(s)) == INADDR_NONE)
27 net = interpret_addr(tok);
29 if (net == INADDR_NONE ||
30 (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
31 DEBUG(0,("access: bad net/mask access control: %s\n", tok));
34 return ((addr & mask) == net);
37 /* string_match - match string against token */
38 static int string_match(char *tok,char *s, char *invalid_char)
46 /* Return True if a token has the magic value "ALL". Return
47 * FAIL if the token is "FAIL". If the token starts with a "."
48 * (domain name), return True if it matches the last fields of
49 * the string. If the token has the magic value "LOCAL",
50 * return True if the string does not contain a "."
51 * character. If the token ends on a "." (network number),
52 * return True if it matches the first fields of the
53 * string. If the token begins with a "@" (netgroup name),
54 * return True if the string is a (host) member of the
55 * netgroup. Return True if the token fully matches the
56 * string. If the token is a netnumber/netmask pair, return
57 * True if the address is a member of the specified subnet. */
59 if (tok[0] == '.') { /* domain: match last fields */
60 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
61 && strcasecmp(tok, s + str_len - tok_len) == 0)
63 } else if (tok[0] == '@') { /* netgroup: look it up */
65 static char *mydomain = NULL;
66 char *hostname = NULL;
67 BOOL netgroup_ok = False;
69 if (!mydomain) yp_get_default_domain(&mydomain);
72 DEBUG(0,("Unable to get default yp domain.\n"));
75 if (!(hostname = strdup(s))) {
76 DEBUG(1,("out of memory for strdup!\n"));
80 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
82 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
86 BOOLSTR(netgroup_ok)));
90 if (netgroup_ok) return(True);
92 DEBUG(0,("access: netgroup support is not configured\n"));
95 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
97 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
99 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
100 if (strchr_m(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
102 } else if (!strcasecmp(tok, s)) { /* match host name or address */
104 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
105 if (strncmp(tok, s, tok_len) == 0)
107 } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
108 if (isdigit((int)s[0]) && masked_match(tok, cut, s))
110 } else if (strchr_m(tok, '*') != 0) {
112 } else if (strchr_m(tok, '?') != 0) {
119 /* client_match - match host name and address against token */
120 static int client_match(char *tok,char *item)
122 char **client = (char **)item;
124 char invalid_char = '\0';
127 * Try to match the address first. If that fails, try to match the host
131 if ((match = string_match(tok, client[1], &invalid_char)) == 0) {
133 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
134 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
136 if (client[0][0] != 0)
137 match = string_match(tok, client[0], &invalid_char);
140 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
141 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
147 /* list_match - match an item against a list of tokens with exceptions */
148 static int list_match(char **list,char *item, int (*match_fn)(char *, char *))
152 if (!list) return False;
155 * Process tokens one at a time. We have exhausted all possible matches
156 * when we reach an "EXCEPT" token or the end of the list. If we do find
157 * a match, look for an "EXCEPT" list and recurse to determine whether
158 * the match is affected by any exceptions.
161 for (; *list ; list++) {
162 if (strcasecmp(*list, "EXCEPT") == 0) /* EXCEPT: give up */
164 if ((match = (*match_fn) (*list, item))) /* True or FAIL */
167 /* Process exceptions to True or FAIL matches. */
169 if (match != False) {
170 while (*list && strcasecmp(*list, "EXCEPT"))
173 for (; *list; list++) {
174 if ((*match_fn) (*list, item)) /* Exception Found */
183 /* return true if access should be allowed */
184 BOOL allow_access(char **deny_list,char **allow_list,
185 char *cname,char *caddr)
192 /* if it is loopback then always allow unless specifically denied */
193 if (strcmp(caddr, "127.0.0.1") == 0) {
195 list_match(deny_list,(char *)client,client_match)) {
201 /* if theres no deny list and no allow list then allow access */
202 if ((!deny_list || *deny_list == 0) &&
203 (!allow_list || *allow_list == 0)) {
207 /* if there is an allow list but no deny list then allow only hosts
209 if (!deny_list || *deny_list == 0)
210 return(list_match(allow_list,(char *)client,client_match));
212 /* if theres a deny list but no allow list then allow
213 all hosts not on the deny list */
214 if (!allow_list || *allow_list == 0)
215 return(!list_match(deny_list,(char *)client,client_match));
217 /* if there are both type of list then allow all hosts on the
219 if (list_match(allow_list,(char *)client,client_match))
222 /* if there are both type of list and it's not on the allow then
223 allow it if its not on the deny */
224 if (list_match(deny_list,(char *)client,client_match))
230 /* return true if the char* contains ip addrs only. Used to avoid
231 gethostbyaddr() calls */
232 static BOOL only_ipaddrs_in_list(char** list)
236 if (!list) return True;
238 for (; *list ; list++)
240 /* factor out the special strings */
241 if (!strcasecmp(*list, "ALL") || !strcasecmp(*list, "FAIL") ||
242 !strcasecmp(*list, "EXCEPT"))
247 if (!is_ipaddress(*list))
251 * if we failed, make sure that it was not because the token
252 * was a network/netmask pair. Only network/netmask pairs
255 if ((p=strchr_m(*list, '/')) == NULL)
258 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list));
267 /* return true if access should be allowed to a service for a socket */
268 BOOL check_access(int sock, char **allow_list, char **deny_list)
271 BOOL only_ip = False;
273 if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0))
280 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
281 if (only_ipaddrs_in_list(allow_list) && only_ipaddrs_in_list(deny_list))
284 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
285 ret = allow_access(deny_list,allow_list, "", get_socket_addr(sock));
289 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
290 ret = allow_access(deny_list,allow_list, get_socket_name(sock),
291 get_socket_addr(sock));
296 DEBUG(2,("Allowed connection from %s (%s)\n",
297 only_ip ? "" : get_socket_name(sock),
298 get_socket_addr(sock)));
302 DEBUG(0,("Denied connection from %s (%s)\n",
303 only_ip ? "" : get_socket_name(sock),
304 get_socket_addr(sock)));