preparing for release of 3.0-alpha18
[Samba.git] / source / lib / access.c
blob4e524735e49cb48394aefcd30fc17617fc5a1db2
1 /*
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
9 */
11 #include "includes.h"
13 #define FAIL (-1)
15 #define ALLONES ((uint32)0xFFFFFFFF)
17 /* masked_match - match address against netnumber/netmask */
18 static int masked_match(char *tok, char *slash, char *s)
20 uint32 net;
21 uint32 mask;
22 uint32 addr;
24 if ((addr = interpret_addr(s)) == INADDR_NONE)
25 return (False);
26 *slash = 0;
27 net = interpret_addr(tok);
28 *slash = '/';
30 if (strlen(slash + 1) > 2) {
31 mask = interpret_addr(slash + 1);
32 } else {
33 mask = (uint32)((ALLONES << atoi(slash + 1)) ^ ALLONES);
36 if (net == INADDR_NONE || mask == INADDR_NONE) {
37 DEBUG(0,("access: bad net/mask access control: %s\n", tok));
38 return (False);
40 return ((addr & mask) == net);
43 /* string_match - match string against token */
44 static int string_match(char *tok,char *s, char *invalid_char)
46 size_t tok_len;
47 size_t str_len;
48 char *cut;
50 *invalid_char = '\0';
52 /* Return True if a token has the magic value "ALL". Return
53 * FAIL if the token is "FAIL". If the token starts with a "."
54 * (domain name), return True if it matches the last fields of
55 * the string. If the token has the magic value "LOCAL",
56 * return True if the string does not contain a "."
57 * character. If the token ends on a "." (network number),
58 * return True if it matches the first fields of the
59 * string. If the token begins with a "@" (netgroup name),
60 * return True if the string is a (host) member of the
61 * netgroup. Return True if the token fully matches the
62 * string. If the token is a netnumber/netmask pair, return
63 * True if the address is a member of the specified subnet.
66 if (tok[0] == '.') { /* domain: match last fields */
67 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
68 && strcasecmp(tok, s + str_len - tok_len) == 0)
69 return (True);
70 } else if (tok[0] == '@') { /* netgroup: look it up */
71 #ifdef HAVE_NETGROUP
72 static char *mydomain = NULL;
73 char *hostname = NULL;
74 BOOL netgroup_ok = False;
76 if (!mydomain) yp_get_default_domain(&mydomain);
78 if (!mydomain) {
79 DEBUG(0,("Unable to get default yp domain.\n"));
80 return False;
82 if (!(hostname = strdup(s))) {
83 DEBUG(1,("out of memory for strdup!\n"));
84 return False;
87 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
89 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
90 hostname,
91 mydomain,
92 tok+1,
93 BOOLSTR(netgroup_ok)));
95 SAFE_FREE(hostname);
97 if (netgroup_ok) return(True);
98 #else
99 DEBUG(0,("access: netgroup support is not configured\n"));
100 return (False);
101 #endif
102 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
103 return (True);
104 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
105 return (FAIL);
106 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
107 if (strchr_m(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
108 return (True);
109 } else if (!strcasecmp(tok, s)) { /* match host name or address */
110 return (True);
111 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
112 if (strncmp(tok, s, tok_len) == 0)
113 return (True);
114 } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
115 if (isdigit((int)s[0]) && masked_match(tok, cut, s))
116 return (True);
117 } else if (strchr_m(tok, '*') != 0) {
118 *invalid_char = '*';
119 } else if (strchr_m(tok, '?') != 0) {
120 *invalid_char = '?';
122 return (False);
126 /* client_match - match host name and address against token */
127 static int client_match(char *tok,char *item)
129 char **client = (char **)item;
130 int match;
131 char invalid_char = '\0';
134 * Try to match the address first. If that fails, try to match the host
135 * name if available.
138 if ((match = string_match(tok, client[1], &invalid_char)) == 0) {
139 if(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 ));
143 if (client[0][0] != 0)
144 match = string_match(tok, client[0], &invalid_char);
146 if(invalid_char)
147 DEBUG(0,("client_match: address match failing due to invalid character '%c' found in \
148 token '%s' in an allow/deny hosts line.\n", invalid_char, tok ));
151 return (match);
154 /* list_match - match an item against a list of tokens with exceptions */
155 static int list_match(char **list,char *item, int (*match_fn)(char *, char *))
157 int match = False;
159 if (!list) return False;
162 * Process tokens one at a time. We have exhausted all possible matches
163 * when we reach an "EXCEPT" token or the end of the list. If we do find
164 * a match, look for an "EXCEPT" list and recurse to determine whether
165 * the match is affected by any exceptions.
168 for (; *list ; list++) {
169 if (strcasecmp(*list, "EXCEPT") == 0) /* EXCEPT: give up */
170 break;
171 if ((match = (*match_fn) (*list, item))) /* True or FAIL */
172 break;
174 /* Process exceptions to True or FAIL matches. */
176 if (match != False) {
177 while (*list && strcasecmp(*list, "EXCEPT"))
178 list++;
180 for (; *list; list++) {
181 if ((*match_fn) (*list, item)) /* Exception Found */
182 return False;
186 return (match);
190 /* return true if access should be allowed */
191 static BOOL allow_access_internal(char **deny_list,char **allow_list,
192 char *cname,char *caddr)
194 char *client[2];
196 client[0] = cname;
197 client[1] = caddr;
199 /* if it is loopback then always allow unless specifically denied */
200 if (strcmp(caddr, "127.0.0.1") == 0) {
202 * If 127.0.0.1 matches both allow and deny then allow.
203 * Patch from Steve Langasek vorlon@netexpress.net.
205 if (deny_list &&
206 list_match(deny_list,(char *)client,client_match) &&
207 (!allow_list ||
208 !list_match(allow_list,(char *)client, client_match))) {
209 return False;
211 return True;
214 /* if theres no deny list and no allow list then allow access */
215 if ((!deny_list || *deny_list == 0) &&
216 (!allow_list || *allow_list == 0)) {
217 return(True);
220 /* if there is an allow list but no deny list then allow only hosts
221 on the allow list */
222 if (!deny_list || *deny_list == 0)
223 return(list_match(allow_list,(char *)client,client_match));
225 /* if theres a deny list but no allow list then allow
226 all hosts not on the deny list */
227 if (!allow_list || *allow_list == 0)
228 return(!list_match(deny_list,(char *)client,client_match));
230 /* if there are both types of list then allow all hosts on the
231 allow list */
232 if (list_match(allow_list,(char *)client,client_match))
233 return (True);
235 /* if there are both types of list and it's not on the allow then
236 allow it if its not on the deny */
237 if (list_match(deny_list,(char *)client,client_match))
238 return (False);
240 return (True);
243 /* return true if access should be allowed */
244 BOOL allow_access(char **deny_list,char **allow_list,
245 const char *cname, const char *caddr)
247 BOOL ret;
249 char *nc_cname = smb_xstrdup(cname);
250 char *nc_caddr = smb_xstrdup(caddr);
252 ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
254 SAFE_FREE(nc_cname);
255 SAFE_FREE(nc_caddr);
256 return ret;
259 /* return true if the char* contains ip addrs only. Used to avoid
260 gethostbyaddr() calls */
261 static BOOL only_ipaddrs_in_list(char** list)
263 BOOL only_ip = True;
265 if (!list) return True;
267 for (; *list ; list++)
269 /* factor out the special strings */
270 if (!strcasecmp(*list, "ALL") || !strcasecmp(*list, "FAIL") ||
271 !strcasecmp(*list, "EXCEPT"))
273 continue;
276 if (!is_ipaddress(*list))
278 char *p;
280 * if we failed, make sure that it was not because the token
281 * was a network/netmask pair. Only network/netmask pairs
282 * have a '/' in them
284 if ((p=strchr_m(*list, '/')) == NULL)
286 only_ip = False;
287 DEBUG(3,("only_ipaddrs_in_list: list has non-ip address (%s)\n", *list));
288 break;
293 return only_ip;
296 /* return true if access should be allowed to a service for a socket */
297 BOOL check_access(int sock, char **allow_list, char **deny_list)
299 BOOL ret = False;
300 BOOL only_ip = False;
302 if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0))
304 ret = True;
307 if (!ret)
309 /* bypass gethostbyaddr() calls if the lists only contain IP addrs */
310 if (only_ipaddrs_in_list(allow_list) && only_ipaddrs_in_list(deny_list))
312 only_ip = True;
313 DEBUG (3, ("check_access: no hostnames in host allow/deny list.\n"));
314 ret = allow_access(deny_list,allow_list, "", get_socket_addr(sock));
316 else
318 DEBUG (3, ("check_access: hostnames in host allow/deny list.\n"));
319 ret = allow_access(deny_list,allow_list, get_socket_name(sock),
320 get_socket_addr(sock));
323 if (ret)
325 DEBUG(2,("Allowed connection from %s (%s)\n",
326 only_ip ? "" : get_socket_name(sock),
327 get_socket_addr(sock)));
329 else
331 DEBUG(0,("Denied connection from %s (%s)\n",
332 only_ip ? "" : get_socket_name(sock),
333 get_socket_addr(sock)));
337 return(ret);