33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
34 |
#include "wptErrors.h" |
#include "wptErrors.h" |
35 |
#include "wptW32API.h" |
#include "wptW32API.h" |
36 |
|
#include "wptCrypto.h" |
37 |
|
|
38 |
#define GPG_CONF "gpg.conf" |
#define GPG_CONF "gpg.conf" |
39 |
|
|
60 |
static char buf[256+64]; |
static char buf[256+64]; |
61 |
BOOL ec; |
BOOL ec; |
62 |
|
|
63 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
64 |
memset (buf, 0, sizeof (buf)); |
memset (buf, 0, sizeof (buf)); |
65 |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
66 |
if (ec != 1) |
if (ec != 1) |
68 |
strcat (buf, "\\gnupg"); |
strcat (buf, "\\gnupg"); |
69 |
if (access (buf, 00)) |
if (access (buf, 00)) |
70 |
return NULL; |
return NULL; |
71 |
return buf; |
return m_strdup (buf); |
72 |
} |
} |
73 |
|
|
74 |
/* |
/* Return the full path of the GnuPG application. First the registry is scanned |
75 |
* Return the full path of the GnuPG application. First the registry is scanned |
for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
|
* for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
|
76 |
*/ |
*/ |
77 |
char* |
char* |
78 |
get_gnupg_path (void) |
get_gnupg_path (void) |
79 |
{ |
{ |
80 |
char *p = NULL, *path = NULL; |
char *p = NULL, *path = NULL; |
81 |
|
|
82 |
p = get_reg_entry_gpg ("HomeDir"); |
p = get_reg_entry_gpg ("HomeDir"); |
83 |
if (p) { |
if (p) { |
85 |
free_if_alloc (p); |
free_if_alloc (p); |
86 |
return path; |
return path; |
87 |
} |
} |
88 |
else { |
else |
89 |
p = multi_gnupg_path (); |
return multi_gnupg_path (); |
|
if (p) |
|
|
return m_strdup (p); |
|
|
} |
|
90 |
return m_strdup ("c:\\gnupg"); |
return m_strdup ("c:\\gnupg"); |
91 |
} /* get_gnupg_path */ |
} |
92 |
|
|
93 |
|
|
94 |
char* |
char* |
161 |
* appended string 'gpg.exe' is used. |
* appended string 'gpg.exe' is used. |
162 |
*/ |
*/ |
163 |
char* |
char* |
164 |
get_gnupg_prog( void ) |
get_gnupg_prog (void) |
165 |
{ |
{ |
166 |
char *p, *path, *pgm = NULL; |
char *p, *path, *pgm = NULL; |
167 |
size_t nlen = 0; |
size_t nlen = 0; |
183 |
|
|
184 |
|
|
185 |
static char * |
static char * |
186 |
default_key_from_cache (int * ret_no_useable) |
default_key_from_cache (int *ret_no_useable) |
187 |
{ |
{ |
188 |
const char * s; |
const char * s; |
189 |
char * keyid = NULL; |
char * keyid = NULL; |
190 |
gpgme_key_t key; |
gpgme_key_t key; |
191 |
gpgme_keycache_t sec = keycache_get_ctx (0); |
gpg_keycache_t sec = keycache_get_ctx (0); |
192 |
|
|
193 |
if (!sec) |
if (!sec) |
194 |
BUG (0); |
BUG (0); |
195 |
gpgme_keycache_rewind (sec); |
gpg_keycache_rewind (sec); |
196 |
while (!gpgme_keycache_next_key (sec, 1, &key)) |
while (!gpg_keycache_next_key (sec, 1, &key)) { |
197 |
{ |
if (key_is_useable (key)) { |
198 |
if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_USABLE, NULL, 0)) |
s = key->subkeys->keyid; |
|
{ |
|
|
s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0); |
|
199 |
if (s) |
if (s) |
200 |
keyid = m_strdup (s+8); |
keyid = m_strdup (s+8); |
201 |
break; |
break; |
202 |
} |
} |
203 |
} |
} |
204 |
if (!keyid) |
if (!keyid) { |
|
{ |
|
205 |
*ret_no_useable = 1; |
*ret_no_useable = 1; |
206 |
msg_box( NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
msg_box( NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
207 |
} |
} |
246 |
return keyid; |
return keyid; |
247 |
} /* get_gnupg_default_key */ |
} /* get_gnupg_default_key */ |
248 |
|
|
249 |
/* |
|
250 |
* Check if the gpg application (exe file) is available. |
/* Check if the gpg application (exe file) is available. */ |
|
*/ |
|
251 |
int |
int |
252 |
check_gnupg_prog( void ) |
check_gnupg_prog (void) |
253 |
{ |
{ |
254 |
char *pgm = NULL; |
char *pgm = NULL; |
255 |
int rc = 0; |
int rc = 0; |
256 |
|
|
257 |
pgm = get_gnupg_prog( ); |
pgm = get_gnupg_prog (); |
258 |
if( pgm == NULL ) |
if (!pgm) |
259 |
rc = WPTERR_GPG_EXEFILE; |
rc = WPTERR_GPG_EXEFILE; |
260 |
if( file_exist_check( pgm ) ) |
if (file_exist_check (pgm)) |
261 |
rc = WPTERR_GPG_EXEFILE; |
rc = WPTERR_GPG_EXEFILE; |
262 |
free_if_alloc( pgm ); |
free_if_alloc (pgm); |
263 |
return rc; |
return rc; |
264 |
} /* check_gpg_prog */ |
} |
265 |
|
|
266 |
|
|
267 |
static int |
static int |
268 |
parse_version_nr( const char * buf, int *major, int *minor, int *patch ) |
parse_version_nr (const char * buf, int *major, int *minor, int *patch) |
269 |
{ |
{ |
270 |
char tmp[8]; |
char tmp[8]; |
271 |
int i; |
int i; |
272 |
|
|
|
if( strncmp( buf, "gpg ", 4 ) ) |
|
|
return -1; |
|
|
buf += 4; |
|
|
if( strncmp( buf, "(GnuPG) ", 8 ) ) |
|
|
return -1; |
|
|
buf += 8; |
|
273 |
i=0; |
i=0; |
274 |
while( buf && *buf != '.' && i < 8 ) |
while( buf && *buf != '.' && i < 8 ) |
275 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
289 |
} |
} |
290 |
|
|
291 |
|
|
292 |
|
/* Check if the gnupg engine fullfills the minimum requirement |
293 |
|
version given in @r_major.@r_minor.@r_patch. On success these |
294 |
|
variables contain the GPG version which is installed. */ |
295 |
int |
int |
296 |
check_gnupg_engine (int * r_major, int * r_minor, int * r_patch) |
check_gnupg_engine (int *r_major, int *r_minor, int *r_patch) |
297 |
{ |
{ |
298 |
gpgme_error_t err; |
gpgme_ctx_t ctx; |
299 |
|
gpgme_engine_info_t inf; |
300 |
char * eng = NULL; |
char * eng = NULL; |
301 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
302 |
int rc; |
int rc; |
303 |
|
|
304 |
err = gpgme_op_version( &eng ); |
gpgme_new (&ctx); |
305 |
if( err ) |
inf = gpgme_ctx_get_engine_info (ctx); |
306 |
|
if (!inf) { |
307 |
|
gpgme_release (ctx); |
308 |
return -1; |
return -1; |
309 |
if( strstr( eng, "IDEA" ) ) |
} |
310 |
|
/* We need to exec GPG again to find out if IDEA is available. */ |
311 |
|
if (gpg_get_version (&eng)) |
312 |
|
return -1; |
313 |
|
if (strstr (eng, "IDEA")) |
314 |
idea_available = 1; |
idea_available = 1; |
315 |
rc = parse_version_nr( eng, &major, &minor, &patch ); |
free (eng); |
316 |
free( eng ); eng = NULL; |
rc = parse_version_nr( inf->version, &major, &minor, &patch ); |
317 |
if( rc ) |
if( rc ) { |
318 |
|
gpgme_release (ctx); |
319 |
return rc; |
return rc; |
320 |
if( major < *r_major |
} |
321 |
|| minor < *r_minor) |
if (major < *r_major || minor < *r_minor) |
322 |
rc = 1; |
rc = 1; |
323 |
else { |
else { |
324 |
if (patch < *r_patch ) |
if (patch < *r_patch) |
325 |
rc = 1; |
rc = 1; |
326 |
rc = 0; |
rc = 0; |
327 |
} |
} |
329 |
*r_minor = minor; |
*r_minor = minor; |
330 |
*r_patch = patch; |
*r_patch = patch; |
331 |
return rc; |
return rc; |
332 |
} /* check_gnupg_engine */ |
} |
333 |
|
|
334 |
|
|
335 |
int |
int |
436 |
* Return the contents of the options file as a char buf. |
* Return the contents of the options file as a char buf. |
437 |
*/ |
*/ |
438 |
char * |
char * |
439 |
get_gnupg_config( void ) |
get_gnupg_config (void) |
440 |
{ |
{ |
441 |
FILE * fp; |
FILE * fp; |
442 |
char * p = NULL, * optfile = NULL; |
char * p = NULL, * optfile = NULL; |
443 |
int fsize, rc = 0; |
int fsize, rc = 0; |
444 |
|
|
445 |
optfile = get_gnupg_cfgfile( ); |
optfile = get_gnupg_cfgfile (); |
446 |
if( optfile == NULL ) |
if( optfile == NULL ) |
447 |
return NULL; |
return NULL; |
448 |
fsize = get_file_size( optfile ); |
fsize = get_file_size( optfile ); |
651 |
} /* keyring_check_last_access */ |
} /* keyring_check_last_access */ |
652 |
|
|
653 |
|
|
654 |
const char * |
const char* |
655 |
gnupg_check_file_ext (const char * fname) |
gnupg_check_file_ext (const char *fname, int *r_type) |
656 |
{ |
{ |
657 |
char file_ext[5]; |
char file_ext[5]; |
658 |
|
|
659 |
if (!strchr( fname, '.' )) |
if (r_type) *r_type = PGP_NONE; |
660 |
|
if (!strchr (fname, '.' )) |
661 |
return "UNKNOWN"; |
return "UNKNOWN"; |
662 |
|
|
663 |
strncpy (file_ext, fname + strlen (fname) - 4, 4); |
strncpy (file_ext, fname + strlen (fname) - 4, 4); |
664 |
file_ext[4] = '\0'; |
file_ext[4] = '\0'; |
665 |
if (!stricmp (file_ext, ".asc")) |
if (!stricmp (file_ext, ".asc")) |
666 |
return "ARMORED"; |
return "ARMORED"; |
667 |
else if (!stricmp (file_ext, ".sig")) |
else if (!stricmp (file_ext, ".sig")) { |
668 |
|
if (r_type) |
669 |
|
*r_type = PGP_SIG; |
670 |
return "SIGNED"; |
return "SIGNED"; |
671 |
else if (!stricmp (file_ext, ".gpg") |
} |
672 |
|| !stricmp (file_ext, ".pgp")) |
else if (!stricmp (file_ext, ".gpg") || !stricmp (file_ext, ".pgp")) { |
673 |
|
if (r_type) |
674 |
|
*r_type = PGP_MESSAGE; |
675 |
return "ENCRYPTED"; |
return "ENCRYPTED"; |
676 |
else |
} |
|
return "UNKNOWN"; |
|
|
|
|
677 |
return "UNKNOWN"; |
return "UNKNOWN"; |
678 |
} /* gnupg_check_file_ext */ |
} |
679 |
|
|
680 |
|
|
681 |
char * |
char * |
904 |
|
|
905 |
|
|
906 |
void |
void |
907 |
gnupg_backup_options (int keep) |
gnupg_backup_options (void) |
908 |
{ |
{ |
909 |
char *optfile = NULL; |
char *cfgfile = NULL; |
910 |
char bak[1024]; |
char bak[512]; |
911 |
|
|
912 |
optfile = get_gnupg_cfgfile (); |
cfgfile = get_gnupg_cfgfile (); |
913 |
if (optfile == NULL) |
if (cfgfile == NULL) |
914 |
return; |
return; |
915 |
if (keep) |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
916 |
_snprintf (bak, DIM (bak)-1, "%s.old", optfile); |
CopyFile (cfgfile, bak, FALSE); |
917 |
else |
free_if_alloc (cfgfile); |
|
_snprintf (bak, DIM (bak)-1, "%s.O", optfile); |
|
|
CopyFile (optfile, bak, keep); |
|
|
free_if_alloc (optfile); |
|
918 |
} /* gnupg_backup_options */ |
} /* gnupg_backup_options */ |
919 |
|
|
920 |
|
|
979 |
} |
} |
980 |
|
|
981 |
|
|
982 |
|
static char* |
983 |
|
get_backup_name (const char *templ) |
984 |
|
{ |
985 |
|
struct tm *tm; |
986 |
|
char *p; |
987 |
|
|
988 |
|
time_t t = time (NULL); |
989 |
|
tm = localtime (&t); |
990 |
|
p = new char [strlen (templ) + 8 + 1]; |
991 |
|
if (!p) |
992 |
|
BUG (0); |
993 |
|
sprintf (p, "%s-%d", templ, tm->tm_wday % 3); |
994 |
|
return p; |
995 |
|
} |
996 |
|
|
997 |
|
|
998 |
void |
void |
999 |
gnupg_backup_keyrings (void) |
gnupg_backup_keyrings (void) |
1000 |
{ |
{ |
1001 |
char * srcpath = NULL, * dstpath = NULL; |
char *srcpath = NULL, *dstpath = NULL; |
1002 |
|
char *name=NULL; |
1003 |
int rc, bakmode=0; |
int rc, bakmode=0; |
1004 |
|
|
1005 |
if (!reg_prefs.auto_backup) |
if (!reg_prefs.auto_backup) |
1007 |
bakmode = reg_prefs.backup.mode; |
bakmode = reg_prefs.backup.mode; |
1008 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1009 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1010 |
if (bakmode == 1) |
if (bakmode == 1) { |
|
{ |
|
1011 |
dstpath = get_gnupg_path (); |
dstpath = get_gnupg_path (); |
1012 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1013 |
} |
} |
1014 |
else if (bakmode == 2) |
else if (bakmode == 2) { |
|
{ |
|
1015 |
char * tmpfile; |
char * tmpfile; |
1016 |
FILE * fp; |
FILE * fp; |
1017 |
|
|
1023 |
if (!fp) |
if (!fp) |
1024 |
rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, _("The backup drive '%s' does not seems to accessable.\n" |
rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, _("The backup drive '%s' does not seems to accessable.\n" |
1025 |
"Please insert/check the drive to continue."), dstpath); |
"Please insert/check the drive to continue."), dstpath); |
1026 |
else |
else { |
|
{ |
|
1027 |
rc = 0; |
rc = 0; |
1028 |
fclose (fp); |
fclose (fp); |
1029 |
unlink (tmpfile); |
unlink (tmpfile); |
1032 |
if (!fp || rc == IDCANCEL) |
if (!fp || rc == IDCANCEL) |
1033 |
return; |
return; |
1034 |
} |
} |
1035 |
else |
else { |
|
{ |
|
1036 |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
1037 |
return; |
return; |
1038 |
} |
} |
1039 |
rc = backup_one_file (srcpath, "pubring", dstpath, "pubring-bak"); |
name = get_backup_name ("pubring-bak"); |
1040 |
|
rc = backup_one_file (srcpath, "pubring", dstpath, name); |
1041 |
if (!rc) |
if (!rc) |
1042 |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
1043 |
|
free_if_alloc (name); |
1044 |
free_if_alloc (srcpath); |
free_if_alloc (srcpath); |
1045 |
free_if_alloc (dstpath); |
free_if_alloc (dstpath); |
1046 |
} /* gnupg_backup_keyrings */ |
} /* gnupg_backup_keyrings */ |