| 1 |
// Copyright (c) 2016 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
#ifndef WINPTY_WINDOWS_SECURITY_H |
| 22 |
#define WINPTY_WINDOWS_SECURITY_H |
| 23 |
|
| 24 |
#include <windows.h> |
| 25 |
#include <aclapi.h> |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
#include <tuple> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
// PSID and PSECURITY_DESCRIPTOR are both pointers to void, but we want |
| 33 |
// Sid and SecurityDescriptor to be different types. |
| 34 |
struct SidTag { typedef PSID type; }; |
| 35 |
struct AclTag { typedef PACL type; }; |
| 36 |
struct SecurityDescriptorTag { typedef PSECURITY_DESCRIPTOR type; }; |
| 37 |
|
| 38 |
template <typename T> |
| 39 |
class SecurityItem { |
| 40 |
public: |
| 41 |
struct Impl { |
| 42 |
virtual ~Impl() {} |
| 43 |
}; |
| 44 |
|
| 45 |
private: |
| 46 |
typedef typename T::type P; |
| 47 |
P m_v; |
| 48 |
std::unique_ptr<Impl> m_pimpl; |
| 49 |
|
| 50 |
public: |
| 51 |
P get() const { return m_v; } |
| 52 |
operator bool() const { return m_v != nullptr; } |
| 53 |
|
| 54 |
SecurityItem() : m_v(nullptr) {} |
| 55 |
SecurityItem(P v, std::unique_ptr<Impl> &&pimpl) : |
| 56 |
m_v(v), m_pimpl(std::move(pimpl)) {} |
| 57 |
SecurityItem(SecurityItem &&other) : |
| 58 |
m_v(other.m_v), m_pimpl(std::move(other.m_pimpl)) { |
| 59 |
other.m_v = nullptr; |
| 60 |
} |
| 61 |
SecurityItem &operator=(SecurityItem &&other) { |
| 62 |
m_v = other.m_v; |
| 63 |
other.m_v = nullptr; |
| 64 |
m_pimpl = std::move(other.m_pimpl); |
| 65 |
return *this; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
typedef SecurityItem<SidTag> Sid; |
| 70 |
typedef SecurityItem<AclTag> Acl; |
| 71 |
typedef SecurityItem<SecurityDescriptorTag> SecurityDescriptor; |
| 72 |
|
| 73 |
Sid getOwnerSid(); |
| 74 |
Sid wellKnownSid( |
| 75 |
const wchar_t *debuggingName, |
| 76 |
SID_IDENTIFIER_AUTHORITY authority, |
| 77 |
BYTE authorityCount, |
| 78 |
DWORD subAuthority0=0, |
| 79 |
DWORD subAuthority1=0); |
| 80 |
Sid builtinAdminsSid(); |
| 81 |
Sid localSystemSid(); |
| 82 |
Sid everyoneSid(); |
| 83 |
|
| 84 |
SecurityDescriptor createPipeSecurityDescriptorOwnerFullControl(); |
| 85 |
SecurityDescriptor createPipeSecurityDescriptorOwnerFullControlEveryoneWrite(); |
| 86 |
SecurityDescriptor getObjectSecurityDescriptor(HANDLE handle); |
| 87 |
|
| 88 |
std::wstring sidToString(PSID sid); |
| 89 |
Sid stringToSid(const std::wstring &str); |
| 90 |
SecurityDescriptor stringToSd(const std::wstring &str); |
| 91 |
std::wstring sdToString(PSECURITY_DESCRIPTOR sd); |
| 92 |
|
| 93 |
DWORD rejectRemoteClientsPipeFlag(); |
| 94 |
|
| 95 |
enum class GetNamedPipeClientProcessId_Result { |
| 96 |
Success, |
| 97 |
Failure, |
| 98 |
UnsupportedOs, |
| 99 |
}; |
| 100 |
|
| 101 |
std::tuple<GetNamedPipeClientProcessId_Result, DWORD, DWORD> |
| 102 |
getNamedPipeClientProcessId(HANDLE serverPipe); |
| 103 |
|
| 104 |
#endif // WINPTY_WINDOWS_SECURITY_H |