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
14 #define ALLOW_PURE_ADDRESSES
16 extern int DEBUGLEVEL;
19 #define INADDR_NONE ((unsigned long)~0)
23 #define FROM_ADDRLEN (4*3+3+1)
27 #define CLIENT_MATCH client_match
29 /* Delimiters for lists of daemons or clients. */
31 static char sep[] = ", \t";
33 /* Constants to be used in assignments only, not in comparisons... */
39 /* Forward declarations. */
40 BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client);
41 static int list_match(char *list,char *item, int (*match_fn)());
42 static int client_match(char *tok,char *item);
43 static int string_match(char *tok,char *s);
44 static int masked_match(char *tok, char *slash, char *s);
45 static int matchname(char *remotehost,struct in_addr addr);
46 BOOL fromhost(int sock,struct from_host *f);
49 /* Size of logical line buffer. */
53 /* return true if access should be allowed to a service*/
54 BOOL check_access(int snum)
57 extern struct from_host Client_info;
61 denyl = lp_hostsdeny(snum);
62 if (denyl) denyl = strdup(denyl);
64 allowl = lp_hostsallow(snum);
65 if (allowl) allowl = strdup(allowl);
68 fromhost(Client,&Client_info);
70 if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
75 if (!fromhost(Client,&Client_info))
76 DEBUG(0,("ERROR: Can't get from_host info\n"));
79 if (allow_access(denyl,allowl,&Client_info))
82 DEBUG(2,("Allowed connection from %s (%s) to %s\n",
83 Client_info.name,Client_info.addr,
84 lp_servicename(snum)));
89 DEBUG(0,("Denied connection from %s (%s) to %s\n",
90 Client_info.name,Client_info.addr,
91 lp_servicename(snum)));
95 if (denyl) free(denyl);
96 if (allowl) free(allowl);
101 /* return true if access should be allowed */
102 BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client)
104 /* if theres no deny list and no allow list then allow access */
105 if ((!deny_list || *deny_list == 0) && (!allow_list || *allow_list == 0))
108 /* if there is an allow list but no deny list then allow only hosts
110 if (!deny_list || *deny_list == 0)
111 return(list_match(allow_list,(char *)client,CLIENT_MATCH));
113 /* if theres a deny list but no allow list then allow
114 all hosts not on the deny list */
115 if (!allow_list || *allow_list == 0)
116 return(!list_match(deny_list,(char *)client,CLIENT_MATCH));
118 /* if there are both type of list then allow all hosts on the allow list */
119 if (list_match(allow_list,(char *)client,CLIENT_MATCH))
122 /* if there are both type of list and it's not on the allow then
123 allow it if its not on the deny */
124 if (list_match(deny_list,(char *)client,CLIENT_MATCH))
130 /* list_match - match an item against a list of tokens with exceptions */
131 /* (All modifications are marked with the initials "jkf") */
132 static int list_match(char *list,char *item, int (*match_fn)())
135 char *listcopy; /* jkf */
139 * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
140 * overwriting the list given as its first parameter.
143 /* jkf -- can get called recursively with NULL list */
144 listcopy = (list == 0) ? (char *)0 : strdup(list);
147 * Process tokens one at a time. We have exhausted all possible matches
148 * when we reach an "EXCEPT" token or the end of the list. If we do find
149 * a match, look for an "EXCEPT" list and recurse to determine whether
150 * the match is affected by any exceptions.
153 for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
154 if (strcasecmp(tok, "EXCEPT") == 0) /* EXCEPT: give up */
156 if ((match = (*match_fn) (tok, item))) /* YES or FAIL */
159 /* Process exceptions to YES or FAIL matches. */
162 while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
164 if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
165 if (listcopy != 0) free(listcopy); /* jkf */
170 if (listcopy != 0) free(listcopy); /* jkf */
175 /* client_match - match host name and address against token */
176 static int client_match(char *tok,char *item)
178 struct from_host *client = (struct from_host *) item;
182 * Try to match the address first. If that fails, try to match the host
186 if ((match = string_match(tok, client->addr)) == 0)
187 if (client->name[0] != 0)
188 match = string_match(tok, client->name);
192 /* string_match - match string against token */
193 static int string_match(char *tok,char *s)
200 * Return YES if a token has the magic value "ALL". Return FAIL if the
201 * token is "FAIL". If the token starts with a "." (domain name), return
202 * YES if it matches the last fields of the string. If the token has the
203 * magic value "LOCAL", return YES if the string does not contain a "."
204 * character. If the token ends on a "." (network number), return YES if
205 * it matches the first fields of the string. If the token begins with a
206 * "@" (netgroup name), return YES if the string is a (host) member of
207 * the netgroup. Return YES if the token fully matches the string. If the
208 * token is a netnumber/netmask pair, return YES if the address is a
209 * member of the specified subnet.
212 if (tok[0] == '.') { /* domain: match last fields */
213 if ((str_len = strlen(s)) > (tok_len = strlen(tok))
214 && strcasecmp(tok, s + str_len - tok_len) == 0)
216 } else if (tok[0] == '@') { /* netgroup: look it up */
218 static char *mydomain = NULL;
219 char *hostname = NULL;
220 BOOL netgroup_ok = False;
222 if (!mydomain) yp_get_default_domain(&mydomain);
224 if (!(hostname = strdup(s))) {
225 DEBUG(1,("out of memory for strdup!\n"));
229 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
231 DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n",
235 BOOLSTR(netgroup_ok)));
237 #ifdef NETGROUP_INSECURE
238 /* if you really want netgroups that match non qualified names
239 then define NETGROUP_INSECURE. It can, however, be a big
243 if (!netgroup_ok && (clnt_domain=strchr(hostname,'.'))) {
244 *clnt_domain++ = '\0';
245 netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
252 if (netgroup_ok) return(YES);
254 DEBUG(0,("access: netgroup support is not configured"));
257 } else if (strcasecmp(tok, "ALL") == 0) { /* all: match any */
259 } else if (strcasecmp(tok, "FAIL") == 0) { /* fail: match any */
261 } else if (strcasecmp(tok, "LOCAL") == 0) { /* local: no dots */
262 if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
264 } else if (!strcasecmp(tok, s)) { /* match host name or address */
266 } else if (tok[(tok_len = strlen(tok)) - 1] == '.') { /* network */
267 if (strncmp(tok, s, tok_len) == 0)
269 } else if ((cut = strchr(tok, '/')) != 0) { /* netnumber/netmask */
270 if (isdigit(s[0]) && masked_match(tok, cut, s))
276 /* masked_match - match address against netnumber/netmask */
277 static int masked_match(char *tok, char *slash, char *s)
283 if ((addr = interpret_addr(s)) == INADDR_NONE)
286 net = interpret_addr(tok);
288 if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
289 DEBUG(0,("access: bad net/mask access control: %s", tok));
292 return ((addr & mask) == net);
296 /* fromhost - find out what is at the other end of a socket */
297 BOOL fromhost(int sock,struct from_host *f)
299 static struct sockaddr sa;
300 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
302 int length = sizeof(sa);
303 static char addr_buf[FROM_ADDRLEN];
304 static char name_buf[MAXHOSTNAMELEN];
305 BOOL takeAddressAsHostname = False;
307 if (getpeername(sock, &sa, &length) < 0)
309 DEBUG(0,("getpeername failed\n"));
314 f->addr = strcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
316 /* Look up the remote host name. */
317 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
318 sizeof(sockin->sin_addr),
320 DEBUG(1,("Gethostbyaddr failed for %s\n",addr_buf));
321 #ifdef ALLOW_PURE_ADDRESSES
322 takeAddressAsHostname = True;
328 /* Save the host name. A later gethostbyxxx() call may clobber it. */
329 f->name = StrnCpy(name_buf,
330 takeAddressAsHostname? f->addr : hp->h_name,
331 sizeof(name_buf) - 1);
334 * Verify that the host name does not belong to someone else. If host
335 * name verification fails, pretend that the host name lookup failed.
337 if (!takeAddressAsHostname && !matchname(f->name, sockin->sin_addr))
339 DEBUG(0,("Matchname failed\n"));
346 /* matchname - determine if host name matches IP address */
347 static int matchname(char *remotehost,struct in_addr addr)
352 if ((hp = Get_Hostbyname(remotehost)) == 0) {
353 DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
358 * Make sure that gethostbyname() returns the "correct" host name.
359 * Unfortunately, gethostbyname("localhost") sometimes yields
360 * "localhost.domain". Since the latter host name comes from the
361 * local DNS, we just have to trust it (all bets are off if the local
362 * DNS is perverted). We always check the address list, though.
365 if (strcasecmp(remotehost, hp->h_name)
366 && strcasecmp(remotehost, "localhost")) {
367 DEBUG(0,("host name/name mismatch: %s != %s",
368 remotehost, hp->h_name));
372 /* Look up the host address in the address list we just got. */
373 for (i = 0; hp->h_addr_list[i]; i++) {
374 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
379 * The host name does not map to the original host address. Perhaps
380 * someone has compromised a name server. More likely someone botched
381 * it, but that could be dangerous, too.
384 DEBUG(0,("host name/address mismatch: %s != %s",
385 inet_ntoa(addr), hp->h_name));