vncExportACL.cpp
6.72 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
214
215
216
217
218
219
220
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004 Martin Scharpf. All Rights Reserved.
// Based on Felix Kasza's DumpACL (http://win32.mvps.org/security/dumpacl.html).
//
// 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 <wchar.h>
#include <tchar.h>
#include "vncExportACL.h"
// Get ACL from regkey HKLM\Software\ORL\WinVNC3\ACL
bool vncExportACL::GetACL(PACL *pACL){
HKEY hk = NULL;
DWORD dwValueLength = SECURITY_DESCRIPTOR_MIN_LENGTH;
_ftprintf(stderr, _T("== Entering GetACL\n"));
__try{
if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("Software\\ORL\\WinVNC3"),
0, KEY_QUERY_VALUE, &hk )){
_ftprintf(stderr, _T("== Error %d: RegOpenKeyEx"), GetLastError());
__leave;
}
// Read the ACL value from the VNC registry key
// First call to RegQueryValueEx just gets the buffer length.
if (ERROR_SUCCESS != RegQueryValueEx(hk, // subkey handle
_T("ACL"), // value name
0, // must be zero
0, // value type , not needed here
NULL, //
&dwValueLength)){ // length of value data
_ftprintf(stderr, _T("== Error %d: RegQueryValueEx 1\tValueLength = %d\n"),
GetLastError(), dwValueLength);
__leave;
}
*pACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwValueLength);
if (ERROR_SUCCESS != RegQueryValueEx(hk, // subkey handle
_T("ACL"), // value name
0, // must be zero
0, // value type
(LPBYTE) *pACL,
&dwValueLength)){ // length of value data
_ftprintf(stderr, _T("== Error %d: RegQueryValueEx 2\n"), GetLastError());
__leave;
}
_ftprintf(stderr, _T("== RegQueryValueEx passed\tdwValueLength = %d\n"), dwValueLength);
} __finally {
if (hk)
RegCloseKey(hk);
}
return true;
}
const TCHAR *vncExportACL::SidToText( PSID psid )
{
// S-rev- + SIA + subauthlen*maxsubauth + terminator
static TCHAR buf[15 + 12 + 12*SID_MAX_SUB_AUTHORITIES + 1];
TCHAR *p = &buf[0];
PSID_IDENTIFIER_AUTHORITY psia;
DWORD numSubAuths, i;
// Validate the binary SID.
if (!IsValidSid(psid))
return FALSE;
psia = GetSidIdentifierAuthority(psid);
p = buf;
p += _sntprintf(p, &buf[sizeof buf] - p, _T("S-%lu-"), 0x0f & *((byte *)psid));
if ((psia->Value[0] != 0) || (psia->Value[1] != 0))
p += _sntprintf( p, &buf[sizeof buf] - p, _T("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
(USHORT) psia->Value[0], (USHORT) psia->Value[1],
(USHORT) psia->Value[2], (USHORT) psia->Value[3],
(USHORT) psia->Value[4], (USHORT) psia->Value[5] );
else
p += _sntprintf(p, &buf[sizeof buf] - p, _T("%lu"), (ULONG) (psia->Value[5]) +
(ULONG) (psia->Value[4] << 8) + (ULONG) (psia->Value[3] << 16) +
(ULONG) (psia->Value[2] << 24));
// Add SID subauthorities to the string.
numSubAuths = *GetSidSubAuthorityCount(psid);
for (i = 0; i < numSubAuths; ++ i)
p += _sntprintf(p, &buf[sizeof buf] - p, _T("-%lu"), *GetSidSubAuthority(psid, i));
return buf;
}
void vncExportACL::PrintSid(PSID psid)
{
TCHAR name[256], domain[256];
DWORD cbname = sizeof name, cbdomain = sizeof domain, rc;
SID_NAME_USE sidUse;
//!! next line has hardcoded server name !!
// NULL server name is usually appropriate, though.
if (LookupAccountSid(NULL, psid, name, &cbname, domain, &cbdomain, &sidUse))
{
//Todo: Check if WellKnownSID and reserve special names for them.
/* switch ( sidUse )
{
case SidTypeWellKnownGroup: type = "well-known group"; break;
default: type = "bad sidUse value"; break;
}
*/
LPWKSTA_INFO_100 wkstainfo = NULL;
NET_API_STATUS nStatus;
TCHAR langroup[MAXLEN];
TCHAR computername[MAXLEN];
nStatus = NetWkstaGetInfo( 0 , 100 , (LPBYTE *) &wkstainfo);
if (nStatus == NERR_Success){
_tcsncpy(langroup, wkstainfo->wki100_langroup, MAXLEN);
_tcsncpy(computername, wkstainfo->wki100_computername, MAXLEN);
langroup[MAXLEN - 1] = _T('\0');
computername[MAXLEN - 1] = _T('\0');
// replace computername with a dot
if (_tcsicmp(computername, domain) == 0)
_tcscpy(domain,_T("."));
else if (_tcsicmp(langroup, domain) == 0)
_tcscpy(domain, _T(".."));
}
else
_tprintf(_T("NetWkstaGetInfo() returned %lu \n"), wkstainfo);
NetApiBufferFree(wkstainfo);
wkstainfo = NULL;
// If domain or username contains whitespace, print enclosing quotes
if (_tcschr(domain, _T(' ')) || _tcschr(name, _T(' ')))
_tprintf(_T("\"%s%s%s\"\n"), domain, (domain == 0 || *domain == _T('\0'))? _T(""): _T("\\"), name);
else
_tprintf(_T("%s%s%s\n"), domain, (domain == 0 || *domain == _T('\0'))? _T(""): _T("\\"), name);
}
else
{
rc = GetLastError();
_tprintf(_T("[%s] *** error %lu\n"), SidToText( psid ), rc);
}
}
void vncExportACL::PrintAce(int index, PACL acl){
ACE_HEADER *ace;
TCHAR *type;
PSID psid;
if (!GetAce(acl, index, (void **) &ace)) {
_tprintf(_T("DACL, entry %d: GetAce() failed, gle == %lu\n"),
index, GetLastError());
return;
}
switch ( ace->AceType ) {
case ACCESS_ALLOWED_ACE_TYPE:
type = _T("allow");
psid = &((ACCESS_ALLOWED_ACE *) ace)->SidStart;
break;
case ACCESS_DENIED_ACE_TYPE:
type = _T("deny");
psid = &((ACCESS_DENIED_ACE *) ace)->SidStart;
break;
default:
type = _T("invalid");
psid = &((ACCESS_ALLOWED_ACE *) ace)->SidStart;
break;
}
_tprintf(_T("%s\t"), type);
_tprintf(_T("0x%08lX\t"), ((ACCESS_ALLOWED_ACE *) ace)->Mask);
PrintSid( psid );
}
void vncExportACL::PrintAcl(PACL acl)
{
DWORD i;
ACL_SIZE_INFORMATION aci;
if ( acl == 0 )
return;
if (!GetAclInformation( acl, &aci, sizeof aci, AclSizeInformation)) {
_tprintf(_T("GAI(): gle == %lu\n"), GetLastError());
return;
}
for ( i = 0; i < aci.AceCount; ++ i )
PrintAce(i, acl);
}