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.

328 lines
6.8 KiB

  1. #include "stdafx.h"
  2. #include "rapisp.h"
  3. #include "devenum.h"
  4. #include <initguid.h>
  5. #include "dccole.h"
  6. #include "key.h"
  7. //#include "findleak.h"
  8. //DECLARE_THIS_FILE;
  9. //
  10. // Final Construction
  11. //
  12. CSecureChannelServer *g_pAppSCServer=NULL;
  13. HRESULT CRapiDevice::FinalConstruct()
  14. {
  15. HRESULT hr;
  16. m_pSink = NULL;
  17. hr = CComDccSink::CreateInstance( &m_pSink );
  18. m_spSink = m_pSink;
  19. if( SUCCEEDED( hr ) )
  20. {
  21. hr = m_pSink->Initialize();
  22. }
  23. if( SUCCEEDED( hr ) )
  24. {
  25. g_pAppSCServer = new CSecureChannelServer();
  26. if (g_pAppSCServer)
  27. {
  28. g_pAppSCServer->SetCertificate(SAC_CERT_V1, (BYTE*)g_abAppCert, sizeof(g_abAppCert), (BYTE*)g_abPriv, sizeof(g_abPriv));
  29. }
  30. }
  31. return( hr );
  32. }
  33. //
  34. // Final Release
  35. //
  36. void CRapiDevice::FinalRelease()
  37. {
  38. if( m_pSink )
  39. {
  40. m_pSink->Shutdown();
  41. }
  42. delete g_pAppSCServer;
  43. g_pAppSCServer = NULL;
  44. }
  45. //
  46. // IMDServiceProvider
  47. //
  48. STDMETHODIMP CRapiDevice::GetDeviceCount ( DWORD *pdwCount )
  49. {
  50. HRESULT hr = S_OK;
  51. CE_FIND_DATA *rgFindData = NULL;
  52. DWORD cItems = 0;
  53. if( NULL == pdwCount )
  54. {
  55. return( E_POINTER );
  56. }
  57. *pdwCount = 0;
  58. if( _Module.g_fDeviceConnected != FALSE )
  59. {
  60. if( !CeFindAllFiles( L"\\*.*",
  61. FAF_ATTRIBUTES | FAF_FOLDERS_ONLY,
  62. &cItems,
  63. &rgFindData ) )
  64. {
  65. hr = HRESULT_FROM_WIN32( CeGetLastError() );
  66. if( SUCCEEDED( hr ) )
  67. {
  68. hr = CeRapiGetError();
  69. }
  70. }
  71. if( SUCCEEDED( hr ) )
  72. {
  73. while( cItems-- )
  74. {
  75. //
  76. // Temp directories are storage cards according to CE group
  77. //
  78. if( rgFindData[cItems].dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY )
  79. {
  80. (*pdwCount)++;
  81. }
  82. }
  83. }
  84. if( SUCCEEDED( hr ) )
  85. {
  86. //
  87. // Count one for the CE device itself
  88. //
  89. (*pdwCount)++;
  90. }
  91. }
  92. if( NULL != rgFindData )
  93. {
  94. CeRapiFreeBuffer( rgFindData );
  95. }
  96. return( hr );
  97. }
  98. STDMETHODIMP CRapiDevice::EnumDevices ( IMDSPEnumDevice ** ppEnumDevice )
  99. {
  100. CComDevice **rgDevices = NULL;
  101. CE_FIND_DATA *rgFindData = NULL;
  102. DWORD dwDevCount = 0;
  103. DWORD cItems;
  104. HRESULT hr = S_OK;
  105. UINT i = 0;
  106. CComEnumDevice *pNewEnum;
  107. CComPtr<IMDSPEnumDevice> spEnum;
  108. if( NULL == ppEnumDevice )
  109. {
  110. return( E_POINTER );
  111. }
  112. *ppEnumDevice = NULL;
  113. if( !_Module.g_fDeviceConnected )
  114. {
  115. UINT iTryCount =0;
  116. for ( iTryCount = 0; iTryCount < 50; iTryCount++ )
  117. {
  118. Sleep( 100 );
  119. if( _Module.g_fDeviceConnected )
  120. {
  121. break;
  122. }
  123. }
  124. if( !_Module.g_fDeviceConnected )
  125. {
  126. // TODO: What to do here? Doc's don't specify
  127. return( E_FAIL );
  128. }
  129. }
  130. #ifdef ATTEMPT_DEVICE_CONNECTION_NOTIFICATION
  131. _Module.g_fInitialAttempt = FALSE;
  132. #endif
  133. if( !CeFindAllFiles( L"\\*.*",
  134. FAF_ATTRIBUTES | FAF_FOLDERS_ONLY | FAF_NAME,
  135. &cItems,
  136. &rgFindData ) )
  137. {
  138. hr = HRESULT_FROM_WIN32( CeGetLastError() );
  139. if( SUCCEEDED( hr ) )
  140. {
  141. hr = CeRapiGetError();
  142. }
  143. }
  144. if( SUCCEEDED( hr ) )
  145. {
  146. for( i = 0; i < cItems; i++ )
  147. {
  148. if( rgFindData[i].dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY )
  149. {
  150. dwDevCount++;
  151. }
  152. }
  153. }
  154. if( SUCCEEDED( hr ) )
  155. {
  156. dwDevCount++;
  157. }
  158. if( SUCCEEDED( hr ) )
  159. {
  160. rgDevices = new CComDevice*[dwDevCount]; // NOTE: Currently there can only be 1 CE Device connected
  161. if( NULL == rgDevices )
  162. {
  163. hr = E_OUTOFMEMORY;
  164. }
  165. }
  166. if( SUCCEEDED( hr ) )
  167. {
  168. for( i = 0; i < dwDevCount; i++ )
  169. {
  170. rgDevices[0] = NULL;
  171. }
  172. }
  173. //
  174. // Initialize the CE Device Itself
  175. //
  176. if( SUCCEEDED(hr) )
  177. {
  178. hr = CComDevice::CreateInstance( &rgDevices[0] );
  179. if( SUCCEEDED( hr ) )
  180. {
  181. rgDevices[0]->AddRef();
  182. hr = rgDevices[0]->Init(L"\\");
  183. }
  184. }
  185. //
  186. // Initialize all the storage cards
  187. //
  188. if( SUCCEEDED(hr) )
  189. {
  190. dwDevCount = 0;
  191. for( i = 0; i < cItems && SUCCEEDED( hr ) ; i++ )
  192. {
  193. if( rgFindData[i].dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY )
  194. {
  195. dwDevCount++;
  196. hr = CComDevice::CreateInstance( &rgDevices[dwDevCount] );
  197. if( SUCCEEDED( hr ) )
  198. {
  199. rgDevices[dwDevCount]->AddRef();
  200. hr = rgDevices[dwDevCount]->Init( rgFindData[i].cFileName );
  201. }
  202. }
  203. }
  204. }
  205. //
  206. // Initialize the enumeration class
  207. //
  208. if( SUCCEEDED(hr) )
  209. {
  210. dwDevCount++; // Add the CE Device itself
  211. hr = CComEnumDevice ::CreateInstance(&pNewEnum);
  212. spEnum = pNewEnum;
  213. }
  214. if( SUCCEEDED(hr) )
  215. {
  216. hr = pNewEnum->Init( rgDevices, dwDevCount );
  217. }
  218. if( SUCCEEDED(hr) )
  219. {
  220. *ppEnumDevice = spEnum;
  221. spEnum.Detach();
  222. }
  223. if( NULL != rgDevices )
  224. {
  225. for( i = 0; i < dwDevCount; i ++ )
  226. {
  227. if( rgDevices[i] )
  228. {
  229. rgDevices[i]->Release();
  230. }
  231. }
  232. delete [] rgDevices;
  233. }
  234. if( NULL != rgFindData )
  235. {
  236. CeRapiFreeBuffer( rgFindData );
  237. }
  238. return( hr );
  239. }
  240. //
  241. // IComponentAuthenticate
  242. //
  243. STDMETHODIMP CRapiDevice::SACAuth( DWORD dwProtocolID,
  244. DWORD dwPass,
  245. BYTE *pbDataIn,
  246. DWORD dwDataInLen,
  247. BYTE **ppbDataOut,
  248. DWORD *pdwDataOutLen)
  249. {
  250. HRESULT hr;
  251. if (g_pAppSCServer)
  252. hr = g_pAppSCServer->SACAuth(dwProtocolID, dwPass, pbDataIn, dwDataInLen, ppbDataOut, pdwDataOutLen);
  253. else
  254. hr = E_FAIL;
  255. return( hr );
  256. }
  257. STDMETHODIMP CRapiDevice::SACGetProtocols (DWORD **ppdwProtocols,
  258. DWORD *pdwProtocolCount)
  259. {
  260. HRESULT hr;
  261. if (g_pAppSCServer)
  262. hr = g_pAppSCServer->SACGetProtocols(ppdwProtocols, pdwProtocolCount);
  263. else
  264. hr = E_FAIL;
  265. return( hr );
  266. }