-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
694 lines (592 loc) · 19 KB
/
main.c
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/* ===================================================================
* Copyright (c) 2005-2017 Vadim Druzhin (cdslow@mail.ru).
*
* 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; version 2 of the License.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
* ===================================================================
*/
#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include "ntpw.h"
#include "log.h"
#include "unicode.h"
#include "dialogs.h"
#include "ctl_groupbox.h"
#include "ctl_edit.h"
#include "ctl_button.h"
#include "ctl_listview.h"
#include "ctl_image.h"
#include "ctl_label.h"
#include "message.h"
#include "dlgpass.h"
#include "dlgabout.h"
#include "dlghiber.h"
#include "version.h"
#define DEF_SAM_PATH L"C:\\WINDOWS\\SYSTEM32\\CONFIG\\SAM"
#define HIBR_TAG_LEN 4
#define HIBR_TAG "HIBR"
static INT_PTR Button_CANCEL(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Button_PATH(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Edit_PATH(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Button_OPEN(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Button_UNLOCK(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Button_PASS(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Button_SAVE(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR ListProc(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR Button_ABOUT(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam);
static BOOL OpenDialog(HWND window, char *namebuf, int bufsize);
static int ListUsers(HWND window, char *path);
static void ListInsert(HWND window, WORD id, int pos, int rid, WCHAR *name);
static void ListDeleteAll(HWND window, WORD id);
static void ListAutosizeColumn(HWND window, WORD id, int column);
static int ListGetPos(HWND window, WORD id);
static LPARAM ListGetParam(HWND window, WORD id, int pos);
static void EnableUserOptions(HWND window, int rid);
static void DisableUserOptions(HWND window);
static void CheckSave(HWND window);
static WCHAR *SearchSAM(void);
static BOOL CheckHiberfil(char const *path);
static HANDLE SuperOpen(char const *file_name);
enum
{
ID_GRP_ABOUT=101,
ID_ICON_ABOUT,
ID_SPACER_ABOUT1,
ID_GRP_ABOUT_FILL,
ID_SPACER_ABOUT2,
ID_GRP_ABOUT_ICON,
ID_BUTTON_ABOUT,
ID_GRP_PATH,
ID_EDIT_PATH,
ID_SPACER_PATH,
ID_BUTTON_PATH,
ID_SPACER_OPEN,
ID_BUTTON_OPEN,
ID_GRP_LIST,
ID_LIST_USERS,
ID_SPACER_LIST,
ID_GRP_ACTIONS,
ID_BUTTON_UNLOCK,
ID_SPACER_UNLOCK,
ID_BUTTON_PASS,
ID_GRP_BUTTON,
ID_SPACER_OK,
ID_BUTTON_SAVE,
ID_GRP_MIDDLE
};
static struct DLG_Item Items[]=
{
{&CtlGroupBoxH, ID_GRP_PATH, L"Path to SAM file", 0, 0, NULL},
{&CtlEdit, ID_EDIT_PATH, NULL, 0, ID_GRP_PATH, Edit_PATH},
{&CtlGroupBoxSpacer, ID_SPACER_PATH, NULL, 0, ID_GRP_PATH, NULL},
{&CtlSmallButton, ID_BUTTON_PATH, L"...", 0, ID_GRP_PATH, Button_PATH},
{&CtlGroupBoxSpacer, ID_SPACER_OPEN, NULL, 0, ID_GRP_PATH, NULL},
{&CtlButton, ID_BUTTON_OPEN, L"Open", 0, ID_GRP_PATH, Button_OPEN},
{&CtlGroupH, ID_GRP_MIDDLE, NULL, 0, 0, NULL},
{&CtlGroupBoxV, ID_GRP_LIST, L"User list", 0, ID_GRP_MIDDLE, NULL},
{&CtlListView, ID_LIST_USERS, L"ID\tName", 0, ID_GRP_LIST, ListProc},
{&CtlGroupBoxSpacer, ID_SPACER_LIST, NULL, 0, ID_GRP_LIST, NULL},
{&CtlGroupH, ID_GRP_ACTIONS, NULL, 0, ID_GRP_LIST, NULL},
{&CtlButton, ID_BUTTON_UNLOCK, L"Unlock", 0, ID_GRP_ACTIONS, Button_UNLOCK},
{&CtlGroupBoxSpacer, ID_SPACER_UNLOCK, NULL, 0, ID_GRP_ACTIONS, NULL},
{&CtlButton, ID_BUTTON_PASS, L"Change password", 0, ID_GRP_ACTIONS, Button_PASS},
{&CtlGroupBoxH, ID_GRP_BUTTON, NULL, 0, 0, NULL},
{&CtlButton, ID_BUTTON_SAVE, L"Save changes", 0, ID_GRP_BUTTON, Button_SAVE},
{&CtlGroupBoxSpacer, ID_SPACER_OK, NULL, 0, ID_GRP_BUTTON, NULL},
{&CtlDefButton, IDCANCEL, L"Exit", 0, ID_GRP_BUTTON, Button_CANCEL},
{&CtlGroupBoxV, ID_GRP_ABOUT, NULL, 0, ID_GRP_MIDDLE, NULL},
{&CtlGroupBoxSpacer, ID_SPACER_ABOUT1, NULL, 0, ID_GRP_ABOUT, NULL},
{&CtlGroupH, ID_GRP_ABOUT_ICON, GRP_TITLE_FILL_CX, 0, ID_GRP_ABOUT, NULL},
{&CtlIcon, ID_ICON_ABOUT, L"IconApp", 0, ID_GRP_ABOUT_ICON, NULL},
{&CtlGroupBoxSpacer, ID_SPACER_ABOUT2, NULL, 0, ID_GRP_ABOUT, NULL},
{&CtlButton, ID_BUTTON_ABOUT, L"About", 0, ID_GRP_ABOUT, Button_ABOUT},
{&CtlGroupH, ID_GRP_ABOUT_FILL, GRP_TITLE_FILL, 0, ID_GRP_ABOUT, NULL},
};
static INT_PTR Button_CANCEL(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
(void)id; /* Unused */
if(WM_INITDIALOG==msg)
{
SetFocus(GetDlgItem(window, ID_BUTTON_PATH));
return TRUE;
}
else if(WM_COMMAND==msg)
{
if(IsWindowEnabled(GetDlgItem(window, ID_BUTTON_SAVE)))
{
if(IDYES==AppMessageBox(window,
L"Changes not saved.\n\nSave before exit?", MB_YESNO)
)
Button_SAVE(window, ID_BUTTON_SAVE, msg, wParam, lParam);
}
EndDialog(window, 0);
return TRUE;
}
return FALSE;
}
static INT_PTR Button_ABOUT(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
(void)id; /* Unused */
(void)wParam; /* Unused */
(void)lParam; /* Unused */
if(WM_COMMAND==msg)
{
AboutDialog(window);
return TRUE;
}
return FALSE;
}
static INT_PTR Button_PATH(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
char path[MAX_PATH];
(void)id; /* Unused */
(void)wParam; /* Unused */
(void)lParam; /* Unused */
if(WM_COMMAND==msg)
{
GetDlgItemText(window, ID_EDIT_PATH, path, sizeof(path));
if(OpenDialog(window, path, sizeof(path)))
{
SetDlgItemText(window, ID_EDIT_PATH, path);
SendMessage(window, WM_COMMAND, ID_BUTTON_OPEN, 0);
}
return TRUE;
}
return FALSE;
}
static INT_PTR Edit_PATH(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
DWORD attr;
char path[MAX_PATH];
(void)id; /* Unused */
(void)lParam; /* Unused */
/* Update initial SAM path in dialog */
if(WM_INITDIALOG == msg)
SetDlgItemTextW(window, ID_EDIT_PATH, SearchSAM());
if(WM_INITDIALOG==msg || (WM_COMMAND==msg && EN_CHANGE==HIWORD(wParam)))
{
GetDlgItemText(window, ID_EDIT_PATH, path, sizeof(path));
attr=GetFileAttributes(path);
if(0xFFFFFFFF!=attr &&
FILE_ATTRIBUTE_DIRECTORY!=(attr&FILE_ATTRIBUTE_DIRECTORY))
EnableWindow(GetDlgItem(window, ID_BUTTON_OPEN), TRUE);
else
EnableWindow(GetDlgItem(window, ID_BUTTON_OPEN), FALSE);
return TRUE;
}
return FALSE;
}
static INT_PTR Button_OPEN(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
char path[MAX_PATH];
(void)id; /* Unused */
(void)wParam; /* Unused */
(void)lParam; /* Unused */
if(WM_COMMAND==msg)
{
GetDlgItemText(window, ID_EDIT_PATH, path, sizeof(path));
if(CheckHiberfil(path))
{
if(HiberWarning(window) != IDOK)
return Button_CANCEL(window, IDCANCEL, msg, wParam, lParam);
}
if(ListUsers(window, path)<0)
AppMessageBox(window, L"Open failed!", MB_OK);
else if(is_hives_ro())
AppMessageBox(window,
L"SAM file opened read-only,\n"
L"so changes cannot be saved!\n\n"
L"Modify file attributes/permissions\n"
L"to allow write access, and reopen it.", MB_OK);
DisableUserOptions(window);
CheckSave(window);
return TRUE;
}
return FALSE;
}
static INT_PTR Button_UNLOCK(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
(void)wParam; /* Unused */
(void)lParam; /* Unused */
if(WM_INITDIALOG==msg)
{
EnableWindow(GetDlgItem(window, id), FALSE);
return TRUE;
}
else if(WM_COMMAND==msg)
{
int pos;
int param;
pos=ListGetPos(window, ID_LIST_USERS);
if(pos>=0)
{
param=(int)ListGetParam(window, ID_LIST_USERS, pos);
if(unlock_account(param))
{
EnableWindow(GetDlgItem(window, id), FALSE);
SendMessage(window, WM_NEXTDLGCTL,
(WPARAM)GetDlgItem(window, ID_LIST_USERS), TRUE);
}
else
AppMessageBox(window, L"Unlock failed!", MB_OK);
CheckSave(window);
}
return TRUE;
}
return FALSE;
}
static INT_PTR Button_PASS(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
(void)wParam; /* Unused */
(void)lParam; /* Unused */
if(WM_INITDIALOG==msg)
{
EnableWindow(GetDlgItem(window, id), FALSE);
return TRUE;
}
else if(WM_COMMAND==msg)
{
int pos;
int param;
char pass[17];
pos=ListGetPos(window, ID_LIST_USERS);
if(pos>=0)
{
param=(int)ListGetParam(window, ID_LIST_USERS, pos);
if(QueryPassword(window, pass, sizeof(pass)))
{
if(!change_password(param, pass))
{
if(0==*pass)
AppMessageBox(window,
L"Password not changed!", MB_OK);
else
AppMessageBox(window,
L"Password not changed!\n"
L"Try again using BLANK passsword.", MB_OK);
}
CheckSave(window);
}
}
return TRUE;
}
return FALSE;
}
static INT_PTR Button_SAVE(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
(void)wParam; /* Unused */
(void)lParam; /* Unused */
if(WM_INITDIALOG==msg)
{
EnableWindow(GetDlgItem(window, id), FALSE);
return TRUE;
}
else if(WM_COMMAND==msg)
{
if(!write_hives())
AppMessageBox(window, L"Writing SAM failed!", MB_OK);
SendMessage(window, WM_NEXTDLGCTL,
(WPARAM)GetDlgItem(window, IDCANCEL), TRUE);
CheckSave(window);
}
return FALSE;
}
static INT_PTR ListProc(
HWND window, WORD id, UINT msg, WPARAM wParam, LPARAM lParam)
{
(void)wParam; /* Unused */
if(WM_NOTIFY==msg)
{
NMHDR *nm=(NMHDR *)lParam;
if((UINT)LVN_ITEMCHANGED==nm->code || (UINT)NM_SETFOCUS==nm->code)
{
int pos;
int param;
pos=ListGetPos(window, id);
if(pos>=0)
{
param=(int)ListGetParam(window, id, pos);
EnableUserOptions(window, param);
}
else
DisableUserOptions(window);
return TRUE;
}
}
return FALSE;
}
static int ListGetPos(HWND window, WORD id)
{
return (int)SendDlgItemMessage(window, id, LVM_GETNEXTITEM,
-1, LVNI_ALL|LVNI_SELECTED);
}
static LPARAM ListGetParam(HWND window, WORD id, int pos)
{
LV_ITEM item;
item.iItem=pos;
item.mask=LVIF_PARAM;
item.iSubItem=0;
SendDlgItemMessage(window, id, LVM_GETITEM, 0, (LPARAM)&item);
return item.lParam;
}
static void EnableUserOptions(HWND window, int rid)
{
if(0!=is_account_locked(rid))
EnableWindow(GetDlgItem(window, ID_BUTTON_UNLOCK), TRUE);
EnableWindow(GetDlgItem(window, ID_BUTTON_PASS), TRUE);
}
static void DisableUserOptions(HWND window)
{
EnableWindow(GetDlgItem(window, ID_BUTTON_UNLOCK), FALSE);
EnableWindow(GetDlgItem(window, ID_BUTTON_PASS), FALSE);
}
static void CheckSave(HWND window)
{
EnableWindow(GetDlgItem(window, ID_BUTTON_SAVE),
is_hives_dirty() && !is_hives_ro());
}
static int ListUsers(HWND window, char *path)
{
char *names[H_COUNT]={""};
struct search_user su;
struct user_info *ui;
int count;
ListDeleteAll(window, ID_LIST_USERS);
names[H_SAM]=path;
if(0==open_hives(names))
return -1;
ui=first_user(&su);
count=0;
while(ui)
{
if(500==ui->rid)
ListInsert(window, ID_LIST_USERS,
0, ui->rid, (WCHAR *)ui->unicode_name);
else
ListInsert(window, ID_LIST_USERS,
count, ui->rid, (WCHAR *)ui->unicode_name);
free(ui);
ui=next_user(&su);
++count;
}
if(count>0)
{
ListAutosizeColumn(window, ID_LIST_USERS, 0);
ListAutosizeColumn(window, ID_LIST_USERS, 1);
RedrawWindow(window, NULL, NULL, RDW_INVALIDATE);
}
return count;
}
static void ListInsert(HWND window, WORD id, int pos, int rid, WCHAR *name)
{
LV_ITEMW litem;
WCHAR buf[12];
litem.mask=LVIF_TEXT|LVIF_PARAM;
litem.iItem=pos;
litem.iSubItem=0;
litem.state=0;
litem.stateMask=0;
litem.pszText=buf;
litem.cchTextMax=0;
litem.iImage=0;
litem.lParam=rid;
Num(buf, rid);
SendDlgItemMessageW(window, id, LVM_INSERTITEMW, 0, (LPARAM)&litem);
litem.mask=LVIF_TEXT;
litem.iSubItem=1;
litem.pszText=name;
SendDlgItemMessageW(window, id, LVM_SETITEMW, 0, (LPARAM)&litem);
}
static void ListDeleteAll(HWND window, WORD id)
{
SendDlgItemMessage(window, id, LVM_DELETEALLITEMS, 0, 0);
}
static void ListAutosizeColumn(HWND window, WORD id, int column)
{
SendDlgItemMessage(window, id, LVM_SETCOLUMNWIDTH, column, LVSCW_AUTOSIZE);
}
static BOOL OpenDialog(HWND window, char *namebuf, int bufsize)
{
static OPENFILENAME ofn;
if(ofn.lStructSize==0)
{
ofn.lStructSize=sizeof(ofn);
/*ofn.hwndOwner=window;*/
ofn.hInstance=NULL;
ofn.lpstrFilter="SAM file\0SAM\0All files\0*.*\0";
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
ofn.nFilterIndex=0;
/*ofn.lpstrFile=namebuf;*/
/*ofn.nMaxFile=bufsize;*/
ofn.lpstrFileTitle=NULL;
ofn.nMaxFileTitle=0;
ofn.lpstrInitialDir=NULL;
ofn.lpstrTitle=NULL;
ofn.Flags=OFN_FILEMUSTEXIST|OFN_LONGNAMES|OFN_EXPLORER|OFN_HIDEREADONLY;
ofn.nFileOffset=0;
ofn.nFileExtension=0;
ofn.lpstrDefExt=NULL;
ofn.lCustData=0;
ofn.lpfnHook=NULL;
ofn.lpTemplateName=NULL;
}
ofn.hwndOwner=window;
ofn.lpstrFile=namebuf;
ofn.nMaxFile=bufsize;
return GetOpenFileName(&ofn);
}
int main(void)
{
HWND window;
UnicodeInit();
/* Set icon for dialog windows */
window=CreateWindow(WC_DIALOG, "", 0,
0, 0, 0, 0, NULL, NULL, NULL, NULL);
if(window)
{
SetClassLongPtr(window, GCLP_HICON,
(LONG_PTR)LoadIcon(GetModuleHandle(NULL), "IconApp"));
DestroyWindow(window);
}
DlgRunU(
NULL,
AppTitle,
0,
/*WS_BORDER|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MAXIMIZEBOX,*/
WS_OVERLAPPEDWINDOW,
0,
Items,
sizeof(Items)/sizeof(*Items),
NULL
);
close_hives();
return 0;
}
#ifdef _MSC_VER
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
(void)hInstance; /* Unused */
(void)hPrevInstance; /* Unused */
(void)lpCmdLine; /* Unused */
(void)nCmdShow; /* Unused */
return main();
}
#endif /* _MSC_VER */
static WCHAR *SearchSAM(void)
{
static WCHAR sam[] = DEF_SAM_PATH;
WCHAR root[] = L"C:\\";
while(root[0] <= L'Z')
{
UINT dt = GetDriveTypeW(root);
if(dt == DRIVE_FIXED)
{
DWORD attr;
sam[0] = root[0];
attr = GetFileAttributesW(sam);
if(attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY))
break;
sam[0] = L'C';
}
++root[0];
}
return sam;
}
static BOOL CheckHiberfil(char const *path)
{
char hiberfil[] = "C:\\HIBERFIL.SYS";
DWORD attr;
HANDLE in;
char buf[HIBR_TAG_LEN];
DWORD count;
BOOL res = TRUE;
hiberfil[0] = path[0];
attr = GetFileAttributes(hiberfil);
if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
return FALSE;
in = CreateFile(hiberfil, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(in == INVALID_HANDLE_VALUE)
in = SuperOpen(hiberfil);
if(in == INVALID_HANDLE_VALUE)
return TRUE;
if(ReadFile(in, buf, HIBR_TAG_LEN, &count, NULL))
{
if(count == HIBR_TAG_LEN && memcmp(buf, HIBR_TAG, HIBR_TAG_LEN) != 0)
res = FALSE;
}
CloseHandle(in);
return res;
}
static HANDLE SuperOpen(char const *file_name)
{
HANDLE ret = INVALID_HANDLE_VALUE;
HANDLE token = INVALID_HANDLE_VALUE;
size_t const priv_size = sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES);
TOKEN_PRIVILEGES *priv = NULL;
TOKEN_PRIVILEGES *old_priv = NULL;
DWORD old_priv_size = 0;
do
{
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
break;
priv = malloc(priv_size);
if(!priv)
break;
old_priv = malloc(priv_size);
if(!old_priv)
break;
priv->PrivilegeCount = 2;
if(!LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &(priv->Privileges[0].Luid)))
break;
priv->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if(!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &(priv->Privileges[1].Luid)))
break;
priv->Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(token, FALSE, priv, priv_size, old_priv, &old_priv_size))
break;
ret = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
}
while(0);
if(old_priv_size != 0)
AdjustTokenPrivileges(token, FALSE, old_priv, 0, NULL, NULL);
free(old_priv);
free(priv);
CloseHandle(token);
return ret;
}