/// <summary>
        /// Creates a Security Descriptor from an SDDL string
        /// </summary>
        /// <param name="sddl">The SDDL string that represents the Security Descriptor</param>
        /// <returns>The Security Descriptor represented by the SDDL string</returns>
        /// <exception cref="System.FormatException" />
        public static SecurityDescriptor SecurityDescriptorFromString(string sddl)
        {
            var sddlRegex = new Regex(sddlExpr, RegexOptions.IgnoreCase);

            var m = sddlRegex.Match(sddl);

            if (!m.Success)
            {
                throw new FormatException("Invalid SDDL String Format");
            }

            var sd = new SecurityDescriptor();

            if (m.Groups["owner"] != null && m.Groups["owner"].Success && !string.IsNullOrEmpty(m.Groups["owner"].Value))
            {
                sd.Owner = SecurityIdentity.SecurityIdentityFromString(m.Groups["owner"].Value);
            }

            if (m.Groups["group"] != null && m.Groups["group"].Success && !string.IsNullOrEmpty(m.Groups["group"].Value))
            {
                sd.Group = SecurityIdentity.SecurityIdentityFromString(m.Groups["group"].Value);
            }

            if (m.Groups["dacl"] != null && m.Groups["dacl"].Success && !string.IsNullOrEmpty(m.Groups["dacl"].Value))
            {
                sd.DACL = AccessControlList.AccessControlListFromString(m.Groups["dacl"].Value);
            }

            if (m.Groups["sacl"] != null && m.Groups["sacl"].Success && !string.IsNullOrEmpty(m.Groups["sacl"].Value))
            {
                sd.SACL = AccessControlList.AccessControlListFromString(m.Groups["sacl"].Value);
            }

            return(sd);
        }
Esempio n. 2
0
        public static AccessControlEntry AccessControlEntryFromString(string aceString)
        {
            Regex aceRegex = new Regex(aceExpr, RegexOptions.IgnoreCase);

            Match aceMatch = aceRegex.Match(aceString);

            if (!aceMatch.Success)
            {
                throw new FormatException("Invalid ACE String Format");
            }

            AccessControlEntry ace = new AccessControlEntry();

            if (aceMatch.Groups["ace_type"] != null && aceMatch.Groups["ace_type"].Success && !string.IsNullOrEmpty(aceMatch.Groups["ace_type"].Value))
            {
                int aceTypeValue = Array.IndexOf(aceTypeStrings, aceMatch.Groups["ace_type"].Value.ToUpper());

                if (aceTypeValue == -1)
                {
                    throw new FormatException("Invalid ACE String Format");
                }

                ace.aceType = (AceType)aceTypeValue;
            }
            else
            {
                throw new FormatException("Invalid ACE String Format");
            }

            if (aceMatch.Groups["ace_flags"] != null && aceMatch.Groups["ace_flags"].Success && !string.IsNullOrEmpty(aceMatch.Groups["ace_flags"].Value))
            {
                string aceFlagsValue = aceMatch.Groups["ace_flags"].Value.ToUpper();
                for (int i = 0; i < aceFlagsValue.Length - 1; i += 2)
                {
                    int flagValue = Array.IndexOf(aceFlagStrings, aceFlagsValue.Substring(i, 2));

                    if (flagValue == -1)
                    {
                        throw new FormatException("Invalid ACE String Format");
                    }

                    ace.flags = ace.flags | ((AceFlags)(int)Math.Pow(2.0d, flagValue));
                }
            }

            if (aceMatch.Groups["rights"] != null && aceMatch.Groups["rights"].Success && !string.IsNullOrEmpty(aceMatch.Groups["rights"].Value))
            {
                string rightsValue = aceMatch.Groups["rights"].Value.ToUpper();
                for (int i = 0; i < rightsValue.Length - 1; i += 2)
                {
                    int rightValue = Array.IndexOf(rightsStrings, rightsValue.Substring(i, 2));

                    if (rightValue == -1)
                    {
                        throw new FormatException("Invalid ACE String Format");
                    }

                    ace.Add((AceRights)(int)Math.Pow(2.0d, rightValue));
                }
            }

            if (aceMatch.Groups["object_guid"] != null && aceMatch.Groups["object_guid"].Success && !string.IsNullOrEmpty(aceMatch.Groups["object_guid"].Value))
            {
                ace.objectGuid = new Guid(aceMatch.Groups["object_guid"].Value);
            }

            if (aceMatch.Groups["inherit_object_guid"] != null && aceMatch.Groups["inherit_object_guid"].Success && !string.IsNullOrEmpty(aceMatch.Groups["inherit_object_guid"].Value))
            {
                ace.inheritObjectGuid = new Guid(aceMatch.Groups["inherit_object_guid"].Value);
            }

            if (aceMatch.Groups["account_sid"] != null && aceMatch.Groups["account_sid"].Success && !string.IsNullOrEmpty(aceMatch.Groups["account_sid"].Value))
            {
                ace.AccountSID = SecurityIdentity.SecurityIdentityFromString(aceMatch.Groups["account_sid"].Value.ToUpper());
            }
            else
            {
                throw new FormatException("Invalid ACE String Format");
            }

            return(ace);
        }