36 bool lookupUser( std::string & name , uid_t & uid , gid_t & gid ) ;
37 bool lookupGroup(
const std::string & group , gid_t & gid ) ;
38 int sysconf_(
int key ) ;
49 m_uid(static_cast<uid_t>(-1)) ,
50 m_gid(static_cast<gid_t>(-1))
62 std::string name = name_in ;
63 if( !IdentityImp::lookupUser( name , m_uid , m_gid ) )
64 throw NoSuchUser( name_in ) ;
66 if( !group.empty() && !IdentityImp::lookupGroup( group , m_gid ) )
67 throw NoSuchGroup( group ) ;
72 return { ::geteuid() , ::getegid() } ;
77 return { ::getuid() , ::getgid() } ;
100 std::ostringstream ss ;
101 ss << static_cast<int>(m_uid) <<
"/" <<
static_cast<int>(m_gid) ;
123 return m_uid == other.m_uid && m_gid == other.m_gid ;
128 return !operator==( other ) ;
135 std::string name = sv_to_string( name_in ) ;
136 if( !IdentityImp::lookupUser( name , result.m_uid , result.m_gid ) )
137 throw NoSuchUser( name_in ) ;
138 return std::make_pair( result , name ) ;
145 std::string name = sv_to_string( name_in ) ;
146 IdentityImp::lookupUser( name , result.m_uid , result.m_gid ) ;
147 return std::make_pair( result , name ) ;
152 gid_t result =
static_cast<gid_t
>(-1) ;
153 IdentityImp::lookupGroup( group , result ) ;
159 return G::Range::within( uid_range ,
static_cast<int>(m_uid) ) ;
164bool G::IdentityImp::lookupUser( std::string & name , uid_t & uid , gid_t & gid )
166 using passwd_t =
struct passwd ;
167 std::array<int,3U> sizes {{ 120 , 0 , 16000 }} ;
168 sizes[1] = IdentityImp::sysconf_( _SC_GETPW_R_SIZE_MAX ) ;
169 for( std::size_t i = 0U ; i < sizes.size() ; i++ )
171 int size = sizes[i] ;
172 if( size <= 0 ) continue ;
173 auto buffer_size =
static_cast<std::size_t
>(size) ;
174 std::vector<char> buffer( buffer_size ) ;
176 passwd_t * result_p = nullptr ;
177 int rc = ::getpwnam_r( name.c_str() , &pwd , buffer.data() , buffer_size , &result_p ) ;
179 if( rc != 0 && e == ERANGE && (i+1U) < sizes.size() )
183 else if( rc == 0 && result_p )
185 name = result_p->pw_name ;
186 uid = result_p->pw_uid ;
187 gid = result_p->pw_gid ;
190 else if( rc == 0 && name ==
"root" )
208bool G::IdentityImp::lookupGroup(
const std::string & group , gid_t & gid )
210 using group_t =
struct group ;
211 std::array<int,3U> sizes {{ 120 , 0 , 16000 }} ;
212 sizes[1] = IdentityImp::sysconf_( _SC_GETGR_R_SIZE_MAX ) ;
213 for(
auto size : sizes )
215 if( size <= 0 ) continue ;
216 auto buffer_size =
static_cast<std::size_t
>(size) ;
217 std::vector<char> buffer( buffer_size ) ;
219 group_t * result_p = nullptr ;
220 int rc = ::getgrnam_r( group.c_str() , &grp , buffer.data() , buffer_size , &result_p ) ;
221 if( rc == 0 && result_p )
223 gid = result_p->gr_gid ;
234int G::IdentityImp::sysconf_(
int key )
236 long n = ::sysconf( key ) ;
237 return ( n < 0 || n > INT_MAX ) ? -1 :
static_cast<int>(n) ;
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
gid_t groupid() const noexcept
Returns the group part (Unix).
bool isRoot() const noexcept
Returns true if the userid is zero.
uid_t userid() const noexcept
Returns the user part (Unix).
static Identity invalid() noexcept
Returns an invalid identity.
bool operator==(const Identity &) const noexcept
Comparison operator.
bool match(std::pair< int, int > uid_range) const
Returns true if the user-id is in the given range or if not implemented.
static Identity root() noexcept
Returns the superuser identity.
static gid_t lookupGroup(const std::string &group)
Does a groupname lookup.
static std::pair< Identity, std::string > lookup(std::string_view user)
Does a username lookup returning the identity and the canonical name.
std::string str() const
Returns a string representation.
static Identity effective() noexcept
Returns the current effective identity.
bool operator!=(const Identity &) const noexcept
Comparison operator.
Identity(const std::string &username, const std::string &group_name_override={})
Constructor for the named identity.
static Identity real() 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.