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