1 | /* $Id: VBoxNetFltInstall.cpp 96572 2022-09-01 20:36:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NetFltInstall - VBoxNetFlt installer command line tool
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <VBox/VBoxNetCfg-win.h>
|
---|
42 | #include <devguid.h>
|
---|
43 | #include <stdio.h>
|
---|
44 |
|
---|
45 | #include <iprt/initterm.h>
|
---|
46 | #include <iprt/message.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Header Files *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | #define NETFLT_ID L"sun_VBoxNetFlt"
|
---|
53 | #define VBOX_NETCFG_APP_NAME L"NetFltInstall"
|
---|
54 | #define VBOX_NETFLT_PT_INF L".\\VBoxNetFlt.inf"
|
---|
55 | #define VBOX_NETFLT_MP_INF L".\\VBoxNetFltM.inf"
|
---|
56 | #define VBOX_NETFLT_RETRIES 10
|
---|
57 |
|
---|
58 |
|
---|
59 | static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
|
---|
60 | {
|
---|
61 | printf("%s", pszString);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Wrapper around GetfullPathNameW that will try an alternative INF location.
|
---|
65 | *
|
---|
66 | * The default location is the current directory. If not found there, the
|
---|
67 | * alternative location is the executable directory. If not found there either,
|
---|
68 | * the first alternative is present to the caller.
|
---|
69 | */
|
---|
70 | static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
|
---|
71 | {
|
---|
72 | LPWSTR pwszFilePart;
|
---|
73 | DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
|
---|
74 | if (dwSize <= 0)
|
---|
75 | return dwSize;
|
---|
76 |
|
---|
77 | /* if it doesn't exist, see if the file exists in the same directory as the executable. */
|
---|
78 | if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
|
---|
79 | {
|
---|
80 | WCHAR wsz[512];
|
---|
81 | DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
|
---|
82 | if (cch > 0)
|
---|
83 | {
|
---|
84 | while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
|
---|
85 | cch--;
|
---|
86 | unsigned i = 0;
|
---|
87 | while (cch < RT_ELEMENTS(wsz))
|
---|
88 | {
|
---|
89 | wsz[cch] = pwszFilePart[i++];
|
---|
90 | if (!wsz[cch])
|
---|
91 | {
|
---|
92 | dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
|
---|
93 | if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
|
---|
94 | return dwSize;
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | cch++;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* fallback */
|
---|
103 | return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static int VBoxNetFltInstall()
|
---|
107 | {
|
---|
108 | WCHAR wszPtInf[MAX_PATH];
|
---|
109 | WCHAR wszMpInf[MAX_PATH];
|
---|
110 | INetCfg *pnc;
|
---|
111 | int rcExit = RTEXITCODE_FAILURE;
|
---|
112 |
|
---|
113 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
114 |
|
---|
115 | HRESULT hr = CoInitialize(NULL);
|
---|
116 | if (hr == S_OK)
|
---|
117 | {
|
---|
118 | for (int i = 0;; i++)
|
---|
119 | {
|
---|
120 | LPWSTR pwszLockedBy = NULL;
|
---|
121 | hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &pwszLockedBy);
|
---|
122 | if (hr == S_OK)
|
---|
123 | {
|
---|
124 | DWORD dwSize;
|
---|
125 | dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, RT_ELEMENTS(wszPtInf), wszPtInf);
|
---|
126 | if (dwSize > 0)
|
---|
127 | {
|
---|
128 | /** @todo add size check for (RT_ELEMENTS(wszPtInf) == dwSize (string length in WCHARs) */
|
---|
129 |
|
---|
130 | dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, RT_ELEMENTS(wszMpInf), wszMpInf);
|
---|
131 | if (dwSize > 0)
|
---|
132 | {
|
---|
133 | /** @todo add size check for (RT_ELEMENTS(wszMpInf) == dwSize (string length in WHCARs) */
|
---|
134 |
|
---|
135 | LPCWSTR apwszInfs[] = { wszPtInf, wszMpInf };
|
---|
136 | hr = VBoxNetCfgWinNetFltInstall(pnc, apwszInfs, 2);
|
---|
137 | if (hr == S_OK)
|
---|
138 | {
|
---|
139 | wprintf(L"installed successfully\n");
|
---|
140 | rcExit = RTEXITCODE_SUCCESS;
|
---|
141 | }
|
---|
142 | else
|
---|
143 | wprintf(L"error installing VBoxNetFlt (%#lx)\n", hr);
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | hr = HRESULT_FROM_WIN32(GetLastError());
|
---|
148 | wprintf(L"error getting full inf path for VBoxNetFltM.inf (%#lx)\n", hr);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | else
|
---|
152 | {
|
---|
153 | hr = HRESULT_FROM_WIN32(GetLastError());
|
---|
154 | wprintf(L"error getting full inf path for VBoxNetFlt.inf (%#lx)\n", hr);
|
---|
155 | }
|
---|
156 |
|
---|
157 | VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
|
---|
158 | break;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy)
|
---|
162 | {
|
---|
163 | if (i < VBOX_NETFLT_RETRIES && !wcscmp(pwszLockedBy, L"6to4svc.dll"))
|
---|
164 | {
|
---|
165 | wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", i + 1, VBOX_NETFLT_RETRIES);
|
---|
166 | CoTaskMemFree(pwszLockedBy);
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", pwszLockedBy);
|
---|
171 | CoTaskMemFree(pwszLockedBy);
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | else
|
---|
176 | {
|
---|
177 | wprintf(L"Error getting the INetCfg interface (%#lx)\n", hr);
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | CoUninitialize();
|
---|
183 | }
|
---|
184 | else
|
---|
185 | wprintf(L"Error initializing COM (%#lx)\n", hr);
|
---|
186 |
|
---|
187 | VBoxNetCfgWinSetLogging(NULL);
|
---|
188 |
|
---|
189 | return rcExit;
|
---|
190 | }
|
---|
191 |
|
---|
192 | int __cdecl main(int argc, char **argv)
|
---|
193 | {
|
---|
194 | RTR3InitExeNoArguments(0);
|
---|
195 | if (argc != 1)
|
---|
196 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n");
|
---|
197 | NOREF(argv);
|
---|
198 |
|
---|
199 | return VBoxNetFltInstall();
|
---|
200 | }
|
---|
201 |
|
---|