39#include "blocxx/BLOCXX_config.h"
45#ifdef BLOCXX_HAVE_UNISTD_H
49#ifdef BLOCXX_HAVE_SYS_TYPES_H
53#ifdef BLOCXX_HAVE_PWD_H
66 HANDLE pToken = (HANDLE)0L;
67 DWORD bufLength = 256;
68 static int* tkUser[256];
70 if ( ::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &pToken) )
72 ::GetTokenInformation(pToken, TokenUser, tkUser, bufLength, &bufLength);
73 sid = ((TOKEN_USER*)tkUser)->User.Sid;
74 TCHAR sName[MAX_PATH], sDName[MAX_PATH];
75 DWORD sNameLen, sDNameLen = sNameLen = MAX_PATH;
77 ::LookupAccountSid( NULL, sid, sName, &sNameLen, sDName, &sDNameLen, &eUse);
101 PSID_IDENTIFIER_AUTHORITY psia;
102 DWORD dwSubAuthorities;
103 DWORD dwSidRev=SID_REVISION;
107 String strResult, strSubResult;
109 if (!uid || !IsValidSid(uid))
111 psia = GetSidIdentifierAuthority(uid);
112 dwSubAuthorities = *GetSidSubAuthorityCount(uid);
113 dwSidSize = (15 + 12 + (12 * dwSubAuthorities) + 1) *
sizeof(TCHAR);
114 if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
117 TEXT(
"0x%02hx%02hx%02hx%02hx%02hx%02hx"),
118 (USHORT)psia->Value[0],
119 (USHORT)psia->Value[1],
120 (USHORT)psia->Value[2],
121 (USHORT)psia->Value[3],
122 (USHORT)psia->Value[4],
123 (USHORT)psia->Value[5]);
129 (ULONG)(psia->Value[5] ) +
130 (ULONG)(psia->Value[4] << 8) +
131 (ULONG)(psia->Value[3] << 16) +
132 (ULONG)(psia->Value[2] << 24) );
135 for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
138 TEXT(
"%s-%lu"), strSubResult.
c_str(),
139 *GetSidSubAuthority(uid, dwCounter) );
142 strResult.
format(TEXT(
"S-%lu-%s"), dwSidRev, strSubResult);
145 return String(Int64(::geteuid()));
168 long getSysconfValue(
int name,
long default_value,
int& error)
171#pragma message(Reminder "TODO: Implement for Win if you use getSysconfValue not only for _SC_GETPW_R_SIZE_MAX")
173 return default_value;
177 long l = sysconf(
name);
185 return default_value;
190 return default_value;
201 long getSysconfValue(
int name,
long default_value)
204 return getSysconfValue(
name, default_value, unused);
217 TCHAR cchName[256], cchDomainName[256];
218 SID_NAME_USE snuOutVar;
219 DWORD cchNameBufLen =
sizeof(cchName), cchDomainNameBufLen =
sizeof(cchDomainName);
221 ok = ::LookupAccountSid(NULL,
226 &cchDomainNameBufLen,
231#ifdef BLOCXX_HAVE_GETPWUID_R
233 size_t const additionalSize =
234#ifdef _SC_GETPW_R_SIZE_MAX
235 getSysconfValue(_SC_GETPW_R_SIZE_MAX, 10240);
239 std::vector<char> additional(additionalSize);
244 rv = ::getpwuid_r(uid, &pw, &additional[0], additional.size(), &result);
247 additional.resize(additional.size() * 2);
249 }
while (rv == ERANGE);
252 passwd* result = ::getpwuid(uid);
257 return result->pw_name;
268 validUserName =
false;
272 static DWORD uid[64];
273 DWORD cbUid =
sizeof(uid) *
sizeof(DWORD);
274 SID_NAME_USE snuOutVar;
275 DWORD cbDomainBufSize = MAX_PATH;
276 TCHAR strDomainBuf[MAX_PATH] = {0};
278 return (validUserName=::LookupAccountName(
285 &snuOutVar))? &uid : NULL;
290#ifdef BLOCXX_HAVE_GETPWNAM_R
292#ifdef _SC_GETPW_R_SIZE_MAX
293 getSysconfValue(_SC_GETPW_R_SIZE_MAX, 10240);
297 std::vector<char> buf(bufsize);
303 rv = ::getpwnam_r(userName.
c_str(), &pwd, &buf[0], bufsize, &result);
306 buf.resize(buf.size() * 2);
308 }
while (rv == ERANGE);
317 struct passwd* result;
318 result = ::getpwnam(userName.
c_str());
322 validUserName =
true;
323 return result->pw_uid;
#define BLOCXX_GLOBAL_MUTEX_INIT()
This class can be used to store a global variable that is lazily initialized in a thread safe manner.
This String class is an abstract data type that represents as NULL terminated string of characters.
const char * c_str() const
int format(const char *fmt,...)
Format this string according to the given format and variable argument list (printf style)
String getEffectiveUserId()
Get the effective user id.
const UserID INVALID_USERID
String getCurrentUserName()
UserID getUserId(const String &userName, bool &validUserName)
Convert a textual username into a platform native user type.
String getUserName(uid_t uid, bool &ok)
If the username is invalid, or if getUserName() fails for any other reason, 'success' will be set to ...