Commit graph

3311 commits

Author SHA1 Message Date
Tom Lane
c9256f5b7d Improve libpq's error recovery for connection loss during COPY.
In pqSendSome, if the connection is already closed at entry, discard any
queued output data before returning.  There is no possibility of ever
sending the data, and anyway this corresponds to what we'd do if we'd
detected a hard error while trying to send().  This avoids possible
indefinite bloat of the output buffer if the application keeps trying
to send data (or even just keeps trying to do PQputCopyEnd, as psql
indeed will).

Because PQputCopyEnd won't transition out of PGASYNC_COPY_IN state
until it's successfully queued the COPY END message, and pqPutMsgEnd
doesn't distinguish a queuing failure from a pqSendSome failure,
this omission allowed an infinite loop in psql if the connection closure
occurred when we had at least 8K queued to send.  It might be worth
refactoring so that we can make that distinction, but for the moment
the other changes made here seem to offer adequate defenses.

To guard against other variants of this scenario, do not allow
PQgetResult to return a PGRES_COPY_XXX result if the connection is
already known dead.  Make sure it returns PGRES_FATAL_ERROR instead.

Per report from Stephen Frost.  Back-patch to all active branches.
2014-02-12 17:50:23 -05:00
Andrew Dunstan
55c018896f Fix makefile syntax. 2014-02-01 19:51:37 -05:00
Andrew Dunstan
ae3c98b9b1 Copy the libpq DLL to the bin directory on Mingw and Cygwin.
This has long been done by the MSVC build system, and has caused
confusion in the past when programs like psql have failed to start
because they can't find the DLL. If it's in the same directory as it now
will be they will find it.

Backpatch to all live branches.
2014-02-01 15:16:52 -05:00
Fujii Masao
5525529db1 Fix bugs in PQhost().
In the platform that doesn't support Unix-domain socket, when
neither host nor hostaddr are specified, the default host
'localhost' is used to connect to the server and PQhost() must
return that, but it didn't. This patch fixes PQhost() so that
it returns the default host in that case.

Also this patch fixes PQhost() so that it doesn't return
Unix-domain socket directory path in the platform that doesn't
support Unix-domain socket.

Back-patch to all supported versions.
2014-01-23 23:02:30 +09:00
Michael Meskes
d68a65b013 Fix descriptor output in ECPG.
While working on most platforms the old way sometimes created alignment
problems. This should fix it. Also the regresion tests were updated to test for
the reported case.

Report and fix by MauMau <maumau307@gmail.com>

Conflicts:
	src/interfaces/ecpg/preproc/type.c
	src/interfaces/ecpg/test/expected/sql-desc.c
2014-01-09 15:58:37 +01:00
Michael Meskes
96de4939c0 Do not use an empty hostname.
When trying to connect to a given database libecpg should not try using an
empty hostname if no hostname was given.
2014-01-01 12:44:58 +01:00
Tom Lane
be26515fc3 Stamp 8.4.19. 2013-12-02 16:06:31 -05:00
Michael Meskes
3b41a7c74a ECPG: Make the preprocessor emit ';' if the variable type for a list of
variables is varchar. This fixes this test case:

int main(void)
{
    exec sql begin declare section;
    varchar a[50], b[50];
    exec sql end declare section;

    return 0;
}

Since varchars are internally turned into custom structs and
the type name is emitted for these variable declarations,
the preprocessed code previously had:

struct varchar_1  { ... }  a _,_  struct varchar_2  { ... }  b ;

The comma in the generated C file was a syntax error.

There are no regression test changes since it's not exercised.

Patch by Boszormenyi Zoltan <zb@cybertec.at>

Conflicts:
	src/interfaces/ecpg/preproc/ecpg.trailer
2013-11-26 17:30:24 +01:00
Michael Meskes
f142c27a32 ECPG: Fix offset to NULL/size indicator array.
Patch by Boszormenyi Zoltan <zb@cybertec.at>
2013-11-26 17:25:42 +01:00
Michael Meskes
5f8f8cbec5 Changed test case slightly so it doesn't have an unused typedef. 2013-11-03 15:41:57 +01:00
Peter Eisentraut
e6a8de2854 Stamp 8.4.18. 2013-10-07 23:10:18 -04:00
Stephen Frost
d6dc44a879 Fix SSL deadlock risk in libpq
In libpq, we set up and pass to OpenSSL callback routines to handle
locking.  When we run out of SSL connections, we try to clean things
up by de-registering the hooks.  Unfortunately, we had a few calls
into the OpenSSL library after these hooks were de-registered during
SSL cleanup which lead to deadlocking.  This moves the thread callback
cleanup to be after all SSL-cleanup related OpenSSL library calls.
I've been unable to reproduce the deadlock with this fix.

In passing, also move the close_SSL call to be after unlocking our
ssl_config mutex when in a failure state.  While it looks pretty
unlikely to be an issue, it could have resulted in deadlocks if we
ended up in this code path due to something other than SSL_new
failing.  Thanks to Heikki for pointing this out.

Back-patch to all supported versions; note that the close_SSL issue
only goes back to 9.0, so that hunk isn't included in the 8.4 patch.

Initially found and reported by Vesa-Matti J Kari; many thanks to
both Heikki and Andres for their help running down the specific
issue and reviewing the patch.
2013-09-23 08:48:47 -04:00
Michael Meskes
83ec831368 Return error if allocation of new element was not possible.
Found by Coverity.
2013-09-08 13:14:45 +02:00
Alvaro Herrera
0883c85334 Check for NULL result from strdup
Per Coverity Scan
2013-07-23 17:38:31 -04:00
Michael Meskes
d29130bd0f Initialize day of year value.
There are cases where the day of year value in struct tm is used, but it never
got calculated. Problem found by Coverity scan.
2013-07-19 09:04:39 +02:00
Michael Meskes
636c55b2ec Also escape double quotes for ECPG's #line statement. 2013-07-06 22:12:52 +02:00
Michael Meskes
434943d576 Applied patch by MauMau <maumau307@gmail.com> to escape filenames in #line statements. 2013-07-05 11:15:42 +02:00
Tom Lane
158e3199ef Expect EWOULDBLOCK from a non-blocking connect() call only on Windows.
On Unix-ish platforms, EWOULDBLOCK may be the same as EAGAIN, which is
*not* a success return, at least not on Linux.  We need to treat it as a
failure to avoid giving a misleading error message.  Per the Single Unix
Spec, only EINPROGRESS and EINTR returns indicate that the connection
attempt is in progress.

On Windows, on the other hand, EWOULDBLOCK (WSAEWOULDBLOCK) is the expected
case.  We must accept EINPROGRESS as well because Cygwin will return that,
and it doesn't seem worth distinguishing Cygwin from native Windows here.
It's not very clear whether EINTR can occur on Windows, but let's leave
that part of the logic alone in the absence of concrete trouble reports.

Also, remove the test for errno == 0, effectively reverting commit
da9501bddb, which AFAICS was just a thinko;
or at best it might have been a workaround for a platform-specific bug,
which we can hope is gone now thirteen years later.  In any case, since
libpq makes no effort to reset errno to zero before calling connect(),
it seems unlikely that that test has ever reliably done anything useful.

Andres Freund and Tom Lane
2013-06-27 12:37:50 -04:00
Tom Lane
292f7b2726 Stamp 8.4.17. 2013-04-01 14:27:59 -04:00
Alvaro Herrera
a8e18f3bbf Translation updates 2013-03-31 14:50:55 -03:00
Tom Lane
cf67814054 Fix overflow check in tm2timestamp (this time for sure).
I fixed this code back in commit 841b4a2d5, but didn't think carefully
enough about the behavior near zero, which meant it improperly rejected
1999-12-31 24:00:00.  Per report from Magnus Hagander.
2013-03-04 15:14:28 -05:00
Tom Lane
2228ce6a11 Stamp 8.4.16. 2013-02-04 16:28:55 -05:00
Peter Eisentraut
bae4b9a233 Translation updates 2013-02-03 23:53:08 -05:00
Michael Meskes
060e2435f2 Made ecpglib use translated messages.
Bug reported and fixed by Chen Huajun <chenhj@cn.fujitsu.com>.
2013-01-27 13:53:22 +01:00
Tom Lane
0191095950 Fix one-byte buffer overrun in PQprintTuples().
This bug goes back to the original Postgres95 sources.  Its significance
to modern PG versions is marginal, since we have not used PQprintTuples()
internally in a very long time, and it doesn't seem to have ever been
documented either.  Still, it *is* exposed to client apps, so somebody
out there might possibly be using it.

Xi Wang
2013-01-20 23:44:06 -05:00
Tom Lane
965edcab6e Stamp 8.4.15. 2012-12-03 15:25:45 -05:00
Peter Eisentraut
42720826b9 Translation updates 2012-12-03 07:51:22 -05:00
Tom Lane
25b2499d56 Produce a more useful error message for over-length Unix socket paths.
The length of a socket path name is constrained by the size of struct
sockaddr_un, and there's not a lot we can do about it since that is a
kernel API.  However, it would be a good thing if we produced an
intelligible error message when the user specifies a socket path that's too
long --- and getaddrinfo's standard API is too impoverished to do this in
the natural way.  So insert explicit tests at the places where we construct
a socket path name.  Now you'll get an error that makes sense and even
tells you what the limit is, rather than something generic like
"Non-recoverable failure in name resolution".

Per trouble report from Jeremy Drake and a fix idea from Andrew Dunstan.
2012-11-29 19:57:33 -05:00
Michael Meskes
f22a6edad8 When processing nested structure pointer variables ecpg always expected an
array datatype which of course is wrong.

Applied patch by Muhammad Usama <m.usama@gmail.com> to fix this.
2012-11-29 17:15:52 +01:00
Tom Lane
cd07b5a07f Fix lo_import and lo_export to return useful error messages more often.
I found that these functions tend to return -1 while leaving an empty error
message string in the PGconn, if they suffer some kind of I/O error on the
file.  The reason is that lo_close, which thinks it's executed a perfectly
fine SQL command, clears the errorMessage.  The minimum-change workaround
is to reorder operations here so that we don't fill the errorMessage until
after lo_close.
2012-10-08 21:52:58 -04:00
Tom Lane
ed23de2692 Stamp 8.4.14. 2012-09-19 17:55:55 -04:00
Tom Lane
1a36a773a6 Stamp 8.4.13. 2012-08-14 18:45:54 -04:00
Peter Eisentraut
87050703be Translation updates 2012-08-14 16:28:09 -04:00
Peter Eisentraut
32d42fa248 Update copyright year in forgotten places
found by Stefan Kaltenbrunner
2012-06-19 21:33:36 +03:00
Tom Lane
8c9f2dc670 Stamp 8.4.12. 2012-05-31 19:11:47 -04:00
Peter Eisentraut
d871821e47 Translation updates 2012-05-31 23:22:50 +03:00
Tom Lane
0209a10bf8 Stamp 8.4.11. 2012-02-23 17:59:21 -05:00
Tom Lane
4ee9db0cd9 Remove arbitrary limitation on length of common name in SSL certificates.
Both libpq and the backend would truncate a common name extracted from a
certificate at 32 bytes.  Replace that fixed-size buffer with dynamically
allocated string so that there is no hard limit.  While at it, remove the
code for extracting peer_dn, which we weren't using for anything; and
don't bother to store peer_cn longer than we need it in libpq.

This limit was not so terribly unreasonable when the code was written,
because we weren't using the result for anything critical, just logging it.
But now that there are options for checking the common name against the
server host name (in libpq) or using it as the user's name (in the server),
this could result in undesirable failures.  In the worst case it even seems
possible to spoof a server name or user name, if the correct name is
exactly 32 bytes and the attacker can persuade a trusted CA to issue a
certificate in which that string is a prefix of the certificate's common
name.  (To exploit this for a server name, he'd also have to send the
connection astray via phony DNS data or some such.)  The case that this is
a realistic security threat is a bit thin, but nonetheless we'll treat it
as one.

Back-patch to 8.4.  Older releases contain the faulty code, but it's not
a security problem because the common name wasn't used for anything
interesting.

Reported and patched by Heikki Linnakangas

Security: CVE-2012-0867
2012-02-23 15:48:21 -05:00
Peter Eisentraut
94e2495ded Translation updates 2012-02-23 20:33:03 +02:00
Michael Meskes
972e27b2e2 In ecpg removed old leftover check for given connection name.
Ever since we introduced real prepared statements this should work for
different connections. The old solution just emulating prepared statements,
though, wasn't able to handle this.

Closes: #6309
2011-12-18 18:46:23 +01:00
Tom Lane
d08b64581f Stamp 8.4.10. 2011-12-01 16:53:13 -05:00
Peter Eisentraut
3208c64591 Translation updates 2011-12-01 22:47:51 +02:00
Tom Lane
07d306785e Stamp 8.4.9. 2011-09-22 18:03:52 -04:00
Peter Eisentraut
16961f9d68 Translation updates 2011-09-22 22:31:27 +03:00
Peter Eisentraut
c10d1de419 Add missing format argument to ecpg_log() call 2011-09-08 22:10:56 +03:00
Michael Meskes
007e7acab2 Fix brace indentation of commit a6d72ac344 to fit PostgreSQL style. 2011-09-02 09:46:12 +02:00
Michael Meskes
a6d72ac344 In ecpglib restore LC_NUMERIC in case of an error. 2011-09-01 15:35:09 +02:00
Tom Lane
427ba0e714 Don't assume that "E" response to NEGOTIATE_SSL_CODE means pre-7.0 server.
These days, such a response is far more likely to signify a server-side
problem, such as fork failure.  Reporting "server does not support SSL"
(in sslmode=require) could be quite misleading.  But the results could
be even worse in sslmode=prefer: if the problem was transient and the
next connection attempt succeeds, we'll have silently fallen back to
protocol version 2.0, possibly disabling features the user needs.

Hence, it seems best to just eliminate the assumption that backing off
to non-SSL/2.0 protocol is the way to recover from an "E" response, and
instead treat the server error the same as we would in non-SSL cases.

I tested this change against a pre-7.0 server, and found that there
was a second logic bug in the "prefer" path: the test to decide whether
to make a fallback connection attempt assumed that we must have opened
conn->ssl, which in fact does not happen given an "E" response.  After
fixing that, the code does indeed connect successfully to pre-7.0,
as long as you didn't set sslmode=require.  (If you did, you get
"Unsupported frontend protocol", which isn't completely off base
given the server certainly doesn't support SSL.)

Since there seems no reason to believe that pre-7.0 servers exist anymore
in the wild, back-patch to all supported branches.
2011-08-27 16:37:12 -04:00
Tom Lane
c68561cbea Ensure we discard unread/unsent data when abandoning a connection attempt.
There are assorted situations wherein PQconnectPoll() will abandon a
connection attempt and try again with different parameters (eg, SSL versus
not SSL).  However, the code forgot to discard any pending data in libpq's
I/O buffers when doing this.  In at least one case (server returns E
message during SSL negotiation), there is unread input data which bollixes
the next connection attempt.  I have not checked to see whether this is
possible in the other cases where we close the socket and retry, but it
seems like a matter of good defensive programming to add explicit
buffer-flushing code to all of them.

This is one of several issues exposed by Daniel Farina's report of
misbehavior after a server-side fork failure.

This has been wrong since forever, so back-patch to all supported branches.
2011-08-27 14:16:30 -04:00
Tom Lane
16976b026a Fix previous patch so it also works if not USE_SSL (mea culpa).
On balance, the need to cover this case changes my mind in favor of pushing
all error-message generation duties into the two fe-secure.c routines.
So do it that way.
2011-07-24 23:29:21 -04:00