From 512a25658dd6b737a16b91fb7eba43deef004d87 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 18 Oct 2020 04:05:48 +0300 Subject: add Sid class It's another newcomer straight from privilege-check. --- include/winapi/sid.hpp | 43 +++++++++++++++++++++++++++++++++++++ include/winapi/utils.hpp | 24 +++++++++++++++++++++ include/winapi/workarounds.hpp | 8 ------- src/cmd_line.cpp | 5 +---- src/handle.cpp | 2 +- src/sid.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ test/unit_tests/sid.cpp | 19 +++++++++++++++++ 7 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 include/winapi/sid.hpp create mode 100644 include/winapi/utils.hpp delete mode 100644 include/winapi/workarounds.hpp create mode 100644 src/sid.cpp create mode 100644 test/unit_tests/sid.cpp diff --git a/include/winapi/sid.hpp b/include/winapi/sid.hpp new file mode 100644 index 0000000..f418b89 --- /dev/null +++ b/include/winapi/sid.hpp @@ -0,0 +1,43 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#include "buffer.hpp" + +#include + +#include + +#include +#include + +namespace winapi { + +class Sid { +public: + BOOST_STATIC_CONSTEXPR std::size_t MAX_SID_SIZE = SECURITY_MAX_SID_SIZE; + + typedef SID Impl; + + static Sid well_known(WELL_KNOWN_SID_TYPE type); + + static Sid builtin_administrators(); + + explicit Sid(const Buffer& buffer) : m_buffer(buffer) {} + + explicit operator SID&() { return get_impl(); } + explicit operator const SID&() const { return get_impl(); } + + std::string to_string() const; + +private: + Impl& get_impl() { return *reinterpret_cast(m_buffer.data()); } + const Impl& get_impl() const { return *reinterpret_cast(m_buffer.data()); } + + Buffer m_buffer; +}; + +} // namespace winapi diff --git a/include/winapi/utils.hpp b/include/winapi/utils.hpp new file mode 100644 index 0000000..8e8407c --- /dev/null +++ b/include/winapi/utils.hpp @@ -0,0 +1,24 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#include + +#include + +#define WINAPI_UNUSED_PARAMETER(...) (void)(__VA_ARGS__) + +namespace winapi { + +struct LocalDelete { + void operator()(void* ptr) const { + const auto ret = ::LocalFree(ptr); + assert(ret == NULL); + WINAPI_UNUSED_PARAMETER(ret); + } +}; + +} // namespace winapi diff --git a/include/winapi/workarounds.hpp b/include/winapi/workarounds.hpp deleted file mode 100644 index a5a3607..0000000 --- a/include/winapi/workarounds.hpp +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) 2020 Egor Tensin -// This file is part of the "winapi-common" project. -// For details, see https://github.com/egor-tensin/winapi-common. -// Distributed under the MIT License. - -#pragma once - -#define WINAPI_UNUSED_PARAMETER(...) (void)(__VA_ARGS__) diff --git a/src/cmd_line.cpp b/src/cmd_line.cpp index eaf505f..d3dc781 100644 --- a/src/cmd_line.cpp +++ b/src/cmd_line.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -34,10 +35,6 @@ std::vector narrow_all(int argc, wchar_t** argv) { return utf; } -struct LocalDelete { - void operator()(wchar_t* argv[]) const { ::LocalFree(argv); } -}; - CommandLine do_parse(std::wstring src) { boost::trim(src); if (src.empty()) { diff --git a/src/handle.cpp b/src/handle.cpp index 1147cf6..c94cb16 100644 --- a/src/handle.cpp +++ b/src/handle.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include diff --git a/src/sid.cpp b/src/sid.cpp new file mode 100644 index 0000000..23be61a --- /dev/null +++ b/src/sid.cpp @@ -0,0 +1,48 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#include +#include +#include +#include +#include + +// clang-format off +#include +#include +// clang-format on + +#include +#include + +namespace winapi { + +Sid Sid::well_known(WELL_KNOWN_SID_TYPE type) { + Buffer buffer; + buffer.resize(MAX_SID_SIZE); + + DWORD cb = static_cast(buffer.size()); + + if (!::CreateWellKnownSid(type, NULL, buffer.data(), &cb)) + throw error::windows(GetLastError(), "CreateWellKnownSid"); + + buffer.resize(cb); + return Sid{buffer}; +} + +Sid Sid::builtin_administrators() { + return well_known(WinBuiltinAdministratorsSid); +} + +std::string Sid::to_string() const { + wchar_t* s = nullptr; + + if (!::ConvertSidToStringSidW(const_cast(&get_impl()), &s)) + throw error::windows(GetLastError(), "ConvertSidToStringSidW"); + + return narrow(std::unique_ptr{s}.get()); +} + +} // namespace winapi diff --git a/test/unit_tests/sid.cpp b/test/unit_tests/sid.cpp new file mode 100644 index 0000000..757f6f4 --- /dev/null +++ b/test/unit_tests/sid.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2020 Egor Tensin +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#include + +#include + +using namespace winapi; + +BOOST_AUTO_TEST_SUITE(sid_tests) + +BOOST_AUTO_TEST_CASE(builtin_administrators) { + const auto sid = Sid::builtin_administrators(); + BOOST_TEST(sid.to_string() == "S-1-5-32-544"); +} + +BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3