private SortedList <int, PswaAuthorizationRule> ParseRuleList(XmlDocument doc, ArrayList errorMessageReport)
        {
            SortedList <int, PswaAuthorizationRule> nums;
            XmlNodeList elementsByTagName = doc.GetElementsByTagName("Rule");
            SortedList <int, PswaAuthorizationRule> nums1 = new SortedList <int, PswaAuthorizationRule>();

            try
            {
                foreach (XmlElement xmlElement in elementsByTagName)
                {
                    PswaAuthorizationRule pswaAuthorizationRule = new PswaAuthorizationRule();
                    pswaAuthorizationRule.DeserializeFromXmlElement(xmlElement);
                    int id = pswaAuthorizationRule.Id;
                    nums1.Add(id, pswaAuthorizationRule);
                }
                return(nums1);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                errorMessageReport.Add(new DataFileLoadError(DataFileLoadError.ErrorStatus.Error, exception));
                nums = null;
            }
            return(nums);
        }
        private void OnTestRuleRuleMatch(PswaAuthorizationRule rule)
        {
            EventHandler <TestRuleRuleMatchEventArgs> eventHandler = this.TestRuleRuleMatch;

            if (eventHandler != null)
            {
                eventHandler(this, new TestRuleRuleMatchEventArgs(rule));
            }
        }
        private void OnTestRuleInvalidRule(PswaAuthorizationRule rule, Exception exception)
        {
            EventHandler <TestRuleInvalidRuleEventArgs> testRuleInvalidRule = this.TestRuleInvalidRule;

            if (testRuleInvalidRule != null)
            {
                testRuleInvalidRule(this, new TestRuleInvalidRuleEventArgs(rule, exception));
            }
        }
        internal PswaAuthorizationRule AddRule(SortedList <int, PswaAuthorizationRule> allRules, string name, string user, PswaUserType userType, string destination, PswaDestinationType destinationType, string configuration, string canonicalDestination)
        {
            string str;
            string str1;
            bool   flag;
            int    nextAvailableId = this.GetNextAvailableId(allRules);
            PswaAuthorizationRule pswaAuthorizationRule = new PswaAuthorizationRule();

            pswaAuthorizationRule.Id = nextAvailableId;
            PswaAuthorizationRule pswaAuthorizationRule1 = pswaAuthorizationRule;

            if (name != null)
            {
                str = name;
            }
            else
            {
                object[] objArray = new object[1];
                objArray[0] = nextAvailableId;
                str         = string.Format(CultureInfo.CurrentCulture, Resources.AuthorizationRuleName_Format, objArray);
            }
            pswaAuthorizationRule1.RuleName = str;
            this.AddRuleSetUserInfo(pswaAuthorizationRule, user, userType);
            this.AddRuleSetDestinationInfo(pswaAuthorizationRule, destination, canonicalDestination, destinationType);
            PswaAuthorizationRule pswaAuthorizationRule2 = pswaAuthorizationRule;

            if (configuration == "*")
            {
                str1 = "*";
            }
            else
            {
                if (string.IsNullOrEmpty(configuration))
                {
                    str1 = "Microsoft.PowerShell";
                }
                else
                {
                    str1 = configuration;
                }
            }
            pswaAuthorizationRule2.ConfigurationName = str1;
            PswaAuthorizationRule pswaAuthorizationRule3 = pswaAuthorizationRule;

            if (destinationType == PswaDestinationType.All)
            {
                flag = false;
            }
            else
            {
                flag = destination != pswaAuthorizationRule.DestinationCanonicalForm;
            }
            pswaAuthorizationRule3.IsCanonicalDestinationSid = flag;
            allRules.Add(pswaAuthorizationRule.Id, pswaAuthorizationRule);
            return(pswaAuthorizationRule);
        }
        internal PswaAuthorizationRule RemoveRule(SortedList <int, PswaAuthorizationRule> allRules, int id)
        {
            PswaAuthorizationRule item = null;

            if (allRules.ContainsKey(id))
            {
                item = allRules[id];
                allRules.Remove(id);
            }
            return(item);
        }
		internal PswaAuthorizationRule AddRule(SortedList<int, PswaAuthorizationRule> allRules, string name, string user, PswaUserType userType, string destination, PswaDestinationType destinationType, string configuration, string canonicalDestination)
		{
			string str;
			string str1;
			bool flag;
			int nextAvailableId = this.GetNextAvailableId(allRules);
			PswaAuthorizationRule pswaAuthorizationRule = new PswaAuthorizationRule();
			pswaAuthorizationRule.Id = nextAvailableId;
			PswaAuthorizationRule pswaAuthorizationRule1 = pswaAuthorizationRule;
			if (name != null)
			{
				str = name;
			}
			else
			{
				object[] objArray = new object[1];
				objArray[0] = nextAvailableId;
				str = string.Format(CultureInfo.CurrentCulture, Resources.AuthorizationRuleName_Format, objArray);
			}
			pswaAuthorizationRule1.RuleName = str;
			this.AddRuleSetUserInfo(pswaAuthorizationRule, user, userType);
			this.AddRuleSetDestinationInfo(pswaAuthorizationRule, destination, canonicalDestination, destinationType);
			PswaAuthorizationRule pswaAuthorizationRule2 = pswaAuthorizationRule;
			if (configuration == "*")
			{
				str1 = "*";
			}
			else
			{
				if (string.IsNullOrEmpty(configuration))
				{
					str1 = "Microsoft.PowerShell";
				}
				else
				{
					str1 = configuration;
				}
			}
			pswaAuthorizationRule2.ConfigurationName = str1;
			PswaAuthorizationRule pswaAuthorizationRule3 = pswaAuthorizationRule;
			if (destinationType == PswaDestinationType.All)
			{
				flag = false;
			}
			else
			{
				flag = destination != pswaAuthorizationRule.DestinationCanonicalForm;
			}
			pswaAuthorizationRule3.IsCanonicalDestinationSid = flag;
			allRules.Add(pswaAuthorizationRule.Id, pswaAuthorizationRule);
			return pswaAuthorizationRule;
		}
        private void AddRuleSetDestinationInfo(PswaAuthorizationRule newRule, string destination, string canonicalDestination, PswaDestinationType destinationType)
        {
            bool flag = false;

            newRule.DestinationType = destinationType;
            if (destinationType != PswaDestinationType.Computer)
            {
                if (destinationType != PswaDestinationType.ComputerGroup)
                {
                    if (destinationType == PswaDestinationType.All)
                    {
                        newRule.Destination = "*";
                        newRule.DestinationCanonicalForm = "*";
                        newRule.IsComputerGroupLocal     = false;
                    }
                    return;
                }
                else
                {
                    if (this.CheckSamAccountFormat(destination))
                    {
                        string str       = null;
                        string stringSid = this.activeDirectoryHelper.ConvertAccountNameToStringSid(destination, out flag, out str);
                        string str1      = null;
                        if (this.activeDirectoryHelper.CheckComputerTypeMatch(flag, stringSid, destinationType, str, out str1))
                        {
                            newRule.Destination = destination;
                            newRule.DestinationCanonicalForm = stringSid;
                            newRule.IsComputerGroupLocal     = flag;
                            return;
                        }
                        else
                        {
                            throw new ArgumentException(str1);
                        }
                    }
                    else
                    {
                        throw new ArgumentException(Resources.InvalidUserAndGroupNameFormat);
                    }
                }
            }
            else
            {
                newRule.Destination = destination;
                newRule.DestinationCanonicalForm = canonicalDestination;
                newRule.IsComputerGroupLocal     = false;
                return;
            }
        }
        private PswaAuthorizationRule CheckAllowAllRule(PswaAuthorizationRule[] rules)
        {
            PswaAuthorizationRule[] pswaAuthorizationRuleArray = rules;
            int num = 0;

            while (num < (int)pswaAuthorizationRuleArray.Length)
            {
                PswaAuthorizationRule pswaAuthorizationRule = pswaAuthorizationRuleArray[num];
                if (!(pswaAuthorizationRule.UserCanonicalForm == "*") || !(pswaAuthorizationRule.DestinationCanonicalForm == "*") || !(pswaAuthorizationRule.ConfigurationName == "*"))
                {
                    num++;
                }
                else
                {
                    PswaAuthorizationRule pswaAuthorizationRule1 = pswaAuthorizationRule;
                    return(pswaAuthorizationRule1);
                }
            }
            return(null);
        }
Example #9
0
        internal void DeserializeFromXmlElement(XmlElement rule)
        {
            string str;
            string str1;

            this.Id       = Convert.ToInt32(rule.GetElementsByTagName("Id")[0].InnerText, CultureInfo.InvariantCulture);
            this.RuleName = rule.GetElementsByTagName("Name")[0].InnerText;
            this.UserType = (PswaUserType)Enum.Parse(typeof(PswaUserType), rule.GetElementsByTagName("UserType")[0].InnerText);
            PswaAuthorizationRule pswaAuthorizationRule = this;

            if (this.UserType == PswaUserType.All)
            {
                str = "*";
            }
            else
            {
                str = null;
            }
            pswaAuthorizationRule.User = str;
            this.UserCanonicalForm     = rule.GetElementsByTagName("UserCanonicalForm")[0].InnerText;
            this.DestinationType       = (PswaDestinationType)Enum.Parse(typeof(PswaDestinationType), rule.GetElementsByTagName("DestinationType")[0].InnerText);
            PswaAuthorizationRule pswaAuthorizationRule1 = this;

            if (this.DestinationType == PswaDestinationType.All)
            {
                str1 = "*";
            }
            else
            {
                str1 = null;
            }
            pswaAuthorizationRule1.Destination = str1;
            this.DestinationCanonicalForm      = rule.GetElementsByTagName("DestinationCanonicalForm")[0].InnerText;
            this.ConfigurationName             = rule.GetElementsByTagName("ConfigurationName")[0].InnerText;
            this.IsUserGroupLocal          = Convert.ToBoolean(rule.GetElementsByTagName("IsUserGroupLocal")[0].InnerText, CultureInfo.InvariantCulture);
            this.IsComputerGroupLocal      = Convert.ToBoolean(rule.GetElementsByTagName("IsComputerGroupLocal")[0].InnerText, CultureInfo.InvariantCulture);
            this.IsCanonicalDestinationSid = Convert.ToBoolean(rule.GetElementsByTagName("IsCanonicalDestinationSid")[0].InnerText, CultureInfo.InvariantCulture);
        }
        private void AddRuleSetUserInfo(PswaAuthorizationRule newRule, string user, PswaUserType userType)
        {
            bool flag = false;

            newRule.UserType = userType;
            if (userType == PswaUserType.All)
            {
                newRule.User = "******";
                newRule.UserCanonicalForm = newRule.User;
                newRule.IsUserGroupLocal  = false;
                return;
            }
            else
            {
                string str = null;
                if (this.CheckSamAccountFormat(user))
                {
                    newRule.User = user;
                    string stringSid = this.activeDirectoryHelper.ConvertAccountNameToStringSid(user, out flag, out str);
                    newRule.IsUserGroupLocal = flag;
                    string str1 = null;
                    if (this.activeDirectoryHelper.CheckUserTypeMatch(newRule.IsUserGroupLocal, stringSid, userType, str, out str1))
                    {
                        newRule.UserCanonicalForm = stringSid;
                        return;
                    }
                    else
                    {
                        throw new ArgumentException(str1);
                    }
                }
                else
                {
                    throw new ArgumentException(Resources.InvalidUserAndGroupNameFormat);
                }
            }
        }
        protected override void ProcessRecord()
        {
            string       str;
            PswaUserType pswaUserType;
            bool         flag;
            bool         flag1;
            bool         flag2;

            string[]            userName            = new string[0];
            PswaUserType        pswaUserType1       = PswaUserType.User;
            string              empty               = string.Empty;
            PswaDestinationType pswaDestinationType = PswaDestinationType.Computer;

            if (base.ParameterSetName == "UserNameComputerName" || base.ParameterSetName == "UserNameComputerGroupName")
            {
                userName      = this.UserName;
                pswaUserType1 = PswaUserType.User;
                for (int i = 0; i < (int)userName.Length; i++)
                {
                    userName[i] = PswaHelper.TranslateLocalAccountName(userName[i]);
                }
            }
            else
            {
                if (base.ParameterSetName == "UserGroupNameComputerName" || base.ParameterSetName == "UserGroupNameComputerGroupName")
                {
                    userName      = this.UserGroupName;
                    pswaUserType1 = PswaUserType.UserGroup;
                    for (int j = 0; j < (int)userName.Length; j++)
                    {
                        userName[j] = PswaHelper.TranslateLocalAccountName(userName[j]);
                    }
                }
            }
            if (base.ParameterSetName == "UserNameComputerName" || base.ParameterSetName == "UserGroupNameComputerName")
            {
                empty = this.ComputerName;
                pswaDestinationType = PswaDestinationType.Computer;
            }
            else
            {
                if (base.ParameterSetName == "UserNameComputerGroupName" || base.ParameterSetName == "UserGroupNameComputerGroupName")
                {
                    empty = PswaHelper.TranslateLocalAccountName(this.ComputerGroupName);
                    pswaDestinationType = PswaDestinationType.ComputerGroup;
                }
            }
            if (empty == "*")
            {
                pswaDestinationType = PswaDestinationType.All;
            }
            SortedList <int, PswaAuthorizationRule> nums = PswaAuthorizationRuleCommandHelper.LoadFromFile(this, "Add");

            if (nums != null)
            {
                str = null;
                try
                {
                    if (pswaDestinationType == PswaDestinationType.Computer)
                    {
                        string str1 = PswaAuthorizationRuleManager.Instance.TryParseDestinationIpAddress(empty);
                        if (str1 == null)
                        {
                            if (!PswaAuthorizationRuleManager.Instance.IsCurrentComputerDomainJoined())
                            {
                                str = empty;
                            }
                            else
                            {
                                string str2 = null;
                                PswaAuthorizationRuleManager.Instance.GetComputerFqdnAndSid(empty, out str2, out str);
                                if (str != null)
                                {
                                    if (string.Compare(str2, empty, StringComparison.OrdinalIgnoreCase) != 0)
                                    {
                                        if (this.Force)
                                        {
                                            flag = true;
                                        }
                                        else
                                        {
                                            object[] objArray = new object[1];
                                            objArray[0] = str2;
                                            flag        = base.ShouldContinue(string.Format(CultureInfo.CurrentCulture, Resources.AuthorizationRule_UseFqdnQuery, objArray), "");
                                        }
                                        bool flag3 = flag;
                                        if (flag3)
                                        {
                                            empty = str2;
                                        }
                                        else
                                        {
                                            return;
                                        }
                                    }
                                }
                                else
                                {
                                    if (this.Force)
                                    {
                                        flag1 = true;
                                    }
                                    else
                                    {
                                        object[] objArray1 = new object[1];
                                        objArray1[0] = empty;
                                        flag1        = base.ShouldContinue(string.Format(CultureInfo.CurrentCulture, Resources.AuthorizationRule_ForceComputerNameQuery, objArray1), "");
                                    }
                                    bool flag4 = flag1;
                                    if (flag4)
                                    {
                                        str = empty;
                                    }
                                    else
                                    {
                                        return;
                                    }
                                }
                            }
                        }
                        else
                        {
                            empty = str1;
                            if (this.Force)
                            {
                                flag2 = true;
                            }
                            else
                            {
                                object[] objArray2 = new object[1];
                                objArray2[0] = empty;
                                flag2        = base.ShouldContinue(string.Format(CultureInfo.CurrentCulture, Resources.AuthorizationRule_UseIpAddressQuery, objArray2), "");
                            }
                            bool flag5 = flag2;
                            if (flag5)
                            {
                                str = empty;
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    goto Label0;
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    base.WriteError(new ErrorRecord(exception, "AddRuleError", ErrorCategory.InvalidOperation, null));
                }
                return;
            }
            else
            {
                return;
            }
Label0:
            ArrayList arrayLists = new ArrayList();

            string[] strArrays = userName;
            int      num       = 0;

            while (num < (int)strArrays.Length)
            {
                string str3 = strArrays[num];
                try
                {
                    PswaAuthorizationRuleManager            instance = PswaAuthorizationRuleManager.Instance;
                    SortedList <int, PswaAuthorizationRule> nums1    = nums;
                    string ruleName = this.RuleName;
                    string str4     = str3;
                    if (str3 == "*")
                    {
                        pswaUserType = PswaUserType.All;
                    }
                    else
                    {
                        pswaUserType = pswaUserType1;
                    }
                    PswaAuthorizationRule pswaAuthorizationRule = instance.AddRule(nums1, ruleName, str4, pswaUserType, empty, pswaDestinationType, this.ConfigurationName, str);
                    arrayLists.Add(pswaAuthorizationRule);
                    if (arrayLists.Count >= this.batchAmount)
                    {
                        this.SaveCurrentBatch(nums, arrayLists);
                    }
                }
                catch (Exception exception3)
                {
                    Exception exception2 = exception3;
                    this.SaveCurrentBatch(nums, arrayLists);
                    base.WriteError(new ErrorRecord(exception2, "AddRuleError", ErrorCategory.InvalidOperation, null));
                }
                num++;
            }
            if (arrayLists.Count > 0)
            {
                this.SaveCurrentBatch(nums, arrayLists);
                return;
            }
            else
            {
                return;
            }
        }
		private void AddRuleSetDestinationInfo(PswaAuthorizationRule newRule, string destination, string canonicalDestination, PswaDestinationType destinationType)
		{
			bool flag = false;
			newRule.DestinationType = destinationType;
			if (destinationType != PswaDestinationType.Computer)
			{
				if (destinationType != PswaDestinationType.ComputerGroup)
				{
					if (destinationType == PswaDestinationType.All)
					{
						newRule.Destination = "*";
						newRule.DestinationCanonicalForm = "*";
						newRule.IsComputerGroupLocal = false;
					}
					return;
				}
				else
				{
					if (this.CheckSamAccountFormat(destination))
					{
						string str = null;
						string stringSid = this.activeDirectoryHelper.ConvertAccountNameToStringSid(destination, out flag, out str);
						string str1 = null;
						if (this.activeDirectoryHelper.CheckComputerTypeMatch(flag, stringSid, destinationType, str, out str1))
						{
							newRule.Destination = destination;
							newRule.DestinationCanonicalForm = stringSid;
							newRule.IsComputerGroupLocal = flag;
							return;
						}
						else
						{
							throw new ArgumentException(str1);
						}
					}
					else
					{
						throw new ArgumentException(Resources.InvalidUserAndGroupNameFormat);
					}
				}
			}
			else
			{
				newRule.Destination = destination;
				newRule.DestinationCanonicalForm = canonicalDestination;
				newRule.IsComputerGroupLocal = false;
				return;
			}
		}
 public TestRuleInvalidRuleEventArgs(PswaAuthorizationRule rule, Exception exception)
 {
     this.Rule      = rule;
     this.Exception = exception;
 }
        internal PswaAuthorizationRule[] TestRule(PswaAuthorizationRule[] rules, string user, string computer, string configuration, bool returnAllMatches, MatchingWildcard wildcardParts = 0)
        {
            string    stringSid;
            string    str;
            ArrayList arrayLists = new ArrayList();

            PswaAuthorizationRule[] pswaAuthorizationRuleArray = rules;
            if (!returnAllMatches)
            {
                PswaAuthorizationRule pswaAuthorizationRule = this.CheckAllowAllRule(pswaAuthorizationRuleArray);
                if (pswaAuthorizationRule != null)
                {
                    PswaAuthorizationRule[] pswaAuthorizationRuleArray1 = new PswaAuthorizationRule[1];
                    pswaAuthorizationRuleArray1[0] = pswaAuthorizationRule;
                    return(pswaAuthorizationRuleArray1);
                }
            }
            string str1 = null;
            bool   flag = false;

            if (wildcardParts.HasFlag(MatchingWildcard.User))
            {
                stringSid = "*";
            }
            else
            {
                stringSid = this.activeDirectoryHelper.ConvertAccountNameToStringSid(user, out flag, out str1);
            }
            string str2       = stringSid;
            bool   flag1      = true;
            string stringSid1 = computer;

            if (!wildcardParts.HasFlag(MatchingWildcard.Destination))
            {
                string str3 = this.TryParseDestinationIpAddress(stringSid1);
                if (str3 == null)
                {
                    if (this.activeDirectoryHelper.IsCurrentComputerDomainJoined())
                    {
                        try
                        {
                            string str4 = this.activeDirectoryHelper.ConvertComputerName(computer, false);
                            stringSid1 = this.activeDirectoryHelper.ConvertAccountNameToStringSid(str4, out flag1, out str1);
                        }
                        catch (Exception exception)
                        {
                        }
                    }
                }
                else
                {
                    stringSid1 = str3;
                }
            }
            if (string.IsNullOrEmpty(configuration))
            {
                str = "Microsoft.PowerShell";
            }
            else
            {
                str = configuration;
            }
            string    str5        = str;
            ArrayList arrayLists1 = new ArrayList();
            ArrayList arrayLists2 = new ArrayList();
            ArrayList arrayLists3 = new ArrayList();

            PswaAuthorizationRule[] pswaAuthorizationRuleArray2 = pswaAuthorizationRuleArray;
            for (int i = 0; i < (int)pswaAuthorizationRuleArray2.Length; i++)
            {
                PswaAuthorizationRule pswaAuthorizationRule1 = pswaAuthorizationRuleArray2[i];
                if (pswaAuthorizationRule1.UserType == PswaUserType.UserGroup || pswaAuthorizationRule1.DestinationType == PswaDestinationType.ComputerGroup)
                {
                    if (pswaAuthorizationRule1.IsUserGroupLocal || pswaAuthorizationRule1.IsComputerGroupLocal)
                    {
                        arrayLists3.Add(pswaAuthorizationRule1);
                    }
                    else
                    {
                        arrayLists1.Add(pswaAuthorizationRule1);
                    }
                }
                else
                {
                    arrayLists2.Add(pswaAuthorizationRule1);
                }
            }
            PswaAuthorizationRule[] pswaAuthorizationRuleArray3 = this.TestNonGroupRule(arrayLists2, str2, stringSid1, str5, wildcardParts);
            if (!returnAllMatches)
            {
                if ((int)pswaAuthorizationRuleArray3.Length > 0)
                {
                    return(pswaAuthorizationRuleArray3);
                }
            }
            else
            {
                arrayLists.AddRange(pswaAuthorizationRuleArray3);
            }
            List <string> strs = new List <string>();
            List <string> accountDomainGroupSid = new List <string>();

            try
            {
                if (!flag && !wildcardParts.HasFlag(MatchingWildcard.User))
                {
                    strs = this.activeDirectoryHelper.GetAccountDomainGroupSid(str2);
                }
                if (!flag1 && !wildcardParts.HasFlag(MatchingWildcard.Destination))
                {
                    accountDomainGroupSid = this.activeDirectoryHelper.GetAccountDomainGroupSid(stringSid1);
                }
            }
            catch (ArgumentException argumentException)
            {
            }
            PswaAuthorizationRule[] pswaAuthorizationRuleArray4 = this.TestDomainGroupRule(arrayLists1, str2, stringSid1, str5, strs, accountDomainGroupSid, wildcardParts);
            if (!returnAllMatches)
            {
                if ((int)pswaAuthorizationRuleArray4.Length > 0)
                {
                    return(pswaAuthorizationRuleArray4);
                }
            }
            else
            {
                arrayLists.AddRange(pswaAuthorizationRuleArray4);
            }
            PswaAuthorizationRule[] pswaAuthorizationRuleArray5 = this.TestLocalGroupRule(arrayLists3, str2, stringSid1, str5, strs, accountDomainGroupSid, returnAllMatches, wildcardParts);
            if (!returnAllMatches)
            {
                if ((int)pswaAuthorizationRuleArray5.Length > 0)
                {
                    return(pswaAuthorizationRuleArray5);
                }
            }
            else
            {
                arrayLists.AddRange(pswaAuthorizationRuleArray5);
            }
            return((PswaAuthorizationRule[])arrayLists.ToArray(typeof(PswaAuthorizationRule)));
        }
		private SortedList<int, PswaAuthorizationRule> ParseRuleList(XmlDocument doc, ArrayList errorMessageReport)
		{
			SortedList<int, PswaAuthorizationRule> nums;
			XmlNodeList elementsByTagName = doc.GetElementsByTagName("Rule");
			SortedList<int, PswaAuthorizationRule> nums1 = new SortedList<int, PswaAuthorizationRule>();
			try
			{
				foreach (XmlElement xmlElement in elementsByTagName)
				{
					PswaAuthorizationRule pswaAuthorizationRule = new PswaAuthorizationRule();
					pswaAuthorizationRule.DeserializeFromXmlElement(xmlElement);
					int id = pswaAuthorizationRule.Id;
					nums1.Add(id, pswaAuthorizationRule);
				}
				return nums1;
			}
			catch (Exception exception1)
			{
				Exception exception = exception1;
				errorMessageReport.Add(new DataFileLoadError(DataFileLoadError.ErrorStatus.Error, exception));
				nums = null;
			}
			return nums;
		}
		private void OnTestRuleRuleMatch(PswaAuthorizationRule rule)
		{
			EventHandler<TestRuleRuleMatchEventArgs> eventHandler = this.TestRuleRuleMatch;
			if (eventHandler != null)
			{
				eventHandler(this, new TestRuleRuleMatchEventArgs(rule));
			}
		}
		private void OnTestRuleInvalidRule(PswaAuthorizationRule rule, Exception exception)
		{
			EventHandler<TestRuleInvalidRuleEventArgs> testRuleInvalidRule = this.TestRuleInvalidRule;
			if (testRuleInvalidRule != null)
			{
				testRuleInvalidRule(this, new TestRuleInvalidRuleEventArgs(rule, exception));
			}
		}
		private PswaAuthorizationRule CheckAllowAllRule(PswaAuthorizationRule[] rules)
		{
			PswaAuthorizationRule[] pswaAuthorizationRuleArray = rules;
			int num = 0;
			while (num < (int)pswaAuthorizationRuleArray.Length)
			{
				PswaAuthorizationRule pswaAuthorizationRule = pswaAuthorizationRuleArray[num];
				if (!(pswaAuthorizationRule.UserCanonicalForm == "*") || !(pswaAuthorizationRule.DestinationCanonicalForm == "*") || !(pswaAuthorizationRule.ConfigurationName == "*"))
				{
					num++;
				}
				else
				{
					PswaAuthorizationRule pswaAuthorizationRule1 = pswaAuthorizationRule;
					return pswaAuthorizationRule1;
				}
			}
			return null;
		}
		private void AddRuleSetUserInfo(PswaAuthorizationRule newRule, string user, PswaUserType userType)
		{
			bool flag = false;
			newRule.UserType = userType;
			if (userType == PswaUserType.All)
			{
				newRule.User = "******";
				newRule.UserCanonicalForm = newRule.User;
				newRule.IsUserGroupLocal = false;
				return;
			}
			else
			{
				string str = null;
				if (this.CheckSamAccountFormat(user))
				{
					newRule.User = user;
					string stringSid = this.activeDirectoryHelper.ConvertAccountNameToStringSid(user, out flag, out str);
					newRule.IsUserGroupLocal = flag;
					string str1 = null;
					if (this.activeDirectoryHelper.CheckUserTypeMatch(newRule.IsUserGroupLocal, stringSid, userType, str, out str1))
					{
						newRule.UserCanonicalForm = stringSid;
						return;
					}
					else
					{
						throw new ArgumentException(str1);
					}
				}
				else
				{
					throw new ArgumentException(Resources.InvalidUserAndGroupNameFormat);
				}
			}
		}
		internal PswaAuthorizationRule[] TestRule(PswaAuthorizationRule[] rules, string user, Uri connectionUri, string configuration, bool returnAllMatches, MatchingWildcard wildcardParts = 0)
		{
			return this.TestRule(rules, user, connectionUri.Host, configuration, returnAllMatches, wildcardParts);
		}
		internal PswaAuthorizationRule[] TestRule(PswaAuthorizationRule[] rules, string user, string computer, string configuration, bool returnAllMatches, MatchingWildcard wildcardParts = 0)
		{
			string stringSid;
			string str;
			ArrayList arrayLists = new ArrayList();
			PswaAuthorizationRule[] pswaAuthorizationRuleArray = rules;
			if (!returnAllMatches)
			{
				PswaAuthorizationRule pswaAuthorizationRule = this.CheckAllowAllRule(pswaAuthorizationRuleArray);
				if (pswaAuthorizationRule != null)
				{
					PswaAuthorizationRule[] pswaAuthorizationRuleArray1 = new PswaAuthorizationRule[1];
					pswaAuthorizationRuleArray1[0] = pswaAuthorizationRule;
					return pswaAuthorizationRuleArray1;
				}
			}
			string str1 = null;
			bool flag = false;
			if (wildcardParts.HasFlag(MatchingWildcard.User))
			{
				stringSid = "*";
			}
			else
			{
				stringSid = this.activeDirectoryHelper.ConvertAccountNameToStringSid(user, out flag, out str1);
			}
			string str2 = stringSid;
			bool flag1 = true;
			string stringSid1 = computer;
			if (!wildcardParts.HasFlag(MatchingWildcard.Destination))
			{
				string str3 = this.TryParseDestinationIpAddress(stringSid1);
				if (str3 == null)
				{
					if (this.activeDirectoryHelper.IsCurrentComputerDomainJoined())
					{
						try
						{
							string str4 = this.activeDirectoryHelper.ConvertComputerName(computer, false);
							stringSid1 = this.activeDirectoryHelper.ConvertAccountNameToStringSid(str4, out flag1, out str1);
						}
						catch (Exception exception)
						{
						}
					}
				}
				else
				{
					stringSid1 = str3;
				}
			}
			if (string.IsNullOrEmpty(configuration))
			{
				str = "Microsoft.PowerShell";
			}
			else
			{
				str = configuration;
			}
			string str5 = str;
			ArrayList arrayLists1 = new ArrayList();
			ArrayList arrayLists2 = new ArrayList();
			ArrayList arrayLists3 = new ArrayList();
			PswaAuthorizationRule[] pswaAuthorizationRuleArray2 = pswaAuthorizationRuleArray;
			for (int i = 0; i < (int)pswaAuthorizationRuleArray2.Length; i++)
			{
				PswaAuthorizationRule pswaAuthorizationRule1 = pswaAuthorizationRuleArray2[i];
				if (pswaAuthorizationRule1.UserType == PswaUserType.UserGroup || pswaAuthorizationRule1.DestinationType == PswaDestinationType.ComputerGroup)
				{
					if (pswaAuthorizationRule1.IsUserGroupLocal || pswaAuthorizationRule1.IsComputerGroupLocal)
					{
						arrayLists3.Add(pswaAuthorizationRule1);
					}
					else
					{
						arrayLists1.Add(pswaAuthorizationRule1);
					}
				}
				else
				{
					arrayLists2.Add(pswaAuthorizationRule1);
				}
			}
			PswaAuthorizationRule[] pswaAuthorizationRuleArray3 = this.TestNonGroupRule(arrayLists2, str2, stringSid1, str5, wildcardParts);
			if (!returnAllMatches)
			{
				if ((int)pswaAuthorizationRuleArray3.Length > 0)
				{
					return pswaAuthorizationRuleArray3;
				}
			}
			else
			{
				arrayLists.AddRange(pswaAuthorizationRuleArray3);
			}
			List<string> strs = new List<string>();
			List<string> accountDomainGroupSid = new List<string>();
			try
			{
				if (!flag && !wildcardParts.HasFlag(MatchingWildcard.User))
				{
					strs = this.activeDirectoryHelper.GetAccountDomainGroupSid(str2);
				}
				if (!flag1 && !wildcardParts.HasFlag(MatchingWildcard.Destination))
				{
					accountDomainGroupSid = this.activeDirectoryHelper.GetAccountDomainGroupSid(stringSid1);
				}
			}
			catch (ArgumentException argumentException)
			{
			}
			PswaAuthorizationRule[] pswaAuthorizationRuleArray4 = this.TestDomainGroupRule(arrayLists1, str2, stringSid1, str5, strs, accountDomainGroupSid, wildcardParts);
			if (!returnAllMatches)
			{
				if ((int)pswaAuthorizationRuleArray4.Length > 0)
				{
					return pswaAuthorizationRuleArray4;
				}
			}
			else
			{
				arrayLists.AddRange(pswaAuthorizationRuleArray4);
			}
			PswaAuthorizationRule[] pswaAuthorizationRuleArray5 = this.TestLocalGroupRule(arrayLists3, str2, stringSid1, str5, strs, accountDomainGroupSid, returnAllMatches, wildcardParts);
			if (!returnAllMatches)
			{
				if ((int)pswaAuthorizationRuleArray5.Length > 0)
				{
					return pswaAuthorizationRuleArray5;
				}
			}
			else
			{
				arrayLists.AddRange(pswaAuthorizationRuleArray5);
			}
			return (PswaAuthorizationRule[])arrayLists.ToArray(typeof(PswaAuthorizationRule));
		}
        protected override void ProcessRecord()
        {
            PswaAuthorizationRule[] rule;
            SortedList <int, PswaAuthorizationRule> nums = PswaAuthorizationRuleCommandHelper.LoadFromFile(this, "Remove");

            if (nums != null)
            {
                if (string.Compare(base.ParameterSetName, "ID", StringComparison.OrdinalIgnoreCase) != 0)
                {
                    rule = this.Rule;
                }
                else
                {
                    rule = PswaAuthorizationRuleManager.Instance.GetRule(nums, this.Id);
                }
                ArrayList arrayLists = new ArrayList();
                PswaAuthorizationRule[] pswaAuthorizationRuleArray = rule;
                for (int i = 0; i < (int)pswaAuthorizationRuleArray.Length; i++)
                {
                    PswaAuthorizationRule pswaAuthorizationRule = pswaAuthorizationRuleArray[i];
                    try
                    {
                        if (!this.Force)
                        {
                            object[] ruleName = new object[2];
                            ruleName[0] = pswaAuthorizationRule.RuleName;
                            ruleName[1] = pswaAuthorizationRule.Id;
                            if (!base.ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.AuthorizationRuleIdName_DisplayFormat, ruleName)))
                            {
                                continue;
                            }
                        }
                        PswaAuthorizationRule pswaAuthorizationRule1 = PswaAuthorizationRuleManager.Instance.RemoveRule(nums, pswaAuthorizationRule.Id);
                        if (pswaAuthorizationRule1 != null)
                        {
                            arrayLists.Add(pswaAuthorizationRule1);
                            if (arrayLists.Count >= this.batchAmount)
                            {
                                this.SaveCurrentBatch(nums, arrayLists);
                            }
                        }
                        else
                        {
                            this.ReportRuleNotFoundById(pswaAuthorizationRule.Id);
                        }
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        this.SaveCurrentBatch(nums, arrayLists);
                        base.WriteError(new ErrorRecord(exception, "RemoveRuleError", ErrorCategory.InvalidOperation, null));
                    }
                }
                if (arrayLists.Count > 0)
                {
                    this.SaveCurrentBatch(nums, arrayLists);
                }
                return;
            }
            else
            {
                return;
            }
        }
		public TestRuleRuleMatchEventArgs(PswaAuthorizationRule rule)
		{
			this.Rule = rule;
		}
		public TestRuleInvalidRuleEventArgs(PswaAuthorizationRule rule, Exception exception)
		{
			this.Rule = rule;
			this.Exception = exception;
		}
Example #25
0
 public TestRuleRuleMatchEventArgs(PswaAuthorizationRule rule)
 {
     this.Rule = rule;
 }