diff options
author | Gluzskiy Alexandr <sss123next@list.ru> | 2010-11-01 15:35:20 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss123next@list.ru> | 2010-11-01 15:35:20 +0200 |
commit | fb9e64ea76b6363b57a4a82d47da0280bb0a1429 (patch) | |
tree | 14ad4ba37b3b96ea0e7fc75e989e9d445eb9c2e7 | |
parent | 0922841b20da6c15076f1467280a0e67407826d1 (diff) |
modified: gpg_wrapper.cpp
modified: main.cpp
modified: new_gpg.rc
-rw-r--r-- | gpg_wrapper.cpp | 4 | ||||
-rw-r--r-- | main.cpp | 191 | ||||
-rw-r--r-- | new_gpg.rc | 9 |
3 files changed, 196 insertions, 8 deletions
diff --git a/gpg_wrapper.cpp b/gpg_wrapper.cpp index 8ab32a3..de44233 100644 --- a/gpg_wrapper.cpp +++ b/gpg_wrapper.cpp @@ -130,9 +130,8 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD success=WriteFile(writestdin,inputpos,size,&transfered,NULL); inputpos+=transfered; - Sleep(200); + boost::this_thread::sleep(boost::posix_time::milliseconds(200)); } - gpg_mutex.unlock(); storeOutput(readstdout,aoutput); @@ -148,6 +147,7 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD CloseHandle(writestdin); *result = pxSuccess; + gpg_mutex.unlock(); return pxSuccess; } @@ -463,7 +463,6 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM SetWindowText(hwndCurKey_p, path.c_str()); } } - bAutoExchange = CheckStateStoreDB(hwndDlg, IDC_AUTO_EXCHANGE, "bAutoExchange"); DestroyWindow(hwndDlg); break; } @@ -574,9 +573,11 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP params.out = &out; params.code = &code; params.result = &result; + gpg_configured = true; boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) gpg_thread.~thread(); + gpg_configured = false; DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); if(p1 != string::npos) @@ -685,9 +686,11 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP params.out = &out; params.code = &code; params.result = &result; + gpg_configured = true; boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) gpg_thread.~thread(); + gpg_configured = false; DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); string::size_type p1 = out.find("(GnuPG) "); if(p1 != string::npos) @@ -724,11 +727,195 @@ static BOOL CALLBACK DlgProcGpgBinOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LP }
mir_free(path); } + gpg_configured = true; DestroyWindow(hwndDlg); ShowFirstRunDialog(); - break; } + break; + case IDC_GENERATE_RANDOM: + { + TCHAR tmp[512]; + GetDlgItemText(hwndDlg, IDC_BIN_PATH, tmp, 512); + if(tmp[0]) + { + char *mir_path = new char [MAX_PATH]; + CallService(MS_UTILS_PATHTOABSOLUTE, (WPARAM)"\\", (LPARAM)mir_path); + SetCurrentDirectoryA(mir_path); + delete [] mir_path; + if(_waccess(tmp, 0) == -1) + { + if(errno == ENOENT) + { + MessageBox(0, _T("gpg binary does not exists.\nplease choose another location"), _T("Warning"), MB_OK); + break; + } + } + } + else + { + MessageBox(0, _T("please choose gpg binary location"), _T("Warning"), MB_OK); + break; + } + { + bool bad_version = false; + DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); + string out; + DWORD code; + wstring cmd = _T("--version"); + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + gpg_configured = true; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + gpg_thread.~thread(); + gpg_configured = false; + DBDeleteContactSetting(NULL, szGPGModuleName, "szGpgBinPath"); + string::size_type p1 = out.find("(GnuPG) "); + if(p1 != string::npos) + { + p1 += strlen("(GnuPG) "); + if(out[p1] != '1') + bad_version = true; + } + else + { + bad_version = false; + MessageBox(0, _T("This is not gnupg binary !\nrecommended to use GnuPG v1.x.x with this plugn."), _T("Warning"), MB_OK); + } + if(bad_version) + MessageBox(0, _T("Unsupported gnupg version found, use at you own risk!\nrecommended to use GnuPG v1.x.x with this plugn."), _T("Warning"), MB_OK); + } + DBWriteContactSettingTString(NULL, szGPGModuleName, "szGpgBinPath", tmp); + GetDlgItemText(hwndDlg, IDC_HOME_DIR, tmp, 512); + while(tmp[_tcslen(tmp)-1] == '\\') + tmp[_tcslen(tmp)-1] = '\0'; + if(!tmp[0]) + { + MessageBox(0, _T("please set keyring's home directory"), _T("Warning"), MB_OK); + break; + }
+ DBWriteContactSettingTString(NULL, szGPGModuleName, "szHomePath", tmp);
+ {
+ TCHAR *path = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
+ DWORD dwFileAttr = GetFileAttributes(path);
+ if (dwFileAttr != INVALID_FILE_ATTRIBUTES)
+ {
+ dwFileAttr &=~ FILE_ATTRIBUTE_READONLY;
+ SetFileAttributes(path, dwFileAttr);
+ } + mir_free(path); + } + } + { + wstring path; + { //generating key file + TCHAR *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + path = tmp; + mir_free(tmp); + path.append(_T("\\new_key")); + wfstream f(path.c_str(), std::ios::out); + if(!f.is_open()) + { + MessageBox(0, _T("Failed to open file"), _T("Error"), MB_OK); + break; + } + f<<"Key-Type: RSA"; + f<<"\n"; + f<<"Key-Length: 2048"; + f<<"\n"; + f<<"Subkey-Type: RSA"; + f<<"\n"; + f<<"Name-Real: "; + f<<get_random(6).c_str(); + f<<"\n"; + f<<"Name-Email: "; + f<<get_random(5).c_str(); + f<<"@"; + f<<get_random(5).c_str(); + f<<"."; + f<<get_random(3).c_str(); + f<<"\n"; + f.close(); + } + { //gpg execution + DWORD code; + string out; + wstring cmd; + cmd += _T("--batch --yes --gen-key \""); + cmd += path; + cmd += _T("\""); + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + gpg_configured = true; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::minutes(10))) + { + gpg_thread.~thread(); + debuglog<<time_str()<<": GPG execution timed out, aborted"; + gpg_configured = false; + break; + } + gpg_configured = false; + if(result == pxNotFound) + break; + DeleteFile(path.c_str()); + string::size_type p1 = 0; + if((p1 = out.find("key ")) != string::npos) + path = toUTF16(out.substr(p1+4, 8)); + else + path.clear(); + } + if(!path.empty()) + { + string out; + DWORD code; + wstring cmd = _T("--batch -a --export "); + cmd += path; + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + gpg_configured = true; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + { + gpg_thread.~thread(); + debuglog<<time_str()<<"GPG execution timed out, aborted\n"; + gpg_configured = false; + break; + } + gpg_configured = false; + if(result == pxNotFound) + break; + string::size_type s = 0; + while((s = out.find("\r", s)) != string::npos) + { + out.erase(s, 1); + } + DBWriteContactSettingString(NULL, szGPGModuleName, "GPGPubKey", out.c_str()); + DBWriteContactSettingTString(NULL, szGPGModuleName, "KeyID", path.c_str()); + extern HWND hwndCurKey_p; + SetWindowText(hwndCurKey_p, path.c_str()); + } + } + bAutoExchange = CheckStateStoreDB(hwndDlg, IDC_AUTO_EXCHANGE, "bAutoExchange"); + gpg_configured = true; + DestroyWindow(hwndDlg); + break; default: break; } @@ -77,21 +77,22 @@ BEGIN PUSHBUTTON "Load other",IDC_OTHER,73,131,56,14
PUSHBUTTON "Delete key",IDC_DELETE_KEY,132,131,56,14
PUSHBUTTON "Generate and use random key",IDC_GENERATE_RANDOM,99,163,118,14
- CONTROL "Turn on key autoexchange",IDC_AUTO_EXCHANGE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,101,180,113,10
END
-IDD_BIN_PATH DIALOGEX 0, 0, 354, 89
+IDD_BIN_PATH DIALOGEX 0, 0, 354, 108
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Set GPG bin path and keyring home dir."
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
- DEFPUSHBUTTON "خت",ID_OK,7,68,50,14
+ DEFPUSHBUTTON "خت",ID_OK,7,87,50,14
LTEXT "Home directory:",IDC_STATIC,9,34,325,8
LTEXT "GnuPG binary:",IDC_STATIC,10,7,147,8
PUSHBUTTON "Browse",IDC_SET_BIN_PATH,293,18,38,14
PUSHBUTTON "Browse",IDC_SET_HOME_DIR,293,44,38,14
EDITTEXT IDC_HOME_DIR,7,44,277,14,ES_AUTOHSCROLL
EDITTEXT IDC_BIN_PATH,7,17,277,14,ES_AUTOHSCROLL
+ PUSHBUTTON "Generate and use random key",IDC_GENERATE_RANDOM,7,66,118,14
+ CONTROL "Turn on key autoexchange",IDC_AUTO_EXCHANGE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,133,68,113,10
END
IDD_NEW_KEY DIALOGEX 0, 0, 427, 68
@@ -198,7 +199,7 @@ BEGIN RIGHTMARGIN, 347
VERTGUIDE, 284
TOPMARGIN, 7
- BOTTOMMARGIN, 82
+ BOTTOMMARGIN, 101
END
IDD_NEW_KEY, DIALOG
|