| 1 |
// Copyright (c) 2015 Ryan Prichard |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
// of this software and associated documentation files (the "Software"), to |
| 5 |
// deal in the Software without restriction, including without limitation the |
| 6 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 8 |
// furnished to do so, subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in |
| 11 |
// all copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 |
// IN THE SOFTWARE. |
| 20 |
|
| 21 |
#include <cstdio> |
| 22 |
#include <cstdlib> |
| 23 |
|
| 24 |
#include <windows.h> |
| 25 |
|
| 26 |
#include "../shared/WindowsSecurity.h" |
| 27 |
#include "../shared/WinptyException.h" |
| 28 |
|
| 29 |
const wchar_t *kPipeName = L"\\\\.\\pipe\\DebugServer"; |
| 30 |
|
| 31 |
// A message may not be larger than this size. |
| 32 |
const int MSG_SIZE = 4096; |
| 33 |
|
| 34 |
static void usage(const char *program, int code) { |
| 35 |
printf("Usage: %s [--everyone]\n" |
| 36 |
"\n" |
| 37 |
"Creates the named pipe %ls and reads messages. Prints each\n" |
| 38 |
"message to stdout. By default, only the current user can send messages.\n" |
| 39 |
"Pass --everyone to let anyone send a message.\n" |
| 40 |
"\n" |
| 41 |
"Use the WINPTY_DEBUG environment variable to enable winpty trace output.\n" |
| 42 |
"(e.g. WINPTY_DEBUG=trace for the default trace output.) Set WINPTYDBG=1\n" |
| 43 |
"to enable trace with older winpty versions.\n", |
| 44 |
program, kPipeName); |
| 45 |
exit(code); |
| 46 |
} |
| 47 |
|
| 48 |
int main(int argc, char *argv[]) { |
| 49 |
bool everyone = false; |
| 50 |
for (int i = 1; i < argc; ++i) { |
| 51 |
std::string arg = argv[i]; |
| 52 |
if (arg == "--everyone") { |
| 53 |
everyone = true; |
| 54 |
} else if (arg == "-h" || arg == "--help") { |
| 55 |
usage(argv[0], 0); |
| 56 |
} else { |
| 57 |
usage(argv[0], 1); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
SecurityDescriptor sd; |
| 62 |
PSECURITY_ATTRIBUTES psa = nullptr; |
| 63 |
SECURITY_ATTRIBUTES sa = {}; |
| 64 |
if (everyone) { |
| 65 |
try { |
| 66 |
sd = createPipeSecurityDescriptorOwnerFullControlEveryoneWrite(); |
| 67 |
} catch (const WinptyException &e) { |
| 68 |
fprintf(stderr, |
| 69 |
"error creating security descriptor: %ls\n", e.what()); |
| 70 |
exit(1); |
| 71 |
} |
| 72 |
sa.nLength = sizeof(sa); |
| 73 |
sa.lpSecurityDescriptor = sd.get(); |
| 74 |
psa = &sa; |
| 75 |
} |
| 76 |
|
| 77 |
HANDLE serverPipe = CreateNamedPipeW( |
| 78 |
kPipeName, |
| 79 |
/*dwOpenMode=*/PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, |
| 80 |
/*dwPipeMode=*/PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | |
| 81 |
rejectRemoteClientsPipeFlag(), |
| 82 |
/*nMaxInstances=*/1, |
| 83 |
/*nOutBufferSize=*/MSG_SIZE, |
| 84 |
/*nInBufferSize=*/MSG_SIZE, |
| 85 |
/*nDefaultTimeOut=*/10 * 1000, |
| 86 |
psa); |
| 87 |
|
| 88 |
if (serverPipe == INVALID_HANDLE_VALUE) { |
| 89 |
fprintf(stderr, "error: could not create %ls pipe: error %u\n", |
| 90 |
kPipeName, static_cast<unsigned>(GetLastError())); |
| 91 |
exit(1); |
| 92 |
} |
| 93 |
|
| 94 |
char msgBuffer[MSG_SIZE + 1]; |
| 95 |
|
| 96 |
while (true) { |
| 97 |
if (!ConnectNamedPipe(serverPipe, nullptr)) { |
| 98 |
fprintf(stderr, "error: ConnectNamedPipe failed\n"); |
| 99 |
fflush(stderr); |
| 100 |
exit(1); |
| 101 |
} |
| 102 |
DWORD bytesRead = 0; |
| 103 |
if (!ReadFile(serverPipe, msgBuffer, MSG_SIZE, &bytesRead, nullptr)) { |
| 104 |
fprintf(stderr, "error: ReadFile on pipe failed\n"); |
| 105 |
fflush(stderr); |
| 106 |
DisconnectNamedPipe(serverPipe); |
| 107 |
continue; |
| 108 |
} |
| 109 |
msgBuffer[bytesRead] = '\n'; |
| 110 |
fwrite(msgBuffer, 1, bytesRead + 1, stdout); |
| 111 |
fflush(stdout); |
| 112 |
|
| 113 |
DWORD bytesWritten = 0; |
| 114 |
WriteFile(serverPipe, "OK", 2, &bytesWritten, nullptr); |
| 115 |
DisconnectNamedPipe(serverPipe); |
| 116 |
} |
| 117 |
} |