Exemple #1
0
        internal string GetSddlForm()
        {
            string sidString = Value;

            WellKnownAccount acct = WellKnownAccount.LookupBySid(sidString);

            if (acct == null || acct.SddlForm == null)
            {
                return(sidString);
            }

            return(acct.SddlForm);
        }
Exemple #2
0
        internal string GetSddlForm()
        {
            string result = ToString();

            WellKnownAccount acct = WellKnownAccount.LookupBySid(result);

            if (acct != null && acct.SddlForm != null)
            {
                result = acct.SddlForm;
            }

            return(result);
        }
Exemple #3
0
        private static byte[] ParseSddlForm(string sddlForm)
        {
            string sid = sddlForm;

            // If only 2 characters long, can't be a full SID string - so assume
            // it's an attempted alias.  Do that conversion first.
            if (sddlForm.Length == 2)
            {
                WellKnownAccount acct = WellKnownAccount.LookupBySddlForm(sddlForm);
                if (acct == null)
                {
                    throw new ArgumentException(
                              "Invalid SDDL string - unrecognized account: " + sddlForm,
                              "sddlForm");
                }
                if (!acct.IsAbsolute)
                {
                    throw new NotImplementedException(
                              "Mono unable to convert account to SID: "
                              + (acct.Name != null ? acct.Name : sddlForm));
                }

                sid = acct.Sid;
            }

            string[] elements          = sid.ToUpperInvariant().Split('-');
            int      numSubAuthorities = elements.Length - 3;

            if (elements.Length < 3 || elements[0] != "S" || numSubAuthorities > 15)
            {
                throw new ArgumentException("Value was invalid.");
            }

            if (elements[1] != "1")
            {
                throw new ArgumentException("Only SIDs with revision 1 are supported");
            }

            byte[] buffer = new byte[8 + (numSubAuthorities * 4)];
            buffer[0] = 1;
            buffer[1] = (byte)numSubAuthorities;

            ulong authority;

            if (!TryParseAuthority(elements[2], out authority))
                throw new ArgumentException("Value was invalid."); }
Exemple #4
0
        public bool IsWellKnown(WellKnownSidType type)
        {
            WellKnownAccount acct = WellKnownAccount.LookupByType(type);

            if (acct == null)
            {
                return(false);
            }

            string sid = Value;

            if (acct.IsAbsolute)
            {
                return(sid == acct.Sid);
            }

            return(sid.StartsWith("S-1-5-21", StringComparison.OrdinalIgnoreCase) &&
                   sid.EndsWith("-" + acct.Rid, StringComparison.OrdinalIgnoreCase));
        }
Exemple #5
0
        public override IdentityReference Translate(Type targetType)
        {
            if (targetType == typeof(NTAccount))
            {
                return(this);                // ? copy
            }
            if (targetType == typeof(SecurityIdentifier))
            {
                WellKnownAccount acct = WellKnownAccount.LookupByName(this.Value);
                if (acct == null || acct.Sid == null)
                {
                    throw new IdentityNotMappedException("Cannot map account name: " + this.Value);
                }

                return(new SecurityIdentifier(acct.Sid));
            }

            throw new ArgumentException("Unknown type", "targetType");
        }
Exemple #6
0
        public override IdentityReference Translate(Type targetType)
        {
            if (targetType == typeof(SecurityIdentifier))
            {
                return(this);
            }

            if (targetType == typeof(NTAccount))
            {
                WellKnownAccount acct = WellKnownAccount.LookupBySid(this.Value);
                if (acct == null || acct.Name == null)
                {
                    throw new IdentityNotMappedException("Unable to map SID: " + this.Value);
                }

                return(new NTAccount(acct.Name));
            }

            throw new ArgumentException("Unknown type.", "targetType");
        }
Exemple #7
0
        public SecurityIdentifier(WellKnownSidType sidType,
                                  SecurityIdentifier domainSid)
        {
            WellKnownAccount acct = WellKnownAccount.LookupByType(sidType);

            if (acct == null)
            {
                throw new ArgumentException("Unable to convert SID type: " + sidType);
            }

            if (acct.IsAbsolute)
            {
                buffer = ParseSddlForm(acct.Sid);
            }
            else
            {
                if (domainSid == null)
                {
                    throw new ArgumentNullException("domainSid");
                }

                buffer = ParseSddlForm(domainSid.Value + "-" + acct.Rid);
            }
        }
Exemple #8
0
        private static byte[] ParseSddlForm(string sddlForm)
        {
            string sid = sddlForm;

            // If only 2 characters long, can't be a full SID string - so assume
            // it's an attempted alias.  Do that conversion first.
            if (sddlForm.Length == 2)
            {
                WellKnownAccount acct = WellKnownAccount.LookupBySddlForm(sddlForm);
                if (acct == null)
                {
                    throw new ArgumentException(
                              "Invalid SDDL string - unrecognized account: " + sddlForm,
                              "sddlForm");
                }
                if (!acct.IsAbsolute)
                {
                    throw new NotImplementedException(
                              "Mono unable to convert account to SID: "
                              + (acct.Name != null ? acct.Name : sddlForm));
                }

                sid = acct.Sid;
            }

            string[] elements          = sid.ToUpperInvariant().Split('-');
            int      numSubAuthorities = elements.Length - 3;

            if (elements.Length < 3 || elements[0] != "S" || numSubAuthorities > 15)
            {
                throw new ArgumentException("Value was invalid.");
            }

            if (elements[1] != "1")
            {
                throw new ArgumentException("Only SIDs with revision 1 are supported");
            }

            byte[] buffer = new byte[8 + (numSubAuthorities * 4)];
            buffer[0] = 1;
            buffer[1] = (byte)numSubAuthorities;

            ulong authority;

            if (!TryParseAuthority(elements[2], out authority))
            {
                throw new ArgumentException("Value was invalid.");
            }
            buffer[2] = (byte)((authority >> 40) & 0xFF);
            buffer[3] = (byte)((authority >> 32) & 0xFF);
            buffer[4] = (byte)((authority >> 24) & 0xFF);
            buffer[5] = (byte)((authority >> 16) & 0xFF);
            buffer[6] = (byte)((authority >> 8) & 0xFF);
            buffer[7] = (byte)((authority >> 0) & 0xFF);

            for (int i = 0; i < numSubAuthorities; ++i)
            {
                uint subAuthority;

                if (!TryParseSubAuthority(elements[i + 3],
                                          out subAuthority))
                {
                    throw new ArgumentException("Value was invalid.");
                }

                // Note sub authorities little-endian!
                int offset = 8 + (i * 4);
                buffer[offset + 0] = (byte)(subAuthority >> 0);
                buffer[offset + 1] = (byte)(subAuthority >> 8);
                buffer[offset + 2] = (byte)(subAuthority >> 16);
                buffer[offset + 3] = (byte)(subAuthority >> 24);
            }

            return(buffer);
        }