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.

761 lines
18 KiB

  1. //#--------------------------------------------------------------
  2. //
  3. // File: rascommain.cpp
  4. //
  5. // Synopsis: this is the main Source File for the RAS Server
  6. // COM component DLL
  7. //
  8. //
  9. // History: 2/10/98 MKarki Created
  10. // 8/04/98 MKarki Changes for Dynamic Config
  11. //
  12. // Copyright (C) 1997-98 Microsoft Corporation
  13. // All rights reserved.
  14. //
  15. //----------------------------------------------------------------
  16. //
  17. // Note: Proxy/Stub Information
  18. // To build a separate proxy/stub DLL,
  19. // run nmake -f radprotops.mk in the project directory.
  20. #include "rascominclude.h"
  21. #include "crascom.h"
  22. CComModule _Module;
  23. BEGIN_OBJECT_MAP(ObjectMap)
  24. OBJECT_ENTRY(__uuidof(IasHelper), CRasCom)
  25. END_OBJECT_MAP()
  26. //
  27. // globals
  28. //
  29. IRecvRequest *g_pIRecvRequest = NULL;
  30. ISdoService *g_pISdoService = NULL;
  31. BOOL g_bInitialized = FALSE;
  32. CRITICAL_SECTION g_SrvCritSect;
  33. //
  34. // ProgID of SdoService component
  35. //
  36. const WCHAR SERVICE_PROG_ID[] = L"IAS.SdoService";
  37. //
  38. // ProgID for IasHelper component
  39. //
  40. const WCHAR HELPER_PROG_ID[] = L"IAS.IasHelper";
  41. //++--------------------------------------------------------------
  42. //
  43. // Function: DllMain
  44. //
  45. // Synopsis: Disabling thread calls
  46. //
  47. // Arguments: [in] HINSTANCE - module handle
  48. // [in] DWORD - reason for call
  49. // reserved
  50. //
  51. // Returns: BOOL - sucess/failure
  52. //
  53. // History: MKarki Created 2/10/98
  54. //
  55. //----------------------------------------------------------------
  56. extern "C" BOOL WINAPI
  57. DllMain(
  58. HINSTANCE hInstance,
  59. DWORD dwReason,
  60. LPVOID lpReserved
  61. )
  62. {
  63. if (dwReason == DLL_PROCESS_ATTACH)
  64. {
  65. InitializeCriticalSection (&g_SrvCritSect);
  66. _Module.Init(ObjectMap, hInstance);
  67. DisableThreadLibraryCalls(hInstance);
  68. }
  69. else if (dwReason == DLL_PROCESS_DETACH)
  70. {
  71. DeleteCriticalSection (&g_SrvCritSect);
  72. _Module.Term();
  73. }
  74. return (TRUE);
  75. } // end of DllMain method
  76. //++--------------------------------------------------------------
  77. //
  78. // Function: DllCanUnloadNow
  79. //
  80. // Synopsis: Used to determine if the DLL can be unloaded
  81. //
  82. // Arguments: NONE
  83. //
  84. // Returns: HRESULT
  85. //
  86. //
  87. // History: MKarki Created 8/20/97
  88. //
  89. //----------------------------------------------------------------
  90. STDAPI
  91. DllCanUnloadNow(
  92. VOID
  93. )
  94. {
  95. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  96. } // end of DllCanUnloadNow method
  97. //++--------------------------------------------------------------
  98. //
  99. // Function: DllGetClassObject
  100. //
  101. // Synopsis: Returns a class factory to create an object
  102. // of the requested type
  103. //
  104. // Arguments: [in] REFCLSID
  105. // [in] REFIID
  106. // [out] LPVOID - class factory
  107. //
  108. // Returns: HRESULT
  109. //
  110. // History: MKarki Created 2/10/98
  111. //
  112. //----------------------------------------------------------------
  113. STDAPI
  114. DllGetClassObject(
  115. REFCLSID rclsid,
  116. REFIID riid,
  117. LPVOID* ppv
  118. )
  119. {
  120. return (_Module.GetClassObject(rclsid, riid, ppv));
  121. } // end of DllGetClassObject method
  122. //++--------------------------------------------------------------
  123. //
  124. // Function: DllRegisterServer
  125. //
  126. // Synopsis: Add entries to the system registry
  127. //
  128. // Arguments: NONE
  129. //
  130. // Returns: HRESULT
  131. //
  132. // History: MKarki Created 2/10/98
  133. //
  134. //----------------------------------------------------------------
  135. STDAPI DllRegisterServer(
  136. VOID
  137. )
  138. {
  139. //
  140. // registers object, typelib and all interfaces in typelib
  141. //
  142. return (_Module.RegisterServer(TRUE));
  143. } // end of DllRegisterServer method
  144. //++--------------------------------------------------------------
  145. //
  146. // Function: DllUnregisterServer
  147. //
  148. // Synopsis: Removes entries from the system registry
  149. //
  150. // Arguments: NONE
  151. //
  152. // Returns: HRESULT
  153. //
  154. // History: MKarki Created 2/10/98
  155. //
  156. //----------------------------------------------------------------
  157. STDAPI
  158. DllUnregisterServer(
  159. VOID
  160. )
  161. {
  162. _Module.UnregisterServer();
  163. return (S_OK);
  164. } // end of DllUnregisterServer method
  165. //++--------------------------------------------------------------
  166. //
  167. // Function: AllocateAttributes
  168. //
  169. // Synopsis: This API allocates the number of attributes spefied
  170. // and returns them in the PIASATTRIBUTE array
  171. //
  172. // Arguments:
  173. // [in] DWORD - number of attributes
  174. // [out] PIASATTRIBUTE* array
  175. //
  176. // Returns: HRESULT
  177. //
  178. // History: MKarki Created 2/10/98
  179. //
  180. //----------------------------------------------------------------
  181. STDAPI
  182. AllocateAttributes (
  183. DWORD dwAttributeCount,
  184. PIASATTRIBUTE *ppIasAttribute
  185. )
  186. {
  187. DWORD dwRetVal = 0;
  188. //
  189. // can only allocate attributes after initialization
  190. //
  191. if (FALSE == g_bInitialized)
  192. {
  193. IASTracePrintf (
  194. "InitializeIas method needs to has not been called before"
  195. "allocating attributes"
  196. );
  197. return (E_FAIL);
  198. }
  199. //
  200. // check if arguments are correct
  201. //
  202. if ((0 == dwAttributeCount) || (NULL == ppIasAttribute))
  203. {
  204. IASTracePrintf (
  205. "Inivalid arguments passed in to AllocateAttributes method"
  206. );
  207. return (E_INVALIDARG);
  208. }
  209. //
  210. // allocate attributes now
  211. //
  212. dwRetVal = ::IASAttributeAlloc (dwAttributeCount, ppIasAttribute);
  213. if (0 != dwRetVal)
  214. {
  215. IASTracePrintf (
  216. "Unable to allocate memory in AllocateAttributes method"
  217. );
  218. return (E_FAIL);
  219. }
  220. return (S_OK);
  221. } // end of AllocateAttributes method
  222. //++--------------------------------------------------------------
  223. //
  224. // Function: FreeAttributes
  225. //
  226. // Synopsis: This API frees the number of attributes spefied
  227. //
  228. // Arguments:
  229. // [in] DWORD - number of attributes
  230. // [in] PIASATTRIBUTE array
  231. //
  232. // Returns: HRESULT
  233. //
  234. // History: MKarki Created 2/10/98
  235. //
  236. //----------------------------------------------------------------
  237. STDAPI
  238. FreeAttributes (
  239. DWORD dwAttributeCount,
  240. PIASATTRIBUTE *ppIasAttribute
  241. )
  242. {
  243. DWORD dwCount = 0;
  244. //
  245. // can only free attributes after initialization
  246. //
  247. if (FALSE == g_bInitialized)
  248. {
  249. IASTracePrintf (
  250. "InitializeIas needs to be called before freeing attributes"
  251. );
  252. return (E_FAIL);
  253. }
  254. //
  255. // check if correct attributes have been passed in
  256. //
  257. if (NULL == ppIasAttribute)
  258. {
  259. IASTracePrintf (
  260. "Invalid arguments passed in to FreeAttributes method"
  261. );
  262. return (E_INVALIDARG);
  263. }
  264. //
  265. // free the attributes now
  266. //
  267. for (dwCount = 0; dwCount < dwAttributeCount; dwCount++)
  268. {
  269. ::IASAttributeRelease (ppIasAttribute[dwCount]);
  270. }
  271. return (S_OK);
  272. } // end of FreeAttributes method
  273. //++--------------------------------------------------------------
  274. //
  275. // Function: DoRequest
  276. //
  277. // Synopsis: This is the API that is called to send request
  278. // to the pipeline
  279. //
  280. // Arguments:
  281. // [in] DWORD - number of attributes
  282. // [in] PIASATTRIBUTE*
  283. // [in] IASREQUEST
  284. // [out] IASRESPONSE
  285. //
  286. // Returns: HRESULT - status
  287. //
  288. // History: MKarki Created 2/10/98
  289. //
  290. //----------------------------------------------------------------
  291. STDAPI
  292. DoRequest (
  293. DWORD dwInAttributeCount,
  294. PIASATTRIBUTE *ppInIasAttribute,
  295. PDWORD pdwOutAttributeCount,
  296. PIASATTRIBUTE **pppOutIasAttribute,
  297. LONG IasRequest,
  298. LONG *pIasResponse,
  299. IASPROTOCOL IasProtocol,
  300. PLONG plReason,
  301. BOOL bProcessVSA
  302. )
  303. {
  304. DWORD dwRetVal = 0;
  305. BOOL bStatus = FALSE;
  306. HRESULT hr = S_OK;
  307. //
  308. // can only make request to pipeline after initialization
  309. //
  310. if (FALSE == g_bInitialized)
  311. {
  312. IASTracePrintf (
  313. "InitializeIas needs to be called before Request processing"
  314. );
  315. return (E_FAIL);
  316. }
  317. //
  318. // check the arguments passed in
  319. //
  320. if (
  321. (NULL == ppInIasAttribute) ||
  322. (NULL == pdwOutAttributeCount) ||
  323. (NULL == pppOutIasAttribute) ||
  324. (NULL == pIasResponse) ||
  325. (NULL == plReason)
  326. )
  327. {
  328. IASTracePrintf (
  329. "Invalid arguments passed in to DoRequest method"
  330. );
  331. return (E_INVALIDARG);
  332. }
  333. //
  334. // make the request to the IASHelper COM Object interface
  335. //
  336. hr = g_pIRecvRequest->Process (
  337. dwInAttributeCount,
  338. ppInIasAttribute,
  339. pdwOutAttributeCount,
  340. pppOutIasAttribute,
  341. IasRequest,
  342. pIasResponse,
  343. IasProtocol,
  344. plReason,
  345. bProcessVSA
  346. );
  347. if (FAILED (hr))
  348. {
  349. IASTracePrintf ( "Surrogate failed in processing request... hr =%x", hr);
  350. }
  351. return (hr);
  352. } // end of DoRequest method
  353. //++--------------------------------------------------------------
  354. //
  355. // Function: InitializeIas
  356. //
  357. // Synopsis: This is the API that is called to Initialize the
  358. // IasHlpr Component
  359. //
  360. // Arguments: none
  361. //
  362. // Returns: HRESULT - status
  363. //
  364. // History: MKarki Created 2/10/98
  365. //
  366. //----------------------------------------------------------------
  367. extern "C" HRESULT WINAPI
  368. InitializeIas (
  369. BOOL bComInit
  370. )
  371. {
  372. HRESULT hr = S_OK;
  373. CLSID clsid;
  374. __try
  375. {
  376. EnterCriticalSection (&g_SrvCritSect);
  377. //
  378. // check if we are already initialized
  379. //
  380. if (TRUE == g_bInitialized)
  381. {
  382. __leave;
  383. }
  384. IASTraceInitialize();
  385. IASTracePrintf ("Initializing Surrogate...");
  386. //
  387. // check if our threads are COM enabled
  388. //
  389. if (FALSE == bComInit)
  390. {
  391. IASTracePrintf (
  392. "Thread calling InitializeIas need to be COM enabled"
  393. );
  394. hr = E_INVALIDARG;
  395. __leave;
  396. }
  397. //
  398. // convert SdoService ProgID to CLSID
  399. //
  400. hr = CLSIDFromProgID (SERVICE_PROG_ID, &clsid);
  401. if (FAILED (hr))
  402. {
  403. IASTracePrintf ( "Unable to get SDO service ID" );
  404. __leave;
  405. }
  406. //
  407. // Create the SdoComponent
  408. //
  409. hr = CoCreateInstance (
  410. clsid,
  411. NULL,
  412. CLSCTX_INPROC_SERVER,
  413. __uuidof (ISdoService),
  414. reinterpret_cast <PVOID*> (&g_pISdoService)
  415. );
  416. if (FAILED (hr))
  417. {
  418. IASTracePrintf ( "Unable to create Surrogate COM component");
  419. __leave;
  420. }
  421. //
  422. // Initialize the Service first
  423. //
  424. hr = g_pISdoService->InitializeService (SERVICE_TYPE_RAS);
  425. if (FAILED (hr))
  426. {
  427. IASTracePrintf ( "Unable to initialize SDO COM component");
  428. __leave;
  429. }
  430. //
  431. // Start the IAS service now
  432. //
  433. hr = g_pISdoService->StartService (SERVICE_TYPE_RAS);
  434. if (FAILED (hr))
  435. {
  436. IASTracePrintf ( "Unable to start SDO component");
  437. //
  438. // got to do a shutdown if could not start the sevice
  439. //
  440. HRESULT hr1 = g_pISdoService->ShutdownService (SERVICE_TYPE_RAS);
  441. if (FAILED (hr1))
  442. {
  443. IASTracePrintf("Unable to shutdown SDO compnent");
  444. }
  445. __leave;
  446. }
  447. //
  448. // convert IasHelper ProgID to CLSID
  449. //
  450. hr = CLSIDFromProgID (HELPER_PROG_ID, &clsid);
  451. if (FAILED (hr))
  452. {
  453. IASTracePrintf("Unable to obtain Surrogate ID");
  454. __leave;
  455. }
  456. //
  457. // Create the IasHelper component
  458. //
  459. hr = CoCreateInstance (
  460. clsid,
  461. NULL,
  462. CLSCTX_INPROC_SERVER,
  463. __uuidof (IRecvRequest),
  464. reinterpret_cast <PVOID*> (&g_pIRecvRequest)
  465. );
  466. if (FAILED (hr))
  467. {
  468. IASTracePrintf("Unable to create Surrogate component");
  469. __leave;
  470. }
  471. //
  472. // initialization complete
  473. //
  474. g_bInitialized = TRUE;
  475. }
  476. __finally
  477. {
  478. if (FAILED (hr))
  479. {
  480. IASTracePrintf ("Surrogate failed initialization.");
  481. //
  482. // do cleanup
  483. //
  484. if (NULL != g_pIRecvRequest)
  485. {
  486. g_pIRecvRequest->Release ();
  487. g_pIRecvRequest = NULL;
  488. }
  489. if (NULL != g_pISdoService)
  490. {
  491. g_pISdoService->Release ();
  492. g_pISdoService = NULL;
  493. }
  494. IASTraceUninitialize();
  495. }
  496. else
  497. {
  498. IASTracePrintf ("Surrogate initialized.");
  499. }
  500. LeaveCriticalSection (&g_SrvCritSect);
  501. }
  502. return (hr);
  503. } // end of InitializeIas method
  504. //++--------------------------------------------------------------
  505. //
  506. // Function: ShutdownIas
  507. //
  508. // Synopsis: This is the API used to shutdown the
  509. // IasHlpr Component
  510. //
  511. // Arguments: none
  512. //
  513. // Returns: VOID
  514. //
  515. // History: MKarki Created 2/10/98
  516. //
  517. //----------------------------------------------------------------
  518. extern "C" VOID WINAPI
  519. ShutdownIas (
  520. VOID
  521. )
  522. {
  523. HRESULT hr = S_OK;
  524. EnterCriticalSection (&g_SrvCritSect);
  525. IASTracePrintf ("Shutting down Surrogate....");
  526. //
  527. // stop the components first
  528. //
  529. if (FALSE == g_bInitialized)
  530. {
  531. LeaveCriticalSection (&g_SrvCritSect);
  532. return;
  533. }
  534. else
  535. {
  536. //
  537. // don't let any requests come through now
  538. //
  539. g_bInitialized = FALSE;
  540. }
  541. //
  542. // release the reference to the interface
  543. //
  544. if (NULL != g_pIRecvRequest)
  545. {
  546. g_pIRecvRequest->Release ();
  547. g_pIRecvRequest = NULL;
  548. }
  549. if (NULL != g_pISdoService)
  550. {
  551. //
  552. // stop the service
  553. //
  554. hr = g_pISdoService->StopService (SERVICE_TYPE_RAS);
  555. if (FAILED (hr))
  556. {
  557. IASTracePrintf ("Unable to stop SDO component");
  558. }
  559. //
  560. // shutdown the service
  561. //
  562. hr = g_pISdoService->ShutdownService (SERVICE_TYPE_RAS);
  563. if (FAILED (hr))
  564. {
  565. IASTracePrintf ("Unable to shutdown SDO component");
  566. }
  567. g_pISdoService->Release ();
  568. g_pISdoService = NULL;
  569. }
  570. IASTracePrintf ("Surrogate Shutdown complete.");
  571. IASTraceUninitialize();
  572. LeaveCriticalSection (&g_SrvCritSect);
  573. return;
  574. } // end of ShutdownIas method
  575. //++--------------------------------------------------------------
  576. //
  577. // Function: ConfigureIas
  578. //
  579. // Synopsis: This is the API that is called to reload the
  580. // IAS configuration information
  581. //
  582. // Arguments: none
  583. //
  584. // Returns: HRESULT - status
  585. //
  586. // History: MKarki Created 09/04/98
  587. //
  588. //----------------------------------------------------------------
  589. extern "C" HRESULT WINAPI
  590. ConfigureIas (
  591. VOID
  592. )
  593. {
  594. HRESULT hr = S_OK;
  595. EnterCriticalSection (&g_SrvCritSect);
  596. IASTracePrintf ("Configuring Surrogate.");
  597. if (FALSE == g_bInitialized)
  598. {
  599. IASTracePrintf (
  600. "InitializeIas needs to be called before configuring surrogate"
  601. );
  602. }
  603. else if (NULL != g_pISdoService)
  604. {
  605. hr = g_pISdoService->ConfigureService (SERVICE_TYPE_RAS);
  606. }
  607. LeaveCriticalSection (&g_SrvCritSect);
  608. return (hr);
  609. } // end of ConfigureIas method
  610. //++--------------------------------------------------------------
  611. //
  612. // Function: MemAllocIas
  613. //
  614. // Synopsis: This is the API used to allocate dynamic memory
  615. //
  616. // Arguments: none
  617. //
  618. // Returns: PVOID - address of allocated memory
  619. //
  620. // History: MKarki Created 2/10/98
  621. //
  622. //----------------------------------------------------------------
  623. extern "C" PVOID WINAPI
  624. MemAllocIas (
  625. DWORD dwSize
  626. )
  627. {
  628. return (::CoTaskMemAlloc (dwSize));
  629. } // end of MemAllocIas API
  630. //++--------------------------------------------------------------
  631. //
  632. // Function: MemFreeIas
  633. //
  634. // Synopsis: This is the API to free the dynamic memory
  635. // allocated through MemAllocIas
  636. //
  637. // Arguments: PVOID - address of allocated memory
  638. //
  639. // Returns: VOID
  640. //
  641. // History: MKarki Created 2/10/98
  642. //
  643. //----------------------------------------------------------------
  644. extern "C" VOID WINAPI
  645. MemFreeIas (
  646. PVOID pAllocMem
  647. )
  648. {
  649. ::CoTaskMemFree (pAllocMem);
  650. } // end of MemFreeIas API
  651. //++--------------------------------------------------------------
  652. //
  653. // Function: MemReallocIas
  654. //
  655. // Synopsis: This is the API to reallocate the already allocate
  656. // dynamic memory allocated through MemAllocIas
  657. //
  658. // Arguments: PVOID - address of allocated memory
  659. // DWORD - new size
  660. //
  661. // Returns: PVOID - adress of new memory
  662. //
  663. // History: MKarki Created 2/10/98
  664. //
  665. //----------------------------------------------------------------
  666. extern "C" PVOID WINAPI
  667. MemReallocIas (
  668. PVOID pAllocMem,
  669. DWORD dwNewSize
  670. )
  671. {
  672. return (::CoTaskMemRealloc (pAllocMem, dwNewSize));
  673. } // end of MemReallocIas API
  674. #include <atlimpl.cpp>