Skip to content

Commit dc4569d

Browse files
committed
reorganized named pipe ipc stuff
1 parent b82e8b9 commit dc4569d

27 files changed

Lines changed: 1011 additions & 259 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ Report*.vspx
1414
Report*.diagsession
1515
enc_temp_folder/
1616
cppcryptfs/fileutil.cpp.security_attributes_on_diriv
17+
cppcryptfs/namedpipe/
18+
cppcryptfsctl/cppcryptfsctl.vcxproj.filters
19+
cppcryptfsctl/cppcryptfsctl.vcxproj.user
20+
libipc/libipc.vcxproj.filters
21+
libipc/libipc.vcxproj.user

cppcryptfs.sln

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppcryptfs", "cppcryptfs\cppcryptfs.vcxproj", "{E21456E4-6518-425E-A6E8-D7D7E247FD0C}"
77
ProjectSection(ProjectDependencies) = postProject
88
{F647F2B8-6614-4D01-B237-F69E5DABF972} = {F647F2B8-6614-4D01-B237-F69E5DABF972}
9+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90} = {9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}
910
EndProjectSection
1011
EndProject
1112
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcppcryptfs", "libcppcryptfs\libcppcryptfs.vcxproj", "{F647F2B8-6614-4D01-B237-F69E5DABF972}"
1213
EndProject
1314
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppcryptfsctl", "cppcryptfsctl\cppcryptfsctl.vcxproj", "{3EA53C5A-FDF6-4E83-9B29-B99E3FB2114D}"
1415
ProjectSection(ProjectDependencies) = postProject
15-
{E21456E4-6518-425E-A6E8-D7D7E247FD0C} = {E21456E4-6518-425E-A6E8-D7D7E247FD0C}
16+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90} = {9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}
1617
EndProjectSection
1718
EndProject
19+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libipc", "libipc\libipc.vcxproj", "{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}"
20+
EndProject
1821
Global
1922
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2023
Debug|x64 = Debug|x64
@@ -47,6 +50,14 @@ Global
4750
{3EA53C5A-FDF6-4E83-9B29-B99E3FB2114D}.Release|x64.Build.0 = Release|x64
4851
{3EA53C5A-FDF6-4E83-9B29-B99E3FB2114D}.Release|x86.ActiveCfg = Release|Win32
4952
{3EA53C5A-FDF6-4E83-9B29-B99E3FB2114D}.Release|x86.Build.0 = Release|Win32
53+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Debug|x64.ActiveCfg = Debug|x64
54+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Debug|x64.Build.0 = Debug|x64
55+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Debug|x86.ActiveCfg = Debug|Win32
56+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Debug|x86.Build.0 = Debug|Win32
57+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Release|x64.ActiveCfg = Release|x64
58+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Release|x64.Build.0 = Release|x64
59+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Release|x86.ActiveCfg = Release|Win32
60+
{9D1343D4-1D7D-4EEA-933C-E2E3D56E8D90}.Release|x86.Build.0 = Release|Win32
5061
EndGlobalSection
5162
GlobalSection(SolutionProperties) = preSolution
5263
HideSolutionNode = FALSE

cppcryptfs/cppcryptfs.cpp

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ THE SOFTWARE.
3232
//
3333

3434
#include "stdafx.h"
35+
#include <iostream>
3536
#include "cppcryptfs.h"
3637
#include "crypt/cryptdefs.h"
3738
#include "ui/CryptPropertySheet.h"
@@ -47,8 +48,8 @@ THE SOFTWARE.
4748
#include "util/util.h"
4849
#include "crypt/crypt.h"
4950
#include "ui/uiutil.h"
50-
#include "namedpipe/server.h"
51-
#include "namedpipe/client.h"
51+
#include "../libipc/server.h"
52+
#include "../libipc/client.h"
5253

5354

5455
#ifdef _DEBUG
@@ -90,49 +91,23 @@ CcppcryptfsApp::CcppcryptfsApp()
9091
CcppcryptfsApp theApp;
9192

9293

93-
static DWORD WINAPI ServerThreadProc(PVOID lpvParam)
94+
95+
96+
static void NamedPipeServerCallback(void* ctx, HANDLE hPipe)
9497
{
95-
//_tprintf(TEXT("\nPipe Server: Main thread awaiting client connection on %s\n"), lpszPipename);
96-
97-
98-
while (true) {
99-
// Wait for the client to connect; if it succeeds,
100-
// the function returns a nonzero value. If the function
101-
// returns zero, GetLastError returns ERROR_PIPE_CONNECTED.
102-
103-
auto hPipe = CreateNamedPipe(
104-
CMD_NAMED_PIPE, // pipe name
105-
PIPE_ACCESS_DUPLEX, // read/write access
106-
PIPE_TYPE_MESSAGE | // message type pipe
107-
PIPE_READMODE_MESSAGE | // message-read mode
108-
PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, // blocking mode, local only
109-
PIPE_UNLIMITED_INSTANCES, // max. instances
110-
CMD_NAMED_PIPE_BUFSIZE, // output buffer size
111-
CMD_NAMED_PIPE_BUFSIZE, // input buffer size
112-
0, // client time-out
113-
NULL); // default security attribute
114-
115-
if (hPipe == INVALID_HANDLE_VALUE)
116-
return 1;
117-
118-
auto fConnected = ConnectNamedPipe(hPipe, NULL) ?
119-
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
120-
121-
if (fConnected) {
122-
theApp.SendCmdArgsToSelf(hPipe);
123-
} else {
124-
// The client could not connect, so close the pipe.
125-
CloseHandle(hPipe);
126-
}
127-
}
98+
auto pApp = reinterpret_cast<CcppcryptfsApp*>(ctx);
12899

129-
return 0;
100+
pApp->SendCmdArgsToSelf(hPipe);
130101
}
131102

132-
133-
bool StartNamedPipeServer()
103+
static bool StartNamedPipeServer()
134104
{
135-
auto hThread = CreateThread(NULL, 0, ServerThreadProc, NULL, 0, NULL);
105+
static NamedPipeServerContext ctx;
106+
107+
ctx.context = &theApp;
108+
ctx.callback = NamedPipeServerCallback;
109+
110+
auto hThread = CreateThread(NULL, 0, NamedPipeServerThreadProc, &ctx, 0, NULL);
136111

137112
if (hThread != NULL)
138113
CloseHandle(hThread);
@@ -169,7 +144,38 @@ BOOL CcppcryptfsApp::InitInstance()
169144

170145
if (hWnd) {
171146
if (have_args()) {
172-
::MessageBox(NULL, L"use cppcryptfsctl.exe to send commands to a running cppcryptfs", L"cppcryptfs", MB_OK | MB_ICONERROR);
147+
148+
bool have_console = OpenConsole(0);
149+
150+
wstring err_mes;
151+
wstring result;
152+
153+
if (!SendArgsToRunningInstance(GetCommandLine(), result)) {
154+
err_mes = L"cppcryptfsctl: Unable to send command. Is cppcryptfs really already running?\n";
155+
} else {
156+
if (result.length() >= CMD_PIPE_RESPONSE_LENGTH) {
157+
if (wcsncmp(result.c_str(), CMD_PIPE_SUCCESS_STR, CMD_PIPE_RESPONSE_LENGTH) == 0) {
158+
if (have_console)
159+
wcout << wstring(result.c_str() + CMD_PIPE_RESPONSE_LENGTH);
160+
} else {
161+
err_mes = wstring(result.c_str() + CMD_PIPE_RESPONSE_LENGTH);
162+
}
163+
} else {
164+
err_mes = L"cppcryptfs: got a mal-formed response from running instance of cppcryptfs\n";
165+
}
166+
}
167+
168+
if (err_mes.length() > 0) {
169+
if (have_console) {
170+
wcerr << err_mes;
171+
} else {
172+
::MessageBox(NULL, err_mes.c_str(), L"cppcryptfs", MB_ICONERROR | MB_OK);
173+
}
174+
}
175+
176+
if (have_console)
177+
CloseConsole();
178+
173179
} else { // if no args, then restore window of running instance
174180
ShowWindow(hWnd, SW_SHOWNORMAL);
175181
}

cppcryptfs/cppcryptfs.vcxproj

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<PlatformToolset>v142</PlatformToolset>
3232
<CharacterSet>Unicode</CharacterSet>
3333
<UseOfMfc>Static</UseOfMfc>
34+
<SpectreMitigation>Spectre</SpectreMitigation>
3435
</PropertyGroup>
3536
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3637
<ConfigurationType>Application</ConfigurationType>
@@ -39,13 +40,15 @@
3940
<WholeProgramOptimization>true</WholeProgramOptimization>
4041
<CharacterSet>Unicode</CharacterSet>
4142
<UseOfMfc>Static</UseOfMfc>
43+
<SpectreMitigation>Spectre</SpectreMitigation>
4244
</PropertyGroup>
4345
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4446
<ConfigurationType>Application</ConfigurationType>
4547
<UseDebugLibraries>true</UseDebugLibraries>
4648
<PlatformToolset>v142</PlatformToolset>
4749
<CharacterSet>Unicode</CharacterSet>
4850
<UseOfMfc>Static</UseOfMfc>
51+
<SpectreMitigation>Spectre</SpectreMitigation>
4952
</PropertyGroup>
5053
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
5154
<ConfigurationType>Application</ConfigurationType>
@@ -54,6 +57,7 @@
5457
<WholeProgramOptimization>true</WholeProgramOptimization>
5558
<CharacterSet>Unicode</CharacterSet>
5659
<UseOfMfc>Static</UseOfMfc>
60+
<SpectreMitigation>Spectre</SpectreMitigation>
5761
</PropertyGroup>
5862
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
5963
<ImportGroup Label="ExtensionSettings">
@@ -130,7 +134,7 @@
130134
<Link>
131135
<SubSystem>Windows</SubSystem>
132136
<GenerateDebugInformation>true</GenerateDebugInformation>
133-
<AdditionalDependencies>$(OutputPath)libcppcryptfs.lib;C:\Program Files\Dokan\Dokan Library-1.3.1\lib\dokan1.lib;version.lib;C:\Program Files\OpenSSL\lib\libcrypto.lib;Shlwapi.lib;Crypt32.lib;</AdditionalDependencies>
137+
<AdditionalDependencies>$(OutputPath)libipc.lib;$(OutputPath)libcppcryptfs.lib;C:\Program Files\Dokan\Dokan Library-1.3.1\lib\dokan1.lib;version.lib;C:\Program Files\OpenSSL\lib\libcrypto.lib;Shlwapi.lib;Crypt32.lib;</AdditionalDependencies>
134138
<AdditionalOptions>/ignore:4099 %(AdditionalOptions)</AdditionalOptions>
135139
<UACExecutionLevel>AsInvoker</UACExecutionLevel>
136140
</Link>
@@ -220,12 +224,10 @@
220224
<ClInclude Include="dokan\CryptThreadData.h" />
221225
<ClInclude Include="dokan\FileNameEnc.h" />
222226
<ClInclude Include="dokan\MountPointManager.h" />
223-
<ClInclude Include="namedpipe\client.h" />
224227
<ClInclude Include="namedpipe\server.h" />
225228
<ClInclude Include="Resource.h" />
226229
<ClInclude Include="stdafx.h" />
227230
<ClInclude Include="targetver.h" />
228-
<ClInclude Include="ui\certutil.h" />
229231
<ClInclude Include="ui\CreatePropertyPage.h" />
230232
<ClInclude Include="ui\CryptAboutPropertyPage.h" />
231233
<ClInclude Include="ui\cryptdefaults.h" />
@@ -268,25 +270,12 @@
268270
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
269271
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
270272
</ClCompile>
271-
<ClCompile Include="namedpipe\client.cpp">
272-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
273-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
274-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
275-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
276-
</ClCompile>
277-
<ClCompile Include="namedpipe\server.cpp">
278-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
279-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
280-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
281-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
282-
</ClCompile>
283273
<ClCompile Include="stdafx.cpp">
284274
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
285275
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
286276
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
287277
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
288278
</ClCompile>
289-
<ClCompile Include="ui\certutil.cpp" />
290279
<ClCompile Include="ui\CreatePropertyPage.cpp" />
291280
<ClCompile Include="ui\CryptAboutPropertyPage.cpp" />
292281
<ClCompile Include="ui\CryptPropertyPage.cpp" />

cppcryptfs/namedpipe/server.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

cppcryptfs/ui/CryptPropertySheet.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ THE SOFTWARE.
3232
#include "stdafx.h"
3333
#include <Dbt.h>
3434
#include "cppcryptfs.h"
35-
#include "namedpipe/server.h"
35+
#include "../libipc/server.h"
3636
#include "CryptPropertySheet.h"
3737
#include "CryptPropertyPage.h"
3838
#include "dokan/cryptdokan.h"
@@ -229,11 +229,6 @@ INT_PTR CCryptPropertySheet::DoModal()
229229

230230
}
231231

232-
static bool ReadCommandLineFromPipe(HANDLE hPipe, wstring& cmdLine)
233-
{
234-
return ReadFromNamedPipe(hPipe, cmdLine) > 0;
235-
}
236-
237232
BOOL CCryptPropertySheet::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
238233
{
239234
// TODO: Add your message handler code here and/or call default
@@ -258,9 +253,13 @@ BOOL CCryptPropertySheet::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct
258253

259254
if (page) {
260255

261-
wstring cmdLine;
262-
if (ReadCommandLineFromPipe(hPipe, cmdLine)) {
263-
page->ProcessCommandLine(cmdLine.c_str(), FALSE, hPipe);
256+
LockZeroBuffer<WCHAR> cmdLine(4096);
257+
if (!cmdLine.IsLocked()) {
258+
MessageBox(L"unable to lock command line buffer", L"cppcryptfs", MB_ICONERROR | MB_OK);
259+
return FALSE;
260+
}
261+
if (ReadFromNamedPipe(hPipe, cmdLine.m_buf, cmdLine.m_len)) {
262+
page->ProcessCommandLine(cmdLine.m_buf, FALSE, hPipe);
264263
return TRUE;
265264
} else {
266265
ConsoleErrMesPipe(L"unable to read command line", hPipe);

0 commit comments

Comments
 (0)