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.

490 lines
9.8 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. XXXX
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <objbase.h>
  10. #include <wbemint.h>
  11. #include <wbemcli.h>
  12. #include "Globals.h"
  13. #include "ProvRegDeCoupled.h"
  14. #include <wmiutils.h>
  15. #include "CGlobals.h"
  16. #include "provcache.h"
  17. #include "aggregator.h"
  18. #include "ProvWsvS.h"
  19. #include <assertbreak.h>
  20. #include <dothrow.h>
  21. #include <os.h>
  22. class DCProxyAggr;
  23. DC_reg::DC_reg( const DC_reg& rhs ):
  24. service_(rhs.service_), context_(rhs.context_),
  25. flags_( rhs.flags_),
  26. CServerObject_DecoupledClientRegistration_Element()
  27. {
  28. *(CServerObject_DecoupledClientRegistration_Element *)(this) = rhs;
  29. };
  30. HRESULT
  31. DC_reg::Load(
  32. long a_Flags,
  33. IWbemContext *a_Context ,
  34. LPCWSTR a_User ,
  35. LPCWSTR a_Locale ,
  36. LPCWSTR a_Scope ,
  37. LPCWSTR a_Registration ,
  38. IUnknown *a_Unknown ,
  39. GUID a_Identity )
  40. {
  41. wchar_t identity[] = L"{00000000-0000-0000-0000-000000000000}";
  42. StringFromGUID2 ( a_Identity, identity, sizeof(identity)/sizeof(identity[0]) ) ;
  43. HRESULT t_Result;
  44. if (
  45. FAILED ( t_Result = SetScope( const_cast<LPWSTR>(a_Scope) ) ) ||
  46. FAILED ( t_Result = SetProvider( const_cast<LPWSTR>(a_Registration) ) ) ||
  47. FAILED ( t_Result = SetUser ( const_cast<LPWSTR>(a_User) ) ) ||
  48. FAILED ( t_Result = SetLocale( const_cast<LPWSTR>(a_Locale) ) ) ||
  49. FAILED ( t_Result = SetClsid( const_cast<LPWSTR>(identity) ) )
  50. )
  51. return t_Result;
  52. flags_ = a_Flags;
  53. service_ = a_Unknown;
  54. context_ = a_Context;
  55. return t_Result;
  56. };
  57. const DC_reg&
  58. DC_reg::operator=(const DC_reg& rhs)
  59. {
  60. CServerObject_DecoupledClientRegistration_Element(*this) = rhs;
  61. service_ = rhs.service_;
  62. context_ = rhs.context_;
  63. return *this;
  64. };
  65. GUID
  66. DC_reg::identity()
  67. {
  68. GUID t_Identity ;
  69. CLSIDFromString ( GetClsid () , & t_Identity ) ;
  70. return t_Identity;
  71. };
  72. IUnknownPtr
  73. DC_reg::service()
  74. {
  75. if( service_ )
  76. return service_;
  77. BSTR t_CreationTime = GetCreationTime () ;
  78. DWORD t_ProcessIdentifier = GetProcessIdentifier () ;
  79. BYTE *t_MarshaledProxy = GetMarshaledProxy () ;
  80. DWORD t_MarshaledProxyLength = GetMarshaledProxyLength () ;
  81. HRESULT t_Result;
  82. if ( t_CreationTime && t_MarshaledProxy )
  83. {
  84. IUnknown *t_Unknown = NULL ;
  85. t_Result = DecoupledProviderSubSystem_Globals :: UnMarshalRegistration ( & t_Unknown , t_MarshaledProxy , t_MarshaledProxyLength ) ;
  86. if ( SUCCEEDED ( t_Result ) ) service_.Attach( t_Unknown );
  87. }
  88. return service_;
  89. };
  90. // Register a requested provider
  91. // on waiting list for the decoupled partner
  92. void
  93. DC_DBReg::Register( const DC_DBkey& key, auto_ref<DCProxyAggr>& ptr)
  94. {
  95. LockGuard<CriticalSection> t_guard(m_Mutex);
  96. aggregators_.regist(ptr, key);
  97. };
  98. // UnRegister a Pseudo Provider
  99. void DC_DBReg::UnRegister( auto_ref<DCProxyAggr>& ptr)
  100. {
  101. LockGuard<CriticalSection> t_guard(m_Mutex);
  102. aggregators_.unregist(ptr);
  103. };
  104. // a a Decoupled provider
  105. HRESULT DC_DBReg::Register( DC_reg& reg )
  106. {
  107. LockGuard<CriticalSection> t_guard(m_Mutex);
  108. if (t_guard.locked() == false)
  109. return WBEM_E_OUT_OF_MEMORY;
  110. // Search for the provider on the namespace
  111. for(requested_providers::iterator it=aggregators_.begin(); it!=aggregators_.end(); ++it)
  112. if(it->key_.equal( reg ) )
  113. break;
  114. if ( it != aggregators_.end() )
  115. return it->client_->Register(reg);
  116. return S_OK;
  117. };
  118. // Unregister a Decoupled provider
  119. HRESULT DC_DBReg::UnRegister( const DC_DBkey& key, const GUID identity )
  120. {
  121. LockGuard<CriticalSection> t_guard(m_Mutex);
  122. if (t_guard.locked() == false)
  123. return WBEM_E_OUT_OF_MEMORY;
  124. for(requested_providers::iterator it=aggregators_.begin(); it!=aggregators_.end(); ++it)
  125. if( it->key_ == key )
  126. {
  127. it->client_->UnRegister(identity);
  128. return S_OK;
  129. }
  130. return S_OK;
  131. };
  132. auto_ref<DCProxyAggr>
  133. DC_DBReg::find(const DC_DBkey& reg) const
  134. {
  135. LockGuard<CriticalSection> t_guard(m_Mutex);
  136. for(requested_providers::iterator it=aggregators_.begin(); it!=aggregators_.end(); ++it)
  137. if( it->key_ == reg )
  138. {
  139. return it->client_;
  140. };
  141. return auto_ref<DCProxyAggr>(NULL);
  142. };
  143. auto_ref<DCProxyAggr>
  144. DC_DBReg::GetAggregator(const DC_DBkey& key) const
  145. {
  146. auto_ref<DCProxyAggr> tmp = find( key );
  147. if( tmp )
  148. return tmp;
  149. return auto_ref<DCProxyAggr>( new DCProxyAggr() );
  150. };
  151. DC_registrar * DC_registrar::instance_ = NULL;
  152. CriticalSection DC_registrar::m_Mutex(NOTHROW_LOCK);
  153. DC_registrar::DC_registrar ():m_ReferenceCount(1)
  154. {
  155. InterlockedIncrement(&DecoupledProviderSubSystem_Globals::s_RegistrarUsers);
  156. CServerObject_DecoupledClientRegistration_Element::VerifyClientKey();
  157. }
  158. DC_registrar::~DC_registrar ()
  159. {
  160. instance_ = NULL;
  161. }
  162. /******************************************************************************
  163. *
  164. * Name:
  165. *
  166. *
  167. * Description:
  168. *
  169. *
  170. *****************************************************************************/
  171. HRESULT
  172. DC_registrar::QueryInterface (REFIID iid ,
  173. LPVOID FAR *iplpv)
  174. {
  175. if (iplpv == NULL)
  176. return E_POINTER;
  177. if (iid == IID_IUnknown)
  178. *iplpv = static_cast<IUnknown *>(this);
  179. else if (iid == IID__IWmiProviderSubsystemRegistrar)
  180. *iplpv = static_cast<_IWmiProviderSubsystemRegistrar *>(this);
  181. else
  182. {
  183. *iplpv = NULL;
  184. return E_NOINTERFACE;
  185. }
  186. AddRef ();
  187. return S_OK;
  188. }
  189. ULONG
  190. DC_registrar::AddRef ()
  191. {
  192. LONG counter = InterlockedIncrement(&m_ReferenceCount);
  193. InterlockedIncrement(&DecoupledProviderSubSystem_Globals::s_RegistrarUsers);
  194. return counter;
  195. }
  196. ULONG
  197. DC_registrar::Release ()
  198. {
  199. InterlockedDecrement (&DecoupledProviderSubSystem_Globals::s_RegistrarUsers);
  200. LONG t_Reference = InterlockedDecrement(&m_ReferenceCount);
  201. if (0 == t_Reference)
  202. {
  203. delete this ;
  204. }
  205. return t_Reference ;
  206. }
  207. HRESULT
  208. DC_registrar :: Register (
  209. long a_Flags ,
  210. IWbemContext *a_Context ,
  211. LPCWSTR a_User ,
  212. LPCWSTR a_Locale ,
  213. LPCWSTR a_Scope ,
  214. LPCWSTR a_Registration ,
  215. DWORD a_ProcessIdentifier ,
  216. IUnknown *a_Unknown ,
  217. GUID a_Identity
  218. )
  219. {
  220. try
  221. {
  222. DC_reg t_Element;
  223. t_Element.Load( a_Flags, a_Context, a_User, a_Locale, a_Scope, a_Registration, a_Unknown, a_Identity );
  224. return mapping_database_.Register(t_Element);
  225. }
  226. catch(...){
  227. return WBEM_E_PROVIDER_FAILURE ;
  228. };
  229. }
  230. /******************************************************************************
  231. *
  232. * Name:
  233. *
  234. *
  235. * Description:
  236. *
  237. *
  238. *****************************************************************************/
  239. HRESULT
  240. DC_registrar::UnRegister (
  241. long a_Flags ,
  242. IWbemContext *a_Context ,
  243. LPCWSTR a_User ,
  244. LPCWSTR a_Locale ,
  245. LPCWSTR a_Scope ,
  246. LPCWSTR a_Registration ,
  247. GUID a_Identity
  248. )
  249. {
  250. try
  251. {
  252. DC_DBkey key( a_User, a_Locale, a_Scope, a_Registration);
  253. return mapping_database_.UnRegister(key, a_Identity);
  254. }
  255. catch(...)
  256. {
  257. return WBEM_E_PROVIDER_FAILURE ;
  258. };
  259. }
  260. /******************************************************************************
  261. *
  262. * Name:
  263. *
  264. *
  265. * Description:
  266. *
  267. *
  268. *****************************************************************************/
  269. HRESULT DC_registrar :: SaveToRegistry (
  270. IUnknown *a_Unknown ,
  271. BYTE *a_MarshaledProxy ,
  272. DWORD a_MarshaledProxyLength
  273. )
  274. {
  275. HRESULT t_Result = S_OK ;
  276. CServerObject_DecoupledServerRegistration t_Element ( *DecoupledProviderSubSystem_Globals :: s_Allocator ) ;
  277. BSTR t_CreationTime = NULL ;
  278. FILETIME t_CreationFileTime ;
  279. FILETIME t_ExitFileTime ;
  280. FILETIME t_KernelFileTime ;
  281. FILETIME t_UserFileTime ;
  282. BOOL t_Status = OS::GetProcessTimes (
  283. GetCurrentProcess (),
  284. & t_CreationFileTime,
  285. & t_ExitFileTime,
  286. & t_KernelFileTime,
  287. & t_UserFileTime
  288. );
  289. if ( t_Status )
  290. {
  291. CWbemDateTime t_Time ;
  292. t_Time.SetFileTimeDate ( t_CreationFileTime , VARIANT_FALSE ) ;
  293. t_Result = t_Time.GetValue ( & t_CreationTime ) ;
  294. if ( SUCCEEDED ( t_Result ) )
  295. {
  296. t_Result = t_Element.SetProcessIdentifier ( GetCurrentProcessId () ) ;
  297. if ( SUCCEEDED ( t_Result ) )
  298. {
  299. t_Result = t_Element.SetCreationTime ( ( BSTR ) t_CreationTime ) ;
  300. }
  301. if ( SUCCEEDED ( t_Result ) )
  302. {
  303. t_Result = t_Element.SetMarshaledProxy ( a_MarshaledProxy , a_MarshaledProxyLength ) ;
  304. }
  305. if ( SUCCEEDED ( t_Result ) )
  306. {
  307. t_Result = t_Element.Save () ;
  308. }
  309. SysFreeString ( t_CreationTime ) ;
  310. }
  311. else
  312. {
  313. t_Result = WBEM_E_UNEXPECTED ;
  314. }
  315. }
  316. else
  317. {
  318. t_Result = WBEM_E_UNEXPECTED ;
  319. }
  320. return t_Result ;
  321. }
  322. /******************************************************************************
  323. *
  324. * Name:
  325. *
  326. *
  327. * Description:
  328. *
  329. *
  330. *****************************************************************************/
  331. HRESULT DC_registrar :: Save ()
  332. {
  333. HRESULT t_Result = S_OK ;
  334. try
  335. {
  336. BYTE *t_MarshaledProxy = NULL ;
  337. DWORD t_MarshaledProxyLength = 0 ;
  338. t_Result = DecoupledProviderSubSystem_Globals :: MarshalRegistration (
  339. this ,
  340. t_MarshaledProxy ,
  341. t_MarshaledProxyLength
  342. ) ;
  343. if ( SUCCEEDED ( t_Result ) )
  344. {
  345. t_Result = SaveToRegistry (
  346. this ,
  347. t_MarshaledProxy ,
  348. t_MarshaledProxyLength
  349. ) ;
  350. delete [] t_MarshaledProxy ;
  351. }
  352. }
  353. catch ( ... )
  354. {
  355. t_Result = WBEM_E_PROVIDER_FAILURE ;
  356. }
  357. return t_Result ;
  358. }
  359. /******************************************************************************
  360. *
  361. * Name:
  362. *
  363. *
  364. * Description:
  365. *
  366. *
  367. *****************************************************************************/
  368. HRESULT
  369. DC_registrar :: Delete ()
  370. {
  371. HRESULT t_Result = S_OK ;
  372. try
  373. {
  374. CServerObject_DecoupledServerRegistration t_Element ( *DecoupledProviderSubSystem_Globals :: s_Allocator ) ;
  375. t_Result = t_Element.Delete () ;
  376. }
  377. catch ( ... )
  378. {
  379. t_Result = WBEM_E_PROVIDER_FAILURE ;
  380. }
  381. return t_Result ;
  382. }