/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This module implements a simple access control language that is based on
* host (or domain) names, NIS (host) netgroup names, IP addresses (or
* network numbers) and daemon process names. When a match is found the
* search is terminated, and depending on whether PROCESS_OPTIONS is defined,
* a list of options is executed or an optional shell command is executed.
*
* Host and user names are looked up on demand, provided that suitable endpoint
* information is available as sockaddr_in structures or TLI netbufs. As a
* side effect, the pattern matching process may change the contents of
* request structure fields.
*
* Diagnostics are reported through syslog(3).
*
* Compile with -DNETGROUP if your library provides support for netgroups.
*
* Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
*/
#ifndef lint
#endif
/* System libraries. */
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <ctype.h>
#include <errno.h>
#include <setjmp.h>
#include <string.h>
extern char *fgets();
extern int errno;
#ifndef INADDR_NONE
#endif
/* Local stuff. */
#include "tcpd.h"
/* Error handling. */
/* Delimiters for lists of daemons or clients. */
/* Constants to be used in assignments only, not in comparisons... */
#define NO 0
/*
* These variables are globally visible so that they can be redirected in
* verification mode.
*/
int hosts_access_verbose = 0;
/*
* In a long-running process, we are not at liberty to just go away.
*/
/* Forward declarations. */
static int table_match();
static int list_match();
static int server_match();
static int client_match();
static int host_match();
static int string_match();
static int masked_match();
#ifdef HAVE_IPV6
static void ipv6_mask();
#endif
/* Size of logical line buffer. */
/* hosts_access - host access control facility */
struct request_info *request;
{
int verdict;
/*
* If the (daemon, client) pair is matched by an entry in the file
* /etc/hosts.allow, access is granted. Otherwise, if the (daemon,
* client) pair is matched by an entry in the file /etc/hosts.deny,
* access is denied. Otherwise, access is granted. A non-existent
* access-control file is treated as an empty file.
*
* After a rule has been matched, the optional language extensions may
* decide to grant or refuse service anyway. Or, while a rule is being
* processed, a serious error is found, and it seems better to play safe
* and deny service. All this is done by jumping back into the
* hosts_access() routine, bypassing the regular return from the
* table_match() function calls below.
*/
if (resident <= 0)
resident++;
if (verdict != 0)
return (YES);
return (NO);
return (YES);
}
/* table_match - match table entries with (daemon, client) pair */
char *table;
struct request_info *request;
{
/*
* Between the fopen() and fclose() calls, avoid jumps that may cause
* file descriptor leaks.
*/
tcpd_context.line = 0;
tcpd_warn("missing newline or line too long");
continue;
}
continue;
tcpd_warn("missing \":\" separator");
continue;
}
}
}
if (match) {
if (hosts_access_verbose > 1)
if (sh_cmd) {
#ifdef PROCESS_OPTIONS
#else
#endif
}
}
return (match);
}
/* list_match - match a request against a list of patterns with exceptions */
char *list;
struct request_info *request;
int (*match_fn) ();
{
char *tok;
/*
* Process tokens one at a time. We have exhausted all possible matches
* when we reach an "EXCEPT" token or the end of the list. If we do find
* a match, look for an "EXCEPT" list and recurse to determine whether
* the match is affected by any exceptions.
*/
return (NO);
/* VOID */ ;
}
}
return (NO);
}
/* server_match - match server information */
char *tok;
struct request_info *request;
{
char *host;
} else { /* daemon@host */
}
}
/* client_match - match client information */
char *tok;
struct request_info *request;
{
char *host;
} else { /* user@host */
}
}
char *tok;
{
char *mask;
/*
* This code looks a little hairy because we want to avoid unnecessary
* hostname lookups.
*
* The KNOWN pattern requires that both address AND name be known; some
* patterns are specific to host names or to host addresses; all other
* patterns are satisfied when either the address OR the name match.
*/
#ifdef NETGROUP
static char *mydomain = 0;
if (mydomain == 0)
#else
return (NO);
#endif
#ifdef HAVE_IPV6
char *cbr;
char *slash;
/*
* In some cases we don't get the sockaddr, only the addr.
* We use inet_pton to convert it to its binary representation
* and match against that.
*/
return (NO);
}
} else {
return (NO);
}
*cbr = '\0';
/*
* A /nnn prefix specifies how many bits of the address we
* need to check.
*/
*slash = '\0';
tcpd_warn("bad IP6 prefix specification");
return (NO);
}
/* Copy, because we need to modify it below */
}
}
tcpd_warn("bad IP6 address specification");
return (NO);
}
/*
* Zero the bits we're not interested in in both addresses
* then compare. Note that we take a copy of the host info
* in that case.
*/
if (mask != IPV6_ABITS) {
}
return (YES);
return (NO);
#endif
} else { /* anything else */
}
}
/* string_match - match string against pattern */
char *tok;
char *string;
{
int n;
return (YES);
} else { /* exact match */
}
}
char *net_tok;
char *mask_tok;
char *string;
{
unsigned long net;
unsigned long mask;
unsigned long addr;
/*
* Disallow forms other than dotted quad: the treatment that inet_addr()
* gives to forms with less than four components is inconsistent with the
* access control language. John P. Rouillard <rouilj@cs.umb.edu>.
*/
return (NO);
return (NO); /* not tcpd_jump() */
}
}
#ifdef HAVE_IPV6
/*
* Function that zeros all but the first "maskbits" bits of the IPV6 address
* This function can be made generic by specifying an address length as
* extra parameter. (So Wietse can implement 1.2.3.4/16)
*/
int maskbits;
{
return;
p += maskbits / 8;
maskbits %= 8;
if (maskbits != 0)
*p++ = 0;
}
#endif