Skip to content

Commit f14413b

Browse files
committed
Sort out getpeereid() and peer auth handling on Windows
The getpeereid() uses have so far been protected by HAVE_UNIX_SOCKETS, so they didn't ever care about Windows support. But in anticipation of Unix-domain socket support on Windows, that needs to be handled differently. Windows doesn't support getpeereid() at this time, so we use the existing not-supported code path. We let configure do its usual thing of picking up the replacement from libpgport, instead of the custom overrides that it was doing before. But then Windows doesn't have struct passwd, so this patch sprinkles some additional #ifdef WIN32 around to make it work. This is similar to existing code that deals with this issue. Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com> Discussion: https://www.postgresql.org/message-id/5974caea-1267-7708-40f2-6009a9d653b0@2ndquadrant.com
1 parent 956ef58 commit f14413b

File tree

6 files changed

+36
-42
lines changed

6 files changed

+36
-42
lines changed

configure

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15685,6 +15685,19 @@ esac
1568515685

1568615686
fi
1568715687

15688+
ac_fn_c_check_func "$LINENO" "getpeereid" "ac_cv_func_getpeereid"
15689+
if test "x$ac_cv_func_getpeereid" = xyes; then :
15690+
$as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
15691+
15692+
else
15693+
case " $LIBOBJS " in
15694+
*" getpeereid.$ac_objext "* ) ;;
15695+
*) LIBOBJS="$LIBOBJS getpeereid.$ac_objext"
15696+
;;
15697+
esac
15698+
15699+
fi
15700+
1568815701
ac_fn_c_check_func "$LINENO" "getrusage" "ac_cv_func_getrusage"
1568915702
if test "x$ac_cv_func_getrusage" = xyes; then :
1569015703
$as_echo "#define HAVE_GETRUSAGE 1" >>confdefs.h
@@ -15863,19 +15876,13 @@ $as_echo "$as_me: On $host_os we will use our strtof wrapper." >&6;}
1586315876
esac
1586415877

1586515878
case $host_os in
15866-
1586715879
# Windows uses a specialised env handler
15868-
# and doesn't need a replacement getpeereid because it doesn't use
15869-
# Unix sockets.
1587015880
mingw*)
1587115881

1587215882
$as_echo "#define HAVE_UNSETENV 1" >>confdefs.h
1587315883

15874-
15875-
$as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
15876-
1587715884
ac_cv_func_unsetenv=yes
15878-
ac_cv_func_getpeereid=yes;;
15885+
;;
1587915886
*)
1588015887
ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv"
1588115888
if test "x$ac_cv_func_unsetenv" = xyes; then :
@@ -15890,21 +15897,8 @@ esac
1589015897

1589115898
fi
1589215899

15893-
ac_fn_c_check_func "$LINENO" "getpeereid" "ac_cv_func_getpeereid"
15894-
if test "x$ac_cv_func_getpeereid" = xyes; then :
15895-
$as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h
1589615900

15897-
else
15898-
case " $LIBOBJS " in
15899-
*" getpeereid.$ac_objext "* ) ;;
15900-
*) LIBOBJS="$LIBOBJS getpeereid.$ac_objext"
15901-
;;
15902-
esac
15903-
15904-
fi
15905-
15906-
15907-
;;
15901+
;;
1590815902
esac
1590915903

1591015904
# System's version of getaddrinfo(), if any, may be used only if we found

configure.in

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,7 @@ AC_REPLACE_FUNCS(m4_normalize([
17291729
explicit_bzero
17301730
fls
17311731
getopt
1732+
getpeereid
17321733
getrusage
17331734
inet_aton
17341735
mkdtemp
@@ -1757,18 +1758,14 @@ case $host_os in
17571758
esac
17581759

17591760
case $host_os in
1760-
17611761
# Windows uses a specialised env handler
1762-
# and doesn't need a replacement getpeereid because it doesn't use
1763-
# Unix sockets.
17641762
mingw*)
17651763
AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement version used.])
1766-
AC_DEFINE(HAVE_GETPEEREID, 1, [Define to 1 because function not required.])
17671764
ac_cv_func_unsetenv=yes
1768-
ac_cv_func_getpeereid=yes;;
1765+
;;
17691766
*)
1770-
AC_REPLACE_FUNCS([unsetenv getpeereid])
1771-
;;
1767+
AC_REPLACE_FUNCS([unsetenv])
1768+
;;
17721769
esac
17731770

17741771
# System's version of getaddrinfo(), if any, may be used only if we found

src/backend/libpq/auth.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ static int ident_inet(hbaPort *port);
7777
* Peer authentication
7878
*----------------------------------------------------------------
7979
*/
80-
#ifdef HAVE_UNIX_SOCKETS
8180
static int auth_peer(hbaPort *port);
82-
#endif
8381

8482

8583
/*----------------------------------------------------------------
@@ -559,11 +557,7 @@ ClientAuthentication(Port *port)
559557
break;
560558

561559
case uaPeer:
562-
#ifdef HAVE_UNIX_SOCKETS
563560
status = auth_peer(port);
564-
#else
565-
Assert(false);
566-
#endif
567561
break;
568562

569563
case uaIdent:
@@ -1984,16 +1978,16 @@ ident_inet(hbaPort *port)
19841978
*
19851979
* Iff authorized, return STATUS_OK, otherwise return STATUS_ERROR.
19861980
*/
1987-
#ifdef HAVE_UNIX_SOCKETS
1988-
19891981
static int
19901982
auth_peer(hbaPort *port)
19911983
{
19921984
uid_t uid;
19931985
gid_t gid;
1986+
#ifndef WIN32
19941987
struct passwd *pw;
19951988
char *peer_user;
19961989
int ret;
1990+
#endif
19971991

19981992
if (getpeereid(port->sock, &uid, &gid) != 0)
19991993
{
@@ -2009,6 +2003,7 @@ auth_peer(hbaPort *port)
20092003
return STATUS_ERROR;
20102004
}
20112005

2006+
#ifndef WIN32
20122007
errno = 0; /* clear errno before call */
20132008
pw = getpwuid(uid);
20142009
if (!pw)
@@ -2030,8 +2025,12 @@ auth_peer(hbaPort *port)
20302025
pfree(peer_user);
20312026

20322027
return ret;
2028+
#else
2029+
/* should have failed with ENOSYS above */
2030+
Assert(false);
2031+
return STATUS_ERROR;
2032+
#endif
20332033
}
2034-
#endif /* HAVE_UNIX_SOCKETS */
20352034

20362035

20372036
/*----------------------------------------------------------------

src/include/port.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ extern int fls(int mask);
354354
#define ftello(a) ftell(a)
355355
#endif
356356

357-
#if !defined(HAVE_GETPEEREID) && !defined(WIN32)
357+
#ifndef HAVE_GETPEEREID
358358
extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
359359
#endif
360360

src/interfaces/libpq/fe-connect.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,19 +2733,19 @@ PQconnectPoll(PGconn *conn)
27332733
char *startpacket;
27342734
int packetlen;
27352735

2736-
#ifdef HAVE_UNIX_SOCKETS
2737-
27382736
/*
27392737
* Implement requirepeer check, if requested and it's a
27402738
* Unix-domain socket.
27412739
*/
27422740
if (conn->requirepeer && conn->requirepeer[0] &&
27432741
IS_AF_UNIX(conn->raddr.addr.ss_family))
27442742
{
2743+
#ifndef WIN32
27452744
char pwdbuf[BUFSIZ];
27462745
struct passwd pass_buf;
27472746
struct passwd *pass;
27482747
int passerr;
2748+
#endif
27492749
uid_t uid;
27502750
gid_t gid;
27512751

@@ -2766,6 +2766,7 @@ PQconnectPoll(PGconn *conn)
27662766
goto error_return;
27672767
}
27682768

2769+
#ifndef WIN32
27692770
passerr = pqGetpwuid(uid, &pass_buf, pwdbuf, sizeof(pwdbuf), &pass);
27702771
if (pass == NULL)
27712772
{
@@ -2788,8 +2789,11 @@ PQconnectPoll(PGconn *conn)
27882789
conn->requirepeer, pass->pw_name);
27892790
goto error_return;
27902791
}
2792+
#else /* WIN32 */
2793+
/* should have failed with ENOSYS above */
2794+
Assert(false);
2795+
#endif /* WIN32 */
27912796
}
2792-
#endif /* HAVE_UNIX_SOCKETS */
27932797

27942798
if (IS_AF_UNIX(conn->raddr.addr.ss_family))
27952799
{

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ sub mkvcbuild
9494
$solution = CreateSolution($vsVersion, $config);
9595

9696
our @pgportfiles = qw(
97-
chklocale.c explicit_bzero.c fls.c fseeko.c getrusage.c inet_aton.c random.c
97+
chklocale.c explicit_bzero.c fls.c fseeko.c getpeereid.c getrusage.c inet_aton.c random.c
9898
srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
9999
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
100100
dirent.c dlopen.c getopt.c getopt_long.c

0 commit comments

Comments
 (0)