Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // Security.h |
| 20 | |
| 21 | // Wrapper classes for a few Windows NT security structures/functions |
| 22 | // that are used by VNC |
| 23 | |
| 24 | #ifndef __RFB_WIN32_SECURITY_H__ |
| 25 | #define __RFB_WIN32_SECURITY_H__ |
| 26 | |
| 27 | #include <rdr/types.h> |
| 28 | #include <rdr/Exception.h> |
| 29 | #include <rfb_win32/Win32Util.h> |
| 30 | #include <rfb_win32/TCharArray.h> |
| 31 | |
| 32 | #include <lmcons.h> |
| 33 | #include <Accctrl.h> |
| 34 | #include <aclapi.h> |
| 35 | |
| 36 | #include <list> |
| 37 | |
| 38 | namespace rfb { |
| 39 | |
| 40 | namespace win32 { |
| 41 | |
| 42 | struct Trustee : public TRUSTEE { |
| 43 | Trustee(const TCHAR* name, |
| 44 | TRUSTEE_FORM form=TRUSTEE_IS_NAME, |
| 45 | TRUSTEE_TYPE type=TRUSTEE_IS_UNKNOWN) |
| 46 | { |
| 47 | pMultipleTrustee = 0; |
| 48 | MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; |
| 49 | TrusteeForm = form; |
| 50 | TrusteeType = type; |
| 51 | ptstrName = (TCHAR*)name; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | struct ExplicitAccess : public EXPLICIT_ACCESS { |
| 56 | ExplicitAccess(const TCHAR* name, |
| 57 | TRUSTEE_FORM type, |
| 58 | DWORD perms, |
| 59 | ACCESS_MODE mode, |
| 60 | DWORD inherit=0) |
| 61 | { |
| 62 | Trustee = rfb::win32::Trustee(name, type); |
| 63 | grfAccessPermissions = perms; |
| 64 | grfAccessMode = mode; |
| 65 | grfInheritance = inherit; |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | // Helper class for building access control lists |
| 70 | struct AccessEntries { |
| 71 | AccessEntries() : entries(0), entry_count(0) {} |
| 72 | ~AccessEntries() {delete [] entries;} |
| 73 | void allocMinEntries(int count) { |
| 74 | if (count > entry_count) { |
| 75 | EXPLICIT_ACCESS* new_entries = new EXPLICIT_ACCESS[entry_count+1]; |
| 76 | if (entries) { |
| 77 | memcpy(new_entries, entries, sizeof(EXPLICIT_ACCESS) * entry_count); |
| 78 | delete entries; |
| 79 | } |
| 80 | entries = new_entries; |
| 81 | } |
| 82 | } |
| 83 | void addEntry(const TCHAR* trusteeName, |
| 84 | DWORD permissions, |
| 85 | ACCESS_MODE mode) |
| 86 | { |
| 87 | allocMinEntries(entry_count+1); |
| 88 | ZeroMemory(&entries[entry_count], sizeof(EXPLICIT_ACCESS)); |
| 89 | entries[entry_count] = ExplicitAccess(trusteeName, TRUSTEE_IS_NAME, permissions, mode); |
| 90 | entry_count++; |
| 91 | } |
| 92 | void addEntry(const PSID sid, DWORD permissions, ACCESS_MODE mode) { |
| 93 | allocMinEntries(entry_count+1); |
| 94 | ZeroMemory(&entries[entry_count], sizeof(EXPLICIT_ACCESS)); |
| 95 | entries[entry_count] = ExplicitAccess((TCHAR*)sid, TRUSTEE_IS_SID, permissions, mode); |
| 96 | entry_count++; |
| 97 | } |
| 98 | |
| 99 | EXPLICIT_ACCESS* entries; |
| 100 | int entry_count; |
| 101 | }; |
| 102 | |
| 103 | // Helper class for handling SIDs |
| 104 | struct Sid { |
| 105 | Sid() : sid(0) {} |
| 106 | Sid(PSID sid_) : sid(sid_) {} |
| 107 | ~Sid() { |
| 108 | if (sid) FreeSid(sid); |
| 109 | } |
| 110 | operator PSID() const {return sid;} |
| 111 | PSID operator=(const PSID sid_) { |
| 112 | if (sid) FreeSid(sid); |
| 113 | sid = sid_; |
| 114 | } |
| 115 | |
| 116 | static PSID Administrators() { |
| 117 | PSID sid = 0; |
| 118 | SID_IDENTIFIER_AUTHORITY ntAuth = SECURITY_NT_AUTHORITY; |
| 119 | if (!AllocateAndInitializeSid(&ntAuth, 2, |
| 120 | SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, |
| 121 | 0, 0, 0, 0, 0, 0, |
| 122 | &sid)) |
| 123 | throw rdr::SystemException("Sid::Administrators", GetLastError()); |
| 124 | return sid; |
| 125 | } |
| 126 | static PSID SYSTEM() { |
| 127 | PSID sid = 0; |
| 128 | SID_IDENTIFIER_AUTHORITY ntAuth = SECURITY_NT_AUTHORITY; |
| 129 | if (!AllocateAndInitializeSid(&ntAuth, 1, |
| 130 | SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, |
| 131 | &sid)) |
| 132 | throw rdr::SystemException("Sid::SYSTEM", GetLastError()); |
| 133 | return sid; |
| 134 | } |
| 135 | |
| 136 | protected: |
| 137 | PSID sid; |
| 138 | }; |
| 139 | |
| 140 | // Helper class for handling & freeing ACLs |
| 141 | struct AccessControlList : public LocalMem { |
| 142 | AccessControlList(int size) : LocalMem(size) {} |
| 143 | AccessControlList(PACL acl_=0) : LocalMem(acl_) {} |
| 144 | operator PACL() {return (PACL)ptr;} |
| 145 | }; |
| 146 | |
| 147 | // Create a new ACL based on supplied entries and, if supplied, existing ACL |
| 148 | static PACL CreateACL(const AccessEntries& ae, PACL existing_acl=0) { |
| 149 | typedef DWORD (WINAPI *_SetEntriesInAcl_proto) (ULONG, PEXPLICIT_ACCESS, PACL, PACL*); |
| 150 | #ifdef UNICODE |
| 151 | const char* fnName = "SetEntriesInAclW"; |
| 152 | #else |
| 153 | const char* fnName = "SetEntriesInAclA"; |
| 154 | #endif |
| 155 | DynamicFn<_SetEntriesInAcl_proto> _SetEntriesInAcl(_T("advapi32.dll"), fnName); |
| 156 | if (!_SetEntriesInAcl.isValid()) |
| 157 | throw rdr::SystemException("CreateACL failed; no SetEntriesInAcl", ERROR_CALL_NOT_IMPLEMENTED); |
| 158 | PACL new_dacl; |
| 159 | DWORD result; |
| 160 | if ((result = (*_SetEntriesInAcl)(ae.entry_count, ae.entries, existing_acl, &new_dacl)) != ERROR_SUCCESS) |
| 161 | throw rdr::SystemException("SetEntriesInAcl", result); |
| 162 | return new_dacl; |
| 163 | } |
| 164 | |
| 165 | // Helper class for memory-management of self-relative SecurityDescriptors |
| 166 | struct SecurityDescriptorPtr : LocalMem { |
| 167 | SecurityDescriptorPtr(int size) : LocalMem(size) {} |
| 168 | SecurityDescriptorPtr(PSECURITY_DESCRIPTOR sd_=0) : LocalMem(sd_) {} |
| 169 | PSECURITY_DESCRIPTOR takeSD() {return takePtr();} |
| 170 | }; |
| 171 | |
| 172 | // Create a new self-relative Security Descriptor, owned by SYSTEM/Administrators, |
| 173 | // with the supplied DACL and no SACL. The returned value can be assigned |
| 174 | // to a SecurityDescriptorPtr to be managed. |
| 175 | static PSECURITY_DESCRIPTOR CreateSdWithDacl(const PACL dacl) { |
| 176 | SECURITY_DESCRIPTOR absSD; |
| 177 | if (!InitializeSecurityDescriptor(&absSD, SECURITY_DESCRIPTOR_REVISION)) |
| 178 | throw rdr::SystemException("InitializeSecurityDescriptor", GetLastError()); |
| 179 | Sid owner(Sid::SYSTEM()); |
| 180 | if (!SetSecurityDescriptorOwner(&absSD, owner, FALSE)) |
| 181 | throw rdr::SystemException("SetSecurityDescriptorOwner", GetLastError()); |
| 182 | Sid group(Sid::Administrators()); |
| 183 | if (!SetSecurityDescriptorGroup(&absSD, group, FALSE)) |
| 184 | throw rdr::SystemException("SetSecurityDescriptorGroupp", GetLastError()); |
| 185 | if (!SetSecurityDescriptorDacl(&absSD, TRUE, dacl, FALSE)) |
| 186 | throw rdr::SystemException("SetSecurityDescriptorDacl", GetLastError()); |
| 187 | DWORD sdSize = GetSecurityDescriptorLength(&absSD); |
| 188 | SecurityDescriptorPtr sd(sdSize); |
| 189 | if (!MakeSelfRelativeSD(&absSD, sd, &sdSize)) |
| 190 | throw rdr::SystemException("MakeSelfRelativeSD", GetLastError()); |
| 191 | return sd.takeSD(); |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | #endif |