Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[4d5089]: ThisMfcApp.cpp Maximize Restore History

Download this file

ThisMfcApp.cpp    614 lines (523 with data), 16.7 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/// \file ThisMfcApp.cpp
/// \brief App object of MFC version of Password Safe
//-----------------------------------------------------------------------------
#include "PasswordSafe.h"
#include "corelib/PwsPlatform.h"
#include "corelib/PWSprefs.h"
#if defined(POCKET_PC)
#include "pocketpc/PocketPC.h"
#include "pocketpc/resource.h"
#else
#include <errno.h>
#include <io.h>
#include "resource.h"
#endif
#include "ThisMfcApp.h"
#include "corelib/Util.h"
#include "corelib/BlowFish.h"
#include "DboxMain.h"
#include "CryptKeyEntry.h"
BEGIN_MESSAGE_MAP(ThisMfcApp, CWinApp)
// ON_COMMAND(ID_HELP, CWinApp::OnHelp)
ON_COMMAND(ID_HELP, OnHelp)
END_MESSAGE_MAP()
ThisMfcApp::ThisMfcApp() :
#if defined(POCKET_PC)
m_bUseAccelerator( false ),
#else
m_bUseAccelerator( true ),
#endif
m_pMRU( NULL ), m_TrayLockedState(UNLOCKED), m_TrayIcon(NULL)
{
// {kjp} Temporary until I'm sure that PwsPlatform.h configures the endianness properly
#if defined(POCKET_PC)
// Double check that *_ENDIAN has been correctly set!
#if defined(LITTLE_ENDIAN)
unsigned char buf[4] = { 1, 0, 0, 0 };
unsigned int ii = 1;
#define ENDIANNESS _T("little endian")
#define ENDIANNESS2 _T("big endian")
#elif defined(BIG_ENDIAN)
unsigned char buf[4] = { 0, 0, 0, 1 };
unsigned int ii = 1;
#define ENDIANNESS _T("big endian")
#define ENDIANNESS2 _T("little endian")
#endif
if (*(unsigned int*)buf != ii)
{
AfxMessageBox(_T("Password Safe has been compiled as ") ENDIANNESS
_T(" but CPU is really ") ENDIANNESS2 _T("\n")
_T("You may not be able to open files or saved files may be incompatible with other platforms."));
}
#endif
}
ThisMfcApp::~ThisMfcApp()
{
delete m_TrayIcon;
if (m_pMRU != NULL) {
m_pMRU->WriteList();
delete m_pMRU;
}
/*
apparently, with vc7, there's a CWinApp::HtmlHelp - I'd like
to see the docs, someday. In the meantime, force with :: syntax
*/
#if !defined(POCKET_PC)
::HtmlHelp(NULL, NULL, HH_CLOSE_ALL, 0);
#endif
}
#if !defined(POCKET_PC)
static void Usage()
{
AfxMessageBox(_T("Usage: PasswordSafe [-r|-s] [password database]\n")
_T("or PasswordSafe [-e|-d] filename"));
}
// tests if file exists, returns empty string if so, displays error message if not
static BOOL CheckFile(const CString &fn)
{
DWORD status = ::GetFileAttributes(fn);
CString ErrMess;
if (status == -1) {
ErrMess = _T("Could not access file: ");
ErrMess += fn;
} else if (status & FILE_ATTRIBUTE_DIRECTORY) {
ErrMess = fn;
ErrMess += _T(" is a directory");
}
if (ErrMess.IsEmpty()) {
return TRUE;
} else {
AfxMessageBox(ErrMess);
return FALSE;
}
}
//Complain if the file has not opened correctly
static void
ErrorMessages(const CString &fn, FILE *fp)
{
#if !defined(POCKET_PC)
if (fp == NULL) {
CString text;
text = _T("A fatal error occured: ");
if (errno==EACCES)
text += _T("Given path is a directory or file is read-only");
else if (errno==EEXIST)
text += _T("The filename already exists.");
else if (errno==EINVAL)
text += _T("Invalid oflag or shflag argument.");
else if (errno==EMFILE)
text += _T("No more file handles available.");
else if (errno==ENOENT)
text += _T("File or path not found.");
text += _T("\nProgram will terminate.");
CString title = _T("Password Safe - ") + fn;
AfxGetMainWnd()->MessageBox(text, title, MB_ICONEXCLAMATION|MB_OK);
}
#endif
}
static BOOL EncryptFile(const CString &fn, const CMyString &passwd)
{
unsigned int len;
unsigned char* buf;
#if defined(UNICODE)
FILE *in = _wfopen(fn, _T("rb"));
#else
FILE *in = fopen(fn, _T("rb"));
#endif
if (in != NULL) {
len = fileLength(in);
buf = new unsigned char[len];
fread(buf, 1, len, in);
fclose(in);
} else {
ErrorMessages(fn, in);
return FALSE;
}
CString out_fn = fn;
out_fn += CIPHERTEXT_SUFFIX;
#if defined(UNICODE)
FILE *out = _wfopen(out_fn, _T("wb"));
#else
FILE *out = fopen(out_fn, _T("wb"));
#endif
if (out != NULL) {
#ifdef KEEP_FILE_MODE_BWD_COMPAT
fwrite( &len, 1, sizeof(len), out);
#else
unsigned char randstuff[StuffSize];
unsigned char randhash[SHA1::HASHLEN]; // HashSize
GetRandomData( randstuff, 8 );
// miserable bug - have to fix this way to avoid breaking existing files
randstuff[8] = randstuff[9] = '\0';
GenRandhash(passwd,
randstuff,
randhash);
fwrite(randstuff, 1, 8, out);
fwrite(randhash, 1, sizeof(randhash), out);
#endif // KEEP_FILE_MODE_BWD_COMPAT
unsigned char thesalt[SaltLength];
GetRandomData( thesalt, SaltLength );
fwrite(thesalt, 1, SaltLength, out);
unsigned char ipthing[8];
GetRandomData( ipthing, 8 );
fwrite(ipthing, 1, 8, out);
LPCSTR pwd = LPCSTR(passwd);
Fish *fish = BlowFish::MakeBlowFish((unsigned char *)pwd, passwd.GetLength(),
thesalt, SaltLength);
_writecbc(out, buf, len, (unsigned char)0, fish, ipthing);
delete fish;
fclose(out);
} else {
ErrorMessages(out_fn, out);
delete [] buf;
return FALSE;
}
delete[] buf;
return TRUE;
}
static BOOL DecryptFile(const CString &fn, const CMyString &passwd)
{
unsigned int len;
unsigned char* buf;
#if defined(UNICODE)
FILE *in = _wfopen(fn, _T("rb"));
#else
FILE *in = fopen(fn, _T("rb"));
#endif
if (in != NULL) {
unsigned char salt[SaltLength];
unsigned char ipthing[8];
unsigned char randstuff[StuffSize];
unsigned char randhash[SHA1::HASHLEN];
#ifdef KEEP_FILE_MODE_BWD_COMPAT
fread(&len, 1, sizeof(len), in); // XXX portability issue
#else
fread(randstuff, 1, 8, in);
randstuff[8] = randstuff[9] = '\0'; // ugly bug workaround
fread(randhash, 1, sizeof(randhash), in);
unsigned char temphash[SHA1::HASHLEN];
GenRandhash(passwd,
randstuff,
temphash);
if (0 != memcmp((char*)randhash,
(char*)temphash, SHA1::HASHLEN)) {
fclose(in);
AfxMessageBox(_T("Incorrect password"));
return FALSE;
}
#endif // KEEP_FILE_MODE_BWD_COMPAT
buf = NULL; // allocated by _readcbc - see there for apologia
fread(salt, 1, SaltLength, in);
fread(ipthing, 1, 8, in);
LPCSTR pwd = LPCSTR(passwd);
unsigned char dummyType;
Fish *fish = BlowFish::MakeBlowFish((unsigned char *)pwd, passwd.GetLength(),
salt, SaltLength);
if (_readcbc(in, buf, len,dummyType, fish, ipthing) == 0) {
delete fish;
delete[] buf; // if not yet allocated, delete[] NULL, which is OK
return FALSE;
}
delete fish;
fclose(in);
} else {
ErrorMessages(fn, in);
return FALSE;
}
int suffix_len = strlen(CIPHERTEXT_SUFFIX);
int filepath_len = fn.GetLength();
CString out_fn = fn;
out_fn = out_fn.Left(filepath_len - suffix_len);
#if defined(UNICODE)
FILE *out = _wfopen(out_fn, _T("wb"));
#else
FILE *out = fopen(out_fn, _T("wb"));
#endif
if (out != NULL) {
fwrite(buf, 1, len, out);
fclose(out);
} else
ErrorMessages(out_fn, out);
delete[] buf; // allocated by _readcbc
return TRUE;
}
#endif
BOOL
ThisMfcApp::InitInstance()
{
/*
* It's always best to start at the beginning. [Glinda, Witch of the North]
*/
#if defined(POCKET_PC)
SHInitExtraControls();
#endif
/*
this instructs the app to use the registry instead of .ini files. The
path ends up being
HKEY_CURRENT_USER\Software\(companyname)\(appname)\(sectionname)\(valuename)
Assuming the open-source version of this is going to become less
Counterpane-centric, I expect this may change, but if it does, an
automagic migration ought to happen. -- {jpr}
Of course, this is legacy, and will go away once the registry is fully replaced
by the in-database preference storage. -- ronys
*/
CString companyname(_T("Counterpane Systems"));
SetRegistryKey(companyname);
int nMRUItems = PWSprefs::GetInstance()->
GetPref(PWSprefs::IntPrefs::MaxMRUItems);
m_mruonfilemenu = PWSprefs::GetInstance()->
GetPref(PWSprefs::BoolPrefs::MRUOnFileMenu);
m_mainmenu = new CMenu;
m_mainmenu->LoadMenu(IDR_MAINMENU);
// Look for "File" menu.
int pos = FindMenuItem(m_mainmenu, _T("&File"));
if (pos == -1) // E.g., in non-English versions
pos = 0; // best guess...
CMenu* m_file_submenu = m_mainmenu->GetSubMenu(pos);
if (m_file_submenu != NULL) // Look for "Open Database"
pos = FindMenuItem(m_file_submenu, ID_MENUITEM_OPEN);
if (pos > -1) {
int irc;
// Create New Popup Menu
CMenu* m_new_popupmenu;
m_new_popupmenu = new CMenu;
m_new_popupmenu->CreatePopupMenu();
if (!m_mruonfilemenu) { // MRU entries in popup menu
// Insert Item onto new popup
irc = m_new_popupmenu->InsertMenu( 0, MF_BYPOSITION, ID_FILE_MRU_ENTRY1, _T("Safe%d") );
ASSERT(irc != 0);
// Insert Popup onto main menu
irc = m_file_submenu->InsertMenu( pos + 2, MF_BYPOSITION | MF_POPUP, (UINT) m_new_popupmenu->m_hMenu, "&Recent Safes" );
ASSERT(irc != 0);
}
else { // MRU entries inline
irc = m_file_submenu->InsertMenu( pos + 2, MF_BYPOSITION, ID_FILE_MRU_ENTRY1, "Recent" );
ASSERT(irc != 0);
}
m_pMRU = new CRecentFileList( 0, _T("MRU"), _T("Safe%d"), nMRUItems );
m_pMRU->ReadList();
}
DboxMain dbox(NULL);
/*
* Command line processing:
* Historically, it appears that if a filename was passed as a commadline argument,
* the application would prompt the user for the password, and the encrypt or decrypt
* the named file, based on the file's suffix. Ugh.
*
* What I'll do is as follows:
* If a file is given in the command line, it is used as the database, overriding the
* registry value. This will allow the user to have several databases, say, one for work
* and one for personal use, and to set up a different shortcut for each.
*
* I think I'll keep the old functionality, but activate it with a "-e" or "-d" flag. (ronys)
* {kjp} ... and I've removed all of it from the Pocket PC build.
*/
#if !defined(POCKET_PC)
if (m_lpCmdLine[0] != '\0') {
CString args = m_lpCmdLine;
if (args[0] != _T('-')) {
StripFileQuotes( args );
if (CheckFile(args)) {
dbox.SetCurFile(args);
} else {
return FALSE;
}
} else { // here if first char of arg is '-'
// first, let's check that there's a second arg
CString fn = args.Right(args.GetLength()-2);
fn.TrimLeft();
StripFileQuotes( fn );
if (args[1] != 'r' && args[1] != 'R' &&
(fn.IsEmpty() || !CheckFile(fn))) {
Usage();
return FALSE;
}
CMyString passkey;
if (args[1] == 'e' || args[1] == 'E' || args[1] == 'd' || args[1] == 'D') {
// get password from user if valid flag given. If note, default below will
// pop usage message
CCryptKeyEntry dlg(NULL);
int nResponse = dlg.DoModal();
if (nResponse==IDOK) {
passkey = dlg.m_cryptkey1;
} else {
return FALSE;
}
}
BOOL status;
switch (args[1]) {
case 'e': case 'E': // do encrpytion
status = EncryptFile(fn, passkey);
if (!status) {
AfxMessageBox(_T("Encryption failed"));
}
return TRUE;
case 'd': case 'D': // do decryption
status = DecryptFile(fn, passkey);
if (!status) {
// nothing to do - DecryptFile displays its own error messages
}
return TRUE;
case 'r': case 'R':
dbox.SetReadOnly(true);
dbox.SetCurFile(fn);
break;
case 's': case 'S':
dbox.SetStartSilent(true);
dbox.SetCurFile(fn);
break;
default:
Usage();
return FALSE;
} // switch
} // else
} // m_lpCmdLine[0] != '\0';
#endif
/*
* normal startup
*/
/*
Here's where PWS currently does DboxMain, which in turn will do
the initial PasskeyEntry (the one that looks like a splash screen).
This makes things very hard to control.
The app object (here) should instead do the initial PasskeyEntry,
and, if successful, move on to DboxMain. I think. {jpr}
*/
m_maindlg = &dbox;
m_pMainWnd = m_maindlg;
// JHF : no tray icon and menu for PPC
#if !defined(POCKET_PC)
//HICON stIcon = app.LoadIcon(IDI_TRAY);
//ASSERT(stIcon != NULL);
m_LockedIcon = app.LoadIcon(IDI_LOCKEDICON);
m_UnLockedIcon = app.LoadIcon(IDI_UNLOCKEDICON);
m_TrayIcon = new CSystemTray(NULL, WM_ICON_NOTIFY, _T("PasswordSafe"),
m_UnLockedIcon, dbox.m_RecentEntriesList,
WM_ICON_NOTIFY, IDR_POPTRAY);
m_TrayIcon->SetTarget(&dbox);
#endif
// Set up an Accelerator table
#if !defined(POCKET_PC)
m_ghAccelTable = LoadAccelerators(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_ACCS));
#endif
//Run dialog
(void) dbox.DoModal();
/*
note that we don't particularly care what the response was
*/
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
void
ThisMfcApp::SetSystemTrayState(STATE s)
{
// need to protect against null m_TrayIcon due to
// tricky initialization order
if (m_TrayIcon != NULL && s != m_TrayLockedState) {
m_TrayLockedState = s;
m_TrayIcon->SetIcon(s == LOCKED ? m_LockedIcon : m_UnLockedIcon);
}
}
#if !defined(POCKET_PC)
// Removes quotation marks from file name parameters
// as in "file with spaces.pws"
void
ThisMfcApp::StripFileQuotes( CString& strFilename )
{
const char* szFilename = strFilename;
int nLen = strFilename.GetLength();
// if the filenames starts with a quote...
if ( *szFilename == '\"' ) {
// remove leading quote
++szFilename;
--nLen;
// trailing quote is optional, remove if present
if ( szFilename[nLen - 1] == '\"' )
--nLen;
strFilename = CString( szFilename, nLen );
}
}
#endif
#if !defined(POCKET_PC)
//Copied from Knowledge Base article Q100770
//But not for WinCE {kjp}
BOOL
ThisMfcApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
if (code < 0)
CWinApp::ProcessMessageFilter(code, lpMsg);
if (m_bUseAccelerator &&
m_maindlg != NULL
&& m_ghAccelTable != NULL) {
if (::TranslateAccelerator(m_maindlg->m_hWnd, m_ghAccelTable, lpMsg))
return TRUE;
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
#endif
void
ThisMfcApp::OnHelp()
{
#if defined(POCKET_PC)
CreateProcess( _T("PegHelp.exe"), _T("pws_ce_help.html#mainhelp"), NULL, NULL, FALSE, 0, NULL, NULL, NULL, NULL );
#else
/*
* Following mess because CPropertySheet is too smart for its own
* good. The "Help" button there is mapped to the App framework,
* and that's that...
*/
CString title;
CWnd *wnd = CWnd::GetCapture();
if (wnd == NULL)
wnd = CWnd::GetFocus();
if (wnd != NULL)
wnd = wnd->GetParent();
if (wnd != NULL) {
wnd->GetWindowText(title);
}
if (title != _T("Options"))
::HtmlHelp(wnd->m_hWnd,
"pwsafe.chm",
HH_DISPLAY_TOPIC, 0);
else
::HtmlHelp(NULL,
"pwsafe.chm::/html/display_tab.html",
HH_DISPLAY_TOPIC, 0);
#endif
}
// FindMenuItem() will find a menu item string from the specified
// popup menu and returns its position (0-based) in the specified
// popup menu. It returns -1 if no such menu item string is found.
int FindMenuItem(CMenu* Menu, LPCTSTR MenuString)
{
ASSERT(Menu);
ASSERT(::IsMenu(Menu->GetSafeHmenu()));
int count = Menu->GetMenuItemCount();
for (int i = 0; i < count; i++) {
CString str;
if (Menu->GetMenuString(i, str, MF_BYPOSITION) &&
(strcmp(str, MenuString) == 0))
return i;
}
return -1;
}
// FindMenuItem() will find a menu item ID from the specified
// popup menu and returns its position (0-based) in the specified
// popup menu. It returns -1 if no such menu item string is found.
int FindMenuItem(CMenu* Menu, int MenuID)
{
ASSERT(Menu);
ASSERT(::IsMenu(Menu->GetSafeHmenu()));
int count = Menu->GetMenuItemCount();
int id;
for (int i = 0; i < count; i++) {
id = Menu->GetMenuItemID(i); // id = 0 for Separator; 1 for popup
if ( id > 1 && id == MenuID)
return i;
}
return -1;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------