Skip to content

Commit 29497f3

Browse files
dahall_cpdahall_cp
dahall_cp
authored and
dahall_cp
committed
* Lots of updates to TaskSchedulerConfig - mostly working
* Minor bug fixes to accommodate connecting from V2 to V1 systems
1 parent 64c1987 commit 29497f3

File tree

11 files changed

+1062
-83
lines changed

11 files changed

+1062
-83
lines changed

Configurer.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Security.Principal;
45
using System.Text;
56

67
namespace TaskSchedulerConfig
78
{
89
class Configurer
910
{
11+
Validator v;
12+
13+
public Configurer(Validator validator)
14+
{
15+
v = validator;
16+
}
17+
18+
internal bool EnableFirewall(object obj)
19+
{
20+
v.Firewall.Enabled = true;
21+
return v.Firewall.Enabled;
22+
}
23+
24+
internal bool EnableFirewallRule(object obj)
25+
{
26+
if (obj is Firewall.Rule)
27+
{
28+
v.Firewall.Rules[(Firewall.Rule)obj] = true;
29+
return v.Firewall.Rules[(Firewall.Rule)obj];
30+
}
31+
throw new ArgumentException();
32+
}
1033
}
1134
}

Firewall.cs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System;
2+
3+
namespace TaskSchedulerConfig
4+
{
5+
class Firewall
6+
{
7+
internal static bool OldFirewall = Environment.OSVersion.Version.Major == 5;
8+
9+
public enum Rule
10+
{
11+
FileAndPrinterSharing,
12+
RemoteTaskManagement
13+
}
14+
15+
public Firewall(string server)
16+
{
17+
Rules = new RulesContainer(this);
18+
Type NetFwMgrType = Type.GetTypeFromProgID(OldFirewall ? "HNetCfg.FwMgr" : "HNetCfg.FwPolicy2", server, false);
19+
Instance = Activator.CreateInstance(NetFwMgrType);
20+
}
21+
22+
public bool Enabled
23+
{
24+
get
25+
{
26+
if (OldFirewall)
27+
return Instance.LocalPolicy.CurrentProfile.FirewallEnabled;
28+
else
29+
{
30+
const int NET_FW_PROFILE2_DOMAIN = 1;
31+
const int NET_FW_PROFILE2_PRIVATE = 2;
32+
const int NET_FW_PROFILE2_PUBLIC = 4;
33+
34+
bool result = false;
35+
int CurrentProfiles = Instance.CurrentProfileTypes;
36+
37+
// The returned 'CurrentProfiles' bit mask can have more than 1 bit set if multiple profiles are active or current at the same time
38+
if ((CurrentProfiles & NET_FW_PROFILE2_DOMAIN) != 0 && Instance.FirewallEnabled(NET_FW_PROFILE2_DOMAIN))
39+
result = true;
40+
if ((CurrentProfiles & NET_FW_PROFILE2_PRIVATE) != 0 && Instance.FirewallEnabled(NET_FW_PROFILE2_PRIVATE))
41+
result = true;
42+
if ((CurrentProfiles & NET_FW_PROFILE2_PUBLIC) != 0 && Instance.FirewallEnabled(NET_FW_PROFILE2_PUBLIC))
43+
result = true;
44+
return result;
45+
}
46+
}
47+
set
48+
{
49+
if (OldFirewall)
50+
{
51+
Instance.LocalPolicy.CurrentProfile.FirewallEnabled = value;
52+
}
53+
else
54+
{
55+
int CurrentProfiles = Instance.CurrentProfileTypes;
56+
Instance.set_FirewallEnabled(CurrentProfiles, value);
57+
}
58+
}
59+
}
60+
61+
public RulesContainer Rules { get; }
62+
63+
public dynamic Instance { get; }
64+
65+
public class RulesContainer
66+
{
67+
private const int NET_FW_SERVICE_FILE_AND_PRINT = 0;
68+
private const string FPS = "File and Printer Sharing";
69+
private const string RSTM = "Remote Scheduled Tasks Management";
70+
71+
private Firewall firewall;
72+
73+
internal RulesContainer(Firewall f) { firewall = f; }
74+
75+
public bool this[Rule rule]
76+
{
77+
get
78+
{
79+
if (Firewall.OldFirewall)
80+
{
81+
switch (rule)
82+
{
83+
case Rule.FileAndPrinterSharing:
84+
return firewall.Instance.LocalPolicy.CurrentProfile.Services.Item(NET_FW_SERVICE_FILE_AND_PRINT).Enabled;
85+
default:
86+
throw new IndexOutOfRangeException("Unrecognized rule");
87+
}
88+
}
89+
else
90+
{
91+
switch (rule)
92+
{
93+
case Rule.FileAndPrinterSharing:
94+
return firewall.Instance.IsRuleGroupCurrentlyEnabled(FPS);
95+
case Rule.RemoteTaskManagement:
96+
return firewall.Instance.IsRuleGroupCurrentlyEnabled(RSTM);
97+
default:
98+
throw new IndexOutOfRangeException("Unrecognized rule");
99+
}
100+
}
101+
}
102+
set
103+
{
104+
if (Firewall.OldFirewall)
105+
{
106+
switch (rule)
107+
{
108+
case Rule.FileAndPrinterSharing:
109+
firewall.Instance.LocalPolicy.CurrentProfile.Services.Item(NET_FW_SERVICE_FILE_AND_PRINT).Enabled = value;
110+
break;
111+
default:
112+
throw new IndexOutOfRangeException("Unrecognized rule");
113+
}
114+
}
115+
else
116+
{
117+
switch (rule)
118+
{
119+
case Rule.FileAndPrinterSharing:
120+
firewall.Instance.EnableRuleGroup(firewall.Instance.CurrentProfileTypes, FPS, value);
121+
break;
122+
case Rule.RemoteTaskManagement:
123+
firewall.Instance.EnableRuleGroup(firewall.Instance.CurrentProfileTypes, RSTM, value);
124+
break;
125+
default:
126+
throw new IndexOutOfRangeException("Unrecognized rule");
127+
}
128+
}
129+
}
130+
}
131+
}
132+
}
133+
}

Main.Designer.cs

Lines changed: 207 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)