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.

261 lines
8.2 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: reg.c
  3. *
  4. * Copyright (c) 1985-95, Microsoft Corporation
  5. *
  6. * History:
  7. * 01-02-96 a-jimhar Created based on reg.c from access351.exe
  8. \***************************************************************************/
  9. /*
  10. 1. on startup, check to see if we're administrator
  11. a) use RegOpenKeyEx on HKEY_USERS\.DEFAULT\Software with read/write
  12. access writes. if it fails, we're not administrator
  13. b) if not, grey menu option
  14. 2. on startup
  15. a) use RegOpenKeyEx on HKEY_CURRENTUSER\Software...
  16. b) if it fails, create these keys with default values.
  17. 3. creating keys
  18. a) RegCreateKeyEx
  19. b) RegSetValue
  20. c) RegCloseKey
  21. */
  22. #include "TCHAR.h"
  23. #include <nt.h>
  24. #include <ntrtl.h>
  25. #include <nturtl.h>
  26. #include "Access.h"
  27. BOOL DoAccessRegEntriesExist( HKEY hkeyRoot );
  28. BOOL CheckRegEntry( HKEY hkeyRoot, LPSTR lpsz, REGSAM sam );
  29. BOOL SetRegString( HKEY hkey, LPSTR lpszEntry, LPSTR lpszValue );
  30. DWORD CopyKey( HKEY hkeySrc, HKEY hkeyDst, LPSTR szKey );
  31. char szAccessRegPath[] = "Control Panel\\Accessibility";
  32. char szHcColorRegPath[] = "Control Panel\\Colors";
  33. char szHcDeskRegPath[] = "Control Panel\\Desktop";
  34. /********************************************************************/
  35. //
  36. BOOL IsDefaultWritable( void )
  37. {
  38. return CheckRegEntry( HKEY_USERS, ".Default", KEY_ALL_ACCESS );
  39. }
  40. /********************************************************************/
  41. BOOL DoAccessRegEntriesExist( HKEY hkeyRoot )
  42. {
  43. char sz[128];
  44. strcpy( sz, szAccessRegPath );
  45. strcat( sz, "\\StickyKeys" );
  46. return CheckRegEntry( hkeyRoot, sz, KEY_READ ); // execute means readonly
  47. }
  48. /********************************************************************/
  49. BOOL CheckRegEntry( HKEY hkeyRoot, LPSTR lpsz, REGSAM sam )
  50. {
  51. HKEY hkey;
  52. BOOL fOk = (ERROR_SUCCESS == RegOpenKeyExA( hkeyRoot, lpsz, 0, sam, &hkey ));
  53. if(fOk)
  54. {
  55. RegCloseKey(hkey);
  56. }
  57. return fOk;
  58. }
  59. /********************************************************************/
  60. BOOL SetRegString( HKEY hkey, LPSTR lpszEntry, LPSTR lpszValue )
  61. {
  62. DWORD dwResult;
  63. dwResult = RegSetValueExA( hkey,
  64. lpszEntry,
  65. 0,
  66. REG_SZ,
  67. lpszValue,
  68. strlen( lpszValue ) + sizeof( TCHAR ) );
  69. if( dwResult != ERROR_SUCCESS )
  70. {
  71. ; // should do something like print a message
  72. return FALSE;
  73. }
  74. else
  75. return TRUE;
  76. }
  77. /***********************************************************************/
  78. #define TEMP_PROFILE "Temp profile (access.cpl)"
  79. typedef BOOL (*PFNGETDEFAULTUSERPROFILEDIRECTORYA)(LPSTR lpProfile, LPDWORD dwSize);
  80. DWORD SaveDefaultSettings( BOOL saveL, BOOL saveU )
  81. {
  82. NTSTATUS Status;
  83. DWORD iStatus = ERROR_SUCCESS;
  84. DWORD dwSize;
  85. HKEY hkeyDst;
  86. BOOLEAN WasEnabled;
  87. char acFile[MAX_PATH];
  88. HANDLE hInstDll;
  89. PFNGETDEFAULTUSERPROFILEDIRECTORYA pfnGetDefaultUserProfileDirectory;
  90. // If save to Logon
  91. if ( saveL )
  92. {
  93. iStatus = RegOpenKeyExA( HKEY_USERS, ".DEFAULT", 0, KEY_WRITE |
  94. KEY_ENUMERATE_SUB_KEYS,
  95. &hkeyDst );
  96. if( iStatus != ERROR_SUCCESS )
  97. return iStatus;
  98. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szAccessRegPath );
  99. // a-anilk
  100. // Now copy the colors and Desktop to .Default required for HighContrast setting
  101. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcColorRegPath );
  102. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcDeskRegPath );
  103. RegCloseKey( hkeyDst );
  104. }
  105. if ( saveU )
  106. {
  107. hInstDll = LoadLibrary (TEXT("userenv.dll"));
  108. if (!hInstDll) {
  109. return (GetLastError());
  110. }
  111. pfnGetDefaultUserProfileDirectory = (PFNGETDEFAULTUSERPROFILEDIRECTORYA)GetProcAddress (hInstDll,
  112. "GetDefaultUserProfileDirectoryA");
  113. if (!pfnGetDefaultUserProfileDirectory) {
  114. FreeLibrary (hInstDll);
  115. return (GetLastError());
  116. }
  117. dwSize = MAX_PATH;
  118. if (!pfnGetDefaultUserProfileDirectory(acFile, &dwSize)) {
  119. FreeLibrary (hInstDll);
  120. return (GetLastError());
  121. }
  122. FreeLibrary (hInstDll);
  123. Status = RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, TRUE, FALSE, &WasEnabled);
  124. if (!NT_SUCCESS(Status)) return iStatus;
  125. strcat(acFile,"\\ntuser.dat");
  126. iStatus = RegLoadKeyA(HKEY_USERS, TEMP_PROFILE, acFile);
  127. if (iStatus == ERROR_SUCCESS) {
  128. iStatus = RegOpenKeyExA( HKEY_USERS, TEMP_PROFILE, 0, KEY_WRITE |
  129. KEY_ENUMERATE_SUB_KEYS, &hkeyDst );
  130. if( iStatus == ERROR_SUCCESS ) {
  131. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szAccessRegPath );
  132. // a-anilk
  133. // Now copy the colors and Desktop to .Default required for HighContrast setting
  134. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcColorRegPath );
  135. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcDeskRegPath );
  136. RegCloseKey( hkeyDst );
  137. }
  138. RegUnLoadKeyA(HKEY_USERS, TEMP_PROFILE);
  139. }
  140. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasEnabled, FALSE, &WasEnabled);
  141. }
  142. return iStatus;
  143. }
  144. /***********************************************************************/
  145. // CopyKey( hKey, hKeyDst, name )
  146. // create the destination key
  147. // for each value
  148. // CopyValue
  149. // for each subkey
  150. // CopyKey
  151. DWORD CopyKey( HKEY hkeySrc, HKEY hkeyDst, LPSTR szKey )
  152. {
  153. HKEY hkeyOld, hkeyNew;
  154. char szValue[128];
  155. char szData[128];
  156. char szBuffer[128];
  157. DWORD iStatus;
  158. UINT nValue, nKey;
  159. UINT iValueLen, iDataLen;
  160. DWORD dwType;
  161. iStatus = RegOpenKeyExA( hkeySrc, szKey, 0, KEY_WRITE |
  162. KEY_ENUMERATE_SUB_KEYS, &hkeyOld );
  163. if( iStatus != ERROR_SUCCESS )
  164. return iStatus;
  165. iStatus = RegOpenKeyExA( hkeyDst, szKey, 0, KEY_WRITE |
  166. KEY_ENUMERATE_SUB_KEYS, &hkeyNew );
  167. if( iStatus != ERROR_SUCCESS )
  168. {
  169. iStatus = RegCreateKeyExA( hkeyDst, szKey, 0, "", 0, KEY_WRITE |
  170. KEY_ENUMERATE_SUB_KEYS, NULL, &hkeyNew, NULL );
  171. if( iStatus != ERROR_SUCCESS )
  172. {
  173. RegCloseKey( hkeyOld );
  174. return iStatus;
  175. }
  176. }
  177. //*********** copy the values **************** //
  178. for( nValue = 0, iValueLen=sizeof szValue, iDataLen=sizeof szValue;
  179. ERROR_SUCCESS == (iStatus = RegEnumValueA(hkeyOld,
  180. nValue,
  181. szValue,
  182. &iValueLen,
  183. NULL, // reserved
  184. &dwType, // don't need type
  185. szData,
  186. &iDataLen ) );
  187. nValue ++, iValueLen=sizeof szValue, iDataLen=sizeof szValue )
  188. {
  189. iStatus = RegSetValueExA( hkeyNew,
  190. szValue,
  191. 0, // reserved
  192. dwType,
  193. szData,
  194. iDataLen);
  195. }
  196. if( iStatus != ERROR_NO_MORE_ITEMS )
  197. {
  198. RegCloseKey( hkeyOld );
  199. RegCloseKey( hkeyNew );
  200. return iStatus;
  201. }
  202. //*********** copy the subtrees ************** //
  203. for( nKey = 0;
  204. ERROR_SUCCESS == (iStatus = RegEnumKeyA(hkeyOld,nKey,szBuffer,sizeof(szBuffer)));
  205. nKey ++ )
  206. {
  207. iStatus = CopyKey( hkeyOld, hkeyNew, szBuffer );
  208. if( iStatus != ERROR_NO_MORE_ITEMS && iStatus != ERROR_SUCCESS )
  209. {
  210. RegCloseKey( hkeyOld );
  211. RegCloseKey( hkeyNew );
  212. return iStatus;
  213. }
  214. }
  215. RegCloseKey( hkeyOld );
  216. RegCloseKey( hkeyNew );
  217. if( iStatus == ERROR_NO_MORE_ITEMS )
  218. return ERROR_SUCCESS;
  219. else
  220. return iStatus;
  221. }