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
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl
);
48 #define GECKO_VERSION "2.47.2"
50 #define GECKO_ARCH "x86"
51 #define GECKO_SHA "e520ce7336cd420cd09c91337d87e74bb420300fd5cbc6f724c1802766b6a61d"
52 #elif defined(__x86_64__)
53 #define GECKO_ARCH "x86_64"
54 #define GECKO_SHA "0596761024823ff3c21f13e1cd5cd3e89dccc698294d62974d8930aeda86ce45"
57 #define GECKO_SHA "???"
60 #define MONO_VERSION "6.0.0"
61 #if defined(__i386__) || defined(__x86_64__)
62 #define MONO_ARCH "x86"
63 #define MONO_SHA "5c7af5976d101be359a77045c9cc8fc40feac028263d00e731578864e8b183d0"
66 #define MONO_SHA "???"
71 const WCHAR
*file_name
;
72 const WCHAR
*subdir_name
;
74 const char *url_default
;
75 const char *config_key
;
76 const char *url_config_key
;
77 const char *dir_config_key
;
78 LPCWSTR dialog_template
;
81 /* Download addon files over HTTP because Wine depends on an external library
82 * for TLS, so we can't be sure that HTTPS will work. The integrity of each file
83 * is checked with a hardcoded cryptographically secure hash. */
84 static const addon_info_t addons_info
[] = {
87 L
"wine-gecko-" GECKO_VERSION
"-" GECKO_ARCH
".msi",
90 "http://source.winehq.org/winegecko.php",
91 "MSHTML", "GeckoUrl", "GeckoCabDir",
92 MAKEINTRESOURCEW(ID_DWL_GECKO_DIALOG
)
96 L
"wine-mono-" MONO_VERSION
"-" MONO_ARCH
".msi",
99 "http://source.winehq.org/winemono.php",
100 "Dotnet", "MonoUrl", "MonoCabDir",
101 MAKEINTRESOURCEW(ID_DWL_MONO_DIALOG
)
105 static const addon_info_t
*addon
;
107 static HWND install_dialog
= NULL
;
108 static LPWSTR url
= NULL
;
109 static IBinding
*dwl_binding
;
110 static WCHAR
*msi_file
;
112 extern const char * CDECL
wine_get_version(void);
114 static WCHAR
* (CDECL
*p_wine_get_dos_file_name
)(const char*);
116 static BOOL
sha_check(const WCHAR
*file_name
)
118 const unsigned char *file_map
;
121 BCRYPT_HASH_HANDLE hash
= NULL
;
122 BCRYPT_ALG_HANDLE alg
= NULL
;
127 file
= CreateFileW(file_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
128 if(file
== INVALID_HANDLE_VALUE
) {
129 WARN("Could not open file: %u\n", GetLastError());
133 size
= GetFileSize(file
, NULL
);
135 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
140 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
145 if(BCryptOpenAlgorithmProvider(&alg
, BCRYPT_SHA256_ALGORITHM
, MS_PRIMITIVE_PROVIDER
, 0))
147 if(BCryptCreateHash(alg
, &hash
, NULL
, 0, NULL
, 0, 0))
149 if(BCryptHashData(hash
, (UCHAR
*)file_map
, size
, 0))
151 if(BCryptFinishHash(hash
, sha
, sizeof(sha
), 0))
154 for(i
=0; i
< sizeof(sha
); i
++)
155 sprintf(buf
+ i
* 2, "%02x", sha
[i
]);
157 ret
= !strcmp(buf
, addon
->sha
);
159 WARN("Got %s, expected %s\n", buf
, addon
->sha
);
162 UnmapViewOfFile(file_map
);
163 if(hash
) BCryptDestroyHash(hash
);
164 if(alg
) BCryptCloseAlgorithmProvider(alg
, 0);
168 static void set_status(DWORD id
)
170 HWND status
= GetDlgItem(install_dialog
, ID_DWL_STATUS
);
173 LoadStringW(hInst
, id
, buf
, ARRAY_SIZE(buf
));
174 SendMessageW(status
, WM_SETTEXT
, 0, (LPARAM
)buf
);
183 static enum install_res
install_file(const WCHAR
*file_name
)
187 res
= MsiInstallProductW(file_name
, NULL
);
188 if(res
== ERROR_PRODUCT_VERSION
)
189 res
= MsiInstallProductW(file_name
, L
"REINSTALL=ALL REINSTALLMODE=vomus");
190 if(res
!= ERROR_SUCCESS
) {
191 ERR("MsiInstallProduct failed: %u\n", res
);
192 return INSTALL_FAILED
;
198 static enum install_res
install_from_dos_file(const WCHAR
*dir
, const WCHAR
*subdir
, const WCHAR
*file_name
)
201 enum install_res ret
;
202 int len
= lstrlenW( dir
);
205 size
+= lstrlenW( subdir
) + lstrlenW( file_name
) + 2;
206 if (!(path
= heap_alloc( size
* sizeof(WCHAR
) ))) return INSTALL_FAILED
;
208 lstrcpyW( path
, dir
);
209 if (!wcsncmp( path
, L
"\\??\\", 4 )) path
[1] = '\\'; /* change \??\ into \\?\ */
210 if (len
&& path
[len
-1] != '/' && path
[len
-1] != '\\') path
[len
++] = '\\';
212 lstrcpyW( path
+ len
, subdir
);
213 lstrcatW( path
, L
"\\" );
214 lstrcatW( path
, file_name
);
216 if (GetFileAttributesW( path
) == INVALID_FILE_ATTRIBUTES
)
218 TRACE( "%s not found\n", debugstr_w(path
) );
223 ret
= install_file( path
);
229 static enum install_res
install_from_unix_file(const char *dir
, const WCHAR
*subdir
, const WCHAR
*file_name
)
232 enum install_res ret
= INSTALL_NEXT
;
234 if (p_wine_get_dos_file_name
&& (dos_dir
= p_wine_get_dos_file_name( dir
)))
236 ret
= install_from_dos_file( dos_dir
, subdir
, file_name
);
237 heap_free( dos_dir
);
242 static HKEY
open_config_key(void)
247 /* @@ Wine registry key: HKCU\Software\Wine\$config_key */
248 res
= RegOpenKeyW(HKEY_CURRENT_USER
, L
"Software\\Wine", &hkey
);
249 if(res
!= ERROR_SUCCESS
)
252 res
= RegOpenKeyA(hkey
, addon
->config_key
, &ret
);
254 return res
== ERROR_SUCCESS
? ret
: NULL
;
257 static enum install_res
install_from_registered_dir(void)
261 DWORD res
, type
, size
= MAX_PATH
;
262 enum install_res ret
;
264 hkey
= open_config_key();
268 package_dir
= heap_alloc(size
);
269 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
270 if(res
== ERROR_MORE_DATA
) {
271 package_dir
= heap_realloc(package_dir
, size
);
272 res
= RegGetValueA(hkey
, NULL
, addon
->dir_config_key
, RRF_RT_ANY
, &type
, (PBYTE
)package_dir
, &size
);
275 if(res
== ERROR_FILE_NOT_FOUND
) {
276 heap_free(package_dir
);
278 } else if(res
!= ERROR_SUCCESS
|| (type
!= REG_SZ
&& type
!= REG_EXPAND_SZ
)) {
279 heap_free(package_dir
);
280 return INSTALL_FAILED
;
283 ret
= install_from_unix_file(package_dir
, L
"", addon
->file_name
);
285 heap_free(package_dir
);
289 static enum install_res
install_from_default_dir(void)
291 const WCHAR
*package_dir
;
292 WCHAR
*dir_buf
= NULL
;
293 enum install_res ret
= INSTALL_NEXT
;
295 if ((package_dir
= _wgetenv( L
"WINEBUILDDIR" )))
297 dir_buf
= heap_alloc( lstrlenW(package_dir
) * sizeof(WCHAR
) + sizeof(L
"\\..\\") );
298 lstrcpyW( dir_buf
, package_dir
);
299 lstrcatW( dir_buf
, L
"\\..\\" );
300 package_dir
= dir_buf
;
302 else package_dir
= _wgetenv( L
"WINEDATADIR" );
306 ret
= install_from_dos_file(package_dir
, addon
->subdir_name
, addon
->file_name
);
310 if (ret
== INSTALL_NEXT
)
311 ret
= install_from_unix_file(INSTALL_DATADIR
"/wine/", addon
->subdir_name
, addon
->file_name
);
312 if (ret
== INSTALL_NEXT
&& strcmp(INSTALL_DATADIR
, "/usr/share") != 0)
313 ret
= install_from_unix_file("/usr/share/wine/", addon
->subdir_name
, addon
->file_name
);
314 if (ret
== INSTALL_NEXT
)
315 ret
= install_from_unix_file("/opt/wine/", addon
->subdir_name
, addon
->file_name
);
319 static WCHAR
*get_cache_file_name(BOOL ensure_exists
)
322 const WCHAR
*home_dir
;
323 WCHAR
*cache_dir
, *ret
;
326 xdg_dir
= getenv( "XDG_CACHE_HOME" );
327 if (xdg_dir
&& *xdg_dir
&& p_wine_get_dos_file_name
)
329 if (!(cache_dir
= p_wine_get_dos_file_name( xdg_dir
))) return NULL
;
331 else if ((home_dir
= _wgetenv( L
"WINEHOMEDIR" )))
333 if (!(cache_dir
= heap_alloc( lstrlenW(home_dir
) * sizeof(WCHAR
) + sizeof(L
"\\.cache") ))) return NULL
;
334 lstrcpyW( cache_dir
, home_dir
);
335 lstrcatW( cache_dir
, L
"\\.cache" );
336 cache_dir
[1] = '\\'; /* change \??\ into \\?\ */
340 if (ensure_exists
&& !CreateDirectoryW( cache_dir
, NULL
) && GetLastError() != ERROR_ALREADY_EXISTS
)
342 WARN( "%s does not exist and could not be created (%u)\n", debugstr_w(cache_dir
), GetLastError() );
343 heap_free( cache_dir
);
347 size
= lstrlenW( cache_dir
) + ARRAY_SIZE(L
"\\wine") + lstrlenW( addon
->file_name
) + 1;
348 if (!(ret
= heap_alloc( size
* sizeof(WCHAR
) )))
350 heap_free( cache_dir
);
353 lstrcpyW( ret
, cache_dir
);
354 lstrcatW( ret
, L
"\\wine" );
355 heap_free( cache_dir
);
357 if (ensure_exists
&& !CreateDirectoryW( ret
, NULL
) && GetLastError() != ERROR_ALREADY_EXISTS
)
359 WARN( "%s does not exist and could not be created (%u)\n", debugstr_w(ret
), GetLastError() );
363 len
= lstrlenW( ret
);
365 lstrcpyW( ret
+ len
, addon
->file_name
);
367 TRACE( "got %s\n", debugstr_w(ret
) );
371 static enum install_res
install_from_cache(void)
373 WCHAR
*cache_file_name
;
374 enum install_res res
;
376 cache_file_name
= get_cache_file_name(FALSE
);
380 if(!sha_check(cache_file_name
)) {
381 WARN("could not validate checksum\n");
382 DeleteFileW(cache_file_name
);
383 heap_free(cache_file_name
);
387 res
= install_file(cache_file_name
);
388 heap_free(cache_file_name
);
392 static IInternetBindInfo InstallCallbackBindInfo
;
394 static HRESULT WINAPI
InstallCallback_QueryInterface(IBindStatusCallback
*iface
,
395 REFIID riid
, void **ppv
)
397 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IBindStatusCallback
, riid
)) {
402 if(IsEqualGUID(&IID_IInternetBindInfo
, riid
)) {
403 TRACE("IID_IInternetBindInfo\n");
404 *ppv
= &InstallCallbackBindInfo
;
411 static ULONG WINAPI
InstallCallback_AddRef(IBindStatusCallback
*iface
)
416 static ULONG WINAPI
InstallCallback_Release(IBindStatusCallback
*iface
)
421 static HRESULT WINAPI
InstallCallback_OnStartBinding(IBindStatusCallback
*iface
,
422 DWORD dwReserved
, IBinding
*pib
)
424 set_status(IDS_DOWNLOADING
);
426 IBinding_AddRef(pib
);
432 static HRESULT WINAPI
InstallCallback_GetPriority(IBindStatusCallback
*iface
,
438 static HRESULT WINAPI
InstallCallback_OnLowResource(IBindStatusCallback
*iface
,
444 static HRESULT WINAPI
InstallCallback_OnProgress(IBindStatusCallback
*iface
, ULONG ulProgress
,
445 ULONG ulProgressMax
, ULONG ulStatusCode
, LPCWSTR szStatusText
)
447 HWND progress
= GetDlgItem(install_dialog
, ID_DWL_PROGRESS
);
450 SendMessageW(progress
, PBM_SETRANGE32
, 0, ulProgressMax
);
452 SendMessageW(progress
, PBM_SETPOS
, ulProgress
, 0);
457 static HRESULT WINAPI
InstallCallback_OnStopBinding(IBindStatusCallback
*iface
,
458 HRESULT hresult
, LPCWSTR szError
)
461 IBinding_Release(dwl_binding
);
465 if(FAILED(hresult
)) {
466 if(hresult
== E_ABORT
)
467 TRACE("Binding aborted\n");
469 ERR("Binding failed %08x\n", hresult
);
474 ERR("No MSI file\n");
478 set_status(IDS_INSTALLING
);
479 EnableWindow(GetDlgItem(install_dialog
, IDCANCEL
), 0);
481 if(sha_check(msi_file
)) {
482 WCHAR
*cache_file_name
;
484 install_file(msi_file
);
486 cache_file_name
= get_cache_file_name(TRUE
);
487 if(cache_file_name
) {
488 CopyFileW(msi_file
, cache_file_name
, FALSE
);
489 heap_free(cache_file_name
);
494 if(LoadStringW(hInst
, IDS_INVALID_SHA
, message
, ARRAY_SIZE(message
)))
495 MessageBoxW(NULL
, message
, NULL
, MB_ICONERROR
);
498 DeleteFileW(msi_file
);
502 EndDialog(install_dialog
, 0);
506 static HRESULT WINAPI
InstallCallback_GetBindInfo(IBindStatusCallback
*iface
,
507 DWORD
* grfBINDF
, BINDINFO
* pbindinfo
)
511 *grfBINDF
= BINDF_ASYNCHRONOUS
;
515 static HRESULT WINAPI
InstallCallback_OnDataAvailable(IBindStatusCallback
*iface
, DWORD grfBSCF
,
516 DWORD dwSize
, FORMATETC
* pformatetc
, STGMEDIUM
* pstgmed
)
519 msi_file
= heap_strdupW(pstgmed
->u
.lpszFileName
);
520 TRACE("got file name %s\n", debugstr_w(msi_file
));
526 static HRESULT WINAPI
InstallCallback_OnObjectAvailable(IBindStatusCallback
*iface
,
527 REFIID riid
, IUnknown
* punk
)
533 static const IBindStatusCallbackVtbl InstallCallbackVtbl
= {
534 InstallCallback_QueryInterface
,
535 InstallCallback_AddRef
,
536 InstallCallback_Release
,
537 InstallCallback_OnStartBinding
,
538 InstallCallback_GetPriority
,
539 InstallCallback_OnLowResource
,
540 InstallCallback_OnProgress
,
541 InstallCallback_OnStopBinding
,
542 InstallCallback_GetBindInfo
,
543 InstallCallback_OnDataAvailable
,
544 InstallCallback_OnObjectAvailable
547 static IBindStatusCallback InstallCallback
= { &InstallCallbackVtbl
};
549 static HRESULT WINAPI
InstallCallbackBindInfo_QueryInterface(IInternetBindInfo
*iface
, REFIID riid
, void **ppv
)
551 return IBindStatusCallback_QueryInterface(&InstallCallback
, riid
, ppv
);
554 static ULONG WINAPI
InstallCallbackBindInfo_AddRef(IInternetBindInfo
*iface
)
559 static ULONG WINAPI
InstallCallbackBindInfo_Release(IInternetBindInfo
*iface
)
564 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindInfo(IInternetBindInfo
*iface
, DWORD
*bindf
, BINDINFO
*bindinfo
)
570 static HRESULT WINAPI
InstallCallbackBindInfo_GetBindString(IInternetBindInfo
*iface
, ULONG string_type
,
571 WCHAR
**strs
, ULONG cnt
, ULONG
*fetched
)
573 switch(string_type
) {
574 case BINDSTRING_USER_AGENT
:
575 TRACE("BINDSTRING_USER_AGENT\n");
577 *strs
= CoTaskMemAlloc(sizeof(L
"Wine Addon Downloader"));
579 return E_OUTOFMEMORY
;
581 lstrcpyW(*strs
, L
"Wine Addon Downloader");
589 static const IInternetBindInfoVtbl InstallCallbackBindInfoVtbl
= {
590 InstallCallbackBindInfo_QueryInterface
,
591 InstallCallbackBindInfo_AddRef
,
592 InstallCallbackBindInfo_Release
,
593 InstallCallbackBindInfo_GetBindInfo
,
594 InstallCallbackBindInfo_GetBindString
597 static IInternetBindInfo InstallCallbackBindInfo
= { &InstallCallbackBindInfoVtbl
};
599 static void append_url_params( WCHAR
*url
)
601 DWORD size
= INTERNET_MAX_URL_LENGTH
* sizeof(WCHAR
);
602 DWORD len
= lstrlenW(url
);
604 lstrcpyW(url
+len
, L
"?arch=");
605 len
+= lstrlenW(L
"?arch=");
606 len
+= MultiByteToWideChar(CP_ACP
, 0, GECKO_ARCH
, sizeof(GECKO_ARCH
),
607 url
+len
, size
/sizeof(WCHAR
)-len
)-1;
608 lstrcpyW(url
+len
, L
"&v=");
609 len
+= lstrlenW(L
"&v=");
610 len
+= MultiByteToWideChar(CP_ACP
, 0, addon
->version
, -1, url
+len
, size
/sizeof(WCHAR
)-len
)-1;
611 lstrcpyW(url
+len
, L
"&winev=");
612 len
+= lstrlenW(L
"&winev=");
613 MultiByteToWideChar(CP_ACP
, 0, wine_get_version(), -1, url
+len
, size
/sizeof(WCHAR
)-len
);
616 static LPWSTR
get_url(void)
618 DWORD size
= INTERNET_MAX_URL_LENGTH
*sizeof(WCHAR
);
619 WCHAR
*url
, *config_key
;
624 static const WCHAR httpW
[] = {'h','t','t','p'};
626 url
= heap_alloc(size
);
627 returned_size
= size
;
629 hkey
= open_config_key();
632 config_key
= heap_strdupAtoW(addon
->url_config_key
);
633 res
= RegQueryValueExW(hkey
, config_key
, NULL
, &type
, (LPBYTE
)url
, &returned_size
);
634 heap_free(config_key
);
636 if(res
== ERROR_SUCCESS
&& type
== REG_SZ
) goto found
;
639 MultiByteToWideChar( CP_ACP
, 0, addon
->url_default
, -1, url
, size
/ sizeof(WCHAR
) );
642 if (returned_size
> sizeof(httpW
) && !memcmp(url
, httpW
, sizeof(httpW
))) append_url_params( url
);
644 TRACE("Got URL %s\n", debugstr_w(url
));
648 static BOOL
start_download(void)
655 hres
= CreateURLMoniker(NULL
, url
, &mon
);
659 hres
= CreateAsyncBindCtx(0, &InstallCallback
, NULL
, &bind_ctx
);
660 if(SUCCEEDED(hres
)) {
661 hres
= IMoniker_BindToStorage(mon
, bind_ctx
, NULL
, &IID_IUnknown
, (void**)&tmp
);
662 IBindCtx_Release(bind_ctx
);
664 IMoniker_Release(mon
);
669 IUnknown_Release(tmp
);
673 static void run_winebrowser(const WCHAR
*url
)
675 PROCESS_INFORMATION pi
;
682 url_len
= lstrlenW(url
);
684 len
= GetSystemDirectoryW(app
, MAX_PATH
- ARRAY_SIZE(L
"\\winebrowser.exe"));
685 lstrcpyW(app
+len
, L
"\\winebrowser.exe");
686 len
+= ARRAY_SIZE(L
"\\winebrowser.exe") - 1;
688 args
= heap_alloc((len
+1+url_len
)*sizeof(WCHAR
));
692 memcpy(args
, app
, len
*sizeof(WCHAR
));
694 memcpy(args
+len
, url
, (url_len
+1) * sizeof(WCHAR
));
696 TRACE("starting %s\n", debugstr_w(args
));
698 memset(&si
, 0, sizeof(si
));
700 ret
= CreateProcessW(app
, args
, NULL
, NULL
, FALSE
, DETACHED_PROCESS
, NULL
, NULL
, &si
, &pi
);
703 CloseHandle(pi
.hThread
);
704 CloseHandle(pi
.hProcess
);
708 static INT_PTR CALLBACK
installer_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
712 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_HIDE
);
713 install_dialog
= hwnd
;
717 switch (((NMHDR
*)lParam
)->code
)
721 if (wParam
== ID_DWL_STATUS
)
722 run_winebrowser(((NMLINK
*)lParam
)->item
.szUrl
);
731 IBinding_Abort(dwl_binding
);
736 ShowWindow(GetDlgItem(hwnd
, ID_DWL_PROGRESS
), SW_SHOW
);
737 EnableWindow(GetDlgItem(hwnd
, ID_DWL_INSTALL
), 0);
738 if(!start_download())
739 EndDialog(install_dialog
, 0);
746 BOOL
install_addon(addon_t addon_type
)
751 addon
= addons_info
+addon_type
;
753 p_wine_get_dos_file_name
= (void *)GetProcAddress(GetModuleHandleW(L
"kernel32.dll"), "wine_get_dos_file_name");
756 * Try to find addon .msi file in following order:
757 * - directory stored in $dir_config_key value of HKCU/Software/Wine/$config_key key
758 * - $datadir/$addon_subdir/
759 * - $INSTALL_DATADIR/wine/$addon_subdir/
760 * - /usr/share/wine/$addon_subdir/
761 * - /opt/wine/$addon_subdir/
762 * - download from URL stored in $url_config_key value of HKCU/Software/Wine/$config_key key
764 if (install_from_registered_dir() == INSTALL_NEXT
765 && install_from_default_dir() == INSTALL_NEXT
766 && install_from_cache() == INSTALL_NEXT
767 && (url
= get_url()))
768 DialogBoxW(hInst
, addon
->dialog_template
, 0, installer_proc
);