2 * Copyright 2006-2010 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
49 #define GECKO_VERSION "2.47.2"
51 #define GECKO_ARCH "x86"
52 #define GECKO_SHA "e520ce7336cd420cd09c91337d87e74bb420300fd5cbc6f724c1802766b6a61d"
53 #elif defined(__x86_64__)
54 #define GECKO_ARCH "x86_64"
55 #define GECKO_SHA "0596761024823ff3c21f13e1cd5cd3e89dccc698294d62974d8930aeda86ce45"
58 #define GECKO_SHA "???"
61 #define MONO_VERSION "7.1.1"
62 #if defined(__i386__) || defined(__x86_64__)
63 #define MONO_ARCH "x86"
64 #define MONO_SHA "9dc8e5603b7bc64354eb94ae4ea0f6821424767a3ff44ff0d19e346a490c11ea"
67 #define MONO_SHA "???"
72 const WCHAR
*file_name
;
73 const WCHAR
*subdir_name
;
75 const char *url_default
;
76 const char *config_key
;
77 const char *url_config_key
;
78 const char *dir_config_key
;
79 LPCWSTR dialog_template
;
82 /* Download addon files over HTTP because Wine depends on an external library
83 * for TLS, so we can't be sure that HTTPS will work. The integrity of each file
84 * is checked with a hardcoded cryptographically secure hash. */
85 static const addon_info_t addons_info
[] = {
88 L
"wine-gecko-" GECKO_VERSION
"-" GECKO_ARCH
".msi",
91 "http://source.winehq.org/winegecko.php",
92 "MSHTML", "GeckoUrl", "GeckoCabDir",
93 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG
)
97 L
"wine-mono-" MONO_VERSION
"-" MONO_ARCH
".msi",
100 "http://source.winehq.org/winemono.php",
101 "Dotnet", "MonoUrl", "MonoCabDir",
102 MAKEINTRESOURCEW(ID_DWL_MONO_DIALOG
)
106 static const addon_info_t
*addon
;
108 static HWND install_dialog
= NULL
;
109 static LPWSTR url
= NULL
;
110 static IBinding
*dwl_binding
;
111 static WCHAR
*msi_file
;
113 extern const char * CDECL
wine_get_version(void);
115 static WCHAR
* (CDECL
*p_wine_get_dos_file_name
)(const char*);
117 static BOOL
sha_check(const WCHAR
*file_name
)
119 const unsigned char *file_map
;
122 BCRYPT_HASH_HANDLE hash
= NULL
;
123 BCRYPT_ALG_HANDLE alg
= NULL
;
128 file
= CreateFileW(file_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
129 if(file
== INVALID_HANDLE_VALUE
) {
130 WARN("Could not open file: %lu\n", GetLastError());
134 size
= GetFileSize(file
, NULL
);
136 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
141 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
146 if(BCryptOpenAlgorithmProvider(&alg
, BCRYPT_SHA256_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0))
148 if(BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0))
150 if(BCryptHashData(hash
, (UCHAR
*)file_map
, size
, 0))
152 if(BCryptFinishHash(hash
, sha
, sizeof(sha
), 0))
155 for(i
=0; i
< sizeof(sha
); i
++)
156 sprintf(buf
+ i
* 2, "%02x", sha
[i
]);
158 ret
= !strcmp(buf
, addon
->sha
);
160 WARN("Got %s, expected %s\n", buf
, addon
->sha
);
163 UnmapViewOfFile(file_map
);
164 if(hash
) BCryptDestroyHash(hash
);
165 if(alg
) BCryptCloseAlgorithmProvider(alg
, 0);
169 static void set_status(DWORD id
)
171 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
174 LoadStringW(hInst
, id
, buf
, ARRAY_SIZE(buf
));
175 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
184 static enum install_res
install_file(const WCHAR
*file_name
)
188 res
= MsiInstallProductW(file_name
, NULL
);
189 if(res
== ERROR_PRODUCT_VERSION
)
190 res
= MsiInstallProductW(file_name
, L
"REINSTALL=ALL REINSTALLMODE=vomus");
191 if(res
!= ERROR_SUCCESS
) {
192 ERR("MsiInstallProduct failed: %lu\n", res
);
193 return INSTALL_FAILED
;
199 static enum install_res
install_from_dos_file(const WCHAR
*dir
, const WCHAR
*subdir
, const WCHAR
*file_name
)
201 WCHAR
*path
, *canonical_path
;
202 enum install_res ret
;
203 int len
= lstrlenW( dir
);
207 size
+= lstrlenW( subdir
) + lstrlenW( file_name
) + 2;
208 if (!(path
= heap_alloc( size
* sizeof(WCHAR
) ))) return INSTALL_FAILED
;
210 lstrcpyW( path
, dir
);
211 if (!wcsncmp( path
, L
"\\??\\", 4 )) path
[1] = '\\'; /* change \??\ into \\?\ */
212 if (len
&& path
[len
-1] != '/' && path
[len
-1] != '\\') path
[len
++] = '\\';
214 lstrcpyW( path
+ len
, subdir
);
215 lstrcatW( path
, L
"\\" );
216 lstrcatW( path
, file_name
);
218 hr
= PathAllocCanonicalize( path
, PATHCCH_ALLOW_LONG_PATHS
, &canonical_path
);
221 ERR( "Failed to canonicalize %s, hr %#lx\n", debugstr_w(path
), hr
);
227 if (GetFileAttributesW( canonical_path
) == INVALID_FILE_ATTRIBUTES
)
229 TRACE( "%s not found\n", debugstr_w(canonical_path
) );
230 LocalFree( canonical_path
);
234 ret
= install_file( canonical_path
);
236 LocalFree( canonical_path
);
240 static enum install_res
install_from_unix_file(const char *dir
, const WCHAR
*subdir
, const WCHAR
*file_name
)
243 enum install_res ret
= INSTALL_NEXT
;
245 if (p_wine_get_dos_file_name
&& (dos_dir
= p_wine_get_dos_file_name( dir
)))
247 ret
= install_from_dos_file( dos_dir
, subdir
, file_name
);
248 heap_free( dos_dir
);
253 static HKEY
open_config_key(void)
258 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
259 res
= RegOpenKeyW(HKEY_CURRENT_USER
, L
"Software\\Wine", &hkey
);
260 if(res
!= ERROR_SUCCESS
)
263 res
= RegOpenKeyA(hkey
, addon
->config_key
, &ret
);
265 return res
== ERROR_SUCCESS
? ret
: NULL
;
268 static enum install_res
install_from_registered_dir(void)
272 DWORD res
, type
, size
= MAX_PATH
;
273 enum install_res ret
;
275 hkey
= open_config_key();
279 package_dir
= heap_alloc(size
);
280 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
281 if(res
== ERROR_MORE_DATA
) {
282 package_dir
= heap_realloc(package_dir
, size
);
283 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
286 if(res
== ERROR_FILE_NOT_FOUND
) {
287 heap_free(package_dir
);
289 } else if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
290 heap_free(package_dir
);
291 return INSTALL_FAILED
;
294 ret
= install_from_unix_file(package_dir
, L
"", addon
->file_name
);
296 heap_free(package_dir
);
300 static enum install_res
install_from_default_dir(void)
302 const WCHAR
*package_dir
;
303 WCHAR
*dir_buf
= NULL
;
304 enum install_res ret
= INSTALL_NEXT
;
306 if ((package_dir
= _wgetenv( L
"WINEBUILDDIR" )))
308 dir_buf
= heap_alloc( lstrlenW(package_dir
) * sizeof(WCHAR
) + sizeof(L
"\\..\\") );
309 lstrcpyW( dir_buf
, package_dir
);
310 lstrcatW( dir_buf
, L
"\\..\\" );
311 package_dir
= dir_buf
;
313 else package_dir
= _wgetenv( L
"WINEDATADIR" );
317 ret
= install_from_dos_file(package_dir
, addon
->subdir_name
, addon
->file_name
);
321 if (ret
== INSTALL_NEXT
)
322 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/", addon
->subdir_name
, addon
->file_name
);
323 if (ret
== INSTALL_NEXT
&& strcmp(INSTALL_DATADIR
, "/usr/share") != 0)
324 ret
= install_from_unix_file("/usr/share/wine/", addon
->subdir_name
, addon
->file_name
);
325 if (ret
== INSTALL_NEXT
)
326 ret
= install_from_unix_file("/opt/wine/", addon
->subdir_name
, addon
->file_name
);
330 static WCHAR
*get_cache_file_name(BOOL ensure_exists
)
333 const WCHAR
*home_dir
;
334 WCHAR
*cache_dir
, *ret
;
337 xdg_dir
= getenv( "XDG_CACHE_HOME" );
338 if (xdg_dir
&& *xdg_dir
&& p_wine_get_dos_file_name
)
340 if (!(cache_dir
= p_wine_get_dos_file_name( xdg_dir
))) return NULL
;
342 else if ((home_dir
= _wgetenv( L
"WINEHOMEDIR" )))
344 if (!(cache_dir
= heap_alloc( lstrlenW(home_dir
) * sizeof(WCHAR
) + sizeof(L
"\\.cache") ))) return NULL
;
345 lstrcpyW( cache_dir
, home_dir
);
346 lstrcatW( cache_dir
, L
"\\.cache" );
347 cache_dir
[1] = '\\'; /* change \??\ into \\?\ */
351 if (ensure_exists
&& !CreateDirectoryW( cache_dir
, NULL
) && GetLastError() != ERROR_ALREADY_EXISTS
)
353 WARN( "%s does not exist and could not be created (%lu)\n", debugstr_w(cache_dir
), GetLastError() );
354 heap_free( cache_dir
);
358 size
= lstrlenW( cache_dir
) + ARRAY_SIZE(L
"\\wine") + lstrlenW( addon
->file_name
) + 1;
359 if (!(ret
= heap_alloc( size
* sizeof(WCHAR
) )))
361 heap_free( cache_dir
);
364 lstrcpyW( ret
, cache_dir
);
365 lstrcatW( ret
, L
"\\wine" );
366 heap_free( cache_dir
);
368 if (ensure_exists
&& !CreateDirectoryW( ret
, NULL
) && GetLastError() != ERROR_ALREADY_EXISTS
)
370 WARN( "%s does not exist and could not be created (%lu)\n", debugstr_w(ret
), GetLastError() );
374 len
= lstrlenW( ret
);
376 lstrcpyW( ret
+ len
, addon
->file_name
);
378 TRACE( "got %s\n", debugstr_w(ret
) );
382 static enum install_res
install_from_cache(void)
384 WCHAR
*cache_file_name
;
385 enum install_res res
;
387 cache_file_name
= get_cache_file_name(FALSE
);
391 if(!sha_check(cache_file_name
)) {
392 WARN("could not validate checksum\n");
393 DeleteFileW(cache_file_name
);
394 heap_free(cache_file_name
);
398 res
= install_file(cache_file_name
);
399 heap_free(cache_file_name
);
403 static IInternetBindInfo InstallCallbackBindInfo
;
405 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
406 REFIID riid
, void **ppv
)
408 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
413 if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
414 TRACE("IID_IInternetBindInfo\n");
415 *ppv
= &InstallCallbackBindInfo
;
422 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
427 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
432 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
433 DWORD dwReserved
, IBinding
*pib
)
435 set_status(IDS_DOWNLOADING
);
437 IBinding_AddRef(pib
);
443 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
449 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
455 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
456 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
458 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
461 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
463 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
468 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
469 HRESULT hresult
, LPCWSTR szError
)
472 IBinding_Release(dwl_binding
);
476 if(FAILED(hresult
)) {
477 if(hresult
== E_ABORT
)
478 TRACE("Binding aborted\n");
480 ERR("Binding failed %08lx\n", hresult
);
485 ERR("No MSI file\n");
489 set_status(IDS_INSTALLING
);
490 EnableWindow(GetDlgItem(install_dialog
, IDCANCEL
), 0);
492 if(sha_check(msi_file
)) {
493 WCHAR
*cache_file_name
;
495 install_file(msi_file
);
497 cache_file_name
= get_cache_file_name(TRUE
);
498 if(cache_file_name
) {
499 CopyFileW(msi_file
, cache_file_name
, FALSE
);
500 heap_free(cache_file_name
);
505 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, ARRAY_SIZE(message
)))
506 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
509 DeleteFileW(msi_file
);
513 EndDialog(install_dialog
, 0);
517 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
518 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
522 *grfBINDF
= BINDF_ASYNCHRONOUS
;
526 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
527 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
530 msi_file
= heap_strdupW(pstgmed
->u
.lpszFileName
);
531 TRACE("got file name %s\n", debugstr_w(msi_file
));
537 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
538 REFIID riid
, IUnknown
* punk
)
544 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
545 InstallCallback_QueryInterface
,
546 InstallCallback_AddRef
,
547 InstallCallback_Release
,
548 InstallCallback_OnStartBinding
,
549 InstallCallback_GetPriority
,
550 InstallCallback_OnLowResource
,
551 InstallCallback_OnProgress
,
552 InstallCallback_OnStopBinding
,
553 InstallCallback_GetBindInfo
,
554 InstallCallback_OnDataAvailable
,
555 InstallCallback_OnObjectAvailable
558 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
560 static HRESULT WINAPI
InstallCallbackBindInfo_QueryInterface(IInternetBindInfo
*iface
, REFIID riid
, void **ppv
)
562 return IBindStatusCallback_QueryInterface(&InstallCallback
, riid
, ppv
);
565 static ULONG WINAPI
InstallCallbackBindInfo_AddRef(IInternetBindInfo
*iface
)
570 static ULONG WINAPI
InstallCallbackBindInfo_Release(IInternetBindInfo
*iface
)
575 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindInfo(IInternetBindInfo
*iface
, DWORD
*bindf
, BINDINFO
*bindinfo
)
581 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindString(IInternetBindInfo
*iface
, ULONG string_type
,
582 WCHAR
**strs
, ULONG cnt
, ULONG
*fetched
)
584 switch(string_type
) {
585 case BINDSTRING_USER_AGENT
:
586 TRACE("BINDSTRING_USER_AGENT\n");
588 *strs
= CoTaskMemAlloc(sizeof(L
"Wine Addon Downloader"));
590 return E_OUTOFMEMORY
;
592 lstrcpyW(*strs
, L
"Wine Addon Downloader");
600 static const IInternetBindInfoVtbl InstallCallbackBindInfoVtbl
= {
601 InstallCallbackBindInfo_QueryInterface
,
602 InstallCallbackBindInfo_AddRef
,
603 InstallCallbackBindInfo_Release
,
604 InstallCallbackBindInfo_GetBindInfo
,
605 InstallCallbackBindInfo_GetBindString
608 static IInternetBindInfo InstallCallbackBindInfo
= { &InstallCallbackBindInfoVtbl
};
610 static void append_url_params( WCHAR
*url
)
612 DWORD size
= INTERNET_MAX_URL_LENGTH
* sizeof(WCHAR
);
613 DWORD len
= lstrlenW(url
);
615 lstrcpyW(url
+len
, L
"?arch=");
616 len
+= lstrlenW(L
"?arch=");
617 len
+= MultiByteToWideChar(CP_ACP
, 0, GECKO_ARCH
, sizeof(GECKO_ARCH
),
618 url
+len
, size
/sizeof(WCHAR
)-len
)-1;
619 lstrcpyW(url
+len
, L
"&v=");
620 len
+= lstrlenW(L
"&v=");
621 len
+= MultiByteToWideChar(CP_ACP
, 0, addon
->version
, -1, url
+len
, size
/sizeof(WCHAR
)-len
)-1;
622 lstrcpyW(url
+len
, L
"&winev=");
623 len
+= lstrlenW(L
"&winev=");
624 MultiByteToWideChar(CP_ACP
, 0, wine_get_version(), -1, url
+len
, size
/sizeof(WCHAR
)-len
);
627 static LPWSTR
get_url(void)
629 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
630 WCHAR
*url
, *config_key
;
635 static const WCHAR httpW
[] = {'h','t','t','p'};
637 url
= heap_alloc(size
);
638 returned_size
= size
;
640 hkey
= open_config_key();
643 config_key
= heap_strdupAtoW(addon
->url_config_key
);
644 res
= RegQueryValueExW(hkey
, config_key
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
645 heap_free(config_key
);
647 if(res
== ERROR_SUCCESS
&& type
== REG_SZ
) goto found
;
650 MultiByteToWideChar( CP_ACP
, 0, addon
->url_default
, -1, url
, size
/ sizeof(WCHAR
) );
653 if (returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) append_url_params( url
);
655 TRACE("Got URL %s\n", debugstr_w(url
));
659 static BOOL
start_download(void)
666 hres
= CreateURLMoniker(NULL
, url
, &mon
);
670 hres
= CreateAsyncBindCtx(0, &InstallCallback
, NULL
, &bind_ctx
);
671 if(SUCCEEDED(hres
)) {
672 hres
= IMoniker_BindToStorage(mon
, bind_ctx
, NULL
, &IID_IUnknown
, (void**)&tmp
);
673 IBindCtx_Release(bind_ctx
);
675 IMoniker_Release(mon
);
680 IUnknown_Release(tmp
);
684 static void run_winebrowser(const WCHAR
*url
)
686 PROCESS_INFORMATION pi
;
693 url_len
= lstrlenW(url
);
695 len
= GetSystemDirectoryW(app
, MAX_PATH
- ARRAY_SIZE(L
"\\winebrowser.exe"));
696 lstrcpyW(app
+len
, L
"\\winebrowser.exe");
697 len
+= ARRAY_SIZE(L
"\\winebrowser.exe") - 1;
699 args
= heap_alloc((len
+1+url_len
)*sizeof(WCHAR
));
703 memcpy(args
, app
, len
*sizeof(WCHAR
));
705 memcpy(args
+len
, url
, (url_len
+1) * sizeof(WCHAR
));
707 TRACE("starting %s\n", debugstr_w(args
));
709 memset(&si
, 0, sizeof(si
));
711 ret
= CreateProcessW(app
, args
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
);
714 CloseHandle(pi
.hThread
);
715 CloseHandle(pi
.hProcess
);
719 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
723 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
724 install_dialog
= hwnd
;
728 switch (((NMHDR
*)lParam
)->code
)
732 if (wParam
== ID_DWL_STATUS
)
733 run_winebrowser(((NMLINK
*)lParam
)->item
.szUrl
);
742 IBinding_Abort(dwl_binding
);
747 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
748 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
749 if(!start_download())
750 EndDialog(install_dialog
, 0);
757 BOOL
install_addon(addon_t addon_type
)
762 addon
= addons_info
+addon_type
;
764 p_wine_get_dos_file_name
= (void *)GetProcAddress(GetModuleHandleW(L
"kernel32.dll"), "wine_get_dos_file_name");
767 * Try to find addon .msi file in following order:
768 * - directory stored in $dir_config_key value of HKCU/Software/Wine/$config_key key
769 * - $datadir/$addon_subdir/
770 * - $INSTALL_DATADIR/wine/$addon_subdir/
771 * - /usr/share/wine/$addon_subdir/
772 * - /opt/wine/$addon_subdir/
773 * - download from URL stored in $url_config_key value of HKCU/Software/Wine/$config_key key
775 if (install_from_registered_dir() == INSTALL_NEXT
776 && install_from_default_dir() == INSTALL_NEXT
777 && install_from_cache() == INSTALL_NEXT
778 && (url
= get_url()))
779 DialogBoxW(hInst
, addon
->dialog_template
, 0, installer_proc
);