33 m_uid(static_cast<uid_t>(-1)) ,
34 m_gid(static_cast<gid_t>(-1)) ,
40 m_uid(static_cast<uid_t>(-1)) ,
41 m_gid(static_cast<gid_t>(-1)) ,
47 m_uid(static_cast<uid_t>(-1)) ,
48 m_gid(static_cast<gid_t>(-1)) ,
51 size_t buffer_size = 0 ;
53 long n = ::sysconf( _SC_GETPW_R_SIZE_MAX ) ;
54 if( n < limits::get_pwnam_r_buffer )
56 buffer_size = limits::get_pwnam_r_buffer ;
61 unsigned long un =
static_cast<unsigned long>(n) ;
62 const size_t size_max = (size_t)-1 ;
63 buffer_size = un > size_max ? size_max :
static_cast<size_t>(un) ;
67 std::vector<char> buffer( buffer_size ) ;
70 ::passwd * result_p = nullptr ;
71 int rc = ::getpwnam_r( name.c_str() , &pwd , &buffer[0] , buffer_size , &result_p ) ;
72 if( rc != 0 || result_p ==
nullptr )
81 throw NoSuchUser(name) ;
86 m_uid = result_p->pw_uid ;
87 m_gid = result_p->pw_gid ;
94 id.m_uid = ::geteuid() ;
95 id.m_gid = ::getegid() ;
102 id.m_uid = ::getuid() ;
103 id.m_gid = ::getgid() ;
127 std::ostringstream ss ;
128 ss << m_uid <<
"/" << m_gid ;
139 return m_uid == other.m_uid && m_gid == other.m_gid ;
144 return ! operator==( other ) ;
149 int rc = ::seteuid(m_uid) ; G_IGNORE_VARIABLE(rc) ;
154 if( ::seteuid(m_uid) && do_throw )
throw UidError() ;
159 if( ::setuid(m_uid) && do_throw )
throw UidError() ;
164 if( ::setegid(m_gid) && do_throw )
throw GidError() ;
169 int rc = ::setegid(m_gid) ; G_IGNORE_VARIABLE(rc) ;
174 if( ::setgid(m_gid) && do_throw )
throw GidError() ;
181 id.setRealUser( do_throw ) ;
186 id.setEffectiveUser( do_throw ) ;
191 id.setEffectiveUser( safe ) ;
196 id.setRealGroup( do_throw ) ;
201 id.setEffectiveGroup( do_throw ) ;
206 id.setEffectiveGroup( safe ) ;
bool isRoot() const
Returns true if the userid is zero.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
static Identity root()
Returns the superuser identity.
static Identity real()
Returns the calling process's real identity.
Identity(const std::string &login_name)
Constructor for the named identity.
static Identity invalid()
Returns an invalid identity.
A combination of user-id and group-id, with a very low-level interface to the get/set/e/uid/gid funct...
static void setRealGroupTo(Identity, bool do_throw=true)
Sets the real group id.
static Identity effective()
Returns the current effective identity.
void setRealUser(bool do_throw=true)
Sets the real userid.
void setRealGroup(bool do_throw=true)
Sets the real group id.
static void setEffectiveUserTo(Identity, bool do_throw=true)
Sets the effective userid.
void setEffectiveGroup(bool do_throw=true)
Sets the effective group id.
bool operator==(const Identity &) const
Comparison operator.
static void setEffectiveGroupTo(Identity, bool do_throw=true)
Sets the effective group id.
std::string str() const
Returns a string representation.
static void setRealUserTo(Identity, bool do_throw=true)
Sets the real userid.
bool operator!=(const Identity &) const
Comparison operator.
void setEffectiveUser(bool do_throw=true)
Sets the effective userid.