2003-06-14 16:35:42 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2023-07-09 08:12:28 +02:00
|
|
|
* user.c
|
2003-06-14 16:35:42 +02:00
|
|
|
*
|
2023-07-09 08:12:28 +02:00
|
|
|
* Wrapper functions for user and home directory lookup.
|
2003-06-14 16:35:42 +02:00
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
2003-06-14 16:35:42 +02:00
|
|
|
*
|
2023-07-09 08:12:28 +02:00
|
|
|
* src/port/user.c
|
2003-06-14 16:35:42 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2004-06-24 20:53:48 +02:00
|
|
|
#include "c.h"
|
2003-06-14 16:35:42 +02:00
|
|
|
|
2003-10-26 05:29:15 +01:00
|
|
|
#include <pwd.h>
|
2003-09-13 16:49:51 +02:00
|
|
|
|
2022-01-11 19:46:12 +01:00
|
|
|
#ifndef WIN32
|
2003-08-14 07:27:18 +02:00
|
|
|
|
2003-06-14 16:35:42 +02:00
|
|
|
/*
|
2022-01-11 19:46:12 +01:00
|
|
|
* pg_get_user_name - get the name of the user with the given ID
|
|
|
|
*
|
|
|
|
* On success, the user name is returned into the buffer (of size buflen),
|
|
|
|
* and "true" is returned. On failure, a localized error message is
|
|
|
|
* returned into the buffer, and "false" is returned.
|
2003-06-14 16:35:42 +02:00
|
|
|
*/
|
2022-01-11 19:46:12 +01:00
|
|
|
bool
|
|
|
|
pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
|
2003-06-14 16:35:42 +02:00
|
|
|
{
|
2022-01-11 19:46:12 +01:00
|
|
|
char pwdbuf[BUFSIZ];
|
|
|
|
struct passwd pwdstr;
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
int pwerr;
|
2003-09-13 16:49:51 +02:00
|
|
|
|
2022-07-23 23:44:29 +02:00
|
|
|
pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
|
2022-01-11 19:46:12 +01:00
|
|
|
if (pw != NULL)
|
|
|
|
{
|
|
|
|
strlcpy(buffer, pw->pw_name, buflen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pwerr != 0)
|
|
|
|
snprintf(buffer, buflen,
|
|
|
|
_("could not look up local user ID %d: %s"),
|
|
|
|
(int) user_id,
|
|
|
|
strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
|
|
|
|
else
|
|
|
|
snprintf(buffer, buflen,
|
|
|
|
_("local user with ID %d does not exist"),
|
|
|
|
(int) user_id);
|
|
|
|
return false;
|
|
|
|
}
|
2003-09-13 16:49:51 +02:00
|
|
|
|
2022-01-11 19:46:12 +01:00
|
|
|
/*
|
|
|
|
* pg_get_user_home_dir - get the home directory of the user with the given ID
|
|
|
|
*
|
|
|
|
* On success, the directory path is returned into the buffer (of size buflen),
|
|
|
|
* and "true" is returned. On failure, a localized error message is
|
|
|
|
* returned into the buffer, and "false" is returned.
|
|
|
|
*
|
|
|
|
* Note that this does not incorporate the common behavior of checking
|
|
|
|
* $HOME first, since it's independent of which user_id is queried.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
char pwdbuf[BUFSIZ];
|
|
|
|
struct passwd pwdstr;
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
int pwerr;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2022-07-23 23:44:29 +02:00
|
|
|
pwerr = getpwuid_r(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
|
2022-01-11 19:46:12 +01:00
|
|
|
if (pw != NULL)
|
|
|
|
{
|
|
|
|
strlcpy(buffer, pw->pw_dir, buflen);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pwerr != 0)
|
|
|
|
snprintf(buffer, buflen,
|
|
|
|
_("could not look up local user ID %d: %s"),
|
|
|
|
(int) user_id,
|
|
|
|
strerror_r(pwerr, pwdbuf, sizeof(pwdbuf)));
|
2003-06-14 16:35:42 +02:00
|
|
|
else
|
2022-01-11 19:46:12 +01:00
|
|
|
snprintf(buffer, buflen,
|
|
|
|
_("local user with ID %d does not exist"),
|
|
|
|
(int) user_id);
|
|
|
|
return false;
|
2003-06-14 16:35:42 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2022-01-11 19:46:12 +01:00
|
|
|
#endif /* !WIN32 */
|