Leaked source code of windows server 2003
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.

204 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. asrsfgen.cpp
  5. Abstract:
  6. Utility program to generate an ASR state-file (asr.sif)
  7. Author:
  8. Guhan Suriyanarayanan (guhans) 10-Jul-2000
  9. Environment:
  10. User-mode only.
  11. Revision History:
  12. 10-Jul-2000 guhans
  13. Initial creation
  14. --*/
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <stdio.h>
  19. #include <windows.h>
  20. #include <winasr.h>
  21. #include "critdrv.h"
  22. #include "log.h"
  23. BOOL
  24. pAcquirePrivilege(
  25. IN CONST PCWSTR szPrivilegeName
  26. )
  27. {
  28. HANDLE hToken = NULL;
  29. BOOL bResult = FALSE;
  30. LUID luid;
  31. TOKEN_PRIVILEGES tNewState;
  32. bResult = OpenProcessToken(GetCurrentProcess(),
  33. MAXIMUM_ALLOWED,
  34. &hToken
  35. );
  36. if (!bResult) {
  37. return FALSE;
  38. }
  39. bResult = LookupPrivilegeValue(NULL, szPrivilegeName, &luid);
  40. if (!bResult) {
  41. CloseHandle(hToken);
  42. return FALSE;
  43. }
  44. tNewState.PrivilegeCount = 1;
  45. tNewState.Privileges[0].Luid = luid;
  46. tNewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  47. //
  48. // We will always call GetLastError below, so clear
  49. // any prior error values on this thread.
  50. //
  51. SetLastError(ERROR_SUCCESS);
  52. bResult = AdjustTokenPrivileges(
  53. hToken, // Token Handle
  54. FALSE, // DisableAllPrivileges
  55. &tNewState, // NewState
  56. (DWORD) 0, // BufferLength
  57. NULL, // PreviousState
  58. NULL // ReturnLength
  59. );
  60. //
  61. // Supposedly, AdjustTokenPriveleges always returns TRUE
  62. // (even when it fails). So, call GetLastError to be
  63. // extra sure everything's cool.
  64. //
  65. if (ERROR_SUCCESS != GetLastError()) {
  66. bResult = FALSE;
  67. }
  68. if (!bResult) {
  69. AsrpPrintDbgMsg(s_Warning, "AdjustTokenPrivileges for %ws failed (%lu)",
  70. szPrivilegeName,
  71. GetLastError()
  72. );
  73. }
  74. CloseHandle(hToken);
  75. return bResult;
  76. }
  77. int __cdecl
  78. wmain(
  79. int argc,
  80. WCHAR *argv[],
  81. WCHAR *envp[]
  82. )
  83. /*++
  84. Routine Description:
  85. Entry point to asrsfgen.exe. Generates an asr.sif file using the ASR API.
  86. Takes an optional command-line parameter to specify the location where the
  87. asr.sif is to be generated. The default location is
  88. %systemroot%\repair\asr.sif.
  89. Arguments:
  90. argc - Number of command-line parameters used to invoke the app
  91. argv - The command-line parameters as an array of strings
  92. envp - The process environment block, not currently used
  93. Return Values:
  94. If the function succeeds, the exit code is zero.
  95. If the function fails, the exit code is a win-32 error code.
  96. --*/
  97. {
  98. DWORD_PTR asrContext = 0;
  99. LPWSTR szCriticalVolumes = NULL;
  100. BOOL bResult = FALSE;
  101. int iReturn = 0;
  102. AsrpInitialiseLogFiles();
  103. AsrpPrintDbgMsg(s_Info, "Creating ASR state file at %ws",
  104. (argc > 1 ? argv[1] : L"default location (%systemroot%\\repair\\asr.sif)")
  105. );
  106. //
  107. // We need to acquire the backup privileges to create asr.sif
  108. //
  109. if (!pAcquirePrivilege(SE_BACKUP_NAME)) {
  110. AsrpPrintDbgMsg(s_Error, "Could not get backup privilege (%lu)", GetLastError());
  111. return ERROR_PRIVILEGE_NOT_HELD;
  112. }
  113. //
  114. // Get the critical volume list
  115. //
  116. szCriticalVolumes = pFindCriticalVolumes();
  117. if (!szCriticalVolumes) {
  118. AsrpPrintDbgMsg(s_Warning, "Critical Volume List is NULL");
  119. }
  120. //
  121. // Create the state file
  122. //
  123. bResult = AsrCreateStateFile(
  124. (argc > 1 ? argv[1] : NULL), // sif path
  125. L"ASR Sif Generation Test Application v 0.1", // Provider name
  126. TRUE, // auto-extend
  127. szCriticalVolumes, // list of critical volumes
  128. &asrContext
  129. );
  130. if (!bResult) {
  131. AsrpPrintDbgMsg(s_Error, "Could not create state file (%lu == 0x%x)", GetLastError(), GetLastError());
  132. iReturn = 1;
  133. }
  134. else {
  135. AsrpPrintDbgMsg(s_Info, "ASR state file successfully created");
  136. }
  137. //
  138. // We're done with these, clean them up
  139. //
  140. if (szCriticalVolumes) {
  141. delete szCriticalVolumes;
  142. szCriticalVolumes = NULL;
  143. }
  144. AsrFreeContext(&asrContext);
  145. AsrpCloseLogFiles();
  146. return iReturn;
  147. }