1 |
/* wptFileManager.cpp - File Manager routines |
/* wptFileManager.cpp - File Manager routines |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
40 |
#include "wptW32API.h" |
#include "wptW32API.h" |
41 |
#include "wptVersion.h" |
#include "wptVersion.h" |
42 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
43 |
#include "wptGPGZIP.h" |
#include "wptZIP.h" |
44 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
45 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
46 |
#include "wptImport.h" |
#include "wptImport.h" |
47 |
|
|
48 |
#include "openpgp.h" |
#include "openpgp.h" |
49 |
|
|
50 |
void progress_cleanup (progress_filter_s * pfx); |
void progress_cleanup (progress_filter_s *pfx); |
51 |
|
BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, |
52 |
char* gpg_keylist_to_pattern (gpgme_key_t *rset); |
WPARAM wparam, LPARAM lparam); |
53 |
|
char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n); |
54 |
gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
55 |
int prev_was_bad, int fd); |
int prev_was_bad, int fd); |
56 |
|
|
57 |
/*-- wptFileVerifyDlg.cpp --*/ |
/*-- wptFileVerifyDlg.cpp --*/ |
58 |
int file_verify_add_state (siglog_context_t c); |
void file_verify_add_state (file_sig_ctx_t c); |
59 |
void file_verify_use_event (void); |
void file_verify_use_event (void); |
60 |
void file_verify_wait (void); |
void file_verify_wait (void); |
61 |
|
|
64 |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
65 |
|
|
66 |
|
|
67 |
char * |
/* Check if the drive given by @fname is a floppy disc. |
68 |
fm_quote_file (const char * name) |
Return value: 1 for success. */ |
69 |
|
static int |
70 |
|
is_floppy_disc (const char * fname) |
71 |
{ |
{ |
72 |
char * p; |
char drv[32] = {0}; |
73 |
size_t len = strlen (name) + 8; |
int i=0; |
74 |
|
|
75 |
if (*name == '"') |
if (!strstr (fname, ":\\")) |
76 |
return m_strdup (name); /* avoid double quotes */ |
return 0; |
|
p = new char[len + 1]; |
|
|
if (!p) |
|
|
BUG (0); |
|
|
_snprintf (p, len, "\"%s\"", name); |
|
77 |
|
|
78 |
return p; |
while (fname && *fname && *fname != '\\') |
79 |
} /* fm_quote_file */ |
drv[i++] = *fname++; |
80 |
|
drv[i++] = '\\'; |
81 |
|
drv[i++] = '\0'; |
82 |
|
i = GetDriveType (drv); |
83 |
|
if (i == DRIVE_REMOVABLE) |
84 |
|
return -1; |
85 |
|
return 0; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
/* Ask the user to overwrite file @fname. |
90 |
|
Return value: 0 for cancel. */ |
91 |
int |
int |
92 |
overwrite_file (const char * fname) |
overwrite_file (const char *fname) |
93 |
{ |
{ |
94 |
int id; |
int id; |
95 |
|
|
99 |
_("\"%s\" already exists.\n" |
_("\"%s\" already exists.\n" |
100 |
"Replace existing file?"), fname); |
"Replace existing file?"), fname); |
101 |
return id == IDNO ? 0 : -1; |
return id == IDNO ? 0 : -1; |
102 |
} /* overwrite_file */ |
} |
103 |
|
|
104 |
|
|
105 |
|
/* Removes 'critical' attributes from the file @fname. |
106 |
|
If @force is 1, the user is not asked for permission. */ |
107 |
static void |
static void |
108 |
remove_crit_file_attrs (const char * fname, int force) |
remove_crit_file_attrs (const char *fname, int force) |
109 |
{ |
{ |
110 |
u32 f_attr; |
u32 f_attr; |
111 |
int id; |
int id; |
123 |
if (id == IDYES) |
if (id == IDYES) |
124 |
SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL); |
SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL); |
125 |
} |
} |
126 |
} /* remove_crit_file_attrs */ |
} |
127 |
|
|
128 |
|
|
129 |
static int inline |
static int inline |
130 |
is_directory (const char * fname) |
is_directory (const char *fname) |
131 |
{ |
{ |
132 |
return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0; |
return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0; |
133 |
} /* is_directory */ |
} |
134 |
|
|
135 |
|
|
136 |
static int inline |
static int inline |
137 |
is_openpgp_ext (const char * name) |
is_openpgp_ext (const char *name) |
138 |
{ |
{ |
139 |
if (strstr (name, ".gpg") || strstr (name, ".asc") |
if (strstr (name, ".gpg") || strstr (name, ".asc") |
140 |
|| strstr (name, ".sig") || strstr (name, ".pgp")) |
|| strstr (name, ".sig") || strstr (name, ".pgp")) |
188 |
if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH) |
if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH) |
189 |
return ".sig"; |
return ".sig"; |
190 |
return ".gpg"; |
return ".gpg"; |
191 |
} /* file_get_extension */ |
} |
192 |
|
|
193 |
|
|
194 |
|
char* |
195 |
|
fm_quote_file (const char * name) |
196 |
|
{ |
197 |
|
char * p; |
198 |
|
size_t len = strlen (name) + 8; |
199 |
|
|
200 |
|
if (*name == '"') |
201 |
|
return m_strdup (name); /* avoid double quotes */ |
202 |
|
p = new char[len + 1]; |
203 |
|
if (!p) |
204 |
|
BUG (0); |
205 |
|
_snprintf (p, len, "\"%s\"", name); |
206 |
|
|
207 |
|
return p; |
208 |
|
} /* fm_quote_file */ |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
/* Check the armor type of the file @fname and return |
213 |
|
a string representation of it. */ |
214 |
|
static const char * |
215 |
|
fm_check_armor_type (const char *fname, int *r_type) |
216 |
|
{ |
217 |
|
FILE * fp; |
218 |
|
char header[768], * p; |
219 |
|
|
220 |
|
if (r_type) |
221 |
|
*r_type = PGP_NONE; |
222 |
|
fp = fopen (fname, "rb"); |
223 |
|
if (!fp) |
224 |
|
return "UNKNOWN"; |
225 |
|
p = fgets (header, sizeof (header) - 1, fp); |
226 |
|
fclose (fp); |
227 |
|
if (!p) |
228 |
|
return "UNKNOWN"; |
229 |
|
|
230 |
|
if (strncmp (header, "-----", 5)) |
231 |
|
goto leave; |
232 |
|
if (strstr( header, "BEGIN PGP PUBLIC KEY" )) { |
233 |
|
if (r_type) *r_type = PGP_PUBKEY; |
234 |
|
return "PUBKEY"; |
235 |
|
} |
236 |
|
else if (strstr (header, "BEGIN PGP PRIVATE KEY") || |
237 |
|
strstr (header, "BEGIN PGP SECRET KEY")) { |
238 |
|
if (r_type) *r_type = PGP_SECKEY; |
239 |
|
return "SECKEY"; |
240 |
|
} |
241 |
|
else if (strstr (header, "BEGIN PGP MESSAGE")) { |
242 |
|
if (r_type) *r_type = PGP_MESSAGE; |
243 |
|
return "ENCRYPTED"; |
244 |
|
} |
245 |
|
else if (strstr( header, "BEGIN PGP SIGNED MESSAGE")) { |
246 |
|
if (r_type) *r_type = PGP_CLEARSIG; |
247 |
|
return "SIGNED-CLEAR"; |
248 |
|
} |
249 |
|
else if (strstr(header, "BEGIN PGP SIGNATURE")) { |
250 |
|
if (r_type) *r_type = PGP_SIG; |
251 |
|
return "SIGNED-DETACH"; |
252 |
|
} |
253 |
|
|
254 |
|
leave: |
255 |
|
return "UNKNOWN"; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
/* Extract file type from @fname. If @r_type is valid, |
260 |
|
it contains the PGP type on success. */ |
261 |
|
static const char * |
262 |
|
fm_get_file_type (const char *fname, int *r_type) |
263 |
|
{ |
264 |
|
gpg_iobuf_t inp; |
265 |
|
armor_filter_context_t afx; |
266 |
|
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
267 |
|
int i = 0, rc = 0; |
268 |
|
const char *s = NULL; |
269 |
|
|
270 |
|
if (r_type) |
271 |
|
*r_type = PGP_NONE; |
272 |
|
if (!fname) { |
273 |
|
safe_free (pkt); |
274 |
|
return NULL; |
275 |
|
} |
276 |
|
|
277 |
|
if (is_floppy_disc (fname)) |
278 |
|
return fm_check_armor_type (fname, r_type); |
279 |
|
|
280 |
|
inp = gpg_iobuf_open (fname); |
281 |
|
if (!inp) { |
282 |
|
const char *s = winpt_strerror (WPTERR_FILE_OPEN); |
283 |
|
log_box( _("File Manager"), MB_ERR, "\"%s\": %s", fname, s ); |
284 |
|
safe_free( pkt ); |
285 |
|
return NULL; |
286 |
|
} |
287 |
|
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
288 |
|
if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */ |
289 |
|
&& !is_openpgp_ext (fname)) { |
290 |
|
gpg_iobuf_close (inp); |
291 |
|
return "UNKNOWN"; |
292 |
|
} |
293 |
|
|
294 |
|
if (gpg_use_armor_filter(inp)) { |
295 |
|
memset (&afx, 0, sizeof (afx)); |
296 |
|
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
297 |
|
} |
298 |
|
|
299 |
|
gpg_init_packet (pkt); |
300 |
|
while (!(rc = gpg_parse_packet (inp, pkt))) { |
301 |
|
switch (pkt->pkttype) { |
302 |
|
case PKT_PUBKEY_ENC: |
303 |
|
s = "ENCRYPTED";rc = -2; |
304 |
|
if (r_type) *r_type = PGP_MESSAGE; |
305 |
|
break; |
306 |
|
case PKT_SYMKEY_ENC: |
307 |
|
case PKT_ENCRYPTED: |
308 |
|
s = "SYMKEYENC";rc = -2; |
309 |
|
if (r_type) *r_type = PGP_MESSAGE; |
310 |
|
break; |
311 |
|
case PKT_SIGNATURE: |
312 |
|
case PKT_ONEPASS_SIG: |
313 |
|
s = "SIGNED"; rc = -2; |
314 |
|
if (r_type) *r_type = PGP_SIG; |
315 |
|
break; |
316 |
|
case PKT_PUBLIC_KEY: |
317 |
|
s = "PUBKEY"; rc = -2; |
318 |
|
if (r_type) *r_type = PGP_PUBKEY; |
319 |
|
break; |
320 |
|
case PKT_SECRET_KEY: |
321 |
|
s = "SECKEY"; rc = -2; |
322 |
|
if (r_type) *r_type = PGP_SECKEY; |
323 |
|
break; |
324 |
|
} |
325 |
|
gpg_free_packet (pkt); |
326 |
|
gpg_init_packet (pkt); |
327 |
|
if (rc == -2) |
328 |
|
break; /* found */ |
329 |
|
} |
330 |
|
safe_free (pkt); |
331 |
|
gpg_iobuf_close (inp); |
332 |
|
if (!s) |
333 |
|
s = fm_check_armor_type (fname, r_type); |
334 |
|
if (!s) |
335 |
|
s = "UNKNOWN"; |
336 |
|
if (!strcmp (s, "SIGNED") |
337 |
|
&& strcmp (fm_check_armor_type (fname, r_type), "SIGNED-CLEAR ")) { |
338 |
|
if (r_type) *r_type = PGP_SIG; |
339 |
|
s = "SIGNED-DETACH"; |
340 |
|
} |
341 |
|
return s; |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
int |
int |
346 |
fm_build( listview_ctrl_t *lv, HWND ctrl ) |
fm_build (listview_ctrl_t *lv, HWND ctrl) |
347 |
{ |
{ |
348 |
int i, rc = 0; |
int i, rc = 0; |
349 |
listview_ctrl_t c; |
listview_ctrl_t c; |
433 |
|
|
434 |
|
|
435 |
static int |
static int |
436 |
fm_set_ftype (listview_ctrl_t lv, const char * name) |
fm_set_ftype (listview_ctrl_t lv, const char *name) |
437 |
{ |
{ |
438 |
const char *type; |
const char *type; |
439 |
int rc; |
int rc; |
441 |
rc = fm_check_for_entry (lv, name); |
rc = fm_check_for_entry (lv, name); |
442 |
if (rc) |
if (rc) |
443 |
return 0; |
return 0; |
444 |
type = fm_get_file_type (name); |
type = fm_get_file_type (name, NULL); |
445 |
if (!type || !strcmp (type, "UNKNOWN")) |
if (!type || !strcmp (type, "UNKNOWN")) |
446 |
type = gnupg_check_file_ext (name); |
type = gnupg_check_file_ext (name, NULL); |
447 |
rc = listview_add_item (lv, " "); |
rc = listview_add_item (lv, " "); |
448 |
if (rc) |
if (rc) |
449 |
return -1; |
return -1; |
507 |
{ |
{ |
508 |
OPENFILENAME open; |
OPENFILENAME open; |
509 |
const char *type; |
const char *type; |
510 |
char file[1024] = ""; |
char file[512] = ""; |
511 |
int rc; |
int rc; |
512 |
|
|
513 |
memset( &open, 0, sizeof (open) ); |
/* XXX: support to add multiple files. */ |
514 |
|
memset (&open, 0, sizeof (open)); |
515 |
open.lStructSize = sizeof (OPENFILENAME); |
open.lStructSize = sizeof (OPENFILENAME); |
516 |
open.hInstance = glob_hinst; |
open.hInstance = glob_hinst; |
517 |
open.lpstrTitle = _("File Open"); |
open.lpstrTitle = _("File Open"); |
522 |
open.Flags = 0; |
open.Flags = 0; |
523 |
|
|
524 |
if (GetOpenFileName (&open)) { |
if (GetOpenFileName (&open)) { |
525 |
type = fm_get_file_type (open.lpstrFile); |
type = fm_get_file_type (open.lpstrFile, NULL); |
526 |
if (!type) |
if (!type) |
527 |
return WPTERR_FILE_OPEN; |
return WPTERR_FILE_OPEN; |
528 |
if (!strcmp (type, "UNKNOWN")) |
if (!strcmp (type, "UNKNOWN")) |
529 |
type = gnupg_check_file_ext (open.lpstrFile); |
type = gnupg_check_file_ext (open.lpstrFile, NULL); |
530 |
rc = listview_add_item (lv, ""); |
rc = listview_add_item (lv, ""); |
531 |
if( !rc ) { |
if (!rc) { |
532 |
listview_add_sub_item (lv, 0, 0, type); |
listview_add_sub_item (lv, 0, 0, type); |
533 |
listview_add_sub_item (lv, 0, 1, open.lpstrFile); |
listview_add_sub_item (lv, 0, 1, open.lpstrFile); |
534 |
} |
} |
538 |
} |
} |
539 |
|
|
540 |
|
|
|
/* Check the armor type of the file @fname and return |
|
|
a string representation of it. */ |
|
|
static const char * |
|
|
fm_check_armor_type (const char *fname) |
|
|
{ |
|
|
FILE * fp; |
|
|
char header[768], * p; |
|
|
|
|
|
fp = fopen (fname, "rb"); |
|
|
if (!fp) |
|
|
return "UNKNOWN"; |
|
|
p = fgets (header, sizeof (header) - 1, fp); |
|
|
fclose (fp); |
|
|
if (!p) |
|
|
return "UNKNOWN"; |
|
|
|
|
|
if( strncmp( header, "-----", 5 ) ) |
|
|
goto leave; |
|
|
if( strstr( header, "BEGIN PGP PUBLIC KEY" ) ) |
|
|
return "PUBKEY"; |
|
|
else if( strstr( header, "BEGIN PGP PRIVATE KEY" ) ) |
|
|
return "SECKEY"; |
|
|
else if( strstr( header, "BEGIN PGP SECRET KEY" ) ) |
|
|
return "SECKEY"; |
|
|
else if( strstr( header, "BEGIN PGP MESSAGE" ) ) |
|
|
return "ENCRYPTED"; |
|
|
else if( strstr( header, "BEGIN PGP SIGNED MESSAGE" ) ) |
|
|
return "SIGNED-CLEAR"; |
|
|
else if( strstr(header, "BEGIN PGP SIGNATURE" ) ) |
|
|
return "SIGNED-DETACH"; |
|
|
|
|
|
leave: |
|
|
return "UNKNOWN"; |
|
|
} |
|
|
|
|
|
|
|
541 |
int |
int |
542 |
fm_assume_onepass_sig (const char * fname) |
fm_assume_onepass_sig (const char * fname) |
543 |
{ |
{ |
547 |
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
548 |
int check = 0; |
int check = 0; |
549 |
|
|
550 |
if (!fname) |
if (!fname) { |
|
{ |
|
551 |
gpg_data_new_from_clipboard (&dat, 0); |
gpg_data_new_from_clipboard (&dat, 0); |
552 |
gpg_data_release_and_set_file (dat, "gpgme.tmp"); |
gpg_data_release_and_set_file (dat, "gpgme.tmp"); |
553 |
|
|
555 |
if (!fp) |
if (!fp) |
556 |
return 0; |
return 0; |
557 |
gpg_iobuf_ioctl (fp, 3, 1, NULL); |
gpg_iobuf_ioctl (fp, 3, 1, NULL); |
558 |
if (gpg_use_armor_filter(fp)) |
if (gpg_use_armor_filter(fp)) { |
|
{ |
|
559 |
memset (&afx, 0, sizeof (afx)); |
memset (&afx, 0, sizeof (afx)); |
560 |
gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx); |
gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx); |
561 |
} |
} |
573 |
} |
} |
574 |
|
|
575 |
|
|
|
static int |
|
|
is_floppy_disc (const char * fname) |
|
|
{ |
|
|
char drv[32] = {0}; |
|
|
int i=0; |
|
|
|
|
|
if (!strstr (fname, ":\\")) |
|
|
return 0; |
|
|
|
|
|
while (fname && *fname && *fname != '\\') |
|
|
drv[i++] = *fname++; |
|
|
drv[i++] = '\\'; |
|
|
drv[i++] = '\0'; |
|
|
i = GetDriveType (drv); |
|
|
if (i == DRIVE_REMOVABLE) |
|
|
return -1; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
const char * |
|
|
fm_get_file_type (const char * fname) |
|
|
{ |
|
|
gpg_iobuf_t inp; |
|
|
armor_filter_context_t afx; |
|
|
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
|
|
int i = 0, rc = 0; |
|
|
const char * s = NULL; |
|
|
|
|
|
if (!fname) { |
|
|
safe_free (pkt); |
|
|
return NULL; |
|
|
} |
|
|
|
|
|
if (is_floppy_disc (fname)) |
|
|
return fm_check_armor_type (fname); |
|
|
|
|
|
inp = gpg_iobuf_open (fname); |
|
|
if (!inp) { |
|
|
const char *s = winpt_strerror (WPTERR_FILE_OPEN); |
|
|
log_box( _("File Manager"), MB_ERR, "\"%s\": %s", fname, s ); |
|
|
safe_free( pkt ); |
|
|
return NULL; |
|
|
} |
|
|
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
|
|
if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */ |
|
|
&& !is_openpgp_ext (fname)) { |
|
|
gpg_iobuf_close (inp); |
|
|
return "UNKNOWN"; |
|
|
} |
|
|
|
|
|
if (gpg_use_armor_filter(inp)) { |
|
|
memset (&afx, 0, sizeof (afx)); |
|
|
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
|
|
} |
|
|
|
|
|
gpg_init_packet (pkt); |
|
|
while (!(rc = gpg_parse_packet (inp, pkt))) { |
|
|
switch (pkt->pkttype) { |
|
|
case PKT_PUBKEY_ENC: s = "ENCRYPTED";rc = -2; break; |
|
|
case PKT_SYMKEY_ENC: |
|
|
case PKT_ENCRYPTED: s = "SYMKEYENC";rc = -2; break; |
|
|
case PKT_SIGNATURE: |
|
|
case PKT_ONEPASS_SIG: s = "SIGNED"; rc = -2; break; |
|
|
case PKT_PUBLIC_KEY: s = "PUBKEY"; rc = -2; break; |
|
|
case PKT_SECRET_KEY: s = "SECKEY"; rc = -2; break; |
|
|
} |
|
|
gpg_free_packet (pkt); |
|
|
gpg_init_packet (pkt); |
|
|
if (rc == -2) |
|
|
break; /* found */ |
|
|
} |
|
|
safe_free (pkt); |
|
|
gpg_iobuf_close (inp); |
|
|
if (!s) |
|
|
s = fm_check_armor_type (fname); |
|
|
if (!s) |
|
|
s = "UNKNOWN"; |
|
|
if (!strcmp( s, "SIGNED") |
|
|
&& strcmp (fm_check_armor_type (fname), "SIGNED-CLEAR ")) |
|
|
s = "SIGNED-DETACH"; |
|
|
return s; |
|
|
} /* fm_get_file_type */ |
|
|
|
|
|
|
|
576 |
int |
int |
577 |
fm_get_current_pos (listview_ctrl_t lv) |
fm_get_current_pos (listview_ctrl_t lv) |
578 |
{ |
{ |
737 |
return n; |
return n; |
738 |
} |
} |
739 |
|
|
|
|
|
740 |
int |
int |
741 |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
742 |
{ |
{ |
773 |
/* we use it here to make sure that pfx_cleanup will not use |
/* we use it here to make sure that pfx_cleanup will not use |
774 |
any weird values. */ |
any weird values. */ |
775 |
memset (&pfx, 0, sizeof (pfx)); |
memset (&pfx, 0, sizeof (pfx)); |
776 |
if (cmd != FM_VERIFY && cmd != FM_SIGN && reg_prefs.fm.progress > 0) { |
ctx->prog_cb = NULL; |
777 |
|
if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) { |
778 |
pfx.hwnd = dlg; |
pfx.hwnd = dlg; |
779 |
gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx); |
/*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/ |
780 |
|
/*ctx->prog_cb = &pfx;*/ |
781 |
} |
} |
782 |
|
|
783 |
/* Commands we need before we can perform the main command */ |
/* Commands we need before we can perform the main command */ |
855 |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
856 |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
857 |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
858 |
case FM_VERIFY: rc = fm_verify( ctx, sig_detached, fname );break; |
case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break; |
859 |
case FM_IMPORT: |
case FM_IMPORT: |
860 |
free_if_alloc (ctx->opaque); |
free_if_alloc (ctx->opaque); |
861 |
ctx->opaque = m_strdup (fname); |
ctx->opaque = m_strdup (fname); |
877 |
progress_cleanup (&pfx2); |
progress_cleanup (&pfx2); |
878 |
} |
} |
879 |
if (ctx->cache_cb) { |
if (ctx->cache_cb) { |
880 |
memset (ctx->pass_cb.pwd, 0, sizeof (ctx->pass_cb)); |
release_gpg_passphrase_cb (&ctx->pass_cb); |
881 |
ctx->cache_cb = 0; /* make sure it's only used for this session! */ |
ctx->cache_cb = 0; /* make sure it's only used for this session! */ |
882 |
} |
} |
883 |
|
|
934 |
if (!s) |
if (!s) |
935 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
936 |
|
|
937 |
free_if_alloc (*dst); |
if (dst != NULL) |
938 |
|
free_if_alloc (*dst); |
939 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
940 |
c->output = m_strdup (s); |
c->output = m_strdup (s); |
941 |
if (!c->output) |
if (!c->output) |
947 |
|
|
948 |
|
|
949 |
int |
int |
950 |
fm_encrypt (fm_state_t c, const char * name, int sign) |
fm_encrypt (fm_state_t c, const char *name, int sign) |
951 |
{ |
{ |
952 |
gpgme_error_t err; |
gpgme_error_t err; |
953 |
gpgme_key_t key = NULL; |
gpgme_key_t key = NULL; |
954 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
955 |
file_data_t in=NULL, out=NULL; |
file_data_t in=NULL, out=NULL; |
956 |
char * keyid = NULL, ext[5]; |
char *keyid = NULL, ext[5]; |
957 |
int no_compr = 0; |
int no_compr = 0; |
958 |
int rc = 0; |
int rc = 0; |
959 |
|
|
|
|
|
960 |
c->output = new char[strlen (name) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
961 |
if (!c->output) |
if (!c->output) |
962 |
BUG (0); |
BUG (0); |
970 |
goto leave; |
goto leave; |
971 |
} |
} |
972 |
|
|
973 |
err = gpg_file_data_new (name, &in, 1); |
err = gpg_file_data_new (name, 1, &in); |
974 |
if (err) |
if (err) |
975 |
goto leave; |
goto leave; |
976 |
err = gpg_file_data_new (c->output, &out, 0); |
err = gpg_file_data_new (c->output, 0, &out); |
977 |
if (err) |
if (err) |
978 |
goto leave; |
goto leave; |
979 |
|
|
980 |
|
/* |
981 |
|
if (c->prog_cb) { |
982 |
|
c->prog_cb->what = name; |
983 |
|
gpg_file_data_set_cb (in, c->prog_cb); |
984 |
|
} |
985 |
|
*/ |
986 |
|
|
987 |
/* XXX |
/* XXX |
988 |
no_compr = is_multi_media (name); |
no_compr = is_multi_media (name); |
1018 |
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1019 |
in->dat, out->dat); |
in->dat, out->dat); |
1020 |
if (!c->cache_cb) |
if (!c->cache_cb) |
1021 |
memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd)); |
release_gpg_passphrase_cb (&c->pass_cb); |
1022 |
if (c->pass_cb.cancel) { |
if (c->pass_cb.cancel) { |
1023 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1024 |
goto leave; |
goto leave; |
1025 |
} |
} |
1026 |
if (err) { |
if (err) { |
1027 |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
1028 |
if (err == gpg_error (GPG_ERR_BAD_PASSPHRASE)) |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
1029 |
agent_del_cache (keyid); |
agent_del_cache (keyid); |
1030 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1031 |
goto leave; |
goto leave; |
1032 |
} |
} |
|
gpgme_key_release (key); |
|
1033 |
} |
} |
1034 |
else { |
else { |
1035 |
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1036 |
in->dat, out->dat); |
in->dat, out->dat); |
1037 |
if (err) { |
if (err) { |
1038 |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
1082 |
|
|
1083 |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
1084 |
|
|
1085 |
err = gpg_file_data_new (name, &in, 1); |
err = gpg_file_data_new (name, 1, &in); |
1086 |
if (err) |
if (err) |
1087 |
goto leave; |
goto leave; |
1088 |
err = gpg_file_data_new (c->output, &out, 0); |
err = gpg_file_data_new (c->output, 0, &out); |
1089 |
if (err) |
if (err) |
1090 |
goto leave; |
goto leave; |
1091 |
|
|
1110 |
} /* fm_sym_encrypt */ |
} /* fm_sym_encrypt */ |
1111 |
|
|
1112 |
|
|
1113 |
|
/* Show the human readable verify result from @sigres. */ |
1114 |
|
static void |
1115 |
|
show_verify_result (gpgme_verify_result_t sigres) |
1116 |
|
{ |
1117 |
|
gpgme_key_t key=NULL; |
1118 |
|
gpgme_signature_t sig=sigres->signatures; |
1119 |
|
const char *s, *keyid; |
1120 |
|
int sigok = 0; |
1121 |
|
int type; |
1122 |
|
char buf[384]; |
1123 |
|
|
1124 |
|
sig = sigres->signatures; |
1125 |
|
sigok = sig->summary & GPGME_SIGSUM_GREEN; |
1126 |
|
s = sigok? _("Good signature") : _("BAD signature"); |
1127 |
|
type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
1128 |
|
keyid = sig->fpr; |
1129 |
|
if (!keyid) |
1130 |
|
return; |
1131 |
|
|
1132 |
|
keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24; |
1133 |
|
get_pubkey (sig->fpr, &key); |
1134 |
|
_snprintf (buf, sizeof (buf)-1, "Signature made %s using %s key ID %s\n" |
1135 |
|
"%s from \"%s\"", |
1136 |
|
strtimestamp (sig->timestamp), get_key_pubalgo (sig->pubkey_algo), |
1137 |
|
keyid, s, key? key->uids->uid : _("user ID not found")); |
1138 |
|
msg_box (NULL, buf, _("Decrypt Verify"), type); |
1139 |
|
} |
1140 |
|
|
1141 |
|
|
1142 |
|
/* Check the recipients if we have at least one secret key. */ |
1143 |
|
bool |
1144 |
|
secret_key_available (gpgme_recipient_t rset) |
1145 |
|
{ |
1146 |
|
gpgme_recipient_t r; |
1147 |
|
gpgme_key_t key; |
1148 |
|
|
1149 |
|
for (r=rset; r; r = r->next) { |
1150 |
|
if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY) |
1151 |
|
continue; |
1152 |
|
else { |
1153 |
|
/* extra check to make sure the key is available right now. */ |
1154 |
|
if (!get_seckey (r->keyid, &key)) |
1155 |
|
return true; |
1156 |
|
} |
1157 |
|
} |
1158 |
|
return false; |
1159 |
|
} |
1160 |
|
|
1161 |
|
|
1162 |
/* Decrypt the file @name. */ |
/* Decrypt the file @name. */ |
1163 |
int |
int |
1164 |
fm_decrypt (fm_state_t c, const char *name) |
fm_decrypt (fm_state_t c, const char *name) |
1165 |
{ |
{ |
1166 |
gpgme_error_t err; |
gpgme_error_t err; |
1167 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
|
gpgme_signature_t sig = NULL; |
|
1168 |
gpgme_decrypt_result_t res; |
gpgme_decrypt_result_t res; |
1169 |
gpgme_verify_result_t sigres; |
gpgme_verify_result_t sigres; |
1170 |
file_data_t in =NULL, out=NULL; |
file_data_t in =NULL, out=NULL; |
1171 |
char keyid[17]; |
int is_signed = 0; |
|
int is_signed = 0, sigok = 0; |
|
1172 |
int rc = 0; |
int rc = 0; |
1173 |
|
|
1174 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1183 |
if (is_openpgp_ext (c->output)) |
if (is_openpgp_ext (c->output)) |
1184 |
c->output[strlen (c->output)-4] = '\0'; |
c->output[strlen (c->output)-4] = '\0'; |
1185 |
else { |
else { |
1186 |
const char *s = get_filename_dlg (c->dlg, FILE_SAVE, |
const char *s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"), |
|
_("Choose Filename for Output"), |
|
1187 |
NULL, NULL); |
NULL, NULL); |
1188 |
if( s ) { |
if (s) { |
1189 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
1190 |
c->output = m_strdup (s); |
c->output = m_strdup (s); |
1191 |
if (!c->output) |
if (!c->output) |
1201 |
|
|
1202 |
remove_crit_file_attrs (c->output, 0); |
remove_crit_file_attrs (c->output, 0); |
1203 |
|
|
1204 |
err = gpg_file_data_new (name, &in, 1); |
err = gpg_file_data_new (name, 1, &in); |
1205 |
if (err) |
if (err) |
1206 |
goto leave; |
goto leave; |
1207 |
err = gpg_file_data_new (c->output, &out, 0); |
err = gpg_file_data_new (c->output, 0, &out); |
1208 |
if (err) |
if (err) |
1209 |
goto leave; |
goto leave; |
1210 |
|
|
1211 |
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
1212 |
if (!c->cache_cb) |
if (!c->cache_cb) |
1213 |
memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd)); |
release_gpg_passphrase_cb (&c->pass_cb); |
1214 |
if (c->pass_cb.cancel) { |
if (c->pass_cb.cancel) { |
1215 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1216 |
goto leave; |
goto leave; |
1217 |
} |
} |
1218 |
|
|
1219 |
res = gpgme_op_decrypt_result (ctx); |
res = gpgme_op_decrypt_result (ctx); |
1220 |
if (res && err == gpg_error (GPG_ERR_NO_SECKEY)) { |
if (res && res->recipients && !secret_key_available (res->recipients)) { |
1221 |
|
const char *keyid = res->recipients->keyid; |
1222 |
char *p = get_key_userid (keyid+8); |
char *p = get_key_userid (keyid+8); |
1223 |
gpgme_pubkey_algo_t pkalgo = (gpgme_pubkey_algo_t)0; |
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
1224 |
|
|
|
if (res->recipients) |
|
|
pkalgo = res->recipients->pubkey_algo; |
|
1225 |
log_box( _("Decryption"), MB_ERR, |
log_box( _("Decryption"), MB_ERR, |
1226 |
_("Encrypted with %s key, ID %s.%s\n" |
_("Encrypted with %s key, ID %s.%s\n" |
1227 |
"Decryption failed: secret key not available."), |
"Decryption failed: secret key not available."), |
1231 |
goto leave; |
goto leave; |
1232 |
} |
} |
1233 |
else if (err) { |
else if (err) { |
1234 |
msg_box( c->dlg, gpgme_strerror( err ), _("Decrypt"), MB_ERR ); |
msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR); |
1235 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1236 |
goto leave; |
goto leave; |
1237 |
} |
} |
1238 |
if( file_exist_check( c->output ) ) { |
if (file_exist_check (c->output)) { |
1239 |
msg_box( c->dlg, _("Decryption failed"), _("Decrypt"), MB_ERR ); |
log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output); |
1240 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1241 |
} |
} |
1242 |
|
|
1243 |
sigres = gpgme_op_verify_result (ctx); |
sigres = gpgme_op_verify_result (ctx); |
1244 |
if (sigres) { |
if (sigres && sigres->signatures) |
1245 |
sig = sigres->signatures; |
show_verify_result (sigres); |
|
sigok = sig->summary & GPGME_SIGSUM_GREEN; |
|
|
const char *s = sigok? _("Good signature") : _("BAD signature"); |
|
|
int type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
|
|
gpgme_key_t key; |
|
|
const char *keyid = sig->fpr; |
|
|
if (!keyid) |
|
|
keyid = "????????????????"; |
|
|
else { |
|
|
keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24; |
|
1246 |
|
|
|
if (get_pubkey (keyid+8, &key)) |
|
|
log_box (_("Verify"), type, _("%s using keyID 0x%s"), s, keyid+8); |
|
|
else |
|
|
log_box (_("Verify"), type, "%s using keyID 0x%08X from %s", |
|
|
s, keyid, _("no user ID")); |
|
|
} |
|
|
} |
|
1247 |
|
|
1248 |
leave: |
leave: |
1249 |
if (in) |
if (in) |
1263 |
file_data_t in=NULL, out=NULL; |
file_data_t in=NULL, out=NULL; |
1264 |
char ext[5]; |
char ext[5]; |
1265 |
|
|
1266 |
if( !c->init_cb || !c->cache_cb ) { |
if (!c->init_cb || !c->cache_cb) { |
1267 |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
1268 |
c->init_cb = 1; |
c->init_cb = 1; |
1269 |
} |
} |
1270 |
|
|
1271 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
1272 |
c->output = new char[strlen( name ) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
1273 |
if( !c->output) |
if( !c->output) |
1274 |
BUG( NULL ); |
BUG( NULL ); |
1275 |
strcpy( ext, file_get_extension( ctx, c->sigmode ) ); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1276 |
strcpy( c->output, name ); |
strcpy (c->output, name); |
1277 |
strcat( c->output, ext ); |
strcat (c->output, ext); |
1278 |
|
|
1279 |
if (!overwrite_file (c->output)) { |
if (!overwrite_file (c->output)) { |
1280 |
rc = ask_filename (c, _("Enter filename for signed file"), NULL); |
rc = ask_filename (c, _("Enter filename for signed file"), NULL); |
1281 |
if (rc) |
if (rc) |
1282 |
goto leave; |
goto leave; |
1283 |
} |
} |
1284 |
remove_crit_file_attrs( c->output, 0 ); |
remove_crit_file_attrs (c->output, 0); |
1285 |
|
|
1286 |
err = gpg_file_data_new (name, &in, 1); |
err = gpg_file_data_new (name, 1, &in); |
1287 |
if (err) |
if (err) |
1288 |
goto leave; |
goto leave; |
1289 |
err = gpg_file_data_new (c->output, &out, 0); |
err = gpg_file_data_new (c->output, 0, &out); |
1290 |
if (err) |
if (err) |
1291 |
goto leave; |
goto leave; |
1292 |
|
|
1293 |
err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode); |
err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode); |
1294 |
if( !c->cache_cb ) |
if( !c->cache_cb ) |
1295 |
memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) ); |
release_gpg_passphrase_cb (&c->pass_cb); |
1296 |
if( c->pass_cb.cancel ) { |
if( c->pass_cb.cancel ) { |
1297 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1298 |
goto leave; |
goto leave; |
1309 |
if (out) |
if (out) |
1310 |
gpg_file_data_release (out); |
gpg_file_data_release (out); |
1311 |
return rc; |
return rc; |
1312 |
} /* fm_sign */ |
} |
1313 |
|
|
1314 |
|
|
1315 |
static int |
static int |
1316 |
fm_add_sig_stat( siglog_context_t log ) |
fm_add_sig_stat (file_sig_ctx_t log) |
1317 |
{ |
{ |
1318 |
gpgme_key_t key; |
gpgme_key_t key; |
1319 |
const char *kid; |
const char *kid; |
1321 |
|
|
1322 |
kid = log->sig->fpr; |
kid = log->sig->fpr; |
1323 |
if (!kid) |
if (!kid) |
1324 |
kid = "????????????????"; |
BUG (NULL); |
1325 |
if (strlen (kid) == 40) |
if (strlen (kid) == 40) |
1326 |
kid += 32; |
kid += 32; |
1327 |
else |
else if (strlen (kid) == 32) |
1328 |
kid += 24; |
kid += 24; |
1329 |
if (get_pubkey (kid, &key)) |
if (get_pubkey (kid, &key)) |
1330 |
not_found = 1; |
log->use_uid = 0; |
1331 |
log->use_uid = 0; |
else { |
|
if (!not_found) { |
|
1332 |
log->user_id = key->uids->uid; |
log->user_id = key->uids->uid; |
1333 |
log->use_uid = 1; |
log->use_uid = 1; |
1334 |
} |
} |
1335 |
file_verify_add_state (log); |
file_verify_add_state (log); |
|
|
|
1336 |
return 0; |
return 0; |
1337 |
} /* fm_add_sig_stat */ |
} |
1338 |
|
|
1339 |
|
|
1340 |
static int |
static int |
1405 |
} |
} |
1406 |
|
|
1407 |
|
|
1408 |
int |
/* Extract automatically the output file name from @name. |
1409 |
fm_verify (fm_state_t c, int detached, const char *name) |
If @detached is 1, a detached sig is assumed. */ |
1410 |
|
static int |
1411 |
|
get_output_file (fm_state_t c, const char *name, int detached) |
1412 |
{ |
{ |
1413 |
gpgme_ctx_t ctx = c->ctx; |
const char *file = NULL; |
1414 |
gpgme_error_t err; |
const char *title; |
1415 |
gpgme_signature_t s; |
char fname[384]; |
1416 |
gpgme_verify_result_t res; |
|
1417 |
struct siglog_context_s log; |
if (detached) |
1418 |
file_data_t in=NULL, out=NULL; |
title = _("Select Data File"); |
1419 |
int rc = 0; |
else |
1420 |
|
title = _("Selected Output File"); |
1421 |
if (detached) { |
|
1422 |
const char *file = NULL; |
if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) { |
1423 |
if (strstr (name, ".sig") || strstr (name, ".asc")) { |
_snprintf (fname, sizeof (fname) - 1, "%s", name); |
1424 |
char fname[512]; |
fname[strlen (fname) - 4] = '\0'; |
1425 |
_snprintf (fname, sizeof (fname) - 1, "%s", name); |
if (file_exist_check (fname) == 0 && detached) |
1426 |
fname[strlen (fname) - 4] = '\0'; |
file = fname; |
1427 |
if (file_exist_check (fname) == 0) |
else if (!detached) { |
1428 |
|
/* If the signature is clear or normal, make sure we do not |
1429 |
|
overwrite the original file if it exists. */ |
1430 |
|
if (file_exist_check (fname) == 0 && !overwrite_file (fname)) { |
1431 |
|
file = get_filesave_dlg (c->dlg, title, NULL, NULL); |
1432 |
|
if (!file) |
1433 |
|
return WPTERR_GENERAL; |
1434 |
|
} |
1435 |
|
else |
1436 |
file = fname; |
file = fname; |
|
} |
|
|
if (!file) |
|
|
file = get_filename_dlg (c->dlg, FILE_OPEN, _("Select Data File"), NULL, NULL); |
|
|
if (file) { |
|
|
free_if_alloc (c->output); |
|
|
c->output = m_strdup (file); |
|
1437 |
} |
} |
|
else { |
|
|
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
c->sigmode = GPGME_SIG_MODE_DETACH; |
|
1438 |
} |
} |
1439 |
|
if (!file) |
1440 |
|
file = get_fileopen_dlg (c->dlg, title, NULL, NULL); |
1441 |
|
if (file) { |
1442 |
|
free_if_alloc (c->output); |
1443 |
|
c->output = m_strdup (file); |
1444 |
|
if (!c->output) |
1445 |
|
BUG (NULL); |
1446 |
|
} |
1447 |
|
else { |
1448 |
|
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
1449 |
|
return WPTERR_GENERAL; |
1450 |
|
} |
1451 |
|
if (detached) |
1452 |
|
c->sigmode = GPGME_SIG_MODE_DETACH; |
1453 |
else { |
else { |
1454 |
if (strstr (name, ".asc")) |
if (strstr (name, ".asc")) |
1455 |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
1456 |
else |
else |
1457 |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
1458 |
} |
} |
1459 |
|
return 0; |
1460 |
|
} |
1461 |
|
|
1462 |
|
|
1463 |
|
/* Verify the signature from the file @name. If @detached 1, |
1464 |
|
it is assumed that a detached signature should be checked. */ |
1465 |
|
int |
1466 |
|
fm_verify (fm_state_t c, int detached, const char *name) |
1467 |
|
{ |
1468 |
|
gpgme_ctx_t ctx = c->ctx; |
1469 |
|
gpgme_error_t err; |
1470 |
|
gpgme_signature_t s; |
1471 |
|
gpgme_verify_result_t res; |
1472 |
|
struct file_sig_ctx_s log; |
1473 |
|
file_data_t in=NULL, out=NULL; |
1474 |
|
int rc = 0; |
1475 |
|
|
1476 |
|
if (strstr (name, ".sig")) |
1477 |
|
detached = 1; |
1478 |
|
|
1479 |
|
if (get_output_file (c, name, detached)) |
1480 |
|
return WPTERR_GENERAL; |
1481 |
|
|
1482 |
memset (&log, 0, sizeof (log)); |
memset (&log, 0, sizeof (log)); |
1483 |
strcpy (log.file, name); /* XXX: check bounds */ |
log.file = m_strdup (name); |
1484 |
|
if (!log.file) |
1485 |
|
BUG (NULL); |
1486 |
file_verify_create_dlg (); |
file_verify_create_dlg (); |
1487 |
|
|
1488 |
err = gpg_file_data_new (name, &in, 1); |
err = gpg_file_data_new (name, 1, &in); |
1489 |
if (err) |
if (err) |
1490 |
goto leave; |
goto leave; |
1491 |
err = gpg_file_data_new (c->output, &out, 0); |
err = gpg_file_data_new (c->output, detached? 1 : 0, &out); |
1492 |
if (err) |
if (err) |
1493 |
goto leave; |
goto leave; |
1494 |
|
|
1495 |
if (c->sigmode == GPGME_SIG_MODE_DETACH) |
if (c->sigmode == GPGME_SIG_MODE_DETACH) |
|
err = gpgme_op_verify (ctx, in->dat, NULL, out->dat); |
|
|
else |
|
1496 |
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
1497 |
|
else |
1498 |
res = gpgme_op_verify_result (ctx); |
err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat); |
|
if (err == gpg_error (GPG_ERR_BAD_SIGNATURE)) { |
|
|
log.sig = res->signatures; |
|
|
fm_add_sig_stat (&log); |
|
|
rc = WPTERR_GENERAL; |
|
|
goto leave; |
|
|
} |
|
1499 |
if (err) { |
if (err) { |
1500 |
msg_box (c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
1501 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1502 |
goto leave; |
goto leave; |
1503 |
} |
} |
1504 |
|
|
1505 |
|
res = gpgme_op_verify_result (ctx); |
1506 |
for (s=res->signatures; s; s=s->next) { |
for (s=res->signatures; s; s=s->next) { |
1507 |
log.sig = s; |
log.sig = s; |
1508 |
fm_add_sig_stat (&log); |
fm_add_sig_stat (&log); |
1509 |
} |
} |
1510 |
if (!c->output) |
if (!c->output) |
1511 |
c->output = m_strdup( name ); /* for later use */ |
c->output = m_strdup (name); /* for later use */ |
1512 |
|
|
1513 |
leave: |
leave: |
1514 |
if (in) |
if (in) |
1515 |
gpg_file_data_release (in); |
gpg_file_data_release (in); |
1516 |
if (out) |
if (out) |
1517 |
gpg_file_data_release (out); |
gpg_file_data_release (out); |
1518 |
|
if (log.file) |
1519 |
|
delete []log.file; |
1520 |
return rc; |
return rc; |
1521 |
} /* fm_verify */ |
} |
1522 |
|
|
1523 |
|
|
1524 |
int |
int |
1535 |
if (!c->output) |
if (!c->output) |
1536 |
BUG (NULL); |
BUG (NULL); |
1537 |
|
|
1538 |
err = gpg_file_data_new (name, &keydata, 1); |
err = gpg_file_data_new (name, 1, &keydata); |
1539 |
if (err) |
if (err) |
1540 |
goto leave; |
goto leave; |
1541 |
|
|
1596 |
if (!name) |
if (!name) |
1597 |
name = "keys.gpg"; |
name = "keys.gpg"; |
1598 |
|
|
1599 |
patt = gpg_keylist_to_pattern (rset); |
patt = gpg_keylist_to_pattern (rset, c->n_recp); |
1600 |
|
|
1601 |
err = gpg_file_data_new (name, &keydata, 0); |
err = gpg_file_data_new (name, 0, &keydata); |
1602 |
if (err) |
if (err) |
1603 |
goto leave; |
goto leave; |
1604 |
|
|
1621 |
} |
} |
1622 |
|
|
1623 |
|
|
1624 |
|
/* Parse the command line and process the given file. */ |
1625 |
int |
int |
1626 |
fm_parse_command_line (char *cmdl) |
fm_parse_command_line (char *cmdl) |
1627 |
{ |
{ |
1629 |
const char *s; |
const char *s; |
1630 |
char *p, *fn = NULL; |
char *p, *fn = NULL; |
1631 |
int count = 0, detached = 0; |
int count = 0, detached = 0; |
1632 |
|
int type; |
1633 |
|
|
1634 |
if( !cmdl || !*cmdl ) |
if (!cmdl || !*cmdl) |
1635 |
return 0; |
return 0; |
1636 |
|
|
1637 |
fm_state_new( &ctx ); |
fm_state_new (&ctx); |
1638 |
ctx->dlg = GetActiveWindow( ); |
ctx->dlg = GetActiveWindow (); |
1639 |
ctx->cache_cb = 1; |
ctx->cache_cb = 1; |
1640 |
|
|
1641 |
p = cmdl; |
p = cmdl; |
1642 |
if( p && *p > 32 && !stristr( p, "winpt.exe" ) |
if (p && *p > 32 && !stristr (p, "winpt.exe") |
1643 |
&& !strstr( p, "--" ) ) { |
&& !strstr (p, "--" )) { |
1644 |
count++; |
count++; |
1645 |
if (*p == '"') { /* need to remove quotes */ |
if (*p == '"') { /* need to remove quotes */ |
1646 |
fn = new char[strlen( p )]; |
fn = new char[strlen (p)]; |
1647 |
if (!fn) |
if (!fn) |
1648 |
BUG( NULL ); |
BUG (NULL); |
1649 |
memcpy( fn, p+1, strlen( p ) - 2 ); |
memcpy (fn, p+1, strlen (p) - 2); |
1650 |
fn[strlen( p ) -2] = '\0'; |
fn[strlen (p) -2] = '\0'; |
1651 |
} |
} |
1652 |
else |
else |
1653 |
fn = m_strdup (p); |
fn = m_strdup (p); |
1654 |
s = fm_get_file_type (fn); |
s = fm_get_file_type (fn, &type); |
1655 |
|
log_box ("debug", MB_OK, "%s %d", s, type); |
1656 |
if (!s || !strcmp (s, "UNKNOWN")) |
if (!s || !strcmp (s, "UNKNOWN")) |
1657 |
s = gnupg_check_file_ext (fn); |
s = gnupg_check_file_ext (fn, &type); |
1658 |
if (*s == 'U') { |
if (type == PGP_NONE) { |
1659 |
log_box( _("File Manager"), MB_ERR, _("%s: no valid OpenPGP data found."), p ); |
log_box (_("File Manager"), MB_ERR, |
1660 |
|
_("%s: no valid OpenPGP data found."), p); |
1661 |
|
free_if_alloc (fn); |
1662 |
return count; |
return count; |
1663 |
} |
} |
1664 |
|
|
1665 |
switch( *s ) { |
log_box ("debug", MB_OK, "type=%d", type); /*XXX: debug*/ |
1666 |
case 'E': fm_decrypt (ctx, fn); break; |
switch (type) { |
1667 |
case 'P': fm_import (ctx, fn); break; |
case PGP_MESSAGE: |
1668 |
case 'S': |
fm_decrypt (ctx, fn); |
1669 |
if (s[1] == 'Y') { |
break; |
1670 |
fm_decrypt (ctx, fn); |
|
1671 |
break; |
case PGP_PUBKEY: |
1672 |
} |
case PGP_SECKEY: |
1673 |
|
fm_import (ctx, fn); |
1674 |
|
break; |
1675 |
|
|
1676 |
|
case PGP_SIG: |
1677 |
|
case PGP_CLEARSIG: |
1678 |
file_verify_use_event (); |
file_verify_use_event (); |
1679 |
if (s[1] == 'I') { |
if (type == PGP_SIG) |
1680 |
if (strlen (s) == 13 && s[7] == 'D') |
detached = 1; |
1681 |
detached = 1; |
fm_verify (ctx, detached, fn); |
1682 |
fm_verify( ctx, detached, fn ); |
file_verify_wait (); |
|
} |
|
|
file_verify_wait( ); |
|
1683 |
break; |
break; |
1684 |
} |
} |
1685 |
} |
} |
1686 |
|
|
1687 |
memset( &ctx->pass_cb, 0, sizeof (ctx->pass_cb) ); |
wipememory (&ctx->pass_cb, sizeof (ctx->pass_cb)); |
1688 |
safe_free( fn ); |
free_if_alloc (fn); |
1689 |
fm_state_release( ctx ); |
fm_state_release (ctx); |
1690 |
return count; |
return count; |
1691 |
} /* fm_parse_command_line */ |
} |
1692 |
|
|
1693 |
|
|
1694 |
const char * |
const char* |
1695 |
default_dirname( const char * name ) |
default_dirname (const char *name) |
1696 |
{ |
{ |
1697 |
char * p = strrchr( name, '\\' ); |
char * p = strrchr( name, '\\' ); |
1698 |
if( !p ) |
if( !p ) |
1751 |
unlink( s ); |
unlink( s ); |
1752 |
} |
} |
1753 |
leave: |
leave: |
1754 |
|
FindClose (hd); |
1755 |
pk_list_free( list ); |
pk_list_free( list ); |
1756 |
free_if_alloc( patt ); |
free_if_alloc( patt ); |
1757 |
return rc; |
return rc; |
1789 |
|
|
1790 |
if( listview_count_items( lv, 0 ) == 0 ) |
if( listview_count_items( lv, 0 ) == 0 ) |
1791 |
return; |
return; |
1792 |
memset( &mdctx, 0, sizeof (mdctx) ); |
memset (&mdctx, 0, sizeof (mdctx)); |
1793 |
mdctx.lv = lv; |
mdctx.lv = lv; |
1794 |
mdctx.mdalgo = mdalgo; |
mdctx.mdalgo = mdalgo; |
1795 |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg, |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg, |
1806 |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
1807 |
if (rc == -1) |
if (rc == -1) |
1808 |
return 0; |
return 0; |
1809 |
mapi_send_ascfile (buf); |
/*mapi_send_ascfile (buf); XXX */ |
1810 |
return 0; |
return 0; |
1811 |
} |
} |