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.

254 lines
6.3 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * chglog.cpp
  7. *
  8. * Abstract:
  9. * Tool for enumerating the change log - forward/reverse
  10. *
  11. * Revision History:
  12. * Brijesh Krishnaswami (brijeshk) 04/09/2000
  13. * created
  14. *
  15. *****************************************************************************/
  16. #include <windows.h>
  17. #include <shellapi.h>
  18. #include <snapshot.h>
  19. #include <stdio.h>
  20. // use the _ASSERT and _VERIFY in dbgtrace.h
  21. #ifdef _ASSERT
  22. #undef _ASSERT
  23. #endif
  24. #ifdef _VERIFY
  25. #undef _VERIFY
  26. #endif
  27. #include <dbgtrace.h>
  28. #include <utils.h>
  29. #include <srdefs.h>
  30. CSnapshot g_CSnapshot;
  31. DWORD SetPrivilegeInAccessToken(WCHAR * pszPrivilegeName);
  32. DWORD DeleteAllChangeLogs(WCHAR * pszRestorePointPath)
  33. {
  34. TraceFunctEnter("DeleteAllFilesBySuffix");
  35. DWORD dwErr, dwReturn=ERROR_INTERNAL_ERROR;
  36. WCHAR szFindFileData[MAX_PATH];
  37. // first construct the prefix of the file that stores the HKLM registry
  38. // snapshot.
  39. wsprintf(szFindFileData, L"%s\\%s*", pszRestorePointPath,
  40. s_cszCurrentChangeLog);
  41. dwErr = ProcessGivenFiles(pszRestorePointPath, DeleteGivenFile,
  42. szFindFileData);
  43. if (ERROR_SUCCESS != dwErr)
  44. {
  45. ErrorTrace(0, "Deleting files failed error %ld", dwErr);
  46. dwReturn = dwErr;
  47. goto cleanup;
  48. }
  49. dwReturn = ERROR_SUCCESS;
  50. cleanup:
  51. TraceFunctLeave();
  52. return dwReturn;
  53. }
  54. DWORD SaveRegKey(HKEY hKey, // handle to parent key
  55. WCHAR * pszSubKeyName, // name of subkey to backup
  56. WCHAR * pszFileName, // filename of backup file
  57. WCHAR * pszDirName); // directory to create above file
  58. void __cdecl
  59. main()
  60. {
  61. PWSTR * argv = NULL;
  62. int argc;
  63. WCHAR szSystemHive[MAX_PATH];
  64. DWORD dwReturn;
  65. InitAsyncTrace();
  66. argv = CommandLineToArgvW(GetCommandLine(), &argc);
  67. if (! argv)
  68. {
  69. printf("Error parsing arguments");
  70. goto done;
  71. }
  72. if (argc !=3)
  73. {
  74. printf("Usage: snaputest c|r path");
  75. goto done;
  76. }
  77. if (0==lstrcmp(argv[1],L"c"))
  78. {
  79. _VERIFY(ERROR_SUCCESS==g_CSnapshot.CreateSnapshot(argv[2], NULL,NULL, TRUE));
  80. }
  81. else if (0==lstrcmp(argv[1], L"r" ))
  82. {
  83. dwReturn = g_CSnapshot.InitRestoreSnapshot(argv[2]);
  84. _ASSERT(ERROR_SUCCESS==dwReturn);
  85. dwReturn = g_CSnapshot.GetSystemHivePath(argv[2],
  86. szSystemHive,
  87. MAX_PATH);
  88. _ASSERT(ERROR_SUCCESS==dwReturn);
  89. dwReturn= DoesFileExist(szSystemHive);
  90. _ASSERT(TRUE==dwReturn);
  91. dwReturn=g_CSnapshot.GetSoftwareHivePath(argv[2],
  92. szSystemHive,
  93. MAX_PATH);
  94. _ASSERT(ERROR_SUCCESS==dwReturn);
  95. dwReturn= DoesFileExist(szSystemHive);
  96. _ASSERT(TRUE==dwReturn);
  97. dwReturn = g_CSnapshot.RestoreSnapshot(argv[2]);
  98. _ASSERT(ERROR_SUCCESS==dwReturn);
  99. }
  100. else if (0==lstrcmp(argv[1], L"d" ))
  101. {
  102. _VERIFY(ERROR_SUCCESS==g_CSnapshot.CleanupAfterRestore(argv[2]));
  103. }
  104. else if (0==lstrcmp(argv[1], L"D" ))
  105. {
  106. _VERIFY(ERROR_SUCCESS==g_CSnapshot.DeleteSnapshot(argv[2]));
  107. }
  108. else
  109. {
  110. printf("Usage: snaputest c|r path");
  111. goto done;
  112. }
  113. done:
  114. #if 0
  115. SetPrivilegeInAccessToken(SE_BACKUP_NAME);
  116. SetPrivilegeInAccessToken(SE_RESTORE_NAME);
  117. CopyFileTimes(L"d:\\a.dll", L"d:\\c.dll");
  118. WCHAR wcsBuffer[MAX_PATH];
  119. if (GetVolumeNameForVolumeMountPoint (L"c:\\", wcsBuffer, MAX_PATH))
  120. {
  121. lstrcat(wcsBuffer, L"temp1\\temp2\\foo.dll");
  122. CreateBaseDirectory(wcsBuffer);
  123. }
  124. #endif
  125. TermAsyncTrace();
  126. return;
  127. }
  128. DWORD SetPrivilegeInAccessToken(WCHAR * pszPrivilegeName)
  129. {
  130. TraceFunctEnter("CSnapshot::SetPrivilegeInAccessToken");
  131. HANDLE hProcess;
  132. HANDLE hAccessToken=NULL;
  133. LUID luidPrivilegeLUID;
  134. TOKEN_PRIVILEGES tpTokenPrivilege;
  135. DWORD dwReturn = ERROR_INTERNAL_ERROR, dwErr;
  136. hProcess = GetCurrentProcess();
  137. if (!hProcess)
  138. {
  139. dwReturn = GetLastError();
  140. ErrorTrace(0, "GetCurrentProcess failed ec=%d", dwReturn);
  141. goto done;
  142. }
  143. if (!OpenProcessToken(hProcess,
  144. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  145. &hAccessToken))
  146. {
  147. dwErr=GetLastError();
  148. ErrorTrace(0, "OpenProcessToken failed ec=%d", dwErr);
  149. if (ERROR_SUCCESS != dwErr)
  150. {
  151. dwReturn = dwErr;
  152. }
  153. goto done;
  154. }
  155. if (!LookupPrivilegeValue(NULL,
  156. pszPrivilegeName,
  157. &luidPrivilegeLUID))
  158. {
  159. dwErr=GetLastError();
  160. ErrorTrace(0, "LookupPrivilegeValue failed ec=%d",dwErr);
  161. if (ERROR_SUCCESS != dwErr)
  162. {
  163. dwReturn = dwErr;
  164. }
  165. goto done;
  166. }
  167. tpTokenPrivilege.PrivilegeCount = 1;
  168. tpTokenPrivilege.Privileges[0].Luid = luidPrivilegeLUID;
  169. tpTokenPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  170. if (!AdjustTokenPrivileges(hAccessToken,
  171. FALSE, // Do not disable all
  172. &tpTokenPrivilege,
  173. sizeof(TOKEN_PRIVILEGES),
  174. NULL, // Ignore previous info
  175. NULL)) // Ignore previous info
  176. {
  177. dwErr=GetLastError();
  178. ErrorTrace(0, "AdjustTokenPrivileges");
  179. if (dwErr != NO_ERROR)
  180. {
  181. dwReturn = dwErr;
  182. }
  183. goto done;
  184. }
  185. dwReturn = ERROR_SUCCESS;
  186. done:
  187. if (hAccessToken != NULL)
  188. {
  189. _VERIFY(TRUE==CloseHandle(hAccessToken));
  190. }
  191. TraceFunctLeave();
  192. return dwReturn;
  193. }