1 | /* $Id: VBoxNetLwfInstall.cpp 69496 2017-10-28 14:55:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NetLwfInstall - VBoxNetLwf installer command line tool
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2017 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 VOID winNetCfgLogger (LPCSTR szString)
|
---|
37 | {
|
---|
38 | printf("%s", szString);
|
---|
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], sizeof(wsz) / sizeof(wsz[0]));
|
---|
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 < sizeof(wsz) / sizeof(wsz[0]))
|
---|
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 Inf[MAX_PATH];
|
---|
86 | INetCfg *pnc;
|
---|
87 | LPWSTR lpszLockedBy = NULL;
|
---|
88 | int r = 1;
|
---|
89 |
|
---|
90 | VBoxNetCfgWinSetLogging(winNetCfgLogger);
|
---|
91 |
|
---|
92 | HRESULT hr = CoInitialize(NULL);
|
---|
93 | if (hr == S_OK)
|
---|
94 | {
|
---|
95 | int i = 0;
|
---|
96 | do
|
---|
97 | {
|
---|
98 | hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &lpszLockedBy);
|
---|
99 | if (hr == S_OK)
|
---|
100 | {
|
---|
101 | DWORD dwSize;
|
---|
102 | dwSize = MyGetfullPathNameW(VBOX_NETLWF_INF, sizeof(Inf)/sizeof(Inf[0]), Inf);
|
---|
103 | if (dwSize > 0)
|
---|
104 | {
|
---|
105 | /** @todo add size check for (sizeof(Inf)/sizeof(Inf[0])) == dwSize (string length in sizeof(Inf[0])) */
|
---|
106 | hr = VBoxNetCfgWinNetLwfInstall(pnc, Inf);
|
---|
107 | if (hr == S_OK)
|
---|
108 | {
|
---|
109 | wprintf(L"installed successfully\n");
|
---|
110 | r = 0;
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | wprintf(L"error installing VBoxNetLwf (0x%x)\n", hr);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | else
|
---|
118 | {
|
---|
119 | hr = HRESULT_FROM_WIN32(GetLastError());
|
---|
120 | wprintf(L"error getting full inf path for VBoxNetLwf.inf (0x%x)\n", hr);
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
|
---|
125 | break;
|
---|
126 | }
|
---|
127 | else if (hr == NETCFG_E_NO_WRITE_LOCK && lpszLockedBy)
|
---|
128 | {
|
---|
129 | if (i < VBOX_NETLWF_RETRIES && !wcscmp(lpszLockedBy, L"6to4svc.dll"))
|
---|
130 | {
|
---|
131 | wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", ++i, VBOX_NETLWF_RETRIES);
|
---|
132 | CoTaskMemFree(lpszLockedBy);
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", lpszLockedBy);
|
---|
137 | r = 1;
|
---|
138 | CoTaskMemFree(lpszLockedBy);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | wprintf(L"Error getting the INetCfg interface (0x%x)\n", hr);
|
---|
145 | r = 1;
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | } while (true);
|
---|
149 |
|
---|
150 | CoUninitialize();
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | wprintf(L"Error initializing COM (0x%x)\n", hr);
|
---|
155 | r = 1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | VBoxNetCfgWinSetLogging(NULL);
|
---|
159 |
|
---|
160 | return r;
|
---|
161 | }
|
---|
162 |
|
---|
163 | int __cdecl main(int argc, char **argv)
|
---|
164 | {
|
---|
165 | RT_NOREF2(argc, argv);
|
---|
166 | return VBoxNetLwfInstall();
|
---|
167 | }
|
---|