walters / rpms / nfs-utils

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