1 | /* $Id: VBoxNetLwfInstall.cpp 85121 2020-07-08 19:33:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NetLwfInstall - VBoxNetLwf installer command line tool
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2020 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #include <VBox/VBoxNetCfg-win.h>
|
---|
28 | #include <devguid.h>
|
---|
29 | #include <stdio.h>
|
---|
30 |
|
---|
31 | #define VBOX_NETCFG_APP_NAME L"NetLwfInstall"
|
---|
32 | #define VBOX_NETLWF_INF L".\\VBoxNetLwf.inf"
|
---|
33 | #define VBOX_NETLWF_RETRIES 10
|
---|
34 |
|
---|
35 |
|
---|
36 | static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
|
---|
37 | {
|
---|
38 | printf("%s", pszString);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /** Wrapper around GetfullPathNameW that will try an alternative INF location.
|
---|
42 | *
|
---|
43 | * The default location is the current directory. If not found there, the
|
---|
44 | * alternative location is the executable directory. If not found there either,
|
---|
45 | * the first alternative is present to the caller.
|
---|
46 | */
|
---|
47 | static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
|
---|
48 | {
|
---|
49 | LPWSTR pwszFilePart;
|
---|
50 | DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
|
---|
51 | if (dwSize <= 0)
|
---|
52 | return dwSize;
|
---|
53 |
|
---|
54 | /* if it doesn't exist, see if the file exists in the same directory as the executable. */
|
---|
55 | if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
|
---|
56 | {
|
---|
57 | WCHAR wsz[512];
|
---|
58 | DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
|
---|
59 | if (cch > 0)
|
---|
60 | {
|
---|
61 | while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
|
---|
62 | cch--;
|
---|
63 | unsigned i = 0;
|
---|
64 | while (cch < RT_ELEMENTS(wsz))
|
---|
65 | {
|
---|
66 | wsz[cch] = pwszFilePart[i++];
|
---|
67 | if (!wsz[cch])
|
---|
68 | {
|
---|
69 | dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
|
---|
70 | if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
|
---|
71 | return dwSize;
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | cch++;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* fallback */
|
---|
80 | return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
|
---|
81 | }
|
---|
82 |
|
---|
83 | static int VBoxNetLwfInstall()
|
---|
84 | {
|
---|
85 | WCHAR wszInf[MAX_PATH];
|
---|
86 | INetCfg *pnc;
|
---|
87 | int rcExit = RTEXITCODE_FAILURE;
|
---|
88 |
|
---|
89 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
90 |
|
---|
91 | HRESULT hr = CoInitialize(NULL);
|
---|
92 | if (hr == S_OK)
|
---|
93 | {
|
---|
94 | for (int i = 0;; i++)
|
---|
95 | {
|
---|
96 | LPWSTR pwszLockedBy = NULL;
|
---|
97 | hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &pwszLockedBy);
|
---|
98 | if (hr == S_OK)
|
---|
99 | {
|
---|
100 | DWORD dwSize;
|
---|
101 | dwSize = MyGetfullPathNameW(VBOX_NETLWF_INF, RT_ELEMENTS(wszInf), wszInf);
|
---|
102 | if (dwSize > 0)
|
---|
103 | {
|
---|
104 | /** @todo add size check for (RT_ELEMENTS(wszInf) == dwSize (string length in WCHARs) */
|
---|
105 | hr = VBoxNetCfgWinNetLwfInstall(pnc, wszInf);
|
---|
106 | if (hr == S_OK)
|
---|
107 | {
|
---|
108 | wprintf(L"installed successfully\n");
|
---|
109 | rcExit = RTEXITCODE_SUCCESS;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | wprintf(L"error installing VBoxNetLwf (%#lx)\n", hr);
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | hr = HRESULT_FROM_WIN32(GetLastError());
|
---|
117 | wprintf(L"error getting full inf path for VBoxNetLwf.inf (%#lx)\n", hr);
|
---|
118 | }
|
---|
119 |
|
---|
120 | VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
|
---|
121 | break;
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy)
|
---|
125 | {
|
---|
126 | if (i < VBOX_NETLWF_RETRIES && !wcscmp(pwszLockedBy, L"6to4svc.dll"))
|
---|
127 | {
|
---|
128 | wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", i + 1, VBOX_NETLWF_RETRIES);
|
---|
129 | CoTaskMemFree(pwszLockedBy);
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n",
|
---|
134 | pwszLockedBy);
|
---|
135 | CoTaskMemFree(pwszLockedBy);
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | wprintf(L"Error getting the INetCfg interface (%#lx)\n", hr);
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | CoUninitialize();
|
---|
147 | }
|
---|
148 | else
|
---|
149 | wprintf(L"Error initializing COM (%#lx)\n", hr);
|
---|
150 |
|
---|
151 | VBoxNetCfgWinSetLogging(NULL);
|
---|
152 |
|
---|
153 | return rcExit;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int __cdecl main(int argc, char **argv)
|
---|
157 | {
|
---|
158 | RT_NOREF2(argc, argv);
|
---|
159 | return VBoxNetLwfInstall();
|
---|
160 | }
|
---|