Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e710b65

Browse files
committed
Remove the use of the pg_auth flat file for client authentication.
(That flat file is now completely useless, but removal will come later.) To do this, postpone client authentication into the startup transaction that's run by InitPostgres. We still collect the startup packet and do SSL initialization (if needed) at the same time we did before. The AuthenticationTimeout is applied separately to startup packet collection and the actual authentication cycle. (This is a bit annoying, since it means a couple extra syscalls; but the signal handling requirements inside and outside a transaction are sufficiently different that it seems best to treat the timeouts as completely independent.) A small security disadvantage is that if the given database name is invalid, this will be reported to the client before any authentication happens. We could work around that by connecting to database "postgres" instead, but consensus seems to be that it's not worth introducing such surprising behavior. Processing of all command-line switches and GUC options received from the client is now postponed until after authentication. This means that PostAuthDelay is much less useful than it used to be --- if you need to investigate problems during InitPostgres you'll have to set PreAuthDelay instead. However, allowing an unauthenticated user to set any GUC options whatever seems a bit too risky, so we'll live with that.
1 parent 585806c commit e710b65

File tree

15 files changed

+534
-603
lines changed

15 files changed

+534
-603
lines changed

src/backend/libpq/auth.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/libpq/auth.c,v 1.183 2009/06/25 11:30:08 mha Exp $
11+
* $PostgreSQL: pgsql/src/backend/libpq/auth.c,v 1.184 2009/08/29 19:26:51 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -33,8 +33,10 @@
3333
#include "libpq/ip.h"
3434
#include "libpq/libpq.h"
3535
#include "libpq/pqformat.h"
36+
#include "miscadmin.h"
3637
#include "storage/ipc.h"
3738

39+
3840
/*----------------------------------------------------------------
3941
* Global authentication functions
4042
*----------------------------------------------------------------
@@ -281,6 +283,15 @@ ClientAuthentication(Port *port)
281283
errmsg("missing or erroneous pg_hba.conf file"),
282284
errhint("See server log for details.")));
283285

286+
/*
287+
* Enable immediate response to SIGTERM/SIGINT/timeout interrupts.
288+
* (We don't want this during hba_getauthmethod() because it might
289+
* have to do database access, eg for role membership checks.)
290+
*/
291+
ImmediateInterruptOK = true;
292+
/* And don't forget to detect one that already arrived */
293+
CHECK_FOR_INTERRUPTS();
294+
284295
/*
285296
* This is the first point where we have access to the hba record for the
286297
* current connection, so perform any verifications based on the hba
@@ -458,6 +469,9 @@ ClientAuthentication(Port *port)
458469
sendAuthRequest(port, AUTH_REQ_OK);
459470
else
460471
auth_failed(port, status);
472+
473+
/* Done with authentication, so we should turn off immediate interrupts */
474+
ImmediateInterruptOK = false;
461475
}
462476

463477

@@ -690,9 +704,6 @@ pg_krb5_recvauth(Port *port)
690704
char *kusername;
691705
char *cp;
692706

693-
if (get_role_line(port->user_name) == NULL)
694-
return STATUS_ERROR;
695-
696707
ret = pg_krb5_init(port);
697708
if (ret != STATUS_OK)
698709
return ret;
@@ -1823,9 +1834,6 @@ authident(hbaPort *port)
18231834
{
18241835
char ident_user[IDENT_USERNAME_MAX + 1];
18251836

1826-
if (get_role_line(port->user_name) == NULL)
1827-
return STATUS_ERROR;
1828-
18291837
switch (port->raddr.addr.ss_family)
18301838
{
18311839
case AF_INET:

src/backend/libpq/crypt.c

+49-34
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $PostgreSQL: pgsql/src/backend/libpq/crypt.c,v 1.77 2009/01/01 17:23:42 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/libpq/crypt.c,v 1.78 2009/08/29 19:26:51 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -20,38 +20,63 @@
2020
#include <crypt.h>
2121
#endif
2222

23+
#include "catalog/pg_authid.h"
2324
#include "libpq/crypt.h"
2425
#include "libpq/md5.h"
26+
#include "miscadmin.h"
27+
#include "utils/builtins.h"
28+
#include "utils/syscache.h"
2529

2630

2731
int
2832
md5_crypt_verify(const Port *port, const char *role, char *client_pass)
2933
{
30-
char *shadow_pass = NULL,
31-
*valuntil = NULL,
32-
*crypt_pwd;
3334
int retval = STATUS_ERROR;
34-
List **line;
35-
ListCell *token;
35+
char *shadow_pass,
36+
*crypt_pwd;
37+
TimestampTz vuntil = 0;
3638
char *crypt_client_pass = client_pass;
39+
HeapTuple roleTup;
40+
Datum datum;
41+
bool isnull;
42+
43+
/*
44+
* Disable immediate interrupts while doing database access. (Note
45+
* we don't bother to turn this back on if we hit one of the failure
46+
* conditions, since we can expect we'll just exit right away anyway.)
47+
*/
48+
ImmediateInterruptOK = false;
3749

38-
if ((line = get_role_line(role)) == NULL)
39-
return STATUS_ERROR;
50+
/* Get role info from pg_authid */
51+
roleTup = SearchSysCache(AUTHNAME,
52+
PointerGetDatum(role),
53+
0, 0, 0);
54+
if (!HeapTupleIsValid(roleTup))
55+
return STATUS_ERROR; /* no such user */
4056

41-
/* Skip over rolename */
42-
token = list_head(*line);
43-
if (token)
44-
token = lnext(token);
45-
if (token)
57+
datum = SysCacheGetAttr(AUTHNAME, roleTup,
58+
Anum_pg_authid_rolpassword, &isnull);
59+
if (isnull)
4660
{
47-
shadow_pass = (char *) lfirst(token);
48-
token = lnext(token);
49-
if (token)
50-
valuntil = (char *) lfirst(token);
61+
ReleaseSysCache(roleTup);
62+
return STATUS_ERROR; /* user has no password */
5163
}
64+
shadow_pass = TextDatumGetCString(datum);
65+
66+
datum = SysCacheGetAttr(AUTHNAME, roleTup,
67+
Anum_pg_authid_rolvaliduntil, &isnull);
68+
if (!isnull)
69+
vuntil = DatumGetTimestampTz(datum);
5270

53-
if (shadow_pass == NULL || *shadow_pass == '\0')
54-
return STATUS_ERROR;
71+
ReleaseSysCache(roleTup);
72+
73+
if (*shadow_pass == '\0')
74+
return STATUS_ERROR; /* empty password */
75+
76+
/* Re-enable immediate response to SIGTERM/SIGINT/timeout interrupts */
77+
ImmediateInterruptOK = true;
78+
/* And don't forget to detect one that already arrived */
79+
CHECK_FOR_INTERRUPTS();
5580

5681
/*
5782
* Compare with the encrypted or plain password depending on the
@@ -119,24 +144,14 @@ md5_crypt_verify(const Port *port, const char *role, char *client_pass)
119144
if (strcmp(crypt_client_pass, crypt_pwd) == 0)
120145
{
121146
/*
122-
* Password OK, now check to be sure we are not past valuntil
147+
* Password OK, now check to be sure we are not past rolvaliduntil
123148
*/
124-
if (valuntil == NULL || *valuntil == '\0')
149+
if (isnull)
125150
retval = STATUS_OK;
151+
else if (vuntil < GetCurrentTimestamp())
152+
retval = STATUS_ERROR;
126153
else
127-
{
128-
TimestampTz vuntil;
129-
130-
vuntil = DatumGetTimestampTz(DirectFunctionCall3(timestamptz_in,
131-
CStringGetDatum(valuntil),
132-
ObjectIdGetDatum(InvalidOid),
133-
Int32GetDatum(-1)));
134-
135-
if (vuntil < GetCurrentTimestamp())
136-
retval = STATUS_ERROR;
137-
else
138-
retval = STATUS_OK;
139-
}
154+
retval = STATUS_OK;
140155
}
141156

142157
if (port->hba->auth_method == uaMD5)

0 commit comments

Comments
 (0)