Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d97ae82

Browse files
committed
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.
1 parent fdbeaaf commit d97ae82

File tree

5 files changed

+71
-75
lines changed

5 files changed

+71
-75
lines changed

src/include/port.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/port.h,v 1.68 2004/12/31 22:03:19 pgsql Exp $
9+
* $PostgreSQL: pgsql/src/include/port.h,v 1.69 2005/01/06 00:59:25 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -98,12 +98,6 @@ extern int find_other_exec(const char *argv0, const char *target,
9898
#define SYSTEMQUOTE ""
9999
#endif
100100

101-
#if defined(WIN32) && !defined(__CYGWIN__)
102-
#define HOMEDIR "USERPROFILE"
103-
#else
104-
#define HOMEDIR "HOME"
105-
#endif
106-
107101
/* Portable delay handling */
108102
extern void pg_usleep(long microsec);
109103

src/interfaces/libpq/fe-connect.c

Lines changed: 41 additions & 25 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.295 2005/01/04 23:18:25 tgl Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.296 2005/01/06 00:59:47 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3175,8 +3175,7 @@ static char *
31753175
PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
31763176
{
31773177
FILE *fp;
3178-
char *pgpassfile;
3179-
char *home;
3178+
char pgpassfile[MAXPGPATH];
31803179
struct stat stat_buf;
31813180

31823181
#define LINELEN NAMEDATALEN*5
@@ -3194,32 +3193,16 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
31943193
if (port == NULL)
31953194
port = DEF_PGPORT_STR;
31963195

3197-
/*
3198-
* Look for it in the home dir. We don't use get_home_path() so we
3199-
* don't pull path.c into our library.
3200-
*/
3201-
if (!(home = getenv(HOMEDIR)))
3196+
if (!pqGetHomeDirectory(pgpassfile, sizeof(pgpassfile)))
32023197
return NULL;
32033198

3204-
pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
3205-
if (!pgpassfile)
3206-
{
3207-
fprintf(stderr, libpq_gettext("out of memory\n"));
3208-
return NULL;
3209-
}
3210-
3211-
#ifndef WIN32
3212-
sprintf(pgpassfile, "%s/%s", home, PGPASSFILE);
3213-
#else
3214-
sprintf(pgpassfile, "%s\\%s", home, PGPASSFILE);
3215-
#endif
3199+
snprintf(pgpassfile + strlen(pgpassfile),
3200+
sizeof(pgpassfile) - strlen(pgpassfile),
3201+
"/%s", PGPASSFILE);
32163202

32173203
/* If password file cannot be opened, ignore it. */
32183204
if (stat(pgpassfile, &stat_buf) == -1)
3219-
{
3220-
free(pgpassfile);
32213205
return NULL;
3222-
}
32233206

32243207
#ifndef WIN32
32253208
/* If password file is insecure, alert the user and ignore it. */
@@ -3228,13 +3211,11 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
32283211
fprintf(stderr,
32293212
libpq_gettext("WARNING: Password file %s has world or group read access; permission should be u=rw (0600)\n"),
32303213
pgpassfile);
3231-
free(pgpassfile);
32323214
return NULL;
32333215
}
32343216
#endif
32353217

32363218
fp = fopen(pgpassfile, "r");
3237-
free(pgpassfile);
32383219
if (fp == NULL)
32393220
return NULL;
32403221

@@ -3270,6 +3251,41 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
32703251
#undef LINELEN
32713252
}
32723253

3254+
/*
3255+
* Obtain user's home directory, return in given buffer
3256+
*
3257+
* This is essentially the same as get_home_path(), but we don't use that
3258+
* because we don't want to pull path.c into libpq (it pollutes application
3259+
* namespace)
3260+
*/
3261+
bool
3262+
pqGetHomeDirectory(char *buf, int bufsize)
3263+
{
3264+
#ifndef WIN32
3265+
char pwdbuf[BUFSIZ];
3266+
struct passwd pwdstr;
3267+
struct passwd *pwd = NULL;
3268+
3269+
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
3270+
return false;
3271+
StrNCpy(buf, pwd->pw_dir, bufsize);
3272+
return true;
3273+
3274+
#else
3275+
3276+
/* TEMPORARY PLACEHOLDER IMPLEMENTATION */
3277+
const char *homedir;
3278+
3279+
homedir = getenv("USERPROFILE");
3280+
if (homedir == NULL)
3281+
homedir = getenv("HOME");
3282+
if (homedir == NULL)
3283+
return false;
3284+
StrNCpy(buf, homedir, bufsize);
3285+
return true;
3286+
#endif
3287+
}
3288+
32733289
/*
32743290
* To keep the API consistent, the locking stubs are always provided, even
32753291
* if they are not required.

src/interfaces/libpq/fe-secure.c

Lines changed: 2 additions & 31 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.62 2005/01/04 23:18:25 tgl Exp $
14+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-secure.c,v 1.63 2005/01/06 00:59:47 tgl Exp $
1515
*
1616
* NOTES
1717
* [ Most of these notes are wrong/obsolete, but perhaps not all ]
@@ -107,6 +107,7 @@
107107
#endif
108108
#include <arpa/inet.h>
109109
#endif
110+
#include <sys/stat.h>
110111

111112
#ifdef ENABLE_THREAD_SAFETY
112113
#include <pthread.h>
@@ -116,11 +117,6 @@
116117
#include "strdup.h"
117118
#endif
118119

119-
#ifndef WIN32
120-
#include <pwd.h>
121-
#endif
122-
#include <sys/stat.h>
123-
124120
#ifdef USE_SSL
125121
#include <openssl/ssl.h>
126122
#include <openssl/dh.h>
@@ -493,31 +489,6 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
493489
/* ------------------------------------------------------------ */
494490
#ifdef USE_SSL
495491

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-
521492
/*
522493
* Certificate verification callback
523494
*

src/interfaces/libpq/libpq-int.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.99 2004/12/31 22:03:50 pgsql Exp $
15+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.100 2005/01/06 00:59:47 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -377,6 +377,7 @@ extern char *const pgresStatus[];
377377

378378
extern int pqPacketSend(PGconn *conn, char pack_type,
379379
const void *buf, size_t buf_len);
380+
extern bool pqGetHomeDirectory(char *buf, int bufsize);
380381

381382
#ifdef ENABLE_THREAD_SAFETY
382383
extern pgthreadlock_t pg_g_threadlock;

src/port/path.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/port/path.c,v 1.46 2004/12/31 22:03:53 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/port/path.c,v 1.47 2005/01/06 01:00:12 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
1515

1616
#include "c.h"
1717

1818
#include <ctype.h>
19+
#include <sys/stat.h>
20+
#ifndef WIN32
21+
#include <unistd.h>
22+
#endif
1923

2024
#include "pg_config_paths.h"
2125

@@ -445,19 +449,29 @@ get_locale_path(const char *my_exec_path, char *ret_path)
445449
bool
446450
get_home_path(char *ret_path)
447451
{
448-
const char *homedir = getenv(HOMEDIR);
452+
#ifndef WIN32
453+
char pwdbuf[BUFSIZ];
454+
struct passwd pwdstr;
455+
struct passwd *pwd = NULL;
456+
457+
if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pwd) != 0)
458+
return false;
459+
StrNCpy(ret_path, pwd->pw_dir, MAXPGPATH);
460+
return true;
461+
462+
#else
463+
464+
/* TEMPORARY PLACEHOLDER IMPLEMENTATION */
465+
const char *homedir;
449466

467+
homedir = getenv("USERPROFILE");
468+
if (homedir == NULL)
469+
homedir = getenv("HOME");
450470
if (homedir == NULL)
451-
{
452-
*ret_path = '\0';
453471
return false;
454-
}
455-
else
456-
{
457-
StrNCpy(ret_path, homedir, MAXPGPATH);
458-
canonicalize_path(ret_path);
459-
return true;
460-
}
472+
StrNCpy(ret_path, homedir, MAXPGPATH);
473+
return true;
474+
#endif
461475
}
462476

463477

0 commit comments

Comments
 (0)