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.

458 lines
14 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. IAS_PRODUCT_LIMITS limits;
  162. hr = SDOGetProductLimits(pSdoComponent->GetMachineSdo(), &limits);
  163. if (FAILED(hr))
  164. {
  165. break;
  166. }
  167. hr = pSdoComponent->InitializeComponentCollection(
  168. PROPERTY_RADIUS_CLIENTS_COLLECTION,
  169. SDO_PROG_ID_CLIENT,
  170. pDSContainer2,
  171. limits.maxClients
  172. );
  173. if ( FAILED(hr) )
  174. break;
  175. pDSObject.Release();
  176. pDSContainer2.Release();
  177. CComBSTR bstrVendorsName(DS_OBJECT_VENDORS);
  178. if (!bstrVendorsName)
  179. {
  180. hr = E_OUTOFMEMORY;
  181. break;
  182. }
  183. hr = pDSContainer->Item(
  184. bstrVendorsName,
  185. &pDSObject
  186. );
  187. if ( FAILED(hr) )
  188. {
  189. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - IDataStoreContainer::Item(Vendors) failed...");
  190. break;
  191. }
  192. hr = pDSObject->QueryInterface(
  193. IID_IDataStoreContainer,
  194. (void**)&pDSContainer2
  195. );
  196. if ( FAILED(hr) )
  197. {
  198. IASTracePrintf("Error in SDO Component - RADIUS::Initialize() - QueryInterface() failed...");
  199. break;
  200. }
  201. hr = pSdoComponent->InitializeComponentCollection(
  202. PROPERTY_RADIUS_VENDORS_COLLECTION,
  203. SDO_PROG_ID_VENDOR,
  204. pDSContainer2
  205. );
  206. } while ( FALSE );
  207. return hr;
  208. }
  209. //////////////////////////////////////////////////////////////////////////////
  210. HRESULT CComponentCfgAccounting::Initialize(CSdoComponent* pSdoComponent)
  211. {
  212. HRESULT hr = E_FAIL;
  213. do
  214. {
  215. BSTR bstrMachineName = NULL;
  216. hr = (pSdoComponent->GetMachineSdo())->GetAttachedComputer(&bstrMachineName);
  217. if ( FAILED(hr) )
  218. {
  219. IASTracePrintf("Error in Accounting SDO - Could not get the name of the attached computer...");
  220. break;
  221. }
  222. wchar_t szLogFileDir[MAX_PATH+1];
  223. hr = ::SDOGetLogFileDirectory(
  224. bstrMachineName,
  225. MAX_PATH,
  226. szLogFileDir
  227. );
  228. if ( FAILED(hr) )
  229. {
  230. SysFreeString(bstrMachineName);
  231. IASTracePrintf("Error in Accounting SDO - Could not get the default log file directory..");
  232. break;
  233. }
  234. _variant_t vtLogFileDir = szLogFileDir;
  235. SysFreeString(bstrMachineName);
  236. hr = pSdoComponent->ChangePropertyDefault(
  237. PROPERTY_ACCOUNTING_LOG_FILE_DIRECTORY,
  238. &vtLogFileDir
  239. );
  240. if ( FAILED(hr) )
  241. {
  242. IASTracePrintf("Error in Accounting SDO - Could not store the default log file directory property..");
  243. break;
  244. }
  245. } while ( FALSE );
  246. return hr;
  247. }
  248. ///////////////////////////////
  249. // CSdoComponent Implementation
  250. ///////////////////////////////
  251. ////////////////////////////////////////////////////////////////////////////////
  252. CSdoComponent::CSdoComponent()
  253. : m_pComponentCfg(NULL),
  254. m_pAttachedMachine(NULL)
  255. {
  256. }
  257. ////////////////////////////////////////////////////////////////////////////////
  258. CSdoComponent::~CSdoComponent()
  259. {
  260. if ( m_pComponentCfg )
  261. delete m_pComponentCfg;
  262. if ( m_pAttachedMachine )
  263. m_pAttachedMachine->Release();
  264. }
  265. ////////////////////////////////////////////////////////////////////////
  266. HRESULT CSdoComponent::InitializeComponentCollection(
  267. LONG CollectionPropertyId,
  268. LPWSTR lpszCreateClassId,
  269. IDataStoreContainer* pDSContainer,
  270. DWORD maxSize
  271. )
  272. {
  273. _ASSERT ( m_pAttachedMachine );
  274. return InitializeCollection(
  275. CollectionPropertyId,
  276. lpszCreateClassId,
  277. m_pAttachedMachine,
  278. pDSContainer,
  279. maxSize
  280. );
  281. }
  282. ////////////////////////////////////////////////////////////////////////
  283. HRESULT CSdoComponent::ChangePropertyDefault(
  284. /*[in]*/ LONG Id,
  285. /*[in]*/ VARIANT* pValue
  286. )
  287. {
  288. return ChangePropertyDefaultInternal(Id, pValue);
  289. }
  290. ////////////////////////////////////////////////////////////////////////
  291. HRESULT CSdoComponent::PutComponentProperty(
  292. /*[in]*/ LONG Id,
  293. /*[in]*/ VARIANT* pValue
  294. )
  295. {
  296. return PutPropertyInternal(Id, pValue);
  297. }
  298. //////////////////////////////////////////////////////////////////////////////
  299. HRESULT CSdoComponent::FinalInitialize(
  300. /*[in]*/ bool fInitNew,
  301. /*[in]*/ ISdoMachine* pAttachedMachine
  302. )
  303. {
  304. _ASSERT ( ! fInitNew );
  305. HRESULT hr;
  306. do
  307. {
  308. hr = Load();
  309. if ( FAILED(hr) )
  310. {
  311. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not load component properties...");
  312. break;
  313. }
  314. _variant_t vtComponentId;
  315. hr = GetPropertyInternal(PROPERTY_COMPONENT_ID, &vtComponentId);
  316. if ( FAILED(hr) )
  317. {
  318. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not get the component Id...");
  319. break;
  320. }
  321. auto_ptr<CComponentCfg> pComponentCfg (new CComponentCfg(V_I4(&vtComponentId)));
  322. if ( NULL == pComponentCfg.get() )
  323. {
  324. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not create component: %lx...",V_I4(&vtComponentId));
  325. hr = E_FAIL;
  326. break;
  327. }
  328. (m_pAttachedMachine = pAttachedMachine)->AddRef();
  329. hr = pComponentCfg->Initialize(this);
  330. if ( FAILED(hr) )
  331. {
  332. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not initialize component: %lx...",V_I4(&vtComponentId));
  333. break;
  334. }
  335. m_pComponentCfg = pComponentCfg.release();
  336. hr = Load();
  337. if ( FAILED(hr) )
  338. {
  339. IASTracePrintf("Error in Component SDO - FinalInitialize() - Could not configure component: %lx...",V_I4(&vtComponentId));
  340. break;
  341. }
  342. } while ( FALSE );
  343. return hr;
  344. }
  345. //////////////////////////////////////////////////////////////////////////////
  346. HRESULT CSdoComponent::Load()
  347. {
  348. HRESULT hr = CSdo::Load();
  349. if ( SUCCEEDED(hr) )
  350. {
  351. if ( m_pComponentCfg )
  352. hr = m_pComponentCfg->Load(this);
  353. }
  354. return hr;
  355. }
  356. //////////////////////////////////////////////////////////////////////////////
  357. HRESULT CSdoComponent::Save()
  358. {
  359. HRESULT hr = CSdo::Save();
  360. if ( SUCCEEDED(hr) )
  361. {
  362. if ( m_pComponentCfg )
  363. {
  364. hr = m_pComponentCfg->Validate (this);
  365. if (SUCCEEDED (hr))
  366. {
  367. hr = m_pComponentCfg->Save(this);
  368. }
  369. }
  370. }
  371. return hr;
  372. }
  373. HRESULT CSdoComponent::ValidateProperty(
  374. SDOPROPERTY* prop,
  375. VARIANT* value
  376. ) throw()
  377. {
  378. HRESULT hr = prop->Validate(value);
  379. if (SUCCEEDED(hr) &&
  380. (m_pComponentCfg->GetId() == IAS_PROTOCOL_MICROSOFT_RADIUS))
  381. {
  382. switch (prop->GetId())
  383. {
  384. case PROPERTY_RADIUS_ACCOUNTING_PORT:
  385. case PROPERTY_RADIUS_AUTHENTICATION_PORT:
  386. {
  387. if (!CPortParser::IsPortStringValid(V_BSTR(value)))
  388. {
  389. hr = E_INVALIDARG;
  390. }
  391. break;
  392. }
  393. default:
  394. {
  395. // Do nothing.
  396. break;
  397. }
  398. }
  399. }
  400. return hr;
  401. }