walters / rpms / nfs-utils

Forked from rpms/nfs-utils 6 years ago
Clone
36e2ddf
commit 71f9f61517bf301f723b79651d53590ef97c3556
36e2ddf
Author: Steve Dickson <steved@redhat.com>
36e2ddf
Date:   Fri Dec 19 14:20:14 2008 -0500
36e2ddf
36e2ddf
    To ensure the hash table of clients has valid
36e2ddf
    access rights, check the modification times on
36e2ddf
    both access files. If one of them have change,
36e2ddf
    update the hash entry instead of creating a
36e2ddf
    new entry.
36e2ddf
    
36e2ddf
    Signed-off-by: Steve Dickson <steved@redhat.com>
36e2ddf
36e2ddf
commit 58e0a308fec476361dd21f7d3856faceb6e308ee
36e2ddf
Author: Steve Dickson <steved@redhat.com>
36e2ddf
Date:   Fri Dec 19 14:11:09 2008 -0500
36e2ddf
36e2ddf
    Clients IP address and host names are check on
36e2ddf
    every RPC request, to both mountd and statd
36e2ddf
    when TCP wrappers are enabled. To help this
36e2ddf
    process scale better the access rights are stored
36e2ddf
    in a hash table, which are hashed per IP address,
36e2ddf
    RPC program and procudure numbers.
36e2ddf
    
36e2ddf
    Signed-off-by: Steve Dickson <steved@redhat.com>
36e2ddf
36e2ddf
commit e47da19d63ea50a4e15f6ab491535d54097744de
36e2ddf
Author: Steve Dickson <steved@redhat.com>
36e2ddf
Date:   Fri Dec 19 14:09:59 2008 -0500
36e2ddf
36e2ddf
    When clients are define as IP addresses in /etc/hosts.deny,
36e2ddf
    access is allow due to misinterpreting the return value of
36e2ddf
    hosts_ctl(). This patch reworks that logic which closes
36e2ddf
    that hole.
36e2ddf
    
36e2ddf
    Signed-off-by: Steve Dickson <steved@redhat.com>
36e2ddf
36e2ddf
diff -up nfs-utils-1.1.4/support/misc/tcpwrapper.c.save nfs-utils-1.1.4/support/misc/tcpwrapper.c
36e2ddf
--- nfs-utils-1.1.4/support/misc/tcpwrapper.c.save	2008-12-19 15:44:05.000000000 -0500
36e2ddf
+++ nfs-utils-1.1.4/support/misc/tcpwrapper.c	2008-12-19 15:46:15.000000000 -0500
36e2ddf
@@ -44,6 +44,10 @@
36e2ddf
 #include <pwd.h>
36e2ddf
 #include <sys/types.h>
36e2ddf
 #include <sys/signal.h>
36e2ddf
+#include <sys/queue.h>
36e2ddf
+#include <sys/stat.h>
36e2ddf
+#include <unistd.h>
36e2ddf
+
36e2ddf
 #ifdef SYSV40
36e2ddf
 #include <netinet/in.h>
36e2ddf
 #include <rpc/rpcent.h>
36e2ddf
@@ -86,6 +90,79 @@ int hosts_ctl(char *daemon, char *name, 
36e2ddf
 #define log_client(addr, proc, prog) \
36e2ddf
   logit(allow_severity, addr, proc, prog, "")
36e2ddf
 
36e2ddf
+#define ALLOW 1
36e2ddf
+#define DENY 0
36e2ddf
+
36e2ddf
+typedef struct _haccess_t {
36e2ddf
+	TAILQ_ENTRY(_haccess_t) list;
36e2ddf
+	int access;
36e2ddf
+    struct in_addr addr;
36e2ddf
+} haccess_t;
36e2ddf
+
36e2ddf
+#define HASH_TABLE_SIZE 1021
36e2ddf
+typedef struct _hash_head {
36e2ddf
+	TAILQ_HEAD(host_list, _haccess_t) h_head;
36e2ddf
+} hash_head;
36e2ddf
+hash_head haccess_tbl[HASH_TABLE_SIZE];
36e2ddf
+static haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long, u_long);
36e2ddf
+static void haccess_add(struct sockaddr_in *addr, u_long, u_long, int);
36e2ddf
+
36e2ddf
+inline unsigned int strtoint(char *str)
36e2ddf
+{
36e2ddf
+	unsigned int n = 0;
36e2ddf
+	int len = strlen(str);
36e2ddf
+	int i;
36e2ddf
+
36e2ddf
+	for (i=0; i < len; i++)
36e2ddf
+		n+=((int)str[i])*i;
36e2ddf
+
36e2ddf
+	return n;
36e2ddf
+}
36e2ddf
+inline int hashint(unsigned int num)
36e2ddf
+{
36e2ddf
+	return num % HASH_TABLE_SIZE;
36e2ddf
+}
36e2ddf
+#define HASH(_addr, _proc, _prog) \
36e2ddf
+	hashint((strtoint((_addr))+(_proc)+(_prog)))
36e2ddf
+
36e2ddf
+void haccess_add(struct sockaddr_in *addr, u_long proc, 
36e2ddf
+	u_long prog, int access)
36e2ddf
+{
36e2ddf
+	hash_head *head;
36e2ddf
+ 	haccess_t *hptr;
36e2ddf
+	int hash;
36e2ddf
+
36e2ddf
+	hptr = (haccess_t *)malloc(sizeof(haccess_t));
36e2ddf
+	if (hptr == NULL)
36e2ddf
+		return;
36e2ddf
+
36e2ddf
+	hash = HASH(inet_ntoa(addr->sin_addr), proc, prog);
36e2ddf
+	head = &(haccess_tbl[hash]);
36e2ddf
+
36e2ddf
+	hptr->access = access;
36e2ddf
+	hptr->addr.s_addr = addr->sin_addr.s_addr;
36e2ddf
+
36e2ddf
+	if (TAILQ_EMPTY(&head->h_head))
36e2ddf
+		TAILQ_INSERT_HEAD(&head->h_head, hptr, list);
36e2ddf
+	else
36e2ddf
+		TAILQ_INSERT_TAIL(&head->h_head, hptr, list);
36e2ddf
+}
36e2ddf
+haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long proc, u_long prog)
36e2ddf
+{
36e2ddf
+	hash_head *head;
36e2ddf
+ 	haccess_t *hptr;
36e2ddf
+	int hash;
36e2ddf
+
36e2ddf
+	hash = HASH(inet_ntoa(addr->sin_addr), proc, prog);
36e2ddf
+	head = &(haccess_tbl[hash]);
36e2ddf
+
36e2ddf
+	TAILQ_FOREACH(hptr, &head->h_head, list) {
36e2ddf
+		if (hptr->addr.s_addr == addr->sin_addr.s_addr)
36e2ddf
+			return hptr;
36e2ddf
+	}
36e2ddf
+	return NULL;
36e2ddf
+}
36e2ddf
+
36e2ddf
 int
36e2ddf
 good_client(daemon, addr)
36e2ddf
 char *daemon;
36e2ddf
@@ -95,47 +172,44 @@ struct sockaddr_in *addr;
36e2ddf
     char **sp;
36e2ddf
     char *tmpname;
36e2ddf
 
36e2ddf
-    /* Check the IP address first. */
36e2ddf
-    if (hosts_ctl(daemon, "", inet_ntoa(addr->sin_addr), ""))
36e2ddf
-	return 1;
36e2ddf
-
36e2ddf
-    /* Check the hostname. */
36e2ddf
-    hp = gethostbyaddr ((const char *) &(addr->sin_addr),
36e2ddf
-			sizeof (addr->sin_addr), AF_INET);
36e2ddf
+	/* First check the address. */
36e2ddf
+	if (hosts_ctl(daemon, "", inet_ntoa(addr->sin_addr), "") == DENY)
36e2ddf
+		return DENY;
36e2ddf
+
36e2ddf
+	/* Now do the hostname lookup */
36e2ddf
+	hp = gethostbyaddr ((const char *) &(addr->sin_addr),
36e2ddf
+		sizeof (addr->sin_addr), AF_INET);
36e2ddf
+	if (!hp)
36e2ddf
+		return DENY; /* never heard of it. misconfigured DNS? */
36e2ddf
+
36e2ddf
+	/* Make sure the hostent is authorative. */
36e2ddf
+	tmpname = strdup(hp->h_name);
36e2ddf
+	if (!tmpname)
36e2ddf
+		return DENY;
36e2ddf
+	hp = gethostbyname(tmpname);
36e2ddf
+	free(tmpname);
36e2ddf
+	if (!hp)
36e2ddf
+		return DENY; /* never heard of it. misconfigured DNS? */
36e2ddf
 
36e2ddf
-    if (!hp)
36e2ddf
-	return 0;
36e2ddf
-
36e2ddf
-    /* must make sure the hostent is authorative. */
36e2ddf
-    tmpname = alloca (strlen (hp->h_name) + 1);
36e2ddf
-    strcpy (tmpname, hp->h_name);
36e2ddf
-    hp = gethostbyname(tmpname);
36e2ddf
-    if (hp) {
36e2ddf
-	/* now make sure the "addr->sin_addr" is on the list */
36e2ddf
+	/* Now make sure the address is on the list */
36e2ddf
 	for (sp = hp->h_addr_list ; *sp ; sp++) {
36e2ddf
-	    if (memcmp(*sp, &(addr->sin_addr), hp->h_length)==0)
36e2ddf
-		break;
36e2ddf
+	    if (memcmp(*sp, &(addr->sin_addr), hp->h_length) == 0)
36e2ddf
+			break;
36e2ddf
 	}
36e2ddf
 	if (!*sp)
36e2ddf
-	    /* it was a FAKE. */
36e2ddf
-	    return 0;
36e2ddf
-    }
36e2ddf
-    else
36e2ddf
-	   /* never heard of it. misconfigured DNS? */
36e2ddf
-	   return 0;
36e2ddf
-
36e2ddf
-   /* Check the official name first. */
36e2ddf
-   if (hosts_ctl(daemon, hp->h_name, "", ""))
36e2ddf
-	return 1;
36e2ddf
-
36e2ddf
-   /* Check aliases. */
36e2ddf
-   for (sp = hp->h_aliases; *sp ; sp++) {
36e2ddf
-	if (hosts_ctl(daemon, *sp, "", ""))
36e2ddf
-	    return 1;
36e2ddf
-   }
36e2ddf
+	    return DENY; /* it was a FAKE. */
36e2ddf
+
36e2ddf
+	/* Check the official name and address. */
36e2ddf
+	if (hosts_ctl(daemon, hp->h_name, inet_ntoa(addr->sin_addr), "") == DENY)
36e2ddf
+		return DENY;
36e2ddf
+
36e2ddf
+	/* Now check aliases. */
36e2ddf
+	for (sp = hp->h_aliases; *sp ; sp++) {
36e2ddf
+		if (hosts_ctl(daemon, *sp, inet_ntoa(addr->sin_addr), "") == DENY)
36e2ddf
+	    	return DENY;
36e2ddf
+	}
36e2ddf
 
36e2ddf
-   /* No match */
36e2ddf
-   return 0;
36e2ddf
+   return ALLOW;
36e2ddf
 }
36e2ddf
 
36e2ddf
 /* check_startup - additional startup code */
36e2ddf
@@ -175,6 +249,33 @@ void    check_startup(void)
36e2ddf
     (void) signal(SIGINT, toggle_verboselog);
36e2ddf
 }
36e2ddf
 
36e2ddf
+/* check_files - check to see if either access files have changed */
36e2ddf
+
36e2ddf
+int check_files()
36e2ddf
+{
36e2ddf
+	static time_t allow_mtime, deny_mtime;
36e2ddf
+	struct stat astat, dstat;
36e2ddf
+	int changed = 0;
36e2ddf
+
36e2ddf
+	if (stat("/etc/hosts.allow", &astat) < 0)
36e2ddf
+		astat.st_mtime = 0;
36e2ddf
+	if (stat("/etc/hosts.deny", &dstat) < 0)
36e2ddf
+		dstat.st_mtime = 0;
36e2ddf
+
36e2ddf
+	if(!astat.st_mtime || !dstat.st_mtime)
36e2ddf
+		return changed;
36e2ddf
+
36e2ddf
+	if (astat.st_mtime != allow_mtime)
36e2ddf
+		changed = 1;
36e2ddf
+	else if (dstat.st_mtime != deny_mtime)
36e2ddf
+		changed = 1;
36e2ddf
+
36e2ddf
+	allow_mtime = astat.st_mtime;
36e2ddf
+	deny_mtime = dstat.st_mtime;
36e2ddf
+
36e2ddf
+	return changed;
36e2ddf
+}
36e2ddf
+
36e2ddf
 /* check_default - additional checks for NULL, DUMP, GETPORT and unknown */
36e2ddf
 
36e2ddf
 int
36e2ddf
@@ -184,12 +285,28 @@ struct sockaddr_in *addr;
36e2ddf
 u_long  proc;
36e2ddf
 u_long  prog;
36e2ddf
 {
36e2ddf
-    if (!(from_local(addr) || good_client(daemon, addr))) {
36e2ddf
-	log_bad_host(addr, proc, prog);
36e2ddf
-	return (FALSE);
36e2ddf
-    }
36e2ddf
-    if (verboselog)
36e2ddf
-	log_client(addr, proc, prog);
36e2ddf
+	haccess_t *acc = NULL;
36e2ddf
+	int changed = check_files();
36e2ddf
+
36e2ddf
+	acc = haccess_lookup(addr, proc, prog);
36e2ddf
+	if (acc && changed == 0)
36e2ddf
+		return (acc->access);
36e2ddf
+
36e2ddf
+	if (!(from_local(addr) || good_client(daemon, addr))) {
36e2ddf
+		log_bad_host(addr, proc, prog);
36e2ddf
+		if (acc)
36e2ddf
+			acc->access = FALSE;
36e2ddf
+		else 
36e2ddf
+			haccess_add(addr, proc, prog, FALSE);
36e2ddf
+		return (FALSE);
36e2ddf
+	}
36e2ddf
+	if (verboselog)
36e2ddf
+		log_client(addr, proc, prog);
36e2ddf
+
36e2ddf
+	if (acc)
36e2ddf
+		acc->access = TRUE;
36e2ddf
+	else 
36e2ddf
+		haccess_add(addr, proc, prog, TRUE);
36e2ddf
     return (TRUE);
36e2ddf
 }
36e2ddf