diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-01-11 13:46:12 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-01-11 13:46:20 -0500 |
commit | 98e93a1fc93e9b54eb477d870ec744e9e1669f34 (patch) | |
tree | 8d16b420c2b1b49c9c21af5d8fe93c9d1da93e1e /src/interfaces/libpq/fe-auth.c | |
parent | 7fa945b857cc1b2964799411f1633468826861ff (diff) | |
download | postgresql-98e93a1fc93e9b54eb477d870ec744e9e1669f34.tar.gz postgresql-98e93a1fc93e9b54eb477d870ec744e9e1669f34.zip |
Clean up messy API for src/port/thread.c.
The point of this patch is to reduce inclusion spam by not needing
to #include <netdb.h> or <pwd.h> in port.h (which is read by every
compile in our tree). To do that, we must remove port.h's
declarations of pqGetpwuid and pqGethostbyname.
pqGethostbyname is only used, and is only ever likely to be used,
in src/port/getaddrinfo.c --- which isn't even built on most
platforms, making pqGethostbyname dead code for most people.
Hence, deal with that by just moving it into getaddrinfo.c.
To clean up pqGetpwuid, invent a couple of simple wrapper
functions with less-messy APIs. This allows removing some
duplicate error-handling code, too.
In passing, remove thread.c from the MSVC build, since it
contains nothing we use on Windows.
Noted while working on 376ce3e40.
Discussion: https://postgr.es/m/1634252654444.90107@mit.edu
Diffstat (limited to 'src/interfaces/libpq/fe-auth.c')
-rw-r--r-- | src/interfaces/libpq/fe-auth.c | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c index 82fc7cdb986..2edc3f48e2e 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -35,7 +35,6 @@ #ifndef MAXHOSTNAMELEN #include <netdb.h> /* for MAXHOSTNAMELEN on some */ #endif -#include <pwd.h> #endif #include "common/md5.h" @@ -1099,14 +1098,17 @@ pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn) /* - * pg_fe_getauthname + * pg_fe_getusername * - * Returns a pointer to malloc'd space containing whatever name the user - * has authenticated to the system. If there is an error, return NULL, - * and append a suitable error message to *errorMessage if that's not NULL. + * Returns a pointer to malloc'd space containing the name of the + * specified user_id. If there is an error, return NULL, and append + * a suitable error message to *errorMessage if that's not NULL. + * + * Caution: on Windows, the user_id argument is ignored, and we always + * fetch the current user's name. */ char * -pg_fe_getauthname(PQExpBuffer errorMessage) +pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage) { char *result = NULL; const char *name = NULL; @@ -1116,17 +1118,13 @@ pg_fe_getauthname(PQExpBuffer errorMessage) char username[256 + 1]; DWORD namesize = sizeof(username); #else - uid_t user_id = geteuid(); char pwdbuf[BUFSIZ]; - struct passwd pwdstr; - struct passwd *pw = NULL; - int pwerr; #endif /* * Some users are using configure --enable-thread-safety-force, so we * might as well do the locking within our library to protect - * pqGetpwuid(). In fact, application developers can use getpwuid() in + * getpwuid(). In fact, application developers can use getpwuid() in * their application if they use the locking call we provide, or install * their own locking function using PQregisterThreadLock(). */ @@ -1140,21 +1138,10 @@ pg_fe_getauthname(PQExpBuffer errorMessage) libpq_gettext("user name lookup failure: error code %lu\n"), GetLastError()); #else - pwerr = pqGetpwuid(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw); - if (pw != NULL) - name = pw->pw_name; + if (pg_get_user_name(user_id, pwdbuf, sizeof(pwdbuf))) + name = pwdbuf; else if (errorMessage) - { - if (pwerr != 0) - appendPQExpBuffer(errorMessage, - libpq_gettext("could not look up local user ID %d: %s\n"), - (int) user_id, - strerror_r(pwerr, pwdbuf, sizeof(pwdbuf))); - else - appendPQExpBuffer(errorMessage, - libpq_gettext("local user with ID %d does not exist\n"), - (int) user_id); - } + appendPQExpBuffer(errorMessage, "%s\n", pwdbuf); #endif if (name) @@ -1170,6 +1157,23 @@ pg_fe_getauthname(PQExpBuffer errorMessage) return result; } +/* + * pg_fe_getauthname + * + * Returns a pointer to malloc'd space containing whatever name the user + * has authenticated to the system. If there is an error, return NULL, + * and append a suitable error message to *errorMessage if that's not NULL. + */ +char * +pg_fe_getauthname(PQExpBuffer errorMessage) +{ +#ifdef WIN32 + return pg_fe_getusername(0, errorMessage); +#else + return pg_fe_getusername(geteuid(), errorMessage); +#endif +} + /* * PQencryptPassword -- exported routine to encrypt a password with MD5 |