35 int sysconf_value(
int key )
37 long n = ::sysconf( key ) ;
38 return ( n < 0 || n > INT_MAX ) ? -1 :
static_cast<int>(n) ;
44 m_uid(static_cast<uid_t>(-1)) ,
45 m_gid(static_cast<gid_t>(-1))
50 m_uid(
static_cast<uid_t
>(-1)) ,
51 m_gid(
static_cast<gid_t
>(-1))
56 m_uid(static_cast<uid_t>(-1)) ,
57 m_gid(static_cast<gid_t>(-1))
69 using passwd_t =
struct passwd ;
70 std::pair<uid_t,gid_t> result( 0 , 0 ) ;
71 std::array<int,3U> sizes {{ 120 , 0 , 16000 }} ;
72 sizes[1] = IdentityImp::sysconf_value( _SC_GETPW_R_SIZE_MAX ) ;
73 for(
auto size : sizes )
75 if( size <= 0 ) continue ;
76 auto buffer_size =
static_cast<std::size_t
>(size) ;
77 std::vector<char> buffer( buffer_size ) ;
79 passwd_t * result_p = nullptr ;
80 int rc = ::getpwnam_r( name.c_str() , &pwd , &buffer[0] , buffer_size , &result_p ) ;
82 if( rc == 0 && result_p )
84 result.first = result_p->pw_uid ;
85 result.second = result_p->pw_gid ;
88 else if( rc == 0 && name ==
"root" )
96 throw NoSuchUser( name ) ;
98 else if( e != ERANGE )
108 using group_t =
struct group ;
110 std::array<int,3U> sizes {{ 120 , 0 , 16000 }} ;
111 sizes[1] = IdentityImp::sysconf_value( _SC_GETGR_R_SIZE_MAX ) ;
112 for(
auto size : sizes )
114 if( size <= 0 ) continue ;
115 auto buffer_size =
static_cast<std::size_t
>(size) ;
116 std::vector<char> buffer( buffer_size ) ;
118 group_t * result_p = nullptr ;
119 int rc = ::getgrnam_r( group.c_str() , &grp , &buffer[0] , buffer_size , &result_p ) ;
120 if( rc == 0 && result_p )
122 result = result_p->gr_gid ;
127 throw NoSuchGroup( group ) ;
136 id.m_uid = ::geteuid() ;
137 id.m_gid = ::getegid() ;
144 static bool first = true ;
154 id.m_uid = (first||with_cache) ? u : ::getuid() ;
155 id.m_gid = (first||with_cache) ? g : ::getgid() ;
179 std::ostringstream ss ;
180 ss << m_uid <<
"/" << m_gid ;
201 return m_uid == other.m_uid && m_gid == other.m_gid ;
206 return !operator==( other ) ;
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
static std::pair< uid_t, gid_t > lookupUser(const std::string &user)
Does a username lookup. Throws on error.
gid_t groupid() const noexcept
Returns the group part.
bool isRoot() const noexcept
Returns true if the userid is zero.
uid_t userid() const noexcept
Returns the user part.
static Identity invalid() noexcept
Returns an invalid identity.
bool operator==(const Identity &) const noexcept
Comparison operator.
static Identity root() noexcept
Returns the superuser identity.
static gid_t lookupGroup(const std::string &group)
Does a groupname lookup. Throws on error.
std::string str() const
Returns a string representation.
static Identity effective() noexcept
Returns the current effective identity.
Identity(const std::string &username, const std::string &group_name_override=std::string())
Constructor for the named identity.
bool operator!=(const Identity &) const noexcept
Comparison operator.
static Identity real(bool with_cache=true) noexcept
Returns the calling process's real identity.
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.
An empty structure that is used to indicate a signal-safe, reentrant implementation.