コード例 #1
0
        /// <summary>
        /// GetServiceSidPtr method implementation
        /// </summary>
        private static IntPtr GetServiceSidPtr(string service)
        {
            NativeMethods.LSA_UNICODE_STRING lSA_UNICODE_STRING = default(NativeMethods.LSA_UNICODE_STRING);
            lSA_UNICODE_STRING.SetTo(service);
            int    cb     = 0;
            IntPtr intPtr = IntPtr.Zero;
            IntPtr result;

            try
            {
                uint num = NativeMethods.RtlCreateServiceSid(ref lSA_UNICODE_STRING, IntPtr.Zero, ref cb);
                if (num == 3221225507u)
                {
                    intPtr = Marshal.AllocHGlobal(cb);
                    num    = NativeMethods.RtlCreateServiceSid(ref lSA_UNICODE_STRING, intPtr, ref cb);
                }
                if (num != 0u)
                {
                    throw new Win32Exception(Convert.ToInt32(num));
                }
                result = intPtr;
            }
            finally
            {
                lSA_UNICODE_STRING.Dispose();
            }
            return(result);
        }
        static ICollection <SecurityIdentifier> GetAvailableCaps(string targetName)
        {
            var  result     = new List <SecurityIdentifier>();
            PSID capIdArray = PSID.Zero;

            try
            {
                var   targetMachine = new NativeMethods.LSA_UNICODE_STRING(targetName);
                ULONG capCount;

                int ntStatus = NativeMethods.LsaGetAppliedCAPIDs(ref targetMachine, ref capIdArray, out capCount);
                if (!Win32.NT_SUCCESS(ntStatus))
                {
                    throw new Win32Exception(NativeMethods.LsaNtStatusToWinError(ntStatus));
                }

                if (capCount != 0 && capIdArray != PSID.Zero)
                {
                    PSID nextSid = capIdArray;
                    while (0 != capCount)
                    {
                        result.Add(new SecurityIdentifier(Marshal.ReadIntPtr(nextSid)));
                        nextSid += Marshal.SizeOf(typeof(IntPtr));
                        --capCount;
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(capIdArray);
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Gets the name of the DNS domain assigned to the local computer
        /// Using Lsa functions
        /// </summary>
        public static string GetPrimaryDnsDomainName(string computerName)
        {
            string dName = string.Empty;

            IntPtr polHandle = IntPtr.Zero;

            NativeMethods.LSA_OBJECT_ATTRIBUTES objAttr = new NativeMethods.LSA_OBJECT_ATTRIBUTES();
            objAttr.Length                   = 0;
            objAttr.RootDirectory            = IntPtr.Zero;
            objAttr.Attributes               = 0;
            objAttr.SecurityDescriptor       = IntPtr.Zero;
            objAttr.SecurityQualityOfService = IntPtr.Zero;

            NativeMethods.LSA_UNICODE_STRING localSysName = new NativeMethods.LSA_UNICODE_STRING();
            localSysName.Buffer        = Marshal.StringToHGlobalUni(computerName);
            localSysName.Length        = (ushort)(computerName.Length * UnicodeEncoding.CharSize);
            localSysName.MaximumLength = localSysName.Length;

            // LsaOpenPolicy
            UInt32 retcode = NativeMethods.LsaOpenPolicy(ref localSysName, ref objAttr,
                                                         (UInt32)(NativeMethods.LsaPolicies.GENERIC_READ |
                                                                  NativeMethods.LsaPolicies.POLICY_VIEW_LOCAL_INFORMATION), out polHandle);
            Int32 win32ErrorCode = NativeMethods.LsaNtStatusToWinError(retcode);

            if (win32ErrorCode == 0)
            {
                NativeMethods.POLICY_INFORMATION_CLASS policyInfo = NativeMethods.POLICY_INFORMATION_CLASS.PolicyPrimaryDomainInformation;
                IntPtr pData = IntPtr.Zero;

                // LsaQueryInformationPolicy
                retcode = NativeMethods.LsaQueryInformationPolicy(polHandle,
                                                                  policyInfo,
                                                                  out pData);
                win32ErrorCode = NativeMethods.LsaNtStatusToWinError(retcode);

                if (win32ErrorCode == 0)
                {
                    NativeMethods.POLICY_PRIMARY_DOMAIN_INFO primaryDomainInfo = (NativeMethods.POLICY_PRIMARY_DOMAIN_INFO)Marshal.PtrToStructure(pData, typeof(NativeMethods.POLICY_PRIMARY_DOMAIN_INFO));
                    dName = Marshal.PtrToStringUni(primaryDomainInfo.DomainName.Buffer);

                    NativeMethods.LsaFreeMemory(pData);
                }

                NativeMethods.LsaClose(polHandle);
            }

            return(dName);
        }
コード例 #4
0
        /// <summary>
        /// GetADFSServiceSID method implmentation
        /// </summary>
        private static string GetADFSServiceSID()
        {
            NativeMethods.LSA_UNICODE_STRING lSA_UNICODE_STRING = default(NativeMethods.LSA_UNICODE_STRING);
            lSA_UNICODE_STRING.SetTo("adfssrv");
            int cb = 0;

            try
            {
                uint num = NativeMethods.RtlCreateServiceSid(ref lSA_UNICODE_STRING, IntPtr.Zero, ref cb);
                if (num == STATUS_BUFFER_TOO_SMALL)
                {
                    IntPtr intPtr = Marshal.AllocHGlobal(cb);
                    try
                    {
                        if (NativeMethods.RtlCreateServiceSid(ref lSA_UNICODE_STRING, intPtr, ref cb) != STATUS_SUCCESS)
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error());
                        }
                        return(new SecurityIdentifier(intPtr).Value);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(intPtr);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteEntry("Error loading ADFS Service SID information : \r" + ex.Message, EventLogEntryType.Error, 666);
                return(string.Empty);
            }
            finally
            {
                lSA_UNICODE_STRING.Dispose();
            }
            return(string.Empty);
        }
コード例 #5
0
 internal static extern uint RtlCreateServiceSid(ref NativeMethods.LSA_UNICODE_STRING serviceName, IntPtr serviceSid, ref int serviceSidLength);
コード例 #6
0
        static ICollection<SecurityIdentifier> GetAvailableCaps(string targetName)
        {
            var result = new List<SecurityIdentifier>();
            PSID capIdArray = PSID.Zero;

            try
            {
                var targetMachine = new NativeMethods.LSA_UNICODE_STRING(targetName);
                ULONG capCount;

                int ntStatus = NativeMethods.LsaGetAppliedCAPIDs(ref targetMachine, ref capIdArray, out capCount);
                if (!Win32.NT_SUCCESS(ntStatus))
                {
                    throw new Win32Exception(NativeMethods.LsaNtStatusToWinError(ntStatus));
                }

                if (capCount != 0 && capIdArray != PSID.Zero)
                {
                    PSID nextSid = capIdArray;
                    while(0 != capCount)
                    {
                        result.Add(new SecurityIdentifier(Marshal.ReadIntPtr(nextSid)));
                        nextSid += Marshal.SizeOf(typeof(IntPtr));
                        --capCount;
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(capIdArray);
            }

            return result;
        }