s3-docs: overrided -> overridden
[Samba.git] / source3 / lib / access.c
blobf9cd9d547c1f08f42b86f13ee66635eb0fe2cced
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
10 Updated for IPv6 by Jeremy Allison (C) 2007.
13 #include "includes.h"
14 #include "memcache.h"
15 #include "interfaces.h"
17 #define NAME_INDEX 0
18 #define ADDR_INDEX 1
20 /* masked_match - match address against netnumber/netmask */
21 static bool masked_match(const char *tok, const char *slash, const char *s)
23 struct sockaddr_storage ss_mask;
24 struct sockaddr_storage ss_tok;
25 struct sockaddr_storage ss_host;
26 char *tok_copy = NULL;
28 if (!interpret_string_addr(&ss_host, s, 0)) {
29 return false;
32 if (*tok == '[') {
33 /* IPv6 address - remove braces. */
34 tok_copy = SMB_STRDUP(tok+1);
35 if (!tok_copy) {
36 return false;
38 /* Remove the terminating ']' */
39 tok_copy[PTR_DIFF(slash,tok)-1] = '\0';
40 } else {
41 tok_copy = SMB_STRDUP(tok);
42 if (!tok_copy) {
43 return false;
45 /* Remove the terminating '/' */
46 tok_copy[PTR_DIFF(slash,tok)] = '\0';
49 if (!interpret_string_addr(&ss_tok, tok_copy, 0)) {
50 SAFE_FREE(tok_copy);
51 return false;
54 SAFE_FREE(tok_copy);
56 if (strlen(slash + 1) > 2) {
57 if (!interpret_string_addr(&ss_mask, slash+1, 0)) {
58 return false;
60 } else {
61 char *endp = NULL;
62 unsigned long val = strtoul(slash+1, &endp, 0);
63 if (slash+1 == endp || (endp && *endp != '\0')) {
64 return false;
66 if (!make_netmask(&ss_mask, &ss_tok, val)) {
67 return false;
71 return same_net((struct sockaddr *)(void *)&ss_host,
72 (struct sockaddr *)(void *)&ss_tok,
73 (struct sockaddr *)(void *)&ss_mask);
76 /* string_match - match string s against token tok */
77 static bool string_match(const char *tok,const char *s)
79 size_t tok_len;
80 size_t str_len;
81 const char *cut;
83 /* Return true if a token has the magic value "ALL". Return
84 * true if the token is "FAIL". If the token starts with a "."
85 * (domain name), return true if it matches the last fields of
86 * the string. If the token has the magic value "LOCAL",
87 * return true if the string does not contain a "."
88 * character. If the token ends on a "." (network number),
89 * return true if it matches the first fields of the
90 * string. If the token begins with a "@" (netgroup name),
91 * return true if the string is a (host) member of the
92 * netgroup. Return true if the token fully matches the
93 * string. If the token is a netnumber/netmask pair, return
94 * true if the address is a member of the specified subnet.
97 if (tok[0] == '.') { /* domain: match last fields */
98 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
99 && strequal(tok, s + str_len - tok_len)) {
100 return true;
102 } else if (tok[0] == '@') { /* netgroup: look it up */
103 #ifdef HAVE_NETGROUP
104 DATA_BLOB tmp;
105 char *mydomain = NULL;
106 char *hostname = NULL;
107 bool netgroup_ok = false;
109 if (memcache_lookup(
110 NULL, SINGLETON_CACHE,
111 data_blob_string_const_null("yp_default_domain"),
112 &tmp)) {
114 SMB_ASSERT(tmp.length > 0);
115 mydomain = (tmp.data[0] == '\0')
116 ? NULL : (char *)tmp.data;
118 else {
119 yp_get_default_domain(&mydomain);
121 memcache_add(
122 NULL, SINGLETON_CACHE,
123 data_blob_string_const_null("yp_default_domain"),
124 data_blob_string_const_null(mydomain?mydomain:""));
127 if (!mydomain) {
128 DEBUG(0,("Unable to get default yp domain. "
129 "Try without it.\n"));
131 if (!(hostname = SMB_STRDUP(s))) {
132 DEBUG(1,("out of memory for strdup!\n"));
133 return false;
136 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
138 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
139 hostname,
140 mydomain?mydomain:"(ANY)",
141 tok+1,
142 BOOLSTR(netgroup_ok)));
144 SAFE_FREE(hostname);
146 if (netgroup_ok)
147 return true;
148 #else
149 DEBUG(0,("access: netgroup support is not configured\n"));
150 return false;
151 #endif
152 } else if (strequal(tok, "ALL")) { /* all: match any */
153 return true;
154 } else if (strequal(tok, "FAIL")) { /* fail: match any */
155 return true;
156 } else if (strequal(tok, "LOCAL")) { /* local: no dots */
157 if (strchr_m(s, '.') == 0 && !strequal(s, "unknown")) {
158 return true;
160 } else if (strequal(tok, s)) { /* match host name or address */
161 return true;
162 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
163 if (strncmp(tok, s, tok_len) == 0) {
164 return true;
166 } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
167 if ((isdigit(s[0]) && strchr_m(tok, '.') != NULL) ||
168 (tok[0] == '[' && cut > tok && cut[-1] == ']') ||
169 ((isxdigit(s[0]) || s[0] == ':') &&
170 strchr_m(tok, ':') != NULL)) {
171 /* IPv4/netmask or
172 * [IPv6:addr]/netmask or IPv6:addr/netmask */
173 return masked_match(tok, cut, s);
175 } else if (strchr_m(tok, '*') != 0 || strchr_m(tok, '?')) {
176 return unix_wild_match(tok, s);
178 return false;
181 /* client_match - match host name and address against token */
182 bool client_match(const char *tok, const void *item)
184 const char **client = (const char **)item;
185 const char *tok_addr = tok;
186 const char *cli_addr = client[ADDR_INDEX];
189 * tok and client[ADDR_INDEX] can be an IPv4 mapped to IPv6,
190 * we try and match the IPv4 part of address only.
191 * Bug #5311 and #7383.
194 if (strnequal(tok_addr, "::ffff:",7)) {
195 tok_addr += 7;
198 if (strnequal(cli_addr,"::ffff:",7)) {
199 cli_addr += 7;
203 * Try to match the address first. If that fails, try to match the host
204 * name if available.
207 if (string_match(tok_addr, cli_addr)) {
208 return true;
211 if (client[NAME_INDEX][0] != 0) {
212 if (string_match(tok, client[NAME_INDEX])) {
213 return true;
217 return false;
220 /* list_match - match an item against a list of tokens with exceptions */
221 bool list_match(const char **list,const void *item,
222 bool (*match_fn)(const char *, const void *))
224 bool match = false;
226 if (!list) {
227 return false;
231 * Process tokens one at a time. We have exhausted all possible matches
232 * when we reach an "EXCEPT" token or the end of the list. If we do find
233 * a match, look for an "EXCEPT" list and recurse to determine whether
234 * the match is affected by any exceptions.
237 for (; *list ; list++) {
238 if (strequal(*list, "EXCEPT")) {
239 /* EXCEPT: give up */
240 break;
242 if ((match = (*match_fn) (*list, item))) {
243 /* true or FAIL */
244 break;
247 /* Process exceptions to true or FAIL matches. */
249 if (match != false) {
250 while (*list && !strequal(*list, "EXCEPT")) {
251 list++;
254 for (; *list; list++) {
255 if ((*match_fn) (*list, item)) {
256 /* Exception Found */
257 return false;
262 return match;
265 /* return true if access should be allowed */
266 static bool allow_access_internal(const char **deny_list,
267 const char **allow_list,
268 const char *cname,
269 const char *caddr)
271 const char *client[2];
273 client[NAME_INDEX] = cname;
274 client[ADDR_INDEX] = caddr;
276 /* if it is loopback then always allow unless specifically denied */
277 if (strcmp(caddr, "127.0.0.1") == 0 || strcmp(caddr, "::1") == 0) {
279 * If 127.0.0.1 matches both allow and deny then allow.
280 * Patch from Steve Langasek vorlon@netexpress.net.
282 if (deny_list &&
283 list_match(deny_list,client,client_match) &&
284 (!allow_list ||
285 !list_match(allow_list,client, client_match))) {
286 return false;
288 return true;
291 /* if theres no deny list and no allow list then allow access */
292 if ((!deny_list || *deny_list == 0) &&
293 (!allow_list || *allow_list == 0)) {
294 return true;
297 /* if there is an allow list but no deny list then allow only hosts
298 on the allow list */
299 if (!deny_list || *deny_list == 0) {
300 return(list_match(allow_list,client,client_match));
303 /* if theres a deny list but no allow list then allow
304 all hosts not on the deny list */
305 if (!allow_list || *allow_list == 0) {
306 return(!list_match(deny_list,client,client_match));
309 /* if there are both types of list then allow all hosts on the
310 allow list */
311 if (list_match(allow_list,(const char *)client,client_match)) {
312 return true;
315 /* if there are both types of list and it's not on the allow then
316 allow it if its not on the deny */
317 if (list_match(deny_list,(const char *)client,client_match)) {
318 return false;
321 return true;
324 /* return true if access should be allowed */
325 bool allow_access(const char **deny_list,
326 const char **allow_list,
327 const char *cname,
328 const char *caddr)
330 bool ret;
331 char *nc_cname = smb_xstrdup(cname);
332 char *nc_caddr = smb_xstrdup(caddr);
334 ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
336 DEBUG(ret ? 3 : 0,
337 ("%s connection from %s (%s)\n",
338 ret ? "Allowed" : "Denied", nc_cname, nc_caddr));
340 SAFE_FREE(nc_cname);
341 SAFE_FREE(nc_caddr);
342 return ret;