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.

425 lines
10 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. policy.cpp
  5. Abstract:
  6. RDS Policy related function
  7. Author:
  8. HueiWang 5/2/2000
  9. --*/
  10. #include "stdafx.h"
  11. #include "policy.h"
  12. extern "C" BOOLEAN RegDenyTSConnectionsPolicy();
  13. typedef struct __RDSLevelShadowMap {
  14. SHADOWCLASS shadowClass;
  15. REMOTE_DESKTOP_SHARING_CLASS rdsLevel;
  16. } RDSLevelShadowMap;
  17. static const RDSLevelShadowMap ShadowMap[] = {
  18. { Shadow_Disable, NO_DESKTOP_SHARING }, // No RDS sharing
  19. { Shadow_EnableInputNotify, CONTROLDESKTOP_PERMISSION_REQUIRE }, // Interact with user permission
  20. { Shadow_EnableInputNoNotify, CONTROLDESKTOP_PERMISSION_NOT_REQUIRE }, // Interact without user permission
  21. { Shadow_EnableNoInputNotify, VIEWDESKTOP_PERMISSION_REQUIRE}, // View with user permission
  22. { Shadow_EnableNoInputNoNotify, VIEWDESKTOP_PERMISSION_NOT_REQUIRE } // View without user permission
  23. };
  24. DWORD
  25. GetPolicyAllowGetHelpSetting(
  26. HKEY hKey,
  27. LPCTSTR pszKeyName,
  28. LPCTSTR pszValueName,
  29. IN DWORD* value
  30. )
  31. /*++
  32. Routine Description:
  33. Routine to query policy registry value.
  34. Parameters:
  35. hKey : Currently open registry key.
  36. pszKeyName : Pointer to a null-terminated string containing
  37. the name of the subkey to open.
  38. pszValueName : Pointer to a null-terminated string containing
  39. the name of the value to query
  40. value : Pointer to DWORD to receive GetHelp policy setting.
  41. Returns:
  42. ERROR_SUCCESS or error code from RegOpenKeyEx().
  43. --*/
  44. {
  45. DWORD dwStatus;
  46. HKEY hPolicyKey = NULL;
  47. DWORD dwType;
  48. DWORD cbData;
  49. //
  50. // Open registry key for system policy
  51. //
  52. dwStatus = RegOpenKeyEx(
  53. hKey,
  54. pszKeyName,
  55. 0,
  56. KEY_READ,
  57. &hPolicyKey
  58. );
  59. if( ERROR_SUCCESS == dwStatus )
  60. {
  61. // query value
  62. cbData = 0;
  63. dwType = 0;
  64. dwStatus = RegQueryValueEx(
  65. hPolicyKey,
  66. pszValueName,
  67. NULL,
  68. &dwType,
  69. NULL,
  70. &cbData
  71. );
  72. if( ERROR_SUCCESS == dwStatus )
  73. {
  74. if( REG_DWORD == dwType )
  75. {
  76. cbData = sizeof(DWORD);
  77. // our registry value is REG_DWORD, if different type,
  78. // assume not exist.
  79. dwStatus = RegQueryValueEx(
  80. hPolicyKey,
  81. pszValueName,
  82. NULL,
  83. &dwType,
  84. (LPBYTE)value,
  85. &cbData
  86. );
  87. ASSERT( ERROR_SUCCESS == dwStatus );
  88. }
  89. else
  90. {
  91. // bad registry key type, assume
  92. // key does not exist.
  93. dwStatus = ERROR_FILE_NOT_FOUND;
  94. }
  95. }
  96. RegCloseKey( hPolicyKey );
  97. }
  98. return dwStatus;
  99. }
  100. SHADOWCLASS
  101. MapRDSLevelToTSShadowSetting(
  102. IN REMOTE_DESKTOP_SHARING_CLASS RDSLevel
  103. )
  104. /*++
  105. Routine Description:
  106. Convert TS Shadow settings to our RDS sharing level.
  107. Parameter:
  108. TSShadowClass : TS Shadow setting.
  109. Returns:
  110. REMOTE_DESKTOP_SHARING_CLASS
  111. --*/
  112. {
  113. SHADOWCLASS shadowClass;
  114. for( int i=0; i < sizeof(ShadowMap)/sizeof(ShadowMap[0]); i++)
  115. {
  116. if( ShadowMap[i].rdsLevel == RDSLevel )
  117. {
  118. break;
  119. }
  120. }
  121. if( i < sizeof(ShadowMap)/sizeof(ShadowMap[0]) )
  122. {
  123. shadowClass = ShadowMap[i].shadowClass;
  124. }
  125. else
  126. {
  127. MYASSERT(FALSE);
  128. shadowClass = Shadow_Disable;
  129. }
  130. return shadowClass;
  131. }
  132. REMOTE_DESKTOP_SHARING_CLASS
  133. MapTSShadowSettingToRDSLevel(
  134. SHADOWCLASS TSShadowClass
  135. )
  136. /*++
  137. Routine Description:
  138. Convert TS Shadow settings to our RDS sharing level.
  139. Parameter:
  140. TSShadowClass : TS Shadow setting.
  141. Returns:
  142. REMOTE_DESKTOP_SHARING_CLASS
  143. --*/
  144. {
  145. REMOTE_DESKTOP_SHARING_CLASS level;
  146. for( int i=0; i < sizeof(ShadowMap)/sizeof(ShadowMap[0]); i++)
  147. {
  148. if( ShadowMap[i].shadowClass == TSShadowClass )
  149. {
  150. break;
  151. }
  152. }
  153. if( i < sizeof(ShadowMap)/sizeof(ShadowMap[0]) )
  154. {
  155. level = ShadowMap[i].rdsLevel;
  156. }
  157. else
  158. {
  159. MYASSERT(FALSE);
  160. level = NO_DESKTOP_SHARING;
  161. }
  162. return level;
  163. }
  164. BOOL
  165. IsUserAllowToGetHelp(
  166. IN ULONG ulSessionId,
  167. IN LPCTSTR pszUserSid
  168. )
  169. /*++
  170. Routine Description:
  171. Determine if caller can 'GetHelp'
  172. Parameters:
  173. ulSessionId : User's TS logon ID.
  174. pszUserSid : User's SID in textual form.
  175. Returns:
  176. TRUE/FALSE
  177. Note:
  178. Must have impersonate user first.
  179. --*/
  180. {
  181. BOOL bAllow;
  182. DWORD dwStatus;
  183. DWORD dwAllow;
  184. LPTSTR pszUserHive = NULL;
  185. if (pszUserSid == NULL) {
  186. MYASSERT(FALSE);
  187. bAllow = FALSE;
  188. goto CLEANUPANDEXIT;
  189. }
  190. //
  191. // Must be able to GetHelp from machine
  192. //
  193. bAllow = TSIsMachinePolicyAllowHelp();
  194. if( TRUE == bAllow )
  195. {
  196. pszUserHive = (LPTSTR)LocalAlloc(
  197. LPTR,
  198. sizeof(TCHAR) * (lstrlen(pszUserSid) + lstrlen(RDS_GROUPPOLICY_SUBTREE) + 2 )
  199. );
  200. if (pszUserHive == NULL) {
  201. MYASSERT(FALSE);
  202. bAllow = FALSE;
  203. goto CLEANUPANDEXIT;
  204. }
  205. lstrcpy( pszUserHive, pszUserSid );
  206. lstrcat( pszUserHive, _TEXT("\\") );
  207. lstrcat( pszUserHive, RDS_GROUPPOLICY_SUBTREE );
  208. //
  209. // Query user level AllowGetHelp setting.
  210. dwStatus = GetPolicyAllowGetHelpSetting(
  211. HKEY_USERS,
  212. pszUserHive,
  213. RDS_ALLOWGETHELP_VALUENAME,
  214. &dwAllow
  215. );
  216. if( ERROR_SUCCESS == dwStatus )
  217. {
  218. bAllow = (POLICY_ENABLE == dwAllow);
  219. }
  220. else
  221. {
  222. // no configuration for this user, assume GetHelp
  223. // is enabled.
  224. bAllow = TRUE;
  225. }
  226. }
  227. CLEANUPANDEXIT:
  228. if( NULL != pszUserHive )
  229. {
  230. LocalFree( pszUserHive );
  231. }
  232. return bAllow;
  233. }
  234. DWORD
  235. GetUserRDSLevel(
  236. IN ULONG ulSessionId,
  237. OUT REMOTE_DESKTOP_SHARING_CLASS* pLevel
  238. )
  239. /*++
  240. same as GetSystemRDSLevel() except it retrieve currently logon user's
  241. RDS level.
  242. --*/
  243. {
  244. DWORD dwStatus;
  245. BOOL bSuccess;
  246. WINSTATIONCONFIG WSConfig;
  247. DWORD dwByteReturned;
  248. memset( &WSConfig, 0, sizeof(WSConfig) );
  249. // Here we call WInStationQueryInformation() since WTSAPI require
  250. // few calls to get the same result
  251. bSuccess = WinStationQueryInformation(
  252. WTS_CURRENT_SERVER,
  253. ulSessionId,
  254. WinStationConfiguration,
  255. &WSConfig,
  256. sizeof( WSConfig ),
  257. &dwByteReturned
  258. );
  259. if( TRUE == bSuccess )
  260. {
  261. dwStatus = ERROR_SUCCESS;
  262. *pLevel = MapTSShadowSettingToRDSLevel( WSConfig.User.Shadow );
  263. }
  264. else
  265. {
  266. dwStatus = GetLastError();
  267. }
  268. return dwStatus;
  269. }
  270. DWORD
  271. ConfigUserSessionRDSLevel(
  272. IN ULONG ulSessionId,
  273. IN REMOTE_DESKTOP_SHARING_CLASS level
  274. )
  275. /*++
  276. --*/
  277. {
  278. WINSTATIONCONFIG winstationConfig;
  279. SHADOWCLASS shadowClass = MapRDSLevelToTSShadowSetting( level );
  280. BOOL bSuccess;
  281. DWORD dwLength;
  282. DWORD dwStatus;
  283. memset( &winstationConfig, 0, sizeof(winstationConfig) );
  284. bSuccess = WinStationQueryInformation(
  285. WTS_CURRENT_SERVER,
  286. ulSessionId,
  287. WinStationConfiguration,
  288. &winstationConfig,
  289. sizeof(winstationConfig),
  290. &dwLength
  291. );
  292. if( TRUE == bSuccess )
  293. {
  294. winstationConfig.User.Shadow = shadowClass;
  295. bSuccess = WinStationSetInformation(
  296. WTS_CURRENT_SERVER,
  297. ulSessionId,
  298. WinStationConfiguration,
  299. &winstationConfig,
  300. sizeof(winstationConfig)
  301. );
  302. }
  303. if( TRUE == bSuccess )
  304. {
  305. dwStatus = ERROR_SUCCESS;
  306. }
  307. else
  308. {
  309. dwStatus = GetLastError();
  310. }
  311. return dwStatus;
  312. }
  313. HRESULT
  314. PolicyGetMaxTicketExpiry(
  315. LONG* value
  316. )
  317. /*++
  318. --*/
  319. {
  320. HRESULT hRes;
  321. CComPtr<IRARegSetting> IRegSetting;
  322. hRes = IRegSetting.CoCreateInstance(
  323. CLSID_RARegSetting,
  324. NULL,
  325. CLSCTX_LOCAL_SERVER | CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_DISABLE_AAA
  326. );
  327. if( SUCCEEDED(hRes) )
  328. {
  329. hRes = IRegSetting->get_MaxTicketExpiry(value);
  330. }
  331. MYASSERT( SUCCEEDED(hRes) );
  332. return hRes;
  333. }