1 |
/* WinPT.cpp - Windows Privacy Tray (WinPT) |
2 |
* Copyright (C) 2000-2009, 2012 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 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <shlobj.h> |
22 |
|
23 |
#include "resource.h" |
24 |
#include "wptTypes.h" |
25 |
#include "wptW32API.h" |
26 |
#include "wptVersion.h" |
27 |
#include "wptErrors.h" |
28 |
#include "wptGPG.h" |
29 |
#include "wptRegistry.h" |
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptDlgs.h" |
32 |
#include "wptNLS.h" |
33 |
#include "wptKeyserver.h" |
34 |
#include "wptCard.h" |
35 |
#include "wptFileManager.h" |
36 |
#include "wptContext.h" |
37 |
#include "wptCardEdit.h" |
38 |
#include "wptCrypto.h" |
39 |
#include "wptUTF8.h" |
40 |
|
41 |
void remove_crit_file_attrs (const char *fname, int force); |
42 |
|
43 |
/* Global variables. */ |
44 |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
45 |
HWND glob_hwnd; /* global window handle for the dialogs */ |
46 |
int scard_support = 0; |
47 |
int debug = 0; |
48 |
int gpg_read_only = 0; |
49 |
char gpgver[3]; |
50 |
/* End */ |
51 |
|
52 |
|
53 |
/* Retrieve the product verion of the given file @fname. |
54 |
Format: MAJOR.MINOR.PATCH1.PATCH2 |
55 |
Return value: 0 on success. */ |
56 |
int |
57 |
get_file_version (const char *fname, WORD *major, WORD *minor, WORD *patch1, WORD *patch2) |
58 |
{ |
59 |
VS_FIXEDFILEINFO *inf; |
60 |
char file[MAX_PATH+1] = {0}; |
61 |
LPVOID buf, data = NULL; |
62 |
UINT qlen = 0; |
63 |
|
64 |
strncpy (file, fname, MAX_PATH); |
65 |
DWORD size = GetFileVersionInfoSize (file, NULL); |
66 |
if (!size) |
67 |
return -1; |
68 |
|
69 |
buf = (LPVOID)new char[size]; |
70 |
if (!buf) |
71 |
BUG (NULL); |
72 |
|
73 |
int err = 0; |
74 |
if (!GetFileVersionInfo (file, 0, size, buf)) { |
75 |
err = -1; |
76 |
goto fail; |
77 |
} |
78 |
|
79 |
if (!VerQueryValue (buf, (char*)"\\", &data, &qlen) || !qlen) { |
80 |
err = -1; |
81 |
goto fail; |
82 |
} |
83 |
|
84 |
inf = (VS_FIXEDFILEINFO*)data; |
85 |
*major = HIWORD (inf->dwProductVersionMS); |
86 |
*minor = LOWORD (inf->dwProductVersionMS); |
87 |
*patch1 = HIWORD (inf->dwProductVersionLS); |
88 |
*patch2 = LOWORD (inf->dwProductVersionLS); |
89 |
|
90 |
fail: |
91 |
delete [](char*)buf; |
92 |
return err; |
93 |
} |
94 |
|
95 |
|
96 |
/* Load the key cache and rebuild the signature cache. */ |
97 |
int |
98 |
update_keycache (HWND hwnd) |
99 |
{ |
100 |
refresh_cache_s rcs; |
101 |
|
102 |
/* no need to rebuild the sig cache each time. */ |
103 |
memset (&rcs, 0, sizeof (rcs)); |
104 |
rcs.kring_update = 1; |
105 |
int err = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, hwnd, |
106 |
keycache_dlg_proc, (LPARAM)&rcs); |
107 |
if (err) { |
108 |
char *cfgf = get_gnupg_config (); |
109 |
if (cfgf && check_gnupg_options (cfgf, 0) == WPTERR_FILE_EXIST) |
110 |
msg_box (GetDesktopWindow (), |
111 |
_("The gpg.conf contains at least one argument which points to a non-existing file."), "WinPT", MB_ERR); |
112 |
free_if_alloc (cfgf); |
113 |
return -1; |
114 |
} |
115 |
return 0; |
116 |
} |
117 |
|
118 |
|
119 |
/* Set GPGME debug mode. If @val is 0, the debug mode is disabled. */ |
120 |
void |
121 |
gpg_set_debug_mode (int val) |
122 |
{ |
123 |
static char buf[MAX_PATH+1]; |
124 |
|
125 |
/* XXX: no gpgme.dbg is created. */ |
126 |
if (val > 0) { |
127 |
char tmp[MAX_PATH+1]; |
128 |
if (GetTempPath (DIM (tmp)-1, tmp) > 0) { |
129 |
_snprintf (buf, DIM (buf)-1, "GPGME_DEBUG=5:%sgpgme.dbg", tmp); |
130 |
putenv (buf); |
131 |
} |
132 |
} |
133 |
else |
134 |
putenv ("GPGME_DEBUG="); |
135 |
} |
136 |
|
137 |
|
138 |
/* Return true if the GPG environment is useable. */ |
139 |
static bool |
140 |
gpg_prefs_ok (void) |
141 |
{ |
142 |
char *p = get_reg_entry_gpg4win ("gpg.exe"); |
143 |
if (!p || file_exist_check (p) != 0) { |
144 |
free_if_alloc (p); |
145 |
p = get_reg_entry_gpg ("gpgProgram"); |
146 |
if (!p || file_exist_check (p) != 0) { |
147 |
free_if_alloc (p); |
148 |
log_debug ("gpg_prefs_ok: could not locate gpg.exe"); |
149 |
return false; |
150 |
} |
151 |
} |
152 |
free_if_alloc (p); |
153 |
p = get_reg_entry_gpg4win (NULL); |
154 |
if (!p || dir_exist_check (p) != 0) { |
155 |
free_if_alloc (p); |
156 |
p = get_reg_entry_gpg ("HomeDir"); |
157 |
if (!p || dir_exist_check (p) != 0) { |
158 |
free_if_alloc (p); |
159 |
log_debug ("gpg_prefs_ok: could not determine home directory"); |
160 |
return false; |
161 |
} |
162 |
} |
163 |
free_if_alloc (p); |
164 |
return true; |
165 |
} |
166 |
|
167 |
|
168 |
/* Check gpg files if they are read-only and ask the user |
169 |
if this should be corrected. */ |
170 |
static void |
171 |
check_readonly_attr (const char *homedir) |
172 |
{ |
173 |
const char *files[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg", NULL}; |
174 |
|
175 |
log_debug("check if there are gpg files with a read-only attribute"); |
176 |
for (int i=0; files[i] != NULL; i++) { |
177 |
char *file = make_filename (homedir, files[i], NULL); |
178 |
remove_crit_file_attrs (file, 0); |
179 |
free_if_alloc (file); |
180 |
} |
181 |
} |
182 |
|
183 |
|
184 |
/* Load the GPG environment. On the first start, some |
185 |
checks are performed to find out in what state GPG is. |
186 |
Return value: 0 everything OK. |
187 |
>0 fatal error. |
188 |
-1 public keyring is empty or does not exist. */ |
189 |
static int |
190 |
load_gpg_env (void) |
191 |
{ |
192 |
SECURITY_ATTRIBUTES sec_attr; |
193 |
char *p; |
194 |
char *pkr; |
195 |
int err = 0; |
196 |
|
197 |
p = get_reg_entry_gpg4win ("gpg.exe"); |
198 |
if (!p) |
199 |
return (1); |
200 |
if (file_exist_check (p)) { |
201 |
free_if_alloc (p); |
202 |
return (1); |
203 |
} |
204 |
free_if_alloc (p); |
205 |
|
206 |
p = get_reg_entry_gpg ("HomeDir"); |
207 |
if (!p || dir_exist_check (p) != 0) { |
208 |
free_if_alloc (p); |
209 |
p = multi_gnupg_path (0); |
210 |
} |
211 |
if (p && dir_exist_check (p)) { |
212 |
memset (&sec_attr, 0, sizeof (sec_attr)); |
213 |
sec_attr.nLength = sizeof (sec_attr); |
214 |
if (!CreateDirectory (p, &sec_attr)) { |
215 |
msg_box (GetDesktopWindow (), |
216 |
_("Could not create GPG home directory"), |
217 |
_("WinPT Error"), MB_ERR); |
218 |
free_if_alloc (p); |
219 |
return (2); |
220 |
} |
221 |
} |
222 |
check_readonly_attr (p); |
223 |
pkr = make_filename (p, "pubring", "gpg"); |
224 |
free_if_alloc (p); |
225 |
if (get_file_size (pkr) == 0) |
226 |
err = -1; |
227 |
free_if_alloc (pkr); |
228 |
return err; |
229 |
} |
230 |
|
231 |
|
232 |
/* check if the default key from the gpg.conf file is available in the |
233 |
keyring. if not, bail out because encryption won't work properly then. */ |
234 |
static int |
235 |
check_default_key (void) |
236 |
{ |
237 |
gpgme_key_t key; |
238 |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
239 |
gpg_keycache_t kc; |
240 |
char *defkey; |
241 |
|
242 |
kc = keycache_get_ctx (0); |
243 |
defkey = get_gnupg_default_key (); |
244 |
if (defkey != NULL) { |
245 |
err = gpg_keycache_find_key (kc, defkey, 0, &key); |
246 |
if (err) { |
247 |
free_if_alloc (defkey); |
248 |
return -1; |
249 |
} |
250 |
} |
251 |
else { |
252 |
/* Actually this is just a warning but we still continue. */ |
253 |
msg_box (GetDesktopWindow (), _("No useable secret key found."), |
254 |
_("WinPT Warning"), MB_WARN); |
255 |
return 0; |
256 |
} |
257 |
|
258 |
/* Because the secret key listing has no information |
259 |
about the validity/status, we need to check the public key. */ |
260 |
kc = keycache_get_ctx (1); |
261 |
if (!gpg_keycache_find_key (kc, defkey, 0, &key) && |
262 |
(key->revoked || key->expired)) { |
263 |
msg_box (GetDesktopWindow (), _("Default secret key is unuseable"), |
264 |
_("WinPT Warning"), MB_ERR); |
265 |
free_if_alloc (defkey); |
266 |
return -1; |
267 |
} |
268 |
free_if_alloc (defkey); |
269 |
return 0; |
270 |
} |
271 |
|
272 |
|
273 |
/* Check that the underlying crypto engine fullfills the minimal |
274 |
requirements so all commands work properly. */ |
275 |
static bool |
276 |
check_crypto_engine (void) |
277 |
{ |
278 |
int ma = 0, mi = 0, pa = 0; |
279 |
|
280 |
int rc = check_gnupg_engine (NEED_GPG_VERSION, &ma, &mi, &pa); |
281 |
if (rc == -1) { |
282 |
msg_box (GetDesktopWindow (), _("Could not read GnuPG version."), |
283 |
_("WinPT Error"), MB_ERR); |
284 |
return false; |
285 |
} |
286 |
else if (rc) { |
287 |
log_box (_("WinPT Error"), MB_ERR, |
288 |
_("A newer GPG version is needed.\n" |
289 |
"Current GPG version %d.%d.%d, required "NEED_GPG_VERSION), |
290 |
ma, mi, pa); |
291 |
return false; |
292 |
} |
293 |
|
294 |
// TODO: smart card support needs to be revamped |
295 |
// and adjusted according to newer OpenPGP cards. |
296 |
/* |
297 |
if ((ma > 1 || pa >= 4) && pcsc_available ()) |
298 |
scard_support = 1; |
299 |
*/ |
300 |
scard_support = 0; |
301 |
|
302 |
gpgver[0] = ma; |
303 |
gpgver[1] = mi; |
304 |
gpgver[2] = pa; |
305 |
return true; |
306 |
} |
307 |
|
308 |
|
309 |
|
310 |
/* Check if both keyrings are empty. This indicates that |
311 |
WinPT should offer to generate a key pair. */ |
312 |
static bool |
313 |
check_for_empty_keyrings (bool pub_only) |
314 |
{ |
315 |
int n = 0; |
316 |
char *p = get_gnupg_keyring (1, 0); |
317 |
if (file_exist_check (p) == 0 && get_file_size (p) == 0) |
318 |
n++; |
319 |
free_if_alloc (p); |
320 |
if (pub_only) |
321 |
return n == 1? true : false; |
322 |
p = get_gnupg_keyring (0, 0); |
323 |
if (file_exist_check (p) == 0 && get_file_size (p) == 0) |
324 |
n++; |
325 |
free_if_alloc (p); |
326 |
return n == 2? true : false; |
327 |
} |
328 |
|
329 |
|
330 |
|
331 |
/* Display info message that WinPT is now in debug mode. */ |
332 |
void |
333 |
winpt_debug_msg (void) |
334 |
{ |
335 |
char output[512]; |
336 |
char temp[MAX_PATH+1]; |
337 |
|
338 |
GetTempPath (DIM (temp) - 1, temp); |
339 |
_snprintf (output, DIM (output)-1, |
340 |
"The GPGME output file is %sgpgme.dbg\n" |
341 |
"The WinPT output file is %swinpt-%08lx.log\n", temp, temp, GetCurrentProcessId()); |
342 |
MessageBox (NULL, output, "WinPT now runs in DEBUG MODE", MB_INFO); |
343 |
} |
344 |
|
345 |
|
346 |
/* Search for insecure ElGamal keys and return the |
347 |
number of founded keys. */ |
348 |
static int |
349 |
count_insecure_elgkeys (void) |
350 |
{ |
351 |
gpgme_key_t key; |
352 |
|
353 |
int n = 0; |
354 |
gpg_keycache_t pc = keycache_get_ctx (1); |
355 |
while (!gpg_keycache_next_key (pc, 0, &key)) { |
356 |
if (key->subkeys->pubkey_algo == GPGME_PK_ELG) |
357 |
n++; |
358 |
} |
359 |
gpg_keycache_rewind (pc); |
360 |
return n; |
361 |
} |
362 |
|
363 |
|
364 |
/* Return 1 if the current OS version is at least Windows XP */ |
365 |
static int |
366 |
check_os_version (void) |
367 |
{ |
368 |
OSVERSIONINFOA osver; |
369 |
memset (&osver, 0, sizeof (osver)); |
370 |
osver.dwOSVersionInfoSize = sizeof (osver); |
371 |
|
372 |
if (!GetVersionEx (&osver)) { |
373 |
MessageBox (NULL, _("Could not read the OS version."), _("WinPT Error"), MB_ERR); |
374 |
return 0; |
375 |
} |
376 |
|
377 |
if (osver.dwMajorVersion < 5 || |
378 |
(osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0)) { |
379 |
MessageBox (NULL, _("WinPT requires Windows XP or higher."), _("WinPT Error"), MB_ERR); |
380 |
return 0; |
381 |
} |
382 |
|
383 |
return 1; |
384 |
} |
385 |
|
386 |
/* Main entry point. */ |
387 |
int WINAPI |
388 |
WinMain (HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int showcmd) |
389 |
{ |
390 |
WNDCLASS wc = {0, winpt_main_proc, 0, 0, hinst, 0, 0, 0, 0, PGM_NAME}; |
391 |
HACCEL accel_tab; |
392 |
MSG msg; |
393 |
HWND hwnd = NULL; |
394 |
WORD ver[4], ptdver[4]; |
395 |
|
396 |
const char *s; |
397 |
int rc, ec, created = 0; |
398 |
int first_start = 0, start_gpgprefs = 0; |
399 |
int winpt_inst_found = 0; |
400 |
int start_manager = 0; |
401 |
|
402 |
log_debug("check OS version"); |
403 |
if (!check_os_version ()) |
404 |
return 0; |
405 |
|
406 |
glob_hinst = hinst; |
407 |
|
408 |
/* Check as early as possible for debug flags and activate |
409 |
the debug mode if requested */ |
410 |
if (cmdline && (stristr (cmdline, "--enable-debug") || |
411 |
stristr (cmdline, "--debug"))) { |
412 |
//gpg_set_debug_mode (1); |
413 |
winpt_debug_msg (); |
414 |
debug = 1; |
415 |
} |
416 |
|
417 |
/* Allow to shutdown the process, for instance by an installer */ |
418 |
if (cmdline && stristr (cmdline, "--stop")) { |
419 |
hwnd = FindWindow ("WinPT", "WinPT"); |
420 |
if (hwnd != NULL) { |
421 |
log_debug ("shutdown an existing WinPT process"); |
422 |
PostMessage (hwnd, WM_DESTROY, 0, 0); |
423 |
} |
424 |
return 0; |
425 |
} |
426 |
|
427 |
log_debug("check PTD and GPGME version"); |
428 |
if (get_file_version ("winpt.exe", &ver[0], &ver[1], &ver[2], &ver[3])) { |
429 |
log_box(_("WinPT Error"), MB_ERR, _("Could not read file version of WinPT")); |
430 |
return 0; |
431 |
} |
432 |
ec = get_file_version ("PTD.dll", &ptdver[0], &ptdver[1], &ptdver[2], &ptdver[3]); |
433 |
if (!ec && (ptdver[0] != ver[0] || ptdver[1] != ver[1] || ptdver[2] != ver[2])) { |
434 |
log_box (_("WinPT Error"), MB_ERR, |
435 |
_("The PTD.dll file has a different version than WinPT.exe\n" |
436 |
"Please update the PTD.dll to version %d.%d.%d"), |
437 |
ver[0], ver[1], ver[2]); |
438 |
return 0; |
439 |
} |
440 |
|
441 |
s = gpgme_check_version (NEED_GPGME_VERSION); |
442 |
if (!s || !*s) { |
443 |
msg_box (GetDesktopWindow (), |
444 |
_("A newer GPGME version is needed; at least "NEED_GPGME_VERSION), |
445 |
_("WinPT Error"), MB_ERR); |
446 |
return 0; |
447 |
} |
448 |
|
449 |
CreateMutex (NULL, TRUE, PGM_NAME); |
450 |
if (GetLastError () == ERROR_ALREADY_EXISTS) { |
451 |
log_debug ("Found running WinPT instance"); |
452 |
winpt_inst_found = 1; |
453 |
} |
454 |
|
455 |
gettext_set_user_domain (); |
456 |
|
457 |
regist_inst_gnupg (1); |
458 |
regist_inst_winpt (1, &created); |
459 |
|
460 |
if (!created) { |
461 |
memset (®_prefs, 0, sizeof (reg_prefs)); |
462 |
get_reg_winpt_prefs (®_prefs); |
463 |
reg_prefs.fm.progress = 0; /* TODO: fix the bug and enable it again */ |
464 |
if (gnupg_load_config () == -2) |
465 |
msg_box (GetDesktopWindow (), |
466 |
_("The gpg.conf file contains the 'textmode' option\n" |
467 |
"which leads to broken binary output during decryption.\n" |
468 |
"If this is on purpose, just continue otherwise the option should be disabled."), |
469 |
_("WinPT Error"), MB_ERR); |
470 |
} |
471 |
|
472 |
if (is_gpg4win_installed ()) { |
473 |
log_debug ("gpg4win: load gpg environment"); |
474 |
load_gpg_env (); /* TODO: check return code. */ |
475 |
} |
476 |
|
477 |
rc = gnupg_check_homedir (); |
478 |
if (rc) { |
479 |
log_box (_("WinPT Error"), MB_ERR, |
480 |
_("GPG home directory is not set correctly.\n" |
481 |
"Please check the GPG registry settings:\n%s."), |
482 |
winpt_strerror (rc)); |
483 |
s = get_fileopen_dlg (GetActiveWindow (), |
484 |
_("Select GPG Public Keyring"), |
485 |
"GPG Keyrings (*.gpg)\0*.gpg\0\0", |
486 |
NULL); |
487 |
|
488 |
char *p; |
489 |
if (s != NULL && (p = strrchr (s, '\\'))) { |
490 |
char *path = substr (s, 0, (p-s)); |
491 |
set_reg_entry_gpg ("HomeDir", path); |
492 |
free_if_alloc (path); |
493 |
} |
494 |
else { |
495 |
msg_box (GetDesktopWindow (), |
496 |
_("GPG home directory could not be determined."), |
497 |
_("WinPT Error"), MB_ERR); |
498 |
goto start; |
499 |
} |
500 |
} |
501 |
|
502 |
rc = check_gnupg_prog (); |
503 |
if (rc) { |
504 |
if (msg_box (GetDesktopWindow (), |
505 |
_("Could not find the GPG binary (gpg.exe).\n" |
506 |
"Do you want to start the GPG preferences to " |
507 |
"correct this problem?"), _("WinPT Error"), |
508 |
MB_INFO|MB_YESNO) == IDYES) |
509 |
start_gpgprefs = 1; |
510 |
else { |
511 |
msg_box (GetDesktopWindow (), |
512 |
winpt_strerror (rc), _("WinPT Error"), MB_ERR); |
513 |
return 0; |
514 |
} |
515 |
} |
516 |
|
517 |
rc = gnupg_access_files (); |
518 |
if (!start_gpgprefs && rc) { |
519 |
if (rc == WPTERR_GPG_KEYRINGS || rc == WPTERR_GPG_OPT_KEYRINGS) { |
520 |
ec = msg_box (GetDesktopWindow (), |
521 |
_("Could not access and/or find the public and secret keyring.\n" |
522 |
"If this is an accident, quit the program and fix it.\n\n" |
523 |
"Continue if you want WinPT to offer you more choices.\n"), |
524 |
"WinPT", MB_INFO|MB_YESNO); |
525 |
if (ec == IDYES) |
526 |
first_start = 1; |
527 |
} |
528 |
if (!first_start) { |
529 |
msg_box (GetDesktopWindow (), winpt_strerror (rc), _("WinPT Error"), MB_ERR); |
530 |
return 0; |
531 |
} |
532 |
} |
533 |
if (check_for_empty_keyrings (false)) { |
534 |
log_debug ("found empty keyrings, assume first start"); |
535 |
first_start = 1; |
536 |
} |
537 |
|
538 |
if (!first_start) { |
539 |
rc = gpg_check_permissions (1); |
540 |
if (rc && rc == 2) /* 2 means read-only mode. */ |
541 |
gpg_read_only = 1; |
542 |
else if (rc) |
543 |
return 0; |
544 |
} |
545 |
|
546 |
init_gnupg_table (); |
547 |
|
548 |
if (fm_parse_command_line (cmdline) > 0) { |
549 |
log_debug ("processed arguments with File Manager, exiting..."); |
550 |
free_gnupg_table (); |
551 |
return 0; |
552 |
} |
553 |
|
554 |
rc = kserver_load_conf (); |
555 |
if (rc) |
556 |
msg_box (GetDesktopWindow (), winpt_strerror (rc), |
557 |
_("Keyserver"), MB_ERR); |
558 |
|
559 |
if (cmdline && (stristr (cmdline, "--keymanager") |
560 |
|| stristr (cmdline, "--cardmanager"))) { |
561 |
/* If an instance of WinPT is running, just send the command |
562 |
to open the key manager. Otherwise start a new instance. */ |
563 |
HWND tray = FindWindow ("WinPT", "WinPT"); |
564 |
if (stristr (cmdline, "keymanager")) |
565 |
start_manager = ID_WINPT_KEY; |
566 |
else |
567 |
start_manager = ID_WINPT_CARD; |
568 |
if (tray != NULL) { |
569 |
PostMessage (tray, WM_COMMAND, start_manager, 0); |
570 |
free_gnupg_table (); |
571 |
return 0; |
572 |
} |
573 |
} |
574 |
|
575 |
/* If we found another WinPT instance, just quit to avoid it |
576 |
will be executed twice. */ |
577 |
if (winpt_inst_found) { |
578 |
log_debug ("%s", "WinMain: WinPT is already running."); |
579 |
free_gnupg_table (); |
580 |
return 0; |
581 |
} |
582 |
|
583 |
wc.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_WINPT)); |
584 |
rc = RegisterClass (&wc); |
585 |
if (rc == FALSE) { |
586 |
msg_box (GetDesktopWindow (), _("Could not register window class"), |
587 |
_("WinPT Error"), MB_ERR); |
588 |
free_gnupg_table (); |
589 |
return 0; |
590 |
} |
591 |
|
592 |
hwnd = CreateWindow (PGM_NAME, |
593 |
PGM_NAME, |
594 |
0, 0, 0, 0, 0, |
595 |
NULL, |
596 |
NULL, |
597 |
hinst, |
598 |
NULL); |
599 |
if (hwnd == NULL) { |
600 |
msg_box (GetDesktopWindow (), |
601 |
_("Could not create window"), |
602 |
_("WinPT Error"), MB_ERR); |
603 |
free_gnupg_table (); |
604 |
return 0; |
605 |
} |
606 |
glob_hwnd = hwnd; |
607 |
UpdateWindow (hwnd); |
608 |
|
609 |
if (!first_start && !start_gpgprefs) { |
610 |
log_debug("backup gpg config file and check back-end"); |
611 |
gnupg_backup_options (); |
612 |
if (!check_crypto_engine ()) { |
613 |
DestroyWindow (hwnd); |
614 |
free_gnupg_table (); |
615 |
return 0; |
616 |
} |
617 |
} |
618 |
|
619 |
if (start_gpgprefs) { |
620 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
621 |
gpgprefs_dlg_proc, 0); |
622 |
if (check_for_empty_keyrings (true)) |
623 |
first_start = 1; /* The public keyring is empty. */ |
624 |
} |
625 |
|
626 |
if (first_start) { |
627 |
struct genkey_s c; |
628 |
int choice; |
629 |
HWND h; |
630 |
start: |
631 |
h = GetDesktopWindow (); |
632 |
if (!gpg_prefs_ok ()) |
633 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, h, |
634 |
gpgprefs_dlg_proc, 0); |
635 |
choice = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FIRST, h, |
636 |
first_run_dlg_proc, 0); |
637 |
switch (choice) { |
638 |
case SETUP_KEYGEN: |
639 |
c.interactive = 1; |
640 |
c.first_start = 1; |
641 |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD, |
642 |
h, keygen_wizard_dlg_proc, (LPARAM)&c); |
643 |
if (!rc) |
644 |
goto start; |
645 |
break; |
646 |
|
647 |
case SETUP_IMPORT: |
648 |
rc = gnupg_copy_keyrings (); |
649 |
if (rc) { |
650 |
msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR); |
651 |
goto start; |
652 |
} |
653 |
break; |
654 |
|
655 |
case SETUP_EXISTING: |
656 |
rc = gnupg_import_keypair (); |
657 |
if (rc) { |
658 |
msg_box (hwnd, winpt_strerror (rc), _("WinPT Error"), MB_ERR); |
659 |
goto start; |
660 |
} |
661 |
break; |
662 |
|
663 |
case SETUP_CARDGEN: |
664 |
rc = DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_CARD_KEYGEN, |
665 |
h, card_keygen_dlg_proc, 0); |
666 |
if (!rc) |
667 |
goto start; |
668 |
break; |
669 |
|
670 |
case 0: /* Cancel/Abort. */ |
671 |
default: |
672 |
DestroyWindow (hwnd); |
673 |
free_gnupg_table (); |
674 |
return 0; |
675 |
} |
676 |
update_keycache (hwnd); |
677 |
if (!check_crypto_engine ()) { |
678 |
DestroyWindow (hwnd); |
679 |
free_gnupg_table (); |
680 |
keycache_release (1); |
681 |
return 0; |
682 |
} |
683 |
} |
684 |
else { |
685 |
gpg_keycache_t c; |
686 |
if (update_keycache (hwnd)) { |
687 |
DestroyWindow (hwnd); |
688 |
free_gnupg_table (); |
689 |
keycache_release (1); |
690 |
return 0; |
691 |
} |
692 |
/* XXX: rewrite this part. */ |
693 |
c = keycache_get_ctx (1); |
694 |
if (!gpg_keycache_get_size (c)) { |
695 |
msg_box (hwnd, _("The keycache was not initialized or is empty.\n" |
696 |
"Please check your GPG config (keyrings, pathes...)"), |
697 |
_("WinPT Error"), MB_ERR); |
698 |
ec = msg_box (GetDesktopWindow (), |
699 |
_("It seems that GPG is not configured properly.\n" |
700 |
"Do you want to start the GPG preferences dialog?"), |
701 |
"WinPT", MB_INFO|MB_YESNO); |
702 |
if (ec == IDYES) { |
703 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
704 |
gpgprefs_dlg_proc, 0); |
705 |
update_keycache (hwnd); |
706 |
} |
707 |
else { |
708 |
DestroyWindow (hwnd); |
709 |
free_gnupg_table (); |
710 |
keycache_release (1); |
711 |
return 0; |
712 |
} |
713 |
} |
714 |
if (check_default_key ()) { |
715 |
char *p = get_gnupg_default_key (); |
716 |
log_box (_("WinPT Error"), MB_ERR, |
717 |
_("Default key (from the GPG config file) could not be found or is unuseable.\n" |
718 |
"The default key will be resetted and can be set later in the Key Manager again.\n\n" |
719 |
"%s: secret key not found."), p? p : "?"); |
720 |
set_gnupg_default_key (NULL); |
721 |
free_if_alloc (p); |
722 |
} |
723 |
if (count_insecure_elgkeys ()) |
724 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_ELGWARN, glob_hwnd, |
725 |
elgamal_warn_dlg_proc, 0); |
726 |
} |
727 |
|
728 |
if (start_manager) |
729 |
PostMessage (hwnd, WM_COMMAND, start_manager, 0); |
730 |
|
731 |
accel_tab = LoadAccelerators (glob_hinst, (LPCTSTR)IDR_WINPT_ACCELERATOR); |
732 |
keyring_check_last_access (); /* init */ |
733 |
while (GetMessage (&msg, hwnd, 0, 0)) { |
734 |
if (!TranslateAccelerator (msg.hwnd, accel_tab, &msg)) { |
735 |
TranslateMessage (&msg); |
736 |
DispatchMessage (&msg); |
737 |
} |
738 |
} |
739 |
|
740 |
return 0; |
741 |
} |