Skip to content

Commit 6daf396

Browse files
committed
Add thread locking to SSL and Kerberos connections.
I have removed the docs mentioning that SSL and Kerberos are not thread-safe. Manfred Spraul
1 parent fcfa2c7 commit 6daf396

File tree

7 files changed

+222
-46
lines changed

7 files changed

+222
-46
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.149 2004/03/23 23:37:17 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.150 2004/03/24 03:44:58 momjian Exp $
33
-->
44

55
<chapter id="libpq">
@@ -3654,8 +3654,7 @@ call <function>fe_setauthsvc</function> at all.
36543654
<literal>crypt()</literal> operating system function, which is often
36553655
not thread-safe.<indexterm><primary>crypt</><secondary>thread
36563656
safety</></> It is better to use the <literal>md5</literal> method,
3657-
which is thread-safe on all platforms. <application>SSL</> connections
3658-
and <application>kerberos</> authentication are also not thread-safe.
3657+
which is thread-safe on all platforms.
36593658
</para>
36603659

36613660
<para>

src/backend/libpq/md5.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/libpq/md5.c,v 1.22 2003/11/29 19:51:49 pgsql Exp $
17+
* $PostgreSQL: pgsql/src/backend/libpq/md5.c,v 1.23 2004/03/24 03:44:58 momjian Exp $
1818
*/
1919

2020

@@ -271,7 +271,7 @@ calculateDigestFromBuffer(uint8 *b, uint32 len, uint8 sum[16])
271271
static void
272272
bytesToHex(uint8 b[16], char *s)
273273
{
274-
static char *hex = "0123456789abcdef";
274+
static const char *hex = "0123456789abcdef";
275275
int q,
276276
w;
277277

src/interfaces/libpq/fe-auth.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.89 2004/01/07 18:56:29 neilc Exp $
13+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.90 2004/03/24 03:44:59 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -590,15 +590,18 @@ fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname,
590590

591591
case AUTH_REQ_KRB4:
592592
#ifdef KRB4
593+
pglock_thread();
593594
if (pg_krb4_sendauth(PQerrormsg, conn->sock,
594595
(struct sockaddr_in *) & conn->laddr.addr,
595596
(struct sockaddr_in *) & conn->raddr.addr,
596597
hostname) != STATUS_OK)
597598
{
598599
snprintf(PQerrormsg, PQERRORMSG_LENGTH,
599600
libpq_gettext("Kerberos 4 authentication failed\n"));
601+
pgunlock_thread();
600602
return STATUS_ERROR;
601603
}
604+
pgunlock_thread();
602605
break;
603606
#else
604607
snprintf(PQerrormsg, PQERRORMSG_LENGTH,
@@ -608,13 +611,16 @@ fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname,
608611

609612
case AUTH_REQ_KRB5:
610613
#ifdef KRB5
614+
pglock_thread();
611615
if (pg_krb5_sendauth(PQerrormsg, conn->sock,
612616
hostname) != STATUS_OK)
613617
{
614618
snprintf(PQerrormsg, PQERRORMSG_LENGTH,
615619
libpq_gettext("Kerberos 5 authentication failed\n"));
620+
pgunlock_thread();
616621
return STATUS_ERROR;
617622
}
623+
pgunlock_thread();
618624
break;
619625
#else
620626
snprintf(PQerrormsg, PQERRORMSG_LENGTH,
@@ -722,6 +728,7 @@ fe_getauthname(char *PQerrormsg)
722728
if (authsvc == 0)
723729
return NULL; /* leave original error message in place */
724730

731+
pglock_thread();
725732
#ifdef KRB4
726733
if (authsvc == STARTUP_KRB4_MSG)
727734
name = pg_krb4_authname(PQerrormsg);
@@ -759,5 +766,6 @@ fe_getauthname(char *PQerrormsg)
759766

760767
if (name && (authn = (char *) malloc(strlen(name) + 1)))
761768
strcpy(authn, name);
769+
pgunlock_thread();
762770
return authn;
763771
}

src/interfaces/libpq/fe-connect.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.268 2004/03/10 21:12:47 momjian Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.269 2004/03/24 03:44:59 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2902,7 +2902,7 @@ int
29022902
PQsetClientEncoding(PGconn *conn, const char *encoding)
29032903
{
29042904
char qbuf[128];
2905-
static char query[] = "set client_encoding to '%s'";
2905+
static const char query[] = "set client_encoding to '%s'";
29062906
PGresult *res;
29072907
int status;
29082908

@@ -3164,3 +3164,44 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
31643164
#undef LINELEN
31653165
}
31663166

3167+
/*
3168+
* To keep the API consistent, the locking stubs are always provided, even
3169+
* if they are not required.
3170+
*/
3171+
3172+
void
3173+
PQinitSSL(int do_init)
3174+
{
3175+
#ifdef USE_SSL
3176+
pq_initssllib = do_init;
3177+
#endif
3178+
}
3179+
3180+
static pgthreadlock_t default_threadlock;
3181+
static void
3182+
default_threadlock(int acquire)
3183+
{
3184+
#ifdef ENABLE_THREAD_SAFETY
3185+
static pthread_mutex_t singlethread_lock = PTHREAD_MUTEX_INITIALIZER;
3186+
if (acquire)
3187+
pthread_mutex_lock(&singlethread_lock);
3188+
else
3189+
pthread_mutex_unlock(&singlethread_lock);
3190+
#endif
3191+
}
3192+
3193+
pgthreadlock_t *g_threadlock = default_threadlock;
3194+
3195+
pgthreadlock_t *
3196+
PQregisterThreadLock(pgthreadlock_t *newhandler)
3197+
{
3198+
pgthreadlock_t *prev;
3199+
3200+
prev = g_threadlock;
3201+
if (newhandler)
3202+
g_threadlock = newhandler;
3203+
else
3204+
g_threadlock = default_threadlock;
3205+
return prev;
3206+
}
3207+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy