[23e682]: / src / ui / wxWidgets / safecombinationprompt.cpp  Maximize  Restore  History

Download this file

362 lines (287 with data), 10.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (c) 2003-2013 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
/** \file safecombinationprompt.cpp
*
*/
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
////@begin includes
#include "SafeCombinationCtrl.h"
////@end includes
#include "safecombinationprompt.h"
#include "os/file.h"
#include "./wxutils.h"
#ifdef __WXMSW__
#include <wx/msw/msvcrt.h>
#endif
#ifndef NO_YUBI
////@begin XPM images
#include "graphics/Yubikey-button.xpm"
////@end XPM images
#endif
#include "./graphics/cpane.xpm"
/*!
* CSafeCombinationPrompt type definition
*/
IMPLEMENT_CLASS( CSafeCombinationPrompt, wxDialog )
/*!
* CSafeCombinationPrompt event table definition
*/
BEGIN_EVENT_TABLE( CSafeCombinationPrompt, wxDialog )
#ifndef NO_YUBI
////@begin CSafeCombinationPrompt event table entries
EVT_BUTTON( ID_YUBIBTN, CSafeCombinationPrompt::OnYubibtnClick )
////@end CSafeCombinationPrompt event table entries
EVT_TIMER(POLLING_TIMER_ID, CSafeCombinationPrompt::OnPollingTimer)
#endif
EVT_BUTTON( wxID_OK, CSafeCombinationPrompt::OnOkClick )
EVT_BUTTON( wxID_CANCEL, CSafeCombinationPrompt::OnCancelClick )
END_EVENT_TABLE()
/*!
* CSafeCombinationPrompt constructors
*/
CSafeCombinationPrompt::CSafeCombinationPrompt(wxWindow* parent, PWScore &core,
const wxString &fname, wxWindowID id,
const wxString& caption,
const wxPoint& pos,
const wxSize& size, long style)
: m_core(core), m_filename(fname), m_tries(0)
{
Init();
Create(parent, id, caption, pos, size, style);
}
/*!
* CSafeCombinationPrompt creator
*/
bool CSafeCombinationPrompt::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin CSafeCombinationPrompt creation
SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
wxDialog::Create( parent, id, caption, pos, size, style );
CreateControls();
if (GetSizer())
{
GetSizer()->SetSizeHints(this);
}
Centre();
////@end CSafeCombinationPrompt creation
#ifndef NO_YUBI
SetupMixin(FindWindow(ID_YUBIBTN), FindWindow(ID_YUBISTATUS));
m_pollingTimer = new wxTimer(this, POLLING_TIMER_ID);
m_pollingTimer->Start(250); // check for Yubikey every 250ms.
#endif
return true;
}
/*!
* CSafeCombinationPrompt destructor
*/
CSafeCombinationPrompt::~CSafeCombinationPrompt()
{
////@begin CSafeCombinationPrompt destruction
////@end CSafeCombinationPrompt destruction
#ifndef NO_YUBI
delete m_pollingTimer;
#endif
}
/*!
* Member initialisation
*/
void CSafeCombinationPrompt::Init()
{
////@begin CSafeCombinationPrompt member initialisation
m_scctrl = NULL;
#ifndef NO_YUBI
m_YubiBtn = NULL;
m_yubiStatusCtrl = NULL;
#endif
////@end CSafeCombinationPrompt member initialisation
}
/*!
* Control creation for CSafeCombinationPrompt
*/
void CSafeCombinationPrompt::CreateControls()
{
////@begin CSafeCombinationPrompt content construction
CSafeCombinationPrompt* itemDialog1 = this;
wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
itemDialog1->SetSizer(itemBoxSizer2);
wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
wxStaticBitmap* itemStaticBitmap4 = new wxStaticBitmap( itemDialog1, wxID_STATIC, itemDialog1->GetBitmapResource(wxT("./graphics/cpane.xpm")), wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(49, 46)), 0 );
itemBoxSizer3->Add(itemStaticBitmap4, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxVERTICAL);
itemBoxSizer3->Add(itemBoxSizer5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxStaticText* itemStaticText6 = new wxStaticText( itemDialog1, wxID_STATIC, _("Please enter the safe combination for this password database."), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer5->Add(itemStaticText6, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
wxStaticText* itemStaticText7 = new wxStaticText( itemDialog1, wxID_STATIC, _("filename"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer5->Add(itemStaticText7, 0, wxALIGN_LEFT|wxALL, 5);
wxBoxSizer* itemBoxSizer8 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer5->Add(itemBoxSizer8, 0, wxGROW|wxALL, 5);
wxStaticText* itemStaticText9 = new wxStaticText( itemDialog1, wxID_STATIC, _("Safe\ncombination:"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer8->Add(itemStaticText9, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
m_scctrl = new CSafeCombinationCtrl( itemDialog1, ID_PASSWORD, &m_password, wxDefaultPosition, wxSize(itemDialog1->ConvertDialogToPixels(wxSize(150, -1)).x, -1) );
itemBoxSizer8->Add(m_scctrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxBoxSizer* itemBoxSizer11 = new wxBoxSizer(wxHORIZONTAL);
itemBoxSizer5->Add(itemBoxSizer11, 0, wxGROW|wxALL, 5);
#ifndef NO_YUBI
m_YubiBtn = new wxBitmapButton( itemDialog1, ID_YUBIBTN, itemDialog1->GetBitmapResource(wxT("graphics/Yubikey-button.xpm")), wxDefaultPosition, itemDialog1->ConvertDialogToPixels(wxSize(40, 15)), wxBU_AUTODRAW );
itemBoxSizer11->Add(m_YubiBtn, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT|wxBOTTOM|wxSHAPED, 5);
#endif
itemBoxSizer11->Add(4, 10, 0, wxALIGN_CENTER_VERTICAL|wxALL, 1);
#ifndef NO_YUBI
m_yubiStatusCtrl = new wxStaticText( itemDialog1, ID_YUBISTATUS, _("Please insert your YubiKey"), wxDefaultPosition, wxDefaultSize, 0 );
itemBoxSizer11->Add(m_yubiStatusCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
#endif
wxStdDialogButtonSizer* itemStdDialogButtonSizer15 = new wxStdDialogButtonSizer;
itemBoxSizer2->Add(itemStdDialogButtonSizer15, 0, wxGROW|wxALL, 5);
wxButton* itemButton16 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
itemButton16->SetDefault();
itemStdDialogButtonSizer15->AddButton(itemButton16);
wxButton* itemButton17 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
itemStdDialogButtonSizer15->AddButton(itemButton17);
wxButton* itemButton18 = new wxButton( itemDialog1, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
itemStdDialogButtonSizer15->AddButton(itemButton18);
itemStdDialogButtonSizer15->Realize();
// Set validators
itemStaticText7->SetValidator( wxGenericValidator(& m_filename) );
////@end CSafeCombinationPrompt content construction
wxWindow* passwdCtrl = FindWindow(ID_PASSWORD);
if (passwdCtrl) {
passwdCtrl->SetFocus();
}
}
/*!
* Should we show tooltips?
*/
bool CSafeCombinationPrompt::ShowToolTips()
{
return true;
}
/*!
* Get bitmap resources
*/
wxBitmap CSafeCombinationPrompt::GetBitmapResource( const wxString& name )
{
// Bitmap retrieval
////@begin CSafeCombinationPrompt bitmap retrieval
wxUnusedVar(name);
if (name == _T("./graphics/cpane.xpm"))
{
wxBitmap bitmap(cpane_xpm);
return bitmap;
}
#ifndef NO_YUBI
else if (name == _T("graphics/Yubikey-button.xpm"))
{
wxBitmap bitmap(Yubikey_button_xpm);
return bitmap;
}
#endif
return wxNullBitmap;
////@end CSafeCombinationPrompt bitmap retrieval
}
/*!
* Get icon resources
*/
wxIcon CSafeCombinationPrompt::GetIconResource( const wxString& name )
{
// Icon retrieval
////@begin CSafeCombinationPrompt icon retrieval
wxUnusedVar(name);
return wxNullIcon;
////@end CSafeCombinationPrompt icon retrieval
}
void CSafeCombinationPrompt::ProcessPhrase()
{
if (m_core.CheckPasskey(tostringx(m_filename),
m_password) != PWScore::SUCCESS) {
wxString errmess;
if (m_tries >= 2) {
errmess = _("Three strikes - yer out!");
} else {
m_tries++;
errmess = _("Incorrect passkey, not a PasswordSafe database, or a corrupt database. (Backup database has same name as original, ending with '~')");
}
wxMessageDialog err(this, errmess,
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
wxTextCtrl *txt = dynamic_cast<wxTextCtrl *>(FindWindow(ID_PASSWORD));
txt->SetSelection(-1,-1);
txt->SetFocus();
return;
}
// m_core.SetReadOnly(m_readOnly);
m_core.SetCurFile(tostringx(m_filename));
EndModal(wxID_OK);
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
*/
void CSafeCombinationPrompt::OnOkClick( wxCommandEvent& /* evt */ )
{
if (Validate() && TransferDataFromWindow()) {
if (m_password.empty()) {
wxMessageDialog err(this, _("The combination cannot be blank."),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
return;
}
if (!pws_os::FileExists(tostdstring(m_filename))) {
wxMessageDialog err(this, _("File or path not found."),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
return;
}
ProcessPhrase();
}
}
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
*/
void CSafeCombinationPrompt::OnCancelClick( wxCommandEvent& /* evt */ )
{
////@begin wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL in CSafeCombinationPrompt.
// Before editing this code, remove the block markers.
EndModal(wxID_CANCEL);
////@end wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL in CSafeCombinationPrompt.
}
#ifndef NO_YUBI
/*!
* wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_YUBIBTN
*/
void CSafeCombinationPrompt::OnYubibtnClick( wxCommandEvent& /* event */ )
{
m_scctrl->AllowEmptyCombinationOnce(); // Allow blank password when Yubi's used
if (Validate() && TransferDataFromWindow()) {
if (!pws_os::FileExists(tostdstring(m_filename))) {
wxMessageDialog err(this, _("File or path not found."),
_("Error"), wxOK | wxICON_EXCLAMATION);
err.ShowModal();
return;
}
StringX response;
if (PerformChallengeResponse(m_password, response)) {
m_password = response;
ProcessPhrase();
UpdateStatus();
}
}
}
void CSafeCombinationPrompt::OnPollingTimer(wxTimerEvent &evt)
{
if (evt.GetId() == POLLING_TIMER_ID) {
HandlePollingTimer(); // in CYubiMixin
}
}
#endif