예제 #1
0
파일: Utils.cs 프로젝트: modulexcite/pash-1
        internal static int LookupSid(string serverName, NetCred credentials, byte[] sid, out string name, out string domainName, out int accountUsage)
        {
            int    num;
            IntPtr zero = IntPtr.Zero;
            int    num1 = 0;
            int    num2 = 0;

            accountUsage = 0;
            name         = null;
            domainName   = null;
            IntPtr intPtr = IntPtr.Zero;

            try
            {
                zero = Utils.ConvertByteArrayToIntPtr(sid);
                Utils.BeginImpersonation(credentials, out intPtr);
                bool flag           = UnsafeNativeMethods.LookupAccountSid(serverName, zero, null, ref num1, null, ref num2, ref accountUsage);
                int  lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error == 122)
                {
                    StringBuilder stringBuilder  = new StringBuilder(num1);
                    StringBuilder stringBuilder1 = new StringBuilder(num2);
                    flag = UnsafeNativeMethods.LookupAccountSid(serverName, zero, stringBuilder, ref num1, stringBuilder1, ref num2, ref accountUsage);
                    if (flag)
                    {
                        name       = stringBuilder.ToString();
                        domainName = stringBuilder1.ToString();
                        num        = 0;
                    }
                    else
                    {
                        lastWin32Error = Marshal.GetLastWin32Error();
                        num            = lastWin32Error;
                    }
                }
                else
                {
                    num = lastWin32Error;
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
                if (intPtr != IntPtr.Zero)
                {
                    Utils.EndImpersonation(intPtr);
                }
            }
            return(num);
        }
예제 #2
0
        internal static int LookupSid(string serverName, NetCred credentials, byte[] sid, out string name, out string domainName, out int accountUsage)
        {
            IntPtr pSid = IntPtr.Zero;

            int nameLength       = 0;
            int domainNameLength = 0;

            StringBuilder sbName;
            StringBuilder sbDomainName;

            accountUsage = 0;
            name         = null;
            domainName   = null;

            IntPtr hUser = IntPtr.Zero;

            try
            {
                pSid = ConvertByteArrayToIntPtr(sid);

                Utils.BeginImpersonation(credentials, out hUser);

                // hUser could be null if no credentials were specified
                Debug.Assert(hUser != IntPtr.Zero ||
                             (credentials == null || (credentials.UserName == null && credentials.Password == null)));

                bool f = UnsafeNativeMethods.LookupAccountSid(serverName, pSid, null, ref nameLength, null, ref domainNameLength, ref accountUsage);

                int lastErr = Marshal.GetLastWin32Error();
                if (lastErr != 122) // ERROR_INSUFFICIENT_BUFFER
                {
                    GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "LookupSid: LookupAccountSid (1st try) failed, gle=" + lastErr);
                    return(lastErr);
                }

                Debug.Assert(f == false);   // should never succeed, with a 0 buffer size

                Debug.Assert(nameLength > 0);
                Debug.Assert(domainNameLength > 0);

                sbName       = new StringBuilder(nameLength);
                sbDomainName = new StringBuilder(domainNameLength);

                f = UnsafeNativeMethods.LookupAccountSid(serverName, pSid, sbName, ref nameLength, sbDomainName, ref domainNameLength, ref accountUsage);

                if (f == false)
                {
                    lastErr = Marshal.GetLastWin32Error();
                    Debug.Assert(lastErr != 0);

                    GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "LookupSid: LookupAccountSid (2nd try) failed, gle=" + lastErr);
                    return(lastErr);
                }

                name       = sbName.ToString();
                domainName = sbDomainName.ToString();

                return(0);
            }
            finally
            {
                if (pSid != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pSid);
                }

                if (hUser != IntPtr.Zero)
                {
                    Utils.EndImpersonation(hUser);
                }
            }
        }