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