33 #include <sys/types.h>
39 bool UserInfo::get_groups(std::vector<id_t>& list)
noexcept {
44 int count = ::getgroups(
sizeof(gid_list)/
sizeof(*gid_list), gid_list);
49 for(
int i=0; i<count; ++i) {
50 list.push_back((id_t)gid_list[i]);
57 bool UserInfo::set_groups(
const std::vector<id_t>& list)
noexcept {
58 std::vector<::gid_t> n_list;
59 n_list.reserve(list.size()+1);
60 for(
const id_t& gid : list) {
61 n_list.push_back( (::gid_t)gid );
63 if( 0 > ::setgroups(n_list.size(), n_list.data()) ) {
70 bool UserInfo::set_effective_gid(id_t group_id)
noexcept {
71 ::gid_t n_group_id = (::gid_t) group_id;
72 if( 0 != ::setegid(n_group_id) ) {
73 ERR_PRINT(
"setegid(%" PRIu64
") failed", group_id);
80 ::uid_t n_user_id = (::uid_t)user_id;
81 if( 0 != ::seteuid(n_user_id) ) {
82 ERR_PRINT(
"seteuid(%" PRIu64
") failed", user_id);
89 char *env_str =
nullptr;
90 long long env_val = 0;
92 env_str = ::getenv(
"SUDO_UID");
93 if(
nullptr != env_str ) {
95 res_uid = (::uid_t) env_val;
100 env_str = ::getenv(
"UID");
101 if(
nullptr != env_str ) {
103 res_uid = (::uid_t) env_val;
109 bool UserInfo::get_env_uid(id_t& res_uid,
const bool try_sudo)
noexcept {
110 ::uid_t n_res_uid = 0;
112 res_uid = (id_t)n_res_uid;
118 bool UserInfo::get_env_username(std::string& username,
const bool try_sudo)
noexcept {
119 char *env_str =
nullptr;
121 env_str = ::getenv(
"SUDO_USER");
122 if(
nullptr != env_str ) {
123 username = std::string(env_str);
127 env_str = ::getenv(
"USER");
128 if(
nullptr != env_str ) {
129 username = std::string(env_str);
135 bool UserInfo::get_creds(id_t& res_uid, id_t& res_gid, std::string& username, std::string& homedir, std::string& shell)
noexcept {
136 ::uid_t n_res_uid = (::uid_t)res_uid;
137 const bool is_root = 0 == n_res_uid;
142 struct passwd* pwd_res =
nullptr;
143 if( 0 != ::getpwuid_r(n_res_uid, &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
144 DBG_PRINT(
"getpwuid(%" PRIu32
") failed", n_res_uid);
147 DBG_PRINT(
"getpwuid(%" PRIu32
"): name '%s', uid %" PRIu32
", gid %" PRIu32
"\n", n_res_uid, pwd_res->pw_name, pwd_res->pw_uid, pwd_res->pw_gid);
148 res_uid = (id_t)n_res_uid;
149 res_gid = (id_t)(::gid_t)( pwd_res->pw_gid );
150 username = std::string(pwd_res->pw_name);
151 homedir = std::string(pwd_res->pw_dir);
152 shell = std::string(pwd_res->pw_shell);
155 std::string tmp_username;
156 if( get_env_username(tmp_username, is_root) ) {
157 struct passwd* pwd_res =
nullptr;
158 if( 0 != ::getpwnam_r(tmp_username.c_str(), &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
159 DBG_PRINT(
"getpwnam(%s) failed\n", tmp_username.c_str());
162 DBG_PRINT(
"getpwnam(%s): name '%s', uid %" PRIu32
", gid %" PRIu32
"\n", tmp_username.c_str(), pwd_res->pw_name, pwd_res->pw_uid, pwd_res->pw_gid);
163 res_uid = (id_t)n_res_uid;
164 res_gid = (id_t)(::gid_t)( pwd_res->pw_gid );
165 username = std::string(pwd_res->pw_name);
166 homedir = std::string(pwd_res->pw_dir);
167 shell = std::string(pwd_res->pw_shell);
174 bool UserInfo::get_creds(
const std::string& username_lookup, id_t& res_uid, id_t& res_gid, std::string& username, std::string& homedir, std::string& shell)
noexcept {
177 struct passwd* pwd_res =
nullptr;
178 if( 0 != ::getpwnam_r(username_lookup.c_str(), &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
179 DBG_PRINT(
"getpwnam(%s) failed\n", username_lookup.c_str());
182 DBG_PRINT(
"getpwnam(%s): name '%s', uid %" PRIu32
", gid %" PRIu32
"\n", username_lookup.c_str(), pwd_res->pw_name, pwd_res->pw_uid, pwd_res->pw_gid);
183 res_uid = (id_t)(::uid_t)( pwd_res->pw_uid );
184 res_gid = (id_t)(::gid_t)( pwd_res->pw_gid );
185 username = std::string(pwd_res->pw_name);
186 homedir = std::string(pwd_res->pw_dir);
187 shell = std::string(pwd_res->pw_shell);
191 UserInfo::UserInfo() noexcept {
192 m_uid = (
id_t)::getuid();
193 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
195 get_groups(m_gid_list);
200 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
202 get_groups(m_gid_list);
207 m_valid = get_creds(username, m_uid, m_gid, m_username, m_homedir, m_shell);
209 get_groups(m_gid_list);
UserInfo() noexcept
Create instance of the user executing this application.
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
#define DBG_PRINT(...)
Use for environment-variable environment::DEBUG conditional debug messages, prefix '[elapsed_time] De...
static bool set_effective_uid(::uid_t user_id)
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
bool to_integer(long long &result, const std::string &str, const char limiter='\0', const char *limiter_pos=nullptr)
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2024 Gothel Software e.K.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
static bool UserInfo_get_env_uid(::uid_t &res_uid, const bool try_sudo) noexcept