On 2019-08-07 15:56, Peter Eisentraut wrote: > Depending on your Windows environment, there might not be a suitable > /tmp directory, so you'll need to specify a directory explicitly using > postgres -k or similar. This leads to the question what the default for > DEFAULT_PGSOCKET_DIR should be on Windows. I think it's probably best, > at least for now, to set it so that by default, neither server nor libpq > use Unix sockets unless explicitly selected. This can be done easily on > the server side by defining DEFAULT_PGSOCKET_DIR as "". But in libpq, I > don't think the code would handle that correctly everywhere, so it would > need some more analysis and restructuring possibly.
Updated patches, which now also address that issue: There is no default socket dir on Windows and it's disabled by default on both client and server. Some comments on the patches: v2-0001-Enable-Unix-domain-sockets-support-on-Windows.patch This is pretty straightforward, apart from maybe some comments, but it would need to be committed last, because it would enable all the Unix socket related code on Windows, which needs to be fixed up by the subsequent patches first. v2-0002-Sort-out-getpeereid-and-struct-passwd-handling-on.patch Maybe a more elegant way with fewer #ifdef WIN32 can be found? v2-0003-psql-Remove-one-use-of-HAVE_UNIX_SOCKETS.patch This could be committed independently. v2-0004-libpq-Remove-unnecessary-uses-of-HAVE_UNIX_SOCKET.patch This one as well. v2-0005-initdb-Detect-Unix-domain-socket-support-dynamica.patch I think this patch contains some nice improvements in general. How much of that ends up being useful depends on how the subsequent patches (esp. 0007) end up, since with Unix-domain sockets disabled by default on Windows, we won't need initdb doing any detection. v2-0006-Fix-handling-of-Unix-domain-sockets-on-Windows-in.patch This is a fairly independent and isolated change. v2-0007-Disable-Unix-sockets-by-default-on-Windows.patch This one is a bit complicated. Since there is no good default location for Unix sockets on Windows, and many systems won't support them for some time, the default implemented here is to not use them by default on the server or client. This needs a fair amount of restructuring in the to support the case of "supports Unix sockets but don't use them by default", while maintaining the existing cases of "doesn't support Unix sockets" and "use Unix sockets by default". There is some room for discussion here. This patch set needs testers with various Windows versions to test different configurations, combinations, and versions. -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From c817dfc0ec2aead25e6fcd33f9e89bd55cb2c0b1 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Wed, 7 Aug 2019 15:44:19 +0200 Subject: [PATCH v2 1/7] Enable Unix-domain sockets support on Windows As of Windows 10 version 1803, Unix-domain sockets are supported on Windows. But it's not automatically detected by configure because it looks for struct sockaddr_un and Windows doesn't define that. So we just make our own definition on Windows and override the configure result. --- config/c-library.m4 | 5 +++-- configure | 5 ++++- src/include/c.h | 11 +++++++++++ src/include/pg_config.h.in | 6 +++--- src/include/pg_config.h.win32 | 6 +++--- src/include/pg_config_manual.h | 7 ------- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/config/c-library.m4 b/config/c-library.m4 index 6f2b0fbb4e..1469b07d2f 100644 --- a/config/c-library.m4 +++ b/config/c-library.m4 @@ -121,10 +121,11 @@ AC_DEFUN([PGAC_UNION_SEMUN], # PGAC_STRUCT_SOCKADDR_UN # ----------------------- -# If `struct sockaddr_un' exists, define HAVE_UNIX_SOCKETS. +# If `struct sockaddr_un' exists, define HAVE_STRUCT_SOCKADDR_UN. +# If it is missing then one could define it. # (Requires test for <sys/un.h>!) AC_DEFUN([PGAC_STRUCT_SOCKADDR_UN], -[AC_CHECK_TYPE([struct sockaddr_un], [AC_DEFINE(HAVE_UNIX_SOCKETS, 1, [Define to 1 if you have unix sockets.])], [], +[AC_CHECK_TYPES([struct sockaddr_un], [], [], [#include <sys/types.h> #ifdef HAVE_SYS_UN_H #include <sys/un.h> diff --git a/configure b/configure index 7a6bfc2339..6e87537be3 100755 --- a/configure +++ b/configure @@ -14135,7 +14135,10 @@ ac_fn_c_check_type "$LINENO" "struct sockaddr_un" "ac_cv_type_struct_sockaddr_un " if test "x$ac_cv_type_struct_sockaddr_un" = xyes; then : -$as_echo "#define HAVE_UNIX_SOCKETS 1" >>confdefs.h +cat >>confdefs.h <<_ACEOF +#define HAVE_STRUCT_SOCKADDR_UN 1 +_ACEOF + fi diff --git a/src/include/c.h b/src/include/c.h index 2a082afab1..434c403269 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -1263,6 +1263,17 @@ extern unsigned long long strtoull(const char *str, char **endptr, int base); #define NON_EXEC_STATIC static #endif +#ifdef HAVE_STRUCT_SOCKADDR_UN +#define HAVE_UNIX_SOCKETS 1 +#elif defined(WIN32) +struct sockaddr_un +{ + unsigned short sun_family; + char sun_path[108]; +}; +#define HAVE_UNIX_SOCKETS 1 +#endif + /* /port compatibility functions */ #include "port.h" diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index 512213aa32..e75c090f85 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -594,6 +594,9 @@ /* Define to 1 if `__ss_len' is a member of `struct sockaddr_storage'. */ #undef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN +/* Define to 1 if the system has the type `struct sockaddr_un'. */ +#undef HAVE_STRUCT_SOCKADDR_UN + /* Define to 1 if `tm_zone' is a member of `struct tm'. */ #undef HAVE_STRUCT_TM_TM_ZONE @@ -682,9 +685,6 @@ /* Define to 1 if you have the <unistd.h> header file. */ #undef HAVE_UNISTD_H -/* Define to 1 if you have unix sockets. */ -#undef HAVE_UNIX_SOCKETS - /* Define to 1 if you have the `unsetenv' function. */ #undef HAVE_UNSETENV diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32 index 2d903c82b8..b947a9fbde 100644 --- a/src/include/pg_config.h.win32 +++ b/src/include/pg_config.h.win32 @@ -463,6 +463,9 @@ /* Define to 1 if `__ss_len' is member of `struct sockaddr_storage'. */ /* #undef HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN */ +/* Define to 1 if the system has the type `struct sockaddr_un'. */ +/* #undef HAVE_STRUCT_SOCKADDR_UN */ + /* Define to 1 if `tm_zone' is member of `struct tm'. */ /* #undef HAVE_STRUCT_TM_TM_ZONE */ @@ -530,9 +533,6 @@ /* Define to 1 if you have the <unistd.h> header file. */ #define HAVE_UNISTD_H 1 -/* Define to 1 if you have unix sockets. */ -/* #undef HAVE_UNIX_SOCKETS */ - /* Define to 1 if you have the `unsetenv' function. */ /* #undef HAVE_UNSETENV */ diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h index 743401cb96..65d20d44b3 100644 --- a/src/include/pg_config_manual.h +++ b/src/include/pg_config_manual.h @@ -109,13 +109,6 @@ */ #define ALIGNOF_BUFFER 32 -/* - * Disable UNIX sockets for certain operating systems. - */ -#if defined(WIN32) -#undef HAVE_UNIX_SOCKETS -#endif - /* * Define this if your operating system supports link() */ base-commit: 66bde49d96a9ddacc49dcbdf1b47b5bd6e31ead5 -- 2.22.0
From 66a8cba64515da97b6614fde23d52bc1f0a6ecd2 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Wed, 7 Aug 2019 15:44:19 +0200 Subject: [PATCH v2 2/7] Sort out getpeereid() and struct passwd handling on Windows The getpeereid() uses are protected by HAVE_UNIX_SOCKETS, so they didn't ever care about Windows support. But that is required now, and so we need to provide a getpeereid() stub for Windows. We can just let configure do its usual thing of picking up the replacefrom from libpgport instead of the custom overrides that it was doing before. But then Windows doesn't have struct passwd, so all this code around getpeereid() won't work anyway. This patch just sprinkles some #ifdef WIN32 around to make it work. Perhaps a configure test for struct passwd would be a better way to protect this code. --- configure | 34 +++++++++++++------------------ configure.in | 9 +++----- src/backend/libpq/auth.c | 6 ++++++ src/include/port.h | 2 +- src/interfaces/libpq/fe-connect.c | 4 ++++ 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/configure b/configure index 6e87537be3..8c48c6ce81 100755 --- a/configure +++ b/configure @@ -15837,6 +15837,19 @@ esac fi +ac_fn_c_check_func "$LINENO" "getpeereid" "ac_cv_func_getpeereid" +if test "x$ac_cv_func_getpeereid" = xyes; then : + $as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h + +else + case " $LIBOBJS " in + *" getpeereid.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS getpeereid.$ac_objext" + ;; +esac + +fi + ac_fn_c_check_func "$LINENO" "getrusage" "ac_cv_func_getrusage" if test "x$ac_cv_func_getrusage" = xyes; then : $as_echo "#define HAVE_GETRUSAGE 1" >>confdefs.h @@ -16017,17 +16030,11 @@ esac case $host_os in # Windows uses a specialised env handler - # and doesn't need a replacement getpeereid because it doesn't use - # Unix sockets. mingw*) $as_echo "#define HAVE_UNSETENV 1" >>confdefs.h - -$as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h - - ac_cv_func_unsetenv=yes - ac_cv_func_getpeereid=yes;; + ac_cv_func_unsetenv=yes;; *) ac_fn_c_check_func "$LINENO" "unsetenv" "ac_cv_func_unsetenv" if test "x$ac_cv_func_unsetenv" = xyes; then : @@ -16040,19 +16047,6 @@ else ;; esac -fi - -ac_fn_c_check_func "$LINENO" "getpeereid" "ac_cv_func_getpeereid" -if test "x$ac_cv_func_getpeereid" = xyes; then : - $as_echo "#define HAVE_GETPEEREID 1" >>confdefs.h - -else - case " $LIBOBJS " in - *" getpeereid.$ac_objext "* ) ;; - *) LIBOBJS="$LIBOBJS getpeereid.$ac_objext" - ;; -esac - fi diff --git a/configure.in b/configure.in index dde3eec89f..03fd9b6a61 100644 --- a/configure.in +++ b/configure.in @@ -1696,6 +1696,7 @@ AC_REPLACE_FUNCS(m4_normalize([ dlopen fls getopt + getpeereid getrusage inet_aton mkdtemp @@ -1726,15 +1727,11 @@ esac case $host_os in # Windows uses a specialised env handler - # and doesn't need a replacement getpeereid because it doesn't use - # Unix sockets. mingw*) AC_DEFINE(HAVE_UNSETENV, 1, [Define to 1 because replacement version used.]) - AC_DEFINE(HAVE_GETPEEREID, 1, [Define to 1 because function not required.]) - ac_cv_func_unsetenv=yes - ac_cv_func_getpeereid=yes;; + ac_cv_func_unsetenv=yes;; *) - AC_REPLACE_FUNCS([unsetenv getpeereid]) + AC_REPLACE_FUNCS([unsetenv]) ;; esac diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c index 0e0a6d8752..67bf82c898 100644 --- a/src/backend/libpq/auth.c +++ b/src/backend/libpq/auth.c @@ -1999,7 +1999,11 @@ auth_peer(hbaPort *port) } errno = 0; /* clear errno before call */ +#ifndef WIN32 pw = getpwuid(uid); +#else + pw = NULL; +#endif if (!pw) { int save_errno = errno; @@ -2011,7 +2015,9 @@ auth_peer(hbaPort *port) return STATUS_ERROR; } +#ifndef WIN32 strlcpy(ident_user, pw->pw_name, IDENT_USERNAME_MAX + 1); +#endif return check_usermap(port->hba->usermap, port->user_name, ident_user, false); } diff --git a/src/include/port.h b/src/include/port.h index b5c03d912b..a3e7b5c03c 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -357,7 +357,7 @@ extern int fls(int mask); #define ftello(a) ftell(a) #endif -#if !defined(HAVE_GETPEEREID) && !defined(WIN32) +#ifndef HAVE_GETPEEREID extern int getpeereid(int sock, uid_t *uid, gid_t *gid); #endif diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index fa5af18ffa..ec456725dd 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -2686,8 +2686,10 @@ PQconnectPoll(PGconn *conn) IS_AF_UNIX(conn->raddr.addr.ss_family)) { char pwdbuf[BUFSIZ]; +#ifndef WIN32 struct passwd pass_buf; struct passwd *pass; +#endif int passerr; uid_t uid; gid_t gid; @@ -2709,6 +2711,7 @@ PQconnectPoll(PGconn *conn) goto error_return; } +#ifndef WIN32 passerr = pqGetpwuid(uid, &pass_buf, pwdbuf, sizeof(pwdbuf), &pass); if (pass == NULL) { @@ -2731,6 +2734,7 @@ PQconnectPoll(PGconn *conn) conn->requirepeer, pass->pw_name); goto error_return; } +#endif /* WIN32 */ } #endif /* HAVE_UNIX_SOCKETS */ -- 2.22.0
From 6c6a7f4a0fdf165b5fee5bdd1dab88388cc2f623 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Wed, 7 Aug 2019 15:44:19 +0200 Subject: [PATCH v2 3/7] psql: Remove one use of HAVE_UNIX_SOCKETS This use was not protecting any unportable code, it was just omitting the code because it wouldn't be used. Remove the use to reduce code complexity a bit. --- src/bin/psql/prompt.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 0fcb8c7783..f8edc942da 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -12,11 +12,6 @@ #include <win32.h> #endif -#ifdef HAVE_UNIX_SOCKETS -#include <unistd.h> -#include <netdb.h> -#endif - #include "common.h" #include "input.h" #include "prompt.h" @@ -139,7 +134,6 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) if (*p == 'm') buf[strcspn(buf, ".")] = '\0'; } -#ifdef HAVE_UNIX_SOCKETS /* UNIX socket */ else { @@ -150,7 +144,6 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) else snprintf(buf, sizeof(buf), "[local:%s]", host); } -#endif } break; /* DB server port number */ -- 2.22.0
From a5fb2dbd8a9bc17541250a7e6093a5d115d8b745 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Wed, 7 Aug 2019 15:44:19 +0200 Subject: [PATCH v2 4/7] libpq: Remove unnecessary uses of HAVE_UNIX_SOCKETS These are not protecting any unportable code and they were not used consistently anyway (other code uses IS_AF_UNIX without HAVE_UNIX_SOCKETS). --- src/interfaces/libpq/fe-connect.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index ec456725dd..ac4fc8a6fc 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -1058,10 +1058,8 @@ connectOptions2(PGconn *conn) else if (ch->host != NULL && ch->host[0] != '\0') { ch->type = CHT_HOST_NAME; -#ifdef HAVE_UNIX_SOCKETS if (is_absolute_path(ch->host)) ch->type = CHT_UNIX_SOCKET; -#endif } else { @@ -1569,7 +1567,6 @@ connectFailureMessage(PGconn *conn, int errorno) { char sebuf[PG_STRERROR_R_BUFLEN]; -#ifdef HAVE_UNIX_SOCKETS if (IS_AF_UNIX(conn->raddr.addr.ss_family)) { char service[NI_MAXHOST]; @@ -1586,7 +1583,6 @@ connectFailureMessage(PGconn *conn, int errorno) service); } else -#endif /* HAVE_UNIX_SOCKETS */ { char host_addr[NI_MAXHOST]; const char *displayed_host; @@ -2676,8 +2672,6 @@ PQconnectPoll(PGconn *conn) char *startpacket; int packetlen; -#ifdef HAVE_UNIX_SOCKETS - /* * Implement requirepeer check, if requested and it's a * Unix-domain socket. @@ -2736,7 +2730,6 @@ PQconnectPoll(PGconn *conn) } #endif /* WIN32 */ } -#endif /* HAVE_UNIX_SOCKETS */ if (IS_AF_UNIX(conn->raddr.addr.ss_family)) { -- 2.22.0
From fc787f67222d32d144529ed8f9e0242c30632e85 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Wed, 7 Aug 2019 15:44:19 +0200 Subject: [PATCH v2 5/7] initdb: Detect Unix-domain socket support dynamically On Windows we now have to support the situation where a binary built with Unix-domain socket support could be used on a system that doesn't support it at run time. So in initdb, for setting up the default pg_hba.conf and postgresql.conf, do a run-time check for support instead of relying on compile-time decisions. This is very similar to what we already do for IPv6, so similar code can be used. A change is that now if Unix-domain socket support is not found, the "local" lines in pg_hba.conf are commented out instead of removed, again similar to IPv6 support. --- src/backend/libpq/pg_hba.conf.sample | 6 +- src/bin/initdb/initdb.c | 170 +++++++++++++-------------- 2 files changed, 84 insertions(+), 92 deletions(-) diff --git a/src/backend/libpq/pg_hba.conf.sample b/src/backend/libpq/pg_hba.conf.sample index c853e36232..0c163d2c6d 100644 --- a/src/backend/libpq/pg_hba.conf.sample +++ b/src/backend/libpq/pg_hba.conf.sample @@ -76,14 +76,14 @@ # TYPE DATABASE USER ADDRESS METHOD -@remove-line-for-nolocal@# "local" is for Unix domain socket connections only -@remove-line-for-nolocal@local all all @authmethodlocal@ +# "local" is for Unix domain socket connections only +local all all @authmethodlocal@ # IPv4 local connections: host all all 127.0.0.1/32 @authmethodhost@ # IPv6 local connections: host all all ::1/128 @authmethodhost@ # Allow replication connections from localhost, by a user with the # replication privilege. -@remove-line-for-nolocal@local replication all @authmethodlocal@ +local replication all @authmethodlocal@ host replication all 127.0.0.1/32 @authmethodhost@ host replication all ::1/128 @authmethodhost@ diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 551d379d85..db7054fbf8 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -233,9 +233,6 @@ static char backend_exec[MAXPGPATH]; static char **replace_token(char **lines, const char *token, const char *replacement); -#ifndef HAVE_UNIX_SOCKETS -static char **filter_lines_with_token(char **lines, const char *token); -#endif static char **readfile(const char *path); static void writefile(char *path, char **lines); static FILE *popen_check(const char *command, const char *mode); @@ -433,36 +430,6 @@ replace_token(char **lines, const char *token, const char *replacement) return result; } -/* - * make a copy of lines without any that contain the token - * - * a sort of poor man's grep -v - */ -#ifndef HAVE_UNIX_SOCKETS -static char ** -filter_lines_with_token(char **lines, const char *token) -{ - int numlines = 1; - int i, - src, - dst; - char **result; - - for (i = 0; lines[i]; i++) - numlines++; - - result = (char **) pg_malloc(numlines * sizeof(char *)); - - for (src = 0, dst = 0; src < numlines; src++) - { - if (lines[src] == NULL || strstr(lines[src], token) == NULL) - result[dst++] = lines[src]; - } - - return result; -} -#endif - /* * get the lines from a text file */ @@ -1072,10 +1039,70 @@ setup_config(void) char repltok[MAXPGPATH]; char path[MAXPGPATH]; char *autoconflines[3]; + bool unix_sockets_work = false; + bool ipv6_works = false; fputs(_("creating configuration files ... "), stdout); fflush(stdout); +#ifdef WIN32 + { + /* need to call WSAStartup before calling socket or getaddrinfo */ + int err = 0; + WSADATA wsaData; + + err = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (err != 0) + { + pg_log_error("WSAStartup failed: %d", err); + exit(1); + } + } +#endif + +#ifdef HAVE_UNIX_SOCKETS + /* + * Probe to see whether Unix-domain sockets are working. + */ + { + pgsocket tmpsock; + + tmpsock = socket(AF_UNIX, SOCK_STREAM, 0); + if (tmpsock != PGINVALID_SOCKET) + { + unix_sockets_work = true; + closesocket(tmpsock); + } + } +#endif + +#ifdef HAVE_IPV6 + /* + * Probe to see if there is really any platform support for IPv6, and + * comment out the relevant pg_hba line if not. This avoids runtime + * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly + * useful on Windows, where executables built on a machine with IPv6 may + * have to run on a machine without. + */ + { + struct addrinfo *gai_result; + struct addrinfo hints; + + /* for best results, this code should match parse_hba_line() */ + hints.ai_flags = AI_NUMERICHOST; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = 0; + hints.ai_protocol = 0; + hints.ai_addrlen = 0; + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + + if (getaddrinfo("::1", NULL, &hints, &gai_result) == 0) + ipv6_works = true; + } +#endif /* !HAVE_IPV6 */ + /* postgresql.conf */ conflines = readfile(conf_file); @@ -1092,8 +1119,11 @@ setup_config(void) conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok); #ifdef HAVE_UNIX_SOCKETS - snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'", - DEFAULT_PGSOCKET_DIR); + if (unix_sockets_work) + snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'", + DEFAULT_PGSOCKET_DIR); + else + snprintf(repltok, sizeof(repltok), "unix_socket_directories = ''"); #else snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''"); #endif @@ -1256,63 +1286,25 @@ setup_config(void) conflines = readfile(hba_file); -#ifndef HAVE_UNIX_SOCKETS - conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@"); -#else - conflines = replace_token(conflines, "@remove-line-for-nolocal@", ""); -#endif - -#ifdef HAVE_IPV6 - - /* - * Probe to see if there is really any platform support for IPv6, and - * comment out the relevant pg_hba line if not. This avoids runtime - * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly - * useful on Windows, where executables built on a machine with IPv6 may - * have to run on a machine without. - */ + if (!unix_sockets_work) { - struct addrinfo *gai_result; - struct addrinfo hints; - int err = 0; - -#ifdef WIN32 - /* need to call WSAStartup before calling getaddrinfo */ - WSADATA wsaData; - - err = WSAStartup(MAKEWORD(2, 2), &wsaData); -#endif - - /* for best results, this code should match parse_hba_line() */ - hints.ai_flags = AI_NUMERICHOST; - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = 0; - hints.ai_protocol = 0; - hints.ai_addrlen = 0; - hints.ai_canonname = NULL; - hints.ai_addr = NULL; - hints.ai_next = NULL; + conflines = replace_token(conflines, + "local all", + "#local all"); + conflines = replace_token(conflines, + "local replication", + "#local replication"); + } - if (err != 0 || - getaddrinfo("::1", NULL, &hints, &gai_result) != 0) - { - conflines = replace_token(conflines, - "host all all ::1", - "#host all all ::1"); - conflines = replace_token(conflines, - "host replication all ::1", - "#host replication all ::1"); - } + if (!ipv6_works) + { + conflines = replace_token(conflines, + "host all all ::1", + "#host all all ::1"); + conflines = replace_token(conflines, + "host replication all ::1", + "#host replication all ::1"); } -#else /* !HAVE_IPV6 */ - /* If we didn't compile IPV6 support at all, always comment it out */ - conflines = replace_token(conflines, - "host all all ::1", - "#host all all ::1"); - conflines = replace_token(conflines, - "host replication all ::1", - "#host replication all ::1"); -#endif /* HAVE_IPV6 */ /* Replace default authentication methods */ conflines = replace_token(conflines, -- 2.22.0
From 00aa3b1f9c621d12c47f8e2e5f40a0a1ef0b0ce3 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Thu, 8 Aug 2019 11:19:04 +0200 Subject: [PATCH v2 6/7] Fix handling of Unix-domain sockets on Windows in tests Don't run the tests using Unix-domain sockets by default on Windows, but allow enabling it explicitly by setting the environment variable PG_TEST_USE_UNIX_SOCKETS. --- src/bin/pg_ctl/t/001_start_stop.pl | 2 +- src/test/authentication/t/001_password.pl | 7 +++---- src/test/authentication/t/002_saslprep.pl | 7 +++---- src/test/perl/PostgresNode.pm | 4 ++-- src/test/perl/TestLib.pm | 8 +++++++- src/test/regress/pg_regress.c | 14 ++++++++++---- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/bin/pg_ctl/t/001_start_stop.pl b/src/bin/pg_ctl/t/001_start_stop.pl index e5d46a6f25..70748354a6 100644 --- a/src/bin/pg_ctl/t/001_start_stop.pl +++ b/src/bin/pg_ctl/t/001_start_stop.pl @@ -27,7 +27,7 @@ print $conf TestLib::slurp_file($ENV{TEMP_CONFIG}) if defined $ENV{TEMP_CONFIG}; -if (!$windows_os) +if ($use_unix_sockets) { print $conf "listen_addresses = ''\n"; print $conf "unix_socket_directories = '$tempdir_short'\n"; diff --git a/src/test/authentication/t/001_password.pl b/src/test/authentication/t/001_password.pl index 3a3b0eb7e8..f69d6dcf3f 100644 --- a/src/test/authentication/t/001_password.pl +++ b/src/test/authentication/t/001_password.pl @@ -3,17 +3,16 @@ # - Plain # - MD5-encrypted # - SCRAM-encrypted -# This test cannot run on Windows as Postgres cannot be set up with Unix -# sockets and needs to go through SSPI. +# This test can only run with Unix-domain sockets. use strict; use warnings; use PostgresNode; use TestLib; use Test::More; -if ($windows_os) +if (!$use_unix_sockets) { - plan skip_all => "authentication tests cannot run on Windows"; + plan skip_all => "authentication tests cannot run without Unix-domain sockets"; } else { diff --git a/src/test/authentication/t/002_saslprep.pl b/src/test/authentication/t/002_saslprep.pl index c4b335c45f..bf57933d94 100644 --- a/src/test/authentication/t/002_saslprep.pl +++ b/src/test/authentication/t/002_saslprep.pl @@ -1,16 +1,15 @@ # Test password normalization in SCRAM. # -# This test cannot run on Windows as Postgres cannot be set up with Unix -# sockets and needs to go through SSPI. +# This test can only run with Unix-domain sockets. use strict; use warnings; use PostgresNode; use TestLib; use Test::More; -if ($windows_os) +if (!$use_unix_sockets) { - plan skip_all => "authentication tests cannot run on Windows"; + plan skip_all => "authentication tests cannot run without Unix-domain sockets"; } else { diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm index 270bd6c856..04d9e6bde1 100644 --- a/src/test/perl/PostgresNode.pm +++ b/src/test/perl/PostgresNode.pm @@ -116,7 +116,7 @@ INIT # Set PGHOST for backward compatibility. This doesn't work for own_host # nodes, so prefer to not rely on this when writing new tests. - $use_tcp = $TestLib::windows_os; + $use_tcp = !$TestLib::use_unix_sockets; $test_localhost = "127.0.0.1"; $last_host_assigned = 1; $test_pghost = $use_tcp ? $test_localhost : TestLib::tempdir_short; @@ -387,7 +387,7 @@ sub set_replication_conf open my $hba, '>>', "$pgdata/pg_hba.conf"; print $hba "\n# Allow replication (set up by PostgresNode.pm)\n"; - if ($TestLib::windows_os) + if ($TestLib::windows_os && !$TestLib::use_unix_sockets) { print $hba "host replication all $test_localhost/32 sspi include_realm=1 map=regress\n"; diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm index 6195c21c59..a20c3709eb 100644 --- a/src/test/perl/TestLib.pm +++ b/src/test/perl/TestLib.pm @@ -50,9 +50,10 @@ our @EXPORT = qw( command_checks_all $windows_os + $use_unix_sockets ); -our ($windows_os, $tmp_check, $log_path, $test_logfile); +our ($windows_os, $use_unix_sockets, $tmp_check, $log_path, $test_logfile); BEGIN { @@ -79,6 +80,11 @@ BEGIN # Must be set early $windows_os = $Config{osname} eq 'MSWin32' || $Config{osname} eq 'msys'; + + # Specifies whether to use Unix sockets for test setups. On + # Windows we don't use them by default since it's not universally + # supported, but it can be overridden if desired. + $use_unix_sockets = (!$windows_os || $ENV{PG_TEST_USE_UNIX_SOCKETS} ne ''); } INIT diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c index 117a9544ea..22b40304ce 100644 --- a/src/test/regress/pg_regress.c +++ b/src/test/regress/pg_regress.c @@ -102,11 +102,10 @@ static char *logfilename; static FILE *logfile; static char *difffilename; static const char *sockdir; -#ifdef HAVE_UNIX_SOCKETS +static bool use_unix_sockets; static const char *temp_sockdir; static char sockself[MAXPGPATH]; static char socklock[MAXPGPATH]; -#endif static _resultmap *resultmap = NULL; @@ -2117,10 +2116,17 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc atexit(stop_postmaster); #ifndef HAVE_UNIX_SOCKETS - /* no unix domain sockets available, so change default */ - hostname = "localhost"; + use_unix_sockets = false; +#elif defined(WIN32) + use_unix_sockets = getenv("PG_TEST_USE_UNIX_SOCKETS") ? true : false; +#else + use_unix_sockets = true; #endif + if (!use_unix_sockets) + /* no unix domain sockets available, so change default */ + hostname = "localhost"; + /* * We call the initialization function here because that way we can set * default parameters and let them be overwritten by the commandline. -- 2.22.0
From bc754ebf8304c03ed4ddb9e71901a639ae9a323c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pe...@eisentraut.org> Date: Tue, 13 Aug 2019 00:47:59 +0200 Subject: [PATCH v2 7/7] Disable Unix sockets by default on Windows --- src/backend/utils/misc/guc.c | 2 +- src/bin/initdb/initdb.c | 2 +- src/bin/psql/prompt.c | 2 ++ src/bin/scripts/pg_isready.c | 4 ++++ src/include/libpq/pqcomm.h | 8 ++++++++ src/include/port/win32.h | 8 ++++++++ src/interfaces/libpq/fe-connect.c | 4 +++- 7 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index eb78522053..c8fe30ce53 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -3939,7 +3939,7 @@ static struct config_string ConfigureNamesString[] = GUC_SUPERUSER_ONLY }, &Unix_socket_directories, -#ifdef HAVE_UNIX_SOCKETS +#if defined(HAVE_UNIX_SOCKETS) && defined(DEFAULT_PGSOCKET_DIR) DEFAULT_PGSOCKET_DIR, #else "", diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index db7054fbf8..da563a9bf6 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -1118,7 +1118,7 @@ setup_config(void) n_buffers * (BLCKSZ / 1024)); conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok); -#ifdef HAVE_UNIX_SOCKETS +#if defined(HAVE_UNIX_SOCKETS) && defined(DEFAULT_PGSOCKET_DIR) if (unix_sockets_work) snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'", DEFAULT_PGSOCKET_DIR); diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index f8edc942da..7764bae064 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -138,7 +138,9 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) else { if (!host +#ifdef DEFAULT_PGSOCKET_DIR || strcmp(host, DEFAULT_PGSOCKET_DIR) == 0 +#endif || *p == 'm') strlcpy(buf, "[local]", sizeof(buf)); else diff --git a/src/bin/scripts/pg_isready.c b/src/bin/scripts/pg_isready.c index 079447f951..4dfc8712bf 100644 --- a/src/bin/scripts/pg_isready.c +++ b/src/bin/scripts/pg_isready.c @@ -164,7 +164,11 @@ main(int argc, char **argv) else if (def->val) pghost_str = def->val; else +#ifdef DEFAULT_PGSOCKET_DIR pghost_str = DEFAULT_PGSOCKET_DIR; +#else + pghost_str = ""; +#endif } else if (strcmp(def->keyword, "hostaddr") == 0) { diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h index baf6a4b6c0..8fe332c1e4 100644 --- a/src/include/libpq/pqcomm.h +++ b/src/include/libpq/pqcomm.h @@ -67,11 +67,19 @@ typedef struct /* Configure the UNIX socket location for the well known port. */ +#ifdef DEFAULT_PGSOCKET_DIR #define UNIXSOCK_PATH(path, port, sockdir) \ snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \ ((sockdir) && *(sockdir) != '\0') ? (sockdir) : \ DEFAULT_PGSOCKET_DIR, \ (port)) +#else +#define UNIXSOCK_PATH(path, port, sockdir) \ + (AssertMacro((sockdir) && *(sockdir) != '\0'), \ + snprintf(path, sizeof(path), "%s/.s.PGSQL.%d", \ + (sockdir), \ + (port))) +#endif /* * The maximum workable length of a socket path is what will fit into diff --git a/src/include/port/win32.h b/src/include/port/win32.h index 9f48a58aed..47c1868149 100644 --- a/src/include/port/win32.h +++ b/src/include/port/win32.h @@ -56,3 +56,11 @@ #else #define PGDLLEXPORT #endif + + +/* + * On Windows, there is no good standard location for AF_UNIX sockets, and + * many builds of Windows don't support them yet, so don't create one by + * default. + */ +#undef DEFAULT_PGSOCKET_DIR diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index ac4fc8a6fc..abfc1c409f 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -1065,7 +1065,7 @@ connectOptions2(PGconn *conn) { if (ch->host) free(ch->host); -#ifdef HAVE_UNIX_SOCKETS +#if defined(HAVE_UNIX_SOCKETS) && defined(DEFAULT_PGSOCKET_DIR) ch->host = strdup(DEFAULT_PGSOCKET_DIR); ch->type = CHT_UNIX_SOCKET; #else @@ -6853,6 +6853,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname, /* 'localhost' matches pghost of '' or the default socket directory */ if (hostname == NULL || hostname[0] == '\0') hostname = DefaultHost; +#ifdef DEFAULT_PGSOCKET_DIR else if (is_absolute_path(hostname)) /* @@ -6861,6 +6862,7 @@ passwordFromFile(const char *hostname, const char *port, const char *dbname, */ if (strcmp(hostname, DEFAULT_PGSOCKET_DIR) == 0) hostname = DefaultHost; +#endif if (port == NULL || port[0] == '\0') port = DEF_PGPORT_STR; -- 2.22.0