diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-08 22:52:02 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-08 22:52:02 +0200 |
commit | dd4c07b6f71dc074dabef6b27e5dd4c49d882bb2 (patch) | |
tree | 27327c4d7f813ec03519bdc5068d83eb49b1ea45 | |
parent | 5657453dcc8626b9f7f13f7ade1eb3e5afa13cc8 (diff) |
merged with miranda_ng main repo
"boosted" process management
evnt_to_db/history>actual_sending logic to avoid srmm problem
-rwxr-xr-x | gpg_wrapper.cpp | 225 | ||||
-rwxr-xr-x | gpg_wrapper.h | 30 | ||||
-rwxr-xr-x | init.cpp | 6 | ||||
-rwxr-xr-x | main.cpp | 442 | ||||
-rwxr-xr-x | main.h | 2 | ||||
-rwxr-xr-x | messages.cpp | 388 | ||||
-rwxr-xr-x | options.cpp | 107 | ||||
-rwxr-xr-x | utilities.cpp | 400 | ||||
-rwxr-xr-x | utilities.h | 1 |
9 files changed, 670 insertions, 931 deletions
diff --git a/gpg_wrapper.cpp b/gpg_wrapper.cpp index adce958..5f9df7d 100755 --- a/gpg_wrapper.cpp +++ b/gpg_wrapper.cpp @@ -15,186 +15,148 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h" - //thx gpg module from Harald Treder, Zakhar V. Bardymov -//boost::mutex gpg_mutex; - -pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr) +pxResult pxExecute(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, boost::process::child *_child) { -// gpg_mutex.lock(); if(!gpg_valid) return pxNotConfigured; extern logtofile debuglog; - BOOL success; - STARTUPINFO sinfo = {0}; - SECURITY_ATTRIBUTES sattrs = {0}; - SECURITY_DESCRIPTOR sdesc = {0}; - PROCESS_INFORMATION pri = {0}; - HANDLE newstdin, newstdout, readstdout, writestdin; - char *inputpos; - unsigned long transfered; - int size; - - wstring commandline; - - sattrs.nLength=sizeof(SECURITY_ATTRIBUTES); - sattrs.bInheritHandle=TRUE; - InitializeSecurityDescriptor(&sdesc,SECURITY_DESCRIPTOR_REVISION); - SetSecurityDescriptorDacl(&sdesc,TRUE,NULL,FALSE); - sattrs.lpSecurityDescriptor=&sdesc; - - success=CreatePipe(&newstdin,&writestdin,&sattrs,0); - if (!success) - { - *result = pxCreatePipeFailed; - return pxCreatePipeFailed; - } - - success=CreatePipe(&readstdout,&newstdout,&sattrs,0); - if (!success) - { - CloseHandle(newstdin); - CloseHandle(writestdin); - *result = pxCreatePipeFailed; - return pxCreatePipeFailed; - } - - GetStartupInfo(&sinfo); - sinfo.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; - sinfo.wShowWindow=SW_HIDE; - sinfo.hStdOutput=newstdout; - sinfo.hStdError=newstdout; - sinfo.hStdInput=newstdin; - - char *mir_path = new char [MAX_PATH]; - CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); - SetCurrentDirectoryA(mir_path); - delete [] mir_path; + TCHAR *bin_path = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", _T("")); { - if(_waccess(bin_path, 0) == -1) + if(!boost::filesystem::exists(bin_path)) { - if(errno == ENOENT) - { - mir_free(bin_path); - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG executable not found"); - *result = pxNotFound; - return pxNotFound; - } + mir_free(bin_path); + if(bDebugLog) + debuglog<<std::string(time_str()+": GPG executable not found"); + *result = pxNotFound; + return pxNotFound; } } - TCHAR *home_dir = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - { //form initial command - commandline += _T("\""); - commandline += bin_path; - commandline += _T("\" --homedir \""); - commandline += home_dir; - commandline += _T("\" "); - commandline += _T("--display-charset utf-8 "); - commandline += _T("-z 9 "); - commandline += *acommandline; - mir_free(bin_path); - mir_free(home_dir); - } - if(bDebugLog) - debuglog<<std::string(time_str()+": gpg in: "+toUTF8(commandline)); + using namespace boost::process; + using namespace boost::process::initializers; + using namespace boost::iostreams; - success = CreateProcess(NULL, (TCHAR*)commandline.c_str(), NULL, NULL, TRUE, CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, (void*)_T("LANGUAGE=en@quot\0LC_ALL=English\0"), NULL, &sinfo, &pri); - if (!success) + std::vector<std::wstring> argv; + std::vector<std::wstring> env; + env.push_back(L"LANGUAGE=en@quot"); + env.push_back(L"LC_ALL=English"); + argv.push_back(bin_path); + argv.push_back(L"--homedir"); + TCHAR *home_dir = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + argv.push_back(home_dir); + mir_free(home_dir); + argv.push_back(L"--display-charset"); + argv.push_back(L"utf-8"); + argv.push_back(L"-z9"); + argv.insert(argv.end(), aargv.begin(), aargv.end()); + + if(bDebugLog) { - CloseHandle(newstdin); - CloseHandle(writestdin); - CloseHandle(newstdout); - CloseHandle(readstdout); - if(bDebugLog) - debuglog<<time_str()<<": Failed to create process\n"; -// gpg_mutex.unlock(); - *result = pxCreateProcessFailed; - return pxCreateProcessFailed; + std::wstring args; + for(int i = 0; i < argv.size(); ++i) + { + args += argv[i]; + args += L" "; + } + args.erase(args.size()-1, 1); + debuglog<<std::string(time_str()+": gpg in: "+toUTF8(args)); } - hProcess = pri.hProcess; - - inputpos=ainput; - while (true) + pipe pout = create_pipe(); { - if(!pri.hProcess) - break; - success=GetExitCodeProcess(pri.hProcess,aexitcode); - if (success && *aexitcode!=STILL_ACTIVE) - break; + file_descriptor_sink sout(pout.sink, close_handle); - storeOutput(readstdout,aoutput); + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); - if (*inputpos!='\0') size=1; - else size=0; + child c = execute(set_args(argv), bind_stdout(sout), /*bind_stdin(sin),*/ show_window(SW_HIDE), hide_console(), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); + _child = &c; - success=WriteFile(writestdin,inputpos,size,&transfered,NULL); - inputpos+=transfered; - boost::this_thread::sleep(boost::posix_time::milliseconds(50)); + delete [] mir_path; + auto ec = wait_for_exit(*_child); + *aexitcode = ec; + _child = nullptr; } + + file_descriptor_source source(pout.source, close_handle); - storeOutput(readstdout,aoutput); + stream<file_descriptor_source> is(source); + try{ + std::string s; + while(std::getline(is, s)) + { + aoutput->append(s); + aoutput->append("\n"); + } + } + catch(const std::exception &e) + { + if(bDebugLog) + debuglog<<std::string(time_str()+": failed to read from stream with error: " + e.what() + "\n\tSuccesfully read : " + *aoutput); + } fix_line_term(*aoutput); if(bDebugLog) debuglog<<std::string(time_str()+": gpg out: "+*aoutput); - WaitForSingleObject(pri.hProcess,INFINITE); - - CloseHandle(pri.hThread); - CloseHandle(pri.hProcess); - CloseHandle(newstdin); - CloseHandle(newstdout); - CloseHandle(readstdout); - CloseHandle(writestdin); - *result = pxSuccess; -// gpg_mutex.unlock(); if(*aexitcode) { if(bDebugLog) debuglog<<std::string(time_str()+": warning: wrong gpg exit status, gpg output: "+*aoutput); return pxSuccessExitCodeInvalid; } + + return pxSuccess; } -void pxEexcute_thread(void *param) +void pxEexcute_thread(gpg_execution_params ¶ms) +{ + pxExecute(params.aargv, params.out, params.code, params.result, params.child); +} + +bool gpg_launcher(gpg_execution_params ¶ms, boost::posix_time::time_duration t) { - gpg_execution_params *params = (gpg_execution_params*)param; - pxResult result = pxExecute(params->cmd, params->useless, params->out, params->code, params->result, params->hProcess, params->proc); + bool ret = true; + boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, params)); + if(!gpg_thread->timed_join(t)) + { + ret = false; + delete gpg_thread; + if(params.child) + boost::process::terminate(*(params.child)); + if(bDebugLog) + debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + } + return ret; } -pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr, string &old_pass, string &new_pass) +pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, boost::process::child *_child, string &old_pass, string &new_pass) { -// gpg_mutex.lock(); if(!gpg_valid) return pxNotConfigured; extern logtofile debuglog; TCHAR *bin_path = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", _T("")); { - if(_waccess(bin_path, 0) == -1) + if(!boost::filesystem::exists(bin_path)) { - if(errno == ENOENT) - { - mir_free(bin_path); - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG executable not found"); - *result = pxNotFound; - return pxNotFound; - } + mir_free(bin_path); + if(bDebugLog) + debuglog<<std::string(time_str()+": GPG executable not found"); + *result = pxNotFound; + return pxNotFound; } } @@ -218,16 +180,16 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, argv.insert(argv.end(), aargv.begin(), aargv.end()); // pipe pout = create_pipe(); - pipe pin = create_pipe(); +// pipe pin = create_pipe(); // file_descriptor_sink sout(pout.sink, close_handle); - file_descriptor_source sin(pin.source, close_handle); +// file_descriptor_source sin(pin.source, close_handle); char *mir_path = new char [MAX_PATH]; CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); //execute(set_args(argv), bind_stdout(sout), bind_stdin(sin), show_window(SW_HIDE), hide_console(), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); - child c = execute(set_args(argv), bind_stdin(sin), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); - //child c = execute(run_exe("c:\\windows\\system32\\cmd.exe"), bind_stdin(sin), inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); + child c = execute(set_args(argv), /*bind_stdin(sin), */inherit_env(), set_env(env), start_in_dir(toUTF16(mir_path))); + _child = &c; delete [] mir_path; @@ -247,7 +209,8 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, // out<<toUTF8(cmd)<<std::endl; //fucked gpg does not want to give us stdin/stdout - wait_for_exit(c); + wait_for_exit(*_child); + _child = nullptr; /* out<<old_pass<<std::endl; out<<new_pass<<std::endl; @@ -348,5 +311,5 @@ pxResult pxExecute_passwd_change(std::vector<std::wstring> &aargv, char *ainput, void pxEexcute_passwd_change_thread(void *param) { gpg_execution_params_pass *params = (gpg_execution_params_pass*)param; - pxResult result = pxExecute_passwd_change(params->args, params->useless, params->out, params->code, params->result, params->hProcess, params->proc, params->old_pass, params->new_pass); + pxResult result = pxExecute_passwd_change(params->args, params->out, params->code, params->result, params->child, params->old_pass, params->new_pass); } diff --git a/gpg_wrapper.h b/gpg_wrapper.h index 44b2f4f..9ff0957 100755 --- a/gpg_wrapper.h +++ b/gpg_wrapper.h @@ -32,36 +32,44 @@ typedef enum { } pxResult; -pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr); -pxResult pxExecute_passwd_change(std::vector<std::string> &aargv, char *ainput, string *aoutput, LPDWORD aexitcode, pxResult *result, HANDLE hProcess, PROCESS_INFORMATION *pr, string &old_pass, string &new_pass); +pxResult pxExecute(std::vector<std::string> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result); +pxResult pxExecute_passwd_change(std::vector<std::string> &aargv, string *aoutput, LPDWORD aexitcode, pxResult *result, string &old_pass, string &new_pass); struct gpg_execution_params { - wstring *cmd; - char *useless; + std::vector<std::wstring> &aargv; +// char *useless; string *out; LPDWORD code; pxResult *result; - HANDLE hProcess; - PROCESS_INFORMATION *proc; + boost::process::child *child; +// HANDLE hProcess; +// PROCESS_INFORMATION *proc; + gpg_execution_params(std::vector<std::wstring> &a): aargv(a) + { + child = nullptr; + } }; struct gpg_execution_params_pass { std::vector<std::wstring> &args; string &old_pass, &new_pass; - char *useless; string *out; LPDWORD code; pxResult *result; - HANDLE hProcess; - PROCESS_INFORMATION *proc; + boost::process::child *child; +// HANDLE hProcess; +// PROCESS_INFORMATION *proc; gpg_execution_params_pass(std::vector<std::wstring> &a, std::string &o, std::string &n): args(a), old_pass(o), new_pass(n) - {} + { + child = nullptr; + } }; -void pxEexcute_thread(void *param); +void pxEexcute_thread(gpg_execution_params ¶ms); +bool gpg_launcher(gpg_execution_params ¶ms, boost::posix_time::time_duration t = boost::posix_time::seconds(10)); void pxEexcute_passwd_change_thread(void *param); #endif
\ No newline at end of file @@ -266,7 +266,8 @@ static int OnModulesLoaded(WPARAM wParam,LPARAM lParam) CreateProtoServiceFunction(szGPGModuleName, PSS_MESSAGE"W", (MIRANDASERVICE)SendMsgSvc);
CreateProtoServiceFunction(szGPGModuleName, PSS_FILE, (MIRANDASERVICE)onSendFile);
- CreateProtoServiceFunction(szGPGModuleName, PSS_FILE"W", (MIRANDASERVICE)onSendFile);
+ CreateProtoServiceFunction(szGPGModuleName, PSS_FILE"W", (MIRANDASERVICE)onSendFile); + clean_temp_dir();
for (HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0); hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0))
if (!CallService(MS_PROTO_ISPROTOONCONTACT, (WPARAM)hContact, (LPARAM)szGPGModuleName))
@@ -287,7 +288,7 @@ extern "C" int __declspec(dllexport) Unload(void) {
for(list<wstring>::iterator p = transfers.begin(); p != transfers.end(); p++)
if(!(*p).empty())
- DeleteFile((*p).c_str());
+ boost::filesystem::remove((*p));
}
mir_free(inopentag);
mir_free(inclosetag);
@@ -295,5 +296,6 @@ extern "C" int __declspec(dllexport) Unload(void) mir_free(outclosetag);
if(password)
mir_free(password);
+ clean_temp_dir();
return 0;
}
@@ -101,21 +101,15 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR pxResult result; wstring::size_type p = 0, p2 = 0, stop = 0; { - gpg_execution_params params; - wstring cmd = _T("--batch --list-secret-keys"); - params.cmd = &cmd; - params.useless = ""; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ 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))) + if(!gpg_launcher(params))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break; } if(result == pxNotFound) @@ -306,24 +300,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR } string out; DWORD code; - wstring cmd = _T("--batch -a --export "); - cmd += fp; -// cmd += _T("\""); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(fp);
+ 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(10))) + if(!gpg_launcher(params))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break; } if(result == pxNotFound) @@ -398,22 +386,16 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR DWORD code; wstring::size_type p = 0, p2 = 0, stop = 0; { - wstring cmd = _T("--batch --list-secret-keys"); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ 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(10))) + if(!gpg_launcher(params))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break; } if(result == pxNotFound) @@ -471,23 +453,17 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR { string out; DWORD code; - wstring cmd = _T("--batch --fingerprint "); - cmd += fp; - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--fingerprint");
+ cmd.push_back(fp);
+ 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 = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params))
{ - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break; } if(result == pxNotFound) @@ -507,17 +483,13 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR } cmd.clear(); out.clear(); - cmd += _T("--batch --delete-secret-and-public-key --fingerprint "); - cmd += fp; + cmd.push_back(L"--batch");
+ cmd.push_back(L"--delete-secret-and-public-key");
+ cmd.push_back(L"--fingerprint");
+ cmd.push_back(fp);
mir_free(fp); - gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params))
{ - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break; } if(result == pxNotFound) @@ -594,14 +566,13 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR { //gpg execution DWORD code; string out; - wstring cmd; - cmd += _T("--batch --yes --gen-key \""); - cmd += path; - cmd += _T("\""); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"--gen-key");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; @@ -613,20 +584,14 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR EnableWindow(GetDlgItem(hwndDlg, IDC_DELETE_KEY), 0); EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_LIST), 0); EnableWindow(GetDlgItem(hwndDlg, IDC_GENERATE_RANDOM), 0); - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::minutes(10))) + if(!gpg_launcher(params, boost::posix_time::minutes(10)))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break; } if(result == pxNotFound) break; - DeleteFile(path.c_str()); + boost::filesystem::remove(path);
string::size_type p1 = 0; if((p1 = out.find("key ")) != string::npos) path = toUTF16(out.substr(p1+4, 8)); @@ -637,23 +602,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR { string out; DWORD code; - wstring cmd = _T("--batch -a --export "); - cmd += path; - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(path);
+ 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(10))) + if(!gpg_launcher(params))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+"GPG execution timed out, aborted");
break; } if(result == pxNotFound) @@ -720,23 +680,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export ");
- cmd += fp;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(fp);
+ 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(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -790,23 +745,18 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR ListView_GetItemText(hwndList, itemnum, 0, fp, 16);
string out;
DWORD code;
- wstring cmd = _T("--batch -a --export-secret-keys ");
- cmd += fp;
- gpg_execution_params params;
+ std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export-secret-keys");
+ cmd.push_back(fp);
+ 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(10)))
+ if(!gpg_launcher(params))
{
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
break;
}
if(result == pxNotFound)
@@ -832,7 +782,6 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR cmd.push_back(L"passwd");
gpg_execution_params_pass params(cmd, old_pass, new_pass);
pxResult result;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
@@ -840,8 +789,8 @@ static INT_PTR CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPAR if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
{
gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
+ if(params.child)
+ boost::process::terminate(*(params.child));
if(bDebugLog)
debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
DestroyWindow(hwndDlg);
@@ -922,12 +871,12 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, _tcscpy(gpg_lang_path, tmp); _tcscat(gpg_lang_path, _T("\\GnuPG\\gnupg.nls\\en@quot.mo")); mir_free(tmp); - if(_waccess(gpg_path, 0) != -1) + if(boost::filesystem::exists(gpg_path))
{ gpg_exists = true; _tcscpy(path, _T("GnuPG\\gpg.exe")); } - if(_waccess(gpg_lang_path, 0) != -1) + if(boost::filesystem::exists(gpg_lang_path))
lang_exists = true; if(gpg_exists && !lang_exists)
MessageBox(0, TranslateT("gpg binary found in miranda folder, but english locale does not exists.\nit's highly recommended to place \\gnupg.nls\\en@quot.mo in gnupg folder under miranda root.\nwithout this file you may expirense many problem with gpg output on non english systems.\nand plugin may completely do not work.\nyou have beed warned."), TranslateT("Warning"), MB_OK); @@ -940,10 +889,9 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szGpgBinPath", (SHGetValue(HKEY_CURRENT_USER, _T("Software\\GNU\\GnuPG"), _T("gpgProgram"), 0, path, &len) == ERROR_SUCCESS)?path:_T("")); if(tmp[0]) { - if(_waccess(tmp, 0) == -1) + if(!boost::filesystem::exists(tmp))
{ - if(errno == ENOENT) - MessageBox(0, TranslateT("wrong gpg binary location found in system.\nplease choose another location"), TranslateT("Warning"), MB_OK); + MessageBox(0, TranslateT("wrong gpg binary location found in system.\nplease choose another location"), TranslateT("Warning"), MB_OK);
} /* char *mir_path = (char*)mir_alloc(MAX_PATH);
CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path);
@@ -961,24 +909,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); string out; DWORD code; - wstring cmd = _T("--version"); - gpg_execution_params params; + std::vector<wstring> 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<<std::string(time_str()+": GPG execution timed out, aborted");
- } + gpg_launcher(params);
gpg_valid = false; DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); @@ -1079,13 +1018,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); SetCurrentDirectoryA(mir_path); delete [] mir_path; - if(_waccess(tmp, 0) == -1) + if(!boost::filesystem::exists(tmp))
{ - if(errno == ENOENT) - { - MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK); - break; - } + MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK);
+ break;
} } else @@ -1098,24 +1034,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); string out; DWORD code; - wstring cmd = _T("--version"); - gpg_execution_params params; + std::vector<wstring> 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))) - { - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - gpg_thread.~thread(); - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- } + gpg_launcher(params);
gpg_valid = false; DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); @@ -1169,13 +1096,10 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); SetCurrentDirectoryA(mir_path); delete [] mir_path; - if(_waccess(tmp, 0) == -1) + if(!boost::filesystem::exists(tmp))
{ - if(errno == ENOENT) - { - MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK); - break; - } + MessageBox(0, TranslateT("gpg binary does not exists.\nplease choose another location"), TranslateT("Warning"), MB_OK);
+ break;
} } else @@ -1188,24 +1112,15 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); string out; DWORD code; - wstring cmd = _T("--version"); - gpg_execution_params params; + std::vector<wstring> 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))) - { - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - gpg_thread.~thread(); - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- } + gpg_launcher(params);
gpg_valid = false; DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); @@ -1278,33 +1193,26 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, { //gpg execution DWORD code; string out; - wstring cmd; - cmd += _T("--batch --yes --gen-key \""); - cmd += path; - cmd += _T("\""); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"--gen-key");
+ cmd.push_back(path);
+ 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::minutes(10))) + if(!gpg_launcher(params, boost::posix_time::minutes(10)))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
gpg_valid = false; break; } gpg_valid = false; if(result == pxNotFound) break; - DeleteFile(path.c_str()); + boost::filesystem::remove(path);
string::size_type p1 = 0; if((p1 = out.find("key ")) != string::npos) path = toUTF16(out.substr(p1+4, 8)); @@ -1315,24 +1223,19 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, { string out; DWORD code; - wstring cmd = _T("--batch -a --export "); - cmd += path; - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(path);
+ 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))) + if(!gpg_launcher(params))
{ - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+"GPG execution timed out, aborted");
gpg_valid = false; break; } @@ -1637,14 +1540,13 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara { //gpg execution DWORD code; string out; - wstring cmd; - cmd += _T("--batch --yes --gen-key \""); - cmd += path; - cmd += _T("\""); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"--gen-key");
+ cmd.push_back(path);
+ gpg_execution_params params(cmd);
pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &out; params.code = &code; params.result = &result; @@ -1660,20 +1562,12 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_EMAIL), 0); EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_COMMENT), 0); EnableWindow(GetDlgItem(hwndDlg, IDC_KEY_EXPIRE_DATE), 0); - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::minutes(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params, boost::posix_time::minutes(10)))
break; - } if(result == pxNotFound) break; } - DeleteFile(path.c_str()); + boost::filesystem::remove(path);
DestroyWindow(hwndDlg); {//parse gpg output LVITEM item = {0}; @@ -1686,24 +1580,15 @@ static INT_PTR CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wPara DWORD code; string::size_type p = 0, p2 = 0, stop = 0; { - wstring cmd = _T("--list-secret-keys"); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--list-secret-keys");
+ 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(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break; - } if(result == pxNotFound) break; } @@ -1849,24 +1734,16 @@ static INT_PTR CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wPar DWORD code; string::size_type p = 0, p2 = 0, stop = 0; { - wstring cmd = _T("--batch --list-keys"); - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-keys");
+ 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(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break; - } if(result == pxNotFound) break; } @@ -1958,25 +1835,18 @@ static INT_PTR CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wPar extern HWND hPubKeyEdit; string out; DWORD code; - wstring cmd = _T("--batch -a --export "); - cmd += id; - gpg_execution_params params; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"-a");
+ cmd.push_back(L"--export");
+ cmd.push_back(id);
+ 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(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break; - } if(result == pxNotFound) break; string::size_type s = 0; @@ -2077,33 +1947,24 @@ static INT_PTR CALLBACK DlgProcImportKeyDialog(HWND hwndDlg, UINT msg, WPARAM wP { string out; DWORD code; - wstring cmd = _T(" --keyserver \""); + std::vector<wstring> cmd;
+ cmd.push_back(L"--keyserver");
TCHAR *server= new TCHAR [128]; GetDlgItemText(hwndDlg, IDC_KEYSERVER, server, 128); - cmd += server; + cmd.push_back(server);
delete [] server; - cmd += _T("\" --recv-keys "); + cmd.push_back(L"--recv-keys");
// char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID_Prescense", ""); // TCHAR *tmp2 = mir_a2t(tmp); // mir_free(tmp); - cmd += toUTF16(hcontact_data[hContact].key_in_prescense); + cmd.push_back(toUTF16(hcontact_data[hContact].key_in_prescense));
// mir_free(tmp2); - gpg_execution_params params; + 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(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog)
- debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
- } + gpg_launcher(params);
MessageBoxA(0, out.c_str(), "GPG output", MB_OK); } break; @@ -2218,7 +2079,7 @@ void InitCheck() if(test_file.good()) home_dir_access = true; test_file.close(); - DeleteFile(test_path.c_str()); + boost::filesystem::remove(test_path);
} home_dir = _tgetenv(_T("TEMP")); test_path = home_dir; @@ -2231,7 +2092,7 @@ void InitCheck() if(test_file.good()) temp_access = true; test_file.close(); - DeleteFile(test_path.c_str()); + boost::filesystem::remove(test_path);
} if(!home_dir_access || !temp_access || !gpg_valid) { @@ -2251,23 +2112,15 @@ void InitCheck() pxResult result; wstring::size_type p = 0, p2 = 0, stop = 0; { - gpg_execution_params params; - wstring cmd = _T("--batch --list-secret-keys"); - params.cmd = &cmd; - params.useless = ""; + std::vector<wstring> cmd;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--list-secret-keys");
+ 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<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
return; - } if(result == pxNotFound) return; } @@ -2502,7 +2355,7 @@ void ImportKey() DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", new_key.c_str()); new_key.clear(); { //gpg execute block - wstring cmd; + std::vector<wstring> cmd;
TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -2513,7 +2366,7 @@ void ImportKey() mir_free(ptmp); _tcscat(tmp2, _T("\\")); _tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2);
wfstream f(tmp2, std::ios::out); if(metaIsProtoMetaContacts(hContact)) ptmp = UniGetContactSettingUtf(metaGetMostOnline(hContact), szGPGModuleName, "GPGPubKey", _T("")); @@ -2523,28 +2376,17 @@ void ImportKey() mir_free(ptmp); f<<new_key.c_str(); f.close(); - cmd += _T(" --batch "); - cmd += _T(" --import \""); - cmd += tmp2; - cmd += _T("\""); + cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(tmp2);
} - gpg_execution_params params; + gpg_execution_params params(cmd);
pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; 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<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
return; - } if(result == pxNotFound) return; { @@ -2772,6 +2614,6 @@ void ImportKey() ptmp = mir_wstrdup(toUTF16(output).c_str()); MessageBox(0, ptmp, _T(""), MB_OK); mir_free(ptmp); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2);
} }
\ No newline at end of file @@ -18,7 +18,7 @@ struct contact_data
{
- list<string> msgs_to_send, msgs_to_pass;
+ list<string> msgs_to_send;// msgs_to_pass;
string key_in_prescense;
};
diff --git a/messages.cpp b/messages.cpp index 1821035..7a4667f 100755 --- a/messages.cpp +++ b/messages.cpp @@ -86,7 +86,7 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, wstring decfile = toUTF16(get_random(10)); path.append(_T("\\tmp\\")); path.append(encfile); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); fstream f(path.c_str(), std::ios::out); while(!f.is_open()) f.open(path.c_str(), std::ios::out); @@ -97,8 +97,8 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, extern TCHAR *password; string out; DWORD code; - wstring cmd; - cmd += _T("--batch "); + std::vector<wstring> cmd; + cmd.push_back(L"--batch"); { char *inkeyid = UniGetContactSettingUtf(metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact, szGPGModuleName, "InKeyID", ""); TCHAR *pass = NULL; @@ -119,17 +119,15 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } if(pass && pass[0]) { - cmd += _T("--passphrase \""); - cmd += pass; - cmd += _T("\" "); + cmd.push_back(L"--passphrase"); + cmd.push_back(pass); } else if(password && password[0]) { if(bDebugLog) debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)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<<std::string(time_str()+": info: passwords not found in database or memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" with out password"); @@ -140,32 +138,21 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, wstring path = tmp2; path += _T("\\tmp\\"); path += decfile; - DeleteFile(path.c_str()); + boost::filesystem::remove(path); } - cmd += _T("--output \""); - cmd += tmp2; - cmd += _T("\\tmp\\"); - cmd += decfile; - cmd += _T("\""); - cmd += _T(" -d -a \""); - cmd += path; - cmd += _T("\""); - gpg_execution_params params; + cmd.push_back(L"--output"); + cmd.push_back(std::wstring(tmp2) + L"\\tmp\\" + decfile); + cmd.push_back(L"-d"); + cmd.push_back(L"-a"); + cmd.push_back(path); + 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 = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); @@ -176,13 +163,13 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } if(result == pxNotFound) { - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); return; } if(result == pxSuccessExitCodeInvalid) { - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); HistoryLog(hContact, db_event(Translate("failed to decrypt message, GPG returned error, turn on debug log for more details"), timestamp, 0, 0)); return; @@ -214,33 +201,25 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, new_key_hcnt_mutex.lock(); new_key_hcnt = hContact; ShowLoadKeyPasswordWindow(); - wstring cmd2 = cmd; + std::vector<wstring> cmd2 = cmd; if(password) { if(bDebugLog) debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); - wstring tmp = _T("--passphrase \""); - tmp += password; - tmp += _T("\" "); - cmd2.insert(0, tmp); + std::vector<wstring> 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(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); @@ -251,21 +230,15 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } if(result == pxNotFound) { - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); return; } } out.clear(); - gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); @@ -276,38 +249,35 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, } if(result == pxNotFound) { - DeleteFile(path.c_str()); + boost::filesystem::remove(path); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); } { wstring tmp = tmp2; tmp += _T("\\tmp\\"); tmp += encfile; - DeleteFile(tmp.c_str()); + boost::filesystem::remove(tmp); } { wstring tmp = tmp2; tmp += _T("\\tmp\\"); tmp += decfile; - if(_waccess(tmp.c_str(), 0) == -1) + if(!boost::filesystem::exists(tmp)) { - if(errno == ENOENT) - { - string str = msg; - str.insert(0, "Received unencrypted message:\n"); - if(bDebugLog) - debuglog<<std::string(time_str()+": info: Failed to decrypt GPG encrypted message."); - char *tmp = new char [str.length()+1]; - strcpy(tmp, str.c_str()); - HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); - DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); - CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"Unable to decrypt PGP encrypted message"); - HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); - DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); - mir_free(tmp); - return; - } + string str = msg; + str.insert(0, "Received unencrypted message:\n"); + if(bDebugLog) + debuglog<<std::string(time_str()+": info: Failed to decrypt GPG encrypted message."); + char *tmp = new char [str.length()+1]; + strcpy(tmp, str.c_str()); + HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); + BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); + DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); + CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"Unable to decrypt PGP encrypted message"); + HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); + DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); + mir_free(tmp); + return; } } str.clear(); @@ -328,7 +298,7 @@ void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, str.append(toUTF16(tmp)); delete [] tmp; f.close(); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); } if(str.empty()) { @@ -424,7 +394,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); DBWriteContactSettingTString(ccs->hContact, szGPGModuleName, "GPGPubKey", str.substr(s1,s2-s1).c_str()); { //gpg execute block - wstring cmd; + std::vector<wstring> cmd; TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -439,7 +409,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) _tcscat(tmp2, _T(".asc")); mir_free(tmp3); //_tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); wfstream f(tmp2, std::ios::out); while(!f.is_open()) f.open(tmp2, std::ios::out); @@ -448,29 +418,18 @@ int RecvMsgSvc(WPARAM w, LPARAM l) mir_free(ptmp); f<<new_key.c_str(); f.close(); - cmd += _T(" --batch "); - cmd += _T(" --import \""); - cmd += tmp2; - cmd += _T("\""); + cmd.push_back(L"--batch"); + cmd.push_back(L"--import"); + cmd.push_back(tmp2); } - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; 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<<std::string(time_str()+": GPG execution timed out, aborted"); + if(!gpg_launcher(params)) return 1; - } - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); if(result == pxNotFound) return 1; if(result == pxSuccessExitCodeInvalid) @@ -683,39 +642,43 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) str.replace(i, 2, _T("\n")); */ string out; DWORD code; - wstring cmd; - wstring file = toUTF16(get_random(10)); - wstring path; + wstring file = toUTF16(get_random(10)), path; + std::vector<std::wstring> cmd; extern bool bJabberAPI, bIsMiranda09; char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); if(!tmp[0]) { mir_free(tmp); HistoryLog(hContact, db_event("Failed to encrypt message with GPG (not found key for encryption in db)", 0,0, DBEF_SENT)); - hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG (not found key for encryption in db)"); - mir_free(msg); +// hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG (not found key for encryption in db)"); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } if(!bJabberAPI || !bIsMiranda09) //force jabber to handle encrypted message by itself - cmd += _T("--comment \"\" --no-version "); + { + cmd.push_back(L"--comment"); + cmd.push_back(L"\"\""); + cmd.push_back(L"--no-version"); + } if(DBGetContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 0)) - cmd += _T("--trust-model always "); - cmd += _T("--batch --yes -e -a -t -r "); + { + cmd.push_back(L"--trust-model"); + cmd.push_back(L"always"); + } + cmd.push_back(L"--batch"); + cmd.push_back(L"--yes"); + cmd.push_back(L"-eatr"); TCHAR *tmp2 = mir_a2t(tmp); mir_free(tmp); - cmd += tmp2; + cmd.push_back(tmp2); mir_free(tmp2); - cmd += _T(" \""); tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - path.append(tmp2); - cmd += tmp2; + path = tmp2; + cmd.push_back(std::wstring(tmp2) + L"\\tmp\\" + file); mir_free(tmp2); - cmd += _T("\\tmp\\"); - cmd += file; - path.append(_T("\\tmp\\")); + path += L"\\tmp\\"; path += file; - cmd += _T("\""); { fstream f(path.c_str(), std::ios::out); while(!f.is_open()) @@ -727,28 +690,20 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) f.write(tmp.c_str(), tmp.size()); f.close(); } - gpg_execution_params params; + 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(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - mir_free(msg); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } if(result == pxNotFound) { - mir_free(msg); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } @@ -758,29 +713,24 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) if(MessageBox(0, TranslateT("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), TranslateT("Warning"), MB_YESNO) == IDYES) { DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1); - cmd.insert(0, _T("--trust-model always ")); - gpg_execution_params params; + std::vector<std::wstring> tmp; + tmp.push_back(L"--trust-model"); + tmp.push_back(L"always"); + cmd.insert(cmd.begin(), tmp.begin(), tmp.end()); + 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(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - mir_free(msg); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } if(result == pxNotFound) { - mir_free(msg); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } @@ -788,25 +738,26 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) } else { - mir_free(msg); + //mir_free(msg); return; } } if(result == pxSuccessExitCodeInvalid) { - mir_free(msg); + //mir_free(msg); HistoryLog(hContact, db_event(Translate("failed o encrypt message, GPG returned error, turn on debug log for more details"), 0,0, DBEF_SENT)); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); return; } if(out.find("usage: ") != string::npos) { MessageBox(0, TranslateT("Something wrong, gpg does not understand us, aborting encryption."), TranslateT("Warning"), MB_OK); - mir_free(msg); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); return; } + boost::filesystem::remove(path); path.append(_T(".asc")); wfstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); while(!f.is_open()) @@ -825,15 +776,15 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) str.append(tmp); delete [] tmp; f.close(); - DeleteFile(path.c_str()); + boost::filesystem::remove(path); } if(str.empty()) { HistoryLog(hContact, db_event("Failed to encrypt message with GPG", 0,0, DBEF_SENT)); - hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG"); +// hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG"); if(bDebugLog) debuglog<<std::string(time_str()+": info: Failed to encrypt message with GPG"); - mir_free(msg); + //mir_free(msg); CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); return; } @@ -853,15 +804,15 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) } HistoryLog(metaGetContact(hContact), db_event((char*)str_event.c_str(), 0,0, DBEF_SENT|dbflags)); }*/ //unneeded ? - hcontact_data[hContact].msgs_to_pass.push_back(str_event); +// hcontact_data[hContact].msgs_to_pass.push_back(str_event); if(bDebugLog) debuglog<<std::string(time_str()+": adding event to contact: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" on send message."); - HistoryLog(hContact, db_event((char*)str_event.c_str(), 0,0, dbflags|DBEF_SENT)); + //HistoryLog(hContact, db_event((char*)str_event.c_str(), 0,0, dbflags|DBEF_SENT)); if(!(flags & PREF_UTF)) flags |= PREF_UTF; fix_line_term(str); sent_msgs.push_back((HANDLE)CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)toUTF8(str).c_str())); - mir_free(msg); + //mir_free(msg); return; } @@ -888,7 +839,7 @@ int SendMsgSvc(WPARAM w, LPARAM l) debuglog<<std::string(time_str()+": info: encrypted messge, let it go, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR))); return CallService(MS_PROTO_CHAINSEND, w, l); } - if(!isContactHaveKey(ccs->hContact)) + /*if(!isContactHaveKey(ccs->hContact)) { if(bDebugLog) debuglog<<std::string(time_str()+": info: contact have not key, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR))); @@ -969,7 +920,7 @@ int SendMsgSvc(WPARAM w, LPARAM l) mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } - } + } */ else if(bDebugLog) debuglog<<std::string(time_str()+": info: contact have key, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR))); if(bDebugLog && metaIsProtoMetaContacts(ccs->hContact)) @@ -981,7 +932,8 @@ int SendMsgSvc(WPARAM w, LPARAM l) mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } - boost::thread *thr = new boost::thread(boost::bind(SendMsgSvc_func, ccs->hContact, msg, (DWORD)ccs->wParam)); + mir_free(msg); + //boost::thread *thr = new boost::thread(boost::bind(SendMsgSvc_func, ccs->hContact, msg, (DWORD)ccs->wParam)); return returnNoError(ccs->hContact); } @@ -1010,28 +962,136 @@ int HookSendMsg(WPARAM w, LPARAM l) return 1; } } - if(isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering + if(metaIsProtoMetaContacts(hContact)) + return 0; + + if(!isContactHaveKey(hContact)) { - if(!hcontact_data[hContact].msgs_to_pass.empty()) + if(bDebugLog) + debuglog<<std::string(time_str()+": info: contact have not key, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + if(bAutoExchange && !strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----") && !strstr((char*)dbei->pBlob, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && gpg_valid) { - event_processing_mutex.lock(); - std::list<string>::iterator end = hcontact_data[hContact].msgs_to_pass.end(); - for(std::list<string>::iterator i = hcontact_data[hContact].msgs_to_pass.begin(); i != end; ++i) + if(bDebugLog) + debuglog<<std::string(time_str()+": info: checking for autoexchange possibility, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + void send_encrypted_msgs_thread(HANDLE hContact); + LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); + DWORD uin = DBGetContactSettingDword(hContact, proto, "UIN", 0); + if(uin) + { + if(bDebugLog) + debuglog<<std::string(time_str()+": info(autoexchange): protocol looks like icq, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + char *proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); + char svc[64]; + strcpy(svc, proto); + strcat(svc, PS_ICQ_CHECKCAPABILITY); + + if(ServiceExists(svc)) + { + if(bDebugLog) + debuglog<<std::string(time_str()+": info(autoexchange, icq): checking for autoexchange icq capability, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + ICQ_CUSTOMCAP cap = {0}; + strcpy(cap.caps, "GPG AutoExchange"); + if(CallService(svc, (WPARAM)hContact, (LPARAM)&cap)) + { + if(bDebugLog) + debuglog<<std::string(time_str()+": info(autoexchange, icq): sending key requiest, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + CallContactService(hContact, PSS_MESSAGE, (WPARAM)((dbei->flags & DBEF_UTF) == DBEF_UTF)?PREF_UTF:0, (LPARAM)"-----PGP KEY REQUEST-----"); + hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob); + boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, hContact)); + //TODO: wait for message + return 0; + } + } + } + else { - if(!strcmp((*i).c_str(), (char*)dbei->pBlob)) + TCHAR *jid = UniGetContactSettingUtf(hContact, proto, "jid", _T("")); + if(jid[0]) { - hcontact_data[hContact].msgs_to_pass.erase(i); if(bDebugLog) - debuglog<<std::string(time_str()+": event message: \""+(char*)dbei->pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+", message is in allowed list"); - event_processing_mutex.unlock(); - return 0; + debuglog<<std::string(time_str()+": info(autoexchange): protocol looks like jabber, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + extern list <JabberAccount*> Accounts; + list<JabberAccount*>::iterator end = Accounts.end(); + for(list<JabberAccount*>::iterator p = Accounts.begin(); p != end; p++) + { + TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); + if(caps) + { + wstring str; + for(int i=0;;i++) + { + str.push_back(caps[i]); + if(caps[i] == '\0') + if(caps[i+1] == '\0') + break; + } + mir_free(caps); + if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) + { + if(bDebugLog) + debuglog<<std::string(time_str()+": info(autoexchange, jabber): autoexchange capability found, sending key request, name: "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))); + CallContactService(hContact, PSS_MESSAGE, (WPARAM)((dbei->flags & DBEF_UTF) == DBEF_UTF)?PREF_UTF:0, (LPARAM)"-----PGP KEY REQUEST-----"); + hcontact_data[hContact].msgs_to_send.push_back((char*)dbei->pBlob); + boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, hContact)); + mir_free((char*)dbei->pBlob); + //TODO: wait for message + return 0; + } + } + } } } - event_processing_mutex.unlock(); } - if(metaIsProtoMetaContacts(hContact) && !isContactSecured(metaGetMostOnline(hContact))) + else + { return 0; - return 1; + } + } + if(isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering + { + DWORD flags; + if((dbei->flags & DBEF_UTF) == DBEF_UTF) + flags |= PREF_UTF; + SendMsgSvc_func(hContact, (char*)dbei->pBlob, flags); + //TODO: handle errors somehow ... + if(bAppendTags) + { + string str_event = (char*)dbei->pBlob; + mir_free(dbei->pBlob); + str_event.insert(0, toUTF8(outopentag)); + str_event.append(toUTF8(outclosetag)); + dbei->pBlob = (PBYTE)mir_strdup(str_event.c_str()); + dbei->cbBlob = str_event.length() + 1; + } + + return 0; +/* bool stop = false; + int count = 0; */ +/* while(!stop) + { + if(count >= 300) + stop = true; + if(!hcontact_data[hContact].msgs_to_pass.empty()) + { + event_processing_mutex.lock(); + std::list<string>::iterator end = hcontact_data[hContact].msgs_to_pass.end(); + for(std::list<string>::iterator i = hcontact_data[hContact].msgs_to_pass.begin(); i != end; ++i) + { + if(!strcmp((*i).c_str(), (char*)dbei->pBlob)) + { + hcontact_data[hContact].msgs_to_pass.erase(i); + if(bDebugLog) + debuglog<<std::string(time_str()+": event message: \""+(char*)dbei->pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+", message is in allowed list"); + event_processing_mutex.unlock(); + return 0; + } + } + event_processing_mutex.unlock(); + } + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + count++; + } */ + //return 1; } if(!isContactSecured(hContact)) { diff --git a/options.cpp b/options.cpp index 8360a26..c7b89f5 100755 --- a/options.cpp +++ b/options.cpp @@ -258,28 +258,22 @@ static INT_PTR CALLBACK DlgProcGpgOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP if(!keep) if(MessageBox(0, TranslateT("This key not used by any contact, do you want to remove it from public keyring ?"), TranslateT("Key info"), MB_YESNO) == IDYES) { - wstring cmd; + std::vector<wstring> cmd; string output; DWORD exitcode; - cmd += _T(" --batch --yes --delete-key "); + cmd.push_back(L"--batch"); + cmd.push_back(L"--yes"); + cmd.push_back(L"--delete-key"); ptmp = mir_a2t(tmp); - cmd += ptmp; + cmd.push_back(ptmp); mir_free(ptmp); - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(!gpg_launcher(params)) { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); mir_free(tmp); break; } @@ -525,21 +519,14 @@ static INT_PTR CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); string out; DWORD code; - wstring cmd = _T("--version"); - gpg_execution_params params; + std::vector<wstring> 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; - 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; - } + gpg_launcher(params); DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp_path); mir_free(tmp_path); string::size_type p1 = out.find("(GnuPG) "); @@ -844,26 +831,16 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam { string out; DWORD code; - wstring cmd = _T(" --export -a "); -// TCHAR *tmp3 = mir_a2t(tmp); - cmd += toUTF16(hcontact_data[hcnt].key_in_prescense); -// mir_free(tmp3); - gpg_execution_params params; + std::vector<wstring> cmd; + cmd.push_back(L"--export"); + cmd.push_back(L"-a"); + cmd.push_back(toUTF16(hcontact_data[hcnt].key_in_prescense)); + 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(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); - } + gpg_launcher(params); //TODO: handle errors if((out.find("-----BEGIN PGP PUBLIC KEY BLOCK-----") != string::npos) && (out.find("-----END PGP PUBLIC KEY BLOCK-----") != string::npos)) { string::size_type p = 0, stop = 0; @@ -971,7 +948,7 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam tmp = (TCHAR*)mir_alloc(sizeof( TCHAR) * (key_buf.length()+1)); _tcscpy(tmp, key_buf.substr(ws1,ws2-ws1).c_str()); { //gpg execute block - wstring cmd; + std::vector<wstring> cmd; TCHAR tmp2[MAX_PATH] = {0}; TCHAR *ptmp; string output; @@ -985,7 +962,7 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam mir_free(ptmp); _tcscat(tmp2, _T("\\")); _tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); wfstream f(tmp2, std::ios::out); ptmp = UniGetContactSettingUtf(hcnt, szGPGModuleName, "GPGPubKey", _T("")); wstring str = ptmp; @@ -997,28 +974,17 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam } f<<str.c_str(); f.close(); - cmd += _T(" --batch "); - cmd += _T(" --import \""); - cmd += tmp2; - cmd += _T("\""); + cmd.push_back(L"--batch"); + cmd.push_back(L"--import"); + cmd.push_back(tmp2); } - gpg_execution_params params; + gpg_execution_params params(cmd); pxResult result; - params.cmd = &cmd; - params.useless = ""; params.out = &output; params.code = &exitcode; 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<<std::string(time_str()+": GPG execution timed out, aborted"); + if(!gpg_launcher(params)) break; - } if(result == pxNotFound) break; mir_free(begin); @@ -1049,7 +1015,7 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam if(output.find("already in secret keyring") != string::npos) { MessageBox(0, TranslateT("Key already in scret key ring."), TranslateT("Info"), MB_OK); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); break; } char *tmp2; @@ -1247,26 +1213,19 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam { string out; DWORD code; - wstring cmd = _T("--batch -a --export "); - cmd += fp; + std::vector<wstring> cmd; + cmd.push_back(L"--batch"); + cmd.push_back(L"-a"); + cmd.push_back(L"--export"); + cmd.push_back(fp); mir_free(fp); - gpg_execution_params params; + 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(10))) - { - gpg_thread.~thread(); - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - if(bDebugLog) - debuglog<<std::string(time_str()+": GPG execution timed out, aborted"); + if(!gpg_launcher(params)) break; - } if(result == pxNotFound) break; string::size_type s = 0; @@ -1280,7 +1239,7 @@ static INT_PTR CALLBACK DlgProcLoadPublicKey(HWND hwndDlg,UINT msg,WPARAM wParam tmp = mir_wstrdup(toUTF16(output).c_str()); MessageBox(0, tmp, _T(""), MB_OK); mir_free(tmp); - DeleteFile(tmp2); + boost::filesystem::remove(tmp2); } key_buf.clear(); if(IsDlgButtonChecked(hwndDlg, IDC_ENABLE_ENCRYPTION)) 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<wstring> 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<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->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<<std::string(time_str()+": info: passwords not found in database or memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->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<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params, boost::posix_time::minutes(15)))
return 0;
- }
while(out.find("public key decryption failed: bad passphrase") != string::npos)
{
extern bool _terminate;
@@ -478,43 +461,35 @@ int onProtoAck(WPARAM w, LPARAM l) new_key_hcnt_mutex.lock();
new_key_hcnt = ack->hContact;
ShowLoadKeyPasswordWindow();
- wstring cmd2 = cmd;
+ std::vector<wstring> cmd2 = cmd;
if(password)
{
if(bDebugLog)
debuglog<<std::string(time_str()+": info: found password in memory, trying to decrypt message from "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)));
- wstring tmp = _T("--passphrase \"");
- tmp += password;
- tmp += _T("\" ");
- cmd2.insert(0, tmp);
+ std::vector<wstring> 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<<std::string(time_str()+": GPG execution timed out, aborted");
- //DeleteFile(filename);
+ //boost::filesystem::remove(filename);
return 0;
}
if(result == pxNotFound)
{
- //DeleteFile(filename);
+ //boost::filesystem::remove(filename);
return 0;
}
}
if(result == pxSuccess)
- DeleteFile(filename);
+ boost::filesystem::remove(filename);
mir_free(filename);
}
}
@@ -553,7 +528,10 @@ std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) DWORD code;
pxResult result;
HANDLE hcnt = metaIsProtoMetaContacts(hContact)?metaGetMostOnline(hContact):hContact;
- wstring cmd = _T("--batch --yes -r ");
+ std::vector<wstring> 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<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params, boost::posix_time::minutes(3)))
return 0;
- }
if(out.find("There is no assurance this key belongs to the named user") != string::npos)
{
out.clear();
if(MessageBox(0, TranslateT("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), TranslateT("Warning"), MB_YESNO) == IDYES)
{
DBWriteContactSettingByte(hcnt, szGPGModuleName, "bAlwaysTrust", 1);
- cmd.insert(0, _T("--trust-model always "));
- 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<<std::string(time_str()+": GPG execution timed out, aborted");
+ std::vector<std::wstring> 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<<toUTF8(str).c_str();
f.close();
- if(_waccess(path_out.c_str(), 0) == -1)
+ if(!boost::filesystem::exists(path_out))
{
- if(errno == ENOENT)
- {
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: Failed to write prescense in file");
- return FALSE;
- }
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: Failed to write prescense in file");
+ return FALSE;
}
{
extern TCHAR *password;
string out;
DWORD code;
- wstring cmd;
+ std::vector<wstring> 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<<std::string(time_str()+": info: found password in memory, trying to encrypt message from self with password");
- cmd += _T("--passphrase \"");
- cmd += password;
- cmd += _T("\" ");
+ cmd.push_back(L"--passphrase");
+ cmd.push_back(password);
}
else if (bDebugLog)
debuglog<<std::string(time_str()+": info: passwords not found in database or memory, trying to encrypt message from self with out password");
mir_free(pass);
mir_free(inkeyid);
}
- cmd += _T("--local-user ");
+ cmd.push_back(L"--local-user");
path_c = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", _T(""));
- cmd += path_c;
- cmd += _T(" --default-key ");
- cmd += path_c;
+ cmd.push_back(path_c);
+ cmd.push_back(L"--default-key");
+ cmd.push_back(path_c);
mir_free(path_c);
- cmd += _T(" --batch --yes -a -b -s \"");
- cmd += path_out;
- cmd += _T("\" ");
- gpg_execution_params params;
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--yes");
+ cmd.push_back(L"-abs");
+ cmd.push_back(path_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)))
- {
- gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
- if(bDebugLog)
- debuglog<<std::string(time_str()+"GPG execution timed out, aborted");
- }
- DeleteFile(path_out.c_str());
+ gpg_launcher(params, boost::posix_time::seconds(15)); // TODO: handle errors
+ boost::filesystem::remove(path_out);
path_out += _T(".asc");
f.open(path_out.c_str(), std::ios::in | std::ios::ate | std::ios::binary);
wstring data;
@@ -981,7 +910,7 @@ static JABBER_HANDLER_FUNC SendHandler(IJabberInterface *ji, HXML node, void *pU data.append(tmp);
delete [] tmp;
f.close();
- DeleteFile(path_out.c_str());
+ boost::filesystem::remove(path_out);
}
if(data.empty())
{
@@ -1084,8 +1013,8 @@ static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface *ji, HXML node, voi status_file_out += status_file;
status_file_out += L".status";
// sign_file_mutex.lock();
- DeleteFile(path_out.c_str());
- DeleteFile(status_file_out.c_str());
+ boost::filesystem::remove(path_out);
+ boost::filesystem::remove(status_file_out);
wfstream f(path_out.c_str(), std::ios::out);
while(!f.is_open())
f.open(path_out.c_str(), std::ios::out);
@@ -1096,48 +1025,36 @@ static JABBER_HANDLER_FUNC PrescenseHandler(IJabberInterface *ji, HXML node, voi f.open(status_file_out.c_str(), std::ios::out);
f<<toUTF8(status_str).c_str();
f.close();
- if(_waccess(path_out.c_str(), 0) == -1)
+ if(!boost::filesystem::exists(path_out))
{
- if(errno == ENOENT)
- {
// sign_file_mutex.unlock();
- if(bDebugLog)
- debuglog<<std::string(time_str()+": info: Failed to write sign in file");
- return FALSE;
- }
+ if(bDebugLog)
+ debuglog<<std::string(time_str()+": info: Failed to write sign in file");
+ return FALSE;
}
{ //gpg
string out;
DWORD code;
- wstring cmd = L" --verify -a \"";
- cmd += path_out;
- cmd += L"\"";
- cmd += L" \"";
- cmd += status_file_out;
- cmd += L"\"";
- gpg_execution_params params;
+ std::vector<wstring> 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<<std::string(time_str()+": GPG execution timed out, aborted");
return FALSE;
}
if(result == pxNotFound)
{
return FALSE;
}
- DeleteFile(path_out.c_str());
- DeleteFile(status_file_out.c_str());
+ boost::filesystem::remove(path_out);
+ boost::filesystem::remove(status_file_out);
if(out.find("key ID ") != string::npos)
{
//need to get hcontact here, i can get jid from hxml, and get handle from jid, maybe exists better way ?
@@ -1294,24 +1211,15 @@ bool isGPGValid() DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp);
string out;
DWORD code;
- wstring cmd = _T("--version");
- gpg_execution_params params;
+ std::vector<wstring> 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<<std::string(time_str()+": GPG execution timed out, aborted");
- }
+ gpg_launcher(params);
gpg_valid = false;
string::size_type p1 = out.find("(GnuPG) ");
if(p1 == string::npos)
@@ -1381,7 +1289,7 @@ int SendBroadcast( HANDLE hContact, int type, int result, HANDLE hProcess, LPARA ack.hContact = hContact;
ack.type = type;
ack.result = result;
- ack.hProcess = hProcess;
+ ack.hProcess = (HANDLE)777;//hProcess;
ack.lParam = lParam;
return CallService( MS_PROTO_BROADCASTACK, 0, ( LPARAM )&ack );
}
@@ -1681,26 +1589,15 @@ void ExportGpGKeysFunc(int type) string out;
DWORD code;
pxResult result;
- gpg_execution_params params;
- wstring cmd = _T("--batch --export-secret-keys -a");
- params.cmd = &cmd;
- params.useless = "";
+ std::vector<wstring> 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<<std::string(time_str()+": GPG execution timed out, aborted");
- //break;
- }
- else if(result == pxNotFound)
- ;//break;
- else
+ gpg_launcher(params); //TODO: handle errors
{
file<<out;
file<<std::endl;
@@ -1919,44 +1816,35 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) }
if(found)
{
- wstring cmd;
- TCHAR tmp2[MAX_PATH] = {0};
+ wstring path;
+ std::vector<std::wstring> 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<<toUTF16(key).c_str();
f.close();
- cmd += _T(" --batch ");
- cmd += _T(" --import \"");
- cmd += tmp2;
- cmd += _T("\"");
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(path);
}
- gpg_execution_params params;
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
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<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
if(result == pxSuccess)
@@ -1965,7 +1853,7 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) if(output.find("already in secret keyring") != string::npos)
{
MessageBox(0, TranslateT("Key already in scret key ring."), TranslateT("Info"), MB_OK);
- DeleteFile(tmp2);
+ boost::filesystem::remove(path);
break;
}
char *tmp2;
@@ -2033,7 +1921,7 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1);
DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", toUTF16(key).c_str());
}
- DeleteFile(tmp2);
+ boost::filesystem::remove(path);
break;
}
}
@@ -2042,7 +1930,7 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) }
if(strstr(line, "-----END PGP PRIVATE KEY BLOCK-----"))
{
- wstring cmd;
+ std::vector<wstring> 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<<toUTF16(key).c_str();
f.close();
- cmd += _T(" --batch ");
- cmd += _T(" --import \"");
- cmd += tmp2;
- cmd += _T("\"");
+ cmd.push_back(L"--batch");
+ cmd.push_back(L"--import");
+ cmd.push_back(tmp2);
}
- gpg_execution_params params;
+ gpg_execution_params params(cmd);
pxResult result;
- params.cmd = &cmd;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
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<<std::string(time_str()+": GPG execution timed out, aborted");
+ if(!gpg_launcher(params))
break;
- }
if(result == pxNotFound)
break;
if(result == pxSuccess)
@@ -2321,16 +2198,15 @@ static INT_PTR CALLBACK DlgProcChangePasswd(HWND hwndDlg, UINT msg, WPARAM wPara cmd.push_back(L"passwd");
gpg_execution_params_pass params(cmd, old_pass, new_pass);
pxResult result;
- params.useless = "";
params.out = &output;
params.code = &exitcode;
params.result = &result;
boost::thread gpg_thread(boost::bind(&pxEexcute_passwd_change_thread, ¶ms));
- if(!gpg_thread.timed_join(boost::posix_time::seconds(100)))
+ if(!gpg_thread.timed_join(boost::posix_time::minutes(10)))
{
gpg_thread.~thread();
- TerminateProcess(params.hProcess, 1);
- params.hProcess = NULL;
+ if(params.child)
+ boost::process::terminate(*(params.child));
if(bDebugLog)
debuglog<<std::string(time_str()+": GPG execution timed out, aborted");
DestroyWindow(hwndDlg);
@@ -2376,3 +2252,31 @@ void ShowChangePasswdDlg() hwndPaaswdDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_CHANGE_PASSWD), NULL, DlgProcChangePasswd);
SetForegroundWindow(hwndPaaswdDlg);
}
+ +
+void clean_temp_dir()
+{
+ using namespace boost::filesystem; + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + wstring path = toUTF16(mir_path); + SetCurrentDirectoryA(mir_path); + delete [] mir_path; + TCHAR *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ path += tmp;
+ mir_free(tmp);
+ path += L"\\tmp";
+ if(exists(path) && is_directory(path))
+ {
+ boost::filesystem::path p(path);
+ for(directory_iterator i = directory_iterator(p), end = directory_iterator(); i != end; ++i)
+ {
+ if(boost::filesystem::is_regular_file(i->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 diff --git a/utilities.h b/utilities.h index 508a241..eaa7ef7 100755 --- a/utilities.h +++ b/utilities.h @@ -105,5 +105,6 @@ void fix_line_term(std::wstring &s); void strip_line_term(std::wstring &s);
void strip_line_term(std::string &s);
void strip_tags(std::wstring &s);
+void clean_temp_dir();
#endif
|