2 // Copyright (C) 2014 Suguru Kawamoto
\r
4 // コードの再利用のため表記はwchar_t型だが実体はchar型でUTF-8
\r
7 #include <ws2tcpip.h>
\r
9 #include <mmsystem.h>
\r
10 typedef SOCKADDR_STORAGE_XP SOCKADDR_STORAGE;
\r
11 typedef SOCKADDR_STORAGE *PSOCKADDR_STORAGE, FAR *LPSOCKADDR_STORAGE;
\r
12 #include <winhttp.h>
\r
14 #include "updater.h"
\r
15 #include "socketwrapper.h"
\r
16 #include "protectprocess.h"
\r
17 #include "mbswrapper.h"
\r
18 #include "apiemulator.h"
\r
26 #define UPDATE_LIST_FILE_FLAG_DIRECTORY 0x00000001
\r
40 CHAR VersionString[32];
\r
41 CHAR Description[1024];
\r
43 UPDATE_LIST_FILE File[1];
\r
46 BOOL ReadFileViaHTTPW(void* pOut, DWORD Length, DWORD* pLength, LPCWSTR UserAgent, LPCWSTR ServerName, LPCWSTR ObjectName)
\r
53 if(hSession = WinHttpOpen(UserAgent, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0))
\r
55 if(hConnect = WinHttpConnect(hSession, ServerName, INTERNET_DEFAULT_HTTP_PORT, 0))
\r
57 if(hRequest = WinHttpOpenRequest(hConnect, L"GET", ObjectName, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0))
\r
59 if(WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
\r
61 if(WinHttpReceiveResponse(hRequest, NULL))
\r
63 if(WinHttpQueryDataAvailable(hRequest, pLength))
\r
65 if(*pLength <= Length)
\r
67 if(WinHttpReadData(hRequest, pOut, Length, pLength))
\r
73 WinHttpCloseHandle(hRequest);
\r
75 WinHttpCloseHandle(hConnect);
\r
77 WinHttpCloseHandle(hSession);
\r
82 BOOL ReadFileViaHTTP(void* pOut, DWORD Length, DWORD* pLength, LPCSTR UserAgent, LPCSTR ServerName, LPCSTR ObjectName)
\r
88 pw0 = DuplicateMtoW(UserAgent, -1);
\r
89 pw1 = DuplicateMtoW(ServerName, -1);
\r
90 pw2 = DuplicateMtoW(ObjectName, -1);
\r
91 r = ReadFileViaHTTPW(pOut, Length, pLength, pw0, pw1, pw2);
\r
92 FreeDuplicatedString(pw0);
\r
93 FreeDuplicatedString(pw1);
\r
94 FreeDuplicatedString(pw2);
\r
98 BOOL SaveMemoryToFileWithTimestamp(LPCTSTR FileName, void* pData, DWORD Size, FILETIME* pTimestamp)
\r
103 if((hFile = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
\r
105 if(WriteFile(hFile, pData, Size, &Size, NULL))
\r
109 if(SetFileTime(hFile, NULL, NULL, pTimestamp))
\r
115 CloseHandle(hFile);
\r
120 BOOL LoadMemoryFromFileWithTimestamp(LPCTSTR FileName, void* pData, DWORD Size, DWORD* pReadSize, FILETIME* pTimestamp)
\r
126 if((hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
\r
128 if(GetFileSizeEx(hFile, &li))
\r
130 if(li.QuadPart <= (LONGLONG)Size)
\r
132 if(ReadFile(hFile, pData, Size, pReadSize, NULL))
\r
134 if(*pReadSize == li.LowPart)
\r
138 if(GetFileTime(hFile, NULL, NULL, pTimestamp))
\r
146 CloseHandle(hFile);
\r
152 BOOL CopyAllFilesInDirectory(LPCTSTR From, LPCTSTR To)
\r
157 SHFILEOPSTRUCT fop;
\r
159 if(pFrom = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(From) + _tcslen(_T("\\*")) + 2)))
\r
161 _tcscpy(pFrom, From);
\r
162 _tcsncpy(pFrom + _tcslen(pFrom), _T("\\*"), _tcslen(_T("\\*")) + 2);
\r
163 if(pTo = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(To) + 2)))
\r
165 _tcsncpy(pTo, To, _tcslen(To) + 2);
\r
166 memset(&fop, 0, sizeof(SHFILEOPSTRUCT));
\r
167 fop.wFunc = FO_COPY;
\r
170 fop.fFlags = FOF_NO_UI;
\r
171 if(SHFileOperation(&fop) == 0)
\r
180 BOOL DeleteDirectoryAndContents(LPCTSTR Path)
\r
184 SHFILEOPSTRUCT fop;
\r
186 if(pFrom = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(Path) + 2)))
\r
188 _tcsncpy(pFrom, Path, _tcslen(Path) + 2);
\r
189 memset(&fop, 0, sizeof(SHFILEOPSTRUCT));
\r
190 fop.wFunc = FO_DELETE;
\r
192 fop.fFlags = FOF_NO_UI;
\r
193 if(SHFileOperation(&fop) == 0)
\r
200 DWORD ListUpdateFile(UPDATE_LIST* pList, DWORD MaxCount, LPCTSTR ServerPath, LPCTSTR ReferenceDir, LPCTSTR Path)
\r
203 TCHAR Temp1[MAX_PATH];
\r
204 TCHAR Temp2[MAX_PATH];
\r
205 TCHAR Temp3[MAX_PATH];
\r
207 WIN32_FIND_DATA Find;
\r
215 if(_tcslen(ReferenceDir) + _tcslen(Path) + _tcslen(_T("\\*")) < MAX_PATH)
\r
217 _tcscpy(Temp1, ReferenceDir);
\r
218 _tcscat(Temp1, Path);
\r
219 _tcscat(Temp1, _T("\\*"));
\r
220 if((hFind = FindFirstFile(Temp1, &Find)) != INVALID_HANDLE_VALUE)
\r
224 if(_tcscmp(Find.cFileName, _T(".")) != 0 && _tcscmp(Find.cFileName, _T("..")) != 0)
\r
226 if(_tcslen(ServerPath) + _tcslen(_T("/")) + _tcslen(Find.cFileName) < 128 && _tcslen(Path) + _tcslen(_T("\\")) + _tcslen(Find.cFileName) < 128)
\r
228 _tcscpy(Temp1, ServerPath);
\r
229 _tcscat(Temp1, _T("/"));
\r
230 _tcscat(Temp1, Find.cFileName);
\r
231 _tcscpy(Temp2, Path);
\r
232 _tcscat(Temp2, _T("\\"));
\r
233 _tcscat(Temp2, Find.cFileName);
\r
234 if(_tcslen(ReferenceDir) + _tcslen(Temp2) < MAX_PATH)
\r
236 _tcscpy(Temp3, ReferenceDir);
\r
237 _tcscat(Temp3, Temp2);
\r
238 if((Find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
\r
240 if(!(Find.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
\r
244 memset(&pList->File[pList->FileCount], 0, sizeof(UPDATE_LIST_FILE));
\r
245 pList->File[pList->FileCount].Flags = UPDATE_LIST_FILE_FLAG_DIRECTORY;
\r
246 _tcscpy(pList->File[pList->FileCount].DstPath, Temp2);
\r
247 pList->FileCount++;
\r
250 if(Result >= MaxCount)
\r
252 Result += ListUpdateFile(pList, MaxCount, Temp1, ReferenceDir, Temp2);
\r
259 if(pBuf = malloc(16777216))
\r
261 if(LoadMemoryFromFileWithTimestamp(Temp3, pBuf, 16777216, &Length, &Time))
\r
263 if(GetHashSHA512(pBuf, Length, &Hash))
\r
265 memset(&pList->File[pList->FileCount], 0, sizeof(UPDATE_LIST_FILE));
\r
266 _tcscpy(pList->File[pList->FileCount].SrcPath, Temp1);
\r
267 memcpy(&pList->File[pList->FileCount].SrcHash, &Hash, 64);
\r
268 _tcscpy(pList->File[pList->FileCount].DstPath, Temp2);
\r
269 pList->File[pList->FileCount].Timestamp = Time;
\r
270 pList->FileCount++;
\r
277 if(Result >= MaxCount)
\r
284 while(FindNextFile(hFind, &Find));
\r
292 BOOL BuildUpdates(LPCTSTR PrivateKeyFile, LPCTSTR Password, LPCTSTR ServerPath, LPCTSTR HashFile, LPCTSTR ListFile, DWORD Version, LPCTSTR VersionString, LPCTSTR Description)
\r
295 char PrivateKey[4096];
\r
297 TCHAR Name[MAX_PATH];
\r
299 UPDATE_LIST* pList;
\r
303 memset(PrivateKey, 0, sizeof(PrivateKey));
\r
304 if(LoadMemoryFromFileWithTimestamp(PrivateKeyFile, &PrivateKey, sizeof(PrivateKey) - sizeof(char), &Length, NULL))
\r
306 if(GetModuleFileName(NULL, Name, MAX_PATH) > 0)
\r
308 if(p = _tcsrchr(Name, _T('\\')))
\r
310 if(pList = (UPDATE_LIST*)malloc(1048576))
\r
312 memset(pList, 0, 1048576);
\r
313 pList->Version = Version;
\r
314 _tcscpy(pList->VersionString, VersionString);
\r
315 _tcscpy(pList->Description, Description);
\r
316 ListUpdateFile(pList, (1048576 - sizeof(UPDATE_LIST)) / sizeof(UPDATE_LIST_FILE) + 1, ServerPath, Name, NULL);
\r
317 Length = (pList->FileCount - 1) * sizeof(UPDATE_LIST_FILE) + sizeof(UPDATE_LIST);
\r
318 if(SaveMemoryToFileWithTimestamp(ListFile, pList, Length, NULL))
\r
320 memcpy(&Hash.Signature, UPDATE_SIGNATURE, 64);
\r
321 if(GetHashSHA512(pList, Length, &Hash.ListHash))
\r
323 if(EncryptSignature(PrivateKey, Password, &Hash, sizeof(UPDATE_HASH), &Buf, sizeof(Buf), &Length))
\r
325 if(SaveMemoryToFileWithTimestamp(HashFile, &Buf, Length, NULL))
\r
338 BOOL CheckForUpdates(BOOL bDownload, LPCTSTR DownloadDir, DWORD* pVersion, LPTSTR pVersionString, LPTSTR pDescription)
\r
345 UPDATE_HASH UpdateHash;
\r
347 UPDATE_LIST* pUpdateList;
\r
349 if(ReadFileViaHTTP(&Buf1, sizeof(Buf1), &Length, HTTP_USER_AGENT, UPDATE_SERVER, UPDATE_HASH_PATH))
\r
351 if(DecryptSignature(UPDATE_RSA_PUBLIC_KEY, NULL, &Buf1, Length, &Buf2, sizeof(Buf2), &Length))
\r
353 if(Length == sizeof(UPDATE_HASH))
\r
355 memcpy(&UpdateHash, &Buf2, sizeof(UPDATE_HASH));
\r
356 if(memcmp(&UpdateHash.Signature, UPDATE_SIGNATURE, 64) == 0)
\r
358 if(pBuf = malloc(1048576))
\r
360 if(ReadFileViaHTTP(pBuf, 1048576, &Length, HTTP_USER_AGENT, UPDATE_SERVER, UPDATE_LIST_PATH))
\r
362 if(GetHashSHA512(pBuf, Length, &Hash))
\r
364 if(memcmp(&Hash, &UpdateHash.ListHash, 64) == 0)
\r
366 if(Length >= sizeof(UPDATE_LIST))
\r
369 pUpdateList = (UPDATE_LIST*)pBuf;
\r
370 if(pUpdateList->Version > *pVersion)
\r
372 *pVersion = pUpdateList->Version;
\r
373 _tcscpy(pVersionString, pUpdateList->VersionString);
\r
374 _tcscpy(pDescription, pUpdateList->Description);
\r
377 bResult = PrepareUpdates(pBuf, Length, DownloadDir);
\r
392 BOOL PrepareUpdates(void* pList, DWORD ListLength, LPCTSTR DownloadDir)
\r
395 UPDATE_LIST* pUpdateList;
\r
401 TCHAR Path[MAX_PATH];
\r
403 if(ListLength >= sizeof(UPDATE_LIST))
\r
405 pUpdateList = (UPDATE_LIST*)pList;
\r
406 if((pUpdateList->FileCount - 1) * sizeof(UPDATE_LIST_FILE) + sizeof(UPDATE_LIST) >= ListLength)
\r
409 DeleteDirectoryAndContents(DownloadDir);
\r
410 CreateDirectory(DownloadDir, NULL);
\r
411 if(pBuf = malloc(16777216))
\r
413 for(i = 0; i < pUpdateList->FileCount; i++)
\r
416 if(pUpdateList->File[i].Flags & UPDATE_LIST_FILE_FLAG_DIRECTORY)
\r
418 _tcscpy(Path, DownloadDir);
\r
419 _tcscat(Path, pUpdateList->File[i].DstPath);
\r
420 if(CreateDirectory(Path, NULL))
\r
423 if(strlen(pUpdateList->File[i].SrcPath) > 0)
\r
425 if(ReadFileViaHTTP(pBuf, 16777216, &Length, HTTP_USER_AGENT, UPDATE_SERVER, pUpdateList->File[i].SrcPath))
\r
427 if(GetHashSHA512(pBuf, Length, &Hash))
\r
429 if(memcmp(&Hash, &pUpdateList->File[i].SrcHash, 64) == 0)
\r
431 _tcscpy(Path, DownloadDir);
\r
432 _tcscat(Path, pUpdateList->File[i].DstPath);
\r
433 if(SaveMemoryToFileWithTimestamp(Path, pBuf, Length, &pUpdateList->File[i].Timestamp))
\r
453 BOOL ApplyUpdates(LPCTSTR DestinationDir, LPCTSTR BackupDirName)
\r
456 TCHAR Source[MAX_PATH];
\r
457 TCHAR Backup[MAX_PATH];
\r
458 TCHAR DestinationBackup[MAX_PATH];
\r
461 if(GetModuleFileName(NULL, Source, MAX_PATH) > 0)
\r
463 if(p = _tcsrchr(Source, _T('\\')))
\r
465 _tcscpy(Backup, Source);
\r
466 _tcscat(Backup, _T("\\"));
\r
467 _tcscat(Backup, BackupDirName);
\r
468 DeleteDirectoryAndContents(Backup);
\r
469 if(CreateDirectory(Backup, NULL))
\r
471 if(CopyAllFilesInDirectory(DestinationDir, Backup))
\r
473 _tcscpy(DestinationBackup, DestinationDir);
\r
474 _tcscat(DestinationBackup, _T("\\"));
\r
475 _tcscat(DestinationBackup, BackupDirName);
\r
476 if(CopyAllFilesInDirectory(Source, DestinationDir))
\r
478 DeleteDirectoryAndContents(DestinationBackup);
\r
483 DeleteDirectoryAndContents(DestinationBackup);
\r
484 CopyAllFilesInDirectory(Backup, DestinationDir);
\r
493 BOOL CleanupUpdates(LPCTSTR DownloadDir)
\r
497 if(DeleteDirectoryAndContents(DownloadDir))
\r
503 BOOL StartUpdateProcess(LPCTSTR DownloadDir, LPCTSTR CommandLine)
\r
506 TCHAR Name[MAX_PATH];
\r
508 TCHAR Path[MAX_PATH];
\r
510 if(GetModuleFileName(NULL, Name, MAX_PATH) > 0)
\r
512 if(p = _tcsrchr(Name, _T('\\')))
\r
516 _tcscpy(Path, DownloadDir);
\r
517 _tcscat(Path, _T("\\"));
\r
519 if(ShellExecute(NULL, _T("open"), Path, CommandLine, NULL, SW_SHOW) > (HINSTANCE)32)
\r
525 // 更新用のプロセスを管理者権限で起動
\r
526 // Windows XP以前など起動できない場合は現在のプロセスで処理を続行
\r
527 BOOL RestartUpdateProcessAsAdministrator(LPCTSTR CommandLine, LPCTSTR Keyword)
\r
530 TCHAR* NewCommandLine;
\r
531 TCHAR Path[MAX_PATH];
\r
532 SHELLEXECUTEINFO Info;
\r
534 if(_tcslen(CommandLine) < _tcslen(Keyword) || _tcscmp(CommandLine + _tcslen(CommandLine) - _tcslen(Keyword), Keyword) != 0)
\r
536 if(NewCommandLine = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(CommandLine) + _tcslen(Keyword) + 1)))
\r
538 _tcscpy(NewCommandLine, CommandLine);
\r
539 _tcscat(NewCommandLine, Keyword);
\r
540 if(GetModuleFileName(NULL, Path, MAX_PATH) > 0)
\r
542 memset(&Info, 0, sizeof(SHELLEXECUTEINFO));
\r
543 Info.cbSize = sizeof(SHELLEXECUTEINFO);
\r
544 Info.fMask = SEE_MASK_NOCLOSEPROCESS;
\r
545 if(IsUserAnAdmin())
\r
546 Info.lpVerb = _T("open");
\r
548 Info.lpVerb = _T("runas");
\r
549 Info.lpFile = Path;
\r
550 Info.lpParameters = NewCommandLine;
\r
551 Info.nShow = SW_SHOW;
\r
552 if(ShellExecuteEx(&Info))
\r
554 WaitForSingleObject(Info.hProcess, INFINITE);
\r
555 CloseHandle(Info.hProcess);
\r
559 free(NewCommandLine);
\r