forked from bailey27/cppcryptfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileutil.cpp
More file actions
1143 lines (849 loc) · 27 KB
/
Copy pathfileutil.cpp
File metadata and controls
1143 lines (849 loc) · 27 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
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
/*
cppcryptfs : user-mode cryptographic virtual overlay filesystem.
Copyright (C) 2016-2026 Bailey Brown (github.com/bailey27/cppcryptfs)
cppcryptfs is based on the design of gocryptfs (github.com/rfjakob/gocryptfs)
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "stdafx.h"
#ifdef _WIN32
#include "Shlwapi.h"
#endif
#include "crypt/cryptdefs.h"
#include "util/fileutil.h"
#include "filename/cryptfilename.h"
#include <string>
#include <vector>
#include "filename/dirivcache.h"
#ifdef _WIN32
// derive attributes for virtual reverse-mode diriv file from
// the attributes of its directory
static DWORD
VirtualAttributesDirIv(DWORD attr)
{
attr &= ~(FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_NORMAL);
attr |= FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_ARCHIVE;
return attr;
}
// derive attributes for virtual reverse-mode longname name file
// from the attributes of its related file or directory
static DWORD
VirtualAttributesNameFile(DWORD attr)
{
bool bForDir = (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
attr &= ~FILE_ATTRIBUTE_DIRECTORY;
if (attr == 0)
attr = bForDir ? FILE_ATTRIBUTE_ARCHIVE : FILE_ATTRIBUTE_NORMAL;
return attr;
}
#endif // _WIN32
bool
adjust_file_offset_down(LARGE_INTEGER& l)
{
long long offset = l.QuadPart;
if (offset < 0)
return false;
if (offset == 0)
return true;
long long blocks = (offset - CIPHER_FILE_OVERHEAD + CIPHER_BS - 1) / CIPHER_BS;
offset -= (blocks*CIPHER_BLOCK_OVERHEAD + CIPHER_FILE_OVERHEAD);
if (offset < 0)
return false;
l.QuadPart = offset;
return true;
}
bool
adjust_file_offset_up(LARGE_INTEGER& l)
{
long long offset = l.QuadPart;
if (offset < 0)
return false;
if (offset == 0)
return true;
long long blocks = (offset + PLAIN_BS - 1) / PLAIN_BS;
offset += (blocks*CIPHER_BLOCK_OVERHEAD + CIPHER_FILE_OVERHEAD);
l.QuadPart = offset;
return true;
}
bool adjust_file_size_down(LARGE_INTEGER& l)
{
return adjust_file_offset_down(l);
}
bool adjust_file_size_up(LARGE_INTEGER& l)
{
return adjust_file_offset_up(l);
}
bool
adjust_file_offset_up_truncate_zero(LARGE_INTEGER& l)
{
long long offset = l.QuadPart;
if (offset < 0)
return false;
if (offset == 0) // truncate zero-length file to 0 bytes
return true;
long long blocks = (offset + PLAIN_BS - 1) / PLAIN_BS;
offset += blocks*CIPHER_BLOCK_OVERHEAD + CIPHER_FILE_OVERHEAD;
l.QuadPart = offset;
return true;
}
#ifdef _WIN32
static bool
read_dir_iv(const TCHAR *path, unsigned char *diriv, FILETIME& LastWriteTime)
{
HANDLE hfile = INVALID_HANDLE_VALUE;
DWORD nRead = 0;
bool caught_wstring = false;
try {
wstring path_str;
path_str.append(path);
if (path_str[path_str.size() - 1] != '\\') {
path_str.push_back('\\');
}
path_str.append(DIR_IV_NAME);
hfile = ::CreateFile(&path_str[0], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hfile == INVALID_HANDLE_VALUE) {
auto lasterr =::GetLastError();
throw(L"\terror opening diriv file " + path_str + L" lasterr = " + to_wstring(lasterr));
}
if (!::ReadFile(hfile, diriv, DIR_IV_LEN, &nRead, NULL)) {
auto lasterr = ::GetLastError();
throw(L"\terror reading diriv file " + path_str + L" lasterr = " + to_wstring(lasterr));
}
if (!::GetFileTime(hfile, NULL, NULL, &LastWriteTime)) {
auto lasterr = ::GetLastError();
throw(L"\terror getting filetime of diriv file " + path_str + L" lasterr = " + to_wstring(lasterr));
}
} catch (const wstring& mes) {
DbgPrint(L"\tget_diriv: %s\n", mes.c_str());
nRead = 0;
caught_wstring = true;
} catch (...) {
nRead = 0;
}
if (hfile != INVALID_HANDLE_VALUE)
::CloseHandle(hfile);
if (!caught_wstring && nRead != DIR_IV_LEN) {
DbgPrint(L"\tget_diriv read incorrect number of bytes from %s, read %u bytes instead of %u\n", path, nRead, DIR_IV_LEN);
}
return nRead == DIR_IV_LEN;
}
#endif // _WIN32
bool
get_dir_iv(CryptContext *con, const WCHAR *path, unsigned char *diriv)
{
if (con && !con->GetConfig()->DirIV()) {
memset(diriv, 0, DIR_IV_LEN);
return true;
}
bool bret = true;
try {
if (con && con->GetConfig()->m_reverse) {
throw(wstring(L"\tcalled for reverse fs"));
}
if (!con->m_dir_iv_cache.lookup(path, diriv)) {
FILETIME LastWritten;
if (!read_dir_iv(path, diriv, LastWritten))
throw(wstring(L"read_dir_iv failed for") + path);
if (!con->m_dir_iv_cache.store(path, diriv, LastWritten)) {
throw(wstring(L"store_diriv failed for ") + path);
}
}
} catch (const wstring& mes) {
DbgPrint(L"\tget_diriv %s\n", mes.c_str());
bret = false;
} catch (...) {
bret = false;
}
return bret;
}
static bool
convert_fdata(CryptContext *con, BOOL isRoot, const BYTE *dir_iv, const WCHAR *path, WIN32_FIND_DATAW& fdata, string *actual_encrypted, wstring& storage)
{
if (!wcscmp(fdata.cFileName, L".") || !wcscmp(fdata.cFileName, L".."))
return true;
bool isReverseConfig = isRoot && con->GetConfig()->m_reverse && !wcscmp(fdata.cFileName, REVERSE_CONFIG_NAME);
long long size = ((long long)fdata.nFileSizeHigh << 32) | fdata.nFileSizeLow;
if (size > 0 && !(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !isReverseConfig) {
LARGE_INTEGER l;
l.LowPart = fdata.nFileSizeLow;
l.HighPart = fdata.nFileSizeHigh;
if (con->GetConfig()->m_reverse) {
if (!adjust_file_size_up(l))
return false;
} else {
if (!adjust_file_size_down(l))
return false;
}
fdata.nFileSizeHigh = l.HighPart;
fdata.nFileSizeLow = l.LowPart;
}
if (wcscmp(fdata.cFileName, L".") && wcscmp(fdata.cFileName, L"..")) {
storage.clear();
const WCHAR *dname;
if (isReverseConfig) {
fdata.dwFileAttributes &= ~FILE_ATTRIBUTE_HIDDEN;
dname = CONFIG_NAME;
} else {
if (con->GetConfig()->m_reverse) {
dname = encrypt_filename(con, dir_iv, fdata.cFileName, storage, actual_encrypted);
} else {
dname = decrypt_filename(con, dir_iv, path, fdata.cFileName, storage);
}
}
if (!dname)
return false;
// wcscpy_s throws a weird exception if src is too long so check the length before calling it
if (wcslen(dname) >= std::size(fdata.cFileName)) {
return false;
}
if (wcscpy_s(fdata.cFileName, dname)) {
return false;
}
}
// short name - not really needed
fdata.cAlternateFileName[0] = '\0';
return true;
}
static bool is_interesting_name(BOOL isRoot, const WIN32_FIND_DATAW& fdata, CryptContext *con)
{
bool reverse = con->GetConfig()->m_reverse;
bool plaintext = con->GetConfig()->m_PlaintextNames;
bool isconfig = !lstrcmpi(fdata.cFileName, CONFIG_NAME);
if (isRoot && (!wcscmp(fdata.cFileName, L".") || !wcscmp(fdata.cFileName, L".."))) {
return false;
} else if ((!reverse && isRoot && isconfig) || (!reverse && !plaintext && !lstrcmpi(fdata.cFileName, DIR_IV_NAME))) {
return false;
} else if (!plaintext && !reverse && is_long_name_file(fdata.cFileName)) {
return false;
} else if (isRoot && reverse && plaintext && isconfig) {
return false;
} else {
return true;
}
}
#ifdef _WIN32
DWORD
find_files(CryptContext *con, const WCHAR *pt_path, const WCHAR *path, PCryptFillFindData fillData, void * dokan_cb, void * dokan_ctx)
{
DWORD ret = 0;
HANDLE hfind = INVALID_HANDLE_VALUE;
bool reverse = con->GetConfig()->m_reverse;
bool plaintext_names = con->GetConfig()->m_PlaintextNames;
KeyDecryptor kdc(plaintext_names ? nullptr : &con->GetConfig()->m_keybuf_manager);
list<wstring> files; // used only if case-insensitive
try {
wstring enc_path_search = path;
WIN32_FIND_DATAW fdata, fdata_dot;
if (enc_path_search[enc_path_search.size() - 1] != '\\')
enc_path_search.push_back('\\');
enc_path_search.push_back('*');
hfind = FindFirstFile(&enc_path_search[0], &fdata);
if (hfind == INVALID_HANDLE_VALUE)
throw((int)GetLastError());
BYTE dir_iv[DIR_IV_LEN];
if (reverse) {
if (!derive_path_iv(con, pt_path, dir_iv, TYPE_DIRIV)) {
throw((int)ERROR_PATH_NOT_FOUND);
}
} else {
if (!get_dir_iv(con, path, dir_iv)) {
DWORD error = GetLastError();
if (error == 0)
error = ERROR_PATH_NOT_FOUND;
throw((int)error);
}
}
bool isRoot = !wcscmp(pt_path, L"\\");
string actual_encrypted;
wstring storage;
storage.reserve(MAX_PATH);
do {
if (reverse && !wcscmp(fdata.cFileName, L".")) {
fdata_dot = fdata;
}
if (!is_interesting_name(isRoot, fdata, con))
continue;
WIN32_FIND_DATAW fdata_orig = fdata;
if (!convert_fdata(con, isRoot, dir_iv, path, fdata, &actual_encrypted, storage))
continue;
fillData(&fdata, &fdata_orig, dokan_cb, dokan_ctx);
if (reverse && !plaintext_names && is_long_name(fdata.cFileName)) {
wcscat_s(fdata.cFileName, MAX_PATH, LONGNAME_SUFFIX_W);
fdata.dwFileAttributes = VirtualAttributesNameFile(fdata.dwFileAttributes);
fdata.ftLastWriteTime = fdata.ftCreationTime;
fdata.cAlternateFileName[0] = '\0';
fdata.nFileSizeHigh = 0;
fdata.nFileSizeLow = (DWORD)actual_encrypted.length();
fdata_orig = fdata;
fillData(&fdata, &fdata_orig, dokan_cb, dokan_ctx);
}
if (con->IsCaseInsensitive()) {
files.push_back(fdata.cFileName);
}
} while (FindNextFile(hfind, &fdata));
DWORD err = GetLastError();
if (err != ERROR_NO_MORE_FILES)
throw((int)err);
if (reverse && !plaintext_names) {
fdata_dot.cAlternateFileName[0] = '\0';
wcscpy_s(fdata_dot.cFileName, DIR_IV_NAME);
fdata_dot.nFileSizeHigh = 0;
fdata_dot.nFileSizeLow = DIR_IV_LEN;
fdata_dot.ftLastWriteTime = fdata_dot.ftCreationTime;
fdata_dot.dwFileAttributes = VirtualAttributesDirIv(fdata_dot.dwFileAttributes);
WIN32_FIND_DATAW fdata_orig = fdata_dot;
fillData(&fdata_dot, &fdata_orig, dokan_cb, dokan_ctx);
}
ret = 0;
} catch (int error) {
ret = (DWORD)error;
} catch (...) {
ret = ERROR_OUTOFMEMORY;
}
if (hfind != INVALID_HANDLE_VALUE)
FindClose(hfind);
if (ret == 0 && con->IsCaseInsensitive())
con->m_case_cache.store(pt_path, files);
return ret;
}
#endif // _WIN32
bool
read_virtual_file(CryptContext *con, LPCWSTR FileName, unsigned char *buf, DWORD buflen, LPDWORD pNread, LONGLONG offset)
{
if (rt_is_dir_iv_file(con, FileName)) {
wstring dirpath;
if (!get_file_directory(FileName, dirpath))
return false;
BYTE dir_iv[DIR_IV_LEN];
if (!derive_path_iv(con, &dirpath[0], dir_iv, TYPE_DIRIV))
return false;
LONGLONG count = min(DIR_IV_LEN - offset, buflen);
if (count <= 0) {
*pNread = 0;
return true;
}
memcpy(buf, dir_iv + offset, static_cast<size_t>(count));
*pNread = (DWORD)count;
return true;
} else if (rt_is_name_file(con, FileName)) {
string actual_encrypted;
if (!get_actual_encrypted(con, FileName, actual_encrypted))
return false;
LONGLONG count = min(actual_encrypted.length() - offset, buflen);
if (count <= 0) {
*pNread = 0;
return true;
}
memcpy(buf, &actual_encrypted[0], static_cast<size_t>(count));
*pNread = (DWORD)count;
return true;
} else {
return false;
}
}
#ifdef _WIN32
DWORD
get_file_information(CryptContext *con, function<LPCWSTR()> getEncPath, LPCWSTR inputPath, HANDLE handle, LPBY_HANDLE_FILE_INFORMATION pInfo)
{
BOOL opened = FALSE;
DWORD dwRet = 0;
bool is_config = rt_is_config_file(con, inputPath);
bool is_dir_iv = rt_is_dir_iv_file(con, inputPath);
bool is_virtual = rt_is_virtual_file(con, inputPath);
bool is_name_file = rt_is_name_file(con, inputPath);
try {
if (!handle || (handle == INVALID_HANDLE_VALUE && !is_virtual)) {
throw((int)ERROR_INVALID_PARAMETER);
}
if (is_dir_iv) {
wstring dirpath;
if (!get_file_directory(getEncPath(), dirpath))
throw((int)ERROR_ACCESS_DENIED);
HANDLE hDir = CreateFile(&dirpath[0], FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir == INVALID_HANDLE_VALUE)
throw((int)GetLastError());
BOOL bResult = GetFileInformationByHandle(hDir, pInfo);
if (!bResult) {
DWORD lastErr = GetLastError();
CloseHandle(hDir);
throw((int)lastErr);
}
CloseHandle(hDir);
pInfo->dwFileAttributes = VirtualAttributesDirIv(pInfo->dwFileAttributes);
pInfo->ftLastWriteTime = pInfo->ftCreationTime;
pInfo->nFileSizeHigh = 0;
pInfo->nFileSizeLow = DIR_IV_LEN;
pInfo->nNumberOfLinks = 1;
} else if (is_name_file) {
wstring enc_filename;
remove_longname_suffix(inputPath, enc_filename);
wstring decrypted_name;
if (!decrypt_path(con, &enc_filename[0], decrypted_name))
throw((int)ERROR_ACCESS_DENIED);
HANDLE hFile = CreateFile(&decrypted_name[0], FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile == INVALID_HANDLE_VALUE)
throw((int)GetLastError());
BOOL bResult = GetFileInformationByHandle(hFile, pInfo);
if (!bResult) {
DWORD lastErr = GetLastError();
CloseHandle(hFile);
throw((int)lastErr);
}
CloseHandle(hFile);
string actual_encrypted;
if (!get_actual_encrypted(con, &enc_filename[0], actual_encrypted))
throw((int)ERROR_ACCESS_DENIED);
pInfo->dwFileAttributes = VirtualAttributesNameFile(pInfo->dwFileAttributes);
pInfo->ftLastWriteTime = pInfo->ftCreationTime;
pInfo->nFileSizeHigh = 0;
pInfo->nFileSizeLow = (DWORD)actual_encrypted.length();
pInfo->nNumberOfLinks = 1;
} else if (!GetFileInformationByHandle(handle, pInfo)) {
if (opened) {
opened = FALSE;
CloseHandle(handle);
}
// FileName is a root directory
// in this case, FindFirstFile can't get directory information
if (wcslen(getEncPath()) == 1) {
pInfo->dwFileAttributes = GetFileAttributes(getEncPath());
} else {
WIN32_FIND_DATAW find;
ZeroMemory(&find, sizeof(WIN32_FIND_DATAW));
HANDLE findHandle = FindFirstFile(getEncPath(), &find);
if (findHandle == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
throw((int)error);
}
pInfo->dwFileAttributes = find.dwFileAttributes;
pInfo->ftCreationTime = find.ftCreationTime;
pInfo->ftLastAccessTime = find.ftLastAccessTime;
pInfo->ftLastWriteTime = find.ftLastWriteTime;
pInfo->nFileSizeHigh = find.nFileSizeHigh;
pInfo->nFileSizeLow = find.nFileSizeLow;
FindClose(findHandle);
}
}
if (!is_config && !is_virtual && !(pInfo->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
LARGE_INTEGER l;
l.LowPart = pInfo->nFileSizeLow;
l.HighPart = pInfo->nFileSizeHigh;
if (con->GetConfig()->m_reverse) {
if (!adjust_file_size_up(l))
throw((int)ERROR_INVALID_PARAMETER);
} else {
if (!adjust_file_size_down(l))
throw((int)ERROR_INVALID_PARAMETER);
}
pInfo->nFileSizeLow = l.LowPart;
pInfo->nFileSizeHigh = l.HighPart;
}
if (is_config)
pInfo->dwFileAttributes &= ~FILE_ATTRIBUTE_HIDDEN;
} catch (int err) {
dwRet = (DWORD)err;
} catch (...) {
dwRet = ERROR_ACCESS_DENIED;
}
if (opened)
CloseHandle(handle);
if (dwRet)
SetLastError(dwRet);
return dwRet;
}
#endif // _WIN32
#ifdef _WIN32
bool
create_dir_iv(CryptContext *con, LPCWSTR path)
{
if (con && !con->GetConfig()->DirIV())
return true;
DWORD error = 0;
HANDLE hfile = INVALID_HANDLE_VALUE;
try {
unsigned char diriv[DIR_IV_LEN];
if (!get_random_bytes(con, diriv, DIR_IV_LEN))
throw ((int)(GetLastError() ? GetLastError() : ERROR_OUTOFMEMORY));
wstring path_str = path;
LPCWSTR encpath = &path_str[0];
if (!encpath)
throw((int)ERROR_OUTOFMEMORY);
if (path_str[path_str.size() - 1] != '\\') {
path_str.push_back('\\');
}
path_str.append(DIR_IV_NAME);
hfile = CreateFile(&path_str[0], GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL);
if (hfile == INVALID_HANDLE_VALUE)
throw((int)ERROR_ACCESS_DENIED);
DWORD nWritten = 0;
if (!WriteFile(hfile, diriv, DIR_IV_LEN, &nWritten, NULL)) {
throw((int)GetLastError());
}
if (nWritten != DIR_IV_LEN) {
throw((int)ERROR_OUTOFMEMORY);
}
FILETIME LastWriteTime;
if (!GetFileTime(hfile, NULL, NULL, &LastWriteTime)) {
throw((int)GetLastError());
}
CloseHandle(hfile);
hfile = INVALID_HANDLE_VALUE;
// assume somebody will want to use it soon
if (con)
con->m_dir_iv_cache.store(path, diriv, LastWriteTime);
DWORD attr = GetFileAttributesW(&path_str[0]);
if (attr != INVALID_FILE_ATTRIBUTES) {
attr |= FILE_ATTRIBUTE_READONLY;
SetFileAttributes(&path_str[0], attr);
}
} catch (int err) {
error = (DWORD)err;
} catch (...) {
error = ERROR_OUTOFMEMORY;
}
if (hfile != INVALID_HANDLE_VALUE)
CloseHandle(hfile);
return error == 0;
}
#endif // _WIN32
#ifdef _WIN32
bool
is_empty_directory(LPCWSTR path, BOOL bMustReallyBeEmpty, CryptContext *con)
{
bool bret = true;
WIN32_FIND_DATAW findData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD error = 0;
try {
wstring enc_path = path;
const WCHAR *filePath = &enc_path[0];
if (!filePath)
throw((int)ERROR_FILE_NOT_FOUND);
if (enc_path[enc_path.size() - 1] != '\\')
enc_path.push_back('\\');
enc_path.push_back('*');
filePath = &enc_path[0];
hFind = FindFirstFile(filePath, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
error = GetLastError();
throw((int)error);
}
vector<wstring> dummy;
auto& deletable_files = con ? con->GetDeletableFiles() : dummy;
auto can_be_deleted = [](const vector<wstring>& deletable_files, LPCWSTR fname) -> bool {
auto count = deletable_files.size();
for (size_t i = 0; i < count; i++) {
if (lstrcmpi(deletable_files[i].c_str(), fname) == 0)
return true;
}
return false;
};
while (hFind != INVALID_HANDLE_VALUE) {
if (wcscmp(findData.cFileName, L"..") != 0 &&
wcscmp(findData.cFileName, L".") != 0 &&
(bMustReallyBeEmpty || !can_be_deleted(deletable_files, findData.cFileName))) {
throw((int)ERROR_DIR_NOT_EMPTY);
}
if (!FindNextFile(hFind, &findData)) {
break;
}
}
error = GetLastError();
if (error != ERROR_NO_MORE_FILES) {
throw((int)error);
}
} catch (int err) {
SetLastError((DWORD)err);
bret = false;
}
error = GetLastError();
if (hFind != INVALID_HANDLE_VALUE && hFind != NULL)
FindClose(hFind);
if (error && !bret)
SetLastError(error);
return bret;
}
bool
can_delete_directory(LPCWSTR path, BOOL bMustReallyBeEmpty, CryptContext *con)
{
return is_empty_directory(path, bMustReallyBeEmpty, con);
}
bool can_delete_file(LPCWSTR path)
{
return true;
}
bool
delete_directory(CryptContext *con, LPCWSTR path)
{
bool bret = true;
try {
wstring path_with_slash = path;
if (path_with_slash[path_with_slash.size() - 1] != '\\')
path_with_slash.push_back('\\');
vector<wstring> dummy;
auto& files_to_delete = con ? con->GetDeletableFiles() : dummy;
wstring file_to_delete;
for (size_t i = 0; i < files_to_delete.size(); i++) {
file_to_delete = path_with_slash + files_to_delete[i];
DWORD attr = GetFileAttributes(file_to_delete.c_str());
if (attr != INVALID_FILE_ATTRIBUTES) {
if (attr & FILE_ATTRIBUTE_READONLY) {
attr &= ~FILE_ATTRIBUTE_READONLY;
if (!SetFileAttributes(file_to_delete.c_str(), attr)) {
throw((int)GetLastError());
}
}
if (!DeleteFile(file_to_delete.c_str())) {
throw((int)GetLastError());
}
}
}
if (con->GetConfig()->DirIV()) {
con->m_dir_iv_cache.remove(path);
}
if (!RemoveDirectory(path)) {
throw((int)GetLastError());
}
if (!con->GetConfig()->m_PlaintextNames && con->GetConfig()->m_LongNames && is_long_name(path)) {
wstring name_file = path;
if (name_file[name_file.size()-1] == '\\') {
name_file.erase(name_file.size() - 1);
}
name_file += LONGNAME_SUFFIX_W;
if (PathFileExists(&name_file[0])) {
if (!DeleteFile(&name_file[0])) {
throw((int)GetLastError());
}
}
}
} catch (int err) {
if (err)
SetLastError((DWORD)err);
bret = false;
} catch (...) {
bret = false;
}
return bret;
}
bool delete_file(const CryptContext *con, const WCHAR *filename, bool cleanup_longname_file_only)
{
if (!cleanup_longname_file_only && PathFileExists(filename)) {
if (!DeleteFile(filename))
return false;
}
if (!con->GetConfig()->m_PlaintextNames && con->GetConfig()->m_LongNames && is_long_name(filename)) {
wstring path = filename;
path += LONGNAME_SUFFIX_W;
if (PathFileExists(&path[0])) {
if (DeleteFile(&path[0]))
return true;
else
return false;
}
return true;
} else {
return true;
}
}
#endif // _WIN32
bool
get_dir_and_file_from_path(LPCWSTR path, wstring *dir, wstring *file)
{
if (!wcscmp(path, L"\\")) {
if (dir)
*dir = L"\\";
if (file)
*file = L"";
return true;
}
LPCWSTR pLastSlash = wcsrchr(path, '\\');
if (!pLastSlash) {
return false;
}
if (file) {
*file = pLastSlash + 1;
}
if (dir) {
if (pLastSlash != path) {
*dir = path;
dir->erase(pLastSlash - path);
} else {
*dir = L"\\";
}
}
return true;
}
bool
get_file_stream(LPCWSTR filename, wstring *file, wstring *stream)
{
LPCWSTR pStreamColon = wcschr(filename, ':');
if (pStreamColon) { // have stream
if (file) {
*file = filename;
file->erase(pStreamColon - filename);
}
if (stream)
*stream = pStreamColon;
return true;
} else {
if (file)
*file = filename;
if (stream)
*stream = L"";
return false;
}
}
bool
remove_stream_type(LPCWSTR stream, wstring& stream_without_type, wstring& type)
{
if (!stream || stream[0] != ':')
return false;
LPCWSTR pLastColon = wcsrchr(stream, ':');
if (pLastColon == stream) {
type = L"";
stream_without_type = stream;
return true;
}
stream_without_type = stream;
stream_without_type.erase(pLastColon - stream);
type = pLastColon;
return true;
}
bool
convert_find_stream_data(CryptContext *con, LPCWSTR pt_path, LPCWSTR path, WIN32_FIND_STREAM_DATA& fdata)
{
BYTE dir_iv[DIR_IV_LEN];
bool plaintext_names = con->GetConfig()->m_PlaintextNames;
bool reverse = con->GetConfig()->m_reverse;
if (!plaintext_names && wcscmp(fdata.cStreamName, L"::$DATA")) {
if (reverse) {
wstring dirpath;
if (!get_file_directory(pt_path, dirpath))
return false;
if (!derive_path_iv(con, dirpath.c_str(), dir_iv, TYPE_DIRIV))
return false;
wstring enc_stream;
if (!encrypt_stream_name(con, dir_iv, fdata.cStreamName, enc_stream))
return false;
wcscpy_s(fdata.cStreamName, enc_stream.c_str());