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.

359 lines
8.4 KiB

  1. // RARegSetting.cpp : Implementation of CRARegSetting
  2. #include "stdafx.h"
  3. #include "RAssistance.h"
  4. #include "common.h"
  5. #include "RARegSetting.h"
  6. #include "assert.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. // CRARegSetting
  9. STDMETHODIMP CRARegSetting::get_AllowGetHelpCPL(BOOL *pVal)
  10. {
  11. // Set default value;
  12. DWORD dwValue;
  13. *pVal = RA_CTL_RA_ENABLE_DEF_VALUE;
  14. HRESULT hr = RegGetDwValueCPL(RA_CTL_RA_ENABLE, &dwValue);
  15. if (hr == S_OK)
  16. {
  17. *pVal = !!dwValue;
  18. }
  19. return hr;
  20. }
  21. STDMETHODIMP CRARegSetting::get_AllowGetHelp(BOOL *pVal)
  22. {
  23. // Set default value;
  24. DWORD dwValue;
  25. *pVal = RA_CTL_RA_ENABLE_DEF_VALUE;
  26. HRESULT hr = RegGetDwValue(RA_CTL_RA_ENABLE, &dwValue);
  27. if (hr == S_OK)
  28. {
  29. *pVal = !!dwValue;
  30. }
  31. return hr;
  32. }
  33. STDMETHODIMP CRARegSetting::put_AllowGetHelp(BOOL newVal)
  34. {
  35. DWORD dwValue = newVal;
  36. HRESULT hr = RegSetDwValue(RA_CTL_RA_ENABLE, dwValue);
  37. return hr;
  38. }
  39. STDMETHODIMP CRARegSetting::get_AllowBuddyHelp(BOOL *pVal)
  40. {
  41. // Set default value;
  42. DWORD dwValue;
  43. *pVal = RA_CTL_ALLOW_BUDDYHELP_DEF_VALUE;
  44. HRESULT hr = RegGetDwValue(RA_CTL_ALLOW_BUDDYHELP, &dwValue);
  45. if (hr == S_OK)
  46. {
  47. *pVal = !!dwValue;
  48. }
  49. return hr;
  50. }
  51. STDMETHODIMP CRARegSetting::get_AllowUnSolicitedFullControl(BOOL *pVal)
  52. {
  53. // Set default value;
  54. DWORD dwValue;
  55. *pVal = RA_CTL_ALLOW_UNSOLICITEDFULLCONTROL_DEF_VALUE;
  56. HRESULT hr = RegGetDwValue(RA_CTL_ALLOW_UNSOLICITEDFULLCONTROL, &dwValue);
  57. if (hr == S_OK)
  58. {
  59. *pVal = !!dwValue;
  60. }
  61. return hr;
  62. }
  63. STDMETHODIMP CRARegSetting::get_AllowUnSolicited(BOOL *pVal)
  64. {
  65. // Set default value;
  66. DWORD dwValue;
  67. *pVal = RA_CTL_ALLOW_UNSOLICITED_DEF_VALUE;
  68. HRESULT hr = RegGetDwValue(RA_CTL_ALLOW_UNSOLICITED, &dwValue);
  69. if (hr == S_OK)
  70. {
  71. *pVal = !!dwValue;
  72. }
  73. return hr;
  74. }
  75. STDMETHODIMP CRARegSetting::put_AllowUnSolicited(BOOL newVal)
  76. {
  77. DWORD dwValue = newVal;
  78. HRESULT hr = RegSetDwValue(RA_CTL_ALLOW_UNSOLICITED, dwValue);
  79. return hr;
  80. }
  81. STDMETHODIMP CRARegSetting::get_AllowFullControl(BOOL *pVal)
  82. {
  83. // Set default value;
  84. DWORD dwValue;
  85. *pVal = RA_CTL_ALLOW_FULLCONTROL_DEF_VALUE;
  86. HRESULT hr = RegGetDwValue(RA_CTL_ALLOW_FULLCONTROL, &dwValue);
  87. if (hr == S_OK)
  88. {
  89. *pVal = !!dwValue;
  90. }
  91. return hr;
  92. }
  93. STDMETHODIMP CRARegSetting::put_AllowFullControl(BOOL newVal)
  94. {
  95. DWORD dwValue = newVal;
  96. HRESULT hr = RegSetDwValue(RA_CTL_ALLOW_FULLCONTROL, dwValue);
  97. return hr;
  98. }
  99. STDMETHODIMP CRARegSetting::get_MaxTicketExpiry(LONG *pVal)
  100. {
  101. // Set default value;
  102. DWORD dwUnit, dwValue;
  103. HRESULT hr = FALSE;
  104. DWORD dwSize = sizeof(DWORD), dwSize1 = sizeof(DWORD);
  105. HKEY hKey=NULL, hPolKey=NULL, hCtlKey=NULL;
  106. *pVal = RA_CTL_TICKET_EXPIRY_DEF_VALUE;
  107. //
  108. // Look up Group Policy settings first
  109. //
  110. RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_REMOTEASSISTANCE_GP, 0, KEY_READ, &hPolKey);
  111. //
  112. // look up Control Panel settings
  113. //
  114. RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_REMOTEASSISTANCE, 0, KEY_READ, &hCtlKey);
  115. //
  116. // Read the reg value if we could open the key
  117. //
  118. if (hPolKey)
  119. hKey = hPolKey;
  120. else if (hCtlKey)
  121. hKey = hCtlKey;
  122. while (hKey)
  123. {
  124. // Get value
  125. if (ERROR_SUCCESS == RegQueryValueEx(hKey, RA_CTL_TICKET_EXPIRY, 0, NULL, (LPBYTE)&dwValue, &dwSize) &&
  126. ERROR_SUCCESS == RegQueryValueEx(hKey, RA_CTL_TICKET_EXPIRY_UNIT, 0, NULL, (LPBYTE)&dwUnit, &dwSize1))
  127. {
  128. *pVal = dwValue * ((dwUnit==RA_IDX_MIN)?60:((dwUnit==RA_IDX_HOUR)?3600:86400)); // 0: minute 1: hour 2: day
  129. break;
  130. }
  131. else if (hPolKey == hKey)
  132. {
  133. //
  134. // Group Policy gets rid of the value if you disable the policy
  135. // Need to read the value from Control Panel settings.
  136. //
  137. assert(hCtlKey != hPolKey);
  138. hKey = hCtlKey;
  139. continue;
  140. }
  141. break;
  142. }
  143. if (hPolKey)
  144. RegCloseKey(hPolKey);
  145. if (hCtlKey)
  146. RegCloseKey(hCtlKey);
  147. return S_OK;
  148. }
  149. STDMETHODIMP CRARegSetting::put_MaxTicketExpiry(LONG newVal)
  150. {
  151. DWORD dwValue = newVal;
  152. DWORD dwUnit = -1, dwBase=0;
  153. RegGetDwValue(RA_CTL_TICKET_EXPIRY_UNIT, &dwUnit);
  154. if (dwUnit != -1)
  155. {
  156. dwBase = (dwUnit==0)?60:((dwUnit==1)?3600:86400);
  157. if (dwValue % dwBase == 0) // no need to change Unit
  158. {
  159. dwValue = dwValue/dwBase;
  160. goto set_value;
  161. }
  162. }
  163. if (dwValue % 86400 == 0)
  164. {
  165. dwValue /= 86400;
  166. dwUnit = RA_IDX_DAY;
  167. }
  168. else if (dwValue % 3600 == 0)
  169. {
  170. dwValue /= 3600;
  171. dwUnit = RA_IDX_HOUR;
  172. }
  173. dwValue = dwValue / 60 + ((dwValue % 60) > 0); // round to the next minutes
  174. dwUnit = RA_IDX_MIN;
  175. set_value:
  176. RegSetDwValue(RA_CTL_TICKET_EXPIRY, dwValue);
  177. RegSetDwValue(RA_CTL_TICKET_EXPIRY_UNIT, dwUnit);
  178. return S_OK;
  179. }
  180. STDMETHODIMP CRARegSetting::get_AllowRemoteAssistance(BOOL *pVal)
  181. {
  182. // Set default value;
  183. DWORD dwValue;
  184. *pVal = RA_CTL_RA_ENABLE_DEF_VALUE;
  185. HRESULT hr = RegGetDwValue(RA_CTL_RA_MODE, &dwValue);
  186. if (hr == S_OK)
  187. {
  188. *pVal = !!dwValue;
  189. }
  190. return hr;
  191. }
  192. STDMETHODIMP CRARegSetting::put_AllowRemoteAssistance(BOOL newVal)
  193. {
  194. DWORD dwValue = newVal;
  195. HRESULT hr = RegSetDwValue(RA_CTL_RA_MODE, dwValue);
  196. return hr;
  197. }
  198. /*****************************************************************
  199. Func:
  200. RegGetDwValueCPL()
  201. Abstract:
  202. Internal Helper function to retrieve RA setting values for Control Panel settings
  203. Return:
  204. DWORD value
  205. ******************************************************************/
  206. HRESULT CRARegSetting::RegGetDwValueCPL(LPCTSTR valueName, DWORD* pdword)
  207. {
  208. HRESULT hr = S_FALSE;
  209. DWORD dwSize = sizeof(DWORD);
  210. HKEY hKey=NULL;
  211. //
  212. // look up Control Panel settings
  213. //
  214. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_REMOTEASSISTANCE, 0, KEY_READ, &hKey))
  215. {
  216. //
  217. // Read the reg value if we could open the key
  218. //
  219. // Get value
  220. LONG lRetVal = RegQueryValueEx(hKey, valueName, 0, NULL, (LPBYTE)pdword, &dwSize );
  221. hr = (lRetVal == ERROR_SUCCESS) ? S_OK : S_FALSE;
  222. }
  223. if (hKey)
  224. RegCloseKey(hKey);
  225. return hr;
  226. }
  227. /*****************************************************************
  228. Func:
  229. RegGetDwValue()
  230. Abstract:
  231. Internal Helper function to retrieve RA setting values
  232. Return:
  233. DWORD value
  234. ******************************************************************/
  235. HRESULT CRARegSetting::RegGetDwValue(LPCTSTR valueName, DWORD* pdword)
  236. {
  237. HRESULT hr = S_FALSE;
  238. DWORD dwSize = sizeof(DWORD);
  239. HKEY hKey=NULL, hPolKey=NULL, hCtlKey=NULL;
  240. //
  241. // Look up Group Policy settings first
  242. //
  243. RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_REMOTEASSISTANCE_GP, 0, KEY_READ, &hPolKey);
  244. //
  245. // look up Control Panel settings
  246. //
  247. RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_REMOTEASSISTANCE, 0, KEY_READ, &hCtlKey);
  248. *pdword = 0;
  249. //
  250. // Read the reg value if we could open the key
  251. //
  252. if (hPolKey)
  253. hKey = hPolKey;
  254. else if (hCtlKey)
  255. hKey = hCtlKey;
  256. while (hKey)
  257. {
  258. // Get value
  259. LONG lRetVal = RegQueryValueEx(hKey, valueName, 0, NULL, (LPBYTE)pdword, &dwSize );
  260. hr = (lRetVal == ERROR_SUCCESS) ? S_OK : S_FALSE;
  261. if (hr == S_FALSE && hPolKey == hKey)
  262. {
  263. //
  264. // Value not found in Group Policy
  265. // Need to read the value from Control Panel settings.
  266. //
  267. assert(hCtlKey != hPolKey);
  268. hKey = hCtlKey;
  269. continue;
  270. }
  271. break;
  272. }
  273. if (hPolKey)
  274. RegCloseKey(hPolKey);
  275. if (hCtlKey)
  276. RegCloseKey(hCtlKey);
  277. return hr;
  278. }
  279. /*****************************************************************
  280. Func:
  281. RegSetDwValue()
  282. Abstract:
  283. Internal Helper function to set RA setting values
  284. Return:
  285. DWORD value
  286. ******************************************************************/
  287. HRESULT CRARegSetting::RegSetDwValue(LPCTSTR valueName, DWORD dwValue)
  288. {
  289. HRESULT hr = S_FALSE;
  290. DWORD dwSize = sizeof(DWORD);
  291. HKEY hKey = NULL;
  292. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_KEY_REMOTEASSISTANCE, 0, KEY_WRITE, &hKey))
  293. {
  294. // Set Value
  295. if (ERROR_SUCCESS ==
  296. RegSetValueEx(hKey,valueName,0,REG_DWORD,(LPBYTE)&dwValue,sizeof(DWORD)))
  297. {
  298. hr = S_OK;
  299. }
  300. RegCloseKey(hKey);
  301. }
  302. return hr;
  303. }