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.

175 lines
3.7 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "regprep.h"
  5. int ProgramStatus = 0;
  6. BOOL
  7. EnableRestorePrivilege(
  8. VOID
  9. );
  10. VOID
  11. PerformRegMods (
  12. HANDLE HiveHandle
  13. );
  14. int
  15. __cdecl
  16. main(
  17. int argc,
  18. char *argv[]
  19. )
  20. {
  21. PCHAR hivePath;
  22. HANDLE hiveHandle;
  23. BOOL result;
  24. if (argc != 2) {
  25. printf("Usage: regprep <hivepath>\n");
  26. exit(-1);
  27. }
  28. result = EnableRestorePrivilege();
  29. if (result == FALSE) {
  30. printf("Could not enable restore privileges\n");
  31. exit(-1);
  32. }
  33. hivePath = argv[1];
  34. hiveHandle = OpenHive(hivePath);
  35. RASSERT(hiveHandle != NULL,"Could not load %s",hivePath);
  36. PerformRegMods(hiveHandle);
  37. CloseHive(hiveHandle);
  38. return ProgramStatus;
  39. }
  40. VOID
  41. PerformRegMods (
  42. HANDLE HiveHandle
  43. )
  44. {
  45. HKEY subKey;
  46. ULONG index;
  47. UCHAR driveLetter;
  48. UCHAR buffer[MAX_PATH];
  49. LONG result;
  50. PUCHAR pch;
  51. printf("Processing registry\n");
  52. //
  53. // Remove the volume names for hard drives from the "MountedDevices"
  54. // hive, i.e.
  55. //
  56. // \DosDevices\C:
  57. // \DosDevices\D:
  58. // ...
  59. //
  60. pch = "MountedDevices";
  61. result = RegOpenKey(HiveHandle,pch,&subKey);
  62. RASSERT(result == ERROR_SUCCESS,"Could not open %s\n",pch);
  63. for (driveLetter = 'C'; driveLetter <= 'Z'; driveLetter++) {
  64. sprintf(buffer,"\\DosDevices\\%c:", driveLetter);
  65. result = RegDeleteValue(subKey,buffer);
  66. }
  67. RegCloseKey(subKey);
  68. //
  69. // Add
  70. //
  71. // CurrentControlSet\Control\Session Manager\KnownDLLs\DllDirectory32
  72. //
  73. index = 1;
  74. while (TRUE) {
  75. sprintf(buffer,
  76. "ControlSet%03d\\Control\\Session Manager\\KnownDLLs",
  77. index);
  78. result = RegOpenKey(HiveHandle,buffer,&subKey);
  79. if (result != ERROR_SUCCESS) {
  80. break;
  81. }
  82. pch = "%SystemRoot%\\SysWow64";
  83. result = RegSetValueEx(subKey,
  84. "DllDirectory32",
  85. 0,
  86. REG_EXPAND_SZ,
  87. pch,
  88. strlen(pch)+1);
  89. RASSERT(result == ERROR_SUCCESS,"Could not set value %s",pch);
  90. RegCloseKey(subKey);
  91. index += 1;
  92. }
  93. printf("Finished.\n");
  94. }
  95. BOOL
  96. EnableRestorePrivilege(
  97. VOID
  98. )
  99. {
  100. BOOL result;
  101. HANDLE hToken;
  102. TOKEN_PRIVILEGES NewPrivileges;
  103. LUID Luid;
  104. //
  105. // Open our process' security token.
  106. //
  107. result = OpenProcessToken(GetCurrentProcess(),
  108. TOKEN_ADJUST_PRIVILEGES,
  109. &hToken);
  110. if (result == FALSE) {
  111. return result;
  112. }
  113. //
  114. // Convert privi name to an LUID.
  115. //
  116. result = LookupPrivilegeValue(NULL,
  117. "SeRestorePrivilege",
  118. &Luid);
  119. if (result == FALSE) {
  120. CloseHandle(hToken);
  121. return FALSE;
  122. }
  123. //
  124. // Construct new data struct to enable / disable the privi.
  125. //
  126. NewPrivileges.PrivilegeCount = 1;
  127. NewPrivileges.Privileges[0].Luid = Luid;
  128. NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  129. //
  130. // Adjust the privileges.
  131. //
  132. result = AdjustTokenPrivileges(hToken,
  133. FALSE,
  134. &NewPrivileges,
  135. 0,
  136. NULL,
  137. NULL);
  138. CloseHandle(hToken);
  139. return result;
  140. }