source: vendor/3.6.23/source3/lib/access.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 9.0 KB
Line 
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.
4
5 The code is used here with permission.
6
7 The code has been considerably changed from the original. Bug reports
8 should be sent to samba@samba.org
9
10 Updated for IPv6 by Jeremy Allison (C) 2007.
11*/
12
13#include "includes.h"
14#include "memcache.h"
15#include "interfaces.h"
16
17#define NAME_INDEX 0
18#define ADDR_INDEX 1
19
20/* masked_match - match address against netnumber/netmask */
21static bool masked_match(const char *tok, const char *slash, const char *s)
22{
23 struct sockaddr_storage ss_mask;
24 struct sockaddr_storage ss_tok;
25 struct sockaddr_storage ss_host;
26 char *tok_copy = NULL;
27
28 if (!interpret_string_addr(&ss_host, s, 0)) {
29 return false;
30 }
31
32 if (*tok == '[') {
33 /* IPv6 address - remove braces. */
34 tok_copy = SMB_STRDUP(tok+1);
35 if (!tok_copy) {
36 return false;
37 }
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;
44 }
45 /* Remove the terminating '/' */
46 tok_copy[PTR_DIFF(slash,tok)] = '\0';
47 }
48
49 if (!interpret_string_addr(&ss_tok, tok_copy, 0)) {
50 SAFE_FREE(tok_copy);
51 return false;
52 }
53
54 SAFE_FREE(tok_copy);
55
56 if (strlen(slash + 1) > 2) {
57 if (!interpret_string_addr(&ss_mask, slash+1, 0)) {
58 return false;
59 }
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;
65 }
66 if (!make_netmask(&ss_mask, &ss_tok, val)) {
67 return false;
68 }
69 }
70
71 return same_net((struct sockaddr *)(void *)&ss_host,
72 (struct sockaddr *)(void *)&ss_tok,
73 (struct sockaddr *)(void *)&ss_mask);
74}
75
76/* string_match - match string s against token tok */
77static bool string_match(const char *tok,const char *s)
78{
79 size_t tok_len;
80 size_t str_len;
81 const char *cut;
82
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.
95 */
96
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;
101 }
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;
108
109 if (memcache_lookup(
110 NULL, SINGLETON_CACHE,
111 data_blob_string_const_null("yp_default_domain"),
112 &tmp)) {
113
114 SMB_ASSERT(tmp.length > 0);
115 mydomain = (tmp.data[0] == '\0')
116 ? NULL : (char *)tmp.data;
117 }
118 else {
119 yp_get_default_domain(&mydomain);
120
121 memcache_add(
122 NULL, SINGLETON_CACHE,
123 data_blob_string_const_null("yp_default_domain"),
124 data_blob_string_const_null(mydomain?mydomain:""));
125 }
126
127 if (!mydomain) {
128 DEBUG(0,("Unable to get default yp domain. "
129 "Try without it.\n"));
130 }
131 if (!(hostname = SMB_STRDUP(s))) {
132 DEBUG(1,("out of memory for strdup!\n"));
133 return false;
134 }
135
136 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
137
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)));
143
144 SAFE_FREE(hostname);
145
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;
159 }
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;
165 }
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);
174 }
175 } else if (strchr_m(tok, '*') != 0 || strchr_m(tok, '?')) {
176 return unix_wild_match(tok, s);
177 }
178 return false;
179}
180
181/* client_match - match host name and address against token */
182bool client_match(const char *tok, const void *item)
183{
184 const char **client = (const char **)item;
185 const char *tok_addr = tok;
186 const char *cli_addr = client[ADDR_INDEX];
187
188 /*
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.
192 */
193
194 if (strnequal(tok_addr, "::ffff:",7)) {
195 tok_addr += 7;
196 }
197
198 if (strnequal(cli_addr,"::ffff:",7)) {
199 cli_addr += 7;
200 }
201
202 /*
203 * Try to match the address first. If that fails, try to match the host
204 * name if available.
205 */
206
207 if (string_match(tok_addr, cli_addr)) {
208 return true;
209 }
210
211 if (client[NAME_INDEX][0] != 0) {
212 if (string_match(tok, client[NAME_INDEX])) {
213 return true;
214 }
215 }
216
217 return false;
218}
219
220/* list_match - match an item against a list of tokens with exceptions */
221bool list_match(const char **list,const void *item,
222 bool (*match_fn)(const char *, const void *))
223{
224 bool match = false;
225
226 if (!list) {
227 return false;
228 }
229
230 /*
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.
235 */
236
237 for (; *list ; list++) {
238 if (strequal(*list, "EXCEPT")) {
239 /* EXCEPT: give up */
240 break;
241 }
242 if ((match = (*match_fn) (*list, item))) {
243 /* true or FAIL */
244 break;
245 }
246 }
247 /* Process exceptions to true or FAIL matches. */
248
249 if (match != false) {
250 while (*list && !strequal(*list, "EXCEPT")) {
251 list++;
252 }
253
254 for (; *list; list++) {
255 if ((*match_fn) (*list, item)) {
256 /* Exception Found */
257 return false;
258 }
259 }
260 }
261
262 return match;
263}
264
265/* return true if access should be allowed */
266static bool allow_access_internal(const char **deny_list,
267 const char **allow_list,
268 const char *cname,
269 const char *caddr)
270{
271 const char *client[2];
272
273 client[NAME_INDEX] = cname;
274 client[ADDR_INDEX] = caddr;
275
276 /* if it is loopback then always allow unless specifically denied */
277 if (strcmp(caddr, "127.0.0.1") == 0 || strcmp(caddr, "::1") == 0) {
278 /*
279 * If 127.0.0.1 matches both allow and deny then allow.
280 * Patch from Steve Langasek vorlon@netexpress.net.
281 */
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;
287 }
288 return true;
289 }
290
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;
295 }
296
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));
301 }
302
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));
307 }
308
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;
313 }
314
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;
319 }
320
321 return true;
322}
323
324/* return true if access should be allowed */
325bool allow_access(const char **deny_list,
326 const char **allow_list,
327 const char *cname,
328 const char *caddr)
329{
330 bool ret;
331 char *nc_cname = smb_xstrdup(cname);
332 char *nc_caddr = smb_xstrdup(caddr);
333
334 ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
335
336 DEBUG(ret ? 3 : 0,
337 ("%s connection from %s (%s)\n",
338 ret ? "Allowed" : "Denied", nc_cname, nc_caddr));
339
340 SAFE_FREE(nc_cname);
341 SAFE_FREE(nc_caddr);
342 return ret;
343}
Note: See TracBrowser for help on using the repository browser.