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
13 #define ALLOW_PURE_ADDRESSES
15 extern int DEBUGLEVEL
;
18 #define INADDR_NONE ((uint32)~0)
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... */
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. */
46 /* return true if access should be allowed to a service*/
47 BOOL
check_access(int snum
)
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))
63 if (allow_access(denyl
,allowl
,client_name(),client_addr()))
66 DEBUG(2,("Allowed connection from %s (%s) to %s\n",
67 client_name(),client_addr(),
68 lp_servicename(snum
)));
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
);
84 /* return true if access should be allowed */
85 BOOL
allow_access(char *deny_list
,char *allow_list
,char *cname
,char *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))
96 /* if there is an allow list but no deny list then allow only hosts
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
))
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
))
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
)())
123 char *listcopy
; /* jkf */
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 */
144 if ((match
= (*match_fn
) (tok
, item
))) /* YES or FAIL */
147 /* Process exceptions to YES or FAIL matches. */
150 while ((tok
= strtok((char *) 0, sep
)) && strcasecmp(tok
, "EXCEPT"))
152 if (tok
== 0 || list_match((char *) 0, item
, match_fn
) == NO
) {
153 if (listcopy
!= 0) free(listcopy
); /* jkf */
158 if (listcopy
!= 0) free(listcopy
); /* jkf */
163 /* client_match - match host name and address against token */
164 static int client_match(char *tok
,char *item
)
166 char **client
= (char **)item
;
170 * Try to match the address first. If that fails, try to match the host
174 if ((match
= string_match(tok
, client
[1])) == 0)
175 if (client
[0][0] != 0)
176 match
= string_match(tok
, client
[0]);
180 /* string_match - match string against token */
181 static int string_match(char *tok
,char *s
)
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)
204 } else if (tok
[0] == '@') { /* netgroup: look it up */
206 static char *mydomain
= NULL
;
207 char *hostname
= NULL
;
208 BOOL netgroup_ok
= False
;
210 if (!mydomain
) yp_get_default_domain(&mydomain
);
213 DEBUG(0,("Unable to get default yp domain.\n"));
216 if (!(hostname
= strdup(s
))) {
217 DEBUG(1,("out of memory for strdup!\n"));
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",
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
235 if (!netgroup_ok
&& (clnt_domain
=strchr(hostname
,'.'))) {
236 *clnt_domain
++ = '\0';
237 netgroup_ok
= innetgr(tok
+ 1, hostname
, (char *) 0, mydomain
);
244 if (netgroup_ok
) return(YES
);
246 DEBUG(0,("access: netgroup support is not configured\n"));
249 } else if (strcasecmp(tok
, "ALL") == 0) { /* all: match any */
251 } else if (strcasecmp(tok
, "FAIL") == 0) { /* fail: match any */
253 } else if (strcasecmp(tok
, "LOCAL") == 0) { /* local: no dots */
254 if (strchr(s
, '.') == 0 && strcasecmp(s
, "unknown") != 0)
256 } else if (!strcasecmp(tok
, s
)) { /* match host name or address */
258 } else if (tok
[(tok_len
= strlen(tok
)) - 1] == '.') { /* network */
259 if (strncmp(tok
, s
, tok_len
) == 0)
261 } else if ((cut
= strchr(tok
, '/')) != 0) { /* netnumber/netmask */
262 if (isdigit(s
[0]) && masked_match(tok
, cut
, s
))
268 /* masked_match - match address against netnumber/netmask */
269 static int masked_match(char *tok
, char *slash
, char *s
)
275 if ((addr
= interpret_addr(s
)) == INADDR_NONE
)
278 net
= interpret_addr(tok
);
280 if (net
== INADDR_NONE
|| (mask
= interpret_addr(slash
+ 1)) == INADDR_NONE
) {
281 DEBUG(0,("access: bad net/mask access control: %s", tok
));
284 return ((addr
& mask
) == net
);