add FIREWALL_EXCEPTION_ATTRIBUTES struct
chris_bednarski committed
Aug 26, 2023 at 17:31 UTC
409d3b63bff29df0859a217ba2843b85d65d2efe
1 file changed
+89
-109
src/ext/Firewall/ca/firewall.cpp
+89
-109
@@ -8,6 +8,20 @@ enum eFirewallExceptionQuery { feqName = 1, feqRemoteAddresses, feqPort, feqProt
8
enum eFirewallExceptionTarget { fetPort = 1, fetApplication, fetUnknown };
9
enum eFirewallExceptionAttributes { feaIgnoreFailures = 1 };
10
11
+struct FIREWALL_EXCEPTION_ATTRIBUTES
12
+{
13
+ LPWSTR pwzName;
14
+
15
+ LPWSTR pwzRemoteAddresses;
16
+ LPWSTR pwzPort;
17
+ int iProtocol;
18
+ LPWSTR pwzProgram;
19
+ int iAttributes;
20
+ int iProfile;
21
+ LPWSTR pwzDescription;
22
+ int iDirection;
23
+};
24
+
25
/******************************************************************
26
SchedFirewallExceptions - immediate custom action worker to
27
register and remove firewall exceptions.
@@ -26,17 +40,9 @@ static UINT SchedFirewallExceptions(
40
PMSIHANDLE hRec = NULL;
41
42
LPWSTR pwzCustomActionData = NULL;
29
- LPWSTR pwzName = NULL;
30
- LPWSTR pwzRemoteAddresses = NULL;
31
- LPWSTR pwzPort = NULL;
32
- int iProtocol = 0;
33
- int iAttributes = 0;
34
- int iProfile = 0;
35
- LPWSTR pwzProgram = NULL;
43
LPWSTR pwzComponent = NULL;
37
- LPWSTR pwzFormattedFile = NULL;
38
- LPWSTR pwzDescription = NULL;
39
- int iDirection = MSI_NULL_INTEGER;
44
+
45
+ FIREWALL_EXCEPTION_ATTRIBUTES attrs = { 0 };
46
47
// initialize
48
hr = WcaInitialize(hInstall, "SchedFirewallExceptions");
@@ -55,34 +61,34 @@ static UINT SchedFirewallExceptions(
61
62
while (S_OK == (hr = WcaFetchRecord(hView, &hRec)))
63
{
58
- hr = WcaGetRecordFormattedString(hRec, feqName, &pwzName);
64
+ hr = WcaGetRecordFormattedString(hRec, feqName, &attrs.pwzName);
65
ExitOnFailure(hr, "Failed to get firewall exception name.");
66
61
- hr = WcaGetRecordFormattedString(hRec, feqRemoteAddresses, &pwzRemoteAddresses);
67
+ hr = WcaGetRecordFormattedString(hRec, feqRemoteAddresses, &attrs.pwzRemoteAddresses);
68
ExitOnFailure(hr, "Failed to get firewall exception remote addresses.");
69
64
- hr = WcaGetRecordFormattedString(hRec, feqPort, &pwzPort);
70
+ hr = WcaGetRecordFormattedString(hRec, feqPort, &attrs.pwzPort);
71
ExitOnFailure(hr, "Failed to get firewall exception port.");
72
67
- hr = WcaGetRecordInteger(hRec, feqProtocol, &iProtocol);
73
+ hr = WcaGetRecordInteger(hRec, feqProtocol, &attrs.iProtocol);
74
ExitOnFailure(hr, "Failed to get firewall exception protocol.");
75
70
- hr = WcaGetRecordFormattedString(hRec, feqProgram, &pwzProgram);
76
+ hr = WcaGetRecordFormattedString(hRec, feqProgram, &attrs.pwzProgram);
77
ExitOnFailure(hr, "Failed to get firewall exception program.");
78
73
- hr = WcaGetRecordInteger(hRec, feqAttributes, &iAttributes);
79
+ hr = WcaGetRecordInteger(hRec, feqAttributes, &attrs.iAttributes);
80
ExitOnFailure(hr, "Failed to get firewall exception attributes.");
81
76
- hr = WcaGetRecordInteger(hRec, feqProfile, &iProfile);
82
+ hr = WcaGetRecordInteger(hRec, feqProfile, &attrs.iProfile);
83
ExitOnFailure(hr, "Failed to get firewall exception profile.");
84
85
hr = WcaGetRecordString(hRec, feqComponent, &pwzComponent);
86
ExitOnFailure(hr, "Failed to get firewall exception component.");
87
82
- hr = WcaGetRecordFormattedString(hRec, feqDescription, &pwzDescription);
88
+ hr = WcaGetRecordFormattedString(hRec, feqDescription, &attrs.pwzDescription);
89
ExitOnFailure(hr, "Failed to get firewall exception description.");
90
85
- hr = WcaGetRecordInteger(hRec, feqDirection, &iDirection);
91
+ hr = WcaGetRecordInteger(hRec, feqDirection, &attrs.iDirection);
92
ExitOnFailure(hr, "Failed to get firewall exception direction.");
93
94
// figure out what we're doing for this exception, treating reinstall the same as install
@@ -98,25 +104,25 @@ static UINT SchedFirewallExceptions(
104
hr = WcaWriteIntegerToCaData(todoComponent, &pwzCustomActionData);
105
ExitOnFailure(hr, "failed to write exception action to custom action data");
106
101
- hr = WcaWriteStringToCaData(pwzName, &pwzCustomActionData);
107
+ hr = WcaWriteStringToCaData(attrs.pwzName, &pwzCustomActionData);
108
ExitOnFailure(hr, "failed to write exception name to custom action data");
109
104
- hr = WcaWriteIntegerToCaData(iProfile, &pwzCustomActionData);
110
+ hr = WcaWriteIntegerToCaData(attrs.iProfile, &pwzCustomActionData);
111
ExitOnFailure(hr, "failed to write exception profile to custom action data");
112
107
- hr = WcaWriteStringToCaData(pwzRemoteAddresses, &pwzCustomActionData);
113
+ hr = WcaWriteStringToCaData(attrs.pwzRemoteAddresses, &pwzCustomActionData);
114
ExitOnFailure(hr, "failed to write exception remote addresses to custom action data");
115
110
- hr = WcaWriteIntegerToCaData(iAttributes, &pwzCustomActionData);
116
+ hr = WcaWriteIntegerToCaData(attrs.iAttributes, &pwzCustomActionData);
117
ExitOnFailure(hr, "failed to write exception attributes to custom action data");
118
113
- if (*pwzProgram)
119
+ if (*attrs.pwzProgram)
120
{
121
// If program is defined, we have an application exception.
122
hr = WcaWriteIntegerToCaData(fetApplication, &pwzCustomActionData);
123
ExitOnFailure(hr, "failed to write exception target (application) to custom action data");
124
119
- hr = WcaWriteStringToCaData(pwzProgram, &pwzCustomActionData);
125
+ hr = WcaWriteStringToCaData(attrs.pwzProgram, &pwzCustomActionData);
126
ExitOnFailure(hr, "failed to write application path to custom action data");
127
}
128
else
@@ -126,16 +132,16 @@ static UINT SchedFirewallExceptions(
132
ExitOnFailure(hr, "failed to write exception target (port) to custom action data");
133
}
134
129
- hr = WcaWriteStringToCaData(pwzPort, &pwzCustomActionData);
135
+ hr = WcaWriteStringToCaData(attrs.pwzPort, &pwzCustomActionData);
136
ExitOnFailure(hr, "failed to write application path to custom action data");
137
132
- hr = WcaWriteIntegerToCaData(iProtocol, &pwzCustomActionData);
138
+ hr = WcaWriteIntegerToCaData(attrs.iProtocol, &pwzCustomActionData);
139
ExitOnFailure(hr, "failed to write exception protocol to custom action data");
140
135
- hr = WcaWriteStringToCaData(pwzDescription, &pwzCustomActionData);
141
+ hr = WcaWriteStringToCaData(attrs.pwzDescription, &pwzCustomActionData);
142
ExitOnFailure(hr, "failed to write firewall rule description to custom action data");
143
138
- hr = WcaWriteIntegerToCaData(iDirection, &pwzCustomActionData);
144
+ hr = WcaWriteIntegerToCaData(attrs.iDirection, &pwzCustomActionData);
145
ExitOnFailure(hr, "failed to write firewall rule direction to custom action data");
146
}
147
@@ -172,14 +178,13 @@ static UINT SchedFirewallExceptions(
178
}
179
180
LExit:
175
- ReleaseStr(pwzCustomActionData);
176
- ReleaseStr(pwzName);
177
- ReleaseStr(pwzRemoteAddresses);
178
- ReleaseStr(pwzPort);
179
- ReleaseStr(pwzProgram);
181
+ ReleaseStr(attrs.pwzName);
182
+ ReleaseStr(attrs.pwzRemoteAddresses);
183
+ ReleaseStr(attrs.pwzPort);
184
+ ReleaseStr(attrs.pwzProgram);
185
+ ReleaseStr(attrs.pwzDescription);
186
ReleaseStr(pwzComponent);
181
- ReleaseStr(pwzDescription);
182
- ReleaseStr(pwzFormattedFile);
187
+ ReleaseStr(pwzCustomActionData);
188
189
return WcaFinalize(er = FAILED(hr) ? ERROR_INSTALL_FAILURE : er);
190
}
@@ -272,12 +277,7 @@ LExit:
277
********************************************************************/
278
static HRESULT CreateFwRuleObject(
279
__in BSTR bstrName,
275
- __in int iProfile,
276
- __in_opt LPCWSTR wzRemoteAddresses,
277
- __in LPCWSTR wzPort,
278
- __in int iProtocol,
279
- __in LPCWSTR wzDescription,
280
- __in int iDirection,
280
+ __in FIREWALL_EXCEPTION_ATTRIBUTES const& attrs,
281
__out INetFwRule** ppNetFwRule
282
)
283
{
@@ -289,11 +289,11 @@ static HRESULT CreateFwRuleObject(
289
*ppNetFwRule = NULL;
290
291
// convert to BSTRs to make COM happy
292
- bstrRemoteAddresses = ::SysAllocString(wzRemoteAddresses);
292
+ bstrRemoteAddresses = ::SysAllocString(attrs.pwzRemoteAddresses);
293
ExitOnNull(bstrRemoteAddresses, hr, E_OUTOFMEMORY, "failed SysAllocString for remote addresses");
294
- bstrPort = ::SysAllocString(wzPort);
294
+ bstrPort = ::SysAllocString(attrs.pwzPort);
295
ExitOnNull(bstrPort, hr, E_OUTOFMEMORY, "failed SysAllocString for port");
296
- bstrDescription = ::SysAllocString(wzDescription);
296
+ bstrDescription = ::SysAllocString(attrs.pwzDescription);
297
ExitOnNull(bstrDescription, hr, E_OUTOFMEMORY, "failed SysAllocString for description");
298
299
hr = ::CoCreateInstance(__uuidof(NetFwRule), NULL, CLSCTX_ALL, __uuidof(INetFwRule), (void**)&pNetFwRule);
@@ -302,12 +302,12 @@ static HRESULT CreateFwRuleObject(
302
hr = pNetFwRule->put_Name(bstrName);
303
ExitOnFailure(hr, "failed to set exception name");
304
305
- hr = pNetFwRule->put_Profiles(static_cast<NET_FW_PROFILE_TYPE2>(iProfile));
305
+ hr = pNetFwRule->put_Profiles(static_cast<NET_FW_PROFILE_TYPE2>(attrs.iProfile));
306
ExitOnFailure(hr, "failed to set exception profile");
307
308
- if (MSI_NULL_INTEGER != iProtocol)
308
+ if (MSI_NULL_INTEGER != attrs.iProtocol)
309
{
310
- hr = pNetFwRule->put_Protocol(static_cast<NET_FW_IP_PROTOCOL>(iProtocol));
310
+ hr = pNetFwRule->put_Protocol(static_cast<NET_FW_IP_PROTOCOL>(attrs.iProtocol));
311
ExitOnFailure(hr, "failed to set exception protocol");
312
}
313
@@ -329,9 +329,9 @@ static HRESULT CreateFwRuleObject(
329
ExitOnFailure(hr, "failed to set exception description '%ls'", bstrDescription);
330
}
331
332
- if (MSI_NULL_INTEGER != iDirection)
332
+ if (MSI_NULL_INTEGER != attrs.iDirection)
333
{
334
- hr = pNetFwRule->put_Direction(static_cast<NET_FW_RULE_DIRECTION> (iDirection));
334
+ hr = pNetFwRule->put_Direction(static_cast<NET_FW_RULE_DIRECTION> (attrs.iDirection));
335
ExitOnFailure(hr, "failed to set exception direction");
336
}
337
@@ -352,15 +352,8 @@ LExit:
352
353
********************************************************************/
354
static HRESULT AddApplicationException(
355
- __in LPCWSTR wzFile,
356
- __in LPCWSTR wzName,
357
- __in int iProfile,
358
- __in_opt LPCWSTR wzRemoteAddresses,
359
- __in BOOL fIgnoreFailures,
360
- __in LPCWSTR wzPort,
361
- __in int iProtocol,
362
- __in LPCWSTR wzDescription,
363
- __in int iDirection
355
+ __in FIREWALL_EXCEPTION_ATTRIBUTES const& attrs,
356
+ __in BOOL fIgnoreFailures
357
)
358
{
359
HRESULT hr = S_OK;
@@ -370,9 +363,9 @@ static HRESULT AddApplicationException(
363
INetFwRule* pNetFwRule = NULL;
364
365
// convert to BSTRs to make COM happy
373
- bstrFile = ::SysAllocString(wzFile);
366
+ bstrFile = ::SysAllocString(attrs.pwzProgram);
367
ExitOnNull(bstrFile, hr, E_OUTOFMEMORY, "failed SysAllocString for path");
375
- bstrName = ::SysAllocString(wzName);
368
+ bstrName = ::SysAllocString(attrs.pwzName);
369
ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "failed SysAllocString for name");
370
371
// get the collection of firewall rules
@@ -387,7 +380,7 @@ static HRESULT AddApplicationException(
380
hr = pNetFwRules->Item(bstrName, &pNetFwRule);
381
if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr)
382
{
390
- hr = CreateFwRuleObject(bstrName, iProfile, wzRemoteAddresses, wzPort, iProtocol, wzDescription, iDirection, &pNetFwRule);
383
+ hr = CreateFwRuleObject(bstrName, attrs, &pNetFwRule);
384
ExitOnFailure(hr, "failed to create FwRule object");
385
386
// set edge traversal to true
@@ -429,15 +422,9 @@ LExit:
422
423
********************************************************************/
424
static HRESULT AddPortException(
432
- __in LPCWSTR wzName,
433
- __in int iProfile,
434
- __in_opt LPCWSTR wzRemoteAddresses,
435
- __in BOOL fIgnoreFailures,
436
- __in LPCWSTR wzPort,
437
- __in int iProtocol,
438
- __in LPCWSTR wzDescription,
439
- __in int iDirection
440
-)
425
+ __in FIREWALL_EXCEPTION_ATTRIBUTES const& attrs,
426
+ __in BOOL fIgnoreFailures
427
+ )
428
{
429
HRESULT hr = S_OK;
430
BSTR bstrName = NULL;
@@ -445,7 +432,7 @@ static HRESULT AddPortException(
432
INetFwRule* pNetFwRule = NULL;
433
434
// convert to BSTRs to make COM happy
448
- bstrName = ::SysAllocString(wzName);
435
+ bstrName = ::SysAllocString(attrs.pwzName);
436
ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "failed SysAllocString for name");
437
438
// get the collection of firewall rules
@@ -460,7 +447,7 @@ static HRESULT AddPortException(
447
hr = pNetFwRules->Item(bstrName, &pNetFwRule);
448
if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr)
449
{
463
- hr = CreateFwRuleObject(bstrName, iProfile, wzRemoteAddresses, wzPort, iProtocol, wzDescription, iDirection, &pNetFwRule);
450
+ hr = CreateFwRuleObject(bstrName, attrs, &pNetFwRule);
451
ExitOnFailure(hr, "failed to create FwRule object");
452
453
// enable it
@@ -535,16 +522,9 @@ extern "C" UINT __stdcall ExecFirewallExceptions(
522
LPWSTR pwz = NULL;
523
LPWSTR pwzCustomActionData = NULL;
524
int iTodo = WCA_TODO_UNKNOWN;
538
- LPWSTR pwzName = NULL;
539
- LPWSTR pwzRemoteAddresses = NULL;
540
- int iAttributes = 0;
525
int iTarget = fetUnknown;
542
- LPWSTR pwzFile = NULL;
543
- LPWSTR pwzPort = NULL;
544
- LPWSTR pwzDescription = NULL;
545
- int iProtocol = 0;
546
- int iProfile = 0;
547
- int iDirection = 0;
526
+
527
+ FIREWALL_EXCEPTION_ATTRIBUTES attrs = { 0 };
528
529
// initialize
530
hr = WcaInitialize(hInstall, "ExecFirewallExceptions");
@@ -576,35 +556,35 @@ extern "C" UINT __stdcall ExecFirewallExceptions(
556
}
557
}
558
579
- hr = WcaReadStringFromCaData(&pwz, &pwzName);
559
+ hr = WcaReadStringFromCaData(&pwz, &attrs.pwzName);
560
ExitOnFailure(hr, "failed to read name from custom action data");
561
582
- hr = WcaReadIntegerFromCaData(&pwz, &iProfile);
562
+ hr = WcaReadIntegerFromCaData(&pwz, &attrs.iProfile);
563
ExitOnFailure(hr, "failed to read profile from custom action data");
564
585
- hr = WcaReadStringFromCaData(&pwz, &pwzRemoteAddresses);
565
+ hr = WcaReadStringFromCaData(&pwz, &attrs.pwzRemoteAddresses);
566
ExitOnFailure(hr, "failed to read remote addresses from custom action data");
567
588
- hr = WcaReadIntegerFromCaData(&pwz, &iAttributes);
568
+ hr = WcaReadIntegerFromCaData(&pwz, &attrs.iAttributes);
569
ExitOnFailure(hr, "failed to read attributes from custom action data");
590
- BOOL fIgnoreFailures = feaIgnoreFailures == (iAttributes & feaIgnoreFailures);
570
+ BOOL fIgnoreFailures = feaIgnoreFailures == (attrs.iAttributes & feaIgnoreFailures);
571
572
hr = WcaReadIntegerFromCaData(&pwz, &iTarget);
573
ExitOnFailure(hr, "failed to read target from custom action data");
574
575
if (iTarget == fetApplication)
576
{
597
- hr = WcaReadStringFromCaData(&pwz, &pwzFile);
577
+ hr = WcaReadStringFromCaData(&pwz, &attrs.pwzProgram);
578
ExitOnFailure(hr, "failed to read file path from custom action data");
579
}
580
601
- hr = WcaReadStringFromCaData(&pwz, &pwzPort);
581
+ hr = WcaReadStringFromCaData(&pwz, &attrs.pwzPort);
582
ExitOnFailure(hr, "failed to read port from custom action data");
603
- hr = WcaReadIntegerFromCaData(&pwz, &iProtocol);
583
+ hr = WcaReadIntegerFromCaData(&pwz, &attrs.iProtocol);
584
ExitOnFailure(hr, "failed to read protocol from custom action data");
605
- hr = WcaReadStringFromCaData(&pwz, &pwzDescription);
585
+ hr = WcaReadStringFromCaData(&pwz, &attrs.pwzDescription);
586
ExitOnFailure(hr, "failed to read protocol from custom action data");
607
- hr = WcaReadIntegerFromCaData(&pwz, &iDirection);
587
+ hr = WcaReadIntegerFromCaData(&pwz, &attrs.iDirection);
588
ExitOnFailure(hr, "failed to read direction from custom action data");
589
590
switch (iTarget)
@@ -614,15 +594,15 @@ extern "C" UINT __stdcall ExecFirewallExceptions(
594
{
595
case WCA_TODO_INSTALL:
596
case WCA_TODO_REINSTALL:
617
- WcaLog(LOGMSG_STANDARD, "Installing firewall exception2 %ls on port %ls, protocol %d", pwzName, pwzPort, iProtocol);
618
- hr = AddPortException(pwzName, iProfile, pwzRemoteAddresses, fIgnoreFailures, pwzPort, iProtocol, pwzDescription, iDirection);
619
- ExitOnFailure(hr, "failed to add/update port exception for name '%ls' on port %ls, protocol %d", pwzName, pwzPort, iProtocol);
597
+ WcaLog(LOGMSG_STANDARD, "Installing firewall exception %ls on port %ls, protocol %d", attrs.pwzName, attrs.pwzPort, attrs.iProtocol);
598
+ hr = AddPortException(attrs, fIgnoreFailures);
599
+ ExitOnFailure(hr, "failed to add/update port exception for name '%ls' on port %ls, protocol %d", attrs.pwzName, attrs.pwzPort, attrs.iProtocol);
600
break;
601
602
case WCA_TODO_UNINSTALL:
623
- WcaLog(LOGMSG_STANDARD, "Uninstalling firewall exception2 %ls on port %ls, protocol %d", pwzName, pwzPort, iProtocol);
624
- hr = RemoveException(pwzName, fIgnoreFailures);
625
- ExitOnFailure(hr, "failed to remove port exception for name '%ls' on port %ls, protocol %d", pwzName, pwzPort, iProtocol);
603
+ WcaLog(LOGMSG_STANDARD, "Uninstalling firewall exception %ls on port %ls, protocol %d", attrs.pwzName, attrs.pwzPort, attrs.iProtocol);
604
+ hr = RemoveException(attrs.pwzName, fIgnoreFailures);
605
+ ExitOnFailure(hr, "failed to remove port exception for name '%ls' on port %ls, protocol %d", attrs.pwzName, attrs.pwzPort, attrs.iProtocol);
606
break;
607
}
608
break;
@@ -632,15 +612,15 @@ extern "C" UINT __stdcall ExecFirewallExceptions(
612
{
613
case WCA_TODO_INSTALL:
614
case WCA_TODO_REINSTALL:
635
- WcaLog(LOGMSG_STANDARD, "Installing firewall exception2 %ls (%ls)", pwzName, pwzFile);
636
- hr = AddApplicationException(pwzFile, pwzName, iProfile, pwzRemoteAddresses, fIgnoreFailures, pwzPort, iProtocol, pwzDescription, iDirection);
637
- ExitOnFailure(hr, "failed to add/update application exception for name '%ls', file '%ls'", pwzName, pwzFile);
615
+ WcaLog(LOGMSG_STANDARD, "Installing firewall exception %ls (%ls)", attrs.pwzName, attrs.pwzProgram);
616
+ hr = AddApplicationException(attrs, fIgnoreFailures);
617
+ ExitOnFailure(hr, "failed to add/update application exception for name '%ls', file '%ls'", attrs.pwzName, attrs.pwzProgram);
618
break;
619
620
case WCA_TODO_UNINSTALL:
641
- WcaLog(LOGMSG_STANDARD, "Uninstalling firewall exception2 %ls (%ls)", pwzName, pwzFile);
642
- hr = RemoveException(pwzName, fIgnoreFailures);
643
- ExitOnFailure(hr, "failed to remove application exception for name '%ls', file '%ls'", pwzName, pwzFile);
621
+ WcaLog(LOGMSG_STANDARD, "Uninstalling firewall exception %ls (%ls)", attrs.pwzName, attrs.pwzProgram);
622
+ hr = RemoveException(attrs.pwzName, fIgnoreFailures);
623
+ ExitOnFailure(hr, "failed to remove application exception for name '%ls', file '%ls'", attrs.pwzName, attrs.pwzProgram);
624
break;
625
}
626
break;
@@ -649,11 +629,11 @@ extern "C" UINT __stdcall ExecFirewallExceptions(
629
630
LExit:
631
ReleaseStr(pwzCustomActionData);
652
- ReleaseStr(pwzName);
653
- ReleaseStr(pwzRemoteAddresses);
654
- ReleaseStr(pwzFile);
655
- ReleaseStr(pwzPort);
656
- ReleaseStr(pwzDescription);
632
+ ReleaseStr(attrs.pwzName);
633
+ ReleaseStr(attrs.pwzRemoteAddresses);
634
+ ReleaseStr(attrs.pwzProgram);
635
+ ReleaseStr(attrs.pwzPort);
636
+ ReleaseStr(attrs.pwzDescription);
637
::CoUninitialize();
638
639
return WcaFinalize(FAILED(hr) ? ERROR_INSTALL_FAILURE : ERROR_SUCCESS);