Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitedf3832

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 stilla 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 parentd877de9 commitedf3832

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
constchar*name=NULL;
721-
char*authn=NULL;
721+
char*authn;
722722
MsgTypeauthsvc;
723+
#ifdefWIN32
724+
charusername[128];
725+
DWORDnamesize=sizeof(username)-1;
726+
#else
727+
charpwdbuf[BUFSIZ];
728+
structpasswdpwdstr;
729+
structpasswd*pw=NULL;
730+
#endif
723731

724732
authsvc=fe_getauthsvc(PQerrormsg);
725733

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

730738
pglock_thread();
739+
731740
#ifdefKRB4
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
#ifdefWIN32
745-
charusername[128];
746-
DWORDnamesize=sizeof(username)-1;
747-
748754
if (GetUserName(username,&namesize))
749755
name=username;
750756
#else
751-
charpwdbuf[BUFSIZ];
752-
structpasswdpwdstr;
753-
structpasswd*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
returnauthn;
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
#ifdefWIN32
1947-
/*needed to use the static libpq under windows as well */
1947+
/*make sure socket support is up and running */
19481948
WSADATAwsaData;
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-
#ifdefWIN32
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-
#ifdefWIN32
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
#ifdefUSE_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+
staticbool
503+
pqGetHomeDirectory(char*buf,intbufsize)
504+
{
505+
#ifndefWIN32
506+
charpwdbuf[BUFSIZ];
507+
structpasswdpwdstr;
508+
structpasswd*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
staticDH*
625651
load_dh_file(intkeylength)
626652
{
627-
#ifdefWIN32
628-
returnNULL;
629-
#else
630-
charpwdbuf[BUFSIZ];
631-
structpasswdpwdstr;
632-
structpasswd*pwd=NULL;
633-
FILE*fp;
653+
charhomedir[MAXPGPATH];
634654
charfnbuf[MAXPGPATH];
635-
DH*dh=NULL;
655+
FILE*fp;
656+
DH*dh;
636657
intcodes;
637658

638-
if (pqGetpwuid(getuid(),&pwdstr,pwdbuf,sizeof(pwdbuf),&pwd)!=0)
659+
if (!pqGetHomeDirectory(homedir,sizeof(homedir)))
639660
returnNULL;
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
returnNULL;
@@ -667,7 +688,6 @@ load_dh_file(int keylength)
667688
}
668689

669690
returndh;
670-
#endif
671691
}
672692

673693
/*
@@ -771,12 +791,7 @@ tmp_dh_cb(SSL *s, int is_export, int keylength)
771791
staticint
772792
client_cert_cb(SSL*ssl,X509**x509,EVP_PKEY**pkey)
773793
{
774-
#ifdefWIN32
775-
return0;
776-
#else
777-
charpwdbuf[BUFSIZ];
778-
structpasswdpwdstr;
779-
structpasswd*pwd=NULL;
794+
charhomedir[MAXPGPATH];
780795
structstatbuf,
781796
buf2;
782797
charfnbuf[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
charsebuf[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
return1;
876-
#endif
877891
}
878892

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

887901
staticpthread_mutex_t*pq_lockarray;
902+
888903
staticvoid
889904
pq_lockingcallback(intmode,intn,constchar*file,intline)
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
staticint
@@ -969,23 +985,17 @@ init_ssl_system(PGconn *conn)
969985
staticint
970986
initialize_SSL(PGconn*conn)
971987
{
972-
#ifndefWIN32
973988
structstatbuf;
974-
charpwdbuf[BUFSIZ];
975-
structpasswdpwdstr;
976-
structpasswd*pwd=NULL;
989+
charhomedir[MAXPGPATH];
977990
charfnbuf[MAXPGPATH];
978-
#endif
979991

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

983-
#ifndefWIN32
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
return0;
10151024
}
@@ -1232,15 +1241,19 @@ PQgetssl(PGconn *conn)
12321241
returnNULL;
12331242
returnconn->ssl;
12341243
}
1235-
#else
1244+
1245+
#else/* !USE_SSL */
1246+
12361247
void*
12371248
PQgetssl(PGconn*conn)
12381249
{
12391250
returnNULL;
12401251
}
1252+
12411253
#endif/* USE_SSL */
12421254

12431255
#ifdefENABLE_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)

[8]ページ先頭

©2009-2025 Movatter.jp