-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPwdCommon.cpp
349 lines (264 loc) · 7.39 KB
/
PwdCommon.cpp
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
#ifdef _LEAKDETECT
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#define _CRTDBG_MAP_ALLOC
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <windows.h>
#include <npapi.h>
#include <ntsecapi.h>
#include <tchar.h>
#include <wchar.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#ifndef _LEAKDETECT
#include <stdlib.h>
#endif
#include <vector>
#ifndef _INCL_PWD_COMMON_H
#define _INCL_PWD_COMMON_H
#include "PwdCommon.h"
#endif
//Default debug level
DEBUG_LEVEL debug_level = Debug;
//Default user - unknown
PWCHAR USER = NULL;
int simple_reg_read_int(HKEY phkey, PWCHAR subkey, PWCHAR keyname, DWORD default_estimated_size, int &value){
int result = 0;
PWCHAR tmp = NULL;
result = simple_reg_read(phkey,
subkey,
keyname,
NULL,
default_estimated_size,
tmp);
if(result){
value = _wtoi(tmp);
}
delete[] tmp;
return result;
}
int simple_reg_read_wstring(HKEY phkey, PWCHAR subkey, PWCHAR keyname, DWORD default_estimated_size, PWCHAR &value) {
value = NULL;
return simple_reg_read(phkey,
subkey,
keyname,
NULL,
default_estimated_size,
value);
}
int simple_reg_read(HKEY phkey, PWCHAR subkey, PWCHAR keyname, DWORD type, DWORD estimated_buf_size, PWCHAR &buffer) {
HKEY hkey;
LONG err;
DWORD buf_size = estimated_buf_size;
buffer = NULL;
err = RegOpenKeyEx(phkey, subkey, 0, KEY_READ, &hkey);
if(err == ERROR_SUCCESS)
{
buffer = new WCHAR[buf_size + 1];
err = RegQueryValueEx(hkey, keyname, NULL, &type, (LPBYTE)buffer, &buf_size);
if(err == ERROR_MORE_DATA){
log_evt(Debug, AT_FILE_LINE, _TEXT("Allocating more memory for reading key."));
//Try and allocate appropirate buffer
//Previous call to RegQueryValueEx stored the required buffer size in buf_size
delete [] buffer;
buffer = new WCHAR[buf_size + 1];
err = RegQueryValueEx(hkey, keyname, NULL, &type, (LPBYTE)buffer, &buf_size);
}
}
RegCloseKey(hkey);
if(err == ERROR_SUCCESS) {
//RegQueryValueEx may return value that is not \0 terminated
(buffer)[buf_size/sizeof(WCHAR)] = 0;
return 1;
}
else {
delete[] buffer;
return 0;
}
}
int log_evt_func_call(DEBUG_LEVEL debug_lvl, PWCHAR at_file_line, PWCHAR func){
return log_evt(debug_lvl, at_file_line, _TEXT("Call to %s()"), func);
}
int log_evt_func_exit(DEBUG_LEVEL debug_lvl, PWCHAR at_file_line, PWCHAR func, ULONG exit_code){
return log_evt(debug_lvl, at_file_line, _TEXT("%s() exiting with return code %x"), func, exit_code);
}
int log_evt(DEBUG_LEVEL debug_lvl, PWCHAR at_file_line, PWCHAR format, ...) {
int result = 0;
va_list args;
size_t char_count = 0;
size_t printf_char_count = 0;
PWCHAR log_format = NULL;
PWCHAR timestamp = NULL;
PWCHAR log_prefix = NULL;
PWCHAR template_format = NULL;
FILE *log_file = NULL;
//Abort if message should not be logged
if(debug_lvl > debug_level)
return 0;
//Log template format
if(USER == NULL){
template_format = _TEXT("%s %s %s %s\n");
}
else {
template_format = _TEXT("%s %s {%s} %s %s\n");
}
//Convert debug level to string
PWCHAR debug_lvl_str = NULL;
switch(debug_lvl){
case Error:
debug_lvl_str = _TEXT("Error");
break;
case Warn:
debug_lvl_str = _TEXT("Warn");
break;
case Info:
debug_lvl_str = _TEXT("Info");
break;
case Debug:
debug_lvl_str = _TEXT("Debug");
break;
default:
debug_lvl_str = _TEXT("Unknown");
}
//Get timestamp
timestamp = log_timestamp();
if(timestamp == NULL)
return 0;
//Calculate size of format string for vsprintf
char_count = 0;
char_count = wcslen(template_format);
char_count += wcslen(timestamp);
char_count += wcslen(debug_lvl_str);
char_count += wcslen(at_file_line);
if(USER != NULL)
char_count += wcslen(USER);
char_count += wcslen(format);
//Format string for vsprintf
log_format = new WCHAR[char_count + 1];
if(log_format != NULL){
if(USER == NULL)
printf_char_count = swprintf_s(log_format, char_count, template_format, timestamp, debug_lvl_str, at_file_line, format);
else
printf_char_count = swprintf_s(log_format, char_count, template_format, timestamp, debug_lvl_str, USER, at_file_line, format);
if(printf_char_count > 0){
va_start(args, format);
log_file = _wfopen(LOG_FILE, L"a");
if(log_file != NULL){
if(vfwprintf(log_file, log_format, args) > 0)
result = 1;
fclose(log_file);
}
va_end(args);
}
delete[] log_format;
}
delete[] timestamp;
return result;
}
PWCHAR log_timestamp() {
PWCHAR w_timestamp = NULL;
CHAR a_timestamp[64];
time_t raw_time;
struct tm* current_time = NULL;
int result = 1;
//Timestamp is always less than 64 chars based on format used below
w_timestamp = new WCHAR[64];
if(w_timestamp == NULL){
return NULL;
}
else {
if(time(&raw_time) != -1){
current_time = localtime(&raw_time);
if(strftime(a_timestamp, 64, "%a %b %d %X %Y", current_time) != 0){
if(ASCII_to_UNICODE(a_timestamp, w_timestamp, 64)){
result = 0;
}
}
}
if(result == 1) {
delete[] w_timestamp;
w_timestamp = NULL;
}
}
return w_timestamp;
}
int tokenize_string(PWCHAR string, WCHAR delimiter, std::vector<PWCHAR> &arr){
PWCHAR token = NULL;
PWCHAR token_cpy = NULL;
if(string == NULL)
return 0;
if(string[0] == 0)
return 0;
int string_len = wcslen(string);
int num_tokens = 1;
for(int x = 0; x < string_len; x++){
if (string[x] == delimiter)
num_tokens++;
}
arr.reserve(num_tokens);
PWCHAR delimiter_ = new WCHAR[2] { delimiter, 0 };
token = wcstok(string, delimiter_);
while(token != NULL) {
PWCHAR token_cpy = new WCHAR[wcslen(token) + 1];
wcscpy(token_cpy, token);
arr.push_back(token_cpy);
token = wcstok(NULL, delimiter_);
}
delete[] delimiter_;
return num_tokens;
}
int untokenize_string_array(const std::vector<PWCHAR> &arr, WCHAR delimiter, PWCHAR* string){
if(arr.size() == 0) {
*string = new WCHAR[1] { 0 };
return 1;
}
size_t num_chars = 0;
for(std::vector<PWCHAR>::size_type i = 0; i != arr.size(); i++) {
num_chars+= wcslen(arr[i]);
if(i != 0) {
++num_chars;
}
}
++num_chars;
*string = new WCHAR[num_chars];
(*string)[0] = 0;
PWCHAR delimiter_ = new WCHAR[2] { delimiter, 0 };
for(std::vector<PWCHAR>::size_type i = 0; i != arr.size(); i++) {
if(i != 0) {
wcscat_s(*string, num_chars, delimiter_);
}
wcscat_s(*string, num_chars, arr[i]);
}
delete[] delimiter_;
return num_chars;
}
int ASCII_to_UNICODE(PCHAR ascii_str, PWCHAR unicode_str, int size){
//MultiByteToWideChar == 0 indicates failure
if(MultiByteToWideChar(CP_ACP, 0, ascii_str, -1, unicode_str, size) == 0)
return 0;
else
return 1;
}
int UNICODE_to_ASCII(PWCHAR unicode_str, PCHAR ascii_str, int size){
//WideCharToMultiByte == 0 indicates failure
if(WideCharToMultiByte(CP_ACP, 0, unicode_str, -1, ascii_str, size, NULL, NULL) == 0)
return 0;
else
return 1;
}
PWCHAR PUNICODE_STRING_to_PWCHAR(PUNICODE_STRING PUN_String)
{
size_t len = PUN_String->Length / sizeof(WCHAR);
PWCHAR temp = new WCHAR[len + 1];
memcpy(temp, PUN_String->Buffer, PUN_String->Length);
temp[len] = 0;
return temp;
}