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.

293 lines
7.8 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. DWORD DeleteGroupPolicyHistory( HKEY hkRoot );
  4. LONG DeleteMachineUserPolicyHistoryKey(HANDLE hToken);
  5. #define MACHINE_USER_GP_KEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy"
  6. typedef struct _UNICODE_STRING {
  7. USHORT Length;
  8. USHORT MaximumLength;
  9. #ifdef MIDL_PASS
  10. [size_is(MaximumLength / 2), length_is((Length) / 2) ] USHORT * Buffer;
  11. #else // MIDL_PASS
  12. PWSTR Buffer;
  13. #endif // MIDL_PASS
  14. } UNICODE_STRING;
  15. typedef UNICODE_STRING *PUNICODE_STRING;
  16. LONG
  17. RtlConvertSidToUnicodeString(
  18. PUNICODE_STRING UnicodeString,
  19. PSID Sid,
  20. BOOLEAN AllocateDestinationString
  21. );
  22. VOID
  23. RtlFreeUnicodeString(
  24. PUNICODE_STRING UnicodeString
  25. );
  26. BOOL
  27. ProcessGPOs( void* );
  28. void
  29. ReadPerfParams (
  30. DWORD* pIterations,
  31. BOOL* pbDeleteHistory,
  32. BOOL* pbSkipFirstIteration,
  33. BOOL* pbUser,
  34. BOOL* pbMach,
  35. WCHAR* szPath
  36. );
  37. void
  38. MeasurePerf( void* lpGPOInfo, HANDLE hToken, BOOL bMach )
  39. {
  40. LARGE_INTEGER Freq;
  41. LARGE_INTEGER Start, Stop, Total;
  42. HKEY hkRoot;
  43. DWORD Iterations;
  44. BOOL bDeleteHistory;
  45. BOOL bSkipFirstIteration;
  46. BOOL bDoMach;
  47. BOOL bDoUser;
  48. WCHAR szPath[MAX_PATH+1];
  49. ReadPerfParams (
  50. &Iterations,
  51. &bDeleteHistory,
  52. &bSkipFirstIteration,
  53. &bDoUser,
  54. &bDoMach,
  55. szPath
  56. );
  57. if ( ( bDoUser && !bMach ) || ( bDoMach && bMach ) )
  58. {
  59. FILE* file;
  60. if ( bMach )
  61. {
  62. lstrcat( szPath, L"MachPerf.log" );
  63. }
  64. else
  65. {
  66. lstrcat( szPath, L"UserPerf.log" );
  67. }
  68. file = _wfopen( szPath, L"a+" );
  69. if ( file )
  70. {
  71. DWORD n;
  72. fwprintf( file, L"\n" );
  73. if ( RegOpenCurrentUser( KEY_READ, &hkRoot ) == ERROR_SUCCESS )
  74. {
  75. QueryPerformanceFrequency( &Freq );
  76. if ( !Freq.QuadPart )
  77. {
  78. Freq.QuadPart = 1;
  79. }
  80. Total.QuadPart = 0;
  81. fprintf( file, "\nNew measurement: %d iterations\n\n", Iterations );
  82. for ( n = 1; n <= Iterations; n++ )
  83. {
  84. if ( bDeleteHistory )
  85. {
  86. DeleteGroupPolicyHistory( hkRoot );
  87. DeleteMachineUserPolicyHistoryKey(hToken);
  88. }
  89. QueryPerformanceCounter( &Start );
  90. ProcessGPOs(lpGPOInfo);
  91. QueryPerformanceCounter( &Stop );
  92. if ( bSkipFirstIteration && (1 == n) )
  93. {
  94. bSkipFirstIteration = FALSE;
  95. n--;
  96. }
  97. else
  98. {
  99. Total.QuadPart += Stop.QuadPart - Start.QuadPart;
  100. }
  101. fwprintf(
  102. file,
  103. L"%d\t%f\t%f\n",
  104. n,
  105. ((double)Start.QuadPart / (double)Freq.QuadPart) * (double) 1000.0,
  106. ((double)Stop.QuadPart / (double)Freq.QuadPart) * (double) 1000.0
  107. );
  108. }
  109. fprintf(
  110. file,
  111. "Time = %f milliseconds per iteration\n\n",
  112. ((double)Total.QuadPart / (double)Freq.QuadPart) / (double)Iterations * (double) 1000.0
  113. );
  114. RegCloseKey( hkRoot );
  115. }
  116. fclose( file );
  117. }
  118. }
  119. }
  120. DWORD DeleteGroupPolicyHistory( HKEY hkRoot )
  121. {
  122. return RegDelnode( hkRoot, L"Software\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\History" );
  123. }
  124. LONG DeleteMachineUserPolicyHistoryKey(HANDLE hToken)
  125. {
  126. UNICODE_STRING SidString;
  127. BOOL bStatus;
  128. DWORD Size;
  129. UCHAR Buffer[sizeof(TOKEN_USER) + sizeof(SID) + ((SID_MAX_SUB_AUTHORITIES-1) * sizeof(ULONG))];
  130. LONG Status;
  131. HKEY hKeyGP;
  132. HKEY hKeyUserGP;
  133. PTOKEN_USER pTokenUser;
  134. hKeyGP = NULL;
  135. hKeyUserGP = NULL;
  136. Size = sizeof(Buffer);
  137. pTokenUser = (PTOKEN_USER) Buffer;
  138. bStatus = GetTokenInformation(
  139. hToken,
  140. TokenUser,
  141. pTokenUser,
  142. Size,
  143. &Size );
  144. if ( ! bStatus )
  145. {
  146. return GetLastError();
  147. }
  148. Status = RtlConvertSidToUnicodeString(
  149. &SidString,
  150. pTokenUser->User.Sid,
  151. TRUE );
  152. if (ERROR_SUCCESS != Status) {
  153. return Status;
  154. }
  155. Status = RegOpenKeyEx (
  156. HKEY_LOCAL_MACHINE,
  157. MACHINE_USER_GP_KEY,
  158. 0,
  159. KEY_READ,
  160. &hKeyGP);
  161. if (ERROR_SUCCESS != Status) {
  162. goto cleanup;
  163. }
  164. Status = RegOpenKeyEx (
  165. hKeyGP,
  166. SidString.Buffer,
  167. 0,
  168. KEY_ALL_ACCESS,
  169. &hKeyUserGP );
  170. if (ERROR_SUCCESS != Status) {
  171. goto cleanup;
  172. }
  173. Status = RegDelnode(
  174. hKeyUserGP,
  175. L"History");
  176. cleanup:
  177. if (hKeyGP) {
  178. RegCloseKey(hKeyGP);
  179. }
  180. if (hKeyUserGP) {
  181. RegCloseKey(hKeyUserGP);
  182. }
  183. RtlFreeUnicodeString( &SidString );
  184. return Status;
  185. }
  186. void
  187. ReadPerfParams (
  188. DWORD* pIterations,
  189. BOOL* pbDeleteHistory,
  190. BOOL* pbSkipFirstIteration,
  191. BOOL* pbUser,
  192. BOOL* pbMach,
  193. WCHAR* szPath
  194. )
  195. {
  196. HKEY hKey;
  197. *pIterations = 5;
  198. *pbDeleteHistory = TRUE;
  199. *pbSkipFirstIteration = TRUE;
  200. *pbUser = TRUE;
  201. *pbMach = TRUE;
  202. lstrcpy( szPath, L"C:\\" );
  203. if ( RegOpenKeyEx (
  204. HKEY_LOCAL_MACHINE,
  205. L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPPerf",
  206. 0,
  207. KEY_READ,
  208. &hKey
  209. ) == ERROR_SUCCESS )
  210. {
  211. DWORD dwType = 0;
  212. DWORD dwData = 0;
  213. DWORD cb = sizeof( DWORD );
  214. if ( RegQueryValueEx( hKey, L"Iterations", 0, &dwType, (BYTE*)&dwData, &cb ) == ERROR_SUCCESS && dwType == REG_DWORD )
  215. {
  216. *pIterations = dwData;
  217. }
  218. cb = sizeof( DWORD );
  219. if ( RegQueryValueEx( hKey, L"DeleteHistory", 0, &dwType, (BYTE*)&dwData, &cb ) == ERROR_SUCCESS && dwType == REG_DWORD )
  220. {
  221. *pbDeleteHistory = dwData;
  222. }
  223. cb = sizeof( DWORD );
  224. if ( RegQueryValueEx( hKey, L"SkipFirst", 0, &dwType, (BYTE*)&dwData, &cb ) == ERROR_SUCCESS && dwType == REG_DWORD )
  225. {
  226. *pbSkipFirstIteration = dwData;
  227. }
  228. cb = sizeof( DWORD );
  229. if ( RegQueryValueEx( hKey, L"User", 0, &dwType, (BYTE*)&dwData, &cb ) == ERROR_SUCCESS && dwType == REG_DWORD )
  230. {
  231. *pbUser = dwData;
  232. }
  233. cb = sizeof( DWORD );
  234. if ( RegQueryValueEx( hKey, L"Machine", 0, &dwType, (BYTE*)&dwData, &cb ) == ERROR_SUCCESS && dwType == REG_DWORD )
  235. {
  236. *pbMach = dwData;
  237. }
  238. cb = MAX_PATH;
  239. if ( !( RegQueryValueEx( hKey, L"Path", 0, &dwType, (BYTE*)szPath, &cb ) == ERROR_SUCCESS && dwType == REG_SZ ) )
  240. {
  241. lstrcpy( szPath, L"C:\\" );
  242. }
  243. RegCloseKey( hKey );
  244. }
  245. }