40 Account( SID_NAME_USE ,
const std::string & ,
const std::string & ,
const std::string & ) ;
41 SID_NAME_USE type {SidTypeInvalid} ;
45 bool valid() const noexcept {
return type == SidTypeUser ; }
48 std::string rootsid() ;
49 std::string sidstr( PSID sid_p ) ;
50 std::string computername() ;
51 Account lookup( std::string_view ,
bool =
false ) ;
66 static_assert(
noexcept(std::string()) ,
"" ) ;
77 auto account = IdentityImp::lookup( name ) ;
78 if( !account.valid() )
79 throw NoSuchUser( name ) ;
85 return { -1 , -1 , IdentityImp::sid() } ;
105 Identity id(
"Administrator" ) ;
106 if(
id != invalid() )
108 return { -1 , -1 , IdentityImp::rootsid() } ;
118 if( m_sid.empty() )
return false ;
131 std::string(
"S-1-0-0") :
142 return m_sid == other.m_sid ;
147 return m_sid != other.m_sid ;
152 auto account = IdentityImp::lookup( name ,
true ) ;
153 if( !account.valid() )
154 throw NoSuchUser( name ) ;
157 id.m_sid = account.sid ;
158 return std::make_pair(
id , account.name ) ;
161std::pair<G::Identity,std::string>
G::Identity::lookup( std::string_view name , std::nothrow_t )
163 auto account = IdentityImp::lookup( name ,
true ) ;
164 if( account.valid() )
167 id.m_sid = account.sid ;
168 return std::make_pair(
id , account.name ) ;
172 return std::make_pair( Identity() , std::string() ) ;
183 return G::Range::within( range , userid() ) ;
188std::string G::IdentityImp::sidstr( PSID sid_p )
190 return G::nowide::convertSidToStringSid( sid_p ) ;
193std::string G::IdentityImp::sid()
195 HANDLE htoken = NULL ;
196 if( !OpenProcessToken( GetCurrentProcess() , TOKEN_QUERY , &htoken ) )
198 G::ScopeExit close( [htoken](){CloseHandle(htoken);} ) ;
201 if( !GetTokenInformation( htoken , TokenUser , &buffer[0] ,
static_cast<DWORD
>(buffer.size()) , &size ) && size )
202 buffer.resize(
static_cast<std::size_t
>(size) ) ;
203 if( !GetTokenInformation( htoken , TokenUser , &buffer[0] ,
static_cast<DWORD
>(buffer.size()) , &size ) )
205 TOKEN_USER * info_p = G::buffer_cast<TOKEN_USER*>( buffer ) ;
206 return sidstr( info_p->User.Sid ) ;
209std::string G::IdentityImp::computername()
211 return nowide::getComputerNameEx() ;
214G::IdentityImp::Account G::IdentityImp::lookup( std::string_view name ,
bool with_canonical_name )
216 const Account error ;
217 if( name.empty() || name.find(
'\\') != std::string::npos )
219 std::string domain = computername() ;
222 std::string full_name = domain.append(1U,
'\\').append(name.data(),name.size()) ;
225 DWORD domainsize = 0 ;
226 SID_NAME_USE type = SidTypeInvalid ;
227 if( nowide::lookupAccountName( full_name , NULL , &sidsize ,
false , &domainsize , &type ) )
230 if( !nowide::lookupAccountName( full_name , sidbuffer.data() , &sidsize ,
true , &domainsize , &type ) )
232 SID * sid_p = G::buffer_cast<SID*>(sidbuffer) ;
234 std::string canonical_name ;
235 if( with_canonical_name )
237 DWORD namebuffersize = 0 ;
238 DWORD domainbuffersize = 0 ;
239 if( nowide::lookupAccountSid( sid_p ,
nullptr ,
false , &namebuffersize ,
false , &domainbuffersize , &type ) )
241 if( !nowide::lookupAccountSid( sid_p , &canonical_name ,
true , &namebuffersize ,
true , &domainbuffersize , &type ) )
243 if( canonical_name.empty() )
247 return { type , sidstr(sid_p) , domain , canonical_name } ;
250std::string G::IdentityImp::rootsid()
254 WELL_KNOWN_SID_TYPE type = WinLocalAccountAndAdministratorSid ;
255 if( !CreateWellKnownSid( type , NULL , &buffer[0] , &size ) && size )
256 buffer.resize(
static_cast<std::size_t
>(size) ) ;
257 if( !CreateWellKnownSid( type , NULL , &buffer[0] , &size ) )
259 SID * sid_p = G::buffer_cast<SID*>( buffer ) ;
260 return sidstr( sid_p ) ;
263G::IdentityImp::Account::Account( SID_NAME_USE type_ ,
const std::string & sid_ ,
264 const std::string & domain_ ,
const std::string & name_ ) :
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).
std::string sid() const
Returns the sid (Windows).
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.
A class that calls an exit function at the end of its scope.
static bool tailMatch(std::string_view in, std::string_view ending) noexcept
Returns true if the string has the given ending (or the given ending is empty).
static int toInt(std::string_view s)
Converts string 's' to an int.
static bool headMatch(std::string_view in, std::string_view head) noexcept
Returns true if the string has the given start (or head is empty).
static std::string tail(std::string_view in, std::size_t pos, std::string_view default_={})
Returns the last part of the string after the given position.
Contains inline functions that convert to and from UTF-8 strings in order to call wide-character "W()...
A substitute for std::vector<char> that has more useful alignment guarantees and explicitly avoids de...