postgresql/src/port
Heikki Linnakangas fe0a0b5993 Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.

pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:

- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom

Unlike the current pgcrypto function, the source is chosen by configure.
That makes it easier to test different implementations, and ensures that
we don't accidentally fall back to a less secure implementation, if the
primary source fails. All of those methods are quite reliable, it would be
pretty surprising for them to fail, so we'd rather find out by failing
hard.

If no strong random source is available, we fall back to using erand48(),
seeded from current timestamp, like PostmasterRandom() was. That isn't
cryptographically secure, but allows us to still work on platforms that
don't have any of the above stronger sources. Because it's not very secure,
the built-in implementation is only used if explicitly requested with
--disable-strong-random.

This replaces the more complicated Fortuna algorithm we used to have in
pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom,
so it doesn't seem worth the maintenance effort to keep that. pgcrypto
functions that require strong random numbers will be disabled with
--disable-strong-random.

Original patch by Magnus Hagander, tons of further work by Michael Paquier
and me.

Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com
Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
2016-12-05 13:42:59 +02:00
..
.gitignore Convert cvsignore to gitignore, and add .gitignore for build targets. 2010-09-22 12:57:04 +02:00
Makefile Replace PostmasterRandom() with a stronger source, second attempt. 2016-12-05 13:42:59 +02:00
README Refer to OS X as "macOS", except for the port name which is still "darwin". 2016-09-25 15:40:57 -04:00
chklocale.c Remove invitation to report a bug about unknown encoding 2016-10-27 18:43:46 -04:00
crypt.c Suppress dead, unportable src/port/crypt.c code. 2014-10-12 23:27:06 -04:00
dirent.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
dirmod.c pgwin32_is_junction's argument should be "const char *" not "char *". 2016-11-05 11:14:10 -04:00
erand48.c Replace PostmasterRandom() with a stronger source, second attempt. 2016-12-05 13:42:59 +02:00
fls.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
fseeko.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
getaddrinfo.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
getopt.c pgindent run for 9.4 2014-05-06 12:12:18 -04:00
getopt_long.c Add support for optional_argument to our own getopt_long() implementation. 2014-09-10 17:21:50 +02:00
getpeereid.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
getrusage.c Remove "sco" and "unixware" ports. 2016-10-11 11:26:04 -04:00
gettimeofday.c pgindent run for 9.5 2015-05-23 21:35:49 -04:00
inet_aton.c pgindent run for 9.4 2014-05-06 12:12:18 -04:00
inet_net_ntop.c Pgindent run before 9.1 beta2. 2011-06-09 14:32:50 -04:00
isinf.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
kill.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
mkdtemp.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
noblock.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
open.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
path.c Suppress GCC 6 warning about self-comparison 2016-03-08 19:41:51 -05:00
pg_crc32c_choose.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
pg_crc32c_sb8.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
pg_crc32c_sse42.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
pg_strong_random.c Replace PostmasterRandom() with a stronger source, second attempt. 2016-12-05 13:42:59 +02:00
pgcheckdir.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
pgmkdirp.c Fix various typos and grammar errors in comments. 2015-04-26 18:42:31 +02:00
pgsleep.c Include <sys/select.h> where needed 2016-09-27 01:05:21 -03:00
pgstrcasecmp.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
pqsignal.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
pthread-win32.h Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
qsort.c Fix a low-probability crash in our qsort implementation. 2015-07-16 22:57:46 -04:00
qsort_arg.c Fix a low-probability crash in our qsort implementation. 2015-07-16 22:57:46 -04:00
quotes.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
random.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
rint.c Upgrade src/port/rint.c to be POSIX-compliant. 2015-03-25 15:54:18 -04:00
snprintf.c Revert error-throwing wrappers for the printf family of functions. 2015-05-19 18:19:38 -04:00
sprompt.c Simplify correct use of simple_prompt(). 2016-08-30 17:02:02 -04:00
srandom.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
strerror.c Remove cvs keywords from all files. 2010-09-20 22:08:53 +02:00
strlcat.c pgindent run for 9.4 2014-05-06 12:12:18 -04:00
strlcpy.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
system.c Promote pg_dumpall shell/connstr quoting functions to src/fe_utils. 2016-08-08 10:07:46 -04:00
tar.c Allow pg_basebackup to stream transaction log in tar mode 2016-10-23 15:23:11 +02:00
thread.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
unsetenv.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
win32.ico Revert binary change in copyright year adjustment. 2012-01-01 19:40:13 -05:00
win32env.c Make pgwin32_putenv() probe every known CRT, regardless of compiler. 2016-12-04 00:16:54 -05:00
win32error.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
win32security.c Re-add translation markers that were lost 2016-09-20 12:00:00 -04:00
win32setlocale.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
win32ver.rc Stamp HEAD as 10devel. 2016-08-15 13:49:49 -04:00

README

src/port/README

libpgport
=========

libpgport must have special behavior.  It supplies functions to both
libraries and applications.  However, there are two complexities:

1)  Libraries need to use object files that are compiled with exactly
the same flags as the library.  libpgport might not use the same flags,
so it is necessary to recompile the object files for individual
libraries.  This is done by removing -lpgport from the link line:

        # Need to recompile any libpgport object files
        LIBS := $(filter-out -lpgport, $(LIBS))

and adding infrastructure to recompile the object files:

        OBJS= execute.o typename.o descriptor.o data.o error.o prepare.o memory.o \
                connect.o misc.o path.o exec.o \
                $(filter snprintf.o, $(LIBOBJS))

The problem is that there is no testing of which object files need to be
added, but missing functions usually show up when linking user
applications.

2) For applications, we use -lpgport before -lpq, so the static files
from libpgport are linked first.  This avoids having applications
dependent on symbols that are _used_ by libpq, but not intended to be
exported by libpq.  libpq's libpgport usage changes over time, so such a
dependency is a problem.  Windows, Linux, and macOS use an export list to
control the symbols exported by libpq.