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);
79 bool UserInfo::set_effective_uid(id_t user_id)
noexcept {
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;
92 env_str = ::getenv(
"SUDO_UID");
93 if (
nullptr != env_str ) {
94 const auto [env_val, consumed, complete] =
jau::to_integer(env_str, strlen(env_str));
96 res_uid = (::uid_t)env_val;
101 env_str = ::getenv(
"UID");
102 if (
nullptr != env_str ) {
103 const auto [env_val, consumed, complete] =
jau::to_integer(env_str, strlen(env_str));
105 res_uid = (::uid_t)env_val;
111 bool UserInfo::get_env_uid(id_t &res_uid,
const bool try_sudo)
noexcept {
112 ::uid_t n_res_uid = 0;
114 res_uid = (id_t)n_res_uid;
120 bool UserInfo::get_env_username(std::string &username,
const bool try_sudo)
noexcept {
121 char *env_str =
nullptr;
123 env_str = ::getenv(
"SUDO_USER");
124 if (
nullptr != env_str ) {
125 username = std::string(env_str);
129 env_str = ::getenv(
"USER");
130 if (
nullptr != env_str ) {
131 username = std::string(env_str);
137 bool UserInfo::get_creds(id_t &res_uid, id_t &res_gid, std::string &username, std::string &homedir, std::string &shell)
noexcept {
138 ::uid_t n_res_uid = (::uid_t)res_uid;
139 const bool is_root = 0 == n_res_uid;
144 struct passwd *pwd_res =
nullptr;
145 if ( 0 != ::getpwuid_r(n_res_uid, &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
146 DBG_PRINT(
"getpwuid(%" PRIu32
") failed", n_res_uid);
149 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);
150 res_uid = (id_t)n_res_uid;
151 res_gid = (id_t)(::gid_t)(pwd_res->pw_gid);
152 username = std::string(pwd_res->pw_name);
153 homedir = std::string(pwd_res->pw_dir);
154 shell = std::string(pwd_res->pw_shell);
157 std::string tmp_username;
158 if ( get_env_username(tmp_username, is_root) ) {
159 struct passwd *pwd_res =
nullptr;
160 if ( 0 != ::getpwnam_r(tmp_username.c_str(), &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
161 DBG_PRINT(
"getpwnam(%s) failed\n", tmp_username.c_str());
164 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);
165 res_uid = (id_t)n_res_uid;
166 res_gid = (id_t)(::gid_t)(pwd_res->pw_gid);
167 username = std::string(pwd_res->pw_name);
168 homedir = std::string(pwd_res->pw_dir);
169 shell = std::string(pwd_res->pw_shell);
176 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 {
179 struct passwd *pwd_res =
nullptr;
180 if ( 0 != ::getpwnam_r(username_lookup.c_str(), &pwd, buffer,
sizeof(buffer), &pwd_res) ||
nullptr == pwd_res ) {
181 DBG_PRINT(
"getpwnam(%s) failed\n", username_lookup.c_str());
184 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);
185 res_uid = (id_t)(::uid_t)(pwd_res->pw_uid);
186 res_gid = (id_t)(::gid_t)(pwd_res->pw_gid);
187 username = std::string(pwd_res->pw_name);
188 homedir = std::string(pwd_res->pw_dir);
189 shell = std::string(pwd_res->pw_shell);
194 m_uid = (
id_t)::getuid();
195 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
197 get_groups(m_gid_list);
202 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
204 get_groups(m_gid_list);
209 m_valid = get_creds(
username, m_uid, m_gid, m_username, m_homedir, m_shell);
211 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 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...
Int64SizeBoolTuple to_integer(const char *str, size_t str_len, const nsize_t radix=10, const char limiter='\0', const char *limiter_pos=nullptr)
Returns tuple [int64_t result, size_t consumed_chars, bool complete] of string to integer conversion ...
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.
std::string to_string(const bit_order_t v) noexcept
Return std::string representation of the given bit_order_t.
static bool UserInfo_get_env_uid(::uid_t &res_uid, const bool try_sudo) noexcept