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.

310 lines
9.6 KiB

  1. // MDServiceProvider.cpp : Implementation of CMDServiceProvider
  2. #include "stdafx.h"
  3. #include "MsPMSP.h"
  4. #include "MDServiceProvider.h"
  5. #include "MDSPEnumDevice.h"
  6. #include "MdspDefs.h"
  7. #include "loghelp.h"
  8. #include "key.h"
  9. #include "resource.h"
  10. #include "serialnumber.h"
  11. #include <WMDMUtil.h>
  12. static const GUID g_DiskClassGuid =
  13. { 0x4d36e967, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
  14. HDEVNOTIFY g_hDevNotify=NULL;
  15. LRESULT CALLBACK MDSPPnPproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  16. {
  17. switch(message)
  18. {
  19. case WM_DEVICECHANGE:
  20. MDSPProcessDeviceChange(wParam, lParam);
  21. return 0;
  22. case WM_CREATE:
  23. return (DoRegisterDeviceInterface(hwnd, g_DiskClassGuid, &g_hDevNotify));
  24. case WM_DESTROY:
  25. PostQuitMessage(0);
  26. if( g_hDevNotify )
  27. DoUnregisterDeviceInterface(g_hDevNotify);
  28. return 0L;
  29. default:
  30. return DefWindowProc(hwnd, message, wParam, lParam);
  31. } //end switch of messages passed to callback
  32. }
  33. DWORD MDSPThreadProc(LPVOID lpParam)
  34. {
  35. static char appname[]="PMSPPnPN";
  36. MSG message;
  37. WNDCLASSEX windowclass;
  38. HWND hWnd;
  39. windowclass.style = CS_HREDRAW | CS_VREDRAW;
  40. windowclass.lpfnWndProc = MDSPPnPproc;
  41. windowclass.cbClsExtra = 0;
  42. windowclass.cbWndExtra = 0;
  43. windowclass.cbSize = sizeof(WNDCLASSEX);
  44. windowclass.hInstance = g_hinstance;
  45. windowclass.hIcon = NULL; // LoadIcon(NULL, IDI_APPLICATION);
  46. windowclass.hIconSm= NULL; // LoadIcon(NULL, IDI_APPLICATION);
  47. windowclass.hCursor = NULL; // LoadCursor(NULL, IDC_ARROW);
  48. windowclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
  49. windowclass.lpszMenuName=NULL;
  50. windowclass.lpszClassName=appname;
  51. RegisterClassEx(&windowclass);
  52. hWnd = CreateWindow (appname, "PMSP PnP Notify",
  53. WS_OVERLAPPEDWINDOW,
  54. CW_USEDEFAULT,
  55. CW_USEDEFAULT,
  56. CW_USEDEFAULT,
  57. CW_USEDEFAULT,
  58. NULL, NULL, g_hinstance, NULL);
  59. // ShowWindow(hWnd, SW_SHOWNORMAL);
  60. UpdateWindow(hWnd);
  61. while(GetMessage(&message, NULL, 0,0))
  62. {
  63. TranslateMessage(&message); //get key events
  64. DispatchMessage(&message);
  65. }
  66. return (DWORD)message.wParam;
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CMDServiceProvider
  70. CMDServiceProvider::~CMDServiceProvider()
  71. {
  72. if( m_hThread )
  73. CloseHandle(m_hThread);
  74. if (g_pAppSCServer)
  75. {
  76. delete g_pAppSCServer;
  77. g_pAppSCServer = NULL;
  78. }
  79. // UtilStartStopService(false);
  80. }
  81. CMDServiceProvider::CMDServiceProvider()
  82. {
  83. // HRESULT hr;
  84. //Temporary: read start drive from Registry
  85. #ifdef MDSP_TEMP
  86. HKEY hKey;
  87. DWORD dwType, dwSize=4;
  88. g_dwStartDrive=1;
  89. if( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, STR_MDSPREG,
  90. 0, KEY_READ, &hKey))
  91. {
  92. RegQueryValueEx(hKey,"StartDrive",0, &dwType, (LPBYTE)&g_dwStartDrive, &dwSize);
  93. RegCloseKey(hKey);
  94. }
  95. #else
  96. g_dwStartDrive=0;
  97. #endif
  98. g_pAppSCServer = new CSecureChannelServer();
  99. if (g_pAppSCServer)
  100. {
  101. /* Beta AppCert and PVK
  102. const BYTE abPVK[] = {
  103. 0x61, 0x21, 0xF8, 0xE5, 0x64, 0xD9, 0x69, 0x9A,
  104. 0xC0, 0x3F, 0xC6, 0x1C, 0xF9, 0x6B, 0xFB, 0x4F,
  105. 0x7A, 0x1D, 0x11, 0x6E
  106. };
  107. const BYTE pCert[] = {
  108. 0x00, 0x01, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  109. 0x2D, 0x40, 0x51, 0x5B, 0xC6, 0x85, 0x6F, 0xF9,
  110. 0x22, 0x2C, 0x60, 0x15, 0xE7, 0x15, 0xA8, 0x96,
  111. 0x0F, 0xCC, 0xC8, 0x5D, 0x22, 0x64, 0x4C, 0xB8,
  112. 0xC8, 0xD2, 0x7D, 0x0B, 0xAC, 0x71, 0x30, 0x7B,
  113. 0xF9, 0x1C, 0x6C, 0xE6, 0xAD, 0xA1, 0x43, 0x87,
  114. 0x38, 0x35, 0xA2, 0xAC, 0xA3, 0x84, 0x1B, 0x82,
  115. 0xD5, 0xFA, 0xAE, 0xF2, 0xEA, 0x23, 0xA3, 0xE2,
  116. 0x03, 0x71, 0x14, 0x5B, 0x01, 0x9A, 0x6A, 0x3A,
  117. 0x00, 0x57, 0x89, 0xF3, 0x44, 0x20, 0xD7, 0x9F,
  118. 0xDB, 0xDE, 0xE9, 0x14, 0x62, 0xB9, 0x2A, 0x49,
  119. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8,
  120. 0x00, 0x00, 0x00, 0x02
  121. };
  122. */
  123. /*
  124. // RTM AppCert and PVK (Subject ID 5, AppSec 1000)
  125. const BYTE abPVK[] = {
  126. 0xB3, 0x2B, 0x3E, 0xE4, 0x01, 0x18, 0xCE, 0x7A,
  127. 0x91, 0x04, 0xB6, 0xE6, 0xC3, 0xF7, 0x30, 0x04,
  128. 0x3C, 0xAA, 0x67, 0x13
  129. };
  130. const BYTE pCert[] = {
  131. 0x00, 0x01, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  132. 0x25, 0x8D, 0x2F, 0x88, 0x21, 0xA6, 0xC4, 0x8F,
  133. 0xE0, 0x01, 0x62, 0x88, 0x1D, 0x09, 0x1F, 0x5F,
  134. 0xDF, 0xC6, 0xA6, 0x42, 0xD9, 0x49, 0x7F, 0x86,
  135. 0x71, 0x3F, 0x5F, 0x39, 0x19, 0x0B, 0xA1, 0xDB,
  136. 0x27, 0x33, 0x68, 0x0B, 0x1B, 0x6E, 0x78, 0x0E,
  137. 0xEC, 0x8A, 0xBB, 0x35, 0xD1, 0x0A, 0x8D, 0x58,
  138. 0x24, 0x90, 0x8D, 0x71, 0x8F, 0x16, 0x5B, 0x64,
  139. 0x52, 0x7C, 0xB3, 0x38, 0xD6, 0x51, 0x1B, 0x60,
  140. 0xB0, 0x03, 0xD6, 0x04, 0x1A, 0xC9, 0x35, 0x4F,
  141. 0x9B, 0x3A, 0x45, 0xDA, 0x94, 0x11, 0x4F, 0x0D,
  142. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8,
  143. 0x00, 0x00, 0x00, 0x05
  144. };
  145. */
  146. g_pAppSCServer->SetCertificate(SAC_CERT_V1, (BYTE*)g_abAppCert, sizeof(g_abAppCert), (BYTE*)g_abPriv, sizeof(g_abPriv));
  147. }
  148. g_bIsWinNT=IsWinNT();
  149. //----------------------------------------------------------
  150. // PnP Notification Code, removed for public beta release
  151. //----------------------------------------------------------
  152. // m_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MDSPThreadProc,
  153. // NULL, 0, &m_dwThreadID);
  154. // CWRg(m_hThread);
  155. m_hThread = NULL;
  156. g_CriticalSection.Lock();
  157. ZeroMemory(g_NotifyInfo, sizeof(MDSPNOTIFYINFO)*MDSP_MAX_DEVICE_OBJ);
  158. ZeroMemory(g_GlobalDeviceInfo, sizeof(MDSPGLOBALDEVICEINFO)*MDSP_MAX_DEVICE_OBJ);
  159. g_CriticalSection.Unlock();
  160. //Error:
  161. //
  162. // This call starts the Wmdm PM service is required.
  163. // This is now done by the library on demand.
  164. // UtilStartStopService(true);
  165. return;
  166. }
  167. STDMETHODIMP CMDServiceProvider::GetDeviceCount(DWORD * pdwCount)
  168. {
  169. HRESULT hr=E_FAIL;
  170. char str[8]="c:";
  171. CFRg(g_pAppSCServer);
  172. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  173. {
  174. CORg(WMDM_E_NOTCERTIFIED);
  175. }
  176. CARg(pdwCount);
  177. int i, cnt;
  178. for(i=g_dwStartDrive, cnt=0; i<MDSP_MAX_DRIVE_COUNT; i++)
  179. {
  180. str[0] = 'A' + i;
  181. if( UtilGetDriveType(str) == DRIVE_REMOVABLE ) cnt ++;
  182. }
  183. *pdwCount = cnt;
  184. hr=S_OK;
  185. Error:
  186. hrLogDWORD("IMDServiceProvider::GetDeviceCount returned 0x%08lx", hr, hr);
  187. return hr;
  188. }
  189. STDMETHODIMP CMDServiceProvider::EnumDevices(IMDSPEnumDevice * * ppEnumDevice)
  190. {
  191. HRESULT hr=E_FAIL;
  192. CFRg(g_pAppSCServer);
  193. if ( !(g_pAppSCServer->fIsAuthenticated()) )
  194. {
  195. CORg(WMDM_E_NOTCERTIFIED);
  196. }
  197. CARg(ppEnumDevice);
  198. CComObject<CMDSPEnumDevice> *pEnumObj;
  199. hr=CComObject<CMDSPEnumDevice>::CreateInstance(&pEnumObj);
  200. if( SUCCEEDED(hr) )
  201. {
  202. hr=pEnumObj->QueryInterface(IID_IMDSPEnumDevice, reinterpret_cast<void**>(ppEnumDevice));
  203. if( FAILED(hr) )
  204. delete pEnumObj;
  205. }
  206. Error:
  207. hrLogDWORD("IMDServiceProvider::EnumDevices returned 0x%08lx", hr, hr);
  208. return hr;
  209. }
  210. STDMETHODIMP CMDServiceProvider::SACAuth(DWORD dwProtocolID,
  211. DWORD dwPass,
  212. BYTE *pbDataIn,
  213. DWORD dwDataInLen,
  214. BYTE **ppbDataOut,
  215. DWORD *pdwDataOutLen)
  216. {
  217. HRESULT hr=E_FAIL;
  218. if (g_pAppSCServer)
  219. hr = g_pAppSCServer->SACAuth(dwProtocolID, dwPass, pbDataIn, dwDataInLen, ppbDataOut, pdwDataOutLen);
  220. else
  221. hr = E_FAIL;
  222. // Error:
  223. hrLogDWORD("IComponentAuthenticate::SACAuth returned 0x%08lx", hr, hr);
  224. return hr;
  225. }
  226. STDMETHODIMP CMDServiceProvider::SACGetProtocols(DWORD **ppdwProtocols,
  227. DWORD *pdwProtocolCount)
  228. {
  229. HRESULT hr=E_FAIL;
  230. if (g_pAppSCServer)
  231. hr = g_pAppSCServer->SACGetProtocols(ppdwProtocols, pdwProtocolCount);
  232. else
  233. hr = E_FAIL;
  234. // Error:
  235. hrLogDWORD("IComponentAuthenticate::SACGetProtocols returned 0x%08lx", hr, hr);
  236. return hr;
  237. }
  238. // IMDSPRevoked
  239. HRESULT CMDServiceProvider::GetRevocationURL( IN OUT LPWSTR* ppwszRevocationURL,
  240. IN OUT DWORD* pdwBufferLen )
  241. {
  242. HRESULT hr = S_OK;
  243. DWORD pdwSubjectIDs[2];
  244. // Check arguments passed in
  245. if( ppwszRevocationURL == NULL || pdwBufferLen == NULL )
  246. {
  247. hr = E_POINTER;
  248. goto Error;
  249. }
  250. // Always use the MS site to update this SP.
  251. if( ::IsMicrosoftRevocationURL( *ppwszRevocationURL ) ) return S_OK;
  252. // Build a new URL from our subject ID
  253. pdwSubjectIDs[0] = ::GetSubjectIDFromAppCert( *(APPCERT*)g_abAppCert );
  254. pdwSubjectIDs[1] = 0;
  255. CORg( ::BuildRevocationURL( pdwSubjectIDs, ppwszRevocationURL, pdwBufferLen ) );
  256. Error:
  257. return hr;
  258. }