Basic doc changes to keep up to date.
[kai/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 Andrew.Tridgell@anu.edu.au
9 */
10
11 #include "includes.h"
12
13 #define ALLOW_PURE_ADDRESSES
14
15 extern int DEBUGLEVEL;
16
17 #ifndef INADDR_NONE
18 #define INADDR_NONE     ((unsigned long)~0)
19 #endif
20
21
22 #define FROM_ADDRLEN  (4*3+3+1)
23 #define Good True
24 #define Bad False
25
26 #define CLIENT_MATCH client_match
27
28 /* Delimiters for lists of daemons or clients. */
29
30 static char sep[] = ", \t";
31
32 /* Constants to be used in assignments only, not in comparisons... */
33
34 #define YES             1
35 #define NO              0
36 #define FAIL            (-1)
37
38 /* Forward declarations. */
39 static int list_match(char *list,char *item, int (*match_fn)());
40 static int client_match(char *tok,char *item);
41 static int string_match(char *tok,char *s);
42 static int masked_match(char *tok, char *slash, char *s);
43 static int matchname(char *remotehost,struct in_addr  addr);
44
45 /* Size of logical line buffer. */
46 #define BUFLEN 2048
47
48
49 /* return true if access should be allowed to a service*/
50 BOOL check_access(int snum)
51 {
52   extern int Client;
53   extern struct from_host Client_info;
54   char *denyl,*allowl;
55   BOOL ret = False;
56
57   denyl = lp_hostsdeny(snum);
58   if (denyl) denyl = strdup(denyl);
59
60   allowl = lp_hostsallow(snum);
61   if (allowl) allowl = strdup(allowl);
62
63
64   fromhost(Client,&Client_info);
65
66   if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
67     ret = True;
68
69   if (!ret)
70     {
71       if (!fromhost(Client,&Client_info))
72         DEBUG(0,("ERROR: Can't get from_host info\n"));
73       else
74         {
75           if (allow_access(denyl,allowl,&Client_info))
76             {
77               if (snum >= 0)
78                 DEBUG(2,("Allowed connection from %s (%s) to %s\n",
79                          Client_info.name,Client_info.addr,
80                          lp_servicename(snum)));
81               ret = True;
82             }
83           else
84             if (snum >= 0)
85               DEBUG(0,("Denied connection from %s (%s) to %s\n",
86                        Client_info.name,Client_info.addr,
87                        lp_servicename(snum)));
88         }
89     }
90
91   if (denyl) free(denyl);
92   if (allowl) free(allowl);
93   return(ret);
94 }
95
96
97 /* return true if access should be allowed */
98 BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client)
99 {
100   /* if theres no deny list and no allow list then allow access */
101   if ((!deny_list || *deny_list == 0) && (!allow_list || *allow_list == 0))
102     return(True);  
103
104   /* if there is an allow list but no deny list then allow only hosts
105      on the allow list */
106   if (!deny_list || *deny_list == 0)
107     return(list_match(allow_list,(char *)client,CLIENT_MATCH));
108
109   /* if theres a deny list but no allow list then allow
110      all hosts not on the deny list */
111   if (!allow_list || *allow_list == 0)
112     return(!list_match(deny_list,(char *)client,CLIENT_MATCH));
113
114   /* if there are both type of list then allow all hosts on the allow list */
115   if (list_match(allow_list,(char *)client,CLIENT_MATCH))
116     return (True);
117
118   /* if there are both type of list and it's not on the allow then
119      allow it if its not on the deny */
120   if (list_match(deny_list,(char *)client,CLIENT_MATCH))
121     return (False);
122
123   return (True);
124 }
125
126 /* list_match - match an item against a list of tokens with exceptions */
127 /* (All modifications are marked with the initials "jkf") */
128 static int list_match(char *list,char *item, int (*match_fn)())
129 {
130     char   *tok;
131     char   *listcopy;           /* jkf */
132     int     match = NO;
133
134     /*
135      * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
136      * overwriting the list given as its first parameter.
137      */
138
139     /* jkf -- can get called recursively with NULL list */
140     listcopy = (list == 0) ? (char *)0 : strdup(list);
141
142     /*
143      * Process tokens one at a time. We have exhausted all possible matches
144      * when we reach an "EXCEPT" token or the end of the list. If we do find
145      * a match, look for an "EXCEPT" list and recurse to determine whether
146      * the match is affected by any exceptions.
147      */
148
149     for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
150         if (strcasecmp(tok, "EXCEPT") == 0)     /* EXCEPT: give up */
151             break;
152         if ((match = (*match_fn) (tok, item)))  /* YES or FAIL */
153             break;
154     }
155     /* Process exceptions to YES or FAIL matches. */
156
157     if (match != NO) {
158         while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
159              /* VOID */ ;
160         if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
161             if (listcopy != 0) free(listcopy); /* jkf */
162             return (match);
163         }
164     }
165
166     if (listcopy != 0) free(listcopy); /* jkf */
167     return (NO);
168 }
169
170
171 /* client_match - match host name and address against token */
172 static int client_match(char *tok,char *item)
173 {
174     struct from_host *client = (struct from_host *) item;
175     int     match;
176
177     /*
178      * Try to match the address first. If that fails, try to match the host
179      * name if available.
180      */
181
182     if ((match = string_match(tok, client->addr)) == 0)
183         if (client->name[0] != 0)
184             match = string_match(tok, client->name);
185     return (match);
186 }
187
188 /* string_match - match string against token */
189 static int string_match(char *tok,char *s)
190 {
191     int     tok_len;
192     int     str_len;
193     char   *cut;
194
195     /*
196      * Return YES if a token has the magic value "ALL". Return FAIL if the
197      * token is "FAIL". If the token starts with a "." (domain name), return
198      * YES if it matches the last fields of the string. If the token has the
199      * magic value "LOCAL", return YES if the string does not contain a "."
200      * character. If the token ends on a "." (network number), return YES if
201      * it matches the first fields of the string. If the token begins with a
202      * "@" (netgroup name), return YES if the string is a (host) member of
203      * the netgroup. Return YES if the token fully matches the string. If the
204      * token is a netnumber/netmask pair, return YES if the address is a
205      * member of the specified subnet.
206      */
207
208     if (tok[0] == '.') {                        /* domain: match last fields */
209         if ((str_len = strlen(s)) > (tok_len = strlen(tok))
210             && strcasecmp(tok, s + str_len - tok_len) == 0)
211             return (YES);
212     } else if (tok[0] == '@') {                 /* netgroup: look it up */
213 #ifdef  NETGROUP
214       static char *mydomain = NULL;
215       char *hostname = NULL;
216       BOOL netgroup_ok = False;
217
218       if (!mydomain) yp_get_default_domain(&mydomain);
219
220       if (!(hostname = strdup(s))) {
221         DEBUG(1,("out of memory for strdup!\n"));
222         return NO;
223       }
224
225       netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
226
227       DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n", 
228                hostname,
229                mydomain, 
230                tok+1,
231                BOOLSTR(netgroup_ok)));
232
233 #ifdef NETGROUP_INSECURE
234       /* if you really want netgroups that match non qualified names
235          then define NETGROUP_INSECURE. It can, however, be a big
236          security hole */
237       {
238         char        *clnt_domain;
239         if (!netgroup_ok && (clnt_domain=strchr(hostname,'.'))) {
240           *clnt_domain++ = '\0';
241           netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
242         }
243       }
244 #endif
245
246       free(hostname);
247       
248       if (netgroup_ok) return(YES);
249 #else
250       DEBUG(0,("access: netgroup support is not configured"));
251       return (NO);
252 #endif
253     } else if (strcasecmp(tok, "ALL") == 0) {   /* all: match any */
254         return (YES);
255     } else if (strcasecmp(tok, "FAIL") == 0) {  /* fail: match any */
256         return (FAIL);
257     } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
258         if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
259             return (YES);
260     } else if (!strcasecmp(tok, s)) {   /* match host name or address */
261         return (YES);
262     } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {       /* network */
263         if (strncmp(tok, s, tok_len) == 0)
264             return (YES);
265     } else if ((cut = strchr(tok, '/')) != 0) { /* netnumber/netmask */
266         if (isdigit(s[0]) && masked_match(tok, cut, s))
267             return (YES);
268     }
269     return (NO);
270 }
271
272 /* masked_match - match address against netnumber/netmask */
273 static int masked_match(char *tok, char *slash, char *s)
274 {
275     unsigned long net;
276     unsigned long mask;
277     unsigned long addr;
278
279     if ((addr = interpret_addr(s)) == INADDR_NONE)
280         return (NO);
281     *slash = 0;
282     net = interpret_addr(tok);
283     *slash = '/';
284     if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
285         DEBUG(0,("access: bad net/mask access control: %s", tok));
286         return (NO);
287     }
288     return ((addr & mask) == net);
289 }
290
291
292 /* fromhost - find out what is at the other end of a socket */
293 BOOL fromhost(int sock,struct from_host *f)
294 {
295     static struct sockaddr sa;
296     struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
297     struct hostent *hp;
298     int     length = sizeof(sa);
299     static char addr_buf[FROM_ADDRLEN];
300     static char name_buf[MAXHOSTNAMELEN];
301     BOOL   takeAddressAsHostname = False;
302
303     if (getpeername(sock, &sa, &length) < 0) 
304       {
305         DEBUG(0,("getpeername failed\n"));
306         return(False);
307       }
308
309     f->sin = sockin;
310     f->addr = strcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
311
312     /* Look up the remote host name. */
313     if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
314                             sizeof(sockin->sin_addr),
315                             AF_INET)) == 0) {
316       DEBUG(1,("Gethostbyaddr failed for %s\n",addr_buf));
317 #ifdef ALLOW_PURE_ADDRESSES
318       takeAddressAsHostname = True;
319 #else
320       return(False);
321 #endif
322     }
323
324     /* Save the host name. A later gethostbyxxx() call may clobber it. */
325     f->name = StrnCpy(name_buf,
326                       takeAddressAsHostname? f->addr : hp->h_name,
327                       sizeof(name_buf) - 1);
328
329     /*
330      * Verify that the host name does not belong to someone else. If host
331      * name verification fails, pretend that the host name lookup failed.
332      */
333     if (!takeAddressAsHostname && !matchname(f->name, sockin->sin_addr))
334       {
335         DEBUG(0,("Matchname failed\n"));
336         return(False);
337       }
338
339     return(True);       
340 }
341
342 /* matchname - determine if host name matches IP address */
343 static int matchname(char *remotehost,struct in_addr  addr)
344 {
345   struct hostent *hp;
346   int     i;
347   
348   if ((hp = Get_Hostbyname(remotehost)) == 0) {
349     DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
350     return (Bad);
351   } 
352
353     /*
354      * Make sure that gethostbyname() returns the "correct" host name.
355      * Unfortunately, gethostbyname("localhost") sometimes yields
356      * "localhost.domain". Since the latter host name comes from the
357      * local DNS, we just have to trust it (all bets are off if the local
358      * DNS is perverted). We always check the address list, though.
359      */
360   
361   if (strcasecmp(remotehost, hp->h_name)
362       && strcasecmp(remotehost, "localhost")) {
363     DEBUG(0,("host name/name mismatch: %s != %s",
364           remotehost, hp->h_name));
365     return (Bad);
366   }
367         
368   /* Look up the host address in the address list we just got. */
369   for (i = 0; hp->h_addr_list[i]; i++) {
370     if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
371       return (Good);
372   }
373
374   /*
375    * The host name does not map to the original host address. Perhaps
376    * someone has compromised a name server. More likely someone botched
377    * it, but that could be dangerous, too.
378    */
379   
380   DEBUG(0,("host name/address mismatch: %s != %s",
381         inet_ntoa(addr), hp->h_name));
382   return (Bad);
383 }
384
385