59 |
static char buf[256+64]; |
static char buf[256+64]; |
60 |
BOOL ec; |
BOOL ec; |
61 |
|
|
62 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
63 |
memset (buf, 0, sizeof (buf)); |
memset (buf, 0, sizeof (buf)); |
64 |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
65 |
if (ec != 1) |
if (ec != 1) |
67 |
strcat (buf, "\\gnupg"); |
strcat (buf, "\\gnupg"); |
68 |
if (access (buf, 00)) |
if (access (buf, 00)) |
69 |
return NULL; |
return NULL; |
70 |
return buf; |
return m_strdup (buf); |
71 |
} |
} |
72 |
|
|
73 |
/* |
/* |
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 */ |
} /* get_gnupg_path */ |
92 |
|
|
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; |
193 |
if (!sec) |
if (!sec) |
194 |
BUG (0); |
BUG (0); |
195 |
gpgme_keycache_rewind (sec); |
gpgme_keycache_rewind (sec); |
196 |
while (!gpgme_keycache_next_key (sec, 1, &key)) |
while (!gpgme_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 |
} |
} |
266 |
|
|
267 |
|
|
268 |
static int |
static int |
269 |
parse_version_nr( const char * buf, int *major, int *minor, int *patch ) |
parse_version_nr (const char * buf, int *major, int *minor, int *patch) |
270 |
{ |
{ |
271 |
char tmp[8]; |
char tmp[8]; |
272 |
int i; |
int i; |
273 |
|
|
|
if( strncmp( buf, "gpg ", 4 ) ) |
|
|
return -1; |
|
|
buf += 4; |
|
|
if( strncmp( buf, "(GnuPG) ", 8 ) ) |
|
|
return -1; |
|
|
buf += 8; |
|
274 |
i=0; |
i=0; |
275 |
while( buf && *buf != '.' && i < 8 ) |
while( buf && *buf != '.' && i < 8 ) |
276 |
tmp[i++] = *buf++; |
tmp[i++] = *buf++; |
293 |
int |
int |
294 |
check_gnupg_engine (int * r_major, int * r_minor, int * r_patch) |
check_gnupg_engine (int * r_major, int * r_minor, int * r_patch) |
295 |
{ |
{ |
296 |
gpgme_error_t err; |
gpgme_ctx_t ctx; |
297 |
|
gpgme_engine_info_t inf; |
298 |
char * eng = NULL; |
char * eng = NULL; |
299 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
300 |
int rc; |
int rc; |
301 |
|
|
302 |
err = gpgme_op_version( &eng ); |
gpgme_new (&ctx); |
303 |
if( err ) |
inf = gpgme_ctx_get_engine_info (ctx); |
304 |
|
if (!inf) { |
305 |
|
gpgme_release (ctx); |
306 |
|
return -1; |
307 |
|
} |
308 |
|
/* We need to exec GPG again to find out if IDEA is available. */ |
309 |
|
if (gpg_get_version (&eng)) |
310 |
return -1; |
return -1; |
311 |
if( strstr( eng, "IDEA" ) ) |
if (strstr (eng, "IDEA")) |
312 |
idea_available = 1; |
idea_available = 1; |
313 |
rc = parse_version_nr( eng, &major, &minor, &patch ); |
free (eng); |
314 |
free( eng ); eng = NULL; |
rc = parse_version_nr( inf->version, &major, &minor, &patch ); |
315 |
if( rc ) |
if( rc ) { |
316 |
|
gpgme_release (ctx); |
317 |
return rc; |
return rc; |
318 |
if( major < *r_major |
} |
319 |
|| minor < *r_minor) |
if (major < *r_major || minor < *r_minor) |
320 |
rc = 1; |
rc = 1; |
321 |
else { |
else { |
322 |
if (patch < *r_patch ) |
if (patch < *r_patch) |
323 |
rc = 1; |
rc = 1; |
324 |
rc = 0; |
rc = 0; |
325 |
} |
} |
434 |
* Return the contents of the options file as a char buf. |
* Return the contents of the options file as a char buf. |
435 |
*/ |
*/ |
436 |
char * |
char * |
437 |
get_gnupg_config( void ) |
get_gnupg_config (void) |
438 |
{ |
{ |
439 |
FILE * fp; |
FILE * fp; |
440 |
char * p = NULL, * optfile = NULL; |
char * p = NULL, * optfile = NULL; |
441 |
int fsize, rc = 0; |
int fsize, rc = 0; |
442 |
|
|
443 |
optfile = get_gnupg_cfgfile( ); |
optfile = get_gnupg_cfgfile (); |
444 |
if( optfile == NULL ) |
if( optfile == NULL ) |
445 |
return NULL; |
return NULL; |
446 |
fsize = get_file_size( optfile ); |
fsize = get_file_size( optfile ); |
899 |
|
|
900 |
|
|
901 |
void |
void |
902 |
gnupg_backup_options (int keep) |
gnupg_backup_options (void) |
903 |
{ |
{ |
904 |
char *optfile = NULL; |
char *cfgfile = NULL; |
905 |
char bak[1024]; |
char bak[512]; |
906 |
|
|
907 |
optfile = get_gnupg_cfgfile (); |
cfgfile = get_gnupg_cfgfile (); |
908 |
if (optfile == NULL) |
if (cfgfile == NULL) |
909 |
return; |
return; |
910 |
if (keep) |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
911 |
_snprintf (bak, DIM (bak)-1, "%s.old", optfile); |
CopyFile (cfgfile, bak, FALSE); |
912 |
else |
free_if_alloc (cfgfile); |
|
_snprintf (bak, DIM (bak)-1, "%s.O", optfile); |
|
|
CopyFile (optfile, bak, keep); |
|
|
free_if_alloc (optfile); |
|
913 |
} /* gnupg_backup_options */ |
} /* gnupg_backup_options */ |
914 |
|
|
915 |
|
|
974 |
} |
} |
975 |
|
|
976 |
|
|
977 |
|
static char* |
978 |
|
get_backup_name (const char *templ) |
979 |
|
{ |
980 |
|
struct tm *tm; |
981 |
|
char *p; |
982 |
|
|
983 |
|
time_t t = time (NULL); |
984 |
|
tm = localtime (&t); |
985 |
|
p = new char [strlen (templ) + 8 + 1]; |
986 |
|
if (!p) |
987 |
|
BUG (0); |
988 |
|
sprintf (p, "%s-%d", templ, tm->tm_wday % 3); |
989 |
|
return p; |
990 |
|
} |
991 |
|
|
992 |
|
|
993 |
void |
void |
994 |
gnupg_backup_keyrings (void) |
gnupg_backup_keyrings (void) |
995 |
{ |
{ |
996 |
char * srcpath = NULL, * dstpath = NULL; |
char *srcpath = NULL, *dstpath = NULL; |
997 |
|
char *name=NULL; |
998 |
int rc, bakmode=0; |
int rc, bakmode=0; |
999 |
|
|
1000 |
if (!reg_prefs.auto_backup) |
if (!reg_prefs.auto_backup) |
1002 |
bakmode = reg_prefs.backup.mode; |
bakmode = reg_prefs.backup.mode; |
1003 |
srcpath = get_gnupg_path (); |
srcpath = get_gnupg_path (); |
1004 |
check_keyring (&srcpath); |
check_keyring (&srcpath); |
1005 |
if (bakmode == 1) |
if (bakmode == 1) { |
|
{ |
|
1006 |
dstpath = get_gnupg_path (); |
dstpath = get_gnupg_path (); |
1007 |
check_keyring (&dstpath); |
check_keyring (&dstpath); |
1008 |
} |
} |
1009 |
else if (bakmode == 2) |
else if (bakmode == 2) { |
|
{ |
|
1010 |
char * tmpfile; |
char * tmpfile; |
1011 |
FILE * fp; |
FILE * fp; |
1012 |
|
|
1018 |
if (!fp) |
if (!fp) |
1019 |
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" |
1020 |
"Please insert/check the drive to continue."), dstpath); |
"Please insert/check the drive to continue."), dstpath); |
1021 |
else |
else { |
|
{ |
|
1022 |
rc = 0; |
rc = 0; |
1023 |
fclose (fp); |
fclose (fp); |
1024 |
unlink (tmpfile); |
unlink (tmpfile); |
1027 |
if (!fp || rc == IDCANCEL) |
if (!fp || rc == IDCANCEL) |
1028 |
return; |
return; |
1029 |
} |
} |
1030 |
else |
else { |
|
{ |
|
1031 |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
1032 |
return; |
return; |
1033 |
} |
} |
1034 |
rc = backup_one_file (srcpath, "pubring", dstpath, "pubring-bak"); |
name = get_backup_name ("pubring-bak"); |
1035 |
|
rc = backup_one_file (srcpath, "pubring", dstpath, name); |
1036 |
if (!rc) |
if (!rc) |
1037 |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
1038 |
|
free_if_alloc (name); |
1039 |
free_if_alloc (srcpath); |
free_if_alloc (srcpath); |
1040 |
free_if_alloc (dstpath); |
free_if_alloc (dstpath); |
1041 |
} /* gnupg_backup_keyrings */ |
} /* gnupg_backup_keyrings */ |