s4:sam.py - enhance "member" tests
[Samba/gebeck_regimport.git] / source3 / lib / access.c
blobb674144150a348a3445796ab029c8f6bf3652aa3
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"
16 #define NAME_INDEX 0
17 #define ADDR_INDEX 1
19 /* masked_match - match address against netnumber/netmask */
20 static bool masked_match(const char *tok, const char *slash, const char *s)
22 struct sockaddr_storage ss_mask;
23 struct sockaddr_storage ss_tok;
24 struct sockaddr_storage ss_host;
25 char *tok_copy = NULL;
27 if (!interpret_string_addr(&ss_host, s, 0)) {
28 return false;
31 if (*tok == '[') {
32 /* IPv6 address - remove braces. */
33 tok_copy = SMB_STRDUP(tok+1);
34 if (!tok_copy) {
35 return false;
37 /* Remove the terminating ']' */
38 tok_copy[PTR_DIFF(slash,tok)-1] = '\0';
39 } else {
40 tok_copy = SMB_STRDUP(tok);
41 if (!tok_copy) {
42 return false;
44 /* Remove the terminating '/' */
45 tok_copy[PTR_DIFF(slash,tok)] = '\0';
48 if (!interpret_string_addr(&ss_tok, tok_copy, 0)) {
49 SAFE_FREE(tok_copy);
50 return false;
53 SAFE_FREE(tok_copy);
55 if (strlen(slash + 1) > 2) {
56 if (!interpret_string_addr(&ss_mask, slash+1, 0)) {
57 return false;
59 } else {
60 char *endp = NULL;
61 unsigned long val = strtoul(slash+1, &endp, 0);
62 if (slash+1 == endp || (endp && *endp != '\0')) {
63 return false;
65 if (!make_netmask(&ss_mask, &ss_tok, val)) {
66 return false;
70 return same_net((struct sockaddr *)(void *)&ss_host,
71 (struct sockaddr *)(void *)&ss_tok,
72 (struct sockaddr *)(void *)&ss_mask);
75 /* string_match - match string s against token tok */
76 static bool string_match(const char *tok,const char *s)
78 size_t tok_len;
79 size_t str_len;
80 const char *cut;
82 /* Return true if a token has the magic value "ALL". Return
83 * true if the token is "FAIL". If the token starts with a "."
84 * (domain name), return true if it matches the last fields of
85 * the string. If the token has the magic value "LOCAL",
86 * return true if the string does not contain a "."
87 * character. If the token ends on a "." (network number),
88 * return true if it matches the first fields of the
89 * string. If the token begins with a "@" (netgroup name),
90 * return true if the string is a (host) member of the
91 * netgroup. Return true if the token fully matches the
92 * string. If the token is a netnumber/netmask pair, return
93 * true if the address is a member of the specified subnet.
96 if (tok[0] == '.') { /* domain: match last fields */
97 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
98 && strequal(tok, s + str_len - tok_len)) {
99 return true;
101 } else if (tok[0] == '@') { /* netgroup: look it up */
102 #ifdef HAVE_NETGROUP
103 DATA_BLOB tmp;
104 char *mydomain = NULL;
105 char *hostname = NULL;
106 bool netgroup_ok = false;
108 if (memcache_lookup(
109 NULL, SINGLETON_CACHE,
110 data_blob_string_const_null("yp_default_domain"),
111 &tmp)) {
113 SMB_ASSERT(tmp.length > 0);
114 mydomain = (tmp.data[0] == '\0')
115 ? NULL : (char *)tmp.data;
117 else {
118 yp_get_default_domain(&mydomain);
120 memcache_add(
121 NULL, SINGLETON_CACHE,
122 data_blob_string_const_null("yp_default_domain"),
123 data_blob_string_const_null(mydomain?mydomain:""));
126 if (!mydomain) {
127 DEBUG(0,("Unable to get default yp domain. "
128 "Try without it.\n"));
130 if (!(hostname = SMB_STRDUP(s))) {
131 DEBUG(1,("out of memory for strdup!\n"));
132 return false;
135 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
137 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
138 hostname,
139 mydomain?mydomain:"(ANY)",
140 tok+1,
141 BOOLSTR(netgroup_ok)));
143 SAFE_FREE(hostname);
145 if (netgroup_ok)
146 return true;
147 #else
148 DEBUG(0,("access: netgroup support is not configured\n"));
149 return false;
150 #endif
151 } else if (strequal(tok, "ALL")) { /* all: match any */
152 return true;
153 } else if (strequal(tok, "FAIL")) { /* fail: match any */
154 return true;
155 } else if (strequal(tok, "LOCAL")) { /* local: no dots */
156 if (strchr_m(s, '.') == 0 && !strequal(s, "unknown")) {
157 return true;
159 } else if (strequal(tok, s)) { /* match host name or address */
160 return true;
161 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
162 if (strncmp(tok, s, tok_len) == 0) {
163 return true;
165 } else if ((cut = strchr_m(tok, '/')) != 0) { /* netnumber/netmask */
166 if ((isdigit(s[0]) && strchr_m(tok, '.') != NULL) ||
167 (tok[0] == '[' && cut > tok && cut[-1] == ']') ||
168 ((isxdigit(s[0]) || s[0] == ':') &&
169 strchr_m(tok, ':') != NULL)) {
170 /* IPv4/netmask or
171 * [IPv6:addr]/netmask or IPv6:addr/netmask */
172 return masked_match(tok, cut, s);
174 } else if (strchr_m(tok, '*') != 0 || strchr_m(tok, '?')) {
175 return unix_wild_match(tok, s);
177 return false;
180 /* client_match - match host name and address against token */
181 bool client_match(const char *tok, const void *item)
183 const char **client = (const char **)item;
186 * Try to match the address first. If that fails, try to match the host
187 * name if available.
190 if (string_match(tok, client[ADDR_INDEX])) {
191 return true;
194 if (strnequal(client[ADDR_INDEX],"::ffff:",7) &&
195 !strnequal(tok, "::ffff:",7)) {
196 /* client[ADDR_INDEX] is an IPv4 mapped to IPv6, but
197 * the list item is not. Try and match the IPv4 part of
198 * address only. This will happen a lot on IPv6 enabled
199 * systems with IPv4 allow/deny lists in smb.conf.
200 * Bug #5311. JRA.
202 if (string_match(tok, (client[ADDR_INDEX])+7)) {
203 return true;
207 if (client[NAME_INDEX][0] != 0) {
208 if (string_match(tok, client[NAME_INDEX])) {
209 return true;
213 return false;
216 /* list_match - match an item against a list of tokens with exceptions */
217 bool list_match(const char **list,const void *item,
218 bool (*match_fn)(const char *, const void *))
220 bool match = false;
222 if (!list) {
223 return false;
227 * Process tokens one at a time. We have exhausted all possible matches
228 * when we reach an "EXCEPT" token or the end of the list. If we do find
229 * a match, look for an "EXCEPT" list and recurse to determine whether
230 * the match is affected by any exceptions.
233 for (; *list ; list++) {
234 if (strequal(*list, "EXCEPT")) {
235 /* EXCEPT: give up */
236 break;
238 if ((match = (*match_fn) (*list, item))) {
239 /* true or FAIL */
240 break;
243 /* Process exceptions to true or FAIL matches. */
245 if (match != false) {
246 while (*list && !strequal(*list, "EXCEPT")) {
247 list++;
250 for (; *list; list++) {
251 if ((*match_fn) (*list, item)) {
252 /* Exception Found */
253 return false;
258 return match;
261 /* return true if access should be allowed */
262 static bool allow_access_internal(const char **deny_list,
263 const char **allow_list,
264 const char *cname,
265 const char *caddr)
267 const char *client[2];
269 client[NAME_INDEX] = cname;
270 client[ADDR_INDEX] = caddr;
272 /* if it is loopback then always allow unless specifically denied */
273 if (strcmp(caddr, "127.0.0.1") == 0 || strcmp(caddr, "::1") == 0) {
275 * If 127.0.0.1 matches both allow and deny then allow.
276 * Patch from Steve Langasek vorlon@netexpress.net.
278 if (deny_list &&
279 list_match(deny_list,client,client_match) &&
280 (!allow_list ||
281 !list_match(allow_list,client, client_match))) {
282 return false;
284 return true;
287 /* if theres no deny list and no allow list then allow access */
288 if ((!deny_list || *deny_list == 0) &&
289 (!allow_list || *allow_list == 0)) {
290 return true;
293 /* if there is an allow list but no deny list then allow only hosts
294 on the allow list */
295 if (!deny_list || *deny_list == 0) {
296 return(list_match(allow_list,client,client_match));
299 /* if theres a deny list but no allow list then allow
300 all hosts not on the deny list */
301 if (!allow_list || *allow_list == 0) {
302 return(!list_match(deny_list,client,client_match));
305 /* if there are both types of list then allow all hosts on the
306 allow list */
307 if (list_match(allow_list,(const char *)client,client_match)) {
308 return true;
311 /* if there are both types of list and it's not on the allow then
312 allow it if its not on the deny */
313 if (list_match(deny_list,(const char *)client,client_match)) {
314 return false;
317 return true;
320 /* return true if access should be allowed */
321 bool allow_access(const char **deny_list,
322 const char **allow_list,
323 const char *cname,
324 const char *caddr)
326 bool ret;
327 char *nc_cname = smb_xstrdup(cname);
328 char *nc_caddr = smb_xstrdup(caddr);
330 ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
332 DEBUG(ret ? 3 : 0,
333 ("%s connection from %s (%s)\n",
334 ret ? "Allowed" : "Denied", nc_cname, nc_caddr));
336 SAFE_FREE(nc_cname);
337 SAFE_FREE(nc_caddr);
338 return ret;