Upgrade libgit2
[TortoiseGit.git] / src / TortoisePlink / LoginDialog.cpp
blob8925c3e3878e5574db562551444126915b0f8bb7
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2018-2019 - TortoiseGit
4 // Copyright (C) 2003, 2013, 2018 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "LoginDialog.h"
21 #include "TortoisePlinkRes.h"
22 #include <string>
23 #include <memory>
25 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
27 HINSTANCE g_hmodThisDll;
28 HWND g_hwndMain;
30 class LoginDialog {
31 public:
32 LoginDialog(const std::string& prompt);
33 ~LoginDialog();
35 static bool DoLoginDialog(char* password, int maxlen, const char* prompt);
37 private:
38 bool myOK;
39 HWND _hdlg;
41 char myPassword[MAX_LENGTH_PASSWORD];
42 std::string myPrompt;
44 void CreateModule(void);
45 void RetrieveValues();
46 void PurgeValues();
48 friend BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
51 BOOL DoLoginDialog(char* password, int maxlen, const char* prompt)
53 g_hmodThisDll = GetModuleHandle(0);
54 g_hwndMain = GetParentHwnd();
55 return LoginDialog::DoLoginDialog(password, maxlen, prompt);
58 BOOL CALLBACK LoginDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
60 if (uMsg == WM_INITDIALOG)
62 auto pDlg = reinterpret_cast<LoginDialog*>(lParam);
63 pDlg->_hdlg = hwndDlg;
64 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);
65 // Set prompt text
66 SendDlgItemMessage(hwndDlg, IDC_LOGIN_PROMPT, WM_SETTEXT, pDlg->myPrompt.length(), reinterpret_cast<LPARAM>(pDlg->myPrompt.c_str()));
67 SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, EM_SETLIMITTEXT, MAX_LENGTH_PASSWORD - 1, 0);
68 // Make sure edit control has the focus
69 //SendDlgItemMessage(hwndDlg, IDC_LOGIN_PASSWORD, WM_SETFOCUS, 0, 0);
70 if (GetDlgCtrlID(reinterpret_cast<HWND>(wParam)) != IDC_LOGIN_PASSWORD)
72 SetFocus(GetDlgItem(hwndDlg, IDC_LOGIN_PASSWORD));
73 return FALSE;
75 return TRUE;
77 else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDCANCEL && HIWORD(wParam) == BN_CLICKED)
79 auto pDlg = reinterpret_cast<LoginDialog*>(GetWindowLongPtr(hwndDlg, GWLP_USERDATA));
80 pDlg->myOK = false;
81 pDlg->PurgeValues();
82 EndDialog(hwndDlg, IDCANCEL);
83 return 1;
85 else if (uMsg == WM_COMMAND && LOWORD(wParam) == IDOK && HIWORD(wParam) == BN_CLICKED)
87 auto pDlg = reinterpret_cast<LoginDialog*>(GetWindowLongPtr(hwndDlg, GWLP_USERDATA));
88 pDlg->myOK = true;
89 pDlg->RetrieveValues();
90 pDlg->PurgeValues();
91 EndDialog(hwndDlg, IDOK);
92 return 1;
94 return 0;
97 LoginDialog::LoginDialog(const std::string& prompt)
99 myPrompt = prompt;
100 SecureZeroMemory(&myPassword, sizeof(myPassword));
103 LoginDialog::~LoginDialog()
105 SecureZeroMemory(&myPassword, sizeof(myPassword));
108 void LoginDialog::CreateModule(void)
110 DialogBoxParam(g_hmodThisDll, MAKEINTRESOURCE(IDD_LOGIN), g_hwndMain, (DLGPROC)(LoginDialogProc), reinterpret_cast<LPARAM>(this));
113 bool LoginDialog::DoLoginDialog(char* password, int maxlen, const char* prompt)
115 auto pDlg = std::make_unique<LoginDialog>(prompt);
117 pDlg->CreateModule();
119 bool ret = pDlg->myOK;
121 if (ret)
122 strncpy_s(password, maxlen, pDlg->myPassword, sizeof(pDlg->myPassword));
124 return ret;
127 void LoginDialog::RetrieveValues()
129 SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_GETTEXT, sizeof(myPassword), reinterpret_cast<LPARAM>(myPassword));
132 void LoginDialog::PurgeValues()
134 // overwrite textfield contents with garbage in order to wipe the cache
135 char gargabe[MAX_LENGTH_PASSWORD];
136 memset(gargabe, L'*', sizeof(gargabe));
137 gargabe[sizeof(gargabe) - 1] = '\0';
138 SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(gargabe));
139 gargabe[0] = '\0';
140 SendDlgItemMessage(_hdlg, IDC_LOGIN_PASSWORD, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(gargabe));
143 HWND GetParentHwnd()
145 return GetDesktopWindow();