diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-12-30 00:36:07 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-12-30 00:36:07 +0200 |
commit | 4d09e7c6562b4a2119fb044b97e7296074ce83db (patch) | |
tree | 0906594a53a9732953bbaeda5e0b711c5a22c8be /src | |
parent | 340cd4c07beac3df1d2184ab499feb049c285a3f (diff) |
added key id to send key menu/message
Diffstat (limited to 'src')
-rwxr-xr-x | src/utilities.cpp | 301 |
1 files changed, 171 insertions, 130 deletions
diff --git a/src/utilities.cpp b/src/utilities.cpp index 16a3022..cbddeb1 100755 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -194,6 +194,7 @@ INT_PTR SendKey(WPARAM w, LPARAM l) if(metaIsProtoMetaContacts(hContact)) hContact = metaGetMostOnline(hContact); char *szMessage; + std::string key_id_str;
{ LPSTR proto = GetContactProto(hContact);
PROTOACCOUNT *acc = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)proto); @@ -204,6 +205,8 @@ INT_PTR SendKey(WPARAM w, LPARAM l) acc_str += "("; acc_str += acc->szModuleName; acc_str += ")" ; + key_id_str = acc_str;
+ key_id_str += "_KeyID";
acc_str += "_GPGPubKey"; } szMessage = UniGetContactSettingUtf(NULL, szGPGModuleName, acc_str.empty()?"GPGPubKey":acc_str.c_str(), ""); @@ -218,14 +221,27 @@ INT_PTR SendKey(WPARAM w, LPARAM l) BYTE enc = DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)szMessage); - HistoryLog(hContact, db_event("Public key sent", 0, 0, DBEF_SENT)); + std::string msg = "Public key ";
+ char *keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, key_id_str.c_str(), "");
+ if(!keyid[0])
+ {
+ mir_free(keyid);
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ }
+ msg += keyid;
+ mir_free(keyid);
+ msg += " sent";
+ mir_free(szMessage);
+ szMessage = mir_strdup(msg.c_str());
+ HistoryLog(hContact, db_event(szMessage, 0, 0, DBEF_SENT));
DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); } - mir_free(szMessage); + else
+ mir_free(szMessage);
return 0; } -extern HANDLE hLoadPublicKey, hToggleEncryption; +extern HANDLE hLoadPublicKey, hToggleEncryption, hSendKey;
INT_PTR ToggleEncryption(WPARAM w, LPARAM l) { @@ -257,7 +273,6 @@ INT_PTR ToggleEncryption(WPARAM w, LPARAM l) setSrmmIcon(hContact); setClistIcon(hContact); enc = enc?0:1; -
CLISTMENUITEM mi = { sizeof(mi) };
mi.flags = CMIM_NAME; enc?mi.pszName="Turn off GPG encryption":mi.pszName="Turn on GPG encryption"; @@ -271,6 +286,31 @@ int OnPreBuildContactMenu(WPARAM w, LPARAM l) if(metaIsProtoMetaContacts(hContact)) hContact = metaGetMostOnline(hContact); + {
+ CLISTMENUITEM mi2 = { sizeof(mi2) };
+ LPSTR proto = GetContactProto(hContact);
+ PROTOACCOUNT *acc = (PROTOACCOUNT*)CallService(MS_PROTO_GETACCOUNT, 0, (LPARAM)proto);
+ std::string setting;
+ if(acc)
+ {
+ setting = toUTF8(acc->tszAccountName);
+ setting += "(";
+ setting += acc->szModuleName;
+ setting += ")" ;
+ setting += "_KeyID";
+ }
+ char *keyid = keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, setting.c_str(), "");
+ if(!keyid[0])
+ {
+ mir_free(keyid);
+ keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", "");
+ }
+ TCHAR buf[128] = {0};
+ mir_sntprintf(buf, 127 * sizeof(TCHAR), _T("%s: %s"), TranslateT("Send publick key"), toUTF16(keyid).c_str());
+ mi2.ptszName = buf;
+ mi2.flags = CMIM_NAME | CMIF_TCHAR;
+ CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hSendKey, (LPARAM)&mi2);
+ }
CLISTMENUITEM mi = { sizeof(mi) };
mi.flags = CMIM_NAME; TCHAR *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); @@ -304,157 +344,160 @@ int onProtoAck(WPARAM w, LPARAM l) case ACKRESULT_SUCCESS: { PROTOFILETRANSFERSTATUS *f = (PROTOFILETRANSFERSTATUS*) ack->hProcess; - TCHAR *filename = NULL; - if(f->flags & PFTS_UNICODE) - { - if(f->tszCurrentFile && f->tszCurrentFile[0]) - filename = mir_wstrdup(f->tszCurrentFile); - if(!filename) - return 0; - } - else + if((f->flags & PFTS_SENDING) != PFTS_SENDING)
{ - if(f->szCurrentFile && f->szCurrentFile[0]) - filename = mir_utf8decodeT(f->szCurrentFile); - if(!filename) - return 0; - } - if(_tcsstr(filename, _T(".gpg"))) //decrypt it - { //process encrypted file - HistoryLog(ack->hContact, db_event("Recieved encrypted file, trying to decrypt", 0,0, 0));
- if(_waccess(f->tszCurrentFile, 0) == -1) + TCHAR *filename = NULL;
+ if(f->flags & PFTS_UNICODE)
{ - if(errno == ENOENT) + if(f->tszCurrentFile && f->tszCurrentFile[0])
+ filename = mir_wstrdup(f->tszCurrentFile);
+ if(!filename)
return 0; } - string out; - DWORD code; - pxResult result; - wstring cmd = _T(" -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) + else
{ - if(MessageBox(0, _T("Target file exists, do you want to replace it ?"), _T("Warning"), MB_YESNO) == IDNO) + if(f->szCurrentFile && f->szCurrentFile[0])
+ filename = mir_utf8decodeT(f->szCurrentFile);
+ if(!filename)
return 0; } - DeleteFile(file.c_str()); - file.insert(0, _T("\"")); - file.insert(file.length(), _T("\" ")); - cmd += file; - extern TCHAR *password; - { // password - TCHAR *pass = NULL; - char *keyid = UniGetContactSettingUtf(ack->hContact, szGPGModuleName, "KeyID", ""); - if(strlen(keyid) > 0) - { - string dbsetting = "szKey_"; - dbsetting += keyid; - dbsetting += "_Password"; - pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T("")); - if(_tcslen(pass) > 0) - debuglog<<time_str()<<": info: found password in database for key id: "<<keyid<<", trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with password\n"; - } - else + if(_tcsstr(filename, _T(".gpg"))) //decrypt it
+ { //process encrypted file
+ HistoryLog(ack->hContact, db_event("Recieved encrypted file, trying to decrypt", 0,0, 0));
+ if(_waccess(f->tszCurrentFile, 0) == -1)
{ - pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T("")); - if(_tcslen(pass) > 0) - debuglog<<time_str()<<": info: found password for all keys in database, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with password\n"; - } - if(_tcslen(pass) > 0) - { - cmd += _T("--passphrase \""); - cmd += pass; - cmd += _T("\" "); + if(errno == ENOENT)
+ return 0;
} - else if(password) + string out;
+ DWORD code;
+ pxResult result;
+ wstring cmd = _T(" -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)
{ - debuglog<<time_str()<<": info: found password in memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with password\n"; - cmd += _T("--passphrase \""); - cmd += password; - cmd += _T("\" "); + if(MessageBox(0, _T("Target file exists, do you want to replace it ?"), _T("Warning"), MB_YESNO) == IDNO)
+ return 0;
} - else - debuglog<<time_str()<<": info: passwords not found in database or memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with out password\n"; - mir_free(pass); - mir_free(keyid); - } - cmd += _T(" -d \""); - cmd += filename; - cmd += _T("\""); - gpg_execution_params params; - 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::minutes(15))) - { - delete gpg_thread; - TerminateProcess(params.hProcess, 1); - params.hProcess = NULL; - debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - return 0; - } - while(out.find("public key decryption failed: bad passphrase") != string::npos) - { - extern bool _terminate; - extern HANDLE new_key_hcnt; - extern boost::mutex new_key_hcnt_mutex; - debuglog<<time_str()<<": info: failed to decrypt messaage from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" password needed, trying to get one\n"; - if(_terminate) - break; - { //save inkey id - string::size_type s = out.find(" encrypted with "); - s = out.find(" ID ", s); - s += strlen(" ID "); - string::size_type s2 = out.find(",",s); - if(metaIsProtoMetaContacts(ack->hContact)) - DBWriteContactSettingString(metaGetMostOnline(ack->hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); + DeleteFile(file.c_str());
+ file.insert(0, _T("\""));
+ file.insert(file.length(), _T("\" "));
+ cmd += file;
+ extern TCHAR *password;
+ { // password
+ TCHAR *pass = NULL;
+ char *keyid = UniGetContactSettingUtf(ack->hContact, szGPGModuleName, "KeyID", "");
+ if(strlen(keyid) > 0)
+ {
+ string dbsetting = "szKey_";
+ dbsetting += keyid;
+ dbsetting += "_Password";
+ pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T(""));
+ if(_tcslen(pass) > 0)
+ debuglog<<time_str()<<": info: found password in database for key id: "<<keyid<<", trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with password\n";
+ }
else - DBWriteContactSettingString(ack->hContact, szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); - } - void ShowLoadKeyPasswordWindow(); - new_key_hcnt_mutex.lock(); - new_key_hcnt = ack->hContact; - ShowLoadKeyPasswordWindow(); - wstring cmd2 = cmd; - if(password) - { - debuglog<<time_str()<<": info: found password in memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<"\n"; - wstring tmp = _T("--passphrase \""); - tmp += password; - tmp += _T("\" "); - cmd2.insert(0, tmp); + {
+ pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
+ if(_tcslen(pass) > 0)
+ debuglog<<time_str()<<": info: found password for all keys in database, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with password\n";
+ }
+ if(_tcslen(pass) > 0)
+ {
+ cmd += _T("--passphrase \"");
+ cmd += pass;
+ cmd += _T("\" ");
+ }
+ else if(password)
+ {
+ debuglog<<time_str()<<": info: found password in memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with password\n";
+ cmd += _T("--passphrase \"");
+ cmd += password;
+ cmd += _T("\" ");
+ }
+ else
+ debuglog<<time_str()<<": info: passwords not found in database or memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" with out password\n";
+ mir_free(pass);
+ mir_free(keyid);
} - out.clear(); + cmd += _T(" -d \"");
+ cmd += filename;
+ cmd += _T("\"");
gpg_execution_params params; - pxResult result; - params.cmd = &cmd2; + params.cmd = &cmd;
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))) + 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; debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - DeleteFile(filename); return 0; } - if(result == pxNotFound) + while(out.find("public key decryption failed: bad passphrase") != string::npos)
{ - DeleteFile(filename); - return 0; + extern bool _terminate;
+ extern HANDLE new_key_hcnt;
+ extern boost::mutex new_key_hcnt_mutex;
+ debuglog<<time_str()<<": info: failed to decrypt messaage from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<" password needed, trying to get one\n";
+ if(_terminate)
+ break;
+ { //save inkey id
+ string::size_type s = out.find(" encrypted with ");
+ s = out.find(" ID ", s);
+ s += strlen(" ID ");
+ string::size_type s2 = out.find(",",s);
+ if(metaIsProtoMetaContacts(ack->hContact))
+ DBWriteContactSettingString(metaGetMostOnline(ack->hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str());
+ else
+ DBWriteContactSettingString(ack->hContact, szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str());
+ }
+ void ShowLoadKeyPasswordWindow();
+ new_key_hcnt_mutex.lock();
+ new_key_hcnt = ack->hContact;
+ ShowLoadKeyPasswordWindow();
+ wstring cmd2 = cmd;
+ if(password)
+ {
+ debuglog<<time_str()<<": info: found password in memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ack->hContact, GCDNF_TCHAR)<<"\n";
+ wstring tmp = _T("--passphrase \"");
+ tmp += password;
+ tmp += _T("\" ");
+ cmd2.insert(0, tmp);
+ }
+ out.clear();
+ gpg_execution_params params;
+ 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)))
+ {
+ delete gpg_thread;
+ TerminateProcess(params.hProcess, 1);
+ params.hProcess = NULL;
+ debuglog<<time_str()<<": GPG execution timed out, aborted\n";
+ DeleteFile(filename);
+ return 0;
+ }
+ if(result == pxNotFound)
+ {
+ DeleteFile(filename);
+ return 0;
+ }
} - } - DeleteFile(filename); - mir_free(filename); + DeleteFile(filename);
+ mir_free(filename);
+ }
} } break; @@ -516,7 +559,6 @@ std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) path_out += file_out; DeleteFile(path_out.c_str()); cmd += _T("\" "); - mir_free(temp); cmd += _T(" -e \""); cmd += filename; cmd += _T("\" "); @@ -526,7 +568,6 @@ std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) params.out = &out; params.code = &code; params.result = &result; - mir_free(keyid); 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))) |