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" |
47 |
|
|
48 |
#include "openpgp.h" |
#include "openpgp.h" |
49 |
|
|
50 |
int algo_from_list (gpgme_recipients_t rset, const char * keyid); |
#define op_begin() SetCursor (LoadCursor (NULL, IDC_WAIT)) |
51 |
void progress_cleanup (progress_filter_s * pfx); |
#define op_end() SetCursor (LoadCursor (NULL, IDC_ARROW)) |
52 |
|
|
53 |
|
void progress_cleanup (progress_filter_s *pfx); |
54 |
|
BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, |
55 |
|
WPARAM wparam, LPARAM lparam); |
56 |
|
char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n); |
57 |
|
gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
58 |
|
int prev_was_bad, int fd); |
59 |
|
|
60 |
/*-- wptFileVerifyDlg.cpp --*/ |
/*-- wptFileVerifyDlg.cpp --*/ |
61 |
int file_verify_add_state (siglog_context_t c); |
void file_verify_add_state (file_sig_ctx_t c); |
62 |
void file_verify_use_event (void); |
void file_verify_use_event (void); |
63 |
void file_verify_wait (void); |
void file_verify_wait (void); |
64 |
|
|
66 |
".mp3", ".wav", ".mid", ".wma", |
".mp3", ".wav", ".mid", ".wma", |
67 |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
68 |
|
|
69 |
char * |
|
70 |
fm_quote_file (const char * name) |
/* Check if the drive given by @fname is a floppy disc. |
71 |
|
Return value: 1 for success. */ |
72 |
|
static int |
73 |
|
is_floppy_disc (const char * fname) |
74 |
{ |
{ |
75 |
char * p; |
char drv[32] = {0}; |
76 |
size_t len = strlen (name) + 8; |
int i=0; |
77 |
|
|
78 |
if (*name == '"') |
if (!strstr (fname, ":\\")) |
79 |
return m_strdup (name); /* avoid double quotes */ |
return 0; |
|
p = new char[len + 1]; |
|
|
if (!p) |
|
|
BUG (0); |
|
|
_snprintf (p, len, "\"%s\"", name); |
|
80 |
|
|
81 |
return p; |
while (fname && *fname && *fname != '\\') |
82 |
} /* fm_quote_file */ |
drv[i++] = *fname++; |
83 |
|
drv[i++] = '\\'; |
84 |
|
drv[i++] = '\0'; |
85 |
|
i = GetDriveType (drv); |
86 |
|
if (i == DRIVE_REMOVABLE) |
87 |
|
return -1; |
88 |
|
return 0; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
/* Ask the user to overwrite file @fname. |
93 |
|
Return value: 0 for cancel. */ |
94 |
int |
int |
95 |
overwrite_file (const char * fname) |
overwrite_file (const char *fname) |
96 |
{ |
{ |
97 |
int id; |
int id; |
98 |
|
|
99 |
if (file_exist_check (fname)) |
if (file_exist_check (fname)) |
100 |
return 1; |
return -1; |
101 |
id = log_box (_("File Manager"), MB_YESNO, |
id = log_box (_("File Manager"), MB_YESNO, |
102 |
_("\"%s\" already exists.\n" |
_("\"%s\" already exists.\n" |
103 |
"Replace existing file?"), fname); |
"Replace existing file?"), fname); |
104 |
return id == IDNO ? 0 : 1; |
return id == IDNO ? 0 : -1; |
105 |
} /* overwrite_file */ |
} |
106 |
|
|
107 |
|
|
108 |
|
/* Removes 'critical' attributes from the file @fname. |
109 |
|
If @force is 1, the user is not asked for permission. */ |
110 |
static void |
static void |
111 |
remove_crit_file_attrs (const char * fname, int force) |
remove_crit_file_attrs (const char *fname, int force) |
112 |
{ |
{ |
113 |
u32 f_attr; |
u32 f_attr; |
114 |
int id; |
int id; |
126 |
if (id == IDYES) |
if (id == IDYES) |
127 |
SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL); |
SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL); |
128 |
} |
} |
129 |
} /* remove_crit_file_attrs */ |
} |
130 |
|
|
131 |
|
|
132 |
static int inline |
static int inline |
133 |
is_directory (const char * fname) |
is_directory (const char *fname) |
134 |
{ |
{ |
135 |
return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0; |
return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0; |
136 |
} /* is_directory */ |
} |
137 |
|
|
138 |
|
|
139 |
static int inline |
static int inline |
140 |
is_openpgp_ext (const char * name) |
is_openpgp_ext (const char *name) |
141 |
{ |
{ |
142 |
if (strstr (name, ".gpg") || strstr (name, ".asc") |
if (strstr (name, ".gpg") || strstr (name, ".asc") |
143 |
|| strstr (name, ".sig") || strstr (name, ".pgp")) |
|| strstr (name, ".sig") || strstr (name, ".pgp")) |
182 |
|
|
183 |
|
|
184 |
const char* |
const char* |
185 |
file_get_extension (gpgme_ctx_t ctx, gpgme_sigmode_t sigmode) |
file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode) |
186 |
{ |
{ |
187 |
int use_armor = (int)gpgme_control (ctx, GPGME_CTRL_ARMOR, -1); |
int use_armor = gpgme_get_armor (ctx); |
188 |
|
|
189 |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
190 |
return ".asc"; |
return ".asc"; |
191 |
if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH) |
if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH) |
192 |
return ".sig"; |
return ".sig"; |
193 |
return ".gpg"; |
return ".gpg"; |
194 |
} /* file_get_extension */ |
} |
195 |
|
|
196 |
|
|
197 |
|
char* |
198 |
|
fm_quote_file (const char * name) |
199 |
|
{ |
200 |
|
char * p; |
201 |
|
size_t len = strlen (name) + 8; |
202 |
|
|
203 |
|
if (*name == '"') |
204 |
|
return m_strdup (name); /* avoid double quotes */ |
205 |
|
p = new char[len + 1]; |
206 |
|
if (!p) |
207 |
|
BUG (0); |
208 |
|
_snprintf (p, len, "\"%s\"", name); |
209 |
|
|
210 |
|
return p; |
211 |
|
} /* fm_quote_file */ |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
/* Check the armor type of the file @fname and return |
216 |
|
a string representation of it. */ |
217 |
|
static const char * |
218 |
|
fm_check_armor_type (const char *fname, int *r_type) |
219 |
|
{ |
220 |
|
FILE * fp; |
221 |
|
char header[768], * p; |
222 |
|
|
223 |
|
if (r_type) |
224 |
|
*r_type = PGP_NONE; |
225 |
|
fp = fopen (fname, "rb"); |
226 |
|
if (!fp) |
227 |
|
return "UNKNOWN"; |
228 |
|
p = fgets (header, sizeof (header) - 1, fp); |
229 |
|
fclose (fp); |
230 |
|
if (!p) |
231 |
|
return "UNKNOWN"; |
232 |
|
|
233 |
|
if (strncmp (header, "-----", 5)) |
234 |
|
goto leave; |
235 |
|
if (strstr( header, "BEGIN PGP PUBLIC KEY" )) { |
236 |
|
if (r_type) *r_type = PGP_PUBKEY; |
237 |
|
return "PUBKEY"; |
238 |
|
} |
239 |
|
else if (strstr (header, "BEGIN PGP PRIVATE KEY") || |
240 |
|
strstr (header, "BEGIN PGP SECRET KEY")) { |
241 |
|
if (r_type) *r_type = PGP_SECKEY; |
242 |
|
return "SECKEY"; |
243 |
|
} |
244 |
|
else if (strstr (header, "BEGIN PGP MESSAGE")) { |
245 |
|
if (r_type) *r_type = PGP_MESSAGE; |
246 |
|
return "ENCRYPTED"; |
247 |
|
} |
248 |
|
else if (strstr( header, "BEGIN PGP SIGNED MESSAGE")) { |
249 |
|
if (r_type) *r_type = PGP_CLEARSIG; |
250 |
|
return "SIGNED-CLEAR"; |
251 |
|
} |
252 |
|
else if (strstr(header, "BEGIN PGP SIGNATURE")) { |
253 |
|
if (r_type) *r_type = PGP_SIG; |
254 |
|
return "SIGNED-DETACH"; |
255 |
|
} |
256 |
|
|
257 |
|
leave: |
258 |
|
return "UNKNOWN"; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
/* Extract file type from @fname. If @r_type is valid, |
263 |
|
it contains the PGP type on success. */ |
264 |
|
static const char * |
265 |
|
fm_get_file_type (const char *fname, int *r_type) |
266 |
|
{ |
267 |
|
gpg_iobuf_t inp; |
268 |
|
armor_filter_context_t afx; |
269 |
|
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
270 |
|
int i = 0, rc = 0; |
271 |
|
const char *s = NULL; |
272 |
|
|
273 |
|
if (r_type) |
274 |
|
*r_type = PGP_NONE; |
275 |
|
if (!fname) { |
276 |
|
safe_free (pkt); |
277 |
|
return NULL; |
278 |
|
} |
279 |
|
|
280 |
|
if (is_floppy_disc (fname)) |
281 |
|
return fm_check_armor_type (fname, r_type); |
282 |
|
|
283 |
|
inp = gpg_iobuf_open (fname); |
284 |
|
if (!inp) { |
285 |
|
const char *s = winpt_strerror (WPTERR_FILE_OPEN); |
286 |
|
log_box( _("File Manager"), MB_ERR, "\"%s\": %s", fname, s ); |
287 |
|
safe_free( pkt ); |
288 |
|
return NULL; |
289 |
|
} |
290 |
|
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
291 |
|
if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */ |
292 |
|
&& !is_openpgp_ext (fname)) { |
293 |
|
gpg_iobuf_close (inp); |
294 |
|
return "UNKNOWN"; |
295 |
|
} |
296 |
|
|
297 |
|
if (gpg_use_armor_filter(inp)) { |
298 |
|
memset (&afx, 0, sizeof (afx)); |
299 |
|
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
300 |
|
} |
301 |
|
|
302 |
|
gpg_init_packet (pkt); |
303 |
|
while (!(rc = gpg_parse_packet (inp, pkt))) { |
304 |
|
switch (pkt->pkttype) { |
305 |
|
case PKT_PUBKEY_ENC: |
306 |
|
s = "ENCRYPTED";rc = -2; |
307 |
|
if (r_type) *r_type = PGP_MESSAGE; |
308 |
|
break; |
309 |
|
case PKT_SYMKEY_ENC: |
310 |
|
case PKT_ENCRYPTED: |
311 |
|
s = "SYMKEYENC";rc = -2; |
312 |
|
if (r_type) *r_type = PGP_MESSAGE; |
313 |
|
break; |
314 |
|
case PKT_SIGNATURE: |
315 |
|
case PKT_ONEPASS_SIG: |
316 |
|
s = "SIGNED"; rc = -2; |
317 |
|
if (r_type) *r_type = PGP_SIG; |
318 |
|
break; |
319 |
|
case PKT_PUBLIC_KEY: |
320 |
|
s = "PUBKEY"; rc = -2; |
321 |
|
if (r_type) *r_type = PGP_PUBKEY; |
322 |
|
break; |
323 |
|
case PKT_SECRET_KEY: |
324 |
|
s = "SECKEY"; rc = -2; |
325 |
|
if (r_type) *r_type = PGP_SECKEY; |
326 |
|
break; |
327 |
|
} |
328 |
|
gpg_free_packet (pkt); |
329 |
|
gpg_init_packet (pkt); |
330 |
|
if (rc == -2) |
331 |
|
break; /* found */ |
332 |
|
} |
333 |
|
safe_free (pkt); |
334 |
|
gpg_iobuf_close (inp); |
335 |
|
if (!s) |
336 |
|
s = fm_check_armor_type (fname, r_type); |
337 |
|
if (!s) |
338 |
|
s = "UNKNOWN"; |
339 |
|
if (!strcmp (s, "SIGNED") |
340 |
|
&& strcmp (fm_check_armor_type (fname, r_type), "SIGNED-CLEAR ")) { |
341 |
|
if (r_type) *r_type = PGP_SIG; |
342 |
|
s = "SIGNED-DETACH"; |
343 |
|
} |
344 |
|
return s; |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
int |
int |
349 |
fm_build( listview_ctrl_t *lv, HWND ctrl ) |
fm_build (listview_ctrl_t *lv, HWND ctrl) |
350 |
{ |
{ |
351 |
int i, rc = 0; |
int i, rc = 0; |
352 |
listview_ctrl_t c; |
listview_ctrl_t c; |
391 |
BUG (0); |
BUG (0); |
392 |
memset (c, 0, sizeof * c); |
memset (c, 0, sizeof * c); |
393 |
rc = gpgme_new (&c->ctx); |
rc = gpgme_new (&c->ctx); |
|
if (!rc) |
|
|
rc = gpgme_recipients_new (&c->recp); |
|
394 |
if (rc) |
if (rc) |
395 |
BUG (0); |
BUG (0); |
396 |
gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); |
/* XXX rc = gpgme_recipients_new (&c->recp);*/ |
397 |
|
/* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */ |
398 |
*ctx = c; |
*ctx = c; |
399 |
return 0; |
return 0; |
400 |
} /* fm_state_new */ |
} /* fm_state_new */ |
401 |
|
|
402 |
|
|
403 |
|
/* Release the FM state handle @c. */ |
404 |
void |
void |
405 |
fm_state_release (fm_state_t c) |
fm_state_release (fm_state_t c) |
406 |
{ |
{ |
407 |
if (c) |
if (!c) |
408 |
{ |
return; |
409 |
if (c->recp) |
if (c->recp) |
410 |
{ |
free (c->recp); |
411 |
gpgme_recipients_release (c->recp); |
if (c->ctx) { |
412 |
c->recp = NULL; |
gpgme_release (c->ctx); |
413 |
} |
c->ctx = NULL; |
|
if (c->ctx) |
|
|
{ |
|
|
gpgme_release (c->ctx); |
|
|
c->ctx = NULL; |
|
|
} |
|
|
free_if_alloc (c->opaque); |
|
|
free_if_alloc (c->output); |
|
|
delete c; c = NULL; |
|
414 |
} |
} |
415 |
} /* fm_state_release */ |
free_if_alloc (c->opaque); |
416 |
|
free_if_alloc (c->output); |
417 |
|
delete c; c = NULL; |
418 |
|
} |
419 |
|
|
420 |
static int |
static int |
421 |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
436 |
|
|
437 |
|
|
438 |
static int |
static int |
439 |
fm_set_ftype (listview_ctrl_t lv, const char * name) |
fm_set_ftype (listview_ctrl_t lv, const char *name) |
440 |
{ |
{ |
441 |
const char *type; |
const char *type; |
442 |
int rc; |
int rc; |
444 |
rc = fm_check_for_entry (lv, name); |
rc = fm_check_for_entry (lv, name); |
445 |
if (rc) |
if (rc) |
446 |
return 0; |
return 0; |
447 |
type = fm_get_file_type (name); |
type = fm_get_file_type (name, NULL); |
448 |
if (!type || !strcmp (type, "UNKNOWN")) |
if (!type || !strcmp (type, "UNKNOWN")) |
449 |
type = gnupg_check_file_ext (name); |
type = gnupg_check_file_ext (name, NULL); |
450 |
rc = listview_add_item (lv, " "); |
rc = listview_add_item (lv, " "); |
451 |
if (rc) |
if (rc) |
452 |
return -1; |
return -1; |
482 |
} |
} |
483 |
|
|
484 |
|
|
485 |
|
/* Add the drag & drop files from @dd_files to the |
486 |
|
list view control @lv. */ |
487 |
int |
int |
488 |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
489 |
{ |
{ |
490 |
char name[384+4]; |
char name[384+4]; |
491 |
int nfiles, rc, i; |
int nfiles, rc, i; |
492 |
|
|
493 |
memset( name, 0, sizeof (name) ); |
memset (name, 0, sizeof (name)); |
494 |
nfiles = DragQueryFile( dd_files, 0xFFFFFFFF, NULL, 0 ); |
nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0); |
495 |
for (i = 0; i < nfiles; i++) { |
for (i = 0; i < nfiles; i++) { |
496 |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
497 |
if (is_directory (name)) |
if (is_directory (name)) |
500 |
rc = fm_set_ftype (lv, name); |
rc = fm_set_ftype (lv, name); |
501 |
if (rc == -1) |
if (rc == -1) |
502 |
break; |
break; |
|
|
|
503 |
} |
} |
504 |
return rc; |
return rc; |
505 |
} /* fm_add_dropped_files */ |
} |
506 |
|
|
507 |
|
|
508 |
int |
int |
510 |
{ |
{ |
511 |
OPENFILENAME open; |
OPENFILENAME open; |
512 |
const char *type; |
const char *type; |
513 |
char file[1024] = ""; |
char file[512] = ""; |
514 |
int rc; |
int rc; |
515 |
|
|
516 |
memset( &open, 0, sizeof (open) ); |
/* XXX: support to add multiple files. */ |
517 |
|
memset (&open, 0, sizeof (open)); |
518 |
open.lStructSize = sizeof (OPENFILENAME); |
open.lStructSize = sizeof (OPENFILENAME); |
519 |
open.hInstance = glob_hinst; |
open.hInstance = glob_hinst; |
520 |
open.lpstrTitle = _("File Open"); |
open.lpstrTitle = _("File Open"); |
521 |
open.lpstrFilter = _("All Files (*.*)\0*.*"); |
open.lpstrFilter = _("All Files (*.*)\0*.*\0\0"); |
522 |
open.hwndOwner = dlg; |
open.hwndOwner = dlg; |
523 |
open.lpstrFile = file; |
open.lpstrFile = file; |
524 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = sizeof (file) - 1; |
525 |
open.Flags = 0; |
open.Flags = 0; |
526 |
|
|
527 |
if (GetOpenFileName (&open)) { |
if (GetOpenFileName (&open)) { |
528 |
type = fm_get_file_type (open.lpstrFile); |
type = fm_get_file_type (open.lpstrFile, NULL); |
529 |
if (!type) |
if (!type) |
530 |
return WPTERR_FILE_OPEN; |
return WPTERR_FILE_OPEN; |
531 |
if (!strcmp (type, "UNKNOWN")) |
if (!strcmp (type, "UNKNOWN")) |
532 |
type = gnupg_check_file_ext (open.lpstrFile); |
type = gnupg_check_file_ext (open.lpstrFile, NULL); |
533 |
rc = listview_add_item (lv, ""); |
rc = listview_add_item (lv, ""); |
534 |
if( !rc ) { |
if (!rc) { |
535 |
listview_add_sub_item (lv, 0, 0, type); |
listview_add_sub_item (lv, 0, 0, type); |
536 |
listview_add_sub_item (lv, 0, 1, open.lpstrFile); |
listview_add_sub_item (lv, 0, 1, open.lpstrFile); |
537 |
} |
} |
538 |
} |
} |
539 |
|
|
540 |
return rc; |
return rc; |
541 |
} /* fm_add_opened_files */ |
} |
|
|
|
|
|
|
|
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"; |
|
|
} /* fm_check_armor_type */ |
|
542 |
|
|
543 |
|
|
544 |
int |
int |
550 |
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
551 |
int check = 0; |
int check = 0; |
552 |
|
|
553 |
if (!fname) |
if (!fname) { |
554 |
{ |
gpg_data_new_from_clipboard (&dat, 0); |
555 |
gpgme_data_new_from_clipboard (&dat); |
gpg_data_release_and_set_file (dat, "gpgme.tmp"); |
|
gpgme_data_release_and_set_file (dat, "gpgme.tmp"); |
|
556 |
|
|
557 |
fp = gpg_iobuf_open ("gpgme.tmp"); |
fp = gpg_iobuf_open ("gpgme.tmp"); |
558 |
if (!fp) |
if (!fp) |
559 |
return 0; |
return 0; |
560 |
gpg_iobuf_ioctl (fp, 3, 1, NULL); |
gpg_iobuf_ioctl (fp, 3, 1, NULL); |
561 |
if (gpg_use_armor_filter(fp)) |
if (gpg_use_armor_filter(fp)) { |
|
{ |
|
562 |
memset (&afx, 0, sizeof (afx)); |
memset (&afx, 0, sizeof (afx)); |
563 |
gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx); |
gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx); |
564 |
} |
} |
576 |
} |
} |
577 |
|
|
578 |
|
|
|
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 */ |
|
|
|
|
|
|
|
579 |
int |
int |
580 |
fm_get_current_pos (listview_ctrl_t lv) |
fm_get_current_pos (listview_ctrl_t lv) |
581 |
{ |
{ |
626 |
case FM_ENCRYPT: |
case FM_ENCRYPT: |
627 |
case FM_ENCRYPT_DIR: |
case FM_ENCRYPT_DIR: |
628 |
case FM_SIGNENCRYPT: |
case FM_SIGNENCRYPT: |
629 |
if (strcmp (status, "ENCRYPTED")) |
if (strcmp (status, "ENCRYPTED") |
630 |
|
&& strcmp (status, "SYMKEYENC")) |
631 |
rc = 1; |
rc = 1; |
632 |
break; |
break; |
633 |
|
|
740 |
return n; |
return n; |
741 |
} |
} |
742 |
|
|
|
|
|
743 |
int |
int |
744 |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
745 |
{ |
{ |
769 |
if (rc) |
if (rc) |
770 |
BUG (0); |
BUG (0); |
771 |
ctx->dlg = dlg; |
ctx->dlg = dlg; |
772 |
|
|
773 |
|
// XXX: for file operations the progress dialog will be |
774 |
|
// reloaded somewhere and thus a 'dummy' dialog remains |
775 |
|
|
776 |
|
/* we use it here to make sure that pfx_cleanup will not use |
777 |
|
any weird values. */ |
778 |
memset (&pfx, 0, sizeof (pfx)); |
memset (&pfx, 0, sizeof (pfx)); |
779 |
gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx); |
ctx->prog_cb = NULL; |
780 |
|
if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) { |
781 |
|
pfx.hwnd = dlg; |
782 |
|
/*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/ |
783 |
|
/*ctx->prog_cb = &pfx;*/ |
784 |
|
} |
785 |
|
|
786 |
/* Commands we need before we can perform the main command */ |
/* Commands we need before we can perform the main command */ |
787 |
switch (fm_cmd) { |
switch (fm_cmd) { |
816 |
if( !listview_get_item_state( lv, i ) ) |
if( !listview_get_item_state( lv, i ) ) |
817 |
continue; |
continue; |
818 |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
819 |
if( !strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT ) |
if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT) |
820 |
n++; |
n++; |
821 |
if( !strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN ) |
if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN) |
822 |
n++; |
n++; |
823 |
if (fm_cmd == FM_WIPE) { |
if (fm_cmd == FM_WIPE) |
|
if (!confirm.rset) |
|
|
gpgme_recipients_new (&confirm.rset); |
|
|
listview_get_item_text (lv, i, 1, fname, sizeof (fname)-1); |
|
|
gpgme_recipients_add_name (confirm.rset, fname); |
|
824 |
ndel++; |
ndel++; |
|
} |
|
825 |
} |
} |
826 |
|
|
827 |
if( n > 1 ) |
if (n > 1 && fm_cmd != FM_SYMENC) |
828 |
ctx->cache_cb = 1; |
ctx->cache_cb = 1; |
829 |
|
|
830 |
if( fm_cmd == FM_WIPE && ndel > 0 ) { |
if (fm_cmd == FM_WIPE && ndel > 0) { |
831 |
|
memset (&confirm, 0, sizeof confirm); |
832 |
|
confirm.lv_files = lv; |
833 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
834 |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
835 |
if (!confirm.yes) |
if (!confirm.yes) |
858 |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
859 |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
860 |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
861 |
case FM_VERIFY: rc = fm_verify( ctx, sig_detached, fname );break; |
case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break; |
862 |
case FM_IMPORT: |
case FM_IMPORT: |
863 |
free_if_alloc (ctx->opaque); |
free_if_alloc (ctx->opaque); |
864 |
ctx->opaque = m_strdup (fname); |
ctx->opaque = m_strdup (fname); |
880 |
progress_cleanup (&pfx2); |
progress_cleanup (&pfx2); |
881 |
} |
} |
882 |
if (ctx->cache_cb) { |
if (ctx->cache_cb) { |
883 |
memset (ctx->pass_cb.pwd, 0, sizeof (ctx->pass_cb)); |
release_gpg_passphrase_cb (&ctx->pass_cb); |
884 |
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! */ |
885 |
} |
} |
886 |
|
|
896 |
leave: |
leave: |
897 |
if (!rc) |
if (!rc) |
898 |
fm_state_release (ctx); |
fm_state_release (ctx); |
|
if (confirm.rset) |
|
|
gpgme_recipients_release (confirm.rset); |
|
899 |
progress_cleanup (&pfx); |
progress_cleanup (&pfx); |
900 |
return rc; |
return rc; |
901 |
} /* fm_parse_files */ |
} /* fm_parse_files */ |
902 |
|
|
903 |
|
|
904 |
|
/* Wipe the given file @name with the delete mode |
905 |
|
from the configuration. |
906 |
|
Return value: 0 on success. */ |
907 |
int |
int |
908 |
fm_wipe (const char * name) |
fm_wipe (const char *name) |
909 |
{ |
{ |
910 |
int rc; |
int rc; |
911 |
|
|
914 |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
915 |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
916 |
return rc; |
return rc; |
917 |
} /* fm_wipe */ |
} |
918 |
|
|
919 |
|
|
920 |
|
/* Dump out the given PGP packets from file @name in a dialog. */ |
921 |
int |
int |
922 |
fm_list( const char * name, HWND dlg ) |
fm_list (const char *name, HWND dlg) |
923 |
{ |
{ |
924 |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
925 |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
926 |
IDS_WINPT_FILE_STAT ); |
IDS_WINPT_FILE_STAT ); |
927 |
return 0; |
return 0; |
928 |
} /* fm_list */ |
} |
929 |
|
|
930 |
|
|
931 |
static int |
static int |
932 |
ask_filename (fm_state_t c, const char * msg, char ** dst) |
ask_filename (fm_state_t c, const char *msg, char **dst) |
933 |
{ |
{ |
934 |
const char * s; |
const char * s; |
935 |
|
|
937 |
if (!s) |
if (!s) |
938 |
return WPTERR_GENERAL; |
return WPTERR_GENERAL; |
939 |
|
|
940 |
free_if_alloc (*dst); |
if (dst != NULL) |
941 |
|
free_if_alloc (*dst); |
942 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
943 |
c->output = m_strdup (s); |
c->output = m_strdup (s); |
944 |
if (!c->output) |
if (!c->output) |
945 |
BUG (0); |
BUG (0); |
946 |
*dst = fm_quote_file (s); |
if (dst) |
947 |
|
*dst = fm_quote_file (s); |
948 |
return 0; |
return 0; |
949 |
} |
} |
950 |
|
|
951 |
|
|
952 |
int |
int |
953 |
fm_encrypt (fm_state_t c, const char * name, int sign) |
fm_encrypt (fm_state_t c, const char *name, int sign) |
954 |
{ |
{ |
955 |
gpgme_error_t err; |
gpgme_error_t err; |
956 |
gpgme_key_t key = NULL; |
gpgme_key_t key = NULL; |
957 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
958 |
char * src = NULL, * dst = NULL; |
file_data_t in=NULL, out=NULL; |
959 |
char * keyid = NULL, ext[5]; |
char *keyid = NULL, ext[5]; |
960 |
int no_compr = 0; |
int no_compr = 0; |
961 |
int rc = 0; |
int rc = 0; |
962 |
|
|
|
src = fm_quote_file (name); |
|
963 |
c->output = new char[strlen (name) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
964 |
if (!c->output) |
if (!c->output) |
965 |
BUG (0); |
BUG (0); |
966 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
967 |
strcpy (c->output, name ); |
strcpy (c->output, name); |
968 |
strcat (c->output, ext ); |
strcat (c->output, ext); |
|
dst = fm_quote_file (c->output); |
|
969 |
|
|
970 |
if (!overwrite_file (c->output)) |
if (!overwrite_file (c->output)) { |
971 |
{ |
rc = ask_filename (c, _("Enter filename for encrypted file"), NULL); |
|
rc = ask_filename (c, _("Enter filename for encrypted file"), &dst); |
|
972 |
if (rc) |
if (rc) |
973 |
goto leave; |
goto leave; |
974 |
} |
} |
975 |
|
|
976 |
|
err = gpg_file_data_new (name, 1, &in); |
977 |
|
if (err) |
978 |
|
goto leave; |
979 |
|
err = gpg_file_data_new (c->output, 0, &out); |
980 |
|
if (err) |
981 |
|
goto leave; |
982 |
|
|
983 |
|
/* |
984 |
|
if (c->prog_cb) { |
985 |
|
c->prog_cb->what = name; |
986 |
|
gpg_file_data_set_cb (in, c->prog_cb); |
987 |
|
} |
988 |
|
*/ |
989 |
|
|
990 |
|
/* XXX |
991 |
no_compr = is_multi_media (name); |
no_compr = is_multi_media (name); |
992 |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
993 |
|
*/ |
994 |
|
|
995 |
if (sign) { |
if (sign) { |
996 |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
997 |
keyid = get_gnupg_default_key (); |
keyid = get_gnupg_default_key (); |
998 |
if (!keyid) { |
if (!keyid) { |
999 |
msg_box (c->dlg, _("Could not get default secret key."), |
msg_box (c->dlg, _("Could not get default secret key."), |
1000 |
_("Signing"), MB_ERR); |
_("Signing"), MB_ERR); |
1001 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1002 |
goto leave; |
goto leave; |
1006 |
gpgme_signers_add (ctx, key); |
gpgme_signers_add (ctx, key); |
1007 |
} |
} |
1008 |
else { |
else { |
1009 |
const char * s; |
gpgme_key_t key = gpgme_signers_enum (ctx, 0); |
1010 |
s = (char *)gpgme_key_get_string_attr (gpgme_signers_enum (ctx, 0), |
if (key && key->subkeys) { |
1011 |
GPGME_ATTR_KEYID, NULL, 0); |
keyid = m_strdup (key->subkeys->keyid); |
1012 |
keyid = m_strdup (s); |
if (!keyid) |
1013 |
|
BUG (NULL); |
1014 |
|
} |
1015 |
} |
} |
1016 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1017 |
set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_SIGN, |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, |
1018 |
c->dlg, _("Signing")); |
c->dlg, _("Signing")); |
1019 |
c->init_cb = 1; |
c->init_cb = 1; |
1020 |
} |
} |
1021 |
err = gpgme_op_file_sign_encrypt (ctx, c->recp, src, dst); |
op_begin (); |
1022 |
|
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1023 |
|
in->dat, out->dat); |
1024 |
|
op_end (); |
1025 |
if (!c->cache_cb) |
if (!c->cache_cb) |
1026 |
memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd)); |
release_gpg_passphrase_cb (&c->pass_cb); |
1027 |
if (c->pass_cb.cancel) { |
if (c->pass_cb.cancel) { |
1028 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1029 |
goto leave; |
goto leave; |
1030 |
} |
} |
1031 |
if (err) { |
if (err) { |
1032 |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
1033 |
if (err == GPGME_Bad_Passphrase) |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
1034 |
agent_del_cache (keyid); |
agent_del_cache (keyid); |
1035 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1036 |
goto leave; |
goto leave; |
1037 |
} |
} |
|
gpgme_key_release (key); |
|
1038 |
} |
} |
1039 |
else { |
else { |
1040 |
err = gpgme_op_file_encrypt (ctx, c->recp, src, dst); |
op_begin (); |
1041 |
|
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1042 |
|
in->dat, out->dat); |
1043 |
|
op_end (); |
1044 |
if (err) { |
if (err) { |
1045 |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
1046 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1051 |
secure_unlink (name, WIPE_MODE_SIMPLE); |
secure_unlink (name, WIPE_MODE_SIMPLE); |
1052 |
|
|
1053 |
leave: |
leave: |
1054 |
|
if (in) |
1055 |
|
gpg_file_data_release (in); |
1056 |
|
if (out) |
1057 |
|
gpg_file_data_release (out); |
1058 |
free_if_alloc (keyid); |
free_if_alloc (keyid); |
|
free_if_alloc (dst); |
|
|
free_if_alloc (src); |
|
1059 |
return rc; |
return rc; |
1060 |
} /* fm_encrypt */ |
} |
1061 |
|
|
1062 |
|
|
1063 |
int |
int |
1064 |
fm_sym_encrypt (fm_state_t c, const char * name) |
fm_sym_encrypt (fm_state_t c, const char * name) |
1065 |
{ |
{ |
|
int rc = 0, cancel = 0; |
|
|
char * src = NULL, * dst = NULL; |
|
|
char ext[5], * pass; |
|
1066 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1067 |
gpgme_error_t err; |
gpgme_error_t err; |
1068 |
|
file_data_t in=NULL, out=NULL; |
1069 |
|
int rc = 0, cancel = 0; |
1070 |
|
char * src = NULL, * dst = NULL; |
1071 |
|
char ext[5], * pass; |
1072 |
|
|
1073 |
pass = request_passphrase2 (_("Symmetric"), &cancel); |
pass = request_passphrase2 (_("Symmetric"), 0, &cancel); |
1074 |
if (cancel) |
if (cancel) |
1075 |
return 0; |
return 0; |
1076 |
|
|
1077 |
gpgme_control (ctx, GPGME_CTRL_CIPHER, -1); |
/* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/ |
|
src = fm_quote_file (name); |
|
1078 |
c->output = new char[strlen (name) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
1079 |
if (!c->output) |
if (!c->output) |
1080 |
BUG (0); |
BUG (0); |
1081 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1082 |
strcpy (c->output, name); |
strcpy (c->output, name); |
1083 |
strcat (c->output, ext); |
strcat (c->output, ext); |
|
dst = fm_quote_file (c->output); |
|
1084 |
|
|
1085 |
if (overwrite_file (c->output) == 0) { |
if (overwrite_file (c->output) == 0) { |
1086 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1087 |
goto leave; |
goto leave; |
1088 |
} |
} |
1089 |
gpgme_set_passphrase (ctx, pass); |
|
1090 |
err = gpgme_op_file_encrypt (ctx, NULL, src, dst); |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
1091 |
|
|
1092 |
|
err = gpg_file_data_new (name, 1, &in); |
1093 |
|
if (err) |
1094 |
|
goto leave; |
1095 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1096 |
|
if (err) |
1097 |
|
goto leave; |
1098 |
|
|
1099 |
|
op_begin (); |
1100 |
|
err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, |
1101 |
|
in->dat, out->dat); |
1102 |
|
op_end (); |
1103 |
if (err) { |
if (err) { |
1104 |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
1105 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1111 |
} |
} |
1112 |
|
|
1113 |
leave: |
leave: |
1114 |
free_if_alloc (src); |
if (in) |
1115 |
free_if_alloc (dst); |
gpg_file_data_release (in); |
1116 |
|
if (out) |
1117 |
|
gpg_file_data_release (out); |
1118 |
sfree_if_alloc (pass); |
sfree_if_alloc (pass); |
1119 |
return rc; |
return rc; |
1120 |
} /* fm_sym_encrypt */ |
} /* fm_sym_encrypt */ |
1121 |
|
|
1122 |
|
|
1123 |
static gpgme_error_t |
/* Show the human readable verify result from @sigres. */ |
1124 |
fm_list_keys( const char * name, gpgme_recipients_t *r_keys ) |
static void |
1125 |
|
show_verify_result (gpgme_verify_result_t sigres) |
1126 |
{ |
{ |
1127 |
return gpgme_op_list_keys( NULL, name, r_keys ); |
gpgme_key_t key=NULL; |
1128 |
} /* fm_list_keys */ |
gpgme_signature_t sig=sigres->signatures; |
1129 |
|
const char *s, *keyid; |
1130 |
|
int sigok = 0; |
1131 |
|
int type; |
1132 |
|
char buf[384]; |
1133 |
|
|
1134 |
|
sig = sigres->signatures; |
1135 |
|
sigok = sig->summary & GPGME_SIGSUM_GREEN; |
1136 |
|
s = sigok? _("Good signature") : _("BAD signature"); |
1137 |
|
type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
1138 |
|
keyid = sig->fpr; |
1139 |
|
if (!keyid) |
1140 |
|
return; |
1141 |
|
|
1142 |
|
keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24; |
1143 |
|
get_pubkey (sig->fpr, &key); |
1144 |
|
_snprintf (buf, sizeof (buf)-1, "Signature made %s using %s key ID %s\n" |
1145 |
|
"%s from \"%s\"", |
1146 |
|
strtimestamp (sig->timestamp), get_key_pubalgo (sig->pubkey_algo), |
1147 |
|
keyid, s, key? key->uids->uid : _("user ID not found")); |
1148 |
|
msg_box (NULL, buf, _("Decrypt Verify"), type); |
1149 |
|
} |
1150 |
|
|
1151 |
|
|
1152 |
|
/* Check the recipients if we have at least one secret key. */ |
1153 |
|
bool |
1154 |
|
secret_key_available (gpgme_recipient_t rset) |
1155 |
|
{ |
1156 |
|
gpgme_recipient_t r; |
1157 |
|
gpgme_key_t key; |
1158 |
|
|
1159 |
|
for (r=rset; r; r = r->next) { |
1160 |
|
if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY) |
1161 |
|
continue; |
1162 |
|
else { |
1163 |
|
/* extra check to make sure the key is available right now. */ |
1164 |
|
if (!get_seckey (r->keyid, &key)) |
1165 |
|
return true; |
1166 |
|
} |
1167 |
|
} |
1168 |
|
return false; |
1169 |
|
} |
1170 |
|
|
1171 |
|
|
1172 |
|
/* Decrypt the file @name. */ |
1173 |
int |
int |
1174 |
fm_decrypt (fm_state_t c, const char * name) |
fm_decrypt (fm_state_t c, const char *name) |
1175 |
{ |
{ |
1176 |
gpgme_error_t err; |
gpgme_error_t err; |
1177 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1178 |
gpgme_recipients_t keys = NULL; |
gpgme_decrypt_result_t res; |
1179 |
gpgme_sig_t sig = NULL; |
gpgme_verify_result_t sigres; |
1180 |
gpgme_op_flags_t flags; |
file_data_t in =NULL, out=NULL; |
1181 |
char * src = NULL, * dst = NULL, keyid[17]; |
int is_signed = 0; |
|
int is_signed = 0, sigok = 0; |
|
1182 |
int rc = 0; |
int rc = 0; |
1183 |
|
|
1184 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1185 |
set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_DECRYPT, |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT, |
1186 |
c->dlg, _("Decryption")); |
c->dlg, _("Decryption")); |
1187 |
c->init_cb = 1; |
c->init_cb = 1; |
1188 |
} |
} |
1189 |
|
|
|
src = fm_quote_file (name); |
|
1190 |
c->output = m_strdup (name); |
c->output = m_strdup (name); |
1191 |
if (!c->output) |
if (!c->output) |
1192 |
BUG (0); |
BUG (0); |
1193 |
if (is_openpgp_ext (c->output)) |
if (is_openpgp_ext (c->output)) |
1194 |
c->output[strlen (c->output)-4] = '\0'; |
c->output[strlen (c->output)-4] = '\0'; |
1195 |
else { |
else { |
1196 |
const char *s = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Filename for Output"), |
const char *s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"), |
1197 |
NULL, NULL); |
NULL, NULL); |
1198 |
if( s ) { |
if (s) { |
1199 |
free_if_alloc( c->output ); |
free_if_alloc (c->output); |
1200 |
c->output = m_strdup( s ); |
c->output = m_strdup (s); |
1201 |
if( !c->output ) |
if (!c->output) |
1202 |
BUG( NULL ); |
BUG (NULL); |
1203 |
} |
} |
1204 |
} |
} |
|
dst = fm_quote_file( c->output ); |
|
|
|
|
|
err = fm_list_keys( src, &keys ); |
|
|
if( err ) |
|
|
goto leave; |
|
|
c->pass_cb.enc_to = keys; |
|
1205 |
|
|
1206 |
if (overwrite_file (c->output) == 0) { |
if (overwrite_file (c->output) == 0) { |
1207 |
rc = ask_filename (c, _("Please enter filename for plaintext file"), &dst); |
rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL); |
1208 |
if (rc) |
if (rc) |
1209 |
goto leave; |
goto leave; |
1210 |
} |
} |
1211 |
remove_crit_file_attrs( c->output, 0 ); |
|
1212 |
err = gpgme_op_file_decrypt( ctx, src, dst ); |
remove_crit_file_attrs (c->output, 0); |
1213 |
if( !c->cache_cb ) |
|
1214 |
memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) ); |
err = gpg_file_data_new (name, 1, &in); |
1215 |
if( c->pass_cb.cancel ) { |
if (err) |
1216 |
|
goto leave; |
1217 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1218 |
|
if (err) |
1219 |
|
goto leave; |
1220 |
|
|
1221 |
|
op_begin (); |
1222 |
|
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
1223 |
|
op_end (); |
1224 |
|
if (!c->cache_cb) |
1225 |
|
release_gpg_passphrase_cb (&c->pass_cb); |
1226 |
|
if (c->pass_cb.cancel) { |
1227 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1228 |
goto leave; |
goto leave; |
1229 |
} |
} |
1230 |
gpgme_decrypt_get_status( ctx, keyid, &flags ); |
|
1231 |
if( err == GPGME_No_Seckey && (flags & GPGME_OPFLAG_NOSECKEY) ) { |
res = gpgme_op_decrypt_result (ctx); |
1232 |
char * p = get_key_userid( keyid+8 ); |
if (res && res->recipients && !secret_key_available (res->recipients)) { |
1233 |
int pkalgo = algo_from_list( keys, keyid ); |
const char *keyid = res->recipients->keyid; |
1234 |
|
char *p = get_key_userid (keyid+8); |
1235 |
|
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
1236 |
|
|
1237 |
log_box( _("Decryption"), MB_ERR, |
log_box( _("Decryption"), MB_ERR, |
1238 |
_("Encrypted with %s key, ID %s.%s\n" |
_("Encrypted with %s key, ID %s.%s\n" |
1239 |
"Decryption failed: secret key not available."), |
"Decryption failed: secret key not available."), |
1240 |
gpgme_key_expand_attr( GPGME_ATTR_ALGO, pkalgo ), |
get_key_pubalgo (pkalgo), keyid+8, p); |
|
keyid+8, p ); |
|
1241 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1242 |
free_if_alloc( p ); |
free_if_alloc (p); |
1243 |
goto leave; |
goto leave; |
1244 |
} |
} |
1245 |
else if( err ) { |
else if (err) { |
1246 |
msg_box( c->dlg, gpgme_strerror( err ), _("Decrypt"), MB_ERR ); |
msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR); |
1247 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1248 |
goto leave; |
goto leave; |
1249 |
} |
} |
1250 |
if( file_exist_check( c->output ) ) { |
if (file_exist_check (c->output)) { |
1251 |
msg_box( c->dlg, _("Decryption failed"), _("Decrypt"), MB_ERR ); |
log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output); |
1252 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1253 |
} |
} |
1254 |
|
|
1255 |
gpgme_decrypt_get_sig_ctx( ctx, &sig ); |
sigres = gpgme_op_verify_result (ctx); |
1256 |
|
if (sigres && sigres->signatures) |
1257 |
sigok = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY ) == GPGME_SIG_STAT_GOOD; |
show_verify_result (sigres); |
1258 |
if( sig ) { |
|
|
const char *id, *s = sigok? _("Good signature") : _("BAD signature"); |
|
|
int type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
|
|
gpgme_key_t key; |
|
|
const char * keyid = gpgme_sig_get_string_attr( sig, GPGME_ATTR_KEYID ); |
|
|
if( !keyid ) |
|
|
keyid = "DEADBEEFDEADBEEF"; |
|
|
if( get_pubkey( keyid+8, &key ) ) |
|
|
log_box( _("Verify"), type, _("%s using keyID 0x%s"), s, keyid+8 ); |
|
|
else { |
|
|
id = gpgme_sig_get_string_attr( sig, GPGME_ATTR_USERID ); |
|
|
log_box( _("Verify"), type, "%s using keyID 0x%08X from %s", |
|
|
s, keyid, id? id : _("Invalid User ID") ); |
|
|
} |
|
|
} |
|
1259 |
|
|
1260 |
leave: |
leave: |
1261 |
free_if_alloc( dst ); |
if (in) |
1262 |
free_if_alloc( src ); |
gpg_file_data_release (in); |
1263 |
gpgme_sig_release( sig ); |
if (out) |
1264 |
gpgme_recipients_release( keys ); |
gpg_file_data_release (out); |
1265 |
return rc; |
return rc; |
1266 |
} /* fm_decrypt */ |
} |
1267 |
|
|
1268 |
|
|
1269 |
int |
int |
1272 |
int rc = 0; |
int rc = 0; |
1273 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1274 |
gpgme_error_t err; |
gpgme_error_t err; |
1275 |
char *src = NULL, *dst = NULL; |
file_data_t in=NULL, out=NULL; |
1276 |
char ext[5]; |
char ext[5]; |
1277 |
|
|
1278 |
if( !c->init_cb || !c->cache_cb ) { |
if (!c->init_cb || !c->cache_cb) { |
1279 |
set_gpg_passphrase_cb( c->ctx, &c->pass_cb, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
1280 |
c->init_cb = 1; |
c->init_cb = 1; |
1281 |
} |
} |
1282 |
|
|
1283 |
src = fm_quote_file( name ); |
free_if_alloc (c->output); |
1284 |
free_if_alloc( c->output ); |
c->output = new char[strlen (name) + 5 + 1]; |
1285 |
c->output = new char[strlen( name ) + 5 + 1]; |
if( !c->output) |
|
if( !c->output ) |
|
1286 |
BUG( NULL ); |
BUG( NULL ); |
1287 |
strcpy( ext, file_get_extension( ctx, c->sigmode ) ); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1288 |
strcpy( c->output, name ); |
strcpy (c->output, name); |
1289 |
strcat( c->output, ext ); |
strcat (c->output, ext); |
|
dst = fm_quote_file( c->output ); |
|
1290 |
|
|
1291 |
if (!overwrite_file (c->output)) { |
if (!overwrite_file (c->output)) { |
1292 |
rc = ask_filename (c, _("Enter filename for signed file"), &dst); |
rc = ask_filename (c, _("Enter filename for signed file"), NULL); |
1293 |
if (rc) |
if (rc) |
1294 |
goto leave; |
goto leave; |
1295 |
} |
} |
1296 |
remove_crit_file_attrs( c->output, 0 ); |
remove_crit_file_attrs (c->output, 0); |
1297 |
err = gpgme_op_file_sign( ctx, c->sigmode, src, dst ); |
|
1298 |
|
err = gpg_file_data_new (name, 1, &in); |
1299 |
|
if (err) |
1300 |
|
goto leave; |
1301 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1302 |
|
if (err) |
1303 |
|
goto leave; |
1304 |
|
|
1305 |
|
op_begin (); |
1306 |
|
err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode); |
1307 |
|
op_end (); |
1308 |
if( !c->cache_cb ) |
if( !c->cache_cb ) |
1309 |
memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) ); |
release_gpg_passphrase_cb (&c->pass_cb); |
1310 |
if( c->pass_cb.cancel ) { |
if( c->pass_cb.cancel ) { |
1311 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1312 |
goto leave; |
goto leave; |
1318 |
} |
} |
1319 |
|
|
1320 |
leave: |
leave: |
1321 |
free_if_alloc( src ); |
if (in) |
1322 |
free_if_alloc( dst ); |
gpg_file_data_release (in); |
1323 |
|
if (out) |
1324 |
|
gpg_file_data_release (out); |
1325 |
return rc; |
return rc; |
1326 |
} /* fm_sign */ |
} |
1327 |
|
|
1328 |
|
|
1329 |
static int |
static int |
1330 |
fm_add_sig_stat( siglog_context_t log ) |
fm_add_sig_stat (file_sig_ctx_t log) |
1331 |
{ |
{ |
1332 |
gpgme_key_t key; |
gpgme_key_t key; |
1333 |
const char *uid, *keyid, * kid; |
const char *kid; |
1334 |
int not_found = 0; |
int not_found = 0; |
1335 |
|
|
1336 |
kid = gpgme_sig_get_string_attr( log->sig, GPGME_ATTR_KEYID ); |
kid = log->sig->fpr; |
1337 |
if( !kid ) |
if (!kid) |
1338 |
kid = "DEADBEEFDEADBEEF"; |
BUG (NULL); |
1339 |
if( strlen( kid ) == 16 ) |
if (strlen (kid) == 40) |
1340 |
keyid = kid + 8; |
kid += 32; |
1341 |
else if( strlen( kid ) == 32 ) |
else if (strlen (kid) == 32) |
1342 |
keyid = kid; |
kid += 24; |
1343 |
else if( strlen( kid ) == 40 ) |
if (get_pubkey (kid, &key)) |
1344 |
keyid = kid + 24; |
log->use_uid = 0; |
1345 |
if( get_pubkey( keyid, &key ) ) |
else { |
1346 |
not_found = 1; |
log->user_id = key->uids->uid; |
1347 |
log->use_uid = 0; |
log->use_uid = 1; |
|
if( !not_found ) { |
|
|
uid = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 ); |
|
|
log->user_id = uid; |
|
|
log->use_uid = 1; |
|
1348 |
} |
} |
1349 |
file_verify_add_state( log ); |
file_verify_add_state (log); |
|
|
|
1350 |
return 0; |
return 0; |
1351 |
} /* fm_add_sig_stat */ |
} |
1352 |
|
|
1353 |
|
|
1354 |
static int |
static int |
1419 |
} |
} |
1420 |
|
|
1421 |
|
|
1422 |
int |
/* Extract automatically the output file name from @name. |
1423 |
fm_verify( fm_state_t c, int detached, const char *name ) |
If @detached is 1, a detached sig is assumed. */ |
1424 |
|
static int |
1425 |
|
get_output_file (fm_state_t c, const char *name, int detached) |
1426 |
{ |
{ |
1427 |
gpgme_ctx_t ctx = c->ctx; |
const char *file = NULL; |
1428 |
gpgme_error_t err; |
const char *title; |
1429 |
gpgme_sig_t sig; |
char fname[384]; |
1430 |
struct siglog_context_s log; |
|
1431 |
char * src = NULL; |
if (detached) |
1432 |
int rc = 0; |
title = _("Select Data File"); |
1433 |
size_t i; |
else |
1434 |
|
title = _("Selected Output File"); |
1435 |
if( detached ) { |
|
1436 |
const char *file = NULL; |
if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) { |
1437 |
if( strstr( name, ".sig" ) || strstr( name, ".asc" ) ) { |
_snprintf (fname, sizeof (fname) - 1, "%s", name); |
1438 |
char fname[512]; |
fname[strlen (fname) - 4] = '\0'; |
1439 |
_snprintf( fname, sizeof (fname) - 1, "%s", name ); |
if (file_exist_check (fname) == 0 && detached) |
1440 |
fname[strlen( fname ) - 4] = '\0'; |
file = fname; |
1441 |
if( file_exist_check( fname ) == 0 ) |
else if (!detached) { |
1442 |
|
/* If the signature is clear or normal, make sure we do not |
1443 |
|
overwrite the original file if it exists. */ |
1444 |
|
if (file_exist_check (fname) == 0 && !overwrite_file (fname)) { |
1445 |
|
file = get_filesave_dlg (c->dlg, title, NULL, NULL); |
1446 |
|
if (!file) |
1447 |
|
return WPTERR_GENERAL; |
1448 |
|
} |
1449 |
|
else |
1450 |
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 ); |
|
|
} |
|
|
else { |
|
|
msg_box( c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR ); |
|
|
return WPTERR_GENERAL; |
|
1451 |
} |
} |
|
c->sigmode = GPGME_SIG_MODE_DETACH; |
|
1452 |
} |
} |
1453 |
|
if (!file) |
1454 |
|
file = get_fileopen_dlg (c->dlg, title, NULL, NULL); |
1455 |
|
if (file) { |
1456 |
|
free_if_alloc (c->output); |
1457 |
|
c->output = m_strdup (file); |
1458 |
|
if (!c->output) |
1459 |
|
BUG (NULL); |
1460 |
|
} |
1461 |
|
else { |
1462 |
|
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
1463 |
|
return WPTERR_GENERAL; |
1464 |
|
} |
1465 |
|
if (detached) |
1466 |
|
c->sigmode = GPGME_SIG_MODE_DETACH; |
1467 |
else { |
else { |
1468 |
if( strstr( name, ".asc" ) ) |
if (strstr (name, ".asc")) |
1469 |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
1470 |
else |
else |
1471 |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
1472 |
} |
} |
1473 |
|
return 0; |
1474 |
|
} |
1475 |
|
|
1476 |
memset( &log, 0, sizeof (log) ); |
|
1477 |
strcpy( log.file, name ); |
/* Verify the signature from the file @name. If @detached 1, |
1478 |
file_verify_create_dlg(); |
it is assumed that a detached signature should be checked. */ |
1479 |
src = fm_quote_file( name ); |
int |
1480 |
|
fm_verify (fm_state_t c, int detached, const char *name) |
1481 |
err = gpgme_op_file_verify( ctx, c->sigmode, &sig, src, c->output ); |
{ |
1482 |
if( err == GPGME_Bad_Signature ) { |
gpgme_ctx_t ctx = c->ctx; |
1483 |
log.sig = sig; |
gpgme_error_t err; |
1484 |
fm_add_sig_stat( &log ); |
gpgme_signature_t s; |
1485 |
rc = WPTERR_GENERAL; |
gpgme_verify_result_t res; |
1486 |
|
struct file_sig_ctx_s log; |
1487 |
|
file_data_t in=NULL, out=NULL; |
1488 |
|
int rc = 0; |
1489 |
|
|
1490 |
|
if (strstr (name, ".sig")) |
1491 |
|
detached = 1; |
1492 |
|
|
1493 |
|
if (get_output_file (c, name, detached)) |
1494 |
|
return WPTERR_GENERAL; |
1495 |
|
|
1496 |
|
memset (&log, 0, sizeof (log)); |
1497 |
|
log.file = m_strdup (name); |
1498 |
|
if (!log.file) |
1499 |
|
BUG (NULL); |
1500 |
|
file_verify_create_dlg (); |
1501 |
|
|
1502 |
|
err = gpg_file_data_new (name, 1, &in); |
1503 |
|
if (err) |
1504 |
goto leave; |
goto leave; |
1505 |
} |
err = gpg_file_data_new (c->output, detached? 1 : 0, &out); |
1506 |
if( err ) { |
if (err) |
1507 |
msg_box( c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR ); |
goto leave; |
1508 |
|
|
1509 |
|
op_begin (); |
1510 |
|
if (c->sigmode == GPGME_SIG_MODE_DETACH) |
1511 |
|
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
1512 |
|
else |
1513 |
|
err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat); |
1514 |
|
op_end (); |
1515 |
|
if (err) { |
1516 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
1517 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1518 |
goto leave; |
goto leave; |
1519 |
|
} |
1520 |
|
|
1521 |
|
res = gpgme_op_verify_result (ctx); |
1522 |
|
for (s=res->signatures; s; s=s->next) { |
1523 |
|
log.sig = s; |
1524 |
|
fm_add_sig_stat (&log); |
1525 |
} |
} |
1526 |
for( i = 0; i < gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_LEVEL ); i++ ) { |
if (!c->output) |
1527 |
gpgme_sig_t _sig; |
c->output = m_strdup (name); /* for later use */ |
|
_sig = (gpgme_sig_t)gpgme_sig_get_ulong_attr( sig, i, GPGME_ATTR_OPAQUE ); |
|
|
log.sig = _sig; |
|
|
fm_add_sig_stat( &log ); |
|
|
} |
|
|
if( !c->output ) |
|
|
c->output = m_strdup( name ); /* for later use */ |
|
1528 |
|
|
1529 |
leave: |
leave: |
1530 |
free_if_alloc( src ); |
if (in) |
1531 |
|
gpg_file_data_release (in); |
1532 |
|
if (out) |
1533 |
|
gpg_file_data_release (out); |
1534 |
|
if (log.file) |
1535 |
|
delete []log.file; |
1536 |
return rc; |
return rc; |
1537 |
} /* fm_verify */ |
} |
1538 |
|
|
1539 |
|
|
1540 |
int |
int |
1541 |
fm_import( fm_state_t c, const char *name ) |
fm_import (fm_state_t c, const char *name) |
1542 |
{ |
{ |
1543 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1544 |
gpgme_error_t err; |
gpgme_error_t err; |
1545 |
char *src = NULL; |
gpgme_import_result_t res; |
1546 |
int import_res[14] = {0}; |
file_data_t keydata = NULL; |
1547 |
int rc = 0; |
int rc = 0; |
1548 |
|
|
1549 |
free_if_alloc( c->output ); |
free_if_alloc (c->output); |
1550 |
c->output = m_strdup( name ); |
c->output = m_strdup (name); |
1551 |
if( !c->output ) |
if (!c->output) |
1552 |
BUG( NULL ); |
BUG (NULL); |
1553 |
src = fm_quote_file( name ); |
|
1554 |
err = gpgme_op_file_import( ctx, NULL, src ); |
err = gpg_file_data_new (name, 1, &keydata); |
1555 |
if( err ) { |
if (err) |
1556 |
msg_box( c->dlg, gpgme_strerror( err ), _("Import"), MB_ERR ); |
goto leave; |
1557 |
rc = WPTERR_GENERAL; |
|
1558 |
|
op_begin (); |
1559 |
|
err = gpgme_op_import (ctx, keydata->dat); |
1560 |
|
op_end (); |
1561 |
|
if (err) { |
1562 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
1563 |
|
rc = WPTERR_GENERAL; |
1564 |
goto leave; |
goto leave; |
1565 |
} |
} |
1566 |
gpgme_get_import_status( ctx, import_res, NULL ); |
|
1567 |
print_import_status( import_res, c->implist_revcert ); |
res = gpgme_op_import_result (ctx); |
1568 |
if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0 ) { |
print_import_status (res); |
1569 |
msg_box( c->dlg, _("Key without a self signature was dectected!\n" |
if (res->no_user_id > 0) { |
1570 |
|
msg_box (c->dlg, _("Key without a self signature was dectected!\n" |
1571 |
"(This key is NOT usable for encryption, etc)\n" |
"(This key is NOT usable for encryption, etc)\n" |
1572 |
"\n" |
"\n" |
1573 |
"Cannot import these key(s)!"), _("Import"), MB_INFO ); |
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
1574 |
} |
} |
1575 |
|
|
1576 |
leave: |
leave: |
1577 |
free_if_alloc( src ); |
if (keydata) |
1578 |
|
gpg_file_data_release (keydata); |
1579 |
return rc; |
return rc; |
1580 |
} /* fm_import */ |
} /* fm_import */ |
1581 |
|
|
1582 |
|
|
1583 |
|
/* Export the selected keys from the File Manager to a file. */ |
1584 |
int |
int |
1585 |
fm_export( fm_state_t c ) |
fm_export (fm_state_t c) |
1586 |
{ |
{ |
1587 |
int rc = 0, id = 0; |
int rc = 0, id = 0; |
1588 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1589 |
gpgme_error_t err; |
gpgme_error_t err; |
1590 |
gpgme_recipients_t rset = c->recp; |
gpgme_key_t *rset = c->recp; |
1591 |
|
file_data_t keydata = NULL; |
1592 |
const char *name, *s = NULL; |
const char *name, *s = NULL; |
1593 |
char *p = NULL, *dst = NULL; |
char *p = NULL, *patt = NULL; |
|
void *recp; |
|
1594 |
|
|
1595 |
if( !gpgme_recipients_count( rset ) ) { |
if (!rset || !rset[0]) { |
1596 |
msg_box( c->dlg, _("No key was selected for export."), _("Export"), MB_ERR ); |
msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR); |
1597 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1598 |
goto leave; |
goto leave; |
1599 |
} |
} |
1600 |
|
|
1601 |
if( gpgme_recipients_count( rset ) == 1 ) { |
if (rset[1] == NULL) { /* count == 1*/ |
1602 |
err = gpgme_recipients_enum_open( rset, &recp ); |
gpgme_key_t k = rset[0]; |
1603 |
if( err ) |
const char *s = k->uids->name; |
1604 |
BUG( NULL ); |
p = new char[strlen (s)+1+8]; |
1605 |
s = gpgme_recipients_enum_read( rset, &recp ); |
if (!p) |
1606 |
gpgme_recipients_enum_close( rset, &recp ); |
BUG (NULL); |
1607 |
p = new char[strlen( s )+1+8]; |
strcpy (p, s ); |
1608 |
if( !p ) |
strcat (p, ".asc"); |
|
BUG( NULL ); |
|
|
strcpy( p, s ); |
|
|
strcat( p, ".asc" ); |
|
1609 |
} |
} |
1610 |
|
|
1611 |
name = get_filename_dlg( c->dlg, FILE_SAVE, _("Choose Name for Key File"), NULL, p? p : NULL ); |
name = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Name for Key File"), |
1612 |
|
NULL, p? p : NULL); |
1613 |
|
|
1614 |
if( !name ) |
if (!name) |
1615 |
name = "keys.gpg"; |
name = "keys.gpg"; |
1616 |
|
|
1617 |
dst = fm_quote_file( name ); |
patt = gpg_keylist_to_pattern (rset, c->n_recp); |
1618 |
err = gpgme_op_file_export( ctx, rset, dst ); |
|
1619 |
if( err ) { |
err = gpg_file_data_new (name, 0, &keydata); |
1620 |
msg_box( c->dlg, gpgme_strerror( err ), _("Export"), MB_ERR ); |
if (err) |
1621 |
rc = WPTERR_GENERAL; |
goto leave; |
1622 |
|
|
1623 |
|
op_begin (); |
1624 |
|
err = gpgme_op_export (ctx, patt, 0, keydata->dat); |
1625 |
|
op_end (); |
1626 |
|
if (err) { |
1627 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR); |
1628 |
|
rc = WPTERR_GENERAL; |
1629 |
goto leave; |
goto leave; |
1630 |
} |
} |
1631 |
log_box( _("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name ); |
log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name); |
1632 |
|
|
1633 |
leave: |
leave: |
1634 |
free_if_alloc( dst ); |
if (keydata) |
1635 |
free_if_alloc( p ); |
gpg_file_data_release (keydata); |
1636 |
|
if (patt) |
1637 |
|
free (patt); |
1638 |
|
free_if_alloc (p); |
1639 |
|
|
1640 |
return rc; |
return rc; |
1641 |
} /* fm_export */ |
} |
1642 |
|
|
1643 |
|
|
1644 |
|
/* Parse the command line and process the given file. */ |
1645 |
int |
int |
1646 |
fm_parse_command_line (char *cmdl) |
fm_parse_command_line (char *cmdl) |
1647 |
{ |
{ |
1649 |
const char *s; |
const char *s; |
1650 |
char *p, *fn = NULL; |
char *p, *fn = NULL; |
1651 |
int count = 0, detached = 0; |
int count = 0, detached = 0; |
1652 |
|
int type; |
1653 |
|
|
1654 |
if( !cmdl || !*cmdl ) |
if (!cmdl || !*cmdl) |
1655 |
return 0; |
return 0; |
1656 |
|
|
1657 |
fm_state_new( &ctx ); |
fm_state_new (&ctx); |
1658 |
ctx->dlg = GetActiveWindow( ); |
ctx->dlg = GetActiveWindow (); |
1659 |
ctx->cache_cb = 1; |
ctx->cache_cb = 1; |
1660 |
|
|
1661 |
p = cmdl; |
p = cmdl; |
1662 |
if( p && *p > 32 && !memistr( p, strlen( p ), "winpt.exe" ) |
if (p && *p > 32 && !stristr (p, "winpt.exe") |
1663 |
&& !strstr( p, "--" ) ) { |
&& !strstr (p, "--" )) { |
1664 |
count++; |
count++; |
1665 |
if( *p == '"' ) { /* need to remove quotes */ |
if (*p == '"') { /* need to remove quotes */ |
1666 |
fn = new char[strlen( p )]; |
fn = new char[strlen (p)]; |
1667 |
if( !fn ) |
if (!fn) |
1668 |
BUG( NULL ); |
BUG (NULL); |
1669 |
memcpy( fn, p+1, strlen( p ) - 2 ); |
memcpy (fn, p+1, strlen (p) - 2); |
1670 |
fn[strlen( p ) -2] = '\0'; |
fn[strlen (p) -2] = '\0'; |
1671 |
} |
} |
1672 |
else |
else |
1673 |
fn = m_strdup (p); |
fn = m_strdup (p); |
1674 |
s = fm_get_file_type (fn); |
s = fm_get_file_type (fn, &type); |
1675 |
if (!s || !strcmp (s, "UNKNOWN")) |
if (!s || !strcmp (s, "UNKNOWN")) |
1676 |
s = gnupg_check_file_ext (fn); |
s = gnupg_check_file_ext (fn, &type); |
1677 |
if (*s == 'U') { |
if (type == PGP_NONE) { |
1678 |
log_box( _("File Manager"), MB_ERR, _("%s: no valid OpenPGP data found."), p ); |
log_box (_("File Manager"), MB_ERR, |
1679 |
|
_("%s: no valid OpenPGP data found."), p); |
1680 |
|
free_if_alloc (fn); |
1681 |
return count; |
return count; |
1682 |
} |
} |
1683 |
switch( *s ) { |
switch (type) { |
1684 |
case 'E': fm_decrypt( ctx, fn ); break; |
case PGP_MESSAGE: |
1685 |
case 'P': fm_import( ctx, fn ); break; |
fm_decrypt (ctx, fn); |
1686 |
case 'S': |
break; |
1687 |
file_verify_use_event( ); |
|
1688 |
if( s[1] == 'I' ) { |
case PGP_PUBKEY: |
1689 |
if( strlen( s ) == 13 && s[7] == 'D' ) |
case PGP_SECKEY: |
1690 |
detached = 1; |
fm_import (ctx, fn); |
1691 |
fm_verify( ctx, detached, fn ); |
break; |
1692 |
} |
|
1693 |
file_verify_wait( ); |
case PGP_SIG: |
1694 |
|
case PGP_CLEARSIG: |
1695 |
|
file_verify_use_event (); |
1696 |
|
if (type == PGP_SIG) |
1697 |
|
detached = 1; |
1698 |
|
fm_verify (ctx, detached, fn); |
1699 |
|
file_verify_wait (); |
1700 |
break; |
break; |
1701 |
} |
} |
1702 |
} |
} |
1703 |
|
|
1704 |
memset( &ctx->pass_cb, 0, sizeof (ctx->pass_cb) ); |
wipememory (&ctx->pass_cb, sizeof (ctx->pass_cb)); |
1705 |
safe_free( fn ); |
free_if_alloc (fn); |
1706 |
fm_state_release( ctx ); |
fm_state_release (ctx); |
1707 |
return count; |
return count; |
1708 |
} /* fm_parse_command_line */ |
} |
1709 |
|
|
1710 |
|
|
1711 |
const char * |
const char* |
1712 |
default_dirname( const char * name ) |
default_dirname (const char *name) |
1713 |
{ |
{ |
1714 |
char * p = strrchr( name, '\\' ); |
char * p = strrchr( name, '\\' ); |
1715 |
if( !p ) |
if( !p ) |
1759 |
rc = -1; |
rc = -1; |
1760 |
goto leave; |
goto leave; |
1761 |
} |
} |
|
if( !overwrite_file( s ) ) { |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
1762 |
|
|
1763 |
rc = pk_archiv_create( list, s ); |
rc = pk_archiv_create( list, s ); |
1764 |
if( rc ) |
if( rc ) |
1768 |
unlink( s ); |
unlink( s ); |
1769 |
} |
} |
1770 |
leave: |
leave: |
1771 |
|
FindClose (hd); |
1772 |
pk_list_free( list ); |
pk_list_free( list ); |
1773 |
free_if_alloc( patt ); |
free_if_alloc( patt ); |
1774 |
return rc; |
return rc; |
1806 |
|
|
1807 |
if( listview_count_items( lv, 0 ) == 0 ) |
if( listview_count_items( lv, 0 ) == 0 ) |
1808 |
return; |
return; |
1809 |
memset( &mdctx, 0, sizeof (mdctx) ); |
memset (&mdctx, 0, sizeof (mdctx)); |
1810 |
mdctx.lv = lv; |
mdctx.lv = lv; |
1811 |
mdctx.mdalgo = mdalgo; |
mdctx.mdalgo = mdalgo; |
1812 |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg, |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg, |
1823 |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
1824 |
if (rc == -1) |
if (rc == -1) |
1825 |
return 0; |
return 0; |
1826 |
mapi_send_ascfile (buf); |
/*mapi_send_ascfile (buf); XXX */ |
1827 |
return 0; |
return 0; |
1828 |
} |
} |