1 |
/* wptGPG.cpp - GnuPG configuration |
/* wptGPG.cpp - GnuPG configuration |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
92 |
char *path; |
char *path; |
93 |
|
|
94 |
path = get_reg_entry_gpg (GPG_REG_HOME); |
path = get_reg_entry_gpg (GPG_REG_HOME); |
95 |
if (path) { |
if (path && dir_exist_check (path) == 0) |
96 |
if (dir_exist_check (path) == 0) |
return path; |
97 |
return path; |
free_if_alloc (path); |
|
free_if_alloc (path); |
|
|
} |
|
98 |
path = multi_gnupg_path (1); |
path = multi_gnupg_path (1); |
99 |
return path; |
return path; |
100 |
} |
} |
113 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
114 |
if (!path) |
if (!path) |
115 |
return NULL; |
return NULL; |
116 |
p = get_reg_entry_gpg ("OptFile"); |
nlen = strlen (path) + 64; |
117 |
if (p && !strcmp (p, "")) { |
optfile = new char[nlen + 1]; |
118 |
nlen = strlen (path) + 64; |
if (!optfile) |
119 |
optfile = new char[nlen + 1]; |
BUG (NULL); |
120 |
if (!optfile) |
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
121 |
BUG (0); |
|
|
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
|
|
} |
|
|
else if (p) { |
|
|
nlen = strlen( p ) + 4; |
|
|
optfile = new char[nlen + 1]; |
|
|
if (!optfile) |
|
|
BUG (NULL); |
|
|
_snprintf (optfile, nlen, "%s", p); |
|
|
} |
|
|
else { |
|
|
nlen = strlen (path) + 64; |
|
|
optfile = new char[nlen + 1]; |
|
|
if( !optfile) |
|
|
BUG (NULL); |
|
|
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
|
|
} |
|
122 |
free_if_alloc (path); |
free_if_alloc (path); |
123 |
free_if_alloc (p); |
free_if_alloc (p); |
124 |
return optfile; |
return optfile; |
160 |
/* 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 |
161 |
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 |
162 |
appended string 'gpg.exe' is used. */ |
appended string 'gpg.exe' is used. */ |
|
|
|
|
/* FIXME: Use gpgme's engine info here. */ |
|
163 |
char* |
char* |
164 |
get_gnupg_prog (void) |
get_gnupg_prog (void) |
165 |
{ |
{ |
166 |
char *p; |
char *path; |
167 |
char *pgm = NULL; |
char *pgm; |
168 |
|
|
169 |
p = get_reg_entry_gpg (GPG_REG_EXE); |
pgm = get_reg_entry_gpg (GPG_REG_EXE); |
170 |
if (!p) { |
if (pgm) |
171 |
char *path = get_gnupg_path (); |
return pgm; |
172 |
if (!path) |
path = get_gnupg_path (); |
173 |
return NULL; |
if (!path) |
174 |
pgm = make_filename (path, "gpg", "exe"); |
return NULL; |
175 |
free_if_alloc (path); |
pgm = make_filename (path, "gpg", "exe"); |
176 |
} |
free_if_alloc (path); |
|
else { |
|
|
pgm = m_strdup (p); |
|
|
free_if_alloc (p); |
|
|
} |
|
177 |
return pgm; |
return pgm; |
178 |
} |
} |
179 |
|
|
183 |
Return value: the keyid of the secret key. */ |
Return value: the keyid of the secret key. */ |
184 |
static char * |
static char * |
185 |
default_key_from_cache (int *ret_no_useable) |
default_key_from_cache (int *ret_no_useable) |
186 |
{ |
{ |
187 |
|
gpgme_key_t key, pk; |
188 |
|
gpg_keycache_t sec, pub; |
189 |
const char *s; |
const char *s; |
190 |
char *keyid = NULL; |
char *keyid = NULL; |
|
gpgme_key_t key; |
|
|
gpg_keycache_t sec = keycache_get_ctx (0); |
|
191 |
|
|
192 |
if (!sec) |
sec = keycache_get_ctx (0); |
193 |
BUG (0); |
pub = keycache_get_ctx (1); |
194 |
gpg_keycache_rewind (sec); |
gpg_keycache_rewind (sec); |
195 |
while (!gpg_keycache_next_key (sec, 1, &key)) { |
while (!gpg_keycache_next_key (sec, 1, &key)) { |
196 |
if (key_is_useable (key)) { |
if (key_is_useable (key) && !get_pubkey (key->subkeys->keyid, &pk)) { |
197 |
s = key->subkeys->keyid; |
s = key->subkeys->keyid; |
198 |
if (s) |
if (s) |
199 |
keyid = m_strdup (s+8); |
keyid = m_strdup (s+8); |
200 |
break; |
break; |
201 |
} |
} |
202 |
} |
} |
215 |
int rc; |
int rc; |
216 |
gpg_optfile_t opt; |
gpg_optfile_t opt; |
217 |
gpg_option_t o; |
gpg_option_t o; |
218 |
char *conf = get_gnupg_cfgfile (); |
char *conf; |
219 |
|
|
220 |
|
conf = get_gnupg_cfgfile (); |
221 |
if (!conf) |
if (!conf) |
222 |
return -1; |
return -1; |
223 |
rc = parse_gpg_options (conf, &opt); |
rc = parse_gpg_options (conf, &opt); |
228 |
o = find_option (opt, "ask-cert-level"); |
o = find_option (opt, "ask-cert-level"); |
229 |
if (o) |
if (o) |
230 |
reg_prefs.gpg.ask_cert_level = 1; |
reg_prefs.gpg.ask_cert_level = 1; |
231 |
|
o = find_option (opt, "ask-cert-expire"); |
232 |
|
if (o) |
233 |
|
reg_prefs.gpg.ask_cert_expire = 1; |
234 |
release_gpg_options (opt); |
release_gpg_options (opt); |
235 |
free_if_alloc (conf); |
free_if_alloc (conf); |
236 |
return 0; |
return 0; |
237 |
} |
} |
238 |
|
|
239 |
|
|
240 |
|
/* handle the case the user added a '!' to force a subkey. */ |
241 |
|
static char* |
242 |
|
extract_keyid (const char *val) |
243 |
|
{ |
244 |
|
size_t len = strlen (val); |
245 |
|
|
246 |
|
if (len > 1 && val[len-1] == '!') { |
247 |
|
char *p = new char[len+1]; |
248 |
|
if (!p) |
249 |
|
BUG (0); |
250 |
|
memset (p, 0, len+1); |
251 |
|
memcpy (p, val, len-1); |
252 |
|
return p; |
253 |
|
} |
254 |
|
return m_strdup (val); |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
char* |
char* |
259 |
get_gnupg_default_key (void) |
get_gnupg_default_key (void) |
260 |
{ |
{ |
261 |
gpg_optfile_t opt = NULL; |
gpg_optfile_t opt = NULL; |
262 |
gpg_option_t e; |
gpg_option_t e; |
263 |
char * keyid = NULL, * optfile = NULL; |
char *keyid = NULL, *optfile = NULL; |
264 |
int no_usable=0, rc = 0; |
int no_usable=0, rc = 0; |
265 |
|
|
266 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
271 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
272 |
return default_key_from_cache (&no_usable); |
return default_key_from_cache (&no_usable); |
273 |
} |
} |
274 |
e = find_option( opt, "default-key" ); |
e = find_option (opt, "default-key"); |
275 |
if ( e ) |
if (!e) |
276 |
keyid = m_strdup( e->val ); |
e = find_option (opt, "local-user"); |
277 |
if( !e ) { |
if (e) |
278 |
e = find_option( opt, "local-user" ); |
keyid = extract_keyid (e->val); |
279 |
if( e ) |
|
|
keyid = m_strdup( e->val ); |
|
|
} |
|
|
if( !e ) { |
|
|
e = find_option( opt, "encrypt-to" ); |
|
|
if( e ) |
|
|
keyid = m_strdup( e->val ); |
|
|
} |
|
280 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
281 |
release_gpg_options (opt); |
release_gpg_options (opt); |
|
|
|
282 |
if (!keyid) |
if (!keyid) |
283 |
keyid = default_key_from_cache (&no_usable); |
keyid = default_key_from_cache (&no_usable); |
284 |
return keyid; |
return keyid; |
285 |
} /* get_gnupg_default_key */ |
} |
286 |
|
|
287 |
|
|
288 |
/* Check if GPG4WIN is available and if so, use the |
/* Check if GPG4WIN is available and if so, use the |
325 |
while (buf && *buf != '.' && i < 8) |
while (buf && *buf != '.' && i < 8) |
326 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
327 |
tmp[i] = 0; buf++; |
tmp[i] = 0; buf++; |
328 |
*major = atol( tmp ); |
*major = atoi (tmp); |
329 |
i=0; |
i=0; |
330 |
while (buf && *buf != '.' && i < 8) |
while (buf && *buf != '.' && i < 8) |
331 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
332 |
tmp[i] = 0; buf++; |
tmp[i] = 0; buf++; |
333 |
*minor = atol (tmp); |
*minor = atoi (tmp); |
334 |
i=0; |
i=0; |
335 |
while (buf && isdigit (*buf) && i < 8) |
while (buf && isdigit (*buf) && i < 8) |
336 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
337 |
tmp[i] = 0; |
tmp[i] = 0; |
338 |
*patch = atol (tmp); |
*patch = atoi (tmp); |
339 |
return 0; |
return 0; |
340 |
} |
} |
341 |
|
|
355 |
int rc = 1; |
int rc = 1; |
356 |
|
|
357 |
/* Convert the needed GPG version to the integer format. */ |
/* Convert the needed GPG version to the integer format. */ |
358 |
rc = parse_version_nr (need_gpg_ver, |
if (parse_version_nr (need_gpg_ver, |
359 |
&need_major, &need_minor, &need_patch); |
&need_major, &need_minor, &need_patch)) |
360 |
if (rc) |
return 1; |
|
return rc; |
|
361 |
|
|
362 |
gpgme_new (&ctx); |
gpgme_new (&ctx); |
363 |
inf = gpgme_ctx_get_engine_info (ctx); |
inf = gpgme_ctx_get_engine_info (ctx); |
367 |
} |
} |
368 |
|
|
369 |
/* 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. */ |
370 |
if (gpg_get_version (&eng)) |
if (gpg_get_version (&eng)) { |
371 |
|
gpgme_release (ctx); |
372 |
return -1; |
return -1; |
373 |
|
} |
374 |
if (strstr (eng, "IDEA")) |
if (strstr (eng, "IDEA")) |
375 |
idea_available = 1; |
idea_available = 1; |
376 |
free (eng); |
safe_free (eng); |
377 |
rc = parse_version_nr (inf->version, &major, &minor, &patch); |
if (parse_version_nr (inf->version, &major, &minor, &patch)) { |
|
if (rc) { |
|
378 |
gpgme_release (ctx); |
gpgme_release (ctx); |
379 |
return rc; |
return 1; |
380 |
} |
} |
381 |
|
gpgme_release (ctx); |
382 |
|
|
383 |
if (major > need_major) |
if (major > need_major) |
384 |
rc = 0; |
rc = 0; |
401 |
{ |
{ |
402 |
gpg_optfile_t opt; |
gpg_optfile_t opt; |
403 |
gpg_option_t e; |
gpg_option_t e; |
|
int rc = 0; |
|
404 |
|
|
405 |
*r_secrings = 0; |
*r_secrings = 0; |
406 |
*r_pubrings = 0; |
*r_pubrings = 0; |
|
rc = parse_gpg_options( fname, &opt ); |
|
|
if( rc ) |
|
|
return WPTERR_FILE_OPEN; |
|
407 |
|
|
408 |
for( e = opt->list; e; e = e->next ) { |
if (parse_gpg_options (fname, &opt)) |
409 |
if( !strcmp( e->name, "secret-keyring" ) ) { |
return WPTERR_FILE_OPEN; |
410 |
if( !file_exist_check( e->val ) ) |
for (e = opt->list; e; e = e->next) { |
411 |
|
if (!strcmp( e->name, "secret-keyring")) { |
412 |
|
if (!file_exist_check (e->val)) |
413 |
r_secrings[0]++; |
r_secrings[0]++; |
414 |
} |
} |
415 |
else if( !strcmp( e->name, "keyring" ) ) { |
else if (!strcmp (e->name, "keyring")) { |
416 |
if( !file_exist_check( e->val ) ) |
if (!file_exist_check (e->val)) |
417 |
r_pubrings[0]++; |
r_pubrings[0]++; |
418 |
} |
} |
419 |
} |
} |
420 |
release_gpg_options( opt ); |
release_gpg_options (opt); |
421 |
return 0; |
return 0; |
422 |
} /* check_gnupg_cfgfile */ |
} |
423 |
|
|
424 |
|
|
425 |
/* Usually GPG creates the pubring.gpg, secring.gpg on |
/* Usually GPG creates the pubring.gpg, secring.gpg on |
429 |
create_empty_keyring (int _pub) |
create_empty_keyring (int _pub) |
430 |
{ |
{ |
431 |
char *name; |
char *name; |
432 |
FILE *f; |
FILE *fp; |
433 |
|
|
434 |
name = get_gnupg_keyring (_pub, 0); |
name = get_gnupg_keyring (_pub, 0); |
435 |
if (file_exist_check (name) != 0) { |
if (name && file_exist_check (name) != 0) { |
436 |
f = fopen (name, "ab"); |
fp = fopen (name, "ab"); |
437 |
if (f != NULL) |
if (fp != NULL) |
438 |
fclose (f); |
fclose (fp); |
439 |
} |
} |
440 |
free_if_alloc (name); |
free_if_alloc (name); |
441 |
} |
} |
469 |
rc = file_exist_check (optfile); |
rc = file_exist_check (optfile); |
470 |
if (!rc && get_file_size (optfile) > 0) { |
if (!rc && get_file_size (optfile) > 0) { |
471 |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
472 |
if (!rc && secrings && pubrings) { |
if (!rc && secrings > 0 && pubrings > 0) { |
473 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
474 |
return 0; /* found two keyrings in the option file */ |
return 0; /* found two keyrings in the option file */ |
475 |
} |
} |
495 |
char *s, *optfile; |
char *s, *optfile; |
496 |
|
|
497 |
s = get_gnupg_path (); |
s = get_gnupg_path (); |
498 |
if( s == NULL ) |
if (!s) |
499 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
500 |
optfile = make_filename (s, GPG_CONF, NULL); |
optfile = make_filename (s, GPG_CONF, NULL); |
501 |
fp = fopen (optfile, "wb"); |
fp = fopen (optfile, "wb"); |
502 |
if (fp == NULL) { |
if (!fp) { |
503 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
504 |
goto fail; |
goto fail; |
505 |
} |
} |
510 |
free_if_alloc (s); |
free_if_alloc (s); |
511 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
512 |
return 0; |
return 0; |
513 |
} /* create_gpg_options */ |
} |
514 |
|
|
515 |
|
|
516 |
/* |
/* Return the contents of the options file as a char buf. */ |
517 |
* Return the contents of the options file as a char buf. |
char* |
|
*/ |
|
|
char * |
|
518 |
get_gnupg_config (void) |
get_gnupg_config (void) |
519 |
{ |
{ |
520 |
FILE * fp; |
FILE *fp; |
521 |
char * p = NULL, * optfile = NULL; |
char *p = NULL, *optfile = NULL; |
522 |
int fsize, rc = 0; |
int fsize, rc = 0; |
523 |
|
|
524 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
525 |
if( optfile == NULL ) |
if (optfile == NULL) |
526 |
return NULL; |
return NULL; |
527 |
fsize = get_file_size( optfile ); |
fsize = get_file_size (optfile); |
528 |
if( !fsize ) { |
if (!fsize) { |
529 |
rc = create_gpg_options( ); |
rc = create_gpg_options (); |
530 |
if ( rc ) |
if (rc) |
531 |
return NULL; |
return NULL; |
532 |
fsize = get_file_size( optfile ); |
fsize = get_file_size (optfile); |
533 |
} |
} |
534 |
if( fsize > 100000 ) |
if (fsize > 100000) |
535 |
goto leave; /* too large */ |
goto leave; /* too large */ |
536 |
p = new char[fsize+1]; |
p = new char[fsize+1]; |
537 |
if( p == NULL ) |
if (!p) |
538 |
BUG( NULL ); |
BUG (NULL); |
539 |
fp = fopen( optfile, "rb" ); |
fp = fopen( optfile, "rb" ); |
540 |
if( fp == NULL ) { |
if (!fp) { |
541 |
free_if_alloc( p ); |
free_if_alloc (p); |
542 |
return NULL; |
return NULL; |
543 |
} |
} |
544 |
fread( p, 1, fsize, fp ); |
fread (p, 1, fsize, fp); |
545 |
fclose( fp ); |
fclose (fp); |
546 |
p[fsize] = '\0'; |
p[fsize] = '\0'; |
547 |
free_if_alloc( optfile ); |
free_if_alloc (optfile); |
548 |
|
|
549 |
leave: |
leave: |
550 |
return p; |
return p; |
551 |
} /* get_gnupg_config */ |
} |
552 |
|
|
553 |
|
|
554 |
|
/* Set the default key in the gpg.conf. |
555 |
|
If @key is NULL, the entry will be deleted. */ |
556 |
int |
int |
557 |
set_gnupg_default_key (const char * key) |
set_gnupg_default_key (const char *key) |
558 |
{ |
{ |
559 |
gpg_optfile_t opt; |
gpg_optfile_t opt; |
560 |
gpg_option_t e; |
gpg_option_t e; |
563 |
|
|
564 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
565 |
if (!optfile) |
if (!optfile) |
566 |
return -1; |
return WPTERR_FILE_OPEN; |
567 |
rc = parse_gpg_options (optfile, &opt); |
rc = parse_gpg_options (optfile, &opt); |
568 |
if( rc ) { |
if (rc) { |
569 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
570 |
return -1; |
return WPTERR_GENERAL; |
571 |
} |
} |
572 |
e = find_option (opt, "default-key"); |
e = find_option (opt, "default-key"); |
573 |
if (e) { |
if (e && !key) |
574 |
|
e->used = 0; |
575 |
|
else if (e) { |
576 |
free_if_alloc (e->val); |
free_if_alloc (e->val); |
577 |
e->val = m_strdup (key); |
e->val = m_strdup (key); |
578 |
e->used = 1; |
e->used = 1; |
579 |
} |
} |
580 |
else |
else if (key) |
581 |
add_entry (opt, ENTRY_MULTI, "default-key", key); |
add_entry (opt, ENTRY_MULTI, "default-key", key); |
582 |
rc = commit_gpg_options (optfile, opt); |
rc = commit_gpg_options (optfile, opt); |
583 |
|
|
585 |
release_gpg_options (opt); |
release_gpg_options (opt); |
586 |
|
|
587 |
return rc; |
return rc; |
588 |
} /* set_gnupg_default_key */ |
} |
589 |
|
|
590 |
|
|
591 |
/* |
/* Set the contents of the options file. */ |
|
* Set the contents of the options file. |
|
|
*/ |
|
592 |
int |
int |
593 |
set_gnupg_options( const char *buf, size_t buflen ) |
set_gnupg_options (const char *buf, size_t buflen) |
594 |
{ |
{ |
595 |
FILE *fp; |
FILE *fp; |
596 |
char *optfile = NULL; |
char *optfile = NULL; |
597 |
|
|
598 |
optfile = get_gnupg_cfgfile( ); |
optfile = get_gnupg_cfgfile (); |
599 |
if( optfile == NULL ) |
if (!optfile) |
600 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
601 |
|
|
602 |
fp = fopen( optfile, "wb" ); |
fp = fopen (optfile, "wb"); |
603 |
if( fp == NULL ) { |
if (!fp) { |
604 |
free_if_alloc( optfile ); |
free_if_alloc (optfile); |
605 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
606 |
} |
} |
607 |
fwrite( buf, 1, buflen, fp ); |
fwrite (buf, 1, buflen, fp); |
608 |
fclose( fp ); |
fclose (fp); |
609 |
free_if_alloc( optfile ); |
free_if_alloc (optfile); |
610 |
return 0; |
return 0; |
611 |
} /* set_gnupg_options */ |
} |
612 |
|
|
613 |
|
|
614 |
/* |
/* |
615 |
* Check if the line contains a valid GPG argument. |
* Check if the line contains a valid GPG argument. |
616 |
*/ |
*/ |
617 |
static int |
static int |
618 |
check_line( const char *buf ) |
check_line (const char *buf) |
619 |
{ |
{ |
620 |
int j, len; |
int j, len; |
621 |
int rc = 0; |
int rc = 0; |
622 |
|
|
623 |
if( *buf == '#' || *buf == '\r' || *buf == '\n' ) |
if (*buf == '#' || *buf == '\r' || *buf == '\n') |
624 |
return 1; |
return 1; |
625 |
rc = 0; |
rc = 0; |
626 |
for ( j = 0; valid_gpg_args[j]; j++ ) { |
for (j = 0; valid_gpg_args[j]; j++) { |
627 |
len = strlen( valid_gpg_args[j] ); |
len = strlen (valid_gpg_args[j]); |
628 |
if( !strncmp( valid_gpg_args[j], buf, len ) ) |
if (!strncmp (valid_gpg_args[j], buf, len)) |
629 |
rc = 1; |
rc = 1; |
630 |
} |
} |
631 |
|
|
632 |
return rc; |
return rc; |
633 |
} /* check_line */ |
} |
634 |
|
|
635 |
|
|
636 |
int |
int |
637 |
check_gnupg_options( const char *buf ) |
check_gnupg_options (const char *buf) |
638 |
{ |
{ |
639 |
char line[1024]; |
char line[1024]; |
640 |
int nbytes = 0; |
int nbytes = 0; |
653 |
} |
} |
654 |
|
|
655 |
return 0; |
return 0; |
656 |
} /* check_gnupg_options */ |
} |
657 |
|
|
658 |
|
|
659 |
/* Store the last access of the file inside the watcher @ctx. */ |
/* Store the last access of the file inside the watcher @ctx. */ |
666 |
|
|
667 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
668 |
file = make_filename (path, ctx->object, NULL); |
file = make_filename (path, ctx->object, NULL); |
669 |
fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); |
fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, |
670 |
|
OPEN_ALWAYS, 0, NULL); |
671 |
if (fd == INVALID_HANDLE_VALUE) { |
if (fd == INVALID_HANDLE_VALUE) { |
672 |
free_if_alloc (path); |
free_if_alloc (path); |
673 |
free_if_alloc (file); |
free_if_alloc (file); |
710 |
|
|
711 |
for (j = 0; j < gpg_table_count; j++) { |
for (j = 0; j < gpg_table_count; j++) { |
712 |
p = gpg_table[j].object = m_strdup (gpg_objs[j]); |
p = gpg_table[j].object = m_strdup (gpg_objs[j]); |
|
if (!p) |
|
|
BUG (NULL); |
|
713 |
memset (&gpg_table[j].access, 0, sizeof (FILETIME)); |
memset (&gpg_table[j].access, 0, sizeof (FILETIME)); |
714 |
memset (&gpg_table[j].last_access, 0, sizeof (FILETIME)); |
memset (&gpg_table[j].last_access, 0, sizeof (FILETIME)); |
715 |
gpg_table[j].modified = 0; |
gpg_table[j].modified = 0; |
823 |
int failed = 0, ans=0, attrs=0; |
int failed = 0, ans=0, attrs=0; |
824 |
|
|
825 |
p = get_gnupg_path (); |
p = get_gnupg_path (); |
826 |
if (check_keyring (&p) && p) { |
if (p && check_keyring (&p)) { |
827 |
name = make_filename (p, "pubring", "gpg"); |
name = make_filename (p, "pubring", "gpg"); |
828 |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
829 |
ans = msg_box (NULL, |
ans = msg_box (NULL, |
905 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
906 |
hwnd = GetDesktopWindow (); |
hwnd = GetDesktopWindow (); |
907 |
|
|
908 |
pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"), |
pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"), |
909 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL); |
"GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL); |
910 |
if (!pring) { |
if (!pring) { |
911 |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
msg_box (hwnd, _("No keyring was chosen. Exit."), |
912 |
|
_("WinPT Error"), MB_ERR); |
913 |
free_if_alloc (path); |
free_if_alloc (path); |
914 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
915 |
} |
} |
916 |
file = make_filename (path, "pubring", "gpg"); |
file = make_filename (path, "pubring", "gpg"); |
917 |
if (file_exist_check (file) == 0) { |
if (file_exist_check (file) == 0) { |
918 |
id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO); |
id = msg_box (hwnd, _("Overwrite old public keyring?"), |
919 |
|
"WinPT", MB_INFO|MB_YESNO); |
920 |
if (id == IDNO) |
if (id == IDNO) |
921 |
goto fail; |
goto fail; |
922 |
} |
} |
927 |
} |
} |
928 |
free_if_alloc (file); |
free_if_alloc (file); |
929 |
|
|
930 |
sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"), |
sring = get_fileopen_dlg (hwnd, _("Please choose your Secret Keyring"), |
931 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL); |
"GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL); |
932 |
if (!sring) { |
if (!sring) { |
933 |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
msg_box (NULL, _("No keyring was chosen. Exit."), |
934 |
|
_("WinPT Error"), MB_ERR); |
935 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
936 |
} |
} |
937 |
file = make_filename (path, "secring", "gpg"); |
file = make_filename (path, "secring", "gpg"); |
938 |
if (file_exist_check (file) == 0) { |
if (file_exist_check (file) == 0) { |
939 |
id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO); |
id = msg_box (hwnd, _("Overwrite old secret keyring?"), |
940 |
if( id == IDNO ) |
"WinPT", MB_INFO|MB_YESNO); |
941 |
|
if (id == IDNO) |
942 |
goto fail; |
goto fail; |
943 |
} |
} |
944 |
if (!CopyFile (sring, file, FALSE)) { |
if (!CopyFile (sring, file, FALSE)) { |
945 |
msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
msg_box (NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
946 |
rc = WPTERR_FILE_READ; |
rc = WPTERR_FILE_READ; |
947 |
} |
} |
948 |
|
|
950 |
free_if_alloc (file); |
free_if_alloc (file); |
951 |
free_if_alloc (path); |
free_if_alloc (path); |
952 |
return rc; |
return rc; |
953 |
} /* gnupg_import_keyrings */ |
} |
954 |
|
|
955 |
|
|
956 |
/* Backup the gpg.conf file. */ |
/* Backup the gpg.conf file. */ |
973 |
backup_one_file (const char *srcpath, const char *srcn, |
backup_one_file (const char *srcpath, const char *srcn, |
974 |
const char *dstpath, const char *dstn) |
const char *dstpath, const char *dstn) |
975 |
{ |
{ |
976 |
char * src, * dst; |
char *src, *dst; |
977 |
BOOL rc; |
BOOL rc; |
978 |
|
|
979 |
src = make_filename (srcpath, srcn, "gpg"); |
src = make_filename (srcpath, srcn, "gpg"); |
985 |
rc = CopyFile (src, dst, FALSE); |
rc = CopyFile (src, dst, FALSE); |
986 |
free_if_alloc (src); |
free_if_alloc (src); |
987 |
free_if_alloc (dst); |
free_if_alloc (dst); |
988 |
if (!rc) |
if (!rc) { |
|
{ |
|
989 |
log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn); |
log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn); |
990 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
991 |
} |
} |
992 |
return 0; |
return 0; |
993 |
} /* backup_one_file */ |
} |
994 |
|
|
995 |
|
|
996 |
/* Figure out first public keyring which is not empty. |
/* Figure out first public keyring which is not empty. |
1048 |
} |
} |
1049 |
|
|
1050 |
|
|
1051 |
/* Make backups of all keyrings. The public key ring is |
/* Make backups of all keyrings. The public key ring is rotated like |
1052 |
rotated like this pubring-%d.gpg. */ |
this pubring-%d.gpg. |
1053 |
|
If @auto_backup is false, no action is performed. |
1054 |
|
@include_secr indicated if the backup includes the secret keyring. */ |
1055 |
void |
void |
1056 |
gnupg_backup_keyrings (void) |
gnupg_backup_keyrings (int auto_backup, int backup_mode, int include_secr) |
1057 |
{ |
{ |
1058 |
char *srcpath = NULL, *dstpath = NULL; |
char *srcpath = NULL, *dstpath = NULL; |
1059 |
char *name=NULL; |
char *name=NULL; |
1060 |
int rc, bakmode=0; |
int rc; |
1061 |
|
|
1062 |
if (!reg_prefs.auto_backup) |
if (!auto_backup) |
1063 |
return; |
return; |
|
bakmode = reg_prefs.backup.mode; |
|
1064 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1065 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1066 |
if (bakmode == 1) { |
if (backup_mode == 1) { |
1067 |
dstpath = multi_gnupg_path (1); |
dstpath = multi_gnupg_path (1); |
1068 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1069 |
} |
} |
1070 |
else if (bakmode == 2) { |
else if (backup_mode == 2) { |
1071 |
char *tmpfile; |
char *tmpfile; |
1072 |
FILE *fp; |
FILE *fp; |
1073 |
|
|
1074 |
dstpath = m_strdup (reg_prefs.backup.path); |
dstpath = m_strdup (reg_prefs.backup.path); |
|
if (!dstpath) |
|
|
BUG (0); |
|
1075 |
tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp"); |
tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp"); |
1076 |
fp = fopen (tmpfile, "wb"); |
fp = fopen (tmpfile, "wb"); |
1077 |
if (!fp) |
if (!fp) |
1078 |
rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, |
rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, |
1079 |
_("The backup drive '%s' does not seems to accessable.\n" |
_("The backup drive '%s' does not seems to be accessable.\n" |
1080 |
"Please insert/check the drive to continue."), dstpath); |
"Please insert/check the drive to continue."), dstpath); |
1081 |
else { |
else { |
1082 |
rc = 0; |
rc = 0; |
1088 |
return; |
return; |
1089 |
} |
} |
1090 |
else { |
else { |
1091 |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), backup_mode); |
1092 |
return; |
return; |
1093 |
} |
} |
1094 |
name = get_backup_name ("pubring-bak"); |
name = get_backup_name ("pubring-bak"); |
1095 |
rc = backup_one_file (srcpath, "pubring", dstpath, name); |
rc = backup_one_file (srcpath, "pubring", dstpath, name); |
1096 |
if (!rc) |
if (!rc && include_secr) |
1097 |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
1098 |
free_if_alloc (name); |
free_if_alloc (name); |
1099 |
free_if_alloc (srcpath); |
free_if_alloc (srcpath); |
1105 |
void |
void |
1106 |
gnupg_display_error (void) |
gnupg_display_error (void) |
1107 |
{ |
{ |
1108 |
char tmpath[512], * errstr; |
char tmpath[512], *errstr; |
1109 |
size_t size = 0; |
size_t size = 0; |
1110 |
FILE * fp; |
FILE *fp; |
1111 |
|
|
1112 |
GetTempPath (sizeof tmpath - 32, (tmpath)); |
get_temp_name (tmpath, sizeof (tmpath), "gpg_stderr"); |
|
strcat (tmpath, "gpg_stderr"); |
|
1113 |
size = get_file_size (tmpath); |
size = get_file_size (tmpath); |
1114 |
if (file_exist_check (tmpath) || size <= 0) |
if (file_exist_check (tmpath) || size <= 0) |
1115 |
return; |
return; |