1 |
twoaday |
2 |
/* wptPassphraseDlg.cpp - Dialog to get the passphrase |
2 |
|
|
* Copyright (C) 2001-2004 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT 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. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <windows.h> |
22 |
|
|
|
23 |
|
|
#include "../resource.h" |
24 |
|
|
#include "wptGPG.h" |
25 |
|
|
#include "wptCommonCtl.h" |
26 |
|
|
#include "wptContext.h" |
27 |
|
|
#include "wptDlgs.h" |
28 |
|
|
#include "wptNLS.h" |
29 |
|
|
#include "wptW32API.h" |
30 |
|
|
#include "wptUTF8.h" |
31 |
|
|
#include "wptVersion.h" |
32 |
|
|
#include "wptTypes.h" |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
struct passphrase_s { |
36 |
|
|
char * title; |
37 |
|
|
char pwd[256]; |
38 |
|
|
int repeat; |
39 |
|
|
int cancel; |
40 |
|
|
}; |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
static BOOL CALLBACK |
44 |
|
|
passwd_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam ) |
45 |
|
|
{ |
46 |
|
|
static passphrase_s * pwd; |
47 |
|
|
static int hide = 1; |
48 |
|
|
int nbytes; |
49 |
|
|
|
50 |
|
|
switch( msg ) { |
51 |
|
|
case WM_INITDIALOG: |
52 |
|
|
hide = 1; |
53 |
|
|
pwd = (passphrase_s *)lparam; |
54 |
|
|
if( pwd == NULL ) |
55 |
|
|
dlg_fatal_error( dlg, "Could not get dialog param!" ); |
56 |
|
|
#ifndef LANG_DE |
57 |
|
|
SetWindowText( dlg, _("Passphrase Dialog") ); |
58 |
|
|
#endif |
59 |
|
|
CheckDlgButton( dlg, IDC_PASSWD_HIDE, BST_CHECKED ); |
60 |
|
|
if( pwd->title ) |
61 |
|
|
SetWindowText( dlg, pwd->title ); |
62 |
|
|
if (pwd->repeat) |
63 |
|
|
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Repeat Passphrase")); |
64 |
|
|
else |
65 |
|
|
SetDlgItemText (dlg, IDC_PASSWD_INFO, _("Enter Passphrase")); |
66 |
|
|
SetFocus( GetDlgItem( dlg, IDC_PASSWD_PWD ) ); |
67 |
|
|
center_window( dlg ); |
68 |
|
|
SetForegroundWindow( dlg ); |
69 |
|
|
return FALSE; |
70 |
|
|
|
71 |
|
|
case WM_COMMAND: |
72 |
|
|
if( HIWORD( wparam ) == BN_CLICKED && LOWORD( wparam ) == IDC_PASSWD_HIDE ) { |
73 |
|
|
HWND hwnd = GetDlgItem( dlg, IDC_PASSWD_PWD ); |
74 |
|
|
hide ^= 1; |
75 |
|
|
SendMessage( hwnd, EM_SETPASSWORDCHAR, hide? '*' : 0, 0 ); |
76 |
|
|
SetFocus( hwnd ); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
switch( LOWORD( wparam ) ) { |
80 |
|
|
case IDOK: |
81 |
|
|
pwd->cancel = 0; |
82 |
|
|
nbytes = GetDlgItemText( dlg, IDC_PASSWD_PWD, pwd->pwd, sizeof pwd->pwd -1 ); |
83 |
|
|
if( !nbytes ) |
84 |
|
|
strcpy( pwd->pwd, "" ); |
85 |
|
|
SetDlgItemText( dlg, IDC_PASSWD_PWD, "" ); |
86 |
|
|
EndDialog( dlg, TRUE ); |
87 |
|
|
return TRUE; |
88 |
|
|
|
89 |
|
|
case IDCANCEL: |
90 |
|
|
pwd->cancel = 1; |
91 |
|
|
EndDialog( dlg, FALSE ); |
92 |
|
|
return FALSE; |
93 |
|
|
} |
94 |
|
|
break; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
return FALSE; |
98 |
|
|
} /* passwd_dlg_proc */ |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
char * |
102 |
|
|
request_passphrase (const char * title, int init, int * ret_cancel) |
103 |
|
|
{ |
104 |
|
|
passphrase_s pass; |
105 |
|
|
char * p; |
106 |
|
|
|
107 |
|
|
memset (&pass, 0, sizeof pass); |
108 |
|
|
if (title && *title) { |
109 |
|
|
pass.title = m_strdup (title); |
110 |
|
|
if (!pass.title) |
111 |
|
|
BUG (0); |
112 |
|
|
} |
113 |
|
|
pass.repeat = init == 0? 1: 0; |
114 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_PASSWD, glob_hwnd, |
115 |
|
|
passwd_dlg_proc, (LPARAM)&pass); |
116 |
|
|
if (pass.cancel == 1) { |
117 |
|
|
*ret_cancel = 1; |
118 |
|
|
return NULL; |
119 |
|
|
} |
120 |
|
|
p = m_strdup (pass.pwd); |
121 |
|
|
if (!p) |
122 |
|
|
BUG (0); |
123 |
|
|
free_if_alloc (pass.title); |
124 |
|
|
memset (&pass, 0, sizeof pass); |
125 |
|
|
return p; |
126 |
|
|
} /* request_passphrase */ |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
char * |
130 |
|
|
request_passphrase2 (const char * title, int * ret_cancel) |
131 |
|
|
{ |
132 |
|
|
char * pass1, * pass2; |
133 |
|
|
int equal = 0, cancel = 0; |
134 |
|
|
|
135 |
|
|
*ret_cancel = 1; |
136 |
|
|
while (!equal) { |
137 |
|
|
pass1 = request_passphrase (title, 1, &cancel); |
138 |
|
|
if (cancel) |
139 |
|
|
return NULL; |
140 |
|
|
pass2 = request_passphrase (title, 0, &cancel); |
141 |
|
|
if (cancel) { |
142 |
|
|
sfree_if_alloc (pass1); |
143 |
|
|
return NULL; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
if (strcmp (pass1, pass2)) { |
147 |
|
|
msg_box (NULL, _("Passphrases do not match. Please try again."), |
148 |
|
|
title, MB_INFO); |
149 |
|
|
sfree_if_alloc (pass1); |
150 |
|
|
sfree_if_alloc (pass2); |
151 |
|
|
} |
152 |
|
|
else { |
153 |
|
|
if (is_8bit_string (pass1)) { |
154 |
|
|
msg_box (NULL, _("The passphrase contains 8-bit characters.\n" |
155 |
|
|
"It is not suggested to use charset specific characters."), |
156 |
|
|
title, MB_ERR); |
157 |
|
|
equal = 0; |
158 |
|
|
sfree_if_alloc (pass1); |
159 |
|
|
sfree_if_alloc (pass2); |
160 |
|
|
} |
161 |
|
|
else |
162 |
|
|
equal = 1; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
sfree_if_alloc (pass2); |
166 |
|
|
*ret_cancel = 0; |
167 |
|
|
return pass1; |
168 |
|
|
} |