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)) ,
44 typedef ::passwd Pwd ;
46 size_t buffer_size = 0 ;
48 long n = ::sysconf( _SC_GETPW_R_SIZE_MAX ) ;
56 unsigned long un =
static_cast<unsigned long>(n) ;
57 const size_t size_max = (size_t)-1 ;
58 buffer_size = un > size_max ? size_max :
static_cast<size_t>(un) ;
62 std::vector<char> buffer( buffer_size ) ;
65 Pwd * result_p = NULL ;
66 int rc = ::getpwnam_r( name.c_str() , &pwd , &buffer[0] , buffer_size , &result_p ) ;
67 if( rc != 0 || result_p == NULL )
76 throw NoSuchUser(name) ;
81 m_uid = result_p->pw_uid ;
82 m_gid = result_p->pw_gid ;
89 id.m_uid = ::geteuid() ;
90 id.m_gid = ::getegid() ;
97 id.m_uid = ::getuid() ;
98 id.m_gid = ::getgid() ;
117 std::ostringstream ss ;
118 ss << m_uid <<
"/" << m_gid ;
129 return m_uid == other.m_uid && m_gid == other.m_gid ;
134 return ! operator==( other ) ;
144 if( ::seteuid(m_uid) && do_throw )
throw UidError() ;
149 if( ::setuid(m_uid) && do_throw )
throw UidError() ;
154 if( ::setegid(m_gid) && do_throw )
throw GidError() ;
164 if( ::setgid(m_gid) && do_throw )
throw GidError() ;
171 id.setRealUser( do_throw ) ;
176 id.setEffectiveUser( do_throw ) ;
181 id.setEffectiveUser( safe ) ;
186 id.setRealGroup( do_throw ) ;
191 id.setEffectiveGroup( do_throw ) ;
196 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 very low-level interface to getpwnam() and the get/set/e/uid/gid functions.
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.