tools: Register the MSI service from wine.inf.
[wine] / dlls / appwiz.cpl / addons.c
1 /*
2  * Copyright 2006-2010 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "cpl.h"
36 #include "winreg.h"
37 #include "ole2.h"
38 #include "commctrl.h"
39 #include "advpub.h"
40 #include "wininet.h"
41 #include "shellapi.h"
42 #include "urlmon.h"
43 #include "msi.h"
44
45 #include "appwiz.h"
46 #include "res.h"
47
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50 #include "wine/library.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(appwizcpl);
53
54 #define GECKO_VERSION "1.2.0"
55
56 #ifdef __i386__
57 #define ARCH_STRING "x86"
58 #define GECKO_SHA "6964d1877668ab7da07a60f6dcf23fb0e261a808"
59 #elif defined(__x86_64__)
60 #define ARCH_STRING "x86_64"
61 #define GECKO_SHA "3ac3c3e880e40f7763824866372ffc56128f0abd"
62 #else
63 #define ARCH_STRING ""
64 #define GECKO_SHA "???"
65 #endif
66
67 #define GECKO_FILE_NAME "wine_gecko-" GECKO_VERSION "-" ARCH_STRING ".msi"
68
69 static const WCHAR mshtml_keyW[] =
70     {'S','o','f','t','w','a','r','e',
71      '\\','W','i','n','e',
72      '\\','M','S','H','T','M','L',0};
73
74 static HWND install_dialog = NULL;
75 static LPWSTR url = NULL;
76
77 static inline char *heap_strdupWtoA(LPCWSTR str)
78 {
79     char *ret = NULL;
80
81     if(str) {
82         DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
83         ret = heap_alloc(size);
84         WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
85     }
86
87     return ret;
88 }
89
90 /* SHA definitions are copied from advapi32. They aren't available in headers. */
91
92 typedef struct {
93    ULONG Unknown[6];
94    ULONG State[5];
95    ULONG Count[2];
96    UCHAR Buffer[64];
97 } SHA_CTX, *PSHA_CTX;
98
99 void WINAPI A_SHAInit(PSHA_CTX);
100 void WINAPI A_SHAUpdate(PSHA_CTX,const unsigned char*,UINT);
101 void WINAPI A_SHAFinal(PSHA_CTX,PULONG);
102
103 static BOOL sha_check(const WCHAR *file_name)
104 {
105     const unsigned char *file_map;
106     HANDLE file, map;
107     ULONG sha[5];
108     char buf[2*sizeof(sha)+1];
109     SHA_CTX ctx;
110     DWORD size, i;
111
112     file = CreateFileW(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
113     if(file == INVALID_HANDLE_VALUE)
114         return FALSE;
115
116     size = GetFileSize(file, NULL);
117
118     map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
119     CloseHandle(file);
120     if(!map)
121         return FALSE;
122
123     file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
124     CloseHandle(map);
125     if(!file_map)
126         return FALSE;
127
128     A_SHAInit(&ctx);
129     A_SHAUpdate(&ctx, file_map, size);
130     A_SHAFinal(&ctx, sha);
131
132     UnmapViewOfFile(file_map);
133
134     for(i=0; i < sizeof(sha); i++)
135         sprintf(buf + i*2, "%02x", *((unsigned char*)sha+i));
136
137     if(strcmp(buf, GECKO_SHA)) {
138         WCHAR message[256];
139
140         WARN("Got %s, expected %s\n", buf, GECKO_SHA);
141
142         if(LoadStringW(hInst, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR)))
143             MessageBoxW(NULL, message, NULL, MB_ICONERROR);
144
145         return FALSE;
146     }
147
148     return TRUE;
149 }
150
151 static void set_status(DWORD id)
152 {
153     HWND status = GetDlgItem(install_dialog, ID_DWL_STATUS);
154     WCHAR buf[64];
155
156     LoadStringW(hInst, id, buf, sizeof(buf)/sizeof(WCHAR));
157     SendMessageW(status, WM_SETTEXT, 0, (LPARAM)buf);
158 }
159
160 static BOOL install_file(const WCHAR *file_name)
161 {
162     ULONG res;
163
164     res = MsiInstallProductW(file_name, NULL);
165     if(res != ERROR_SUCCESS) {
166         ERR("MsiInstallProduct failed: %u\n", res);
167         return FALSE;
168     }
169
170     return TRUE;
171 }
172
173 static BOOL install_from_unix_file(const char *file_name)
174 {
175     LPWSTR dos_file_name;
176     int fd;
177     BOOL ret;
178
179     static WCHAR * (CDECL *wine_get_dos_file_name)(const char*);
180     static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
181
182     fd = open(file_name, O_RDONLY);
183     if(fd == -1) {
184         TRACE("%s not found\n", debugstr_a(file_name));
185         return FALSE;
186     }
187
188     close(fd);
189
190     if(!wine_get_dos_file_name)
191         wine_get_dos_file_name = (void*)GetProcAddress(GetModuleHandleW(kernel32W), "wine_get_dos_file_name");
192
193     if(wine_get_dos_file_name) { /* Wine UNIX mode */
194         dos_file_name = wine_get_dos_file_name(file_name);
195         if(!dos_file_name) {
196             ERR("Could not get dos file name of %s\n", debugstr_a(file_name));
197             return FALSE;
198         }
199     } else { /* Windows mode */
200         UINT res;
201         WARN("Could not get wine_get_dos_file_name function, calling install_cab directly.\n");
202         res = MultiByteToWideChar( CP_ACP, 0, file_name, -1, 0, 0);
203         dos_file_name = heap_alloc (res*sizeof(WCHAR));
204         MultiByteToWideChar( CP_ACP, 0, file_name, -1, dos_file_name, res);
205     }
206
207     ret = install_file(dos_file_name);
208
209     heap_free(dos_file_name);
210     return ret;
211 }
212
213 static BOOL install_from_registered_dir(void)
214 {
215     char *file_name;
216     HKEY hkey;
217     DWORD res, type, size = MAX_PATH;
218     BOOL ret;
219
220     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
221     res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
222     if(res != ERROR_SUCCESS)
223         return FALSE;
224
225     file_name = heap_alloc(size+sizeof(GECKO_FILE_NAME));
226     res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
227     if(res == ERROR_MORE_DATA) {
228         file_name = heap_realloc(file_name, size+sizeof(GECKO_FILE_NAME));
229         res = RegGetValueA(hkey, NULL, "GeckoCabDir", RRF_RT_ANY, &type, (PBYTE)file_name, &size);
230     }
231     RegCloseKey(hkey);
232     if(res != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) {
233         heap_free(file_name);
234         return FALSE;
235     }
236
237     strcat(file_name, GECKO_FILE_NAME);
238
239     TRACE("Trying %s\n", debugstr_a(file_name));
240
241     ret = install_from_unix_file(file_name);
242
243     heap_free(file_name);
244     return ret;
245 }
246
247 static BOOL install_from_default_dir(void)
248 {
249     const char *data_dir, *subdir;
250     char *file_name;
251     int len, len2;
252     BOOL ret;
253
254     if((data_dir = wine_get_data_dir()))
255         subdir = "/gecko/";
256     else if((data_dir = wine_get_build_dir()))
257         subdir = "/../gecko/";
258     else
259         return FALSE;
260
261     len = strlen(data_dir);
262     len2 = strlen(subdir);
263
264     file_name = heap_alloc(len+len2+sizeof(GECKO_FILE_NAME));
265     memcpy(file_name, data_dir, len);
266     memcpy(file_name+len, subdir, len2);
267     memcpy(file_name+len+len2, GECKO_FILE_NAME, sizeof(GECKO_FILE_NAME));
268
269     ret = install_from_unix_file(file_name);
270
271     heap_free(file_name);
272
273     if (!ret)
274         ret = install_from_unix_file(INSTALL_DATADIR "/wine/gecko/" GECKO_FILE_NAME);
275     if (!ret && strcmp(INSTALL_DATADIR, "/usr/share"))
276         ret = install_from_unix_file("/usr/share/wine/gecko/" GECKO_FILE_NAME);
277     return ret;
278 }
279
280 static HRESULT WINAPI InstallCallback_QueryInterface(IBindStatusCallback *iface,
281         REFIID riid, void **ppv)
282 {
283     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IBindStatusCallback, riid)) {
284         *ppv = iface;
285         return S_OK;
286     }
287
288     return E_INVALIDARG;
289 }
290
291 static ULONG WINAPI InstallCallback_AddRef(IBindStatusCallback *iface)
292 {
293     return 2;
294 }
295
296 static ULONG WINAPI InstallCallback_Release(IBindStatusCallback *iface)
297 {
298     return 1;
299 }
300
301 static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
302         DWORD dwReserved, IBinding *pib)
303 {
304     set_status(IDS_DOWNLOADING);
305     return S_OK;
306 }
307
308 static HRESULT WINAPI InstallCallback_GetPriority(IBindStatusCallback *iface,
309         LONG *pnPriority)
310 {
311     return E_NOTIMPL;
312 }
313
314 static HRESULT WINAPI InstallCallback_OnLowResource(IBindStatusCallback *iface,
315        DWORD dwReserved)
316 {
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI InstallCallback_OnProgress(IBindStatusCallback *iface, ULONG ulProgress,
321         ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
322 {
323     HWND progress = GetDlgItem(install_dialog, ID_DWL_PROGRESS);
324
325     if(ulProgressMax)
326         SendMessageW(progress, PBM_SETRANGE32, 0, ulProgressMax);
327     if(ulProgress)
328         SendMessageW(progress, PBM_SETPOS, ulProgress, 0);
329
330     return S_OK;
331 }
332
333 static HRESULT WINAPI InstallCallback_OnStopBinding(IBindStatusCallback *iface,
334         HRESULT hresult, LPCWSTR szError)
335 {
336     if(FAILED(hresult)) {
337         ERR("Binding failed %08x\n", hresult);
338         return S_OK;
339     }
340
341     set_status(IDS_INSTALLING);
342     return S_OK;
343 }
344
345 static HRESULT WINAPI InstallCallback_GetBindInfo(IBindStatusCallback *iface,
346         DWORD* grfBINDF, BINDINFO* pbindinfo)
347 {
348     /* FIXME */
349     *grfBINDF = 0;
350     return S_OK;
351 }
352
353 static HRESULT WINAPI InstallCallback_OnDataAvailable(IBindStatusCallback *iface, DWORD grfBSCF,
354         DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
355 {
356     ERR("\n");
357     return E_NOTIMPL;
358 }
359
360 static HRESULT WINAPI InstallCallback_OnObjectAvailable(IBindStatusCallback *iface,
361         REFIID riid, IUnknown* punk)
362 {
363     ERR("\n");
364     return E_NOTIMPL;
365 }
366
367 static const IBindStatusCallbackVtbl InstallCallbackVtbl = {
368     InstallCallback_QueryInterface,
369     InstallCallback_AddRef,
370     InstallCallback_Release,
371     InstallCallback_OnStartBinding,
372     InstallCallback_GetPriority,
373     InstallCallback_OnLowResource,
374     InstallCallback_OnProgress,
375     InstallCallback_OnStopBinding,
376     InstallCallback_GetBindInfo,
377     InstallCallback_OnDataAvailable,
378     InstallCallback_OnObjectAvailable
379 };
380
381 static IBindStatusCallback InstallCallback = { &InstallCallbackVtbl };
382
383 static LPWSTR get_url(void)
384 {
385     HKEY hkey;
386     DWORD res, type;
387     DWORD size = INTERNET_MAX_URL_LENGTH*sizeof(WCHAR);
388     DWORD returned_size;
389     LPWSTR url;
390
391     static const WCHAR wszGeckoUrl[] = {'G','e','c','k','o','U','r','l',0};
392     static const WCHAR httpW[] = {'h','t','t','p'};
393     static const WCHAR arch_formatW[] = {'?','a','r','c','h','='};
394     static const WCHAR v_formatW[] = {'&','v','='};
395
396     /* @@ Wine registry key: HKCU\Software\Wine\MSHTML */
397     res = RegOpenKeyW(HKEY_CURRENT_USER, mshtml_keyW, &hkey);
398     if(res != ERROR_SUCCESS)
399         return NULL;
400
401     url = heap_alloc(size);
402     returned_size = size;
403
404     res = RegQueryValueExW(hkey, wszGeckoUrl, NULL, &type, (LPBYTE)url, &returned_size);
405     RegCloseKey(hkey);
406     if(res != ERROR_SUCCESS || type != REG_SZ) {
407         heap_free(url);
408         return NULL;
409     }
410
411     if(returned_size > sizeof(httpW) && !memcmp(url, httpW, sizeof(httpW))) {
412         DWORD len;
413
414         len = strlenW(url);
415         memcpy(url+len, arch_formatW, sizeof(arch_formatW));
416         len += sizeof(arch_formatW)/sizeof(WCHAR);
417         len += MultiByteToWideChar(CP_ACP, 0, ARCH_STRING, sizeof(ARCH_STRING), url+len, size/sizeof(WCHAR)-len)-1;
418         memcpy(url+len, v_formatW, sizeof(v_formatW));
419         len += sizeof(v_formatW)/sizeof(WCHAR);
420         MultiByteToWideChar(CP_ACP, 0, GECKO_VERSION, -1, url+len, size/sizeof(WCHAR)-len);
421     }
422
423     TRACE("Got URL %s\n", debugstr_w(url));
424     return url;
425 }
426
427 static DWORD WINAPI download_proc(PVOID arg)
428 {
429     WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
430     HRESULT hres;
431
432     GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
433     GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);
434
435     TRACE("using temp file %s\n", debugstr_w(tmp_file));
436
437     hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
438     if(FAILED(hres)) {
439         ERR("URLDownloadToFile failed: %08x\n", hres);
440         return 0;
441     }
442
443     if(sha_check(tmp_file))
444         install_file(tmp_file);
445     DeleteFileW(tmp_file);
446     EndDialog(install_dialog, 0);
447     return 0;
448 }
449
450 static INT_PTR CALLBACK installer_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
451 {
452     switch(msg) {
453     case WM_INITDIALOG:
454         ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_HIDE);
455         install_dialog = hwnd;
456         return TRUE;
457
458     case WM_COMMAND:
459         switch(wParam) {
460         case IDCANCEL:
461             EndDialog(hwnd, 0);
462             return FALSE;
463
464         case ID_DWL_INSTALL:
465             ShowWindow(GetDlgItem(hwnd, ID_DWL_PROGRESS), SW_SHOW);
466             EnableWindow(GetDlgItem(hwnd, ID_DWL_INSTALL), 0);
467             EnableWindow(GetDlgItem(hwnd, IDCANCEL), 0); /* FIXME */
468             CreateThread(NULL, 0, download_proc, NULL, 0, NULL);
469             return FALSE;
470         }
471     }
472
473     return FALSE;
474 }
475
476 BOOL install_wine_gecko(void)
477 {
478     if(!*ARCH_STRING)
479         return FALSE;
480
481     /*
482      * Try to find Gecko .cab file in following order:
483      * - directory stored in GeckoCabDir value of HKCU/Wine/Software/MSHTML key
484      * - $datadir/gecko/
485      * - $INSTALL_DATADIR/wine/gecko/
486      * - /usr/share/wine/gecko/
487      * - download from URL stored in GeckoUrl value of HKCU/Wine/Software/MSHTML key
488      */
489     if(!install_from_registered_dir()
490        && !install_from_default_dir()
491        && (url = get_url()))
492         DialogBoxW(hInst, MAKEINTRESOURCEW(ID_DWL_DIALOG), 0, installer_proc);
493
494     heap_free(url);
495     url = NULL;
496     return TRUE;
497 }