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