1 |
/* wptGPG.cpp - GnuPG configuration |
/* wptGPG.cpp - GnuPG configuration |
2 |
* Copyright (C) 2001-2004 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
10 |
* |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
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. |
15 |
* |
* |
16 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
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 <string.h> |
#include <config.h> |
22 |
#include <stdio.h> |
#endif |
23 |
#include <windows.h> |
|
24 |
#include <shlobj.h> |
#include <windows.h> |
25 |
#include <ctype.h> |
#include <string.h> |
26 |
#include <io.h> |
#include <stdio.h> |
27 |
|
#include <shlobj.h> |
28 |
#include "wptGPG.h" |
#include <ctype.h> |
29 |
#include "wptGPGCmds.h" |
#include <io.h> |
30 |
#include "wptGPGOptSkel.h" |
#include <time.h> |
31 |
#include "wptTypes.h" |
|
32 |
#include "wptNLS.h" |
#include "wptGPG.h" |
33 |
#include "wptRegistry.h" |
#include "wptGpgCmds.h" |
34 |
#include "wptErrors.h" |
#include "wptGPGOptSkel.h" |
35 |
#include "wptW32API.h" |
#include "wptTypes.h" |
36 |
|
#include "wptNLS.h" |
37 |
#define GPG_CONF "gpg.conf" |
#include "wptRegistry.h" |
38 |
|
#include "wptErrors.h" |
39 |
struct gpg_watcher_s { |
#include "wptW32API.h" |
40 |
FILETIME last_access; |
#include "wptCrypto.h" |
41 |
FILETIME access; |
|
42 |
char * object; |
#define GPG_CONF "gpg.conf" |
43 |
int modified; |
#define GPG_REG_EXE "gpgProgram" /* registry name for the binary. */ |
44 |
}; |
#define GPG_REG_HOME "HomeDir" /* registry name of the home dir. */ |
45 |
|
|
46 |
/* XXX need to watch for gpg.conf due to the fact keyring entries could be changed */ |
struct gpg_watcher_s { |
47 |
static const char * gpg_objs[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg"}; |
FILETIME last_access; |
48 |
static gpg_watcher_s gpg_table[3]; |
FILETIME access; |
49 |
static int gpg_table_count = DIM (gpg_table); |
char *object; |
50 |
|
int modified; |
51 |
int idea_available = 0; |
}; |
52 |
|
|
53 |
static int check_keyring (char ** r_path); |
|
54 |
|
/* XXX need to watch for gpg.conf due to the fact keyring entries could be changed */ |
55 |
|
static const char * gpg_objs[] = {"pubring.gpg", "secring.gpg", "trustdb.gpg"}; |
56 |
static char* |
static gpg_watcher_s gpg_table[3]; |
57 |
multi_gnupg_path (void) |
static int gpg_table_count = DIM (gpg_table); |
58 |
{ |
|
59 |
static char buf[256+64]; |
int idea_available = 0; |
60 |
BOOL ec; |
|
61 |
|
static int check_keyring (char ** r_path); |
62 |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
|
63 |
memset (buf, 0, sizeof (buf)); |
|
64 |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
/* Return the application data folder of the current user. */ |
65 |
if (ec != 1) |
static char* |
66 |
return NULL; |
multi_gnupg_path (int strict) |
67 |
strcat (buf, "\\gnupg"); |
{ |
68 |
if (access (buf, 00)) |
static char buf[256+64]; |
69 |
return NULL; |
BOOL ec; |
70 |
return m_strdup (buf); |
|
71 |
} |
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
72 |
|
memset (buf, 0, sizeof (buf)); |
73 |
/* |
/* XXX: ec should be NOERROR (MSDN) but NOERROR is defined as '0' !? */ |
74 |
* Return the full path of the GnuPG application. First the registry is scanned |
ec = SHGetSpecialFolderPath (HWND_DESKTOP, buf, CSIDL_APPDATA, TRUE); |
75 |
* for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
if (ec != 1) { |
76 |
*/ |
log_debug ("multi_gnupg_path: SHGetSpecialFolderPath() failed\r\n", |
77 |
char* |
(int)GetLastError ()); |
78 |
get_gnupg_path (void) |
return NULL; |
79 |
{ |
} |
80 |
char *p = NULL, *path = NULL; |
strcat (buf, "\\gnupg"); |
81 |
|
if (strict && access (buf, 00)) |
82 |
p = get_reg_entry_gpg ("HomeDir"); |
return NULL; |
83 |
if (p) { |
return m_strdup (buf); |
84 |
path = m_strdup (p); |
} |
85 |
free_if_alloc (p); |
|
86 |
return path; |
|
87 |
} |
/* Return the full path of the GnuPG application. First the registry is scanned |
88 |
else |
for the entry 'HomeDir'. If it wasn't set, the default dir C:\GNUPG is used. |
89 |
return multi_gnupg_path (); |
*/ |
90 |
return m_strdup ("c:\\gnupg"); |
char* |
91 |
} /* get_gnupg_path */ |
get_gnupg_path (void) |
92 |
|
{ |
93 |
|
char *p = NULL; |
94 |
char* |
char *path = NULL; |
95 |
get_gnupg_cfgfile (void) |
|
96 |
{ |
p = get_reg_entry_gpg (GPG_REG_HOME); |
97 |
char *p = NULL, *optfile = NULL, *path = NULL; |
if (p) { |
98 |
size_t nlen = 0; |
path = m_strdup (p); |
99 |
|
free_if_alloc (p); |
100 |
path = get_gnupg_path (); |
return path; |
101 |
if (path == NULL) |
} |
102 |
return NULL; |
else |
103 |
p = get_reg_entry_gpg ("OptFile"); |
return multi_gnupg_path (1); |
104 |
if (p && !strcmp (p, "")) |
return m_strdup ("c:\\gnupg"); |
105 |
{ |
} |
106 |
nlen = strlen (path) + 64; |
|
107 |
optfile = new char[nlen + 1]; |
|
108 |
if (optfile == NULL) |
/* Return the full path of the gpg config file. |
109 |
BUG (0); |
A value of NULL indicates an error. */ |
110 |
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
char* |
111 |
} |
get_gnupg_cfgfile (void) |
112 |
else if( p ) { |
{ |
113 |
nlen = strlen( p ) + 4; |
char *p = NULL; |
114 |
optfile = new char[nlen + 1]; |
char *optfile = NULL; |
115 |
if( optfile == NULL ) |
char *path = NULL; |
116 |
BUG( NULL ); |
size_t nlen = 0; |
117 |
_snprintf( optfile, nlen, "%s", p ); |
|
118 |
} |
path = get_gnupg_path (); |
119 |
else { |
if (!path) |
120 |
nlen = strlen( path ) + 64; |
return NULL; |
121 |
optfile = new char[nlen + 1]; |
p = get_reg_entry_gpg ("OptFile"); |
122 |
if( optfile == NULL ) |
if (p && !strcmp (p, "")) { |
123 |
BUG( NULL ); |
nlen = strlen (path) + 64; |
124 |
_snprintf( optfile, nlen, "%s\\"GPG_CONF, path ); |
optfile = new char[nlen + 1]; |
125 |
} |
if (!optfile) |
126 |
free_if_alloc (path); |
BUG (0); |
127 |
free_if_alloc (p); |
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
128 |
|
} |
129 |
return optfile; |
else if (p) { |
130 |
} /* get_gnupg_cfgfile */ |
nlen = strlen( p ) + 4; |
131 |
|
optfile = new char[nlen + 1]; |
132 |
|
if (!optfile) |
133 |
char * |
BUG (NULL); |
134 |
get_gnupg_keyring (int pub, int strict) |
_snprintf (optfile, nlen, "%s", p); |
135 |
{ |
} |
136 |
char * optfile = NULL, * path = NULL; |
else { |
137 |
char * keyring = NULL; |
nlen = strlen (path) + 64; |
138 |
|
optfile = new char[nlen + 1]; |
139 |
path = get_gnupg_path (); |
if( !optfile) |
140 |
if (!path) |
BUG (NULL); |
141 |
return NULL; |
_snprintf (optfile, nlen, "%s\\"GPG_CONF, path); |
142 |
keyring = make_filename (path, pub? "pubring" : "secring", "gpg"); |
} |
143 |
if (!strict && !file_exist_check (keyring)) { |
free_if_alloc (path); |
144 |
free_if_alloc (path); |
free_if_alloc (p); |
145 |
return keyring; |
return optfile; |
146 |
} |
} |
147 |
if (file_exist_check (keyring) || get_file_size (keyring) == 0) { |
|
148 |
free_if_alloc (keyring); |
|
149 |
optfile = make_filename (path, GPG_CONF, NULL); |
/* Return the full path of the keyring. If @pub is 1, the public |
150 |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
keyring is return, otherwise the secret keyring. */ |
151 |
} |
char* |
152 |
free_if_alloc (path); |
get_gnupg_keyring (int pub, int strict) |
153 |
free_if_alloc (optfile); |
{ |
154 |
return keyring; |
char *optfile = NULL; |
155 |
} /* get_gnupg_keyring */ |
char *path = NULL; |
156 |
|
char *keyring = NULL; |
157 |
|
|
158 |
/* |
path = get_gnupg_path (); |
159 |
* Return the full path (with the gpg exe name). First the registry is scanned |
if (!path) |
160 |
* for the entry 'gpgProgram'. If it wasn't set, the default path is the |
return NULL; |
161 |
* appended string 'gpg.exe' is used. |
keyring = make_filename (path, pub? "pubring" : "secring", "gpg"); |
162 |
*/ |
if (!strict && !file_exist_check (keyring)) { |
163 |
char* |
free_if_alloc (path); |
164 |
get_gnupg_prog (void) |
return keyring; |
165 |
{ |
} |
166 |
char *p, *path, *pgm = NULL; |
if (file_exist_check (keyring) || pub && get_file_size (keyring) == 0) { |
167 |
size_t nlen = 0; |
free_if_alloc (keyring); |
168 |
|
optfile = make_filename (path, GPG_CONF, NULL); |
169 |
path = get_gnupg_path (); |
keyring = get_gnupg_keyring_from_options (optfile, pub); |
170 |
if (path == NULL) |
} |
171 |
return NULL; |
free_if_alloc (path); |
172 |
|
free_if_alloc (optfile); |
173 |
p = get_reg_entry_gpg ("gpgProgram"); |
return keyring; |
174 |
if (p == NULL) |
} |
175 |
pgm = make_filename (path, "gpg", "exe"); |
|
176 |
else { |
|
177 |
pgm = m_strdup (p); |
/* Return the full path (with the gpg exe name). First the registry is scanned |
178 |
free_if_alloc (p); |
for the entry 'gpgProgram'. If it wasn't set, the default path is the |
179 |
} |
appended string 'gpg.exe' is used. */ |
180 |
free_if_alloc (path); |
|
181 |
return pgm; |
/* FIXME: Use gpgme's engine info here. */ |
182 |
} /* get_gnupg_prog */ |
char* |
183 |
|
get_gnupg_prog (void) |
184 |
|
{ |
185 |
static char * |
char *p; |
186 |
default_key_from_cache (int *ret_no_useable) |
char *pgm = NULL; |
187 |
{ |
|
188 |
const char * s; |
p = get_reg_entry_gpg (GPG_REG_EXE); |
189 |
char * keyid = NULL; |
if (!p) { |
190 |
gpgme_key_t key; |
char *path = get_gnupg_path (); |
191 |
gpg_keycache_t sec = keycache_get_ctx (0); |
if (!path) |
192 |
|
return NULL; |
193 |
if (!sec) |
pgm = make_filename (path, "gpg", "exe"); |
194 |
BUG (0); |
free_if_alloc (path); |
195 |
gpg_keycache_rewind (sec); |
} |
196 |
while (!gpg_keycache_next_key (sec, 1, &key)) { |
else { |
197 |
if (key_is_useable (key)) { |
pgm = m_strdup (p); |
198 |
s = key->subkeys->keyid; |
free_if_alloc (p); |
199 |
if (s) |
} |
200 |
keyid = m_strdup (s+8); |
return pgm; |
201 |
break; |
} |
202 |
} |
|
203 |
} |
|
204 |
if (!keyid) { |
/* Retrieve the first usable secret key from cache. |
205 |
*ret_no_useable = 1; |
If no usable was found, @ret_no_useable is 1. |
206 |
msg_box( NULL, _("No useable secret key found."), _("GPG Error"), MB_ERR); |
Return value: the keyid of the secret key. */ |
207 |
} |
static char * |
208 |
return keyid; |
default_key_from_cache (int *ret_no_useable) |
209 |
} /* default_key_from_cache */ |
{ |
210 |
|
const char *s; |
211 |
|
char *keyid = NULL; |
212 |
char * |
gpgme_key_t key; |
213 |
get_gnupg_default_key (void) |
gpg_keycache_t sec = keycache_get_ctx (0); |
214 |
{ |
|
215 |
gpg_optfile_t opt = NULL; |
if (!sec) |
216 |
gpg_option_t e; |
BUG (0); |
217 |
char * keyid = NULL, * optfile = NULL; |
gpg_keycache_rewind (sec); |
218 |
int no_usable=0, rc = 0; |
while (!gpg_keycache_next_key (sec, 1, &key)) { |
219 |
|
if (key_is_useable (key)) { |
220 |
optfile = get_gnupg_cfgfile (); |
s = key->subkeys->keyid; |
221 |
if (!optfile) |
if (s) |
222 |
return default_key_from_cache( &no_usable ); |
keyid = m_strdup (s+8); |
223 |
rc = parse_gpg_options( optfile, &opt ); |
break; |
224 |
if( rc ) { |
} |
225 |
free_if_alloc( optfile ); |
} |
226 |
return default_key_from_cache( &no_usable ); |
if (!keyid) |
227 |
} |
*ret_no_useable = 1; |
228 |
e = find_option( opt, "default-key" ); |
return keyid; |
229 |
if ( e ) |
} |
230 |
keyid = m_strdup( e->val ); |
|
231 |
if( !e ) { |
|
232 |
e = find_option( opt, "local-user" ); |
/* Load the gpg.conf and search for some options |
233 |
if( e ) |
and store the result in the global preference context. |
234 |
keyid = m_strdup( e->val ); |
Return value: 0 on success. */ |
235 |
} |
int |
236 |
if( !e ) { |
gnupg_load_config (void) |
237 |
e = find_option( opt, "encrypt-to" ); |
{ |
238 |
if( e ) |
int rc; |
239 |
keyid = m_strdup( e->val ); |
gpg_optfile_t opt; |
240 |
} |
gpg_option_t o; |
241 |
free_if_alloc( optfile ); |
char *conf = get_gnupg_cfgfile (); |
242 |
release_gpg_options( opt ); |
if (!conf) |
243 |
|
return -1; |
244 |
if( !keyid ) |
rc = parse_gpg_options (conf, &opt); |
245 |
keyid = default_key_from_cache( &no_usable ); |
if (rc) { |
246 |
return keyid; |
free_if_alloc (conf); |
247 |
} /* get_gnupg_default_key */ |
return -1; |
248 |
|
} |
249 |
|
o = find_option (opt, "ask-cert-level"); |
250 |
/* Check if the gpg application (exe file) is available. */ |
if (o) |
251 |
int |
reg_prefs.gpg.ask_cert_level = 1; |
252 |
check_gnupg_prog (void) |
release_gpg_options (opt); |
253 |
{ |
free_if_alloc (conf); |
254 |
char *pgm = NULL; |
return 0; |
255 |
int rc = 0; |
} |
256 |
|
|
257 |
pgm = get_gnupg_prog (); |
|
258 |
if (!pgm) |
char* |
259 |
rc = WPTERR_GPG_EXEFILE; |
get_gnupg_default_key (void) |
260 |
if (file_exist_check (pgm)) |
{ |
261 |
rc = WPTERR_GPG_EXEFILE; |
gpg_optfile_t opt = NULL; |
262 |
free_if_alloc (pgm); |
gpg_option_t e; |
263 |
return rc; |
char * keyid = NULL, * optfile = NULL; |
264 |
} |
int no_usable=0, rc = 0; |
265 |
|
|
266 |
|
optfile = get_gnupg_cfgfile (); |
267 |
static int |
if (!optfile) |
268 |
parse_version_nr (const char * buf, int *major, int *minor, int *patch) |
return default_key_from_cache (&no_usable); |
269 |
{ |
rc = parse_gpg_options (optfile, &opt); |
270 |
char tmp[8]; |
if (rc) { |
271 |
int i; |
free_if_alloc (optfile); |
272 |
|
return default_key_from_cache (&no_usable); |
273 |
i=0; |
} |
274 |
while( buf && *buf != '.' && i < 8 ) |
e = find_option( opt, "default-key" ); |
275 |
tmp[i++] = *buf++; |
if ( e ) |
276 |
tmp[i] = 0; buf++; |
keyid = m_strdup( e->val ); |
277 |
*major = atol( tmp ); |
if( !e ) { |
278 |
i=0; |
e = find_option( opt, "local-user" ); |
279 |
while( buf && *buf != '.' && i < 8 ) |
if( e ) |
280 |
tmp[i++] = *buf++; |
keyid = m_strdup( e->val ); |
281 |
tmp[i] = 0; buf++; |
} |
282 |
*minor = atol( tmp ); |
if( !e ) { |
283 |
i=0; |
e = find_option( opt, "encrypt-to" ); |
284 |
while( buf && isdigit( *buf ) && i < 8 ) |
if( e ) |
285 |
tmp[i++] = *buf++; |
keyid = m_strdup( e->val ); |
286 |
tmp[i] = 0; |
} |
287 |
*patch = atol( tmp ); |
free_if_alloc (optfile); |
288 |
return 0; |
release_gpg_options (opt); |
289 |
} |
|
290 |
|
if (!keyid) |
291 |
|
keyid = default_key_from_cache (&no_usable); |
292 |
/* Check if the gnupg engine fullfills the minimum requirement |
return keyid; |
293 |
version given in @r_major.@r_minor.@r_patch. On success these |
} /* get_gnupg_default_key */ |
294 |
variables contain the GPG version which is installed. */ |
|
295 |
int |
|
296 |
check_gnupg_engine (int *r_major, int *r_minor, int *r_patch) |
char* get_reg_entry_gpg4win (const char *path); |
297 |
{ |
|
298 |
gpgme_ctx_t ctx; |
/* Check if GPG4WIN is available and if so, use the |
299 |
gpgme_engine_info_t inf; |
install path to figure out where the gpg.exe is. */ |
300 |
char * eng = NULL; |
char* |
301 |
int major=0, minor=0, patch=0; |
check_for_gpg4win (void) |
302 |
int rc; |
{ |
303 |
|
return get_reg_entry_gpg4win ("gpg.exe"); |
304 |
gpgme_new (&ctx); |
} |
305 |
inf = gpgme_ctx_get_engine_info (ctx); |
|
306 |
if (!inf) { |
|
307 |
gpgme_release (ctx); |
/* Check if the gpg application (exe file) is available. */ |
308 |
return -1; |
int |
309 |
} |
check_gnupg_prog (void) |
310 |
/* We need to exec GPG again to find out if IDEA is available. */ |
{ |
311 |
if (gpg_get_version (&eng)) |
char *gpgexe = NULL; |
312 |
return -1; |
int rc = 0; |
313 |
if (strstr (eng, "IDEA")) |
|
314 |
idea_available = 1; |
gpgexe = get_gnupg_prog (); |
315 |
free (eng); |
if (!gpgexe || file_exist_check (gpgexe)) { |
316 |
rc = parse_version_nr( inf->version, &major, &minor, &patch ); |
free_if_alloc (gpgexe); |
317 |
if( rc ) { |
gpgexe = check_for_gpg4win (); |
318 |
gpgme_release (ctx); |
if (!gpgexe || file_exist_check (gpgexe)) |
319 |
return rc; |
rc = WPTERR_GPG_EXEFILE; |
320 |
} |
else |
321 |
if (major < *r_major || minor < *r_minor) |
set_reg_entry_gpg (GPG_REG_EXE, gpgexe); |
322 |
rc = 1; |
} |
323 |
else { |
free_if_alloc (gpgexe); |
324 |
if (patch < *r_patch) |
return rc; |
325 |
rc = 1; |
} |
326 |
rc = 0; |
|
327 |
} |
|
328 |
*r_major = major; |
static int |
329 |
*r_minor = minor; |
parse_version_nr (const char * buf, int *major, int *minor, int *patch) |
330 |
*r_patch = patch; |
{ |
331 |
return rc; |
char tmp[8]; |
332 |
} |
int i; |
333 |
|
|
334 |
|
i=0; |
335 |
int |
while (buf && *buf != '.' && i < 8) |
336 |
check_gnupg_cfgfile (const char *fname, int *r_secrings, int *r_pubrings) |
tmp[i++] = *buf++; |
337 |
{ |
tmp[i] = 0; buf++; |
338 |
gpg_optfile_t opt; |
*major = atol( tmp ); |
339 |
gpg_option_t e; |
i=0; |
340 |
int rc = 0; |
while (buf && *buf != '.' && i < 8) |
341 |
|
tmp[i++] = *buf++; |
342 |
if( r_secrings ) |
tmp[i] = 0; buf++; |
343 |
*r_secrings = 0; |
*minor = atol (tmp); |
344 |
if( r_pubrings ) |
i=0; |
345 |
*r_pubrings = 0; |
while (buf && isdigit (*buf) && i < 8) |
346 |
rc = parse_gpg_options( fname, &opt ); |
tmp[i++] = *buf++; |
347 |
if( rc ) |
tmp[i] = 0; |
348 |
return WPTERR_FILE_OPEN; |
*patch = atol (tmp); |
349 |
|
return 0; |
350 |
for( e = opt->list; e; e = e->next ) { |
} |
351 |
if( !strcmp( e->name, "secret-keyring" ) ) { |
|
352 |
if( !file_exist_check( e->val ) ) |
|
353 |
r_secrings[0]++; |
/* Check if the gnupg engine fullfills the minimum requirement |
354 |
} |
version given in @r_major.@r_minor.@r_patch. On success these |
355 |
else if( !strcmp( e->name, "keyring" ) ) { |
variables contain the GPG version which is installed. */ |
356 |
if( !file_exist_check( e->val ) ) |
int |
357 |
r_pubrings[0]++; |
check_gnupg_engine (int *r_major, int *r_minor, int *r_patch) |
358 |
} |
{ |
359 |
} |
gpgme_ctx_t ctx; |
360 |
release_gpg_options( opt ); |
gpgme_engine_info_t inf; |
361 |
return 0; |
char *eng = NULL; |
362 |
} /* check_gnupg_cfgfile */ |
int major=0, minor=0, patch=0; |
363 |
|
int rc = 1; |
364 |
/* |
|
365 |
* Check if both keyrings are located in the gnupg home directory. |
gpgme_new (&ctx); |
366 |
*/ |
inf = gpgme_ctx_get_engine_info (ctx); |
367 |
int |
if (!inf) { |
368 |
gnupg_access_files (void) |
gpgme_release (ctx); |
369 |
{ |
return -1; |
370 |
int rc = 0; |
} |
371 |
int pubring_ok = 0, secring_ok = 0; |
|
372 |
int secrings = 0, pubrings = 0; |
/* We need to exec GPG again to find out if IDEA is available. */ |
373 |
char *optfile; |
if (gpg_get_version (&eng)) |
374 |
|
return -1; |
375 |
if (gnupg_access_keyring (1)) |
if (strstr (eng, "IDEA")) |
376 |
rc = WPTERR_GPG_KEYRINGS; |
idea_available = 1; |
377 |
else |
free (eng); |
378 |
pubring_ok = 1; |
rc = parse_version_nr (inf->version, &major, &minor, &patch); |
379 |
|
if (rc) { |
380 |
if (gnupg_access_keyring (0)) |
gpgme_release (ctx); |
381 |
rc = WPTERR_GPG_KEYRINGS; |
return rc; |
382 |
else |
} |
383 |
secring_ok = 1; |
|
384 |
if (!pubring_ok || !secring_ok) { |
if (major > *r_major) |
385 |
optfile = get_gnupg_cfgfile (); |
rc = 0; |
386 |
if (!optfile) |
else if (major == *r_major && minor > *r_minor) |
387 |
return WPTERR_GPG_KEYRINGS; |
rc = 0; |
388 |
rc = file_exist_check (optfile); |
else if (major == *r_major && minor == *r_minor && |
389 |
if (!rc && get_file_size(optfile) > 0) { |
patch >= *r_patch) |
390 |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
rc = 0; |
391 |
if (!rc && secrings && pubrings) { |
|
392 |
free_if_alloc (optfile); |
*r_major = major; |
393 |
return 0; /* found two keyrings in the option file */ |
*r_minor = minor; |
394 |
} |
*r_patch = patch; |
395 |
else if ((!rc && pubrings && secring_ok) |
return rc; |
396 |
|| (!rc && secrings && pubring_ok)) { |
} |
397 |
free_if_alloc (optfile); |
|
398 |
return 0; /* found one keyring and one entry in the options file */ |
|
399 |
} |
int |
400 |
else |
check_gnupg_cfgfile (const char *fname, int *r_secrings, int *r_pubrings) |
401 |
return WPTERR_GPG_OPT_KEYRINGS; |
{ |
402 |
} |
gpg_optfile_t opt; |
403 |
free_if_alloc (optfile); |
gpg_option_t e; |
404 |
rc = WPTERR_GPG_KEYRINGS; |
int rc = 0; |
405 |
} |
|
406 |
return rc; |
*r_secrings = 0; |
407 |
} /* gnupg_access_files */ |
*r_pubrings = 0; |
408 |
|
rc = parse_gpg_options( fname, &opt ); |
409 |
|
if( rc ) |
410 |
static int |
return WPTERR_FILE_OPEN; |
411 |
create_gpg_options( void ) |
|
412 |
{ |
for( e = opt->list; e; e = e->next ) { |
413 |
FILE *fp; |
if( !strcmp( e->name, "secret-keyring" ) ) { |
414 |
char *s, *optfile; |
if( !file_exist_check( e->val ) ) |
415 |
|
r_secrings[0]++; |
416 |
s = get_gnupg_path( ); |
} |
417 |
if( s == NULL ) |
else if( !strcmp( e->name, "keyring" ) ) { |
418 |
return WPTERR_FILE_CREAT; |
if( !file_exist_check( e->val ) ) |
419 |
optfile = make_filename( s, GPG_CONF, NULL ); |
r_pubrings[0]++; |
420 |
fp = fopen( optfile, "wb" ); |
} |
421 |
if( fp == NULL ) { |
} |
422 |
return WPTERR_FILE_CREAT; |
release_gpg_options( opt ); |
423 |
goto fail; |
return 0; |
424 |
} |
} /* check_gnupg_cfgfile */ |
425 |
fwrite( options_skel, 1, strlen( options_skel ), fp ); |
|
426 |
fclose( fp ); |
|
427 |
|
/* |
428 |
fail: |
* Check if both keyrings are located in the gnupg home directory. |
429 |
free_if_alloc( s ); |
*/ |
430 |
free_if_alloc( optfile ); |
int |
431 |
return 0; |
gnupg_access_files (void) |
432 |
} /* create_gpg_options */ |
{ |
433 |
|
int rc = 0; |
434 |
|
int pubring_ok = 0, secring_ok = 0; |
435 |
/* |
int secrings = 0, pubrings = 0; |
436 |
* Return the contents of the options file as a char buf. |
char *optfile; |
437 |
*/ |
|
438 |
char * |
if (gnupg_access_keyring (1)) |
439 |
get_gnupg_config (void) |
rc = WPTERR_GPG_KEYRINGS; |
440 |
{ |
else |
441 |
FILE * fp; |
pubring_ok = 1; |
442 |
char * p = NULL, * optfile = NULL; |
|
443 |
int fsize, rc = 0; |
if (gnupg_access_keyring (0)) |
444 |
|
rc = WPTERR_GPG_KEYRINGS; |
445 |
optfile = get_gnupg_cfgfile (); |
else |
446 |
if( optfile == NULL ) |
secring_ok = 1; |
447 |
return NULL; |
|
448 |
fsize = get_file_size( optfile ); |
if (!pubring_ok || !secring_ok) { |
449 |
if( !fsize ) { |
optfile = get_gnupg_cfgfile (); |
450 |
rc = create_gpg_options( ); |
if (!optfile) |
451 |
if ( rc ) |
return WPTERR_GPG_KEYRINGS; |
452 |
return NULL; |
rc = file_exist_check (optfile); |
453 |
fsize = get_file_size( optfile ); |
if (!rc && get_file_size (optfile) > 0) { |
454 |
} |
rc = check_gnupg_cfgfile (optfile, &secrings, &pubrings); |
455 |
if( fsize > 100000 ) |
if (!rc && secrings && pubrings) { |
456 |
goto leave; /* too large */ |
free_if_alloc (optfile); |
457 |
p = new char[fsize+1]; |
return 0; /* found two keyrings in the option file */ |
458 |
if( p == NULL ) |
} |
459 |
BUG( NULL ); |
else if ((!rc && pubrings && secring_ok) |
460 |
fp = fopen( optfile, "rb" ); |
|| (!rc && secrings && pubring_ok)) { |
461 |
if( fp == NULL ) { |
free_if_alloc (optfile); |
462 |
free_if_alloc( p ); |
return 0; /* found one keyring and one entry in the options file */ |
463 |
return NULL; |
} |
464 |
} |
else |
465 |
fread( p, 1, fsize, fp ); |
return WPTERR_GPG_OPT_KEYRINGS; |
466 |
fclose( fp ); |
} |
467 |
p[fsize] = '\0'; |
free_if_alloc (optfile); |
468 |
free_if_alloc( optfile ); |
rc = WPTERR_GPG_KEYRINGS; |
469 |
|
} |
470 |
leave: |
return rc; |
471 |
return p; |
} /* gnupg_access_files */ |
472 |
} /* get_gnupg_config */ |
|
473 |
|
|
474 |
|
static int |
475 |
int |
create_gpg_options (void) |
476 |
set_gnupg_default_key (const char * key) |
{ |
477 |
{ |
FILE *fp; |
478 |
gpg_optfile_t opt; |
char *s, *optfile; |
479 |
gpg_option_t e; |
|
480 |
char *optfile = NULL; |
s = get_gnupg_path (); |
481 |
int rc = 0; |
if( s == NULL ) |
482 |
|
return WPTERR_FILE_CREAT; |
483 |
optfile = get_gnupg_cfgfile (); |
optfile = make_filename (s, GPG_CONF, NULL); |
484 |
if (!optfile) |
fp = fopen( optfile, "wb" ); |
485 |
return -1; |
if( fp == NULL ) { |
486 |
rc = parse_gpg_options (optfile, &opt); |
return WPTERR_FILE_CREAT; |
487 |
if( rc ) { |
goto fail; |
488 |
free_if_alloc (optfile); |
} |
489 |
return -1; |
fwrite( options_skel, 1, strlen( options_skel ), fp ); |
490 |
} |
fclose( fp ); |
491 |
e = find_option (opt, "default-key"); |
|
492 |
if (e) { |
fail: |
493 |
free_if_alloc (e->val); |
free_if_alloc( s ); |
494 |
e->val = m_strdup (key); |
free_if_alloc( optfile ); |
495 |
e->used = 1; |
return 0; |
496 |
} |
} /* create_gpg_options */ |
497 |
else |
|
498 |
add_entry (opt, ENTRY_MULTI, "default-key", key); |
|
499 |
rc = commit_gpg_options (optfile, opt); |
/* |
500 |
|
* Return the contents of the options file as a char buf. |
501 |
free_if_alloc (optfile); |
*/ |
502 |
release_gpg_options (opt); |
char * |
503 |
|
get_gnupg_config (void) |
504 |
return rc; |
{ |
505 |
} /* set_gnupg_default_key */ |
FILE * fp; |
506 |
|
char * p = NULL, * optfile = NULL; |
507 |
|
int fsize, rc = 0; |
508 |
/* |
|
509 |
* Set the contents of the options file. |
optfile = get_gnupg_cfgfile (); |
510 |
*/ |
if( optfile == NULL ) |
511 |
int |
return NULL; |
512 |
set_gnupg_options( const char *buf, size_t buflen ) |
fsize = get_file_size( optfile ); |
513 |
{ |
if( !fsize ) { |
514 |
FILE *fp; |
rc = create_gpg_options( ); |
515 |
char *optfile = NULL; |
if ( rc ) |
516 |
|
return NULL; |
517 |
optfile = get_gnupg_cfgfile( ); |
fsize = get_file_size( optfile ); |
518 |
if( optfile == NULL ) |
} |
519 |
return WPTERR_FILE_CREAT; |
if( fsize > 100000 ) |
520 |
|
goto leave; /* too large */ |
521 |
fp = fopen( optfile, "wb" ); |
p = new char[fsize+1]; |
522 |
if( fp == NULL ) { |
if( p == NULL ) |
523 |
free_if_alloc( optfile ); |
BUG( NULL ); |
524 |
return WPTERR_FILE_CREAT; |
fp = fopen( optfile, "rb" ); |
525 |
} |
if( fp == NULL ) { |
526 |
fwrite( buf, 1, buflen, fp ); |
free_if_alloc( p ); |
527 |
fclose( fp ); |
return NULL; |
528 |
free_if_alloc( optfile ); |
} |
529 |
return 0; |
fread( p, 1, fsize, fp ); |
530 |
} /* set_gnupg_options */ |
fclose( fp ); |
531 |
|
p[fsize] = '\0'; |
532 |
/* |
free_if_alloc( optfile ); |
533 |
* Check if the line contains a valid GPG argument. |
|
534 |
*/ |
leave: |
535 |
static int |
return p; |
536 |
check_line( const char *buf ) |
} /* get_gnupg_config */ |
537 |
{ |
|
538 |
int j, len; |
|
539 |
int rc = 0; |
int |
540 |
|
set_gnupg_default_key (const char * key) |
541 |
if( *buf == '#' || *buf == '\r' || *buf == '\n' ) |
{ |
542 |
return 1; |
gpg_optfile_t opt; |
543 |
rc = 0; |
gpg_option_t e; |
544 |
for ( j = 0; valid_gpg_args[j]; j++ ) { |
char *optfile = NULL; |
545 |
len = strlen( valid_gpg_args[j] ); |
int rc = 0; |
546 |
if( !strncmp( valid_gpg_args[j], buf, len ) ) |
|
547 |
rc = 1; |
optfile = get_gnupg_cfgfile (); |
548 |
} |
if (!optfile) |
549 |
|
return -1; |
550 |
return rc; |
rc = parse_gpg_options (optfile, &opt); |
551 |
} /* check_line */ |
if( rc ) { |
552 |
|
free_if_alloc (optfile); |
553 |
|
return -1; |
554 |
int |
} |
555 |
check_gnupg_options( const char *buf ) |
e = find_option (opt, "default-key"); |
556 |
{ |
if (e) { |
557 |
char line[1024]; |
free_if_alloc (e->val); |
558 |
int nbytes = 0; |
e->val = m_strdup (key); |
559 |
unsigned j; |
e->used = 1; |
560 |
|
} |
561 |
for ( j = 0; j<strlen( buf ) && j < sizeof(line); j++ ) { |
else |
562 |
line[nbytes++] = buf[j]; |
add_entry (opt, ENTRY_MULTI, "default-key", key); |
563 |
if ( buf[j] == '\n' || j == ( strlen( buf ) - 1 ) ) { |
rc = commit_gpg_options (optfile, opt); |
564 |
line[nbytes] = '\0'; |
|
565 |
if( !check_line( line ) ) { |
free_if_alloc (optfile); |
566 |
msg_box( NULL, line, "options", MB_OK ); |
release_gpg_options (opt); |
567 |
return 1; |
|
568 |
} |
return rc; |
569 |
nbytes = 0; |
} /* set_gnupg_default_key */ |
570 |
} |
|
571 |
} |
|
572 |
|
/* |
573 |
return 0; |
* Set the contents of the options file. |
574 |
} /* check_gnupg_options */ |
*/ |
575 |
|
int |
576 |
|
set_gnupg_options( const char *buf, size_t buflen ) |
577 |
static int |
{ |
578 |
get_last_gnupg_access (gpg_watcher_s * ctx) |
FILE *fp; |
579 |
{ |
char *optfile = NULL; |
580 |
HANDLE fd; |
|
581 |
char *path = NULL, *file = NULL; |
optfile = get_gnupg_cfgfile( ); |
582 |
|
if( optfile == NULL ) |
583 |
path = get_gnupg_path (); |
return WPTERR_FILE_CREAT; |
584 |
file = make_filename (path, ctx->object, NULL); |
|
585 |
fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); |
fp = fopen( optfile, "wb" ); |
586 |
if( fd == INVALID_HANDLE_VALUE ) { |
if( fp == NULL ) { |
587 |
free_if_alloc (path); |
free_if_alloc( optfile ); |
588 |
free_if_alloc (file); |
return WPTERR_FILE_CREAT; |
589 |
return WPTERR_FILE_OPEN; |
} |
590 |
} |
fwrite( buf, 1, buflen, fp ); |
591 |
GetFileTime (fd, NULL, NULL, &ctx->access); |
fclose( fp ); |
592 |
CloseHandle (fd); |
free_if_alloc( optfile ); |
593 |
free_if_alloc (path); |
return 0; |
594 |
free_if_alloc (file); |
} /* set_gnupg_options */ |
595 |
return 0; |
|
596 |
} /* get_last_gnupg_access */ |
/* |
597 |
|
* Check if the line contains a valid GPG argument. |
598 |
|
*/ |
599 |
static void |
static int |
600 |
check_last_gnupg_access( gpg_watcher_s *ctx ) |
check_line( const char *buf ) |
601 |
{ |
{ |
602 |
ctx->modified = 0; |
int j, len; |
603 |
|
int rc = 0; |
604 |
if( ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime && |
|
605 |
ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime ) |
if( *buf == '#' || *buf == '\r' || *buf == '\n' ) |
606 |
ctx->modified = 1; |
return 1; |
607 |
|
rc = 0; |
608 |
ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime; |
for ( j = 0; valid_gpg_args[j]; j++ ) { |
609 |
ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime; |
len = strlen( valid_gpg_args[j] ); |
610 |
} /* check_last_gnupg_access */ |
if( !strncmp( valid_gpg_args[j], buf, len ) ) |
611 |
|
rc = 1; |
612 |
|
} |
613 |
void |
|
614 |
init_gnupg_table (void) |
return rc; |
615 |
{ |
} /* check_line */ |
616 |
int j; |
|
617 |
|
|
618 |
for (j = 0; j < gpg_table_count; j++) { |
int |
619 |
gpg_table[j].object = m_strdup (gpg_objs[j]); |
check_gnupg_options( const char *buf ) |
620 |
memset (&gpg_table[j].access, 0, sizeof (FILETIME)); |
{ |
621 |
memset (&gpg_table[j].last_access, 0, sizeof (FILETIME)); |
char line[1024]; |
622 |
gpg_table[j].modified = 0; |
int nbytes = 0; |
623 |
} |
unsigned j; |
624 |
} /* init_gnupg_table */ |
|
625 |
|
for ( j = 0; j<strlen( buf ) && j < sizeof(line); j++ ) { |
626 |
|
line[nbytes++] = buf[j]; |
627 |
void |
if ( buf[j] == '\n' || j == ( strlen( buf ) - 1 ) ) { |
628 |
free_gnupg_table (void) |
line[nbytes] = '\0'; |
629 |
{ |
if( !check_line( line ) ) { |
630 |
int j; |
msg_box( NULL, line, "options", MB_OK ); |
631 |
|
return 1; |
632 |
for (j=0; j < gpg_table_count; j++) |
} |
633 |
free_if_alloc (gpg_table[j].object); |
nbytes = 0; |
634 |
} /* free_gnupg_table */ |
} |
635 |
|
} |
636 |
|
|
637 |
int |
return 0; |
638 |
keyring_check_last_access (void) |
} /* check_gnupg_options */ |
639 |
{ |
|
640 |
int rc, j; |
|
641 |
|
/* Store the last access of the file inside the watcher @ctx. */ |
642 |
rc = 0; |
static int |
643 |
for (j = 0; j < gpg_table_count; j++) { |
get_last_gnupg_access (gpg_watcher_s *ctx) |
644 |
get_last_gnupg_access( &gpg_table[j] ); |
{ |
645 |
check_last_gnupg_access( &gpg_table[j] ); |
HANDLE fd; |
646 |
if ( gpg_table[j].modified ) |
char *path; |
647 |
rc++; |
char *file; |
648 |
} |
|
649 |
|
path = get_gnupg_path (); |
650 |
return rc; |
file = make_filename (path, ctx->object, NULL); |
651 |
} /* keyring_check_last_access */ |
fd = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); |
652 |
|
if (fd == INVALID_HANDLE_VALUE) { |
653 |
|
free_if_alloc (path); |
654 |
const char * |
free_if_alloc (file); |
655 |
gnupg_check_file_ext (const char * fname) |
return WPTERR_FILE_OPEN; |
656 |
{ |
} |
657 |
char file_ext[5]; |
GetFileTime (fd, NULL, NULL, &ctx->access); |
658 |
|
CloseHandle (fd); |
659 |
if (!strchr( fname, '.' )) |
free_if_alloc (path); |
660 |
return "UNKNOWN"; |
free_if_alloc (file); |
661 |
|
return 0; |
662 |
strncpy (file_ext, fname + strlen (fname) - 4, 4); |
} |
663 |
file_ext[4] = '\0'; |
|
664 |
if (!stricmp (file_ext, ".asc")) |
|
665 |
return "ARMORED"; |
/* Check if the file inside watcher @ctx was modified. */ |
666 |
else if (!stricmp (file_ext, ".sig")) |
static void |
667 |
return "SIGNED"; |
check_last_gnupg_access (gpg_watcher_s *ctx) |
668 |
else if (!stricmp (file_ext, ".gpg") |
{ |
669 |
|| !stricmp (file_ext, ".pgp")) |
ctx->modified = 0; |
670 |
return "ENCRYPTED"; |
|
671 |
else |
if (ctx->last_access.dwHighDateTime != ctx->access.dwHighDateTime && |
672 |
return "UNKNOWN"; |
ctx->last_access.dwLowDateTime != ctx->access.dwLowDateTime) |
673 |
|
ctx->modified = 1; |
674 |
return "UNKNOWN"; |
|
675 |
} /* gnupg_check_file_ext */ |
ctx->last_access.dwLowDateTime = ctx->access.dwLowDateTime; |
676 |
|
ctx->last_access.dwHighDateTime = ctx->access.dwHighDateTime; |
677 |
|
} |
678 |
char * |
|
679 |
get_gnupg_keyring_from_options (const char * fname, int pub) |
|
680 |
{ |
/* Init GPG watcher table for all monitored files. */ |
681 |
gpg_optfile_t opt; |
void |
682 |
gpg_option_t e; |
init_gnupg_table (void) |
683 |
char * kring = NULL; |
{ |
684 |
int rc = 0; |
char *p; |
685 |
|
int j; |
686 |
rc = parse_gpg_options (fname, &opt); |
|
687 |
if (rc) |
for (j = 0; j < gpg_table_count; j++) { |
688 |
return NULL; |
p = gpg_table[j].object = m_strdup (gpg_objs[j]); |
689 |
if (pub) |
if (!p) |
690 |
e = find_option (opt, "keyring"); |
BUG (NULL); |
691 |
else |
memset (&gpg_table[j].access, 0, sizeof (FILETIME)); |
692 |
e = find_option (opt, "secret-keyring"); |
memset (&gpg_table[j].last_access, 0, sizeof (FILETIME)); |
693 |
if (e) |
gpg_table[j].modified = 0; |
694 |
kring = m_strdup (e->val); |
} |
695 |
release_gpg_options (opt); |
} |
696 |
|
|
697 |
return kring; |
|
698 |
} /* get_gnupg_keyring_from_options */ |
void |
699 |
|
free_gnupg_table (void) |
700 |
|
{ |
701 |
|
int j; |
702 |
/* XXX: does not work with write-protected floppies */ |
|
703 |
static int |
for (j=0; j < gpg_table_count; j++) |
704 |
my_access (const char * fname) |
free_if_alloc (gpg_table[j].object); |
705 |
{ |
} |
706 |
HANDLE hd; |
|
707 |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
|
708 |
if (hd == INVALID_HANDLE_VALUE) |
/* Return the amount of files modified since the last call. */ |
709 |
return -1; |
int |
710 |
CloseHandle (hd); |
keyring_check_last_access (void) |
711 |
return 0; |
{ |
712 |
} /* my_access */ |
int rc, j; |
713 |
|
|
714 |
|
rc = 0; |
715 |
int |
for (j = 0; j < gpg_table_count; j++) { |
716 |
gpg_check_permissions (int showmsg) |
get_last_gnupg_access (&gpg_table[j]); |
717 |
{ |
check_last_gnupg_access (&gpg_table[j]); |
718 |
char * p, * name = NULL; |
if (gpg_table[j].modified) |
719 |
int failed = 0, ans=0, attrs=0; |
rc++; |
720 |
|
} |
721 |
p = get_gnupg_path (); |
|
722 |
check_keyring (&p); |
return rc; |
723 |
if (p) { |
} |
724 |
name = make_filename (p, "pubring", "gpg"); |
|
725 |
free_if_alloc (p); |
|
726 |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
const char* |
727 |
ans = msg_box (NULL, |
gnupg_check_file_ext (const char *fname, int *r_type) |
728 |
_("The selected keyring has the read-only file\n" |
{ |
729 |
"attribute. In this state you do not have write\n" |
char file_ext[5]; |
730 |
"access. Do you want to remove the attribute?"), |
|
731 |
_("GPG Information"), MB_YESNO); |
if (r_type) |
732 |
if (ans == IDYES) { |
*r_type = PGP_NONE; |
733 |
attrs &= ~FILE_ATTRIBUTE_READONLY; |
if (!strchr (fname, '.' )) |
734 |
if (!SetFileAttributes (name, attrs)) { |
return "UNKNOWN"; |
735 |
msg_box (NULL, _("Could not reset read-only state."), |
|
736 |
_("GPG Error"), MB_ERR); |
strncpy (file_ext, fname + strlen (fname) - 4, 4); |
737 |
failed = 1; |
file_ext[4] = '\0'; |
738 |
} |
if (!stricmp (file_ext, ".asc")) |
739 |
} |
return "ARMORED"; |
740 |
else if (ans == IDNO) { |
else if (!stricmp (file_ext, ".sig")) { |
741 |
/* |
if (r_type) |
742 |
msg_box (NULL, _("All commands with write access to the keyring\n" |
*r_type = PGP_SIG; |
743 |
"will be disabled."), _("GPG Information"), MB_INFO); |
return "SIGNED"; |
744 |
*/ |
} |
745 |
failed = 1; |
else if (!stricmp (file_ext, ".gpg") || !stricmp (file_ext, ".pgp")) { |
746 |
} |
if (r_type) |
747 |
} |
*r_type = PGP_MESSAGE; |
748 |
if (my_access (name)) { |
return "ENCRYPTED"; |
749 |
if (showmsg) |
} |
750 |
msg_box (NULL, |
return "UNKNOWN"; |
751 |
_("You do not have file access to modify the contents of\n" |
} |
752 |
"one or both of the selected keyrings.\n" |
|
753 |
"\n" |
|
754 |
"The keyrings are in a read-only state which is propably\n" |
char* |
755 |
"caused by another program which already opened the files.\n"), |
get_gnupg_keyring_from_options (const char * fname, int pub) |
756 |
_("GPG Warning"), MB_WARN); |
{ |
757 |
failed = 2; |
gpg_optfile_t opt; |
758 |
} |
gpg_option_t e; |
759 |
} |
char * kring = NULL; |
760 |
free_if_alloc (name); |
int rc = 0; |
761 |
return failed; |
|
762 |
} /* gpg_check_permissions */ |
rc = parse_gpg_options (fname, &opt); |
763 |
|
if (rc) |
764 |
|
return NULL; |
765 |
static int |
if (pub) |
766 |
check_homedir (void) |
e = find_option (opt, "keyring"); |
767 |
{ |
else |
768 |
char * homedir = NULL; |
e = find_option (opt, "secret-keyring"); |
769 |
int yes = 0; |
if (e) |
770 |
|
kring = m_strdup (e->val); |
771 |
homedir = get_reg_entry_gpg ("HomeDir"); |
release_gpg_options (opt); |
772 |
if (!homedir) |
|
773 |
homedir = multi_gnupg_path (); |
return kring; |
774 |
if (!homedir) |
} |
775 |
homedir = m_strdup ("c:\\gnupg"); |
|
776 |
if (homedir) { |
|
777 |
if (GetFileAttributes (homedir) == 0xFFFFFFFF) { |
|
778 |
yes = log_box (_("Preferences"), MB_YESNO, |
/* XXX: does not work with write-protected floppies */ |
779 |
_("%s does not exit.\n" |
static int |
780 |
"Do you want to create this directory?"), homedir); |
my_access (const char * fname) |
781 |
if (yes == IDYES) { |
{ |
782 |
if (CreateDirectory (homedir, NULL) == FALSE) { |
HANDLE hd; |
783 |
free_if_alloc (homedir); |
hd = CreateFile (fname, GENERIC_WRITE, FILE_SHARE_WRITE, |
784 |
return WPTERR_DIR_CREAT; |
NULL, OPEN_EXISTING, 0, NULL); |
785 |
} |
if (hd == INVALID_HANDLE_VALUE) |
786 |
} |
return -1; |
787 |
return WPTERR_DIR_OPEN; |
CloseHandle (hd); |
788 |
} |
return 0; |
789 |
free_if_alloc (homedir); |
} |
790 |
} |
|
791 |
return 0; |
|
792 |
} /* check_homedir */ |
/* Check the file permissions of the public keyring. |
793 |
|
If @showmsg is 1 output a message in case of errors. |
794 |
|
Return value: 1 if read-only attribute |
795 |
int |
2 if file is opened by another process exclusively. */ |
796 |
gnupg_check_homedir (void) |
int |
797 |
{ |
gpg_check_permissions (int showmsg) |
798 |
char *homedir = NULL, *prog = NULL; |
{ |
799 |
int rc = 0, ec = 0; |
char * p, * name = NULL; |
800 |
|
int failed = 0, ans=0, attrs=0; |
801 |
rc = check_homedir (); |
|
802 |
if (rc) |
p = get_gnupg_path (); |
803 |
return rc; |
check_keyring (&p); |
804 |
if ((homedir = get_reg_entry_gpg ("HomeDir")) && |
if (p) { |
805 |
!(prog = get_reg_entry_gpg ("gpgProgram" ))) { |
name = make_filename (p, "pubring", "gpg"); |
806 |
prog = make_filename (homedir, "gpg", "exe"); |
free_if_alloc (p); |
807 |
if (file_exist_check (prog) == 0) { |
if ((attrs=GetFileAttributes (name)) & FILE_ATTRIBUTE_READONLY) { |
808 |
rc = set_reg_entry_gpg ("gpgProgram", prog); |
ans = msg_box (NULL, |
809 |
if (rc) |
_("The selected keyring has the read-only file\n" |
810 |
goto fail; |
"attribute. In this state you do not have write\n" |
811 |
} |
"access. Do you want to remove the attribute?"), |
812 |
free_if_alloc (homedir); |
_("GPG Information"), MB_YESNO); |
813 |
free_if_alloc (prog); |
if (ans == IDYES) { |
814 |
return rc; |
attrs &= ~FILE_ATTRIBUTE_READONLY; |
815 |
} |
if (!SetFileAttributes (name, attrs)) { |
816 |
if ((prog = get_reg_entry_gpg ("gpgProgram")) |
msg_box (NULL, _("Could not reset read-only state."), |
817 |
&& file_exist_check (prog)) { |
_("GPG Error"), MB_ERR); |
818 |
free_if_alloc (prog); |
failed = 1; |
819 |
homedir = get_reg_entry_gpg ("HomeDir"); |
} |
820 |
if (!homedir) { |
} |
821 |
rc = WPTERR_GENERAL; |
else if (ans == IDNO) { |
822 |
goto fail; |
/* All commands with write access will be disabled. */ |
823 |
} |
failed = 1; |
824 |
prog = make_filename (homedir, "gpg", "exe"); |
} |
825 |
if (file_exist_check (prog) == 0) { |
} |
826 |
rc = set_reg_entry_gpg ("gpgProgram", prog); |
if (my_access (name)) { |
827 |
if (rc) |
if (showmsg) |
828 |
goto fail; |
msg_box (NULL, |
829 |
free_if_alloc (prog); |
_("You do not have file access to modify the contents of\n" |
830 |
return rc; |
"one or both of the selected keyrings.\n" |
831 |
} |
"\n" |
832 |
} |
"The keyrings are in a read-only state which is propably\n" |
833 |
|
"caused by another program which already opened the files.\n"), |
834 |
/* Change the return code if homedir doesn't exist or if the program |
_("GPG Warning"), MB_WARN); |
835 |
doesn't exist. Note that exist_checks return 0 to suggest existance. */ |
failed = 2; |
836 |
if ((!homedir || dir_exist_check (homedir))) |
} |
837 |
rc = WPTERR_GENERAL; |
} |
838 |
|
free_if_alloc (name); |
839 |
fail: |
return failed; |
840 |
free_if_alloc (homedir); |
} |
841 |
free_if_alloc (prog); |
|
842 |
return rc; |
|
843 |
} /* gnupg_check_homedir */ |
/* Check the GPG home dir. If all methods failed, try to |
844 |
|
create the default folder. */ |
845 |
|
static int |
846 |
int |
check_homedir (void) |
847 |
gnupg_copy_keyrings (void) |
{ |
848 |
{ |
char *homedir = NULL; |
849 |
const char * pring, * sring; |
int yes = 0, set_reg=0; |
850 |
char * file = NULL, * path = NULL; |
int rc = 0; |
851 |
int id = 0, rc = 0; |
|
852 |
HWND hwnd; |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
853 |
|
if (!homedir) { |
854 |
path = get_gnupg_path (); |
set_reg = 1; |
855 |
if (!path) |
homedir = multi_gnupg_path (0); |
856 |
return WPTERR_GENERAL; |
} |
857 |
hwnd = GetDesktopWindow (); |
if (!homedir) |
858 |
|
homedir = m_strdup ("c:\\gnupg"); |
859 |
pring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your public keyring"), |
if (homedir) { |
860 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL); |
if (GetFileAttributes (homedir) == 0xFFFFFFFF) { |
861 |
if (!pring) { |
yes = log_box (_("Preferences"), MB_YESNO, |
862 |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
_("%s does not exit.\n" |
863 |
free_if_alloc (path); |
"Do you want to create this directory?"), homedir); |
864 |
return WPTERR_GENERAL; |
if (yes == IDYES) { |
865 |
} |
if (!CreateDirectory (homedir, NULL)) |
866 |
file = make_filename (path, "pubring", "gpg"); |
rc = WPTERR_DIR_CREAT; |
867 |
if (file_exist_check (file) == 0) { |
} |
868 |
id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO); |
else |
869 |
if (id == IDNO) |
rc = WPTERR_DIR_OPEN; |
870 |
goto fail; |
} |
871 |
} |
if (set_reg) |
872 |
if (!CopyFile (pring, file, FALSE)) { |
set_reg_entry_gpg (GPG_REG_HOME, homedir); |
873 |
msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
free_if_alloc (homedir); |
874 |
rc = WPTERR_FILE_READ; |
} |
875 |
goto fail; |
return rc; |
876 |
} |
} |
877 |
free_if_alloc (file); |
|
878 |
|
|
879 |
sring = get_filename_dlg (hwnd, FILE_OPEN, _("Please choose your secret keyring"), |
int |
880 |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL); |
gnupg_check_homedir (void) |
881 |
if (!sring) { |
{ |
882 |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
char *homedir = NULL; |
883 |
return WPTERR_GENERAL; |
char *prog = NULL; |
884 |
} |
int rc = 0; |
885 |
file = make_filename (path, "secring", "gpg"); |
|
886 |
if (file_exist_check (file) == 0) { |
rc = check_homedir (); |
887 |
id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO); |
if (rc) |
888 |
if( id == IDNO ) |
return rc; |
889 |
goto fail; |
if ((homedir = get_reg_entry_gpg (GPG_REG_HOME)) && |
890 |
} |
!(prog = get_reg_entry_gpg (GPG_REG_EXE ))) { |
891 |
if (!CopyFile (sring, file, FALSE)) { |
prog = make_filename (homedir, "gpg", "exe"); |
892 |
msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
if (file_exist_check (prog) == 0) { |
893 |
rc = WPTERR_FILE_READ; |
rc = set_reg_entry_gpg (GPG_REG_EXE, prog); |
894 |
} |
if (rc) |
895 |
|
goto fail; |
896 |
fail: |
} |
897 |
free_if_alloc (file); |
free_if_alloc (homedir); |
898 |
free_if_alloc (path); |
free_if_alloc (prog); |
899 |
return rc; |
return rc; |
900 |
} /* gnupg_import_keyrings */ |
} |
901 |
|
if ((prog = get_reg_entry_gpg (GPG_REG_EXE)) |
902 |
|
&& file_exist_check (prog)) { |
903 |
void |
free_if_alloc (prog); |
904 |
gnupg_backup_options (void) |
homedir = get_reg_entry_gpg (GPG_REG_HOME); |
905 |
{ |
if (!homedir) { |
906 |
char *cfgfile = NULL; |
rc = WPTERR_GENERAL; |
907 |
char bak[512]; |
goto fail; |
908 |
|
} |
909 |
cfgfile = get_gnupg_cfgfile (); |
prog = make_filename (homedir, "gpg", "exe"); |
910 |
if (cfgfile == NULL) |
if (file_exist_check (prog) == 0) { |
911 |
return; |
rc = set_reg_entry_gpg (GPG_REG_EXE, prog); |
912 |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
if (rc) |
913 |
CopyFile (cfgfile, bak, FALSE); |
goto fail; |
914 |
free_if_alloc (cfgfile); |
free_if_alloc (prog); |
915 |
} /* gnupg_backup_options */ |
return rc; |
916 |
|
} |
917 |
|
} |
918 |
|
|
919 |
static int |
/* Change the return code if homedir doesn't exist or if the program |
920 |
backup_one_file (const char * srcpath, const char * srcn, |
doesn't exist. Note that exist_checks return 0 to suggest existance. */ |
921 |
const char * dstpath, const char * dstn) |
if ((!homedir || dir_exist_check (homedir))) |
922 |
{ |
rc = WPTERR_GENERAL; |
923 |
char * src, * dst; |
|
924 |
BOOL rc; |
fail: |
925 |
|
free_if_alloc (homedir); |
926 |
src = make_filename (srcpath, srcn, "gpg"); |
free_if_alloc (prog); |
927 |
if (!src) |
return rc; |
928 |
BUG (NULL); |
} /* gnupg_check_homedir */ |
929 |
dst = make_filename (dstpath, dstn, "gpg"); |
|
930 |
if (!dst) |
|
931 |
BUG (NULL); |
int |
932 |
rc = CopyFile (src, dst, FALSE); |
gnupg_copy_keyrings (void) |
933 |
free_if_alloc (src); |
{ |
934 |
free_if_alloc (dst); |
const char * pring, * sring; |
935 |
if (!rc) |
char * file = NULL, * path = NULL; |
936 |
{ |
int id = 0, rc = 0; |
937 |
log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn); |
HWND hwnd; |
938 |
return WPTERR_GENERAL; |
|
939 |
} |
path = get_gnupg_path (); |
940 |
return 0; |
if (!path) |
941 |
} /* backup_one_file */ |
return WPTERR_GENERAL; |
942 |
|
hwnd = GetDesktopWindow (); |
943 |
|
|
944 |
static int |
pring = get_fileopen_dlg (hwnd, _("Please choose your public keyring"), |
945 |
check_keyring (char ** r_path) |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"),NULL); |
946 |
{ |
if (!pring) { |
947 |
char * p; |
msg_box (hwnd, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR); |
948 |
char * opt, * name; |
free_if_alloc (path); |
949 |
|
return WPTERR_GENERAL; |
950 |
if (!*r_path) |
} |
951 |
return 0; |
file = make_filename (path, "pubring", "gpg"); |
952 |
p = make_filename (*r_path, "pubring", "gpg"); |
if (file_exist_check (file) == 0) { |
953 |
if (!p || get_file_size (p) > 0) |
id = msg_box (hwnd, _("Overwrite old public keyring?"), "WinPT", MB_INFO|MB_YESNO); |
954 |
return 0; |
if (id == IDNO) |
955 |
|
goto fail; |
956 |
opt = get_gnupg_cfgfile (); |
} |
957 |
if (!opt) |
if (!CopyFile (pring, file, FALSE)) { |
958 |
BUG (0); |
msg_box (hwnd, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
959 |
name = get_gnupg_keyring_from_options (opt, 1); |
rc = WPTERR_FILE_READ; |
960 |
free_if_alloc (opt); |
goto fail; |
961 |
free_if_alloc (p); |
} |
962 |
if (!name) |
free_if_alloc (file); |
963 |
return 0; |
|
964 |
p = strrchr (name, '\\'); |
sring = get_fileopen_dlg (hwnd, _("Please choose your secret keyring"), |
965 |
if (!p) |
_("GPG Keyrings (*.gpg)\0*.gpg\0\0"), NULL); |
966 |
{ |
if (!sring) { |
967 |
free_if_alloc (name); |
msg_box( NULL, _("No keyring was chosen. Exit."), _("WinPT Error"), MB_ERR ); |
968 |
return 0; |
return WPTERR_GENERAL; |
969 |
} |
} |
970 |
free_if_alloc (*r_path); |
file = make_filename (path, "secring", "gpg"); |
971 |
*r_path = new char [strlen (name)+1]; |
if (file_exist_check (file) == 0) { |
972 |
memset (*r_path, 0, strlen (name)); |
id = msg_box (hwnd, _("Overwrite old secret keyring?"), "WinPT", MB_INFO|MB_YESNO); |
973 |
strncpy (*r_path, name, (p-name)); |
if( id == IDNO ) |
974 |
free_if_alloc (name); |
goto fail; |
975 |
return 1; |
} |
976 |
} |
if (!CopyFile (sring, file, FALSE)) { |
977 |
|
msg_box( NULL, _("Could not copy file."), _("WinPT Error"), MB_ERR); |
978 |
|
rc = WPTERR_FILE_READ; |
979 |
static char* |
} |
980 |
get_backup_name (const char *templ) |
|
981 |
{ |
fail: |
982 |
struct tm *tm; |
free_if_alloc (file); |
983 |
char *p; |
free_if_alloc (path); |
984 |
|
return rc; |
985 |
time_t t = time (NULL); |
} /* gnupg_import_keyrings */ |
986 |
tm = localtime (&t); |
|
987 |
p = new char [strlen (templ) + 8 + 1]; |
|
988 |
if (!p) |
void |
989 |
BUG (0); |
gnupg_backup_options (void) |
990 |
sprintf (p, "%s-%d", templ, tm->tm_wday % 3); |
{ |
991 |
return p; |
char *cfgfile = NULL; |
992 |
} |
char bak[512]; |
993 |
|
|
994 |
|
cfgfile = get_gnupg_cfgfile (); |
995 |
void |
if (cfgfile == NULL) |
996 |
gnupg_backup_keyrings (void) |
return; |
997 |
{ |
_snprintf (bak, DIM (bak)-1, "%s.bak", cfgfile); |
998 |
char *srcpath = NULL, *dstpath = NULL; |
CopyFile (cfgfile, bak, FALSE); |
999 |
char *name=NULL; |
free_if_alloc (cfgfile); |
1000 |
int rc, bakmode=0; |
} /* gnupg_backup_options */ |
1001 |
|
|
1002 |
if (!reg_prefs.auto_backup) |
|
1003 |
return; |
|
1004 |
bakmode = reg_prefs.backup.mode; |
static int |
1005 |
srcpath = get_gnupg_path (); |
backup_one_file (const char *srcpath, const char *srcn, |
1006 |
check_keyring (&srcpath); |
const char *dstpath, const char *dstn) |
1007 |
if (bakmode == 1) { |
{ |
1008 |
dstpath = get_gnupg_path (); |
char * src, * dst; |
1009 |
check_keyring (&dstpath); |
BOOL rc; |
1010 |
} |
|
1011 |
else if (bakmode == 2) { |
src = make_filename (srcpath, srcn, "gpg"); |
1012 |
char * tmpfile; |
if (!src) |
1013 |
FILE * fp; |
BUG (NULL); |
1014 |
|
dst = make_filename (dstpath, dstn, "gpg"); |
1015 |
dstpath = m_strdup (reg_prefs.backup.path); |
if (!dst) |
1016 |
if (!dstpath) |
BUG (NULL); |
1017 |
BUG (0); |
rc = CopyFile (src, dst, FALSE); |
1018 |
tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp"); |
free_if_alloc (src); |
1019 |
fp = fopen (tmpfile, "wb"); |
free_if_alloc (dst); |
1020 |
if (!fp) |
if (!rc) |
1021 |
rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, _("The backup drive '%s' does not seems to accessable.\n" |
{ |
1022 |
"Please insert/check the drive to continue."), dstpath); |
log_box (_("Backup"), MB_ERR, _("Backup keyring \"%s\" failed"), srcn); |
1023 |
else { |
return WPTERR_GENERAL; |
1024 |
rc = 0; |
} |
1025 |
fclose (fp); |
return 0; |
1026 |
unlink (tmpfile); |
} /* backup_one_file */ |
1027 |
} |
|
1028 |
free_if_alloc (tmpfile); |
|
1029 |
if (!fp || rc == IDCANCEL) |
static int |
1030 |
return; |
check_keyring (char ** r_path) |
1031 |
} |
{ |
1032 |
else { |
char * p; |
1033 |
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
char * opt, * name; |
1034 |
return; |
|
1035 |
} |
if (!*r_path) |
1036 |
name = get_backup_name ("pubring-bak"); |
return 0; |
1037 |
rc = backup_one_file (srcpath, "pubring", dstpath, name); |
p = make_filename (*r_path, "pubring", "gpg"); |
1038 |
if (!rc) |
if (!p || get_file_size (p) > 0) |
1039 |
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
return 0; |
1040 |
free_if_alloc (name); |
|
1041 |
free_if_alloc (srcpath); |
opt = get_gnupg_cfgfile (); |
1042 |
free_if_alloc (dstpath); |
if (!opt) |
1043 |
} /* gnupg_backup_keyrings */ |
BUG (0); |
1044 |
|
name = get_gnupg_keyring_from_options (opt, 1); |
1045 |
|
free_if_alloc (opt); |
1046 |
void |
free_if_alloc (p); |
1047 |
gnupg_display_error (void) |
if (!name) |
1048 |
{ |
return 0; |
1049 |
char tmpath[512], * errstr; |
p = strrchr (name, '\\'); |
1050 |
size_t size = 0; |
if (!p) |
1051 |
FILE * fp; |
{ |
1052 |
|
free_if_alloc (name); |
1053 |
GetTempPath (sizeof tmpath - 32, (tmpath)); |
return 0; |
1054 |
strcat (tmpath, "gpg_stderr"); |
} |
1055 |
size = get_file_size (tmpath); |
free_if_alloc (*r_path); |
1056 |
if (file_exist_check (tmpath) || size <= 0) |
*r_path = new char [strlen (name)+1]; |
1057 |
return; |
memset (*r_path, 0, strlen (name)); |
1058 |
fp = fopen( tmpath, "rb" ); |
strncpy (*r_path, name, (p-name)); |
1059 |
if (!fp) { |
free_if_alloc (name); |
1060 |
msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO ); |
return 1; |
1061 |
return; |
} |
1062 |
} |
|
1063 |
errstr = new char[size+1]; |
|
1064 |
if (!errstr) |
static char* |
1065 |
BUG (0); |
get_backup_name (const char *templ) |
1066 |
fread (errstr, 1, size, fp); |
{ |
1067 |
errstr[size] = '\0'; |
struct tm *tm; |
1068 |
fclose (fp); |
char *p; |
1069 |
msg_box (NULL, errstr, _("GPG Error"), MB_INFO); |
|
1070 |
free_if_alloc (errstr); |
time_t t = time (NULL); |
1071 |
} /* gnupg_display_error */ |
tm = localtime (&t); |
1072 |
|
p = new char [strlen (templ) + 8 + 1]; |
1073 |
|
if (!p) |
1074 |
int |
BUG (0); |
1075 |
gnupg_access_keyring (int _pub) |
sprintf (p, "%s-%d", templ, tm->tm_wday % 3); |
1076 |
{ |
return p; |
1077 |
int rc = 0; |
} |
1078 |
char * name = get_gnupg_keyring (_pub, 1); |
|
1079 |
if (file_exist_check (name)) |
|
1080 |
rc = -1; |
void |
1081 |
free_if_alloc (name); |
gnupg_backup_keyrings (void) |
1082 |
return rc; |
{ |
1083 |
} |
char *srcpath = NULL, *dstpath = NULL; |
1084 |
|
char *name=NULL; |
1085 |
|
int rc, bakmode=0; |
1086 |
|
|
1087 |
|
if (!reg_prefs.auto_backup) |
1088 |
|
return; |
1089 |
|
bakmode = reg_prefs.backup.mode; |
1090 |
|
srcpath = get_gnupg_path (); |
1091 |
|
check_keyring (&srcpath); |
1092 |
|
if (bakmode == 1) { |
1093 |
|
dstpath = get_gnupg_path (); |
1094 |
|
check_keyring (&dstpath); |
1095 |
|
} |
1096 |
|
else if (bakmode == 2) { |
1097 |
|
char * tmpfile; |
1098 |
|
FILE * fp; |
1099 |
|
|
1100 |
|
dstpath = m_strdup (reg_prefs.backup.path); |
1101 |
|
if (!dstpath) |
1102 |
|
BUG (0); |
1103 |
|
tmpfile = make_filename (reg_prefs.backup.path, "test", "tmp"); |
1104 |
|
fp = fopen (tmpfile, "wb"); |
1105 |
|
if (!fp) |
1106 |
|
rc = log_box (_("Backup"), MB_WARN|MB_RETRYCANCEL, |
1107 |
|
_("The backup drive '%s' does not seems to accessable.\n" |
1108 |
|
"Please insert/check the drive to continue."), dstpath); |
1109 |
|
else { |
1110 |
|
rc = 0; |
1111 |
|
fclose (fp); |
1112 |
|
remove (tmpfile); |
1113 |
|
} |
1114 |
|
free_if_alloc (tmpfile); |
1115 |
|
if (!fp || rc == IDCANCEL) |
1116 |
|
return; |
1117 |
|
} |
1118 |
|
else { |
1119 |
|
log_box (_("Backup"), MB_ERR, _("Invalid backup mode %d"), bakmode); |
1120 |
|
return; |
1121 |
|
} |
1122 |
|
name = get_backup_name ("pubring-bak"); |
1123 |
|
rc = backup_one_file (srcpath, "pubring", dstpath, name); |
1124 |
|
if (!rc) |
1125 |
|
rc = backup_one_file (srcpath, "secring", dstpath, "secring-bak"); |
1126 |
|
free_if_alloc (name); |
1127 |
|
free_if_alloc (srcpath); |
1128 |
|
free_if_alloc (dstpath); |
1129 |
|
} /* gnupg_backup_keyrings */ |
1130 |
|
|
1131 |
|
|
1132 |
|
/* Display GPG error from file if possible. */ |
1133 |
|
void |
1134 |
|
gnupg_display_error (void) |
1135 |
|
{ |
1136 |
|
char tmpath[512], * errstr; |
1137 |
|
size_t size = 0; |
1138 |
|
FILE * fp; |
1139 |
|
|
1140 |
|
GetTempPath (sizeof tmpath - 32, (tmpath)); |
1141 |
|
strcat (tmpath, "gpg_stderr"); |
1142 |
|
size = get_file_size (tmpath); |
1143 |
|
if (file_exist_check (tmpath) || size <= 0) |
1144 |
|
return; |
1145 |
|
fp = fopen( tmpath, "rb" ); |
1146 |
|
if (!fp) { |
1147 |
|
msg_box( NULL, _("No GPG error description available."), _("GPG Error"), MB_INFO ); |
1148 |
|
return; |
1149 |
|
} |
1150 |
|
errstr = new char[size+1]; |
1151 |
|
if (!errstr) |
1152 |
|
BUG (0); |
1153 |
|
fread (errstr, 1, size, fp); |
1154 |
|
errstr[size] = '\0'; |
1155 |
|
fclose (fp); |
1156 |
|
msg_box (NULL, errstr, _("GPG Error"), MB_INFO); |
1157 |
|
free_if_alloc (errstr); |
1158 |
|
} |
1159 |
|
|
1160 |
|
|
1161 |
|
|
1162 |
|
/* check that the requested GPG keyring exist and. |
1163 |
|
Return value: 0 for success. */ |
1164 |
|
int |
1165 |
|
gnupg_access_keyring (int _pub) |
1166 |
|
{ |
1167 |
|
int rc = 0; |
1168 |
|
char *name = get_gnupg_keyring (_pub, 1); |
1169 |
|
if (!name || file_exist_check (name)) |
1170 |
|
rc = -1; |
1171 |
|
free_if_alloc (name); |
1172 |
|
return rc; |
1173 |
|
} |