walters / rpms / nfs-utils

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