17 #ifndef __STOUT_OS_POSIX_SU_HPP__ 18 #define __STOUT_OS_POSIX_SU_HPP__ 26 #include <sys/syscall.h> 47 int size = sysconf(_SC_GETPW_R_SIZE_MAX);
56 char* buffer =
new char[
size];
58 if (getpwnam_r(
user->c_str(), &pwd, buffer,
size, &result) == 0) {
62 if (result ==
nullptr) {
74 if (errno == ERANGE) {
100 return ErrnoError(
"Failed to get username information");
110 if (::
setuid(uid) == -1) {
124 int size = sysconf(_SC_GETPW_R_SIZE_MAX);
133 char* buffer =
new char[
size];
135 if (getpwnam_r(
user->c_str(), &pwd, buffer,
size, &result) == 0) {
139 if (result ==
nullptr) {
151 if (errno == ERANGE) {
177 return ErrnoError(
"Failed to get username information");
187 if (::
setgid(gid) == -1) {
201 return Error(
"Failed to get the gid of the user: " +
214 int ngroups = NGROUPS_MAX;
221 return std::vector<gid_t>(gids, gids + ngroups);
226 const std::vector<gid_t>& gids,
229 int ngroups =
static_cast<int>(gids.size());
230 gid_t _gids[ngroups];
232 for (
int i = 0; i < ngroups; i++) {
243 int maxgroups = sysconf(_SC_NGROUPS_MAX);
244 if (maxgroups == -1) {
245 return Error(
"Failed to get sysconf(_SC_NGROUPS_MAX)");
248 if (ngroups > maxgroups) {
254 "The uid of the user who is associated with the group " 255 "list we are setting is missing");
268 #pragma GCC diagnostic push 269 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 270 if (::syscall(SYS_initgroups, ngroups, _gids,
uid.get()) == -1) {
271 #pragma GCC diagnostic pop 290 int size = sysconf(_SC_GETPW_R_SIZE_MAX);
299 char* buffer =
new char[
size];
301 if (getpwuid_r(
uid.get(), &pwd, buffer,
size, &result) == 0) {
304 if (result ==
nullptr) {
315 if (errno != ERANGE) {
330 return Error(
"Failed to getgid: " +
339 if (::initgroups(user.c_str(), gid.
get()) == -1 && errno != EPERM) {
340 return ErrnoError(
"Failed to set supplementary groups");
345 return Error(
"Failed to getuid: " +
356 #endif // __STOUT_OS_POSIX_SU_HPP__ bool isNone() const
Definition: result.hpp:113
Try< uid_t > uid(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:224
Definition: nothing.hpp:16
Definition: errorbase.hpp:36
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
UINT uid_t
Definition: windows.hpp:183
static Result< T > error(const std::string &message)
Definition: result.hpp:54
Result< std::string > user(Option< uid_t > uid=None())
Definition: su.hpp:284
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
gid_t pw_gid
Definition: pwd.hpp:30
Result< uid_t > getuid(const Option< std::string > &user=None())
Definition: su.hpp:41
Try< std::vector< gid_t > > getgrouplist(const std::string &user)
Definition: su.hpp:195
Try< Nothing > setgroups(const std::vector< gid_t > &gids, const Option< uid_t > &uid=None())
Definition: su.hpp:225
char * pw_name
Definition: pwd.hpp:28
Result< gid_t > getgid(const Option< std::string > &user=None())
Definition: su.hpp:118
Try< Nothing > setgid(gid_t gid)
Definition: su.hpp:185
uid_t pw_uid
Definition: pwd.hpp:29
Try< Nothing > su(const std::string &user)
Definition: su.hpp:326
#define UNREACHABLE()
Definition: unreachable.hpp:22
T & get()&
Definition: result.hpp:116
bool isSome() const
Definition: result.hpp:112
bool isError() const
Definition: result.hpp:114
UINT gid_t
Definition: windows.hpp:184
Try< Nothing > setuid(uid_t uid)
Definition: su.hpp:108