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.

429 lines
12 KiB

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1997-1998 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: sdoserviceias.cpp
  6. //
  7. // Project: Everest
  8. //
  9. // Description: SDO Service Class Implementation
  10. //
  11. // Author: TLP 9/1/98
  12. //
  13. ///////////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "sdoserviceias.h"
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CSdoServiceIAS Class Implementation
  18. /////////////////////////////////////////////////////////////////////////////
  19. /////////////////////////////////////////////////////////////////////////////
  20. CSdoServiceIAS::CSdoServiceIAS()
  21. : m_clsSdoServiceControlImpl(this),
  22. m_pAttachedMachine(NULL),
  23. m_bstrAttachedComputer(NULL)
  24. {
  25. m_PropertyStatus[0] = PROPERTY_UNINITIALIZED; // RADIUS Server Groups
  26. m_PropertyStatus[1] = PROPERTY_UNINITIALIZED; // Policies
  27. m_PropertyStatus[2] = PROPERTY_UNINITIALIZED; // Profiles
  28. m_PropertyStatus[3] = PROPERTY_UNINITIALIZED; // Protocols
  29. m_PropertyStatus[4] = PROPERTY_UNINITIALIZED; // Auditors
  30. m_PropertyStatus[5] = PROPERTY_UNINITIALIZED; // RequestHandlers
  31. m_PropertyStatus[6] = PROPERTY_UNINITIALIZED; // Proxy Policies
  32. m_PropertyStatus[7] = PROPERTY_UNINITIALIZED; // Proxy Profiles
  33. }
  34. /////////////////////////////////////////////////////////////////////////////
  35. CSdoServiceIAS::~CSdoServiceIAS()
  36. {
  37. if ( m_pAttachedMachine )
  38. m_pAttachedMachine->Release();
  39. if ( m_bstrAttachedComputer )
  40. SysFreeString(m_bstrAttachedComputer);
  41. }
  42. /////////////////////////////////////////////////////////////////////////////
  43. HRESULT CSdoServiceIAS::FinalInitialize(
  44. /*[in]*/ bool fInitNew,
  45. /*[in]*/ ISdoMachine* pAttachedMachine
  46. )
  47. {
  48. _ASSERT ( ! fInitNew );
  49. HRESULT hr;
  50. do
  51. {
  52. hr = GetPropertyInternal(PROPERTY_SDO_NAME, &m_ServiceName);
  53. if ( FAILED(hr) )
  54. {
  55. IASTracePrintf("Error in Service SDO - FinalInitialize() - Could not get the service name...");
  56. break;
  57. }
  58. hr = pAttachedMachine->GetAttachedComputer(&m_bstrAttachedComputer);
  59. if ( FAILED(hr) )
  60. {
  61. IASTracePrintf("Error in Service SDO - FinalInitialize() - Could not get name of attached computer...");
  62. break;
  63. }
  64. hr = LoadProperties();
  65. if ( FAILED(hr) )
  66. break;
  67. (m_pAttachedMachine = pAttachedMachine)->AddRef();
  68. }
  69. while (FALSE);
  70. return hr;
  71. }
  72. /////////////////////////////////////////////////////////////////////////////
  73. // Lazy initialization of IAS Service SDO properties
  74. //
  75. IAS_PROPERTY_INFO g_PropertyInfo[MAX_SERVICE_PROPERTIES] =
  76. {
  77. {
  78. PROPERTY_IAS_RADIUSSERVERGROUPS_COLLECTION,
  79. SDO_PROG_ID_RADIUSGROUP,
  80. DS_OBJECT_RADIUSGROUPS
  81. },
  82. {
  83. PROPERTY_IAS_POLICIES_COLLECTION,
  84. SDO_PROG_ID_POLICY,
  85. DS_OBJECT_POLICIES
  86. },
  87. {
  88. PROPERTY_IAS_PROFILES_COLLECTION,
  89. SDO_PROG_ID_PROFILE,
  90. DS_OBJECT_PROFILES
  91. },
  92. {
  93. PROPERTY_IAS_PROTOCOLS_COLLECTION,
  94. NULL,
  95. DS_OBJECT_PROTOCOLS
  96. },
  97. {
  98. PROPERTY_IAS_AUDITORS_COLLECTION,
  99. NULL,
  100. DS_OBJECT_AUDITORS
  101. },
  102. {
  103. PROPERTY_IAS_REQUESTHANDLERS_COLLECTION,
  104. NULL,
  105. DS_OBJECT_REQUESTHANDLERS
  106. },
  107. {
  108. PROPERTY_IAS_PROXYPOLICIES_COLLECTION,
  109. SDO_PROG_ID_POLICY,
  110. DS_OBJECT_PROXY_POLICIES
  111. },
  112. {
  113. PROPERTY_IAS_PROXYPROFILES_COLLECTION,
  114. SDO_PROG_ID_PROFILE,
  115. DS_OBJECT_PROXY_PROFILES
  116. }
  117. };
  118. HRESULT CSdoServiceIAS::InitializeProperty(LONG Id)
  119. {
  120. HRESULT hr = S_OK;
  121. do
  122. {
  123. if ( PROPERTY_SDO_START <= Id )
  124. {
  125. PIAS_PROPERTY_INFO pInfo = &g_PropertyInfo[Id - PROPERTY_SDO_START];
  126. _ASSERT ( pInfo->Id == Id );
  127. if ( PROPERTY_UNINITIALIZED == m_PropertyStatus[Id - PROPERTY_SDO_START] )
  128. {
  129. _ASSERT( pInfo->lpszDSContainerName );
  130. CComPtr<IDataStoreContainer> pDSContainerRoot;
  131. hr = m_pDSObject->QueryInterface(IID_IDataStoreContainer, (void**)&pDSContainerRoot);
  132. if ( FAILED(hr) )
  133. {
  134. IASTracePrintf("Error in Service SDO - InitializeProperty() - QueryInterface(1) failed...");
  135. break;
  136. }
  137. _bstr_t bstrContainerName = pInfo->lpszDSContainerName;
  138. CComPtr<IDataStoreObject> pDSObject;
  139. hr = pDSContainerRoot->Item(bstrContainerName, &pDSObject);
  140. if ( FAILED(hr) )
  141. {
  142. IASTracePrintf("Error in Service SDO - InitializeProperty() - Could not retrieve container: '%ls'...", bstrContainerName);
  143. break;
  144. }
  145. CComPtr<IDataStoreContainer> pDSContainerCollection;
  146. hr = pDSObject->QueryInterface(IID_IDataStoreContainer, (void**)&pDSContainerCollection);
  147. if ( FAILED(hr) )
  148. {
  149. IASTracePrintf("Error in Service SDO - InitializeProperty() - QueryInterface(2) failed...");
  150. break;
  151. }
  152. hr = InitializeCollection(
  153. pInfo->Id,
  154. pInfo->lpszItemProgId,
  155. m_pAttachedMachine,
  156. pDSContainerCollection
  157. );
  158. if ( FAILED(hr) )
  159. {
  160. IASTracePrintf("Error in Service SDO - InitializeProperty() - Could not initialize collection for: '%ls'...", bstrContainerName);
  161. break;
  162. }
  163. else
  164. {
  165. m_PropertyStatus[Id - PROPERTY_SDO_START] = PROPERTY_INITIALIZED;
  166. }
  167. }
  168. }
  169. } while ( FALSE );
  170. return hr;
  171. }
  172. //////////////////////////////////////////////////////////////////////////////
  173. LPCWSTR CSdoServiceIAS::GetServiceName(void)
  174. {
  175. if ( VT_EMPTY == V_VT(&m_ServiceName) )
  176. {
  177. HRESULT hr = GetPropertyInternal(PROPERTY_SDO_DATASTORE_NAME, &m_ServiceName);
  178. if ( FAILED(hr) )
  179. throw _com_error(hr);
  180. }
  181. return V_BSTR(&m_ServiceName);
  182. }
  183. /////////////////////////////////////////////////////////////////////////////
  184. //
  185. // FuncName: QueryInterfaceInternal()
  186. //
  187. // Description: Function called by AtlInternalQueryInterface() because
  188. // we used COM_INTERFACE_ENTRY_FUNC in the definition of
  189. // CRequest. Its purpose is to return a pointer to one
  190. // or the request object's "raw" interfaces.
  191. //
  192. // Preconditions: None
  193. //
  194. // Inputs: Defined by ATL COM_INTERFACE_ENTRY_FUNC macro
  195. //
  196. // Outputs: Defined by ATL COM_INTERFACE_ENTRY_FUNC macro
  197. //
  198. //////////////////////////////////////////////////////////////////////////////
  199. HRESULT WINAPI CSdoServiceIAS::QueryInterfaceInternal(
  200. void* pThis,
  201. REFIID riid,
  202. LPVOID* ppv,
  203. DWORD_PTR dw
  204. )
  205. {
  206. if ( InlineIsEqualGUID(riid, IID_ISdoServiceControl) )
  207. {
  208. *ppv = &(static_cast<CSdoServiceIAS*>(pThis))->m_clsSdoServiceControlImpl;
  209. ((LPUNKNOWN)*ppv)->AddRef();
  210. return S_OK;
  211. }
  212. else
  213. {
  214. _ASSERT(FALSE);
  215. return E_NOTIMPL;
  216. }
  217. }
  218. //////////////////////////////////////////////////////////////////////////////
  219. CSdoServiceIAS::CSdoServiceControlImpl::CSdoServiceControlImpl(CSdoServiceIAS* pSdoServiceIAS)
  220. : m_pSdoServiceIAS(pSdoServiceIAS)
  221. {
  222. }
  223. //////////////////////////////////////////////////////////////////////////////
  224. CSdoServiceIAS::CSdoServiceControlImpl::~CSdoServiceControlImpl()
  225. {
  226. }
  227. /////////////////////////////////////////////////////////////////////////////
  228. STDMETHODIMP CSdoServiceIAS::CSdoServiceControlImpl::StartService()
  229. {
  230. HRESULT hr = E_FAIL;
  231. SC_HANDLE hServiceManager;
  232. SC_HANDLE hService;
  233. CSdoLock theLock(*m_pSdoServiceIAS);
  234. try
  235. {
  236. IASTracePrintf("Service SDO is attempting to start service '%ls'...", m_pSdoServiceIAS->GetServiceName());
  237. if ( NULL != (hServiceManager = OpenSCManager(m_pSdoServiceIAS->m_bstrAttachedComputer, NULL, SC_MANAGER_ALL_ACCESS)) )
  238. {
  239. if ( NULL != (hService = OpenService(hServiceManager, m_pSdoServiceIAS->GetServiceName(), SERVICE_ALL_ACCESS)) )
  240. {
  241. if ( ::StartService(hService, NULL, NULL) == TRUE )
  242. {
  243. hr = S_OK;
  244. IASTracePrintf("Service SDO successfully started service '%ls'...", m_pSdoServiceIAS->GetServiceName());
  245. }
  246. CloseServiceHandle(hService);
  247. }
  248. CloseServiceHandle(hServiceManager);
  249. }
  250. if ( FAILED(hr) )
  251. {
  252. hr = HRESULT_FROM_WIN32(GetLastError());
  253. IASTracePrintf("Error in Service SDO - StartService() - could not start service '%ls'...", m_pSdoServiceIAS->GetServiceName());
  254. }
  255. }
  256. catch(...)
  257. {
  258. IASTracePrintf("Error in Service SDO - StartService() - caught unknown exception...");
  259. hr = E_FAIL;
  260. }
  261. return hr;
  262. }
  263. /////////////////////////////////////////////////////////////////////////////
  264. STDMETHODIMP CSdoServiceIAS::CSdoServiceControlImpl::GetServiceStatus(
  265. /*[out]*/ LONG *pStatus
  266. )
  267. {
  268. HRESULT hr = E_FAIL;
  269. SC_HANDLE hServiceManager;
  270. SC_HANDLE hService;
  271. CSdoLock theLock(*m_pSdoServiceIAS);
  272. // Check preconditions
  273. _ASSERTE( NULL != pStatus );
  274. if ( NULL == pStatus )
  275. return E_INVALIDARG;
  276. try
  277. {
  278. IASTracePrintf("Service SDO is attempting to retrieve status for service '%ls'...", m_pSdoServiceIAS->GetServiceName());
  279. if ( NULL != (hServiceManager = OpenSCManager(m_pSdoServiceIAS->m_bstrAttachedComputer, NULL, SC_MANAGER_ALL_ACCESS)) )
  280. {
  281. if ( NULL != (hService = OpenService(hServiceManager, m_pSdoServiceIAS->GetServiceName(), SERVICE_ALL_ACCESS)) )
  282. {
  283. SERVICE_STATUS ServiceStatus;
  284. if ( TRUE == QueryServiceStatus(hService, &ServiceStatus) )
  285. {
  286. *pStatus = (LONG)ServiceStatus.dwCurrentState;
  287. hr = S_OK;
  288. IASTracePrintf("Service SDO successfully retrieved status for service '%ls'...", m_pSdoServiceIAS->GetServiceName());
  289. }
  290. CloseServiceHandle(hService);
  291. }
  292. CloseServiceHandle(hServiceManager);
  293. }
  294. if ( FAILED(hr) )
  295. {
  296. // Returns ERROR_SERVICE_DOES_NOT_EXIST if IAS has not been installed
  297. //
  298. hr = HRESULT_FROM_WIN32(GetLastError());
  299. IASTracePrintf("Error in Service SDO - GetServiceStatus() - Could not retrieve status for service '%ls'...", m_pSdoServiceIAS->GetServiceName());
  300. }
  301. }
  302. catch(...)
  303. {
  304. IASTracePrintf("Error in Service SDO - GetServiceStatus() - caught unknown exception...");
  305. hr = E_FAIL;
  306. }
  307. return hr;
  308. }
  309. /////////////////////////////////////////////////////////////////////////////
  310. STDMETHODIMP CSdoServiceIAS::CSdoServiceControlImpl::StopService()
  311. {
  312. CSdoLock theLock(*m_pSdoServiceIAS);
  313. HRESULT hr;
  314. try
  315. {
  316. IASTracePrintf("Service SDO is attempting to send control code %d (stop) to service '%ls'...", SERVICE_CONTROL_STOP, m_pSdoServiceIAS->GetServiceName());
  317. hr = ControlIAS(SERVICE_CONTROL_STOP);
  318. }
  319. catch(...)
  320. {
  321. IASTracePrintf("Error in Service SDO - StopService() - caught unknown exception...");
  322. hr = E_FAIL;
  323. }
  324. return hr;
  325. }
  326. /////////////////////////////////////////////////////////////////////////////
  327. STDMETHODIMP CSdoServiceIAS::CSdoServiceControlImpl::ResetService()
  328. {
  329. CSdoLock theLock(*m_pSdoServiceIAS);
  330. HRESULT hr;
  331. try
  332. {
  333. IASTracePrintf("Service SDO is attempting to send control code %d (reset) to service '%ls'...", SERVICE_CONTROL_RESET, m_pSdoServiceIAS->GetServiceName());
  334. hr = ControlIAS(SERVICE_CONTROL_RESET);
  335. }
  336. catch(...)
  337. {
  338. IASTracePrintf("Error in Service SDO - StopService() - caught unknown exception...");
  339. hr = E_FAIL;
  340. }
  341. return hr;
  342. }
  343. /////////////////////////////////////////////////////////////////////////////
  344. HRESULT CSdoServiceIAS::CSdoServiceControlImpl::ControlIAS(DWORD dwControlCode)
  345. {
  346. HRESULT hr = E_FAIL;
  347. SC_HANDLE hServiceManager;
  348. SC_HANDLE hService;
  349. if ( NULL != (hServiceManager = OpenSCManager(m_pSdoServiceIAS->m_bstrAttachedComputer, NULL, SC_MANAGER_ALL_ACCESS)) )
  350. {
  351. if ( NULL != (hService = OpenService(hServiceManager, m_pSdoServiceIAS->GetServiceName(), SERVICE_ALL_ACCESS)) )
  352. {
  353. SERVICE_STATUS ServiceStatus;
  354. if ( TRUE == ControlService(hService, dwControlCode, &ServiceStatus) )
  355. {
  356. hr = S_OK;
  357. IASTracePrintf("Service SDO successfully sent control code %d to service '%ls'...", dwControlCode, m_pSdoServiceIAS->GetServiceName());
  358. }
  359. CloseServiceHandle(hService);
  360. }
  361. CloseServiceHandle(hServiceManager);
  362. }
  363. if ( FAILED(hr) )
  364. {
  365. hr = HRESULT_FROM_WIN32(GetLastError());
  366. IASTracePrintf("Service SDO Status - control code %d could not be delivered to service '%ls'...", dwControlCode, m_pSdoServiceIAS->GetServiceName());
  367. }
  368. return hr;
  369. }