forked from lallousx86/ResetPermission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResetPermission.cpp
1086 lines (915 loc) · 29.7 KB
/
ResetPermission.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
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
Reset files permission (c) Elias Bachaalany
lallousz-x86@yahoo.com
History
---------
08/24/2013 - Initial version
08/30/2013 - Enclose the folder with quotes if it contains at least one space character
09/17/2013 - Added "Reset files permission" as a optional action
- Added "Reset hidden and system files"
03/31/2014 - Fixed double backslash when folder is root
01/08/2014 - Added "Do not follow symbolic links" option
03/31/2015 - Allow editing of the generated command textbox
- Added "More actions" to add Explorer shell context menu
11/03/2015 - Added /SKIPSL switch to takeown.exe
11/15/2015 - v1.1.3
- Added HELP button to redirect to blog entry
- Added warning when attempting to change permission of a root folder
02/13/2016 - v1.1.4
- Minor code changes
- Update the console window title when the commands execute
02/28/2016 - v1.1.5
- Refactored code
- Added the Advanced button
- Added Backup/Restore permissions
06/14/2016 - v1.1.6
- Made the dialog non-static
- Refactored the code
- bugfix: Add/Remove from the Explorer folder context menu did not work unless a folder was selected.
Fix: No folder selection is needed for that action.
12/19/2016 - v1.1.7
- Attempt to make ResetPermission AntiVirus false-positive free by using
local app data folder instead of temp and by not deleting the temp batch script
04/30/2017 - v1.2.0
- Browsing a folder will remember and focus on the previous folder
- Support unicode path
- Warn if the user is trying to reset permissions on a non-supported (or ACL-able) file system
- Fix: command line parsing was erroneous
- Fix: invoking the tool from the shell context menu was failing if the path contained space characters
-------------------------------------------------------------------------*/
#include "stdafx.h"
//-------------------------------------------------------------------------
static LPCTSTR STR_HELP_URL = _TEXT("http://lallouslab.net/2013/08/26/resetting-ntfs-files-permission-in-windows-graphical-utility/");
static LPCTSTR STR_SELECT_FOLDER = _TEXT("Please select a folder");
static LPCTSTR STR_ERROR = _TEXT("Error");
static LPCTSTR STR_CONFIRMATION = _TEXT("Confirmation");
static LPCTSTR STR_RESET_FN = _TEXT("resetperm.bat");
static LPCTSTR STR_HKCR_CTXMENU_BASE = _TEXT("\"HKCR\\Folder\\shell\\Reset Permission");
static LPCTSTR STR_HKCR_CTXMENU_CMD = _TEXT("\\command");
static stringT STR_CMD_PAUSE = _TEXT("pause\r\n");
static LPCTSTR STR_CMD_ICACLS = _TEXT("icacls ");
static LPCTSTR STR_CMD_TAKEOWN = _TEXT("takeown");
static LPCTSTR STR_CMD_ATTRIB = _TEXT("attrib");
static LPCTSTR STR_FOLDER_LALLOUSLAB = _TEXT("\\lallouslab");
static LPCTSTR STR_CMD_REG = _TEXT("reg");
static stringT STR_NEWLINE = _TEXT("\r\n");
static stringT STR_NEWLINE2 = STR_NEWLINE + STR_NEWLINE;
static LPCTSTR STR_WARNING = _TEXT("Warning!");
static LPCTSTR STR_FS_NOT_SUPPORTED_WARNING =
_TEXT("The selected path does not support file permissions and thus using this tool might not have any effects!\n\n")
_TEXT("Are you sure you want to continue?");
static LPCTSTR STR_ROOT_WARNING =
_TEXT("You are about to change the permission of a root folder!\n")
_TEXT("This is a **DANGEROUS** operation! It is better to choose a specific folder instead!\n\n")
_TEXT("!! If you choose to proceed then you might render your system unstable !!\n\n")
_TEXT("Are you sure you want to continue?");
static LPCTSTR STR_ADDREM_CTXMENU_CONFIRM =
_TEXT("You are about to add or remove the ResetPermission tool to/from the Windows Explorer folder context menu!\n")
_TEXT("\n")
_TEXT("Are you sure you want to continue?");
static LPCTSTR STR_TITLE_BACKUP_PERMS =
_TEXT("Pick the file you wish to backup the permissions into");
static LPCTSTR STR_TITLE_RESTORE_PERMS =
_TEXT("Pick the permissions backup file you wish to restore from");
static LPCTSTR STR_CHECK_THE_BATCHOGRAPHY_BOOK =
_TEXT("REM -- Check out the book: Batchography - The Art of Batch Files Programming\r\n")
_TEXT("REM -- http://lallouslab.net/2016/05/10/batchography/\r\n")
_TEXT("\r\n");
//-------------------------------------------------------------------------
static bool IsSupportedFileSystem(LPCTSTR Path)
{
do
{
// Get the root path only
TCHAR RootPath[4];
_tcsncpy_s(RootPath, Path, _countof(RootPath) - 1);
if (_tcslen(RootPath) < 3)
break;
// Get the file system name
TCHAR FileSysName[MAX_PATH + 1];
if (!GetVolumeInformation(
RootPath,
nullptr,
0,
nullptr,
nullptr,
nullptr,
FileSysName,
_countof(FileSysName)))
{
break;
}
// Compare against supported file systems
return _tcscmp(FileSysName, _TEXT("NTFS")) == 0;
} while (false);
// If we fail to determinte the FS type, then assume we support it and
// let the user decide what to do
return true;
}
//-------------------------------------------------------------------------
// The premise behind this function is that if we can convert a UTF-16
// string to both UTF-8 and ASCII and they are equal then no encoding is required
static bool ConvertUtf16ToUtf8(
const wchar_t *utf16_str,
bool *bEncodingRequired,
std::string *utf8_str)
{
size_t utf16_len = wcslen(utf16_str);
int utf8_size = WideCharToMultiByte(
CP_UTF8,
0,
utf16_str,
utf16_len,
nullptr,
0,
nullptr,
nullptr);
if (utf8_size == 0)
return false;
// If the UTF8 string's length is the same as the the UTF16 length
// then it means we did not need extra bytes to encode some UTF8 characters
// that are not part of the ASCII set
bool bNeedEncoding = utf8_size != utf16_len;
if (utf8_str != nullptr)
{
std::string buf_utf8;
buf_utf8.resize(utf8_size);
utf8_size = WideCharToMultiByte(
CP_UTF8,
0,
utf16_str,
utf16_len,
&buf_utf8[0],
buf_utf8.size(),
nullptr,
nullptr);
if (utf8_size == 0)
return false;
*utf8_str = buf_utf8;
}
if (bEncodingRequired != nullptr)
*bEncodingRequired = bNeedEncoding;
return true;
}
//-------------------------------------------------------------------------
// BrowseFolder helper callback that sets the default path
// https://www.arclab.com/en/kb/cppmfc/select-folder-shbrowseforfolder.html
static INT CALLBACK BrowseFolderSetDefaultPathCallback(
HWND hwnd,
UINT uMsg,
LPARAM lp,
LPARAM pData)
{
if (uMsg == BFFM_INITIALIZED)
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
return 0;
}
//-------------------------------------------------------------------------
bool ResetPermissionDialog::BrowseFolder(
HWND hOwner,
LPCTSTR szCaption,
stringT &folderpath)
{
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_EDITBOX | BIF_VALIDATE | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.hwndOwner = hOwner;
bi.lpszTitle = szCaption;
stringT CurFolder;
if (GetFolderText(CurFolder, false, false, false))
{
bi.lParam = (LPARAM)CurFolder.c_str();
bi.lpfn = BrowseFolderSetDefaultPathCallback;
}
LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);
if (pIDL == NULL)
return false;
TCHAR buffer[_MAX_PATH] = { 0 };
bool bOk = ::SHGetPathFromIDList(pIDL, buffer) != 0;
if (bOk)
folderpath = buffer;
// free the item id list
CoTaskMemFree(pIDL);
return bOk;
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::QuotePath(stringT &Path)
{
if (Path.find(_T(' ')) != stringT::npos)
Path = _TEXT("\"") + Path + _TEXT("\"");
}
//-------------------------------------------------------------------------
bool ResetPermissionDialog::BrowseFileName(
bool bSave,
LPCTSTR Caption,
LPCTSTR Extension,
LPCTSTR DefaultFile,
stringT &out)
{
TCHAR FileName[MAX_PATH2] = { 0 };
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.lpstrDefExt = Extension;
ofn.hwndOwner = hDlg;
ofn.lpstrFile = FileName;
ofn.nMaxFile = _countof(FileName);
ofn.lpstrTitle = Caption;
ofn.lpstrFilter = TEXT("All files\0*.*\0\0");
if (bSave)
ofn.Flags = OFN_OVERWRITEPROMPT;
else
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
bool bOk = (bSave ? GetSaveFileName : GetOpenFileName)(&ofn) == TRUE;
if (bOk)
out = ofn.lpstrFile;
return bOk;
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::ShowPopupMenu(
int IdMenu,
int IdBtnPos)
{
HMENU hMenu = LoadMenu(
hInstance,
MAKEINTRESOURCE(IdMenu));
HWND hBtn = GetDlgItem(hDlg, IdBtnPos);
HMENU hSubMenu = GetSubMenu(hMenu, 0);
RECT rect;
GetClientRect(hBtn, &rect);
POINT pt = { rect.left, rect.top };
ClientToScreen(hBtn, &pt);
TrackPopupMenu(
hSubMenu,
TPM_LEFTALIGN | TPM_LEFTBUTTON,
pt.x,
pt.y + 25,
0,
hDlg, NULL);
DestroyMenu(hMenu);
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::UpdateCheckboxes(bool bGet)
{
if (bGet)
{
bRecurse = SendDlgItemMessage(
hDlg,
IDCHK_RECURSE,
BM_GETCHECK,
0,
0) == BST_CHECKED;
bResetPerm = SendDlgItemMessage(
hDlg,
IDCHK_RESETPERM,
BM_GETCHECK,
0,
0) == BST_CHECKED;
bRmHidSys = SendDlgItemMessage(
hDlg,
IDCHK_RM_HS,
BM_GETCHECK,
0,
0) == BST_CHECKED;
bTakeOwn = SendDlgItemMessage(
hDlg,
IDCHK_TAKEOWN,
BM_GETCHECK,
0,
0) == BST_CHECKED;
bDontFollowLinks = SendDlgItemMessage(
hDlg,
IDCHK_DONTFOLLOWLINKS,
BM_GETCHECK,
0,
0) == BST_CHECKED;
}
else
{
SendDlgItemMessage(
hDlg,
IDCHK_RECURSE,
BM_SETCHECK,
bRecurse ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(
hDlg,
IDCHK_RESETPERM,
BM_SETCHECK,
bResetPerm ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(
hDlg,
IDCHK_RM_HS,
BM_SETCHECK,
bRmHidSys ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(
hDlg,
IDCHK_TAKEOWN,
BM_SETCHECK,
bTakeOwn ? BST_CHECKED : BST_UNCHECKED,
0);
SendDlgItemMessage(
hDlg,
IDCHK_DONTFOLLOWLINKS,
BM_SETCHECK,
bDontFollowLinks ? BST_CHECKED : BST_UNCHECKED,
0);
}
}
//-------------------------------------------------------------------------
LPCTSTR ResetPermissionDialog::GetArg(size_t idx)
{
return idx >= m_nbArgs ? nullptr : m_pArgs[idx];
}
//-------------------------------------------------------------------------
bool ResetPermissionDialog::GetCommandWindowText(stringT &Cmd)
{
HWND hwndCtrl = GetDlgItem(hDlg, IDTXT_COMMAND);
if (hwndCtrl == NULL)
return false;
int len = GetWindowTextLength(hwndCtrl);
if (GetLastError() != NO_ERROR)
return false;
TCHAR *szCmd = new TCHAR[len + 1];
if (szCmd == NULL)
return false;
GetWindowText(hwndCtrl, szCmd, len);
bool bOk = GetLastError() == NO_ERROR;
Cmd = szCmd;
delete[] szCmd;
return bOk;
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::SetCommandWindowText(LPCTSTR Str)
{
SetDlgItemText(hDlg, IDTXT_COMMAND, Str);
}
//-------------------------------------------------------------------------
bool ResetPermissionDialog::GetFolderText(
stringT &Folder,
bool bWarnRoot,
bool bAddWildCard,
bool bQuoteIfNeeded)
{
TCHAR Path[MAX_PATH * 4];
UINT len = GetDlgItemText(
hDlg,
IDTXT_FOLDER,
Path,
_countof(Path));
if (len == 0)
return false;
if (bWarnRoot)
{
// Warn if resetting root permissions
if (_tcslen(Path) == 3 && Path[1] == _TCHAR(':') && Path[2] == _TCHAR('\\'))
{
if (MessageBox(hDlg, STR_ROOT_WARNING, STR_WARNING, MB_YESNO | MB_ICONWARNING) == IDNO)
return false;
}
// Warn if tool is used on unsupported file system
if (!IsSupportedFileSystem(Path))
{
if (MessageBox(hDlg, STR_FS_NOT_SUPPORTED_WARNING, STR_WARNING, MB_YESNO | MB_ICONWARNING) == IDNO)
return false;
}
}
Folder = Path;
// Add the wildcard mask
if (bAddWildCard)
{
if (*Folder.rbegin() != TCHAR('\\'))
Folder += _TEXT("\\");
Folder += _TEXT("*");
}
// Quote the folder if needed
if (bQuoteIfNeeded)
QuotePath(Folder);
return true;
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::SetFolderText(LPCTSTR Value)
{
SetDlgItemText(hDlg, IDTXT_FOLDER, Value);
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::InitCommand(stringT &cmd)
{
cmd += STR_CHECK_THE_BATCHOGRAPHY_BOOK;
LPCTSTR TempScript = GenerateWorkBatchFileName();
if (TempScript != nullptr)
{
cmd += _TEXT("REM -- Temp script location: ");
cmd += TempScript + STR_NEWLINE2;
}
}
//-------------------------------------------------------------------------
// Update the command text
void ResetPermissionDialog::UpdateCommandText()
{
UpdateCheckboxes(true);
stringT folder;
if (GetFolderText(folder, false, true, true) == 0)
return;
stringT cmd;
InitCommand(cmd);
// Form takeown.exe command
if (bTakeOwn)
{
// Update the command prompt's title
cmd += _TEXT("TITLE taking ownership of folder: ") + folder + STR_NEWLINE;
cmd += STR_CMD_TAKEOWN;
if (bRecurse)
cmd += _TEXT(" /r ");
if (bDontFollowLinks)
cmd += _TEXT(" /SKIPSL ");
cmd += _TEXT(" /f ") + folder + STR_NEWLINE2;
}
//
// Form icacls.exe command
//
if (bResetPerm)
{
// Update the command prompt's title
cmd += _TEXT("TITLE Taking ownership of folder: ") + folder + STR_NEWLINE;
cmd += STR_CMD_ICACLS + folder;
if (bRecurse)
cmd += _TEXT(" /T ");
if (bDontFollowLinks)
cmd += _TEXT(" /L ");
cmd += _TEXT(" /Q /C /RESET") + STR_NEWLINE2;
}
// Form attribute.exe command
if (bRmHidSys)
{
// Update the command prompt's title
cmd += _TEXT("TITLE Changing files attributes in folder: ") + folder + STR_NEWLINE;
cmd += STR_CMD_ATTRIB;
if (bRecurse)
cmd += _TEXT(" /s ");
cmd += _TEXT(" -h -s ") + folder + STR_NEWLINE2;
}
// Always add a pause and a new line
cmd += STR_CMD_PAUSE;
cmd += STR_NEWLINE;
// Update the
SetCommandWindowText(cmd.c_str());
}
//-------------------------------------------------------------------------
LPCTSTR ResetPermissionDialog::GenerateWorkBatchFileName()
{
// Make temp file name
static TCHAR CmdFileName[MAX_PATH2] = { 0 };
// Compute if it was not already computed
if (CmdFileName[0] == _TCHAR('\0'))
{
// Attempt to use local user AppData folder
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, CmdFileName)))
{
_tcsncat_s(CmdFileName, STR_FOLDER_LALLOUSLAB, _countof(CmdFileName));
// Work directory note found? Create it!
if ((GetFileAttributes(CmdFileName) == INVALID_FILE_ATTRIBUTES)
&& !CreateDirectory(CmdFileName, nullptr))
{
// Failed to create the folder. Discard the local app folder and use temp folder
CmdFileName[0] = _T('\0');
}
}
// Revert to temp folder if this fails
if (CmdFileName[0] == _TCHAR('\0'))
{
// Get temp path via the API
if (GetTempPath(_countof(CmdFileName), CmdFileName) == 0)
{
// Attempt to get it again via the environment variable
if (GetEnvironmentVariable(_TEXT("TEMP"), CmdFileName, _countof(CmdFileName)) == 0)
return nullptr;
}
}
if (CmdFileName[_tcslen(CmdFileName) - 1] != TCHAR('\\'))
_tcsncat_s(CmdFileName, _TEXT("\\"), _countof(CmdFileName));
_tcsncat_s(CmdFileName, STR_RESET_FN, _countof(CmdFileName));
}
return CmdFileName;
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::AddToExplorerContextMenu(bool bAdd)
{
stringT cmd;
InitCommand(cmd);
cmd += STR_CMD_REG;
if (bAdd)
cmd += TEXT(" ADD ");
else
cmd += TEXT(" DELETE ");
cmd += STR_HKCR_CTXMENU_BASE;
if (bAdd)
cmd += STR_HKCR_CTXMENU_CMD;
cmd += TEXT("\" /f ");
if (bAdd)
{
cmd += TEXT("/ve /t REG_SZ /d \"\\\"");
cmd += AppPath;
cmd += TEXT("\\\" \"\\\"%%1\"\\\"\"");
}
cmd += STR_NEWLINE;
cmd += STR_CMD_PAUSE;
// Confirm
if (MessageBox(hDlg, STR_ADDREM_CTXMENU_CONFIRM, STR_CONFIRMATION, MB_YESNO | MB_ICONQUESTION) == IDNO)
return;
// Execute the command
ExecuteCommand(cmd);
}
//-------------------------------------------------------------------------
void ResetPermissionDialog::BackRestorePermissions(bool bBackup)
{
// Browse for permission backup file
stringT PermsFile;
if (!ResetPermissionDialog::BrowseFileName(
bBackup,
bBackup ? STR_TITLE_BACKUP_PERMS : STR_TITLE_RESTORE_PERMS,
TEXT("permissions.txt"),
TEXT("*.txt"),
PermsFile))
{
return;
}
QuotePath(PermsFile);
// Get the folder location
stringT folder;
if (GetFolderText(folder, false, bBackup ? true : false, true) == 0)
return;
stringT cmd;
InitCommand(cmd);
// Update the command prompt's title
if (bBackup)
{
cmd += _TEXT("TITLE Backing up permissions of folder: ") + folder + STR_NEWLINE;
cmd += STR_CMD_ICACLS + folder + _TEXT(" /save ") + PermsFile;
if (bRecurse)
cmd += _TEXT(" /T ");
}
else
{
cmd += _TEXT("TITLE Restoring permissions of folder: ") + folder + STR_NEWLINE;
cmd += STR_CMD_ICACLS + folder + _TEXT(" /restore ") + PermsFile;
}
cmd += STR_NEWLINE2 + STR_CMD_PAUSE;
SetCommandWindowText(cmd.c_str());
}
//-------------------------------------------------------------------------
bool ResetPermissionDialog::ExecuteCommand(stringT &Cmd)
{
std::string Utf8Cmd;
#ifdef _UNICODE
std::string utf8_str;
bool bEncodingRequired;
if (!ConvertUtf16ToUtf8(Cmd.c_str(), &bEncodingRequired, &utf8_str))
{
MessageBox(
hDlg,
_TEXT("Failed to convert input command to UTF-8"),
TEXT("Error"),
MB_OK | MB_ICONERROR);
return false;
}
if (bEncodingRequired)
{
// 65001 = utf-8 # ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
Utf8Cmd = "CHCP 65001\r\n\r\n" + utf8_str;
}
else
{
Utf8Cmd = utf8_str;
}
#else
Utf8Cmd = Cmd;
#endif
// Overwrite/create the previous temp Batch file
LPCTSTR CmdFileName = GenerateWorkBatchFileName();
// Write the temp Batch file (as binary)
FILE *fp;
if (_tfopen_s(&fp, CmdFileName, _TEXT("wb")) != 0)
{
stringT err_msg = TEXT("Failed to write batch file to: ");
err_msg += CmdFileName;
MessageBox(
hDlg,
err_msg.c_str(),
TEXT("Error"),
MB_OK | MB_ICONERROR);
return false;
}
fwrite(&Utf8Cmd[0], 1, Utf8Cmd.size(), fp);
fclose(fp);
// Execute the temp batch file
return SUCCEEDED(
ShellExecute(
hDlg,
_TEXT("open"),
CmdFileName,
NULL,
NULL,
SW_SHOW));
}
//-------------------------------------------------------------------------
// Execute the command typed in the command textbox
bool ResetPermissionDialog::ExecuteWindowCommand(bool bValidateFolder)
{
// Warn if this is a root folder
if (bValidateFolder)
{
stringT Path;
if (!GetFolderText(Path, true, false, false))
return false;
}
// Get the window command
stringT Cmd;
if (!GetCommandWindowText(Cmd))
{
MessageBox(
hDlg,
TEXT("Failed to get command text"),
TEXT("Error"),
MB_OK | MB_ICONERROR);
return false;
}
// Execute the command
if (!ExecuteCommand(Cmd))
{
MessageBox(
hDlg,
TEXT("Failed to execute get command text"),
TEXT("Error"),
MB_OK | MB_ICONERROR);
return false;
}
return true;
}
//-------------------------------------------------------------------------
INT_PTR CALLBACK ResetPermissionDialog::AboutDlgProc(
HWND hAboutDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
if (message == WM_INITDIALOG)
{
return TRUE;
}
else if (message == WM_COMMAND && LOWORD(wParam) == IDOK)
{
EndDialog(hAboutDlg, 0);
return TRUE;
}
return FALSE;
}
//-------------------------------------------------------------------------
INT_PTR CALLBACK ResetPermissionDialog::s_MainDialogProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
ResetPermissionDialog *Dlg;
// Dialog initialized? Let's setup / remember the instance pointer
if (message == WM_INITDIALOG)
{
// Get the passed instance
Dlg = (ResetPermissionDialog *)lParam;
// Associate the dialog instance with the window's user data
SetWindowLongPtr(
hWnd,
GWLP_USERDATA,
LONG_PTR(Dlg));
}
// Extract the dialog instance from the window's data
else
{
Dlg = (ResetPermissionDialog *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
}
// Dispatch the message to this specific dialog instance
INT_PTR Ret = Dlg == nullptr ? FALSE : Dlg->MainDialogProc(hWnd, message, wParam, lParam);
if (message == WM_DESTROY)
{
// Delete the dialog instance
delete Dlg;
}
return Ret;
}
//-------------------------------------------------------------------------
INT_PTR CALLBACK ResetPermissionDialog::MainDialogProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
hDlg = hWnd;
// Set the initial states/configuration
bRecurse = true;
bResetPerm = true;
bRmHidSys = false;
bTakeOwn = false;
bDontFollowLinks = true;
UpdateCheckboxes(false);
HICON hIcon = LoadIcon(
hInstance,
MAKEINTRESOURCE(IDI_SMALL));
SendMessage(
hDlg,
WM_SETICON,
ICON_BIG,
(LPARAM)hIcon);
LPCTSTR Arg = GetArg(1);
#ifdef _DEBUG
if (Arg == NULL)
Arg = _TEXT("C:\\Temp\\perm");
// Enable editing for the folder text editbox in debug mode
SendDlgItemMessage(hDlg, IDTXT_FOLDER, EM_SETREADONLY, FALSE, 0);
#endif
if (Arg != NULL)
{
SetFolderText(Arg);
UpdateCommandText();
}
return TRUE;
}
case WM_MENUCOMMAND:
break;
case WM_COMMAND:
{
UINT wmId = LOWORD(wParam);
UINT wmEvent = HIWORD(wParam);
#ifdef _DEBUG
TCHAR b[1024];
_sntprintf_s(
b,
_countof(b),
_TEXT("WM_COMMAND: wmParam=%08X lParam=%08X | ID=%04X Event=%04X\n"),
wParam,
lParam,
wmId,
wmEvent);
OutputDebugString(b);
// Reflect the folder text changes when the control is editable
if (wmId == IDTXT_FOLDER && wmEvent == EN_CHANGE)
UpdateCommandText();
#endif
switch (wmId)
{
//
// Handle checkboxes
//
case IDCHK_RECURSE:
case IDCHK_DONTFOLLOWLINKS:
case IDCHK_TAKEOWN:
case IDCHK_RESETPERM:
case IDCHK_RM_HS:
{
// Reforumulate the command text on each option change
if (wmEvent == BN_CLICKED)
{
UpdateCommandText();
return TRUE;
}
break;
}
//
// Handle context menu
//
case IDM_ADDTOEXPLORERFOLDERCONTEXTMENU:
case IDM_REMOVEFROMEXPLORERFOLDERCONTEXTMENU:
AddToExplorerContextMenu(wmId == IDM_ADDTOEXPLORERFOLDERCONTEXTMENU);
break;
case IDM_BACKUPPERMSCONTEXTMENU:
case IDM_RESTOREPERMSCONTEXTMENU:
BackRestorePermissions(wmId == IDM_BACKUPPERMSCONTEXTMENU);
break;
//
// About box
//
case IDBTN_ABOUT:
{
DialogBox(
hInstance,
MAKEINTRESOURCE(IDD_ABOUTBOX),
hDlg,
AboutDlgProc);
return TRUE;
}
//
// Choose folder
//
case IDBTN_CHOOSE_FOLDER:
{
stringT Folder;
if (BrowseFolder(hDlg, STR_SELECT_FOLDER, Folder))
{
SetFolderText(Folder.c_str());
UpdateCommandText();
}
return TRUE;
}
//
// Trigger the "Advanced" menu
//
case IDBTN_ADVANCED:
{
ShowPopupMenu(IDR_ADVANCED_MENU, IDBTN_ADVANCED);
return TRUE;
}
//
// GO button
//
case IDOK:
{
// Validate the input folder and execute the command
ExecuteWindowCommand(true);
return TRUE;
}
// HELP button
case IDBTN_HELP:
{
ShellExecute(