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 "wptGpgCmds.h" |
#include "wptGPGME.h" |
30 |
#include "wptGPGOptSkel.h" |
#include "wptGPGCmds.h" |
31 |
#include "wptTypes.h" |
#include "wptTypes.h" |
32 |
#include "wptNLS.h" |
#include "wptNLS.h" |
33 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
64 |
multi_gnupg_path (int strict) |
multi_gnupg_path (int strict) |
65 |
{ |
{ |
66 |
static char buf[MAX_PATH+64]; |
static char buf[MAX_PATH+64]; |
|
BOOL ec; |
|
67 |
|
|
68 |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
69 |
memset (buf, 0, sizeof (buf)); |
memset (buf, 0, sizeof (buf)); |
70 |
/* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */ |
if (!SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE)) { |
71 |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
log_debug ("multi_gnupg_path: SHGetSpecialFolderPath() failed with: %d\r\n", |
|
if (ec != 1) { |
|
|
log_debug ("multi_gnupg_path: SHGetSpecialFolderPath() failed\r\n", |
|
72 |
(int)GetLastError ()); |
(int)GetLastError ()); |
73 |
return NULL; |
return NULL; |
74 |
} |
} |
84 |
char* |
char* |
85 |
get_gnupg_path (void) |
get_gnupg_path (void) |
86 |
{ |
{ |
87 |
char *path; |
char *path = get_reg_entry_gpg (GPG_REG_HOME); |
|
|
|
|
path = get_reg_entry_gpg (GPG_REG_HOME); |
|
88 |
if (path && dir_exist_check (path) == 0) |
if (path && dir_exist_check (path) == 0) |
89 |
return path; |
return path; |
90 |
free_if_alloc (path); |
free_if_alloc (path); |
97 |
char* |
char* |
98 |
get_gnupg_cfgfile (void) |
get_gnupg_cfgfile (void) |
99 |
{ |
{ |
100 |
char *optfile; |
char *path = get_gnupg_path (); |
|
char *path; |
|
|
|
|
|
path = get_gnupg_path (); |
|
101 |
if (!path) |
if (!path) |
102 |
return NULL; |
return NULL; |
103 |
optfile = make_filename (path, GPG_CONF, NULL); |
char *optfile = make_filename (path, GPG_CONF, NULL); |
104 |
free_if_alloc (path); |
free_if_alloc (path); |
105 |
|
|
106 |
return optfile; |
return optfile; |
113 |
config_file_t opt; |
config_file_t opt; |
114 |
conf_option_t e; |
conf_option_t e; |
115 |
char *kring = NULL; |
char *kring = NULL; |
|
int rc; |
|
116 |
|
|
117 |
rc = parse_config (fname, &opt); |
if (parse_config (fname, &opt)) |
|
if (rc) |
|
118 |
return NULL; |
return NULL; |
119 |
if (pub) |
if (pub) |
120 |
e = conf_find_option (opt, "keyring"); |
e = conf_find_option (opt, "keyring"); |
150 |
return keyring; |
return keyring; |
151 |
} |
} |
152 |
if (file_exist_check (keyring) || |
if (file_exist_check (keyring) || |
153 |
pub && get_file_size (keyring) == 0) { |
(pub && get_file_size (keyring) == 0)) { |
154 |
free_if_alloc (keyring); |
free_if_alloc (keyring); |
155 |
optfile = make_filename (path, GPG_CONF, NULL); |
optfile = make_filename (path, GPG_CONF, NULL); |
156 |
keyring = get_keyring_from_conf (optfile, pub); |
keyring = get_keyring_from_conf (optfile, pub); |
323 |
|
|
324 |
static int |
static int |
325 |
parse_version_nr (const char *buf, int *major, int *minor, int *patch) |
parse_version_nr (const char *buf, int *major, int *minor, int *patch) |
326 |
{ |
{ |
327 |
char tmp[8]; |
char *p; |
328 |
int i; |
char *tmp = m_strdup(buf); |
329 |
|
|
330 |
i=0; |
int pos = 0; |
331 |
while (buf && *buf != '.' && i < 8) |
while ((p = strsep(&tmp, ".")) != NULL) { |
332 |
tmp[i++] = *buf++; |
switch (++pos) { |
333 |
tmp[i] = 0; buf++; |
case 1: *major = atoi (p); break; |
334 |
*major = atoi (tmp); |
case 2: *minor = atoi (p); break; |
335 |
i=0; |
case 3: *patch = atoi (p); break; |
336 |
while (buf && *buf != '.' && i < 8) |
} |
337 |
tmp[i++] = *buf++; |
} |
338 |
tmp[i] = 0; buf++; |
delete[] tmp; |
339 |
*minor = atoi (tmp); |
if (pos != 3) |
340 |
i=0; |
return -1; |
|
while (buf && isdigit (*buf) && i < 8) |
|
|
tmp[i++] = *buf++; |
|
|
tmp[i] = 0; |
|
|
*patch = atoi (tmp); |
|
341 |
return 0; |
return 0; |
342 |
} |
} |
343 |
|
|
349 |
check_gnupg_engine (const char *need_gpg_ver, |
check_gnupg_engine (const char *need_gpg_ver, |
350 |
int *r_major, int *r_minor, int *r_patch) |
int *r_major, int *r_minor, int *r_patch) |
351 |
{ |
{ |
|
gpgme_ctx_t ctx; |
|
352 |
gpgme_engine_info_t inf; |
gpgme_engine_info_t inf; |
353 |
char *eng = NULL; |
char *eng = NULL; |
354 |
int major=0, minor=0, patch=0; |
int major=0, minor=0, patch=0; |
360 |
&need_major, &need_minor, &need_patch)) |
&need_major, &need_minor, &need_patch)) |
361 |
return 1; |
return 1; |
362 |
|
|
363 |
gpgme_new (&ctx); |
if (gpgme_get_engine_info (&inf)) |
364 |
inf = gpgme_ctx_get_engine_info (ctx); |
return -1; |
|
if (!inf) { |
|
|
gpgme_release (ctx); |
|
|
return -1; |
|
|
} |
|
365 |
|
|
366 |
/* 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. */ |
367 |
if (gpg_get_version (&eng)) { |
if (gpg_get_version (&eng)) |
368 |
gpgme_release (ctx); |
return -1; |
|
return -1; |
|
|
} |
|
369 |
if (strstr (eng, "IDEA")) |
if (strstr (eng, "IDEA")) |
370 |
idea_available = 1; |
idea_available = 1; |
371 |
safe_free (eng); |
safe_free (eng); |
372 |
if (parse_version_nr (inf->version, &major, &minor, &patch)) { |
|
373 |
gpgme_release (ctx); |
if (parse_version_nr (inf->version, &major, &minor, &patch)) |
374 |
return 1; |
return 1; |
|
} |
|
|
gpgme_release (ctx); |
|
375 |
|
|
376 |
if (major > need_major) |
if (major > need_major) |
377 |
rc = 0; |
rc = 0; |
423 |
static void |
static void |
424 |
create_empty_keyring (int _pub) |
create_empty_keyring (int _pub) |
425 |
{ |
{ |
426 |
char *name; |
char *name = get_gnupg_keyring (_pub, 0); |
|
FILE *fp; |
|
|
|
|
|
name = get_gnupg_keyring (_pub, 0); |
|
427 |
if (name && file_exist_check (name) != 0) { |
if (name && file_exist_check (name) != 0) { |
428 |
fp = fopen (name, "ab"); |
FILE *fp = fopen (name, "ab"); |
429 |
if (fp != NULL) |
if (fp != NULL) |
430 |
fclose (fp); |
fclose (fp); |
431 |
} |
} |
480 |
} |
} |
481 |
|
|
482 |
|
|
|
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; |
|
|
} |
|
|
|
|
483 |
|
|
484 |
/* Return the contents of the options file as a char buf. */ |
/* Return the contents of the options file as a char buf. */ |
485 |
char* |
char* |
486 |
get_gnupg_config (void) |
get_gnupg_config (void) |
487 |
{ |
{ |
488 |
FILE *fp; |
FILE *fp; |
489 |
char *p = NULL, *optfile = NULL; |
char *p = NULL, *optfile; |
490 |
int fsize; |
int fsize; |
491 |
|
|
492 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
493 |
if (!optfile) |
if (!optfile) |
494 |
return NULL; |
return NULL; |
495 |
fsize = get_file_size (optfile); |
fsize = get_file_size (optfile); |
496 |
if (!fsize) { |
if (fsize < 1 || fsize > 100000) |
497 |
if (create_gpg_conf ()) |
goto leave; /* too large or does not exist */ |
498 |
return NULL; |
|
499 |
fsize = get_file_size (optfile); |
fp = fopen (optfile, "rb"); |
500 |
} |
if (!fp) |
501 |
if (fsize > 100000) |
goto leave; |
|
goto leave; /* too large */ |
|
502 |
p = new char[fsize+1]; |
p = new char[fsize+1]; |
503 |
if (!p) |
if (!p) |
504 |
BUG (NULL); |
BUG (NULL); |
|
fp = fopen( optfile, "rb" ); |
|
|
if (!fp) { |
|
|
free_if_alloc (p); |
|
|
return NULL; |
|
|
} |
|
505 |
fread (p, 1, fsize, fp); |
fread (p, 1, fsize, fp); |
506 |
fclose (fp); |
fclose (fp); |
507 |
p[fsize] = '\0'; |
p[fsize] = '\0'; |
508 |
free_if_alloc (optfile); |
|
|
|
|
509 |
leave: |
leave: |
510 |
|
free_if_alloc (optfile); |
511 |
return p; |
return p; |
512 |
} |
} |
513 |
|
|
691 |
void |
void |
692 |
free_gnupg_table (void) |
free_gnupg_table (void) |
693 |
{ |
{ |
694 |
int j; |
for (int j=0; j < gpg_table_count; j++) { |
|
|
|
|
for (j=0; j < gpg_table_count; j++) { |
|
695 |
free_if_alloc (gpg_table[j].object); |
free_if_alloc (gpg_table[j].object); |
696 |
free_if_alloc (gpg_table[j].fpath_object); |
free_if_alloc (gpg_table[j].fpath_object); |
697 |
} |
} |
703 |
keyring_check_last_access (void) |
keyring_check_last_access (void) |
704 |
{ |
{ |
705 |
int nfiles; |
int nfiles; |
|
int pos; |
|
706 |
|
|
707 |
nfiles = 0; |
nfiles = 0; |
708 |
for (pos = 0; pos < gpg_table_count; pos++) { |
for (int pos = 0; pos < gpg_table_count; pos++) { |
709 |
get_last_gnupg_access (&gpg_table[pos]); |
get_last_gnupg_access (&gpg_table[pos]); |
710 |
check_last_gnupg_access (&gpg_table[pos]); |
check_last_gnupg_access (&gpg_table[pos]); |
711 |
if (gpg_table[pos].modified) |
if (gpg_table[pos].modified) |
774 |
case 0: api_mode = FILE_SHARE_READ; break; |
case 0: api_mode = FILE_SHARE_READ; break; |
775 |
case 1: api_mode = FILE_SHARE_WRITE; break; |
case 1: api_mode = FILE_SHARE_WRITE; break; |
776 |
case 2: api_mode = FILE_SHARE_WRITE|FILE_SHARE_READ; break; |
case 2: api_mode = FILE_SHARE_WRITE|FILE_SHARE_READ; break; |
777 |
|
default: api_mode = FILE_SHARE_READ; break; |
778 |
} |
} |
779 |
|
|
780 |
return check_file_access (fname, api_mode); |
return check_file_access (fname, api_mode); |
841 |
int val; |
int val; |
842 |
int rc = 0; |
int rc = 0; |
843 |
|
|
|
#ifdef WINPT_MOBILE |
|
|
return 0; |
|
|
#endif |
|
|
|
|
844 |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
845 |
if (!homedir) |
if (!homedir) |
846 |
homedir = multi_gnupg_path (0); |
homedir = multi_gnupg_path (0); |
863 |
|
|
864 |
|
|
865 |
int |
int |
866 |
|
gnupg_import_keypair (void) |
867 |
|
{ |
868 |
|
const char *file; |
869 |
|
gpgme_error_t err; |
870 |
|
GPGME gpgme; |
871 |
|
HWND hwnd = GetDesktopWindow (); |
872 |
|
|
873 |
|
file = get_fileopen_dlg (hwnd, _("Please choose your Key Pair"), |
874 |
|
"GPG Key File (*.gpg)\0*.gpg\0" |
875 |
|
"GPG Armored Key File (*.asc)\0*.asc\0\0", |
876 |
|
NULL); |
877 |
|
if (file == NULL) |
878 |
|
return WPTERR_FILE_OPEN; |
879 |
|
err = gpgme.importFromFile (file); |
880 |
|
if (err) |
881 |
|
msg_box (hwnd, gpgme_strerror (err), _("WinPT Error"), MB_ERR); |
882 |
|
else |
883 |
|
msg_box (hwnd, _("Key pair successfully imported."), "WinPT", MB_OK); |
884 |
|
return 0; |
885 |
|
} |
886 |
|
|
887 |
|
int |
888 |
gnupg_copy_keyrings (void) |
gnupg_copy_keyrings (void) |
889 |
{ |
{ |
890 |
const char *pring, *sring; |
const char *pring, *sring; |
891 |
char *file = NULL, *path = NULL; |
char *file = NULL, *path; |
892 |
int id = 0, rc = 0; |
int id, rc = 0; |
893 |
HWND hwnd; |
HWND hwnd; |
894 |
|
|
895 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
898 |
hwnd = GetDesktopWindow (); |
hwnd = GetDesktopWindow (); |
899 |
|
|
900 |
pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"), |
pring = get_fileopen_dlg (hwnd, _("Please choose your Public Keyring"), |
901 |
"GPG Keyrings (*.gpg)\0*.gpg\0\0",NULL); |
"GPG Keyrings (*.gpg)\0*.gpg\0\0", NULL); |
902 |
if (!pring) { |
if (!pring) { |
903 |
msg_box (hwnd, _("No keyring was chosen. Exit."), |
msg_box (hwnd, _("No keyring was chosen. Exit."), |
904 |
_("WinPT Error"), MB_ERR); |
_("WinPT Error"), MB_ERR); |
905 |
free_if_alloc (path); |
free_if_alloc (path); |
906 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
907 |
} |
} |
908 |
file = make_filename (path, "pubring", "gpg"); |
file = make_filename (path, "pubring", "gpg"); |
909 |
if (file_exist_check (file) == 0) { |
if (file_exist_check (file) == 0) { |
910 |
id = msg_box (hwnd, _("Overwrite old public keyring?"), |
id = msg_box (hwnd, _("Overwrite old public keyring?"), |
911 |
"WinPT", MB_INFO|MB_YESNO); |
"WinPT", MB_INFO|MB_YESNO); |
912 |
if (id == IDNO) |
if (id == IDNO) |
913 |
goto fail; |
goto fail; |
914 |
} |
} |
915 |
if (!CopyFile (pring, file, FALSE)) { |
if (!CopyFile (pring, file, FALSE)) { |
916 |
msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
msg_box (hwnd, _("Could not copy public keyring."), |
917 |
|
_("WinPT Error"), MB_ERR); |
918 |
rc = WPTERR_FILE_READ; |
rc = WPTERR_FILE_READ; |
919 |
goto fail; |
goto fail; |
920 |
} |
} |
935 |
goto fail; |
goto fail; |
936 |
} |
} |
937 |
if (!CopyFile (sring, file, FALSE)) { |
if (!CopyFile (sring, file, FALSE)) { |
938 |
msg_box (NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
msg_box (NULL, _("Could not copy secret keyring."), _("WinPT Error"), MB_ERR); |
939 |
rc = WPTERR_FILE_READ; |
rc = WPTERR_FILE_READ; |
940 |
} |
} |
941 |
|
|
950 |
void |
void |
951 |
gnupg_backup_options (void) |
gnupg_backup_options (void) |
952 |
{ |
{ |
|
char *cfgfile; |
|
953 |
char bak[MAX_PATH+32]; |
char bak[MAX_PATH+32]; |
954 |
|
|
955 |
cfgfile = get_gnupg_cfgfile (); |
char *cfgfile = get_gnupg_cfgfile (); |
956 |
if (!cfgfile) |
if (!cfgfile) |
957 |
return; |
return; |
958 |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |