Skip to content

Commit edf3832

Browse files
committed
Clean up code in libpq that obtains user's home directory: make a single
subroutine that can hide platform dependencies. The WIN32 path is still a stub, but I await a fix from one of the win32 hackers. Also clean up unnecessary #ifdef WIN32 ugliness in a couple of places.
1 parent d877de9 commit edf3832

File tree

3 files changed

+68
-62
lines changed

3 files changed

+68
-62
lines changed

src/interfaces/libpq/fe-auth.c

Lines changed: 15 additions & 13 deletions
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.97 2004/12/31 22:03:50 pgsql Exp $
13+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.98 2005/01/04 23:18:25 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -718,8 +718,16 @@ char *
718718
fe_getauthname(char *PQerrormsg)
719719
{
720720
const char *name = NULL;
721-
char *authn = NULL;
721+
char *authn;
722722
MsgType authsvc;
723+
#ifdef WIN32
724+
char username[128];
725+
DWORD namesize = sizeof(username) - 1;
726+
#else
727+
char pwdbuf[BUFSIZ];
728+
struct passwd pwdstr;
729+
struct passwd *pw = NULL;
730+
#endif
723731

724732
authsvc = fe_getauthsvc(PQerrormsg);
725733

@@ -728,6 +736,7 @@ fe_getauthname(char *PQerrormsg)
728736
return NULL; /* leave original error message in place */
729737

730738
pglock_thread();
739+
731740
#ifdef KRB4
732741
if (authsvc == STARTUP_KRB4_MSG)
733742
name = pg_krb4_authname(PQerrormsg);
@@ -742,18 +751,10 @@ fe_getauthname(char *PQerrormsg)
742751
|| (authsvc == STARTUP_KRB5_MSG && !name))
743752
{
744753
#ifdef WIN32
745-
char username[128];
746-
DWORD namesize = sizeof(username) - 1;
747-
748754
if (GetUserName(username, &namesize))
749755
name = username;
750756
#else
751-
char pwdbuf[BUFSIZ];
752-
struct passwd pwdstr;
753-
struct passwd *pw = NULL;
754-
755-
if (pqGetpwuid(geteuid(), &pwdstr,
756-
pwdbuf, sizeof(pwdbuf), &pw) == 0)
757+
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pw) == 0)
757758
name = pw->pw_name;
758759
#endif
759760
}
@@ -763,8 +764,9 @@ fe_getauthname(char *PQerrormsg)
763764
libpq_gettext("fe_getauthname: invalid authentication system: %d\n"),
764765
authsvc);
765766

766-
if (name && (authn = (char *) malloc(strlen(name) + 1)))
767-
strcpy(authn, name);
767+
authn = name ? strdup(name) : NULL;
768+
768769
pgunlock_thread();
770+
769771
return authn;
770772
}

src/interfaces/libpq/fe-connect.c

Lines changed: 4 additions & 14 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.294 2004/12/31 22:03:50 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.295 2005/01/04 23:18:25 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1944,7 +1944,7 @@ makeEmptyPGconn(void)
19441944
PGconn *conn;
19451945

19461946
#ifdef WIN32
1947-
/* needed to use the static libpq under windows as well */
1947+
/* make sure socket support is up and running */
19481948
WSADATA wsaData;
19491949

19501950
if (WSAStartup(MAKEWORD(1, 1), &wsaData))
@@ -2324,12 +2324,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
23242324

23252325
/* All done */
23262326
closesocket(tmpsock);
2327-
#ifdef WIN32
2328-
WSASetLastError(save_errno);
2329-
#else
2330-
errno = save_errno;
2331-
#endif
2332-
2327+
SOCK_ERRNO_SET(save_errno);
23332328
return TRUE;
23342329

23352330
cancel_errReturn:
@@ -2346,12 +2341,7 @@ internal_cancel(SockAddr *raddr, int be_pid, int be_key,
23462341
}
23472342
if (tmpsock >= 0)
23482343
closesocket(tmpsock);
2349-
#ifdef WIN32
2350-
WSASetLastError(save_errno);
2351-
#else
2352-
errno = save_errno;
2353-
#endif
2354-
2344+
SOCK_ERRNO_SET(save_errno);
23552345
return FALSE;
23562346
}
23572347

src/interfaces/libpq/fe-secure.c

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.61 2004/12/31 22:03:50 pgsql Exp $
14+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.62 2005/01/04 23:18:25 tgl Exp $
1515
*
1616
* NOTES
1717
* [ Most of these notes are wrong/obsolete, but perhaps not all ]
@@ -492,6 +492,32 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
492492
/* SSL specific code */
493493
/* ------------------------------------------------------------ */
494494
#ifdef USE_SSL
495+
496+
/*
497+
* Obtain user's home directory, return in given buffer
498+
*
499+
* This code isn't really SSL-specific, but currently we only need it in
500+
* SSL-related places.
501+
*/
502+
static bool
503+
pqGetHomeDirectory(char *buf, int bufsize)
504+
{
505+
#ifndef WIN32
506+
char pwdbuf[BUFSIZ];
507+
struct passwd pwdstr;
508+
struct passwd *pwd = NULL;
509+
510+
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
511+
return false;
512+
StrNCpy(buf, pwd->pw_dir, bufsize);
513+
return true;
514+
515+
#else
516+
517+
return false; /* PLACEHOLDER */
518+
#endif
519+
}
520+
495521
/*
496522
* Certificate verification callback
497523
*
@@ -612,7 +638,7 @@ verify_peer(PGconn *conn)
612638

613639
return -1;
614640
}
615-
#endif
641+
#endif /* NOT_USED */
616642

617643
/*
618644
* Load precomputed DH parameters.
@@ -624,23 +650,18 @@ verify_peer(PGconn *conn)
624650
static DH *
625651
load_dh_file(int keylength)
626652
{
627-
#ifdef WIN32
628-
return NULL;
629-
#else
630-
char pwdbuf[BUFSIZ];
631-
struct passwd pwdstr;
632-
struct passwd *pwd = NULL;
633-
FILE *fp;
653+
char homedir[MAXPGPATH];
634654
char fnbuf[MAXPGPATH];
635-
DH *dh = NULL;
655+
FILE *fp;
656+
DH *dh;
636657
int codes;
637658

638-
if (pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
659+
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
639660
return NULL;
640661

641662
/* attempt to open file. It's not an error if it doesn't exist. */
642663
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/dh%d.pem",
643-
pwd->pw_dir, keylength);
664+
homedir, keylength);
644665

645666
if ((fp = fopen(fnbuf, "r")) == NULL)
646667
return NULL;
@@ -667,7 +688,6 @@ load_dh_file(int keylength)
667688
}
668689

669690
return dh;
670-
#endif
671691
}
672692

673693
/*
@@ -771,12 +791,7 @@ tmp_dh_cb(SSL *s, int is_export, int keylength)
771791
static int
772792
client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
773793
{
774-
#ifdef WIN32
775-
return 0;
776-
#else
777-
char pwdbuf[BUFSIZ];
778-
struct passwd pwdstr;
779-
struct passwd *pwd = NULL;
794+
char homedir[MAXPGPATH];
780795
struct stat buf,
781796
buf2;
782797
char fnbuf[MAXPGPATH];
@@ -785,7 +800,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
785800
int (*cb) () = NULL; /* how to read user password */
786801
char sebuf[256];
787802

788-
if (pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
803+
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
789804
{
790805
printfPQExpBuffer(&conn->errorMessage,
791806
libpq_gettext("could not get user information\n"));
@@ -794,7 +809,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
794809

795810
/* read the user certificate */
796811
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/postgresql.crt",
797-
pwd->pw_dir);
812+
homedir);
798813
if ((fp = fopen(fnbuf, "r")) == NULL)
799814
{
800815
printfPQExpBuffer(&conn->errorMessage,
@@ -817,7 +832,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
817832

818833
/* read the user key */
819834
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/postgresql.key",
820-
pwd->pw_dir);
835+
homedir);
821836
if (stat(fnbuf, &buf) == -1)
822837
{
823838
printfPQExpBuffer(&conn->errorMessage,
@@ -873,7 +888,6 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
873888
}
874889

875890
return 1;
876-
#endif
877891
}
878892

879893
#ifdef ENABLE_THREAD_SAFETY
@@ -885,6 +899,7 @@ pq_threadidcallback(void)
885899
}
886900

887901
static pthread_mutex_t *pq_lockarray;
902+
888903
static void
889904
pq_lockingcallback(int mode, int n, const char *file, int line)
890905
{
@@ -893,6 +908,7 @@ pq_lockingcallback(int mode, int n, const char *file, int line)
893908
else
894909
pthread_mutex_unlock(&pq_lockarray[n]);
895910
}
911+
896912
#endif /* ENABLE_THREAD_SAFETY */
897913

898914
static int
@@ -969,23 +985,17 @@ init_ssl_system(PGconn *conn)
969985
static int
970986
initialize_SSL(PGconn *conn)
971987
{
972-
#ifndef WIN32
973988
struct stat buf;
974-
char pwdbuf[BUFSIZ];
975-
struct passwd pwdstr;
976-
struct passwd *pwd = NULL;
989+
char homedir[MAXPGPATH];
977990
char fnbuf[MAXPGPATH];
978-
#endif
979991

980992
if (init_ssl_system(conn))
981993
return -1;
982994

983-
#ifndef WIN32
984995
/* Set up to verify server cert, if root.crt is present */
985-
if (pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0)
996+
if (pqGetHomeDirectory(homedir, sizeof(homedir)))
986997
{
987-
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/root.crt",
988-
pwd->pw_dir);
998+
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/root.crt", homedir);
989999
if (stat(fnbuf, &buf) == 0)
9901000
{
9911001
if (!SSL_CTX_load_verify_locations(SSL_context, fnbuf, NULL))
@@ -1009,7 +1019,6 @@ initialize_SSL(PGconn *conn)
10091019

10101020
/* set up mechanism to provide client certificate, if available */
10111021
SSL_CTX_set_client_cert_cb(SSL_context, client_cert_cb);
1012-
#endif
10131022

10141023
return 0;
10151024
}
@@ -1232,15 +1241,19 @@ PQgetssl(PGconn *conn)
12321241
return NULL;
12331242
return conn->ssl;
12341243
}
1235-
#else
1244+
1245+
#else /* !USE_SSL */
1246+
12361247
void *
12371248
PQgetssl(PGconn *conn)
12381249
{
12391250
return NULL;
12401251
}
1252+
12411253
#endif /* USE_SSL */
12421254

12431255
#ifdef ENABLE_THREAD_SAFETY
1256+
12441257
/*
12451258
* Block SIGPIPE for this thread. This prevents send()/write() from exiting
12461259
* the application.
@@ -1322,4 +1335,5 @@ pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
13221335

13231336
SOCK_ERRNO_SET(save_errno);
13241337
}
1325-
#endif
1338+
1339+
#endif /* ENABLE_THREAD_SAFETY */

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