mirror of
				https://github.com/winfsp/winfsp.git
				synced 2025-10-30 19:48:38 -05:00 
			
		
		
		
	tst: passthrough-dotnet: EnableBackupRestorePrivileges
This commit is contained in:
		| @@ -771,7 +771,7 @@ namespace passthrough | |||||||
|                 if (null == PassThrough || null == MountPoint) |                 if (null == PassThrough || null == MountPoint) | ||||||
|                     throw new CommandLineUsageException(); |                     throw new CommandLineUsageException(); | ||||||
|  |  | ||||||
|                 //EnableBackupRestorePrivileges(); |                 EnableBackupRestorePrivileges(); | ||||||
|  |  | ||||||
|                 if (null != DebugLogFile) |                 if (null != DebugLogFile) | ||||||
|                     if (0 > FileSystem.SetDebugLogFile(DebugLogFile)) |                     if (0 > FileSystem.SetDebugLogFile(DebugLogFile)) | ||||||
| @@ -840,43 +840,73 @@ namespace passthrough | |||||||
|                 throw new CommandLineUsageException(); |                 throw new CommandLineUsageException(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
| #if false |  | ||||||
|         /* |         /* | ||||||
|          * Turns out there is no managed way to adjust privileges. |          * Turns out there is no managed way to adjust privileges. | ||||||
|          * So we have to write our own using P/Invoke. Joy! |          * So we have to write our own using P/Invoke. Joy! | ||||||
|  |          * | ||||||
|  |          * NOTE: The P/Invoke definitions below are not for general use. | ||||||
|          */ |          */ | ||||||
|         static NTSTATUS EnableBackupRestorePrivileges(VOID) |         private struct LUID_AND_ATTRIBUTES | ||||||
|         { |         { | ||||||
|             union |             public UInt64 Luid; | ||||||
|  |             public UInt32 Attributes; | ||||||
|  |         } | ||||||
|  |         [StructLayout(LayoutKind.Sequential)] | ||||||
|  |         private struct TOKEN_PRIVILEGES | ||||||
|         { |         { | ||||||
|                 TOKEN_PRIVILEGES P; |             public UInt32 PrivilegeCount; | ||||||
|                 UINT8 B[sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES)]; |             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public LUID_AND_ATTRIBUTES[] Privileges; | ||||||
|             } Privileges; |         } | ||||||
|             HANDLE Token; |         [DllImport("advapi32.dll", SetLastError = true)] | ||||||
|  |         private static extern Boolean OpenProcessToken( | ||||||
|  |             IntPtr ProcessHandle, | ||||||
|  |             UInt32 DesiredAccess, | ||||||
|  |             out IntPtr TokenHandle); | ||||||
|  |         [DllImport("kernel32.dll", SetLastError = true)] | ||||||
|  |         private static extern Boolean CloseHandle( | ||||||
|  |             IntPtr hObject); | ||||||
|  |         [DllImport("advapi32.dll", SetLastError = true)] | ||||||
|  |         private static extern Boolean LookupPrivilegeValueW( | ||||||
|  |             [MarshalAs(UnmanagedType.LPWStr)] String lpSystemName, | ||||||
|  |             [MarshalAs(UnmanagedType.LPWStr)] String lpName, | ||||||
|  |             out UInt64 lpLuid); | ||||||
|  |         [DllImport("advapi32.dll", SetLastError = true)] | ||||||
|  |         private static extern Boolean AdjustTokenPrivileges( | ||||||
|  |             IntPtr TokenHandle, | ||||||
|  |             Boolean DisableAllPrivileges, | ||||||
|  |             ref TOKEN_PRIVILEGES NewState, | ||||||
|  |             UInt32 BufferLength, | ||||||
|  |             IntPtr PreviousState, | ||||||
|  |             IntPtr ReturnLength); | ||||||
|  |         private static Int32 EnableBackupRestorePrivileges() | ||||||
|  |         { | ||||||
|  |             TOKEN_PRIVILEGES Privileges; | ||||||
|  |             IntPtr Token; | ||||||
|  |  | ||||||
|             Privileges.P.PrivilegeCount = 2; |             Privileges = default(TOKEN_PRIVILEGES); | ||||||
|             Privileges.P.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |             Privileges.PrivilegeCount = 2; | ||||||
|             Privileges.P.Privileges[1].Attributes = SE_PRIVILEGE_ENABLED; |             Privileges.Privileges = new LUID_AND_ATTRIBUTES[2]; | ||||||
|  |             Privileges.Privileges[0].Attributes = 2/*SE_PRIVILEGE_ENABLED*/; | ||||||
|  |             Privileges.Privileges[1].Attributes = 2/*SE_PRIVILEGE_ENABLED*/; | ||||||
|  |  | ||||||
|             if (!LookupPrivilegeValueW(0, SE_BACKUP_NAME, &Privileges.P.Privileges[0].Luid) || |             if (!LookupPrivilegeValueW(null, "SeBackupPrivilege", out Privileges.Privileges[0].Luid) || | ||||||
|                 !LookupPrivilegeValueW(0, SE_RESTORE_NAME, &Privileges.P.Privileges[1].Luid)) |                 !LookupPrivilegeValueW(null, "SeRestorePrivilege", out Privileges.Privileges[1].Luid)) | ||||||
|                 return NtStatusFromWin32(GetLastError()); |                 return Marshal.GetLastWin32Error(); | ||||||
|  |  | ||||||
|             if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &Token)) |             if (!OpenProcessToken((IntPtr)(-1), 0x0020/*TOKEN_ADJUST_PRIVILEGES*/, out Token)) | ||||||
|                 return NtStatusFromWin32(GetLastError()); |                 return Marshal.GetLastWin32Error(); | ||||||
|  |  | ||||||
|             if (!AdjustTokenPrivileges(Token, FALSE, &Privileges.P, 0, 0, 0)) |             if (!AdjustTokenPrivileges(Token, false, ref Privileges, 0, IntPtr.Zero, IntPtr.Zero)) | ||||||
|             { |             { | ||||||
|                 CloseHandle(Token); |                 CloseHandle(Token); | ||||||
|  |  | ||||||
|                 return NtStatusFromWin32(GetLastError()); |                 return Marshal.GetLastWin32Error(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             CloseHandle(Token); |             CloseHandle(Token); | ||||||
|  |  | ||||||
|             return STATUS_SUCCESS; |             return 0; | ||||||
|         } |         } | ||||||
| #endif |  | ||||||
|  |  | ||||||
|         private Ptfs _Ptfs; |         private Ptfs _Ptfs; | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user