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