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.

304 lines
7.8 KiB

  1. #include <stdio.h>
  2. #include <netcfgx.h>
  3. #include <devguid.h>
  4. //
  5. // Localization library and MessageIds.
  6. //
  7. #include <nls.h>
  8. #include "localmsg.h"
  9. EXTERN_C void ausage(void);
  10. HRESULT
  11. HrCreateINetCfg (
  12. IN BOOL fAcquireWriteLock,
  13. OUT INetCfg** ppINetCfg)
  14. {
  15. HRESULT hr;
  16. INetCfg* pINetCfg;
  17. // Get the INetCfg interface.
  18. //
  19. hr = CoCreateInstance(
  20. CLSID_CNetCfg,
  21. NULL,
  22. CLSCTX_INPROC_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  23. IID_INetCfg,
  24. reinterpret_cast<void**>(&pINetCfg));
  25. if (S_OK == hr) {
  26. INetCfgLock * pnclock = NULL;
  27. if (fAcquireWriteLock) {
  28. // Get the locking interface
  29. hr = pINetCfg->QueryInterface(IID_INetCfgLock,
  30. reinterpret_cast<LPVOID *>(&pnclock));
  31. if (SUCCEEDED(hr)) {
  32. LPWSTR pwszLockHolder;
  33. // Attempt to lock the INetCfg for read/write
  34. hr = pnclock->AcquireWriteLock(100, L"InstallIPv6",
  35. &pwszLockHolder);
  36. if (S_FALSE == hr) {
  37. // Couldn't acquire the lock
  38. hr = NETCFG_E_NO_WRITE_LOCK;
  39. NlsPutMsg(STDOUT, IPV6_MESSAGE_0);
  40. // printf("The write lock could not be acquired.\n");
  41. NlsPutMsg(STDOUT, IPV6_MESSAGE_1, pwszLockHolder);
  42. // printf("You must close %ls first.\n", pwszLockHolder);
  43. }
  44. if (pwszLockHolder) {
  45. CoTaskMemFree(pwszLockHolder);
  46. }
  47. }
  48. }
  49. if (S_OK == hr) {
  50. hr = pINetCfg->Initialize (NULL);
  51. if (S_OK == hr) {
  52. *ppINetCfg = pINetCfg;
  53. pINetCfg->AddRef();
  54. }
  55. else {
  56. if (pnclock) {
  57. pnclock->ReleaseWriteLock();
  58. }
  59. }
  60. }
  61. if (pnclock) {
  62. pnclock->Release();
  63. }
  64. //Transfer ownership to caller.
  65. pINetCfg->Release();
  66. }
  67. return hr;
  68. }
  69. VOID
  70. pAddOrRemoveIpv6(BOOL fAddIpv6)
  71. {
  72. HRESULT hr;
  73. INetCfg* pINetCfg;
  74. hr = HrCreateINetCfg (TRUE, &pINetCfg);
  75. if (S_OK == hr) {
  76. INetCfgClassSetup* pSetup;
  77. // Get the setup interface used for installing
  78. // and uninstalling components.
  79. //
  80. hr = pINetCfg->QueryNetCfgClass (
  81. &GUID_DEVCLASS_NETTRANS,
  82. IID_INetCfgClassSetup,
  83. (VOID**)&pSetup);
  84. if (S_OK == hr) {
  85. OBO_TOKEN OboToken;
  86. INetCfgComponent* pIComp;
  87. ZeroMemory (&OboToken, sizeof(OboToken));
  88. OboToken.Type = OBO_USER;
  89. if (fAddIpv6) {
  90. NlsPutMsg(STDOUT, IPV6_MESSAGE_2);
  91. // printf("Installing...\n");
  92. hr = pSetup->Install (
  93. L"MS_TCPIP6",
  94. &OboToken,
  95. 0, 0, NULL, NULL,
  96. &pIComp);
  97. if (pIComp) {
  98. pIComp->Release();
  99. }
  100. }
  101. else {
  102. // Need to remove the component.
  103. // Find it first.
  104. //
  105. hr = pINetCfg->FindComponent (
  106. L"MS_TCPIP6",
  107. &pIComp);
  108. if (S_OK == hr) {
  109. NlsPutMsg(STDOUT, IPV6_MESSAGE_3);
  110. // printf("Uninstalling...\n");
  111. hr = pSetup->DeInstall (
  112. pIComp,
  113. &OboToken,
  114. NULL);
  115. pIComp->Release();
  116. }
  117. else {
  118. NlsPutMsg(STDOUT, IPV6_MESSAGE_4);
  119. // printf("Microsoft IPv6 Developer Edition is not installed.\n");
  120. }
  121. }
  122. if (SUCCEEDED(hr)) {
  123. if (NETCFG_S_REBOOT == hr) {
  124. hr = S_OK;
  125. NlsPutMsg(STDOUT, IPV6_MESSAGE_5);
  126. // printf("A reboot is required to complete this action.\n");
  127. }
  128. else {
  129. NlsPutMsg(STDOUT, IPV6_MESSAGE_6);
  130. // printf("Succeeded.\n");
  131. }
  132. }
  133. else {
  134. NlsPutMsg(STDOUT, IPV6_MESSAGE_7);
  135. // printf("Failed to complete the action.\n");
  136. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr) {
  137. hr = S_OK;
  138. NlsPutMsg(STDOUT, IPV6_MESSAGE_8);
  139. // printf("The INF file for Microsoft IPv6 Developer Edition could not be found.\n");
  140. }
  141. else if (NETCFG_E_NEED_REBOOT == hr) {
  142. NlsPutMsg(STDOUT, IPV6_MESSAGE_9);
  143. // printf("A reboot is required before any further changes can be made.\n");
  144. }
  145. else {
  146. NlsPutMsg(STDOUT, IPV6_MESSAGE_10, hr);
  147. // printf("Error 0x%08x\n", hr);
  148. }
  149. }
  150. pSetup->Release();
  151. }
  152. hr = pINetCfg->Uninitialize();
  153. if (SUCCEEDED(hr))
  154. {
  155. INetCfgLock * pnclock;
  156. // Get the locking interface
  157. hr = pINetCfg->QueryInterface(IID_INetCfgLock,
  158. reinterpret_cast<LPVOID *>(&pnclock));
  159. if (SUCCEEDED(hr))
  160. {
  161. // Attempt to lock the INetCfg for read/write
  162. hr = pnclock->ReleaseWriteLock();
  163. pnclock->Release();
  164. }
  165. }
  166. pINetCfg->Release();
  167. }
  168. else if (NETCFG_E_NO_WRITE_LOCK == hr) {
  169. // Message has already been printed
  170. }
  171. else if (HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) == hr) {
  172. ausage();
  173. }
  174. else {
  175. NlsPutMsg(STDOUT, IPV6_MESSAGE_11, hr);
  176. // printf("Problem 0x%08x occurred.\n", hr);
  177. }
  178. }
  179. EXTERN_C
  180. BOOL
  181. IsIpv6Installed()
  182. {
  183. HRESULT hr = S_OK;
  184. BOOL fInitCom = TRUE;
  185. BOOL fPresent = FALSE;
  186. // Initialize COM.
  187. //
  188. hr = CoInitializeEx( NULL,
  189. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  190. if (RPC_E_CHANGED_MODE == hr) {
  191. // If we changed mode, then we won't uninitialize COM when we are done.
  192. //
  193. hr = S_OK;
  194. fInitCom = FALSE;
  195. }
  196. if (SUCCEEDED(hr)) {
  197. HRESULT hr;
  198. INetCfg* pINetCfg;
  199. hr = HrCreateINetCfg (FALSE, &pINetCfg);
  200. if (S_OK == hr) {
  201. fPresent = (S_OK == pINetCfg->FindComponent(L"MS_TCPIP6", NULL));
  202. pINetCfg->Uninitialize();
  203. pINetCfg->Release();
  204. }
  205. else {
  206. NlsPutMsg(STDOUT, IPV6_MESSAGE_12, hr);
  207. // printf("Problem 0x%08x occurred while accessing network configuration.\n", hr);
  208. exit(1);
  209. }
  210. if (fInitCom) {
  211. CoUninitialize();
  212. }
  213. }
  214. else {
  215. NlsPutMsg(STDOUT, IPV6_MESSAGE_13, hr);
  216. // printf("Problem 0x%08x initializing COM library\n", hr);
  217. }
  218. return fPresent;
  219. }
  220. EXTERN_C
  221. void
  222. AddOrRemoveIpv6 (
  223. IN BOOL fAddIpv6)
  224. {
  225. HRESULT hr = S_OK;
  226. BOOL fInitCom = TRUE;
  227. // Initialize COM.
  228. //
  229. hr = CoInitializeEx( NULL,
  230. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  231. if (RPC_E_CHANGED_MODE == hr) {
  232. // If we changed mode, then we won't uninitialize COM when we are done.
  233. //
  234. hr = S_OK;
  235. fInitCom = FALSE;
  236. }
  237. if (SUCCEEDED(hr)) {
  238. pAddOrRemoveIpv6(fAddIpv6);
  239. if (fInitCom) {
  240. CoUninitialize();
  241. }
  242. }
  243. else {
  244. NlsPutMsg(STDOUT, IPV6_MESSAGE_13, hr);
  245. // printf("Problem 0x%08x initializing COM library\n", hr);
  246. }
  247. }