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 |
* |
* |
43 |
#include "wptGPGZIP.h" |
#include "wptGPGZIP.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); |
void progress_cleanup (progress_filter_s *pfx); |
51 |
void progress_cleanup (progress_filter_s * pfx); |
BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, |
52 |
|
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, |
55 |
|
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 |
|
|
63 |
".mp3", ".wav", ".mid", ".wma", |
".mp3", ".wav", ".mid", ".wma", |
64 |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
65 |
|
|
66 |
char * |
|
67 |
fm_quote_file (const char * name) |
/* Check if the drive given by @fname is a floppy disc. |
68 |
|
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 |
|
|
96 |
if (file_exist_check (fname)) |
if (file_exist_check (fname)) |
97 |
return 1; |
return -1; |
98 |
id = log_box (_("File Manager"), MB_YESNO, |
id = log_box (_("File Manager"), MB_YESNO, |
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")) |
179 |
|
|
180 |
|
|
181 |
const char* |
const char* |
182 |
file_get_extension (gpgme_ctx_t ctx, gpgme_sigmode_t sigmode) |
file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode) |
183 |
{ |
{ |
184 |
int use_armor = (int)gpgme_control (ctx, GPGME_CTRL_ARMOR, -1); |
int use_armor = gpgme_get_armor (ctx); |
185 |
|
|
186 |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
187 |
return ".asc"; |
return ".asc"; |
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; |
388 |
BUG (0); |
BUG (0); |
389 |
memset (c, 0, sizeof * c); |
memset (c, 0, sizeof * c); |
390 |
rc = gpgme_new (&c->ctx); |
rc = gpgme_new (&c->ctx); |
|
if (!rc) |
|
|
rc = gpgme_recipients_new (&c->recp); |
|
391 |
if (rc) |
if (rc) |
392 |
BUG (0); |
BUG (0); |
393 |
gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); |
/* XXX rc = gpgme_recipients_new (&c->recp);*/ |
394 |
|
/* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */ |
395 |
*ctx = c; |
*ctx = c; |
396 |
return 0; |
return 0; |
397 |
} /* fm_state_new */ |
} /* fm_state_new */ |
398 |
|
|
399 |
|
|
400 |
|
/* Release the FM state handle @c. */ |
401 |
void |
void |
402 |
fm_state_release (fm_state_t c) |
fm_state_release (fm_state_t c) |
403 |
{ |
{ |
404 |
if (c) |
if (!c) |
405 |
{ |
return; |
406 |
if (c->recp) |
if (c->recp) |
407 |
{ |
free (c->recp); |
408 |
gpgme_recipients_release (c->recp); |
if (c->ctx) { |
409 |
c->recp = NULL; |
gpgme_release (c->ctx); |
410 |
} |
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; |
|
411 |
} |
} |
412 |
} /* fm_state_release */ |
free_if_alloc (c->opaque); |
413 |
|
free_if_alloc (c->output); |
414 |
|
delete c; c = NULL; |
415 |
|
} |
416 |
|
|
417 |
static int |
static int |
418 |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
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; |
479 |
} |
} |
480 |
|
|
481 |
|
|
482 |
|
/* Add the drag & drop files from @dd_files to the |
483 |
|
list view control @lv. */ |
484 |
int |
int |
485 |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
486 |
{ |
{ |
487 |
char name[384+4]; |
char name[384+4]; |
488 |
int nfiles, rc, i; |
int nfiles, rc, i; |
489 |
|
|
490 |
memset( name, 0, sizeof (name) ); |
memset (name, 0, sizeof (name)); |
491 |
nfiles = DragQueryFile( dd_files, 0xFFFFFFFF, NULL, 0 ); |
nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0); |
492 |
for (i = 0; i < nfiles; i++) { |
for (i = 0; i < nfiles; i++) { |
493 |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
494 |
if (is_directory (name)) |
if (is_directory (name)) |
497 |
rc = fm_set_ftype (lv, name); |
rc = fm_set_ftype (lv, name); |
498 |
if (rc == -1) |
if (rc == -1) |
499 |
break; |
break; |
|
|
|
500 |
} |
} |
501 |
return rc; |
return rc; |
502 |
} /* fm_add_dropped_files */ |
} |
503 |
|
|
504 |
|
|
505 |
int |
int |
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"); |
518 |
open.lpstrFilter = _("All Files (*.*)\0*.*"); |
open.lpstrFilter = _("All Files (*.*)\0*.*\0\0"); |
519 |
open.hwndOwner = dlg; |
open.hwndOwner = dlg; |
520 |
open.lpstrFile = file; |
open.lpstrFile = file; |
521 |
open.nMaxFile = sizeof (file) - 1; |
open.nMaxFile = sizeof (file) - 1; |
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 |
} |
} |
535 |
} |
} |
536 |
|
|
537 |
return rc; |
return rc; |
538 |
} /* 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 */ |
|
539 |
|
|
540 |
|
|
541 |
int |
int |
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); |
552 |
gpgme_data_new_from_clipboard (&dat); |
gpg_data_release_and_set_file (dat, "gpgme.tmp"); |
|
gpgme_data_release_and_set_file (dat, "gpgme.tmp"); |
|
553 |
|
|
554 |
fp = gpg_iobuf_open ("gpgme.tmp"); |
fp = gpg_iobuf_open ("gpgme.tmp"); |
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 |
{ |
{ |
623 |
case FM_ENCRYPT: |
case FM_ENCRYPT: |
624 |
case FM_ENCRYPT_DIR: |
case FM_ENCRYPT_DIR: |
625 |
case FM_SIGNENCRYPT: |
case FM_SIGNENCRYPT: |
626 |
if (strcmp (status, "ENCRYPTED")) |
if (strcmp (status, "ENCRYPTED") |
627 |
|
&& strcmp (status, "SYMKEYENC")) |
628 |
rc = 1; |
rc = 1; |
629 |
break; |
break; |
630 |
|
|
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 |
{ |
{ |
766 |
if (rc) |
if (rc) |
767 |
BUG (0); |
BUG (0); |
768 |
ctx->dlg = dlg; |
ctx->dlg = dlg; |
769 |
|
|
770 |
|
// XXX: for file operations the progress dialog will be |
771 |
|
// reloaded somewhere and thus a 'dummy' dialog remains |
772 |
|
|
773 |
|
/* we use it here to make sure that pfx_cleanup will not use |
774 |
|
any weird values. */ |
775 |
memset (&pfx, 0, sizeof (pfx)); |
memset (&pfx, 0, sizeof (pfx)); |
776 |
gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx); |
ctx->prog_cb = NULL; |
777 |
|
if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) { |
778 |
|
pfx.hwnd = dlg; |
779 |
|
/*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 */ |
784 |
switch (fm_cmd) { |
switch (fm_cmd) { |
813 |
if( !listview_get_item_state( lv, i ) ) |
if( !listview_get_item_state( lv, i ) ) |
814 |
continue; |
continue; |
815 |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
816 |
if( !strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT ) |
if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT) |
817 |
n++; |
n++; |
818 |
if( !strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN ) |
if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN) |
819 |
n++; |
n++; |
820 |
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); |
|
821 |
ndel++; |
ndel++; |
|
} |
|
822 |
} |
} |
823 |
|
|
824 |
if( n > 1 ) |
if (n > 1 && fm_cmd != FM_SYMENC) |
825 |
ctx->cache_cb = 1; |
ctx->cache_cb = 1; |
826 |
|
|
827 |
if( fm_cmd == FM_WIPE && ndel > 0 ) { |
if (fm_cmd == FM_WIPE && ndel > 0) { |
828 |
|
memset (&confirm, 0, sizeof confirm); |
829 |
|
confirm.lv_files = lv; |
830 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
831 |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
832 |
if (!confirm.yes) |
if (!confirm.yes) |
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 |
|
|
893 |
leave: |
leave: |
894 |
if (!rc) |
if (!rc) |
895 |
fm_state_release (ctx); |
fm_state_release (ctx); |
|
if (confirm.rset) |
|
|
gpgme_recipients_release (confirm.rset); |
|
896 |
progress_cleanup (&pfx); |
progress_cleanup (&pfx); |
897 |
return rc; |
return rc; |
898 |
} /* fm_parse_files */ |
} /* fm_parse_files */ |
899 |
|
|
900 |
|
|
901 |
|
/* Wipe the given file @name with the delete mode |
902 |
|
from the configuration. |
903 |
|
Return value: 0 on success. */ |
904 |
int |
int |
905 |
fm_wipe (const char * name) |
fm_wipe (const char *name) |
906 |
{ |
{ |
907 |
int rc; |
int rc; |
908 |
|
|
911 |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
912 |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
913 |
return rc; |
return rc; |
914 |
} /* fm_wipe */ |
} |
915 |
|
|
916 |
|
|
917 |
|
/* Dump out the given PGP packets from file @name in a dialog. */ |
918 |
int |
int |
919 |
fm_list( const char * name, HWND dlg ) |
fm_list (const char *name, HWND dlg) |
920 |
{ |
{ |
921 |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
922 |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
923 |
IDS_WINPT_FILE_STAT ); |
IDS_WINPT_FILE_STAT ); |
924 |
return 0; |
return 0; |
925 |
} /* fm_list */ |
} |
926 |
|
|
927 |
|
|
928 |
static int |
static int |
929 |
ask_filename (fm_state_t c, const char * msg, char ** dst) |
ask_filename (fm_state_t c, const char *msg, char **dst) |
930 |
{ |
{ |
931 |
const char * s; |
const char * s; |
932 |
|
|
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) |
942 |
BUG (0); |
BUG (0); |
943 |
*dst = fm_quote_file (s); |
if (dst) |
944 |
|
*dst = fm_quote_file (s); |
945 |
return 0; |
return 0; |
946 |
} |
} |
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 |
char * src = NULL, * dst = 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 |
|
|
|
src = fm_quote_file (name); |
|
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); |
963 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
964 |
strcpy (c->output, name ); |
strcpy (c->output, name); |
965 |
strcat (c->output, ext ); |
strcat (c->output, ext); |
|
dst = fm_quote_file (c->output); |
|
966 |
|
|
967 |
if (!overwrite_file (c->output)) |
if (!overwrite_file (c->output)) { |
968 |
{ |
rc = ask_filename (c, _("Enter filename for encrypted file"), NULL); |
|
rc = ask_filename (c, _("Enter filename for encrypted file"), &dst); |
|
969 |
if (rc) |
if (rc) |
970 |
goto leave; |
goto leave; |
971 |
} |
} |
972 |
|
|
973 |
|
err = gpg_file_data_new (name, 1, &in); |
974 |
|
if (err) |
975 |
|
goto leave; |
976 |
|
err = gpg_file_data_new (c->output, 0, &out); |
977 |
|
if (err) |
978 |
|
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 |
988 |
no_compr = is_multi_media (name); |
no_compr = is_multi_media (name); |
989 |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
990 |
|
*/ |
991 |
|
|
992 |
if (sign) { |
if (sign) { |
993 |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
994 |
keyid = get_gnupg_default_key (); |
keyid = get_gnupg_default_key (); |
995 |
if (!keyid) { |
if (!keyid) { |
996 |
msg_box (c->dlg, _("Could not get default secret key."), |
msg_box (c->dlg, _("Could not get default secret key."), |
997 |
_("Signing"), MB_ERR); |
_("Signing"), MB_ERR); |
998 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
999 |
goto leave; |
goto leave; |
1003 |
gpgme_signers_add (ctx, key); |
gpgme_signers_add (ctx, key); |
1004 |
} |
} |
1005 |
else { |
else { |
1006 |
const char * s; |
gpgme_key_t key = gpgme_signers_enum (ctx, 0); |
1007 |
s = (char *)gpgme_key_get_string_attr (gpgme_signers_enum (ctx, 0), |
if (key && key->subkeys) { |
1008 |
GPGME_ATTR_KEYID, NULL, 0); |
keyid = m_strdup (key->subkeys->keyid); |
1009 |
keyid = m_strdup (s); |
if (!keyid) |
1010 |
|
BUG (NULL); |
1011 |
|
} |
1012 |
} |
} |
1013 |
if (!c->init_cb || !c->cache_cb) { |
if (!c->init_cb || !c->cache_cb) { |
1014 |
set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_SIGN, |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, |
1015 |
c->dlg, _("Signing")); |
c->dlg, _("Signing")); |
1016 |
c->init_cb = 1; |
c->init_cb = 1; |
1017 |
} |
} |
1018 |
err = gpgme_op_file_sign_encrypt (ctx, c->recp, src, dst); |
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1019 |
|
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 == GPGME_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_file_encrypt (ctx, c->recp, src, dst); |
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1036 |
|
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); |
1039 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1044 |
secure_unlink (name, WIPE_MODE_SIMPLE); |
secure_unlink (name, WIPE_MODE_SIMPLE); |
1045 |
|
|
1046 |
leave: |
leave: |
1047 |
|
if (in) |
1048 |
|
gpg_file_data_release (in); |
1049 |
|
if (out) |
1050 |
|
gpg_file_data_release (out); |
1051 |
free_if_alloc (keyid); |
free_if_alloc (keyid); |
|
free_if_alloc (dst); |
|
|
free_if_alloc (src); |
|
1052 |
return rc; |
return rc; |
1053 |
} /* fm_encrypt */ |
} |
1054 |
|
|
1055 |
|
|
1056 |
int |
int |
1057 |
fm_sym_encrypt (fm_state_t c, const char * name) |
fm_sym_encrypt (fm_state_t c, const char * name) |
1058 |
{ |
{ |
|
int rc = 0, cancel = 0; |
|
|
char * src = NULL, * dst = NULL; |
|
|
char ext[5], * pass; |
|
1059 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1060 |
gpgme_error_t err; |
gpgme_error_t err; |
1061 |
|
file_data_t in=NULL, out=NULL; |
1062 |
|
int rc = 0, cancel = 0; |
1063 |
|
char * src = NULL, * dst = NULL; |
1064 |
|
char ext[5], * pass; |
1065 |
|
|
1066 |
pass = request_passphrase2 (_("Symmetric"), &cancel); |
pass = request_passphrase2 (_("Symmetric"), 0, &cancel); |
1067 |
if (cancel) |
if (cancel) |
1068 |
return 0; |
return 0; |
1069 |
|
|
1070 |
gpgme_control (ctx, GPGME_CTRL_CIPHER, -1); |
/* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/ |
|
src = fm_quote_file (name); |
|
1071 |
c->output = new char[strlen (name) + 5 + 1]; |
c->output = new char[strlen (name) + 5 + 1]; |
1072 |
if (!c->output) |
if (!c->output) |
1073 |
BUG (0); |
BUG (0); |
1074 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1075 |
strcpy (c->output, name); |
strcpy (c->output, name); |
1076 |
strcat (c->output, ext); |
strcat (c->output, ext); |
|
dst = fm_quote_file (c->output); |
|
1077 |
|
|
1078 |
if (overwrite_file (c->output) == 0) { |
if (overwrite_file (c->output) == 0) { |
1079 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1080 |
goto leave; |
goto leave; |
1081 |
} |
} |
1082 |
gpgme_set_passphrase (ctx, pass); |
|
1083 |
err = gpgme_op_file_encrypt (ctx, NULL, src, dst); |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
1084 |
|
|
1085 |
|
err = gpg_file_data_new (name, 1, &in); |
1086 |
|
if (err) |
1087 |
|
goto leave; |
1088 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1089 |
|
if (err) |
1090 |
|
goto leave; |
1091 |
|
|
1092 |
|
err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in->dat, out->dat); |
1093 |
if (err) { |
if (err) { |
1094 |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
1095 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1101 |
} |
} |
1102 |
|
|
1103 |
leave: |
leave: |
1104 |
free_if_alloc (src); |
if (in) |
1105 |
free_if_alloc (dst); |
gpg_file_data_release (in); |
1106 |
|
if (out) |
1107 |
|
gpg_file_data_release (out); |
1108 |
sfree_if_alloc (pass); |
sfree_if_alloc (pass); |
1109 |
return rc; |
return rc; |
1110 |
} /* fm_sym_encrypt */ |
} /* fm_sym_encrypt */ |
1111 |
|
|
1112 |
|
|
1113 |
static gpgme_error_t |
/* Show the human readable verify result from @sigres. */ |
1114 |
fm_list_keys( const char * name, gpgme_recipients_t *r_keys ) |
static void |
1115 |
|
show_verify_result (gpgme_verify_result_t sigres) |
1116 |
{ |
{ |
1117 |
return gpgme_op_list_keys( NULL, name, r_keys ); |
gpgme_key_t key=NULL; |
1118 |
} /* fm_list_keys */ |
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. */ |
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; |
1168 |
gpgme_recipients_t keys = NULL; |
gpgme_decrypt_result_t res; |
1169 |
gpgme_sig_t sig = NULL; |
gpgme_verify_result_t sigres; |
1170 |
gpgme_op_flags_t flags; |
file_data_t in =NULL, out=NULL; |
1171 |
char * src = NULL, * dst = NULL, 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) { |
1175 |
set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_DECRYPT, |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT, |
1176 |
c->dlg, _("Decryption")); |
c->dlg, _("Decryption")); |
1177 |
c->init_cb = 1; |
c->init_cb = 1; |
1178 |
} |
} |
1179 |
|
|
|
src = fm_quote_file (name); |
|
1180 |
c->output = m_strdup (name); |
c->output = m_strdup (name); |
1181 |
if (!c->output) |
if (!c->output) |
1182 |
BUG (0); |
BUG (0); |
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, _("Choose Filename for Output"), |
const char *s = get_filesave_dlg (c->dlg, _("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) |
1192 |
BUG( NULL ); |
BUG (NULL); |
1193 |
} |
} |
1194 |
} |
} |
|
dst = fm_quote_file( c->output ); |
|
|
|
|
|
err = fm_list_keys( src, &keys ); |
|
|
if( err ) |
|
|
goto leave; |
|
|
c->pass_cb.enc_to = keys; |
|
1195 |
|
|
1196 |
if (overwrite_file (c->output) == 0) { |
if (overwrite_file (c->output) == 0) { |
1197 |
rc = ask_filename (c, _("Please enter filename for plaintext file"), &dst); |
rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL); |
1198 |
if (rc) |
if (rc) |
1199 |
goto leave; |
goto leave; |
1200 |
} |
} |
1201 |
remove_crit_file_attrs( c->output, 0 ); |
|
1202 |
err = gpgme_op_file_decrypt( ctx, src, dst ); |
remove_crit_file_attrs (c->output, 0); |
1203 |
if( !c->cache_cb ) |
|
1204 |
memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) ); |
err = gpg_file_data_new (name, 1, &in); |
1205 |
if( c->pass_cb.cancel ) { |
if (err) |
1206 |
|
goto leave; |
1207 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1208 |
|
if (err) |
1209 |
|
goto leave; |
1210 |
|
|
1211 |
|
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
1212 |
|
if (!c->cache_cb) |
1213 |
|
release_gpg_passphrase_cb (&c->pass_cb); |
1214 |
|
if (c->pass_cb.cancel) { |
1215 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1216 |
goto leave; |
goto leave; |
1217 |
} |
} |
1218 |
gpgme_decrypt_get_status( ctx, keyid, &flags ); |
|
1219 |
if( err == GPGME_No_Seckey && (flags & GPGME_OPFLAG_NOSECKEY) ) { |
res = gpgme_op_decrypt_result (ctx); |
1220 |
char * p = get_key_userid( keyid+8 ); |
if (res && res->recipients && !secret_key_available (res->recipients)) { |
1221 |
int pkalgo = algo_from_list( keys, keyid ); |
const char *keyid = res->recipients->keyid; |
1222 |
|
char *p = get_key_userid (keyid+8); |
1223 |
|
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
1224 |
|
|
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."), |
1228 |
gpgme_key_expand_attr( GPGME_ATTR_ALGO, pkalgo ), |
get_key_pubalgo (pkalgo), keyid+8, p); |
|
keyid+8, p ); |
|
1229 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1230 |
free_if_alloc( p ); |
free_if_alloc (p); |
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 |
gpgme_decrypt_get_sig_ctx( ctx, &sig ); |
sigres = gpgme_op_verify_result (ctx); |
1244 |
|
if (sigres && sigres->signatures) |
1245 |
sigok = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY ) == GPGME_SIG_STAT_GOOD; |
show_verify_result (sigres); |
1246 |
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") ); |
|
|
} |
|
|
} |
|
1247 |
|
|
1248 |
leave: |
leave: |
1249 |
free_if_alloc( dst ); |
if (in) |
1250 |
free_if_alloc( src ); |
gpg_file_data_release (in); |
1251 |
gpgme_sig_release( sig ); |
if (out) |
1252 |
gpgme_recipients_release( keys ); |
gpg_file_data_release (out); |
1253 |
return rc; |
return rc; |
1254 |
} /* fm_decrypt */ |
} |
1255 |
|
|
1256 |
|
|
1257 |
int |
int |
1260 |
int rc = 0; |
int rc = 0; |
1261 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1262 |
gpgme_error_t err; |
gpgme_error_t err; |
1263 |
char *src = NULL, *dst = 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->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") ); |
1268 |
c->init_cb = 1; |
c->init_cb = 1; |
1269 |
} |
} |
1270 |
|
|
1271 |
src = fm_quote_file( name ); |
free_if_alloc (c->output); |
1272 |
free_if_alloc( c->output ); |
c->output = new char[strlen (name) + 5 + 1]; |
1273 |
c->output = new char[strlen( name ) + 5 + 1]; |
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); |
|
dst = fm_quote_file( c->output ); |
|
1278 |
|
|
1279 |
if (!overwrite_file (c->output)) { |
if (!overwrite_file (c->output)) { |
1280 |
rc = ask_filename (c, _("Enter filename for signed file"), &dst); |
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 |
err = gpgme_op_file_sign( ctx, c->sigmode, src, dst ); |
|
1286 |
|
err = gpg_file_data_new (name, 1, &in); |
1287 |
|
if (err) |
1288 |
|
goto leave; |
1289 |
|
err = gpg_file_data_new (c->output, 0, &out); |
1290 |
|
if (err) |
1291 |
|
goto leave; |
1292 |
|
|
1293 |
|
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; |
1304 |
} |
} |
1305 |
|
|
1306 |
leave: |
leave: |
1307 |
free_if_alloc( src ); |
if (in) |
1308 |
free_if_alloc( dst ); |
gpg_file_data_release (in); |
1309 |
|
if (out) |
1310 |
|
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 *uid, *keyid, * kid; |
const char *kid; |
1320 |
int not_found = 0; |
int not_found = 0; |
1321 |
|
|
1322 |
kid = gpgme_sig_get_string_attr( log->sig, GPGME_ATTR_KEYID ); |
kid = log->sig->fpr; |
1323 |
if( !kid ) |
if (!kid) |
1324 |
kid = "DEADBEEFDEADBEEF"; |
BUG (NULL); |
1325 |
if( strlen( kid ) == 16 ) |
if (strlen (kid) == 40) |
1326 |
keyid = kid + 8; |
kid += 32; |
1327 |
else if( strlen( kid ) == 32 ) |
else if (strlen (kid) == 32) |
1328 |
keyid = kid; |
kid += 24; |
1329 |
else if( strlen( kid ) == 40 ) |
if (get_pubkey (kid, &key)) |
1330 |
keyid = kid + 24; |
log->use_uid = 0; |
1331 |
if( get_pubkey( keyid, &key ) ) |
else { |
1332 |
not_found = 1; |
log->user_id = key->uids->uid; |
1333 |
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; |
|
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_sig_t sig; |
char fname[384]; |
1416 |
struct siglog_context_s log; |
|
1417 |
char * src = NULL; |
if (detached) |
1418 |
int rc = 0; |
title = _("Select Data File"); |
1419 |
size_t i; |
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 |
memset( &log, 0, sizeof (log) ); |
|
1463 |
strcpy( log.file, name ); |
/* Verify the signature from the file @name. If @detached 1, |
1464 |
file_verify_create_dlg(); |
it is assumed that a detached signature should be checked. */ |
1465 |
src = fm_quote_file( name ); |
int |
1466 |
|
fm_verify (fm_state_t c, int detached, const char *name) |
1467 |
err = gpgme_op_file_verify( ctx, c->sigmode, &sig, src, c->output ); |
{ |
1468 |
if( err == GPGME_Bad_Signature ) { |
gpgme_ctx_t ctx = c->ctx; |
1469 |
log.sig = sig; |
gpgme_error_t err; |
1470 |
fm_add_sig_stat( &log ); |
gpgme_signature_t s; |
1471 |
rc = WPTERR_GENERAL; |
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)); |
1483 |
|
log.file = m_strdup (name); |
1484 |
|
if (!log.file) |
1485 |
|
BUG (NULL); |
1486 |
|
file_verify_create_dlg (); |
1487 |
|
|
1488 |
|
err = gpg_file_data_new (name, 1, &in); |
1489 |
|
if (err) |
1490 |
goto leave; |
goto leave; |
1491 |
} |
err = gpg_file_data_new (c->output, detached? 1 : 0, &out); |
1492 |
if( err ) { |
if (err) |
1493 |
msg_box( c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR ); |
goto leave; |
1494 |
|
|
1495 |
|
if (c->sigmode == GPGME_SIG_MODE_DETACH) |
1496 |
|
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
1497 |
|
else |
1498 |
|
err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat); |
1499 |
|
if (err) { |
1500 |
|
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) { |
1507 |
|
log.sig = s; |
1508 |
|
fm_add_sig_stat (&log); |
1509 |
} |
} |
1510 |
for( i = 0; i < gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_LEVEL ); i++ ) { |
if (!c->output) |
1511 |
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 */ |
|
1512 |
|
|
1513 |
leave: |
leave: |
1514 |
free_if_alloc( src ); |
if (in) |
1515 |
|
gpg_file_data_release (in); |
1516 |
|
if (out) |
1517 |
|
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 |
1525 |
fm_import( fm_state_t c, const char *name ) |
fm_import (fm_state_t c, const char *name) |
1526 |
{ |
{ |
1527 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1528 |
gpgme_error_t err; |
gpgme_error_t err; |
1529 |
char *src = NULL; |
gpgme_import_result_t res; |
1530 |
int import_res[14] = {0}; |
file_data_t keydata = NULL; |
1531 |
int rc = 0; |
int rc = 0; |
1532 |
|
|
1533 |
free_if_alloc( c->output ); |
free_if_alloc (c->output); |
1534 |
c->output = m_strdup( name ); |
c->output = m_strdup (name); |
1535 |
if( !c->output ) |
if (!c->output) |
1536 |
BUG( NULL ); |
BUG (NULL); |
1537 |
src = fm_quote_file( name ); |
|
1538 |
err = gpgme_op_file_import( ctx, NULL, src ); |
err = gpg_file_data_new (name, 1, &keydata); |
1539 |
if( err ) { |
if (err) |
1540 |
msg_box( c->dlg, gpgme_strerror( err ), _("Import"), MB_ERR ); |
goto leave; |
1541 |
rc = WPTERR_GENERAL; |
|
1542 |
|
err = gpgme_op_import (ctx, keydata->dat); |
1543 |
|
if (err) { |
1544 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
1545 |
|
rc = WPTERR_GENERAL; |
1546 |
goto leave; |
goto leave; |
1547 |
} |
} |
1548 |
gpgme_get_import_status( ctx, import_res, NULL ); |
|
1549 |
print_import_status( import_res, c->implist_revcert ); |
res = gpgme_op_import_result (ctx); |
1550 |
if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0 ) { |
print_import_status (res); |
1551 |
msg_box( c->dlg, _("Key without a self signature was dectected!\n" |
if (res->no_user_id > 0) { |
1552 |
|
msg_box (c->dlg, _("Key without a self signature was dectected!\n" |
1553 |
"(This key is NOT usable for encryption, etc)\n" |
"(This key is NOT usable for encryption, etc)\n" |
1554 |
"\n" |
"\n" |
1555 |
"Cannot import these key(s)!"), _("Import"), MB_INFO ); |
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
1556 |
} |
} |
1557 |
|
|
1558 |
leave: |
leave: |
1559 |
free_if_alloc( src ); |
if (keydata) |
1560 |
|
gpg_file_data_release (keydata); |
1561 |
return rc; |
return rc; |
1562 |
} /* fm_import */ |
} /* fm_import */ |
1563 |
|
|
1564 |
|
|
1565 |
|
/* Export the selected keys from the File Manager to a file. */ |
1566 |
int |
int |
1567 |
fm_export( fm_state_t c ) |
fm_export (fm_state_t c) |
1568 |
{ |
{ |
1569 |
int rc = 0, id = 0; |
int rc = 0, id = 0; |
1570 |
gpgme_ctx_t ctx = c->ctx; |
gpgme_ctx_t ctx = c->ctx; |
1571 |
gpgme_error_t err; |
gpgme_error_t err; |
1572 |
gpgme_recipients_t rset = c->recp; |
gpgme_key_t *rset = c->recp; |
1573 |
|
file_data_t keydata = NULL; |
1574 |
const char *name, *s = NULL; |
const char *name, *s = NULL; |
1575 |
char *p = NULL, *dst = NULL; |
char *p = NULL, *patt = NULL; |
|
void *recp; |
|
1576 |
|
|
1577 |
if( !gpgme_recipients_count( rset ) ) { |
if (!rset || !rset[0]) { |
1578 |
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); |
1579 |
rc = WPTERR_GENERAL; |
rc = WPTERR_GENERAL; |
1580 |
goto leave; |
goto leave; |
1581 |
} |
} |
1582 |
|
|
1583 |
if( gpgme_recipients_count( rset ) == 1 ) { |
if (rset[1] == NULL) { /* count == 1*/ |
1584 |
err = gpgme_recipients_enum_open( rset, &recp ); |
gpgme_key_t k = rset[0]; |
1585 |
if( err ) |
const char *s = k->uids->name; |
1586 |
BUG( NULL ); |
p = new char[strlen (s)+1+8]; |
1587 |
s = gpgme_recipients_enum_read( rset, &recp ); |
if (!p) |
1588 |
gpgme_recipients_enum_close( rset, &recp ); |
BUG (NULL); |
1589 |
p = new char[strlen( s )+1+8]; |
strcpy (p, s ); |
1590 |
if( !p ) |
strcat (p, ".asc"); |
|
BUG( NULL ); |
|
|
strcpy( p, s ); |
|
|
strcat( p, ".asc" ); |
|
1591 |
} |
} |
1592 |
|
|
1593 |
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"), |
1594 |
|
NULL, p? p : NULL); |
1595 |
|
|
1596 |
if( !name ) |
if (!name) |
1597 |
name = "keys.gpg"; |
name = "keys.gpg"; |
1598 |
|
|
1599 |
dst = fm_quote_file( name ); |
patt = gpg_keylist_to_pattern (rset, c->n_recp); |
1600 |
err = gpgme_op_file_export( ctx, rset, dst ); |
|
1601 |
if( err ) { |
err = gpg_file_data_new (name, 0, &keydata); |
1602 |
msg_box( c->dlg, gpgme_strerror( err ), _("Export"), MB_ERR ); |
if (err) |
1603 |
rc = WPTERR_GENERAL; |
goto leave; |
1604 |
|
|
1605 |
|
err = gpgme_op_export (ctx, patt, 0, keydata->dat); |
1606 |
|
if (err) { |
1607 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR); |
1608 |
|
rc = WPTERR_GENERAL; |
1609 |
goto leave; |
goto leave; |
1610 |
} |
} |
1611 |
log_box( _("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name ); |
log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name); |
1612 |
|
|
1613 |
leave: |
leave: |
1614 |
free_if_alloc( dst ); |
if (keydata) |
1615 |
free_if_alloc( p ); |
gpg_file_data_release (keydata); |
1616 |
|
if (patt) |
1617 |
|
free (patt); |
1618 |
|
free_if_alloc (p); |
1619 |
|
|
1620 |
return rc; |
return rc; |
1621 |
} /* fm_export */ |
} |
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 && !memistr( p, strlen( 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 |
switch( *s ) { |
|
1665 |
case 'E': fm_decrypt( ctx, fn ); break; |
log_box ("debug", MB_OK, "type=%d", type); /*XXX: debug*/ |
1666 |
case 'P': fm_import( ctx, fn ); break; |
switch (type) { |
1667 |
case 'S': |
case PGP_MESSAGE: |
1668 |
file_verify_use_event( ); |
fm_decrypt (ctx, fn); |
1669 |
if( s[1] == 'I' ) { |
break; |
1670 |
if( strlen( s ) == 13 && s[7] == 'D' ) |
|
1671 |
detached = 1; |
case PGP_PUBKEY: |
1672 |
fm_verify( ctx, detached, fn ); |
case PGP_SECKEY: |
1673 |
} |
fm_import (ctx, fn); |
1674 |
file_verify_wait( ); |
break; |
1675 |
|
|
1676 |
|
case PGP_SIG: |
1677 |
|
case PGP_CLEARSIG: |
1678 |
|
file_verify_use_event (); |
1679 |
|
if (type == PGP_SIG) |
1680 |
|
detached = 1; |
1681 |
|
fm_verify (ctx, detached, fn); |
1682 |
|
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 ) |
1742 |
rc = -1; |
rc = -1; |
1743 |
goto leave; |
goto leave; |
1744 |
} |
} |
|
if( !overwrite_file( s ) ) { |
|
|
rc = -1; |
|
|
goto leave; |
|
|
} |
|
1745 |
|
|
1746 |
rc = pk_archiv_create( list, s ); |
rc = pk_archiv_create( list, s ); |
1747 |
if( rc ) |
if( rc ) |
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 |
} |
} |