diff --git a/CHANGELOG b/CHANGELOG
index 5bbf9a1..b435501 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -6,6 +6,7 @@
 - fix another expire regression introduced in the "mitigate manual umount" patch.
 - correct check for busy offset mounts before offset umount.
 - make double quote handing consistent.
+- fix handling of trailing white space in wildcard lookup.
 
 4/1/2007 autofs-5.0.1 rc3
 -------------------------
diff --git a/lib/parse_subs.c b/lib/parse_subs.c
index 81db459..bdb19cd 100644
--- a/lib/parse_subs.c
+++ b/lib/parse_subs.c
@@ -157,10 +157,22 @@ char *dequote(const char *str, int origlen, unsigned int logopt)
 	const char *scp;
 	int len = origlen;
 	int quote = 0, dquote = 0;
+	int i, j;
 
 	if (ret == NULL)
 		return NULL;
 
+	/* first thing to do is strip white space from the end */
+	i = len - 1;
+	while (isspace(str[i])) {
+		/* of course, we have to keep escaped white-space */
+		j = i - 1;
+		if (j > 0 && (str[j] == '\\' || str[j] == '"'))
+			break;
+		i--;
+		len--;
+	}
+
 	for (scp = str; len > 0 && *scp; scp++, len--) {
 		if (!quote) {
 			if (*scp == '"') {
diff --git a/modules/parse_sun.c b/modules/parse_sun.c
index 0a3cac9..7020902 100644
--- a/modules/parse_sun.c
+++ b/modules/parse_sun.c
@@ -135,9 +135,32 @@ int expandsunent(const char *src, char *dst, const char *key,
 		switch (ch) {
 		case '&':
 			l = strlen(key);
-			if (dst) {
-				strcpy(dst, key);
-				dst += l;
+			/*
+			 * In order to ensure that any spaces in the key
+			 * re preserved, we need to escape them here.
+			 */
+			if (strchr(key, ' ')) {
+				char *keyp = key;
+				while (*keyp) {
+					if (isspace(*keyp)) {
+						if (dst) {
+							*dst++ = '\\';
+							*dst++ = *keyp++;
+						} else
+							keyp++;
+						l++;
+					} else {
+						if (dst)
+							*dst++ = *keyp++;
+						else
+							keyp++;
+					}
+				}
+			} else {
+				if (dst) {
+					strcpy(dst, key);
+					dst += l;
+				}
 			}
 			len += l;
 			break;