Fix reversed bool check for access. Found by kukks.
[abartlet/samba.git/.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
15 #define NAME_INDEX 0
16 #define ADDR_INDEX 1
17
18 /* masked_match - match address against netnumber/netmask */
19 static bool masked_match(const char *tok, const char *slash, const char *s)
20 {
21         struct sockaddr_storage ss_mask;
22         struct sockaddr_storage ss_tok;
23         struct sockaddr_storage ss_host;
24         char *tok_copy = NULL;
25
26         if (!interpret_string_addr(&ss_host, s, 0)) {
27                 return false;
28         }
29
30         if (*tok == '[') {
31                 /* IPv6 address - remove braces. */
32                 tok_copy = SMB_STRDUP(tok+1);
33                 if (!tok_copy) {
34                         return false;
35                 }
36                 /* Remove the terminating ']' */
37                 tok_copy[PTR_DIFF(slash,tok)-1] = '\0';
38         } else {
39                 tok_copy = SMB_STRDUP(tok);
40                 if (!tok_copy) {
41                         return false;
42                 }
43                 /* Remove the terminating '/' */
44                 tok_copy[PTR_DIFF(slash,tok)] = '\0';
45         }
46
47         if (!interpret_string_addr(&ss_tok, tok_copy, 0)) {
48                 SAFE_FREE(tok_copy);
49                 return false;
50         }
51
52         SAFE_FREE(tok_copy);
53
54         if (strlen(slash + 1) > 2) {
55                 if (!interpret_string_addr(&ss_mask, slash+1, 0)) {
56                         return false;
57                 }
58         } else {
59                 char *endp = NULL;
60                 unsigned long val = strtoul(slash+1, &endp, 0);
61                 if (slash+1 == endp || (endp && *endp != '\0')) {
62                         return false;
63                 }
64                 if (!make_netmask(&ss_mask, &ss_tok, val)) {
65                         return false;
66                 }
67         }
68
69         return same_net(&ss_host, &ss_tok, &ss_mask);
70 }
71
72 /* string_match - match string s against token tok */
73 static bool string_match(const char *tok,const char *s)
74 {
75         size_t     tok_len;
76         size_t     str_len;
77         const char   *cut;
78
79         /* Return true if a token has the magic value "ALL". Return
80          * true if the token is "FAIL". If the token starts with a "."
81          * (domain name), return true if it matches the last fields of
82          * the string. If the token has the magic value "LOCAL",
83          * return true if the string does not contain a "."
84          * character. If the token ends on a "." (network number),
85          * return true if it matches the first fields of the
86          * string. If the token begins with a "@" (netgroup name),
87          * return true if the string is a (host) member of the
88          * netgroup. Return true if the token fully matches the
89          * string. If the token is a netnumber/netmask pair, return
90          * true if the address is a member of the specified subnet.
91          */
92
93         if (tok[0] == '.') {                    /* domain: match last fields */
94                 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
95                     && strequal(tok, s + str_len - tok_len)) {
96                         return true;
97                 }
98         } else if (tok[0] == '@') { /* netgroup: look it up */
99 #ifdef  HAVE_NETGROUP
100                 static char *mydomain = NULL;
101                 char *hostname = NULL;
102                 bool netgroup_ok = false;
103
104                 if (!mydomain)
105                         yp_get_default_domain(&mydomain);
106
107                 if (!mydomain) {
108                         DEBUG(0,("Unable to get default yp domain. "
109                                 "Try without it.\n"));
110                 }
111                 if (!(hostname = SMB_STRDUP(s))) {
112                         DEBUG(1,("out of memory for strdup!\n"));
113                         return false;
114                 }
115
116                 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
117
118                 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
119                          hostname,
120                          mydomain?mydomain:"(ANY)",
121                          tok+1,
122                          BOOLSTR(netgroup_ok)));
123
124                 SAFE_FREE(hostname);
125
126                 if (netgroup_ok)
127                         return true;
128 #else
129                 DEBUG(0,("access: netgroup support is not configured\n"));
130                 return false;
131 #endif
132         } else if (strequal(tok, "ALL")) {      /* all: match any */
133                 return true;
134         } else if (strequal(tok, "FAIL")) {     /* fail: match any */
135                 return true;
136         } else if (strequal(tok, "LOCAL")) {    /* local: no dots */
137                 if (strchr_m(s, '.') == 0 && !strequal(s, "unknown")) {
138                         return true;
139                 }
140         } else if (strequal(tok, s)) {   /* match host name or address */
141                 return true;
142         } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {   /* network */
143                 if (strncmp(tok, s, tok_len) == 0) {
144                         return true;
145                 }
146         } else if ((cut = strchr_m(tok, '/')) != 0) {   /* netnumber/netmask */
147                 if ((isdigit(s[0]) && strchr_m(tok, '.') != NULL) ||
148                         (tok[0] == '[' && cut > tok && cut[-1] == ']') ||
149                         ((isxdigit(s[0]) || s[0] == ':') &&
150                                 strchr_m(tok, ':') != NULL)) {
151                         /* IPv4/netmask or
152                          * [IPv6:addr]/netmask or IPv6:addr/netmask */
153                         return masked_match(tok, cut, s);
154                 }
155         } else if (strchr_m(tok, '*') != 0 || strchr_m(tok, '?')) {
156                 return unix_wild_match(tok, s);
157         }
158         return false;
159 }
160
161 /* client_match - match host name and address against token */
162 static bool client_match(const char *tok, const void *item)
163 {
164         const char **client = (const char **)item;
165         bool match = false;
166
167         /*
168          * Try to match the address first. If that fails, try to match the host
169          * name if available.
170          */
171
172         if ((match = string_match(tok, client[ADDR_INDEX])) == false) {
173                 if (client[NAME_INDEX][0] != 0) {
174                         match = string_match(tok, client[NAME_INDEX]);
175                 }
176         }
177
178         return match;
179 }
180
181 /* list_match - match an item against a list of tokens with exceptions */
182 static bool list_match(const char **list,const void *item,
183                 bool (*match_fn)(const char *, const void *))
184 {
185         bool match = false;
186
187         if (!list) {
188                 return false;
189         }
190
191         /*
192          * Process tokens one at a time. We have exhausted all possible matches
193          * when we reach an "EXCEPT" token or the end of the list. If we do find
194          * a match, look for an "EXCEPT" list and recurse to determine whether
195          * the match is affected by any exceptions.
196          */
197
198         for (; *list ; list++) {
199                 if (strequal(*list, "EXCEPT")) {
200                         /* EXCEPT: give up */
201                         break;
202                 }
203                 if ((match = (*match_fn) (*list, item))) {
204                         /* true or FAIL */
205                         break;
206                 }
207         }
208         /* Process exceptions to true or FAIL matches. */
209
210         if (match != false) {
211                 while (*list  && !strequal(*list, "EXCEPT")) {
212                         list++;
213                 }
214
215                 for (; *list; list++) {
216                         if ((*match_fn) (*list, item)) {
217                                 /* Exception Found */
218                                 return false;
219                         }
220                 }
221         }
222
223         return match;
224 }
225
226 /* return true if access should be allowed */
227 static bool allow_access_internal(const char **deny_list,
228                                 const char **allow_list,
229                                 const char *cname,
230                                 const char *caddr)
231 {
232         const char *client[2];
233
234         client[NAME_INDEX] = cname;
235         client[ADDR_INDEX] = caddr;
236
237         /* if it is loopback then always allow unless specifically denied */
238         if (strcmp(caddr, "127.0.0.1") == 0 || strcmp(caddr, "::1") == 0) {
239                 /*
240                  * If 127.0.0.1 matches both allow and deny then allow.
241                  * Patch from Steve Langasek vorlon@netexpress.net.
242                  */
243                 if (deny_list &&
244                         list_match(deny_list,client,client_match) &&
245                                 (!allow_list ||
246                                 !list_match(allow_list,client, client_match))) {
247                         return false;
248                 }
249                 return true;
250         }
251
252         /* if theres no deny list and no allow list then allow access */
253         if ((!deny_list || *deny_list == 0) &&
254             (!allow_list || *allow_list == 0)) {
255                 return true;
256         }
257
258         /* if there is an allow list but no deny list then allow only hosts
259            on the allow list */
260         if (!deny_list || *deny_list == 0) {
261                 return(list_match(allow_list,client,client_match));
262         }
263
264         /* if theres a deny list but no allow list then allow
265            all hosts not on the deny list */
266         if (!allow_list || *allow_list == 0) {
267                 return(!list_match(deny_list,client,client_match));
268         }
269
270         /* if there are both types of list then allow all hosts on the
271            allow list */
272         if (list_match(allow_list,(const char *)client,client_match)) {
273                 return true;
274         }
275
276         /* if there are both types of list and it's not on the allow then
277            allow it if its not on the deny */
278         if (list_match(deny_list,(const char *)client,client_match)) {
279                 return false;
280         }
281
282         return true;
283 }
284
285 /* return true if access should be allowed */
286 bool allow_access(const char **deny_list,
287                 const char **allow_list,
288                 const char *cname,
289                 const char *caddr)
290 {
291         bool ret;
292         char *nc_cname = smb_xstrdup(cname);
293         char *nc_caddr = smb_xstrdup(caddr);
294
295         ret = allow_access_internal(deny_list, allow_list, nc_cname, nc_caddr);
296
297         SAFE_FREE(nc_cname);
298         SAFE_FREE(nc_caddr);
299         return ret;
300 }
301
302 /* return true if the char* contains ip addrs only.  Used to avoid
303 name lookup calls */
304
305 static bool only_ipaddrs_in_list(const char **list)
306 {
307         bool only_ip = true;
308
309         if (!list) {
310                 return true;
311         }
312
313         for (; *list ; list++) {
314                 /* factor out the special strings */
315                 if (strequal(*list, "ALL") || strequal(*list, "FAIL") ||
316                     strequal(*list, "EXCEPT")) {
317                         continue;
318                 }
319
320                 if (!is_ipaddress(*list)) {
321                         /*
322                          * If we failed, make sure that it was not because
323                          * the token was a network/netmask pair. Only
324                          * network/netmask pairs have a '/' in them.
325                          */
326                         if ((strchr_m(*list, '/')) == NULL) {
327                                 only_ip = false;
328                                 DEBUG(3,("only_ipaddrs_in_list: list has "
329                                         "non-ip address (%s)\n",
330                                         *list));
331                                 break;
332                         }
333                 }
334         }
335
336         return only_ip;
337 }
338
339 /* return true if access should be allowed to a service for a socket */
340 bool check_access(int sock, const char **allow_list, const char **deny_list)
341 {
342         bool ret = false;
343         bool only_ip = false;
344
345         if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0))
346                 ret = true;
347
348         if (!ret) {
349                 /* Bypass name resolution calls if the lists
350                  * only contain IP addrs */
351                 if (only_ipaddrs_in_list(allow_list) &&
352                                 only_ipaddrs_in_list(deny_list)) {
353                         only_ip = true;
354                         DEBUG (3, ("check_access: no hostnames "
355                                 "in host allow/deny list.\n"));
356                         ret = allow_access(deny_list,
357                                         allow_list,
358                                         "",
359                                         get_peer_addr(sock));
360                 } else {
361                         DEBUG (3, ("check_access: hostnames in "
362                                 "host allow/deny list.\n"));
363                         ret = allow_access(deny_list,
364                                         allow_list,
365                                         get_peer_name(sock,true),
366                                         get_peer_addr(sock));
367                 }
368
369                 if (ret) {
370                         DEBUG(2,("Allowed connection from %s (%s)\n",
371                                  only_ip ? "" : get_peer_name(sock,true),
372                                  get_peer_addr(sock)));
373                 } else {
374                         DEBUG(0,("Denied connection from %s (%s)\n",
375                                  only_ip ? "" : get_peer_name(sock,true),
376                                  get_peer_addr(sock)));
377                 }
378         }
379
380         return(ret);
381 }