17 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
*/ |
20 |
|
#ifdef HAVE_CONFIG_H |
21 |
|
#include <config.h> |
22 |
|
#endif |
23 |
|
|
24 |
#include <windows.h> |
#include <windows.h> |
25 |
#include <string.h> |
#include <string.h> |
26 |
#include <stdio.h> |
#include <stdio.h> |
30 |
#include <time.h> |
#include <time.h> |
31 |
|
|
32 |
#include "wptGPG.h" |
#include "wptGPG.h" |
33 |
#include "wptGPGCmds.h" |
#include "wptGpgCmds.h" |
34 |
#include "wptGPGOptSkel.h" |
#include "wptGPGOptSkel.h" |
35 |
#include "wptTypes.h" |
#include "wptTypes.h" |
36 |
#include "wptNLS.h" |
#include "wptNLS.h" |
39 |
#include "wptW32API.h" |
#include "wptW32API.h" |
40 |
#include "wptCrypto.h" |
#include "wptCrypto.h" |
41 |
|
|
42 |
#define GPG_CONF "gpg.conf" |
#define GPG_CONF "gpg.conf" |
43 |
|
#define GPG_REG_EXE "gpgProgram" /* registry name for the binary. */ |
44 |
|
#define GPG_REG_HOME "HomeDir" /* registry name of the home dir. */ |
45 |
|
|
46 |
struct gpg_watcher_s { |
struct gpg_watcher_s { |
47 |
FILETIME last_access; |
FILETIME last_access; |
63 |
|
|
64 |
/* Return the application data folder of the current user. */ |
/* Return the application data folder of the current user. */ |
65 |
static char* |
static char* |
66 |
multi_gnupg_path (void) |
multi_gnupg_path (int strict) |
67 |
{ |
{ |
68 |
static char buf[256+64]; |
static char buf[256+64]; |
69 |
BOOL ec; |
BOOL ec; |
72 |
memset (buf, 0, sizeof (buf)); |
memset (buf, 0, sizeof (buf)); |
73 |
/* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */ |
/* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */ |
74 |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
75 |
if (ec != 1) |
if (ec != 1) { |
76 |
|
log_debug ("multi_gnupg_path: SHGetSpecialFolderPath() failed\r\n", |
77 |
|
(int)GetLastError ()); |
78 |
return NULL; |
return NULL; |
79 |
|
} |
80 |
strcat (buf, "\\gnupg"); |
strcat (buf, "\\gnupg"); |
81 |
if (access (buf, 00)) |
if (strict && access (buf, 00)) |
82 |
return NULL; |
return NULL; |
83 |
return m_strdup (buf); |
return m_strdup (buf); |
84 |
} |
} |
90 |
char* |
char* |
91 |
get_gnupg_path (void) |
get_gnupg_path (void) |
92 |
{ |
{ |
93 |
char *p = NULL, *path = NULL; |
char *p = NULL; |
94 |
|
char *path = NULL; |
95 |
|
|
96 |
p = get_reg_entry_gpg ("HomeDir"); |
p = get_reg_entry_gpg (GPG_REG_HOME); |
97 |
if (p) { |
if (p) { |
98 |
path = m_strdup (p); |
path = m_strdup (p); |
99 |
free_if_alloc (p); |
free_if_alloc (p); |
100 |
return path; |
return path; |
101 |
} |
} |
102 |
else |
else |
103 |
return multi_gnupg_path (); |
return multi_gnupg_path (1); |
104 |
return m_strdup ("c:\\gnupg"); |
return m_strdup ("c:\\gnupg"); |
105 |
} |
} |
106 |
|
|
110 |
char* |
char* |
111 |
get_gnupg_cfgfile (void) |
get_gnupg_cfgfile (void) |
112 |
{ |
{ |
113 |
char *p = NULL, *optfile = NULL, *path = NULL; |
char *p = NULL; |
114 |
|
char *optfile = NULL; |
115 |
|
char *path = NULL; |
116 |
size_t nlen = 0; |
size_t nlen = 0; |
117 |
|
|
118 |
path = get_gnupg_path (); |
path = get_gnupg_path (); |
163 |
free_if_alloc (path); |
free_if_alloc (path); |
164 |
return keyring; |
return keyring; |
165 |
} |
} |
166 |
if (file_exist_check (keyring) || get_file_size (keyring) == 0) { |
if (file_exist_check (keyring) || pub && get_file_size (keyring) == 0) { |
167 |
free_if_alloc (keyring); |
free_if_alloc (keyring); |
168 |
optfile = make_filename (path, GPG_CONF, NULL); |
optfile = make_filename (path, GPG_CONF, NULL); |
169 |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
177 |
/* 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 |
178 |
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 |
179 |
appended string 'gpg.exe' is used. */ |
appended string 'gpg.exe' is used. */ |
180 |
|
|
181 |
|
/* FIXME: Use gpgme's engine info here. */ |
182 |
char* |
char* |
183 |
get_gnupg_prog (void) |
get_gnupg_prog (void) |
184 |
{ |
{ |
185 |
char *p; |
char *p; |
186 |
char *pgm = NULL; |
char *pgm = NULL; |
|
size_t nlen = 0; |
|
187 |
|
|
188 |
p = get_reg_entry_gpg ("gpgProgram"); |
p = get_reg_entry_gpg (GPG_REG_EXE); |
189 |
if (!p) { |
if (!p) { |
190 |
char *path = get_gnupg_path (); |
char *path = get_gnupg_path (); |
191 |
if (!path) |
if (!path) |
207 |
static char * |
static char * |
208 |
default_key_from_cache (int *ret_no_useable) |
default_key_from_cache (int *ret_no_useable) |
209 |
{ |
{ |
210 |
const char * s; |
const char *s; |
211 |
char * keyid = NULL; |
char *keyid = NULL; |
212 |
gpgme_key_t key; |
gpgme_key_t key; |
213 |
gpg_keycache_t sec = keycache_get_ctx (0); |
gpg_keycache_t sec = keycache_get_ctx (0); |
214 |
|
|
223 |
break; |
break; |
224 |
} |
} |
225 |
} |
} |
226 |
if (!keyid) { |
if (!keyid) |
227 |
*ret_no_useable = 1; |
*ret_no_useable = 1; |
|
msg_box (NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
|
|
} |
|
228 |
return keyid; |
return keyid; |
229 |
} |
} |
230 |
|
|
268 |
return default_key_from_cache (&no_usable); |
return default_key_from_cache (&no_usable); |
269 |
rc = parse_gpg_options (optfile, &opt); |
rc = parse_gpg_options (optfile, &opt); |
270 |
if (rc) { |
if (rc) { |
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 ) |
293 |
} /* get_gnupg_default_key */ |
} /* get_gnupg_default_key */ |
294 |
|
|
295 |
|
|
296 |
|
char* get_reg_entry_gpg4win (const char *path); |
297 |
|
|
298 |
|
/* Check if GPG4WIN is available and if so, use the |
299 |
|
install path to figure out where the gpg.exe is. */ |
300 |
|
char* |
301 |
|
check_for_gpg4win (void) |
302 |
|
{ |
303 |
|
return get_reg_entry_gpg4win ("gpg.exe"); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
/* Check if the gpg application (exe file) is available. */ |
/* Check if the gpg application (exe file) is available. */ |
308 |
int |
int |
309 |
check_gnupg_prog (void) |
check_gnupg_prog (void) |
310 |
{ |
{ |
311 |
char *pgm = NULL; |
char *gpgexe = NULL; |
312 |
int rc = 0; |
int rc = 0; |
313 |
|
|
314 |
pgm = get_gnupg_prog (); |
gpgexe = get_gnupg_prog (); |
315 |
if (!pgm) |
if (!gpgexe || file_exist_check (gpgexe)) { |
316 |
rc = WPTERR_GPG_EXEFILE; |
free_if_alloc (gpgexe); |
317 |
if (file_exist_check (pgm)) |
gpgexe = check_for_gpg4win (); |
318 |
rc = WPTERR_GPG_EXEFILE; |
if (!gpgexe || file_exist_check (gpgexe)) |
319 |
free_if_alloc (pgm); |
rc = WPTERR_GPG_EXEFILE; |
320 |
|
else |
321 |
|
set_reg_entry_gpg (GPG_REG_EXE, gpgexe); |
322 |
|
} |
323 |
|
free_if_alloc (gpgexe); |
324 |
return rc; |
return rc; |
325 |
} |
} |
326 |
|
|
379 |
gpgme_release (ctx); |
gpgme_release (ctx); |
380 |
return rc; |
return rc; |
381 |
} |
} |
382 |
|
/* FIXME: This check is wrong! */ |
383 |
if (major < *r_major || minor < *r_minor) |
if (major < *r_major || minor < *r_minor) |
384 |
rc = 1; |
rc = 1; |
385 |
else { |
else { |
442 |
rc = WPTERR_GPG_KEYRINGS; |
rc = WPTERR_GPG_KEYRINGS; |
443 |
else |
else |
444 |
secring_ok = 1; |
secring_ok = 1; |
445 |
|
|
446 |
if (!pubring_ok || !secring_ok) { |
if (!pubring_ok || !secring_ok) { |
447 |
optfile = get_gnupg_cfgfile (); |
optfile = get_gnupg_cfgfile (); |
448 |
if (!optfile) |
if (!optfile) |
449 |
return WPTERR_GPG_KEYRINGS; |
return WPTERR_GPG_KEYRINGS; |
450 |
rc = file_exist_check (optfile); |
rc = file_exist_check (optfile); |
451 |
if (!rc && get_file_size(optfile) > 0) { |
if (!rc && get_file_size (optfile) > 0) { |
452 |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
453 |
if (!rc && secrings && pubrings) { |
if (!rc && secrings && pubrings) { |
454 |
free_if_alloc (optfile); |
free_if_alloc (optfile); |
475 |
FILE *fp; |
FILE *fp; |
476 |
char *s, *optfile; |
char *s, *optfile; |
477 |
|
|
478 |
s = get_gnupg_path( ); |
s = get_gnupg_path (); |
479 |
if( s == NULL ) |
if( s == NULL ) |
480 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
481 |
optfile = make_filename( s, GPG_CONF, NULL ); |
optfile = make_filename (s, GPG_CONF, NULL); |
482 |
fp = fopen( optfile, "wb" ); |
fp = fopen( optfile, "wb" ); |
483 |
if( fp == NULL ) { |
if( fp == NULL ) { |
484 |
return WPTERR_FILE_CREAT; |
return WPTERR_FILE_CREAT; |
843 |
check_homedir (void) |
check_homedir (void) |
844 |
{ |
{ |
845 |
char *homedir = NULL; |
char *homedir = NULL; |
846 |
int yes = 0; |
int yes = 0, set_reg=0; |
847 |
|
int rc = 0; |
848 |
|
|
849 |
homedir = get_reg_entry_gpg ("HomeDir"); |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
850 |
if (!homedir) |
if (!homedir) { |
851 |
homedir = multi_gnupg_path (); |
set_reg = 1; |
852 |
|
homedir = multi_gnupg_path (0); |
853 |
|
} |
854 |
if (!homedir) |
if (!homedir) |
855 |
homedir = m_strdup ("c:\\gnupg"); |
homedir = m_strdup ("c:\\gnupg"); |
856 |
if (homedir) { |
if (homedir) { |
859 |
_("%s does not exit.\n" |
_("%s does not exit.\n" |
860 |
"Do you want to create this directory?"), homedir); |
"Do you want to create this directory?"), homedir); |
861 |
if (yes == IDYES) { |
if (yes == IDYES) { |
862 |
BOOL ec = CreateDirectory (homedir, NULL); |
if (!CreateDirectory (homedir, NULL)) |
863 |
free_if_alloc (homedir); |
rc = WPTERR_DIR_CREAT; |
|
if (ec == FALSE) |
|
|
return WPTERR_DIR_CREAT; |
|
|
return 0; |
|
864 |
} |
} |
865 |
return WPTERR_DIR_OPEN; |
else |
866 |
|
rc = WPTERR_DIR_OPEN; |
867 |
} |
} |
868 |
|
if (set_reg) |
869 |
|
set_reg_entry_gpg (GPG_REG_HOME, homedir); |
870 |
free_if_alloc (homedir); |
free_if_alloc (homedir); |
871 |
} |
} |
872 |
return 0; |
return rc; |
873 |
} |
} |
874 |
|
|
875 |
|
|
878 |
{ |
{ |
879 |
char *homedir = NULL; |
char *homedir = NULL; |
880 |
char *prog = NULL; |
char *prog = NULL; |
881 |
int rc = 0, ec = 0; |
int rc = 0; |
882 |
|
|
883 |
rc = check_homedir (); |
rc = check_homedir (); |
884 |
if (rc) |
if (rc) |
885 |
return rc; |
return rc; |
886 |
if ((homedir = get_reg_entry_gpg ("HomeDir")) && |
if ((homedir = get_reg_entry_gpg (GPG_REG_HOME)) && |
887 |
!(prog = get_reg_entry_gpg ("gpgProgram" ))) { |
!(prog = get_reg_entry_gpg (GPG_REG_EXE ))) { |
888 |
prog = make_filename (homedir, "gpg", "exe"); |
prog = make_filename (homedir, "gpg", "exe"); |
889 |
if (file_exist_check (prog) == 0) { |
if (file_exist_check (prog) == 0) { |
890 |
rc = set_reg_entry_gpg ("gpgProgram", prog); |
rc = set_reg_entry_gpg (GPG_REG_EXE, prog); |
891 |
if (rc) |
if (rc) |
892 |
goto fail; |
goto fail; |
893 |
} |
} |
895 |
free_if_alloc (prog); |
free_if_alloc (prog); |
896 |
return rc; |
return rc; |
897 |
} |
} |
898 |
if ((prog = get_reg_entry_gpg ("gpgProgram")) |
if ((prog = get_reg_entry_gpg (GPG_REG_EXE)) |
899 |
&& file_exist_check (prog)) { |
&& file_exist_check (prog)) { |
900 |
free_if_alloc (prog); |
free_if_alloc (prog); |
901 |
homedir = get_reg_entry_gpg ("HomeDir"); |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
902 |
if (!homedir) { |
if (!homedir) { |
903 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
904 |
goto fail; |
goto fail; |
905 |
} |
} |
906 |
prog = make_filename (homedir, "gpg", "exe"); |
prog = make_filename (homedir, "gpg", "exe"); |
907 |
if (file_exist_check (prog) == 0) { |
if (file_exist_check (prog) == 0) { |
908 |
rc = set_reg_entry_gpg ("gpgProgram", prog); |
rc = set_reg_entry_gpg (GPG_REG_EXE, prog); |
909 |
if (rc) |
if (rc) |
910 |
goto fail; |
goto fail; |
911 |
free_if_alloc (prog); |
free_if_alloc (prog); |
1106 |
else { |
else { |
1107 |
rc = 0; |
rc = 0; |
1108 |
fclose (fp); |
fclose (fp); |
1109 |
unlink (tmpfile); |
remove (tmpfile); |
1110 |
} |
} |
1111 |
free_if_alloc (tmpfile); |
free_if_alloc (tmpfile); |
1112 |
if (!fp || rc == IDCANCEL) |
if (!fp || rc == IDCANCEL) |