34 #include <sys/types.h>
40 bool UserInfo::get_groups(std::vector<id_t>& list)
noexcept {
45 int count = ::getgroups(
sizeof(gid_list)/
sizeof(*gid_list), gid_list);
50 for(
int i=0; i<count; ++i) {
51 list.push_back((id_t)gid_list[i]);
58 bool UserInfo::set_groups(
const std::vector<id_t>& list)
noexcept {
59 std::vector<::gid_t> n_list;
60 n_list.reserve(list.size()+1);
61 for(
const id_t& gid : list) {
62 n_list.push_back( (::gid_t)gid );
64 if( 0 > ::setgroups(n_list.size(), n_list.data()) ) {
71 bool UserInfo::set_effective_gid(id_t group_id)
noexcept {
72 ::gid_t n_group_id = (::gid_t) group_id;
73 if( 0 != ::setegid(n_group_id) ) {
80 bool UserInfo::set_effective_uid(id_t user_id)
noexcept {
81 ::uid_t n_user_id = (::uid_t)user_id;
82 if( 0 != ::seteuid(n_user_id) ) {
90 char *env_str =
nullptr;
93 env_str = ::getenv(
"SUDO_UID");
94 if (
nullptr != env_str ) {
95 const auto [consumed, complete] =
jau::fromIntString(env_val, env_str, strlen(env_str));
102 env_str = ::getenv(
"UID");
103 if (
nullptr != env_str ) {
104 const auto [consumed, complete] =
jau::fromIntString(env_val, env_str, strlen(env_str));
112 bool UserInfo::get_env_uid(id_t &res_uid,
const bool try_sudo)
noexcept {
113 ::uid_t n_res_uid = 0;
115 res_uid = (id_t)n_res_uid;
121 bool UserInfo::get_env_username(std::string &username,
const bool try_sudo)
noexcept {
122 char *env_str =
nullptr;
124 env_str = ::getenv(
"SUDO_USER");
125 if (
nullptr != env_str ) {
126 username = std::string(env_str);
130 env_str = ::getenv(
"USER");
131 if (
nullptr != env_str ) {
132 username = std::string(env_str);
138 bool UserInfo::get_creds(id_t &res_uid, id_t &res_gid, std::string &username, std::string &homedir, std::string &shell)
noexcept {
139 ::uid_t n_res_uid = (::uid_t)res_uid;
140 const bool is_root = 0 == n_res_uid;
145 struct passwd *pwd_res =
nullptr;
146 if ( 0 != ::getpwuid_r(n_res_uid, &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
150 jau_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);
151 res_uid = (id_t)n_res_uid;
152 res_gid = (id_t)(::gid_t)(pwd_res->pw_gid);
153 username = std::string(pwd_res->pw_name);
154 homedir = std::string(pwd_res->pw_dir);
155 shell = std::string(pwd_res->pw_shell);
158 std::string tmp_username;
159 if ( get_env_username(tmp_username, is_root) ) {
160 struct passwd *pwd_res =
nullptr;
161 if ( 0 != ::getpwnam_r(tmp_username.c_str(), &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
162 jau_DBG_PRINT(
"getpwnam(%s) failed\n", tmp_username.c_str());
165 jau_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);
166 res_uid = (id_t)n_res_uid;
167 res_gid = (id_t)(::gid_t)(pwd_res->pw_gid);
168 username = std::string(pwd_res->pw_name);
169 homedir = std::string(pwd_res->pw_dir);
170 shell = std::string(pwd_res->pw_shell);
177 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 {
180 struct passwd *pwd_res =
nullptr;
181 if ( 0 != ::getpwnam_r(username_lookup.c_str(), &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
182 jau_DBG_PRINT(
"getpwnam(%s) failed\n", username_lookup.c_str());
185 jau_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);
186 res_uid = (id_t)(::uid_t)(pwd_res->pw_uid);
187 res_gid = (id_t)(::gid_t)(pwd_res->pw_gid);
188 username = std::string(pwd_res->pw_name);
189 homedir = std::string(pwd_res->pw_dir);
190 shell = std::string(pwd_res->pw_shell);
195 m_uid = (
id_t)::getuid();
196 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
198 get_groups(m_gid_list);
203 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
205 get_groups(m_gid_list);
210 m_valid = get_creds(
username, m_uid, m_gid, m_username, m_homedir, m_shell);
212 get_groups(m_gid_list);
id_t uid() const noexcept
const std::string & username() const noexcept
UserInfo() noexcept
Create instance of the user executing this application.
#define jau_DBG_PRINT(fmt,...)
Use for environment-variable environment::DEBUG conditional debug messages, prefix '[elapsed_time] De...
#define jau_ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
std::string_view to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr SizeBoolPair fromIntString(value_type &result, std::string_view str, uint32_t radix=10, const char separator=0) noexcept
Converts a given integer string representation to the given result reference, compatible with ::strto...
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