1 |
/* wptFileManager.cpp - File Manager routines |
/* wptFileManager.cpp - File Manager routines |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
53 |
#include "wptKeyManager.h" |
#include "wptKeyManager.h" |
54 |
#include "openpgp.h" |
#include "openpgp.h" |
55 |
|
|
|
#define op_begin() SetCursor (LoadCursor (NULL, IDC_WAIT)) |
|
|
#define op_end() SetCursor (LoadCursor (NULL, IDC_ARROW)) |
|
56 |
|
|
57 |
void progress_cleanup (progress_filter_s *pfx); |
void progress_cleanup (progress_filter_s *pfx); |
58 |
BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, |
BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, |
61 |
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, |
62 |
int prev_was_bad, int fd); |
int prev_was_bad, int fd); |
63 |
|
|
|
/*-- wptFileVerifyDlg.cpp --*/ |
|
|
void file_verify_add_state (file_sig_ctx_t c); |
|
|
void file_verify_use_event (void); |
|
|
void file_verify_wait (void); |
|
64 |
|
|
65 |
static const char * mm_files[] = {".mov", ".avi", ".mpg", ".mpeg", |
/* Symbolic column IDs. */ |
66 |
|
enum { |
67 |
|
FM_COL_STAT = 0, |
68 |
|
FM_COL_NAME = 1, |
69 |
|
FM_COL_OP = 2 |
70 |
|
}; |
71 |
|
|
72 |
|
static const char *mm_files[] = {".mov", ".avi", ".mpg", ".mpeg", |
73 |
".mp3", ".wav", ".mid", ".wma", |
".mp3", ".wav", ".mid", ".wma", |
74 |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
75 |
|
|
228 |
|
|
229 |
/* Check the armor type of the file @fname and return |
/* Check the armor type of the file @fname and return |
230 |
a string representation of it. */ |
a string representation of it. */ |
231 |
static const char * |
static const char* |
232 |
fm_check_armor_type (const char *fname, int *r_type) |
fm_check_armor_type (const char *fname, int *r_type) |
233 |
{ |
{ |
234 |
FILE * fp; |
FILE *fp; |
235 |
char header[768], * p; |
char header[768], *p; |
236 |
|
|
237 |
if (r_type) |
if (r_type) |
238 |
*r_type = PGP_NONE; |
*r_type = PGP_NONE; |
384 |
{0, 80, (char *)_("Status") }, |
{0, 80, (char *)_("Status") }, |
385 |
{1, 256, (char *)_("Name") }, |
{1, 256, (char *)_("Name") }, |
386 |
{2, 128, (char *)_("Operation") }, |
{2, 128, (char *)_("Operation") }, |
387 |
{0, 0, NULL } |
{0, 0, NULL} |
388 |
}; |
}; |
389 |
|
|
390 |
rc = listview_new (&c); |
rc = listview_new (&c); |
414 |
fm_state_new (fm_state_t * ctx) |
fm_state_new (fm_state_t * ctx) |
415 |
{ |
{ |
416 |
gpgme_error_t rc; |
gpgme_error_t rc; |
417 |
fm_state_s * c; |
fm_state_s *c; |
418 |
|
|
419 |
c = new fm_state_s; |
c = new fm_state_s; |
420 |
if (!c) |
if (!c) |
423 |
rc = gpgme_new (&c->ctx); |
rc = gpgme_new (&c->ctx); |
424 |
if (rc) |
if (rc) |
425 |
BUG (0); |
BUG (0); |
|
/* XXX rc = gpgme_recipients_new (&c->recp);*/ |
|
|
/* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */ |
|
426 |
*ctx = c; |
*ctx = c; |
427 |
return 0; |
return 0; |
428 |
} /* fm_state_new */ |
} |
429 |
|
|
430 |
|
|
431 |
/* Release the FM state handle @c. */ |
/* Release the FM state handle @c. */ |
441 |
safe_free (c->recp); |
safe_free (c->recp); |
442 |
free_if_alloc (c->opaque); |
free_if_alloc (c->opaque); |
443 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
444 |
delete c; c = NULL; |
delete c; |
445 |
} |
} |
446 |
|
|
447 |
|
|
448 |
static int |
static int |
449 |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
450 |
{ |
{ |
459 |
} |
} |
460 |
|
|
461 |
return 0; |
return 0; |
462 |
} /* fm_check_for_entry */ |
} |
463 |
|
|
464 |
|
|
465 |
static int |
static int |
483 |
} |
} |
484 |
|
|
485 |
|
|
486 |
|
/* Add all files from the directory @path to the list view @lv. */ |
487 |
static int |
static int |
488 |
fm_add_dir_files (listview_ctrl_t lv, char *path) |
fm_add_dir_files (listview_ctrl_t lv, char *path) |
489 |
{ |
{ |
548 |
if (!type) |
if (!type) |
549 |
return WPTERR_FILE_OPEN; |
return WPTERR_FILE_OPEN; |
550 |
if (!strcmp (type, "UNKNOWN")) |
if (!strcmp (type, "UNKNOWN")) |
551 |
type = gnupg_check_file_ext (name, NULL); |
type = gnupg_check_file_ext (name, NULL); |
552 |
rc = listview_add_item (lv, ""); |
rc = listview_add_item (lv, ""); |
553 |
if (!rc) { |
if (!rc) { |
554 |
listview_add_sub_item (lv, 0, 0, type); |
listview_add_sub_item (lv, 0, 0, type); |
555 |
listview_add_sub_item (lv, 0, 1, name); |
listview_add_sub_item (lv, 0, 1, name); |
556 |
} |
} |
948 |
case FM_IMPORT: |
case FM_IMPORT: |
949 |
free_if_alloc (ctx->opaque); |
free_if_alloc (ctx->opaque); |
950 |
ctx->opaque = m_strdup (fname); |
ctx->opaque = m_strdup (fname); |
|
if (!ctx->opaque) |
|
|
BUG (0); |
|
951 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
952 |
file_import_dlg_proc, (LPARAM)ctx); |
file_import_dlg_proc, (LPARAM)ctx); |
953 |
if (ctx->cancel == 1) |
if (ctx->cancel == 1) |
1029 |
free_if_alloc (*dst); |
free_if_alloc (*dst); |
1030 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
1031 |
c->output = m_strdup (s); |
c->output = m_strdup (s); |
|
if (!c->output) |
|
|
BUG (0); |
|
1032 |
if (dst) |
if (dst) |
1033 |
*dst = fm_quote_file (s); |
*dst = fm_quote_file (s); |
1034 |
return 0; |
return 0; |
1096 |
gpgme_key_t sigkey = gpgme_signers_enum (ctx, 0); |
gpgme_key_t sigkey = gpgme_signers_enum (ctx, 0); |
1097 |
if (sigkey && sigkey->subkeys) { |
if (sigkey && sigkey->subkeys) { |
1098 |
keyid = m_strdup (sigkey->subkeys->keyid); |
keyid = m_strdup (sigkey->subkeys->keyid); |
|
if (!keyid) |
|
|
BUG (NULL); |
|
1099 |
} |
} |
1100 |
} |
} |
1101 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1256 |
} |
} |
1257 |
|
|
1258 |
|
|
1259 |
|
/* If the decrypt result contains the original file name, |
1260 |
|
we use it instead of the artificial "output - .gpg" string. */ |
1261 |
|
static int |
1262 |
|
restore_original_name (const char *output, const char *plaintext_filename) |
1263 |
|
{ |
1264 |
|
char *dir; |
1265 |
|
char *orig; |
1266 |
|
int rc = 0; |
1267 |
|
|
1268 |
|
dir = strrchr (output, '\\'); |
1269 |
|
if (!dir) |
1270 |
|
orig = strdup (plaintext_filename); |
1271 |
|
else { |
1272 |
|
orig = (char*)calloc (1, strlen (plaintext_filename)+ 1 + |
1273 |
|
strlen (output)+1); |
1274 |
|
if (!orig) |
1275 |
|
BUG (0); |
1276 |
|
memcpy (orig, output, (dir-output)+1); |
1277 |
|
strcat (orig, plaintext_filename); |
1278 |
|
} |
1279 |
|
if (overwrite_file (orig)) { |
1280 |
|
DeleteFile (orig); |
1281 |
|
if (!MoveFile (output, orig)) |
1282 |
|
rc = -1; |
1283 |
|
} |
1284 |
|
safe_free (orig); |
1285 |
|
return rc; |
1286 |
|
} |
1287 |
|
|
1288 |
|
|
1289 |
/* Decrypt the file @name. */ |
/* Decrypt the file @name. */ |
1290 |
int |
int |
1291 |
fm_decrypt (fm_state_t c, const char *name) |
fm_decrypt (fm_state_t c, const char *name) |
1294 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1295 |
gpgme_decrypt_result_t res; |
gpgme_decrypt_result_t res; |
1296 |
gpgme_verify_result_t sigres; |
gpgme_verify_result_t sigres; |
1297 |
file_data_t in =NULL, out=NULL; |
file_data_t in = NULL, out = NULL; |
1298 |
int rc = 0; |
int rc = 0; |
1299 |
|
|
1300 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1304 |
} |
} |
1305 |
|
|
1306 |
c->output = m_strdup (name); |
c->output = m_strdup (name); |
|
if (!c->output) |
|
|
BUG (0); |
|
1307 |
if (is_openpgp_ext (c->output)) |
if (is_openpgp_ext (c->output)) |
1308 |
c->output[strlen (c->output)-4] = '\0'; |
c->output[strlen (c->output)-4] = '\0'; |
1309 |
else { |
else { |
1313 |
if (s) { |
if (s) { |
1314 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
1315 |
c->output = m_strdup (s); |
c->output = m_strdup (s); |
|
if (!c->output) |
|
|
BUG (NULL); |
|
1316 |
} |
} |
1317 |
} |
} |
1318 |
|
|
1346 |
char *p = get_key_userid (keyid+8); |
char *p = get_key_userid (keyid+8); |
1347 |
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
1348 |
|
|
1349 |
log_box( _("Decryption"), MB_ERR, |
log_box (_("Decryption"), MB_ERR, |
1350 |
_("Encrypted with %s key, ID %s.%s\n" |
_("Encrypted with %s key, ID %s.%s\n" |
1351 |
"Decryption failed: secret key not available."), |
"Decryption failed: secret key not available."), |
1352 |
get_key_pubalgo (pkalgo), keyid+8, p); |
get_key_pubalgo (pkalgo), keyid+8, p); |
1363 |
log_box ("Decrypt", MB_ERR, |
log_box ("Decrypt", MB_ERR, |
1364 |
_("Decryption failed.\n%s: does not exist."), c->output); |
_("Decryption failed.\n%s: does not exist."), c->output); |
1365 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1366 |
|
goto leave; |
1367 |
|
} |
1368 |
|
else if (res && res->file_name) { |
1369 |
|
char *file = strrchr (c->output, '\\'); |
1370 |
|
int id = log_box (_("Decrypt"), MB_QUEST_ASK, |
1371 |
|
_("The original file name is '%s'.\n\n" |
1372 |
|
"Do you want to use this instead of '%s'?"), |
1373 |
|
res->file_name, file? file+1 : c->output); |
1374 |
|
if (id == IDYES) { |
1375 |
|
/* before we can move the file, it needs to be closed first. */ |
1376 |
|
gpg_file_data_release (out); |
1377 |
|
out = NULL; |
1378 |
|
restore_original_name (c->output, res->file_name); |
1379 |
|
} |
1380 |
} |
} |
|
|
|
1381 |
sigres = gpgme_op_verify_result (ctx); |
sigres = gpgme_op_verify_result (ctx); |
1382 |
if (sigres && sigres->signatures) |
if (sigres && sigres->signatures) |
1383 |
show_verify_result (sigres); |
show_verify_result (sigres); |
1384 |
|
|
1385 |
leave: |
leave: |
1386 |
if (in) |
if (in) |
1585 |
if (file) { |
if (file) { |
1586 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
1587 |
c->output = m_strdup (file); |
c->output = m_strdup (file); |
|
if (!c->output) |
|
|
BUG (NULL); |
|
1588 |
} |
} |
1589 |
else { |
else { |
1590 |
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
1623 |
|
|
1624 |
memset (&log, 0, sizeof (log)); |
memset (&log, 0, sizeof (log)); |
1625 |
log.file = m_strdup (name); |
log.file = m_strdup (name); |
|
if (!log.file) |
|
|
BUG (NULL); |
|
1626 |
file_verify_create_dlg (); |
file_verify_create_dlg (); |
1627 |
|
|
1628 |
err = gpg_file_data_new (name, F_DATA_READ, &in); |
err = gpg_file_data_new (name, F_DATA_READ, &in); |
1676 |
|
|
1677 |
free_if_alloc (c->output); |
free_if_alloc (c->output); |
1678 |
c->output = m_strdup (name); |
c->output = m_strdup (name); |
|
if (!c->output) |
|
|
BUG (NULL); |
|
1679 |
|
|
1680 |
err = gpg_file_data_new (name, F_DATA_READ, &keydata); |
err = gpg_file_data_new (name, F_DATA_READ, &keydata); |
1681 |
if (err) |
if (err) |
1809 |
|
|
1810 |
case PGP_SIG: |
case PGP_SIG: |
1811 |
case PGP_CLEARSIG: |
case PGP_CLEARSIG: |
|
file_verify_use_event (); |
|
1812 |
if (type == PGP_SIG) |
if (type == PGP_SIG) |
1813 |
detached = 1; |
detached = 1; |
1814 |
fm_verify (ctx, detached, fn); |
fm_verify (ctx, detached, fn); |