forked from bailey27/cppcryptfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppcryptfs.cpp
More file actions
executable file
·397 lines (288 loc) · 11.6 KB
/
Copy pathcppcryptfs.cpp
File metadata and controls
executable file
·397 lines (288 loc) · 11.6 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
/*
cppcryptfs : user-mode cryptographic virtual overlay filesystem.
Copyright (C) 2016-2022 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.
*/
// cppcryptfs.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include <iostream>
#include "cppcryptfs.h"
#include "dokan/cryptdokan.h"
#include "crypt/cryptdefs.h"
#include "ui/CryptPropertySheet.h"
#include "ui/MountPropertyPage.h"
#include "ui/CreatePropertyPage.h"
#include "ui/SettingsPropertyPage.h"
#include "ui/MoreSettingsPropertyPage.h"
#include "ui/CryptAboutPropertyPage.h"
#include "ui/RecentItems.h"
#include "ui/TrayIcon.h"
#include "dokan/cryptdokan.h"
#include "util/getopt.h"
#include "util/LockZeroBuffer.h"
#include "util/util.h"
#include "crypt/crypt.h"
#include "ui/uiutil.h"
#include "../libipc/server.h"
#include "../libipc/client.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CcppcryptfsApp
BEGIN_MESSAGE_MAP(CcppcryptfsApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// CcppcryptfsApp construction
CcppcryptfsApp::CcppcryptfsApp()
{
// support Restart Manager
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_mountedLetters = 0;
// get a shared ptr to an OpenSSL EVP context to force detection of AES-NI instructions
// so we can use AES-NI even if EVP is never used
auto context = get_crypt_context(BLOCK_IV_LEN, AES_MODE_GCM);
}
// The one and only CcppcryptfsApp object
CcppcryptfsApp theApp;
static void NamedPipeServerCallback(void* ctx, HANDLE hPipe)
{
auto pApp = reinterpret_cast<CcppcryptfsApp*>(ctx);
pApp->SendCmdArgsToSelf(hPipe);
}
static bool StartNamedPipeServer()
{
static NamedPipeServerContext ctx;
ctx.context = &theApp;
ctx.callback = NamedPipeServerCallback;
auto hThread = CreateThread(NULL, 0, NamedPipeServerThreadProc, &ctx, 0, NULL);
if (hThread != NULL)
CloseHandle(hThread);
return hThread != NULL;
}
// CcppcryptfsApp initialization
BOOL CcppcryptfsApp::InitInstance()
{
const WCHAR *szUniqueNamedMutex = L"cppcryptfs-A7DDB0CF-A856-4E8A-A4E9-722473FB5E49";
HANDLE hAppMutex = CreateMutex(NULL, TRUE, szUniqueNamedMutex);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
// Program already running - but hAppMutex is NOT NULL.
// Aparently it is not necessary to close it in this case.
// Do something and then return FALSE to exit the app.
// we can't use classname because our main window is a PropertySheet and has
// class name "DIALOG". I've seen info about how to change the class
// name of a dialog-based app, but not for one based on a PropertySheet
// TODO - it may be possible to use a custom class name
// currently use "#32770" for the class (which is the class string for generic dialog boxes)
HWND hWnd = FindWindow(L"#32770", L"cppcryptfs");
if (hWnd) {
if (have_args()) {
bool have_console = OpenConsole(0);
wstring err_mes;
wstring result;
if (SendArgsToRunningInstance(GetCommandLine(), result, err_mes)) {
if (err_mes.length() > 0)
err_mes += L"\n";
else
err_mes = L"cppcryptfsctl: Unable to send command. Is cppcryptfs really already running?\n";
} else {
if (result.length() >= CMD_PIPE_RESPONSE_LENGTH) {
if (wcsncmp(result.c_str(), CMD_PIPE_SUCCESS_STR, CMD_PIPE_RESPONSE_LENGTH) == 0) {
wstring mes = result.c_str() + CMD_PIPE_RESPONSE_LENGTH;
if (mes.length() > 0) {
if (have_console)
wcout << mes << endl;
else
::MessageBox(NULL, mes.c_str(), L"cppcryptfs", MB_OK);
}
} else {
err_mes = wstring(result.c_str() + CMD_PIPE_RESPONSE_LENGTH);
}
} else {
err_mes = L"cppcryptfs: got a mal-formed response from running instance of cppcryptfs\n";
}
}
if (err_mes.length() > 0) {
if (have_console) {
wcerr << err_mes;
} else {
::MessageBox(NULL, err_mes.c_str(), L"cppcryptfs", MB_ICONERROR | MB_OK);
}
}
if (have_console)
CloseConsole();
} else { // if no args, then restore window of running instance
ShowWindow(hWnd, SW_SHOWNORMAL);
}
} else {
::MessageBox(NULL, L"cppcryptfs is already running, but window not found!", L"cppcryptfs", MB_OK | MB_ICONERROR);
}
return FALSE;
} else {
wstring mes;
bool dokVerCheck = check_dokany_version(mes);
if (!dokVerCheck && mes.length() < 1) {
mes = L"problem with Dokany version";
}
if (mes.length()) {
::MessageBox(NULL, mes.c_str(), L"cppcryptfs", MB_OK | (dokVerCheck ? MB_ICONEXCLAMATION : MB_ICONERROR));
}
if (!dokVerCheck) {
return FALSE;
}
}
StartNamedPipeServer();
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Activate "Windows Native" visual manager for enabling themes in MFC controls
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("cppcryptfs"));
CCryptPropertySheet dlg(L"cppcryptfs");
CMountPropertyPage mount;
RecentItems ritems(CPPCRYPTFS_FOLDERS_SECTION, TEXT("LastDir"), mount.m_numLastDirs);
ritems.Populate(mount.m_lastDirs, TEXT("C:\\"));
RecentItems ritems4(CPPCRYPTFS_CONFIGPATHS_SECTION, TEXT("LastConfig"), mount.m_numLastConfigs);
ritems4.Populate(mount.m_lastConfigs, TEXT("C:\\"));
dlg.AddPage(&mount);
dlg.m_nMountPageIndex = dlg.GetPageCount() - 1;
CCreatePropertyPage create;
RecentItems ritems2(CPPCRYPTFS_FOLDERS_SECTION, TEXT("LastDir"), create.m_numLastDirs);
ritems2.Populate(create.m_lastDirs, TEXT("C:\\"));
RecentItems ritems3(CPPCRYPTFS_CONFIGPATHS_SECTION, TEXT("LastConfig"), create.m_numLastConfigs);
ritems3.Populate(create.m_lastConfigs, TEXT("C:\\"));
dlg.AddPage(&create);
CSettingsPropertyPage settings;
dlg.AddPage(&settings);
CMoreSettingsPropertyPage more_settings;
dlg.AddPage(&more_settings);
CCryptAboutPropertyPage about;
dlg.AddPage(&about);
m_pMainWnd = &dlg;
HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
CMenuTrayIcon TI(L"cppcryptfs", hIcon, IDR_PopUps, ID_IDR_SHOWCPPCRYPTFS);
crypt_at_start();
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
else if (nResponse == -1)
{
TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
}
// Delete the shell manager created above.
if (pShellManager != NULL)
{
delete pShellManager;
}
// Upon app closing:
if (hAppMutex) {
ReleaseMutex(hAppMutex); // Explicitly release mutex
CloseHandle(hAppMutex); // close handle before terminating
}
// any at app exit cleanup of the encryted filesystems occurs here
crypt_at_exit();
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
BOOL CcppcryptfsApp::WriteProfileInt(LPCWSTR section, LPCWSTR entry, INT val)
{
if (lstrcmpi(section, CryptSettingsRegValName) && NeverSaveHistory()) {
return TRUE;
}
return CWinApp::WriteProfileInt(section, entry, val);
}
BOOL CcppcryptfsApp::WriteProfileString(LPCWSTR section, LPCWSTR entry, LPCWSTR val)
{
if (lstrcmpi(section, CryptSettingsRegValName) && NeverSaveHistory()) {
return TRUE;
}
return CWinApp::WriteProfileString(section, entry, val);
}
BOOL CcppcryptfsApp::WriteProfileBinary(LPCWSTR section, LPCWSTR entry, LPBYTE pData, UINT nBytes)
{
if (lstrcmpi(section, CryptSettingsRegValName) && NeverSaveHistory()) {
return TRUE;
}
return CWinApp::WriteProfileBinary(section, entry, pData, nBytes);
}
void CcppcryptfsApp::SendCmdArgsToSelf(HANDLE hPipe)
{
HWND hWnd = ::FindWindow(L"#32770", L"cppcryptfs");
if (!hWnd)
return;
DWORD win_proc_id = 0;
GetWindowThreadProcessId(hWnd, &win_proc_id);
if (win_proc_id != ::GetCurrentProcessId())
return;
static_assert(sizeof(WCHAR) == sizeof(wchar_t), "sizeof(WCHAR) != sizeof(wchar_t).");
COPYDATASTRUCT cd;
memset(&cd, 0, sizeof(cd));
cd.dwData = CPPCRYPTFS_COPYDATA_PIPE;
std::vector<HANDLE> pipe_v;
pipe_v.push_back(hPipe);
cd.cbData =static_cast<DWORD>(sizeof(hPipe));
cd.lpData = static_cast<PVOID>(&pipe_v[0]);
SetLastError(0);
SendMessageW(hWnd, WM_COPYDATA, NULL, (LPARAM)&cd);
DWORD dwErr = GetLastError();
if (dwErr) {
if (dwErr == ERROR_ACCESS_DENIED) {
ConsoleErrMesPipe(L"SendMessage() returned error \"access denied\".\n\nPerhaps there is"
" already an instance of cppcryptfs running with administrator\nprivileges, but"
" you invoked this instance of cppcryptfs from a command prompt\nthat is not running"
" with administrator privileges.\n\nIf this is the case, then you should start a"
" CMD.exe window using\n\"Run as administrator\" and invoke cppcryptfs from within it.", hPipe);
} else {
WCHAR buf[80];
_snwprintf_s(buf, _TRUNCATE, L"SendMessage() returned error code %u", dwErr);
ConsoleErrMesPipe(buf, hPipe);
}
}
}