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 Andrew.Tridgell@anu.edu.au
13 #define ALLOW_PURE_ADDRESSES
15 extern int DEBUGLEVEL;
18 #define INADDR_NONE ((unsigned long)~0)
22 #define FROM_ADDRLEN (4*3+3+1)
26 #define CLIENT_MATCH client_match
28 /* Delimiters for lists of daemons or clients. */
30 static char sep[] = ", \t";
32 /* Constants to be used in assignments only, not in comparisons... */
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);
45 /* Size of logical line buffer. */
49 /* return true if access should be allowed to a service*/
50 BOOL check_access(int snum)
53 extern struct from_host Client_info;
57 denyl = lp_hostsdeny(snum);
58 if (denyl) denyl = strdup(denyl);
60 allowl = lp_hostsallow(snum);
61 if (allowl) allowl = strdup(allowl);
64 fromhost(Client,&Client_info);
66 if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
71 if (!fromhost(Client,&Client_info))
72 DEBUG(0,("ERROR: Can't get from_host info\n"));
75 if (allow_access(denyl,allowl,&Client_info))
78 DEBUG(2,("Allowed connection from %s (%s) to %s\n",
79 Client_info.name,Client_info.addr,
80 lp_servicename(snum)));
85 DEBUG(0,("Denied connection from %s (%s) to %s\n",
86 Client_info.name,Client_info.addr,
87 lp_servicename(snum)));
91 if (denyl) free(denyl);
92 if (allowl) free(allowl);
97 /* return true if access should be allowed */
98 BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client)
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))
104 /* if there is an allow list but no deny list then allow only hosts
106 if (!deny_list || *deny_list == 0)
107 return(list_match(allow_list,(char *)client,CLIENT_MATCH));
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));
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))
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))
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)())
131 char *listcopy; /* jkf */
135 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
136 * overwriting the list given as its first parameter.
139 /* jkf -- can get called recursively with NULL list */
140 listcopy = (list == 0) ? (char *)0 : strdup(list);
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.
149 for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
150 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
152 if ((match = (*match_fn) (tok, item))) /* YES or FAIL */
155 /* Process exceptions to YES or FAIL matches. */
158 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
160 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
161 if (listcopy != 0) free(listcopy); /* jkf */
166 if (listcopy != 0) free(listcopy); /* jkf */
171 /* client_match - match host name and address against token */
172 static int client_match(char *tok,char *item)
174 struct from_host *client = (struct from_host *) item;
178 * Try to match the address first. If that fails, try to match the host
182 if ((match = string_match(tok, client->addr)) == 0)
183 if (client->name[0] != 0)
184 match = string_match(tok, client->name);
188 /* string_match - match string against token */
189 static int string_match(char *tok,char *s)
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.
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)
212 } else if (tok[0] == '@') { /* netgroup: look it up */
214 static char *mydomain = NULL;
215 char *hostname = NULL;
216 BOOL netgroup_ok = False;
218 if (!mydomain) yp_get_default_domain(&mydomain);
220 if (!(hostname = strdup(s))) {
221 DEBUG(1,("out of memory for strdup!\n"));
225 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
227 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
231 BOOLSTR(netgroup_ok)));
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
239 if (!netgroup_ok && (clnt_domain=strchr(hostname,'.'))) {
240 *clnt_domain++ = '\0';
241 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
248 if (netgroup_ok) return(YES);
250 DEBUG(0,("access: netgroup support is not configured"));
253 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
255 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
257 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
258 if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
260 } else if (!strcasecmp(tok, s)) { /* match host name or address */
262 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
263 if (strncmp(tok, s, tok_len) == 0)
265 } else if ((cut = strchr(tok, '/')) != 0) { /* netnumber/netmask */
266 if (isdigit(s[0]) && masked_match(tok, cut, s))
272 /* masked_match - match address against netnumber/netmask */
273 static int masked_match(char *tok, char *slash, char *s)
279 if ((addr = interpret_addr(s)) == INADDR_NONE)
282 net = interpret_addr(tok);
284 if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
285 DEBUG(0,("access: bad net/mask access control: %s", tok));
288 return ((addr & mask) == net);
292 /* fromhost - find out what is at the other end of a socket */
293 BOOL fromhost(int sock,struct from_host *f)
295 static struct sockaddr sa;
296 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
298 int length = sizeof(sa);
299 static char addr_buf[FROM_ADDRLEN];
300 static char name_buf[MAXHOSTNAMELEN];
301 BOOL takeAddressAsHostname = False;
303 if (getpeername(sock, &sa, &length) < 0)
305 DEBUG(0,("getpeername failed\n"));
310 f->addr = strcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
312 /* Look up the remote host name. */
313 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
314 sizeof(sockin->sin_addr),
316 DEBUG(1,("Gethostbyaddr failed for %s\n",addr_buf));
317 #ifdef ALLOW_PURE_ADDRESSES
318 takeAddressAsHostname = True;
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);
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.
333 if (!takeAddressAsHostname && !matchname(f->name, sockin->sin_addr))
335 DEBUG(0,("Matchname failed\n"));
342 /* matchname - determine if host name matches IP address */
343 static int matchname(char *remotehost,struct in_addr addr)
348 if ((hp = Get_Hostbyname(remotehost)) == 0) {
349 DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
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.
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));
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)
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.
380 DEBUG(0,("host name/address mismatch: %s != %s",
381 inet_ntoa(addr), hp->h_name));