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.

404 lines
11 KiB

  1. /*
  2. File inetcfgp.h
  3. Private helper functions for dealing with inetcfg.
  4. Paul Mayfield, 1/5/98 (implementation by shaunco)
  5. */
  6. #include "inetcfgp.h"
  7. //+---------------------------------------------------------------------------
  8. //
  9. // Function: HrCreateAndInitializeINetCfg
  10. //
  11. // Purpose: Cocreate and initialize the root INetCfg object. This will
  12. // optionally initialize COM for the caller too.
  13. //
  14. // Arguments:
  15. // fInitCom [in out] TRUE to call CoInitialize before creating.
  16. // ppnc [out] The returned INetCfg object.
  17. // fGetWriteLock [in] TRUE if a writable INetCfg is needed
  18. // cmsTimeout [in] See INetCfg::AcquireWriteLock
  19. // szwClientDesc [in] See INetCfg::AcquireWriteLock
  20. // ppszwClientDesc [out] See INetCfg::AcquireWriteLock
  21. //
  22. // Returns: S_OK or an error code.
  23. //
  24. // Author: shaunco 7 May 1997
  25. //
  26. // Notes:
  27. //
  28. HRESULT APIENTRY
  29. HrCreateAndInitializeINetCfg (
  30. BOOL* pfInitCom,
  31. INetCfg** ppnc,
  32. BOOL fGetWriteLock,
  33. DWORD cmsTimeout,
  34. LPCWSTR szwClientDesc,
  35. LPWSTR* ppszwClientDesc)
  36. {
  37. HRESULT hr = S_OK;
  38. BOOL fCoUninitialize = *pfInitCom;
  39. // Initialize the output parameters.
  40. *ppnc = NULL;
  41. if (ppszwClientDesc)
  42. {
  43. *ppszwClientDesc = NULL;
  44. }
  45. // Initialize COM if the caller requested.
  46. if (*pfInitCom)
  47. {
  48. //For whistler bug 398715 gangz
  49. //
  50. hr = CoInitializeEx (NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
  51. if( RPC_E_CHANGED_MODE == hr )
  52. {
  53. hr = CoInitializeEx (NULL,
  54. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
  55. }
  56. if ( SUCCEEDED(hr) )
  57. {
  58. hr = S_OK;
  59. *pfInitCom = TRUE;
  60. fCoUninitialize = TRUE;
  61. }
  62. else
  63. {
  64. *pfInitCom = FALSE;
  65. fCoUninitialize = FALSE;
  66. }
  67. }
  68. if (SUCCEEDED(hr))
  69. {
  70. // Create the object implementing INetCfg.
  71. //
  72. INetCfg* pnc;
  73. hr = CoCreateInstance (&CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER,
  74. &IID_INetCfg, (void**)&pnc);
  75. if (SUCCEEDED(hr))
  76. {
  77. INetCfgLock* pnclock = NULL;
  78. if (fGetWriteLock)
  79. {
  80. // Get the locking interface
  81. hr = INetCfg_QueryInterface(pnc, &IID_INetCfgLock,
  82. (void**)&pnclock);
  83. if (SUCCEEDED(hr))
  84. {
  85. // Attempt to lock the INetCfg for read/write
  86. hr = INetCfgLock_AcquireWriteLock(pnclock, cmsTimeout,
  87. szwClientDesc, ppszwClientDesc);
  88. if (S_FALSE == hr)
  89. {
  90. // Couldn't acquire the lock
  91. hr = NETCFG_E_NO_WRITE_LOCK;
  92. }
  93. }
  94. }
  95. if (SUCCEEDED(hr))
  96. {
  97. // Initialize the INetCfg object.
  98. //
  99. hr = INetCfg_Initialize (pnc, NULL);
  100. if (SUCCEEDED(hr))
  101. {
  102. *ppnc = pnc;
  103. IUnknown_AddRef (pnc);
  104. }
  105. else
  106. {
  107. if (pnclock)
  108. {
  109. INetCfgLock_ReleaseWriteLock(pnclock);
  110. }
  111. }
  112. // Transfer reference to caller.
  113. }
  114. ReleaseObj (pnclock);
  115. ReleaseObj (pnc);
  116. }
  117. // If we failed anything above, and we've initialized COM,
  118. // be sure an uninitialize it.
  119. //
  120. if (FAILED(hr) && fCoUninitialize)
  121. {
  122. CoUninitialize ();
  123. }
  124. }
  125. return hr;
  126. }
  127. //+---------------------------------------------------------------------------
  128. //
  129. // Function: HrUninitializeAndUnlockINetCfg
  130. //
  131. // Purpose: Uninitializes and unlocks the INetCfg object
  132. //
  133. // Arguments:
  134. // pnc [in] INetCfg to uninitialize and unlock
  135. //
  136. // Returns: S_OK if success, OLE or Win32 error otherwise
  137. //
  138. // Author: danielwe 13 Nov 1997
  139. //
  140. // Notes:
  141. //
  142. HRESULT APIENTRY
  143. HrUninitializeAndUnlockINetCfg(
  144. INetCfg* pnc)
  145. {
  146. HRESULT hr = INetCfg_Uninitialize (pnc);
  147. if (SUCCEEDED(hr))
  148. {
  149. INetCfgLock* pnclock;
  150. // Get the locking interface
  151. hr = INetCfg_QueryInterface (pnc, &IID_INetCfgLock, (void**)&pnclock);
  152. if (SUCCEEDED(hr))
  153. {
  154. // Attempt to lock the INetCfg for read/write
  155. hr = INetCfgLock_ReleaseWriteLock (pnclock);
  156. ReleaseObj (pnclock);
  157. }
  158. }
  159. return hr;
  160. }
  161. //+---------------------------------------------------------------------------
  162. //
  163. // Function: HrUninitializeAndReleaseINetCfg
  164. //
  165. // Purpose: Unintialize and release an INetCfg object. This will
  166. // optionally uninitialize COM for the caller too.
  167. //
  168. // Arguments:
  169. // fUninitCom [in] TRUE to uninitialize COM after the INetCfg is
  170. // uninitialized and released.
  171. // pnc [in] The INetCfg object.
  172. // fHasLock [in] TRUE if the INetCfg was locked for write and
  173. // must be unlocked.
  174. //
  175. // Returns: S_OK or an error code.
  176. //
  177. // Author: shaunco 7 May 1997
  178. //
  179. // Notes: The return value is the value returned from
  180. // INetCfg::Uninitialize. Even if this fails, the INetCfg
  181. // is still released. Therefore, the return value is for
  182. // informational purposes only. You can't touch the INetCfg
  183. // object after this call returns.
  184. //
  185. HRESULT APIENTRY
  186. HrUninitializeAndReleaseINetCfg (
  187. BOOL fUninitCom,
  188. INetCfg* pnc,
  189. BOOL fHasLock)
  190. {
  191. HRESULT hr = S_OK;
  192. if (fHasLock)
  193. {
  194. hr = HrUninitializeAndUnlockINetCfg (pnc);
  195. }
  196. else
  197. {
  198. hr = INetCfg_Uninitialize (pnc);
  199. }
  200. ReleaseObj (pnc);
  201. if (fUninitCom)
  202. {
  203. CoUninitialize ();
  204. }
  205. return hr;
  206. }
  207. //+---------------------------------------------------------------------------
  208. //
  209. // Function: HrEnumComponentsInClasses
  210. //
  211. // Purpose: Given an array of class guids, return all of the components
  212. // from INetCfg that belong to those classes in one, unified,
  213. // array.
  214. //
  215. // Arguments:
  216. // pNetCfg [in]
  217. // cpguidClass [in]
  218. // apguidClass [in]
  219. // celt [in]
  220. // rgelt [out]
  221. // pceltFetched [out]
  222. //
  223. // Returns: S_OK or an error.
  224. //
  225. // Author: shaunco 12 Dec 1997
  226. //
  227. // Notes:
  228. //
  229. HRESULT APIENTRY
  230. HrEnumComponentsInClasses (
  231. INetCfg* pNetCfg,
  232. ULONG cpguidClass,
  233. GUID** apguidClass,
  234. ULONG celt,
  235. INetCfgComponent** rgelt,
  236. ULONG* pceltFetched)
  237. {
  238. ULONG iGuid;
  239. HRESULT hr = S_OK;
  240. // Initialize the output paramters
  241. //
  242. *pceltFetched = 0;
  243. for (iGuid = 0; iGuid < cpguidClass; iGuid++)
  244. {
  245. // Get the INetCfgClass object this guid represents.
  246. //
  247. INetCfgClass* pClass;
  248. hr = INetCfg_QueryNetCfgClass (pNetCfg, apguidClass[iGuid],
  249. &IID_INetCfgClass, (void**)&pClass);
  250. if (SUCCEEDED(hr))
  251. {
  252. // Get the component enumerator for this class.
  253. //
  254. IEnumNetCfgComponent* pEnum;
  255. hr = INetCfgClass_EnumComponents (pClass, &pEnum);
  256. if (SUCCEEDED(hr))
  257. {
  258. // Enumerate the components.
  259. //
  260. ULONG celtFetched;
  261. hr = IEnumNetCfgComponent_Next (pEnum, celt,
  262. rgelt, &celtFetched);
  263. if (SUCCEEDED(hr))
  264. {
  265. celt -= celtFetched;
  266. rgelt += celtFetched;
  267. *pceltFetched += celtFetched;
  268. }
  269. ReleaseObj (pEnum);
  270. }
  271. ReleaseObj (pClass);
  272. }
  273. }
  274. return hr;
  275. }
  276. //+---------------------------------------------------------------------------
  277. //
  278. // Function: ReleaseObj
  279. //
  280. // Purpose: Makes it easier to call punk->Release. Also allows NULL
  281. // input.
  282. //
  283. // Arguments:
  284. // punk [in] IUnknown pointer to release.
  285. //
  286. // Returns: punk->Release if punk is non-NULL, zero otherwise.
  287. //
  288. // Author: shaunco 13 Dec 1997
  289. //
  290. // Notes:
  291. //
  292. ULONG APIENTRY
  293. ReleaseObj (
  294. void* punk)
  295. {
  296. return (punk) ? IUnknown_Release ((IUnknown*)punk) : 0;
  297. }
  298. //+---------------------------------------------------------------------------
  299. //
  300. // Function: HrCreateNetConnectionUtilities
  301. //
  302. // Purpose: Retrieve an interface to the Net Connection Ui Utilities
  303. //
  304. // Arguments:
  305. // ppncuu [out] The returned INetConnectionUiUtilities interface.
  306. //
  307. // Returns: S_OK and SUCCESS, a HRESULT error on failure
  308. //
  309. // Author: scottbri 15 Oct 1998
  310. //
  311. // Notes:
  312. //
  313. HRESULT APIENTRY
  314. HrCreateNetConnectionUtilities(INetConnectionUiUtilities ** ppncuu)
  315. {
  316. HRESULT hr;
  317. hr = CoCreateInstance (&CLSID_NetConnectionUiUtilities, NULL,
  318. CLSCTX_INPROC_SERVER,
  319. &IID_INetConnectionUiUtilities, (void**)ppncuu);
  320. return hr;
  321. }
  322. //To get the firewall's group policy value for bug 342810 328673
  323. //
  324. BOOL
  325. IsGPAEnableFirewall(
  326. void)
  327. {
  328. BOOL fEnableFirewall = FALSE;
  329. BOOL fComInitialized = FALSE;
  330. BOOL fCleanupOle = TRUE;
  331. HRESULT hr;
  332. INetConnectionUiUtilities * pNetConUtilities = NULL;
  333. hr = CoInitializeEx(NULL,
  334. COINIT_MULTITHREADED|COINIT_DISABLE_OLE1DDE);
  335. if ( RPC_E_CHANGED_MODE == hr )
  336. {
  337. hr = CoInitializeEx (NULL,
  338. COINIT_APARTMENTTHREADED |COINIT_DISABLE_OLE1DDE);
  339. }
  340. if (FAILED(hr))
  341. {
  342. fCleanupOle = FALSE;
  343. fComInitialized = FALSE;
  344. }
  345. else
  346. {
  347. fCleanupOle = TRUE;
  348. fComInitialized = TRUE;
  349. }
  350. if ( fComInitialized )
  351. {
  352. hr = HrCreateNetConnectionUtilities(&pNetConUtilities);
  353. if ( SUCCEEDED(hr))
  354. {
  355. fEnableFirewall =
  356. INetConnectionUiUtilities_UserHasPermission(
  357. pNetConUtilities, NCPERM_PersonalFirewallConfig );
  358. INetConnectionUiUtilities_Release(pNetConUtilities);
  359. }
  360. if (fCleanupOle)
  361. {
  362. CoUninitialize();
  363. }
  364. }
  365. return fEnableFirewall;
  366. }