Exemple #1
0
        private bool FindFirstMatchingRule(Packet packet, out uint ruleId)
        {
            SortedSet <uint> matchingRuleSet = new SortedSet <uint>();

            ruleId = uint.MaxValue;
            //Console.WriteLine("Processing " + packet.UserName + "in thread : " + Thread.CurrentThread.ManagedThreadId);
            //Rules for matching name
            matchingRuleSet.UnionWith(FindRulesMatchingUserName(packet));
            if (matchingRuleSet.Count == 0)
            {
                return(false);
            }
            //Console.WriteLine("FindRulesMatchingUserName count " + userRuleSet.Count);

            //Rules for same masked ip
            HashSet <uint> maskedIPAllHostRuleSet = FindRulesMatchingIP(packet);

            //Console.WriteLine("FindRulesMatchingIP count " + maskedIPRuleSet.Count);

            //Add all Rules with hostname
            maskedIPAllHostRuleSet.UnionWith(myHostnameRuleSet);
            //Console.WriteLine("FindRulesMatchingIP count " + maskedIPRuleSet.Count);

            matchingRuleSet.IntersectWith(maskedIPAllHostRuleSet);
            //Console.WriteLine("IntersectWith FindRulesMatchingUserName FindRulesMatchingIP count " + userRuleSet.Count);
            if (matchingRuleSet.Count == 0)
            {
                //No rule matched
                return(false);
            }

            // ruleId = matchingRuleSet.Min();
            bool matched = false;

            foreach (uint id in matchingRuleSet)
            {
                if (!myHostnameRuleSet.Contains(id))
                {
                    matched = true;
                    ruleId  = id;
                    break;
                }
                IRule rule;
                Debug.Assert(myRules.TryGetValue(id, out rule));

                HostRule hostRule = rule as HostRule;
                Debug.Assert(hostRule != null);

                string packetHost = DNSlookupHelper.lookupCachedHost(packet.IPAddress);
                if (string.Equals(packetHost, hostRule.HostName))
                {
                    matched = true;
                    ruleId  = id;
                    break;
                }
            }
            return(matched);
        }
Exemple #2
0
        private void BuildHostnameRuleSet(uint ruleId, IRule rule)
        {
            HostRule hostRule = rule as HostRule;

            if (hostRule != null)
            {
                myHostnameRuleSet.Add(ruleId);
            }
        }
Exemple #3
0
        public static bool CreateHostnameRule(string line, out HostRule rule)
        {
            rule = null;
            //System.Console.WriteLine(line);
            string[] fields = line.Split('|');
            if (fields.Length != RULE_FIELDS_COUNT)
            {
                return(false);
            }

            bool isAllowed = false;

            if (!BaseRule.TryParseIsAllowed(fields[2], out isAllowed))
            {
                return(false);
            }

            string username = fields[0];
            string hostname = fields[1];

            rule = new HostRule(username, hostname, isAllowed);
            return(true);
        }