Source code of Windows XP (NT5)
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.

297 lines
9.1 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 <nt.h>
  23. #include <ntrtl.h>
  24. #include <nturtl.h>
  25. #include "Access.h"
  26. BOOL DoAccessRegEntriesExist( HKEY hkeyRoot );
  27. BOOL CheckRegEntry( HKEY hkeyRoot, LPSTR lpsz, REGSAM sam );
  28. LONG OpenAccessRegKeyW( HKEY hkeyRoot, LPSTR lpstr, PHKEY phkey );
  29. BOOL CloseAccessRegKey( HKEY hkey );
  30. BOOL SetRegString( HKEY hkey, LPSTR lpszEntry, LPSTR lpszValue );
  31. DWORD CopyKey( HKEY hkeySrc, HKEY hkeyDst, LPSTR szKey );
  32. char szAccessRegPath[] = "Control Panel\\Accessibility";
  33. char szHcColorRegPath[] = "Control Panel\\Colors";
  34. char szHcDeskRegPath[] = "Control Panel\\Desktop";
  35. /********************************************************************/
  36. //
  37. BOOL IsDefaultWritable( void )
  38. {
  39. return CheckRegEntry( HKEY_USERS, ".Default", KEY_ALL_ACCESS );
  40. }
  41. /********************************************************************/
  42. BOOL DoAccessRegEntriesExist( HKEY hkeyRoot )
  43. {
  44. char sz[128];
  45. strcpy( sz, szAccessRegPath );
  46. strcat( sz, "\\StickyKeys" );
  47. return CheckRegEntry( hkeyRoot, sz, KEY_READ ); // execute means readonly
  48. }
  49. /********************************************************************/
  50. BOOL CheckRegEntry( HKEY hkeyRoot, LPSTR lpsz, REGSAM sam )
  51. {
  52. HKEY hkey;
  53. BOOL fOk = (ERROR_SUCCESS == RegOpenKeyExA( hkeyRoot, lpsz, 0, sam, &hkey ));
  54. if(fOk)
  55. {
  56. RegCloseKey(hkey);
  57. }
  58. return fOk;
  59. }
  60. /********************************************************************/
  61. LONG OpenAccessRegKeyW( HKEY hkeyRoot, LPSTR lpstr, PHKEY phkey )
  62. {
  63. LONG dw;
  64. LONG dwDisposition;
  65. char sz[128];
  66. strcpy( sz, szAccessRegPath );
  67. strcat( sz, "\\" );
  68. strcat( sz, lpstr );
  69. // dw = RegOpenKey( hkeyRoot, sz, phkey );
  70. // dw = RegOpenKey( hkeyRoot, "\\Software", phkey );
  71. // dw = RegOpenKey( hkeyRoot, "\\FOOBAR", phkey );
  72. // dw = RegOpenKey( hkeyRoot, "Software", phkey );
  73. // dw = RegOpenKey( hkeyRoot, "FOOBAR", phkey );
  74. // dw = RegOpenKey( hkeyRoot, "Software\\Microsoft\\Accessibility\\StickyKeys", phkey );
  75. dw = RegCreateKeyExA( hkeyRoot,
  76. sz,
  77. 0,
  78. NULL, // CLASS NAME??
  79. 0, // by default is non-volatile
  80. KEY_ALL_ACCESS,
  81. NULL, // default security descriptor
  82. phkey,
  83. &dwDisposition ); // yes we throw this away
  84. if( dw != ERROR_SUCCESS )
  85. {
  86. // should do something
  87. }
  88. return dw;
  89. }
  90. /********************************************************************/
  91. BOOL CloseAccessRegKey( HKEY hkey )
  92. {
  93. DWORD dw;
  94. dw = RegCloseKey( hkey );
  95. if( dw == ERROR_SUCCESS )
  96. return TRUE;
  97. else
  98. return FALSE;
  99. }
  100. /********************************************************************/
  101. BOOL SetRegString( HKEY hkey, LPSTR lpszEntry, LPSTR lpszValue )
  102. {
  103. DWORD dwResult;
  104. dwResult = RegSetValueExA( hkey,
  105. lpszEntry,
  106. 0,
  107. REG_SZ,
  108. lpszValue,
  109. strlen( lpszValue ) + sizeof( TCHAR ) );
  110. if( dwResult != ERROR_SUCCESS )
  111. {
  112. ; // should do something like print a message
  113. return FALSE;
  114. }
  115. else
  116. return TRUE;
  117. }
  118. /***********************************************************************/
  119. #define TEMP_PROFILE "Temp profile (access.cpl)"
  120. typedef BOOL (*PFNGETDEFAULTUSERPROFILEDIRECTORYA)(LPSTR lpProfile, LPDWORD dwSize);
  121. DWORD SaveDefaultSettings( BOOL saveL, BOOL saveU )
  122. {
  123. NTSTATUS Status;
  124. DWORD iStatus = ERROR_SUCCESS;
  125. DWORD dwSize;
  126. HKEY hkeyDst;
  127. BOOLEAN WasEnabled;
  128. char acFile[MAX_PATH];
  129. HANDLE hInstDll;
  130. PFNGETDEFAULTUSERPROFILEDIRECTORYA pfnGetDefaultUserProfileDirectory;
  131. // If save to Logon
  132. if ( saveL )
  133. {
  134. iStatus = RegOpenKeyA( HKEY_USERS, ".DEFAULT", &hkeyDst );
  135. if( iStatus != ERROR_SUCCESS )
  136. return iStatus;
  137. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szAccessRegPath );
  138. // a-anilk
  139. // Now copy the colors and Desktop to .Default required for HighContrast setting
  140. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcColorRegPath );
  141. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcDeskRegPath );
  142. RegCloseKey( hkeyDst );
  143. }
  144. if ( saveU )
  145. {
  146. hInstDll = LoadLibrary (TEXT("userenv.dll"));
  147. if (!hInstDll) {
  148. return (GetLastError());
  149. }
  150. pfnGetDefaultUserProfileDirectory = (PFNGETDEFAULTUSERPROFILEDIRECTORYA)GetProcAddress (hInstDll,
  151. "GetDefaultUserProfileDirectoryA");
  152. if (!pfnGetDefaultUserProfileDirectory) {
  153. FreeLibrary (hInstDll);
  154. return (GetLastError());
  155. }
  156. dwSize = MAX_PATH;
  157. if (!pfnGetDefaultUserProfileDirectory(acFile, &dwSize)) {
  158. FreeLibrary (hInstDll);
  159. return (GetLastError());
  160. }
  161. FreeLibrary (hInstDll);
  162. Status = RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, TRUE, FALSE, &WasEnabled);
  163. if (!NT_SUCCESS(Status)) return iStatus;
  164. strcat(acFile,"\\ntuser.dat");
  165. iStatus = RegLoadKeyA(HKEY_USERS, TEMP_PROFILE, acFile);
  166. if (iStatus == ERROR_SUCCESS) {
  167. iStatus = RegOpenKeyA( HKEY_USERS, TEMP_PROFILE, &hkeyDst );
  168. if( iStatus == ERROR_SUCCESS ) {
  169. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szAccessRegPath );
  170. // a-anilk
  171. // Now copy the colors and Desktop to .Default required for HighContrast setting
  172. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcColorRegPath );
  173. iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcDeskRegPath );
  174. RegCloseKey( hkeyDst );
  175. }
  176. RegUnLoadKeyA(HKEY_USERS, TEMP_PROFILE);
  177. }
  178. RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasEnabled, FALSE, &WasEnabled);
  179. }
  180. return iStatus;
  181. }
  182. /***********************************************************************/
  183. // CopyKey( hKey, hKeyDst, name )
  184. // create the destination key
  185. // for each value
  186. // CopyValue
  187. // for each subkey
  188. // CopyKey
  189. DWORD CopyKey( HKEY hkeySrc, HKEY hkeyDst, LPSTR szKey )
  190. {
  191. HKEY hkeyOld, hkeyNew;
  192. char szValue[128];
  193. char szData[128];
  194. char szBuffer[128];
  195. DWORD iStatus;
  196. UINT nValue, nKey;
  197. UINT iValueLen, iDataLen;
  198. DWORD dwType;
  199. iStatus = RegOpenKeyA( hkeySrc, szKey, &hkeyOld );
  200. if( iStatus != ERROR_SUCCESS )
  201. return iStatus;
  202. iStatus = RegOpenKeyA( hkeyDst, szKey, &hkeyNew );
  203. if( iStatus != ERROR_SUCCESS )
  204. {
  205. iStatus = RegCreateKeyA( hkeyDst, szKey, &hkeyNew );
  206. if( iStatus != ERROR_SUCCESS )
  207. {
  208. RegCloseKey( hkeyOld );
  209. return iStatus;
  210. }
  211. }
  212. //*********** copy the values **************** //
  213. for( nValue = 0, iValueLen=sizeof szValue, iDataLen=sizeof szValue;
  214. ERROR_SUCCESS == (iStatus = RegEnumValueA(hkeyOld,
  215. nValue,
  216. szValue,
  217. &iValueLen,
  218. NULL, // reserved
  219. &dwType, // don't need type
  220. szData,
  221. &iDataLen ) );
  222. nValue ++, iValueLen=sizeof szValue, iDataLen=sizeof szValue )
  223. {
  224. iStatus = RegSetValueExA( hkeyNew,
  225. szValue,
  226. 0, // reserved
  227. dwType,
  228. szData,
  229. iDataLen);
  230. }
  231. if( iStatus != ERROR_NO_MORE_ITEMS )
  232. {
  233. RegCloseKey( hkeyOld );
  234. RegCloseKey( hkeyNew );
  235. return iStatus;
  236. }
  237. //*********** copy the subtrees ************** //
  238. for( nKey = 0;
  239. ERROR_SUCCESS == (iStatus = RegEnumKeyA(hkeyOld,nKey,szBuffer,sizeof(szBuffer)));
  240. nKey ++ )
  241. {
  242. iStatus = CopyKey( hkeyOld, hkeyNew, szBuffer );
  243. if( iStatus != ERROR_NO_MORE_ITEMS && iStatus != ERROR_SUCCESS )
  244. {
  245. RegCloseKey( hkeyOld );
  246. RegCloseKey( hkeyNew );
  247. return iStatus;
  248. }
  249. }
  250. RegCloseKey( hkeyOld );
  251. RegCloseKey( hkeyNew );
  252. if( iStatus == ERROR_NO_MORE_ITEMS )
  253. return ERROR_SUCCESS;
  254. else
  255. return iStatus;
  256. }