Пример #1
0
        internal TerminalSessionCollection(TerminalServer server)
            : base(new System.Collections.Generic.List <TerminalSession>())
        {
            int    count = 0;
            IntPtr ptr   = IntPtr.Zero;

            using (WtsServerCookie cookie = server.OpenServer())
            {
                if (!NativeMethods.WTSEnumerateSessions(cookie.Handle, 0, 1, out ptr, out count))
                {
                    throw PscxException.LastWin32Exception();
                }
            }

            try
            {
                foreach (WTS_SESSION_INFO info in Utils.ReadNativeArray <WTS_SESSION_INFO>(ptr, count))
                {
                    Items.Add(new TerminalSession(server, info));
                }
            }
            finally
            {
                NativeMethods.WTSFreeMemory(ptr);
            }
        }
Пример #2
0
        private static void AdjustToken()
        {
            using (Process process = Process.GetCurrentProcess())
            {
                SafeTokenHandle hToken = SafeTokenHandle.InvalidHandle;

                try
                {
                    if (!NativeMethods.OpenProcessToken(process.Handle, TokenAccessLevels.AdjustPrivileges, ref hToken))
                    {
                        throw PscxException.LastWin32Exception();
                    }

                    TokenPrivilegeCollection privileges = new TokenPrivilegeCollection();

                    privileges.Enable(TokenPrivilege.Backup);
                    privileges.Enable(TokenPrivilege.Restore);

                    Utils.AdjustTokenPrivileges(hToken, privileges);
                }
                finally
                {
                    hToken.Dispose();
                }
            }
        }
Пример #3
0
        public unsafe TokenPrivilegeCollection(WindowsIdentity identity)
        {
            int bufferSize = 1024;

            byte[] buffer = new byte[bufferSize];

            using (SafeTokenHandle tokenHandle = new SafeTokenHandle(identity.Token, false))
            {
                if (!TryGetTokenPrivileges(tokenHandle, buffer, out bufferSize))
                {
                    buffer = new byte[bufferSize];

                    if (!TryGetTokenPrivileges(tokenHandle, buffer, out bufferSize))
                    {
                        throw PscxException.LastWin32Exception();
                    }
                }
            }

            fixed(void *ptr = buffer)
            {
                TOKEN_PRIVILEGES *   tp = (TOKEN_PRIVILEGES *)(ptr);
                LUID_AND_ATTRIBUTES *la = &tp->Privileges;

                int remaining = tp->PrivilegeCount;

                while (remaining-- > 0)
                {
                    Add(new TokenPrivilege(*la++));
                }
            }
        }
Пример #4
0
        internal WtsServerCookie(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
            {
                throw PscxException.LastWin32Exception();
            }

            _handle = handle;
        }
Пример #5
0
 public void Logoff(Boolean wait)
 {
     using (WtsServerCookie cookie = _server.OpenServer())
     {
         if (!NativeMethods.WTSLogoffSession(cookie.Handle, _sessionId, wait))
         {
             throw PscxException.LastWin32Exception();
         }
     }
 }
Пример #6
0
 public void Shutdown(WtsShutdownType type)
 {
     using (WtsServerCookie cookie = OpenServer())
     {
         if (!NativeMethods.WTSShutdownSystem(cookie.Handle, (int)(type)))
         {
             throw PscxException.LastWin32Exception();
         }
     }
 }
Пример #7
0
 public void TerminateProcess(int processId, int exitCode)
 {
     using (WtsServerCookie cookie = OpenServer())
     {
         if (!NativeMethods.WTSTerminateProcess(cookie.Handle, processId, exitCode))
         {
             throw PscxException.LastWin32Exception();
         }
     }
 }
Пример #8
0
 public TokenPrivilege(string systemName, string privilege, bool enabled)
 {
     if (NativeMethods.LookupPrivilegeValue(systemName, privilege, ref value.Luid))
     {
         value.Attributes = (enabled ? PrivilegeStatus.Enabled : PrivilegeStatus.Disabled);
     }
     else
     {
         throw PscxException.LastWin32Exception();
     }
 }
Пример #9
0
        private AssemblyCacheType GetCacheType(string path)
        {
            WriteDebug("GetCacheType: " + path);

            if (PSDriveInfo == null)
            {
                // TODO: extract root from provider qualified path, e.g. assemblycache::\\gac\system.web
                throw PscxException.NotImplementedYet("Provider-qualified paths are not yet supported. Please map a new drive to this location.");
            }

            string root = PSDriveInfo.Root;

            return(Utils.ParseEnumOrThrow <AssemblyCacheType>(root));
        }
Пример #10
0
        public unsafe bool TryGetTokenPrivileges(SafeTokenHandle hToken, byte[] bytes, out int bufferSize)
        {
            fixed(void *buffer = bytes)
            {
                if (!UnsafeNativeMethods.GetTokenInformation(hToken,
                                                             TOKEN_INFORMATION_CLASS.TokenPrivileges,
                                                             buffer,
                                                             bytes.Length,
                                                             out bufferSize))
                {
                    if (Marshal.GetLastWin32Error() == NativeMethods.ERROR_INSUFFICIENT_BUFFER)
                    {
                        return(false);
                    }

                    throw PscxException.LastWin32Exception();
                }
            }

            return(true);
        }
Пример #11
0
 void IPscxErrorHandler.WriteLastWin32Error(string errorId, ErrorCategory category, object target)
 {
     ErrorHandler.WriteWin32Error(PscxException.LastWin32Exception(), errorId, category, target);
 }