Skip to contentSkip to conversation

mountd: clean up hostname handling when authenticating clients

PATCH In review Jeff Layton 1 participant 2 messages Oct 25, 2006 – Nov 1, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 mountd: clean up hostname handling when authenticating clients

The current scheme for authenticating clients is what causes us to end up with rmtab entries that look like this:

*:/foo
*:/scratch
*,foo.bar.baz:/scratch

When a host is authenticated, we build a comma-separated list of hostnames (via client_compose) and stuff that into the m_hostname field of a static nfs_client struct. We then see if any of those hostnames match any of the existing m_hostnames in the nfs_clients (via repeated calls to client_member). Unfortunately, this means that when we later go to do things like add the host to the kernel caches or the rmtab, it's using a hostname like "*,foo.bar.baz".

This patch simplifies this scheme by getting rid of client_compose, client_member, and a couple of helper functions only used by them. It then adds a new client_resolve function that just turns an address into a hostent. Instead of using client_member, we just make repeated calls to client_check. This means we can now use the m_hostname field for this struct as originally intended, to hold the hostname of the client.

support/export/client.c renamed from support/export/client.c.hname +4 -90
@@ -234,102 +234,16 @@ client_find(struct hostent *hp)
 	return NULL;
 }
 
-/*
- * Find client name given an IP address
- * This is found by gathering all known names that match that IP address,
- * sorting them and joining them with '+'
- *
- */
-static char *add_name(char *old, char *add);
-
-char *
-client_compose(struct in_addr addr)
+struct hostent *
+client_resolve(struct in_addr addr)
 {
 	struct hostent *he = NULL;
-	char *name = NULL;
-	int i;
 
-	if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
-		he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
+	he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
 	if (he == NULL)
 		he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
 
-	for (i = 0 ; i < MCL_MAXTYPES; i++) {
-		nfs_client	*clp;
-		for (clp = clientlist[i]; clp ; clp = clp->m_next) {
-			if (!client_check(clp, he))
-				continue;
-			name = add_name(name, clp->m_hostname);
-		}
-	}
-	return name;
-}
-
-int
-client_member(char *client, char *name)
-{
-	/* check if "client" (a ',' separated list of names)
-	 * contains 'name' as a member
-	 */
-	int l = strlen(name);
-	while (*client) {
-		if (strncmp(client, name, l) == 0 &&
-		    (client[l] == ',' || client[l] == '\0'))
-			return 1;
-		client = strchr(client, ',');
-		if (client == NULL)
-			return 0;
-		client++;
-	}
-	return 0;
-}
-
-
-int
-name_cmp(char *a, char *b)
-{
-	/* compare strings a and b, but only upto ',' in a */
-	while (*a && *b && *a != ',' && *a == *b)
-		a++, b++;
-	if (!*b && (!*a || !a == ',') )
-		return 0;
-	if (!*b) return 1;
-	if (!*a || *a == ',') return -1;
-	return *a - *b;
-}
-
-static char *
-add_name(char *old, char *add)
-{
-	int len = strlen(add)+2;
-	char *new;
-	char *cp;
-	if (old) len += strlen(old);
-	
-	new = malloc(len);
-	if (!new) {
-		free(old);
-		return NULL;
-	}
-	cp = old;
-	while (cp && *cp && name_cmp(cp, add) < 0) {
-		/* step cp forward over a name */
-		char *e = strchr(cp, ',');
-		if (e)
-			cp = e+1;
-		else
-			cp = cp + strlen(cp);
-	}
-	strncpy(new, old, cp-old);
-	new[cp-old] = 0;
-	if (cp != old && !*cp)
-		strcat(new, ",");
-	strcat(new, add);
-	if (cp && *cp) {
-		strcat(new, ",");
-		strcat(new, cp);
-	}
-	return new;
+	return he;
 }
 
 /*
support/include/exportfs.h renamed from support/include/exportfs.h.hname +1 -2
@@ -55,8 +55,7 @@ int				client_check(nfs_client *, struct
 int				client_match(nfs_client *, char *hname);
 void				client_release(nfs_client *);
 void				client_freeall(void);
-char *				client_compose(struct in_addr addr);
-int 				client_member(char *client, char *name);
+struct hostent *		client_resolve(struct in_addr addr);
 
 int				export_read(char *fname);
 void				export_add(nfs_export *);
utils/mountd/cache.c renamed from utils/mountd/cache.c.hname +11 -9
@@ -53,8 +53,8 @@ void auth_unix_ip(FILE *f)
 	char *cp;
 	char class[20];
 	char ipaddr[20];
-	char *client;
 	struct in_addr addr;
+	struct hostent *he;
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
 		return;
 
@@ -73,18 +73,16 @@ void auth_unix_ip(FILE *f)
 	auth_reload();
 
 	/* addr is a valid, interesting address, find the domain name... */
-	client = client_compose(addr);
-
+	he = client_resolve(addr);
 	
 	qword_print(f, "nfsd");
 	qword_print(f, ipaddr);
 	qword_printint(f, time(0)+30*60);
-	if (client)
-		qword_print(f, *client?client:"DEFAULT");
 	qword_eol(f);
+	if (he)
+		qword_print(f, *he->h_name?he->h_name:"DEFAULT");
 
-	if (client) free(client);
-	
+	if (he) free(he);
 }
 
 void nfsd_fh(FILE *f)
@@ -106,6 +104,7 @@ void nfsd_fh(FILE *f)
 	nfs_export *exp;
 	int i;
 	int dev_missing = 0;
+	struct hostent *hp = NULL;
 
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
 		return;
@@ -167,11 +166,12 @@ void nfsd_fh(FILE *f)
 	auth_reload();
 
 	/* Now determine export point for this fsid/domain */
+	hp = gethostbyname(dom);
 	for (i=0 ; i < MCL_MAXTYPES; i++) {
 		for (exp = exportlist[i]; exp; exp = exp->m_next) {
 			struct stat stb;
 
-			if (!client_member(dom, exp->m_client->m_hostname))
+			if (!client_check(exp->m_client, hp))
 				continue;
 			if (exp->m_export.e_mountpoint &&
 			    !is_mountpoint(exp->m_export.e_mountpoint[0]?
@@ -248,6 +248,7 @@ void nfsd_export(FILE *f)
 	int i;
 	char *dom, *path;
 	nfs_export *exp, *found = NULL;
+	struct hostent *hp;
 
 
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
@@ -268,9 +269,10 @@ void nfsd_export(FILE *f)
 	auth_reload();
 
 	/* now find flags for this export point in this domain */
+	hp = gethostbyname(dom);
 	for (i=0 ; i < MCL_MAXTYPES; i++) {
 		for (exp = exportlist[i]; exp; exp = exp->m_next) {
-			if (!client_member(dom, exp->m_client->m_hostname))
+			if (!client_check(exp->m_client, hp))
 				continue;
 			if (strcmp(path, exp->m_export.e_path))
 				continue;
utils/mountd/auth.c renamed from utils/mountd/auth.c.hname +3 -8
@@ -79,16 +79,11 @@ auth_authenticate_internal(char *what, s
 		if (my_client.m_naddr != 1 ||
 		    my_client.m_addrlist[0].s_addr != caller->sin_addr.s_addr) {
 			/* different client to last time, so do a lookup */
-			char *n;
 			my_client.m_naddr = 0;
 			my_client.m_addrlist[0] = caller->sin_addr;
-			n = client_compose(caller->sin_addr);
-			*error = unknown_host;
-			if (!n)
-				return NULL;
-			strcpy(my_client.m_hostname, *n?n:"DEFAULT");
-			free(n);
 			my_client.m_naddr = 1;
+			strncpy(my_client.m_hostname, hp->h_name,
+					sizeof(my_client.m_hostname));
 		}
 
 		my_exp.m_client = &my_client;
@@ -96,7 +91,7 @@ auth_authenticate_internal(char *what, s
 		exp = NULL;
 		for (i = 0; !exp && i < MCL_MAXTYPES; i++) 
 			for (exp = exportlist[i]; exp; exp = exp->m_next) {
-				if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
+				if (!client_check(exp->m_client, hp))
 					continue;
 				if (strcmp(path, exp->m_export.e_path))
 					continue;

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs

#1 lore raw
Quoted 15 lines · Jeff Layton

The current scheme for authenticating clients is what causes us to end up with rmtab entries that look like this:

*:/foo
*:/scratch
*,foo.bar.baz:/scratch

When a host is authenticated, we build a comma-separated list of hostnames (via client_compose) and stuff that into the m_hostname field of a static nfs_client struct. We then see if any of those hostnames match any of the existing m_hostnames in the nfs_clients (via repeated calls to client_member). Unfortunately, this means that when we later go to do things like add the host to the kernel caches or the rmtab, it's using a hostname like "*,foo.bar.baz".

Quoted 6 lines

This patch simplifies this scheme by getting rid of client_compose, client_member, and a couple of helper functions only used by them. It then adds a new client_resolve function that just turns an address into a hostent. Instead of using client_member, we just make repeated calls to client_check. This means we can now use the m_hostname field for this struct as originally intended, to hold the hostname of the client.

Below is a respun version of the above patch. Since this patch eliminates client_compose, populating the my_client struct in auth_authenticate_internal is essentially zero cost. We might as well make sure that it always has up to date info. This also simplifies that code a bit too.

Any more thoughts on whether this is an acceptable approach, or whether there are problems with it that I haven't considered?

support/export/client.c renamed from support/export/client.c.mhost +4 -90
@@ -234,102 +234,16 @@ client_find(struct hostent *hp)
 	return NULL;
 }
 
-/*
- * Find client name given an IP address
- * This is found by gathering all known names that match that IP address,
- * sorting them and joining them with '+'
- *
- */
-static char *add_name(char *old, char *add);
-
-char *
-client_compose(struct in_addr addr)
+struct hostent *
+client_resolve(struct in_addr addr)
 {
 	struct hostent *he = NULL;
-	char *name = NULL;
-	int i;
 
-	if (clientlist[MCL_WILDCARD] || clientlist[MCL_NETGROUP])
-		he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
+	he = get_reliable_hostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
 	if (he == NULL)
 		he = get_hostent((const char*)&addr, sizeof(addr), AF_INET);
 
-	for (i = 0 ; i < MCL_MAXTYPES; i++) {
-		nfs_client	*clp;
-		for (clp = clientlist[i]; clp ; clp = clp->m_next) {
-			if (!client_check(clp, he))
-				continue;
-			name = add_name(name, clp->m_hostname);
-		}
-	}
-	return name;
-}
-
-int
-client_member(char *client, char *name)
-{
-	/* check if "client" (a ',' separated list of names)
-	 * contains 'name' as a member
-	 */
-	int l = strlen(name);
-	while (*client) {
-		if (strncmp(client, name, l) == 0 &&
-		    (client[l] == ',' || client[l] == '\0'))
-			return 1;
-		client = strchr(client, ',');
-		if (client == NULL)
-			return 0;
-		client++;
-	}
-	return 0;
-}
-
-
-int
-name_cmp(char *a, char *b)
-{
-	/* compare strings a and b, but only upto ',' in a */
-	while (*a && *b && *a != ',' && *a == *b)
-		a++, b++;
-	if (!*b && (!*a || !a == ',') )
-		return 0;
-	if (!*b) return 1;
-	if (!*a || *a == ',') return -1;
-	return *a - *b;
-}
-
-static char *
-add_name(char *old, char *add)
-{
-	int len = strlen(add)+2;
-	char *new;
-	char *cp;
-	if (old) len += strlen(old);
-	
-	new = malloc(len);
-	if (!new) {
-		free(old);
-		return NULL;
-	}
-	cp = old;
-	while (cp && *cp && name_cmp(cp, add) < 0) {
-		/* step cp forward over a name */
-		char *e = strchr(cp, ',');
-		if (e)
-			cp = e+1;
-		else
-			cp = cp + strlen(cp);
-	}
-	strncpy(new, old, cp-old);
-	new[cp-old] = 0;
-	if (cp != old && !*cp)
-		strcat(new, ",");
-	strcat(new, add);
-	if (cp && *cp) {
-		strcat(new, ",");
-		strcat(new, cp);
-	}
-	return new;
+	return he;
 }
 
 /*
support/include/exportfs.h renamed from support/include/exportfs.h.mhost +1 -2
@@ -55,8 +55,7 @@ int				client_check(nfs_client *, struct
 int				client_match(nfs_client *, char *hname);
 void				client_release(nfs_client *);
 void				client_freeall(void);
-char *				client_compose(struct in_addr addr);
-int 				client_member(char *client, char *name);
+struct hostent *		client_resolve(struct in_addr addr);
 
 int				export_read(char *fname);
 void				export_add(nfs_export *);
utils/mountd/cache.c renamed from utils/mountd/cache.c.mhost +11 -9
@@ -53,8 +53,8 @@ void auth_unix_ip(FILE *f)
 	char *cp;
 	char class[20];
 	char ipaddr[20];
-	char *client;
 	struct in_addr addr;
+	struct hostent *he;
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
 		return;
 
@@ -73,18 +73,16 @@ void auth_unix_ip(FILE *f)
 	auth_reload();
 
 	/* addr is a valid, interesting address, find the domain name... */
-	client = client_compose(addr);
-
+	he = client_resolve(addr);
 	
 	qword_print(f, "nfsd");
 	qword_print(f, ipaddr);
 	qword_printint(f, time(0)+30*60);
-	if (client)
-		qword_print(f, *client?client:"DEFAULT");
 	qword_eol(f);
+	if (he)
+		qword_print(f, *he->h_name?he->h_name:"DEFAULT");
 
-	if (client) free(client);
-	
+	if (he) free(he);
 }
 
 void nfsd_fh(FILE *f)
@@ -106,6 +104,7 @@ void nfsd_fh(FILE *f)
 	nfs_export *exp;
 	int i;
 	int dev_missing = 0;
+	struct hostent *hp = NULL;
 
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
 		return;
@@ -167,11 +166,12 @@ void nfsd_fh(FILE *f)
 	auth_reload();
 
 	/* Now determine export point for this fsid/domain */
+	hp = gethostbyname(dom);
 	for (i=0 ; i < MCL_MAXTYPES; i++) {
 		for (exp = exportlist[i]; exp; exp = exp->m_next) {
 			struct stat stb;
 
-			if (!client_member(dom, exp->m_client->m_hostname))
+			if (!client_check(exp->m_client, hp))
 				continue;
 			if (exp->m_export.e_mountpoint &&
 			    !is_mountpoint(exp->m_export.e_mountpoint[0]?
@@ -248,6 +248,7 @@ void nfsd_export(FILE *f)
 	int i;
 	char *dom, *path;
 	nfs_export *exp, *found = NULL;
+	struct hostent *hp;
 
 
 	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
@@ -268,9 +269,10 @@ void nfsd_export(FILE *f)
 	auth_reload();
 
 	/* now find flags for this export point in this domain */
+	hp = gethostbyname(dom);
 	for (i=0 ; i < MCL_MAXTYPES; i++) {
 		for (exp = exportlist[i]; exp; exp = exp->m_next) {
-			if (!client_member(dom, exp->m_client->m_hostname))
+			if (!client_check(exp->m_client, hp))
 				continue;
 			if (strcmp(path, exp->m_export.e_path))
 				continue;
utils/mountd/auth.c renamed from utils/mountd/auth.c.mhost +6 -16
@@ -75,28 +75,18 @@ auth_authenticate_internal(char *what, s
 
 	if (new_cache) {
 		int i;
-		/* return static nfs_export with details filled in */
-		if (my_client.m_naddr != 1 ||
-		    my_client.m_addrlist[0].s_addr != caller->sin_addr.s_addr) {
-			/* different client to last time, so do a lookup */
-			char *n;
-			my_client.m_naddr = 0;
-			my_client.m_addrlist[0] = caller->sin_addr;
-			n = client_compose(caller->sin_addr);
-			*error = unknown_host;
-			if (!n)
-				return NULL;
-			strcpy(my_client.m_hostname, *n?n:"DEFAULT");
-			free(n);
-			my_client.m_naddr = 1;
-		}
 
+		/* return static nfs_export with details filled in */
+		my_client.m_addrlist[0] = caller->sin_addr;
+		my_client.m_naddr = 1;
+		strncpy(my_client.m_hostname, hp->h_name,
+			sizeof(my_client.m_hostname));
 		my_exp.m_client = &my_client;
 
 		exp = NULL;
 		for (i = 0; !exp && i < MCL_MAXTYPES; i++) 
 			for (exp = exportlist[i]; exp; exp = exp->m_next) {
-				if (!client_member(my_client.m_hostname, exp->m_client->m_hostname))
+				if (!client_check(exp->m_client, hp))
 					continue;
 				if (strcmp(path, exp->m_export.e_path))
 					continue;

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs