mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
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.
This commit is contained in:
@ -10,7 +10,7 @@
|
|||||||
* exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
|
* exceed INITIAL_EXPBUFFER_SIZE (currently 256 bytes).
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.97 2004/12/31 22:03:50 pgsql Exp $
|
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-auth.c,v 1.98 2005/01/04 23:18:25 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -718,8 +718,16 @@ char *
|
|||||||
fe_getauthname(char *PQerrormsg)
|
fe_getauthname(char *PQerrormsg)
|
||||||
{
|
{
|
||||||
const char *name = NULL;
|
const char *name = NULL;
|
||||||
char *authn = NULL;
|
char *authn;
|
||||||
MsgType authsvc;
|
MsgType authsvc;
|
||||||
|
#ifdef WIN32
|
||||||
|
char username[128];
|
||||||
|
DWORD namesize = sizeof(username) - 1;
|
||||||
|
#else
|
||||||
|
char pwdbuf[BUFSIZ];
|
||||||
|
struct passwd pwdstr;
|
||||||
|
struct passwd *pw = NULL;
|
||||||
|
#endif
|
||||||
|
|
||||||
authsvc = fe_getauthsvc(PQerrormsg);
|
authsvc = fe_getauthsvc(PQerrormsg);
|
||||||
|
|
||||||
@ -728,6 +736,7 @@ fe_getauthname(char *PQerrormsg)
|
|||||||
return NULL; /* leave original error message in place */
|
return NULL; /* leave original error message in place */
|
||||||
|
|
||||||
pglock_thread();
|
pglock_thread();
|
||||||
|
|
||||||
#ifdef KRB4
|
#ifdef KRB4
|
||||||
if (authsvc == STARTUP_KRB4_MSG)
|
if (authsvc == STARTUP_KRB4_MSG)
|
||||||
name = pg_krb4_authname(PQerrormsg);
|
name = pg_krb4_authname(PQerrormsg);
|
||||||
@ -742,18 +751,10 @@ fe_getauthname(char *PQerrormsg)
|
|||||||
|| (authsvc == STARTUP_KRB5_MSG && !name))
|
|| (authsvc == STARTUP_KRB5_MSG && !name))
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
char username[128];
|
|
||||||
DWORD namesize = sizeof(username) - 1;
|
|
||||||
|
|
||||||
if (GetUserName(username, &namesize))
|
if (GetUserName(username, &namesize))
|
||||||
name = username;
|
name = username;
|
||||||
#else
|
#else
|
||||||
char pwdbuf[BUFSIZ];
|
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pw) == 0)
|
||||||
struct passwd pwdstr;
|
|
||||||
struct passwd *pw = NULL;
|
|
||||||
|
|
||||||
if (pqGetpwuid(geteuid(), &pwdstr,
|
|
||||||
pwdbuf, sizeof(pwdbuf), &pw) == 0)
|
|
||||||
name = pw->pw_name;
|
name = pw->pw_name;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -763,8 +764,9 @@ fe_getauthname(char *PQerrormsg)
|
|||||||
libpq_gettext("fe_getauthname: invalid authentication system: %d\n"),
|
libpq_gettext("fe_getauthname: invalid authentication system: %d\n"),
|
||||||
authsvc);
|
authsvc);
|
||||||
|
|
||||||
if (name && (authn = (char *) malloc(strlen(name) + 1)))
|
authn = name ? strdup(name) : NULL;
|
||||||
strcpy(authn, name);
|
|
||||||
pgunlock_thread();
|
pgunlock_thread();
|
||||||
|
|
||||||
return authn;
|
return authn;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.294 2004/12/31 22:03:50 pgsql Exp $
|
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.295 2005/01/04 23:18:25 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -1944,7 +1944,7 @@ makeEmptyPGconn(void)
|
|||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
/* needed to use the static libpq under windows as well */
|
/* make sure socket support is up and running */
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
|
|
||||||
if (WSAStartup(MAKEWORD(1, 1), &wsaData))
|
if (WSAStartup(MAKEWORD(1, 1), &wsaData))
|
||||||
@ -2324,12 +2324,7 @@ retry5:
|
|||||||
|
|
||||||
/* All done */
|
/* All done */
|
||||||
closesocket(tmpsock);
|
closesocket(tmpsock);
|
||||||
#ifdef WIN32
|
SOCK_ERRNO_SET(save_errno);
|
||||||
WSASetLastError(save_errno);
|
|
||||||
#else
|
|
||||||
errno = save_errno;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
cancel_errReturn:
|
cancel_errReturn:
|
||||||
@ -2346,12 +2341,7 @@ cancel_errReturn:
|
|||||||
}
|
}
|
||||||
if (tmpsock >= 0)
|
if (tmpsock >= 0)
|
||||||
closesocket(tmpsock);
|
closesocket(tmpsock);
|
||||||
#ifdef WIN32
|
SOCK_ERRNO_SET(save_errno);
|
||||||
WSASetLastError(save_errno);
|
|
||||||
#else
|
|
||||||
errno = save_errno;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.61 2004/12/31 22:03:50 pgsql Exp $
|
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.62 2005/01/04 23:18:25 tgl Exp $
|
||||||
*
|
*
|
||||||
* NOTES
|
* NOTES
|
||||||
* [ Most of these notes are wrong/obsolete, but perhaps not all ]
|
* [ 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)
|
|||||||
/* SSL specific code */
|
/* SSL specific code */
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
#ifdef USE_SSL
|
#ifdef USE_SSL
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Obtain user's home directory, return in given buffer
|
||||||
|
*
|
||||||
|
* This code isn't really SSL-specific, but currently we only need it in
|
||||||
|
* SSL-related places.
|
||||||
|
*/
|
||||||
|
static bool
|
||||||
|
pqGetHomeDirectory(char *buf, int bufsize)
|
||||||
|
{
|
||||||
|
#ifndef WIN32
|
||||||
|
char pwdbuf[BUFSIZ];
|
||||||
|
struct passwd pwdstr;
|
||||||
|
struct passwd *pwd = NULL;
|
||||||
|
|
||||||
|
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
|
||||||
|
return false;
|
||||||
|
StrNCpy(buf, pwd->pw_dir, bufsize);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
return false; /* PLACEHOLDER */
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Certificate verification callback
|
* Certificate verification callback
|
||||||
*
|
*
|
||||||
@ -612,7 +638,7 @@ verify_peer(PGconn *conn)
|
|||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif /* NOT_USED */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Load precomputed DH parameters.
|
* Load precomputed DH parameters.
|
||||||
@ -624,23 +650,18 @@ verify_peer(PGconn *conn)
|
|||||||
static DH *
|
static DH *
|
||||||
load_dh_file(int keylength)
|
load_dh_file(int keylength)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
char homedir[MAXPGPATH];
|
||||||
return NULL;
|
|
||||||
#else
|
|
||||||
char pwdbuf[BUFSIZ];
|
|
||||||
struct passwd pwdstr;
|
|
||||||
struct passwd *pwd = NULL;
|
|
||||||
FILE *fp;
|
|
||||||
char fnbuf[MAXPGPATH];
|
char fnbuf[MAXPGPATH];
|
||||||
DH *dh = NULL;
|
FILE *fp;
|
||||||
|
DH *dh;
|
||||||
int codes;
|
int codes;
|
||||||
|
|
||||||
if (pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
|
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/* attempt to open file. It's not an error if it doesn't exist. */
|
/* attempt to open file. It's not an error if it doesn't exist. */
|
||||||
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/dh%d.pem",
|
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/dh%d.pem",
|
||||||
pwd->pw_dir, keylength);
|
homedir, keylength);
|
||||||
|
|
||||||
if ((fp = fopen(fnbuf, "r")) == NULL)
|
if ((fp = fopen(fnbuf, "r")) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -667,7 +688,6 @@ load_dh_file(int keylength)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return dh;
|
return dh;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -771,12 +791,7 @@ tmp_dh_cb(SSL *s, int is_export, int keylength)
|
|||||||
static int
|
static int
|
||||||
client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
char homedir[MAXPGPATH];
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
char pwdbuf[BUFSIZ];
|
|
||||||
struct passwd pwdstr;
|
|
||||||
struct passwd *pwd = NULL;
|
|
||||||
struct stat buf,
|
struct stat buf,
|
||||||
buf2;
|
buf2;
|
||||||
char fnbuf[MAXPGPATH];
|
char fnbuf[MAXPGPATH];
|
||||||
@ -785,7 +800,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
|||||||
int (*cb) () = NULL; /* how to read user password */
|
int (*cb) () = NULL; /* how to read user password */
|
||||||
char sebuf[256];
|
char sebuf[256];
|
||||||
|
|
||||||
if (pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
|
if (!pqGetHomeDirectory(homedir, sizeof(homedir)))
|
||||||
{
|
{
|
||||||
printfPQExpBuffer(&conn->errorMessage,
|
printfPQExpBuffer(&conn->errorMessage,
|
||||||
libpq_gettext("could not get user information\n"));
|
libpq_gettext("could not get user information\n"));
|
||||||
@ -794,7 +809,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
|||||||
|
|
||||||
/* read the user certificate */
|
/* read the user certificate */
|
||||||
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/postgresql.crt",
|
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/postgresql.crt",
|
||||||
pwd->pw_dir);
|
homedir);
|
||||||
if ((fp = fopen(fnbuf, "r")) == NULL)
|
if ((fp = fopen(fnbuf, "r")) == NULL)
|
||||||
{
|
{
|
||||||
printfPQExpBuffer(&conn->errorMessage,
|
printfPQExpBuffer(&conn->errorMessage,
|
||||||
@ -817,7 +832,7 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
|||||||
|
|
||||||
/* read the user key */
|
/* read the user key */
|
||||||
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/postgresql.key",
|
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/postgresql.key",
|
||||||
pwd->pw_dir);
|
homedir);
|
||||||
if (stat(fnbuf, &buf) == -1)
|
if (stat(fnbuf, &buf) == -1)
|
||||||
{
|
{
|
||||||
printfPQExpBuffer(&conn->errorMessage,
|
printfPQExpBuffer(&conn->errorMessage,
|
||||||
@ -873,7 +888,6 @@ client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_THREAD_SAFETY
|
#ifdef ENABLE_THREAD_SAFETY
|
||||||
@ -885,6 +899,7 @@ pq_threadidcallback(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static pthread_mutex_t *pq_lockarray;
|
static pthread_mutex_t *pq_lockarray;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pq_lockingcallback(int mode, int n, const char *file, int line)
|
pq_lockingcallback(int mode, int n, const char *file, int line)
|
||||||
{
|
{
|
||||||
@ -893,6 +908,7 @@ pq_lockingcallback(int mode, int n, const char *file, int line)
|
|||||||
else
|
else
|
||||||
pthread_mutex_unlock(&pq_lockarray[n]);
|
pthread_mutex_unlock(&pq_lockarray[n]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ENABLE_THREAD_SAFETY */
|
#endif /* ENABLE_THREAD_SAFETY */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -969,23 +985,17 @@ init_ssl_system(PGconn *conn)
|
|||||||
static int
|
static int
|
||||||
initialize_SSL(PGconn *conn)
|
initialize_SSL(PGconn *conn)
|
||||||
{
|
{
|
||||||
#ifndef WIN32
|
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
char pwdbuf[BUFSIZ];
|
char homedir[MAXPGPATH];
|
||||||
struct passwd pwdstr;
|
|
||||||
struct passwd *pwd = NULL;
|
|
||||||
char fnbuf[MAXPGPATH];
|
char fnbuf[MAXPGPATH];
|
||||||
#endif
|
|
||||||
|
|
||||||
if (init_ssl_system(conn))
|
if (init_ssl_system(conn))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
#ifndef WIN32
|
|
||||||
/* Set up to verify server cert, if root.crt is present */
|
/* Set up to verify server cert, if root.crt is present */
|
||||||
if (pqGetpwuid(getuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) == 0)
|
if (pqGetHomeDirectory(homedir, sizeof(homedir)))
|
||||||
{
|
{
|
||||||
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/root.crt",
|
snprintf(fnbuf, sizeof(fnbuf), "%s/.postgresql/root.crt", homedir);
|
||||||
pwd->pw_dir);
|
|
||||||
if (stat(fnbuf, &buf) == 0)
|
if (stat(fnbuf, &buf) == 0)
|
||||||
{
|
{
|
||||||
if (!SSL_CTX_load_verify_locations(SSL_context, fnbuf, NULL))
|
if (!SSL_CTX_load_verify_locations(SSL_context, fnbuf, NULL))
|
||||||
@ -1009,7 +1019,6 @@ initialize_SSL(PGconn *conn)
|
|||||||
|
|
||||||
/* set up mechanism to provide client certificate, if available */
|
/* set up mechanism to provide client certificate, if available */
|
||||||
SSL_CTX_set_client_cert_cb(SSL_context, client_cert_cb);
|
SSL_CTX_set_client_cert_cb(SSL_context, client_cert_cb);
|
||||||
#endif
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1232,15 +1241,19 @@ PQgetssl(PGconn *conn)
|
|||||||
return NULL;
|
return NULL;
|
||||||
return conn->ssl;
|
return conn->ssl;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
|
#else /* !USE_SSL */
|
||||||
|
|
||||||
void *
|
void *
|
||||||
PQgetssl(PGconn *conn)
|
PQgetssl(PGconn *conn)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* USE_SSL */
|
#endif /* USE_SSL */
|
||||||
|
|
||||||
#ifdef ENABLE_THREAD_SAFETY
|
#ifdef ENABLE_THREAD_SAFETY
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Block SIGPIPE for this thread. This prevents send()/write() from exiting
|
* Block SIGPIPE for this thread. This prevents send()/write() from exiting
|
||||||
* the application.
|
* the application.
|
||||||
@ -1322,4 +1335,5 @@ pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
|
|||||||
|
|
||||||
SOCK_ERRNO_SET(save_errno);
|
SOCK_ERRNO_SET(save_errno);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
#endif /* ENABLE_THREAD_SAFETY */
|
||||||
|
Reference in New Issue
Block a user