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
25 #define UPDATE_LIST_FILE_FLAG_DIRECTORY 0x00000001
\r
39 CHAR VersionString[32];
\r
40 CHAR Description[1024];
\r
42 UPDATE_LIST_FILE File[1];
\r
45 BOOL ReadFileViaHTTPW(void* pOut, DWORD Length, DWORD* pLength, LPCWSTR UserAgent, LPCWSTR ServerName, LPCWSTR ObjectName)
\r
52 if(hSession = WinHttpOpen(UserAgent, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0))
\r
54 if(hConnect = WinHttpConnect(hSession, ServerName, INTERNET_DEFAULT_HTTP_PORT, 0))
\r
56 if(hRequest = WinHttpOpenRequest(hConnect, L"GET", ObjectName, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0))
\r
58 if(WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0))
\r
60 if(WinHttpReceiveResponse(hRequest, NULL))
\r
62 if(WinHttpQueryDataAvailable(hRequest, pLength))
\r
64 if(*pLength <= Length)
\r
66 if(WinHttpReadData(hRequest, pOut, Length, pLength))
\r
72 WinHttpCloseHandle(hRequest);
\r
74 WinHttpCloseHandle(hConnect);
\r
76 WinHttpCloseHandle(hSession);
\r
81 BOOL ReadFileViaHTTP(void* pOut, DWORD Length, DWORD* pLength, LPCSTR UserAgent, LPCSTR ServerName, LPCSTR ObjectName)
\r
87 pw0 = DuplicateMtoW(UserAgent, -1);
\r
88 pw1 = DuplicateMtoW(ServerName, -1);
\r
89 pw2 = DuplicateMtoW(ObjectName, -1);
\r
90 r = ReadFileViaHTTPW(pOut, Length, pLength, pw0, pw1, pw2);
\r
91 FreeDuplicatedString(pw0);
\r
92 FreeDuplicatedString(pw1);
\r
93 FreeDuplicatedString(pw2);
\r
97 BOOL SaveMemoryToFileWithTimestamp(LPCTSTR FileName, void* pData, DWORD Size, FILETIME* pTimestamp)
\r
102 if((hFile = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
\r
104 if(WriteFile(hFile, pData, Size, &Size, NULL))
\r
108 if(SetFileTime(hFile, NULL, NULL, pTimestamp))
\r
114 CloseHandle(hFile);
\r
119 BOOL LoadMemoryFromFileWithTimestamp(LPCTSTR FileName, void* pData, DWORD Size, DWORD* pReadSize, FILETIME* pTimestamp)
\r
125 if((hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE)
\r
127 if(GetFileSizeEx(hFile, &li))
\r
129 if(li.QuadPart <= (LONGLONG)Size)
\r
131 if(ReadFile(hFile, pData, Size, pReadSize, NULL))
\r
133 if(*pReadSize == li.LowPart)
\r
137 if(GetFileTime(hFile, NULL, NULL, pTimestamp))
\r
145 CloseHandle(hFile);
\r
151 BOOL CopyAllFilesInDirectory(LPCTSTR From, LPCTSTR To)
\r
156 SHFILEOPSTRUCT fop;
\r
158 if(pFrom = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(From) + _tcslen(_T("\\*")) + 2)))
\r
160 _tcscpy(pFrom, From);
\r
161 _tcsncpy(pFrom + _tcslen(pFrom), _T("\\*"), _tcslen(_T("\\*")) + 2);
\r
162 if(pTo = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(To) + 2)))
\r
164 _tcsncpy(pTo, To, _tcslen(To) + 2);
\r
165 memset(&fop, 0, sizeof(SHFILEOPSTRUCT));
\r
166 fop.wFunc = FO_COPY;
\r
169 fop.fFlags = FOF_NO_UI;
\r
170 if(SHFileOperation(&fop) == 0)
\r
179 BOOL DeleteDirectoryAndContents(LPCTSTR Path)
\r
183 SHFILEOPSTRUCT fop;
\r
185 if(pFrom = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(Path) + 2)))
\r
187 _tcsncpy(pFrom, Path, _tcslen(Path) + 2);
\r
188 memset(&fop, 0, sizeof(SHFILEOPSTRUCT));
\r
189 fop.wFunc = FO_DELETE;
\r
191 fop.fFlags = FOF_NO_UI;
\r
192 if(SHFileOperation(&fop) == 0)
\r
199 DWORD ListUpdateFile(UPDATE_LIST* pList, DWORD MaxCount, LPCTSTR ServerPath, LPCTSTR ReferenceDir, LPCTSTR Path)
\r
202 TCHAR Temp1[MAX_PATH];
\r
203 TCHAR Temp2[MAX_PATH];
\r
204 TCHAR Temp3[MAX_PATH];
\r
206 WIN32_FIND_DATA Find;
\r
214 if(_tcslen(ReferenceDir) + _tcslen(Path) + _tcslen(_T("\\*")) < MAX_PATH)
\r
216 _tcscpy(Temp1, ReferenceDir);
\r
217 _tcscat(Temp1, Path);
\r
218 _tcscat(Temp1, _T("\\*"));
\r
219 if((hFind = FindFirstFile(Temp1, &Find)) != INVALID_HANDLE_VALUE)
\r
223 if(_tcscmp(Find.cFileName, _T(".")) != 0 && _tcscmp(Find.cFileName, _T("..")) != 0)
\r
225 if(_tcslen(ServerPath) + _tcslen(_T("/")) + _tcslen(Find.cFileName) < 128 && _tcslen(Path) + _tcslen(_T("\\")) + _tcslen(Find.cFileName) < 128)
\r
227 _tcscpy(Temp1, ServerPath);
\r
228 _tcscat(Temp1, _T("/"));
\r
229 _tcscat(Temp1, Find.cFileName);
\r
230 _tcscpy(Temp2, Path);
\r
231 _tcscat(Temp2, _T("\\"));
\r
232 _tcscat(Temp2, Find.cFileName);
\r
233 if(_tcslen(ReferenceDir) + _tcslen(Temp2) < MAX_PATH)
\r
235 _tcscpy(Temp3, ReferenceDir);
\r
236 _tcscat(Temp3, Temp2);
\r
237 if((Find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
\r
239 if(!(Find.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
\r
243 memset(&pList->File[pList->FileCount], 0, sizeof(UPDATE_LIST_FILE));
\r
244 pList->File[pList->FileCount].Flags = UPDATE_LIST_FILE_FLAG_DIRECTORY;
\r
245 _tcscpy(pList->File[pList->FileCount].DstPath, Temp2);
\r
246 pList->FileCount++;
\r
249 if(Result >= MaxCount)
\r
251 Result += ListUpdateFile(pList, MaxCount, Temp1, ReferenceDir, Temp2);
\r
258 if(pBuf = malloc(16777216))
\r
260 if(LoadMemoryFromFileWithTimestamp(Temp3, pBuf, 16777216, &Length, &Time))
\r
262 if(GetHashSHA512(pBuf, Length, &Hash))
\r
264 memset(&pList->File[pList->FileCount], 0, sizeof(UPDATE_LIST_FILE));
\r
265 _tcscpy(pList->File[pList->FileCount].SrcPath, Temp1);
\r
266 memcpy(&pList->File[pList->FileCount].SrcHash, &Hash, 64);
\r
267 _tcscpy(pList->File[pList->FileCount].DstPath, Temp2);
\r
268 pList->File[pList->FileCount].Timestamp = Time;
\r
269 pList->FileCount++;
\r
276 if(Result >= MaxCount)
\r
283 while(FindNextFile(hFind, &Find));
\r
291 BOOL BuildUpdates(LPCTSTR PrivateKeyFile, LPCTSTR Password, LPCTSTR ServerPath, LPCTSTR HashFile, LPCTSTR ListFile, DWORD Version, LPCTSTR VersionString, LPCTSTR Description)
\r
294 char PrivateKey[4096];
\r
296 TCHAR Name[MAX_PATH];
\r
298 UPDATE_LIST* pList;
\r
302 memset(PrivateKey, 0, sizeof(PrivateKey));
\r
303 if(LoadMemoryFromFileWithTimestamp(PrivateKeyFile, &PrivateKey, sizeof(PrivateKey) - sizeof(char), &Length, NULL))
\r
305 if(GetModuleFileName(NULL, Name, MAX_PATH) > 0)
\r
307 if(p = _tcsrchr(Name, _T('\\')))
\r
309 if(pList = (UPDATE_LIST*)malloc(1048576))
\r
311 memset(pList, 0, 1048576);
\r
312 pList->Version = Version;
\r
313 _tcscpy(pList->VersionString, VersionString);
\r
314 _tcscpy(pList->Description, Description);
\r
315 ListUpdateFile(pList, (1048576 - sizeof(UPDATE_LIST)) / sizeof(UPDATE_LIST_FILE) + 1, ServerPath, Name, NULL);
\r
316 Length = (pList->FileCount - 1) * sizeof(UPDATE_LIST_FILE) + sizeof(UPDATE_LIST);
\r
317 if(SaveMemoryToFileWithTimestamp(ListFile, pList, Length, NULL))
\r
319 memcpy(&Hash.Signature, UPDATE_SIGNATURE, 64);
\r
320 if(GetHashSHA512(pList, Length, &Hash.ListHash))
\r
322 if(EncryptSignature(PrivateKey, Password, &Hash, sizeof(UPDATE_HASH), &Buf, sizeof(Buf), &Length))
\r
324 if(SaveMemoryToFileWithTimestamp(HashFile, &Buf, Length, NULL))
\r
337 BOOL CheckForUpdates(BOOL bDownload, LPCTSTR DownloadDir, DWORD* pVersion, LPTSTR pVersionString, LPTSTR pDescription)
\r
344 UPDATE_HASH UpdateHash;
\r
346 UPDATE_LIST* pUpdateList;
\r
348 if(ReadFileViaHTTP(&Buf1, sizeof(Buf1), &Length, HTTP_USER_AGENT, UPDATE_SERVER, UPDATE_HASH_PATH))
\r
350 if(DecryptSignature(UPDATE_RSA_PUBLIC_KEY, NULL, &Buf1, Length, &Buf2, sizeof(Buf2), &Length))
\r
352 if(Length == sizeof(UPDATE_HASH))
\r
354 memcpy(&UpdateHash, &Buf2, sizeof(UPDATE_HASH));
\r
355 if(memcmp(&UpdateHash.Signature, UPDATE_SIGNATURE, 64) == 0)
\r
357 if(pBuf = malloc(1048576))
\r
359 if(ReadFileViaHTTP(pBuf, 1048576, &Length, HTTP_USER_AGENT, UPDATE_SERVER, UPDATE_LIST_PATH))
\r
361 if(GetHashSHA512(pBuf, Length, &Hash))
\r
363 if(memcmp(&Hash, &UpdateHash.ListHash, 64) == 0)
\r
365 if(Length >= sizeof(UPDATE_LIST))
\r
368 pUpdateList = (UPDATE_LIST*)pBuf;
\r
369 if(pUpdateList->Version > *pVersion)
\r
371 *pVersion = pUpdateList->Version;
\r
372 _tcscpy(pVersionString, pUpdateList->VersionString);
\r
373 _tcscpy(pDescription, pUpdateList->Description);
\r
376 bResult = PrepareUpdates(pBuf, Length, DownloadDir);
\r
391 BOOL PrepareUpdates(void* pList, DWORD ListLength, LPCTSTR DownloadDir)
\r
394 UPDATE_LIST* pUpdateList;
\r
400 TCHAR Path[MAX_PATH];
\r
402 if(ListLength >= sizeof(UPDATE_LIST))
\r
404 pUpdateList = (UPDATE_LIST*)pList;
\r
405 if((pUpdateList->FileCount - 1) * sizeof(UPDATE_LIST_FILE) + sizeof(UPDATE_LIST) >= ListLength)
\r
408 DeleteDirectoryAndContents(DownloadDir);
\r
409 CreateDirectory(DownloadDir, NULL);
\r
410 if(pBuf = malloc(16777216))
\r
412 for(i = 0; i < pUpdateList->FileCount; i++)
\r
415 if(pUpdateList->File[i].Flags & UPDATE_LIST_FILE_FLAG_DIRECTORY)
\r
417 _tcscpy(Path, DownloadDir);
\r
418 _tcscat(Path, pUpdateList->File[i].DstPath);
\r
419 if(CreateDirectory(Path, NULL))
\r
422 if(strlen(pUpdateList->File[i].SrcPath) > 0)
\r
424 if(ReadFileViaHTTP(pBuf, 16777216, &Length, HTTP_USER_AGENT, UPDATE_SERVER, pUpdateList->File[i].SrcPath))
\r
426 if(GetHashSHA512(pBuf, Length, &Hash))
\r
428 if(memcmp(&Hash, &pUpdateList->File[i].SrcHash, 64) == 0)
\r
430 _tcscpy(Path, DownloadDir);
\r
431 _tcscat(Path, pUpdateList->File[i].DstPath);
\r
432 if(SaveMemoryToFileWithTimestamp(Path, pBuf, Length, &pUpdateList->File[i].Timestamp))
\r
452 BOOL ApplyUpdates(LPCTSTR DestinationDir, LPCTSTR BackupDirName)
\r
455 TCHAR Source[MAX_PATH];
\r
456 TCHAR Backup[MAX_PATH];
\r
457 TCHAR DestinationBackup[MAX_PATH];
\r
460 if(GetModuleFileName(NULL, Source, MAX_PATH) > 0)
\r
462 if(p = _tcsrchr(Source, _T('\\')))
\r
464 _tcscpy(Backup, Source);
\r
465 _tcscat(Backup, _T("\\"));
\r
466 _tcscat(Backup, BackupDirName);
\r
467 DeleteDirectoryAndContents(Backup);
\r
468 if(CreateDirectory(Backup, NULL))
\r
470 if(CopyAllFilesInDirectory(DestinationDir, Backup))
\r
472 _tcscpy(DestinationBackup, DestinationDir);
\r
473 _tcscat(DestinationBackup, _T("\\"));
\r
474 _tcscat(DestinationBackup, BackupDirName);
\r
475 if(CopyAllFilesInDirectory(Source, DestinationDir))
\r
477 DeleteDirectoryAndContents(DestinationBackup);
\r
482 DeleteDirectoryAndContents(DestinationBackup);
\r
483 CopyAllFilesInDirectory(Backup, DestinationDir);
\r
492 BOOL CleanupUpdates(LPCTSTR DownloadDir)
\r
496 if(DeleteDirectoryAndContents(DownloadDir))
\r
502 BOOL StartUpdateProcess(LPCTSTR DownloadDir, LPCTSTR CommandLine)
\r
505 TCHAR Name[MAX_PATH];
\r
507 TCHAR Path[MAX_PATH];
\r
509 if(GetModuleFileName(NULL, Name, MAX_PATH) > 0)
\r
511 if(p = _tcsrchr(Name, _T('\\')))
\r
515 _tcscpy(Path, DownloadDir);
\r
516 _tcscat(Path, _T("\\"));
\r
518 if(ShellExecute(NULL, _T("open"), Path, CommandLine, NULL, SW_SHOW) > (HINSTANCE)32)
\r
524 // 更新用のプロセスを管理者権限で起動
\r
525 // Windows XP以前など起動できない場合は現在のプロセスで処理を続行
\r
526 BOOL RestartUpdateProcessAsAdministrator(LPCTSTR CommandLine, LPCTSTR Keyword)
\r
529 TCHAR* NewCommandLine;
\r
530 TCHAR Path[MAX_PATH];
\r
531 SHELLEXECUTEINFO Info;
\r
533 if(_tcslen(CommandLine) < _tcslen(Keyword) || _tcscmp(CommandLine + _tcslen(CommandLine) - _tcslen(Keyword), Keyword) != 0)
\r
535 if(NewCommandLine = (TCHAR*)malloc(sizeof(TCHAR) * (_tcslen(CommandLine) + _tcslen(Keyword) + 1)))
\r
537 _tcscpy(NewCommandLine, CommandLine);
\r
538 _tcscat(NewCommandLine, Keyword);
\r
539 if(GetModuleFileName(NULL, Path, MAX_PATH) > 0)
\r
541 memset(&Info, 0, sizeof(SHELLEXECUTEINFO));
\r
542 Info.cbSize = sizeof(SHELLEXECUTEINFO);
\r
543 Info.fMask = SEE_MASK_NOCLOSEPROCESS;
\r
544 Info.lpVerb = _T("runas");
\r
545 Info.lpFile = Path;
\r
546 Info.lpParameters = NewCommandLine;
\r
547 Info.nShow = SW_SHOW;
\r
548 if(ShellExecuteEx(&Info))
\r
550 WaitForSingleObject(Info.hProcess, INFINITE);
\r
551 CloseHandle(Info.hProcess);
\r
555 free(NewCommandLine);
\r