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