Commit d97ae823 authored by Tom Lane's avatar Tom Lane

Make the various places that determine the user's "home directory"

consistent.  On Unix we now always consult getpwuid(); $HOME isn't used
at all.  On Windows the code currently consults $USERPROFILE, or $HOME
if that's not defined, but I expect this will change as soon as the win32
hackers come to a consensus.  Nothing done yet about changing the file
names used underneath $USERPROFILE.
parent fdbeaaf9
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/port.h,v 1.68 2004/12/31 22:03:19 pgsql Exp $ * $PostgreSQL: pgsql/src/include/port.h,v 1.69 2005/01/06 00:59:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -98,12 +98,6 @@ extern int find_other_exec(const char *argv0, const char *target, ...@@ -98,12 +98,6 @@ extern int find_other_exec(const char *argv0, const char *target,
#define SYSTEMQUOTE "" #define SYSTEMQUOTE ""
#endif #endif
#if defined(WIN32) && !defined(__CYGWIN__)
#define HOMEDIR "USERPROFILE"
#else
#define HOMEDIR "HOME"
#endif
/* Portable delay handling */ /* Portable delay handling */
extern void pg_usleep(long microsec); extern void pg_usleep(long microsec);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.295 2005/01/04 23:18:25 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.296 2005/01/06 00:59:47 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -3175,8 +3175,7 @@ static char * ...@@ -3175,8 +3175,7 @@ static char *
PasswordFromFile(char *hostname, char *port, char *dbname, char *username) PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
{ {
FILE *fp; FILE *fp;
char *pgpassfile; char pgpassfile[MAXPGPATH];
char *home;
struct stat stat_buf; struct stat stat_buf;
#define LINELEN NAMEDATALEN*5 #define LINELEN NAMEDATALEN*5
...@@ -3194,32 +3193,16 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username) ...@@ -3194,32 +3193,16 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
if (port == NULL) if (port == NULL)
port = DEF_PGPORT_STR; port = DEF_PGPORT_STR;
/* if (!pqGetHomeDirectory(pgpassfile, sizeof(pgpassfile)))
* Look for it in the home dir. We don't use get_home_path() so we
* don't pull path.c into our library.
*/
if (!(home = getenv(HOMEDIR)))
return NULL; return NULL;
pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1); snprintf(pgpassfile + strlen(pgpassfile),
if (!pgpassfile) sizeof(pgpassfile) - strlen(pgpassfile),
{ "/%s", PGPASSFILE);
fprintf(stderr, libpq_gettext("out of memory\n"));
return NULL;
}
#ifndef WIN32
sprintf(pgpassfile, "%s/%s", home, PGPASSFILE);
#else
sprintf(pgpassfile, "%s\\%s", home, PGPASSFILE);
#endif
/* If password file cannot be opened, ignore it. */ /* If password file cannot be opened, ignore it. */
if (stat(pgpassfile, &stat_buf) == -1) if (stat(pgpassfile, &stat_buf) == -1)
{
free(pgpassfile);
return NULL; return NULL;
}
#ifndef WIN32 #ifndef WIN32
/* If password file is insecure, alert the user and ignore it. */ /* If password file is insecure, alert the user and ignore it. */
...@@ -3228,13 +3211,11 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username) ...@@ -3228,13 +3211,11 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
fprintf(stderr, fprintf(stderr,
libpq_gettext("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)\n"), libpq_gettext("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)\n"),
pgpassfile); pgpassfile);
free(pgpassfile);
return NULL; return NULL;
} }
#endif #endif
fp = fopen(pgpassfile, "r"); fp = fopen(pgpassfile, "r");
free(pgpassfile);
if (fp == NULL) if (fp == NULL)
return NULL; return NULL;
...@@ -3270,6 +3251,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username) ...@@ -3270,6 +3251,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
#undef LINELEN #undef LINELEN
} }
/*
* Obtain user's home directory, return in given buffer
*
* This is essentially the same as get_home_path(), but we don't use that
* because we don't want to pull path.c into libpq (it pollutes application
* namespace)
*/
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
/* TEMPORARY PLACEHOLDER IMPLEMENTATION */
const char *homedir;
homedir = getenv("USERPROFILE");
if (homedir == NULL)
homedir = getenv("HOME");
if (homedir == NULL)
return false;
StrNCpy(buf, homedir, bufsize);
return true;
#endif
}
/* /*
* To keep the API consistent, the locking stubs are always provided, even * To keep the API consistent, the locking stubs are always provided, even
* if they are not required. * if they are not required.
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.62 2005/01/04 23:18:25 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.63 2005/01/06 00:59:47 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 ]
...@@ -107,6 +107,7 @@ ...@@ -107,6 +107,7 @@
#endif #endif
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#include <sys/stat.h>
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
#include <pthread.h> #include <pthread.h>
...@@ -116,11 +117,6 @@ ...@@ -116,11 +117,6 @@
#include "strdup.h" #include "strdup.h"
#endif #endif
#ifndef WIN32
#include <pwd.h>
#endif
#include <sys/stat.h>
#ifdef USE_SSL #ifdef USE_SSL
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/dh.h> #include <openssl/dh.h>
...@@ -493,31 +489,6 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len) ...@@ -493,31 +489,6 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
#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
* *
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.99 2004/12/31 22:03:50 pgsql Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.100 2005/01/06 00:59:47 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -377,6 +377,7 @@ extern char *const pgresStatus[]; ...@@ -377,6 +377,7 @@ extern char *const pgresStatus[];
extern int pqPacketSend(PGconn *conn, char pack_type, extern int pqPacketSend(PGconn *conn, char pack_type,
const void *buf, size_t buf_len); const void *buf, size_t buf_len);
extern bool pqGetHomeDirectory(char *buf, int bufsize);
#ifdef ENABLE_THREAD_SAFETY #ifdef ENABLE_THREAD_SAFETY
extern pgthreadlock_t pg_g_threadlock; extern pgthreadlock_t pg_g_threadlock;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/port/path.c,v 1.46 2004/12/31 22:03:53 pgsql Exp $ * $PostgreSQL: pgsql/src/port/path.c,v 1.47 2005/01/06 01:00:12 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
#include "c.h" #include "c.h"
#include <ctype.h> #include <ctype.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "pg_config_paths.h" #include "pg_config_paths.h"
...@@ -445,19 +449,29 @@ get_locale_path(const char *my_exec_path, char *ret_path) ...@@ -445,19 +449,29 @@ get_locale_path(const char *my_exec_path, char *ret_path)
bool bool
get_home_path(char *ret_path) get_home_path(char *ret_path)
{ {
const char *homedir = getenv(HOMEDIR); #ifndef WIN32
char pwdbuf[BUFSIZ];
struct passwd pwdstr;
struct passwd *pwd = NULL;
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
return false;
StrNCpy(ret_path, pwd->pw_dir, MAXPGPATH);
return true;
#else
/* TEMPORARY PLACEHOLDER IMPLEMENTATION */
const char *homedir;
homedir = getenv("USERPROFILE");
if (homedir == NULL)
homedir = getenv("HOME");
if (homedir == NULL) if (homedir == NULL)
{
*ret_path = '\0';
return false; return false;
} StrNCpy(ret_path, homedir, MAXPGPATH);
else return true;
{ #endif
StrNCpy(ret_path, homedir, MAXPGPATH);
canonicalize_path(ret_path);
return true;
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment