Esempio n. 1
0
        private void RemoveRulesNotRequried(List <FirewallRule> newRules)
        {
#if !DotNetCoreClrIOT
            List <string> rulesToBeDeleted = new List <string>();
            foreach (var rule in this.rules)
            {
                NetFwRule fwRule = (NetFwRule)rule;
                if (fwRule == null)
                {
                    continue;
                }
                if (FabricNodeFirewallRules.IsFabricFirewallRule(fwRule))
                {
                    if (newRules.All(newRule => newRule.Name != fwRule.Name)) // Firewall rule is not in the set of new rules
                    {
                        rulesToBeDeleted.Add(fwRule.Name);
                    }
                }
            }
            foreach (string ruleToBeDeleted in rulesToBeDeleted)
            {
                rules.Remove(ruleToBeDeleted);
            }
#endif
        }
 public static bool RuleIsV1FabricFirewallRule(NetFwRule rule)
 {
     return(rule.Name.IndexOf(FabricExceptionSubString, 0, StringComparison.OrdinalIgnoreCase) != -1 ||
            rule.Name.IndexOf(LeaseDriverExceptionSubString, 0, StringComparison.OrdinalIgnoreCase) != -1 ||
            rule.Name.IndexOf(ApplicationPortRangeExceptionTCPSubString, 0, StringComparison.OrdinalIgnoreCase) != -1 ||
            rule.Name.IndexOf(ApplicationPortRangeExceptionUDPSubString, 0, StringComparison.OrdinalIgnoreCase) != -1 ||
            (!string.IsNullOrEmpty(rule.ApplicationName) && rule.ApplicationName.IndexOf("Fabric.Code.1.0", 0, StringComparison.OrdinalIgnoreCase) != -1));
 }
Esempio n. 3
0
 public TestFirewallRule(NetFwRule rule)
 {
     this.Name            = rule.Name;
     this.Protocol        = (NET_FW_IP_PROTOCOL_)rule.Protocol;
     this.Ports           = rule.LocalPorts;
     this.Direction       = rule.Direction;
     this.ApplicationPath = rule.ApplicationName;
     this.Grouping        = rule.Grouping;
 }
Esempio n. 4
0
        private void AddOrUpdateRule(FirewallRule newRule)
        {
#if !DotNetCoreClrIOT
            try
            {
                bool addRule = true;
#if DotNetCoreClrLinux
                bool updateRuleForLinux = false;
#endif
                INetFwRule fwRule = null;
                foreach (var rule in rules)
                {
                    fwRule = (INetFwRule)rule;
                    if (fwRule.Name == newRule.Name)
                    {
                        addRule = false;
                        if (String.IsNullOrEmpty(newRule.ApplicationPath) ||
                            String.IsNullOrEmpty(fwRule.ApplicationName) ||
                            fwRule.ApplicationName != newRule.ApplicationPath)
                        {
                            fwRule.ApplicationName = newRule.ApplicationPath;
                        }

                        if (String.IsNullOrEmpty(newRule.Ports) ||
                            String.IsNullOrEmpty(fwRule.LocalPorts) ||
                            fwRule.LocalPorts != newRule.Ports)
                        {
                            fwRule.LocalPorts = newRule.Ports;
#if DotNetCoreClrLinux
                            updateRuleForLinux = true;
#endif
                        }

                        break;
                    }
                }

                if (addRule)
                {
                    NetFwRule netFwRule = newRule.GetNetFwRule() as NetFwRule;
                    rules.Add(netFwRule);
                }
#if DotNetCoreClrLinux
                else if (null != fwRule && updateRuleForLinux)
                {
                    rules.Update(fwRule);
                }
#endif
            }
            catch (Exception ex)
            {
                DeployerTrace.WriteError("Error encountered in AddorUpdateRule: {0}", ex.ToString());
                throw;
            }
#endif
        }
Esempio n. 5
0
        public bool UpdateRule(FirewallRule rule)
        {
            bool bAdd = (rule.guid == null);

            try
            {
                NetFwRule FwRule;
                if (bAdd)
                {
                    FwRule = new NetFwRule()
                    {
                        Entry = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")), Rule = rule
                    }
                }
                ;
                else if (!Rules.TryGetValue(rule.guid, out FwRule))
                {
                    Priv10Logger.LogError("Failed Updating rule: ruls is not longer present");
                    return(false);
                }
                else
                {
                    FwRule.Rule = rule;
                }

                if (!SaveRule(rule, FwRule.Entry))
                {
                    return(false);
                }

                if (bAdd)
                {
                    NetFwPolicy.Rules.Add(FwRule.Entry);

                    rule.Index = RuleCounter++;
                    rule.guid  = Guid.NewGuid().ToString("B");
                    Rules.Add(rule.guid, FwRule);
                }
            }
            catch (Exception err)
            {
                Priv10Logger.LogError("Failed to Write rule: " + err.Message);
                return(false);
            }
            return(true);
        }
Esempio n. 6
0
        public static void RemoveWinFabRules()
        {
            var NetFWPolicyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false);
            var mgr             = (INetFwPolicy2)Activator.CreateInstance(NetFWPolicyType);

            List <TestFirewallRule> rules = new List <TestFirewallRule>();

            foreach (var rule in mgr.Rules)
            {
                NetFwRule fwRule = (NetFwRule)rule;
                if (!String.IsNullOrEmpty(fwRule.Grouping) && ((NetFwRule)rule).Grouping.Equals("WindowsFabric"))
                {
                    rules.Add(new TestFirewallRule((NetFwRule)rule));
                }
            }
            TestFirewallManiuplator.RemoveRules(rules);
        }
Esempio n. 7
0
        private void RemoveV1Rules()
        {
            List <string> V1RulesToBeRemoved = new List <string>();

            foreach (var rule in rules)
            {
                NetFwRule fwRule = (NetFwRule)rule;
                if (V1FabricFirewallRuleSpecifications.RuleIsV1FabricFirewallRule(fwRule))
                {
                    V1RulesToBeRemoved.Add(fwRule.Name);
                }
            }
            foreach (string ruleName in V1RulesToBeRemoved)
            {
                rules.Remove(ruleName);
            }
        }
Esempio n. 8
0
        public static void AddRules(List <TestFirewallRule> rules)
        {
            var NetFWPolicyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false);
            var mgr             = (INetFwPolicy2)Activator.CreateInstance(NetFWPolicyType);

            foreach (var rule in rules)
            {
                var       NetFwRuleType = Type.GetTypeFromProgID("HNetCfg.FwRule", false);
                NetFwRule newRule       = (NetFwRule)Activator.CreateInstance(NetFwRuleType);
                newRule.Name            = rule.Name;
                newRule.Description     = rule.Name;
                newRule.Protocol        = (int)rule.Protocol;
                newRule.ApplicationName = rule.ApplicationPath;
                newRule.LocalPorts      = rule.Ports;
                newRule.Grouping        = rule.Grouping;
                mgr.Rules.Add(newRule);
            }
        }
Esempio n. 9
0
        public void RemoveWindowsFabricRules()
        {
#if !DotNetCoreClrIOT
            List <string> windowsFabricRuleNames = new List <string>();
            foreach (var rule in this.rules)
            {
                NetFwRule fwRule = (NetFwRule)rule;
                if (FabricNodeFirewallRules.IsFabricFirewallRule(fwRule))
                {
                    windowsFabricRuleNames.Add(fwRule.Name);
                }
            }

            foreach (var ruleName in windowsFabricRuleNames)
            {
                this.rules.Remove(ruleName);
            }
#endif
        }