Example #1
0
        // Consider, V2, jruiz: implement this. How do you set the working directory
        // for a service?
        //public string InitialWorkingDirectory {
        //    get {
        //    }
        //    set {
        //    }
        //}


        /// <include file='doc\ServiceProcessInstaller.uex' path='docs/doc[@for="ServiceProcessInstaller.AccountHasRight"]/*' />
        /// <devdoc>
        /// Enumerates through the rights of the given account and checks whether the given right
        /// is in the list.
        /// </devdoc>
        private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName)
        {
            IntPtr pRights     = (IntPtr)0;
            int    rightsCount = 0;

            // This function gives us back a pointer to the start of an array of LSA_UNICODE_STRING structs (in pRights).
            int status = NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out pRights, out rightsCount);

            if (status == NativeMethods.STATUS_OBJECT_NAME_NOT_FOUND)
            {
                // this means that the accountSid has no specific rights
                return(false);
            }

            if (status != 0)
            {
                throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(status));
            }

            bool found = false;

            try {
                // look through the rights and see if the desired one is present.
                IntPtr pCurRights = pRights;
                for (int i = 0; i < rightsCount; i++)
                {
                    // Get this element in the array they gave us
                    NativeMethods.LSA_UNICODE_STRING_withPointer uStr = new NativeMethods.LSA_UNICODE_STRING_withPointer();
                    Marshal.PtrToStructure(pCurRights, uStr); // copy the buffer portion to an array & create a string from that
                    char[] rightChars = new char[uStr.length];
                    Marshal.Copy(uStr.pwstr, rightChars, 0, rightChars.Length);
                    string right = new string(rightChars, 0, rightChars.Length);
                    // see if this is the one we're looking for
                    if (string.Compare(right, rightName, false, CultureInfo.InvariantCulture) == 0)
                    {
                        found = true;
                        break;
                    }
                    // move to the next element in the array
                    pCurRights = (IntPtr)((long)pCurRights + Marshal.SizeOf(typeof(NativeMethods.LSA_UNICODE_STRING)));
                }
            }
            finally {
                // make sure we free the memory they allocated for us
                SafeNativeMethods.LsaFreeMemory(pRights);
            }

            return(found);
        }
        private static void RemoveAccountRight(IntPtr policyHandle, byte[] accountSid, string rightName)
        {
            System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING lsa_unicode_string;
            lsa_unicode_string = new System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING {
                buffer        = rightName,
                length        = (short)(lsa_unicode_string.buffer.Length * 2),
                maximumLength = lsa_unicode_string.length
            };
            int ntStatus = System.ServiceProcess.NativeMethods.LsaRemoveAccountRights(policyHandle, accountSid, false, lsa_unicode_string, 1);

            if (ntStatus != 0)
            {
                throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(ntStatus));
            }
        }
        private static void RemoveAccountRight(IntPtr policyHandle, byte[] accountSid, string rightName)
        {
            NativeMethods.LSA_UNICODE_STRING lSA_UNICODE_STRING = new NativeMethods.LSA_UNICODE_STRING();
            lSA_UNICODE_STRING.buffer = rightName;
            NativeMethods.LSA_UNICODE_STRING lSA_UNICODE_STRING2 = lSA_UNICODE_STRING;
            lSA_UNICODE_STRING2.length = (short)(lSA_UNICODE_STRING2.buffer.Length * 2);
            NativeMethods.LSA_UNICODE_STRING lSA_UNICODE_STRING3 = lSA_UNICODE_STRING;
            lSA_UNICODE_STRING3.maximumLength = lSA_UNICODE_STRING3.length;
            int num = NativeMethods.LsaRemoveAccountRights(policyHandle, accountSid, false, lSA_UNICODE_STRING, 1);

            if (num == 0)
            {
                return;
            }
            throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(num));
        }
        private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName)
        {
            IntPtr intPtr = (IntPtr)0;
            int    num    = 0;
            int    num2   = NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out intPtr, out num);

            switch (num2)
            {
            case -1073741772:
                return(false);

            default:
                throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(num2));

            case 0:
            {
                bool result = false;
                try
                {
                    IntPtr intPtr2 = intPtr;
                    for (int i = 0; i < num; i++)
                    {
                        NativeMethods.LSA_UNICODE_STRING_withPointer lSA_UNICODE_STRING_withPointer = new NativeMethods.LSA_UNICODE_STRING_withPointer();
                        Marshal.PtrToStructure(intPtr2, (object)lSA_UNICODE_STRING_withPointer);
                        char[] array = new char[lSA_UNICODE_STRING_withPointer.length / 2];
                        Marshal.Copy(lSA_UNICODE_STRING_withPointer.pwstr, array, 0, array.Length);
                        if (string.Compare(new string(array, 0, array.Length), rightName, StringComparison.Ordinal) == 0)
                        {
                            return(true);
                        }
                        intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(typeof(NativeMethods.LSA_UNICODE_STRING)));
                    }
                    return(result);
                }
                finally
                {
                    SafeNativeMethods.LsaFreeMemory(intPtr);
                }
            }
            }
        }
        private IntPtr OpenSecurityPolicy()
        {
            GCHandle gCHandle = GCHandle.Alloc(new NativeMethods.LSA_OBJECT_ATTRIBUTES(), GCHandleType.Pinned);

            try
            {
                int    num = 0;
                IntPtr pointerObjectAttributes = gCHandle.AddrOfPinnedObject();
                IntPtr result = default(IntPtr);
                num = NativeMethods.LsaOpenPolicy((NativeMethods.LSA_UNICODE_STRING)null, pointerObjectAttributes, 2064, out result);
                if (num != 0)
                {
                    throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(num));
                }
                return(result);
            }
            finally
            {
                gCHandle.Free();
            }
        }
Example #6
0
        /// <include file='doc\ServiceProcessInstaller.uex' path='docs/doc[@for="ServiceProcessInstaller.OpenSecurityPolicy"]/*' />
        /// <devdoc>
        /// Returns an LSA handle to the local machine's security policy. Call LsaClose when finished.
        /// </devdoc>
        private IntPtr OpenSecurityPolicy()
        {
            byte[]   attribs       = new byte[33];
            GCHandle attribsHandle = GCHandle.Alloc(attribs, GCHandleType.Pinned);

            try {
                IntPtr policyHandle;
                int    result         = 0;
                IntPtr attribsPointer = attribsHandle.AddrOfPinnedObject();
                result = NativeMethods.LsaOpenPolicy(null, attribsPointer, NativeMethods.POLICY_CREATE_ACCOUNT | NativeMethods.POLICY_LOOKUP_NAMES, out policyHandle);
                if (result != 0)
                {
                    throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(result));
                }

                return(policyHandle);
            }
            finally {
                attribsHandle.Free();
            }
        }
        private static bool AccountHasRight(IntPtr policyHandle, byte[] accountSid, string rightName)
        {
            IntPtr zero        = IntPtr.Zero;
            int    rightsCount = 0;
            int    ntStatus    = System.ServiceProcess.NativeMethods.LsaEnumerateAccountRights(policyHandle, accountSid, out zero, out rightsCount);

            if (ntStatus == -1073741772)
            {
                return(false);
            }
            if (ntStatus != 0)
            {
                throw new Win32Exception(SafeNativeMethods.LsaNtStatusToWinError(ntStatus));
            }
            try
            {
                IntPtr ptr = zero;
                for (int i = 0; i < rightsCount; i++)
                {
                    System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING_withPointer structure = new System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING_withPointer();
                    Marshal.PtrToStructure(ptr, structure);
                    char[] destination = new char[structure.length / 2];
                    Marshal.Copy(structure.pwstr, destination, 0, destination.Length);
                    string strA = new string(destination, 0, destination.Length);
                    if (string.Compare(strA, rightName, StringComparison.Ordinal) == 0)
                    {
                        return(true);
                    }
                    ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(typeof(System.ServiceProcess.NativeMethods.LSA_UNICODE_STRING)));
                }
            }
            finally
            {
                SafeNativeMethods.LsaFreeMemory(zero);
            }
            return(false);
        }