/[winpt]/trunk/Src/wptFileManager.cpp
ViewVC logotype

Contents of /trunk/Src/wptFileManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 211 - (show annotations)
Sun May 7 12:36:48 2006 UTC (18 years, 9 months ago) by twoaday
File size: 48765 byte(s)


1 /* wptFileManager.cpp - File Manager routines
2 * Copyright (C) 2001-2006 Timo Schulz
3 * Copyright (C) 2005 g10 Code GmbH
4 *
5 * This file is part of WinPT.
6 *
7 * WinPT is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * WinPT is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with WinPT; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <windows.h>
28 #include <commdlg.h>
29 #include <io.h>
30 #include <stdio.h>
31
32 #include "resource.h"
33 #include "wptTypes.h"
34 #include "wptGPG.h"
35 #include "wptAgent.h"
36 #include "wptCommonCtl.h"
37 #include "wptContext.h"
38 #include "wptErrors.h"
39 #include "wptKeylist.h"
40 #include "wptFileManager.h"
41 #include "wptNLS.h"
42 #include "wptW32API.h"
43 #include "wptVersion.h"
44 #include "wptDlgs.h"
45 #include "wptZIP.h"
46 #include "wptUTF8.h"
47 #include "wptRegistry.h"
48 #include "wptImport.h"
49 #include "wptCrypto.h"
50 #include "wptKeyManager.h"
51 #include "openpgp.h"
52
53
54 void progress_cleanup (progress_filter_s *pfx);
55 BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg,
56 WPARAM wparam, LPARAM lparam);
57 char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n);
58 gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
59 int prev_was_bad, int fd);
60
61
62 /* Symbolic column IDs. */
63 enum {
64 FM_COL_STAT = 0,
65 FM_COL_NAME = 1,
66 FM_COL_OP = 2
67 };
68
69 static const char *mm_files[] = {".mov", ".avi", ".mpg", ".mpeg",
70 ".mp3", ".wav", ".mid", ".wma",
71 ".gif", ".jpg", ".png", ".jpeg", ".dib", 0};
72
73
74 /* Check if the drive given by @fname is a floppy disc.
75 Return value: -1 for success. */
76 static int
77 is_floppy_disc (const char *fname)
78 {
79 char drv[32] = {0};
80 int max = sizeof (drv)-1;
81 int i=0;
82
83 if (!strstr (fname, ":\\"))
84 return 0;
85
86 while (fname && *fname && *fname != '\\' && i < max)
87 drv[i++] = *fname++;
88 drv[i++] = '\\';
89 drv[i++] = '\0';
90 i = GetDriveType (drv);
91 if (i == DRIVE_REMOVABLE)
92 return -1;
93 return 0;
94 }
95
96
97 /* Ask the user to overwrite file @fname.
98 Return value: 0 for cancel. */
99 static int
100 overwrite_file (const char *fname)
101 {
102 int id;
103
104 if (file_exist_check (fname))
105 return -1;
106 id = log_box (_("File Manager"), MB_YESNO,
107 _("\"%s\" already exists.\n"
108 "Replace existing file?"), fname);
109 return id == IDNO ? 0 : -1;
110 }
111
112
113 /* Removes 'critical' attributes from the file @fname.
114 If @force is 1, the user is not asked for permission. */
115 void
116 remove_crit_file_attrs (const char *fname, int force)
117 {
118 DWORD fattr;
119 int id = 0;
120
121 if (file_exist_check (fname))
122 return; /* Does not exist */
123
124 fattr = GetFileAttributes (fname);
125 if ((fattr & FILE_ATTRIBUTE_READONLY) && force)
126 id = IDYES;
127 else if (fattr & FILE_ATTRIBUTE_READONLY)
128 id = log_box (_("File Manager"), MB_YESNO,
129 _("\"%s\" has read-only attribute.\n"
130 "Set attribute to normal?"), fname);
131 if (id == IDYES) {
132 if (!SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL))
133 msg_box (NULL, _("Could not reset file attribute to normal."),
134 _("File Manager"), MB_ERR);
135 }
136 }
137
138
139 /* Return 1 if the given path @fname is a directory, 0 otherwise. */
140 static int
141 is_directory (const char *fname)
142 {
143 return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0;
144 }
145
146
147 /* Return -1 if the given name @name is a valid PGP extension. */
148 static int
149 is_openpgp_ext (const char *name)
150 {
151 if (stristr (name, ".gpg") || stristr (name, ".asc")
152 || stristr (name, ".sig") || stristr (name, ".pgp"))
153 return -1;
154 return 0;
155 }
156
157
158 static int
159 is_multi_media (const char * name)
160 {
161 const char * val;
162 char * p;
163 int i;
164 int ans=0;
165
166 i = get_reg_winpt_single (CFG_NOZIP_MMEDIA);
167 if (i == -1)
168 {
169 ans = msg_box (NULL, _("Multi-Media files are already compressed, GPG would compress\n"
170 "them anyway and this costs a lot of time.\n"
171 "It is possible to disable compression for these files.\n"
172 "Do you want to disable it?"),
173 _("File Manager"), MB_YESNO|MB_INFO);
174 set_reg_winpt_single (CFG_NOZIP_MMEDIA, ans == IDYES? 1 : 0);
175 if (ans == IDNO)
176 return 0;
177 }
178 else if (i == 0)
179 return 0;
180
181 p = strrchr (name, '.');
182 if (!p)
183 return 0;
184 for (i=0; (val = mm_files[i]); i++) {
185 if (!stricmp (p, val))
186 return -1;
187 }
188 return 0;
189 }
190
191
192 /* Return a GPG file extension which depends on the operation
193 mode in @ctx and the sig mode @sigmode. */
194 const char*
195 file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode)
196 {
197 int use_armor = gpgme_get_armor (ctx);
198
199 if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR)
200 return ".asc";
201 if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH)
202 return ".sig";
203 return ".gpg";
204 }
205
206
207 /* Quote a file to avoid shell problems with spaces in the files. */
208 char*
209 fm_quote_file (const char * name)
210 {
211 char * p;
212 size_t len = strlen (name) + 8;
213
214 if (*name == '"')
215 return m_strdup (name); /* avoid double quotes */
216 p = new char[len + 1];
217 if (!p)
218 BUG (0);
219 _snprintf (p, len, "\"%s\"", name);
220
221 return p;
222 }
223
224
225
226 /* Check the armor type of the file @fname and return
227 a string representation of it. */
228 static const char*
229 fm_check_armor_type (const char *fname, int *r_type)
230 {
231 FILE *fp;
232 char header[768], *p;
233
234 if (r_type)
235 *r_type = PGP_NONE;
236 fp = fopen (fname, "rb");
237 if (!fp)
238 return "UNKNOWN";
239 p = fgets (header, sizeof (header) - 1, fp);
240 fclose (fp);
241 if (!p)
242 return "UNKNOWN";
243
244 if (strncmp (header, "-----", 5))
245 goto leave;
246 if (strstr( header, "BEGIN PGP PUBLIC KEY" )) {
247 if (r_type) *r_type = PGP_PUBKEY;
248 return "PUBKEY";
249 }
250 else if (strstr (header, "BEGIN PGP PRIVATE KEY") ||
251 strstr (header, "BEGIN PGP SECRET KEY")) {
252 if (r_type) *r_type = PGP_SECKEY;
253 return "SECKEY";
254 }
255 else if (strstr (header, "BEGIN PGP MESSAGE")) {
256 if (r_type) *r_type = PGP_MESSAGE;
257 return "ENCRYPTED";
258 }
259 else if (strstr( header, "BEGIN PGP SIGNED MESSAGE")) {
260 if (r_type) *r_type = PGP_CLEARSIG;
261 return "SIGNED-CLEAR";
262 }
263 else if (strstr(header, "BEGIN PGP SIGNATURE")) {
264 if (r_type) *r_type = PGP_SIG;
265 return "SIGNED-DETACH";
266 }
267
268 leave:
269 return "UNKNOWN";
270 }
271
272
273 /* Extract file type from @fname. If @r_type is valid,
274 it contains the PGP type on success. */
275 static const char*
276 fm_get_file_type (const char *fname, int *r_type)
277 {
278 gpg_iobuf_t inp;
279 armor_filter_context_t afx;
280 PACKET *pkt;
281 const char *s = NULL;
282 size_t count = 0, compr = 0;
283 int rc = 0;
284
285 if (r_type)
286 *r_type = PGP_NONE;
287 if (!fname) {
288 log_debug ("fm_get_file_type: !fname\r\n");
289 return NULL;
290 }
291
292 if (is_floppy_disc (fname))
293 return fm_check_armor_type (fname, r_type);
294
295 inp = gpg_iobuf_open (fname);
296 if (!inp) {
297 const char *err = winpt_strerror (WPTERR_FILE_OPEN);
298 log_box (_("File Manager"), MB_ERR, "\"%s\": %s", fname, err);
299 return NULL;
300 }
301 gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */
302 if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */
303 && !is_openpgp_ext (fname)) {
304 gpg_iobuf_close (inp);
305 return "UNKNOWN";
306 }
307
308 if (gpg_use_armor_filter(inp)) {
309 memset (&afx, 0, sizeof (afx));
310 gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx);
311 }
312 pkt = (PACKET *)calloc (1, sizeof *pkt);
313 if (!pkt)
314 BUG (NULL);
315 gpg_init_packet (pkt);
316 while (!(rc = gpg_parse_packet (inp, pkt))) {
317 switch (pkt->pkttype) {
318 case PKT_PUBKEY_ENC:
319 s = "ENCRYPTED";rc = -2;
320 if (r_type) *r_type = PGP_MESSAGE;
321 break;
322 case PKT_SYMKEY_ENC:
323 case PKT_ENCRYPTED:
324 s = "SYMKEYENC";rc = -2;
325 if (r_type) *r_type = PGP_MESSAGE;
326 break;
327 case PKT_SIGNATURE:
328 case PKT_ONEPASS_SIG:
329 s = "SIGNED"; rc = -2;
330 if (r_type) *r_type = PGP_SIG;
331 break;
332 case PKT_PUBLIC_KEY:
333 s = "PUBKEY"; rc = -2;
334 if (r_type) *r_type = PGP_PUBKEY;
335 break;
336 case PKT_SECRET_KEY:
337 s = "SECKEY"; rc = -2;
338 if (r_type) *r_type = PGP_SECKEY;
339 break;
340
341 case PKT_COMPRESSED:
342 /* If we only find 1 packet and it is compressed,
343 we assume a compress one-pass signature. */
344 if (count != 0)
345 break;
346 s = "SIGNED"; rc = -2;
347 compr = 1;
348 break;
349
350 default:
351 break;
352 }
353 count++;
354 gpg_free_packet (pkt);
355 gpg_init_packet (pkt);
356 if (rc == -2)
357 break; /* found */
358 }
359 safe_free (pkt);
360 gpg_iobuf_close (inp);
361 if (!s)
362 s = fm_check_armor_type (fname, r_type);
363 if (!s)
364 s = "UNKNOWN";
365 if (!strcmp (s, "SIGNED") && !compr
366 && strcmp (fm_check_armor_type (fname, r_type), "SIGNED-CLEAR ")) {
367 if (r_type) *r_type = PGP_SIG;
368 s = "SIGNED-DETACH";
369 }
370 return s;
371 }
372
373
374 /* Build the File Manager list view control. */
375 int
376 fm_build (listview_ctrl_t *lv, HWND ctrl)
377 {
378 int i;
379 listview_ctrl_t c;
380 struct listview_column_s col[] = {
381 {0, 80, (char *)_("Status") },
382 {1, 256, (char *)_("Name") },
383 {2, 128, (char *)_("Operation") },
384 {0, 0, NULL}
385 };
386
387 listview_new (&c, ctrl);
388 for (i = 0; col[i].width; i++)
389 listview_add_column (c, &col[i]);
390 listview_set_ext_style (c);
391 if (lv)
392 *lv = c;
393 return 0;
394 }
395
396
397 /* Release the File Manager listview control. */
398 void
399 fm_delete (listview_ctrl_t lv)
400 {
401 if (lv) {
402 listview_release(lv);
403 }
404 }
405
406
407 int
408 fm_state_new (fm_state_t * ctx)
409 {
410 fm_state_s *c;
411
412 c = new fm_state_s;
413 if (!c)
414 BUG (0);
415 memset (c, 0, sizeof *c);
416 if (gpgme_new (&c->ctx))
417 BUG (0);
418 *ctx = c;
419 return 0;
420 }
421
422
423 /* Release the FM state handle @c. */
424 void
425 fm_state_release (fm_state_t c)
426 {
427 if (!c)
428 return;
429 if (c->ctx) {
430 gpgme_release (c->ctx);
431 c->ctx = NULL;
432 }
433 safe_free (c->recp);
434 free_if_alloc (c->opaque);
435 free_if_alloc (c->output);
436 delete c;
437 }
438
439
440 static int
441 fm_check_for_entry( listview_ctrl_t lv, const char *file )
442 {
443 char name[512];
444 int i;
445
446 memset (name, 0, sizeof (name));
447 for (i = 0; i < listview_count_items( lv, 0 ); i++) {
448 listview_get_item_text( lv, i, 1, name, sizeof (name) - 1 );
449 if( !strcmp( name, file ) )
450 return 1; /* found */
451 }
452
453 return 0;
454 }
455
456
457 static int
458 fm_set_ftype (listview_ctrl_t lv, const char *name)
459 {
460 const char *type;
461 int rc;
462
463 rc = fm_check_for_entry (lv, name);
464 if (rc)
465 return 0;
466 type = fm_get_file_type (name, NULL);
467 if (!type || !strcmp (type, "UNKNOWN"))
468 type = gnupg_check_file_ext (name, NULL);
469 rc = listview_add_item (lv, " ");
470 if (rc)
471 return -1;
472 listview_add_sub_item (lv, 0, 0, type);
473 listview_add_sub_item (lv, 0, 1, name);
474 return 0;
475 }
476
477
478 /* Add all files from the directory @path to the list view @lv. */
479 static int
480 fm_add_dir_files (listview_ctrl_t lv, char *path)
481 {
482 struct _finddata_t fd;
483 char * p;
484 long hd;
485
486 strcat (path, "\\*");
487 hd = _findfirst (path, &fd);
488 do {
489 p = new char [(strlen (path) + strlen (fd.name))+1];
490 if (!p)
491 BUG (0);
492 memcpy (p, path, strlen (path)-1);
493 p[strlen (path)-1] = 0;
494 strcat (p, fd.name);
495 if (!is_directory (p))
496 fm_set_ftype (lv, p);
497 free_if_alloc (p);
498 } while (_findnext (hd, &fd) == 0);
499 _findclose (hd);
500 return 0;
501 }
502
503
504 /* Add the drag & drop files from @dd_files to the
505 list view control @lv. */
506 int
507 fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files)
508 {
509 char name[384+4];
510 int nfiles;
511 int rc = 0;
512 int i;
513
514 memset (name, 0, sizeof (name));
515 nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0);
516 for (i = 0; i < nfiles; i++) {
517 DragQueryFile (dd_files, i, name, sizeof (name) -1);
518 if (is_directory (name))
519 rc = fm_add_dir_files (lv, name);
520 else
521 rc = fm_set_ftype (lv, name);
522 if (rc == -1)
523 break; /* XXX: fixme? */
524 }
525 DragFinish (dd_files);
526 return rc;
527 }
528
529
530 /* Add a single file @name to the list view and before
531 figure out the type of it.
532 Return value: 0 on success. */
533 static int
534 add_single_file (listview_ctrl_t lv, const char *name)
535 {
536 const char *type;
537 int rc = 0;
538
539 type = fm_get_file_type (name, NULL);
540 if (!type)
541 return WPTERR_FILE_OPEN;
542 if (!strcmp (type, "UNKNOWN"))
543 type = gnupg_check_file_ext (name, NULL);
544 rc = listview_add_item (lv, "");
545 if (!rc) {
546 listview_add_sub_item (lv, 0, 0, type);
547 listview_add_sub_item (lv, 0, 1, name);
548 }
549 return rc;
550 }
551
552
553 /* Use the common Open-File-Dialog to allow the user to
554 add one ore more selected files to the listview @lv. */
555 int
556 fm_add_opened_files (listview_ctrl_t lv, HWND dlg)
557 {
558 OPENFILENAME open;
559 char file[512], name[MAX_PATH+1];
560 char *path = NULL;
561 const char *s;
562 int i, len=0, n=0;
563 int rc=0;
564
565 memset (&open, 0, sizeof (open));
566 open.lStructSize = sizeof (OPENFILENAME);
567 open.hInstance = glob_hinst;
568 open.lpstrTitle = _("File Open");
569 open.lpstrFilter = "All Files (*.*)\0*.*\0\0";
570 open.hwndOwner = dlg;
571 open.lpstrFile = file;
572 open.nMaxFile = sizeof (file) - 1;
573 open.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER ;
574
575 memset (file, 0, sizeof file);
576 if (!GetOpenFileName (&open))
577 return 0;
578
579 s = file;
580 len = sizeof (file)-1;
581 for (;;) {
582 if (len < 2 || (*s == '\0' && *(s+1) == '\0'))
583 break;
584 memset (name, 0, sizeof (name));
585 for (i=0; len > 0; len--, i++) {
586 if (*s == '\0') {
587 name[i] = *s++;
588 break;
589 }
590 name[i] = *s++;
591 }
592 if (n == 0)
593 path = strdup (name);
594 else {
595 char *p = make_filename (path, name, NULL);
596 rc = add_single_file (lv, p);
597 safe_free (p);
598 }
599 n++;
600 }
601 if (n == 1) /* single file selected. */
602 rc = add_single_file (lv, path);
603 safe_free (path);
604 return rc;
605 }
606
607
608 int
609 fm_assume_onepass_sig (const char * fname)
610 {
611 armor_filter_context_t afx;
612 gpg_iobuf_t fp;
613 gpgme_data_t dat;
614 PACKET *pkt;
615 char tmpfile[MAX_PATH+1];
616 int check = 0;
617
618 pkt = (PACKET *)calloc (1, sizeof *pkt);
619 if (!fname) {
620 get_temp_name (tmpfile, sizeof (tmpfile)-1, "gpgme.tmp");
621 gpg_data_new_from_clipboard (&dat, 0);
622 gpg_data_release_and_set_file (dat, tmpfile);
623
624 fp = gpg_iobuf_open (tmpfile);
625 if (!fp)
626 return 0;
627 gpg_iobuf_ioctl (fp, 3, 1, NULL);
628 if (gpg_use_armor_filter(fp)) {
629 memset (&afx, 0, sizeof (afx));
630 gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx);
631 }
632 gpg_init_packet (pkt);
633 if (!gpg_parse_packet (fp, pkt)
634 && pkt->pkttype == PKT_COMPRESSED)
635 check = 1;
636 gpg_free_packet (pkt);
637 gpg_iobuf_close (fp);
638 remove (tmpfile);
639 }
640 /* XXX: implement it for real files */
641 safe_free (pkt);
642 return check;
643 }
644
645
646 int
647 fm_get_current_pos (listview_ctrl_t lv)
648 {
649 int i = 0, items;
650
651 items = listview_count_items (lv, 0);
652 if (!items)
653 return -1;
654 else if (items == 1) {
655 listview_select_one (lv, 0);
656 return 0;
657 }
658 else if (items > 1) {
659 i = listview_get_curr_pos (lv);
660 if (i == -1) {
661 msg_box (lv->ctrl, _("Please select a file."),
662 _("File Manager"), MB_ERR);
663 return -1;
664 }
665 return i;
666 }
667
668 return -1;
669 }
670
671
672 static int
673 fm_check_detached_sig (listview_ctrl_t lv, int pos)
674 {
675 char type[128];
676
677 listview_get_item_text (lv, pos, 0, type, sizeof (type)-1);
678 return !strcmp (type, "SIGNED-DETACH")? 1 : 0;
679 }
680
681
682 int
683 fm_check_file_type (listview_ctrl_t lv, int pos, int fm_cmd)
684 {
685 char status[128];
686 int rc = 0;
687
688 listview_get_item_text (lv, pos, 0, status, sizeof (status) - 1);
689
690 switch (fm_cmd) {
691 case FM_ENCRYPT:
692 case FM_ENCRYPT_DIR:
693 case FM_SIGNENCRYPT:
694 if (strcmp (status, "ENCRYPTED")
695 && strcmp (status, "SYMKEYENC"))
696 rc = 1;
697 break;
698
699 case FM_DECRYPT:
700 if (!strcmp (status, "DATA")
701 || !strcmp (status, "ENCRYPTED")
702 || !strcmp (status, "SYMKEYENC")
703 || !strcmp (status, "ARMORED"))
704 rc = 1;
705 break;
706
707 case FM_SIGN:
708 if( strncmp( status, "SIGNED", 6 ) )
709 rc = 1;
710 break;
711
712 case FM_VERIFY:
713 if( !strncmp( status, "SIGNED", 6 )
714 || !strcmp( status, "COMPRESSED" ) )
715 rc = 1;
716 break;
717
718 case FM_SYMENC:
719 if( strcmp( status, "SYMKEYENC" ) )
720 rc = 1;
721 break;
722
723 case FM_IMPORT:
724 if( !strcmp( status, "PUBKEY" )
725 || !strcmp( status, "SECKEY" ) )
726 rc = 1;
727 break;
728
729 case FM_WIPE:
730 case FM_LIST:
731 rc = 1;
732 break;
733 }
734
735 return rc;
736 }
737
738
739 /* Set the file status of the given command @fm_cmd.
740 @success is 0 on success. */
741 static void
742 fm_set_status (listview_ctrl_t lv, int pos, int fm_cmd,
743 gpgme_sig_mode_t sigmode, int success, const char *output)
744 {
745 char status[128], operat[128];
746 int update = 1;
747 const char *s;
748
749 if (fm_cmd == FM_LIST)
750 return;
751 success ? s = "SUCCESS" : s = "FAILED";
752 strcpy (operat, s);
753
754 switch (fm_cmd) {
755 case FM_ENCRYPT:
756 case FM_ENCRYPT_DIR:
757 case FM_SIGNENCRYPT: strcpy (status, "ENCRYPTED"); break;
758 case FM_DECRYPT: strcpy (status, "UNKNOWN"); break;
759 case FM_SIGN:
760 if (sigmode == GPGME_SIG_MODE_DETACH)
761 strcpy (status, "SIGNED-DETACH");
762 else if (sigmode == GPGME_SIG_MODE_CLEAR)
763 strcpy (status, "SIGNED-CLEAR");
764 else
765 strcpy (status, "SIGNED");
766 break;
767 case FM_VERIFY: update = 0; break;
768 case FM_SYMENC: strcpy (status, "SYMKEYENC"); break;
769 case FM_IMPORT: update = 0; break;
770 case FM_WIPE: strcpy (status, "WIPED"); break;
771 default: strcpy (status, "UNKNOWN"); break;
772 }
773
774 if (success && update) {
775 listview_add_sub_item (lv, pos, 0, status);
776 listview_add_sub_item (lv, pos, 1, output);
777 }
778 listview_add_sub_item( lv, pos, 2, operat );
779 }
780
781
782 int
783 fm_clearsign_8bit (listview_ctrl_t lv, fm_state_s *ctx)
784 {
785 FILE *f;
786 byte buf[32];
787 char name[256];
788 int i, n, cnt=0;
789
790 if (ctx->sigmode != GPGME_SIG_MODE_CLEAR)
791 return 0;
792 listview_get_item_text (lv, -1, 1, name, sizeof (name)-1);
793 if (stristr (name, ".TXT"))
794 return 0;
795 f = fopen (name, "rb");
796 if (!f)
797 return -1; /* should never happen */
798 n = fread (buf, 1, 32, f);
799 for (i = 0; i < n; i++) {
800 if (buf[i] == 0x00 || buf[i] > 170)
801 cnt++;
802 }
803 fclose (f);
804 if (!cnt)
805 return 0;
806 n = -1;
807 i = log_box (_("File Manager"), MB_WARN|MB_YESNO,
808 _("\"%s\" does not seems to be a text file.\n"
809 "Do you really want to clearsign it?"), name);
810 if (i == IDYES)
811 n = 0;
812 return n;
813 }
814
815
816 int
817 fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd)
818 {
819 struct secdel_confirm_s confirm = {0};
820 struct progress_filter_s pfx, pfx2;
821 fm_state_s * ctx;
822 int fm_cmd, sig_detached = 0;
823 int rc = 0, i, n, ndel = 0;
824 char fname[512], status[128];
825
826 switch (cmd) {
827 case ID_FILEMISC_ENCRYPT: fm_cmd = FM_ENCRYPT; break;
828 case ID_FILEMISC_ENCRYPT_ZIP:fm_cmd = FM_ENCRYPT_ZIP; break;
829 case ID_FILEMISC_DECRYPT: fm_cmd = FM_DECRYPT; break;
830 case ID_FILEMISC_SYMENC: fm_cmd = FM_SYMENC; break;
831 case ID_FILEMISC_SIGN: fm_cmd = FM_SIGN; break;
832 case ID_FILEMISC_VERIFY: fm_cmd = FM_VERIFY; break;
833 case ID_FILEMISC_IMPORT: fm_cmd = FM_IMPORT; break;
834 case ID_FILEMISC_WIPE: fm_cmd = FM_WIPE; break;
835 case ID_FILEMISC_LIST: fm_cmd = FM_LIST; break;
836 case ID_FILEMISC_SIGNENC: fm_cmd = FM_SIGNENCRYPT; break;
837 default: return 1; /* unknown command */
838 }
839
840 if (fm_get_current_pos (lv) == -1)
841 return WPTERR_GENERAL;
842 rc = fm_state_new (&ctx);
843 if (rc)
844 BUG (0);
845 ctx->dlg = dlg;
846
847 // XXX: for file operations the progress dialog will be
848 // reloaded somewhere and thus a 'dummy' dialog remains
849
850 /* we use it here to make sure that pfx_cleanup will not use
851 any weird values. */
852 memset (&pfx, 0, sizeof (pfx));
853 ctx->prog_cb = NULL;
854 if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) {
855 pfx.hwnd = dlg;
856 /*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/
857 /*ctx->prog_cb = &pfx;*/
858 }
859
860 /* Commands we need before we can perform the main command */
861 switch (fm_cmd) {
862 case FM_ENCRYPT_ZIP:
863 case FM_ENCRYPT:
864 case FM_SIGNENCRYPT:
865 if (fm_cmd == FM_SIGNENCRYPT)
866 ctx->req_signer = 1;
867 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_ENCRYPT,
868 ctx->dlg, file_encrypt_dlg_proc, (LPARAM)ctx);
869 if (ctx->cancel == 1) {
870 rc = WPTERR_GENERAL;
871 goto leave;
872 }
873 break;
874
875 case FM_SIGN:
876 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FILE_SIGN, dlg,
877 file_sign_dlg_proc, (LPARAM) ctx);
878 if (ctx->cancel == 1 || fm_clearsign_8bit (lv, ctx)) {
879 rc = WPTERR_GENERAL;
880 goto leave;
881 }
882 break;
883
884 case FM_WIPE:
885 memset (&pfx2, 0, sizeof (pfx2));
886 secure_unlink_set_cb (progress_callback, &pfx2);
887 break;
888 }
889
890 for( i = 0, n = 0; i < listview_count_items( lv, 0 ); i++ ) {
891 if( !listview_get_item_state( lv, i ) )
892 continue;
893 listview_get_item_text( lv, i, 0, status, sizeof (status) -1 );
894 if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT)
895 n++;
896 if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN)
897 n++;
898 if (fm_cmd == FM_WIPE)
899 ndel++;
900 }
901
902 if (n > 1 && fm_cmd != FM_SYMENC)
903 ctx->cache_cb = 1;
904
905 if (fm_cmd == FM_WIPE && ndel > 0) {
906 memset (&confirm, 0, sizeof confirm);
907 confirm.lv_files = lv;
908 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg,
909 file_secdel_confirm_dlg_proc, (LPARAM)&confirm);
910 if (!confirm.yes)
911 goto leave;
912 }
913
914 if (fm_cmd == FM_ENCRYPT_ZIP)
915 fm_encrypt_into_zip (ctx, lv);
916
917 for (i = 0; i < listview_count_items (lv, 0); i++) {
918 if (!listview_get_item_state (lv, i))
919 continue;
920 listview_get_item_text (lv, i, 1, fname, sizeof (fname) - 1);
921 if( file_exist_check (fname) && !is_directory (fname)) {
922 log_box (_("File Manager"), MB_ERR,
923 _("\"%s\" does not exist"), fname);
924 continue;
925 }
926 if (is_directory (fname))
927 fm_cmd = FM_ENCRYPT_DIR;
928 if (!fm_check_file_type (lv, i, fm_cmd))
929 continue;
930 sig_detached = fm_check_detached_sig (lv, i);
931 switch (fm_cmd) {
932 case FM_LIST: rc = fm_list( fname, dlg ); break;
933 case FM_WIPE: rc = fm_wipe( fname ); break;
934 case FM_ENCRYPT: rc = fm_encrypt( ctx, fname, 0 ); break;
935 case FM_ENCRYPT_DIR: rc = fm_encrypt_directory( ctx, fname ); break;
936 case FM_SIGNENCRYPT: rc = fm_encrypt( ctx, fname, 1 ); break;
937 case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break;
938 case FM_SIGN: rc = fm_sign( ctx, fname ); break;
939 case FM_SYMENC: rc = fm_sym_encrypt (ctx, fname); break;
940 case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break;
941 case FM_IMPORT:
942 free_if_alloc (ctx->opaque);
943 ctx->opaque = m_strdup (fname);
944 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,
945 file_import_dlg_proc, (LPARAM)ctx);
946 if (ctx->cancel == 1)
947 continue;
948 rc = fm_import (ctx, fname);
949 break;
950 }
951 if (ctx->cancel == 1) {
952 ctx->cancel = 0;
953 continue;
954 }
955 fm_set_status (lv, i, fm_cmd, ctx->sigmode, !rc, ctx->output);
956 free_if_alloc (ctx->output);
957 progress_cleanup (&pfx);
958 }
959
960 if (fm_cmd == FM_WIPE) {
961 secure_unlink_set_cb (NULL, NULL);
962 progress_cleanup (&pfx2);
963 }
964 if (ctx->cache_cb) {
965 release_gpg_passphrase_cb (&ctx->pass_cb);
966 ctx->cache_cb = 0; /* make sure it's only used for this session! */
967 }
968
969 /* remove wipe files from the list */
970 n = listview_count_items (lv, 0);
971 while (n--) {
972 listview_get_item_text (lv, n, 0, status, sizeof (status) - 1);
973 if (!strcmp (status, "WIPED"))
974 listview_del_item (lv, n);
975 }
976
977 leave:
978 if (!rc)
979 fm_state_release (ctx);
980 progress_cleanup (&pfx);
981 return rc;
982 } /* fm_parse_files */
983
984
985 /* Wipe the given file @name with the delete mode
986 from the configuration.
987 Return value: 0 on success. */
988 int
989 fm_wipe (const char *name)
990 {
991 int rc;
992
993 SetCursor (LoadCursor (NULL, IDC_WAIT));
994 remove_crit_file_attrs (name, 1);
995 rc = secure_unlink (name, reg_prefs.wipe_mode);
996 SetCursor (LoadCursor (NULL, IDC_ARROW));
997 return rc;
998 }
999
1000
1001 /* Dump out the given PGP packets from file @name in a dialog. */
1002 int
1003 fm_list (const char *name, HWND dlg)
1004 {
1005 dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg,
1006 file_stat_dlg_proc, (LPARAM)name, _("File Status"),
1007 IDS_WINPT_FILE_STAT );
1008 return 0;
1009 }
1010
1011
1012 static int
1013 ask_filename (fm_state_t c, const char *msg, char **dst)
1014 {
1015 const char * s;
1016
1017 s = get_filesave_dlg (c->dlg, msg, NULL, NULL);
1018 if (!s)
1019 return WPTERR_GENERAL;
1020
1021 if (dst != NULL)
1022 free_if_alloc (*dst);
1023 free_if_alloc (c->output);
1024 c->output = m_strdup (s);
1025 if (dst)
1026 *dst = fm_quote_file (s);
1027 return 0;
1028 }
1029
1030
1031 int
1032 fm_encrypt (fm_state_t c, const char *name, int sign)
1033 {
1034 gpgme_error_t err;
1035 gpgme_key_t key = NULL;
1036 gpgme_ctx_t ctx = c->ctx;
1037 file_data_t in=NULL, out=NULL;
1038 char *keyid = NULL, ext[5];
1039 /*int no_compr = 0;*/
1040 int rc = 0;
1041
1042 c->output = new char[strlen (name) + 5 + 1];
1043 if (!c->output)
1044 BUG (0);
1045 strcpy (ext, file_get_extension (ctx, c->sigmode));
1046 strcpy (c->output, name);
1047 strcat (c->output, ext);
1048
1049 if (!overwrite_file (c->output)) {
1050 rc = ask_filename (c, _("Enter filename for encrypted file"), NULL);
1051 if (rc)
1052 goto leave;
1053 }
1054
1055 err = gpg_file_data_new (name, F_DATA_READ, &in);
1056 if (err)
1057 goto leave;
1058 remove_crit_file_attrs (c->output, 0);
1059 err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1060 if (err)
1061 goto leave;
1062
1063 /*
1064 if (c->prog_cb) {
1065 c->prog_cb->what = name;
1066 gpg_file_data_set_cb (in, c->prog_cb);
1067 }
1068 */
1069
1070 /* XXX: disable compression for multi-media files.
1071 no_compr = is_multi_media (name);
1072 gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr);
1073 */
1074
1075 if (sign) {
1076 if (gpgme_signers_enum (ctx, 0) == NULL) {
1077 keyid = get_gnupg_default_key ();
1078 if (!keyid) {
1079 msg_box (c->dlg, _("Could not get default secret key."),
1080 _("Signing"), MB_ERR);
1081 rc = WPTERR_GENERAL;
1082 goto leave;
1083 }
1084 if (get_seckey (keyid, &key))
1085 BUG (0);
1086 gpgme_signers_add (ctx, key);
1087 }
1088 else {
1089 gpgme_key_t sigkey = gpgme_signers_enum (ctx, 0);
1090 if (sigkey && sigkey->subkeys) {
1091 keyid = m_strdup (sigkey->subkeys->keyid);
1092 }
1093 }
1094 if (!c->init_cb || !c->cache_cb) {
1095 set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN,
1096 c->dlg, _("Signing"));
1097 c->init_cb = 1;
1098 }
1099 op_begin ();
1100 err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
1101 in->dat, out->dat);
1102 op_end ();
1103 if (!c->cache_cb)
1104 release_gpg_passphrase_cb (&c->pass_cb);
1105 if (c->pass_cb.cancel) {
1106 rc = WPTERR_GENERAL;
1107 goto leave;
1108 }
1109 if (err) {
1110 msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
1111 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
1112 agent_del_cache (keyid);
1113 rc = WPTERR_GENERAL;
1114 goto leave;
1115 }
1116 }
1117 else {
1118 op_begin ();
1119 err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
1120 in->dat, out->dat);
1121 op_end ();
1122 if (err) {
1123 msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);
1124 rc = WPTERR_GENERAL;
1125 goto leave;
1126 }
1127 }
1128
1129 leave:
1130 if (in)
1131 gpg_file_data_release (in);
1132 if (out)
1133 gpg_file_data_release (out);
1134 free_if_alloc (keyid);
1135 if (!rc && c->wipe)
1136 secure_unlink (name, WIPE_MODE_SIMPLE);
1137 return rc;
1138 }
1139
1140
1141 int
1142 fm_sym_encrypt (fm_state_t c, const char * name)
1143 {
1144 gpgme_ctx_t ctx = c->ctx;
1145 gpgme_error_t err;
1146 file_data_t in=NULL, out=NULL;
1147 int rc = 0, cancel = 0;
1148 char ext[5], * pass;
1149
1150 pass = request_passphrase2 (_("Symmetric"), 0, &cancel);
1151 if (cancel) {
1152 c->cancel = 1;
1153 return 0;
1154 }
1155
1156 /* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/
1157 c->output = new char[strlen (name) + 5 + 1];
1158 if (!c->output)
1159 BUG (0);
1160 strcpy (ext, file_get_extension (ctx, c->sigmode));
1161 strcpy (c->output, name);
1162 strcat (c->output, ext);
1163
1164 if (overwrite_file (c->output) == 0) {
1165 rc = WPTERR_GENERAL;
1166 goto leave;
1167 }
1168
1169 gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);
1170
1171 err = gpg_file_data_new (name, 1, &in);
1172 if (err)
1173 goto leave;
1174 err = gpg_file_data_new (c->output, 0, &out);
1175 if (err)
1176 goto leave;
1177
1178 op_begin ();
1179 err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST,
1180 in->dat, out->dat);
1181 op_end ();
1182 if (err) {
1183 msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR);
1184 rc = WPTERR_GENERAL;
1185 goto leave;
1186 }
1187 if (file_exist_check (c->output)) {
1188 msg_box (c->dlg, _("Encryption failed."), _("Symmetric"), MB_ERR);
1189 rc = WPTERR_GENERAL;
1190 }
1191
1192 leave:
1193 if (in)
1194 gpg_file_data_release (in);
1195 if (out)
1196 gpg_file_data_release (out);
1197 sfree_if_alloc (pass);
1198 return rc;
1199 }
1200
1201
1202 char* get_pka_status (gpgme_signature_t sig);
1203
1204
1205 /* Show the human readable verify result from @sigres. */
1206 static void
1207 show_verify_result (gpgme_verify_result_t sigres)
1208 {
1209 gpgme_signature_t sig=sigres->signatures;
1210 winpt_key_s key;
1211 const char *s, *keyid, *uid;
1212 char *pka_info;
1213 char buf[384];
1214 int sigok = 0;
1215
1216 sig = sigres->signatures;
1217 sigok = sig->summary & GPGME_SIGSUM_GREEN;
1218 s = sigok? _("Good signature") : _("BAD signature");
1219 keyid = sig->fpr;
1220 if (!keyid)
1221 return;
1222 pka_info = get_pka_status (sig);
1223 keyid = get_keyid_from_fpr (sig->fpr);
1224 memset (&key, 0, sizeof (key));
1225 if (!winpt_get_pubkey (sig->fpr, &key))
1226 uid = key.ext->uids->uid;
1227 else
1228 uid = _("user ID not found");
1229 _snprintf (buf, sizeof (buf)-1, _("Signature made %s using %s key ID %s\n"
1230 "%s from \"%s\"\n%s"),
1231 strtimestamp (sig->timestamp),
1232 get_key_pubalgo (sig->pubkey_algo),
1233 keyid, s, uid, pka_info? pka_info : "");
1234 msg_box (NULL, buf, _("Decrypt Verify"), sigok? MB_OK: MB_ICONWARNING|MB_OK);
1235 free_if_alloc (pka_info);
1236 }
1237
1238
1239 /* Check the recipients if we have at least one secret key. */
1240 bool
1241 secret_key_available (gpgme_recipient_t rset)
1242 {
1243 gpgme_recipient_t r;
1244 gpgme_key_t key;
1245
1246 for (r=rset; r; r = r->next) {
1247 if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY)
1248 continue;
1249 else {
1250 /* extra check to make sure the key is available right now. */
1251 if (!get_seckey (r->keyid, &key))
1252 return true;
1253 }
1254 }
1255 return false;
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 *file_name)
1263 {
1264 char *dir;
1265 char *orig;
1266 int rc = 0;
1267
1268 dir = strrchr (output, '\\');
1269 if (!dir)
1270 orig = strdup (file_name);
1271 else {
1272 orig = (char*)calloc (1, strlen (file_name)+ 1 +
1273 strlen (output)+1);
1274 if (!orig)
1275 BUG (0);
1276 memcpy (orig, output, (dir-output)+1);
1277 strcat (orig, file_name);
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. */
1290 int
1291 fm_decrypt (fm_state_t c, const char *name)
1292 {
1293 gpgme_error_t err;
1294 gpgme_ctx_t ctx = c->ctx;
1295 gpgme_decrypt_result_t res;
1296 gpgme_verify_result_t sigres;
1297 file_data_t in = NULL, out = NULL;
1298 int rc = 0;
1299
1300 if (!c->init_cb || !c->cache_cb) {
1301 set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT,
1302 c->dlg, _("Decryption"));
1303 c->init_cb = 1;
1304 }
1305
1306 c->output = m_strdup (name);
1307 if (is_openpgp_ext (c->output))
1308 c->output[strlen (c->output)-4] = '\0';
1309 else {
1310 const char *s;
1311 s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"),
1312 NULL, NULL);
1313 if (s) {
1314 free_if_alloc (c->output);
1315 c->output = m_strdup (s);
1316 }
1317 }
1318
1319 if (overwrite_file (c->output) == 0) {
1320 rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL);
1321 if (rc)
1322 goto leave;
1323 }
1324
1325 err = gpg_file_data_new (name, F_DATA_READ, &in);
1326 if (err)
1327 goto leave;
1328 remove_crit_file_attrs (c->output, 0);
1329 err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1330 if (err)
1331 goto leave;
1332
1333 op_begin ();
1334 err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat);
1335 op_end ();
1336 if (!c->cache_cb)
1337 release_gpg_passphrase_cb (&c->pass_cb);
1338 if (c->pass_cb.cancel) {
1339 rc = WPTERR_GENERAL;
1340 goto leave;
1341 }
1342
1343 res = gpgme_op_decrypt_result (ctx);
1344 if (res && res->recipients && !secret_key_available (res->recipients)) {
1345 const char *keyid = res->recipients->keyid;
1346 char *p = get_key_userid (keyid+8);
1347 gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo;
1348
1349 log_box (_("Decryption"), MB_ERR,
1350 _("Encrypted with %s key, ID %s.%s\n"
1351 "Decryption failed: secret key not available."),
1352 get_key_pubalgo (pkalgo), keyid+8, p);
1353 rc = WPTERR_GENERAL;
1354 free_if_alloc (p);
1355 goto leave;
1356 }
1357 else if (err) {
1358 msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR);
1359 rc = WPTERR_GENERAL;
1360 goto leave;
1361 }
1362 if (file_exist_check (c->output)) {
1363 log_box ("Decrypt", MB_ERR,
1364 _("Decryption failed.\n%s: does not exist."), c->output);
1365 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);
1382 if (sigres && sigres->signatures)
1383 show_verify_result (sigres);
1384
1385 leave:
1386 if (in)
1387 gpg_file_data_release (in);
1388 if (out)
1389 gpg_file_data_release (out);
1390
1391 return rc;
1392 }
1393
1394
1395 int
1396 fm_sign (fm_state_t c, const char * name)
1397 {
1398 gpgme_ctx_t ctx = c->ctx;
1399 gpgme_error_t err;
1400 file_data_t in=NULL, out=NULL;
1401 char ext[5];
1402 int rc = 0;
1403
1404 if (!c->init_cb || !c->cache_cb) {
1405 set_gpg_passphrase_cb (&c->pass_cb, c->ctx,
1406 GPG_CMD_SIGN, c->dlg, _("Signing"));
1407 c->init_cb = 1;
1408 }
1409
1410 free_if_alloc (c->output);
1411 c->output = new char[strlen (name) + 5 + 1];
1412 if (!c->output)
1413 BUG (NULL);
1414 strcpy (ext, file_get_extension (ctx, c->sigmode));
1415 strcpy (c->output, name);
1416 strcat (c->output, ext);
1417
1418 if (!overwrite_file (c->output)) {
1419 rc = ask_filename (c, _("Enter filename for signed file"), NULL);
1420 if (rc)
1421 goto leave;
1422 }
1423
1424 err = gpg_file_data_new (name, F_DATA_READ, &in);
1425 if (err)
1426 goto leave;
1427 remove_crit_file_attrs (c->output, 0);
1428 err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1429 if (err)
1430 goto leave;
1431
1432 op_begin ();
1433 err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode);
1434 op_end ();
1435 if (!c->cache_cb)
1436 release_gpg_passphrase_cb (&c->pass_cb);
1437 if (c->pass_cb.cancel) {
1438 rc = WPTERR_GENERAL;
1439 goto leave;
1440 }
1441 if (err) {
1442 msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
1443 rc = WPTERR_GENERAL;
1444 goto leave;
1445 }
1446
1447 leave:
1448 if (in)
1449 gpg_file_data_release (in);
1450 if (out)
1451 gpg_file_data_release (out);
1452 return rc;
1453 }
1454
1455
1456 static void
1457 fm_add_sig_stat (file_sig_ctx_t log)
1458 {
1459 struct winpt_key_s key;
1460 const char *kid;
1461
1462 memset (&key, 0, sizeof (key));
1463 kid = get_keyid_from_fpr (log->sig->fpr);
1464 log->use_uid = 0;
1465 if (!winpt_get_pubkey (kid, &key)) {
1466 log->user_id = key.ext->uids->uid;
1467 log->use_uid = 1;
1468 }
1469 file_verify_add_state (log);
1470 }
1471
1472
1473 /* Verify a detached signature from the clipboard. */
1474 static int
1475 verify_pasted (listview_ctrl_t lv, fm_state_t ctx,
1476 const char *dat, int pos, HWND dlg)
1477 {
1478 FILE *fp;
1479 char stat[32];
1480 char file[256], *fname = NULL;
1481 int del_end = 0;
1482
1483 listview_get_item_text (lv, pos, 0, stat, sizeof (stat)-1);
1484 listview_get_item_text (lv, pos, 1, file, sizeof (file)-1);
1485 if (strcmp (stat, "UNKNOWN"))
1486 return 0;
1487 fname = make_filename (NULL, file, "asc");
1488 if (file_exist_check (fname) != 0) {
1489 fp = fopen (fname, "wb");
1490 if (fp == NULL) {
1491 log_box (_("File Manager"), MB_ERR, "could not create '%s'", fname);
1492 free_if_alloc (fname);
1493 return WPTERR_GENERAL;
1494 }
1495 fwrite (dat, 1, strlen (dat), fp);
1496 fclose (fp);
1497 del_end = 1;
1498 }
1499 fm_verify (ctx, 1, fname);
1500 if (del_end)
1501 remove (fname);
1502 free_if_alloc (fname);
1503 return 0;
1504 }
1505
1506
1507 /* Figure out if the clipboard contains a detached signature. */
1508 int
1509 fm_verify_pasted_detsig (listview_ctrl_t lv, HWND dlg)
1510 {
1511 fm_state_t ctx = NULL;
1512 char * dat=NULL;
1513 int i, fnd = 0;
1514
1515 dat = get_clip_text (NULL);
1516 if (!dat || !strstr (dat, "BEGIN PGP SIGNATURE")) {
1517 msg_box (dlg, _("Could not find detached signature in the clipboard."),
1518 _("File Manager"), MB_ERR);
1519 free_if_alloc (dat);
1520 return WPTERR_GENERAL;
1521 }
1522 /* XXX find a way to filter out bad signatures or just ignore all in
1523 this case */
1524 fm_state_new (&ctx);
1525 i = listview_get_curr_pos (lv);
1526 if (i= -1) {
1527 verify_pasted (lv, ctx, dat, i, dlg);
1528 fnd = 1;
1529 }
1530 else {
1531 for (i=0; i < listview_count_items (lv, 0); i++) {
1532 verify_pasted (lv, ctx, dat, i, dlg);
1533 fnd = 1;
1534 }
1535 }
1536 if (!fnd)
1537 msg_box (dlg, _("No files to check."), _("File Manager"), MB_INFO);
1538 free_if_alloc (dat);
1539 fm_state_release (ctx);
1540 return 0;
1541 }
1542
1543
1544 /* Extract automatically the output file name from @name.
1545 If @detached is 1, a detached sig is assumed. */
1546 static int
1547 get_output_file (fm_state_t c, const char *name, int detached)
1548 {
1549 const char *file = NULL;
1550 const char *title;
1551 char fname[384];
1552
1553 if (detached)
1554 title = _("Select Data File");
1555 else
1556 title = _("Selected Output File");
1557
1558 if (stristr (name, ".sig") ||
1559 stristr (name, ".asc") ||
1560 stristr (name, ".gpg")) {
1561 _snprintf (fname, sizeof (fname) - 1, "%s", name);
1562 fname[strlen (fname) - 4] = '\0';
1563 if (file_exist_check (fname) == 0 && detached)
1564 file = fname;
1565 else if (!detached) {
1566 /* If the signature is clear or normal, make sure we do not
1567 overwrite the original file if it exists. */
1568 if (file_exist_check (fname) == 0 && !overwrite_file (fname)) {
1569 file = get_filesave_dlg (c->dlg, title, NULL, NULL);
1570 if (!file)
1571 return WPTERR_GENERAL;
1572 }
1573 else
1574 file = fname;
1575 }
1576 }
1577 if (!file)
1578 file = get_fileopen_dlg (c->dlg, title, NULL, NULL);
1579 if (file) {
1580 free_if_alloc (c->output);
1581 c->output = m_strdup (file);
1582 }
1583 else {
1584 msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);
1585 return WPTERR_GENERAL;
1586 }
1587 if (detached)
1588 c->sigmode = GPGME_SIG_MODE_DETACH;
1589 else {
1590 if (stristr (name, ".asc"))
1591 c->sigmode = GPGME_SIG_MODE_CLEAR;
1592 else
1593 c->sigmode = GPGME_SIG_MODE_NORMAL;
1594 }
1595 return 0;
1596 }
1597
1598
1599 /* Verify the signature from the file @name. If @detached 1,
1600 it is assumed that a detached signature should be checked. */
1601 int
1602 fm_verify (fm_state_t c, int detached, const char *name)
1603 {
1604 gpgme_ctx_t ctx = c->ctx;
1605 gpgme_error_t err;
1606 gpgme_signature_t s;
1607 gpgme_verify_result_t res;
1608 struct file_sig_ctx_s log;
1609 file_data_t in=NULL, out=NULL;
1610 int rc = 0;
1611
1612 if (stristr (name, ".sig"))
1613 detached = 1;
1614
1615 if (get_output_file (c, name, detached))
1616 return WPTERR_GENERAL;
1617
1618 memset (&log, 0, sizeof (log));
1619 log.file = m_strdup (name);
1620 file_verify_create_dlg ();
1621
1622 err = gpg_file_data_new (name, F_DATA_READ, &in);
1623 if (err)
1624 goto leave;
1625 err = gpg_file_data_new (c->output,
1626 detached? F_DATA_READ : F_DATA_WRITE, &out);
1627 if (err)
1628 goto leave;
1629
1630 op_begin ();
1631 if (c->sigmode == GPGME_SIG_MODE_DETACH)
1632 err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);
1633 else
1634 err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat);
1635 op_end ();
1636 if (err) {
1637 msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
1638 rc = WPTERR_GENERAL;
1639 goto leave;
1640 }
1641
1642 res = gpgme_op_verify_result (ctx);
1643 for (s=res->signatures; s; s=s->next) {
1644 log.sig = s;
1645 fm_add_sig_stat (&log);
1646 }
1647 if (!c->output)
1648 c->output = m_strdup (name); /* for later use */
1649
1650 leave:
1651 if (in)
1652 gpg_file_data_release (in);
1653 if (out)
1654 gpg_file_data_release (out);
1655 free_if_alloc (log.file);
1656 return rc;
1657 }
1658
1659
1660 /* Import the keys from the file @name.
1661 Return value: 0 on success. */
1662 int
1663 fm_import (fm_state_t c, const char *name)
1664 {
1665 gpgme_ctx_t ctx = c->ctx;
1666 gpgme_error_t err;
1667 gpgme_import_result_t res;
1668 file_data_t keydata = NULL;
1669 int rc = 0;
1670
1671 free_if_alloc (c->output);
1672 c->output = m_strdup (name);
1673
1674 err = gpg_file_data_new (name, F_DATA_READ, &keydata);
1675 if (err)
1676 goto leave;
1677
1678 op_begin ();
1679 err = gpgme_op_import (ctx, keydata->dat);
1680 op_end ();
1681 if (err) {
1682 msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR);
1683 rc = WPTERR_GENERAL;
1684 goto leave;
1685 }
1686
1687 res = gpgme_op_import_result (ctx);
1688 print_import_status (res);
1689 if (res->no_user_id > 0) {
1690 msg_box (c->dlg, _("Key without a self signature was dectected!\n"
1691 "(This key is NOT usable for encryption, etc)\n"
1692 "\n"
1693 "Cannot import these key(s)!"), _("Import"), MB_INFO);
1694 }
1695
1696 leave:
1697 if (keydata)
1698 gpg_file_data_release (keydata);
1699 return rc;
1700 }
1701
1702
1703 /* Export the selected keys from the File Manager to a file. */
1704 int
1705 fm_export (fm_state_t c)
1706 {
1707 gpgme_ctx_t ctx = c->ctx;
1708 gpgme_error_t err;
1709 gpgme_key_t *rset = c->recp;
1710 file_data_t keydata = NULL;
1711 const char *name;
1712 char *p = NULL, *patt = NULL;
1713 int rc = 0;
1714
1715 if (!rset || !rset[0]) {
1716 msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR);
1717 rc = WPTERR_GENERAL;
1718 goto leave;
1719 }
1720
1721 if (rset[1] == NULL) /* count == 1*/
1722 p = km_gen_export_filename (rset[0]->subkeys->keyid+8, 0);
1723
1724 name = get_filesave_dlg (c->dlg, _("Choose Name for Key File"),
1725 NULL, p? p : NULL);
1726 if (!name)
1727 name = "keys.gpg";
1728
1729 patt = gpg_keylist_to_pattern (rset, c->n_recp);
1730
1731 err = gpg_file_data_new (name, F_DATA_WRITE, &keydata);
1732 if (err)
1733 goto leave;
1734
1735 op_begin ();
1736 err = gpgme_op_export (ctx, patt, 0, keydata->dat);
1737 op_end ();
1738 if (err) {
1739 msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR);
1740 rc = WPTERR_GENERAL;
1741 goto leave;
1742 }
1743 log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name);
1744
1745 leave:
1746 if (keydata)
1747 gpg_file_data_release (keydata);
1748 safe_free (patt);
1749 free_if_alloc (p);
1750 return rc;
1751 }
1752
1753
1754 /* Parse the command line and process the given file. */
1755 int
1756 fm_parse_command_line (char *cmdl)
1757 {
1758 fm_state_t ctx;
1759 const char *s;
1760 char *p, *fn = NULL;
1761 int count = 0, detached = 0;
1762 int type;
1763
1764 if (!cmdl || !*cmdl)
1765 return 0;
1766
1767 fm_state_new (&ctx);
1768 ctx->dlg = GetActiveWindow ();
1769 ctx->cache_cb = 1;
1770
1771 p = cmdl;
1772 if (p && *p > 32 && !stristr (p, "winpt.exe")
1773 && !strstr (p, "--" )) {
1774 count++;
1775 if (*p == '"') { /* need to remove quotes */
1776 fn = new char[strlen (p)];
1777 if (!fn)
1778 BUG (NULL);
1779 memcpy (fn, p+1, strlen (p) - 2);
1780 fn[strlen (p) -2] = '\0';
1781 }
1782 else
1783 fn = m_strdup (p);
1784 s = fm_get_file_type (fn, &type);
1785 if (!s || !strcmp (s, "UNKNOWN"))
1786 s = gnupg_check_file_ext (fn, &type);
1787 if (type == PGP_NONE) {
1788 log_box (_("File Manager"), MB_ERR,
1789 _("%s: no valid OpenPGP data found."), p);
1790 free_if_alloc (fn);
1791 return count;
1792 }
1793 switch (type) {
1794 case PGP_MESSAGE:
1795 fm_decrypt (ctx, fn);
1796 break;
1797
1798 case PGP_PUBKEY:
1799 case PGP_SECKEY:
1800 fm_import (ctx, fn);
1801 break;
1802
1803 case PGP_SIG:
1804 case PGP_CLEARSIG:
1805 if (type == PGP_SIG)
1806 detached = 1;
1807 fm_verify (ctx, detached, fn);
1808 file_verify_wait ();
1809 break;
1810
1811 default:
1812 break;
1813 }
1814 }
1815
1816 wipememory (&ctx->pass_cb, sizeof (ctx->pass_cb));
1817 free_if_alloc (fn);
1818 fm_state_release (ctx);
1819 return count;
1820 }
1821
1822
1823 /* Extract the last folder name from @name. */
1824 const char*
1825 default_dirname (const char *name)
1826 {
1827 char *p = strrchr (name, '\\');
1828 if (!p)
1829 return NULL;
1830 return p+1;
1831 }
1832
1833
1834 /* Store all selected files from @lv in a zip archive
1835 and encrypt the zip archive then.
1836 Return value: 0 on success. */
1837 int
1838 fm_encrypt_into_zip (fm_state_t ctx, listview_ctrl_t lv)
1839 {
1840 PK_FILE_LIST list=NULL;
1841 const char *outfile, *ext;
1842 char *out_enc;
1843 int nitems;
1844 int i, idx = -1;
1845 int rc;
1846
1847 nitems = listview_count_items (lv, 0);
1848 if (!nitems) {
1849 msg_box (NULL, _("Encrypting into a ZIP archive makes sense with multiple files"),
1850 _("File Manager"), MB_ERR);
1851 return WPTERR_GENERAL;
1852 }
1853
1854 outfile = get_filesave_dlg (ctx->dlg, _("Choose File Name for Output"),
1855 NULL, "Encrypted_Files.zip");
1856 if (!outfile)
1857 return WPTERR_GENERAL;
1858
1859 for (i=0; i < nitems; i++) {
1860 char name[300];
1861 if (!listview_get_item_state (lv, i))
1862 continue;
1863 if (idx == -1)
1864 idx = i;
1865 listview_get_item_text (lv, i, 1, name, sizeof (name)-1);
1866 pk_list_add (&list, name);
1867 }
1868
1869 pk_archiv_create (list, outfile);
1870 pk_list_free (list);
1871
1872 rc = fm_encrypt (ctx, outfile, 0);
1873 DeleteFile (outfile);
1874 if (rc)
1875 return rc;
1876
1877 ext = file_get_extension (ctx->ctx, ctx->sigmode)+1;
1878 out_enc = make_filename (NULL, outfile, ext);
1879 fm_set_status (lv, idx, FM_ENCRYPT, (gpgme_sig_mode_t)0, 1, out_enc);
1880 free_if_alloc (out_enc);
1881
1882 for (i=0; i < nitems; i++) {
1883 if (i != idx && listview_get_item_state (lv, i))
1884 listview_del_item (lv, i);
1885 }
1886 return 0;
1887 }
1888
1889
1890 int
1891 fm_encrypt_directory (fm_state_t c, const char *name)
1892 {
1893 PK_FILE_LIST list = NULL;
1894 WIN32_FIND_DATA findbuf;
1895 HANDLE hd;
1896 const char * s;
1897 char * patt = NULL, * p;
1898 int rc = 0;
1899
1900 if (!is_directory (name))
1901 return -1;
1902 patt = new char[strlen (name) + 4];
1903 if (!patt)
1904 BUG (NULL);
1905 strcpy (patt, name);
1906 strcat (patt, "\\*");
1907 hd = FindFirstFile (patt, &findbuf);
1908 if (!hd) {
1909 free_if_alloc (patt);
1910 return WPTERR_GENERAL;
1911 }
1912 if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) {
1913 p = make_filename( name, findbuf.cFileName, NULL );
1914 pk_list_add( &list, p );
1915 free_if_alloc( p );
1916 }
1917 while( FindNextFile( hd, &findbuf ) ) {
1918 if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) {
1919 p = make_filename( name, findbuf.cFileName, NULL );
1920 pk_list_add( &list, p );
1921 free_if_alloc( p );
1922 }
1923 }
1924 s = get_filesave_dlg (c->dlg, _("Choose a Name for the Archive"),
1925 NULL, default_dirname (name));
1926 if( !s ) {
1927 msg_box( c->dlg, _("Invalid archive name. Exit."), _("Encrypt Directory"), MB_ERR );
1928 rc = -1;
1929 goto leave;
1930 }
1931
1932 rc = pk_archiv_create( list, s );
1933 if( rc )
1934 msg_box( c->dlg, _("Could not create zip archive."), _("Encrypt Directory"), MB_ERR );
1935 else {
1936 fm_encrypt( c, s, 0 );
1937 remove( s );
1938 }
1939 leave:
1940 FindClose (hd);
1941 pk_list_free( list );
1942 free_if_alloc( patt );
1943 return rc;
1944 }
1945
1946
1947 static int CALLBACK
1948 fm_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby)
1949 {
1950 const char *a = 0;
1951 const char *b = 0;
1952
1953 switch ((int)sortby) {
1954 case FM_SORT_STAT:
1955 break;
1956 case FM_SORT_NAME:
1957 break;
1958 case FM_SORT_OP:
1959 break;
1960 }
1961 return stricmp (a, b);
1962 }
1963
1964
1965 /* Sort the list items from @lv with the mode given by @sortby. */
1966 int
1967 fm_sort (listview_ctrl_t lv, int sortby)
1968 {
1969 return listview_sort_items (lv, sortby, fm_cmp_cb);
1970 }
1971
1972
1973 /* Start the 'print md' dialog. Pass over the listview control
1974 @lv and the digest algo @mdalgo. */
1975 void
1976 fm_print_md (listview_ctrl_t lv, HWND dlg, int mdalgo)
1977 {
1978 struct md_file_s mdctx;
1979
1980 if (listview_count_items (lv, 0) == 0)
1981 return;
1982 memset (&mdctx, 0, sizeof (mdctx));
1983 mdctx.lv = lv;
1984 mdctx.mdalgo = mdalgo;
1985 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg,
1986 mdsum_dlg_proc, (LPARAM)&mdctx);
1987 }
1988
1989
1990 /* Send the selected file in @lv via MAPI to a mail recipient. */
1991 int
1992 fm_send_file (listview_ctrl_t lv)
1993 {
1994 char buf[128];
1995 int rc;
1996
1997 rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1);
1998 if (rc != -1)
1999 mapi_send_ascfile (buf);
2000 return 0;
2001 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26