chsvlib
chsv helper source code
chsvwinutilex\CreateDACL.cpp

Demonstrates a usage of the Chusov::Win32::Security::CreateDACL functions with STL iterators, STL containers and with initializer lists. Also, demonstrates some other elements of the chsvwinutil.h header.

#include <iostream>
#include <list>
#include <memory>
#include "chsvwinutilex.h"
using namespace Chusov;
using namespace Chusov::Win32;
using namespace Chusov::Win32::Security;
typedef Chusov::Memory::raw_delete_t<ACCESS_DENIED_ACE> some_deleter; //chosen just to compile the code, but might be any
std::unique_ptr<ACCESS_DENIED_ACE, some_deleter> GetEavesdropperIdentity()
{
return CreateACE<ACCESS_DENIED_ACE_TYPE>(GetSidByPrincipalName(L"EavesdropperOfThisComputer").get(), GENERIC_ALL);
}
AutoACL IteratorDACL()
{
std::list<ACCESS_ALLOWED_ACE_INFO> _allowed({{L"Administrator", GENERIC_ALL}, {L"\\\\Office\\Observer", GENERIC_READ}, {L"TrustedDomain\\TrustedUser", GENERIC_READ | GENERIC_WRITE}});
std::list<std::unique_ptr<ACCESS_DENIED_ACE, some_deleter>> _denied;
_denied.emplace_back(GetEavesdropperIdentity());
return CreateDACL(_allowed.begin(), _allowed.end(), _denied.begin(), _denied.end());
}
AutoACL ListDACL()
{
std::list<ACCESS_ALLOWED_ACE_INFO> _allowed({{L"Administrator", GENERIC_ALL}, {L"\\\\Office\\Observer", GENERIC_READ}, {L"TrustedDomain\\TrustedUser", GENERIC_READ | GENERIC_WRITE}});
std::list<std::unique_ptr<ACCESS_DENIED_ACE, some_deleter>> _denied;
_denied.emplace_back(GetEavesdropperIdentity());
return CreateDACL(_allowed, _denied);
}
int wmain(int argc, wchar_t **argv)
{
try
{
auto strCreate = GDI::GetUIFileNameForRead(NULL, L"Choose a file to set its security attributes", L"All files\0*.*\0\0", L"*.txt");
if (strCreate.empty())
return -1; //if the user has cancelled the opening, the program returns
DWORD dwErr = SetNamedSecurityInfoW(const_cast<LPWSTR>(strCreate.c_str()), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
{
{GetNameByPrincipalSid(GetWellKnownSid(WinBuiltinAdministratorsSid).get()).c_str(), GENERIC_ALL},
{GetNameByPrincipalSid(GetWellKnownSid(WinBuiltinUsersSid).get()), FILE_GENERIC_WRITE | FILE_GENERIC_READ | FILE_GENERIC_EXECUTE},
{L"\\\\Office\\Observer", FILE_GENERIC_READ}
},
{
{L"EavesdropperOfThisComputer", FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | FILE_GENERIC_EXECUTE},
{GetNameByPrincipalSid(GetWellKnownSid(WinBuiltinGuestsSid).get()).c_str(), GENERIC_READ}
}).get(), NULL);
if (dwErr != ERROR_SUCCESS)
{
std::wcerr << L"Exception caught: " << ex.What() << std::endl;
}
return 0;
}
Helper functionality for working with Win32 APIs.
Defines a class of objects specifying various exceptions with extended support for description format...
Definition: chsvexception.h:392
virtual const wchar_t * What() const noexcept
Returns a description, corresponding to the exception, represented as a wide string.
Definition: chsvexception.h:573
void ChsvExceptionByWin32Code(DWORD nCode, va_list ap, const wchar_t *pszDescription)
Creates an instance of the Chusov::Exceptions::ChsvCodeExceptionTempl class and throws it as an excep...
Definition: chsvwinutilex.h:156
std::basic_string< wchar_t, traits_t, alloc_t > GetUIFileNameForRead(HWND hParent=NULL, LPCWSTR lpszDialogTitle=NULL, LPCWSTR lpszFilter=NULL, LPCWSTR lpszDefExt=NULL, LPCWSTR lpszInitialDir=NULL, bool fShowReadOnlyFiles=true, bool fCreatePrompt=false, bool fAddToRecent=true, bool fFileMustExist=true)
Displays standard explorer-style Windows dialog box offering a selection of a file to be opened for r...
Definition: chsvwinutilex.h:2255
AutoSID GetWellKnownSid(sid_authority_t authority_id, DWORD dwSubAuthority0, DWORD dwSubAuthority1, DWORD dwSubAuthority2, DWORD dwSubAuthority3, DWORD dwSubAuthority4, DWORD dwSubAuthority5, DWORD dwSubAuthority6, DWORD dwSubAuthority7)
A wrapper over the Windows API AllocateAndInitializeSid function used to create well-known security i...
Definition: chsvwinutilex.h:2025
AutoToken GetCurrentImpersonationToken()
Returns a handle to an impersonation token of the calling thread. If the thread does not have one,...
Definition: chsvwinutilex.h:607
AutoACL CreateDACL(AllowedInputIterator itAccessAllowedACEsBegin, AllowedInputIterator itAccessAllowedACEsEnd, DeniedInputIterator itAccessDeniedACEsBegin, DeniedInputIterator itAccessDeniedACEsEnd, DWORD dwAclRevision=ACL_REVISION)
Creates a discretionary access control list (DACL) from two sequences of access-allowed and access-de...
Definition: chsvwinutilex.h:1536
AutoSID GetTokenUser(HANDLE hToken)
Returns a Windows security identifier of a principal, described by the specified token.
Definition: chsvwinutilex.h:765
AutoSID GetSidByPrincipalName(LPCWSTR pszPrincipalName)
Returns a smart AutoSID pointer to a security identifier of a principal, specified by its name.
Definition: chsvwinutilex.h:510
std::basic_string< wchar_t, traits_t, alloc_t > GetNameByPrincipalSid(PSID pPrincipalSid)
Returns a string describing domain and user names which correspond to the specified security identifi...
Definition: chsvwinutilex.h:575
A namespace containing mechanisms simplifying working with Windows security APIs, such as security de...
Definition: chsvwinutilex.h:447
std::unique_ptr< typename std::remove_pointer< PACL >::type, Chusov::Memory::raw_delete_t< typename std::remove_pointer< PACL >::type > > AutoACL
An alias for a type of smart PACL pointers to Windows access-control lists, such as a discretionary a...
Definition: chsvwinutilex.h:483
Axillary chsvlib elements simplifying calls to Win32 API.
Definition: chsvwinutil.h:103
Basic chsvlib namespace.
Definition: chsverr.h:312
A type of a deleter object used with smart pointers to a raw memory block allocated using the global ...
Definition: chsvmem.h:4186