From 270ca4cfb74b5bbcff9402264bc476b9c79028c1 Mon Sep 17 00:00:00 2001 From: Alexander Gluzsky Date: Thu, 24 Jan 2013 19:26:22 +0000 Subject: some memory allocation bugs fixed enhanced key export/import git-svn-id: http://svn.miranda-ng.org/main/trunk@3267 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/New_GPG/res/new_gpg.rc | 30 ++- plugins/New_GPG/src/init.cpp | 4 +- plugins/New_GPG/src/options.cpp | 18 ++ plugins/New_GPG/src/resource.h | 9 +- plugins/New_GPG/src/utilities.cpp | 428 +++++++++++++++++++++++++------------- 5 files changed, 345 insertions(+), 144 deletions(-) (limited to 'plugins') diff --git a/plugins/New_GPG/res/new_gpg.rc b/plugins/New_GPG/res/new_gpg.rc index b86a849cb3..e93473f664 100755 --- a/plugins/New_GPG/res/new_gpg.rc +++ b/plugins/New_GPG/res/new_gpg.rc @@ -1,6 +1,6 @@ // Microsoft Visual C++ generated resource script. // -#include "..\src\resource.h" +#include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// @@ -183,6 +183,20 @@ BEGIN CONTROL "Remember choice",IDC_REMEMBER,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,17,22,157,10 END +IDD_EXPORT_TYPE DIALOGEX 0, 0, 281, 90 +STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +CAPTION "Choose which keys to export" +FONT 8, "MS Shell Dlg", 400, 0, 0x1 +BEGIN + GROUPBOX "Статический",IDC_STATIC,7,7,267,63 + PUSHBUTTON "Ok",IDC_OK,7,71,50,14 + PUSHBUTTON "Cancel",IDC_CANCEL,224,71,50,14 + CONTROL "Export public keys (only set for contacts)",IDC_PUBLIC, + "Button",BS_AUTORADIOBUTTON,15,23,248,10 + CONTROL "Export all private keys",IDC_PRIVATE,"Button",BS_AUTORADIOBUTTON,15,36,246,10 + CONTROL "Export public and private keys",IDC_ALL,"Button",BS_AUTORADIOBUTTON,15,50,248,10 +END + ///////////////////////////////////////////////////////////////////////////// // @@ -266,6 +280,15 @@ BEGIN TOPMARGIN, 7 BOTTOMMARGIN, 56 END + + IDD_EXPORT_TYPE, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 274 + TOPMARGIN, 7 + BOTTOMMARGIN, 85 + HORZGUIDE, 70 + END END #endif // APSTUDIO_INVOKED @@ -385,12 +408,14 @@ BEGIN "Button",BS_AUTOCHECKBOX | WS_TABSTOP,3,76,233,10 END -IDD_OPT_GPG_ADVANCED DIALOGEX 0, 0, 286, 32 +IDD_OPT_GPG_ADVANCED DIALOGEX 0, 0, 286, 50 STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD FONT 8, "MS Shell Dlg", 400, 0, 0x0 BEGIN CONTROL "Turn on presence signing (Jabber) (required by xep 0-27)",IDC_PRESCENSE_SUBSCRIPTION, "Button",BS_AUTOCHECKBOX | WS_TABSTOP,12,9,268,10 + PUSHBUTTON "Export keys",IDC_EXPORT,12,26,60,14 + PUSHBUTTON "Import keys",IDC_IMPORT,82,26,68,14 END @@ -417,6 +442,7 @@ BEGIN IDD_OPT_GPG_ADVANCED, DIALOG BEGIN VERTGUIDE, 12 + BOTTOMMARGIN, 40 END END #endif // APSTUDIO_INVOKED diff --git a/plugins/New_GPG/src/init.cpp b/plugins/New_GPG/src/init.cpp index 1caaa9c0e1..2abe389d1e 100755 --- a/plugins/New_GPG/src/init.cpp +++ b/plugins/New_GPG/src/init.cpp @@ -230,7 +230,7 @@ extern "C" int __declspec(dllexport) Load() mi.position=-0x7FFFFFFF; mi.flags=CMIF_TCHAR; mi.hIcon=LoadSkinnedIcon(SKINICON_OTHER_MIRANDA); - mi.ptszName=LPGENT("Export GPG Public keys from all users"); + mi.ptszName=LPGENT("Export GPG Public keys"); mi.pszService="/ExportGPGKeys"; hExportGpgKeys = Menu_AddMainMenuItem(&mi); @@ -239,7 +239,7 @@ extern "C" int __declspec(dllexport) Load() mi.position=-0x7FFFFFFF; mi.flags=CMIF_TCHAR; mi.hIcon=LoadSkinnedIcon(SKINICON_OTHER_MIRANDA); - mi.ptszName=LPGENT("Import GPG Public keys from all users"); + mi.ptszName=LPGENT("Import GPG Public keys"); mi.pszService="/ImportGPGKeys"; hImportGpgKeys = Menu_AddMainMenuItem(&mi); diff --git a/plugins/New_GPG/src/options.cpp b/plugins/New_GPG/src/options.cpp index 6b26457864..feea41b67a 100755 --- a/plugins/New_GPG/src/options.cpp +++ b/plugins/New_GPG/src/options.cpp @@ -720,6 +720,24 @@ static INT_PTR CALLBACK DlgProcGpgAdvOpts(HWND hwndDlg, UINT msg, WPARAM wParam, case WM_COMMAND: { + switch (LOWORD(wParam)) + { + case IDC_EXPORT: + { + INT_PTR ExportGpGKeys(WPARAM w, LPARAM l); + ExportGpGKeys(NULL, NULL); + } + break; + case IDC_IMPORT: + { + INT_PTR ImportGpGKeys(WPARAM w, LPARAM l); + ImportGpGKeys(NULL, NULL); + } + break; + default: + break; + } + SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); break; } diff --git a/plugins/New_GPG/src/resource.h b/plugins/New_GPG/src/resource.h index 1135dbd3ea..7ffd83c47b 100755 --- a/plugins/New_GPG/src/resource.h +++ b/plugins/New_GPG/src/resource.h @@ -17,6 +17,7 @@ #define IDI_UNSECURED 113 #define IDD_OPT_GPG_ADVANCED 113 #define IDD_ENCRYPTED_FILE_MSG_BOX 114 +#define IDD_EXPORT_TYPE 115 #define IDC_SET_BIN_PATH 1016 #define IDC_SET_HOME_DIR 1017 #define IDC_BIN_PATH 1018 @@ -40,6 +41,8 @@ #define IDC_SELECT_EXISTING 1026 #define IDC_KEY_EMAIL 1026 #define IDC_IGNORE 1026 +#define IDC_OK 1026 +#define IDC_EXPORT 1026 #define IDC_DELETE_KEY_BUTTON 1027 #define IDC_IN_CLOSE_TAG 1027 #define IDC_KEY_REAL_NAME 1027 @@ -61,6 +64,7 @@ #define IDC_LOG_FILE_SET 1046 #define IDC_IMPORT 1046 #define IDC_DECRYPT 1046 +#define IDC_CANCEL 1046 #define IDC_SAVE_PASSWORD 1047 #define IDC_DEBUG_LOG 1048 #define IDC_JABBER_API 1049 @@ -88,6 +92,9 @@ #define IDC_KEY_ID 1071 #define IDC_COMBO1 1072 #define IDC_ACCOUNT 1072 +#define IDC_PUBLIC 1073 +#define IDC_PRIVATE 1074 +#define IDC_ALL 1075 // Next default values for new objects // @@ -95,7 +102,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 115 #define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1073 +#define _APS_NEXT_CONTROL_VALUE 1076 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif diff --git a/plugins/New_GPG/src/utilities.cpp b/plugins/New_GPG/src/utilities.cpp index 129b7cb9cc..6812fe8922 100755 --- a/plugins/New_GPG/src/utilities.cpp +++ b/plugins/New_GPG/src/utilities.cpp @@ -1407,15 +1407,14 @@ bool isTabsrmmUsed() return found; } - -INT_PTR ExportGpGKeys(WPARAM w, LPARAM l) +void ExportGpGKeysFunc(int type) { - TCHAR *p = GetFilePath(_T("Choose file to export public keys"), _T("*"), _T("Any file"), true); + TCHAR *p = GetFilePath(_T("Choose file to export keys"), _T("*"), _T("Any file"), true); if(!p || !p[0]) { delete [] p; //TODO: handle error - return 1; + return; } char *path = mir_t2a(p); delete [] p; @@ -1424,150 +1423,194 @@ INT_PTR ExportGpGKeys(WPARAM w, LPARAM l) mir_free(path); int exported_keys = 0; if(!file.is_open()) - return 1; //TODO: handle error - for(HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) + return; //TODO: handle error + if(!type || type == 2) { - char *k = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", ""); - if(!k[0]) + for(HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { - mir_free(k); - continue; - } - std::string key = k; - mir_free(k); - - const char* proto = (const char*)GetContactProto(hContact); - std::string id = "Comment: login "; - const char * uid = (const char*)CallProtoService(proto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); - DBVARIANT dbv = {0}; - DBCONTACTGETSETTING dbcgs = {0}; - dbcgs.pValue = &dbv; - dbcgs.szModule = proto; - dbcgs.szSetting = uid; - CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&dbcgs); - switch(dbcgs.pValue->type) - { - case DBVT_DELETED: - continue; - break; - case DBVT_BYTE: - { - char _id[64]; - mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal); - id += _id; - } - break; - case DBVT_WORD: - { - char _id[64]; - mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal); - id += _id; - } - break; - case DBVT_DWORD: + char *k = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", ""); + if(!k[0]) { - char _id[64]; - mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal); - id += _id; + mir_free(k); + continue; } - break; - case DBVT_ASCIIZ: + std::string key = k; + mir_free(k); + + const char* proto = (const char*)GetContactProto(hContact); + std::string id = "Comment: login "; + const char * uid = (const char*)CallProtoService(proto, PS_GETCAPS, (WPARAM)PFLAG_UNIQUEIDSETTING, 0); + DBVARIANT dbv = {0}; + DBCONTACTGETSETTING dbcgs = {0}; + dbcgs.pValue = &dbv; + dbcgs.szModule = proto; + dbcgs.szSetting = uid; + CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&dbcgs); + switch(dbcgs.pValue->type) { - id += dbcgs.pValue->pszVal; + case DBVT_DELETED: + continue; + break; + case DBVT_BYTE: + { + char _id[64]; + mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal); + id += _id; + } + break; + case DBVT_WORD: + { + char _id[64]; + mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal); + id += _id; + } + break; + case DBVT_DWORD: + { + char _id[64]; + mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal); + id += _id; + } + break; + case DBVT_ASCIIZ: + { + id += dbcgs.pValue->pszVal; + CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); + } + break; + case DBVT_UTF8: + { + char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal); + if(tmp[0]) + id += tmp; + mir_free(tmp); + CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); + } + break; + case DBVT_BLOB: + //TODO CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); - } - break; - case DBVT_UTF8: - { - char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal); - if(tmp[0]) - id += tmp; - mir_free(tmp); + break; + case DBVT_WCHAR: + //TODO CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); + break; } - break; - case DBVT_BLOB: - //TODO - CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); - break; - case DBVT_WCHAR: - //TODO - CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); - break; - } - id += " contact_id "; - ZeroMemory(&dbv, sizeof(dbv)); - ZeroMemory(&dbcgs, sizeof(dbcgs)); - dbcgs.pValue = &dbv; - dbcgs.szModule = proto; - dbcgs.szSetting = uid; - CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs); - switch(dbcgs.pValue->type) - { - case DBVT_DELETED: - continue; - break; - case DBVT_BYTE: - { - char _id[64]; - mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal); - id += _id; - } - break; - case DBVT_WORD: - { - char _id[64]; - mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal); - id += _id; - } - break; - case DBVT_DWORD: - { - char _id[64]; - mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal); - id += _id; - } - break; - case DBVT_ASCIIZ: + id += " contact_id "; + ZeroMemory(&dbv, sizeof(dbv)); + ZeroMemory(&dbcgs, sizeof(dbcgs)); + dbcgs.pValue = &dbv; + dbcgs.szModule = proto; + dbcgs.szSetting = uid; + CallService(MS_DB_CONTACT_GETSETTING, (WPARAM)hContact, (LPARAM)&dbcgs); + switch(dbcgs.pValue->type) { - id += dbcgs.pValue->pszVal; + case DBVT_DELETED: + continue; + break; + case DBVT_BYTE: + { + char _id[64]; + mir_snprintf(_id, 63, "%d", dbcgs.pValue->bVal); + id += _id; + } + break; + case DBVT_WORD: + { + char _id[64]; + mir_snprintf(_id, 63, "%d", dbcgs.pValue->wVal); + id += _id; + } + break; + case DBVT_DWORD: + { + char _id[64]; + mir_snprintf(_id, 63, "%d", dbcgs.pValue->dVal); + id += _id; + } + break; + case DBVT_ASCIIZ: + { + id += dbcgs.pValue->pszVal; + CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); + } + break; + case DBVT_UTF8: + { + char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal); + if(tmp[0]) + id += tmp; + mir_free(tmp); + CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); + } + break; + case DBVT_BLOB: + //TODO CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); - } - break; - case DBVT_UTF8: - { - char *tmp = mir_utf8decodeA(dbcgs.pValue->pszVal); - if(tmp[0]) - id += tmp; - mir_free(tmp); + break; + case DBVT_WCHAR: + //TODO CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); + break; } - break; - case DBVT_BLOB: - //TODO - CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); - break; - case DBVT_WCHAR: - //TODO - CallService(MS_DB_CONTACT_FREEVARIANT, 0, (LPARAM)&dbv); - break; + std::string::size_type p1 = key.find("-----BEGIN PGP PUBLIC KEY BLOCK-----"); + if(p1 == std::string::npos) + continue; + p1 += strlen("-----BEGIN PGP PUBLIC KEY BLOCK-----"); + p1 ++; + id += '\n'; + key.insert(p1, id); + file<", s); - tmp2 = new char [output.substr(s,s2-s).length()+1]; + tmp2 = (char*)mir_alloc((output.substr(s,s2-s).length()+1) * sizeof(char)); strcpy(tmp2, output.substr(s,s2-s).c_str()); mir_utf8decode(tmp2, 0); if(hContact) @@ -1868,7 +1911,7 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) } else { - tmp2 = new char [output.substr(s2,s-s2).length()+1]; + tmp2 = (char*)mir_alloc((output.substr(s2,s-s2).length()+1) * sizeof(char)); strcpy(tmp2, output.substr(s2,s-s2).c_str()); mir_utf8decode(tmp2, 0); if(hContact) @@ -1886,11 +1929,59 @@ INT_PTR ImportGpGKeys(WPARAM w, LPARAM l) } key.clear(); } + if(strstr(line, "-----END PGP PRIVATE KEY BLOCK-----")) + { + wstring cmd; + TCHAR tmp2[MAX_PATH] = {0}; + TCHAR *ptmp; + string output; + DWORD exitcode; + { + ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + _tcscpy(tmp2, ptmp); + mir_free(ptmp); + _tcscat(tmp2, _T("\\")); + _tcscat(tmp2, _T("temporary_exported.asc")); + DeleteFile(tmp2); + wfstream f(tmp2, std::ios::out); + f<