diff --git a/configure b/configure
index 2b3076e616db514230771894b06de7eaca49b9a9..13069d4d3059d740521929ef024dcb924e3f7cd7 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.ac Revision: 293047 .
+# From configure.ac Revision: 294431 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.65 for asterisk trunk.
 #
@@ -12711,6 +12711,27 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+fi
+
+ac_fn_c_check_member "$LINENO" "struct ucred" "uid" "ac_cv_member_struct_ucred_uid" "#include <sys/socket.h>
+"
+if test "x$ac_cv_member_struct_ucred_uid" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_UCRED_UID 1
+_ACEOF
+
+
+fi
+ac_fn_c_check_member "$LINENO" "struct ucred" "cr_uid" "ac_cv_member_struct_ucred_cr_uid" "#include <sys/socket.h>
+"
+if test "x$ac_cv_member_struct_ucred_cr_uid" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_UCRED_CR_UID 1
+_ACEOF
+
+
 fi
 
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether time.h and sys/time.h may both be included" >&5
diff --git a/configure.ac b/configure.ac
index 7538bdf3d9f71bb4e7ccada50ad2d6039c0d94f0..ee27cebd8dc5350384002d397f828e2c37a29e29 100644
--- a/configure.ac
+++ b/configure.ac
@@ -489,6 +489,7 @@ AC_TYPE_OFF_T
 AC_TYPE_PID_T
 AC_TYPE_SIZE_T
 AC_CHECK_MEMBERS([struct stat.st_blksize])
+AC_CHECK_MEMBERS([struct ucred.uid, struct ucred.cr_uid], [], [], [#include <sys/socket.h>])
 AC_HEADER_TIME
 AC_STRUCT_TM
 AC_C_VOLATILE
diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in
index 430c4c0aacf86cbe372eb8c5237cb562f58cd90f..94dde0b31db693d9c69cb62b1432631ab66b6783 100644
--- a/include/asterisk/autoconfig.h.in
+++ b/include/asterisk/autoconfig.h.in
@@ -816,6 +816,12 @@
 /* Define to 1 if `st_blksize' is a member of `struct stat'. */
 #undef HAVE_STRUCT_STAT_ST_BLKSIZE
 
+/* Define to 1 if `cr_uid' is a member of `struct ucred'. */
+#undef HAVE_STRUCT_UCRED_CR_UID
+
+/* Define to 1 if `uid' is a member of `struct ucred'. */
+#undef HAVE_STRUCT_UCRED_UID
+
 /* Define to 1 if you have the mISDN Supplemental Services library. */
 #undef HAVE_SUPPSERV
 
diff --git a/main/asterisk.c b/main/asterisk.c
index ca3f68ba9f76705747b089ff1d93b181c162e9fd..465b29e0c7b046ec9063b0eb35886789c069bea8 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -1202,12 +1202,18 @@ static int read_credentials(int fd, char *buffer, size_t size, struct console *c
 		return result;
 	}
 
-#if defined(SO_PEERCRED)
+#if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
 	if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len)) {
 		return result;
 	}
+#if defined(HAVE_STRUCT_UCRED_UID)
 	uid = cred.uid;
 	gid = cred.gid;
+#else /* defined(HAVE_STRUCT_UCRED_CR_UID) */
+	uid = cred.cr_uid;
+	gid = cred.cr_gid;
+#endif /* defined(HAVE_STRUCT_UCRED_UID) */
+
 #elif defined(HAVE_GETPEEREID)
 	if (getpeereid(fd, &uid, &gid)) {
 		return result;