Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.8 KiB

  1. //#pragma title( "BkupRstr.cpp - Get backup and restore privileges" )
  2. /*
  3. Copyright (c) 1995-1998, Mission Critical Software, Inc. All rights reserved.
  4. ===============================================================================
  5. Module - BkupRstr.cpp
  6. System - Common
  7. Author - Rich Denham
  8. Created - 1997-05-30
  9. Description - Get backup and restore privileges
  10. Updates -
  11. ===============================================================================
  12. */
  13. #include <stdio.h>
  14. #include <windows.h>
  15. #include <lm.h>
  16. #include "Common.hpp"
  17. #include "UString.hpp"
  18. #include "BkupRstr.hpp"
  19. // Get backup and restore privileges using WCHAR machine name.
  20. BOOL // ret-TRUE if successful.
  21. GetBkupRstrPriv(
  22. WCHAR const * sMachineW // in -NULL or machine name
  23. )
  24. {
  25. BOOL bRc=FALSE; // boolean return code.
  26. HANDLE hToken=INVALID_HANDLE_VALUE; // process token.
  27. DWORD rcOs, rcOs2; // OS return code.
  28. WKSTA_INFO_100 * pWkstaInfo; // Workstation info
  29. struct
  30. {
  31. TOKEN_PRIVILEGES tkp; // token privileges.
  32. LUID_AND_ATTRIBUTES x[3]; // room for several.
  33. } token;
  34. rcOs = OpenProcessToken(
  35. GetCurrentProcess(),
  36. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  37. &hToken )
  38. ? 0 : GetLastError();
  39. if ( !rcOs )
  40. {
  41. memset( &token, 0, sizeof token );
  42. bRc = LookupPrivilegeValue(
  43. sMachineW,
  44. SE_BACKUP_NAME,
  45. &token.tkp.Privileges[0].Luid );
  46. if ( bRc )
  47. {
  48. bRc = LookupPrivilegeValue(
  49. sMachineW,
  50. SE_RESTORE_NAME,
  51. &token.tkp.Privileges[1].Luid );
  52. }
  53. if ( !bRc )
  54. {
  55. rcOs = GetLastError();
  56. }
  57. else
  58. {
  59. token.tkp.PrivilegeCount = 2;
  60. token.tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  61. token.tkp.Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
  62. AdjustTokenPrivileges( hToken, FALSE, &token.tkp, 0, NULL, 0 );
  63. rcOs = GetLastError();
  64. }
  65. }
  66. if ( hToken != INVALID_HANDLE_VALUE )
  67. {
  68. CloseHandle( hToken );
  69. hToken = INVALID_HANDLE_VALUE;
  70. }
  71. // If we had any error, try NetWkstaGetInfo.
  72. // If NetWkstaGetInfo fails, then use it's error condition instead.
  73. if ( rcOs )
  74. {
  75. pWkstaInfo = NULL,
  76. rcOs2 = NetWkstaGetInfo(
  77. const_cast<WCHAR *>(sMachineW),
  78. 100,
  79. (BYTE **) &pWkstaInfo );
  80. if ( pWkstaInfo )
  81. {
  82. NetApiBufferFree( pWkstaInfo );
  83. }
  84. if ( rcOs2 )
  85. {
  86. rcOs = rcOs2;
  87. }
  88. }
  89. if ( !rcOs )
  90. {
  91. bRc = TRUE;
  92. }
  93. else
  94. {
  95. SetLastError(rcOs);
  96. bRc = FALSE;
  97. }
  98. return bRc;
  99. }
  100. // BkupRstr.cpp - end of file