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.

243 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. Terminal Server ACL module
  5. Abstract:
  6. This module implements ACL checking for the ISAPI extension.
  7. Author:
  8. Marc Reyhner 9/7/2000
  9. --*/
  10. #include "stdafx.h"
  11. #include <atlbase.h>
  12. #include <atlconv.h>
  13. #include "tsproxyacl.h"
  14. #include "tsproxy.h"
  15. #define USER_ACL_KEY TS_PROXY_REG_KEY _T("\\User Permissions")
  16. #define DENY_LIST _T("Server Deny List")
  17. #define ALLOW_LIST _T("Server Allow List")
  18. #define DENY_BY_DEFAULT _T("Deny By Default")
  19. // our local helper function
  20. static BOOL ServerOnDenyList(LPSTR server);
  21. static BOOL ServerOnAllowList(LPSTR server);
  22. static BOOL CheckUserPermissions(LPSTR server, LPSTR user);
  23. static DWORD GetRegDword(LPTSTR key, LPTSTR value, DWORD dwDefault);
  24. static LPTSTR GetRegMultiString(LPTSTR key, LPTSTR value);
  25. static LPVOID GetAndAllocateRegValue(LPTSTR key, LPTSTR value, DWORD type);
  26. BOOL
  27. VerifyServerAccess(
  28. IN LPEXTENSION_CONTROL_BLOCK lpECB,
  29. IN LPSTR server,
  30. IN LPSTR user
  31. )
  32. /*++
  33. Routine Description:
  34. This checks to see if the user is allowed access to the
  35. given server
  36. Arguments:
  37. lpECB - extension control block
  38. server - Server access is requested to
  39. user - User requesting access
  40. Return Value:
  41. TRUE - Access is allowed
  42. FALSE - Access is denied
  43. --*/
  44. {
  45. if (0 == GetRegDword(TS_PROXY_REG_KEY,DENY_BY_DEFAULT,0)) {
  46. if (ServerOnDenyList(server)) {
  47. return FALSE;
  48. }
  49. // allow by default so return TRUE since they aren't on
  50. // the deny list
  51. return TRUE;
  52. }
  53. if (ServerOnDenyList(server)) {
  54. return FALSE;
  55. }
  56. if (ServerOnAllowList(server)) {
  57. return TRUE;
  58. }
  59. return CheckUserPermissions(server,user);
  60. }
  61. BOOL ServerOnDenyList(LPSTR server)
  62. {
  63. USES_CONVERSION;
  64. LPTSTR serverList;
  65. LPTSTR currentServer;
  66. UINT serverLength;
  67. serverList = GetRegMultiString(TS_PROXY_REG_KEY,DENY_LIST);
  68. if (!serverList) {
  69. return FALSE;
  70. }
  71. currentServer = serverList;
  72. while (serverLength = _tcslen(currentServer)) {
  73. if (_tcsicmp(A2T(server),currentServer)==0) {
  74. HeapFree(GetProcessHeap(),0,serverList);
  75. return TRUE;
  76. }
  77. currentServer += (serverLength+1);
  78. }
  79. HeapFree(GetProcessHeap(),0,serverList);
  80. return FALSE;
  81. }
  82. BOOL ServerOnAllowList(LPSTR server)
  83. {
  84. USES_CONVERSION;
  85. LPTSTR serverList;
  86. LPTSTR currentServer;
  87. UINT serverLength;
  88. serverList = GetRegMultiString(TS_PROXY_REG_KEY,ALLOW_LIST);
  89. if (!serverList) {
  90. return FALSE;
  91. }
  92. currentServer = serverList;
  93. while (serverLength = _tcslen(currentServer)) {
  94. if (_tcsicmp(A2T(server),currentServer)==0) {
  95. HeapFree(GetProcessHeap(),0,serverList);
  96. return TRUE;
  97. }
  98. currentServer += (serverLength+1);
  99. }
  100. HeapFree(GetProcessHeap(),0,serverList);
  101. return FALSE;
  102. }
  103. BOOL CheckUserPermissions(LPSTR server, LPSTR user)
  104. {
  105. USES_CONVERSION;
  106. LPTSTR serverList;
  107. LPTSTR currentServer;
  108. UINT serverLength;
  109. serverList = GetRegMultiString(USER_ACL_KEY,A2T(user));
  110. if (!serverList) {
  111. return FALSE;
  112. }
  113. currentServer = serverList;
  114. while (serverLength = _tcslen(currentServer)) {
  115. if (_tcsicmp(A2T(server),currentServer)==0) {
  116. HeapFree(GetProcessHeap(),0,serverList);
  117. return TRUE;
  118. }
  119. currentServer += (serverLength+1);
  120. }
  121. HeapFree(GetProcessHeap(),0,serverList);
  122. return FALSE;
  123. }
  124. DWORD GetRegDword(LPTSTR key, LPTSTR value, DWORD dwDefault)
  125. {
  126. DWORD dwResult;
  127. LPDWORD lpdwRegValue;
  128. lpdwRegValue = (LPDWORD)GetAndAllocateRegValue(key,value,REG_DWORD);
  129. if (lpdwRegValue) {
  130. dwResult = *lpdwRegValue;
  131. HeapFree(GetProcessHeap(),0,lpdwRegValue);
  132. } else {
  133. dwResult = dwDefault;
  134. }
  135. return dwResult;
  136. }
  137. LPTSTR GetRegMultiString(LPTSTR key, LPTSTR value)
  138. {
  139. return (LPTSTR)GetAndAllocateRegValue(key,value,REG_MULTI_SZ);
  140. }
  141. LPVOID GetAndAllocateRegValue(LPTSTR key, LPTSTR value, DWORD type)
  142. {
  143. LONG retCode;
  144. HKEY hRegKey;
  145. LPBYTE lpValueData;
  146. DWORD dwValueSize;
  147. DWORD dwType;
  148. BOOL failure;
  149. hRegKey = NULL;
  150. failure = FALSE;
  151. lpValueData = NULL;
  152. retCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,key,0,KEY_QUERY_VALUE,&hRegKey);
  153. if (ERROR_SUCCESS != retCode) {
  154. failure = TRUE;
  155. goto CLEANUP_AND_EXIT;
  156. }
  157. retCode = RegQueryValueEx(hRegKey,value,NULL,&dwType,NULL,&dwValueSize);
  158. if (retCode != ERROR_SUCCESS) {
  159. failure = TRUE;
  160. goto CLEANUP_AND_EXIT;
  161. }
  162. // Make sure the data is the type they requested
  163. if (dwType != type) {
  164. failure = TRUE;
  165. goto CLEANUP_AND_EXIT;
  166. }
  167. lpValueData = (LPBYTE)HeapAlloc(GetProcessHeap(),0,dwValueSize);
  168. if (!lpValueData) {
  169. failure = TRUE;
  170. goto CLEANUP_AND_EXIT;
  171. }
  172. retCode = RegQueryValueEx(hRegKey,value,NULL,&dwType,lpValueData,
  173. &dwValueSize);
  174. if (retCode != ERROR_SUCCESS) {
  175. failure = TRUE;
  176. goto CLEANUP_AND_EXIT;
  177. }
  178. CLEANUP_AND_EXIT:
  179. if (failure) {
  180. if (lpValueData) {
  181. HeapFree(GetProcessHeap(),0,lpValueData);
  182. lpValueData = NULL;
  183. }
  184. }
  185. if (hRegKey) {
  186. RegCloseKey(hRegKey);
  187. }
  188. return lpValueData;
  189. }