Make the various places that determine the user's "home directory"
authorTom Lane <[email protected]>
Thu, 6 Jan 2005 01:00:12 +0000 (01:00 +0000)
committerTom Lane <[email protected]>
Thu, 6 Jan 2005 01:00:12 +0000 (01:00 +0000)
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.

src/include/port.h
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/fe-secure.c
src/interfaces/libpq/libpq-int.h
src/port/path.c

index 9a73045a07bea8566ea22fe41c3419555a0de02c..c1db902647f46c61b97e964549bece9c0e38fee0 100644 (file)
@@ -98,12 +98,6 @@ extern int find_other_exec(const char *argv0, const char *target,
 #define SYSTEMQUOTE ""
 #endif
 
-#if defined(WIN32) && !defined(__CYGWIN__)
-#define HOMEDIR "USERPROFILE"
-#else
-#define HOMEDIR "HOME"
-#endif
-
 /* Portable delay handling */
 extern void pg_usleep(long microsec);
 
index 0cdc51895c10cbe7b94317a60dda936f577591e5..f213c8cd48abbc8839887e1c4a1d80690bf95c1d 100644 (file)
@@ -3175,8 +3175,7 @@ static char *
 PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
 {
        FILE       *fp;
-       char       *pgpassfile;
-       char       *home;
+       char            pgpassfile[MAXPGPATH];
        struct stat stat_buf;
 
 #define LINELEN NAMEDATALEN*5
@@ -3194,32 +3193,16 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
        if (port == NULL)
                port = DEF_PGPORT_STR;
 
-       /*
-        * 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)))
+       if (!pqGetHomeDirectory(pgpassfile, sizeof(pgpassfile)))
                return NULL;
 
-       pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
-       if (!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
+       snprintf(pgpassfile + strlen(pgpassfile),
+                        sizeof(pgpassfile) - strlen(pgpassfile),
+                        "/%s", PGPASSFILE);
 
        /* If password file cannot be opened, ignore it. */
        if (stat(pgpassfile, &stat_buf) == -1)
-       {
-               free(pgpassfile);
                return NULL;
-       }
 
 #ifndef WIN32
        /* If password file is insecure, alert the user and ignore it. */
@@ -3228,13 +3211,11 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
                fprintf(stderr,
                                libpq_gettext("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)\n"),
                                pgpassfile);
-               free(pgpassfile);
                return NULL;
        }
 #endif
 
        fp = fopen(pgpassfile, "r");
-       free(pgpassfile);
        if (fp == NULL)
                return NULL;
 
@@ -3270,6 +3251,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
 #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
  * if they are not required.
index bc3011a30233c6762975aedf61a27fb36c6d1c02..0ae0837a98c3dfcaa6ab703604831e27eb38dc6b 100644 (file)
 #endif
 #include <arpa/inet.h>
 #endif
+#include <sys/stat.h>
 
 #ifdef ENABLE_THREAD_SAFETY
 #include <pthread.h>
 #include "strdup.h"
 #endif
 
-#ifndef WIN32
-#include <pwd.h>
-#endif
-#include <sys/stat.h>
-
 #ifdef USE_SSL
 #include <openssl/ssl.h>
 #include <openssl/dh.h>
@@ -493,31 +489,6 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
 /* ------------------------------------------------------------ */
 #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
  *
index 4b252a6b1d18616be106e2d985d89463d2d4ddeb..407e8c551758b1b7164374bda00ab2a5304fa4cc 100644 (file)
@@ -377,6 +377,7 @@ extern char *const pgresStatus[];
 
 extern int pqPacketSend(PGconn *conn, char pack_type,
                         const void *buf, size_t buf_len);
+extern bool pqGetHomeDirectory(char *buf, int bufsize);
 
 #ifdef ENABLE_THREAD_SAFETY
 extern pgthreadlock_t pg_g_threadlock;
index 3ebd388913c22785f6f97693bbf016c600bdbd44..3e18f5b71436909bc04a7b68f5d940d63f8e3dad 100644 (file)
 #include "c.h"
 
 #include <ctype.h>
+#include <sys/stat.h>
+#ifndef WIN32
+#include <unistd.h>
+#endif
 
 #include "pg_config_paths.h"
 
@@ -445,19 +449,29 @@ get_locale_path(const char *my_exec_path, char *ret_path)
 bool
 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)
-       {
-               *ret_path = '\0';
                return false;
-       }
-       else
-       {
-               StrNCpy(ret_path, homedir, MAXPGPATH);
-               canonicalize_path(ret_path);
-               return true;
-       }
+       StrNCpy(ret_path, homedir, MAXPGPATH);
+       return true;
+#endif
 }