From e7c4081ac0449d998b274d46e6869f69abdecf36 Mon Sep 17 00:00:00 2001 From: Alexander Gluzsky Date: Sat, 12 Jan 2013 06:34:11 +0000 Subject: additional private keys checks additional debug and error messages improoved debug logging removed metacontcats encodings problem workaround git-svn-id: http://svn.miranda-ng.org/main/trunk@3062 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/New_GPG/src/globals.h | 2 +- plugins/New_GPG/src/gpg_wrapper.cpp | 15 ++- plugins/New_GPG/src/log.cpp | 78 +++++++-------- plugins/New_GPG/src/log.h | 1 + plugins/New_GPG/src/main.cpp | 142 +++++++++++++++++++++++----- plugins/New_GPG/src/messages.cpp | 183 ++++++++++++++++++++++-------------- plugins/New_GPG/src/options.cpp | 17 ++-- plugins/New_GPG/src/utilities.cpp | 97 ++++++++++--------- 8 files changed, 341 insertions(+), 194 deletions(-) (limited to 'plugins') diff --git a/plugins/New_GPG/src/globals.h b/plugins/New_GPG/src/globals.h index 4e561d8358..9942e16e96 100755 --- a/plugins/New_GPG/src/globals.h +++ b/plugins/New_GPG/src/globals.h @@ -16,7 +16,7 @@ #ifndef GLOBALS_H #define GLOBALS_H -extern bool bAppendTags, bPresenceSigning, bStripTags, gpg_valid, gpg_keyexist, tabsrmm_used, bSameAction, bFileTransfers; +extern bool bAppendTags, bPresenceSigning, bStripTags, gpg_valid, gpg_keyexist, tabsrmm_used, bSameAction, bFileTransfers, bDebugLog; extern TCHAR *inopentag, *inclosetag, *outopentag, *outclosetag; extern logtofile debuglog; #endif diff --git a/plugins/New_GPG/src/gpg_wrapper.cpp b/plugins/New_GPG/src/gpg_wrapper.cpp index b2c65854cf..f253b2571e 100755 --- a/plugins/New_GPG/src/gpg_wrapper.cpp +++ b/plugins/New_GPG/src/gpg_wrapper.cpp @@ -80,7 +80,8 @@ pxResult pxExecute(wstring *acommandline, char *ainput, string *aoutput, LPDWORD if(errno == ENOENT) { mir_free(bin_path); - debuglog<szModuleName, "metacontacts")) + continue; + if(StriStr(accounts[i]->szModuleName, "weather")) + continue; + std::string acc = toUTF8(accounts[i]->tszAccountName); + acc += "("; + acc += accounts[i]->szModuleName; + acc += ")"; + acc += "_KeyID"; + keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, acc.c_str(), ""); + if(keyid[0]) + { + question = Translate("Your secret key whith id: "); + keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", ""); + key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); + void ShowFirstRunDialog(); + if((p = out.find(keyid)) == string::npos) + { + question += keyid; + question += Translate(" for account "); + question += toUTF8(accounts[i]->tszAccountName); + question += Translate(" deleted from gpg secret keyring\nDo you want to set another key ?"); + if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) + ShowFirstRunDialog(); + } + p2 = p; + p = out.find("[", p); + p2 = out.find("\n", p2); + if((p != std::string::npos) && (p < p2)) + { + p = out.find("expires:", p); + p += strlen("expires:"); + p++; + p2 = out.find("]", p); + TCHAR *expire_date = mir_wstrdup(toUTF16(out.substr(p,p2-p)).c_str()); + bool expired = false; + { + boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); + TCHAR buf[5]; + mir_sntprintf(buf, 5, _T("%s"), expire_date); + int year = _ttoi(buf); + if(year < now.date().year()) + expired = true; + else if(year == now.date().year()) + { + mir_sntprintf(buf, 3, _T("%s"), expire_date+5); + int month = _ttoi(buf); + if(month < now.date().month()) + expired = true; + else if(month == now.date().month()) + { + mir_sntprintf(buf, 3, _T("%s"), expire_date+8); + int day = _ttoi(buf); + if(day <= now.date().day_number()) + expired = true; + } + } + } + if(expired) + { + question += keyid; + question += Translate(" for account "); + question += toUTF8(accounts[i]->tszAccountName); + question += Translate(" expired and will not work\nDo you want to set another key ?"); + if(MessageBoxA(0, question.c_str(), Translate("Own secret key warning"), MB_YESNO) == IDYES) + ShowFirstRunDialog(); + } + mir_free(expire_date); + } + } + mir_free(keyid); + } + question = Translate("Your secret key whith id: "); + keyid = UniGetContactSettingUtf(NULL, szGPGModuleName, "KeyID", ""); + key = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); void ShowFirstRunDialog(); if(!DBGetContactSettingByte(NULL, szGPGModuleName, "FirstRun", 1) && (!keyid[0] || !key[0])) { @@ -2263,7 +2360,8 @@ void ImportKey() gpg_thread.~thread(); TerminateProcess(params.hProcess, 1); params.hProcess = NULL; - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); return 0; } } @@ -399,12 +413,14 @@ int RecvMsgSvc(WPARAM w, LPARAM l) wstring::size_type s1 = wstring::npos, s2 = wstring::npos; if(bAutoExchange && (str.find(_T("-----PGP KEY RESPONSE-----")) != wstring::npos)) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); if(s1 != wstring::npos && s2 != wstring::npos) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); DBWriteContactSettingTString(ccs->hContact, szGPGModuleName, "GPGPubKey", str.substr(s1,s2-s1).c_str()); { //gpg execute block @@ -450,12 +466,18 @@ int RecvMsgSvc(WPARAM w, LPARAM l) gpg_thread.~thread(); TerminateProcess(params.hProcess, 1); params.hProcess = NULL; - debuglog<hContact, db_event(Translate("failed to decrypt message, GPG returned error, turn on debug log for more details"))); + return 1; + } { char *tmp = NULL; string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); @@ -532,7 +554,8 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } if((s2 != wstring::npos) && (s1 != wstring::npos)) { //this is public key - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); s1 = 0; while((s1 = str.find(_T("\r"), s1)) != wstring::npos) str.erase(s1, 1); @@ -554,7 +577,8 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } if(bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && gpg_valid && gpg_keyexist) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); char *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); if(tmp[0]) { @@ -631,25 +655,13 @@ void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) wstring str; bool isansi = false; DWORD dbflags = 0; - if(flags & PREF_UTF) + if((flags & PREF_UTF) == PREF_UTF) dbflags |= DBEF_UTF; - if(!metaIsSubcontact(hContact)) - str = toUTF16(msg); - else - {//workaround ... - debuglog<lParam)); + char *msg = nullptr; + if((ccs->wParam & PREF_UTF) == PREF_UTF) + msg = mir_strdup((char*)(ccs->lParam)); + else + msg = mir_utf8encode((char*)(ccs->lParam)); if (!msg) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } if(strstr(msg,"-----BEGIN PGP MESSAGE-----")) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); return CallService(MS_PROTO_CHAINSEND, w, l); } if(!isContactHaveKey(ccs->hContact)) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); if(bAutoExchange && !strstr(msg, "-----PGP KEY REQUEST-----") && !strstr(msg, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && gpg_valid) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); void send_encrypted_msgs_thread(HANDLE hContact); LPSTR proto = GetContactProto(ccs->hContact); DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); if(uin) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); char *proto = GetContactProto(ccs->hContact); char svc[64]; strcpy(svc, proto); @@ -862,12 +895,14 @@ int SendMsgSvc(WPARAM w, LPARAM l) if(ServiceExists(svc)) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); ICQ_CUSTOMCAP cap = {0}; strcpy(cap.caps, "GPG AutoExchange"); if(CallService(svc, (WPARAM)ccs->hContact, (LPARAM)&cap)) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----"); hcontact_data[ccs->hContact].msgs_to_send.push_back(msg); boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, ccs->hContact)); @@ -881,7 +916,8 @@ int SendMsgSvc(WPARAM w, LPARAM l) TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); if(jid[0]) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); extern list Accounts; list::iterator end = Accounts.end(); for(list::iterator p = Accounts.begin(); p != end; p++) @@ -900,7 +936,8 @@ int SendMsgSvc(WPARAM w, LPARAM l) mir_free(caps); if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----"); hcontact_data[ccs->hContact].msgs_to_send.push_back(msg); boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, ccs->hContact)); @@ -918,13 +955,14 @@ int SendMsgSvc(WPARAM w, LPARAM l) return CallService(MS_PROTO_CHAINSEND, w, l); } } - else + else if(bDebugLog) debuglog<hContact, GCDNF_TCHAR))); - if(metaIsProtoMetaContacts(ccs->hContact)) + if(bDebugLog && metaIsProtoMetaContacts(ccs->hContact)) debuglog<hContact, GCDNF_TCHAR))); if(!isContactSecured(ccs->hContact) || metaIsProtoMetaContacts(ccs->hContact)) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } @@ -946,12 +984,14 @@ int HookSendMsg(WPARAM w, LPARAM l) { if(isContactSecured(hContact) && strstr((char*)dbei->pBlob, "-----BEGIN PGP MESSAGE-----")) //our service data, can be double added by metacontacts e.t.c. { - debuglog<pBlob, "-----PGP KEY RESPONSE-----") || strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----"))) ///do not show service data in history { - debuglog<pBlob)) { hcontact_data[hContact].msgs_to_pass.erase(i); - debuglog<pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+", message is in allowed list"); + if(bDebugLog) + debuglog<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; } @@ -979,7 +1020,8 @@ int HookSendMsg(WPARAM w, LPARAM l) } if(!isContactSecured(hContact)) { - debuglog<pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" is unsecured"); + if(bDebugLog) + debuglog<pBlob+"\" passed event filter, contact "+toUTF8((TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR))+" is unsecured"); return 0; } if(!(dbei->flags & DBEF_SENT) && metaIsProtoMetaContacts((HANDLE)w)) @@ -989,7 +1031,8 @@ int HookSendMsg(WPARAM w, LPARAM l) tmp[28] = '\0'; if(strstr(tmp, "-----BEGIN PGP MESSAGE-----")) { - debuglog< 0) + if(_tcslen(pass) > 0 && bDebugLog) debuglog<hContact, GCDNF_TCHAR))+" with password"); } else { pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T("")); - if(_tcslen(pass) > 0) + if(_tcslen(pass) > 0 && bDebugLog) debuglog<hContact, GCDNF_TCHAR))+" with password"); } if(_tcslen(pass) > 0) @@ -373,12 +373,13 @@ int onProtoAck(WPARAM w, LPARAM l) } else if(password) { - debuglog<hContact, GCDNF_TCHAR))+" with password"); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))+" with password"); cmd += _T("--passphrase \""); cmd += password; cmd += _T("\" "); } - else + else if (bDebugLog) debuglog<hContact, GCDNF_TCHAR))+" with out password"); mir_free(pass); mir_free(keyid); @@ -398,7 +399,8 @@ int onProtoAck(WPARAM w, LPARAM l) delete gpg_thread; TerminateProcess(params.hProcess, 1); params.hProcess = NULL; - debuglog<hContact, GCDNF_TCHAR))+" password needed, trying to get one"); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))+" password needed, trying to get one"); if(_terminate) break; { //save inkey id @@ -426,7 +429,8 @@ int onProtoAck(WPARAM w, LPARAM l) wstring cmd2 = cmd; if(password) { - debuglog<hContact, GCDNF_TCHAR))); + if(bDebugLog) + debuglog<hContact, GCDNF_TCHAR))); wstring tmp = _T("--passphrase \""); tmp += password; tmp += _T("\" "); @@ -446,7 +450,8 @@ int onProtoAck(WPARAM w, LPARAM l) delete gpg_thread; TerminateProcess(params.hProcess, 1); params.hProcess = NULL; - debuglog<