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