This commit was manufactured by cvs2svn to create tag
[Samba/gbeck.git] / source / lib / access.c
blob31f9db4e554899a7d005285bc9e45159682bdd5c
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 extern int DEBUGLEVEL;
15 /* Delimiters for lists of daemons or clients. */
16 static char *sep = ", \t";
18 #define FAIL (-1)
20 /* masked_match - match address against netnumber/netmask */
21 static int masked_match(char *tok, char *slash, char *s)
23 uint32 net;
24 uint32 mask;
25 uint32 addr;
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);
37 return ((addr & mask) == net);
40 /* string_match - match string against token */
41 static int string_match(char *tok,char *s)
43 int tok_len;
44 int str_len;
45 char *cut;
47 /* Return True if a token has the magic value "ALL". Return
48 * FAIL if the token is "FAIL". If the token starts with a "."
49 * (domain name), return True if it matches the last fields of
50 * the string. If the token has the magic value "LOCAL",
51 * return True if the string does not contain a "."
52 * character. If the token ends on a "." (network number),
53 * return True if it matches the first fields of the
54 * string. If the token begins with a "@" (netgroup name),
55 * return True if the string is a (host) member of the
56 * netgroup. Return True if the token fully matches the
57 * string. If the token is a netnumber/netmask pair, return
58 * True if the address is a member of the specified subnet. */
60 if (tok[0] == '.') { /* domain: match last fields */
61 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
62 && strcasecmp(tok, s + str_len - tok_len) == 0)
63 return (True);
64 } else if (tok[0] == '@') { /* netgroup: look it up */
65 #ifdef HAVE_NETGROUP
66 static char *mydomain = NULL;
67 char *hostname = NULL;
68 BOOL netgroup_ok = False;
70 if (!mydomain) yp_get_default_domain(&mydomain);
72 if (!mydomain) {
73 DEBUG(0,("Unable to get default yp domain.\n"));
74 return False;
76 if (!(hostname = strdup(s))) {
77 DEBUG(1,("out of memory for strdup!\n"));
78 return False;
81 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
83 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
84 hostname,
85 mydomain,
86 tok+1,
87 BOOLSTR(netgroup_ok)));
89 free(hostname);
91 if (netgroup_ok) return(True);
92 #else
93 DEBUG(0,("access: netgroup support is not configured\n"));
94 return (False);
95 #endif
96 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
97 return (True);
98 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
99 return (FAIL);
100 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
101 if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
102 return (True);
103 } else if (!strcasecmp(tok, s)) { /* match host name or address */
104 return (True);
105 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
106 if (strncmp(tok, s, tok_len) == 0)
107 return (True);
108 } else if ((cut = strchr(tok, '/')) != 0) { /* netnumber/netmask */
109 if (isdigit((int)s[0]) && masked_match(tok, cut, s))
110 return (True);
112 return (False);
116 /* client_match - match host name and address against token */
117 static int client_match(char *tok,char *item)
119 char **client = (char **)item;
120 int match;
123 * Try to match the address first. If that fails, try to match the host
124 * name if available.
127 if ((match = string_match(tok, client[1])) == 0)
128 if (client[0][0] != 0)
129 match = string_match(tok, client[0]);
130 return (match);
133 /* list_match - match an item against a list of tokens with exceptions */
134 /* (All modifications are marked with the initials "jkf") */
135 static int list_match(char *list,char *item, int (*match_fn)(char *, char *))
137 char *tok;
138 char *listcopy; /* jkf */
139 int match = False;
142 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
143 * overwriting the list given as its first parameter.
146 /* jkf -- can get called recursively with NULL list */
147 listcopy = (list == 0) ? (char *)0 : strdup(list);
150 * Process tokens one at a time. We have exhausted all possible matches
151 * when we reach an "EXCEPT" token or the end of the list. If we do find
152 * a match, look for an "EXCEPT" list and recurse to determine whether
153 * the match is affected by any exceptions.
156 for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
157 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
158 break;
159 if ((match = (*match_fn) (tok, item))) /* True or FAIL */
160 break;
162 /* Process exceptions to True or FAIL matches. */
164 if (match != False) {
165 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
166 /* VOID */ ;
167 if (tok == 0 || list_match((char *) 0, item, match_fn) == False) {
168 if (listcopy != 0) free(listcopy); /* jkf */
169 return (match);
173 if (listcopy != 0) free(listcopy); /* jkf */
174 return (False);
178 /* return true if access should be allowed */
179 BOOL allow_access(char *deny_list,char *allow_list,
180 char *cname,char *caddr)
182 char *client[2];
184 client[0] = cname;
185 client[1] = caddr;
187 /* if theres no deny list and no allow list then allow access */
188 if ((!deny_list || *deny_list == 0) &&
189 (!allow_list || *allow_list == 0)) {
190 return(True);
193 /* if there is an allow list but no deny list then allow only hosts
194 on the allow list */
195 if (!deny_list || *deny_list == 0)
196 return(list_match(allow_list,(char *)client,client_match));
198 /* if theres a deny list but no allow list then allow
199 all hosts not on the deny list */
200 if (!allow_list || *allow_list == 0)
201 return(!list_match(deny_list,(char *)client,client_match));
203 /* if there are both type of list then allow all hosts on the
204 allow list */
205 if (list_match(allow_list,(char *)client,client_match))
206 return (True);
208 /* if there are both type of list and it's not on the allow then
209 allow it if its not on the deny */
210 if (list_match(deny_list,(char *)client,client_match))
211 return (False);
213 return (True);
216 /* return true if access should be allowed to a service for a socket */
217 BOOL check_access(int sock, char *allow_list, char *deny_list)
219 BOOL ret = False;
221 if (deny_list) deny_list = strdup(deny_list);
222 if (allow_list) allow_list = strdup(allow_list);
224 if ((!deny_list || *deny_list==0) && (!allow_list || *allow_list==0)) {
225 ret = True;
228 if (!ret) {
229 if (allow_access(deny_list,allow_list,
230 client_name(sock),client_addr(sock))) {
231 DEBUG(2,("Allowed connection from %s (%s)\n",
232 client_name(sock),client_addr(sock)));
233 ret = True;
234 } else {
235 DEBUG(0,("Denied connection from %s (%s)\n",
236 client_name(sock),client_addr(sock)));
240 if (deny_list) free(deny_list);
241 if (allow_list) free(allow_list);
242 return(ret);