1 |
/* WinPT.cpp - Windows Privacy Tray (WinPT) |
/* WinPT.cpp - Windows Privacy Tray (WinPT) |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
40 |
#include "wptCardEdit.h" |
#include "wptCardEdit.h" |
41 |
#include "wptCrypto.h" |
#include "wptCrypto.h" |
42 |
|
|
|
#define MIN_GPG_VER "1.4.2" /* Minimal GPG version. */ |
|
|
#define MIN_GPGME_VER "1.2.0" /* Minimal GPGME version. */ |
|
|
|
|
|
|
|
43 |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
HINSTANCE glob_hinst; /* global instance for the dialogs */ |
44 |
HWND glob_hwnd; /* global window handle for the dialogs */ |
HWND glob_hwnd; /* global window handle for the dialogs */ |
45 |
HWND activ_hwnd; |
HWND activ_hwnd; |
|
LOCK mo_file; |
|
46 |
int scard_support = 0; |
int scard_support = 0; |
47 |
int debug = 0; |
int debug = 0; |
48 |
int mobile = 0; |
int mobile = 0; |
91 |
load_gettext (int prev_inst) |
load_gettext (int prev_inst) |
92 |
{ |
{ |
93 |
char *nls = NULL; |
char *nls = NULL; |
|
char *file = NULL; |
|
94 |
|
|
95 |
nls = get_gettext_lang (); |
nls = get_gettext_lang (); |
96 |
if (nls) { |
if (nls != NULL) { |
97 |
set_gettext_file ("winpt", nls); |
set_gettext_file ("winpt", nls); |
|
file = make_filename (nls, "winpt", "mo"); |
|
|
if (!file_exist_check (nls) && init_file_lock (&mo_file, file)) { |
|
|
if (!prev_inst) |
|
|
msg_box (NULL, _("Could not initizalize file lock.\n" |
|
|
"Native Language Support"), |
|
|
_("WinPT Error"), MB_ERR); |
|
|
} |
|
98 |
free_if_alloc (nls); |
free_if_alloc (nls); |
|
free_if_alloc (file); |
|
99 |
} |
} |
100 |
} |
} |
101 |
|
|
102 |
|
|
103 |
|
/* Load the GPG environment. On the first start, some |
104 |
|
checks are performed to find out in what state GPG is. |
105 |
|
Return value: 0 everything OK. |
106 |
|
>0 fatal error. |
107 |
|
-1 public keyring is empty or does not exist. */ |
108 |
|
static int |
109 |
|
load_gpg_env (void) |
110 |
|
{ |
111 |
|
SECURITY_ATTRIBUTES sec_attr; |
112 |
|
char *p; |
113 |
|
char *pkr; |
114 |
|
|
115 |
|
p = get_reg_entry_gpg4win ("gpg.exe"); |
116 |
|
if (!p) |
117 |
|
return (1); |
118 |
|
if (file_exist_check (p)) { |
119 |
|
free_if_alloc (p); |
120 |
|
return (1); |
121 |
|
} |
122 |
|
free_if_alloc (p); |
123 |
|
p = multi_gnupg_path (0); |
124 |
|
if (p && dir_exist_check (p)) { |
125 |
|
memset (&sec_attr, 0, sizeof (sec_attr)); |
126 |
|
sec_attr.nLength = sizeof (sec_attr); |
127 |
|
if (!CreateDirectory (p, &sec_attr)) { |
128 |
|
msg_box (NULL, _("Could not create GPG home directory"), |
129 |
|
_("WinPT Error"), MB_ERR); |
130 |
|
free_if_alloc (p); |
131 |
|
return (2); |
132 |
|
} |
133 |
|
} |
134 |
|
pkr = make_filename (p, "pubring", "gpg"); |
135 |
|
free_if_alloc (p); |
136 |
|
if (!pkr) |
137 |
|
return -1; |
138 |
|
if (get_file_size (pkr) == 0) { |
139 |
|
free_if_alloc (pkr); |
140 |
|
return -1; |
141 |
|
} |
142 |
|
return 0; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
/* 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 |
147 |
keyring. if not, bail out because encryption won't work properly then. */ |
keyring. if not, bail out because encryption won't work properly then. */ |
148 |
static int |
static int |
150 |
{ |
{ |
151 |
gpgme_key_t key; |
gpgme_key_t key; |
152 |
gpgme_error_t err = GPG_ERR_NO_ERROR; |
gpgme_error_t err = GPG_ERR_NO_ERROR; |
153 |
char * defkey; |
char *defkey; |
154 |
|
|
155 |
defkey = get_gnupg_default_key (); |
defkey = get_gnupg_default_key (); |
156 |
if (defkey) |
if (defkey) |
198 |
|
|
199 |
/* Check that the underlying crypto engine fullfills the minimal |
/* Check that the underlying crypto engine fullfills the minimal |
200 |
requirements so all commands work properly. */ |
requirements so all commands work properly. */ |
201 |
static int |
static bool |
202 |
check_crypto_engine (void) |
check_crypto_engine (void) |
203 |
{ |
{ |
204 |
int ma=1, mi=4, pa=2; /* GPG 1.4.2 */ |
int ma=0, mi=0, pa=0; |
205 |
int rc; |
int rc; |
206 |
|
|
207 |
rc = check_gnupg_engine (&ma, &mi, &pa); |
rc = check_gnupg_engine (NEED_GPG_VERSION, &ma, &mi, &pa); |
208 |
if (rc == -1) { |
if (rc == -1) { |
209 |
msg_box (NULL, _("Could not read GnuPG version."), |
msg_box (NULL, _("Could not read GnuPG version."), |
210 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
211 |
return rc; |
return false; |
212 |
} |
} |
213 |
else if (rc) { |
else if (rc) { |
214 |
log_box (_("WinPT Error"), MB_ERR, |
log_box (_("WinPT Error"), MB_ERR, |
215 |
_("Sorry, you need a newer GPG version.\n" |
_("Sorry, you need a newer GPG version.\n" |
216 |
"GPG version %d.%d.%d required GPG version "MIN_GPG_VER), |
"GPG version %d.%d.%d required GPG version "NEED_GPG_VERSION), |
217 |
ma, mi, pa); |
ma, mi, pa); |
218 |
return rc; |
return false; |
219 |
} |
} |
220 |
/* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */ |
/* We enable smartcard support for GPG: >= 2 or >= 1.4.3 */ |
221 |
if (ma > 1 || pa >= 3) |
if (ma > 1 || pa >= 3) |
222 |
scard_support = 1; |
scard_support = 1; |
223 |
|
|
224 |
gpgver[0] = ma; |
gpgver[0] = ma; |
225 |
gpgver[1] = mi; |
gpgver[1] = mi; |
226 |
gpgver[2] = pa; |
gpgver[2] = pa; |
227 |
return rc; |
return true; |
228 |
} |
} |
229 |
|
|
230 |
|
|
249 |
} |
} |
250 |
|
|
251 |
|
|
252 |
|
/* Check if both keyrings are empty. This indicates that |
253 |
|
WinPT should offer to generate a key pair. */ |
254 |
|
static bool |
255 |
|
check_for_empty_keyrings (bool pub_only) |
256 |
|
{ |
257 |
|
char *p; |
258 |
|
int n = 0; |
259 |
|
|
260 |
|
p = get_gnupg_keyring (1, 0); |
261 |
|
if (file_exist_check (p) == 0 && get_file_size (p) == 0) |
262 |
|
n++; |
263 |
|
free_if_alloc (p); |
264 |
|
if (pub_only) |
265 |
|
return n == 1? true : false; |
266 |
|
p = get_gnupg_keyring (0, 0); |
267 |
|
if (file_exist_check (p) == 0 && get_file_size (p) == 0) |
268 |
|
n++; |
269 |
|
free_if_alloc (p); |
270 |
|
return n==2? true : false; |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
/* Enable the mobility mode. */ |
/* Enable the mobility mode. */ |
275 |
static void |
static void |
276 |
enable_mobile_mode (void) |
enable_mobile_mode (void) |
340 |
return 0; |
return 0; |
341 |
} |
} |
342 |
*/ |
*/ |
343 |
|
|
344 |
if (gpg_md_selftest ()) { |
if (gpg_md_selftest ()) { |
345 |
msg_box (NULL, _("Cryptographic selftest failed."), |
msg_box (NULL, _("Cryptographic selftest failed."), |
346 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
347 |
return 0; |
return 0; |
348 |
} |
} |
349 |
|
|
350 |
s = gpgme_check_version (MIN_GPGME_VER); |
s = gpgme_check_version (NEED_GPGME_VERSION); |
351 |
if (!s || !*s) { |
if (!s || !*s) { |
352 |
msg_box (NULL, _("A newer GPGME version is needed; at least "MIN_GPGME_VER), |
msg_box (NULL, _("A newer GPGME version is needed; at least "NEED_GPGME_VERSION), |
353 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
354 |
return 0; |
return 0; |
355 |
} |
} |
387 |
gnupg_load_config (); |
gnupg_load_config (); |
388 |
} |
} |
389 |
|
|
390 |
|
if (is_gpg4win_installed ()) |
391 |
|
load_gpg_env (); /* XXX: check return code. */ |
392 |
|
|
393 |
rc = gnupg_check_homedir (); |
rc = gnupg_check_homedir (); |
394 |
if (rc) { |
if (rc) { |
395 |
log_box (_("WinPT Error"), MB_ERR, |
log_box (_("WinPT Error"), MB_ERR, |
454 |
return 0; |
return 0; |
455 |
} |
} |
456 |
} |
} |
457 |
|
if (check_for_empty_keyrings (false)) |
458 |
|
first_start = 1; |
459 |
|
|
460 |
if (!first_start) { |
if (!first_start) { |
461 |
rc = gpg_check_permissions (1); |
rc = gpg_check_permissions (1); |
542 |
|
|
543 |
if (!first_start && !start_gpgprefs) { |
if (!first_start && !start_gpgprefs) { |
544 |
gnupg_backup_options (); |
gnupg_backup_options (); |
545 |
rc = check_crypto_engine (); |
if (!check_crypto_engine ()) { |
|
if (rc) { |
|
546 |
DestroyWindow (hwnd); |
DestroyWindow (hwnd); |
547 |
free_gnupg_table (); |
free_gnupg_table (); |
548 |
return 0; |
return 0; |
550 |
} |
} |
551 |
|
|
552 |
if (start_gpgprefs) { |
if (start_gpgprefs) { |
|
char *ring; |
|
553 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_GPGPREFS, hwnd, |
554 |
gpgprefs_dlg_proc, 0); |
gpgprefs_dlg_proc, 0); |
555 |
ring = get_gnupg_keyring (0, !NO_STRICT); |
if (check_for_empty_keyrings (true)) |
556 |
if (gnupg_access_keyring (0) == -1 && get_file_size (ring) == 0) |
first_start = 1; /* The public keyring is empty! */ |
|
first_start = 1; /* The keyring is empty! */ |
|
|
free_if_alloc (ring); |
|
557 |
} |
} |
558 |
|
|
559 |
if (first_start) { |
if (first_start) { |
584 |
} |
} |
585 |
break; |
break; |
586 |
|
|
587 |
case -1: |
case -1: /* Cancel/Abort. */ |
588 |
DestroyWindow (hwnd); |
DestroyWindow (hwnd); |
589 |
free_gnupg_table (); |
free_gnupg_table (); |
590 |
return 0; |
return 0; |