made "hide files" and "veto files" into per-service parameter sections,
[Samba.git] / source / lib / access.c
blob599cb5ca7e390df9843ddc1db989930c697bebde
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.
5 The code is used here with permission.
7 The code has been considerably changed from the original. Bug reports
8 should be sent to samba-bugs@samba.anu.edu.au
9 */
11 #include "includes.h"
13 #define ALLOW_PURE_ADDRESSES
15 extern int DEBUGLEVEL;
17 #ifndef INADDR_NONE
18 #define INADDR_NONE ((uint32)~0)
19 #endif
22 #define Good True
23 #define Bad False
25 #define CLIENT_MATCH client_match
27 /* Delimiters for lists of daemons or clients. */
29 static char sep[] = ", \t";
31 /* Constants to be used in assignments only, not in comparisons... */
33 #define YES 1
34 #define NO 0
35 #define FAIL (-1)
37 /* Forward declarations. */
38 static int list_match(char *list,char *item, int (*match_fn)());
39 static int client_match(char *tok,char *item);
40 static int string_match(char *tok,char *s);
41 static int masked_match(char *tok, char *slash, char *s);
43 /* Size of logical line buffer. */
44 #define BUFLEN 2048
46 /* return true if access should be allowed to a service*/
47 BOOL check_access(int snum)
49 char *denyl,*allowl;
50 BOOL ret = False;
52 denyl = lp_hostsdeny(snum);
53 if (denyl) denyl = strdup(denyl);
55 allowl = lp_hostsallow(snum);
56 if (allowl) allowl = strdup(allowl);
58 if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
59 ret = True;
61 if (!ret)
63 if (allow_access(denyl,allowl,client_name(),client_addr()))
65 if (snum >= 0)
66 DEBUG(2,("Allowed connection from %s (%s) to %s\n",
67 client_name(),client_addr(),
68 lp_servicename(snum)));
69 ret = True;
71 else
72 if (snum >= 0)
73 DEBUG(0,("Denied connection from %s (%s) to %s\n",
74 client_name(),client_addr(),
75 lp_servicename(snum)));
78 if (denyl) free(denyl);
79 if (allowl) free(allowl);
80 return(ret);
84 /* return true if access should be allowed */
85 BOOL allow_access(char *deny_list,char *allow_list,char *cname,char *caddr)
87 char *client[2];
89 client[0] = cname;
90 client[1] = caddr;
92 /* if theres no deny list and no allow list then allow access */
93 if ((!deny_list || *deny_list == 0) && (!allow_list || *allow_list == 0))
94 return(True);
96 /* if there is an allow list but no deny list then allow only hosts
97 on the allow list */
98 if (!deny_list || *deny_list == 0)
99 return(list_match(allow_list,(char *)client,CLIENT_MATCH));
101 /* if theres a deny list but no allow list then allow
102 all hosts not on the deny list */
103 if (!allow_list || *allow_list == 0)
104 return(!list_match(deny_list,(char *)client,CLIENT_MATCH));
106 /* if there are both type of list then allow all hosts on the allow list */
107 if (list_match(allow_list,(char *)client,CLIENT_MATCH))
108 return (True);
110 /* if there are both type of list and it's not on the allow then
111 allow it if its not on the deny */
112 if (list_match(deny_list,(char *)client,CLIENT_MATCH))
113 return (False);
115 return (True);
118 /* list_match - match an item against a list of tokens with exceptions */
119 /* (All modifications are marked with the initials "jkf") */
120 static int list_match(char *list,char *item, int (*match_fn)())
122 char *tok;
123 char *listcopy; /* jkf */
124 int match = NO;
127 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
128 * overwriting the list given as its first parameter.
131 /* jkf -- can get called recursively with NULL list */
132 listcopy = (list == 0) ? (char *)0 : strdup(list);
135 * Process tokens one at a time. We have exhausted all possible matches
136 * when we reach an "EXCEPT" token or the end of the list. If we do find
137 * a match, look for an "EXCEPT" list and recurse to determine whether
138 * the match is affected by any exceptions.
141 for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
142 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
143 break;
144 if ((match = (*match_fn) (tok, item))) /* YES or FAIL */
145 break;
147 /* Process exceptions to YES or FAIL matches. */
149 if (match != NO) {
150 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
151 /* VOID */ ;
152 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
153 if (listcopy != 0) free(listcopy); /* jkf */
154 return (match);
158 if (listcopy != 0) free(listcopy); /* jkf */
159 return (NO);
163 /* client_match - match host name and address against token */
164 static int client_match(char *tok,char *item)
166 char **client = (char **)item;
167 int match;
170 * Try to match the address first. If that fails, try to match the host
171 * name if available.
174 if ((match = string_match(tok, client[1])) == 0)
175 if (client[0][0] != 0)
176 match = string_match(tok, client[0]);
177 return (match);
180 /* string_match - match string against token */
181 static int string_match(char *tok,char *s)
183 int tok_len;
184 int str_len;
185 char *cut;
188 * Return YES if a token has the magic value "ALL". Return FAIL if the
189 * token is "FAIL". If the token starts with a "." (domain name), return
190 * YES if it matches the last fields of the string. If the token has the
191 * magic value "LOCAL", return YES if the string does not contain a "."
192 * character. If the token ends on a "." (network number), return YES if
193 * it matches the first fields of the string. If the token begins with a
194 * "@" (netgroup name), return YES if the string is a (host) member of
195 * the netgroup. Return YES if the token fully matches the string. If the
196 * token is a netnumber/netmask pair, return YES if the address is a
197 * member of the specified subnet.
200 if (tok[0] == '.') { /* domain: match last fields */
201 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
202 && strcasecmp(tok, s + str_len - tok_len) == 0)
203 return (YES);
204 } else if (tok[0] == '@') { /* netgroup: look it up */
205 #ifdef NETGROUP
206 static char *mydomain = NULL;
207 char *hostname = NULL;
208 BOOL netgroup_ok = False;
210 if (!mydomain) yp_get_default_domain(&mydomain);
212 if (!mydomain) {
213 DEBUG(0,("Unable to get default yp domain.\n"));
214 return NO;
216 if (!(hostname = strdup(s))) {
217 DEBUG(1,("out of memory for strdup!\n"));
218 return NO;
221 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
223 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
224 hostname,
225 mydomain,
226 tok+1,
227 BOOLSTR(netgroup_ok)));
229 #ifdef NETGROUP_INSECURE
230 /* if you really want netgroups that match non qualified names
231 then define NETGROUP_INSECURE. It can, however, be a big
232 security hole */
234 char *clnt_domain;
235 if (!netgroup_ok && (clnt_domain=strchr(hostname,'.'))) {
236 *clnt_domain++ = '\0';
237 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
240 #endif
242 free(hostname);
244 if (netgroup_ok) return(YES);
245 #else
246 DEBUG(0,("access: netgroup support is not configured\n"));
247 return (NO);
248 #endif
249 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
250 return (YES);
251 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
252 return (FAIL);
253 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
254 if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
255 return (YES);
256 } else if (!strcasecmp(tok, s)) { /* match host name or address */
257 return (YES);
258 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
259 if (strncmp(tok, s, tok_len) == 0)
260 return (YES);
261 } else if ((cut = strchr(tok, '/')) != 0) { /* netnumber/netmask */
262 if (isdigit(s[0]) && masked_match(tok, cut, s))
263 return (YES);
265 return (NO);
268 /* masked_match - match address against netnumber/netmask */
269 static int masked_match(char *tok, char *slash, char *s)
271 uint32 net;
272 uint32 mask;
273 uint32 addr;
275 if ((addr = interpret_addr(s)) == INADDR_NONE)
276 return (NO);
277 *slash = 0;
278 net = interpret_addr(tok);
279 *slash = '/';
280 if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
281 DEBUG(0,("access: bad net/mask access control: %s", tok));
282 return (NO);
284 return ((addr & mask) == net);