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.

478 lines
17 KiB

  1. //=============================================================================
  2. //
  3. // Copyright (c) 1996-1999, Microsoft Corporation, All rights reserved
  4. //
  5. // NSREP.H
  6. //
  7. // Represents the ESS functionality for a given namespace
  8. //
  9. // Classes defined:
  10. //
  11. // CEssNamespace
  12. //
  13. // History:
  14. //
  15. // 11/27/96 a-levn Compiles.
  16. // 1/6/97 a-levn Updated to initialize TSS.
  17. //
  18. //=============================================================================
  19. #ifndef __NSREP_ESS__H_
  20. #define __NSREP_ESS__H_
  21. #include "pragmas.h"
  22. #include "binding.h"
  23. #include "permfilt.h"
  24. #include "permcons.h"
  25. #include "tempfilt.h"
  26. #include "tempcons.h"
  27. #include "corefind.h"
  28. #include "consprov.h"
  29. #include "provreg.h"
  30. #include "wbemtss.h"
  31. #include "poller.h"
  32. #include "essutils.h"
  33. #include "clscache.h"
  34. #include <map>
  35. #include <set>
  36. #define WMIESS_REPORT(X)
  37. class CEss;
  38. class CEssNamespace : public CUpdateLockable
  39. {
  40. protected:
  41. CEss* m_pEss;
  42. long m_lRef;
  43. PSECURITY_DESCRIPTOR m_pAdminOnlySD;
  44. DWORD m_cAdminOnlySD;
  45. //
  46. // protects level 1 members. These are members that can be used even
  47. // when level2 members are locked.
  48. //
  49. CCritSec m_csLevel1;
  50. //
  51. // protects level 2 members. When both a level1 and level2 lock need to
  52. // be aquired, the level2 lock MUST be obtained first. This is a wbem cs
  53. // because we hold this lock across calls to core ( which can conceivably
  54. // take longer than 2 minutes - the deadline for normal critical sections )
  55. //
  56. CWbemCriticalSection m_csLevel2;
  57. //
  58. // level 1 members
  59. //
  60. HANDLE m_hInitComplete;
  61. //
  62. // If events are signaled when we are in the unitialized state, then
  63. // they are temporarily stored here.
  64. //
  65. CPointerArray<CEventRepresentation> m_aDeferredEvents;
  66. LPWSTR m_wszName;
  67. _IWmiProviderFactory* m_pProviderFactory;
  68. IWbemServices* m_pCoreSvc;
  69. IWbemServices* m_pFullSvc;
  70. IWbemInternalServices* m_pInternalCoreSvc;
  71. IWbemInternalServices* m_pInternalFullSvc;
  72. //
  73. // Level 2 members.
  74. //
  75. BOOL m_bInResync;
  76. BOOL m_bStage1Complete;
  77. int m_cActive;
  78. CBindingTable m_Bindings;
  79. CConsumerProviderCache m_ConsumerProviderCache;
  80. CEventProviderCache m_EventProviderCache;
  81. CPoller m_Poller;
  82. CEssClassCache m_ClassCache;
  83. CCoreEventProvider* m_pCoreEventProvider;
  84. CNtSid m_sidAdministrators;
  85. //
  86. // this structure maps tells us if we need to do anything for a provider
  87. // when a class changes.
  88. //
  89. typedef std::set<WString,WSiless,wbem_allocator<WString> > ProviderSet;
  90. typedef std::map<WString,ProviderSet,WSiless,wbem_allocator<ProviderSet> >
  91. ClassToProviderMap;
  92. ClassToProviderMap m_mapProviderInterestClasses;
  93. //
  94. // the state and init members are both level 1 and level2. They can be
  95. // read when the level1 lock is held. They can only be modified when the
  96. // level2 and level1 locks are held.
  97. //
  98. HRESULT m_hresInit;
  99. enum {
  100. //
  101. // Initialization is Pending. Can service
  102. // events from core in this state (though will be defferred ).
  103. // it is expected that Initialize() will be called
  104. // sometime in the near future. We also can support limited ops
  105. // while in this state. Any operations that deal with event
  106. // subsciptions or provider objects can be serviced. Any ops that
  107. // deal with event provider registrations must wait for initialization.
  108. //
  109. e_InitializePending,
  110. //
  111. // Quiet - Initialization is not pending. The namespace is known to
  112. // be empty of any ess related onjects. Can service events in this
  113. // state, but they are simply discarded.
  114. //
  115. e_Quiet,
  116. //
  117. // We have loaded subscription objects. All ess operations can be
  118. // performed. Events from core now can be processed.
  119. //
  120. e_Initialized,
  121. //
  122. // Shutdown has been called. All operations return error.
  123. //
  124. e_Shutdown
  125. } m_eState;
  126. protected:
  127. class CConsumerClassDeletionSink : public CEmbeddedObjectSink<CEssNamespace>
  128. {
  129. public:
  130. CConsumerClassDeletionSink(CEssNamespace* pNamespace) :
  131. CEmbeddedObjectSink<CEssNamespace>(pNamespace){}
  132. STDMETHOD(Indicate)(long lNumObjects, IWbemClassObject** apObjects);
  133. } m_ClassDeletionSink;
  134. friend CConsumerClassDeletionSink;
  135. protected:
  136. inline void LogOp( LPCWSTR wszOp, IWbemClassObject* pObj );
  137. HRESULT EnsureInitPending();
  138. HRESULT CheckMonitor(IWbemClassObject* pPrevMonitorObj,
  139. IWbemClassObject* pMonitorObj);
  140. HRESULT CheckEventFilter(IWbemClassObject* pPrevFilterObj,
  141. IWbemClassObject* pFilterObj);
  142. HRESULT CheckEventConsumer(IWbemClassObject* pPrevConsumerObj,
  143. IWbemClassObject* pConsumerObj);
  144. HRESULT CheckBinding(IWbemClassObject* pPrevBindingObj,
  145. IWbemClassObject* pBindingObj);
  146. HRESULT CheckEventProviderRegistration(IWbemClassObject* pReg);
  147. HRESULT CheckTimerInstruction(IWbemClassObject* pInst);
  148. HRESULT ActOnSystemEvent(CEventRepresentation& Event, long lFlags);
  149. HRESULT HandleClassChange(LPCWSTR wszClassName, IWbemClassObject* pClass);
  150. HRESULT HandleClassCreation(LPCWSTR wszClassName,IWbemClassObject* pClass);
  151. HRESULT HandleConsumerClassDeletion(LPCWSTR wszClassName);
  152. HRESULT PrepareForResync();
  153. HRESULT ReactivateAllFilters();
  154. HRESULT CommitResync();
  155. HRESULT ReloadMonitor(ADDREF IWbemClassObject* pEventMonitorObj);
  156. HRESULT ReloadEventFilter(ADDREF IWbemClassObject* pEventFilterObj);
  157. HRESULT ReloadEventConsumer(READ_ONLY IWbemClassObject* pConsumerObj,
  158. long lFlags);
  159. HRESULT ReloadBinding(READ_ONLY IWbemClassObject* pBindingObj);
  160. HRESULT ReloadTimerInstruction(READ_ONLY IWbemClassObject* pInstObj);
  161. HRESULT ReloadProvider(READ_ONLY IWbemClassObject* pInstObj);
  162. HRESULT ReloadEventProviderRegistration(IWbemClassObject* pInstObj);
  163. HRESULT ReloadConsumerProviderRegistration(IWbemClassObject* pInstObj);
  164. HRESULT AddMonitor(ADDREF IWbemClassObject* pEventMonitorObj);
  165. HRESULT AddEventFilter(ADDREF IWbemClassObject* pEventFilterObj,
  166. BOOL bInRestart = FALSE);
  167. HRESULT AddEventConsumer(READ_ONLY IWbemClassObject* pConsumerObj,
  168. long lFlags,
  169. BOOL bInRestart = FALSE);
  170. HRESULT AddBinding(LPCWSTR wszFilterKey, LPCWSTR wszConsumerKey,
  171. READ_ONLY IWbemClassObject* pBindingObj);
  172. HRESULT AddBinding(IWbemClassObject* pBindingObj);
  173. HRESULT AddTimerInstruction(READ_ONLY IWbemClassObject* pInstObj);
  174. HRESULT AddProvider(READ_ONLY IWbemClassObject* pInstObj);
  175. HRESULT AddEventProviderRegistration(READ_ONLY IWbemClassObject* pInstObj);
  176. HRESULT RemoveMonitor(IWbemClassObject* pEventMonitorObj);
  177. HRESULT RemoveEventFilter(IWbemClassObject* pEventFilterObj);
  178. HRESULT RemoveEventConsumer(IWbemClassObject* pConsumerObj);
  179. HRESULT RemoveBinding(LPCWSTR wszFilterKey, LPCWSTR wszConsumerKey);
  180. HRESULT RemoveTimerInstruction(IWbemClassObject* pInstObj);
  181. HRESULT RemoveProvider(IWbemClassObject* pInstObj);
  182. HRESULT RemoveEventProviderRegistration(IWbemClassObject* pInstObj);
  183. HRESULT RemoveConsumerProviderRegistration(IWbemClassObject* pInstObj);
  184. HRESULT AssertBindings(IWbemClassObject* pEndpoint);
  185. HRESULT DeleteConsumerProvider(IWbemClassObject* pReg);
  186. HRESULT PerformSubscriptionInitialization();
  187. HRESULT PerformProviderInitialization();
  188. BOOL IsNeededOnStartup();
  189. HRESULT GetCurrentState(IWbemClassObject* pTemplate,
  190. IWbemClassObject** ppObj);
  191. HRESULT CheckSecurity(IWbemClassObject* pPrevObj,
  192. IWbemClassObject* pObj);
  193. HRESULT CheckSidForPrivilege( PSID sid );
  194. HRESULT EnsureSessionSid(IWbemClassObject* pPrevObj, CNtSid& ActingSid);
  195. HRESULT CheckOverwriteSecurity( IWbemClassObject* pPrevObj,
  196. CNtSid& ActingSid);
  197. HRESULT PutSidInObject(IWbemClassObject* pObj, CNtSid& Sid);
  198. HRESULT IsCallerAdministrator();
  199. HRESULT AttemptToActivateFilter(READ_ONLY CEventFilter* pFilter);
  200. HRESULT GetFilterEventNamespace(CEventFilter* pFilter,
  201. RELEASE_ME CEssNamespace** ppNamespace);
  202. void FireNCFilterEvent(DWORD dwIndex, CEventFilter *pFilter);
  203. CQueueingEventSink* GetQueueingEventSink( LPCWSTR wszSinkName );
  204. HRESULT ScheduleFirePostponed();
  205. ~CEssNamespace();
  206. public:
  207. CEssNamespace(CEss* pEss);
  208. ULONG AddRef();
  209. ULONG Release();
  210. CEss* GetEss() { return m_pEss; }
  211. //
  212. // On return, namespace can be used for limited operations. Events
  213. // can be signaled ( though they may be defferred internally ) and
  214. // operations dealing with subscriptions can be performed.
  215. //
  216. HRESULT PreInitialize( LPCWSTR wszName );
  217. //
  218. // Performs initialization but does NOT transition state to Initialized.
  219. // This is done by calling MarkAsInitialized(). This allows a caller to
  220. // atomically perform initalization of multiple namespaces.
  221. //
  222. HRESULT Initialize();
  223. //
  224. // Finishes loading event provider registrations and processes
  225. // subcriptions. Transitions to FullyInitialized() state.
  226. //
  227. HRESULT CompleteInitialization();
  228. //
  229. // Transitions state to Initialized.
  230. //
  231. void MarkAsInitialized( HRESULT hres );
  232. //
  233. // Transitions state to Initialize Pending if previously in the Quiet
  234. // state. Returns TRUE if transition was made.
  235. //
  236. BOOL MarkAsInitPendingIfQuiet();
  237. //
  238. // Waits for Initialization to complete.
  239. //
  240. HRESULT WaitForInitialization();
  241. HRESULT Park();
  242. HRESULT Shutdown();
  243. LPCWSTR GetName() {return m_wszName;}
  244. HRESULT GetNamespacePointer(RELEASE_ME IWbemServices** ppNamespace);
  245. HRESULT LoadEventProvider(LPCWSTR wszProviderName,
  246. IWbemEventProvider** ppProv);
  247. HRESULT LoadConsumerProvider(LPCWSTR wszProviderName,
  248. IUnknown** ppProv);
  249. HRESULT DecorateObject(IWbemClassObject* pObject);
  250. HRESULT ProcessEvent(CEventRepresentation& Event, long lFlags);
  251. HRESULT ProcessQueryObjectSinkEvent( READ_ONLY CEventRepresentation& Event );
  252. HRESULT SignalEvent( CEventRepresentation& Event,
  253. long lFlags,
  254. BOOL bAdminOnly = FALSE );
  255. HRESULT ValidateSystemEvent(CEventRepresentation& Event);
  256. void SetActive();
  257. void SetInactive();
  258. HRESULT ActivateFilter(READ_ONLY CEventFilter* pFilter);
  259. HRESULT DeactivateFilter(READ_ONLY CEventFilter* pFilter);
  260. //
  261. // public versions of register/remove notification sink. Do NOT use
  262. // these versions if calling from within ESS. The reason is that these
  263. // versions wait for initialization and lock the namespace which might
  264. // cause deadlocks if called from within ESS. We also don't want to
  265. // generate self instrumentation events for internal calls.
  266. //
  267. HRESULT RegisterNotificationSink(
  268. WBEM_CWSTR wszQueryLanguage, WBEM_CWSTR wszQuery,
  269. long lFlags, WMIMSG_QOS_FLAG lQosFlags,
  270. IWbemContext* pContext,
  271. IWbemObjectSink* pSink );
  272. HRESULT RemoveNotificationSink( IWbemObjectSink* pSink );
  273. HRESULT ReloadProvider( long lFlags, LPCWSTR wszProvider );
  274. //
  275. // Internal versions of register/remove notification sink. They do
  276. // not lock, wait for initialization, or fire self instrumentation events.
  277. // If calling these methods from within ess, specify bInternal as TRUE.
  278. // The pOwnerSid is used when access checks for the subscription should
  279. // be performed based on a particular SID. currently this is only used
  280. // for cross-namespace subscriptions.
  281. //
  282. HRESULT InternalRegisterNotificationSink(
  283. WBEM_CWSTR wszQueryLanguage, WBEM_CWSTR wszQuery,
  284. long lFlags, WMIMSG_QOS_FLAG lQosFlags,
  285. IWbemContext* pContext, IWbemObjectSink* pSink,
  286. bool bInternal, PSID pOwnerSid );
  287. HRESULT InternalRemoveNotificationSink(IWbemObjectSink* pSink);
  288. CWinMgmtTimerGenerator& GetTimerGenerator();
  289. CConsumerProviderCache& GetConsumerProviderCache()
  290. {return m_ConsumerProviderCache;}
  291. DWORD GetProvidedEventMask(IWbemClassObject* pClass);
  292. HRESULT EnsureConsumerWatchInstruction();
  293. HRESULT InitializeTimerGenerator();
  294. HRESULT ScheduleDelivery(CQueueingEventSink* pDest);
  295. HRESULT RaiseErrorEvent(IWbemEvent* pEvent, BOOL bAdminOnly = FALSE );
  296. void IncrementObjectCount();
  297. void DecrementObjectCount();
  298. HRESULT AddSleepCharge(DWORD dwSleep);
  299. HRESULT AddCache();
  300. HRESULT RemoveCache();
  301. HRESULT AddToCache(DWORD dwAdd, DWORD dwMemberTotal,
  302. DWORD* pdwSleep = NULL);
  303. HRESULT RemoveFromCache(DWORD dwRemove);
  304. HRESULT LockForUpdate();
  305. HRESULT UnlockForUpdate();
  306. bool IsShutdown() { return m_eState == e_Shutdown; }
  307. HRESULT GetProviderNamespacePointer(IWbemServices** ppServices);
  308. HRESULT GetClass( LPCWSTR wszClassName, _IWmiObject** ppClass)
  309. { return m_ClassCache.GetClass(wszClassName, GetCurrentEssContext(),
  310. ppClass);}
  311. HRESULT GetClassFromCore(LPCWSTR wszClassName, _IWmiObject** ppClass);
  312. HRESULT GetInstance(LPCWSTR wszPath, _IWmiObject** ppInstance);
  313. HRESULT GetDbInstance(LPCWSTR wszDbKey, _IWmiObject** ppInstance);
  314. HRESULT CreateInstanceEnum(LPCWSTR wszClass, long lFlags,
  315. IWbemObjectSink* pSink);
  316. HRESULT ExecQuery(LPCWSTR wszQuery, long lFlags, IWbemObjectSink* pSink);
  317. CNtSid& GetAdministratorsSid() {return m_sidAdministrators;}
  318. HRESULT GetToken(PSID pSid, IWbemToken** ppToken);
  319. HRESULT RegisterFilterForAllClassChanges(CEventFilter* pFilter,
  320. QL_LEVEL_1_RPN_EXPRESSION* pExpr);
  321. HRESULT RegisterSinkForAllClassChanges(IWbemObjectSink* pSink,
  322. QL_LEVEL_1_RPN_EXPRESSION* pExpr);
  323. HRESULT RegisterSinkForClassChanges(IWbemObjectSink* pSink,
  324. LPCWSTR wszClassName);
  325. HRESULT UnregisterFilterFromAllClassChanges(CEventFilter* pFilter);
  326. HRESULT UnregisterSinkFromAllClassChanges(IWbemObjectSink* pSink);
  327. HRESULT RegisterProviderForClassChanges( LPCWSTR wszClassName,
  328. LPCWSTR wszProvName );
  329. HRESULT FirePostponedOperations();
  330. HRESULT PostponeRelease(IUnknown* pUnk);
  331. static PSID GetSidFromObject(IWbemClassObject* pObj);
  332. BOOL DoesThreadOwnNamespaceLock();
  333. void DumpStatistics(FILE* f, long lFlags);
  334. friend class CEss;
  335. friend class CEssMetaData;
  336. friend class CFilterEnumSink;
  337. friend class CConsumerEnumSink;
  338. friend class CBindingEnumSink;
  339. friend class CMonitorEnumSink;
  340. friend class CInResync;
  341. friend class CAssertBindingsSink;
  342. friend class CFirePostponed;
  343. };
  344. class CInResync
  345. {
  346. protected:
  347. CEssNamespace* m_pNamespace;
  348. public:
  349. CInResync(CEssNamespace* pNamespace) : m_pNamespace(pNamespace)
  350. {
  351. m_pNamespace->PrepareForResync();
  352. }
  353. void Commit()
  354. {
  355. if ( m_pNamespace != NULL )
  356. {
  357. m_pNamespace->ReactivateAllFilters();
  358. m_pNamespace->CommitResync();
  359. m_pNamespace = NULL;
  360. }
  361. }
  362. ~CInResync()
  363. {
  364. try
  365. {
  366. Commit();
  367. }
  368. catch( ... )
  369. {
  370. }
  371. }
  372. };
  373. //
  374. // The IWbemMetadata pointers are unmarshaled out when
  375. // an external client calls RegisterProxy with an IWbemFilterProxy that is a COM-Proxy
  376. // since a CStdIdentity wrapping them will be created, we need to keep the DLL alive
  377. //
  378. extern CLifeControl * g_pLifeControl;
  379. class CEssMetaData : public CMetaData
  380. {
  381. protected:
  382. CEssNamespace* m_pNamespace;
  383. CLifeControl * m_pControl;
  384. public:
  385. CEssMetaData(CEssNamespace* pNamespace):
  386. m_pControl(g_pLifeControl),
  387. m_pNamespace(pNamespace)
  388. {
  389. m_pControl->ObjectCreated(this);
  390. };
  391. ~CEssMetaData()
  392. {
  393. m_pControl->ObjectDestroyed(this);
  394. };
  395. virtual HRESULT GetClass( LPCWSTR wszName,
  396. IWbemContext* pContext,
  397. _IWmiObject** ppClass );
  398. };
  399. #endif