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.

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