vncImportACL.cpp
13.5 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/////////////////////////////////////////////////////////////////////////////
// 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 <wchar.h>
#include <tchar.h>
#include "vncImportACL.h"
vncImportACL::vncImportACL(){
lastAllowACE = NULL;
lastDenyACE = NULL;
}
vncImportACL::~vncImportACL(){
_ftprintf(stderr, _T("deleting ACE_DATA linked lists\n"));
ACE_DATA *tmp = NULL;
for (ACE_DATA *i = lastDenyACE; i != NULL; i = tmp){
if (i->pSID)
delete i->pSID;
tmp = i->next;
delete i;
}
for (ACE_DATA *j = lastAllowACE; j != NULL; j = tmp){
if (j->pSID)
delete j->pSID;
tmp = j->next;
delete j;
}
}
void vncImportACL::GetOldACL(){
PACL pOldACL = NULL;
ACL_SIZE_INFORMATION asi;
HKEY hk = NULL;
DWORD dwValueLength = SECURITY_DESCRIPTOR_MIN_LENGTH;
__try{
if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("Software\\ORL\\WinVNC3"),
0, KEY_QUERY_VALUE, &hk )){
__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
__leave;
}
pOldACL = (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) pOldACL,
&dwValueLength)){ // length of value data
__leave;
}
} __finally {
if (hk)
RegCloseKey(hk);
}
if ( pOldACL == NULL )
return;
if (!GetAclInformation( pOldACL, &asi, sizeof asi, AclSizeInformation)) {
_ftprintf(stderr, _T("GAI(): gle == %lu\n"), GetLastError());
return;
}
for (DWORD i = 0; i < asi.AceCount; ++ i)
ReadAce(i, pOldACL);
HeapFree(GetProcessHeap(), 0, pOldACL);
}
void vncImportACL::ReadAce(int index, PACL pACL){
ACE_HEADER *ace;
//char *type;
//PSID psid;
ACE_DATA * pACEdata;
if (!GetAce( pACL, index, (void **) &ace)) {
_ftprintf(stderr, _T("DACL, entry %d: GetAce() failed, gle == %lu\n"),
index, GetLastError());
return;
}
pACEdata = new ACE_DATA;
switch ( ace->AceType ) {
case ACCESS_ALLOWED_ACE_TYPE:
pACEdata->type = ACCESS_ALLOWED;
pACEdata->pSID = CopySID(&((ACCESS_ALLOWED_ACE *) ace)->SidStart);
pACEdata->mask = ((ACCESS_ALLOWED_ACE *) ace)->Mask;
pACEdata->next = lastAllowACE;
lastAllowACE = pACEdata;
break;
case ACCESS_DENIED_ACE_TYPE:
pACEdata->type = ACCESS_DENIED;
pACEdata->pSID = CopySID(&((ACCESS_DENIED_ACE *) ace)->SidStart);
pACEdata->mask = ((ACCESS_DENIED_ACE *) ace)->Mask;
pACEdata->next = lastDenyACE;
lastDenyACE = pACEdata;
break;
default:
_ftprintf(stderr, _T("Error: Unknown ACE type\n"));
delete pACEdata;
break;
}
}
PSID vncImportACL::CopySID(PSID pSID){
PSID pNewSID = NULL;
DWORD sidLength = 0;
if (IsValidSid(pSID)){
sidLength = GetLengthSid(pSID);
pNewSID = (PSID) new char[sidLength];
CopySid(sidLength, pNewSID, pSID);
}
return pNewSID;
}
int vncImportACL::ScanInput(){
int rc = 0;
vncScanInput scanInput;
do {
TCHAR linebuf[MAXLEN];
TCHAR *line = linebuf;
TCHAR *token[3];
TCHAR accesstype[MAXLEN];
TCHAR accessmaskstring[MAXLEN];
TCHAR domainaccount[MAXLEN];
DWORD accessmask = 0;
token[0] = accesstype;
token[1] = accessmaskstring;
token[2] = domainaccount;
if (!scanInput.GetLine(line)) {
rc |= 1;
break;
}
scanInput.RemoveNewline(line);
scanInput.RemoveComment(line);
if (scanInput.isEmptyLine(line))
continue;
if (scanInput.Tokenize(line, token)
&& _stscanf(accessmaskstring, _T("%x"), &accessmask) == 1) {
FillAceData(accesstype, accessmask, domainaccount);
} else {
_tprintf(_T("Error tokenizing line\n"));
rc |= 2;
}
} while(!feof(stdin));
return rc;
}
PACL vncImportACL::BuildACL(){
PACL pACL = NULL;
// Need canonical order and normalization??
// Solution canonical order: aceAllowStart and aceDenyStart?
// Solution normalization: Check for multiple occurrance of SID
// in allow list and deny list and merge them.
long aclSize = 8; // For ACL header
for (ACE_DATA *i = lastDenyACE; i != NULL; i = i->next){
aclSize += GetLengthSid(i->pSID) + sizeof(ACCESS_DENIED_ACE) - sizeof(DWORD);
}
for (ACE_DATA *j = lastAllowACE; j != NULL; j = j->next){
aclSize += GetLengthSid(j->pSID) + sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD);
}
pACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, aclSize);
// Initialize the new ACL.
if (InitializeAcl(pACL, aclSize, ACL_REVISION)) {
// Add the access-denied ACEs to the ACL.
for (ACE_DATA *i = lastDenyACE; i != NULL; i = i->next){
//Todo: add error handling.
AddAccessDeniedAce(pACL, ACL_REVISION, i->mask, i->pSID);
}
for (ACE_DATA *j = lastAllowACE; j != NULL; j = j->next){
//Todo: add error handling.
AddAccessAllowedAce(pACL, ACL_REVISION, j->mask, j->pSID);
}
}
return pACL;
}
bool vncImportACL::SetACL(PACL pACL){
HKEY hk = NULL;
bool isSaveOK = false;
ACL_SIZE_INFORMATION AclInformation = {0, 0, 0};
DWORD nAclInformationLength = sizeof(AclInformation);
// Todo: Better error handling
if (pACL)
GetAclInformation(pACL, &AclInformation, nAclInformationLength, AclSizeInformation);
__try{
if (ERROR_SUCCESS != RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("Software\\ORL\\WinVNC3"),
0, KEY_SET_VALUE, &hk )){
_ftprintf(stderr, _T("Error %d: RegOpenKeyEx\n"), GetLastError());
__leave;
}
// Add the ACL value to the VNC registry key
if (ERROR_SUCCESS != RegSetValueEx(hk,
_T("ACL"),
0,
REG_BINARY,
(LPBYTE) pACL,
AclInformation.AclBytesInUse)){
_ftprintf(stderr, _T("Error %d: RegSetValueEx\n"), GetLastError());
__leave;
}
isSaveOK = true;
_ftprintf(stderr, _T("RegSetValueEx passed\n"));
} __finally {
if (hk)
RegCloseKey(hk);
}
return isSaveOK;
}
bool vncImportACL::FillAceData(const TCHAR *type,
DWORD mask,
const TCHAR *user){
ACE_TYPE acetype;
TCHAR *domainaccount = NULL;
PSID pSID = NULL;
//type
if (_tcsicmp(type, _T("allow")) == 0)
acetype = ACCESS_ALLOWED;
else if (_tcsicmp(type, _T("deny")) == 0)
acetype = ACCESS_DENIED;
else {
_ftprintf(stderr, _T("error with ACE type, neither 'allow' nor 'deny'\n"));
return false;
}
//mask
mask &= ALL_RIGHTS;
// if name starts with ".\", replace the dot with the computername.
// if name starts with "..\", replace the dots with the domainname.
if (_tcsncmp(user, _T(".\\"), 2) == 0)
domainaccount = AddComputername(user);
else if (_tcsncmp(user, _T("..\\"), 2) == 0)
domainaccount = AddDomainname(user);
else {
domainaccount = new TCHAR[_tcslen(user)+1];
_tcscpy(domainaccount,user);
}
pSID = GetSID(domainaccount);
if (!pSID)
return false;
_tprintf(_T("account: %s, mask: %x, type: %s\n"),
domainaccount, mask, acetype == ACCESS_ALLOWED ? _T("allow") : _T("deny"));
ACE_DATA *ace = new ACE_DATA;
ace->type = acetype;
ace->mask = mask;
ace->pSID = pSID;
switch (acetype){
case ACCESS_ALLOWED:
ace->next = lastAllowACE;
lastAllowACE = ace;
break;
case ACCESS_DENIED:
ace->next = lastDenyACE;
lastDenyACE = ace;
break;
default:
break;
}
delete [] domainaccount;
domainaccount = NULL;
return true; // Needs better error signalling.
}
PSID vncImportACL::GetSID(const TCHAR *domainaccount){
PSID pSid = NULL;
unsigned long ulDomLen = 0;
unsigned long ulSidLen = 0;
SID_NAME_USE peUse;
TCHAR *domain = NULL;
// Get accounts's SID
// First call to LookupAccountName is expected to fail.
// Sets pSid and domain size
LookupAccountName(NULL, domainaccount, pSid, &ulSidLen,
domain, &ulDomLen, &peUse);
pSid = (PSID)new TCHAR[ulSidLen];
domain = new TCHAR[ulDomLen];
LookupAccountName(NULL, domainaccount,
pSid, &ulSidLen, domain, &ulDomLen, &peUse);
if (!IsValidSid(pSid)){
_ftprintf(stderr, _T("%s: SID not valid.\n"), domainaccount);
delete [] pSid;
pSid = NULL;
}
delete [] domain;
return pSid;
}
// SplitString splits a string 'input' on the first occurence of char 'separator'
// into string 'head' and 'tail' (removing the separator).
// If separator is not found, head = "" and tail = input.
const TCHAR * vncImportACL::SplitString(const TCHAR *input, TCHAR separator, TCHAR *head){
const TCHAR * tail;
int l;
tail = _tcschr(input, separator);
if (tail){
l = tail - input;
// get rid of separator
tail = tail + 1;
_tcsncpy(head, input, l);
head[l] = _T('\0');
} else {
tail = input;
head[0] = _T('\0');
}
return tail;
}
TCHAR *vncImportACL::AddComputername(const TCHAR *user){
unsigned long buflen = MAXLEN;
TCHAR computername[MAXLEN];
if (!GetComputerName(computername, &buflen)){
_ftprintf(stderr, _T("GetComputerName error %i\n"), GetLastError());
return NULL;
}
_ftprintf(stderr, _T("Detected computername = %s\n"), computername);
// Length of computername and user minus beginning dot plus terminating '\0'.
TCHAR *domainaccount = new TCHAR[_tcslen(computername) + _tcslen(user)];
_tcscpy(domainaccount, computername);
_tcscat(domainaccount, user + 1);
return domainaccount;
}
TCHAR *vncImportACL::AddDomainname(const TCHAR *user){
unsigned long buflen = MAXLEN;
TCHAR domain[MAXLEN * sizeof(wchar_t)];
LPWKSTA_INFO_100 wkstainfo = NULL;
NET_API_STATUS nStatus;
nStatus = NetWkstaGetInfo( 0 , 100 , (LPBYTE *) &wkstainfo);
if (nStatus == NERR_Success)
_tcsncpy(domain, wkstainfo->wki100_langroup, MAXLEN);
else
_ftprintf(stderr, _T("NetWkstaGetInfo() returned %lu \n"), wkstainfo);
domain[MAXLEN - 1] = _T('\0');
_ftprintf(stderr, _T("Detected domain = %s\n"),domain);
NetApiBufferFree(wkstainfo);
wkstainfo = NULL;
// Length of domainname and user minus beginning dots plus terminating '\0'.
TCHAR *domainaccount = new TCHAR[_tcslen(domain) + _tcslen(user) - 1];
_tcscpy(domainaccount, domain);
_tcscat(domainaccount, user + 2);
return domainaccount;
}
// vncScanInput struct
bool vncScanInput::GetLine(TCHAR *line){
line[0] = _T('\0');
_fgetts(line, MAXLEN, stdin);
return ferror(stdin) ? false : true;
}
bool vncScanInput::isEmptyLine(TCHAR *line){
for (unsigned int i = 0; i < _tcslen(line); i++)
if (line[i] != _T(' ') && line[i] != _T('\t'))
return false;
return true;
}
void vncScanInput::RemoveComment(TCHAR *line){
if (TCHAR *comment = _tcschr(line, _T(';')))
*comment = _T('\0');
}
void vncScanInput::RemoveNewline(TCHAR *line){
if (line[_tcslen(line)-1] == _T('\n'))
line[_tcslen(line)-1] = _T('\0');
}
bool vncScanInput::Tokenize(const TCHAR *line, TCHAR **token){
int tokenCount = 0;
// Loop until the end of line or until 4th token found
for (; _tcslen(line) > 0 && tokenCount < 4;) {
int len = 0;
switch (*line) {
case _T(' ') :
case _T('\t'):
line++; // Eat whitespace
break;
case _T('"') :
line++; // Eat opening quote
len = GetQuoteLength(line);
tokenCount = AddToken(token, tokenCount, &line, len);
line++; // Eat closing quote
break;
default:
len = GetWordLength(line);
tokenCount = AddToken(token, tokenCount, &line, len);
break;
}
}
return (tokenCount == 3 && _tcslen(line) == 0) ? true : false;
}
int vncScanInput::GetWordLength(const TCHAR *line){
int length = 0;
int blength = 0;
int tlength = 0;
const TCHAR *nextblank = _tcschr(line, _T(' '));
const TCHAR *nexttab = _tcschr(line, _T('\t'));
if (nextblank)
blength = nextblank - line;
if (nexttab)
tlength = nexttab - line;
if (nextblank && nexttab) // Found blank and tab
length = __min(blength, tlength);
else if (nextblank) // Found only a blank
length = blength;
else if (nexttab) // Found only a tab
length = tlength;
if (!length) // Found neither blank nor tab
length = _tcslen(line);
return length;
}
int vncScanInput::GetQuoteLength(const TCHAR *line){
const TCHAR *eoq = _tcschr(line, _T('"'));
int len = 0;
if (eoq)
len = eoq - line;
return len;
}
int vncScanInput::AddToken(TCHAR **token, int tokenCount, const TCHAR **line, int len){
if (len) {
if (tokenCount < 3) {
_tcsncpy(token[tokenCount], *line, len);
token[tokenCount][len] = _T('\0');
}
*line += len; // Eat token
tokenCount++;
}
return tokenCount;
}