17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
*/ |
|
|
|
20 |
#include <windows.h> |
#include <windows.h> |
21 |
|
|
22 |
#include "../resource.h" |
#include "../resource.h" |
33 |
#include "wptCard.h" |
#include "wptCard.h" |
34 |
#include "wptFileManager.h" |
#include "wptFileManager.h" |
35 |
#include "wptContext.h" |
#include "wptContext.h" |
36 |
|
#include "wptCardEdit.h" |
37 |
|
|
38 |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
39 |
HWND glob_hwnd; /* global window handle for the dialogs */ |
HWND glob_hwnd; /* global window handle for the dialogs */ |
41 |
LOCK mo_file; |
LOCK mo_file; |
42 |
int scard_support = 0; |
int scard_support = 0; |
43 |
int debug = 0; |
int debug = 0; |
44 |
|
int mobile = 0; |
45 |
int gpg_read_only = 0; |
int gpg_read_only = 0; |
46 |
char gpgver[3]; |
char gpgver[3]; |
47 |
|
|
|
|
|
48 |
static void |
static void |
49 |
update_keycache (HWND hwnd) |
update_keycache (HWND hwnd) |
50 |
{ |
{ |
57 |
} /* update_keycache */ |
} /* update_keycache */ |
58 |
|
|
59 |
|
|
60 |
|
/* Set GPGME debug mode. If @val is 0, the debug mode is disabled. */ |
61 |
|
void |
62 |
|
gpg_set_debug_mode (int val) |
63 |
|
{ |
64 |
|
if (val) |
65 |
|
putenv ("GPGME_DEBUG=5:gpgme.dbg"); |
66 |
|
else |
67 |
|
putenv ("GPGME_DEBUG="); |
68 |
|
} |
69 |
|
|
70 |
static char * |
static char * |
71 |
get_gettext_lang (void) |
get_gettext_lang (void) |
72 |
{ |
{ |
81 |
static void |
static void |
82 |
load_gettext (void) |
load_gettext (void) |
83 |
{ |
{ |
84 |
char * nls = NULL; |
char *nls = NULL; |
85 |
char * file = NULL; |
char *file = NULL; |
86 |
|
|
87 |
nls = get_gettext_lang (); |
nls = get_gettext_lang (); |
88 |
if (nls) { |
if (nls) { |
102 |
/* check if the default key from the gpg.conf file is available in the |
/* check if the default key from the gpg.conf file is available in the |
103 |
keyring. if not, bail out because encryption won't work properly then. */ |
keyring. if not, bail out because encryption won't work properly then. */ |
104 |
static int |
static int |
105 |
check_default_key (gpgme_keycache_t kc) |
check_default_key (gpg_keycache_t kc) |
106 |
{ |
{ |
107 |
gpgme_key_t key; |
gpgme_key_t key; |
108 |
gpgme_error_t err = GPGME_No_Error; |
gpgme_error_t err = GPG_ERR_NO_ERROR; |
109 |
char * defkey; |
char * defkey; |
110 |
|
|
111 |
defkey = get_gnupg_default_key (); |
defkey = get_gnupg_default_key (); |
112 |
if (defkey) |
if (defkey) |
113 |
err = gpgme_keycache_find_key (kc, defkey, 0, &key); |
err = gpg_keycache_find_key (kc, defkey, 0, &key); |
114 |
free_if_alloc (defkey); |
free_if_alloc (defkey); |
115 |
return err? -1 : 0; |
return err? -1 : 0; |
116 |
} /* check_default_key */ |
} /* check_default_key */ |
137 |
return NULL; |
return NULL; |
138 |
strncpy (currdir, cmd, 255); |
strncpy (currdir, cmd, 255); |
139 |
j = strlen (currdir); |
j = strlen (currdir); |
140 |
while (j--) |
while (j--) { |
|
{ |
|
141 |
if (currdir[j] == '\\') |
if (currdir[j] == '\\') |
142 |
break; |
break; |
143 |
} |
} |
151 |
static int |
static int |
152 |
check_crypto_engine (void) |
check_crypto_engine (void) |
153 |
{ |
{ |
154 |
int ma=1, mi=2, pa=4; /* GPG 1.2.4 */ |
int ma=1, mi=4, pa=1; /* GPG 1.4.1 */ |
155 |
int rc; |
int rc; |
156 |
|
|
157 |
rc = check_gnupg_engine (&ma, &mi, &pa); |
rc = check_gnupg_engine (&ma, &mi, &pa); |
162 |
else if (rc) { |
else if (rc) { |
163 |
log_box (_("WinPT Error"), MB_ERR, |
log_box (_("WinPT Error"), MB_ERR, |
164 |
_("Sorry, you need a newer GPG version.\n" |
_("Sorry, you need a newer GPG version.\n" |
165 |
"GPG version %d.%d.%d requred GPG version 1.2.4"), |
"GPG version %d.%d.%d required GPG version 1.4.0"), |
166 |
ma, mi, pa); |
ma, mi, pa); |
167 |
return rc; |
return rc; |
168 |
} |
} |
169 |
/* We enable smartcard support for GPG: 1.9.x and >= 1.3.90 */ |
/* We enable smartcard support for GPG: 1.9.x or >= 1.4.0 */ |
170 |
if (ma > 1 || mi >= 9 || mi > 3) |
if (ma >= 1 && mi >= 4) |
171 |
scard_support = 1; |
scard_support = 1; |
172 |
|
|
173 |
gpgver[0] = ma; |
gpgver[0] = ma; |
196 |
} |
} |
197 |
|
|
198 |
|
|
199 |
|
static void |
200 |
|
enable_mobile_mode (void) |
201 |
|
{ |
202 |
|
memset (®_prefs, 0, sizeof (reg_prefs)); |
203 |
|
reg_prefs.always_trust = 0; |
204 |
|
reg_prefs.auto_backup = 0; |
205 |
|
reg_prefs.cache_time = 0; |
206 |
|
reg_prefs.expert = 0; |
207 |
|
reg_prefs.keylist_mode = 1; |
208 |
|
reg_prefs.kserv_conf = m_strdup ("keyserver.conf"); |
209 |
|
reg_prefs.no_zip_mmedia = 1; |
210 |
|
reg_prefs.use_tmpfiles = 1; |
211 |
|
reg_prefs.word_wrap = 80; |
212 |
|
reg_prefs.use_viewer = 0; /* XXX */ |
213 |
|
} |
214 |
|
|
215 |
|
char* get_subkey_fingerprint (gpgme_ctx_t ctx, const char *keyid); |
216 |
|
|
217 |
|
|
218 |
int WINAPI |
int WINAPI |
219 |
WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd) |
WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd) |
220 |
{ |
{ |
224 |
int first_start = 0, start_gpgprefs = 0; |
int first_start = 0, start_gpgprefs = 0; |
225 |
const char * s; |
const char * s; |
226 |
MSG msg; |
MSG msg; |
227 |
HWND hwnd; |
HWND hwnd = NULL; |
228 |
|
|
229 |
glob_hinst = hinst; |
glob_hinst = hinst; |
230 |
|
|
|
gpgme_lib_init (); |
|
231 |
#ifdef _DEBUG |
#ifdef _DEBUG |
232 |
gpgme_set_debug_mode (1); |
gpg_set_debug_mode (1); |
233 |
|
debug = 1; |
234 |
#endif |
#endif |
|
gpgme_set_pgm_string ("WinPT "PGM_VERSION); |
|
235 |
|
|
236 |
s = PTD_get_version (); |
s = PTD_get_version (); |
237 |
if (strcmp (s, "0.8.0")) |
if (strcmp (s, "0.8.0")) { |
|
{ |
|
238 |
log_box (_("Privacy Tray Dynamic (PTD)"), MB_ERR, |
log_box (_("Privacy Tray Dynamic (PTD)"), MB_ERR, |
239 |
_("Please update your PTD.dll to the newest version, " |
_("Please update your PTD.dll to the newest version, " |
240 |
"the version (%s) you use is too old."), s); |
"the version (%s) you use is too old."), s); |
241 |
return 0; |
return 0; |
242 |
} |
} |
243 |
|
|
244 |
if (gpg_md_selftest ()) |
if (gpg_md_selftest ()) { |
|
{ |
|
245 |
msg_box (NULL, _("Cryptographic selftest failed."), |
msg_box (NULL, _("Cryptographic selftest failed."), |
246 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
247 |
return 0; |
return 0; |
248 |
} |
} |
249 |
|
|
250 |
|
s = gpgme_check_version ("1.1.0"); |
251 |
|
if (!s || !*s) { |
252 |
|
msg_box (NULL, _("A newer GPGME version is needed."), "WinPT Error", MB_ERR); |
253 |
|
return 0; |
254 |
|
} |
255 |
|
|
256 |
|
if (cmdline && stristr (cmdline, "--mobile")) { |
257 |
|
msg_box (NULL, "WARNING: mobile modus is not fully implemented yet!", |
258 |
|
"WinPT", MB_INFO); |
259 |
|
mobile = 1; |
260 |
|
} |
261 |
|
|
262 |
set_default_kserver (); |
set_default_kserver (); |
263 |
regist_inst_gnupg (1); |
|
264 |
regist_inst_winpt (1, &created); |
if (!mobile) { |
265 |
if (!created) |
regist_inst_gnupg (1); |
266 |
{ |
regist_inst_winpt (1, &created); |
267 |
|
} |
268 |
|
else { |
269 |
|
enable_mobile_mode (); |
270 |
|
/* XXX: ask for GPG path */ |
271 |
|
created = 1; /* Disable registry writing */ |
272 |
|
} |
273 |
|
|
274 |
|
if (!created) { |
275 |
memset (®_prefs, 0, sizeof (reg_prefs)); |
memset (®_prefs, 0, sizeof (reg_prefs)); |
276 |
reg_prefs.use_tmpfiles = 1; /* default */ |
reg_prefs.use_tmpfiles = 1; /* default */ |
277 |
|
reg_prefs.fm.progress = 0; /* XXX: fix the bug and enable it again */ |
278 |
get_reg_winpt_prefs (®_prefs); |
get_reg_winpt_prefs (®_prefs); |
279 |
if (!reg_prefs.no_hotkeys) |
if (!reg_prefs.no_hotkeys) |
280 |
hotkeys_modify (); |
hotkeys_modify (); |
281 |
} |
} |
282 |
|
|
283 |
rc = gnupg_check_homedir (); |
rc = gnupg_check_homedir (); |
284 |
if (rc) |
if (rc) { |
|
{ |
|
285 |
log_box (_("WinPT Error"), MB_ERR, |
log_box (_("WinPT Error"), MB_ERR, |
286 |
_("GPG home directory is not set correctly.\n" |
_("GPG home directory is not set correctly.\n" |
287 |
"Please check the GPG registry settings:\n%s."), |
"Please check the GPG registry settings:\n%s."), |
288 |
winpt_strerror (rc)); |
winpt_strerror (rc)); |
289 |
const char * s = get_filename_dlg (GetActiveWindow (), FILE_OPEN, |
const char * s = get_fileopen_dlg (GetActiveWindow (), |
290 |
_("Select GPG Public Keyring"), |
_("Select GPG Public Keyring"), |
291 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), |
292 |
NULL); |
NULL); |
293 |
if (s && !file_exist_check (s)) |
if (s != NULL) { |
|
{ |
|
294 |
size_t n; |
size_t n; |
295 |
char * p = strrchr (s, '\\'); |
char * p = strrchr (s, '\\'); |
296 |
if (!p) |
if (!p) |
309 |
gnupg_check_homedir (); /* change gpgProgram if needed */ |
gnupg_check_homedir (); /* change gpgProgram if needed */ |
310 |
} |
} |
311 |
} |
} |
312 |
else |
else { |
|
{ |
|
313 |
msg_box (NULL, _("GPG home directory could not be determited."), |
msg_box (NULL, _("GPG home directory could not be determited."), |
314 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
315 |
goto start; |
goto start; |
317 |
} |
} |
318 |
|
|
319 |
rc = check_gnupg_prog (); |
rc = check_gnupg_prog (); |
320 |
if (rc) |
if (rc) { |
|
{ |
|
321 |
if (msg_box (NULL, _("Could not find the GPG binary (gpg.exe).\n" |
if (msg_box (NULL, _("Could not find the GPG binary (gpg.exe).\n" |
322 |
"Do you want to start the GPG preferences to " |
"Do you want to start the GPG preferences to " |
323 |
"correct this problem?"), _("WinPT Error"), |
"correct this problem?"), _("WinPT Error"), |
375 |
} |
} |
376 |
|
|
377 |
load_keyserver_conf (cmdline? 1 : 0); |
load_keyserver_conf (cmdline? 1 : 0); |
378 |
|
|
379 |
if (cmdline && (stristr (cmdline, "--keymanager") |
if (cmdline && (stristr (cmdline, "--keymanager") |
380 |
|| stristr (cmdline, "--cardmanager"))) { |
|| stristr (cmdline, "--cardmanager"))) { |
381 |
update_keycache (GetDesktopWindow ()); |
update_keycache (GetDesktopWindow ()); |
382 |
if (stristr (cmdline, "keymanager")) |
if (stristr (cmdline, "keymanager")) |
383 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC, |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYMISC, |
384 |
GetDesktopWindow(), keymanager_dlg_proc, NULL, |
GetDesktopWindow(), keymanager_dlg_proc, NULL, |
385 |
_("Key Manager"), IDS_WINPT_KEYMISC); |
_("Key Manager"), IDS_WINPT_KEYMISC); |
386 |
else { |
else { |
387 |
gpgme_card_t crd = smartcard_init (); |
gpg_card_t crd = gpg_card_load (); |
388 |
if (crd) |
if (crd) |
389 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT, |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_CARD_EDIT, |
390 |
GetDesktopWindow(), card_edit_dlg_proc, |
GetDesktopWindow(), card_edit_dlg_proc, |
391 |
(LPARAM)crd, _("Card Manager"), |
(LPARAM)crd, _("Card Manager"), |
392 |
IDS_WINPT_CARD_EDIT); |
IDS_WINPT_CARD_EDIT); |
393 |
gpgme_card_release (crd); |
gpg_card_release (crd); |
394 |
} |
} |
395 |
keycache_release (); |
keycache_release (0); |
396 |
free_gnupg_table (); |
free_gnupg_table (); |
397 |
return 0; |
return 0; |
398 |
} |
} |
403 |
return 0; |
return 0; |
404 |
} |
} |
405 |
|
|
406 |
/*if (file_exist_check ("loadimage.exe")) |
if (cmdline) { |
407 |
PTD_create_loadimage (NULL);*/ |
if (stristr (cmdline, "--enable-debug") || stristr (cmdline, "--debug")) { |
408 |
|
gpg_set_debug_mode (1); |
409 |
if (cmdline && stristr (cmdline, "--enable-debug")) { |
winpt_debug_msg (); |
410 |
gpgme_set_debug_mode (1); |
debug = 1; |
411 |
winpt_debug_msg (); |
} |
|
debug = 1; |
|
412 |
} |
} |
413 |
|
|
414 |
wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT)); |
wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT)); |
435 |
UpdateWindow (hwnd); |
UpdateWindow (hwnd); |
436 |
|
|
437 |
if (!first_start && !start_gpgprefs) { |
if (!first_start && !start_gpgprefs) { |
438 |
gnupg_backup_options (1); |
gnupg_backup_options (); |
|
gnupg_backup_options (0); |
|
|
|
|
439 |
rc = check_crypto_engine (); |
rc = check_crypto_engine (); |
440 |
if (rc) { |
if (rc) { |
441 |
DestroyWindow (hwnd); |
DestroyWindow (hwnd); |
443 |
return 0; |
return 0; |
444 |
} |
} |
445 |
} |
} |
446 |
|
|
447 |
if (start_gpgprefs) |
if (start_gpgprefs) { |
|
{ |
|
448 |
char *ring; |
char *ring; |
449 |
size_t size = 0; |
size_t size = 0; |
450 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
456 |
} |
} |
457 |
|
|
458 |
if (first_start) { |
if (first_start) { |
459 |
struct key_wizard_s c, dummy; |
struct first_start_s fs; |
460 |
|
struct genkey_s c; |
461 |
|
HWND h; |
462 |
start: |
start: |
463 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, hwnd, |
h = GetDesktopWindow (); |
464 |
first_run_dlg_proc, (LPARAM)&dummy); |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, h, |
465 |
switch (dummy.interactive) |
gpgprefs_dlg_proc, NULL); |
466 |
{ |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, h, |
467 |
|
first_run_dlg_proc, (LPARAM)&fs); |
468 |
|
switch (fs.choice) { |
469 |
case SETUP_KEYGEN: |
case SETUP_KEYGEN: |
470 |
c.interactive = 1; |
c.interactive = 1; |
471 |
|
c.first_start = 1; |
472 |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD, |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD, |
473 |
hwnd, keygen_wizard_dlg_proc, (LPARAM)&c); |
h, keygen_wizard_dlg_proc, (LPARAM)&c); |
474 |
if (!rc) |
if (!rc) |
475 |
goto start; |
goto start; |
|
update_keycache (hwnd); |
|
|
check_crypto_engine (); |
|
476 |
break; |
break; |
477 |
|
|
478 |
case SETUP_IMPORT: |
case SETUP_IMPORT: |
481 |
msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR); |
msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR); |
482 |
goto start; |
goto start; |
483 |
} |
} |
|
update_keycache (hwnd); |
|
|
check_crypto_engine (); |
|
484 |
break; |
break; |
485 |
|
|
|
case SETUP_EXISTING: |
|
|
break; /* todo */ |
|
|
|
|
486 |
case -1: |
case -1: |
487 |
DestroyWindow (hwnd); |
DestroyWindow (hwnd); |
488 |
free_gnupg_table (); |
free_gnupg_table (); |
489 |
return 0; |
return 0; |
490 |
} |
} |
491 |
|
update_keycache (hwnd); |
492 |
|
check_crypto_engine (); |
493 |
} |
} |
494 |
else { |
else { |
495 |
gpgme_keycache_t c; |
gpg_keycache_t c; |
496 |
update_keycache (hwnd); |
update_keycache (hwnd); |
497 |
c = keycache_get_ctx (1); |
c = keycache_get_ctx (1); |
498 |
if (!c || !gpgme_keycache_count (c)) { |
if (!c || !gpg_keycache_get_size (c)) { |
499 |
gnupg_display_error (); |
gnupg_display_error (); |
500 |
msg_box (hwnd, _("The keycache was not initialized or is empty.\n" |
msg_box (hwnd, _("The keycache was not initialized or is empty.\n" |
501 |
"Please check your GPG config (keyrings, pathes...)"), |
"Please check your GPG config (keyrings, pathes...)"), |