mit-samba: Allow nesting on the event context
[idra/samba.git] / source3 / lib / access.c
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
16 #define NAME_INDEX 0
17 #define ADDR_INDEX 1
18
19 /* masked_match - match address against netnumber/netmask */
20 static bool masked_match(const char *tok, const char *slash, const char *s)
21 {
22         struct sockaddr_storage ss_mask;
23         struct sockaddr_storage ss_tok;
24         struct sockaddr_storage ss_host;
25         char *tok_copy = NULL;
26
27         if (!interpret_string_addr(&ss_host, s, 0)) {
28                 return false;
29         }
30
31         if (*tok == '[') {
32                 /* IPv6 address - remove braces. */
33                 tok_copy = SMB_STRDUP(tok+1);
34                 if (!tok_copy) {
35                         return false;
36                 }
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;
43                 }
44                 /* Remove the terminating '/' */
45                 tok_copy[PTR_DIFF(slash,tok)] = '\0';
46         }
47
48         if (!interpret_string_addr(&ss_tok, tok_copy, 0)) {
49                 SAFE_FREE(tok_copy);
50                 return false;
51         }
52
53         SAFE_FREE(tok_copy);
54
55         if (strlen(slash + 1) > 2) {
56                 if (!interpret_string_addr(&ss_mask, slash+1, 0)) {
57                         return false;
58                 }
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;
64                 }
65                 if (!make_netmask(&ss_mask, &ss_tok, val)) {
66                         return false;
67                 }
68         }
69
70         return same_net((struct sockaddr *)(void *)&ss_host,
71                         (struct sockaddr *)(void *)&ss_tok,
72                         (struct sockaddr *)(void *)&ss_mask);
73 }
74
75 /* string_match - match string s against token tok */
76 static bool string_match(const char *tok,const char *s)
77 {
78         size_t     tok_len;
79         size_t     str_len;
80         const char   *cut;
81
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.
94          */
95
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;
100                 }
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;
107
108                 if (memcache_lookup(
109                             NULL, SINGLETON_CACHE,
110                             data_blob_string_const_null("yp_default_domain"),
111                             &tmp)) {
112
113                         SMB_ASSERT(tmp.length > 0);
114                         mydomain = (tmp.data[0] == '\0')
115                                 ? NULL : (char *)tmp.data;
116                 }
117                 else {
118                         yp_get_default_domain(&mydomain);
119
120                         memcache_add(
121                                 NULL, SINGLETON_CACHE,
122                                 data_blob_string_const_null("yp_default_domain"),
123                                 data_blob_string_const_null(mydomain?mydomain:""));
124                 }
125
126                 if (!mydomain) {
127                         DEBUG(0,("Unable to get default yp domain. "
128                                 "Try without it.\n"));
129                 }
130                 if (!(hostname = SMB_STRDUP(s))) {
131                         DEBUG(1,("out of memory for strdup!\n"));
132                         return false;
133                 }
134
135                 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
136
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)));
142
143                 SAFE_FREE(hostname);
144
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;
158                 }
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;
164                 }
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);
173                 }
174         } else if (strchr_m(tok, '*') != 0 || strchr_m(tok, '?')) {
175                 return unix_wild_match(tok, s);
176         }
177         return false;
178 }
179
180 /* client_match - match host name and address against token */
181 bool client_match(const char *tok, const void *item)
182 {
183         const char **client = (const char **)item;
184
185         /*
186          * Try to match the address first. If that fails, try to match the host
187          * name if available.
188          */
189
190         if (string_match(tok, client[ADDR_INDEX])) {
191                 return true;
192         }
193
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.
201                  */
202                 if (string_match(tok, (client[ADDR_INDEX])+7)) {
203                         return true;
204                 }
205         }
206
207         if (client[NAME_INDEX][0] != 0) {
208                 if (string_match(tok, client[NAME_INDEX])) {
209                         return true;
210                 }
211         }
212
213         return false;
214 }
215
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 *))
219 {
220         bool match = false;
221
222         if (!list) {
223                 return false;
224         }
225
226         /*
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.
231          */
232
233         for (; *list ; list++) {
234                 if (strequal(*list, "EXCEPT")) {
235                         /* EXCEPT: give up */
236                         break;
237                 }
238                 if ((match = (*match_fn) (*list, item))) {
239                         /* true or FAIL */
240                         break;
241                 }
242         }
243         /* Process exceptions to true or FAIL matches. */
244
245         if (match != false) {
246                 while (*list  && !strequal(*list, "EXCEPT")) {
247                         list++;
248                 }
249
250                 for (; *list; list++) {
251                         if ((*match_fn) (*list, item)) {
252                                 /* Exception Found */
253                                 return false;
254                         }
255                 }
256         }
257
258         return match;
259 }
260
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)
266 {
267         const char *client[2];
268
269         client[NAME_INDEX] = cname;
270         client[ADDR_INDEX] = caddr;
271
272         /* if it is loopback then always allow unless specifically denied */
273         if (strcmp(caddr, "127.0.0.1") == 0 || strcmp(caddr, "::1") == 0) {
274                 /*
275                  * If 127.0.0.1 matches both allow and deny then allow.
276                  * Patch from Steve Langasek vorlon@netexpress.net.
277                  */
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;
283                 }
284                 return true;
285         }
286
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;
291         }
292
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));
297         }
298
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));
303         }
304
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;
309         }
310
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;
315         }
316
317         return true;
318 }
319
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)
325 {
326         bool ret;
327         char *nc_cname = smb_xstrdup(cname);
328         char *nc_caddr = smb_xstrdup(caddr);
329
330         ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
331
332         DEBUG(ret ? 3 : 0,
333               ("%s connection from %s (%s)\n",
334                ret ? "Allowed" : "Denied", nc_cname, nc_caddr));
335
336         SAFE_FREE(nc_cname);
337         SAFE_FREE(nc_caddr);
338         return ret;
339 }