From dd4c07b6f71dc074dabef6b27e5dd4c49d882bb2 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Fri, 8 Feb 2013 22:52:02 +0200 Subject: merged with miranda_ng main repo "boosted" process management evnt_to_db/history>actual_sending logic to avoid srmm problem --- utilities.cpp | 400 ++++++++++++++++++++++------------------------------------ 1 file changed, 152 insertions(+), 248 deletions(-) (limited to 'utilities.cpp') diff --git a/utilities.cpp b/utilities.cpp index b42d8e7..736e5cf 100755 --- a/utilities.cpp +++ b/utilities.cpp @@ -377,27 +377,23 @@ int onProtoAck(WPARAM w, LPARAM l) if(file_msg_state < 1) return 0; HistoryLog(ack->hContact, db_event("Recieved encrypted file, trying to decrypt", 0,0, 0)); - if(_waccess(f->tszCurrentFile, 0) == -1) - { - if(errno == ENOENT) - return 0; - } + if(!boost::filesystem::exists(f->tszCurrentFile)) + return 0; string out; DWORD code; pxResult result; - wstring cmd = _T(" -o "); + std::vector cmd; + cmd.push_back(L"-o"); wstring file = filename; wstring::size_type p1 = file.rfind(_T(".gpg")); file.erase(p1, _tcslen(_T(".gpg"))); - if(_waccess(file.c_str(), 0) != -1) + if(boost::filesystem::exists(file)) { if(MessageBox(0, TranslateT("Target file exists, do you want to replace it ?"), TranslateT("Warning"), MB_YESNO) == IDNO) return 0; } - DeleteFile(file.c_str()); - file.insert(0, _T("\"")); - file.insert(file.length(), _T("\" ")); - cmd += file; + cmd.push_back(file); + boost::filesystem::remove(file); extern TCHAR *password; { // password TCHAR *pass = NULL; @@ -419,42 +415,29 @@ int onProtoAck(WPARAM w, LPARAM l) } if(_tcslen(pass) > 0) { - cmd += _T("--passphrase \""); - cmd += pass; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(pass); } else if(password) { if(bDebugLog) debuglog<hContact, GCDNF_TCHAR))+" with password"); - cmd += _T("--passphrase \""); - cmd += password; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(password); } else if (bDebugLog) debuglog<hContact, GCDNF_TCHAR))+" with out password"); mir_free(pass); mir_free(keyid); } - cmd += _T(" -d \""); - cmd += filename; - cmd += _T("\""); - gpg_execution_params params; - params.cmd = &cmd; - params.useless = ""; + cmd.push_back(L"-d"); + cmd.push_back(filename); + gpg_execution_params params(cmd); params.out = &out; params.code = &code; params.result = &result; - boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::minutes(15))) - { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<hContact; ShowLoadKeyPasswordWindow(); - wstring cmd2 = cmd; + std::vector cmd2 = cmd; if(password) { if(bDebugLog) debuglog<hContact, GCDNF_TCHAR))); - wstring tmp = _T("--passphrase \""); - tmp += password; - tmp += _T("\" "); - cmd2.insert(0, tmp); + std::vector tmp; + tmp.push_back(L"--passphrase"); + tmp.push_back(password); + cmd2.insert(cmd2.begin(), tmp.begin(), tmp.end()); } out.clear(); - gpg_execution_params params; + gpg_execution_params params(cmd2); //pxResult result; - params.cmd = &cmd2; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - gpg_thread = gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(15))) + if(!gpg_launcher(params, boost::posix_time::seconds(15))) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< cmd; + cmd.push_back(L"--batch"); + cmd.push_back(L"--tes"); + cmd.push_back(L"-r"); char *keyid = UniGetContactSettingUtf(hcnt, szGPGModuleName, "KeyID", ""); TCHAR *szKeyid = mir_a2t(keyid); TCHAR *name = _tcsrchr(filename,_T('\\')); @@ -563,58 +541,42 @@ std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) name++; TCHAR *file_out = new TCHAR [_tcslen(filename)+4]; mir_sntprintf(file_out, _tcslen(name)+7, _T("%s.gpg"), name); - cmd += szKeyid; + cmd.push_back(szKeyid); if(DBGetContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 0)) - cmd += _T(" --trust-model always "); + { + cmd.push_back(L"--trust-model"); + cmd.push_back(L"always"); + } mir_free(szKeyid); mir_free(keyid); - cmd += _T(" -o \""); + cmd.push_back(L"-o"); TCHAR *temp = _tgetenv(_T("TEMP")); - cmd += temp; - cmd += _T("\\"); - cmd += file_out; + cmd.push_back(wstring(temp) + L"\\" + file_out); wstring path_out = temp; path_out += _T("\\"); path_out += file_out; - DeleteFile(path_out.c_str()); - cmd += _T("\" "); - cmd += _T(" -e \""); - cmd += filename; - cmd += _T("\" "); - gpg_execution_params params; - params.cmd = &cmd; - params.useless = ""; + boost::filesystem::remove(path_out); + cmd.push_back(L"-e"); + cmd.push_back(filename); + gpg_execution_params params(cmd); params.out = &out; params.code = &code; params.result = &result; delete [] file_out; - boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(180))) - { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<timed_join(boost::posix_time::seconds(180))) - { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< tmp; + tmp.push_back(L"--trust-model"); + tmp.push_back(L"always"); + cmd.insert(cmd.begin(), tmp.begin(), tmp.end()); + if(!gpg_launcher(params, boost::posix_time::minutes(3))) return 0; - } } else return 0; @@ -694,9 +656,8 @@ int onSendFile(WPARAM w, LPARAM l) TCHAR **file=(TCHAR **)ccs->lParam; for(i = 0; file[i]; i++) { - if(_waccess(file[i], 0) == -1) - if(errno == ENOENT) - return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) + if(!boost::filesystem::exists(file[i])) + return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) if (_tcsstr(file[i],_T(".gpg"))) continue; std::wstring path_out = encrypt_file(ccs->hContact, file[i]); @@ -710,9 +671,8 @@ int onSendFile(WPARAM w, LPARAM l) char **file = (char**) ccs->lParam; for(i = 0; file[i]; i++) { - if(_access(file[i], 0) == -1) - if(errno == ENOENT) - return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) + if(!boost::filesystem::exists(file[i])) + return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) if (strstr(file[i],".gpg")) continue; TCHAR *tmp = mir_utf8decodeT(file[i]); @@ -729,23 +689,6 @@ int onSendFile(WPARAM w, LPARAM l) return CallService(MS_PROTO_CHAINSEND, w, l); } -void storeOutput(HANDLE ahandle, string *output) -{ - BOOL success; - char *readbuffer = NULL; - unsigned long transfered, available; - - do { - PeekNamedPipe(ahandle,NULL,0,NULL,&available,NULL); - if (!available) - continue; - readbuffer = (char*)mir_alloc(available); - success=ReadFile(ahandle,readbuffer,available,&transfered,NULL); - if (success && transfered) - output->append(readbuffer, available); - mir_free(readbuffer); - } while (available>0); -} void HistoryLog(HANDLE hContact, db_event evt) { @@ -875,24 +818,21 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pU mir_free(path_c); path_out += _T("\\tmp\\"); path_out += file; - DeleteFile(path_out.c_str()); + boost::filesystem::remove(path_out); wfstream f(path_out.c_str(), std::ios::out); f< cmd; { char *inkeyid; { @@ -925,49 +865,38 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pU } if(pass[0]) { - cmd += _T("--passphrase \""); - cmd += pass; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(pass); } else if(password) { if(bDebugLog) debuglog< cmd; + cmd.push_back(L"--verify"); + cmd.push_back(L"-a"); + cmd.push_back(path_out); + cmd.push_back(status_file_out); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(15))) + if(!gpg_launcher(params, boost::posix_time::seconds(15))) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< cmd; + cmd.push_back(L"--version"); + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; gpg_valid = true; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< cmd; + cmd.push_back(L"--batch"); + cmd.push_back(L"-export-secret-keys"); + cmd.push_back(L"-a"); + gpg_execution_params params(cmd); params.out = &out; params.code = &code; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog< cmd; TCHAR *ptmp; string output; DWORD exitcode; { HANDLE hcnt = hContact; ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - _tcscpy(tmp2, ptmp); + path = ptmp; + mir_free(ptmp); mir_free(ptmp); - _tcscat(tmp2, _T("\\")); - _tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); - wfstream f(tmp2, std::ios::out); + wstring rand = toUTF16(get_random(10)); + path += L"\\"; + path += rand; + boost::filesystem::remove(path); + wfstream f(path, std::ios::out); f< cmd; TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -2053,32 +1941,21 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) mir_free(ptmp); _tcscat(tmp2, _T("\\")); _tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); wfstream f(tmp2, std::ios::out); f<path())) + { + if((i->path().filename().generic_string().length() == 10 && (i->path().filename().generic_string().find(".") == std::string::npos)) || + i->path().extension() == ".sig" || i->path().extension() == ".asc" || i->path().extension() == ".status") + boost::filesystem::remove(i->path()); + } + } + } +} \ No newline at end of file -- cgit v1.2.3