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.

519 lines
13 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1997-1998 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: sdocomponent.cpp
  6. //
  7. // Project: Everest
  8. //
  9. // Description: IAS Server Data Object - IAS Component Class Implementation
  10. //
  11. // Author: TLP 6/18/98
  12. //
  13. /////////////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include <ias.h>
  16. #include <iascomp.h>
  17. #include <portparser.h>
  18. #include "sdocomponent.h"
  19. #include "sdohelperfuncs.h"
  20. ///////////////////////////////
  21. // CComponentCfg Implementation
  22. ///////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////
  24. CComponentCfg::CComponentCfg(LONG lComponentId)
  25. : m_lComponentId(lComponentId),
  26. m_pComponentCfg(NULL)
  27. {
  28. // create letter object
  29. //
  30. switch( lComponentId )
  31. {
  32. case IAS_PROVIDER_MICROSOFT_NTSAM_AUTH:
  33. m_pComponentCfg = (CComponentCfg*) new CComponentCfgAuth(lComponentId);
  34. break;
  35. case IAS_PROVIDER_MICROSOFT_ACCOUNTING:
  36. m_pComponentCfg = (CComponentCfg*) new CComponentCfgAccounting(lComponentId);
  37. break;
  38. case IAS_PROTOCOL_MICROSOFT_RADIUS:
  39. m_pComponentCfg = (CComponentCfg*) new CComponentCfgRADIUS(lComponentId);
  40. break;
  41. default:
  42. m_pComponentCfg = (CComponentCfg*) new CComponentCfgNoOp(lComponentId);
  43. break;
  44. }
  45. }
  46. //////////////////////////////////////////////////////////////////////////////
  47. HRESULT CComponentCfgAuth::Load(CSdoComponent* pSdoComponent)
  48. {
  49. HRESULT hr = S_OK;
  50. do
  51. {
  52. // Determine if were attached to the local machine
  53. //
  54. BSTR bstrMachine = NULL;
  55. hr = (pSdoComponent->GetMachineSdo())->GetAttachedComputer(&bstrMachine);
  56. if ( FAILED(hr) )
  57. break;
  58. wchar_t computerName[MAX_COMPUTERNAME_LENGTH + 1];
  59. DWORD size = MAX_COMPUTERNAME_LENGTH;
  60. GetComputerName(computerName, &size);
  61. LONG lResult = ERROR_SUCCESS;
  62. HKEY hKeyRemote = HKEY_LOCAL_MACHINE;
  63. if ( lstrcmpi(computerName, bstrMachine ) )
  64. {
  65. // We're not attached to the local machine so connect to the
  66. // registry of the remote machine
  67. //
  68. lResult = RegConnectRegistry(
  69. bstrMachine,
  70. HKEY_LOCAL_MACHINE,
  71. &hKeyRemote
  72. );
  73. }
  74. SysFreeString(bstrMachine);
  75. if ( ERROR_SUCCESS != lResult )
  76. {
  77. IASTracePrintf("Error in NT SAM Authentication SDO - Could not attach to the remote registry..");
  78. hr = HRESULT_FROM_WIN32(GetLastError());
  79. break;
  80. }
  81. // Open the IAS key
  82. //
  83. CRegKey IASKey;
  84. lResult = IASKey.Open(
  85. hKeyRemote,
  86. IAS_POLICY_REG_KEY,
  87. KEY_READ
  88. );
  89. if ( lResult != ERROR_SUCCESS )
  90. {
  91. IASTracePrintf("Error in NT SAM Authentication SDO - Could not open IAS registry key..");
  92. hr = HRESULT_FROM_WIN32(GetLastError());
  93. break;
  94. }
  95. // Get the value of the Allow LAN Manager Authentication key.
  96. // Note that this key may not even be present. In this case
  97. // the property object will just use the schema defined default.
  98. //
  99. VARIANT vt;
  100. DWORD dwValue;
  101. lResult = IASKey.QueryValue(
  102. dwValue,
  103. (LPCTSTR) IAS_NTSAM_AUTH_ALLOW_LM
  104. );
  105. if ( lResult == ERROR_SUCCESS )
  106. {
  107. V_VT(&vt) = VT_BOOL;
  108. V_BOOL(&vt) = (dwValue ? VARIANT_TRUE : VARIANT_FALSE);
  109. hr = pSdoComponent->PutComponentProperty(
  110. PROPERTY_NTSAM_ALLOW_LM_AUTHENTICATION,
  111. &vt
  112. );
  113. if ( FAILED(hr) )
  114. {
  115. IASTracePrintf("Error in NT SAM Authentication SDO - Could not store the Allow LM property..");
  116. break;
  117. }
  118. }
  119. } while ( FALSE );
  120. return hr;
  121. }
  122. //////////////////////////////////////////////////////////////////////////////
  123. HRESULT CComponentCfgRADIUS::Initialize(CSdoComponent* pSdoComponent)
  124. {
  125. HRESULT hr = E_FAIL;
  126. do
  127. {
  128. CComPtr<IDataStoreContainer> pDSContainer;
  129. hr = (pSdoComponent->GetComponentDataStore())->QueryInterface(IID_IDataStoreContainer, (void**)&pDSContainer);
  130. if ( FAILED(hr) )
  131. {
  132. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - QueryInterface() failed...");
  133. break;
  134. }
  135. CComBSTR bstrClientsName(DS_OBJECT_CLIENTS);
  136. if (!bstrClientsName)
  137. {
  138. hr = E_OUTOFMEMORY;
  139. break;
  140. }
  141. CComPtr<IDataStoreObject> pDSObject;
  142. hr = pDSContainer->Item(
  143. bstrClientsName,
  144. &pDSObject
  145. );
  146. if ( FAILED(hr) )
  147. {
  148. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - IDataStoreContainer::Item(Clients) failed...");
  149. break;
  150. }
  151. CComPtr<IDataStoreContainer> pDSContainer2;
  152. hr = pDSObject->QueryInterface(
  153. IID_IDataStoreContainer,
  154. (void**)&pDSContainer2
  155. );
  156. if ( FAILED(hr) )
  157. {
  158. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - QueryInterface() failed...");
  159. break;
  160. }
  161. hr = pSdoComponent->InitializeComponentCollection(
  162. PROPERTY_RADIUS_CLIENTS_COLLECTION,
  163. SDO_PROG_ID_CLIENT,
  164. pDSContainer2
  165. );
  166. if ( FAILED(hr) )
  167. break;
  168. pDSObject.Release();
  169. pDSContainer2.Release();
  170. CComBSTR bstrVendorsName(DS_OBJECT_VENDORS);
  171. if (!bstrVendorsName)
  172. {
  173. hr = E_OUTOFMEMORY;
  174. break;
  175. }
  176. hr = pDSContainer->Item(
  177. bstrVendorsName,
  178. &pDSObject
  179. );
  180. if ( FAILED(hr) )
  181. {
  182. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - IDataStoreContainer::Item(Vendors) failed...");
  183. break;
  184. }
  185. hr = pDSObject->QueryInterface(
  186. IID_IDataStoreContainer,
  187. (void**)&pDSContainer2
  188. );
  189. if ( FAILED(hr) )
  190. {
  191. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - QueryInterface() failed...");
  192. break;
  193. }
  194. hr = pSdoComponent->InitializeComponentCollection(
  195. PROPERTY_RADIUS_VENDORS_COLLECTION,
  196. SDO_PROG_ID_VENDOR,
  197. pDSContainer2
  198. );
  199. } while ( FALSE );
  200. return hr;
  201. }
  202. //////////////////////////////////////////////////////////////////////////////
  203. HRESULT CComponentCfgRADIUS::Validate (CSdoComponent* pSdoComponent)
  204. {
  205. HRESULT hr = S_OK;
  206. do
  207. {
  208. // get the RADIUS authentication port value now
  209. _variant_t varAuthValue;
  210. hr = pSdoComponent->GetProperty (
  211. PROPERTY_RADIUS_AUTHENTICATION_PORT,
  212. &varAuthValue
  213. );
  214. if (FAILED (hr))
  215. {
  216. IASTracePrintf ("Error in SDO Component - RADIUS::Validate - GetProperty () failed...");
  217. break;
  218. }
  219. // validate the authentication port now
  220. hr = ValidatePort (V_BSTR (&varAuthValue));
  221. if (FAILED (hr))
  222. {
  223. IASTracePrintf ("Error in SDO Component - RADIUS::Validate - Invalid IP Address or UDP port value specified...");
  224. break;
  225. }
  226. // get the RADIUS accounting port format now
  227. _variant_t varAcctValue;
  228. hr = pSdoComponent->GetProperty (
  229. PROPERTY_RADIUS_ACCOUNTING_PORT,
  230. &varAcctValue
  231. );
  232. if (FAILED (hr))
  233. {
  234. IASTracePrintf ("Error in SDO Component - RADIUS::Validate - GetProperty () failed...");
  235. break;
  236. }
  237. // validate the accounting port now
  238. hr = ValidatePort (V_BSTR (&varAcctValue));
  239. if (FAILED (hr))
  240. {
  241. IASTracePrintf ("Error in SDO Component - RADIUS::Validate - Invalid IP Address or UDP port value specified...");
  242. break;
  243. }
  244. }
  245. while (FALSE);
  246. return (hr);
  247. }
  248. // validating the port value provided
  249. HRESULT CComponentCfgRADIUS::ValidatePort (PWCHAR pwszPortInfo)
  250. {
  251. _ASSERT (pwszPortInfo);
  252. HRESULT hr = S_OK;
  253. CPortParser parser (pwszPortInfo);
  254. do
  255. {
  256. //
  257. // get the IP address
  258. //
  259. DWORD dwIPAddress = 0;
  260. hr = parser.GetIPAddress(&dwIPAddress);
  261. if (S_FALSE == hr)
  262. {
  263. break;
  264. }
  265. else if (S_OK == hr)
  266. {
  267. //
  268. // get the ports associated with this IP address
  269. //
  270. do
  271. {
  272. WORD wPort = 0;
  273. hr = parser.GetNextPort (&wPort);
  274. }
  275. while (S_OK == hr);
  276. }
  277. }
  278. while (SUCCEEDED (hr));
  279. return (hr);
  280. } // end of CPorts::CollectPortInfo method
  281. //////////////////////////////////////////////////////////////////////////////
  282. HRESULT CComponentCfgAccounting::Initialize(CSdoComponent* pSdoComponent)
  283. {
  284. HRESULT hr = E_FAIL;
  285. do
  286. {
  287. BSTR bstrMachineName = NULL;
  288. hr = (pSdoComponent->GetMachineSdo())->GetAttachedComputer(&bstrMachineName);
  289. if ( FAILED(hr) )
  290. {
  291. IASTracePrintf("Error in Accounting SDO - Could not get the name of the attached computer...");
  292. break;
  293. }
  294. wchar_t szLogFileDir[MAX_PATH+1];
  295. hr = ::SDOGetLogFileDirectory(
  296. bstrMachineName,
  297. MAX_PATH,
  298. szLogFileDir
  299. );
  300. if ( FAILED(hr) )
  301. {
  302. SysFreeString(bstrMachineName);
  303. IASTracePrintf("Error in Accounting SDO - Could not get the default log file directory..");
  304. break;
  305. }
  306. _variant_t vtLogFileDir = szLogFileDir;
  307. SysFreeString(bstrMachineName);
  308. hr = pSdoComponent->ChangePropertyDefault(
  309. PROPERTY_ACCOUNTING_LOG_FILE_DIRECTORY,
  310. &vtLogFileDir
  311. );
  312. if ( FAILED(hr) )
  313. {
  314. IASTracePrintf("Error in Accounting SDO - Could not store the default log file directory property..");
  315. break;
  316. }
  317. } while ( FALSE );
  318. return hr;
  319. }
  320. ///////////////////////////////
  321. // CSdoComponent Implementation
  322. ///////////////////////////////
  323. ////////////////////////////////////////////////////////////////////////////////
  324. CSdoComponent::CSdoComponent()
  325. : m_pComponentCfg(NULL),
  326. m_pAttachedMachine(NULL)
  327. {
  328. }
  329. ////////////////////////////////////////////////////////////////////////////////
  330. CSdoComponent::~CSdoComponent()
  331. {
  332. if ( m_pComponentCfg )
  333. delete m_pComponentCfg;
  334. if ( m_pAttachedMachine )
  335. m_pAttachedMachine->Release();
  336. }
  337. ////////////////////////////////////////////////////////////////////////
  338. HRESULT CSdoComponent::InitializeComponentCollection(
  339. /*[in]*/ LONG CollectionPropertyId,
  340. /*[in]*/ LPWSTR lpszCreateClassId,
  341. /*[in]*/ IDataStoreContainer* pDSContainer
  342. )
  343. {
  344. _ASSERT ( m_pAttachedMachine );
  345. return InitializeCollection(
  346. CollectionPropertyId,
  347. lpszCreateClassId,
  348. m_pAttachedMachine,
  349. pDSContainer
  350. );
  351. }
  352. ////////////////////////////////////////////////////////////////////////
  353. HRESULT CSdoComponent::ChangePropertyDefault(
  354. /*[in]*/ LONG Id,
  355. /*[in]*/ VARIANT* pValue
  356. )
  357. {
  358. return ChangePropertyDefaultInternal(Id, pValue);
  359. }
  360. ////////////////////////////////////////////////////////////////////////
  361. HRESULT CSdoComponent::PutComponentProperty(
  362. /*[in]*/ LONG Id,
  363. /*[in]*/ VARIANT* pValue
  364. )
  365. {
  366. return PutPropertyInternal(Id, pValue);
  367. }
  368. //////////////////////////////////////////////////////////////////////////////
  369. HRESULT CSdoComponent::FinalInitialize(
  370. /*[in]*/ bool fInitNew,
  371. /*[in]*/ ISdoMachine* pAttachedMachine
  372. )
  373. {
  374. _ASSERT ( ! fInitNew );
  375. HRESULT hr;
  376. do
  377. {
  378. hr = Load();
  379. if ( FAILED(hr) )
  380. {
  381. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not load component properties...");
  382. break;
  383. }
  384. _variant_t vtComponentId;
  385. hr = GetPropertyInternal(PROPERTY_COMPONENT_ID, &vtComponentId);
  386. if ( FAILED(hr) )
  387. {
  388. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not get the component Id...");
  389. break;
  390. }
  391. auto_ptr<CComponentCfg> pComponentCfg (new CComponentCfg(V_I4(&vtComponentId)));
  392. if ( NULL == pComponentCfg.get() )
  393. {
  394. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not create component: %lx...",V_I4(&vtComponentId));
  395. hr = E_FAIL;
  396. break;
  397. }
  398. (m_pAttachedMachine = pAttachedMachine)->AddRef();
  399. hr = pComponentCfg->Initialize(this);
  400. if ( FAILED(hr) )
  401. {
  402. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not initialize component: %lx...",V_I4(&vtComponentId));
  403. break;
  404. }
  405. m_pComponentCfg = pComponentCfg.release();
  406. hr = Load();
  407. if ( FAILED(hr) )
  408. {
  409. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not configure component: %lx...",V_I4(&vtComponentId));
  410. break;
  411. }
  412. } while ( FALSE );
  413. return hr;
  414. }
  415. //////////////////////////////////////////////////////////////////////////////
  416. HRESULT CSdoComponent::Load()
  417. {
  418. HRESULT hr = CSdo::Load();
  419. if ( SUCCEEDED(hr) )
  420. {
  421. if ( m_pComponentCfg )
  422. hr = m_pComponentCfg->Load(this);
  423. }
  424. return hr;
  425. }
  426. //////////////////////////////////////////////////////////////////////////////
  427. HRESULT CSdoComponent::Save()
  428. {
  429. HRESULT hr = CSdo::Save();
  430. if ( SUCCEEDED(hr) )
  431. {
  432. if ( m_pComponentCfg )
  433. {
  434. hr = m_pComponentCfg->Validate (this);
  435. if (SUCCEEDED (hr))
  436. {
  437. hr = m_pComponentCfg->Save(this);
  438. }
  439. }
  440. }
  441. return hr;
  442. }