Example #1
0
        public TokenPrivilege(string privilegeName)
        {
            if (privilegeName == null)
            {
                throw new ArgumentNullException("privilegeName");
            }

            this.luid = LuidFromPrivilege(privilegeName);
        }
Example #2
0
        private static Win32.LUID LuidFromPrivilege(string privilege)
        {
            Win32.LUID luid;
            luid.LowPart  = 0;
            luid.HighPart = 0;

            //
            // Look up the privilege LUID inside the cache
            //

            RuntimeHelpers.PrepareConstrainedRegions();

            try
            {
                privilegeLock.AcquireReaderLock(Timeout.Infinite);

                if (luids.Contains(privilege))
                {
                    luid = (Win32.LUID)luids[privilege];

                    privilegeLock.ReleaseReaderLock();
                }
                else
                {
                    privilegeLock.ReleaseReaderLock();

                    if (!NativeMethods.LookupPrivilegeValue(null, privilege, ref luid))
                    {
                        int error = Marshal.GetLastWin32Error();

                        if (error == Win32Error.ERROR_NOT_ENOUGH_MEMORY)
                        {
                            throw new InsufficientMemoryException();
                        }
                        else if (error == Win32Error.ERROR_ACCESS_DENIED)
                        {
                            throw new UnauthorizedAccessException("Caller does not have the rights to look up " +
                                                                  "privilege local unique identifier");
                        }
                        else if (error == Win32Error.ERROR_NO_SUCH_PRIVILEGE)
                        {
                            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                                      "{0} is not a valid privilege name",
                                                                      privilege),
                                                        "privilege");
                        }
                        else
                        {
                            throw new Win32Exception(error);
                        }
                    }

                    privilegeLock.AcquireWriterLock(Timeout.Infinite);
                }
            }
            finally
            {
                if (privilegeLock.IsReaderLockHeld)
                {
                    privilegeLock.ReleaseReaderLock();
                }

                if (privilegeLock.IsWriterLockHeld)
                {
                    if (!luids.Contains(privilege))
                    {
                        luids[privilege] = luid;
                        privileges[luid] = privilege;
                    }

                    privilegeLock.ReleaseWriterLock();
                }
            }

            return(luid);
        }
Example #3
0
 public static extern bool LookupPrivilegeValue(
     string systemName,
     string privilegeName,
     ref Win32.LUID luid);
        private static Win32.LUID LuidFromPrivilege(string privilege)
        {
            Win32.LUID luid;
            luid.LowPart = 0;
            luid.HighPart = 0;

            //
            // Look up the privilege LUID inside the cache
            //

            RuntimeHelpers.PrepareConstrainedRegions();

            try
            {
                privilegeLock.AcquireReaderLock(Timeout.Infinite);

                if (luids.Contains(privilege))
                {
                    luid = (Win32.LUID)luids[privilege];

                    privilegeLock.ReleaseReaderLock();
                }
                else
                {
                    privilegeLock.ReleaseReaderLock();

                    if (!NativeMethods.LookupPrivilegeValue(null, privilege, ref luid))
                    {
                        int error = Marshal.GetLastWin32Error();

                        if (error == Win32Error.ERROR_NOT_ENOUGH_MEMORY)
                        {
                            throw new InsufficientMemoryException();
                        }
                        else if (error == Win32Error.ERROR_ACCESS_DENIED)
                        {
                            throw new UnauthorizedAccessException("Caller does not have the rights to look up " +
                                                                  "privilege local unique identifier");
                        }
                        else if (error == Win32Error.ERROR_NO_SUCH_PRIVILEGE)
                        {
                            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                                      "{0} is not a valid privilege name",
                                                                      privilege),
                                                        "privilege");
                        }
                        else
                        {
                            throw new Win32Exception(error);
                        }
                    }

                    privilegeLock.AcquireWriterLock(Timeout.Infinite);
                }
            }
            finally
            {
                if (privilegeLock.IsReaderLockHeld)
                {
                    privilegeLock.ReleaseReaderLock();
                }

                if (privilegeLock.IsWriterLockHeld)
                {
                    if (!luids.Contains(privilege))
                    {
                        luids[privilege] = luid;
                        privileges[luid] = privilege;
                    }

                    privilegeLock.ReleaseWriterLock();
                }
            }

            return luid;
        }
        public TokenPrivilege(string privilegeName)
        {
            if (privilegeName == null)
            {
                throw new ArgumentNullException("privilegeName");
            }

            this.luid = LuidFromPrivilege(privilegeName);
        }