1 |
/* wptGPG.cpp - GnuPG configuration |
/* wptGPG.cpp - GnuPG configuration |
2 |
* Copyright (C) 2001-2006 Timo Schulz |
* Copyright (C) 2001-2009 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* General Public License for more details. |
* General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
15 |
*/ |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
26 |
#include <time.h> |
#include <time.h> |
27 |
|
|
28 |
#include "wptGPG.h" |
#include "wptGPG.h" |
29 |
|
#include "wptGPGME.h" |
30 |
#include "wptGpgCmds.h" |
#include "wptGpgCmds.h" |
|
#include "wptGPGOptSkel.h" |
|
31 |
#include "wptTypes.h" |
#include "wptTypes.h" |
32 |
#include "wptNLS.h" |
#include "wptNLS.h" |
33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
63 |
char* |
char* |
64 |
multi_gnupg_path (int strict) |
multi_gnupg_path (int strict) |
65 |
{ |
{ |
66 |
static char buf[256+64]; |
static char buf[MAX_PATH+64]; |
67 |
BOOL ec; |
BOOL ec; |
68 |
|
|
69 |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
102 |
char* |
char* |
103 |
get_gnupg_cfgfile (void) |
get_gnupg_cfgfile (void) |
104 |
{ |
{ |
105 |
char *optfile = NULL; |
char *optfile; |
106 |
char *path; |
char *path; |
|
size_t nlen; |
|
107 |
|
|
108 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
109 |
if (!path) |
if (!path) |
110 |
return NULL; |
return NULL; |
111 |
nlen = strlen (path) + 64; |
optfile = make_filename (path, GPG_CONF, NULL); |
|
optfile = new char[nlen + 1]; |
|
|
if (!optfile) |
|
|
BUG (NULL); |
|
|
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
|
|
|
|
112 |
free_if_alloc (path); |
free_if_alloc (path); |
113 |
|
|
114 |
return optfile; |
return optfile; |
115 |
} |
} |
116 |
|
|
117 |
|
|
118 |
|
static char* |
119 |
|
get_keyring_from_conf (const char *fname, int pub) |
120 |
|
{ |
121 |
|
config_file_t opt; |
122 |
|
conf_option_t e; |
123 |
|
char *kring = NULL; |
124 |
|
int rc; |
125 |
|
|
126 |
|
rc = parse_config (fname, &opt); |
127 |
|
if (rc) |
128 |
|
return NULL; |
129 |
|
if (pub) |
130 |
|
e = conf_find_option (opt, "keyring"); |
131 |
|
else |
132 |
|
e = conf_find_option (opt, "secret-keyring"); |
133 |
|
if (e != NULL) |
134 |
|
kring = m_strdup (e->val); |
135 |
|
release_config (opt); |
136 |
|
|
137 |
|
return kring; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
/* Return the full path of the keyring. If @pub is 1, the public |
/* Return the full path of the keyring. If @pub is 1, the public |
142 |
keyring is return, otherwise the secret keyring. */ |
keyring is return, otherwise the secret keyring. */ |
143 |
char* |
char* |
144 |
get_gnupg_keyring (int pub, int strict) |
get_gnupg_keyring (int pub, int strict) |
145 |
{ |
{ |
146 |
char *optfile = NULL; |
char *optfile; |
147 |
char *path = NULL; |
char *path; |
148 |
char *keyring = NULL; |
char *keyring; |
149 |
|
|
150 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
151 |
if (!path) |
if (!path) |
159 |
free_if_alloc (path); |
free_if_alloc (path); |
160 |
return keyring; |
return keyring; |
161 |
} |
} |
162 |
if (file_exist_check (keyring) || pub && get_file_size (keyring) == 0) { |
if (file_exist_check (keyring) || |
163 |
|
(pub && get_file_size (keyring) == 0)) { |
164 |
free_if_alloc (keyring); |
free_if_alloc (keyring); |
165 |
optfile = make_filename (path, GPG_CONF, NULL); |
optfile = make_filename (path, GPG_CONF, NULL); |
166 |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
keyring = get_keyring_from_conf (optfile, pub); |
167 |
|
free_if_alloc (optfile); |
168 |
} |
} |
169 |
free_if_alloc (path); |
free_if_alloc (path); |
170 |
free_if_alloc (optfile); |
|
171 |
return keyring; |
return keyring; |
172 |
} |
} |
173 |
|
|
227 |
int |
int |
228 |
gnupg_load_config (void) |
gnupg_load_config (void) |
229 |
{ |
{ |
230 |
gpg_optfile_t opt; |
config_file_t opt; |
231 |
char *conf; |
char *conf; |
232 |
|
int rc = 0; |
233 |
|
|
234 |
conf = get_gnupg_cfgfile (); |
conf = get_gnupg_cfgfile (); |
235 |
if (!conf) |
if (!conf) |
238 |
free_if_alloc (conf); |
free_if_alloc (conf); |
239 |
return -1; |
return -1; |
240 |
} |
} |
241 |
if (find_option (opt, "ask-cert-level")) |
free_if_alloc (conf); |
242 |
|
if (conf_find_option (opt, "ask-cert-level")) |
243 |
reg_prefs.gpg.ask_cert_level = 1; |
reg_prefs.gpg.ask_cert_level = 1; |
244 |
if (find_option (opt, "ask-cert-expire")) |
if (conf_find_option (opt, "ask-cert-expire")) |
245 |
reg_prefs.gpg.ask_cert_expire = 1; |
reg_prefs.gpg.ask_cert_expire = 1; |
246 |
|
/* The 'textmode' option for GPG is useful for text files |
247 |
|
but it breaks the output of any binary data. Thus we return |
248 |
|
a warning here to inform the user that binary output is broken. */ |
249 |
|
if (conf_find_option (opt, "textmode")) |
250 |
|
rc = -2; |
251 |
release_config (opt); |
release_config (opt); |
252 |
free_if_alloc (conf); |
|
253 |
return 0; |
return rc; |
254 |
} |
} |
255 |
|
|
256 |
|
|
260 |
{ |
{ |
261 |
size_t len = strlen (val); |
size_t len = strlen (val); |
262 |
|
|
263 |
if (len > 1 && val[len-1] == '!') { |
if (len > 1 && val[len-1] == '!') |
264 |
char *p = new char[len+1]; |
return substr (val, 0, len-1); |
|
if (!p) |
|
|
BUG (0); |
|
|
memset (p, 0, len+1); |
|
|
memcpy (p, val, len-1); |
|
|
return p; |
|
|
} |
|
265 |
return m_strdup (val); |
return m_strdup (val); |
266 |
} |
} |
267 |
|
|
268 |
|
|
269 |
|
/* Return the default key. |
270 |
|
This can be either a substring or a key ID. */ |
271 |
char* |
char* |
272 |
get_gnupg_default_key (void) |
get_gnupg_default_key (void) |
273 |
{ |
{ |
274 |
gpg_optfile_t opt = NULL; |
config_file_t opt = NULL; |
275 |
gpg_option_t e; |
conf_option_t e; |
276 |
char *keyid = NULL, *optfile = NULL; |
char *keyid = NULL, *optfile = NULL; |
277 |
int no_usable=0; |
int no_usable=0; |
278 |
|
|
283 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
284 |
return default_key_from_cache (&no_usable); |
return default_key_from_cache (&no_usable); |
285 |
} |
} |
286 |
e = find_option (opt, "default-key"); |
/* First we search for config entries which specify a default key. */ |
287 |
|
e = conf_find_option (opt, "default-key"); |
288 |
if (!e) |
if (!e) |
289 |
e = find_option (opt, "local-user"); |
e = conf_find_option (opt, "local-user"); |
290 |
if (e) |
if (e) |
291 |
keyid = extract_keyid (e->val); |
keyid = extract_keyid (e->val); |
292 |
|
|
293 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
294 |
release_config (opt); |
release_config (opt); |
295 |
|
|
296 |
|
/* If no entry in the config has been found, we get a key |
297 |
|
from the key cache. */ |
298 |
if (!keyid) |
if (!keyid) |
299 |
keyid = default_key_from_cache (&no_usable); |
keyid = default_key_from_cache (&no_usable); |
300 |
return keyid; |
return keyid; |
333 |
|
|
334 |
static int |
static int |
335 |
parse_version_nr (const char *buf, int *major, int *minor, int *patch) |
parse_version_nr (const char *buf, int *major, int *minor, int *patch) |
336 |
{ |
{ |
337 |
char tmp[8]; |
char *p; |
338 |
int i; |
char *tmp = m_strdup(buf); |
339 |
|
|
340 |
i=0; |
int pos=0; |
341 |
while (buf && *buf != '.' && i < 8) |
while ((p = strsep(&tmp, ".")) != NULL) { |
342 |
tmp[i++] = *buf++; |
switch (++pos) { |
343 |
tmp[i] = 0; buf++; |
case 1: *major = atoi (p); break; |
344 |
*major = atoi (tmp); |
case 2: *minor = atoi (p); break; |
345 |
i=0; |
case 3: *patch = atoi (p); break; |
346 |
while (buf && *buf != '.' && i < 8) |
} |
347 |
tmp[i++] = *buf++; |
} |
348 |
tmp[i] = 0; buf++; |
delete[] tmp; |
349 |
*minor = atoi (tmp); |
if (pos != 3) |
350 |
i=0; |
return -1; |
|
while (buf && isdigit (*buf) && i < 8) |
|
|
tmp[i++] = *buf++; |
|
|
tmp[i] = 0; |
|
|
*patch = atoi (tmp); |
|
351 |
return 0; |
return 0; |
352 |
} |
} |
353 |
|
|
359 |
check_gnupg_engine (const char *need_gpg_ver, |
check_gnupg_engine (const char *need_gpg_ver, |
360 |
int *r_major, int *r_minor, int *r_patch) |
int *r_major, int *r_minor, int *r_patch) |
361 |
{ |
{ |
|
gpgme_ctx_t ctx; |
|
362 |
gpgme_engine_info_t inf; |
gpgme_engine_info_t inf; |
363 |
char *eng = NULL; |
char *eng = NULL; |
364 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
370 |
&need_major, &need_minor, &need_patch)) |
&need_major, &need_minor, &need_patch)) |
371 |
return 1; |
return 1; |
372 |
|
|
373 |
gpgme_new (&ctx); |
if (gpgme_get_engine_info (&inf)) |
374 |
inf = gpgme_ctx_get_engine_info (ctx); |
return -1; |
|
if (!inf) { |
|
|
gpgme_release (ctx); |
|
|
return -1; |
|
|
} |
|
375 |
|
|
376 |
/* 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. */ |
377 |
if (gpg_get_version (&eng)) { |
if (gpg_get_version (&eng)) |
378 |
gpgme_release (ctx); |
return -1; |
|
return -1; |
|
|
} |
|
379 |
if (strstr (eng, "IDEA")) |
if (strstr (eng, "IDEA")) |
380 |
idea_available = 1; |
idea_available = 1; |
381 |
safe_free (eng); |
safe_free (eng); |
382 |
if (parse_version_nr (inf->version, &major, &minor, &patch)) { |
|
383 |
gpgme_release (ctx); |
if (parse_version_nr (inf->version, &major, &minor, &patch)) |
384 |
return 1; |
return 1; |
|
} |
|
|
gpgme_release (ctx); |
|
385 |
|
|
386 |
if (major > need_major) |
if (major > need_major) |
387 |
rc = 0; |
rc = 0; |
404 |
static int |
static int |
405 |
cfgfile_count_keyrings (const char *fname, int *r_secrings, int *r_pubrings) |
cfgfile_count_keyrings (const char *fname, int *r_secrings, int *r_pubrings) |
406 |
{ |
{ |
407 |
gpg_optfile_t opt; |
config_file_t opt; |
408 |
gpg_option_t e; |
conf_option_t e; |
409 |
|
|
410 |
*r_secrings = 0; |
*r_secrings = 0; |
411 |
*r_pubrings = 0; |
*r_pubrings = 0; |
493 |
} |
} |
494 |
|
|
495 |
|
|
|
static int |
|
|
create_gpg_conf (void) |
|
|
{ |
|
|
FILE *fp; |
|
|
char *s, *optfile; |
|
|
|
|
|
s = get_gnupg_path (); |
|
|
if (!s) |
|
|
return WPTERR_FILE_CREAT; |
|
|
optfile = make_filename (s, GPG_CONF, NULL); |
|
|
fp = fopen (optfile, "wb"); |
|
|
if (!fp) { |
|
|
return WPTERR_FILE_CREAT; |
|
|
goto fail; |
|
|
} |
|
|
fwrite (options_skel, 1, strlen (options_skel), fp); |
|
|
fclose (fp); |
|
|
|
|
|
fail: |
|
|
free_if_alloc (s); |
|
|
free_if_alloc (optfile); |
|
|
return 0; |
|
|
} |
|
|
|
|
496 |
|
|
497 |
/* Return the contents of the options file as a char buf. */ |
/* Return the contents of the options file as a char buf. */ |
498 |
char* |
char* |
499 |
get_gnupg_config (void) |
get_gnupg_config (void) |
500 |
{ |
{ |
501 |
FILE *fp; |
FILE *fp; |
502 |
char *p = NULL, *optfile = NULL; |
char *p = NULL, *optfile; |
503 |
int fsize; |
int fsize; |
504 |
|
|
505 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
506 |
if (!optfile) |
if (!optfile) |
507 |
return NULL; |
return NULL; |
508 |
fsize = get_file_size (optfile); |
fsize = get_file_size (optfile); |
509 |
if (!fsize) { |
if (fsize < 1 || fsize > 100000) |
510 |
if (create_gpg_conf ()) |
goto leave; /* too large or does not exist */ |
511 |
return NULL; |
|
512 |
fsize = get_file_size (optfile); |
fp = fopen (optfile, "rb"); |
513 |
} |
if (!fp) |
514 |
if (fsize > 100000) |
goto leave; |
|
goto leave; /* too large */ |
|
515 |
p = new char[fsize+1]; |
p = new char[fsize+1]; |
516 |
if (!p) |
if (!p) |
517 |
BUG (NULL); |
BUG (NULL); |
|
fp = fopen( optfile, "rb" ); |
|
|
if (!fp) { |
|
|
free_if_alloc (p); |
|
|
return NULL; |
|
|
} |
|
518 |
fread (p, 1, fsize, fp); |
fread (p, 1, fsize, fp); |
519 |
fclose (fp); |
fclose (fp); |
520 |
p[fsize] = '\0'; |
p[fsize] = '\0'; |
521 |
free_if_alloc (optfile); |
|
|
|
|
522 |
leave: |
leave: |
523 |
|
free_if_alloc (optfile); |
524 |
return p; |
return p; |
525 |
} |
} |
526 |
|
|
530 |
int |
int |
531 |
set_gnupg_default_key (const char *key) |
set_gnupg_default_key (const char *key) |
532 |
{ |
{ |
533 |
gpg_optfile_t opt; |
config_file_t opt; |
534 |
gpg_option_t e; |
conf_option_t e; |
535 |
char *optfile = NULL; |
char *optfile = NULL; |
536 |
int rc = 0; |
int rc = 0; |
537 |
|
|
543 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
544 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
545 |
} |
} |
546 |
e = find_option (opt, "default-key"); |
e = conf_find_option (opt, "default-key"); |
547 |
if (e && !key) |
if (e && !key) |
548 |
e->used = 0; |
e->used = 0; |
549 |
else if (e) { |
else if (e) { |
552 |
e->used = 1; |
e->used = 1; |
553 |
} |
} |
554 |
else if (key) |
else if (key) |
555 |
add_entry (opt, ENTRY_MULTI, "default-key", key); |
conf_add_entry (opt, ENTRY_MULTI, "default-key", key); |
556 |
rc = commit_config (optfile, opt); |
rc = commit_config (optfile, opt); |
557 |
|
|
558 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
606 |
check_line (const char *buf) |
check_line (const char *buf) |
607 |
{ |
{ |
608 |
int j, len; |
int j, len; |
609 |
int rc; |
int rc = 0; |
610 |
|
|
611 |
if (*buf == '#' || *buf == '\r' || *buf == '\n') |
if (*buf == '#' || *buf == '\r' || *buf == '\n') |
612 |
return 1; |
return 1; |
|
rc = 0; |
|
613 |
for (j = 0; valid_gpg_args[j]; j++) { |
for (j = 0; valid_gpg_args[j]; j++) { |
614 |
len = strlen (valid_gpg_args[j]); |
len = strlen (valid_gpg_args[j]); |
615 |
if (!strncmp (valid_gpg_args[j], buf, len)) |
if (!strncmp (valid_gpg_args[j], buf, len)) |
626 |
int nbytes = 0, lineno=0; |
int nbytes = 0, lineno=0; |
627 |
unsigned j; |
unsigned j; |
628 |
|
|
629 |
for (j = 0; j < strlen (buf) && j < sizeof (line); j++) { |
for (j = 0; j < strlen (buf) && j < DIM (line); j++) { |
630 |
line[nbytes++] = buf[j]; |
line[nbytes++] = buf[j]; |
631 |
if (buf[j] == '\n' || j == (strlen (buf) - 1)) { |
if (buf[j] == '\n' || j == (strlen (buf) - 1)) { |
632 |
line[nbytes] = '\0'; |
line[nbytes] = '\0'; |
654 |
HANDLE fd; |
HANDLE fd; |
655 |
|
|
656 |
fd = CreateFile (ctx->fpath_object, GENERIC_READ, FILE_SHARE_READ, |
fd = CreateFile (ctx->fpath_object, GENERIC_READ, FILE_SHARE_READ, |
657 |
NULL, OPEN_ALWAYS, 0, NULL); |
NULL, OPEN_EXISTING, 0, NULL); |
658 |
if (fd == INVALID_HANDLE_VALUE) |
if (fd == INVALID_HANDLE_VALUE) |
659 |
return WPTERR_FILE_OPEN; |
return WPTERR_FILE_OPEN; |
660 |
GetFileTime (fd, NULL, NULL, &ctx->access); |
GetFileTime (fd, NULL, NULL, &ctx->access); |
704 |
void |
void |
705 |
free_gnupg_table (void) |
free_gnupg_table (void) |
706 |
{ |
{ |
707 |
int j; |
for (int j=0; j < gpg_table_count; j++) { |
|
|
|
|
for (j=0; j < gpg_table_count; j++) { |
|
708 |
free_if_alloc (gpg_table[j].object); |
free_if_alloc (gpg_table[j].object); |
709 |
free_if_alloc (gpg_table[j].fpath_object); |
free_if_alloc (gpg_table[j].fpath_object); |
710 |
} |
} |
716 |
keyring_check_last_access (void) |
keyring_check_last_access (void) |
717 |
{ |
{ |
718 |
int nfiles; |
int nfiles; |
|
int pos; |
|
719 |
|
|
720 |
nfiles = 0; |
nfiles = 0; |
721 |
for (pos = 0; pos < gpg_table_count; pos++) { |
for (int pos = 0; pos < gpg_table_count; pos++) { |
722 |
get_last_gnupg_access (&gpg_table[pos]); |
get_last_gnupg_access (&gpg_table[pos]); |
723 |
check_last_gnupg_access (&gpg_table[pos]); |
check_last_gnupg_access (&gpg_table[pos]); |
724 |
if (gpg_table[pos].modified) |
if (gpg_table[pos].modified) |
758 |
} |
} |
759 |
|
|
760 |
|
|
761 |
char* |
/* Check if the device where file @fname is stored on, is write-protected. */ |
|
get_gnupg_keyring_from_options (const char *fname, int pub) |
|
|
{ |
|
|
gpg_optfile_t opt; |
|
|
gpg_option_t e; |
|
|
char *kring = NULL; |
|
|
int rc; |
|
|
|
|
|
rc = parse_config (fname, &opt); |
|
|
if (rc) |
|
|
return NULL; |
|
|
if (pub) |
|
|
e = find_option (opt, "keyring"); |
|
|
else |
|
|
e = find_option (opt, "secret-keyring"); |
|
|
if (e) |
|
|
kring = m_strdup (e->val); |
|
|
release_config (opt); |
|
|
|
|
|
return kring; |
|
|
} |
|
|
|
|
|
|
|
|
/* Check if the device file @fname is stored on, is write-protected. */ |
|
762 |
static int |
static int |
763 |
my_access (const char *fname) |
check_file_access (const char *fname, int mode) |
764 |
{ |
{ |
765 |
HANDLE hd; |
HANDLE hd; |
766 |
|
|
767 |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, |
if (!mode) |
768 |
|
mode = FILE_SHARE_WRITE; |
769 |
|
hd = CreateFile (fname, GENERIC_WRITE, mode, |
770 |
NULL, OPEN_EXISTING, 0, NULL); |
NULL, OPEN_EXISTING, 0, NULL); |
771 |
if (hd == INVALID_HANDLE_VALUE) |
if (hd == INVALID_HANDLE_VALUE) |
772 |
return -1; |
return -1; |
774 |
return 0; |
return 0; |
775 |
} |
} |
776 |
|
|
777 |
|
|
778 |
|
/* Check the device where the file @fname is stored on, is either |
779 |
|
write-protected or if the file is already opened by another process |
780 |
|
exclusively. And as a result, we cannot use the file for output. */ |
781 |
|
int |
782 |
|
gpg_check_file_permissions (const char *fname, int mode) |
783 |
|
{ |
784 |
|
int api_mode; |
785 |
|
|
786 |
|
switch (mode) { |
787 |
|
case 0: api_mode = FILE_SHARE_READ; break; |
788 |
|
case 1: api_mode = FILE_SHARE_WRITE; break; |
789 |
|
case 2: api_mode = FILE_SHARE_WRITE|FILE_SHARE_READ; break; |
790 |
|
default: api_mode = FILE_SHARE_READ; break; |
791 |
|
} |
792 |
|
|
793 |
|
return check_file_access (fname, api_mode); |
794 |
|
} |
795 |
|
|
796 |
|
|
797 |
/* Check the file permissions of the public keyring. |
/* Check the file permissions of the public keyring. |
798 |
If @showmsg is 1 output a message in case of errors. |
If @showmsg is 1 output a message in case of errors. |
827 |
failed = 1; |
failed = 1; |
828 |
} |
} |
829 |
} |
} |
830 |
if (my_access (name)) { |
if (gpg_check_file_permissions (name, 1)) { |
831 |
if (showmsg) |
if (showmsg) |
832 |
msg_box (NULL, |
msg_box (NULL, |
833 |
_("You do not have file access to modify the contents of\n" |
_("You do not have file access to modify the contents of\n" |
876 |
|
|
877 |
|
|
878 |
int |
int |
879 |
|
gnupg_import_keypair (void) |
880 |
|
{ |
881 |
|
const char *file; |
882 |
|
gpgme_error_t err; |
883 |
|
GPGME gpgme; |
884 |
|
HWND hwnd = GetDesktopWindow (); |
885 |
|
|
886 |
|
file = get_fileopen_dlg (hwnd, _("Please choose your Key Pair"), |
887 |
|
"GPG Key File (*.gpg)\0*.gpg\0" |
888 |
|
"GPG Armored Key File (*.asc)\0*.asc\0\0", |
889 |
|
NULL); |
890 |
|
if (file == NULL) |
891 |
|
return WPTERR_FILE_OPEN; |
892 |
|
err = gpgme.importFromFile (file); |
893 |
|
if (err) |
894 |
|
msg_box (hwnd, gpgme_strerror (err), _("WinPT Error"), MB_ERR); |
895 |
|
else |
896 |
|
msg_box (hwnd, _("Key pair successfully imported."), "WinPT", MB_OK); |
897 |
|
return 0; |
898 |
|
} |
899 |
|
|
900 |
|
int |
901 |
gnupg_copy_keyrings (void) |
gnupg_copy_keyrings (void) |
902 |
{ |
{ |
903 |
const char *pring, *sring; |
const char *pring, *sring; |
904 |
char *file = NULL, *path = NULL; |
char *file = NULL, *path; |
905 |
int id = 0, rc = 0; |
int id, rc = 0; |
906 |
HWND hwnd; |
HWND hwnd; |
907 |
|
|
908 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
911 |
hwnd = GetDesktopWindow (); |
hwnd = GetDesktopWindow (); |
912 |
|
|
913 |
pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"), |
pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"), |
914 |
"GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL); |
"GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL); |
915 |
if (!pring) { |
if (!pring) { |
916 |
msg_box (hwnd, _("No keyring was chosen. Exit."), |
msg_box (hwnd, _("No keyring was chosen. Exit."), |
917 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
918 |
free_if_alloc (path); |
free_if_alloc (path); |
919 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
920 |
} |
} |
921 |
file = make_filename (path, "pubring", "gpg"); |
file = make_filename (path, "pubring", "gpg"); |
922 |
if (file_exist_check (file) == 0) { |
if (file_exist_check (file) == 0) { |
923 |
id = msg_box (hwnd, _("Overwrite old public keyring?"), |
id = msg_box (hwnd, _("Overwrite old public keyring?"), |
924 |
"WinPT", MB_INFO|MB_YESNO); |
"WinPT", MB_INFO|MB_YESNO); |
925 |
if (id == IDNO) |
if (id == IDNO) |
926 |
goto fail; |
goto fail; |
927 |
} |
} |
928 |
if (!CopyFile (pring, file, FALSE)) { |
if (!CopyFile (pring, file, FALSE)) { |
929 |
msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
msg_box (hwnd, _("Could not copy public keyring."), |
930 |
|
_("WinPT Error"), MB_ERR); |
931 |
rc = WPTERR_FILE_READ; |
rc = WPTERR_FILE_READ; |
932 |
goto fail; |
goto fail; |
933 |
} |
} |
948 |
goto fail; |
goto fail; |
949 |
} |
} |
950 |
if (!CopyFile (sring, file, FALSE)) { |
if (!CopyFile (sring, file, FALSE)) { |
951 |
msg_box (NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
msg_box (NULL, _("Could not copy secret keyring."), _("WinPT Error"), MB_ERR); |
952 |
rc = WPTERR_FILE_READ; |
rc = WPTERR_FILE_READ; |
953 |
} |
} |
954 |
|
|
1013 |
opt = get_gnupg_cfgfile (); |
opt = get_gnupg_cfgfile (); |
1014 |
if (!opt) |
if (!opt) |
1015 |
BUG (0); |
BUG (0); |
1016 |
name = get_gnupg_keyring_from_options (opt, 1); |
name = get_keyring_from_conf (opt, 1); |
1017 |
free_if_alloc (opt); |
free_if_alloc (opt); |
1018 |
free_if_alloc (p); |
free_if_alloc (p); |
1019 |
if (!name) |
if (!name) |
1037 |
get_backup_name (const char *templ) |
get_backup_name (const char *templ) |
1038 |
{ |
{ |
1039 |
struct tm *tm; |
struct tm *tm; |
1040 |
|
const char *fmt; |
1041 |
char *p; |
char *p; |
1042 |
time_t t; |
time_t t; |
1043 |
|
|
1044 |
t = time (NULL); |
t = time (NULL); |
1045 |
tm = localtime (&t); |
tm = localtime (&t); |
1046 |
p = new char [strlen (templ) + 8 + 1]; |
fmt = "%s-%d"; |
1047 |
|
p = new char [strlen (templ) + strlen (fmt) + 8 + 1]; |
1048 |
if (!p) |
if (!p) |
1049 |
BUG (0); |
BUG (0); |
1050 |
sprintf (p, "%s-%d", templ, tm->tm_wday % 3); |
sprintf (p, fmt, templ, tm->tm_wday % 3); |
1051 |
return p; |
return p; |
1052 |
} |
} |
1053 |
|
|
1059 |
void |
void |
1060 |
gnupg_backup_keyrings (int auto_backup, int backup_mode, int include_secr) |
gnupg_backup_keyrings (int auto_backup, int backup_mode, int include_secr) |
1061 |
{ |
{ |
1062 |
char *srcpath = NULL, *dstpath = NULL; |
char *srcpath, *dstpath; |
1063 |
char *name=NULL; |
char *name; |
1064 |
int rc; |
int rc; |
1065 |
|
|
1066 |
if (!auto_backup) |
if (!auto_backup) |
1068 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1069 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1070 |
if (backup_mode == 1) { |
if (backup_mode == 1) { |
1071 |
dstpath = multi_gnupg_path (1); |
/* If the backup mode uses the home directory the source |
1072 |
|
and destination folder will be the same. */ |
1073 |
|
dstpath = get_gnupg_path (); |
1074 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1075 |
} |
} |
1076 |
else if (backup_mode == 2) { |
else if (backup_mode == 2) { |
1090 |
DeleteFile (tmpfile); |
DeleteFile (tmpfile); |
1091 |
} |
} |
1092 |
free_if_alloc (tmpfile); |
free_if_alloc (tmpfile); |
1093 |
if (!fp || rc == IDCANCEL) |
if (!fp || rc == IDCANCEL) { |
1094 |
|
free_if_alloc (dstpath); |
1095 |
|
free_if_alloc (srcpath); |
1096 |
return; |
return; |
1097 |
|
} |
1098 |
} |
} |
1099 |
else { |
else { |
1100 |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), backup_mode); |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), backup_mode); |
1101 |
|
free_if_alloc (srcpath); |
1102 |
return; |
return; |
1103 |
} |
} |
1104 |
name = get_backup_name ("pubring-bak"); |
name = get_backup_name ("pubring-bak"); |