1 |
/* wptGPG.cpp - GnuPG configuration |
/* wptGPG.cpp - GnuPG configuration |
2 |
* Copyright (C) 2001-2004 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
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 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
#include <config.h> |
22 |
#endif |
#endif |
23 |
|
|
24 |
#include <string.h> |
#include <windows.h> |
25 |
#include <string.h> |
#include <string.h> |
26 |
#include <stdio.h> |
#include <stdio.h> |
|
#include <windows.h> |
|
27 |
#include <shlobj.h> |
#include <shlobj.h> |
28 |
#include <ctype.h> |
#include <ctype.h> |
29 |
#include <io.h> |
#include <io.h> |
30 |
|
#include <time.h> |
31 |
|
|
32 |
#include "wptGPG.h" |
#include "wptGPG.h" |
33 |
#include "wptGPGCmds.h" |
#include "wptGpgCmds.h" |
34 |
#include "wptGPGOptSkel.h" |
#include "wptGPGOptSkel.h" |
35 |
#include "wptTypes.h" |
#include "wptTypes.h" |
36 |
#include "wptNLS.h" |
#include "wptNLS.h" |
39 |
#include "wptW32API.h" |
#include "wptW32API.h" |
40 |
#include "wptCrypto.h" |
#include "wptCrypto.h" |
41 |
|
|
42 |
#define GPG_CONF "gpg.conf" |
#define GPG_CONF "gpg.conf" |
43 |
|
#define GPG_REG_EXE "gpgProgram" /* registry name for the binary. */ |
44 |
|
#define GPG_REG_HOME "HomeDir" /* registry name of the home dir. */ |
45 |
|
|
46 |
struct gpg_watcher_s { |
struct gpg_watcher_s { |
47 |
FILETIME last_access; |
FILETIME last_access; |
62 |
|
|
63 |
|
|
64 |
/* Return the application data folder of the current user. */ |
/* Return the application data folder of the current user. */ |
65 |
static char* |
char* |
66 |
multi_gnupg_path (void) |
multi_gnupg_path (int strict) |
67 |
{ |
{ |
68 |
static char buf[256+64]; |
static char buf[256+64]; |
69 |
BOOL ec; |
BOOL ec; |
72 |
memset (buf, 0, sizeof (buf)); |
memset (buf, 0, sizeof (buf)); |
73 |
/* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */ |
/* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */ |
74 |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
75 |
if (ec != 1) |
if (ec != 1) { |
76 |
|
log_debug ("multi_gnupg_path: SHGetSpecialFolderPath() failed\r\n", |
77 |
|
(int)GetLastError ()); |
78 |
return NULL; |
return NULL; |
79 |
|
} |
80 |
strcat (buf, "\\gnupg"); |
strcat (buf, "\\gnupg"); |
81 |
if (access (buf, 00)) |
if (strict && access (buf, 00)) |
82 |
return NULL; |
return NULL; |
83 |
return m_strdup (buf); |
return m_strdup (buf); |
84 |
} |
} |
85 |
|
|
86 |
|
|
87 |
/* Return the full path of the GnuPG application. First the registry is scanned |
/* Return the full path to the GPG home directory. First the 'HomeDir' entry |
88 |
for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
from the registry is used. Then the default $APPDATA\gnupg path. */ |
|
*/ |
|
89 |
char* |
char* |
90 |
get_gnupg_path (void) |
get_gnupg_path (void) |
91 |
{ |
{ |
92 |
char *p = NULL, *path = NULL; |
char *path; |
93 |
|
|
94 |
p = get_reg_entry_gpg ("HomeDir"); |
path = get_reg_entry_gpg (GPG_REG_HOME); |
95 |
if (p) { |
if (path) { |
96 |
path = m_strdup (p); |
if (dir_exist_check (path) == 0) |
97 |
free_if_alloc (p); |
return path; |
98 |
return path; |
free_if_alloc (path); |
99 |
} |
} |
100 |
else |
path = multi_gnupg_path (1); |
101 |
return multi_gnupg_path (); |
return path; |
|
return m_strdup ("c:\\gnupg"); |
|
102 |
} |
} |
103 |
|
|
104 |
|
|
107 |
char* |
char* |
108 |
get_gnupg_cfgfile (void) |
get_gnupg_cfgfile (void) |
109 |
{ |
{ |
110 |
char *p = NULL, *optfile = NULL, *path = NULL; |
char *p = NULL; |
111 |
|
char *optfile = NULL; |
112 |
|
char *path = NULL; |
113 |
size_t nlen = 0; |
size_t nlen = 0; |
114 |
|
|
115 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
116 |
if (!path) |
if (!path) |
117 |
return NULL; |
return NULL; |
118 |
p = get_reg_entry_gpg ("OptFile"); |
p = get_reg_entry_gpg ("OptFile"); |
119 |
if (p && !strcmp (p, "")) { |
if (p) { |
120 |
nlen = strlen (path) + 64; |
nlen = strlen (p) + 4; |
|
optfile = new char[nlen + 1]; |
|
|
if (!optfile) |
|
|
BUG (0); |
|
|
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
|
|
} |
|
|
else if (p) { |
|
|
nlen = strlen( p ) + 4; |
|
121 |
optfile = new char[nlen + 1]; |
optfile = new char[nlen + 1]; |
122 |
if (!optfile) |
if (!optfile) |
123 |
BUG (NULL); |
BUG (NULL); |
149 |
if (!path) |
if (!path) |
150 |
return NULL; |
return NULL; |
151 |
keyring = make_filename (path, pub? "pubring" : "secring", "gpg"); |
keyring = make_filename (path, pub? "pubring" : "secring", "gpg"); |
152 |
if (!strict && !file_exist_check (keyring)) { |
if (strict && !file_exist_check (keyring)) { |
153 |
|
free_if_alloc (path); |
154 |
|
return keyring; |
155 |
|
} |
156 |
|
else if (!strict) { |
157 |
free_if_alloc (path); |
free_if_alloc (path); |
158 |
return keyring; |
return keyring; |
159 |
} |
} |
160 |
if (file_exist_check (keyring) || get_file_size (keyring) == 0) { |
if (file_exist_check (keyring) || pub && get_file_size (keyring) == 0) { |
161 |
free_if_alloc (keyring); |
free_if_alloc (keyring); |
162 |
optfile = make_filename (path, GPG_CONF, NULL); |
optfile = make_filename (path, GPG_CONF, NULL); |
163 |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
171 |
/* Return the full path (with the gpg exe name). First the registry is scanned |
/* Return the full path (with the gpg exe name). First the registry is scanned |
172 |
for the entry 'gpgProgram'. If it wasn't set, the default path is the |
for the entry 'gpgProgram'. If it wasn't set, the default path is the |
173 |
appended string 'gpg.exe' is used. */ |
appended string 'gpg.exe' is used. */ |
174 |
|
|
175 |
|
/* FIXME: Use gpgme's engine info here. */ |
176 |
char* |
char* |
177 |
get_gnupg_prog (void) |
get_gnupg_prog (void) |
178 |
{ |
{ |
179 |
char *p; |
char *p; |
180 |
char *pgm = NULL; |
char *pgm = NULL; |
|
size_t nlen = 0; |
|
181 |
|
|
182 |
p = get_reg_entry_gpg ("gpgProgram"); |
p = get_reg_entry_gpg (GPG_REG_EXE); |
183 |
if (!p) { |
if (!p) { |
184 |
char *path = get_gnupg_path (); |
char *path = get_gnupg_path (); |
185 |
if (!path) |
if (!path) |
201 |
static char * |
static char * |
202 |
default_key_from_cache (int *ret_no_useable) |
default_key_from_cache (int *ret_no_useable) |
203 |
{ |
{ |
204 |
const char * s; |
const char *s; |
205 |
char * keyid = NULL; |
char *keyid = NULL; |
206 |
gpgme_key_t key; |
gpgme_key_t key; |
207 |
gpg_keycache_t sec = keycache_get_ctx (0); |
gpg_keycache_t sec = keycache_get_ctx (0); |
208 |
|
|
217 |
break; |
break; |
218 |
} |
} |
219 |
} |
} |
220 |
if (!keyid) { |
if (!keyid) |
221 |
*ret_no_useable = 1; |
*ret_no_useable = 1; |
|
msg_box (NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
|
|
} |
|
222 |
return keyid; |
return keyid; |
223 |
} |
} |
224 |
|
|
225 |
|
|
226 |
|
/* Load the gpg.conf and search for some options |
227 |
|
and store the result in the global preference context. |
228 |
|
Return value: 0 on success. */ |
229 |
|
int |
230 |
|
gnupg_load_config (void) |
231 |
|
{ |
232 |
|
int rc; |
233 |
|
gpg_optfile_t opt; |
234 |
|
gpg_option_t o; |
235 |
|
char *conf = get_gnupg_cfgfile (); |
236 |
|
if (!conf) |
237 |
|
return -1; |
238 |
|
rc = parse_gpg_options (conf, &opt); |
239 |
|
if (rc) { |
240 |
|
free_if_alloc (conf); |
241 |
|
return -1; |
242 |
|
} |
243 |
|
o = find_option (opt, "ask-cert-level"); |
244 |
|
if (o) |
245 |
|
reg_prefs.gpg.ask_cert_level = 1; |
246 |
|
o = find_option (opt, "ask-cert-expire"); |
247 |
|
if (o) |
248 |
|
reg_prefs.gpg.ask_cert_expire = 1; |
249 |
|
release_gpg_options (opt); |
250 |
|
free_if_alloc (conf); |
251 |
|
return 0; |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
char* |
char* |
256 |
get_gnupg_default_key (void) |
get_gnupg_default_key (void) |
257 |
{ |
{ |
265 |
return default_key_from_cache (&no_usable); |
return default_key_from_cache (&no_usable); |
266 |
rc = parse_gpg_options (optfile, &opt); |
rc = parse_gpg_options (optfile, &opt); |
267 |
if (rc) { |
if (rc) { |
268 |
free_if_alloc( optfile ); |
free_if_alloc (optfile); |
269 |
return default_key_from_cache( &no_usable ); |
return default_key_from_cache (&no_usable); |
270 |
} |
} |
271 |
e = find_option( opt, "default-key" ); |
e = find_option( opt, "default-key" ); |
272 |
if ( e ) |
if ( e ) |
281 |
if( e ) |
if( e ) |
282 |
keyid = m_strdup( e->val ); |
keyid = m_strdup( e->val ); |
283 |
} |
} |
284 |
free_if_alloc( optfile ); |
free_if_alloc (optfile); |
285 |
release_gpg_options( opt ); |
release_gpg_options (opt); |
286 |
|
|
287 |
if( !keyid ) |
if (!keyid) |
288 |
keyid = default_key_from_cache( &no_usable ); |
keyid = default_key_from_cache (&no_usable); |
289 |
return keyid; |
return keyid; |
290 |
} /* get_gnupg_default_key */ |
} /* get_gnupg_default_key */ |
291 |
|
|
292 |
|
|
293 |
|
/* Check if GPG4WIN is available and if so, use the |
294 |
|
install path to figure out where the gpg.exe is. */ |
295 |
|
char* |
296 |
|
check_for_gpg4win (void) |
297 |
|
{ |
298 |
|
return get_reg_entry_gpg4win ("gpg.exe"); |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
/* Check if the gpg application (exe file) is available. */ |
/* Check if the gpg application (exe file) is available. */ |
303 |
int |
int |
304 |
check_gnupg_prog (void) |
check_gnupg_prog (void) |
305 |
{ |
{ |
306 |
char *pgm = NULL; |
char *gpgexe = NULL; |
307 |
int rc = 0; |
int rc = 0; |
308 |
|
|
309 |
pgm = get_gnupg_prog (); |
gpgexe = get_gnupg_prog (); |
310 |
if (!pgm) |
if (!gpgexe || file_exist_check (gpgexe)) { |
311 |
rc = WPTERR_GPG_EXEFILE; |
free_if_alloc (gpgexe); |
312 |
if (file_exist_check (pgm)) |
gpgexe = check_for_gpg4win (); |
313 |
rc = WPTERR_GPG_EXEFILE; |
if (!gpgexe || file_exist_check (gpgexe)) |
314 |
free_if_alloc (pgm); |
rc = WPTERR_GPG_EXEFILE; |
315 |
|
else |
316 |
|
set_reg_entry_gpg (GPG_REG_EXE, gpgexe); |
317 |
|
} |
318 |
|
free_if_alloc (gpgexe); |
319 |
return rc; |
return rc; |
320 |
} |
} |
321 |
|
|
322 |
|
|
323 |
static int |
static int |
324 |
parse_version_nr (const char * buf, int *major, int *minor, int *patch) |
parse_version_nr (const char *buf, int *major, int *minor, int *patch) |
325 |
{ |
{ |
326 |
char tmp[8]; |
char tmp[8]; |
327 |
int i; |
int i; |
337 |
tmp[i] = 0; buf++; |
tmp[i] = 0; buf++; |
338 |
*minor = atol (tmp); |
*minor = atol (tmp); |
339 |
i=0; |
i=0; |
340 |
while (buf && isdigit( *buf ) && i < 8) |
while (buf && isdigit (*buf) && i < 8) |
341 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
342 |
tmp[i] = 0; |
tmp[i] = 0; |
343 |
*patch = atol (tmp); |
*patch = atol (tmp); |
349 |
version given in @r_major.@r_minor.@r_patch. On success these |
version given in @r_major.@r_minor.@r_patch. On success these |
350 |
variables contain the GPG version which is installed. */ |
variables contain the GPG version which is installed. */ |
351 |
int |
int |
352 |
check_gnupg_engine (int *r_major, int *r_minor, int *r_patch) |
check_gnupg_engine (const char *need_gpg_ver, |
353 |
|
int *r_major, int *r_minor, int *r_patch) |
354 |
{ |
{ |
355 |
gpgme_ctx_t ctx; |
gpgme_ctx_t ctx; |
356 |
gpgme_engine_info_t inf; |
gpgme_engine_info_t inf; |
357 |
char * eng = NULL; |
char *eng = NULL; |
358 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
359 |
int rc; |
int need_major = 0, need_minor = 0, need_patch = 0; |
360 |
|
int rc = 1; |
361 |
|
|
362 |
|
/* Convert the needed GPG version to the integer format. */ |
363 |
|
if (parse_version_nr (need_gpg_ver, |
364 |
|
&need_major, &need_minor, &need_patch)) |
365 |
|
return 1; |
366 |
|
|
367 |
gpgme_new (&ctx); |
gpgme_new (&ctx); |
368 |
inf = gpgme_ctx_get_engine_info (ctx); |
inf = gpgme_ctx_get_engine_info (ctx); |
369 |
if (!inf) { |
if (!inf) { |
370 |
gpgme_release (ctx); |
gpgme_release (ctx); |
371 |
return -1; |
return -1; |
372 |
} |
} |
373 |
|
|
374 |
/* We need to exec GPG again to find out if IDEA is available. */ |
/* We need to exec GPG again to find out if IDEA is available. */ |
375 |
if (gpg_get_version (&eng)) |
if (gpg_get_version (&eng)) |
376 |
return -1; |
return -1; |
377 |
if (strstr (eng, "IDEA")) |
if (strstr (eng, "IDEA")) |
378 |
idea_available = 1; |
idea_available = 1; |
379 |
free (eng); |
free (eng); |
380 |
rc = parse_version_nr( inf->version, &major, &minor, &patch ); |
if (parse_version_nr (inf->version, &major, &minor, &patch)) { |
|
if( rc ) { |
|
381 |
gpgme_release (ctx); |
gpgme_release (ctx); |
382 |
return rc; |
return 1; |
383 |
} |
} |
384 |
if (major < *r_major || minor < *r_minor) |
|
385 |
rc = 1; |
if (major > need_major) |
|
else { |
|
|
if (patch < *r_patch) |
|
|
rc = 1; |
|
386 |
rc = 0; |
rc = 0; |
387 |
} |
else if (major == need_major && minor > need_minor) |
388 |
|
rc = 0; |
389 |
|
else if (major == need_major && minor == need_minor && |
390 |
|
patch >= need_patch) |
391 |
|
rc = 0; |
392 |
|
|
393 |
|
/* Return the current GPG version. */ |
394 |
*r_major = major; |
*r_major = major; |
395 |
*r_minor = minor; |
*r_minor = minor; |
396 |
*r_patch = patch; |
*r_patch = patch; |
426 |
} /* check_gnupg_cfgfile */ |
} /* check_gnupg_cfgfile */ |
427 |
|
|
428 |
|
|
429 |
/* |
/* Usually GPG creates the pubring.gpg, secring.gpg on |
430 |
* Check if both keyrings are located in the gnupg home directory. |
the first start, but to make sure they always exist |
431 |
*/ |
create them empty if needed. */ |
432 |
|
static void |
433 |
|
create_empty_keyring (int _pub) |
434 |
|
{ |
435 |
|
char *name; |
436 |
|
FILE *f; |
437 |
|
|
438 |
|
name = get_gnupg_keyring (_pub, 0); |
439 |
|
if (file_exist_check (name) != 0) { |
440 |
|
f = fopen (name, "ab"); |
441 |
|
if (f != NULL) |
442 |
|
fclose (f); |
443 |
|
} |
444 |
|
free_if_alloc (name); |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
|
/* Check if both keyrings are located in the gnupg home directory. */ |
449 |
int |
int |
450 |
gnupg_access_files (void) |
gnupg_access_files (void) |
451 |
{ |
{ |
454 |
int secrings = 0, pubrings = 0; |
int secrings = 0, pubrings = 0; |
455 |
char *optfile; |
char *optfile; |
456 |
|
|
457 |
|
create_empty_keyring (1); |
458 |
if (gnupg_access_keyring (1)) |
if (gnupg_access_keyring (1)) |
459 |
rc = WPTERR_GPG_KEYRINGS; |
rc = WPTERR_GPG_KEYRINGS; |
460 |
else |
else |
461 |
pubring_ok = 1; |
pubring_ok = 1; |
462 |
|
|
463 |
|
create_empty_keyring (0); |
464 |
if (gnupg_access_keyring (0)) |
if (gnupg_access_keyring (0)) |
465 |
rc = WPTERR_GPG_KEYRINGS; |
rc = WPTERR_GPG_KEYRINGS; |
466 |
else |
else |
467 |
secring_ok = 1; |
secring_ok = 1; |
468 |
|
|
469 |
if (!pubring_ok || !secring_ok) { |
if (!pubring_ok || !secring_ok) { |
470 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
471 |
if (!optfile) |
if (!optfile) |
472 |
return WPTERR_GPG_KEYRINGS; |
return WPTERR_GPG_KEYRINGS; |
473 |
rc = file_exist_check (optfile); |
rc = file_exist_check (optfile); |
474 |
if (!rc && get_file_size(optfile) > 0) { |
if (!rc && get_file_size (optfile) > 0) { |
475 |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
476 |
if (!rc && secrings && pubrings) { |
if (!rc && secrings && pubrings) { |
477 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
489 |
rc = WPTERR_GPG_KEYRINGS; |
rc = WPTERR_GPG_KEYRINGS; |
490 |
} |
} |
491 |
return rc; |
return rc; |
492 |
} /* gnupg_access_files */ |
} |
493 |
|
|
494 |
|
|
495 |
static int |
static int |
498 |
FILE *fp; |
FILE *fp; |
499 |
char *s, *optfile; |
char *s, *optfile; |
500 |
|
|
501 |
s = get_gnupg_path( ); |
s = get_gnupg_path (); |
502 |
if( s == NULL ) |
if( s == NULL ) |
503 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
504 |
optfile = make_filename( s, GPG_CONF, NULL ); |
optfile = make_filename (s, GPG_CONF, NULL); |
505 |
fp = fopen( optfile, "wb" ); |
fp = fopen (optfile, "wb"); |
506 |
if( fp == NULL ) { |
if (fp == NULL) { |
507 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
508 |
goto fail; |
goto fail; |
509 |
} |
} |
510 |
fwrite( options_skel, 1, strlen( options_skel ), fp ); |
fwrite (options_skel, 1, strlen (options_skel), fp); |
511 |
fclose( fp ); |
fclose (fp); |
512 |
|
|
513 |
fail: |
fail: |
514 |
free_if_alloc( s ); |
free_if_alloc (s); |
515 |
free_if_alloc( optfile ); |
free_if_alloc (optfile); |
516 |
return 0; |
return 0; |
517 |
} /* create_gpg_options */ |
} /* create_gpg_options */ |
518 |
|
|
557 |
} /* get_gnupg_config */ |
} /* get_gnupg_config */ |
558 |
|
|
559 |
|
|
560 |
|
/* Set the default key in the gpg.conf. |
561 |
|
If @key is NULL, the entry will be deleted. */ |
562 |
int |
int |
563 |
set_gnupg_default_key (const char * key) |
set_gnupg_default_key (const char *key) |
564 |
{ |
{ |
565 |
gpg_optfile_t opt; |
gpg_optfile_t opt; |
566 |
gpg_option_t e; |
gpg_option_t e; |
569 |
|
|
570 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
571 |
if (!optfile) |
if (!optfile) |
572 |
return -1; |
return WPTERR_FILE_OPEN; |
573 |
rc = parse_gpg_options (optfile, &opt); |
rc = parse_gpg_options (optfile, &opt); |
574 |
if( rc ) { |
if (rc) { |
575 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
576 |
return -1; |
return WPTERR_GENERAL; |
577 |
} |
} |
578 |
e = find_option (opt, "default-key"); |
e = find_option (opt, "default-key"); |
579 |
if (e) { |
if (e && !key) |
580 |
|
e->used = 0; |
581 |
|
else if (e) { |
582 |
free_if_alloc (e->val); |
free_if_alloc (e->val); |
583 |
e->val = m_strdup (key); |
e->val = m_strdup (key); |
584 |
e->used = 1; |
e->used = 1; |
591 |
release_gpg_options (opt); |
release_gpg_options (opt); |
592 |
|
|
593 |
return rc; |
return rc; |
594 |
} /* set_gnupg_default_key */ |
} |
595 |
|
|
596 |
|
|
597 |
/* |
/* |
696 |
if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime && |
if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime && |
697 |
ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime) |
ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime) |
698 |
ctx->modified = 1; |
ctx->modified = 1; |
699 |
|
|
700 |
|
/* XXX: find a better way. without it, winpt --keymanager loads |
701 |
|
the key cache twice. */ |
702 |
|
if (ctx->last_access.dwLowDateTime == 0) |
703 |
|
ctx->modified = 0; |
704 |
|
|
705 |
ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime; |
ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime; |
706 |
ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime; |
ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime; |
807 |
|
|
808 |
/* XXX: does not work with write-protected floppies */ |
/* XXX: does not work with write-protected floppies */ |
809 |
static int |
static int |
810 |
my_access (const char * fname) |
my_access (const char *fname) |
811 |
{ |
{ |
812 |
HANDLE hd; |
HANDLE hd; |
813 |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, |
819 |
} |
} |
820 |
|
|
821 |
|
|
822 |
|
/* Check the file permissions of the public keyring. |
823 |
|
If @showmsg is 1 output a message in case of errors. |
824 |
|
Return value: 1 if read-only attribute |
825 |
|
2 if file is opened by another process exclusively. */ |
826 |
int |
int |
827 |
gpg_check_permissions (int showmsg) |
gpg_check_permissions (int showmsg) |
828 |
{ |
{ |
829 |
char * p, * name = NULL; |
char *p = NULL; |
830 |
|
char *name = NULL; |
831 |
int failed = 0, ans=0, attrs=0; |
int failed = 0, ans=0, attrs=0; |
832 |
|
|
833 |
p = get_gnupg_path (); |
p = get_gnupg_path (); |
834 |
check_keyring (&p); |
if (check_keyring (&p) && p) { |
|
if (p) { |
|
835 |
name = make_filename (p, "pubring", "gpg"); |
name = make_filename (p, "pubring", "gpg"); |
|
free_if_alloc (p); |
|
836 |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
837 |
ans = msg_box (NULL, |
ans = msg_box (NULL, |
838 |
_("The selected keyring has the read-only file\n" |
_("The selected keyring has the read-only file\n" |
848 |
} |
} |
849 |
} |
} |
850 |
else if (ans == IDNO) { |
else if (ans == IDNO) { |
851 |
/* |
/* All commands with write access will be disabled. */ |
|
msg_box (NULL, _("All commands with write access to the keyring\n" |
|
|
"will be disabled."), _("GPG Information"), MB_INFO); |
|
|
*/ |
|
852 |
failed = 1; |
failed = 1; |
853 |
} |
} |
854 |
} |
} |
864 |
failed = 2; |
failed = 2; |
865 |
} |
} |
866 |
} |
} |
867 |
|
free_if_alloc (p); |
868 |
free_if_alloc (name); |
free_if_alloc (name); |
869 |
return failed; |
return failed; |
870 |
} /* gpg_check_permissions */ |
} |
871 |
|
|
872 |
|
|
873 |
/* Check the GPG home dir. If all methods failed, try to |
/* Check the GPG home dir. First try to read the 'HomeDir' registry entry, |
874 |
create the default folder. */ |
then check for $APPDATA\gnupg. Create the dir if it does not exists. */ |
875 |
static int |
int |
876 |
check_homedir (void) |
gnupg_check_homedir (void) |
877 |
{ |
{ |
878 |
char *homedir = NULL; |
char *homedir = NULL; |
879 |
int yes = 0; |
int val = 0; |
880 |
|
int rc = 0; |
881 |
|
|
882 |
homedir = get_reg_entry_gpg ("HomeDir"); |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
|
if (!homedir) |
|
|
homedir = multi_gnupg_path (); |
|
883 |
if (!homedir) |
if (!homedir) |
884 |
homedir = m_strdup ("c:\\gnupg"); |
homedir = multi_gnupg_path (0); |
885 |
if (homedir) { |
if (homedir) { |
886 |
if (GetFileAttributes (homedir) == 0xFFFFFFFF) { |
if (GetFileAttributes (homedir) == 0xFFFFFFFF) { |
887 |
yes = log_box (_("Preferences"), MB_YESNO, |
val = log_box (_("Preferences"), MB_YESNO, |
888 |
_("%s does not exit.\n" |
_("%s does not exit.\n" |
889 |
"Do you want to create this directory?"), homedir); |
"Do you want to create this directory?"), homedir); |
890 |
if (yes == IDYES) { |
if (val == IDYES) { |
891 |
BOOL ec = CreateDirectory (homedir, NULL); |
if (!CreateDirectory (homedir, NULL)) |
892 |
free_if_alloc (homedir); |
rc = WPTERR_DIR_CREAT; |
|
if (ec == FALSE) |
|
|
return WPTERR_DIR_CREAT; |
|
|
return 0; |
|
893 |
} |
} |
894 |
return WPTERR_DIR_OPEN; |
else |
895 |
} |
rc = WPTERR_DIR_OPEN; |
|
free_if_alloc (homedir); |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
int |
|
|
gnupg_check_homedir (void) |
|
|
{ |
|
|
char *homedir = NULL; |
|
|
char *prog = NULL; |
|
|
int rc = 0, ec = 0; |
|
|
|
|
|
rc = check_homedir (); |
|
|
if (rc) |
|
|
return rc; |
|
|
if ((homedir = get_reg_entry_gpg ("HomeDir")) && |
|
|
!(prog = get_reg_entry_gpg ("gpgProgram" ))) { |
|
|
prog = make_filename (homedir, "gpg", "exe"); |
|
|
if (file_exist_check (prog) == 0) { |
|
|
rc = set_reg_entry_gpg ("gpgProgram", prog); |
|
|
if (rc) |
|
|
goto fail; |
|
896 |
} |
} |
897 |
free_if_alloc (homedir); |
free_if_alloc (homedir); |
|
free_if_alloc (prog); |
|
|
return rc; |
|
898 |
} |
} |
|
if ((prog = get_reg_entry_gpg ("gpgProgram")) |
|
|
&& file_exist_check (prog)) { |
|
|
free_if_alloc (prog); |
|
|
homedir = get_reg_entry_gpg ("HomeDir"); |
|
|
if (!homedir) { |
|
|
rc = WPTERR_GENERAL; |
|
|
goto fail; |
|
|
} |
|
|
prog = make_filename (homedir, "gpg", "exe"); |
|
|
if (file_exist_check (prog) == 0) { |
|
|
rc = set_reg_entry_gpg ("gpgProgram", prog); |
|
|
if (rc) |
|
|
goto fail; |
|
|
free_if_alloc (prog); |
|
|
return rc; |
|
|
} |
|
|
} |
|
|
|
|
|
/* Change the return code if homedir doesn't exist or if the program |
|
|
doesn't exist. Note that exist_checks return 0 to suggest existance. */ |
|
|
if ((!homedir || dir_exist_check (homedir))) |
|
|
rc = WPTERR_GENERAL; |
|
|
|
|
|
fail: |
|
|
free_if_alloc (homedir); |
|
|
free_if_alloc (prog); |
|
899 |
return rc; |
return rc; |
900 |
} /* gnupg_check_homedir */ |
} |
901 |
|
|
902 |
|
|
903 |
int |
int |
913 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
914 |
hwnd = GetDesktopWindow (); |
hwnd = GetDesktopWindow (); |
915 |
|
|
916 |
pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"), |
pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"), |
917 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL); |
"GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL); |
918 |
if (!pring) { |
if (!pring) { |
919 |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
920 |
free_if_alloc (path); |
free_if_alloc (path); |
933 |
} |
} |
934 |
free_if_alloc (file); |
free_if_alloc (file); |
935 |
|
|
936 |
sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"), |
sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"), |
937 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL); |
"GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL); |
938 |
if (!sring) { |
if (!sring) { |
939 |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
940 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
957 |
} /* gnupg_import_keyrings */ |
} /* gnupg_import_keyrings */ |
958 |
|
|
959 |
|
|
960 |
|
/* Backup the gpg.conf file. */ |
961 |
void |
void |
962 |
gnupg_backup_options (void) |
gnupg_backup_options (void) |
963 |
{ |
{ |
965 |
char bak[512]; |
char bak[512]; |
966 |
|
|
967 |
cfgfile = get_gnupg_cfgfile (); |
cfgfile = get_gnupg_cfgfile (); |
968 |
if (cfgfile == NULL) |
if (!cfgfile) |
969 |
return; |
return; |
970 |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
971 |
CopyFile (cfgfile, bak, FALSE); |
CopyFile (cfgfile, bak, FALSE); |
972 |
free_if_alloc (cfgfile); |
free_if_alloc (cfgfile); |
973 |
} /* gnupg_backup_options */ |
} |
|
|
|
974 |
|
|
975 |
|
|
976 |
static int |
static int |
998 |
} /* backup_one_file */ |
} /* backup_one_file */ |
999 |
|
|
1000 |
|
|
1001 |
|
/* Figure out first public keyring which is not empty. |
1002 |
|
Return value: 1 on success. */ |
1003 |
static int |
static int |
1004 |
check_keyring (char ** r_path) |
check_keyring (char **r_path) |
1005 |
{ |
{ |
1006 |
char * p; |
char *p; |
1007 |
char * opt, * name; |
char *opt; |
1008 |
|
char *name; |
1009 |
|
|
1010 |
if (!*r_path) |
if (!*r_path) |
1011 |
return 0; |
return 0; |
1012 |
p = make_filename (*r_path, "pubring", "gpg"); |
p = make_filename (*r_path, "pubring", "gpg"); |
1013 |
if (!p || get_file_size (p) > 0) |
if (!p || get_file_size (p) <= 0) |
1014 |
return 0; |
return 0; |
1015 |
|
|
1016 |
opt = get_gnupg_cfgfile (); |
opt = get_gnupg_cfgfile (); |
1022 |
if (!name) |
if (!name) |
1023 |
return 0; |
return 0; |
1024 |
p = strrchr (name, '\\'); |
p = strrchr (name, '\\'); |
1025 |
if (!p) |
if (!p) { |
|
{ |
|
1026 |
free_if_alloc (name); |
free_if_alloc (name); |
1027 |
return 0; |
return 0; |
1028 |
} |
} |
1035 |
} |
} |
1036 |
|
|
1037 |
|
|
1038 |
|
/* Return a temp name based on the day of the week. */ |
1039 |
static char* |
static char* |
1040 |
get_backup_name (const char *templ) |
get_backup_name (const char *templ) |
1041 |
{ |
{ |
1042 |
struct tm *tm; |
struct tm *tm; |
1043 |
char *p; |
char *p; |
1044 |
|
time_t t; |
1045 |
|
|
1046 |
time_t t = time (NULL); |
t = time (NULL); |
1047 |
tm = localtime (&t); |
tm = localtime (&t); |
1048 |
p = new char [strlen (templ) + 8 + 1]; |
p = new char [strlen (templ) + 8 + 1]; |
1049 |
if (!p) |
if (!p) |
1053 |
} |
} |
1054 |
|
|
1055 |
|
|
1056 |
|
/* Make backups of all keyrings. The public key ring is |
1057 |
|
rotated like this pubring-%d.gpg. */ |
1058 |
void |
void |
1059 |
gnupg_backup_keyrings (void) |
gnupg_backup_keyrings (void) |
1060 |
{ |
{ |
1065 |
if (!reg_prefs.auto_backup) |
if (!reg_prefs.auto_backup) |
1066 |
return; |
return; |
1067 |
bakmode = reg_prefs.backup.mode; |
bakmode = reg_prefs.backup.mode; |
1068 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1069 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1070 |
if (bakmode == 1) { |
if (bakmode == 1) { |
1071 |
dstpath = get_gnupg_path (); |
dstpath = multi_gnupg_path (1); |
1072 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1073 |
} |
} |
1074 |
else if (bakmode == 2) { |
else if (bakmode == 2) { |
1075 |
char * tmpfile; |
char *tmpfile; |
1076 |
FILE * fp; |
FILE *fp; |
1077 |
|
|
1078 |
dstpath = m_strdup (reg_prefs.backup.path); |
dstpath = m_strdup (reg_prefs.backup.path); |
1079 |
if (!dstpath) |
if (!dstpath) |
1087 |
else { |
else { |
1088 |
rc = 0; |
rc = 0; |
1089 |
fclose (fp); |
fclose (fp); |
1090 |
unlink (tmpfile); |
remove (tmpfile); |
1091 |
} |
} |
1092 |
free_if_alloc (tmpfile); |
free_if_alloc (tmpfile); |
1093 |
if (!fp || rc == IDCANCEL) |
if (!fp || rc == IDCANCEL) |
1104 |
free_if_alloc (name); |
free_if_alloc (name); |
1105 |
free_if_alloc (srcpath); |
free_if_alloc (srcpath); |
1106 |
free_if_alloc (dstpath); |
free_if_alloc (dstpath); |
1107 |
} /* gnupg_backup_keyrings */ |
} |
1108 |
|
|
1109 |
|
|
1110 |
/* Display GPG error from file if possible. */ |
/* Display GPG error from file if possible. */ |
1111 |
void |
void |
1112 |
gnupg_display_error (void) |
gnupg_display_error (void) |
1113 |
{ |
{ |
1114 |
char tmpath[512], * errstr; |
char tmpath[512], *errstr; |
1115 |
size_t size = 0; |
size_t size = 0; |
1116 |
FILE * fp; |
FILE *fp; |
1117 |
|
|
1118 |
GetTempPath (sizeof tmpath - 32, (tmpath)); |
get_temp_name (tmpath, sizeof (tmpath), "gpg_stderr"); |
|
strcat (tmpath, "gpg_stderr"); |
|
1119 |
size = get_file_size (tmpath); |
size = get_file_size (tmpath); |
1120 |
if (file_exist_check (tmpath) || size <= 0) |
if (file_exist_check (tmpath) || size <= 0) |
1121 |
return; |
return; |
1122 |
fp = fopen( tmpath, "rb" ); |
fp = fopen( tmpath, "rb" ); |
1123 |
if (!fp) { |
if (!fp) { |
1124 |
msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO ); |
msg_box (NULL, _("No GPG error description available."), |
1125 |
|
_("GPG Error"), MB_INFO); |
1126 |
return; |
return; |
1127 |
} |
} |
1128 |
errstr = new char[size+1]; |
errstr = new char[size+1]; |