vncAccessControl.cpp
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004 Martin Scharpf. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check
// http://ultravnc.sourceforge.net/
#include "vncAccessControl.h"
/*
* GetACL: Gets ACL from reg and puts it in class variable pACL.
*/
PACL
vncAccessControl::GetACL(void){
HKEY hk = NULL;
PACL pInitACL = NULL;
DWORD dwValueLength = 0;
__try{
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\ORL\\WinVNC3"),
0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS){
__leave;
}
// Read the ACL value from the VNC registry key
// First call to RegQueryValueEx just gets the buffer length.
if (RegQueryValueEx(hk, _T("ACL"), 0, 0, NULL, &dwValueLength)
!= ERROR_SUCCESS){
__leave;
}
if (dwValueLength >= sizeof(ACL))
pInitACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwValueLength);
else {
__leave;
}
if (RegQueryValueEx(hk, _T("ACL"), 0, 0, (LPBYTE) pInitACL, &dwValueLength)
!= ERROR_SUCCESS){
__leave;
}
} __finally {
if (hk)
RegCloseKey(hk);
}
return pInitACL;
}
/*
* GetSD: Accessor function for SecurityDescriptor
* Creates an (absolute) SD from the GetACL return value
*/
PSECURITY_DESCRIPTOR
vncAccessControl::GetSD(){
PSECURITY_DESCRIPTOR pSD;
PSECURITY_DESCRIPTOR pSelfRelativeSD;
PACL pACL = NULL;
DWORD dwBufferLength = 0;
// If we can't retrieve a valid ACL we create an empty one (i.e. no access).
if (!(pACL = GetACL()) || !IsValidAcl(pACL)) {
pACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ACL));
// Initialize the new ACL.
if (!InitializeAcl(pACL, sizeof(ACL), ACL_REVISION)) {
; // Todo: Report an error.
}
}
// Construct SD
pSD = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, SECURITY_DESCRIPTOR_MIN_LENGTH);
if(InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION) &&
// Set our ACL to the SD.
SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE) &&
// AccessCheck() is picky about what is in the SD.
SetSecurityDescriptorOwner(pSD, GetOwnerSID(), FALSE) &&
SetSecurityDescriptorGroup(pSD, GetOwnerSID(), FALSE)) {
} else {
// Todo: Report an error.
}
// Make SD self-relative and use LocalAlloc
MakeSelfRelativeSD(pSD, NULL, &dwBufferLength);
pSelfRelativeSD = (PSECURITY_DESCRIPTOR) LocalAlloc(0, dwBufferLength);
MakeSelfRelativeSD(pSD, pSelfRelativeSD, &dwBufferLength);
FreeSD(pSD);
return pSelfRelativeSD;
}
/*
* SetSD: Changes the class variable pACL and the reg key.
* The ACL is the DACL of the provided SD.
*/
BOOL
vncAccessControl::SetSD(PSECURITY_DESCRIPTOR pSD){
BOOL isOK = FALSE;
BOOL bDaclPresent = FALSE;
BOOL bDaclDefaulted = FALSE;
PACL pDACL = NULL;
PACL pNewACL = NULL;
GetSecurityDescriptorDacl(pSD, &bDaclPresent, &pDACL, &bDaclDefaulted);
if (bDaclPresent && pDACL && IsValidAcl(pDACL) && StoreACL(pDACL))
isOK = TRUE;
return isOK;
}
/*
* GetOwnerSID: Helperfunction for SD creation
*/
PSID
vncAccessControl::GetOwnerSID(void){
PSID pAdminSid = NULL;
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
// Create a SID for the BUILTIN\Administrators group.
AllocateAndInitializeSid( &SIDAuth, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &pAdminSid);
return pAdminSid;
}
/*
* StoreACL: Stores the value of the class variable pACL in the registry.
* Returns TRUE if successful.
*/
BOOL
vncAccessControl::StoreACL(PACL pACL){
HKEY hk = NULL;
BOOL isSaveOK = FALSE;
ACL_SIZE_INFORMATION AclInfo = {0, 0, 0};
DWORD nAclInformationLength = sizeof(AclInfo);
// Todo: Better error handling
if (pACL)
GetAclInformation(pACL, &AclInfo, nAclInformationLength, AclSizeInformation);
__try{ if (RegCreateKey(HKEY_LOCAL_MACHINE, _T("Software\\ORL\\WinVNC3"), &hk)
!= ERROR_SUCCESS){
__leave;
}
if (hk)
RegCloseKey(hk);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\ORL\\WinVNC3"), 0, KEY_SET_VALUE, &hk)
!= ERROR_SUCCESS){
__leave;
}
if (RegSetValueEx(hk, _T("ACL"), 0, REG_BINARY, (LPBYTE) pACL, AclInfo.AclBytesInUse)
!= ERROR_SUCCESS){
__leave;
}
isSaveOK = TRUE;
} __finally {
if (hk)
RegCloseKey(hk);
}
return isSaveOK;
}
/*
* FreeSD: Frees the memory of an absolute SD.
*/
void
vncAccessControl::FreeSD(PSECURITY_DESCRIPTOR pSD){
PSID pOwnerSID = NULL;
PSID pGroupSID = NULL;
PACL pDACL = NULL;
PACL pSACL = NULL;
BOOL bOwnerDefaulted = FALSE;
BOOL bGroupDefaulted = FALSE;
BOOL bDaclPresent = FALSE;
BOOL bDaclDefaulted = FALSE;
BOOL bSaclPresent = FALSE;
BOOL bSaclDefaulted = FALSE;
if (pSD) {
GetSecurityDescriptorOwner(pSD, &pOwnerSID, &bOwnerDefaulted);
GetSecurityDescriptorGroup(pSD, &pGroupSID, &bGroupDefaulted);
GetSecurityDescriptorDacl(pSD, &bDaclPresent, &pDACL, &bDaclDefaulted);
GetSecurityDescriptorSacl(pSD, &bSaclPresent, &pSACL, &bSaclDefaulted);
}
// Clean up
if (pSD)
HeapFree(GetProcessHeap(), 0, pSD);
if (bDaclPresent && pDACL)
HeapFree(GetProcessHeap(), 0, pDACL);
if (bSaclPresent && pSACL)
HeapFree(GetProcessHeap(), 0, pSACL);
if (pOwnerSID)
HeapFree(GetProcessHeap(), 0, pOwnerSID);
if (pGroupSID)
HeapFree(GetProcessHeap(), 0, pGroupSID);
}