1
0
mirror of https://github.com/openSUSE/snapper.git synced 2026-02-05 06:46:08 +01:00

- handle case where _SC_GETPW_R_SIZE_MAX hint is too small

This commit is contained in:
Arvin Schnell
2020-09-29 09:42:17 +02:00
parent eb8d3b0571
commit f96f802b8c
4 changed files with 50 additions and 10 deletions

View File

@@ -324,6 +324,29 @@ namespace snapper
}
bool
get_uid_dir(uid_t uid, string& dir)
{
struct passwd pwd;
struct passwd* result;
vector<char> buf(sysconf(_SC_GETPW_R_SIZE_MAX, 1024));
int e;
while ((e = getpwuid_r(uid, &pwd, buf.data(), buf.size(), &result)) == ERANGE)
buf.resize(2 * buf.size());
if (e != 0 || result == NULL)
return false;
memset(pwd.pw_passwd, 0, strlen(pwd.pw_passwd));
dir = pwd.pw_dir;
return true;
}
bool
get_user_uid(const char* username, uid_t& uid)
{

View File

@@ -90,9 +90,27 @@ namespace snapper
string datetime(time_t time, bool utc, bool classic);
time_t scan_datetime(const string& str, bool utc);
/**
* For the user with uid get the username (passwd.pw_name) and the gid
* (passwd.pw_gid).
*/
bool get_uid_username_gid(uid_t uid, string& username, gid_t& gid);
/**
* For the user with uid get the home directory (passwd.pw_dir).
*/
bool get_uid_dir(uid_t uid, string& dir);
/*
* For the user with username get the uid (passwd.pw_uid).
*/
bool get_user_uid(const char* username, uid_t& uid);
/*
* For the group with groupname get the gid (group.gr_gid).
*/
bool get_group_gid(const char* groupname, gid_t& gid);
vector<gid_t> getgrouplist(const char* username, gid_t gid);

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) [2004-2013] Novell, Inc.
* Copyright (c) 2018 SUSE LLC
* Copyright (c) [2018-2020] SUSE LLC
*
* All Rights Reserved.
*
@@ -158,17 +158,11 @@ namespace snapper
if (geteuid())
{
struct passwd pwd;
struct passwd* result;
string dir;
long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
char buf[bufsize];
if (getpwuid_r(geteuid(), &pwd, buf, bufsize, &result) == 0 && result == &pwd)
if (get_uid_dir(geteuid(), dir))
{
memset(pwd.pw_passwd, 0, strlen(pwd.pw_passwd));
logger_data->filename = string(pwd.pw_dir) + "/.snapper.log";
logger_data->filename = dir + "/.snapper.log";
}
}

View File

@@ -23,6 +23,11 @@ test1()
cout << "username:" << username << endl;
cout << "gid:" << gid << endl;
string dir;
if (!get_uid_dir(uid, dir))
cerr << "failed to get dir" << endl;
cout << "dir:" << dir << endl;
vector<gid_t> gids = getgrouplist(username.c_str(), gid);
cout << "gids:";
for (gid_t gid : gids)