From: Arvin Schnell Date: Tue, 29 Sep 2020 07:42:17 +0000 (+0200) Subject: - handle case where _SC_GETPW_R_SIZE_MAX hint is too small X-Git-Tag: v0.8.15~28^2 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=f96f802b8cf6afa0d2144caecb71e11bd40ce25d;p=thirdparty%2Fsnapper.git - handle case where _SC_GETPW_R_SIZE_MAX hint is too small --- diff --git a/snapper/AppUtil.cc b/snapper/AppUtil.cc index 3c505a86..8e80e719 100644 --- a/snapper/AppUtil.cc +++ b/snapper/AppUtil.cc @@ -324,6 +324,29 @@ namespace snapper } + bool + get_uid_dir(uid_t uid, string& dir) + { + struct passwd pwd; + struct passwd* result; + + vector 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) { diff --git a/snapper/AppUtil.h b/snapper/AppUtil.h index b9858cfa..0f070c44 100644 --- a/snapper/AppUtil.h +++ b/snapper/AppUtil.h @@ -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 getgrouplist(const char* username, gid_t gid); diff --git a/snapper/Logger.cc b/snapper/Logger.cc index d778ebae..a21d7303 100644 --- a/snapper/Logger.cc +++ b/snapper/Logger.cc @@ -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"; } } diff --git a/testsuite-real/ug-tests.cc b/testsuite-real/ug-tests.cc index f5af1b43..832dc558 100644 --- a/testsuite-real/ug-tests.cc +++ b/testsuite-real/ug-tests.cc @@ -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 gids = getgrouplist(username.c_str(), gid); cout << "gids:"; for (gid_t gid : gids)