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.

1287 lines
30 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. MainDll.cpp
  5. Abstract:
  6. History:
  7. --*/
  8. #include "PreComp.h"
  9. #include <wbemint.h>
  10. #include<olectl.h>
  11. #include <stdio.h>
  12. #include <HelperFuncs.h>
  13. #include <Logging.h>
  14. #include <CGlobals.h>
  15. #include "Globals.h"
  16. #include "ClassFac.h"
  17. #include "ProvResv.h"
  18. #include "ProvFact.h"
  19. #include "ProvSubS.h"
  20. #include "ProvAggr.h"
  21. #include "ProvLoad.h"
  22. #include "ProvSelf.h"
  23. #include "ProvHost.h"
  24. #include "StrobeThread.h"
  25. #include "ProvCache.h"
  26. #include "Guids.h"
  27. #include <strsafe.h>
  28. #include <autoptr.h>
  29. #include <scopeguard.h>
  30. /******************************************************************************
  31. *
  32. * Name:
  33. *
  34. *
  35. * Description:
  36. *
  37. *
  38. *****************************************************************************/
  39. HINSTANCE g_hInst=NULL;
  40. CriticalSection s_CriticalSection(NOTHROW_LOCK) ;
  41. /******************************************************************************
  42. *
  43. * Name:
  44. *
  45. *
  46. * Description:
  47. *
  48. *
  49. *****************************************************************************/
  50. HRESULT DllStartup ()
  51. {
  52. HRESULT t_Result = S_OK ;
  53. WmiStatusCode t_StatusCode = WmiDebugLog :: Initialize ( *ProviderSubSystem_Globals :: s_Allocator ) ;
  54. t_Result = ProviderSubSystem_Globals :: Initialize_SharedCounters () ;
  55. if ( FAILED ( t_Result ) )
  56. {
  57. t_Result = S_OK ;
  58. }
  59. if ( SUCCEEDED ( t_Result ) )
  60. {
  61. t_Result = ProviderSubSystem_Globals :: Initialize_Events () ;
  62. if ( SUCCEEDED ( t_Result ) )
  63. {
  64. }
  65. else
  66. {
  67. t_Result = WBEM_E_INVALID_PARAMETER ;
  68. }
  69. }
  70. if ( SUCCEEDED ( t_Result ) )
  71. {
  72. t_Result = ProviderSubSystem_Common_Globals :: CreateSystemAces () ;
  73. }
  74. if ( SUCCEEDED ( t_Result ) )
  75. {
  76. t_Result = ProviderSubSystem_Common_Globals :: CreateMethodSecurityDescriptor () ;
  77. }
  78. if ( SUCCEEDED ( t_Result ) )
  79. {
  80. ProviderSubSystem_Globals :: s_StrobeThread = new StrobeThread ( *ProviderSubSystem_Globals :: s_Allocator, ProviderSubSystem_Globals :: s_StrobeTimeout ) ;
  81. if ( ProviderSubSystem_Globals :: s_StrobeThread)
  82. {
  83. if (!Dispatcher::scheduleTimer(*ProviderSubSystem_Globals :: s_StrobeThread, ProviderSubSystem_Globals :: s_StrobeTimeout, 0 , WT_EXECUTELONGFUNCTION))
  84. {
  85. ProviderSubSystem_Globals :: s_StrobeThread->Release();
  86. ProviderSubSystem_Globals :: s_StrobeThread=0;
  87. t_Result = WBEM_E_OUT_OF_MEMORY;
  88. }
  89. else
  90. ProviderSubSystem_Globals :: s_StrobeThread->Release();
  91. }
  92. else
  93. {
  94. t_Result = WBEM_E_OUT_OF_MEMORY;
  95. }
  96. }
  97. if ( SUCCEEDED ( t_Result ) )
  98. {
  99. ProviderSubSystem_Globals :: s_CoFreeUnusedLibrariesEvent = OpenEvent (
  100. EVENT_ALL_ACCESS,
  101. FALSE,
  102. L"WINMGMT_PROVIDER_CANSHUTDOWN"
  103. ) ;
  104. if ( ProviderSubSystem_Globals :: s_CoFreeUnusedLibrariesEvent == NULL )
  105. {
  106. t_Result = WBEM_E_UNEXPECTED ;
  107. }
  108. }
  109. if ( SUCCEEDED ( t_Result ) )
  110. {
  111. _IWmiCoreServices *t_CoreService = NULL ;
  112. t_Result = CoCreateInstance (
  113. CLSID_IWmiCoreServices ,
  114. NULL ,
  115. CLSCTX_INPROC_SERVER ,
  116. IID__IWmiCoreServices ,
  117. ( void ** ) & t_CoreService
  118. ) ;
  119. if ( SUCCEEDED ( t_Result ) )
  120. {
  121. IWbemServices *t_Service = NULL ;
  122. LONG t_Flags = WMICORE_FLAG_REPOSITORY | WMICORE_CLIENT_TYPE_PROVIDER ;
  123. t_Result = t_CoreService->GetServices (
  124. L"root" ,
  125. NULL,
  126. NULL,
  127. t_Flags ,
  128. IID_IWbemServices ,
  129. ( void ** ) & t_Service
  130. ) ;
  131. if ( SUCCEEDED ( t_Result ) )
  132. {
  133. CServerObject_GlobalRegistration t_Registration ;
  134. t_Result = t_Registration.SetContext (
  135. NULL ,
  136. NULL ,
  137. t_Service
  138. ) ;
  139. if ( SUCCEEDED ( t_Result ) )
  140. {
  141. t_Result = t_Registration.Load (
  142. e_All
  143. ) ;
  144. if ( SUCCEEDED ( t_Result ) )
  145. {
  146. ProviderSubSystem_Globals :: s_StrobeTimeout = t_Registration.GetUnloadTimeoutMilliSeconds () >> 1 ;
  147. ProviderSubSystem_Globals :: s_InternalCacheTimeout = t_Registration.GetUnloadTimeoutMilliSeconds () ;
  148. ProviderSubSystem_Globals :: s_ObjectCacheTimeout = t_Registration.GetObjectUnloadTimeoutMilliSeconds () ;
  149. ProviderSubSystem_Globals :: s_EventCacheTimeout = t_Registration.GetEventUnloadTimeoutMilliSeconds () ;
  150. }
  151. }
  152. if ( SUCCEEDED ( t_Result ) )
  153. {
  154. CServerObject_HostQuotaRegistration t_Registration ;
  155. t_Result = t_Registration.SetContext (
  156. NULL ,
  157. NULL ,
  158. t_Service
  159. ) ;
  160. if ( SUCCEEDED ( t_Result ) )
  161. {
  162. t_Result = t_Registration.Load (
  163. e_All
  164. ) ;
  165. if ( SUCCEEDED ( t_Result ) )
  166. {
  167. ProviderSubSystem_Globals ::s_Quota_ProcessLimitCount = t_Registration.GetProcessLimitAllHosts () ;
  168. ProviderSubSystem_Globals ::s_Quota_ProcessMemoryLimitCount = t_Registration.GetMemoryPerHost () ;
  169. ProviderSubSystem_Globals ::s_Quota_JobMemoryLimitCount = t_Registration.GetMemoryAllHosts () ;
  170. ProviderSubSystem_Globals ::s_Quota_HandleCount = t_Registration.GetHandlesPerHost () ;
  171. ProviderSubSystem_Globals ::s_Quota_NumberOfThreads = t_Registration.GetThreadsPerHost () ;
  172. ProviderSubSystem_Globals ::s_Quota_PrivatePageCount = t_Registration.GetMemoryPerHost () ;
  173. }
  174. }
  175. }
  176. t_Service->Release () ;
  177. }
  178. t_CoreService->Release () ;
  179. }
  180. }
  181. if ( SUCCEEDED ( t_Result ) )
  182. {
  183. t_Result = ProviderSubSystem_Globals :: CreateJobObject () ;
  184. }
  185. if ( SUCCEEDED ( t_Result ) )
  186. {
  187. ProviderSubSystem_Globals :: s_Initialized = 1 ;
  188. }
  189. return t_Result ;
  190. }
  191. /******************************************************************************
  192. *
  193. * Name:
  194. *
  195. *
  196. * Description:
  197. *
  198. *
  199. *****************************************************************************/
  200. HRESULT DllShutdown ()
  201. {
  202. HRESULT t_Result = S_OK ;
  203. if (ProviderSubSystem_Globals :: s_StrobeThread)
  204. {
  205. Dispatcher::cancelTimer(*ProviderSubSystem_Globals :: s_StrobeThread);
  206. Dispatcher::close();
  207. ProviderSubSystem_Globals :: s_StrobeThread = 0;
  208. }
  209. t_Result = ProviderSubSystem_Globals :: UnInitialize_Events () ;
  210. t_Result = ProviderSubSystem_Globals :: UnInitialize_SharedCounters () ;
  211. if ( ProviderSubSystem_Globals :: s_CoFreeUnusedLibrariesEvent )
  212. {
  213. CloseHandle ( ProviderSubSystem_Globals :: s_CoFreeUnusedLibrariesEvent ) ;
  214. ProviderSubSystem_Globals :: s_CoFreeUnusedLibrariesEvent = NULL ;
  215. }
  216. t_Result = ProviderSubSystem_Common_Globals :: DeleteMethodSecurityDescriptor () ;
  217. t_Result = ProviderSubSystem_Common_Globals :: DeleteSystemAces () ;
  218. WmiStatusCode t_StatusCode = WmiDebugLog :: UnInitialize ( *ProviderSubSystem_Globals :: s_Allocator ) ;
  219. t_Result = ProviderSubSystem_Globals :: DeleteJobObject () ;
  220. ProviderSubSystem_Globals :: s_Initialized = 0 ;
  221. return t_Result ;
  222. }
  223. /******************************************************************************
  224. *
  225. * Name:
  226. *
  227. *
  228. * Description:
  229. *
  230. *
  231. *****************************************************************************/
  232. BOOL APIENTRY DllMain (
  233. HINSTANCE hInstance,
  234. ULONG ulReason ,
  235. LPVOID pvReserved
  236. )
  237. {
  238. g_hInst=hInstance;
  239. BOOL t_Status = TRUE ;
  240. if ( DLL_PROCESS_DETACH == ulReason )
  241. {
  242. HRESULT t_Result = ProviderSubSystem_Globals :: Global_Shutdown () ;
  243. if ( SUCCEEDED ( t_Result ) )
  244. {
  245. t_Status = TRUE ;
  246. }
  247. else
  248. {
  249. t_Status = FALSE ;
  250. }
  251. WmiHelper :: DeleteCriticalSection ( & s_CriticalSection ) ;
  252. t_Status = TRUE ;
  253. }
  254. else if ( DLL_PROCESS_ATTACH == ulReason )
  255. {
  256. DisableThreadLibraryCalls ( hInstance ) ;
  257. WmiStatusCode t_StatusCode = WmiHelper :: InitializeCriticalSection ( & s_CriticalSection ) ;
  258. if ( t_StatusCode == e_StatusCode_Success )
  259. {
  260. HRESULT t_Result = ProviderSubSystem_Globals :: Global_Startup () ;
  261. if ( SUCCEEDED ( t_Result ) )
  262. {
  263. t_Status = TRUE ;
  264. }
  265. else
  266. {
  267. t_Status = FALSE ;
  268. }
  269. }
  270. else
  271. {
  272. t_Status = FALSE ;
  273. }
  274. }
  275. else if ( DLL_THREAD_DETACH == ulReason )
  276. {
  277. t_Status = TRUE ;
  278. }
  279. else if ( DLL_THREAD_ATTACH == ulReason )
  280. {
  281. t_Status = TRUE ;
  282. }
  283. return t_Status ;
  284. }
  285. /******************************************************************************
  286. *
  287. * Name:
  288. *
  289. *
  290. * Description:
  291. *
  292. *
  293. *****************************************************************************/
  294. STDAPI DllGetClassObject (
  295. REFCLSID rclsid ,
  296. REFIID riid,
  297. void **ppv
  298. )
  299. {
  300. HRESULT t_Result = S_OK ;
  301. WmiStatusCode t_StatusCode = WmiHelper :: EnterCriticalSection ( & s_CriticalSection , FALSE ) ;
  302. if ( t_StatusCode == e_StatusCode_Success )
  303. {
  304. if ( ProviderSubSystem_Globals :: s_Initialized == 0 )
  305. {
  306. DllStartup () ;
  307. }
  308. if ( rclsid == CLSID_WmiProvSS )
  309. {
  310. CServerProvSubSysClassFactory *lpunk = new CServerProvSubSysClassFactory ;
  311. if ( lpunk == NULL )
  312. {
  313. t_Result = E_OUTOFMEMORY ;
  314. }
  315. else
  316. {
  317. t_Result = lpunk->QueryInterface ( riid , ppv ) ;
  318. if ( FAILED ( t_Result ) )
  319. {
  320. delete lpunk ;
  321. }
  322. else
  323. {
  324. }
  325. }
  326. }
  327. else if ( rclsid == CLSID_WmiProviderBindingFactory )
  328. {
  329. CServerClassFactory <CServerObject_BindingFactory,_IWmiProviderFactory> *lpunk = new CServerClassFactory <CServerObject_BindingFactory,_IWmiProviderFactory> ;
  330. if ( lpunk == NULL )
  331. {
  332. t_Result = E_OUTOFMEMORY ;
  333. }
  334. else
  335. {
  336. t_Result = lpunk->QueryInterface ( riid , ppv ) ;
  337. if ( FAILED ( t_Result ) )
  338. {
  339. delete lpunk ;
  340. }
  341. else
  342. {
  343. }
  344. }
  345. }
  346. else if ( rclsid == CLSID_WmiProviderInProcFactory )
  347. {
  348. CServerClassFactory <CServerObject_RawFactory,_IWmiProviderFactory> *lpunk = new CServerClassFactory <CServerObject_RawFactory,_IWmiProviderFactory> ;
  349. if ( lpunk == NULL )
  350. {
  351. t_Result = E_OUTOFMEMORY ;
  352. }
  353. else
  354. {
  355. t_Result = lpunk->QueryInterface ( riid , ppv ) ;
  356. if ( FAILED ( t_Result ) )
  357. {
  358. delete lpunk ;
  359. }
  360. else
  361. {
  362. }
  363. }
  364. }
  365. else if ( rclsid == CLSID_ProvSubSys_Provider )
  366. {
  367. CServerClassFactory <CServerObject_IWbemServices,IWbemServices> *lpunk = new CServerClassFactory <CServerObject_IWbemServices,IWbemServices> ;
  368. if ( lpunk == NULL )
  369. {
  370. t_Result = E_OUTOFMEMORY ;
  371. }
  372. else
  373. {
  374. t_Result = lpunk->QueryInterface ( riid , ppv ) ;
  375. if ( FAILED ( t_Result ) )
  376. {
  377. delete lpunk ;
  378. }
  379. else
  380. {
  381. }
  382. }
  383. }
  384. else
  385. {
  386. t_Result = CLASS_E_CLASSNOTAVAILABLE ;
  387. }
  388. WmiHelper :: LeaveCriticalSection ( & s_CriticalSection ) ;
  389. }
  390. else
  391. {
  392. t_Result = E_OUTOFMEMORY ;
  393. }
  394. return t_Result ;
  395. }
  396. /******************************************************************************
  397. *
  398. * Name:
  399. *
  400. *
  401. * Description:
  402. *
  403. *
  404. *****************************************************************************/
  405. STDAPI DllCanUnloadNow ()
  406. {
  407. /*
  408. * Place code in critical section
  409. */
  410. WmiStatusCode t_StatusCode = WmiHelper :: EnterCriticalSection ( & s_CriticalSection , FALSE ) ;
  411. if ( t_StatusCode == e_StatusCode_Success )
  412. {
  413. BOOL unload = (
  414. ProviderSubSystem_Globals :: s_LocksInProgress || ProviderSubSystem_Globals :: s_ObjectsInProgress
  415. ) ;
  416. unload = ! unload ;
  417. if ( unload )
  418. {
  419. DllShutdown () ;
  420. }
  421. WmiHelper :: LeaveCriticalSection ( & s_CriticalSection ) ;
  422. return unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  423. }
  424. else
  425. {
  426. return FALSE ;
  427. }
  428. }
  429. /******************************************************************************
  430. *
  431. * Name:
  432. *
  433. *
  434. * Description:
  435. *
  436. *
  437. *****************************************************************************/
  438. HRESULT SetApplicationSecurity ( HKEY a_Key , LPCWSTR a_Name , DWORD a_Access )
  439. {
  440. HRESULT t_Result = S_OK ;
  441. SID_IDENTIFIER_AUTHORITY t_NtAuthoritySid = SECURITY_NT_AUTHORITY ;
  442. PSID t_Administrator_Sid = NULL ;
  443. ACCESS_ALLOWED_ACE *t_Administrator_ACE = NULL ;
  444. WORD t_Administrator_ACESize = 0 ;
  445. BOOL t_BoolResult = AllocateAndInitializeSid (
  446. & t_NtAuthoritySid ,
  447. 2 ,
  448. SECURITY_BUILTIN_DOMAIN_RID,
  449. DOMAIN_ALIAS_RID_ADMINS,
  450. 0,
  451. 0,
  452. 0,
  453. 0,
  454. 0,
  455. 0,
  456. & t_Administrator_Sid
  457. );
  458. if ( t_BoolResult )
  459. {
  460. DWORD t_SidLength = ::GetLengthSid ( t_Administrator_Sid );
  461. t_Administrator_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  462. t_Administrator_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ t_Administrator_ACESize ] ;
  463. if ( t_Administrator_ACE )
  464. {
  465. CopySid ( t_SidLength, (PSID) & t_Administrator_ACE->SidStart, t_Administrator_Sid ) ;
  466. t_Administrator_ACE->Mask = a_Access;
  467. t_Administrator_ACE->Header.AceType = 0 ;
  468. t_Administrator_ACE->Header.AceFlags = 3 ;
  469. t_Administrator_ACE->Header.AceSize = t_Administrator_ACESize ;
  470. }
  471. else
  472. {
  473. t_Result = E_OUTOFMEMORY ;
  474. }
  475. }
  476. else
  477. {
  478. DWORD t_LastError = ::GetLastError();
  479. t_Result = E_OUTOFMEMORY ;
  480. }
  481. PSID t_Interactive_Sid = NULL ;
  482. ACCESS_ALLOWED_ACE *t_Interactive_ACE = NULL ;
  483. WORD t_Interactive_ACESize = 0 ;
  484. t_BoolResult = AllocateAndInitializeSid (
  485. & t_NtAuthoritySid ,
  486. 1 ,
  487. SECURITY_INTERACTIVE_RID,
  488. 0,
  489. 0,
  490. 0,
  491. 0,
  492. 0,
  493. 0,
  494. 0,
  495. & t_Interactive_Sid
  496. );
  497. if ( t_BoolResult )
  498. {
  499. DWORD t_SidLength = ::GetLengthSid ( t_Interactive_Sid );
  500. t_Interactive_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  501. t_Interactive_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ t_Interactive_ACESize ] ;
  502. if ( t_Interactive_ACE )
  503. {
  504. CopySid ( t_SidLength, (PSID) & t_Interactive_ACE->SidStart, t_Interactive_Sid ) ;
  505. t_Interactive_ACE->Mask = a_Access;
  506. t_Interactive_ACE->Header.AceType = 0 ;
  507. t_Interactive_ACE->Header.AceFlags = 3 ;
  508. t_Interactive_ACE->Header.AceSize = t_Interactive_ACESize ;
  509. }
  510. else
  511. {
  512. t_Result = E_OUTOFMEMORY ;
  513. }
  514. }
  515. else
  516. {
  517. DWORD t_LastError = ::GetLastError();
  518. t_Result = E_OUTOFMEMORY ;
  519. }
  520. PSID t_System_Sid = NULL ;
  521. ACCESS_ALLOWED_ACE *t_System_ACE = NULL ;
  522. WORD t_System_ACESize = 0 ;
  523. t_BoolResult = AllocateAndInitializeSid (
  524. & t_NtAuthoritySid ,
  525. 1 ,
  526. SECURITY_LOCAL_SYSTEM_RID,
  527. 0,
  528. 0,
  529. 0,
  530. 0,
  531. 0,
  532. 0,
  533. 0,
  534. & t_System_Sid
  535. );
  536. if ( t_BoolResult )
  537. {
  538. DWORD t_SidLength = ::GetLengthSid ( t_System_Sid );
  539. t_System_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  540. t_System_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ t_System_ACESize ] ;
  541. if ( t_System_ACE )
  542. {
  543. CopySid ( t_SidLength, (PSID) & t_System_ACE->SidStart, t_System_Sid ) ;
  544. t_System_ACE->Mask = a_Access;
  545. t_System_ACE->Header.AceType = 0 ;
  546. t_System_ACE->Header.AceFlags = 3 ;
  547. t_System_ACE->Header.AceSize = t_System_ACESize ;
  548. }
  549. else
  550. {
  551. t_Result = E_OUTOFMEMORY ;
  552. }
  553. }
  554. else
  555. {
  556. DWORD t_LastError = ::GetLastError();
  557. t_Result = E_OUTOFMEMORY ;
  558. }
  559. PSID t_LocalService_Sid = NULL ;
  560. ACCESS_ALLOWED_ACE *t_LocalService_ACE = NULL ;
  561. WORD t_LocalService_ACESize = 0 ;
  562. t_BoolResult = AllocateAndInitializeSid (
  563. & t_NtAuthoritySid ,
  564. 1 ,
  565. SECURITY_LOCAL_SERVICE_RID,
  566. 0,
  567. 0,
  568. 0,
  569. 0,
  570. 0,
  571. 0,
  572. 0,
  573. & t_LocalService_Sid
  574. );
  575. if ( t_BoolResult )
  576. {
  577. DWORD t_SidLength = ::GetLengthSid ( t_LocalService_Sid );
  578. t_LocalService_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  579. t_LocalService_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ t_LocalService_ACESize ] ;
  580. if ( t_LocalService_ACE )
  581. {
  582. CopySid ( t_SidLength, (PSID) & t_LocalService_ACE->SidStart, t_LocalService_Sid ) ;
  583. t_LocalService_ACE->Mask = a_Access;
  584. t_LocalService_ACE->Header.AceType = 0 ;
  585. t_LocalService_ACE->Header.AceFlags = 3 ;
  586. t_LocalService_ACE->Header.AceSize = t_LocalService_ACESize ;
  587. }
  588. else
  589. {
  590. t_Result = E_OUTOFMEMORY ;
  591. }
  592. }
  593. else
  594. {
  595. DWORD t_LastError = ::GetLastError();
  596. t_Result = E_OUTOFMEMORY ;
  597. }
  598. PSID t_NetworkService_Sid = NULL ;
  599. ACCESS_ALLOWED_ACE *t_NetworkService_ACE = NULL ;
  600. WORD t_NetworkService_ACESize = 0 ;
  601. t_BoolResult = AllocateAndInitializeSid (
  602. & t_NtAuthoritySid ,
  603. 1 ,
  604. SECURITY_NETWORK_SERVICE_RID,
  605. 0,
  606. 0,
  607. 0,
  608. 0,
  609. 0,
  610. 0,
  611. 0,
  612. & t_NetworkService_Sid
  613. );
  614. if ( t_BoolResult )
  615. {
  616. DWORD t_SidLength = ::GetLengthSid ( t_NetworkService_Sid );
  617. t_NetworkService_ACESize = sizeof(ACCESS_ALLOWED_ACE) + (WORD) ( t_SidLength - sizeof(DWORD) ) ;
  618. t_NetworkService_ACE = (ACCESS_ALLOWED_ACE*) new BYTE [ t_NetworkService_ACESize ] ;
  619. if ( t_NetworkService_ACE )
  620. {
  621. CopySid ( t_SidLength, (PSID) & t_NetworkService_ACE->SidStart, t_NetworkService_Sid ) ;
  622. t_NetworkService_ACE->Mask = a_Access;
  623. t_NetworkService_ACE->Header.AceType = 0 ;
  624. t_NetworkService_ACE->Header.AceFlags = 3 ;
  625. t_NetworkService_ACE->Header.AceSize = t_NetworkService_ACESize ;
  626. }
  627. else
  628. {
  629. t_Result = E_OUTOFMEMORY ;
  630. }
  631. }
  632. else
  633. {
  634. DWORD t_LastError = ::GetLastError();
  635. t_Result = E_OUTOFMEMORY ;
  636. }
  637. // Now we need to set permissions on the registry: Everyone read; Admins full.
  638. // We have the sid for admins from the above code. Now get the sid for "Everyone"
  639. DWORD t_TotalAclSize = sizeof(ACL) + t_Administrator_ACESize + t_Interactive_ACESize + t_LocalService_ACESize +
  640. t_NetworkService_ACESize + t_System_ACESize ;
  641. PACL t_Dacl = (PACL) new BYTE [ t_TotalAclSize ] ;
  642. if ( t_Dacl )
  643. {
  644. if ( :: InitializeAcl ( t_Dacl, t_TotalAclSize, ACL_REVISION ) )
  645. {
  646. DWORD t_AceIndex = 0 ;
  647. if ( t_Interactive_ACESize && :: AddAce ( t_Dacl , ACL_REVISION , t_AceIndex , t_Interactive_ACE , t_Interactive_ACESize ) )
  648. {
  649. t_AceIndex ++ ;
  650. }
  651. if ( t_System_ACESize && :: AddAce ( t_Dacl , ACL_REVISION , t_AceIndex , t_System_ACE , t_System_ACESize ) )
  652. {
  653. t_AceIndex ++ ;
  654. }
  655. if ( t_LocalService_ACESize && :: AddAce ( t_Dacl , ACL_REVISION , t_AceIndex , t_LocalService_ACE , t_LocalService_ACESize ) )
  656. {
  657. t_AceIndex ++ ;
  658. }
  659. if ( t_NetworkService_ACESize && :: AddAce ( t_Dacl , ACL_REVISION , t_AceIndex , t_NetworkService_ACE , t_NetworkService_ACESize ) )
  660. {
  661. t_AceIndex ++ ;
  662. }
  663. if ( t_Administrator_ACESize && :: AddAce ( t_Dacl , ACL_REVISION , t_AceIndex , t_Administrator_ACE , t_Administrator_ACESize ) )
  664. {
  665. t_AceIndex ++ ;
  666. }
  667. SECURITY_INFORMATION t_SecurityInfo = 0L;
  668. t_SecurityInfo |= DACL_SECURITY_INFORMATION;
  669. t_SecurityInfo |= PROTECTED_DACL_SECURITY_INFORMATION;
  670. SECURITY_DESCRIPTOR t_SecurityDescriptor ;
  671. t_BoolResult = InitializeSecurityDescriptor ( & t_SecurityDescriptor , SECURITY_DESCRIPTOR_REVISION ) ;
  672. if ( t_BoolResult )
  673. {
  674. t_BoolResult = SetSecurityDescriptorDacl (
  675. & t_SecurityDescriptor ,
  676. TRUE ,
  677. t_Dacl ,
  678. FALSE
  679. ) ;
  680. if ( t_BoolResult )
  681. {
  682. t_BoolResult = SetSecurityDescriptorOwner (
  683. & t_SecurityDescriptor ,
  684. t_System_Sid,
  685. FALSE
  686. ) ;
  687. }
  688. if ( t_BoolResult )
  689. {
  690. t_BoolResult = SetSecurityDescriptorGroup (
  691. & t_SecurityDescriptor ,
  692. t_Administrator_Sid,
  693. FALSE
  694. ) ;
  695. }
  696. if ( t_BoolResult )
  697. {
  698. DWORD t_Length = GetSecurityDescriptorLength ( & t_SecurityDescriptor ) ;
  699. SECURITY_DESCRIPTOR *t_RelativeSecurityDescriptor = ( SECURITY_DESCRIPTOR * ) new BYTE [ t_Length ];
  700. ; if ( t_RelativeSecurityDescriptor )
  701. {
  702. t_BoolResult = MakeSelfRelativeSD (
  703. & t_SecurityDescriptor ,
  704. t_RelativeSecurityDescriptor ,
  705. & t_Length
  706. );
  707. if ( t_BoolResult )
  708. {
  709. DWORD t_ValueType = REG_BINARY ;
  710. DWORD t_DataSize = t_Length ;
  711. LONG t_RegResult = RegSetValueEx (
  712. a_Key ,
  713. a_Name ,
  714. 0 ,
  715. t_ValueType ,
  716. LPBYTE ( t_RelativeSecurityDescriptor ) ,
  717. t_DataSize
  718. ) ;
  719. if ( t_RegResult != ERROR_SUCCESS )
  720. {
  721. t_Result = E_FAIL ;
  722. }
  723. }
  724. delete [] t_RelativeSecurityDescriptor ;
  725. }
  726. else
  727. {
  728. t_Result = E_OUTOFMEMORY ;
  729. }
  730. }
  731. else
  732. {
  733. t_Result = E_FAIL ;
  734. }
  735. }
  736. else
  737. {
  738. t_Result = E_FAIL ;
  739. }
  740. }
  741. delete [] ( ( BYTE * ) t_Dacl ) ;
  742. }
  743. else
  744. {
  745. t_Result = E_OUTOFMEMORY ;
  746. }
  747. if ( t_Administrator_ACE )
  748. {
  749. delete [] ( ( BYTE * ) t_Administrator_ACE ) ;
  750. }
  751. if ( t_Interactive_ACE )
  752. {
  753. delete [] ( ( BYTE * ) t_Interactive_ACE ) ;
  754. }
  755. if ( t_System_ACE )
  756. {
  757. delete [] ( ( BYTE * ) t_System_ACE ) ;
  758. }
  759. if ( t_LocalService_ACE )
  760. {
  761. delete [] ( ( BYTE * ) t_LocalService_ACE ) ;
  762. }
  763. if ( t_NetworkService_ACE )
  764. {
  765. delete [] ( ( BYTE * ) t_NetworkService_ACE ) ;
  766. }
  767. if ( t_Interactive_Sid )
  768. {
  769. FreeSid ( t_Interactive_Sid ) ;
  770. }
  771. if ( t_System_Sid )
  772. {
  773. FreeSid ( t_System_Sid ) ;
  774. }
  775. if ( t_LocalService_Sid )
  776. {
  777. FreeSid ( t_LocalService_Sid ) ;
  778. }
  779. if ( t_NetworkService_Sid )
  780. {
  781. FreeSid ( t_NetworkService_Sid ) ;
  782. }
  783. if ( t_Administrator_Sid )
  784. {
  785. FreeSid ( t_Administrator_Sid ) ;
  786. }
  787. return t_Result ;
  788. }
  789. /******************************************************************************
  790. *
  791. * Name:
  792. *
  793. *
  794. * Description:
  795. *
  796. *
  797. *****************************************************************************/
  798. //Strings used during self registeration
  799. #define REG_FORMAT_STR L"%s\\%s"
  800. #define NOT_INSERT_STR L"NotInsertable"
  801. #define INPROC32_STR L"InprocServer32"
  802. #define LOCALSERVER32_STR L"LocalServer32"
  803. #define THREADING_MODULE_STR L"ThreadingModel"
  804. #define APARTMENT_STR L"Both"
  805. #define APPID_VALUE_STR L"AppId"
  806. #define APPID_STR L"APPID\\"
  807. #define CLSID_STR L"CLSID\\"
  808. #define WMI_PROVIDER_SUBSYSTEM __TEXT("Microsoft WMI Provider Subsystem")
  809. #define WMI_PROVIDER_HOST __TEXT("Microsoft WMI Provider Subsystem Host")
  810. #define WMI_PROVIDER_BINDINGFACTORY __TEXT("Microsoft WMI Provider Subsystem Binding Factory")
  811. #define WMI_PROVIDER_INPROCFACTORY __TEXT("Microsoft WMI Provider Subsystem InProc Factory")
  812. #define WMI_PROVIDER_HOSTINPROCFACTORY __TEXT("Microsoft WMI Provider Subsystem Host InProc Factory")
  813. #define WMI_PROVIDER __TEXT("Microsoft WMI Provider Subsystem Self Instrumentation")
  814. #define WMI_REFRESHER_MANAGER __TEXT("Microsoft WMI Provider Subsystem Refresher Manager")
  815. /******************************************************************************
  816. *
  817. * Name:
  818. *
  819. *
  820. * Description:
  821. *
  822. *
  823. *****************************************************************************/
  824. BOOL SetKeyAndValue ( wchar_t *pszKey , wchar_t *pszSubkey , wchar_t *pszValueName , wchar_t *pszValue )
  825. {
  826. HKEY hKey;
  827. wchar_t szKey[256];
  828. StringCchCopyW ( szKey , 256, pszKey ) ;
  829. if (NULL!=pszSubkey)
  830. {
  831. StringCchCatW ( szKey , 256, L"\\" ) ;
  832. StringCchCatW ( szKey , 256, pszSubkey ) ;
  833. }
  834. if ( ERROR_SUCCESS != RegCreateKeyEx (
  835. HKEY_CLASSES_ROOT ,
  836. szKey ,
  837. 0,
  838. NULL,
  839. REG_OPTION_NON_VOLATILE ,
  840. KEY_ALL_ACCESS,
  841. NULL,
  842. &hKey,
  843. NULL
  844. )
  845. )
  846. {
  847. return FALSE ;
  848. }
  849. if ( NULL != pszValue )
  850. {
  851. if ( ERROR_SUCCESS != RegSetValueEx (
  852. hKey,
  853. pszValueName,
  854. 0,
  855. REG_SZ,
  856. (BYTE *) pszValue ,
  857. (lstrlen(pszValue)+1)*sizeof(wchar_t)
  858. )
  859. )
  860. {
  861. return FALSE;
  862. }
  863. }
  864. RegCloseKey ( hKey ) ;
  865. return TRUE;
  866. }
  867. /******************************************************************************
  868. *
  869. * Name:
  870. *
  871. *
  872. * Description:
  873. *
  874. *
  875. *****************************************************************************/
  876. class OnScopeClose_DeleteString
  877. {
  878. private:
  879. wchar_t *m_Executable ;
  880. protected:
  881. public:
  882. OnScopeClose_DeleteString ( wchar_t *a_Executable ) : m_Executable ( a_Executable )
  883. {
  884. }
  885. ~OnScopeClose_DeleteString ()
  886. {
  887. delete [] m_Executable ;
  888. }
  889. } ;
  890. STDAPI RegisterServer (
  891. BOOL a_Local ,
  892. BOOL a_InProc ,
  893. GUID a_ProviderClassId ,
  894. wchar_t *a_ProviderName ,
  895. wchar_t *a_ExecutableArguments
  896. )
  897. {
  898. wchar_t t_Executable [MAX_PATH + 1];
  899. t_Executable [ 0 ] = 0 ;
  900. t_Executable [ MAX_PATH ] = 0 ;
  901. DWORD t_Length = GetModuleFileName (
  902. g_hInst ,
  903. ( wchar_t * ) t_Executable ,
  904. sizeof ( t_Executable ) / sizeof ( wchar_t ) - 1
  905. ) ;
  906. if ( t_Length )
  907. {
  908. DWORD t_ExecutableAndArgsLength = wcslen ( t_Executable ) + 1 ;
  909. if (a_ExecutableArguments)
  910. {
  911. t_ExecutableAndArgsLength += sizeof ( L" " )/sizeof(wchar_t) + wcslen ( a_ExecutableArguments ) ;
  912. }
  913. wchar_t *t_ExecutableAndArgs = new wchar_t [ t_ExecutableAndArgsLength ] ;
  914. if ( t_ExecutableAndArgs )
  915. {
  916. OnScopeClose_DeleteString t_GarbageCollect ( t_ExecutableAndArgs ) ;
  917. StringCchCopyW ( t_ExecutableAndArgs ,t_ExecutableAndArgsLength, t_Executable ) ;
  918. if ( a_ExecutableArguments )
  919. {
  920. StringCchCatW ( t_ExecutableAndArgs , t_ExecutableAndArgsLength, L" " ) ;
  921. StringCchCatW ( t_ExecutableAndArgs , t_ExecutableAndArgsLength, a_ExecutableArguments ) ;
  922. }
  923. wchar_t szProviderClassID[128];
  924. wchar_t szProviderCLSIDClassID[128];
  925. int iRet = StringFromGUID2(a_ProviderClassId,szProviderClassID, 128);
  926. if ( a_Local )
  927. {
  928. wchar_t szProviderCLSIDAppID[128];
  929. StringCchCopyW(szProviderCLSIDAppID,128, APPID_STR);
  930. StringCchCatW(szProviderCLSIDAppID,128, szProviderClassID);
  931. if (FALSE ==SetKeyAndValue(szProviderCLSIDAppID, NULL, NULL, a_ProviderName ))
  932. return SELFREG_E_CLASS;
  933. HKEY t_Key ;
  934. DWORD t_Disposition = 0 ;
  935. LONG t_RegResult = RegOpenKeyEx (
  936. HKEY_CLASSES_ROOT ,
  937. szProviderCLSIDAppID ,
  938. NULL ,
  939. KEY_READ | KEY_WRITE ,
  940. & t_Key
  941. ) ;
  942. if ( t_RegResult == ERROR_SUCCESS )
  943. {
  944. ON_BLOCK_EXIT(RegCloseKey, t_Key);
  945. HRESULT t_Result = SetApplicationSecurity ( t_Key , L"LaunchPermission" , COM_RIGHTS_EXECUTE ) ;
  946. if ( FAILED ( t_Result ) )
  947. {
  948. return SELFREG_E_CLASS;
  949. }
  950. }
  951. else
  952. {
  953. return SELFREG_E_CLASS;
  954. }
  955. }
  956. StringCchCopyW(szProviderCLSIDClassID,128, CLSID_STR);
  957. StringCchCatW(szProviderCLSIDClassID,128, szProviderClassID);
  958. if ( a_Local )
  959. {
  960. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL , APPID_VALUE_STR, szProviderClassID ))
  961. return SELFREG_E_CLASS;
  962. }
  963. //Create entries under CLSID
  964. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, a_ProviderName ))
  965. return SELFREG_E_CLASS;
  966. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INSERT_STR, NULL, NULL))
  967. return SELFREG_E_CLASS;
  968. if ( a_Local )
  969. {
  970. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, LOCALSERVER32_STR, NULL,t_ExecutableAndArgs))
  971. return SELFREG_E_CLASS;
  972. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, LOCALSERVER32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  973. return SELFREG_E_CLASS;
  974. }
  975. if ( a_InProc )
  976. {
  977. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,t_ExecutableAndArgs))
  978. return SELFREG_E_CLASS;
  979. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  980. return SELFREG_E_CLASS;
  981. }
  982. return S_OK;
  983. }
  984. else
  985. {
  986. return E_FAIL ;
  987. }
  988. }
  989. else
  990. {
  991. return E_FAIL ;
  992. }
  993. }
  994. /******************************************************************************
  995. *
  996. * Name:
  997. *
  998. *
  999. * Description:
  1000. *
  1001. *
  1002. *****************************************************************************/
  1003. STDAPI UnregisterServer( BOOL a_Local , BOOL a_InProc , GUID a_ProviderClassId )
  1004. {
  1005. wchar_t szTemp[128];
  1006. wchar_t szProviderClassID[128];
  1007. wchar_t szProviderCLSIDClassID[128];
  1008. int iRet = StringFromGUID2(a_ProviderClassId ,szProviderClassID, 128);
  1009. StringCchCopyW(szProviderCLSIDClassID,128, CLSID_STR);
  1010. StringCchCatW(szProviderCLSIDClassID,128, szProviderClassID);
  1011. //Delete entries under CLSID
  1012. StringCchPrintfW(szTemp, 128, REG_FORMAT_STR, szProviderCLSIDClassID, NOT_INSERT_STR);
  1013. RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  1014. if ( a_Local )
  1015. {
  1016. wchar_t szProviderCLSIDAppID[128];
  1017. StringCchCopyW(szProviderCLSIDAppID,128, APPID_STR);
  1018. StringCchCatW(szProviderCLSIDAppID,128, szProviderClassID);
  1019. //Delete entries under APPID
  1020. DWORD t_Status = RegDeleteKey(HKEY_CLASSES_ROOT, szProviderCLSIDAppID);
  1021. StringCchPrintfW(szTemp, 128, REG_FORMAT_STR,szProviderCLSIDClassID, LOCALSERVER32_STR);
  1022. t_Status = RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  1023. }
  1024. if ( a_InProc )
  1025. {
  1026. StringCchPrintfW(szTemp, 128, REG_FORMAT_STR,szProviderCLSIDClassID, INPROC32_STR);
  1027. RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  1028. }
  1029. RegDeleteKey(HKEY_CLASSES_ROOT, szProviderCLSIDClassID);
  1030. return S_OK;
  1031. }
  1032. /******************************************************************************
  1033. *
  1034. * Name:
  1035. *
  1036. *
  1037. * Description:
  1038. *
  1039. *
  1040. *****************************************************************************/
  1041. STDAPI DllRegisterServer ()
  1042. {
  1043. HRESULT t_Result ;
  1044. #ifdef WMIASLOCAL
  1045. t_Result = RegisterServer ( TRUE , FALSE , CLSID_WmiProviderHost , WMI_PROVIDER_HOST , NULL ) ;
  1046. #else
  1047. t_Result = RegisterServer ( FALSE , TRUE , CLSID_WmiProvSS , WMI_PROVIDER_SUBSYSTEM , NULL ) ;
  1048. t_Result = RegisterServer ( FALSE , TRUE , CLSID_WmiProviderBindingFactory , WMI_PROVIDER_BINDINGFACTORY , NULL ) ;
  1049. t_Result = RegisterServer ( FALSE , TRUE , CLSID_WmiProviderInProcFactory , WMI_PROVIDER_INPROCFACTORY , NULL ) ;
  1050. t_Result = RegisterServer ( FALSE , TRUE , CLSID_ProvSubSys_Provider , WMI_PROVIDER , NULL ) ;
  1051. #endif
  1052. return t_Result ;
  1053. }
  1054. /******************************************************************************
  1055. *
  1056. * Name:
  1057. *
  1058. *
  1059. * Description:
  1060. *
  1061. *
  1062. *****************************************************************************/
  1063. STDAPI DllUnregisterServer ()
  1064. {
  1065. HRESULT t_Result ;
  1066. #ifdef WMIASLOCAL
  1067. t_Result = UnregisterServer ( TRUE , FALSE , CLSID_WmiProviderHost ) ;
  1068. #else
  1069. t_Result = UnregisterServer ( FALSE , TRUE , CLSID_WmiProvSS ) ;
  1070. t_Result = UnregisterServer ( FALSE , TRUE , CLSID_WmiProviderBindingFactory ) ;
  1071. t_Result = UnregisterServer ( FALSE , TRUE , CLSID_WmiProviderInProcFactory ) ;
  1072. t_Result = UnregisterServer ( FALSE , TRUE , CLSID_ProvSubSys_Provider ) ;
  1073. #endif
  1074. return t_Result ;
  1075. }