Skip to content

Commit

Permalink
nss: Build infrastructure
Browse files Browse the repository at this point in the history
Finally this adds the infrastructure to build a postgres installation
with libnss support.
  • Loading branch information
danielgustafsson authored and kevinburke committed Oct 29, 2021
1 parent c3cee0e commit 4965823
Show file tree
Hide file tree
Showing 9 changed files with 541 additions and 12 deletions.
375 changes: 372 additions & 3 deletions configure

Large diffs are not rendered by default.

80 changes: 77 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ fi
#
# There is currently only one supported SSL/TLS library: OpenSSL.
#
PGAC_ARG_REQ(with, ssl, [LIB], [use LIB for SSL/TLS support (openssl)])
PGAC_ARG_REQ(with, ssl, [LIB], [use LIB for SSL/TLS support (openssl, nss)])
if test x"$with_ssl" = x"" ; then
with_ssl=no
fi
Expand Down Expand Up @@ -1268,8 +1268,77 @@ if test "$with_ssl" = openssl ; then
# function was removed.
AC_CHECK_FUNCS([CRYPTO_lock])
AC_DEFINE([USE_OPENSSL], 1, [Define to 1 to build with OpenSSL support. (--with-ssl=openssl)])
elif test "$with_ssl" = nss ; then
PGAC_PATH_PROGS(NSS_CONFIG, nss-config)
PGAC_PATH_PROGS(NSPR_CONFIG, nspr-config)

if test -n "$NSS_CONFIG"; then
NSS_LIBS=`$NSS_CONFIG --libs`
NSS_CFLAGS=`$NSS_CONFIG --cflags`
NSS_VERSION=`$NSS_CONFIG --version`
if echo "$NSS_VERSION" | sed ['s/[.]/ /g'] | \
$AWK '{if ([$]1 == 3 && ([$]2 >= 42)) exit 1; else exit 0; }'
then
AC_MSG_ERROR([
*** The installed version of NSS is too old to use with PostgreSQL.
*** NSS version 3.42 or later is required, this is $NSS_VERSION.])
fi
AC_MSG_NOTICE([using NSS version $NSS_VERSION])
else
# No nss-config was found, manually try to get the version and check for
# library capabilities
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#define RC_INVOKED
#include <nss/nss.h>
], [
#if NSS_VMAJOR < 3 || (NSS_VMAJOR == 3 && NSS_VMINOR < 42)
choke me
#endif
])], [],
[AC_MSG_ERROR([
*** The installed version of NSS is too old to use with PostgreSQL.
*** NSS version 3.42 or later is required.])])

AC_CHECK_LIB(nss3, NSS_InitContext, [], [AC_MSG_ERROR([library 'nss3' is required for NSS])])
AC_CHECK_LIB(ssl3, SSL_GetImplementedCiphers, [], [AC_MSG_ERROR([library 'ssl3' is required for NSS])])
fi

if test -n "$NSPR_CONFIG"; then
NSPR_LIBS=`$NSPR_CONFIG --libs`
NSPR_CFLAGS=`$NSPR_CONFIG --cflags`
NSPR_VERSION=`$NSPR_CONFIG --version`

if echo "$NSPR_VERSION" | sed ['s/[.]/ /g'] | \
$AWK '{if ([$1] == 4 && ([$]2 >= 20)) exit 1; else exit 0; }'
then
AC_MSG_ERROR([
*** The installed version of NSPR is too old to use with PostgreSQL.
*** NSPR version 4.20 or later is required, this is $NSPR_VERSION.])
fi
AC_MSG_NOTICE([using NSPR version $NSPR_VERSION])
else

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#define NO_NSPR_10_SUPPORT
#include <nspr/prinit.h>
], [
#if PR_VMAJOR < 4 || (PR_VMAJOR == 4 && PR_VMINOR < 20)
choke me
#endif
])], [],
[AC_MSG_ERROR([
*** The installed version of NSPR is too old to use with PostgreSQL.
*** NSPR version 4.20 or later is required.])])

AC_CHECK_LIB(nspr4, PR_GetDefaultIOMethods, [], [AC_MSG_ERROR([library 'nspr4' is required for NSS])])
fi

LDFLAGS="$LDFLAGS $NSS_LIBS $NSPR_LIBS"
CFLAGS="$CFLAGS $NSS_CFLAGS $NSPR_CFLAGS"

AC_DEFINE([USE_NSS], 1, [Define to 1 if you have NSS support,])
elif test "$with_ssl" != no ; then
AC_MSG_ERROR([--with-ssl must specify openssl])
AC_MSG_ERROR([--with-ssl must specify one of openssl or nss])
fi
AC_SUBST(with_ssl)

Expand Down Expand Up @@ -1459,6 +1528,9 @@ fi
if test "$with_ssl" = openssl ; then
AC_CHECK_HEADER(openssl/ssl.h, [], [AC_MSG_ERROR([header file <openssl/ssl.h> is required for OpenSSL])])
AC_CHECK_HEADER(openssl/err.h, [], [AC_MSG_ERROR([header file <openssl/err.h> is required for OpenSSL])])
elif test "$with_ssl" = nss ; then
AC_CHECK_HEADER(nss/ssl.h, [], [AC_MSG_ERROR([header file <nss/ssl.h> is required for NSS])])
AC_CHECK_HEADER(nss/nss.h, [], [AC_MSG_ERROR([header file <nss/nss.h> is required for NSS])])
fi

if test "$with_pam" = yes ; then
Expand Down Expand Up @@ -2251,6 +2323,8 @@ fi
AC_MSG_CHECKING([which random number source to use])
if test x"$with_ssl" = x"openssl" ; then
AC_MSG_RESULT([OpenSSL])
elif test x"$with_ssl" = x"nss" ; then
AC_MSG_RESULT([NSS])
elif test x"$PORTNAME" = x"win32" ; then
AC_MSG_RESULT([Windows native])
else
Expand All @@ -2260,7 +2334,7 @@ else
if test x"$ac_cv_file__dev_urandom" = x"no" ; then
AC_MSG_ERROR([
no source of strong random numbers was found
PostgreSQL can use OpenSSL, native Windows API or /dev/urandom as a source of random numbers.])
PostgreSQL can use OpenSSL, NSS, native Windows API or /dev/urandom as a source of random numbers.])
fi
fi

Expand Down
4 changes: 4 additions & 0 deletions src/backend/libpq/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ OBJS = \

ifeq ($(with_ssl),openssl)
OBJS += be-secure-openssl.o
else
ifeq ($(with_ssl),nss)
OBJS += be-secure-nss.o
endif
endif

ifeq ($(with_gssapi),yes)
Expand Down
8 changes: 8 additions & 0 deletions src/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,21 @@ OBJS_COMMON += \
cryptohash_openssl.o \
hmac_openssl.o
else
ifeq ($(with_ssl),nss)
OBJS_COMMON += \
hmac.o \
cipher_nss.o \
protocol_nss.o \
cryptohash_nss.o
else
OBJS_COMMON += \
cryptohash.o \
hmac.o \
md5.o \
sha1.o \
sha2.o
endif
endif

# A few files are currently only built for frontend, not server
# (Mkvcbuild.pm has a copy of this list, too). logging.c is excluded
Expand Down
12 changes: 12 additions & 0 deletions src/include/pg_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@
/* Define to 1 if you have the `m' library (-lm). */
#undef HAVE_LIBM

/* Define to 1 if you have the `nspr4' library (-lnspr4). */
#undef HAVE_LIBNSPR4

/* Define to 1 if you have the `nss3' library (-lnss3). */
#undef HAVE_LIBNSS3

/* Define to 1 if you have the `pam' library (-lpam). */
#undef HAVE_LIBPAM

Expand All @@ -352,6 +358,9 @@
/* Define to 1 if you have the `ssl' library (-lssl). */
#undef HAVE_LIBSSL

/* Define to 1 if you have the `ssl3' library (-lssl3). */
#undef HAVE_LIBSSL3

/* Define to 1 if you have the `wldap32' library (-lwldap32). */
#undef HAVE_LIBWLDAP32

Expand Down Expand Up @@ -926,6 +935,9 @@
/* Define to select named POSIX semaphores. */
#undef USE_NAMED_POSIX_SEMAPHORES

/* Define to 1 if you have NSS support, */
#undef USE_NSS

/* Define to 1 to build with OpenSSL support. (--with-ssl=openssl) */
#undef USE_OPENSSL

Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/libpq/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ OBJS += \
fe-secure-openssl.o
endif

ifeq ($(with_ssl), nss)
OBJS += \
fe-secure-nss.o
endif

ifeq ($(with_gssapi),yes)
OBJS += \
fe-gssapi-common.o \
Expand Down
3 changes: 2 additions & 1 deletion src/tools/msvc/Install.pm
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ sub CopyContribFiles
{
# These configuration-based exclusions must match vcregress.pl
next if ($d eq "uuid-ossp" && !defined($config->{uuid}));
next if ($d eq "sslinfo" && !defined($config->{openssl}));
next if ($d eq "sslinfo" && !defined($config->{openssl})
&& !defined($config->{nss}));
next if ($d eq "xml2" && !defined($config->{xml}));
next if ($d =~ /_plperl$/ && !defined($config->{perl}));
next if ($d =~ /_plpython$/ && !defined($config->{python}));
Expand Down
40 changes: 35 additions & 5 deletions src/tools/msvc/Mkvcbuild.pm
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ sub mkvcbuild
push(@pgcommonallfiles, 'hmac_openssl.c');
push(@pgcommonallfiles, 'protocol_openssl.c');
}
elsif ($solution->{options}->{nss})
{
push(@pgcommonallfiles, 'cryptohash_nss.c');
push(@pgcommonallfiles, 'cipher_nss.c');
push(@pgcommonallfiles, 'protocol_nss.c');
}
else
{
push(@pgcommonallfiles, 'cryptohash.c');
Expand Down Expand Up @@ -202,12 +208,19 @@ sub mkvcbuild
$postgres->FullExportDLL('postgres.lib');

# The OBJS scraper doesn't know about ifdefs, so remove appropriate files
# if building without OpenSSL.
if (!$solution->{options}->{openssl})
# if building without various options.
if (!$solution->{options}->{openssl} && !$solution->{options}->{nss})
{
$postgres->RemoveFile('src/backend/libpq/be-secure-common.c');
}
if (!$solution->{options}->{openssl})
{
$postgres->RemoveFile('src/backend/libpq/be-secure-openssl.c');
}
if (!$solution->{options}->{nss})
{
$postgres->RemoveFile('src/backend/libpq/be-secure-nss.c');
}
if (!$solution->{options}->{gss})
{
$postgres->RemoveFile('src/backend/libpq/be-gssapi-common.c');
Expand Down Expand Up @@ -265,12 +278,19 @@ sub mkvcbuild
$libpq->AddReference($libpgcommon, $libpgport);

# The OBJS scraper doesn't know about ifdefs, so remove appropriate files
# if building without OpenSSL.
if (!$solution->{options}->{openssl})
# if building without various options
if (!$solution->{options}->{openssl} && !$solution->{options}->{nss})
{
$libpq->RemoveFile('src/interfaces/libpq/fe-secure-common.c');
}
if (!$solution->{options}->{openssl})
{
$libpq->RemoveFile('src/interfaces/libpq/fe-secure-openssl.c');
}
if (!$solution->{options}->{nss})
{
$libpq->RemoveFile('src/interfaces/libpq/fe-secure-nss.c');
}
if (!$solution->{options}->{gss})
{
$libpq->RemoveFile('src/interfaces/libpq/fe-gssapi-common.c');
Expand Down Expand Up @@ -438,9 +458,14 @@ sub mkvcbuild
push @contrib_excludes, 'xml2';
}

if (!$solution->{options}->{openssl} && !$solution->{options}->{nss})
{
push @contrib_excludes, 'sslinfo';
}

if (!$solution->{options}->{openssl})
{
push @contrib_excludes, 'sslinfo', 'ssl_passphrase_callback';
push @contrib_excludes, 'ssl_passphrase_callback';
}

if (!$solution->{options}->{uuid})
Expand Down Expand Up @@ -470,6 +495,11 @@ sub mkvcbuild
$pgcrypto->AddFiles('contrib/pgcrypto', 'openssl.c',
'pgp-mpi-openssl.c');
}
elsif ($solution->{options}->{nss})
{
$pgcrypto->AddFiles('contrib/pgcrypto', 'nss.c',
'pgp-mpi-internal.c', 'imath.c', 'blf.c');
}
else
{
$pgcrypto->AddFiles(
Expand Down
26 changes: 26 additions & 0 deletions src/tools/msvc/Solution.pm
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,13 @@ sub GenerateFiles
HAVE_LIBLDAP => undef,
HAVE_LIBLZ4 => undef,
HAVE_LIBM => undef,
HAVE_LIBNSPR4 => undef,
HAVE_LIBNSS3 => undef,
HAVE_LIBPAM => undef,
HAVE_LIBREADLINE => undef,
HAVE_LIBSELINUX => undef,
HAVE_LIBSSL => undef,
HAVE_LIBSSL3 => undef,
HAVE_LIBWLDAP32 => undef,
HAVE_LIBXML2 => undef,
HAVE_LIBXSLT => undef,
Expand Down Expand Up @@ -499,6 +502,7 @@ sub GenerateFiles
USE_LLVM => undef,
USE_NAMED_POSIX_SEMAPHORES => undef,
USE_OPENSSL => undef,
USE_NSS => undef,
USE_PAM => undef,
USE_SLICING_BY_8_CRC32C => undef,
USE_SSE42_CRC32C => undef,
Expand Down Expand Up @@ -560,6 +564,13 @@ sub GenerateFiles
$define{HAVE_OPENSSL_INIT_SSL} = 1;
}
}
if ($self->{options}->{nss})
{
$define{USE_NSS} = 1;
$define{HAVE_LIBNSPR4} = 1;
$define{HAVE_LIBNSS3} = 1;
$define{HAVE_LIBSSL3} = 1;
}

$self->GenerateConfigHeader('src/include/pg_config.h', \%define, 1);
$self->GenerateConfigHeader('src/include/pg_config_ext.h', \%define, 0);
Expand Down Expand Up @@ -1019,6 +1030,21 @@ sub AddProject
}
}
}
if ($self->{options}->{nss})
{
$proj->AddIncludeDir($self->{options}->{nss} . '\..\public\nss');
$proj->AddIncludeDir($self->{options}->{nss} . '\include\nspr');
foreach my $lib (qw(plds4 plc4 nspr4))
{
$proj->AddLibrary($self->{options}->{nss} .
'\lib\lib' . "$lib.lib", 0);
}
foreach my $lib (qw(ssl3 smime3 nss3))
{
$proj->AddLibrary($self->{options}->{nss} .
'\lib' . "\\$lib.dll.lib", 0);
}
}
if ($self->{options}->{nls})
{
$proj->AddIncludeDir($self->{options}->{nls} . '\include');
Expand Down

0 comments on commit 4965823

Please sign in to comment.