コード例 #1
0
        public static void AddRule(Rule rule, IPSecuritySection section)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }
            if (rule.IpAddress == null)
            {
                throw new ArgumentNullException("rule.IpAddress");
            }

            IPAddressFilterCollection collection = section.IpAddressFilters;

            int dummy;

            if (ExistsAddressFilter(collection, rule.DomainName, rule.IpAddress, rule.SubnetMask, out dummy))
            {
                throw new AlreadyExistsException("rule");
            }

            try {
                Rule element = collection.Add(rule);
                element.Allowed = rule.Allowed;
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
コード例 #2
0
        public static Rule SetRule(Rule rule, dynamic model, IPSecuritySection section)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }

            string subnetMask = DynamicHelper.Value(model.subnet_mask);

            if (subnetMask != null)
            {
                try {
                    // Throw format exception of subnet mask is not in valid format
                    IPAddress.Parse(subnetMask);
                }
                catch (FormatException e) {
                    throw new ApiArgumentException("subnet_mask", e);
                }
            }

            string    inIp      = DynamicHelper.Value(model.ip_address);
            IPAddress ipAddress = null;

            if (inIp != null)
            {
                try {
                    ipAddress = IPAddress.Parse(inIp);
                }
                catch (FormatException e) {
                    throw new ApiArgumentException("ip_address", e);
                }

                if (ipAddress != rule.IpAddress && section.IpAddressFilters.Any(r => r.IpAddress.Equals(ipAddress)))
                {
                    throw new AlreadyExistsException("ip_address");
                }
            }

            try {
                rule.IpAddress  = ipAddress != null ? ipAddress : rule.IpAddress;
                rule.Allowed    = DynamicHelper.To <bool>(model.allowed) ?? rule.Allowed;
                rule.SubnetMask = subnetMask ?? rule.SubnetMask;
                rule.DomainName = DynamicHelper.Value(model.domain_name) ?? rule.DomainName;
            }
            catch (FileLoadException e) {
                throw new LockedException(IPRestrictionsGlobals.IPSecuritySectionName, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }

            return(rule);
        }
コード例 #3
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            // Rule must be created for a specific ip restriction section
            if (model.ip_restriction == null)
            {
                throw new ApiArgumentException("ip_restriction");
            }
            if (!(model.ip_restriction is JObject))
            {
                throw new ApiArgumentException("ip_restriction");
            }
            string ipUuid = DynamicHelper.Value(model.ip_restriction.id);

            if (ipUuid == null)
            {
                throw new ApiArgumentException("ip_restriction.id");
            }

            // Get the ip restriction feature id
            IPRestrictionId ipId = new IPRestrictionId(ipUuid);

            // Get site
            Site site = ipId.SiteId == null ? null : SiteHelper.GetSite(ipId.SiteId.Value);

            string            configPath = ManagementUnit.ResolveConfigScope(model);
            IPSecuritySection section    = IPRestrictionsHelper.GetSection(site, ipId.Path, configPath);

            // Create ip restriction rule
            Rule rule = IPRestrictionsHelper.CreateRule(model, section);

            // Add it
            IPRestrictionsHelper.AddRule(rule, section);

            // Save
            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic r = IPRestrictionsHelper.RuleToJsonModel(rule, site, ipId.Path);

            return(Created(IPRestrictionsHelper.GetRuleLocation(r.id), r));
        }
コード例 #4
0
        public static void DeleteRule(Rule rule, IPSecuritySection section)
        {
            IPAddressFilterCollection collection = section.IpAddressFilters;

            int dummy;

            if (ExistsAddressFilter(collection, rule.DomainName, rule.IpAddress, rule.SubnetMask, out dummy))
            {
                try {
                    collection.RemoveAt(dummy);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
コード例 #5
0
        public static Rule CreateRule(dynamic model, IPSecuritySection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            IPAddress ip        = null;
            string    ipAddress = DynamicHelper.Value(model.ip_address);

            if (string.IsNullOrEmpty(ipAddress))
            {
                throw new ApiArgumentException("ip_address");
            }

            try {
                // Throw format error if ip address is not a valid ip address
                ip = IPAddress.Parse(ipAddress);
            }
            catch (FormatException e) {
                throw new ApiArgumentException("ip_address", e);
            }

            if (model.allowed == null)
            {
                throw new ApiArgumentException("allowed");
            }

            Rule rule = section.IpAddressFilters.CreateElement();

            rule.IpAddress = ip;

            SetRule(rule, model, section);

            return(rule);
        }