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

Contents of /trunk/Src/wptFileManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (show annotations)
Mon May 1 12:22:18 2006 UTC (18 years, 10 months ago) by twoaday
File size: 48800 byte(s)
See ChangeLog.


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 gpgme_error_t rc;
411 fm_state_s *c;
412
413 c = new fm_state_s;
414 if (!c)
415 BUG (0);
416 memset (c, 0, sizeof *c);
417 rc = gpgme_new (&c->ctx);
418 if (rc)
419 BUG (0);
420 *ctx = c;
421 return 0;
422 }
423
424
425 /* Release the FM state handle @c. */
426 void
427 fm_state_release (fm_state_t c)
428 {
429 if (!c)
430 return;
431 if (c->ctx) {
432 gpgme_release (c->ctx);
433 c->ctx = NULL;
434 }
435 safe_free (c->recp);
436 free_if_alloc (c->opaque);
437 free_if_alloc (c->output);
438 delete c;
439 }
440
441
442 static int
443 fm_check_for_entry( listview_ctrl_t lv, const char *file )
444 {
445 char name[512];
446 int i;
447
448 memset (name, 0, sizeof (name));
449 for (i = 0; i < listview_count_items( lv, 0 ); i++) {
450 listview_get_item_text( lv, i, 1, name, sizeof (name) - 1 );
451 if( !strcmp( name, file ) )
452 return 1; /* found */
453 }
454
455 return 0;
456 }
457
458
459 static int
460 fm_set_ftype (listview_ctrl_t lv, const char *name)
461 {
462 const char *type;
463 int rc;
464
465 rc = fm_check_for_entry (lv, name);
466 if (rc)
467 return 0;
468 type = fm_get_file_type (name, NULL);
469 if (!type || !strcmp (type, "UNKNOWN"))
470 type = gnupg_check_file_ext (name, NULL);
471 rc = listview_add_item (lv, " ");
472 if (rc)
473 return -1;
474 listview_add_sub_item (lv, 0, 0, type);
475 listview_add_sub_item (lv, 0, 1, name);
476 return 0;
477 }
478
479
480 /* Add all files from the directory @path to the list view @lv. */
481 static int
482 fm_add_dir_files (listview_ctrl_t lv, char *path)
483 {
484 struct _finddata_t fd;
485 char * p;
486 long hd;
487
488 strcat (path, "\\*");
489 hd = _findfirst (path, &fd);
490 do {
491 p = new char [(strlen (path) + strlen (fd.name))+1];
492 if (!p)
493 BUG (0);
494 memcpy (p, path, strlen (path)-1);
495 p[strlen (path)-1] = 0;
496 strcat (p, fd.name);
497 if (!is_directory (p))
498 fm_set_ftype (lv, p);
499 free_if_alloc (p);
500 } while (_findnext (hd, &fd) == 0);
501 _findclose (hd);
502 return 0;
503 }
504
505
506 /* Add the drag & drop files from @dd_files to the
507 list view control @lv. */
508 int
509 fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files)
510 {
511 char name[384+4];
512 int nfiles;
513 int rc = 0;
514 int i;
515
516 memset (name, 0, sizeof (name));
517 nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0);
518 for (i = 0; i < nfiles; i++) {
519 DragQueryFile (dd_files, i, name, sizeof (name) -1);
520 if (is_directory (name))
521 rc = fm_add_dir_files (lv, name);
522 else
523 rc = fm_set_ftype (lv, name);
524 if (rc == -1)
525 break; /* XXX: fixme? */
526 }
527 DragFinish (dd_files);
528 return rc;
529 }
530
531
532 /* Add a single file @name to the list view and before
533 figure out the type of it.
534 Return value: 0 on success. */
535 static int
536 add_single_file (listview_ctrl_t lv, const char *name)
537 {
538 const char *type;
539 int rc = 0;
540
541 type = fm_get_file_type (name, NULL);
542 if (!type)
543 return WPTERR_FILE_OPEN;
544 if (!strcmp (type, "UNKNOWN"))
545 type = gnupg_check_file_ext (name, NULL);
546 rc = listview_add_item (lv, "");
547 if (!rc) {
548 listview_add_sub_item (lv, 0, 0, type);
549 listview_add_sub_item (lv, 0, 1, name);
550 }
551 return rc;
552 }
553
554
555 /* Use the common Open-File-Dialog to allow the user to
556 add one ore more selected files to the listview @lv. */
557 int
558 fm_add_opened_files (listview_ctrl_t lv, HWND dlg)
559 {
560 OPENFILENAME open;
561 char file[512], name[MAX_PATH+1];
562 char *path = NULL;
563 const char *s;
564 int i, len=0, n=0;
565 int rc=0;
566
567 memset (&open, 0, sizeof (open));
568 open.lStructSize = sizeof (OPENFILENAME);
569 open.hInstance = glob_hinst;
570 open.lpstrTitle = _("File Open");
571 open.lpstrFilter = "All Files (*.*)\0*.*\0\0";
572 open.hwndOwner = dlg;
573 open.lpstrFile = file;
574 open.nMaxFile = sizeof (file) - 1;
575 open.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER ;
576
577 memset (file, 0, sizeof file);
578 if (!GetOpenFileName (&open))
579 return 0;
580
581 s = file;
582 len = sizeof (file)-1;
583 for (;;) {
584 if (len < 2 || (*s == '\0' && *(s+1) == '\0'))
585 break;
586 memset (name, 0, sizeof (name));
587 for (i=0; len > 0; len--, i++) {
588 if (*s == '\0') {
589 name[i] = *s++;
590 break;
591 }
592 name[i] = *s++;
593 }
594 if (n == 0)
595 path = strdup (name);
596 else {
597 char *p = make_filename (path, name, NULL);
598 rc = add_single_file (lv, p);
599 safe_free (p);
600 }
601 n++;
602 }
603 if (n == 1) /* single file selected. */
604 rc = add_single_file (lv, path);
605 safe_free (path);
606 return rc;
607 }
608
609
610 int
611 fm_assume_onepass_sig (const char * fname)
612 {
613 armor_filter_context_t afx;
614 gpg_iobuf_t fp;
615 gpgme_data_t dat;
616 PACKET *pkt;
617 char tmpfile[MAX_PATH+1];
618 int check = 0;
619
620 pkt = (PACKET *)calloc (1, sizeof *pkt);
621 if (!fname) {
622 get_temp_name (tmpfile, sizeof (tmpfile)-1, "gpgme.tmp");
623 gpg_data_new_from_clipboard (&dat, 0);
624 gpg_data_release_and_set_file (dat, tmpfile);
625
626 fp = gpg_iobuf_open (tmpfile);
627 if (!fp)
628 return 0;
629 gpg_iobuf_ioctl (fp, 3, 1, NULL);
630 if (gpg_use_armor_filter(fp)) {
631 memset (&afx, 0, sizeof (afx));
632 gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx);
633 }
634 gpg_init_packet (pkt);
635 if (!gpg_parse_packet (fp, pkt)
636 && pkt->pkttype == PKT_COMPRESSED)
637 check = 1;
638 gpg_free_packet (pkt);
639 gpg_iobuf_close (fp);
640 remove (tmpfile);
641 }
642 /* XXX: implement it for real files */
643 safe_free (pkt);
644 return check;
645 }
646
647
648 int
649 fm_get_current_pos (listview_ctrl_t lv)
650 {
651 int i = 0, items;
652
653 items = listview_count_items (lv, 0);
654 if (!items)
655 return -1;
656 else if (items == 1) {
657 listview_select_one (lv, 0);
658 return 0;
659 }
660 else if (items > 1) {
661 i = listview_get_curr_pos (lv);
662 if (i == -1) {
663 msg_box (lv->ctrl, _("Please select a file."),
664 _("File Manager"), MB_ERR);
665 return -1;
666 }
667 return i;
668 }
669
670 return -1;
671 }
672
673
674 static int
675 fm_check_detached_sig (listview_ctrl_t lv, int pos)
676 {
677 char type[128];
678
679 listview_get_item_text (lv, pos, 0, type, sizeof (type)-1);
680 return !strcmp (type, "SIGNED-DETACH")? 1 : 0;
681 }
682
683
684 int
685 fm_check_file_type (listview_ctrl_t lv, int pos, int fm_cmd)
686 {
687 char status[128];
688 int rc = 0;
689
690 listview_get_item_text (lv, pos, 0, status, sizeof (status) - 1);
691
692 switch (fm_cmd) {
693 case FM_ENCRYPT:
694 case FM_ENCRYPT_DIR:
695 case FM_SIGNENCRYPT:
696 if (strcmp (status, "ENCRYPTED")
697 && strcmp (status, "SYMKEYENC"))
698 rc = 1;
699 break;
700
701 case FM_DECRYPT:
702 if (!strcmp (status, "DATA")
703 || !strcmp (status, "ENCRYPTED")
704 || !strcmp (status, "SYMKEYENC")
705 || !strcmp (status, "ARMORED"))
706 rc = 1;
707 break;
708
709 case FM_SIGN:
710 if( strncmp( status, "SIGNED", 6 ) )
711 rc = 1;
712 break;
713
714 case FM_VERIFY:
715 if( !strncmp( status, "SIGNED", 6 )
716 || !strcmp( status, "COMPRESSED" ) )
717 rc = 1;
718 break;
719
720 case FM_SYMENC:
721 if( strcmp( status, "SYMKEYENC" ) )
722 rc = 1;
723 break;
724
725 case FM_IMPORT:
726 if( !strcmp( status, "PUBKEY" )
727 || !strcmp( status, "SECKEY" ) )
728 rc = 1;
729 break;
730
731 case FM_WIPE:
732 case FM_LIST:
733 rc = 1;
734 break;
735 }
736
737 return rc;
738 }
739
740
741 /* Set the file status of the given command @fm_cmd.
742 @success is 0 on success. */
743 static void
744 fm_set_status (listview_ctrl_t lv, int pos, int fm_cmd,
745 gpgme_sig_mode_t sigmode, int success, const char *output)
746 {
747 char status[128], operat[128];
748 int update = 1;
749 const char *s;
750
751 if (fm_cmd == FM_LIST)
752 return;
753 success ? s = "SUCCESS" : s = "FAILED";
754 strcpy (operat, s);
755
756 switch (fm_cmd) {
757 case FM_ENCRYPT:
758 case FM_ENCRYPT_DIR:
759 case FM_SIGNENCRYPT: strcpy (status, "ENCRYPTED"); break;
760 case FM_DECRYPT: strcpy (status, "UNKNOWN"); break;
761 case FM_SIGN:
762 if (sigmode == GPGME_SIG_MODE_DETACH)
763 strcpy (status, "SIGNED-DETACH");
764 else if (sigmode == GPGME_SIG_MODE_CLEAR)
765 strcpy (status, "SIGNED-CLEAR");
766 else
767 strcpy (status, "SIGNED");
768 break;
769 case FM_VERIFY: update = 0; break;
770 case FM_SYMENC: strcpy (status, "SYMKEYENC"); break;
771 case FM_IMPORT: update = 0; break;
772 case FM_WIPE: strcpy (status, "WIPED"); break;
773 default: strcpy (status, "UNKNOWN"); break;
774 }
775
776 if (success && update) {
777 listview_add_sub_item (lv, pos, 0, status);
778 listview_add_sub_item (lv, pos, 1, output);
779 }
780 listview_add_sub_item( lv, pos, 2, operat );
781 }
782
783
784 int
785 fm_clearsign_8bit (listview_ctrl_t lv, fm_state_s *ctx)
786 {
787 FILE *f;
788 byte buf[32];
789 char name[256];
790 int i, n, cnt=0;
791
792 if (ctx->sigmode != GPGME_SIG_MODE_CLEAR)
793 return 0;
794 listview_get_item_text (lv, -1, 1, name, sizeof (name)-1);
795 if (stristr (name, ".TXT"))
796 return 0;
797 f = fopen (name, "rb");
798 if (!f)
799 return -1; /* should never happen */
800 n = fread (buf, 1, 32, f);
801 for (i = 0; i < n; i++) {
802 if (buf[i] == 0x00 || buf[i] > 170)
803 cnt++;
804 }
805 fclose (f);
806 if (!cnt)
807 return 0;
808 n = -1;
809 i = log_box (_("File Manager"), MB_WARN|MB_YESNO,
810 _("\"%s\" does not seems to be a text file.\n"
811 "Do you really want to clearsign it?"), name);
812 if (i == IDYES)
813 n = 0;
814 return n;
815 }
816
817
818 int
819 fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd)
820 {
821 struct secdel_confirm_s confirm = {0};
822 struct progress_filter_s pfx, pfx2;
823 fm_state_s * ctx;
824 int fm_cmd, sig_detached = 0;
825 int rc = 0, i, n, ndel = 0;
826 char fname[512], status[128];
827
828 switch (cmd) {
829 case ID_FILEMISC_ENCRYPT: fm_cmd = FM_ENCRYPT; break;
830 case ID_FILEMISC_ENCRYPT_ZIP:fm_cmd = FM_ENCRYPT_ZIP; break;
831 case ID_FILEMISC_DECRYPT: fm_cmd = FM_DECRYPT; break;
832 case ID_FILEMISC_SYMENC: fm_cmd = FM_SYMENC; break;
833 case ID_FILEMISC_SIGN: fm_cmd = FM_SIGN; break;
834 case ID_FILEMISC_VERIFY: fm_cmd = FM_VERIFY; break;
835 case ID_FILEMISC_IMPORT: fm_cmd = FM_IMPORT; break;
836 case ID_FILEMISC_WIPE: fm_cmd = FM_WIPE; break;
837 case ID_FILEMISC_LIST: fm_cmd = FM_LIST; break;
838 case ID_FILEMISC_SIGNENC: fm_cmd = FM_SIGNENCRYPT; break;
839 default: return 1; /* unknown command */
840 }
841
842 if (fm_get_current_pos (lv) == -1)
843 return WPTERR_GENERAL;
844 rc = fm_state_new (&ctx);
845 if (rc)
846 BUG (0);
847 ctx->dlg = dlg;
848
849 // XXX: for file operations the progress dialog will be
850 // reloaded somewhere and thus a 'dummy' dialog remains
851
852 /* we use it here to make sure that pfx_cleanup will not use
853 any weird values. */
854 memset (&pfx, 0, sizeof (pfx));
855 ctx->prog_cb = NULL;
856 if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) {
857 pfx.hwnd = dlg;
858 /*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/
859 /*ctx->prog_cb = &pfx;*/
860 }
861
862 /* Commands we need before we can perform the main command */
863 switch (fm_cmd) {
864 case FM_ENCRYPT_ZIP:
865 case FM_ENCRYPT:
866 case FM_SIGNENCRYPT:
867 if (fm_cmd == FM_SIGNENCRYPT)
868 ctx->req_signer = 1;
869 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_ENCRYPT,
870 ctx->dlg, file_encrypt_dlg_proc, (LPARAM)ctx);
871 if (ctx->cancel == 1) {
872 rc = WPTERR_GENERAL;
873 goto leave;
874 }
875 break;
876
877 case FM_SIGN:
878 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FILE_SIGN, dlg,
879 file_sign_dlg_proc, (LPARAM) ctx);
880 if (ctx->cancel == 1 || fm_clearsign_8bit (lv, ctx)) {
881 rc = WPTERR_GENERAL;
882 goto leave;
883 }
884 break;
885
886 case FM_WIPE:
887 memset (&pfx2, 0, sizeof (pfx2));
888 secure_unlink_set_cb (progress_callback, &pfx2);
889 break;
890 }
891
892 for( i = 0, n = 0; i < listview_count_items( lv, 0 ); i++ ) {
893 if( !listview_get_item_state( lv, i ) )
894 continue;
895 listview_get_item_text( lv, i, 0, status, sizeof (status) -1 );
896 if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT)
897 n++;
898 if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN)
899 n++;
900 if (fm_cmd == FM_WIPE)
901 ndel++;
902 }
903
904 if (n > 1 && fm_cmd != FM_SYMENC)
905 ctx->cache_cb = 1;
906
907 if (fm_cmd == FM_WIPE && ndel > 0) {
908 memset (&confirm, 0, sizeof confirm);
909 confirm.lv_files = lv;
910 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg,
911 file_secdel_confirm_dlg_proc, (LPARAM)&confirm);
912 if (!confirm.yes)
913 goto leave;
914 }
915
916 if (fm_cmd == FM_ENCRYPT_ZIP)
917 fm_encrypt_into_zip (ctx, lv);
918
919 for (i = 0; i < listview_count_items (lv, 0); i++) {
920 if (!listview_get_item_state (lv, i))
921 continue;
922 listview_get_item_text (lv, i, 1, fname, sizeof (fname) - 1);
923 if( file_exist_check (fname) && !is_directory (fname)) {
924 log_box (_("File Manager"), MB_ERR,
925 _("\"%s\" does not exist"), fname);
926 continue;
927 }
928 if (is_directory (fname))
929 fm_cmd = FM_ENCRYPT_DIR;
930 if (!fm_check_file_type (lv, i, fm_cmd))
931 continue;
932 sig_detached = fm_check_detached_sig (lv, i);
933 switch (fm_cmd) {
934 case FM_LIST: rc = fm_list( fname, dlg ); break;
935 case FM_WIPE: rc = fm_wipe( fname ); break;
936 case FM_ENCRYPT: rc = fm_encrypt( ctx, fname, 0 ); break;
937 case FM_ENCRYPT_DIR: rc = fm_encrypt_directory( ctx, fname ); break;
938 case FM_SIGNENCRYPT: rc = fm_encrypt( ctx, fname, 1 ); break;
939 case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break;
940 case FM_SIGN: rc = fm_sign( ctx, fname ); break;
941 case FM_SYMENC: rc = fm_sym_encrypt (ctx, fname); break;
942 case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break;
943 case FM_IMPORT:
944 free_if_alloc (ctx->opaque);
945 ctx->opaque = m_strdup (fname);
946 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,
947 file_import_dlg_proc, (LPARAM)ctx);
948 if (ctx->cancel == 1)
949 continue;
950 rc = fm_import (ctx, fname);
951 break;
952 }
953 if (ctx->cancel == 1) {
954 ctx->cancel = 0;
955 continue;
956 }
957 fm_set_status (lv, i, fm_cmd, ctx->sigmode, !rc, ctx->output);
958 free_if_alloc (ctx->output);
959 progress_cleanup (&pfx);
960 }
961
962 if (fm_cmd == FM_WIPE) {
963 secure_unlink_set_cb (NULL, NULL);
964 progress_cleanup (&pfx2);
965 }
966 if (ctx->cache_cb) {
967 release_gpg_passphrase_cb (&ctx->pass_cb);
968 ctx->cache_cb = 0; /* make sure it's only used for this session! */
969 }
970
971 /* remove wipe files from the list */
972 n = listview_count_items (lv, 0);
973 while (n--) {
974 listview_get_item_text (lv, n, 0, status, sizeof (status) - 1);
975 if (!strcmp (status, "WIPED"))
976 listview_del_item (lv, n);
977 }
978
979 leave:
980 if (!rc)
981 fm_state_release (ctx);
982 progress_cleanup (&pfx);
983 return rc;
984 } /* fm_parse_files */
985
986
987 /* Wipe the given file @name with the delete mode
988 from the configuration.
989 Return value: 0 on success. */
990 int
991 fm_wipe (const char *name)
992 {
993 int rc;
994
995 SetCursor (LoadCursor (NULL, IDC_WAIT));
996 remove_crit_file_attrs (name, 1);
997 rc = secure_unlink (name, reg_prefs.wipe_mode);
998 SetCursor (LoadCursor (NULL, IDC_ARROW));
999 return rc;
1000 }
1001
1002
1003 /* Dump out the given PGP packets from file @name in a dialog. */
1004 int
1005 fm_list (const char *name, HWND dlg)
1006 {
1007 dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg,
1008 file_stat_dlg_proc, (LPARAM)name, _("File Status"),
1009 IDS_WINPT_FILE_STAT );
1010 return 0;
1011 }
1012
1013
1014 static int
1015 ask_filename (fm_state_t c, const char *msg, char **dst)
1016 {
1017 const char * s;
1018
1019 s = get_filesave_dlg (c->dlg, msg, NULL, NULL);
1020 if (!s)
1021 return WPTERR_GENERAL;
1022
1023 if (dst != NULL)
1024 free_if_alloc (*dst);
1025 free_if_alloc (c->output);
1026 c->output = m_strdup (s);
1027 if (dst)
1028 *dst = fm_quote_file (s);
1029 return 0;
1030 }
1031
1032
1033 int
1034 fm_encrypt (fm_state_t c, const char *name, int sign)
1035 {
1036 gpgme_error_t err;
1037 gpgme_key_t key = NULL;
1038 gpgme_ctx_t ctx = c->ctx;
1039 file_data_t in=NULL, out=NULL;
1040 char *keyid = NULL, ext[5];
1041 /*int no_compr = 0;*/
1042 int rc = 0;
1043
1044 c->output = new char[strlen (name) + 5 + 1];
1045 if (!c->output)
1046 BUG (0);
1047 strcpy (ext, file_get_extension (ctx, c->sigmode));
1048 strcpy (c->output, name);
1049 strcat (c->output, ext);
1050
1051 if (!overwrite_file (c->output)) {
1052 rc = ask_filename (c, _("Enter filename for encrypted file"), NULL);
1053 if (rc)
1054 goto leave;
1055 }
1056
1057 err = gpg_file_data_new (name, F_DATA_READ, &in);
1058 if (err)
1059 goto leave;
1060 remove_crit_file_attrs (c->output, 0);
1061 err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1062 if (err)
1063 goto leave;
1064
1065 /*
1066 if (c->prog_cb) {
1067 c->prog_cb->what = name;
1068 gpg_file_data_set_cb (in, c->prog_cb);
1069 }
1070 */
1071
1072 /* XXX: disable compression for multi-media files.
1073 no_compr = is_multi_media (name);
1074 gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr);
1075 */
1076
1077 if (sign) {
1078 if (gpgme_signers_enum (ctx, 0) == NULL) {
1079 keyid = get_gnupg_default_key ();
1080 if (!keyid) {
1081 msg_box (c->dlg, _("Could not get default secret key."),
1082 _("Signing"), MB_ERR);
1083 rc = WPTERR_GENERAL;
1084 goto leave;
1085 }
1086 if (get_seckey (keyid, &key))
1087 BUG (0);
1088 gpgme_signers_add (ctx, key);
1089 }
1090 else {
1091 gpgme_key_t sigkey = gpgme_signers_enum (ctx, 0);
1092 if (sigkey && sigkey->subkeys) {
1093 keyid = m_strdup (sigkey->subkeys->keyid);
1094 }
1095 }
1096 if (!c->init_cb || !c->cache_cb) {
1097 set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN,
1098 c->dlg, _("Signing"));
1099 c->init_cb = 1;
1100 }
1101 op_begin ();
1102 err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
1103 in->dat, out->dat);
1104 op_end ();
1105 if (!c->cache_cb)
1106 release_gpg_passphrase_cb (&c->pass_cb);
1107 if (c->pass_cb.cancel) {
1108 rc = WPTERR_GENERAL;
1109 goto leave;
1110 }
1111 if (err) {
1112 msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
1113 if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE)
1114 agent_del_cache (keyid);
1115 rc = WPTERR_GENERAL;
1116 goto leave;
1117 }
1118 }
1119 else {
1120 op_begin ();
1121 err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
1122 in->dat, out->dat);
1123 op_end ();
1124 if (err) {
1125 msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);
1126 rc = WPTERR_GENERAL;
1127 goto leave;
1128 }
1129 }
1130
1131 leave:
1132 if (in)
1133 gpg_file_data_release (in);
1134 if (out)
1135 gpg_file_data_release (out);
1136 free_if_alloc (keyid);
1137 if (!rc && c->wipe)
1138 secure_unlink (name, WIPE_MODE_SIMPLE);
1139 return rc;
1140 }
1141
1142
1143 int
1144 fm_sym_encrypt (fm_state_t c, const char * name)
1145 {
1146 gpgme_ctx_t ctx = c->ctx;
1147 gpgme_error_t err;
1148 file_data_t in=NULL, out=NULL;
1149 int rc = 0, cancel = 0;
1150 char ext[5], * pass;
1151
1152 pass = request_passphrase2 (_("Symmetric"), 0, &cancel);
1153 if (cancel) {
1154 c->cancel = 1;
1155 return 0;
1156 }
1157
1158 /* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/
1159 c->output = new char[strlen (name) + 5 + 1];
1160 if (!c->output)
1161 BUG (0);
1162 strcpy (ext, file_get_extension (ctx, c->sigmode));
1163 strcpy (c->output, name);
1164 strcat (c->output, ext);
1165
1166 if (overwrite_file (c->output) == 0) {
1167 rc = WPTERR_GENERAL;
1168 goto leave;
1169 }
1170
1171 gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);
1172
1173 err = gpg_file_data_new (name, 1, &in);
1174 if (err)
1175 goto leave;
1176 err = gpg_file_data_new (c->output, 0, &out);
1177 if (err)
1178 goto leave;
1179
1180 op_begin ();
1181 err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST,
1182 in->dat, out->dat);
1183 op_end ();
1184 if (err) {
1185 msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR);
1186 rc = WPTERR_GENERAL;
1187 goto leave;
1188 }
1189 if (file_exist_check (c->output)) {
1190 msg_box (c->dlg, _("Encryption failed."), _("Symmetric"), MB_ERR);
1191 rc = WPTERR_GENERAL;
1192 }
1193
1194 leave:
1195 if (in)
1196 gpg_file_data_release (in);
1197 if (out)
1198 gpg_file_data_release (out);
1199 sfree_if_alloc (pass);
1200 return rc;
1201 }
1202
1203
1204 char* get_pka_status (gpgme_signature_t sig);
1205
1206
1207 /* Show the human readable verify result from @sigres. */
1208 static void
1209 show_verify_result (gpgme_verify_result_t sigres)
1210 {
1211 gpgme_signature_t sig=sigres->signatures;
1212 winpt_key_s key;
1213 const char *s, *keyid, *uid;
1214 char *pka_info;
1215 char buf[384];
1216 int sigok = 0;
1217
1218 sig = sigres->signatures;
1219 sigok = sig->summary & GPGME_SIGSUM_GREEN;
1220 s = sigok? _("Good signature") : _("BAD signature");
1221 keyid = sig->fpr;
1222 if (!keyid)
1223 return;
1224 pka_info = get_pka_status (sig);
1225 keyid = get_keyid_from_fpr (sig->fpr);
1226 memset (&key, 0, sizeof (key));
1227 if (!winpt_get_pubkey (sig->fpr, &key))
1228 uid = key.ext->uids->uid;
1229 else
1230 uid = _("user ID not found");
1231 _snprintf (buf, sizeof (buf)-1, _("Signature made %s using %s key ID %s\n"
1232 "%s from \"%s\"\n%s"),
1233 strtimestamp (sig->timestamp),
1234 get_key_pubalgo (sig->pubkey_algo),
1235 keyid, s, uid, pka_info? pka_info : "");
1236 msg_box (NULL, buf, _("Decrypt Verify"), sigok? MB_OK: MB_ICONWARNING|MB_OK);
1237 free_if_alloc (pka_info);
1238 }
1239
1240
1241 /* Check the recipients if we have at least one secret key. */
1242 bool
1243 secret_key_available (gpgme_recipient_t rset)
1244 {
1245 gpgme_recipient_t r;
1246 gpgme_key_t key;
1247
1248 for (r=rset; r; r = r->next) {
1249 if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY)
1250 continue;
1251 else {
1252 /* extra check to make sure the key is available right now. */
1253 if (!get_seckey (r->keyid, &key))
1254 return true;
1255 }
1256 }
1257 return false;
1258 }
1259
1260
1261 /* If the decrypt result contains the original file name,
1262 we use it instead of the artificial "output - .gpg" string. */
1263 static int
1264 restore_original_name (const char *output, const char *file_name)
1265 {
1266 char *dir;
1267 char *orig;
1268 int rc = 0;
1269
1270 dir = strrchr (output, '\\');
1271 if (!dir)
1272 orig = strdup (file_name);
1273 else {
1274 orig = (char*)calloc (1, strlen (file_name)+ 1 +
1275 strlen (output)+1);
1276 if (!orig)
1277 BUG (0);
1278 memcpy (orig, output, (dir-output)+1);
1279 strcat (orig, file_name);
1280 }
1281 if (overwrite_file (orig)) {
1282 DeleteFile (orig);
1283 if (!MoveFile (output, orig))
1284 rc = -1;
1285 }
1286 safe_free (orig);
1287 return rc;
1288 }
1289
1290
1291 /* Decrypt the file @name. */
1292 int
1293 fm_decrypt (fm_state_t c, const char *name)
1294 {
1295 gpgme_error_t err;
1296 gpgme_ctx_t ctx = c->ctx;
1297 gpgme_decrypt_result_t res;
1298 gpgme_verify_result_t sigres;
1299 file_data_t in = NULL, out = NULL;
1300 int rc = 0;
1301
1302 if (!c->init_cb || !c->cache_cb) {
1303 set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT,
1304 c->dlg, _("Decryption"));
1305 c->init_cb = 1;
1306 }
1307
1308 c->output = m_strdup (name);
1309 if (is_openpgp_ext (c->output))
1310 c->output[strlen (c->output)-4] = '\0';
1311 else {
1312 const char *s;
1313 s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"),
1314 NULL, NULL);
1315 if (s) {
1316 free_if_alloc (c->output);
1317 c->output = m_strdup (s);
1318 }
1319 }
1320
1321 if (overwrite_file (c->output) == 0) {
1322 rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL);
1323 if (rc)
1324 goto leave;
1325 }
1326
1327 err = gpg_file_data_new (name, F_DATA_READ, &in);
1328 if (err)
1329 goto leave;
1330 remove_crit_file_attrs (c->output, 0);
1331 err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1332 if (err)
1333 goto leave;
1334
1335 op_begin ();
1336 err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat);
1337 op_end ();
1338 if (!c->cache_cb)
1339 release_gpg_passphrase_cb (&c->pass_cb);
1340 if (c->pass_cb.cancel) {
1341 rc = WPTERR_GENERAL;
1342 goto leave;
1343 }
1344
1345 res = gpgme_op_decrypt_result (ctx);
1346 if (res && res->recipients && !secret_key_available (res->recipients)) {
1347 const char *keyid = res->recipients->keyid;
1348 char *p = get_key_userid (keyid+8);
1349 gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo;
1350
1351 log_box (_("Decryption"), MB_ERR,
1352 _("Encrypted with %s key, ID %s.%s\n"
1353 "Decryption failed: secret key not available."),
1354 get_key_pubalgo (pkalgo), keyid+8, p);
1355 rc = WPTERR_GENERAL;
1356 free_if_alloc (p);
1357 goto leave;
1358 }
1359 else if (err) {
1360 msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR);
1361 rc = WPTERR_GENERAL;
1362 goto leave;
1363 }
1364 if (file_exist_check (c->output)) {
1365 log_box ("Decrypt", MB_ERR,
1366 _("Decryption failed.\n%s: does not exist."), c->output);
1367 rc = WPTERR_GENERAL;
1368 goto leave;
1369 }
1370 else if (res && res->file_name) {
1371 char *file = strrchr (c->output, '\\');
1372 int id = log_box (_("Decrypt"), MB_QUEST_ASK,
1373 _("The original file name is '%s'.\n\n"
1374 "Do you want to use this instead of '%s'?"),
1375 res->file_name, file? file+1 : c->output);
1376 if (id == IDYES) {
1377 /* before we can move the file, it needs to be closed first. */
1378 gpg_file_data_release (out);
1379 out = NULL;
1380 restore_original_name (c->output, res->file_name);
1381 }
1382 }
1383 sigres = gpgme_op_verify_result (ctx);
1384 if (sigres && sigres->signatures)
1385 show_verify_result (sigres);
1386
1387 leave:
1388 if (in)
1389 gpg_file_data_release (in);
1390 if (out)
1391 gpg_file_data_release (out);
1392
1393 return rc;
1394 }
1395
1396
1397 int
1398 fm_sign (fm_state_t c, const char * name)
1399 {
1400 gpgme_ctx_t ctx = c->ctx;
1401 gpgme_error_t err;
1402 file_data_t in=NULL, out=NULL;
1403 char ext[5];
1404 int rc = 0;
1405
1406 if (!c->init_cb || !c->cache_cb) {
1407 set_gpg_passphrase_cb (&c->pass_cb, c->ctx,
1408 GPG_CMD_SIGN, c->dlg, _("Signing"));
1409 c->init_cb = 1;
1410 }
1411
1412 free_if_alloc (c->output);
1413 c->output = new char[strlen (name) + 5 + 1];
1414 if (!c->output)
1415 BUG (NULL);
1416 strcpy (ext, file_get_extension (ctx, c->sigmode));
1417 strcpy (c->output, name);
1418 strcat (c->output, ext);
1419
1420 if (!overwrite_file (c->output)) {
1421 rc = ask_filename (c, _("Enter filename for signed file"), NULL);
1422 if (rc)
1423 goto leave;
1424 }
1425
1426 err = gpg_file_data_new (name, F_DATA_READ, &in);
1427 if (err)
1428 goto leave;
1429 remove_crit_file_attrs (c->output, 0);
1430 err = gpg_file_data_new (c->output, F_DATA_WRITE, &out);
1431 if (err)
1432 goto leave;
1433
1434 op_begin ();
1435 err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode);
1436 op_end ();
1437 if (!c->cache_cb)
1438 release_gpg_passphrase_cb (&c->pass_cb);
1439 if (c->pass_cb.cancel) {
1440 rc = WPTERR_GENERAL;
1441 goto leave;
1442 }
1443 if (err) {
1444 msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
1445 rc = WPTERR_GENERAL;
1446 goto leave;
1447 }
1448
1449 leave:
1450 if (in)
1451 gpg_file_data_release (in);
1452 if (out)
1453 gpg_file_data_release (out);
1454 return rc;
1455 }
1456
1457
1458 static void
1459 fm_add_sig_stat (file_sig_ctx_t log)
1460 {
1461 struct winpt_key_s key;
1462 const char *kid;
1463
1464 memset (&key, 0, sizeof (key));
1465 kid = get_keyid_from_fpr (log->sig->fpr);
1466 log->use_uid = 0;
1467 if (!winpt_get_pubkey (kid, &key)) {
1468 log->user_id = key.ext->uids->uid;
1469 log->use_uid = 1;
1470 }
1471 file_verify_add_state (log);
1472 }
1473
1474
1475 /* Verify a detached signature from the clipboard. */
1476 static int
1477 verify_pasted (listview_ctrl_t lv, fm_state_t ctx,
1478 const char *dat, int pos, HWND dlg)
1479 {
1480 FILE *fp;
1481 char stat[32];
1482 char file[256], *fname = NULL;
1483 int del_end = 0;
1484
1485 listview_get_item_text (lv, pos, 0, stat, sizeof (stat)-1);
1486 listview_get_item_text (lv, pos, 1, file, sizeof (file)-1);
1487 if (strcmp (stat, "UNKNOWN"))
1488 return 0;
1489 fname = make_filename (NULL, file, "asc");
1490 if (file_exist_check (fname) != 0) {
1491 fp = fopen (fname, "wb");
1492 if (fp == NULL) {
1493 log_box (_("File Manager"), MB_ERR, "could not create '%s'", fname);
1494 free_if_alloc (fname);
1495 return WPTERR_GENERAL;
1496 }
1497 fwrite (dat, 1, strlen (dat), fp);
1498 fclose (fp);
1499 del_end = 1;
1500 }
1501 fm_verify (ctx, 1, fname);
1502 if (del_end)
1503 remove (fname);
1504 free_if_alloc (fname);
1505 return 0;
1506 }
1507
1508
1509 /* Figure out if the clipboard contains a detached signature. */
1510 int
1511 fm_verify_pasted_detsig (listview_ctrl_t lv, HWND dlg)
1512 {
1513 fm_state_t ctx = NULL;
1514 char * dat=NULL;
1515 int i, fnd = 0;
1516
1517 dat = get_clip_text (NULL);
1518 if (!dat || !strstr (dat, "BEGIN PGP SIGNATURE")) {
1519 msg_box (dlg, _("Could not find detached signature in the clipboard."),
1520 _("File Manager"), MB_ERR);
1521 free_if_alloc (dat);
1522 return WPTERR_GENERAL;
1523 }
1524 /* XXX find a way to filter out bad signatures or just ignore all in
1525 this case */
1526 fm_state_new (&ctx);
1527 i = listview_get_curr_pos (lv);
1528 if (i= -1) {
1529 verify_pasted (lv, ctx, dat, i, dlg);
1530 fnd = 1;
1531 }
1532 else {
1533 for (i=0; i < listview_count_items (lv, 0); i++) {
1534 verify_pasted (lv, ctx, dat, i, dlg);
1535 fnd = 1;
1536 }
1537 }
1538 if (!fnd)
1539 msg_box (dlg, _("No files to check."), _("File Manager"), MB_INFO);
1540 free_if_alloc (dat);
1541 fm_state_release (ctx);
1542 return 0;
1543 }
1544
1545
1546 /* Extract automatically the output file name from @name.
1547 If @detached is 1, a detached sig is assumed. */
1548 static int
1549 get_output_file (fm_state_t c, const char *name, int detached)
1550 {
1551 const char *file = NULL;
1552 const char *title;
1553 char fname[384];
1554
1555 if (detached)
1556 title = _("Select Data File");
1557 else
1558 title = _("Selected Output File");
1559
1560 if (stristr (name, ".sig") ||
1561 stristr (name, ".asc") ||
1562 stristr (name, ".gpg")) {
1563 _snprintf (fname, sizeof (fname) - 1, "%s", name);
1564 fname[strlen (fname) - 4] = '\0';
1565 if (file_exist_check (fname) == 0 && detached)
1566 file = fname;
1567 else if (!detached) {
1568 /* If the signature is clear or normal, make sure we do not
1569 overwrite the original file if it exists. */
1570 if (file_exist_check (fname) == 0 && !overwrite_file (fname)) {
1571 file = get_filesave_dlg (c->dlg, title, NULL, NULL);
1572 if (!file)
1573 return WPTERR_GENERAL;
1574 }
1575 else
1576 file = fname;
1577 }
1578 }
1579 if (!file)
1580 file = get_fileopen_dlg (c->dlg, title, NULL, NULL);
1581 if (file) {
1582 free_if_alloc (c->output);
1583 c->output = m_strdup (file);
1584 }
1585 else {
1586 msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);
1587 return WPTERR_GENERAL;
1588 }
1589 if (detached)
1590 c->sigmode = GPGME_SIG_MODE_DETACH;
1591 else {
1592 if (stristr (name, ".asc"))
1593 c->sigmode = GPGME_SIG_MODE_CLEAR;
1594 else
1595 c->sigmode = GPGME_SIG_MODE_NORMAL;
1596 }
1597 return 0;
1598 }
1599
1600
1601 /* Verify the signature from the file @name. If @detached 1,
1602 it is assumed that a detached signature should be checked. */
1603 int
1604 fm_verify (fm_state_t c, int detached, const char *name)
1605 {
1606 gpgme_ctx_t ctx = c->ctx;
1607 gpgme_error_t err;
1608 gpgme_signature_t s;
1609 gpgme_verify_result_t res;
1610 struct file_sig_ctx_s log;
1611 file_data_t in=NULL, out=NULL;
1612 int rc = 0;
1613
1614 if (stristr (name, ".sig"))
1615 detached = 1;
1616
1617 if (get_output_file (c, name, detached))
1618 return WPTERR_GENERAL;
1619
1620 memset (&log, 0, sizeof (log));
1621 log.file = m_strdup (name);
1622 file_verify_create_dlg ();
1623
1624 err = gpg_file_data_new (name, F_DATA_READ, &in);
1625 if (err)
1626 goto leave;
1627 err = gpg_file_data_new (c->output,
1628 detached? F_DATA_READ : F_DATA_WRITE, &out);
1629 if (err)
1630 goto leave;
1631
1632 op_begin ();
1633 if (c->sigmode == GPGME_SIG_MODE_DETACH)
1634 err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);
1635 else
1636 err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat);
1637 op_end ();
1638 if (err) {
1639 msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR);
1640 rc = WPTERR_GENERAL;
1641 goto leave;
1642 }
1643
1644 res = gpgme_op_verify_result (ctx);
1645 for (s=res->signatures; s; s=s->next) {
1646 log.sig = s;
1647 fm_add_sig_stat (&log);
1648 }
1649 if (!c->output)
1650 c->output = m_strdup (name); /* for later use */
1651
1652 leave:
1653 if (in)
1654 gpg_file_data_release (in);
1655 if (out)
1656 gpg_file_data_release (out);
1657 free_if_alloc (log.file);
1658 return rc;
1659 }
1660
1661
1662 /* Import the keys from the file @name.
1663 Return value: 0 on success. */
1664 int
1665 fm_import (fm_state_t c, const char *name)
1666 {
1667 gpgme_ctx_t ctx = c->ctx;
1668 gpgme_error_t err;
1669 gpgme_import_result_t res;
1670 file_data_t keydata = NULL;
1671 int rc = 0;
1672
1673 free_if_alloc (c->output);
1674 c->output = m_strdup (name);
1675
1676 err = gpg_file_data_new (name, F_DATA_READ, &keydata);
1677 if (err)
1678 goto leave;
1679
1680 op_begin ();
1681 err = gpgme_op_import (ctx, keydata->dat);
1682 op_end ();
1683 if (err) {
1684 msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR);
1685 rc = WPTERR_GENERAL;
1686 goto leave;
1687 }
1688
1689 res = gpgme_op_import_result (ctx);
1690 print_import_status (res);
1691 if (res->no_user_id > 0) {
1692 msg_box (c->dlg, _("Key without a self signature was dectected!\n"
1693 "(This key is NOT usable for encryption, etc)\n"
1694 "\n"
1695 "Cannot import these key(s)!"), _("Import"), MB_INFO);
1696 }
1697
1698 leave:
1699 if (keydata)
1700 gpg_file_data_release (keydata);
1701 return rc;
1702 }
1703
1704
1705 /* Export the selected keys from the File Manager to a file. */
1706 int
1707 fm_export (fm_state_t c)
1708 {
1709 gpgme_ctx_t ctx = c->ctx;
1710 gpgme_error_t err;
1711 gpgme_key_t *rset = c->recp;
1712 file_data_t keydata = NULL;
1713 const char *name;
1714 char *p = NULL, *patt = NULL;
1715 int rc = 0;
1716
1717 if (!rset || !rset[0]) {
1718 msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR);
1719 rc = WPTERR_GENERAL;
1720 goto leave;
1721 }
1722
1723 if (rset[1] == NULL) /* count == 1*/
1724 p = km_gen_export_filename (rset[0]->subkeys->keyid+8, 0);
1725
1726 name = get_filesave_dlg (c->dlg, _("Choose Name for Key File"),
1727 NULL, p? p : NULL);
1728 if (!name)
1729 name = "keys.gpg";
1730
1731 patt = gpg_keylist_to_pattern (rset, c->n_recp);
1732
1733 err = gpg_file_data_new (name, F_DATA_WRITE, &keydata);
1734 if (err)
1735 goto leave;
1736
1737 op_begin ();
1738 err = gpgme_op_export (ctx, patt, 0, keydata->dat);
1739 op_end ();
1740 if (err) {
1741 msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR);
1742 rc = WPTERR_GENERAL;
1743 goto leave;
1744 }
1745 log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name);
1746
1747 leave:
1748 if (keydata)
1749 gpg_file_data_release (keydata);
1750 safe_free (patt);
1751 free_if_alloc (p);
1752 return rc;
1753 }
1754
1755
1756 /* Parse the command line and process the given file. */
1757 int
1758 fm_parse_command_line (char *cmdl)
1759 {
1760 fm_state_t ctx;
1761 const char *s;
1762 char *p, *fn = NULL;
1763 int count = 0, detached = 0;
1764 int type;
1765
1766 if (!cmdl || !*cmdl)
1767 return 0;
1768
1769 fm_state_new (&ctx);
1770 ctx->dlg = GetActiveWindow ();
1771 ctx->cache_cb = 1;
1772
1773 p = cmdl;
1774 if (p && *p > 32 && !stristr (p, "winpt.exe")
1775 && !strstr (p, "--" )) {
1776 count++;
1777 if (*p == '"') { /* need to remove quotes */
1778 fn = new char[strlen (p)];
1779 if (!fn)
1780 BUG (NULL);
1781 memcpy (fn, p+1, strlen (p) - 2);
1782 fn[strlen (p) -2] = '\0';
1783 }
1784 else
1785 fn = m_strdup (p);
1786 s = fm_get_file_type (fn, &type);
1787 if (!s || !strcmp (s, "UNKNOWN"))
1788 s = gnupg_check_file_ext (fn, &type);
1789 if (type == PGP_NONE) {
1790 log_box (_("File Manager"), MB_ERR,
1791 _("%s: no valid OpenPGP data found."), p);
1792 free_if_alloc (fn);
1793 return count;
1794 }
1795 switch (type) {
1796 case PGP_MESSAGE:
1797 fm_decrypt (ctx, fn);
1798 break;
1799
1800 case PGP_PUBKEY:
1801 case PGP_SECKEY:
1802 fm_import (ctx, fn);
1803 break;
1804
1805 case PGP_SIG:
1806 case PGP_CLEARSIG:
1807 if (type == PGP_SIG)
1808 detached = 1;
1809 fm_verify (ctx, detached, fn);
1810 file_verify_wait ();
1811 break;
1812
1813 default:
1814 break;
1815 }
1816 }
1817
1818 wipememory (&ctx->pass_cb, sizeof (ctx->pass_cb));
1819 free_if_alloc (fn);
1820 fm_state_release (ctx);
1821 return count;
1822 }
1823
1824
1825 /* Extract the last folder name from @name. */
1826 const char*
1827 default_dirname (const char *name)
1828 {
1829 char *p = strrchr (name, '\\');
1830 if (!p)
1831 return NULL;
1832 return p+1;
1833 }
1834
1835
1836 /* Store all selected files from @lv in a zip archive
1837 and encrypt the zip archive then.
1838 Return value: 0 on success. */
1839 int
1840 fm_encrypt_into_zip (fm_state_t ctx, listview_ctrl_t lv)
1841 {
1842 PK_FILE_LIST list=NULL;
1843 const char *outfile, *ext;
1844 char *out_enc;
1845 int nitems;
1846 int i, idx = -1;
1847 int rc;
1848
1849 nitems = listview_count_items (lv, 0);
1850 if (!nitems) {
1851 msg_box (NULL, _("Encrypting into a ZIP archive makes sense with multiple files"),
1852 _("File Manager"), MB_ERR);
1853 return WPTERR_GENERAL;
1854 }
1855
1856 outfile = get_filesave_dlg (ctx->dlg, _("Choose File Name for Output"),
1857 NULL, "Encrypted_Files.zip");
1858 if (!outfile)
1859 return WPTERR_GENERAL;
1860
1861 for (i=0; i < nitems; i++) {
1862 char name[300];
1863 if (!listview_get_item_state (lv, i))
1864 continue;
1865 if (idx == -1)
1866 idx = i;
1867 listview_get_item_text (lv, i, 1, name, sizeof (name)-1);
1868 pk_list_add (&list, name);
1869 }
1870
1871 pk_archiv_create (list, outfile);
1872 pk_list_free (list);
1873
1874 rc = fm_encrypt (ctx, outfile, 0);
1875 DeleteFile (outfile);
1876 if (rc)
1877 return rc;
1878
1879 ext = file_get_extension (ctx->ctx, ctx->sigmode)+1;
1880 out_enc = make_filename (NULL, outfile, ext);
1881 fm_set_status (lv, idx, FM_ENCRYPT, (gpgme_sig_mode_t)0, 1, out_enc);
1882 free_if_alloc (out_enc);
1883
1884 for (i=0; i < nitems; i++) {
1885 if (i != idx && listview_get_item_state (lv, i))
1886 listview_del_item (lv, i);
1887 }
1888 return 0;
1889 }
1890
1891
1892 int
1893 fm_encrypt_directory (fm_state_t c, const char *name)
1894 {
1895 PK_FILE_LIST list = NULL;
1896 WIN32_FIND_DATA findbuf;
1897 HANDLE hd;
1898 const char * s;
1899 char * patt = NULL, * p;
1900 int rc = 0;
1901
1902 if (!is_directory (name))
1903 return -1;
1904 patt = new char[strlen (name) + 4];
1905 if (!patt)
1906 BUG (NULL);
1907 strcpy (patt, name);
1908 strcat (patt, "\\*");
1909 hd = FindFirstFile (patt, &findbuf);
1910 if (!hd) {
1911 free_if_alloc (patt);
1912 return WPTERR_GENERAL;
1913 }
1914 if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) {
1915 p = make_filename( name, findbuf.cFileName, NULL );
1916 pk_list_add( &list, p );
1917 free_if_alloc( p );
1918 }
1919 while( FindNextFile( hd, &findbuf ) ) {
1920 if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) {
1921 p = make_filename( name, findbuf.cFileName, NULL );
1922 pk_list_add( &list, p );
1923 free_if_alloc( p );
1924 }
1925 }
1926 s = get_filesave_dlg (c->dlg, _("Choose a Name for the Archive"),
1927 NULL, default_dirname (name));
1928 if( !s ) {
1929 msg_box( c->dlg, _("Invalid archive name. Exit."), _("Encrypt Directory"), MB_ERR );
1930 rc = -1;
1931 goto leave;
1932 }
1933
1934 rc = pk_archiv_create( list, s );
1935 if( rc )
1936 msg_box( c->dlg, _("Could not create zip archive."), _("Encrypt Directory"), MB_ERR );
1937 else {
1938 fm_encrypt( c, s, 0 );
1939 remove( s );
1940 }
1941 leave:
1942 FindClose (hd);
1943 pk_list_free( list );
1944 free_if_alloc( patt );
1945 return rc;
1946 }
1947
1948
1949 static int CALLBACK
1950 fm_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby)
1951 {
1952 const char *a = 0;
1953 const char *b = 0;
1954
1955 switch ((int)sortby) {
1956 case FM_SORT_STAT:
1957 break;
1958 case FM_SORT_NAME:
1959 break;
1960 case FM_SORT_OP:
1961 break;
1962 }
1963 return stricmp (a, b);
1964 }
1965
1966
1967 /* Sort the list items from @lv with the mode given by @sortby. */
1968 int
1969 fm_sort (listview_ctrl_t lv, int sortby)
1970 {
1971 return listview_sort_items (lv, sortby, fm_cmp_cb);
1972 }
1973
1974
1975 /* Start the 'print md' dialog. Pass over the listview control
1976 @lv and the digest algo @mdalgo. */
1977 void
1978 fm_print_md (listview_ctrl_t lv, HWND dlg, int mdalgo)
1979 {
1980 struct md_file_s mdctx;
1981
1982 if (listview_count_items (lv, 0) == 0)
1983 return;
1984 memset (&mdctx, 0, sizeof (mdctx));
1985 mdctx.lv = lv;
1986 mdctx.mdalgo = mdalgo;
1987 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg,
1988 mdsum_dlg_proc, (LPARAM)&mdctx);
1989 }
1990
1991
1992 /* Send the selected file in @lv via MAPI to a mail recipient. */
1993 int
1994 fm_send_file (listview_ctrl_t lv)
1995 {
1996 char buf[128];
1997 int rc;
1998
1999 rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1);
2000 if (rc != -1)
2001 mapi_send_ascfile (buf);
2002 return 0;
2003 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26