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.

273 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 2001-2002 Microsoft Corporation
  3. Module Name:
  4. ISA.cpp
  5. Abstract:
  6. The ISA setup needs to successfully open the SharedAccess service and get the
  7. its status in order to succeed. But on whistler we remove this from advanced
  8. server since it's a consumer feature so the ISA setup bails out.
  9. We fake the service API call return values to make the ISA setup happy.
  10. History:
  11. 04/24/2001 maonis Created
  12. 02/15/2002 robkenny Conversion to CompareString was incorrect
  13. Security review.
  14. --*/
  15. #include "precomp.h"
  16. IMPLEMENT_SHIM_BEGIN(ISA)
  17. #include "ShimHookMacro.h"
  18. APIHOOK_ENUM_BEGIN
  19. APIHOOK_ENUM_ENTRY(OpenServiceA)
  20. APIHOOK_ENUM_ENTRY(OpenServiceW)
  21. APIHOOK_ENUM_ENTRY(QueryServiceStatus)
  22. APIHOOK_ENUM_ENTRY(QueryServiceConfigA)
  23. APIHOOK_ENUM_ENTRY(ChangeServiceConfigA)
  24. APIHOOK_ENUM_ENTRY(CloseServiceHandle)
  25. APIHOOK_ENUM_END
  26. SC_HANDLE BogusSharedAccessHandle = (SC_HANDLE)0xBAADF00D;
  27. /*++
  28. Abstract:
  29. This checks to see if the service is being opened is SharedAccess.
  30. If so we simply return a fake handle.
  31. History:
  32. 04/24/2001 maonis Created
  33. --*/
  34. SC_HANDLE
  35. APIHOOK(OpenServiceA)(
  36. SC_HANDLE hSCManager, // handle to SCM database
  37. LPCSTR lpServiceName, // service name
  38. DWORD dwDesiredAccess // access
  39. )
  40. {
  41. DPFN(eDbgLevelInfo, "Calling OpenServiceA on %s", lpServiceName);
  42. SC_HANDLE hService = ORIGINAL_API(OpenServiceA)(hSCManager, lpServiceName, dwDesiredAccess);
  43. if (hService == NULL)
  44. {
  45. if (lpServiceName)
  46. {
  47. // Check to see if the app is attempting to open the SharedAccess service,
  48. // if it is, fake success by returning a bogus (non-NULL) handle
  49. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  50. if ((CompareStringA(lcid, NORM_IGNORECASE, lpServiceName, -1, "SharedAccess", -1) == CSTR_EQUAL))
  51. {
  52. LOGN(eDbgLevelError, "App is attempting to open the SharedAccess server, faking success");
  53. return BogusSharedAccessHandle;
  54. }
  55. }
  56. }
  57. return hService;
  58. }
  59. SC_HANDLE
  60. APIHOOK(OpenServiceW)(
  61. SC_HANDLE hSCManager, // handle to SCM database
  62. LPCWSTR lpServiceName, // service name
  63. DWORD dwDesiredAccess // access
  64. )
  65. {
  66. DPFN(eDbgLevelInfo, "Calling OpenServiceW on %S", lpServiceName);
  67. SC_HANDLE hService = ORIGINAL_API(OpenServiceW)(hSCManager, lpServiceName, dwDesiredAccess);
  68. if (hService == NULL)
  69. {
  70. // Check to see if the app is attempting to open the SharedAccess service,
  71. // if it is, fake success by returning a bogus (non-NULL) handle
  72. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  73. if ((CompareStringW(lcid, NORM_IGNORECASE, lpServiceName, -1, L"SharedAccess", -1) == CSTR_EQUAL))
  74. {
  75. LOGN(eDbgLevelError, "App is attempting to open the SharedAccess server, faking success");
  76. return BogusSharedAccessHandle;
  77. }
  78. }
  79. return hService;
  80. }
  81. /*++
  82. Abstract:
  83. This checks to see if the service handle is 0xBAADF00D, if so just sets
  84. the service status to SERVICE_STOPPED.
  85. History:
  86. 04/24/2001 maonis Created
  87. --*/
  88. BOOL
  89. APIHOOK(QueryServiceStatus)(
  90. SC_HANDLE hService, // handle to service
  91. LPSERVICE_STATUS lpServiceStatus // service status
  92. )
  93. {
  94. if (hService == BogusSharedAccessHandle)
  95. {
  96. lpServiceStatus->dwCurrentState = SERVICE_STOPPED;
  97. return TRUE;
  98. }
  99. else
  100. {
  101. return ORIGINAL_API(QueryServiceStatus)(hService, lpServiceStatus);
  102. }
  103. }
  104. /*++
  105. Abstract:
  106. ISA calls this API first with a NULL lpServiceConfig to get the size
  107. of the buffer needs to be allocated for the structure; then it calls
  108. the API again with the pointer to the structure.
  109. History:
  110. 05/07/2001 maonis Created
  111. --*/
  112. BOOL
  113. APIHOOK(QueryServiceConfigA)(
  114. SC_HANDLE hService, // handle to service
  115. LPQUERY_SERVICE_CONFIGA lpServiceConfig, // buffer
  116. DWORD cbBufSize, // size of buffer
  117. LPDWORD pcbBytesNeeded // bytes needed
  118. )
  119. {
  120. if (hService == BogusSharedAccessHandle)
  121. {
  122. if (lpServiceConfig)
  123. {
  124. lpServiceConfig->lpDependencies = NULL;
  125. return TRUE;
  126. }
  127. else
  128. {
  129. *pcbBytesNeeded = sizeof(QUERY_SERVICE_CONFIGA);
  130. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  131. return TRUE;
  132. }
  133. }
  134. else
  135. {
  136. return ORIGINAL_API(QueryServiceConfigA)(hService, lpServiceConfig, cbBufSize, pcbBytesNeeded);
  137. }
  138. }
  139. /*++
  140. Abstract:
  141. We simply make this API succeed when hService is 0xBAADF00D.
  142. History:
  143. 05/07/2001 maonis Created
  144. --*/
  145. BOOL
  146. APIHOOK(ChangeServiceConfigA)(
  147. SC_HANDLE hService, // handle to service
  148. DWORD dwServiceType, // type of service
  149. DWORD dwStartType, // when to start service
  150. DWORD dwErrorControl, // severity of start failure
  151. LPCSTR lpBinaryPathName, // service binary file name
  152. LPCSTR lpLoadOrderGroup, // load ordering group name
  153. LPDWORD lpdwTagId, // tag identifier
  154. LPCSTR lpDependencies, // array of dependency names
  155. LPCSTR lpServiceStartName, // account name
  156. LPCSTR lpPassword, // account password
  157. LPCSTR lpDisplayName // display name
  158. )
  159. {
  160. if (hService == BogusSharedAccessHandle)
  161. {
  162. return TRUE;
  163. }
  164. else
  165. {
  166. return ORIGINAL_API(ChangeServiceConfigA)(
  167. hService,
  168. dwServiceType,
  169. dwStartType,
  170. dwErrorControl,
  171. lpBinaryPathName,
  172. lpLoadOrderGroup,
  173. lpdwTagId,
  174. lpDependencies,
  175. lpServiceStartName,
  176. lpPassword,
  177. lpDisplayName);
  178. }
  179. }
  180. /*++
  181. Abstract:
  182. This checks to see if the service handle is 0xBAADF00D, if so simply return
  183. History:
  184. 04/24/2001 maonis Created
  185. --*/
  186. BOOL
  187. APIHOOK(CloseServiceHandle)(
  188. SC_HANDLE hSCObject // handle to service or SCM object
  189. )
  190. {
  191. if (hSCObject == BogusSharedAccessHandle)
  192. {
  193. return TRUE;
  194. }
  195. else
  196. {
  197. return ORIGINAL_API(CloseServiceHandle)(hSCObject);
  198. }
  199. }
  200. /*++
  201. Register hooked functions
  202. --*/
  203. HOOK_BEGIN
  204. APIHOOK_ENTRY(Advapi32.DLL, OpenServiceA)
  205. APIHOOK_ENTRY(Advapi32.DLL, OpenServiceW)
  206. APIHOOK_ENTRY(Advapi32.DLL, QueryServiceStatus)
  207. APIHOOK_ENTRY(Advapi32.DLL, QueryServiceConfigA)
  208. APIHOOK_ENTRY(Advapi32.DLL, ChangeServiceConfigA)
  209. APIHOOK_ENTRY(Advapi32.DLL, CloseServiceHandle)
  210. HOOK_END
  211. IMPLEMENT_SHIM_END