SecurityDescriptor::SecurityDescriptor(RCString s) { PSECURITY_DESCRIPTOR pSD; ULONG size; Win32Check(::ConvertStringSecurityDescriptorToSecurityDescriptor(s, SDDL_REVISION_1, &pSD, &size)); m_blob = Blob(pSD, size); Win32Check(!::LocalFree(pSD)); }
void AccessToken::AdjustPrivilege(RCString spriv) { TOKEN_PRIVILEGES tp = { 1 }; Win32Check(::LookupPrivilegeValue(0, spriv, &tp.Privileges[0].Luid)); tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; ::SetLastError(0); Win32Check(::AdjustTokenPrivileges((HANDLE)(intptr_t)HandleAccess(_self), FALSE, &tp, 0, (PTOKEN_PRIVILEGES)0, 0) && !::GetLastError()); // can return TRUE even when ERROR_NOT_ALL_ASSIGNED }
SecurityDescriptor GetServiceSecurityDacl(ServiceController& service) { DWORD dw; if (::QueryServiceObjectSecurity(service.m_handle, DACL_SECURITY_INFORMATION, &dw, 0, &dw)) Throw(E_FAIL); Win32Check(::GetLastError()==ERROR_INSUFFICIENT_BUFFER); SECURITY_DESCRIPTOR *psd = (SECURITY_DESCRIPTOR*)alloca(dw); Win32Check(::QueryServiceObjectSecurity(service.m_handle, DACL_SECURITY_INFORMATION, psd, dw, &dw)); return SecurityDescriptor(psd); }
void Acl::AddAccessAllowedAce(DWORD dwAceRevision, DWORD AccessMask, const Sid& sid) { if (::AddAccessAllowedAce(_self, dwAceRevision, AccessMask, sid)) return; Win32Check(::GetLastError()==ERROR_ALLOTTED_SPACE_EXCEEDED); size_t cbAcl = m_blob.Size+sizeof(ACCESS_ALLOWED_ACE)+sid.Size; Acl nacl(cbAcl); CopyTo(nacl); m_blob = nacl.m_blob; Win32Check(::AddAccessAllowedAce(_self, dwAceRevision, AccessMask, sid)); }
AbsoluteSecurityDescriptor::AbsoluteSecurityDescriptor(const SecurityDescriptor& sd) { DWORD dwSD = 0, dwOwner = 0, dwGroup = 0, dwDacl = 0, dwSacl = 0; if (::MakeAbsoluteSD(sd, NULL, &dwSD, NULL, &dwDacl, NULL, &dwSacl, NULL, &dwOwner, NULL, &dwGroup)) Throw(E_FAIL); Win32Check(::GetLastError() == ERROR_INSUFFICIENT_BUFFER); m_blob.Size = dwSD; m_owner.Size = dwOwner; m_group.Size = dwGroup; m_dacl.Size = dwDacl; m_sacl.Size = dwSacl; Win32Check(::MakeAbsoluteSD(sd, m_blob.data(), &dwSD, (PACL)m_dacl.data(), &dwDacl, (PACL)m_sacl.data(), &dwSacl, m_owner.data(), &dwOwner, m_group.data(), &dwGroup)); }
COperatingSystem::COsVerInfo COperatingSystem::get_Version() { # if UCFG_WDM RTL_OSVERSIONINFOEXW r = { sizeof r }; NtCheck(::RtlGetVersion((RTL_OSVERSIONINFOW*)&r)); # elif UCFG_WCE OSVERSIONINFO r = { sizeof r }; Win32Check(::GetVersionEx(&r)); # else OSVERSIONINFOEX r = { sizeof r }; Win32Check(::GetVersionEx((OSVERSIONINFO*)&r)); #endif return r; }
String Sid::ToString() const { LPTSTR pstr; Win32Check(::ConvertSidToStringSid(_self, &pstr)); String r = pstr; LocalFree(pstr); return r; }
void ServiceBase::put_Status(DWORD v) { SERVICE_STATUS st = { SERVICE_WIN32_OWN_PROCESS, m_status = v, 0, 0 }; if (CanStop) st.dwControlsAccepted |= SERVICE_ACCEPT_STOP; if (CanShutdown) st.dwControlsAccepted |= SERVICE_ACCEPT_SHUTDOWN; if (CanPauseAndContinue) st.dwControlsAccepted |= SERVICE_ACCEPT_PAUSE_CONTINUE; if (CanHandlePowerEvent) st.dwControlsAccepted |= SERVICE_ACCEPT_POWEREVENT; if (CanHandleSessionChangeEvent) st.dwControlsAccepted |= SERVICE_ACCEPT_SESSIONCHANGE; switch (v) { case SERVICE_STOP_PENDING: case SERVICE_START_PENDING: st.dwCheckPoint = 1; st.dwWaitHint = 1000; break; case SERVICE_STOPPED: if (ExitCode) { if (HRESULT_FACILITY(ExitCode) == FACILITY_WIN32) st.dwWin32ExitCode = HRESULT_CODE(ExitCode); else { st.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR; st.dwServiceSpecificExitCode = ExitCode; } } break; } Win32Check(::SetServiceStatus(ServiceHandle, &st)); }
void ServiceBase::Run() { SERVICE_TABLE_ENTRY serviceTable[2] = { { (LPTSTR)(LPCTSTR)ServiceName, &ServiceMainFunction }, { 0, 0 } }; s_pServiceBase = this; Win32Check(::StartServiceCtrlDispatcher(serviceTable)); }
Acl SecurityDescriptor::get_Dacl() { BOOL bDaclPresent, bDaclDefaulted; ACL *pacl; Win32Check(::GetSecurityDescriptorDacl(_self, &bDaclPresent, &pacl, &bDaclDefaulted)); if (!bDaclPresent) Throw(E_FAIL); return Acl(pacl); }
CStringVector AFXAPI COperatingSystem::QueryDosDevice(RCString dev) { for(int size=_MAX_PATH;; size*=2) { TCHAR *buf = (TCHAR*)alloca(sizeof(TCHAR)*size); DWORD dw = ::QueryDosDevice(dev, buf, size); if (dw && dw < size) return AsciizArrayToStringArray(buf); Win32Check(GetLastError() != ERROR_INSUFFICIENT_BUFFER); } }
void ServiceController::Start(const CStringVector& ar) { LPCTSTR *p = 0; if (!ar.empty()) { p = (LPCTSTR*)alloca(ar.size()*sizeof(LPCTSTR)); for (size_t i=0; i<ar.size(); ++i) p[i] = ar[i]; } Win32Check(::StartService(m_handle, (DWORD)ar.size(), p)); }
String Path::GetTruePath(RCString p) { #if UCFG_USE_POSIX char buf[PATH_MAX]; for (const char *psz = p;; psz = buf) { int len = ::readlink(psz, buf, sizeof(buf)-1); if (len == -1) { if (errno == EINVAL) return psz; CCheck(-1); } buf[len] = 0; } #elif UCFG_WIN32_FULL TCHAR buf[_MAX_PATH]; DWORD len = ::GetLongPathName(p, buf, _countof(buf)-1); Win32Check(len != 0); buf[len] = 0; typedef DWORD (WINAPI *PFN_GetFinalPathNameByHandle)(HANDLE hFile, LPTSTR lpszFilePath, DWORD cchFilePath, DWORD dwFlags); DlProcWrap<PFN_GetFinalPathNameByHandle> pfn("KERNEL32.DLL", EXT_WINAPI_WA_NAME(GetFinalPathNameByHandle)); if (!pfn) return buf; TCHAR buf2[_MAX_PATH]; File file; file.Attach(::CreateFile(buf, 0, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)); len = pfn(Handle(file), buf2, _countof(buf2)-1, 0); Win32Check(len != 0); buf2[len] = 0; #if UCFG_USE_REGEX wcmatch m; if (regex_search(buf2, m, s_reDosName)) return m[1]; #else String sbuf(buf2); //!!! incoplete check, better to use Regex int idx = sbuf.Find(':'); if (idx != -1) return sbuf.Mid(idx-1); #endif return buf2; #else return p; #endif }
ServiceController CSCManager::CreateService(RCString serviceName, RCString displayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword) { ServiceController result; Win32Check((result.m_handle = ::CreateService(m_handle, serviceName, displayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword)) != 0); return result; }
void __fastcall TVersionInfo::InitializeFixedFileInfoField(void) { LPVOID Buffer; UINT BufferLength; Win32Check( VerQueryValue(FFileVersionInfoData, L"\\", &Buffer, &BufferLength) ); assert(BufferLength != 0); assert(BufferLength == sizeof(FFixedFileInfo)); memmove(&FFixedFileInfo, Buffer, BufferLength); }
String Path::GetTempPath() { #if UCFG_USE_POSIX char buf[PATH_MAX+1] = "XXXXXX"; int fd = CCheck(::mkstemp(buf)); close(fd); return buf; #else TCHAR buf[MAX_PATH]; DWORD r = ::GetTempPath(_countof(buf), buf); Win32Check(r && r<_countof(buf)); //!!! need to improve return buf; #endif }
pair<String, UINT> Path::GetTempFileName(RCString path, RCString prefix, UINT uUnique) { #if UCFG_USE_POSIX char buf[PATH_MAX+1]; ZeroStruct(buf); int fd = CCheck(::mkstemp(strncpy(buf, Path::Combine(path, prefix+"XXXXXX"), _countof(buf)-1))); close(fd); return pair<String, UINT>(buf, 1); #else TCHAR buf[MAX_PATH]; UINT r = Win32Check(::GetTempFileName(path, prefix, uUnique, buf)); return pair<String, UINT>(buf, r); #endif }
int AFXAPI InetCheck(int i) { if (!i) { DWORD dw = GetLastError(); if (dw == ERROR_INTERNET_EXTENDED_ERROR) { TCHAR buf[256]; DWORD code, len = _countof(buf); Win32Check(::InternetGetLastResponseInfo(&code, buf, &len)); code = code; //!!!! todo } Throw(dw ? HRESULT_FROM_WIN32(dw) : E_EXT_UnknownWin32Error); } return i; }
Sid::operator SID *() const { if (!m_vec.empty()) return (SID*)&m_vec[0]; if (!m_pSid && !m_ssid.empty()) { vector<String> v = m_ssid.Split("-"); if (v[0] != "S") Throw(E_FAIL); int nSuba = v.size()-3; SID_IDENTIFIER_AUTHORITY auth = { 0 }; auth.Value[5] = (BYTE)atoi(v[2]) ; DWORD a[8] = { 0 }; for (int i=0; i<nSuba; i++) a[i] = atoi(v[3+i]); Win32Check(::AllocateAndInitializeSid(&auth, (BYTE)nSuba, a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], (PSID*)&m_pSid)); } return m_pSid; }
TDateTime __fastcall TVersionInfo::GetFileDate(void) const { FILETIME AFileTime; SYSTEMTIME ASystemTime; // The FILETIME structure is a 64-bit value representing the // number of 100-nanosecond intervals since January 1, 1601. // // We need to convert this to a TDateTime double value: // // The integral part of a TDateTime value is the number of days // that have passed since 12/30/1899. The fractional part of a // TDateTime value is the time of day. AFileTime = GetRawFileDate(); Win32Check( FileTimeToSystemTime(&AFileTime, &ASystemTime) ); return SystemTimeToDateTime(ASystemTime); }
void ServiceBase::ServiceMainCallback(const vector<String>& args) { TRC(1, ServiceName); ServiceHandle = ::RegisterServiceCtrlHandlerEx(ServiceName, &HandlerEx, this); Win32Check(ServiceHandle != 0); Status = SERVICE_START_PENDING; try { signal(SIGINT, SIG_DFL); // signals hang during logoff signal(SIGBREAK, SIG_DFL); OnStart(); Status = SERVICE_RUNNING; m_evStop.lock(); } catch (RCExc DBG_PARAM(ex)) { TRC(0, ex.what()); } Status = SERVICE_STOPPED; }
Acl::Acl(ACL *pacl) { ACL_SIZE_INFORMATION asi; Win32Check(::GetAclInformation(pacl, &asi, sizeof asi, AclSizeInformation)); m_blob = Blob(pacl, asi.AclBytesInUse); }
Sid AccessToken::get_User() { BYTE buf[1000]; DWORD dw; Win32Check(::GetTokenInformation((HANDLE)(intptr_t)HandleAccess(_self), TokenUser, buf, sizeof(buf), &dw)); return (SID*)((TOKEN_USER*)buf)->User.Sid; }
Ace Acl::operator[](int idx) { ACE_HEADER *pace; Win32Check(::GetAce(_self, idx, (void**)&pace)); return Ace(pace); }
void SecurityDescriptor::put_Dacl(const Acl& acl, bool bDaclDefaulted) { Win32Check(::SetSecurityDescriptorDacl(_self, true, acl, bDaclDefaulted)); }
Acl::Acl(size_t size) : m_blob(0, size) { Win32Check(::InitializeAcl(_self, size, ACL_REVISION)); }
size_t Acl::get_Count() const { ACL_SIZE_INFORMATION asi; Win32Check(::GetAclInformation(_self, &asi, sizeof asi, AclSizeInformation)); return asi.AceCount; }
void SetServiceSecurityDacl(ServiceController& service, const SecurityDescriptor& sd) { Win32Check(::SetServiceObjectSecurity(service.m_handle, DACL_SECURITY_INFORMATION, sd)); }
void Acl::Add(const Ace& ace, DWORD dwStartingAceIndex) { Win32Check(::AddAce(_self, ACL_REVISION, dwStartingAceIndex, ace, ace.Size)); }
AccessToken::AccessToken(Process& process) { HANDLE h; Win32Check(::OpenProcessToken((HANDLE)(intptr_t)HandleAccess(*process.m_pimpl), MAXIMUM_ALLOWED, &h)); Attach(h); }