diff --git a/auth.c b/auth.c
index cd95da9319d8d766a1681b991e7dbc9caa8d18ce..ddb4248631dd9eaad5b5cdbcaa8813705b31d37e 100644
--- a/auth.c
+++ b/auth.c
@@ -682,7 +682,9 @@ fakepw(void)
 	fake.pw_name = "NOUSER";
 	fake.pw_passwd =
 	    "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
+#ifdef HAVE_PW_GECOS_IN_PASSWD
 	fake.pw_gecos = "NOUSER";
+#endif
 	fake.pw_uid = privsep_pw == NULL ? (uid_t)-1 : privsep_pw->pw_uid;
 	fake.pw_gid = privsep_pw == NULL ? (gid_t)-1 : privsep_pw->pw_gid;
 #ifdef HAVE_PW_CLASS_IN_PASSWD
diff --git a/config.h.in b/config.h.in
index baf0011a74b7c79e91eb54441423a56f3c86b2bf..eccd37f18c24fc4e2b03d91fcf63ea0e203159cd 100644
--- a/config.h.in
+++ b/config.h.in
@@ -732,6 +732,9 @@
 /* Define if your password has a pw_change field */
 #undef HAVE_PW_CHANGE_IN_PASSWD
 
+/* Define if your password has a pw_gecos field */
+#undef HAVE_PW_GECOS_IN_PASSWD
+
 /* Define if your password has a pw_class field */
 #undef HAVE_PW_CLASS_IN_PASSWD
 
diff --git a/configure b/configure
index 43c4372630dad751318dffdbd80b2642638ca780..902d850fb9d33bf9389d6428e6d3047cc27f4f02 100755
--- a/configure
+++ b/configure
@@ -13620,6 +13620,40 @@ $as_echo "#define HAVE_PW_CHANGE_IN_PASSWD 1" >>confdefs.h
 
 fi
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for pw_gecos field in struct passwd" >&5
+$as_echo_n "checking for pw_gecos field in struct passwd... " >&6; }
+if ${ac_cv_have_pw_gecos_in_struct_passwd+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+
+	cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+ #include <pwd.h>
+int
+main ()
+{
+ struct passwd p; p.pw_gecos = 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+   ac_cv_have_pw_gecos_in_struct_passwd="yes"
+else
+   ac_cv_have_pw_gecos_in_struct_passwd="no"
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_have_pw_gecos_in_struct_passwd" >&5
+$as_echo "$ac_cv_have_pw_gecos_in_struct_passwd" >&6; }
+if test "x$ac_cv_have_pw_gecos_in_struct_passwd" = "xyes" ; then
+
+$as_echo "#define HAVE_PW_GECOS_IN_PASSWD 1" >>confdefs.h
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for msg_accrights field in struct msghdr" >&5
 $as_echo_n "checking for msg_accrights field in struct msghdr... " >&6; }
 if ${ac_cv_have_accrights_in_msghdr+:} false; then :
diff --git a/configure.ac b/configure.ac
index 7a915272d26f0d464ee40c7a610e41e1de957180..bf540b14d73d7235506371b2bf1e923e79ff20d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3100,6 +3100,19 @@ if test "x$ac_cv_have_pw_change_in_struct_passwd" = "xyes" ; then
 		[Define if your password has a pw_change field])
 fi
 
+AC_CACHE_CHECK([for pw_gecos field in struct passwd],
+		ac_cv_have_pw_gecos_in_struct_passwd, [
+	AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ #include <pwd.h> ]],
+	[[ struct passwd p; p.pw_gecos = 0; ]])],
+	[ ac_cv_have_pw_gecos_in_struct_passwd="yes" ],
+	[ ac_cv_have_pw_gecos_in_struct_passwd="no" 
+	])
+])
+if test "x$ac_cv_have_pw_gecos_in_struct_passwd" = "xyes" ; then
+	AC_DEFINE([HAVE_PW_GECOS_IN_PASSWD], [1],
+		[Define if your password has a pw_gecos field])
+fi
+
 dnl make sure we're using the real structure members and not defines
 AC_CACHE_CHECK([for msg_accrights field in struct msghdr],
 		ac_cv_have_accrights_in_msghdr, [
diff --git a/misc.c b/misc.c
index 5f63090d981823d66d1143ac8822411eb7f6964f..b2b3bae07c3c4e8a69d9275e902f2fafb5d75999 100644
--- a/misc.c
+++ b/misc.c
@@ -205,8 +205,10 @@ pwcopy(struct passwd *pw)
 	struct passwd *copy = xcalloc(1, sizeof(*copy));
 
 	copy->pw_name = xstrdup(pw->pw_name);
-	copy->pw_passwd = xstrdup(pw->pw_passwd);
+	copy->pw_passwd = pw->pw_passwd ? xstrdup(pw->pw_passwd) : NULL;
+#ifdef HAVE_PW_GECOS_IN_PASSWD
 	copy->pw_gecos = xstrdup(pw->pw_gecos);
+#endif
 	copy->pw_uid = pw->pw_uid;
 	copy->pw_gid = pw->pw_gid;
 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
diff --git a/monitor.c b/monitor.c
index a166fed2eec786b0cf4d9c579f2aebf281619894..f865057d5ab074dfbcf2fc03e0f3d77ae9580ee0 100644
--- a/monitor.c
+++ b/monitor.c
@@ -761,7 +761,9 @@ mm_answer_pwnamallow(int sock, Buffer *m)
 	buffer_put_string(m, pwent, sizeof(struct passwd));
 	buffer_put_cstring(m, pwent->pw_name);
 	buffer_put_cstring(m, "*");
+#ifdef HAVE_PW_GECOS_IN_PASSWD
 	buffer_put_cstring(m, pwent->pw_gecos);
+#endif
 #ifdef HAVE_PW_CLASS_IN_PASSWD
 	buffer_put_cstring(m, pwent->pw_class);
 #endif
diff --git a/monitor_wrap.c b/monitor_wrap.c
index 1f60658e9839445ab9ebe97b486a9a66239c4d6a..3c3856ad2b735e91b0cd1d69247d4062af3f59ec 100644
--- a/monitor_wrap.c
+++ b/monitor_wrap.c
@@ -259,7 +259,9 @@ mm_getpwnamallow(const char *username)
 		fatal("%s: struct passwd size mismatch", __func__);
 	pw->pw_name = buffer_get_string(&m, NULL);
 	pw->pw_passwd = buffer_get_string(&m, NULL);
+#ifdef HAVE_PW_GECOS_IN_PASSWD
 	pw->pw_gecos = buffer_get_string(&m, NULL);
+#endif
 #ifdef HAVE_PW_CLASS_IN_PASSWD
 	pw->pw_class = buffer_get_string(&m, NULL);
 #endif