forked from irwir/eMule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirewallOpener.cpp
294 lines (267 loc) · 9.59 KB
/
FirewallOpener.cpp
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
//this file is part of eMule
//Copyright (C)2002-2008 Merkur ( strEmail.Format("%s@%s", "devteam", "emule-project.net") / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
// class to configure the ICS-Firewall of Windows XP - will not work with WinXP-SP2 yet
#include "StdAfx.h"
#include "firewallopener.h"
#include "emule.h"
#include "preferences.h"
#include "otherfunctions.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define RETURN_ON_FAIL(x) if (!SUCCEEDED(x)) return false;
CFirewallOpener::CFirewallOpener(void)
{
m_bInited = false;
m_pINetSM = NULL;
}
CFirewallOpener::~CFirewallOpener(void)
{
UnInit();
}
bool CFirewallOpener::Init(bool bPreInit)
{
if (!m_bInited){
ASSERT ( m_liAddedRules.IsEmpty() );
if (thePrefs.GetWindowsVersion() != _WINVER_XP_ || !SUCCEEDED(CoInitialize(NULL)))
return false;
HRESULT hr = CoInitializeSecurity (NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (!SUCCEEDED(hr) || !SUCCEEDED(::CoCreateInstance (__uuidof(NetSharingManager), NULL, CLSCTX_ALL, __uuidof(INetSharingManager), (void**)&m_pINetSM)) ){
CoUninitialize();
return false;
}
}
m_bInited = true;
if (bPreInit){
// will return here in order to not create an instance when not really needed
// preinit is only used to call CoInitializeSecurity before its too late for that (aka something else called it)
// will have to look deeper into this issue in order to find a nicer way if possible
return true;
}
if (m_pINetSM == NULL){
if (!SUCCEEDED(::CoCreateInstance (__uuidof(NetSharingManager), NULL, CLSCTX_ALL, __uuidof(INetSharingManager), (void**)&m_pINetSM)) ){
UnInit();
return false;
}
}
return true;
}
void CFirewallOpener::UnInit(){
if (!m_bInited)
return;
for (int i = 0; i != m_liAddedRules.GetCount(); i++){
if (m_liAddedRules[i].m_bRemoveOnExit)
RemoveRule(m_liAddedRules[i]);
}
m_liAddedRules.RemoveAll();
m_bInited = false;
if (m_pINetSM != NULL){
m_pINetSM->Release();
m_pINetSM = NULL;
}
else
ASSERT ( false );
CoUninitialize();
}
bool CFirewallOpener::DoAction(const EFOCAction eAction, const CICSRuleInfo& riPortRule){
if ( !Init() )
return false;
//TODO lets see if we can find a reliable method to find out the internet standard connection set by the user
bool bSuccess = true;
bool bPartialSucceeded = false;
bool bFoundAtLeastOneConn = false;
INetSharingEveryConnectionCollectionPtr NSECCP;
IEnumVARIANTPtr varEnum;
IUnknownPtr pUnk;
RETURN_ON_FAIL(m_pINetSM->get_EnumEveryConnection(&NSECCP));
RETURN_ON_FAIL(NSECCP->get__NewEnum(&pUnk));
RETURN_ON_FAIL(pUnk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&varEnum));
_variant_t var;
while (S_OK == varEnum->Next(1, &var, NULL)) {
INetConnectionPtr NCP;
if (V_VT(&var) == VT_UNKNOWN
&& SUCCEEDED(V_UNKNOWN(&var)->QueryInterface(__uuidof(INetConnection),(void**)&NCP)))
{
INetConnectionPropsPtr pNCP;
if ( !SUCCEEDED(m_pINetSM->get_NetConnectionProps (NCP, &pNCP)) )
continue;
DWORD dwCharacteristics = 0;
pNCP->get_Characteristics(&dwCharacteristics);
if (dwCharacteristics & (NCCF_FIREWALLED)) {
NETCON_MEDIATYPE MediaType = NCM_NONE;
pNCP->get_MediaType (&MediaType);
if ((MediaType != NCM_SHAREDACCESSHOST_LAN) && (MediaType != NCM_SHAREDACCESSHOST_RAS) ){
INetSharingConfigurationPtr pNSC;
if ( !SUCCEEDED(m_pINetSM->get_INetSharingConfigurationForINetConnection (NCP, &pNSC)) )
continue;
VARIANT_BOOL varbool = VARIANT_FALSE;
pNSC->get_InternetFirewallEnabled(&varbool);
if (varbool == VARIANT_FALSE)
continue;
bFoundAtLeastOneConn = true;
switch(eAction){
case FOC_ADDRULE:{
bool bResult;
// we do not want to overwrite an existing rule
if (FindRule(FOC_FINDRULEBYPORT, riPortRule, pNSC, NULL)){
bResult = true;
}
else
bResult = AddRule(riPortRule, pNSC, pNCP);
bSuccess = bSuccess && bResult;
if (bResult && !bPartialSucceeded)
m_liAddedRules.Add(riPortRule); // keep track of added rules
bPartialSucceeded = bPartialSucceeded || bResult;
break;
}
case FOC_FWCONNECTIONEXISTS:
return true;
case FOC_DELETERULEBYNAME:
case FOC_DELETERULEEXCACT:
bSuccess = bSuccess && FindRule(eAction, riPortRule, pNSC, NULL);
break;
case FOC_FINDRULEBYNAME:
if (FindRule(FOC_FINDRULEBYNAME, riPortRule, pNSC, NULL))
return true;
else
bSuccess = false;
break;
case FOC_FINDRULEBYPORT:
if (FindRule(FOC_FINDRULEBYPORT, riPortRule, pNSC, NULL))
return true;
else
bSuccess = false;
break;
default:
ASSERT ( false );
}
}
}
}
var.Clear();
}
return bSuccess && bFoundAtLeastOneConn;
}
bool CFirewallOpener::AddRule(const CICSRuleInfo& riPortRule, const INetSharingConfigurationPtr pNSC, const INetConnectionPropsPtr pNCP){
INetSharingPortMappingPtr pNSPM;
HRESULT hr = pNSC->AddPortMapping(riPortRule.m_strRuleName.AllocSysString(),
riPortRule.m_byProtocol,
riPortRule.m_nPortNumber,
riPortRule.m_nPortNumber,
0,
CComBSTR(L"127.0.0.1"),
ICSTT_IPADDRESS,
&pNSPM);
CComBSTR bstrName;
pNCP->get_Name(&bstrName);
if ( SUCCEEDED(hr) && SUCCEEDED(pNSPM->Enable())){
theApp.QueueDebugLogLine(false, _T("Succeeded to add Rule '%s' for Port '%u' on Connection '%s'"),riPortRule.m_strRuleName, riPortRule.m_nPortNumber, CString(bstrName));
return true;
}
else{
theApp.QueueDebugLogLine(false, _T("Failed to add Rule '%s' for Port '%u' on Connection '%s'"),riPortRule.m_strRuleName, riPortRule.m_nPortNumber, CString(bstrName));
return false;
}
}
bool CFirewallOpener::FindRule(const EFOCAction eAction, const CICSRuleInfo& riPortRule, const INetSharingConfigurationPtr pNSC, INetSharingPortMappingPropsPtr* outNSPMP){
INetSharingPortMappingCollectionPtr pNSPMC;
RETURN_ON_FAIL(pNSC->get_EnumPortMappings (ICSSC_DEFAULT, &pNSPMC));
INetSharingPortMappingPtr pNSPM;
IEnumVARIANTPtr varEnum;
IUnknownPtr pUnk;
RETURN_ON_FAIL(pNSPMC->get__NewEnum(&pUnk));
RETURN_ON_FAIL(pUnk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&varEnum));
_variant_t var;
while (S_OK == varEnum->Next(1, &var, NULL)) {
INetSharingPortMappingPropsPtr pNSPMP;
if (V_VT(&var) == VT_DISPATCH
&& SUCCEEDED(V_DISPATCH(&var)->QueryInterface(__uuidof(INetSharingPortMapping),(void**)&pNSPM))
&& SUCCEEDED(pNSPM->get_Properties (&pNSPMP)))
{
UCHAR ucProt = 0;
long uExternal = 0;
CComBSTR bstrName;
pNSPMP->get_IPProtocol (&ucProt);
pNSPMP->get_ExternalPort (&uExternal);
pNSPMP->get_Name(&bstrName);
switch(eAction){
case FOC_FINDRULEBYPORT:
if (riPortRule.m_nPortNumber == uExternal && riPortRule.m_byProtocol == ucProt){
if (outNSPMP != NULL)
*outNSPMP = pNSPM;
return true;
}
break;
case FOC_FINDRULEBYNAME:
if (riPortRule.m_strRuleName == CString(bstrName)){
if (outNSPMP != NULL)
*outNSPMP = pNSPM;
return true;
}
break;
case FOC_DELETERULEEXCACT:
if (riPortRule.m_strRuleName == CString(bstrName)
&& riPortRule.m_nPortNumber == uExternal && riPortRule.m_byProtocol == ucProt)
{
RETURN_ON_FAIL(pNSC->RemovePortMapping(pNSPM));
theApp.QueueDebugLogLine(false,_T("Rule removed"));
}
break;
case FOC_DELETERULEBYNAME:
if (riPortRule.m_strRuleName == CString(bstrName)){
RETURN_ON_FAIL(pNSC->RemovePortMapping(pNSPM));
theApp.QueueDebugLogLine(false,_T("Rule removed"));
}
break;
default:
ASSERT( false );
}
}
var.Clear();
}
switch(eAction){
case FOC_DELETERULEBYNAME:
case FOC_DELETERULEEXCACT:
return true;
case FOC_FINDRULEBYPORT:
case FOC_FINDRULEBYNAME:
default:
return false;
}
}
bool CFirewallOpener::RemoveRule(const CString strName){
return DoAction(FOC_DELETERULEBYNAME, CICSRuleInfo(0, 0, strName) );
}
bool CFirewallOpener::RemoveRule(const CICSRuleInfo& riPortRule){
return DoAction(FOC_DELETERULEEXCACT, riPortRule);
}
bool CFirewallOpener::DoesRuleExist(const CString strName){
return DoAction(FOC_FINDRULEBYNAME, CICSRuleInfo(0, 0, strName) );
}
bool CFirewallOpener::DoesRuleExist(const uint16 nPortNumber,const uint8 byProtocol){
return DoAction(FOC_FINDRULEBYPORT, CICSRuleInfo(nPortNumber, byProtocol, _T("")) );
}
bool CFirewallOpener::OpenPort(const uint16 nPortNumber,const uint8 byProtocol,const CString strRuleName, const bool bRemoveOnExit){
return DoAction(FOC_ADDRULE, CICSRuleInfo(nPortNumber, byProtocol, strRuleName, bRemoveOnExit));
}
bool CFirewallOpener::OpenPort(const CICSRuleInfo& riPortRule){
return DoAction(FOC_ADDRULE, riPortRule);
}
bool CFirewallOpener::DoesFWConnectionExist(){
return DoAction(FOC_FWCONNECTIONEXISTS, CICSRuleInfo());
}