[RAPPS] Some fixes to our completely broken cert pinning attempt.
[reactos.git] / base / applications / rapps / loaddlg.cpp
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * FILE: base/applications/rapps/loaddlg.cpp
5 * PURPOSE: Displaying a download dialog
6 * COPYRIGHT: Copyright 2001 John R. Sheets (for CodeWeavers)
7 * Copyright 2004 Mike McCormack (for CodeWeavers)
8 * Copyright 2005 Ge van Geldorp (gvg@reactos.org)
9 * Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
10 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
11 * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
12 */
13
14 /*
15 * Based on Wine dlls/shdocvw/shdocvw_main.c
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31 #include "rapps.h"
32
33 #include <shlobj_undoc.h>
34 #include <shlguid_undoc.h>
35
36 #include <atlbase.h>
37 #include <atlcom.h>
38 #include <atlwin.h>
39 #include <wininet.h>
40 #include <shellutils.h>
41
42 #include <rosctrls.h>
43 #include <windowsx.h>
44
45 #include "rosui.h"
46 #include "dialogs.h"
47 #include "misc.h"
48
49 #ifdef USE_CERT_PINNING
50 #define CERT_ISSUER_INFO "US\r\nLet's Encrypt\r\nLet's Encrypt Authority X3"
51 #define CERT_SUBJECT_INFO "svn.reactos.org"
52 #endif
53
54 enum DownloadStatus
55 {
56 DLSTATUS_WAITING = IDS_STATUS_WAITING,
57 DLSTATUS_DOWNLOADING = IDS_STATUS_DOWNLOADING,
58 DLSTATUS_WAITING_INSTALL = IDS_STATUS_DOWNLOADED,
59 DLSTATUS_INSTALLING = IDS_STATUS_INSTALLING,
60 DLSTATUS_INSTALLED = IDS_STATUS_INSTALLED,
61 DLSTATUS_FINISHED = IDS_STATUS_FINISHED
62 };
63
64 ATL::CStringW LoadStatusString(DownloadStatus StatusParam)
65 {
66 ATL::CStringW szString;
67 szString.LoadStringW(StatusParam);
68 return szString;
69 }
70
71 struct DownloadInfo
72 {
73 DownloadInfo() {}
74 DownloadInfo(const CAvailableApplicationInfo& AppInfo)
75 :szUrl(AppInfo.m_szUrlDownload), szName(AppInfo.m_szName), szSHA1(AppInfo.m_szSHA1)
76 {
77 }
78
79 ATL::CStringW szUrl;
80 ATL::CStringW szName;
81 ATL::CStringW szSHA1;
82 };
83
84 struct DownloadParam
85 {
86 DownloadParam() : Dialog(NULL), AppInfo(), szCaption(NULL) {}
87 DownloadParam(HWND dlg, const ATL::CSimpleArray<DownloadInfo> &info, LPCWSTR caption)
88 : Dialog(dlg), AppInfo(info), szCaption(caption)
89 {
90 }
91
92 HWND Dialog;
93 ATL::CSimpleArray<DownloadInfo> AppInfo;
94 LPCWSTR szCaption;
95 };
96
97
98 class CDownloadDialog :
99 public CComObjectRootEx<CComMultiThreadModelNoCS>,
100 public IBindStatusCallback
101 {
102 HWND m_hDialog;
103 PBOOL m_pbCancelled;
104 BOOL m_UrlHasBeenCopied;
105
106 public:
107 ~CDownloadDialog()
108 {
109 //DestroyWindow(m_hDialog);
110 }
111
112 HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
113 {
114 m_hDialog = Dlg;
115 m_pbCancelled = pbCancelled;
116 m_UrlHasBeenCopied = FALSE;
117 return S_OK;
118 }
119
120 virtual HRESULT STDMETHODCALLTYPE OnStartBinding(
121 DWORD dwReserved,
122 IBinding *pib)
123 {
124 return S_OK;
125 }
126
127 virtual HRESULT STDMETHODCALLTYPE GetPriority(
128 LONG *pnPriority)
129 {
130 return S_OK;
131 }
132
133 virtual HRESULT STDMETHODCALLTYPE OnLowResource(
134 DWORD reserved)
135 {
136 return S_OK;
137 }
138
139 virtual HRESULT STDMETHODCALLTYPE OnProgress(
140 ULONG ulProgress,
141 ULONG ulProgressMax,
142 ULONG ulStatusCode,
143 LPCWSTR szStatusText)
144 {
145 HWND Item;
146 LONG r;
147
148 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_PROGRESS);
149 if (Item)
150 {
151 WCHAR szProgress[100];
152
153 /* format the bits and bytes into pretty and accessible units... */
154 StrFormatByteSizeW(ulProgress, szProgress, _countof(szProgress));
155
156 /* use our subclassed progress bar text subroutine */
157 ATL::CStringW m_ProgressText;
158
159 if (ulProgressMax)
160 {
161 /* total size is known */
162 WCHAR szProgressMax[100];
163 UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
164
165 /* send the current progress to the progress bar */
166 SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
167
168 /* format total download size */
169 StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
170
171 /* generate the text on progress bar */
172 m_ProgressText.Format(L"%u%% \x2014 %ls / %ls",
173 uiPercentage,
174 szProgress,
175 szProgressMax);
176 }
177 else
178 {
179 /* send the current progress to the progress bar */
180 SendMessageW(Item, PBM_SETPOS, 0, 0);
181
182 /* total size is not known, display only current size */
183 m_ProgressText.Format(L"%ls...",
184 szProgress);
185 }
186 /* and finally display it */
187 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetString());
188 }
189
190 Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
191 if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
192 {
193 DWORD len = wcslen(szStatusText) + 1;
194 ATL::CStringW buf;
195
196 /* beautify our url for display purposes */
197 if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
198 {
199 /* just use the original */
200 buf.ReleaseBuffer();
201 buf = szStatusText;
202 }
203 else
204 {
205 buf.ReleaseBuffer();
206 }
207
208 /* paste it into our dialog and don't do it again in this instance */
209 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetString());
210 m_UrlHasBeenCopied = TRUE;
211 }
212
213 SetLastError(ERROR_SUCCESS);
214 r = GetWindowLongPtrW(m_hDialog, GWLP_USERDATA);
215 if (r || GetLastError() != ERROR_SUCCESS)
216 {
217 *m_pbCancelled = TRUE;
218 return E_ABORT;
219 }
220
221 return S_OK;
222 }
223
224 virtual HRESULT STDMETHODCALLTYPE OnStopBinding(
225 HRESULT hresult,
226 LPCWSTR szError)
227 {
228 return S_OK;
229 }
230
231 virtual HRESULT STDMETHODCALLTYPE GetBindInfo(
232 DWORD *grfBINDF,
233 BINDINFO *pbindinfo)
234 {
235 return S_OK;
236 }
237
238 virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(
239 DWORD grfBSCF,
240 DWORD dwSize,
241 FORMATETC *pformatetc,
242 STGMEDIUM *pstgmed)
243 {
244 return S_OK;
245 }
246
247 virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(
248 REFIID riid,
249 IUnknown *punk)
250 {
251 return S_OK;
252 }
253
254 BEGIN_COM_MAP(CDownloadDialog)
255 COM_INTERFACE_ENTRY_IID(IID_IBindStatusCallback, IBindStatusCallback)
256 END_COM_MAP()
257 };
258
259 class CDowloadingAppsListView
260 : public CUiWindow<CListView>
261 {
262 public:
263 HWND Create(HWND hwndParent)
264 {
265 RECT r = {10, 150, 320, 350};
266 const DWORD style = WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL
267 | LVS_SHOWSELALWAYS | LVS_NOSORTHEADER | LVS_NOCOLUMNHEADER;
268
269 HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE);
270
271 AddColumn(0, 150, LVCFMT_LEFT);
272 AddColumn(1, 120, LVCFMT_LEFT);
273
274 return hwnd;
275 }
276
277 VOID LoadList(ATL::CSimpleArray<DownloadInfo> arrInfo)
278 {
279 for (INT i = 0; i < arrInfo.GetSize(); ++i)
280 {
281 AddRow(i, arrInfo[i].szName.GetString(), DLSTATUS_WAITING);
282 }
283 }
284
285 VOID SetDownloadStatus(INT ItemIndex, DownloadStatus Status)
286 {
287 HWND hListView = GetWindow();
288 ATL::CStringW szBuffer = LoadStatusString(Status);
289 ListView_SetItemText(hListView, ItemIndex, 1, const_cast<LPWSTR>(szBuffer.GetString()));
290 }
291
292 BOOL AddItem(INT ItemIndex, LPWSTR lpText)
293 {
294 LVITEMW Item;
295
296 ZeroMemory(&Item, sizeof(Item));
297
298 Item.mask = LVIF_TEXT | LVIF_STATE;
299 Item.pszText = lpText;
300 Item.iItem = ItemIndex;
301
302 return InsertItem(&Item);
303 }
304
305 VOID AddRow(INT RowIndex, LPCWSTR szAppName, const DownloadStatus Status)
306 {
307 ATL::CStringW szStatus = LoadStatusString(Status);
308 AddItem(RowIndex,
309 const_cast<LPWSTR>(szAppName));
310 SetDownloadStatus(RowIndex, Status);
311 }
312
313 BOOL AddColumn(INT Index, INT Width, INT Format)
314 {
315 LVCOLUMNW Column;
316 ZeroMemory(&Column, sizeof(Column));
317
318 Column.mask = LVCF_FMT | LVCF_WIDTH | LVCF_SUBITEM;
319 Column.iSubItem = Index;
320 Column.cx = Width;
321 Column.fmt = Format;
322
323 return (InsertColumn(Index, &Column) == -1) ? FALSE : TRUE;
324 }
325 };
326
327 extern "C"
328 HRESULT WINAPI CDownloadDialog_Constructor(HWND Dlg, BOOL *pbCancelled, REFIID riid, LPVOID *ppv)
329 {
330 return ShellObjectCreatorInit<CDownloadDialog>(Dlg, pbCancelled, riid, ppv);
331 }
332
333 #ifdef USE_CERT_PINNING
334 static BOOL CertIsValid(HINTERNET hFile, LPWSTR lpszHostName)
335 {
336 DWORD certInfoLength;
337 INTERNET_CERTIFICATE_INFOA certInfo;
338 int ValidFlags = 0;
339
340 /* Despite what the header indicates, the implementation of INTERNET_CERTIFICATE_INFO is not Unicode-aware. */
341 certInfoLength = sizeof(certInfo);
342 if (!InternetQueryOptionA(hFile,
343 INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
344 &certInfo,
345 &certInfoLength))
346 {
347 return FALSE;
348 }
349
350 if (certInfo.lpszSubjectInfo)
351 {
352 if (strcmp(certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) == 0)
353 ValidFlags |= 1;
354 LocalFree(certInfo.lpszSubjectInfo);
355 }
356 if (certInfo.lpszIssuerInfo)
357 {
358 if (strcmp(certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) == 0)
359 ValidFlags |= 2;
360 LocalFree(certInfo.lpszIssuerInfo);
361 }
362 if (certInfo.lpszProtocolName)
363 LocalFree(certInfo.lpszProtocolName);
364 if (certInfo.lpszSignatureAlgName)
365 LocalFree(certInfo.lpszSignatureAlgName);
366 if (certInfo.lpszEncryptionAlgName)
367 LocalFree(certInfo.lpszEncryptionAlgName);
368
369 return ValidFlags == 3;
370 }
371 #endif
372
373 inline VOID MessageBox_LoadString(HWND hMainWnd, INT StringID)
374 {
375 ATL::CString szMsgText;
376 if (szMsgText.LoadStringW(StringID))
377 {
378 MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
379 }
380 }
381
382 // CDownloadManager
383 ATL::CSimpleArray<DownloadInfo> CDownloadManager::AppsToInstallList;
384 CDowloadingAppsListView CDownloadManager::DownloadsListView;
385
386 VOID CDownloadManager::Download(const DownloadInfo &DLInfo, BOOL bIsModal)
387 {
388 AppsToInstallList.RemoveAll();
389 AppsToInstallList.Add(DLInfo);
390 LaunchDownloadDialog(bIsModal);
391 }
392
393 INT_PTR CALLBACK CDownloadManager::DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
394 {
395 static WCHAR szCaption[MAX_PATH];
396
397 switch (uMsg)
398 {
399 case WM_INITDIALOG:
400 {
401 HICON hIconSm, hIconBg;
402 ATL::CStringW szTempCaption;
403
404 hIconBg = (HICON) GetClassLongW(hMainWnd, GCLP_HICON);
405 hIconSm = (HICON) GetClassLongW(hMainWnd, GCLP_HICONSM);
406
407 if (hIconBg && hIconSm)
408 {
409 SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
410 SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
411 }
412
413 SetWindowLongW(Dlg, GWLP_USERDATA, 0);
414 HWND Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
415 if (Item)
416 {
417 // initialize the default values for our nifty progress bar
418 // and subclass it so that it learns to print a status text
419 SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
420 SendMessageW(Item, PBM_SETPOS, 0, 0);
421
422 SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
423 }
424
425 // Add a ListView
426 HWND hListView = DownloadsListView.Create(Dlg);
427 if (!hListView)
428 {
429 return FALSE;
430 }
431 DownloadsListView.LoadList(AppsToInstallList);
432
433 // Get a dlg string for later use
434 GetWindowTextW(Dlg, szCaption, _countof(szCaption));
435
436 // Hide a placeholder from displaying
437 szTempCaption = szCaption;
438 szTempCaption.Replace(L"%ls", L"");
439 SetWindowText(Dlg, szTempCaption.GetString());
440
441 ShowWindow(Dlg, SW_SHOW);
442
443 // Start download process
444 DownloadParam *param = new DownloadParam(Dlg, AppsToInstallList, szCaption);
445 DWORD ThreadId;
446 HANDLE Thread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) param, 0, &ThreadId);
447
448 if (!Thread)
449 {
450 return FALSE;
451 }
452
453 CloseHandle(Thread);
454 AppsToInstallList.RemoveAll();
455 return TRUE;
456 }
457
458 case WM_COMMAND:
459 if (wParam == IDCANCEL)
460 {
461 SetWindowLongW(Dlg, GWLP_USERDATA, 1);
462 PostMessageW(Dlg, WM_CLOSE, 0, 0);
463 }
464 return FALSE;
465
466 case WM_CLOSE:
467 EndDialog(Dlg, 0);
468 //DestroyWindow(Dlg);
469 return TRUE;
470
471 default:
472 return FALSE;
473 }
474 }
475
476 LRESULT CALLBACK CDownloadManager::DownloadProgressProc(HWND hWnd,
477 UINT uMsg,
478 WPARAM wParam,
479 LPARAM lParam,
480 UINT_PTR uIdSubclass,
481 DWORD_PTR dwRefData)
482 {
483 static ATL::CStringW szProgressText;
484
485 switch (uMsg)
486 {
487 case WM_SETTEXT:
488 {
489 if (lParam)
490 {
491 szProgressText = (PCWSTR) lParam;
492 }
493 return TRUE;
494 }
495
496 case WM_ERASEBKGND:
497 case WM_PAINT:
498 {
499 PAINTSTRUCT ps;
500 HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
501 HBITMAP hbmMem;
502 HANDLE hOld;
503 RECT myRect;
504 UINT win_width, win_height;
505
506 GetClientRect(hWnd, &myRect);
507
508 /* grab the progress bar rect size */
509 win_width = myRect.right - myRect.left;
510 win_height = myRect.bottom - myRect.top;
511
512 /* create an off-screen DC for double-buffering */
513 hdcMem = CreateCompatibleDC(hDC);
514 hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
515
516 hOld = SelectObject(hdcMem, hbmMem);
517
518 /* call the original draw code and redirect it to our memory buffer */
519 DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
520
521 /* draw our nifty progress text over it */
522 SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
523 DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
524 &myRect,
525 DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
526 GetSysColor(COLOR_CAPTIONTEXT),
527 GetSysColor(COLOR_3DSHADOW),
528 1, 1);
529
530 /* transfer the off-screen DC to the screen */
531 BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
532
533 /* free the off-screen DC */
534 SelectObject(hdcMem, hOld);
535 DeleteObject(hbmMem);
536 DeleteDC(hdcMem);
537
538 EndPaint(hWnd, &ps);
539 return 0;
540 }
541
542 /* Raymond Chen says that we should safely unsubclass all the things!
543 (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
544
545 case WM_NCDESTROY:
546 {
547 szProgressText.Empty();
548 RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
549 }
550 /* Fall-through */
551 default:
552 return DefSubclassProc(hWnd, uMsg, wParam, lParam);
553 }
554 }
555
556 VOID CDownloadManager::SetProgressMarquee(HWND Item, BOOL Enable)
557 {
558 if (!Item)
559 return;
560
561 DWORD style = GetWindowLongPtr(Item, GWL_STYLE);
562 if (!style)
563 return;
564
565 if (!SetWindowLongPtr(Item, GWL_STYLE, (Enable ? style | PBS_MARQUEE : style & ~PBS_MARQUEE)))
566 return;
567
568 SendMessageW(Item, PBM_SETMARQUEE, Enable, 0);
569 }
570
571 DWORD WINAPI CDownloadManager::ThreadFunc(LPVOID param)
572 {
573 CComPtr<IBindStatusCallback> dl;
574 ATL::CStringW Path;
575 PWSTR p, q;
576
577 HWND hDlg = static_cast<DownloadParam*>(param)->Dialog;
578 HWND Item;
579 INT iAppId;
580
581 ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
582 ULONG dwCurrentBytesRead = 0;
583 ULONG dwStatusLen = sizeof(dwStatus);
584
585 BOOL bCancelled = FALSE;
586 BOOL bTempfile = FALSE;
587 BOOL bCab = FALSE;
588
589 HINTERNET hOpen = NULL;
590 HINTERNET hFile = NULL;
591 HANDLE hOut = INVALID_HANDLE_VALUE;
592
593 unsigned char lpBuffer[4096];
594 LPCWSTR lpszAgent = L"RApps/1.0";
595 URL_COMPONENTS urlComponents;
596 size_t urlLength, filenameLength;
597
598 const ATL::CSimpleArray<DownloadInfo> &InfoArray = static_cast<DownloadParam*>(param)->AppInfo;
599 LPCWSTR szCaption = static_cast<DownloadParam*>(param)->szCaption;
600 ATL::CStringW szNewCaption;
601
602 if (InfoArray.GetSize() <= 0)
603 {
604 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
605 goto end;
606 }
607
608 for (iAppId = 0; iAppId < InfoArray.GetSize(); ++iAppId)
609 {
610 // Reset progress bar
611 Item = GetDlgItem(hDlg, IDC_DOWNLOAD_PROGRESS);
612 if (Item)
613 {
614 SetProgressMarquee(Item, FALSE);
615 SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) L"");
616 SendMessageW(Item, PBM_SETPOS, 0, 0);
617 }
618
619 // Change caption to show the currently downloaded app
620 if (!bCab)
621 {
622 szNewCaption.Format(szCaption, InfoArray[iAppId].szName.GetString());
623 }
624 else
625 {
626 szNewCaption.LoadStringW(IDS_DL_DIALOG_DB_DOWNLOAD_DISP);
627 }
628
629 SetWindowTextW(hDlg, szNewCaption.GetString());
630
631 // build the path for the download
632 p = wcsrchr(InfoArray[iAppId].szUrl.GetString(), L'/');
633 q = wcsrchr(InfoArray[iAppId].szUrl.GetString(), L'?');
634
635 // do we have a final slash separator?
636 if (!p)
637 goto end;
638
639 // prepare the tentative length of the filename, maybe we've to remove part of it later on
640 filenameLength = wcslen(p) * sizeof(WCHAR);
641
642 /* do we have query arguments in the target URL after the filename? account for them
643 (e.g. https://example.org/myfile.exe?no_adware_plz) */
644 if (q && q > p && (q - p) > 0)
645 filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
646
647 // is this URL an update package for RAPPS? if so store it in a different place
648 if (InfoArray[iAppId].szUrl == APPLICATION_DATABASE_URL)
649 {
650 bCab = TRUE;
651 if (!GetStorageDirectory(Path))
652 goto end;
653 }
654 else
655 {
656 Path = SettingsInfo.szDownloadDir;
657 }
658
659 // is the path valid? can we access it?
660 if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
661 {
662 if (!CreateDirectoryW(Path.GetString(), NULL))
663 goto end;
664 }
665
666 // append a \ to the provided file system path, and the filename portion from the URL after that
667 Path += L"\\";
668 Path += (LPWSTR) (p + 1);
669
670 if (!bCab && InfoArray[iAppId].szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
671 {
672 // only open it in case of total correctness
673 if (VerifyInteg(InfoArray[iAppId].szSHA1.GetString(), Path))
674 goto run;
675 }
676
677 // Add the download URL
678 SetDlgItemTextW(hDlg, IDC_DOWNLOAD_STATUS, InfoArray[iAppId].szUrl.GetString());
679
680 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_DOWNLOADING);
681
682 // download it
683 bTempfile = TRUE;
684 CDownloadDialog_Constructor(hDlg, &bCancelled, IID_PPV_ARG(IBindStatusCallback, &dl));
685
686 if (dl == NULL)
687 goto end;
688
689 /* FIXME: this should just be using the system-wide proxy settings */
690 switch (SettingsInfo.Proxy)
691 {
692 case 0: // preconfig
693 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
694 break;
695 case 1: // direct (no proxy)
696 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
697 break;
698 case 2: // use proxy
699 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
700 break;
701 default: // preconfig
702 hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
703 break;
704 }
705
706 if (!hOpen)
707 goto end;
708
709 hFile = InternetOpenUrlW(hOpen, InfoArray[iAppId].szUrl.GetString(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
710
711 if (!hFile)
712 {
713 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
714 goto end;
715 }
716
717 if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
718 goto end;
719
720 if (dwStatus != HTTP_STATUS_OK)
721 {
722 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
723 goto end;
724 }
725
726 dwStatusLen = sizeof(dwStatus);
727
728 memset(&urlComponents, 0, sizeof(urlComponents));
729 urlComponents.dwStructSize = sizeof(urlComponents);
730
731 urlLength = InfoArray[iAppId].szUrl.GetLength();
732 urlComponents.dwSchemeLength = urlLength + 1;
733 urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
734 urlComponents.dwHostNameLength = urlLength + 1;
735 urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
736
737 if (!InternetCrackUrlW(InfoArray[iAppId].szUrl, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
738 goto end;
739
740 dwContentLen = 0;
741
742 if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
743 HttpQueryInfoW(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
744
745 if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
746 dwContentLen = FtpGetFileSize(hFile, &dwStatus);
747
748 if (!dwContentLen)
749 {
750 // content-length is not known, enable marquee mode
751 SetProgressMarquee(Item, TRUE);
752 }
753
754 #ifdef USE_CERT_PINNING
755 // are we using HTTPS to download the RAPPS update package? check if the certificate is original
756 if ((urlComponents.nScheme == INTERNET_SCHEME_HTTPS) &&
757 (wcscmp(InfoArray[iAppId].szUrl, APPLICATION_DATABASE_URL) == 0) &&
758 (!CertIsValid(hFile, urlComponents.lpszHostName)))
759 {
760 MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
761 goto end;
762 }
763 #endif
764
765 free(urlComponents.lpszScheme);
766 free(urlComponents.lpszHostName);
767
768 hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
769
770 if (hOut == INVALID_HANDLE_VALUE)
771 goto end;
772
773 dwCurrentBytesRead = 0;
774 do
775 {
776 if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
777 {
778 MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
779 goto end;
780 }
781
782 if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
783 {
784 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
785 goto end;
786 }
787
788 dwCurrentBytesRead += dwBytesRead;
789 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
790 } while (dwBytesRead && !bCancelled);
791
792 CloseHandle(hOut);
793 hOut = INVALID_HANDLE_VALUE;
794
795 if (bCancelled)
796 goto end;
797
798 if (!dwContentLen)
799 {
800 // set progress bar to 100%
801 SetProgressMarquee(Item, FALSE);
802
803 dwContentLen = dwCurrentBytesRead;
804 dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, InfoArray[iAppId].szUrl.GetString());
805 }
806
807 /* if this thing isn't a RAPPS update and it has a SHA-1 checksum
808 verify its integrity by using the native advapi32.A_SHA1 functions */
809 if (!bCab && InfoArray[iAppId].szSHA1[0] != 0)
810 {
811 ATL::CStringW szMsgText;
812
813 // change a few strings in the download dialog to reflect the verification process
814 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_TITLE))
815 goto end;
816
817 SetWindowTextW(hDlg, szMsgText.GetString());
818 SendMessageW(GetDlgItem(hDlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetString());
819
820 // this may take a while, depending on the file size
821 if (!VerifyInteg(InfoArray[iAppId].szSHA1.GetString(), Path.GetString()))
822 {
823 if (!szMsgText.LoadStringW(IDS_INTEG_CHECK_FAIL))
824 goto end;
825
826 MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
827 goto end;
828 }
829 }
830
831 run:
832 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_WAITING_INSTALL);
833
834 // run it
835 if (!bCab)
836 {
837 SHELLEXECUTEINFOW shExInfo = {0};
838 shExInfo.cbSize = sizeof(shExInfo);
839 shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
840 shExInfo.lpVerb = L"open";
841 shExInfo.lpFile = Path.GetString();
842 shExInfo.lpParameters = L"";
843 shExInfo.nShow = SW_SHOW;
844
845 if (ShellExecuteExW(&shExInfo))
846 {
847 //reflect installation progress in the titlebar
848 //TODO: make a separate string with a placeholder to include app name?
849 ATL::CStringW szMsgText = LoadStatusString(DLSTATUS_INSTALLING);
850 SetWindowTextW(hDlg, szMsgText.GetString());
851
852 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_INSTALLING);
853
854 //TODO: issue an install operation separately so that the apps could be downloaded in the background
855 WaitForSingleObject(shExInfo.hProcess, INFINITE);
856 CloseHandle(shExInfo.hProcess);
857 }
858 else
859 {
860 MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_INSTALL);
861 }
862 }
863
864 end:
865 if (hOut != INVALID_HANDLE_VALUE)
866 CloseHandle(hOut);
867
868 InternetCloseHandle(hFile);
869 InternetCloseHandle(hOpen);
870
871 if (bTempfile)
872 {
873 if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
874 DeleteFileW(Path.GetString());
875 }
876
877 DownloadsListView.SetDownloadStatus(iAppId, DLSTATUS_FINISHED);
878 }
879
880 delete static_cast<DownloadParam*>(param);
881 SendMessageW(hDlg, WM_CLOSE, 0, 0);
882 return 0;
883 }
884
885 BOOL CDownloadManager::DownloadListOfApplications(const ATL::CSimpleArray<CAvailableApplicationInfo>& AppsList, BOOL bIsModal)
886 {
887 if (AppsList.GetSize() == 0)
888 return FALSE;
889
890 // Initialize shared variables
891 for (INT i = 0; i < AppsList.GetSize(); ++i)
892 {
893 AppsToInstallList.Add(AppsList[i]); // implicit conversion to DownloadInfo
894 }
895
896 // Create a dialog and issue a download process
897 LaunchDownloadDialog(bIsModal);
898
899 return TRUE;
900 }
901
902 BOOL CDownloadManager::DownloadApplication(CAvailableApplicationInfo* pAppInfo, BOOL bIsModal)
903 {
904 if (!pAppInfo)
905 return FALSE;
906
907 Download(*pAppInfo, bIsModal);
908 return TRUE;
909 }
910
911 VOID CDownloadManager::DownloadApplicationsDB(LPCWSTR lpUrl)
912 {
913 static DownloadInfo DatabaseDLInfo;
914 DatabaseDLInfo.szUrl = lpUrl;
915 DatabaseDLInfo.szName.LoadStringW(IDS_DL_DIALOG_DB_DISP);
916 Download(DatabaseDLInfo, TRUE);
917 }
918
919 //TODO: Reuse the dialog
920 VOID CDownloadManager::LaunchDownloadDialog(BOOL bIsModal)
921 {
922 if (bIsModal)
923 {
924 DialogBoxW(hInst,
925 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
926 hMainWnd,
927 DownloadDlgProc);
928 }
929 else
930 {
931 CreateDialogW(hInst,
932 MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
933 hMainWnd,
934 DownloadDlgProc);
935 }
936 }
937 // CDownloadManager