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

Commitfc160b6

Browse files
peterepull[bot]
authored andcommitted
More use of getpwuid_r() directly
Remove src/port/user.c, call getpwuid_r() directly. This reduces somecomplexity and allows better control of the error behavior. Forexample, the old code would in some circumstances silently truncatethe result string, or produce error message strings that the callerwouldn't use.src/port/user.c used to be called src/port/thread.c and containedvarious portability complications to support thread-safety. These areall obsolete, and all but the user-lookup functions have already beenremoved. This patch completes this by also removing the user-lookupfunctions.Also convert src/backend/libpq/auth.c to use getpwuid_r() forthread-safety.Originally, I tried to be overly correct by usingsysconf(_SC_GETPW_R_SIZE_MAX) to get the buffer size for getpwuid_r(),but that doesn't work on FreeBSD. All the OS where I could find thesource code internally use 1024 as the suggested buffer size, so Ijust ended up hardcoding that. The previous code used BUFSIZ, whichis an unrelated constant from stdio.h, so its use seemedinappropriate.Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>Discussion:https://www.postgresql.org/message-id/flat/5f293da9-ceb4-4937-8e52-82c25db8e4d3%40eisentraut.org
1 parent692221f commitfc160b6

File tree

10 files changed

+72
-123
lines changed

10 files changed

+72
-123
lines changed

‎src/backend/libpq/auth.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,10 @@ auth_peer(hbaPort *port)
18571857
uid_tuid;
18581858
gid_tgid;
18591859
#ifndefWIN32
1860+
structpasswdpwbuf;
18601861
structpasswd*pw;
1862+
charbuf[1024];
1863+
intrc;
18611864
intret;
18621865
#endif
18631866

@@ -1876,16 +1879,18 @@ auth_peer(hbaPort *port)
18761879
}
18771880

18781881
#ifndefWIN32
1879-
errno=0;/* clear errno before call */
1880-
pw=getpwuid(uid);
1881-
if (!pw)
1882+
rc=getpwuid_r(uid,&pwbuf,buf,sizeofbuf,&pw);
1883+
if (rc!=0)
1884+
{
1885+
errno=rc;
1886+
ereport(LOG,
1887+
errmsg("could not look up local user ID %ld: %m", (long)uid));
1888+
returnSTATUS_ERROR;
1889+
}
1890+
elseif (!pw)
18821891
{
1883-
intsave_errno=errno;
1884-
18851892
ereport(LOG,
1886-
(errmsg("could not look up local user ID %ld: %s",
1887-
(long)uid,
1888-
save_errno ?strerror(save_errno) :_("user does not exist"))));
1893+
errmsg("local user with ID %ld does not exist", (long)uid));
18891894
returnSTATUS_ERROR;
18901895
}
18911896

‎src/bin/psql/nls.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ GETTEXT_FILES = $(FRONTEND_COMMON_GETTEXT_FILES) \
2323
../../common/exec.c\
2424
../../common/fe_memutils.c\
2525
../../common/username.c\
26-
../../common/wait_error.c\
27-
../../port/user.c
26+
../../common/wait_error.c
2827
GETTEXT_TRIGGERS =$(FRONTEND_COMMON_GETTEXT_TRIGGERS)\
2928
HELP0 HELPN N_ simple_prompt simple_prompt_extended
3029
GETTEXT_FLAGS =$(FRONTEND_COMMON_GETTEXT_FLAGS)\

‎src/include/port.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,6 @@ extern size_t strnlen(const char *str, size_t maxlen);
436436
externchar*strsep(char**stringp,constchar*delim);
437437
#endif
438438

439-
/* port/user.c */
440-
#ifndefWIN32
441-
externboolpg_get_user_name(uid_tuser_id,char*buffer,size_tbuflen);
442-
externboolpg_get_user_home_dir(uid_tuser_id,char*buffer,size_tbuflen);
443-
#endif
444-
445439
/*
446440
* Callers should use the qsort() macro defined below instead of calling
447441
* pg_qsort() directly.

‎src/interfaces/libpq/fe-auth.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include<unistd.h>
2929
#include<fcntl.h>
3030
#include<limits.h>
31+
#include<pwd.h>
3132
#include<sys/param.h>/* for MAXHOSTNAMELEN on most */
3233
#include<sys/socket.h>
3334
#ifdefHAVE_SYS_UCRED_H
@@ -1203,7 +1204,10 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
12031204
charusername[256+1];
12041205
DWORDnamesize=sizeof(username);
12051206
#else
1206-
charpwdbuf[BUFSIZ];
1207+
structpasswdpwbuf;
1208+
structpasswd*pw;
1209+
charbuf[1024];
1210+
intrc;
12071211
#endif
12081212

12091213
#ifdefWIN32
@@ -1214,10 +1218,19 @@ pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
12141218
"user name lookup failure: error code %lu",
12151219
GetLastError());
12161220
#else
1217-
if (pg_get_user_name(user_id,pwdbuf,sizeof(pwdbuf)))
1218-
name=pwdbuf;
1219-
elseif (errorMessage)
1220-
appendPQExpBuffer(errorMessage,"%s\n",pwdbuf);
1221+
rc=getpwuid_r(user_id,&pwbuf,buf,sizeofbuf,&pw);
1222+
if (rc!=0)
1223+
{
1224+
errno=rc;
1225+
if (errorMessage)
1226+
libpq_append_error(errorMessage,"could not look up local user ID %ld: %m", (long)user_id);
1227+
}
1228+
elseif (!pw)
1229+
{
1230+
if (errorMessage)
1231+
libpq_append_error(errorMessage,"local user with ID %ld does not exist", (long)user_id);
1232+
}
1233+
name=pw->pw_name;
12211234
#endif
12221235

12231236
if (name)

‎src/interfaces/libpq/fe-connect.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include<netdb.h>
5151
#include<netinet/in.h>
5252
#include<netinet/tcp.h>
53+
#include<pwd.h>
5354
#endif
5455

5556
#ifdefWIN32
@@ -7702,10 +7703,24 @@ pqGetHomeDirectory(char *buf, int bufsize)
77027703
constchar*home;
77037704

77047705
home=getenv("HOME");
7705-
if (home==NULL||home[0]=='\0')
7706-
returnpg_get_user_home_dir(geteuid(),buf,bufsize);
7707-
strlcpy(buf,home,bufsize);
7708-
return true;
7706+
if (home&&home[0])
7707+
{
7708+
strlcpy(buf,home,bufsize);
7709+
return true;
7710+
}
7711+
else
7712+
{
7713+
structpasswdpwbuf;
7714+
structpasswd*pw;
7715+
chartmpbuf[1024];
7716+
intrc;
7717+
7718+
rc=getpwuid_r(geteuid(),&pwbuf,tmpbuf,sizeoftmpbuf,&pw);
7719+
if (rc!=0|| !pw)
7720+
return false;
7721+
strlcpy(buf,pw->pw_dir,bufsize);
7722+
return true;
7723+
}
77097724
#else
77107725
chartmppath[MAX_PATH];
77117726

‎src/interfaces/libpq/nls.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ GETTEXT_FILES = fe-auth.c \
1313
fe-secure-common.c\
1414
fe-secure-gssapi.c\
1515
fe-secure-openssl.c\
16-
win32.c\
17-
../../port/user.c
16+
win32.c
1817
GETTEXT_TRIGGERS = libpq_append_conn_error:2\
1918
libpq_append_error:2\
2019
libpq_gettext\

‎src/port/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ OBJS = \
5757
quotes.o\
5858
snprintf.o\
5959
strerror.o\
60-
tar.o\
61-
user.o
60+
tar.o
6261

6362
# libpgport.a, libpgport_shlib.a, and libpgport_srv.a contain the same files
6463
# foo.o, foo_shlib.o, and foo_srv.o are all built from foo.c

‎src/port/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pgport_sources = [
2020
'snprintf.c',
2121
'strerror.c',
2222
'tar.c',
23-
'user.c',
2423
]
2524

2625
if host_system=='windows'

‎src/port/path.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#definenear
3333
#include<shlobj.h>
3434
#else
35+
#include<pwd.h>
3536
#include<unistd.h>
3637
#endif
3738

@@ -934,10 +935,24 @@ get_home_path(char *ret_path)
934935
constchar*home;
935936

936937
home=getenv("HOME");
937-
if (home==NULL||home[0]=='\0')
938-
returnpg_get_user_home_dir(geteuid(),ret_path,MAXPGPATH);
939-
strlcpy(ret_path,home,MAXPGPATH);
940-
return true;
938+
if (home&&home[0])
939+
{
940+
strlcpy(ret_path,home,MAXPGPATH);
941+
return true;
942+
}
943+
else
944+
{
945+
structpasswdpwbuf;
946+
structpasswd*pw;
947+
charbuf[1024];
948+
intrc;
949+
950+
rc=getpwuid_r(geteuid(),&pwbuf,buf,sizeofbuf,&pw);
951+
if (rc!=0|| !pw)
952+
return false;
953+
strlcpy(ret_path,pw->pw_dir,MAXPGPATH);
954+
return true;
955+
}
941956
#else
942957
char*tmppath;
943958

‎src/port/user.c

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp