Module: check_mk
Branch: master
Commit: fe34b546c598371e95dcd253304813b8ce42aa96
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fe34b546c59837…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Fri Feb 2 16:35:29 2018 +0100
Windows agent: fix unpacking of binary plugins
Broken by #5700 in master.
---
agents/windows/WritableFile.cc | 9 +++++++++
agents/windows/WritableFile.h | 8 ++++++++
agents/windows/build_version | 2 +-
agents/windows/check_mk_agent.cc | 21 +++++++++++++--------
4 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/agents/windows/WritableFile.cc b/agents/windows/WritableFile.cc
index e8aaded..43bb7d4 100644
--- a/agents/windows/WritableFile.cc
+++ b/agents/windows/WritableFile.cc
@@ -59,6 +59,15 @@ WritableFile &WritableFile::operator<<(const std::string
&s) {
return *this;
}
+WritableFile &WritableFile::operator<<(const std::vector<BYTE> &s) {
+ DWORD written = 0;
+ if (!_winapi.WriteFile(_hFile.get(), s.data(), s.size(), &written,
+ nullptr)) {
+ throw FileError(_path, get_win_error_as_string(_winapi));
+ }
+ return *this;
+}
+
std::unordered_set<std::string> getDefaultWhitelist(
const Environment &env, const WinApiAdaptor &winapi) {
std::unordered_set<std::string> whitelist = {
diff --git a/agents/windows/WritableFile.h b/agents/windows/WritableFile.h
index 48dca6b..b251b8b 100644
--- a/agents/windows/WritableFile.h
+++ b/agents/windows/WritableFile.h
@@ -68,6 +68,14 @@ public:
*/
WritableFile &operator<<(const std::string &s);
+ /**
+ * Write given byte sequence to file.
+ *
+ * @param[in] s Byte sequence to be written
+ * @throw FileError If the write operation fails
+ */
+ WritableFile &operator<<(const std::vector<BYTE> &s);
+
private:
const std::string _path;
WrappedHandle<InvalidHandleTraits> _hFile;
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 2aeb20d..09dcc41 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3086
+3090
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index 220cd6f..cf27f1b 100644
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -110,7 +110,6 @@ using std::vector;
// | Declarations of macros, structs and function prototypes |
// '----------------------------------------------------------------------'
-
static const char RT_PROTOCOL_VERSION[2] = {'0', '0'};
#define SERVICE_NAME "Check_MK_Agent"
@@ -843,7 +842,7 @@ std::string managePluginPath(const std::string &filePath,
}
template <typename LengthT>
-std::vector<BYTE> readData(std::ifstream &ifs,
+std::vector<BYTE> readData(std::ifstream &ifs, bool zeroTerminate,
const std::function<void(LengthT)> &check =
[](LengthT) {}) {
LengthT length = 0;
@@ -852,22 +851,28 @@ std::vector<BYTE> readData(std::ifstream &ifs,
return {};
}
check(length);
- std::vector<BYTE> dataBuffer;
- dataBuffer.reserve(static_cast<size_t>(length + 1));
+ size_t count = length;
+ if (zeroTerminate) {
+ count += 1;
+ }
+ std::vector<BYTE> dataBuffer(count, 0);
ifs.read(reinterpret_cast<char *>(dataBuffer.data()), length);
if (!ifs.good()) {
throw UnpackError(integrityErrorMsg);
}
- dataBuffer[length] = '\0';
+ if (zeroTerminate) {
+ dataBuffer[length] = '\0';
+ }
+
return dataBuffer;
}
void extractPlugin(const Environment &env, std::ifstream &ifs,
WritableFile &uninstallFile) {
// Read Filename
- const auto filepath = readData<BYTE>(ifs);
+ const auto filepath = readData<BYTE>(ifs, true);
if (!ifs.good()) {
if (ifs.eof()) {
@@ -884,7 +889,7 @@ void extractPlugin(const Environment &env, std::ifstream
&ifs,
"' exceeds 20 MB");
}
};
- const auto content = readData<int>(ifs, checkPluginSize);
+ const auto content = readData<int>(ifs, false, checkPluginSize);
if (!ifs.good()) {
throw UnpackError(integrityErrorMsg);
}
@@ -895,7 +900,7 @@ void extractPlugin(const Environment &env, std::ifstream
&ifs,
// Write plugin
WritableFile pluginFile(pluginPath, 0, CREATE_NEW, s_winapi);
- pluginFile << reinterpret_cast<char const *>(content.data());
+ pluginFile << content;
}
} // namespace