VirtualBox

source: vbox/trunk/src/VBox/Installer/win/InstallHelper/VBoxInstallHelper.cpp@ 52592

Last change on this file since 52592 was 52592, checked in by vboxsync, 11 years ago

NetFlt/win: NDIS6: fixes, enable disconnect interface, PM support for NetAdp6, installer helper functions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.8 KB
Line 
1/* $Id: VBoxInstallHelper.cpp 52592 2014-09-03 20:23:24Z vboxsync $ */
2/** @file
3 * VBoxInstallHelper - Various helper routines for Windows host installer.
4 */
5
6/*
7 * Copyright (C) 2008-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#ifdef VBOX_WITH_NETFLT
23# include "VBox/VBoxNetCfg-win.h"
24# include "VBox/VBoxDrvCfg-win.h"
25#endif /* VBOX_WITH_NETFLT */
26
27#include <VBox/version.h>
28
29#include <windows.h>
30#include <wchar.h>
31#include <stdio.h>
32
33#include <msi.h>
34#include <msiquery.h>
35
36#define _WIN32_DCOM
37#include <windows.h>
38#include <assert.h>
39#include <shellapi.h>
40#define INITGUID
41#include <guiddef.h>
42#include <devguid.h>
43#include <objbase.h>
44#include <setupapi.h>
45#include <shlobj.h>
46#include <cfgmgr32.h>
47
48#include "VBoxCommon.h"
49
50#ifndef VBOX_OSE
51# include "internal/VBoxSerial.h"
52#endif
53
54
55/*******************************************************************************
56* Header Files *
57*******************************************************************************/
58#ifdef DEBUG
59# define NonStandardAssert(_expr) assert(_expr)
60#else
61# define NonStandardAssert(_expr) do{ }while(0)
62#endif
63
64#define MY_WTEXT_HLP(a_str) L##a_str
65#define MY_WTEXT(a_str) MY_WTEXT_HLP(a_str)
66
67
68BOOL APIENTRY DllMain(HANDLE hModule,
69 DWORD ul_reason_for_call,
70 LPVOID lpReserved)
71{
72 return TRUE;
73}
74
75static void logString(MSIHANDLE hInstall, LPCSTR szString, ...)
76{
77 PMSIHANDLE newHandle = MsiCreateRecord(2);
78
79 char szBuffer[1024];
80 va_list va;
81 va_start(va, szString);
82 _vsnprintf(szBuffer, sizeof(szBuffer), szString, va);
83 va_end(va);
84
85 MsiRecordSetStringA(newHandle, 0, szBuffer);
86 MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_INFO), newHandle);
87 MsiCloseHandle(newHandle);
88
89#if 0/*def DEBUG - wrong? What does '%s' expect, wchar or char? */
90 _tprintf(_T("Debug: %s\n"), szBuffer);
91#endif
92}
93
94static void logStringW(MSIHANDLE hInstall, LPCWSTR pwszString, ...)
95{
96 PMSIHANDLE newHandle = MsiCreateRecord(2);
97
98 WCHAR szBuffer[1024];
99 va_list va;
100 va_start(va, pwszString);
101 _vsnwprintf(szBuffer, RT_ELEMENTS(szBuffer), pwszString, va);
102 va_end(va);
103
104 MsiRecordSetStringW(newHandle, 0, szBuffer);
105 MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_INFO), newHandle);
106 MsiCloseHandle(newHandle);
107}
108
109UINT __stdcall IsSerialCheckNeeded(MSIHANDLE hModule)
110{
111#ifndef VBOX_OSE
112 /*BOOL bRet =*/ serialCheckNeeded(hModule);
113#endif
114 return ERROR_SUCCESS;
115}
116
117UINT __stdcall CheckSerial(MSIHANDLE hModule)
118{
119#ifndef VBOX_OSE
120 /*BOOL bRet =*/ serialIsValid(hModule);
121#endif
122 return ERROR_SUCCESS;
123}
124
125DWORD Exec(MSIHANDLE hModule,
126 const WCHAR *pwszAppName, WCHAR *pwszCmdLine, const WCHAR *pwszWorkDir,
127 DWORD *pdwExitCode)
128{
129 STARTUPINFOW si;
130 PROCESS_INFORMATION pi;
131 DWORD rc = ERROR_SUCCESS;
132
133 ZeroMemory(&si, sizeof(si));
134 si.dwFlags = STARTF_USESHOWWINDOW;
135#ifdef UNICODE
136 si.dwFlags |= CREATE_UNICODE_ENVIRONMENT;
137#endif
138 si.wShowWindow = SW_HIDE; /* For debugging: SW_SHOW; */
139 si.cb = sizeof(si);
140 ZeroMemory(&pi, sizeof(pi));
141
142 logStringW(hModule, L"Executing command line: %s %s (Working Dir: %s)",
143 pwszAppName, pwszCmdLine, pwszWorkDir == NULL ? L"Current" : pwszWorkDir);
144
145 SetLastError(0);
146 if (!CreateProcessW(pwszAppName, /* Module name. */
147 pwszCmdLine, /* Command line. */
148 NULL, /* Process handle not inheritable. */
149 NULL, /* Thread handle not inheritable. */
150 FALSE, /* Set handle inheritance to FALSE .*/
151 0, /* No creation flags. */
152 NULL, /* Use parent's environment block. */
153 pwszWorkDir, /* Use parent's starting directory. */
154 &si, /* Pointer to STARTUPINFO structure. */
155 &pi)) /* Pointer to PROCESS_INFORMATION structure. */
156 {
157 rc = GetLastError();
158 logStringW(hModule, L"Executing command line: CreateProcess() failed! Error: %ld", rc);
159 return rc;
160 }
161
162 /* Wait until child process exits. */
163 if (WAIT_FAILED == WaitForSingleObject(pi.hProcess, 30 * 1000 /* Wait 30 secs max. */))
164 {
165 rc = GetLastError();
166 logStringW(hModule, L"Executing command line: WaitForSingleObject() failed! Error: %u", rc);
167 }
168 else
169 {
170 if (!GetExitCodeProcess(pi.hProcess, pdwExitCode))
171 {
172 rc = GetLastError();
173 logStringW(hModule, L"Executing command line: GetExitCodeProcess() failed! Error: %u", rc);
174 }
175 }
176
177 /* Close process and thread handles. */
178 CloseHandle(pi.hProcess);
179 CloseHandle(pi.hThread);
180
181 logStringW(hModule, L"Executing command returned: %u (exit code %u)", rc, *pdwExitCode);
182 return rc;
183}
184
185UINT __stdcall InstallPythonAPI(MSIHANDLE hModule)
186{
187 logStringW(hModule, L"InstallPythonAPI: Checking for installed Python environment ...");
188
189 HKEY hkPythonCore = NULL;
190 BOOL bFound = FALSE;
191 LONG rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Python\\PythonCore", 0, KEY_READ, &hkPythonCore);
192 if (rc != ERROR_SUCCESS)
193 {
194 logStringW(hModule, L"InstallPythonAPI: Python seems not to be installed.");
195 return ERROR_SUCCESS;
196 }
197
198 WCHAR wszPath[MAX_PATH] = { 0 };
199 WCHAR wszVal[MAX_PATH] = { 0 };
200
201 for (int i = 0;; ++i)
202 {
203 WCHAR wszRoot[MAX_PATH] = { 0 };
204 DWORD dwLen = sizeof(wszPath);
205 DWORD dwKeyType = REG_SZ;
206
207 rc = RegEnumKeyExW(hkPythonCore, i, wszRoot, &dwLen, NULL, NULL, NULL, NULL);
208 if (rc != ERROR_SUCCESS || dwLen <= 0)
209 break;
210
211 swprintf_s(wszPath, RT_ELEMENTS(wszPath), L"%s\\InstallPath", wszRoot);
212 dwLen = sizeof(wszVal);
213
214 HKEY hkPythonInstPath = NULL;
215 rc = RegOpenKeyExW(hkPythonCore, wszPath, 0, KEY_READ, &hkPythonInstPath);
216 if (rc != ERROR_SUCCESS)
217 continue;
218
219 rc = RegQueryValueExW(hkPythonInstPath, L"", NULL, &dwKeyType, (LPBYTE)wszVal, &dwLen);
220 if (rc == ERROR_SUCCESS)
221 logStringW(hModule, L"InstallPythonAPI: Path \"%s\" detected.", wszVal);
222
223 RegCloseKey(hkPythonInstPath);
224 }
225 RegCloseKey(hkPythonCore);
226
227 /* Python path found? */
228 WCHAR wszExec[MAX_PATH] = { 0 };
229 WCHAR wszCmdLine[MAX_PATH] = { 0 };
230 DWORD dwExitCode = 0;
231 if (wcslen(wszVal) > 0)
232 {
233 /* Cool, check for installed Win32 extensions. */
234 logStringW(hModule, L"InstallPythonAPI: Python installed. Checking for Win32 extensions ...");
235 swprintf_s(wszExec, RT_ELEMENTS(wszExec), L"%s\\python.exe", wszVal);
236 swprintf_s(wszCmdLine, RT_ELEMENTS(wszCmdLine), L"%s\\python.exe -c \"import win32api\"", wszVal);
237
238 DWORD dwRetExec = Exec(hModule, wszExec, wszCmdLine, NULL, &dwExitCode);
239 if ( (ERROR_SUCCESS == dwRetExec)
240 && ( 0 == dwExitCode))
241 {
242 /* Did we get the correct error level (=0)? */
243 logStringW(hModule, L"InstallPythonAPI: Win32 extensions installed.");
244 bFound = TRUE;
245 }
246 else
247 logStringW(hModule, L"InstallPythonAPI: Win32 extensions not found.");
248 }
249
250 BOOL bInstalled = FALSE;
251 if (bFound) /* Is Python and all required stuff installed? */
252 {
253 /* Get the VBoxAPI setup string. */
254 WCHAR wszPathTargetDir[MAX_PATH] = {0};
255 VBoxGetProperty(hModule, L"CustomActionData", wszPathTargetDir, sizeof(wszPathTargetDir));
256 if (wcslen(wszPathTargetDir))
257 {
258 /* Set final path. */
259 swprintf_s(wszPath, RT_ELEMENTS(wszPath), L"%s", wszPathTargetDir);
260
261 /* Install our API module. */
262 swprintf_s(wszCmdLine, RT_ELEMENTS(wszCmdLine), L"%s\\python.exe vboxapisetup.py install", wszVal);
263
264 /* Set required environment variables. */
265 if (!SetEnvironmentVariableW(L"VBOX_INSTALL_PATH", wszPathTargetDir))
266 logStringW(hModule, L"InstallPythonAPI: Could set environment variable VBOX_INSTALL_PATH!");
267 else
268 {
269 DWORD dwRetExec = Exec(hModule, wszExec, wszCmdLine, wszPath, &dwExitCode);
270 if ( (ERROR_SUCCESS == dwRetExec)
271 && ( 0 == dwExitCode))
272 {
273 /* All done! */
274 logStringW(hModule, L"InstallPythonAPI: VBoxAPI for Python successfully installed.");
275 bInstalled = TRUE;
276 }
277 else
278 {
279 if (dwRetExec)
280 logStringW(hModule, L"InstallPythonAPI: Error while executing installation of VBox API: %ld", dwRetExec);
281 else
282 logStringW(hModule, L"InstallPythonAPI: Python reported an error while installing VBox API: %ld", dwExitCode);
283 }
284 }
285 }
286 else
287 logStringW(hModule, L"InstallPythonAPI: Unable to retrieve VBox installation path!");
288 }
289
290 VBoxSetProperty(hModule, L"VBOX_PYTHON_IS_INSTALLED", bInstalled ? L"1" : L"0");
291
292 if (!bInstalled)
293 logStringW(hModule, L"InstallPythonAPI: VBox API not installed.");
294 return ERROR_SUCCESS; /* Do not fail here. */
295}
296
297static LONG installBrandingValue(MSIHANDLE hModule,
298 const WCHAR *pwszFileName,
299 const WCHAR *pwszSection,
300 const WCHAR *pwszValue)
301{
302 LONG rc;
303 WCHAR wszValue[_MAX_PATH];
304 if (GetPrivateProfileStringW(pwszSection, pwszValue, NULL,
305 wszValue, sizeof(wszValue), pwszFileName) > 0)
306 {
307 HKEY hkBranding;
308 WCHAR wszKey[_MAX_PATH];
309
310 if (wcsicmp(L"General", pwszSection) != 0)
311 swprintf_s(wszKey, RT_ELEMENTS(wszKey), L"SOFTWARE\\%s\\VirtualBox\\Branding\\", VBOX_VENDOR_SHORT, pwszSection);
312 else
313 swprintf_s(wszKey, RT_ELEMENTS(wszKey), L"SOFTWARE\\%s\\VirtualBox\\Branding", VBOX_VENDOR_SHORT);
314
315 rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszKey, 0, KEY_WRITE, &hkBranding);
316 if (rc == ERROR_SUCCESS)
317 {
318 rc = RegSetValueExW(hkBranding,
319 pwszValue,
320 NULL,
321 REG_SZ,
322 (BYTE *)wszValue,
323 (DWORD)wcslen(wszValue));
324 if (rc != ERROR_SUCCESS)
325 logStringW(hModule, L"InstallBranding: Could not write value %s! Error %ld", pwszValue, rc);
326 RegCloseKey (hkBranding);
327 }
328 }
329 else
330 rc = ERROR_NOT_FOUND;
331 return rc;
332}
333
334UINT CopyDir(MSIHANDLE hModule, const WCHAR *pwszDestDir, const WCHAR *pwszSourceDir)
335{
336 UINT rc;
337 WCHAR wszDest[_MAX_PATH + 1];
338 WCHAR wszSource[_MAX_PATH + 1];
339
340 swprintf_s(wszDest, RT_ELEMENTS(wszDest), L"%s%c", pwszDestDir, '\0');
341 swprintf_s(wszSource, RT_ELEMENTS(wszSource), L"%s%c", pwszSourceDir, '\0');
342
343 SHFILEOPSTRUCTW s = {0};
344 s.hwnd = NULL;
345 s.wFunc = FO_COPY;
346 s.pTo = wszDest;
347 s.pFrom = wszSource;
348 s.fFlags = FOF_SILENT |
349 FOF_NOCONFIRMATION |
350 FOF_NOCONFIRMMKDIR |
351 FOF_NOERRORUI;
352
353 logStringW(hModule, L"CopyDir: DestDir=%s, SourceDir=%s", wszDest, wszSource);
354 int r = SHFileOperationW(&s);
355 if (r != 0)
356 {
357 logStringW(hModule, L"CopyDir: Copy operation returned status 0x%x", r);
358 rc = ERROR_GEN_FAILURE;
359 }
360 else
361 rc = ERROR_SUCCESS;
362 return rc;
363}
364
365UINT RemoveDir(MSIHANDLE hModule, const WCHAR *pwszDestDir)
366{
367 UINT rc;
368 WCHAR wszDest[_MAX_PATH + 1];
369
370 swprintf_s(wszDest, RT_ELEMENTS(wszDest), L"%s%c", pwszDestDir, '\0');
371
372 SHFILEOPSTRUCTW s = {0};
373 s.hwnd = NULL;
374 s.wFunc = FO_DELETE;
375 s.pFrom = wszDest;
376 s.fFlags = FOF_SILENT
377 | FOF_NOCONFIRMATION
378 | FOF_NOCONFIRMMKDIR
379 | FOF_NOERRORUI;
380
381 logStringW(hModule, L"RemoveDir: DestDir=%s", wszDest);
382 int r = SHFileOperationW(&s);
383 if (r != 0)
384 {
385 logStringW(hModule, L"RemoveDir: Remove operation returned status 0x%x", r);
386 rc = ERROR_GEN_FAILURE;
387 }
388 else
389 rc = ERROR_SUCCESS;
390 return rc;
391}
392
393UINT RenameDir(MSIHANDLE hModule, const WCHAR *pwszDestDir, const WCHAR *pwszSourceDir)
394{
395 UINT rc;
396 WCHAR wszDest[_MAX_PATH + 1];
397 WCHAR wszSource[_MAX_PATH + 1];
398
399 swprintf_s(wszDest, RT_ELEMENTS(wszDest), L"%s%c", pwszDestDir, '\0');
400 swprintf_s(wszSource, RT_ELEMENTS(wszSource), L"%s%c", pwszSourceDir, '\0');
401
402 SHFILEOPSTRUCTW s = {0};
403 s.hwnd = NULL;
404 s.wFunc = FO_RENAME;
405 s.pTo = wszDest;
406 s.pFrom = wszSource;
407 s.fFlags = FOF_SILENT
408 | FOF_NOCONFIRMATION
409 | FOF_NOCONFIRMMKDIR
410 | FOF_NOERRORUI;
411
412 logStringW(hModule, L"RenameDir: DestDir=%s, SourceDir=%s", wszDest, wszSource);
413 int r = SHFileOperationW(&s);
414 if (r != 0)
415 {
416 logStringW(hModule, L"RenameDir: Rename operation returned status 0x%x", r);
417 rc = ERROR_GEN_FAILURE;
418 }
419 else
420 rc = ERROR_SUCCESS;
421 return rc;
422}
423
424UINT __stdcall UninstallBranding(MSIHANDLE hModule)
425{
426 UINT rc;
427 logStringW(hModule, L"UninstallBranding: Handling branding file ...");
428
429 WCHAR wszPathTargetDir[_MAX_PATH];
430 WCHAR wszPathDest[_MAX_PATH];
431
432 rc = VBoxGetProperty(hModule, L"CustomActionData", wszPathTargetDir, sizeof(wszPathTargetDir));
433 if (rc == ERROR_SUCCESS)
434 {
435 /** @todo Check trailing slash after %s. */
436/** @todo r=bird: using swprintf_s for formatting paths without checking for
437 * overflow not good. You're dodging the buffer overflow issue only to end up
438 * with incorrect behavior! (Truncated file/dir names)
439 *
440 * This applies almost to all swprintf_s calls in this file!!
441 */
442 swprintf_s(wszPathDest, RT_ELEMENTS(wszPathDest), L"%scustom", wszPathTargetDir);
443 rc = RemoveDir(hModule, wszPathDest);
444 if (rc != ERROR_SUCCESS)
445 {
446 /* Check for hidden .custom directory and remove it. */
447 swprintf_s(wszPathDest, RT_ELEMENTS(wszPathDest), L"%s.custom", wszPathTargetDir);
448 rc = RemoveDir(hModule, wszPathDest);
449 }
450 }
451
452 logStringW(hModule, L"UninstallBranding: Handling done. (rc=%u (ignored))", rc);
453 return ERROR_SUCCESS; /* Do not fail here. */
454}
455
456UINT __stdcall InstallBranding(MSIHANDLE hModule)
457{
458 UINT rc;
459 logStringW(hModule, L"InstallBranding: Handling branding file ...");
460
461 WCHAR wszPathMSI[_MAX_PATH];
462 rc = VBoxGetProperty(hModule, L"SOURCEDIR", wszPathMSI, sizeof(wszPathMSI));
463 if (rc == ERROR_SUCCESS)
464 {
465 WCHAR wszPathTargetDir[_MAX_PATH];
466 rc = VBoxGetProperty(hModule, L"CustomActionData", wszPathTargetDir, sizeof(wszPathTargetDir));
467 if (rc == ERROR_SUCCESS)
468 {
469 /** @todo Check for trailing slash after %s. */
470 WCHAR wszPathDest[_MAX_PATH];
471 swprintf_s(wszPathDest, RT_ELEMENTS(wszPathDest), L"%s", wszPathTargetDir);
472
473 WCHAR wszPathSource[_MAX_PATH];
474 swprintf_s(wszPathSource, RT_ELEMENTS(wszPathSource), L"%s.custom", wszPathMSI);
475 rc = CopyDir(hModule, wszPathDest, wszPathSource);
476 if (rc == ERROR_SUCCESS)
477 {
478 swprintf_s(wszPathDest, RT_ELEMENTS(wszPathDest), L"%scustom", wszPathTargetDir);
479 swprintf_s(wszPathSource, RT_ELEMENTS(wszPathSource), L"%s.custom", wszPathTargetDir);
480 rc = RenameDir(hModule, wszPathDest, wszPathSource);
481 }
482 }
483 }
484
485 logStringW(hModule, L"InstallBranding: Handling done. (rc=%u (ignored))", rc);
486 return ERROR_SUCCESS; /* Do not fail here. */
487}
488
489#ifdef VBOX_WITH_NETFLT
490
491/** @todo should use some real VBox app name */
492#define VBOX_NETCFG_APP_NAME L"VirtualBox Installer"
493#define VBOX_NETCFG_MAX_RETRIES 10
494#define NETFLT_PT_INF_REL_PATH L"VBoxNetFlt.inf"
495#define NETFLT_MP_INF_REL_PATH L"VBoxNetFltM.inf"
496#define NETFLT_ID L"sun_VBoxNetFlt" /** @todo Needs to be changed (?). */
497#define NETADP_ID L"sun_VBoxNetAdp" /** @todo Needs to be changed (?). */
498
499#define NETLWF_INF_NAME L"VBoxNetLwf.inf"
500#define NETLWF_ID L"sun_VBoxNetLwf" /** @todo Needs to be changed (?). */
501#define NETADP6_ID L"sun_VBoxNetAdp6" /** @todo Needs to be changed (?). */
502
503static MSIHANDLE g_hCurrentModule = NULL;
504
505static VOID netCfgLoggerCallback(LPCSTR szString)
506{
507 if (g_hCurrentModule)
508 logString(g_hCurrentModule, szString);
509}
510
511static VOID netCfgLoggerDisable()
512{
513 if (g_hCurrentModule)
514 {
515 VBoxNetCfgWinSetLogging((LOG_ROUTINE)NULL);
516 g_hCurrentModule = NULL;
517 }
518}
519
520static VOID netCfgLoggerEnable(MSIHANDLE hModule)
521{
522 NonStandardAssert(hModule);
523
524 if (g_hCurrentModule)
525 netCfgLoggerDisable();
526
527 g_hCurrentModule = hModule;
528
529 VBoxNetCfgWinSetLogging((LOG_ROUTINE)netCfgLoggerCallback);
530}
531
532static UINT errorConvertFromHResult(MSIHANDLE hModule, HRESULT hr)
533{
534 UINT uRet;
535 switch (hr)
536 {
537 case S_OK:
538 uRet = ERROR_SUCCESS;
539 break;
540
541 case NETCFG_S_REBOOT:
542 {
543 logStringW(hModule, L"Reboot required, setting REBOOT property to \"force\"");
544 HRESULT hr2 = MsiSetPropertyW(hModule, L"REBOOT", L"Force");
545 if (hr2 != ERROR_SUCCESS)
546 logStringW(hModule, L"Failed to set REBOOT property, error = 0x%x", hr2);
547 uRet = ERROR_SUCCESS; /* Never fail here. */
548 break;
549 }
550
551 default:
552 logStringW(hModule, L"Converting unhandled HRESULT (0x%x) to ERROR_GEN_FAILURE", hr);
553 uRet = ERROR_GEN_FAILURE;
554 }
555 return uRet;
556}
557
558static MSIHANDLE createNetCfgLockedMsgRecord(MSIHANDLE hModule)
559{
560 MSIHANDLE hRecord = MsiCreateRecord(2);
561 if (hRecord)
562 {
563 UINT uErr = MsiRecordSetInteger(hRecord, 1, 25001);
564 if (uErr != ERROR_SUCCESS)
565 {
566 logStringW(hModule, L"createNetCfgLockedMsgRecord: MsiRecordSetInteger failed, error = 0x%x", uErr);
567 MsiCloseHandle(hRecord);
568 hRecord = NULL;
569 }
570 }
571 else
572 logStringW(hModule, L"createNetCfgLockedMsgRecord: Failed to create a record");
573
574 return hRecord;
575}
576
577static UINT doNetCfgInit(MSIHANDLE hModule, INetCfg **ppnc, BOOL bWrite)
578{
579 MSIHANDLE hMsg = NULL;
580 UINT uErr = ERROR_GEN_FAILURE;
581 int MsgResult;
582 int cRetries = 0;
583
584 do
585 {
586 LPWSTR lpszLockedBy;
587 HRESULT hr = VBoxNetCfgWinQueryINetCfg(ppnc, bWrite, VBOX_NETCFG_APP_NAME, 10000, &lpszLockedBy);
588 if (hr != NETCFG_E_NO_WRITE_LOCK)
589 {
590 if (FAILED(hr))
591 logStringW(hModule, L"doNetCfgInit: VBoxNetCfgWinQueryINetCfg failed, error = 0x%x", hr);
592 uErr = errorConvertFromHResult(hModule, hr);
593 break;
594 }
595
596 /* hr == NETCFG_E_NO_WRITE_LOCK */
597
598 if (!lpszLockedBy)
599 {
600 logStringW(hModule, L"doNetCfgInit: lpszLockedBy == NULL, breaking");
601 break;
602 }
603
604 /* on vista the 6to4svc.dll periodically maintains the lock for some reason,
605 * if this is the case, increase the wait period by retrying multiple times
606 * NOTE: we could alternatively increase the wait timeout,
607 * however it seems unneeded for most cases, e.g. in case some network connection property
608 * dialog is opened, it would be better to post a notification to the user as soon as possible
609 * rather than waiting for a longer period of time before displaying it */
610 if ( cRetries < VBOX_NETCFG_MAX_RETRIES
611 && !wcscmp(lpszLockedBy, L"6to4svc.dll"))
612 {
613 cRetries++;
614 logStringW(hModule, L"doNetCfgInit: lpszLockedBy is 6to4svc.dll, retrying %d out of %d", cRetries, VBOX_NETCFG_MAX_RETRIES);
615 MsgResult = IDRETRY;
616 }
617 else
618 {
619 if (!hMsg)
620 {
621 hMsg = createNetCfgLockedMsgRecord(hModule);
622 if (!hMsg)
623 {
624 logStringW(hModule, L"doNetCfgInit: Failed to create a message record, breaking");
625 CoTaskMemFree(lpszLockedBy);
626 break;
627 }
628 }
629
630 UINT rTmp = MsiRecordSetStringW(hMsg, 2, lpszLockedBy);
631 NonStandardAssert(rTmp == ERROR_SUCCESS);
632 if (rTmp != ERROR_SUCCESS)
633 {
634 logStringW(hModule, L"doNetCfgInit: MsiRecordSetStringW failed, error = 0x%x", rTmp);
635 CoTaskMemFree(lpszLockedBy);
636 break;
637 }
638
639 MsgResult = MsiProcessMessage(hModule, (INSTALLMESSAGE)(INSTALLMESSAGE_USER | MB_RETRYCANCEL), hMsg);
640 NonStandardAssert(MsgResult == IDRETRY || MsgResult == IDCANCEL);
641 logStringW(hModule, L"doNetCfgInit: MsiProcessMessage returned (0x%x)", MsgResult);
642 }
643 CoTaskMemFree(lpszLockedBy);
644 } while(MsgResult == IDRETRY);
645
646 if (hMsg)
647 MsiCloseHandle(hMsg);
648
649 return uErr;
650}
651
652static UINT vboxNetFltQueryInfArray(MSIHANDLE hModule, OUT LPWSTR pwszPtInf, OUT LPWSTR pwszMpInf, DWORD dwSize)
653{
654 DWORD dwBuf = dwSize - RT_MAX(sizeof(NETFLT_PT_INF_REL_PATH), sizeof(NETFLT_MP_INF_REL_PATH));
655 UINT uErr = MsiGetPropertyW(hModule, L"CustomActionData", pwszPtInf, &dwBuf);
656 if ( uErr == ERROR_SUCCESS
657 && dwBuf)
658 {
659 wcscpy(pwszMpInf, pwszPtInf);
660
661 wcsncat(pwszPtInf, NETFLT_PT_INF_REL_PATH, sizeof(NETFLT_PT_INF_REL_PATH));
662 logStringW(hModule, L"vboxNetFltQueryInfArray: INF 1: %s", pwszPtInf);
663
664 wcsncat(pwszMpInf, NETFLT_MP_INF_REL_PATH, sizeof(NETFLT_MP_INF_REL_PATH));
665 logStringW(hModule, L"vboxNetFltQueryInfArray: INF 2: %s", pwszMpInf);
666 }
667 else if (uErr != ERROR_SUCCESS)
668 logStringW(hModule, L"vboxNetFltQueryInfArray: MsiGetPropertyW failed, error = 0x%x", uErr);
669 else
670 {
671 logStringW(hModule, L"vboxNetFltQueryInfArray: Empty installation directory");
672 uErr = ERROR_GEN_FAILURE;
673 }
674
675 return uErr;
676}
677
678#endif /*VBOX_WITH_NETFLT*/
679
680UINT __stdcall UninstallNetFlt(MSIHANDLE hModule)
681{
682#ifdef VBOX_WITH_NETFLT
683 INetCfg *pNetCfg;
684 UINT uErr;
685
686 netCfgLoggerEnable(hModule);
687
688 BOOL bOldIntMode = SetupSetNonInteractiveMode(FALSE);
689
690 __try
691 {
692 logStringW(hModule, L"Uninstalling NetFlt");
693
694 uErr = doNetCfgInit(hModule, &pNetCfg, TRUE);
695 if (uErr == ERROR_SUCCESS)
696 {
697 HRESULT hr = VBoxNetCfgWinNetFltUninstall(pNetCfg);
698 if (hr != S_OK)
699 logStringW(hModule, L"UninstallNetFlt: VBoxNetCfgWinUninstallComponent failed, error = 0x%x", hr);
700
701 uErr = errorConvertFromHResult(hModule, hr);
702
703 VBoxNetCfgWinReleaseINetCfg(pNetCfg, TRUE);
704
705 logStringW(hModule, L"Uninstalling NetFlt done, error = 0x%x", uErr);
706 }
707 else
708 logStringW(hModule, L"UninstallNetFlt: doNetCfgInit failed, error = 0x%x", uErr);
709 }
710 __finally
711 {
712 if (bOldIntMode)
713 {
714 /* The prev mode != FALSE, i.e. non-interactive. */
715 SetupSetNonInteractiveMode(bOldIntMode);
716 }
717 netCfgLoggerDisable();
718 }
719#endif /* VBOX_WITH_NETFLT */
720
721 /* Never fail the install even if we did not succeed. */
722 return ERROR_SUCCESS;
723}
724
725UINT __stdcall InstallNetFlt(MSIHANDLE hModule)
726{
727#ifdef VBOX_WITH_NETFLT
728 UINT uErr;
729 INetCfg *pNetCfg;
730
731 netCfgLoggerEnable(hModule);
732
733 BOOL bOldIntMode = SetupSetNonInteractiveMode(FALSE);
734
735 __try
736 {
737
738 logStringW(hModule, L"InstallNetFlt: Installing NetFlt");
739
740 uErr = doNetCfgInit(hModule, &pNetCfg, TRUE);
741 if (uErr == ERROR_SUCCESS)
742 {
743 WCHAR wszPtInf[MAX_PATH];
744 WCHAR wszMpInf[MAX_PATH];
745 uErr = vboxNetFltQueryInfArray(hModule, wszPtInf, wszMpInf, sizeof(wszMpInf));
746 if (uErr == ERROR_SUCCESS)
747 {
748 LPCWSTR const apwszInfs[] = { wszPtInf, wszMpInf };
749 HRESULT hr = VBoxNetCfgWinNetFltInstall(pNetCfg, &apwszInfs[0], RT_ELEMENTS(apwszInfs));
750 if (FAILED(hr))
751 logStringW(hModule, L"InstallNetFlt: VBoxNetCfgWinNetFltInstall failed, error = 0x%x", hr);
752
753 uErr = errorConvertFromHResult(hModule, hr);
754 }
755 else
756 logStringW(hModule, L"InstallNetFlt: vboxNetFltQueryInfArray failed, error = 0x%x", uErr);
757
758 VBoxNetCfgWinReleaseINetCfg(pNetCfg, TRUE);
759
760 logStringW(hModule, L"InstallNetFlt: Done");
761 }
762 else
763 logStringW(hModule, L"InstallNetFlt: doNetCfgInit failed, error = 0x%x", uErr);
764 }
765 __finally
766 {
767 if (bOldIntMode)
768 {
769 /* The prev mode != FALSE, i.e. non-interactive. */
770 SetupSetNonInteractiveMode(bOldIntMode);
771 }
772 netCfgLoggerDisable();
773 }
774#endif /* VBOX_WITH_NETFLT */
775
776 /* Never fail the install even if we did not succeed. */
777 return ERROR_SUCCESS;
778}
779
780
781UINT __stdcall UninstallNetLwf(MSIHANDLE hModule)
782{
783#ifdef VBOX_WITH_NETFLT
784 INetCfg *pNetCfg;
785 UINT uErr;
786
787 netCfgLoggerEnable(hModule);
788
789 BOOL bOldIntMode = SetupSetNonInteractiveMode(FALSE);
790
791 __try
792 {
793 logStringW(hModule, L"Uninstalling NetLwf");
794
795 uErr = doNetCfgInit(hModule, &pNetCfg, TRUE);
796 if (uErr == ERROR_SUCCESS)
797 {
798 HRESULT hr = VBoxNetCfgWinNetLwfUninstall(pNetCfg);
799 if (hr != S_OK)
800 logStringW(hModule, L"UninstallNetLwf: VBoxNetCfgWinUninstallComponent failed, error = 0x%x", hr);
801
802 uErr = errorConvertFromHResult(hModule, hr);
803
804 VBoxNetCfgWinReleaseINetCfg(pNetCfg, TRUE);
805
806 logStringW(hModule, L"Uninstalling NetLwf done, error = 0x%x", uErr);
807 }
808 else
809 logStringW(hModule, L"UninstallNetLwf: doNetCfgInit failed, error = 0x%x", uErr);
810 }
811 __finally
812 {
813 if (bOldIntMode)
814 {
815 /* The prev mode != FALSE, i.e. non-interactive. */
816 SetupSetNonInteractiveMode(bOldIntMode);
817 }
818 netCfgLoggerDisable();
819 }
820#endif /* VBOX_WITH_NETFLT */
821
822 /* Never fail the install even if we did not succeed. */
823 return ERROR_SUCCESS;
824}
825
826UINT __stdcall InstallNetLwf(MSIHANDLE hModule)
827{
828#ifdef VBOX_WITH_NETFLT
829 UINT uErr;
830 INetCfg *pNetCfg;
831
832 netCfgLoggerEnable(hModule);
833
834 BOOL bOldIntMode = SetupSetNonInteractiveMode(FALSE);
835
836 __try
837 {
838
839 logStringW(hModule, L"InstallNetLwf: Installing NetLwf");
840
841 uErr = doNetCfgInit(hModule, &pNetCfg, TRUE);
842 if (uErr == ERROR_SUCCESS)
843 {
844 WCHAR wszInfName[] = L"VBoxNetLwf.inf";
845 WCHAR wszInf[MAX_PATH];
846 DWORD cchInf = RT_ELEMENTS(wszInf) - sizeof(NETLWF_INF_NAME) - 1;
847 UINT uErr = MsiGetPropertyW(hModule, L"CustomActionData", wszInf, &cchInf);
848 if (uErr == ERROR_SUCCESS)
849 {
850 if (cchInf)
851 {
852 if (wszInf[cchInf - 1] != L'\\')
853 {
854 wszInf[cchInf++] = L'\\';
855 wszInf[cchInf] = L'\0';
856 }
857
858 wcscat(wszInf, NETLWF_INF_NAME);
859
860 HRESULT hr = VBoxNetCfgWinNetLwfInstall(pNetCfg, wszInf);
861 if (FAILED(hr))
862 logStringW(hModule, L"InstallNetLwf: VBoxNetCfgWinNetLwfInstall failed, error = 0x%x", hr);
863
864 uErr = errorConvertFromHResult(hModule, hr);
865 }
866 else
867 {
868 logStringW(hModule, L"vboxNetFltQueryInfArray: Empty installation directory");
869 uErr = ERROR_GEN_FAILURE;
870 }
871 }
872 else
873 logStringW(hModule, L"vboxNetFltQueryInfArray: MsiGetPropertyW failed, error = 0x%x", uErr);
874
875 VBoxNetCfgWinReleaseINetCfg(pNetCfg, TRUE);
876
877 logStringW(hModule, L"InstallNetLwf: Done");
878 }
879 else
880 logStringW(hModule, L"InstallNetLwf: doNetCfgInit failed, error = 0x%x", uErr);
881 }
882 __finally
883 {
884 if (bOldIntMode)
885 {
886 /* The prev mode != FALSE, i.e. non-interactive. */
887 SetupSetNonInteractiveMode(bOldIntMode);
888 }
889 netCfgLoggerDisable();
890 }
891#endif /* VBOX_WITH_NETFLT */
892
893 /* Never fail the install even if we did not succeed. */
894 return ERROR_SUCCESS;
895}
896
897
898#if 0
899static BOOL RenameHostOnlyConnectionsCallback(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDev, PVOID pContext)
900{
901 WCHAR DevName[256];
902 DWORD winEr;
903
904 if (SetupDiGetDeviceRegistryPropertyW(hDevInfo, pDev,
905 SPDRP_FRIENDLYNAME , /* IN DWORD Property,*/
906 NULL, /*OUT PDWORD PropertyRegDataType, OPTIONAL*/
907 (PBYTE)DevName, /*OUT PBYTE PropertyBuffer,*/
908 sizeof(DevName), /* IN DWORD PropertyBufferSize,*/
909 NULL /*OUT PDWORD RequiredSize OPTIONAL*/
910 ))
911 {
912 HKEY hKey = SetupDiOpenDevRegKey(hDevInfo, pDev,
913 DICS_FLAG_GLOBAL, /* IN DWORD Scope,*/
914 0, /*IN DWORD HwProfile, */
915 DIREG_DRV, /* IN DWORD KeyType, */
916 KEY_READ /*IN REGSAM samDesired*/
917 );
918 NonStandardAssert(hKey != INVALID_HANDLE_VALUE);
919 if (hKey != INVALID_HANDLE_VALUE)
920 {
921 WCHAR guid[50];
922 DWORD cbGuid=sizeof(guid);
923 winEr = RegQueryValueExW(hKey,
924 L"NetCfgInstanceId", /*__in_opt LPCTSTR lpValueName,*/
925 NULL, /*__reserved LPDWORD lpReserved,*/
926 NULL, /*__out_opt LPDWORD lpType,*/
927 (LPBYTE)guid, /*__out_opt LPBYTE lpData,*/
928 &cbGuid /*guid__inout_opt LPDWORD lpcbData*/
929 );
930 NonStandardAssert(winEr == ERROR_SUCCESS);
931 if (winEr == ERROR_SUCCESS)
932 {
933 WCHAR ConnectoinName[128];
934 ULONG cbName = sizeof(ConnectoinName);
935
936 HRESULT hr = VBoxNetCfgWinGenHostonlyConnectionName (DevName, ConnectoinName, &cbName);
937 NonStandardAssert(hr == S_OK);
938 if (SUCCEEDED(hr))
939 {
940 hr = VBoxNetCfgWinRenameConnection(guid, ConnectoinName);
941 NonStandardAssert(hr == S_OK);
942 }
943 }
944 }
945 RegCloseKey(hKey);
946 }
947 else
948 {
949 NonStandardAssert(0);
950 }
951
952 return TRUE;
953}
954#endif
955
956static UINT _createHostOnlyInterface(MSIHANDLE hModule, LPCWSTR pwszId, LPCWSTR pwszInfName)
957{
958#ifdef VBOX_WITH_NETFLT
959 netCfgLoggerEnable(hModule);
960
961 BOOL bSetupModeInteractive = SetupSetNonInteractiveMode(FALSE);
962 bool bSetStaticIp = true;
963
964 logStringW(hModule, L"CreateHostOnlyInterface: Creating host-only interface");
965
966 HRESULT hr;
967
968 GUID guid;
969 WCHAR wszMpInf[MAX_PATH];
970 DWORD cchMpInf = RT_ELEMENTS(wszMpInf) - wcslen(pwszInfName) - 1 - 1;
971 LPCWSTR pwszInfPath = NULL;
972 bool bIsFile = false;
973 UINT uErr = MsiGetPropertyW(hModule, L"CustomActionData", wszMpInf, &cchMpInf);
974 if (uErr == ERROR_SUCCESS)
975 {
976 if (cchMpInf)
977 {
978 logStringW(hModule, L"CreateHostOnlyInterface: NetAdpDir property = %s", wszMpInf);
979 if (wszMpInf[cchMpInf - 1] != L'\\')
980 {
981 wszMpInf[cchMpInf++] = L'\\';
982 wszMpInf[cchMpInf] = L'\0';
983 }
984
985 wcscat(wszMpInf, pwszInfName);
986 pwszInfPath = wszMpInf;
987 bIsFile = true;
988
989 logStringW(hModule, L"CreateHostOnlyInterface: Resulting INF path = %s", pwszInfPath);
990 }
991 else
992 logStringW(hModule, L"CreateHostOnlyInterface: VBox installation path is empty");
993 }
994 else
995 logStringW(hModule, L"CreateHostOnlyInterface: Unable to retrieve VBox installation path, error = 0x%x", uErr);
996
997 /* Make sure the inf file is installed. */
998 if (pwszInfPath != NULL && bIsFile)
999 {
1000 logStringW(hModule, L"CreateHostOnlyInterface: Calling VBoxDrvCfgInfInstall(%s)", pwszInfPath);
1001 hr = VBoxDrvCfgInfInstall(pwszInfPath);
1002 logStringW(hModule, L"CreateHostOnlyInterface: VBoxDrvCfgInfInstall returns 0x%x", hr);
1003 if (FAILED(hr))
1004 logStringW(hModule, L"CreateHostOnlyInterface: Failed to install INF file, error = 0x%x", hr);
1005 }
1006
1007 if (SUCCEEDED(hr))
1008 {
1009 //first, try to update Host Only Network Interface
1010 BOOL fRebootRequired = FALSE;
1011 hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(pwszInfPath, &fRebootRequired, pwszId);
1012 if (SUCCEEDED(hr))
1013 {
1014 if (fRebootRequired)
1015 {
1016 logStringW(hModule, L"UpdateHostOnlyInterfaces: Reboot required, setting REBOOT property to force");
1017 HRESULT hr2 = MsiSetPropertyW(hModule, L"REBOOT", L"Force");
1018 if (hr2 != ERROR_SUCCESS)
1019 logStringW(hModule, L"UpdateHostOnlyInterfaces: Failed to set REBOOT property, error = 0x%x", hr2);
1020 }
1021 }
1022 else
1023 logStringW(hModule, L"UpdateHostOnlyInterfaces: VBoxNetCfgWinUpdateHostOnlyNetworkInterface failed, hr = 0x%x", hr);
1024 //in fail case call CreateHostOnlyInterface
1025 if (FAILED(hr))
1026 {
1027 logStringW(hModule, L"CreateHostOnlyInterface: calling VBoxNetCfgWinCreateHostOnlyNetworkInterface");
1028 hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface(pwszInfPath, bIsFile, &guid, NULL, NULL);
1029 logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinCreateHostOnlyNetworkInterface returns 0x%x", hr);
1030 if (SUCCEEDED(hr))
1031 {
1032 ULONG ip = inet_addr("192.168.56.1");
1033 ULONG mask = inet_addr("255.255.255.0");
1034 logStringW(hModule, L"CreateHostOnlyInterface: calling VBoxNetCfgWinEnableStaticIpConfig");
1035 hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask);
1036 logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinEnableStaticIpConfig returns 0x%x", hr);
1037 if (FAILED(hr))
1038 logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinEnableStaticIpConfig failed, error = 0x%x", hr);
1039 }
1040 else
1041 logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinCreateHostOnlyNetworkInterface failed, error = 0x%x", hr);
1042 }
1043 }
1044
1045 if (SUCCEEDED(hr))
1046 logStringW(hModule, L"CreateHostOnlyInterface: Creating host-only interface done");
1047
1048 /* Restore original setup mode. */
1049 logStringW(hModule, L"CreateHostOnlyInterface: Almost done...");
1050 if (bSetupModeInteractive)
1051 SetupSetNonInteractiveMode(bSetupModeInteractive);
1052
1053 netCfgLoggerDisable();
1054
1055#endif /* VBOX_WITH_NETFLT */
1056
1057 logStringW(hModule, L"CreateHostOnlyInterface: Returns success (ignoring all failures)");
1058 /* Never fail the install even if we did not succeed. */
1059 return ERROR_SUCCESS;
1060}
1061
1062UINT __stdcall CreateHostOnlyInterface(MSIHANDLE hModule)
1063{
1064 return _createHostOnlyInterface(hModule, NETADP_ID, L"VBoxNetAdp.inf");
1065}
1066
1067UINT __stdcall Ndis6CreateHostOnlyInterface(MSIHANDLE hModule)
1068{
1069 return _createHostOnlyInterface(hModule, NETADP6_ID, L"VBoxNetAdp6.inf");
1070}
1071
1072
1073static UINT _removeHostOnlyInterfaces(MSIHANDLE hModule, LPCWSTR pwszId)
1074{
1075#ifdef VBOX_WITH_NETFLT
1076 netCfgLoggerEnable(hModule);
1077
1078 logStringW(hModule, L"RemoveHostOnlyInterfaces: Removing all host-only interfaces");
1079
1080 BOOL bSetupModeInteractive = SetupSetNonInteractiveMode(FALSE);
1081
1082 HRESULT hr = VBoxNetCfgWinRemoveAllNetDevicesOfId(pwszId);
1083 if (SUCCEEDED(hr))
1084 {
1085 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, pwszId, L"Net", 0/* could be SUOI_FORCEDELETE */);
1086 if (FAILED(hr))
1087 {
1088 logStringW(hModule, L"RemoveHostOnlyInterfaces: NetAdp uninstalled successfully, but failed to remove INF files");
1089 }
1090 }
1091 else
1092 logStringW(hModule, L"RemoveHostOnlyInterfaces: NetAdp uninstall failed, hr = 0x%x", hr);
1093
1094 /* Restore original setup mode. */
1095 if (bSetupModeInteractive)
1096 SetupSetNonInteractiveMode(bSetupModeInteractive);
1097
1098 netCfgLoggerDisable();
1099#endif /* VBOX_WITH_NETFLT */
1100
1101 /* Never fail the install even if we did not succeed. */
1102 return ERROR_SUCCESS;
1103}
1104
1105UINT __stdcall RemoveHostOnlyInterfaces(MSIHANDLE hModule)
1106{
1107 return _removeHostOnlyInterfaces(hModule, NETADP_ID);
1108}
1109
1110UINT __stdcall Ndis6RemoveHostOnlyInterfaces(MSIHANDLE hModule)
1111{
1112 return _removeHostOnlyInterfaces(hModule, NETADP6_ID);
1113}
1114
1115
1116
1117static UINT _stopHostOnlyInterfaces(MSIHANDLE hModule, LPCWSTR pwszId)
1118{
1119#ifdef VBOX_WITH_NETFLT
1120 netCfgLoggerEnable(hModule);
1121
1122 logStringW(hModule, L"StopHostOnlyInterfaces: Stopping all host-only interfaces");
1123
1124 BOOL bSetupModeInteractive = SetupSetNonInteractiveMode(FALSE);
1125
1126 HRESULT hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(pwszId, VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE);
1127 if (SUCCEEDED(hr))
1128 {
1129 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, pwszId, L"Net", 0/* could be SUOI_FORCEDELETE */);
1130 if (FAILED(hr))
1131 logStringW(hModule, L"StopHostOnlyInterfaces: VBoxDrvCfgInfUninstallAllSetupDi failed, hr = 0x%x", hr);
1132 }
1133 else
1134 logStringW(hModule, L"StopHostOnlyInterfaces: Disabling host interfaces failed, hr = 0x%x", hr);
1135
1136 /* Restore original setup mode. */
1137 if (bSetupModeInteractive)
1138 SetupSetNonInteractiveMode(bSetupModeInteractive);
1139
1140 netCfgLoggerDisable();
1141#endif /* VBOX_WITH_NETFLT */
1142
1143 /* Never fail the install even if we did not succeed. */
1144 return ERROR_SUCCESS;
1145}
1146
1147UINT __stdcall StopHostOnlyInterfaces(MSIHANDLE hModule)
1148{
1149 return _stopHostOnlyInterfaces(hModule, NETADP_ID);
1150}
1151
1152UINT __stdcall Ndis6StopHostOnlyInterfaces(MSIHANDLE hModule)
1153{
1154 return _stopHostOnlyInterfaces(hModule, NETADP6_ID);
1155}
1156
1157static UINT _updateHostOnlyInterfaces(MSIHANDLE hModule, LPCWSTR pwszInfName, LPCWSTR pwszId)
1158{
1159#ifdef VBOX_WITH_NETFLT
1160 netCfgLoggerEnable(hModule);
1161
1162 logStringW(hModule, L"UpdateHostOnlyInterfaces: Updating all host-only interfaces");
1163
1164 BOOL bSetupModeInteractive = SetupSetNonInteractiveMode(FALSE);
1165
1166 WCHAR wszMpInf[MAX_PATH];
1167 DWORD cchMpInf = RT_ELEMENTS(wszMpInf) - wcslen(pwszInfName) - 1 - 1;
1168 LPCWSTR pwszInfPath = NULL;
1169 bool bIsFile = false;
1170 UINT uErr = MsiGetPropertyW(hModule, L"CustomActionData", wszMpInf, &cchMpInf);
1171 if (uErr == ERROR_SUCCESS)
1172 {
1173 if (cchMpInf)
1174 {
1175 logStringW(hModule, L"UpdateHostOnlyInterfaces: NetAdpDir property = %s", wszMpInf);
1176 if (wszMpInf[cchMpInf - 1] != L'\\')
1177 {
1178 wszMpInf[cchMpInf++] = L'\\';
1179 wszMpInf[cchMpInf] = L'\0';
1180 }
1181
1182 wcscat(wszMpInf, pwszInfName);
1183 pwszInfPath = wszMpInf;
1184 bIsFile = true;
1185
1186 logStringW(hModule, L"UpdateHostOnlyInterfaces: Resulting INF path = %s", pwszInfPath);
1187
1188 DWORD attrFile = GetFileAttributesW(pwszInfPath);
1189 if (attrFile == INVALID_FILE_ATTRIBUTES)
1190 {
1191 DWORD dwErr = GetLastError();
1192 logStringW(hModule, L"UpdateHostOnlyInterfaces: File \"%s\" not found, dwErr=%ld",
1193 pwszInfPath, dwErr);
1194 }
1195 else
1196 {
1197 logStringW(hModule, L"UpdateHostOnlyInterfaces: File \"%s\" exists",
1198 pwszInfPath);
1199
1200 BOOL fRebootRequired = FALSE;
1201 HRESULT hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(pwszInfPath, &fRebootRequired, pwszId);
1202 if (SUCCEEDED(hr))
1203 {
1204 if (fRebootRequired)
1205 {
1206 logStringW(hModule, L"UpdateHostOnlyInterfaces: Reboot required, setting REBOOT property to force");
1207 HRESULT hr2 = MsiSetPropertyW(hModule, L"REBOOT", L"Force");
1208 if (hr2 != ERROR_SUCCESS)
1209 logStringW(hModule, L"UpdateHostOnlyInterfaces: Failed to set REBOOT property, error = 0x%x", hr2);
1210 }
1211 }
1212 else
1213 logStringW(hModule, L"UpdateHostOnlyInterfaces: VBoxNetCfgWinUpdateHostOnlyNetworkInterface failed, hr = 0x%x", hr);
1214 }
1215 }
1216 else
1217 logStringW(hModule, L"UpdateHostOnlyInterfaces: VBox installation path is empty");
1218 }
1219 else
1220 logStringW(hModule, L"UpdateHostOnlyInterfaces: Unable to retrieve VBox installation path, error = 0x%x", uErr);
1221
1222 /* Restore original setup mode. */
1223 if (bSetupModeInteractive)
1224 SetupSetNonInteractiveMode(bSetupModeInteractive);
1225
1226 netCfgLoggerDisable();
1227#endif /* VBOX_WITH_NETFLT */
1228
1229 /* Never fail the install even if we did not succeed. */
1230 return ERROR_SUCCESS;
1231}
1232
1233UINT __stdcall UpdateHostOnlyInterfaces(MSIHANDLE hModule)
1234{
1235 return _updateHostOnlyInterfaces(hModule, L"VBoxNetAdp.inf", NETADP_ID);
1236}
1237
1238UINT __stdcall Ndis6UpdateHostOnlyInterfaces(MSIHANDLE hModule)
1239{
1240 return _updateHostOnlyInterfaces(hModule, L"VBoxNetAdp6.inf", NETADP6_ID);
1241}
1242
1243static bool isTAPDevice(const WCHAR *pwszGUID)
1244{
1245 HKEY hNetcard;
1246 bool bIsTapDevice = false;
1247 LONG lStatus = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
1248 L"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",
1249 0, KEY_READ, &hNetcard);
1250 if (lStatus != ERROR_SUCCESS)
1251 return false;
1252
1253 int i = 0;
1254 for (;;)
1255 {
1256 WCHAR wszEnumName[256];
1257 WCHAR wszNetCfgInstanceId[256];
1258 DWORD dwKeyType;
1259 HKEY hNetCardGUID;
1260
1261 DWORD dwLen = sizeof(wszEnumName);
1262 lStatus = RegEnumKeyExW(hNetcard, i, wszEnumName, &dwLen, NULL, NULL, NULL, NULL);
1263 if (lStatus != ERROR_SUCCESS)
1264 break;
1265
1266 lStatus = RegOpenKeyExW(hNetcard, wszEnumName, 0, KEY_READ, &hNetCardGUID);
1267 if (lStatus == ERROR_SUCCESS)
1268 {
1269 dwLen = sizeof(wszNetCfgInstanceId);
1270 lStatus = RegQueryValueExW(hNetCardGUID, L"NetCfgInstanceId", NULL, &dwKeyType, (LPBYTE)wszNetCfgInstanceId, &dwLen);
1271 if ( lStatus == ERROR_SUCCESS
1272 && dwKeyType == REG_SZ)
1273 {
1274 WCHAR wszNetProductName[256];
1275 WCHAR wszNetProviderName[256];
1276
1277 wszNetProductName[0] = 0;
1278 dwLen = sizeof(wszNetProductName);
1279 lStatus = RegQueryValueExW(hNetCardGUID, L"ProductName", NULL, &dwKeyType, (LPBYTE)wszNetProductName, &dwLen);
1280
1281 wszNetProviderName[0] = 0;
1282 dwLen = sizeof(wszNetProviderName);
1283 lStatus = RegQueryValueExW(hNetCardGUID, L"ProviderName", NULL, &dwKeyType, (LPBYTE)wszNetProviderName, &dwLen);
1284
1285 if ( !wcscmp(wszNetCfgInstanceId, pwszGUID)
1286 && !wcscmp(wszNetProductName, L"VirtualBox TAP Adapter")
1287 && ( (!wcscmp(wszNetProviderName, L"innotek GmbH")) /* Legacy stuff. */
1288 || (!wcscmp(wszNetProviderName, L"Sun Microsystems, Inc.")) /* Legacy stuff. */
1289 || (!wcscmp(wszNetProviderName, MY_WTEXT(VBOX_VENDOR))) /* Reflects current vendor string. */
1290 )
1291 )
1292 {
1293 bIsTapDevice = true;
1294 RegCloseKey(hNetCardGUID);
1295 break;
1296 }
1297 }
1298 RegCloseKey(hNetCardGUID);
1299 }
1300 ++i;
1301 }
1302
1303 RegCloseKey(hNetcard);
1304 return bIsTapDevice;
1305}
1306
1307#define SetErrBreak(strAndArgs) \
1308 if (1) { \
1309 rc = 0; \
1310 logStringW(hModule, strAndArgs); \
1311 break; \
1312 } else do {} while (0)
1313
1314int removeNetworkInterface(MSIHANDLE hModule, const WCHAR *pwszGUID)
1315{
1316 int rc = 1;
1317 do
1318 {
1319 WCHAR wszPnPInstanceId[512] = {0};
1320
1321 /* We have to find the device instance ID through a registry search */
1322
1323 HKEY hkeyNetwork = 0;
1324 HKEY hkeyConnection = 0;
1325
1326 do /* break-loop */
1327 {
1328 WCHAR wszRegLocation[256];
1329 swprintf(wszRegLocation,
1330 L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s",
1331 pwszGUID);
1332 LONG lStatus = RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRegLocation, 0, KEY_READ, &hkeyNetwork);
1333 if ((lStatus != ERROR_SUCCESS) || !hkeyNetwork)
1334 SetErrBreak((L"VBox HostInterfaces: Host interface network was not found in registry (%s)! [1]", wszRegLocation));
1335
1336 lStatus = RegOpenKeyExW(hkeyNetwork, L"Connection", 0, KEY_READ, &hkeyConnection);
1337 if ((lStatus != ERROR_SUCCESS) || !hkeyConnection)
1338 SetErrBreak((L"VBox HostInterfaces: Host interface network was not found in registry (%s)! [2]", wszRegLocation));
1339
1340 DWORD len = sizeof(wszPnPInstanceId);
1341 DWORD dwKeyType;
1342 lStatus = RegQueryValueExW(hkeyConnection, L"PnPInstanceID", NULL,
1343 &dwKeyType, (LPBYTE)&wszPnPInstanceId[0], &len);
1344 if ((lStatus != ERROR_SUCCESS) || (dwKeyType != REG_SZ))
1345 SetErrBreak((L"VBox HostInterfaces: Host interface network was not found in registry (%s)! [3]", wszRegLocation));
1346 }
1347 while (0);
1348
1349 if (hkeyConnection)
1350 RegCloseKey(hkeyConnection);
1351 if (hkeyNetwork)
1352 RegCloseKey(hkeyNetwork);
1353
1354 /*
1355 * Now we are going to enumerate all network devices and
1356 * wait until we encounter the right device instance ID
1357 */
1358
1359 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
1360 BOOL fResult;
1361
1362 do
1363 {
1364 DWORD ret = 0;
1365 GUID netGuid;
1366 SP_DEVINFO_DATA DeviceInfoData;
1367 DWORD index = 0;
1368 DWORD size = 0;
1369
1370 /* initialize the structure size */
1371 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
1372
1373 /* copy the net class GUID */
1374 memcpy(&netGuid, &GUID_DEVCLASS_NET, sizeof (GUID_DEVCLASS_NET));
1375
1376 /* return a device info set contains all installed devices of the Net class */
1377 hDeviceInfo = SetupDiGetClassDevs(&netGuid, NULL, NULL, DIGCF_PRESENT);
1378 if (hDeviceInfo == INVALID_HANDLE_VALUE)
1379 {
1380 logStringW(hModule, L"VBox HostInterfaces: SetupDiGetClassDevs failed (0x%08X)!", GetLastError());
1381 SetErrBreak(L"VBox HostInterfaces: Uninstallation failed!");
1382 }
1383
1384 BOOL fFoundDevice = FALSE;
1385
1386 /* enumerate the driver info list */
1387 while (TRUE)
1388 {
1389 WCHAR *pwszDeviceHwid;
1390
1391 fResult = SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData);
1392 if (!fResult)
1393 {
1394 if (GetLastError() == ERROR_NO_MORE_ITEMS)
1395 break;
1396 else
1397 {
1398 index++;
1399 continue;
1400 }
1401 }
1402
1403 /* try to get the hardware ID registry property */
1404 fResult = SetupDiGetDeviceRegistryProperty(hDeviceInfo,
1405 &DeviceInfoData,
1406 SPDRP_HARDWAREID,
1407 NULL,
1408 NULL,
1409 0,
1410 &size);
1411 if (!fResult)
1412 {
1413 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
1414 {
1415 index++;
1416 continue;
1417 }
1418
1419 pwszDeviceHwid = (WCHAR *)malloc(size);
1420 if (pwszDeviceHwid)
1421 {
1422 fResult = SetupDiGetDeviceRegistryProperty(hDeviceInfo,
1423 &DeviceInfoData,
1424 SPDRP_HARDWAREID,
1425 NULL,
1426 (PBYTE)pwszDeviceHwid,
1427 size,
1428 NULL);
1429 if (!fResult)
1430 {
1431 free(pwszDeviceHwid);
1432 pwszDeviceHwid = NULL;
1433 index++;
1434 continue;
1435 }
1436 }
1437 }
1438 else
1439 {
1440 /* something is wrong. This shouldn't have worked with a NULL buffer */
1441 index++;
1442 continue;
1443 }
1444
1445 for (WCHAR *t = pwszDeviceHwid;
1446 t && *t && t < &pwszDeviceHwid[size / sizeof(WCHAR)];
1447 t += wcslen(t) + 1)
1448 {
1449 if (!_wcsicmp(L"vboxtap", t))
1450 {
1451 /* get the device instance ID */
1452 WCHAR wszDevID[MAX_DEVICE_ID_LEN];
1453 if (CM_Get_Device_IDW(DeviceInfoData.DevInst,
1454 wszDevID, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS)
1455 {
1456 /* compare to what we determined before */
1457 if (!wcscmp(wszDevID, wszPnPInstanceId))
1458 {
1459 fFoundDevice = TRUE;
1460 break;
1461 }
1462 }
1463 }
1464 }
1465
1466 if (pwszDeviceHwid)
1467 {
1468 free(pwszDeviceHwid);
1469 pwszDeviceHwid = NULL;
1470 }
1471
1472 if (fFoundDevice)
1473 break;
1474
1475 index++;
1476 }
1477
1478 if (fFoundDevice)
1479 {
1480 fResult = SetupDiSetSelectedDevice(hDeviceInfo, &DeviceInfoData);
1481 if (!fResult)
1482 {
1483 logStringW(hModule, L"VBox HostInterfaces: SetupDiSetSelectedDevice failed (0x%08X)!", GetLastError());
1484 SetErrBreak(L"VBox HostInterfaces: Uninstallation failed!");
1485 }
1486
1487 fResult = SetupDiCallClassInstaller(DIF_REMOVE, hDeviceInfo, &DeviceInfoData);
1488 if (!fResult)
1489 {
1490 logStringW(hModule, L"VBox HostInterfaces: SetupDiCallClassInstaller (DIF_REMOVE) failed (0x%08X)!", GetLastError());
1491 SetErrBreak(L"VBox HostInterfaces: Uninstallation failed!");
1492 }
1493 }
1494 else
1495 SetErrBreak(L"VBox HostInterfaces: Host interface network device not found!");
1496 }
1497 while (0);
1498
1499 /* clean up the device info set */
1500 if (hDeviceInfo != INVALID_HANDLE_VALUE)
1501 SetupDiDestroyDeviceInfoList(hDeviceInfo);
1502 }
1503 while (0);
1504 return rc;
1505}
1506
1507UINT __stdcall UninstallTAPInstances(MSIHANDLE hModule)
1508{
1509 static const WCHAR *s_wszNetworkKey = L"SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}";
1510 HKEY hCtrlNet;
1511
1512 LONG lStatus = RegOpenKeyExW(HKEY_LOCAL_MACHINE, s_wszNetworkKey, 0, KEY_READ, &hCtrlNet);
1513 if (lStatus == ERROR_SUCCESS)
1514 {
1515 logStringW(hModule, L"VBox HostInterfaces: Enumerating interfaces ...");
1516 for (int i = 0; ; ++i)
1517 {
1518 WCHAR wszNetworkGUID[256] = { 0 };
1519 DWORD dwLen = (DWORD)sizeof(wszNetworkGUID);
1520 lStatus = RegEnumKeyExW(hCtrlNet, i, wszNetworkGUID, &dwLen, NULL, NULL, NULL, NULL);
1521 if (lStatus != ERROR_SUCCESS)
1522 {
1523 switch (lStatus)
1524 {
1525 case ERROR_NO_MORE_ITEMS:
1526 logStringW(hModule, L"VBox HostInterfaces: No interfaces found.");
1527 break;
1528 default:
1529 logStringW(hModule, L"VBox HostInterfaces: Enumeration failed: %ld", lStatus);
1530 break;
1531 }
1532 break;
1533 }
1534
1535 if (isTAPDevice(wszNetworkGUID))
1536 {
1537 logStringW(hModule, L"VBox HostInterfaces: Removing interface \"%s\" ...", wszNetworkGUID);
1538 removeNetworkInterface(hModule, wszNetworkGUID);
1539 lStatus = RegDeleteKeyW(hCtrlNet, wszNetworkGUID);
1540 }
1541 }
1542 RegCloseKey(hCtrlNet);
1543 logStringW(hModule, L"VBox HostInterfaces: Removing interfaces done.");
1544 }
1545 return ERROR_SUCCESS;
1546}
1547
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette