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.

463 lines
12 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <memory.h>
  5. #include <wchar.h>
  6. #include "windows.h"
  7. #include "winreg.h"
  8. #define TEST_STRING "Test String"
  9. #define TEST_STRING_W L"Test String"
  10. BOOLEAN
  11. AdjustPrivilege(
  12. PSTR SecurityNameString
  13. )
  14. {
  15. HANDLE TokenHandle;
  16. LUID_AND_ATTRIBUTES LuidAndAttributes;
  17. // PSTR SecurityNameString;
  18. TOKEN_PRIVILEGES TokenPrivileges;
  19. TOKEN_PRIVILEGES PreviousTokenPrivileges;
  20. DWORD ReturnLength;
  21. if( !OpenProcessToken( GetCurrentProcess(),
  22. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  23. &TokenHandle ) ) {
  24. printf( "OpenProcessToken failed \n" );
  25. return( FALSE );
  26. }
  27. // SecurityNameString = SE_RESTORE_NAME; // SE_SECURITY_NAME;
  28. if( !LookupPrivilegeValue( NULL,
  29. SecurityNameString,
  30. &( LuidAndAttributes.Luid ) ) ) {
  31. printf( "LookupPrivilegeValue failed, Error = %#x \n", GetLastError() );
  32. return( FALSE );
  33. }
  34. LuidAndAttributes.Attributes = SE_PRIVILEGE_ENABLED;
  35. TokenPrivileges.PrivilegeCount = 1;
  36. TokenPrivileges.Privileges[0] = LuidAndAttributes;
  37. if( !AdjustTokenPrivileges( TokenHandle,
  38. FALSE,
  39. &TokenPrivileges,
  40. 0,
  41. NULL,
  42. NULL ) ) {
  43. printf( "AdjustTokenPrivileges failed, Error = %#x \n", GetLastError() );
  44. return( FALSE );
  45. }
  46. if( GetLastError() != NO_ERROR ) {
  47. return( FALSE );
  48. }
  49. return( TRUE );
  50. }
  51. #define ENVIRONMENT_NAME L"Environment"
  52. #define TESTKEY_NAME L"TestKey"
  53. #define TESTKEY_FULL_NAME L"Environment\\TestKey"
  54. #define KEY1_NAME L"Key1"
  55. #define KEY1_FULL_NAME L"Environment\\TestKey\\Key1"
  56. #define KEY2_NAME L"Key2"
  57. #define KEY2_FULL_NAME L"Environment\\TestKey\\Key2"
  58. #define VALUE_NAME L"123"
  59. #define VALUE_DATA L"This is a string"
  60. INT __cdecl
  61. main()
  62. {
  63. DWORD Status;
  64. HKEY TestKeyHandle;
  65. HKEY Key1Handle;
  66. HKEY Key2Handle;
  67. HKEY EnvironmentHandle;
  68. WCHAR ValueData[] = VALUE_DATA;
  69. WCHAR BufferForKeyName[100];
  70. WCHAR BufferForKeyClass[100];
  71. WCHAR BufferForValueEntryName[100];
  72. BYTE BufferForValueEntryData[100];
  73. DWORD DataType;
  74. DWORD DataSize;
  75. DWORD NameSize;
  76. DWORD ClassSize;
  77. DWORD cSubKeys;
  78. DWORD cbMaxSubkey;
  79. DWORD cbMaxClass;
  80. DWORD cValues;
  81. DWORD vbMaxValueName;
  82. DWORD cbMaxValueData;
  83. DWORD cbSecurityDescriptor;
  84. FILETIME ftLastWriteTime;
  85. BYTE BufferForSecurityDescriptor[2048];
  86. HANDLE NotificationEvent;
  87. PWSTR File1 = L"d:\\File1";
  88. PWSTR File2 = L"d:\\File2";
  89. DWORD Disposition;
  90. /*
  91. Key = NULL;
  92. Status = RegOpenKeyExW( HKEY_CURRENT_USER,
  93. L"",
  94. 0,
  95. MAXIMUM_ALLOWED,
  96. &Key );
  97. */
  98. // AdjustPrivilege( SE_BACKUP_NAME );
  99. // AdjustPrivilege( SE_RESTORE_NAME );
  100. NotificationEvent = CreateEvent( NULL,
  101. FALSE,
  102. FALSE,
  103. NULL );
  104. if( NotificationEvent == NULL ) {
  105. printf( "CreateEvent failed, ErrorCode = %d \n", GetLastError() );
  106. }
  107. Status = RegOpenKeyExW( HKEY_CURRENT_USER,
  108. TESTKEY_FULL_NAME,
  109. 0,
  110. MAXIMUM_ALLOWED,
  111. &TestKeyHandle );
  112. if( Status != 0 ) {
  113. printf( "RegOpenKeyExW failed, Status = %d \n", Status );
  114. } else {
  115. printf( "RegOpenKeyExW succeeded \n" );
  116. }
  117. Status = RegOpenKeyW( HKEY_CURRENT_USER,
  118. ENVIRONMENT_NAME,
  119. &EnvironmentHandle );
  120. if( Status != 0 ) {
  121. printf( "RegOpenKeyW failed, Status = %d \n", Status );
  122. } else {
  123. printf( "RegOpenKeyW succeeded \n" );
  124. }
  125. Status = RegCreateKeyExW( TestKeyHandle,
  126. KEY1_NAME,
  127. NULL,
  128. NULL,
  129. REG_OPTION_NON_VOLATILE,
  130. MAXIMUM_ALLOWED,
  131. NULL,
  132. &Key1Handle,
  133. &Disposition );
  134. if( Status != 0 ) {
  135. printf( "RegCreateKeyExW failed, Status = %d \n", Status );
  136. } else {
  137. printf( "RegCreateKeyExW succeeded \n" );
  138. }
  139. Status = RegCreateKeyW( TestKeyHandle,
  140. KEY2_NAME,
  141. &Key2Handle );
  142. if( Status != 0 ) {
  143. printf( "RegCreateKeyW failed, Status = %d \n", Status );
  144. } else {
  145. printf( "RegCreateKeyW succeeded \n" );
  146. }
  147. Status = RegSetValueExW( Key1Handle,
  148. VALUE_NAME,
  149. NULL,
  150. REG_SZ,
  151. ValueData,
  152. sizeof( ValueData ) );
  153. if( Status != 0 ) {
  154. printf( "RegSetValueExW failed, Status = %d \n", Status );
  155. } else {
  156. printf( "RegSetValueExW succeeded \n" );
  157. }
  158. Status = RegSetValueW( Key1Handle,
  159. NULL,
  160. REG_SZ,
  161. ValueData,
  162. sizeof( ValueData ) );
  163. if( Status != 0 ) {
  164. printf( "RegSetValueW failed, Status = %d \n", Status );
  165. } else {
  166. printf( "RegSetValueW succeeded \n" );
  167. }
  168. Status = RegFlushKey( Key1Handle );
  169. if( Status != 0 ) {
  170. printf( "RegFlushKey failed, Status = %d \n", Status );
  171. } else {
  172. printf( "RegFlushKey succeeded \n" );
  173. }
  174. DataSize = sizeof( BufferForValueEntryData );
  175. memset( BufferForValueEntryData, '\0', DataSize );
  176. Status = RegQueryValueExW( Key1Handle,
  177. VALUE_NAME,
  178. NULL,
  179. &DataType,
  180. BufferForValueEntryData,
  181. &DataSize );
  182. if( Status != 0 ) {
  183. printf( "RegQueryValueExW failed, Status = %d \n", Status );
  184. } else {
  185. printf( "RegQueryValueExW succeeded \n" );
  186. }
  187. DataSize = sizeof( BufferForValueEntryData );
  188. memset( BufferForValueEntryData, '\0', DataSize );
  189. Status = RegQueryValueW( Key1Handle,
  190. NULL,
  191. BufferForValueEntryData,
  192. &DataSize );
  193. if( Status != 0 ) {
  194. printf( "RegQueryValueW failed, Status = %d \n", Status );
  195. } else {
  196. printf( "RegQueryValueW succeeded \n" );
  197. }
  198. DataSize = sizeof( BufferForValueEntryData );
  199. memset( BufferForValueEntryData, 'X', DataSize );
  200. NameSize = sizeof( BufferForValueEntryName );
  201. memset( BufferForValueEntryName, 'X', NameSize );
  202. Status = RegEnumValueW( Key1Handle,
  203. 0,
  204. BufferForValueEntryName,
  205. &NameSize,
  206. NULL,
  207. &DataType,
  208. BufferForValueEntryData,
  209. &DataSize );
  210. if( Status != 0 ) {
  211. printf( "RegEnumValueW failed, Status = %d \n", Status );
  212. } else {
  213. printf( "RegEnumValueW succeeded \n" );
  214. }
  215. NameSize = sizeof( BufferForKeyName );
  216. ClassSize = sizeof( BufferForKeyClass );
  217. Status = RegEnumKeyExW( TestKeyHandle,
  218. 0,
  219. BufferForKeyName,
  220. &NameSize,
  221. NULL,
  222. BufferForKeyClass,
  223. &ClassSize,
  224. &ftLastWriteTime );
  225. if( Status != 0 ) {
  226. printf( "RegEnumKeyExW failed, Status = %d \n", Status );
  227. } else {
  228. printf( "RegEnumKeyExW succeeded \n" );
  229. }
  230. NameSize = sizeof( BufferForKeyName );
  231. Status = RegEnumKeyW( TestKeyHandle,
  232. 0,
  233. BufferForKeyName,
  234. &NameSize );
  235. if( Status != 0 ) {
  236. printf( "RegEnumKeyW failed, Status = %d \n", Status );
  237. } else {
  238. printf( "RegEnumKeyW succeeded \n" );
  239. }
  240. ClassSize = sizeof( BufferForKeyClass );
  241. Status = RegQueryInfoKeyW( Key1Handle,
  242. BufferForKeyClass,
  243. &ClassSize,
  244. NULL,
  245. &cSubKeys,
  246. &cbMaxSubkey,
  247. &cbMaxClass,
  248. &cValues,
  249. &vbMaxValueName,
  250. &cbMaxValueData,
  251. &cbSecurityDescriptor,
  252. &ftLastWriteTime );
  253. if( Status != 0 ) {
  254. printf( "RegQueryInfoKeyW failed, Status = %d \n", Status );
  255. } else {
  256. printf( "RegQueryInfoKeyW succeeded \n" );
  257. }
  258. Status = RegGetKeySecurity( Key1Handle,
  259. DACL_SECURITY_INFORMATION,
  260. ( PSECURITY_DESCRIPTOR )BufferForSecurityDescriptor,
  261. &cbSecurityDescriptor );
  262. if( Status != 0 ) {
  263. printf( "RegGetKeySecurity failed, Status = %d \n", Status );
  264. } else {
  265. printf( "RegGetKeySecurity succeeded \n" );
  266. }
  267. Status = RegSetKeySecurity( Key1Handle,
  268. DACL_SECURITY_INFORMATION,
  269. ( PSECURITY_DESCRIPTOR )BufferForSecurityDescriptor );
  270. if( Status != 0 ) {
  271. printf( "RegSetKeySecurity failed, Status = %d \n", Status );
  272. } else {
  273. printf( "RegSetKeySecurity succeeded \n" );
  274. }
  275. /*
  276. Status = RegSaveKeyW( Key1Handle,
  277. File1,
  278. NULL );
  279. if( Status != 0 ) {
  280. printf( "RegSaveKeyW failed, Status = %d \n", Status );
  281. } else {
  282. printf( "RegSaveKeyW succeeded \n" );
  283. }
  284. Status = RegRestoreKeyW( Key2Handle,
  285. File1,
  286. 0 );
  287. if( Status != 0 ) {
  288. printf( "RegRestoreKeyW failed, Status = %d \n", Status );
  289. } else {
  290. printf( "RegRestoreKeyW succeeded \n" );
  291. }
  292. */
  293. Status = RegDeleteValueW( Key1Handle,
  294. VALUE_NAME );
  295. if( Status != 0 ) {
  296. printf( "RegDeleteValueW failed, Status = %d \n", Status );
  297. } else {
  298. printf( "RegDeleteValueW succeeded \n" );
  299. }
  300. Status = RegCloseKey( Key1Handle );
  301. if( Status != 0 ) {
  302. printf( "RegCloseKey failed, Status = %d \n", Status );
  303. } else {
  304. printf( "RegCloseKey succeeded \n" );
  305. }
  306. Status = RegDeleteKeyW( TestKeyHandle,
  307. KEY1_NAME );
  308. if( Status != 0 ) {
  309. printf( "RegCloseKey failed, Status = %d \n", Status );
  310. } else {
  311. printf( "RegCloseKey succeeded \n" );
  312. }
  313. Status = RegNotifyChangeKeyValue( HKEY_CURRENT_USER,
  314. TRUE,
  315. REG_NOTIFY_CHANGE_NAME |
  316. REG_NOTIFY_CHANGE_ATTRIBUTES |
  317. REG_NOTIFY_CHANGE_LAST_SET |
  318. REG_NOTIFY_CHANGE_SECURITY,
  319. NotificationEvent,
  320. TRUE );
  321. if( Status != 0 ) {
  322. printf( "RegNotifyChangeKeyValue failed, Status = %d \n", Status );
  323. } else {
  324. printf( "RegNotifyChangeKeyValue succeeded \n" );
  325. }
  326. //
  327. // Cleanup
  328. //
  329. CloseHandle( NotificationEvent );
  330. // DeleteFileW( File1 );
  331. RegCloseKey( Key2Handle );
  332. RegDeleteKeyW( TestKeyHandle,
  333. KEY2_NAME );
  334. }