jaulib v1.4.0-2-g788cf73
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
user_info.cpp
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#if !defined(_WIN32)
25
26 #include "jau/debug.hpp"
27 #include "jau/os/user_info.hpp"
28
29 extern "C" {
30 #include <unistd.h>
31 #include <grp.h>
32 #include <pwd.h>
33 #include <sys/types.h>
34 }
35
36 using namespace jau;
37 using namespace jau::os;
38
39 bool UserInfo::get_groups(std::vector<id_t>& list) noexcept {
40 // jau::fprintf_td(stderr, "%s: uid %" PRIu32 ", euid %" PRIu32 ", gid %" PRIu32 ", egid %" PRIu32 "\n",
41 // title.c_str(), ::getuid(), ::geteuid(), ::getgid(), ::getegid());
42 list.clear();
43 ::gid_t gid_list[64];
44 int count = ::getgroups(sizeof(gid_list)/sizeof(*gid_list), gid_list);
45 if( 0 > count ) {
46 DBG_PRINT("getgroups() failed");
47 return false;
48 } else {
49 for(int i=0; i<count; ++i) {
50 list.push_back((id_t)gid_list[i]);
51 }
52 DBG_PRINT("getgroups(): %s", jau::to_string(list).c_str());
53 return true;
54 }
55 }
56
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 );
62 }
63 if( 0 > ::setgroups(n_list.size(), n_list.data()) ) {
64 ERR_PRINT("setgroups failed");
65 return false;
66 }
67 return true;
68 }
69
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);
74 return false;
75 }
76 return true;
77 }
78
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);
83 return false;
84 }
85 return true;
86 }
87
88 static bool UserInfo_get_env_uid(::uid_t &res_uid, const bool try_sudo) noexcept {
89 char *env_str = nullptr;
90 // int64_t env_val = 0;
91 if ( try_sudo ) {
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));
95 if ( complete ) {
96 res_uid = (::uid_t)env_val;
97 return true;
98 }
99 }
100 }
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));
104 if ( complete ) {
105 res_uid = (::uid_t)env_val;
106 return true;
107 }
108 }
109 return false;
110 }
111 bool UserInfo::get_env_uid(id_t &res_uid, const bool try_sudo) noexcept {
112 ::uid_t n_res_uid = 0;
113 if ( UserInfo_get_env_uid(n_res_uid, try_sudo) ) {
114 res_uid = (id_t)n_res_uid;
115 return true;
116 }
117 return false;
118 }
119
120 bool UserInfo::get_env_username(std::string &username, const bool try_sudo) noexcept {
121 char *env_str = nullptr;
122 if ( try_sudo ) {
123 env_str = ::getenv("SUDO_USER");
124 if ( nullptr != env_str ) {
125 username = std::string(env_str);
126 return true;
127 }
128 }
129 env_str = ::getenv("USER");
130 if ( nullptr != env_str ) {
131 username = std::string(env_str);
132 return true;
133 }
134 return false;
135 }
136
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;
140 struct passwd pwd;
141 char buffer[1024];
142
143 if ( !is_root || UserInfo_get_env_uid(n_res_uid, is_root) ) {
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);
147 return false;
148 }
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);
155 return true;
156 } else {
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());
162 return false;
163 }
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);
170 return true;
171 }
172 }
173 return false;
174 }
175
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 {
177 struct passwd pwd;
178 char buffer[1024];
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());
182 return false;
183 }
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);
190 return true;
191 }
192
194 m_uid = (id_t)::getuid();
195 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
196 if ( m_valid ) {
197 get_groups(m_gid_list);
198 }
199 }
201 m_uid = uid;
202 m_valid = get_creds(m_uid, m_gid, m_username, m_homedir, m_shell);
203 if ( m_valid ) {
204 get_groups(m_gid_list);
205 }
206 }
207
208 UserInfo::UserInfo(const std::string &username) noexcept {
209 m_valid = get_creds(username, m_uid, m_gid, m_username, m_homedir, m_shell);
210 if ( m_valid ) {
211 get_groups(m_gid_list);
212 }
213 }
214
215#endif // !_WIN32
id_t uid() const noexcept
Definition user_info.hpp:78
const std::string & username() const noexcept
Definition user_info.hpp:80
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: '.
Definition debug.hpp:122
#define DBG_PRINT(...)
Use for environment-variable environment::DEBUG conditional debug messages, prefix '[elapsed_time] De...
Definition debug.hpp:72
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.
Definition backtrace.hpp:32
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
Definition user_info.cpp:88