summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins/src/driverlogic/context.cpp
blob: a01fd29bac72065a2dc512f9b04a5568abbea50b (plain)
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
#include "stdafx.h"
#include "context.h"
#include <libcommon/string.h>
#include <libcommon/wmi/connection.h>
#include <libcommon/wmi/resultset.h>
#include <libcommon/wmi/wmi.h>
#include <log/log.h>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <memory>
#include <sstream>

using namespace common;

namespace
{

std::vector<std::wstring> BlockToRows(const std::wstring &textBlock)
{
	//
	// This is such a hack :-(
	//
	// It only works because the tokenizer is greedy and because we don't care about
	// empty lines for this usage.
	//
	return common::string::Tokenize(textBlock, L"\r\n");
}

void LogAllAdapters(wmi::Connection &connection)
{
	auto resultset = connection.query(L"SELECT * from Win32_NetworkAdapter");

	struct NetworkAdapter
	{
		size_t interfaceIndex;
		std::wstring manufacturer;
		std::wstring name;
		std::wstring pnpDeviceId;
		std::wstring alias;
	};

	std::vector<NetworkAdapter> adapters;

	//
	// Find all adapters and extract the most important data.
	//

	auto StringOrNa = [](const _variant_t &variant)
	{
		if (VT_BSTR == V_VT(&variant))
		{
			return std::wstring(V_BSTR(&variant));
		}

		return std::wstring(L"n/a");
	};

	while(resultset.advance())
	{
		auto interfaceIndex = wmi::WmiGetPropertyAlways(resultset.result(), L"InterfaceIndex");
		auto manufacturer = wmi::WmiGetProperty(resultset.result(), L"Manufacturer");
		auto name = wmi::WmiGetProperty(resultset.result(), L"Name");
		auto pnpDeviceId = wmi::WmiGetProperty(resultset.result(), L"PNPDeviceID");
		auto alias = wmi::WmiGetProperty(resultset.result(), L"NetConnectionID");

		NetworkAdapter adapter;

		adapter.interfaceIndex = static_cast<size_t>(V_UI8(&interfaceIndex));
		adapter.manufacturer = StringOrNa(manufacturer);
		adapter.name = StringOrNa(name);
		adapter.pnpDeviceId = StringOrNa(pnpDeviceId);
		adapter.alias = StringOrNa(alias);

		adapters.emplace_back(adapter);
	}

	//
	// Flatten the adapter information so we can log it more easily.
	//

	std::vector<std::wstring> details;

	for (const auto &adapter : adapters)
	{
		details.emplace_back(L"Adapter");

		{
			std::wstringstream ss;

			ss << L"    InterfaceIndex: " << adapter.interfaceIndex;

			details.emplace_back(ss.str());
		}

		details.emplace_back(std::wstring(L"    Manufacturer: ").append(adapter.manufacturer));
		details.emplace_back(std::wstring(L"    Name: ").append(adapter.name));
		details.emplace_back(std::wstring(L"    PnpDeviceId: ").append(adapter.pnpDeviceId));
		details.emplace_back(std::wstring(L"    Alias: ").append(adapter.alias));
	}

	PluginLogWithDetails(L"Adapters known to WMI", details);
}

std::wstring DoubleBackslashes(const std::wstring &str)
{
	auto result(str);

	size_t offset = 0;

	for (size_t index = 0; index < str.size(); ++index)
	{
		if (L'\\' == str[index])
		{
			result.insert(index + offset, 1, L'\\');
			++offset;
		}
	}

	return result;
}

} // anonymous namespace

Context::Context()
	: m_connection(wmi::Connection::Namespace::Cimv2)
{
}

Context::BaselineStatus Context::establishBaseline(const std::wstring &textBlock)
{
	m_baseline = ParseVirtualNics(textBlock);

	if (m_baseline.empty())
	{
		return BaselineStatus::NO_INTERFACES_PRESENT;
	}

	for (const auto &nic : m_baseline)
	{
		if (0 == _wcsicmp(nic.alias.c_str(), L"mullvad"))
		{
			return BaselineStatus::MULLVAD_INTERFACE_PRESENT;
		}
	}

	return BaselineStatus::SOME_INTERFACES_PRESENT;
}

void Context::recordCurrentState(const std::wstring &textBlock)
{
	m_currentState = ParseVirtualNics(textBlock);
}

Context::VirtualNic Context::getNewAdapter()
{
	std::vector<VirtualNic> added;

	for (const auto &nic : m_currentState)
	{
		if (m_baseline.end() == m_baseline.find(nic))
		{
			added.push_back(nic);
		}
	}

	if (added.size() != 1)
	{
		throw std::runtime_error("Unable to identify newly added virtual adapter");
	}

	return added[0];
}

std::set<Context::VirtualNic> Context::ParseVirtualNics(const std::wstring &textBlock)
{
	// ROOT\NET\0000
	//     Name: TAP - Windows Adapter V9
	//     Hardware IDs :
	//         tap0901
	// 1 matching device(s) found.

	std::set<VirtualNic> nics;

	auto text = BlockToRows(textBlock);

	size_t line = 0;

	while (nullptr != wcschr(text.at(line).c_str(), L'\\'))
	{
		auto nameDelimiter = wcschr(text.at(line + 1).c_str(), L':');

		if (nullptr == nameDelimiter)
		{
			throw std::runtime_error("Unexpected formatting in input data");
		}

		VirtualNic nic;

		nic.node = text.at(line);
		nic.name = std::wstring(nameDelimiter + 2);
		nic.alias = GetNicAlias(nic.node, nic.name);

		nics.emplace(std::move(nic));
		line += 4;
	}

	return nics;
}

std::wstring Context::GetNicAlias(const std::wstring &node, const std::wstring &name)
{
	//
	// The name cannot be used when querying WMI, because WMI sometimes normalizes the
	// names in its dataset, thereby destroying their uniqueness.
	//
	// E.g. if a network interface has a name of "TAP-Windows Adapter V9 #2" it will
	// sometimes be reported by WMI as "TAP-Windows Adapter V9".
	//
	// Also, the node string cannot be used as-is. We have to double the backslashes in it
	// or the string will be rejected by WMI.
	//

	const auto formattedNode = DoubleBackslashes(node);

	std::wstringstream ss;

	ss << L"SELECT * FROM Win32_NetworkAdapter WHERE PNPDeviceID = \"" << formattedNode << L"\"";

	auto resultset = m_connection.query(ss.str().c_str());

	if (false == resultset.advance())
	{
		PluginLog(std::wstring(L"WMI query failed for adapter: ").append(name));
		LogAllAdapters(m_connection);

		throw std::runtime_error("Unable to look up virtual adapter using WMI");
	}

	auto alias = wmi::WmiGetPropertyAlways(resultset.result(), L"NetConnectionID");

	return V_BSTR(&alias);
}