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

Diff of /trunk/Src/wptFileManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 23 by twoaday, Fri Sep 30 10:10:16 2005 UTC
# Line 42  Line 42 
42  #include "wptGPGZIP.h"  #include "wptGPGZIP.h"
43  #include "wptUTF8.h"  #include "wptUTF8.h"
44  #include "wptRegistry.h"  #include "wptRegistry.h"
45    #include "wptImport.h"
46    
47  #include "openpgp.h"  #include "openpgp.h"
48    
 int  algo_from_list (gpgme_recipients_t rset, const char * keyid);  
49  void progress_cleanup (progress_filter_s * pfx);  void progress_cleanup (progress_filter_s * pfx);
50    
51    char* gpg_keylist_to_pattern (gpgme_key_t *rset);
52    gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf,
53                                     int prev_was_bad, int fd);
54    
55  /*-- wptFileVerifyDlg.cpp --*/  /*-- wptFileVerifyDlg.cpp --*/
56  int  file_verify_add_state (siglog_context_t c);  int  file_verify_add_state (siglog_context_t c);
57  void file_verify_use_event (void);  void file_verify_use_event (void);
# Line 57  static const char * mm_files[] = {".mov" Line 61  static const char * mm_files[] = {".mov"
61                                    ".mp3", ".wav", ".mid", ".wma",                                    ".mp3", ".wav", ".mid", ".wma",
62                                    ".gif", ".jpg", ".png", ".jpeg", ".dib", 0};                                    ".gif", ".jpg", ".png", ".jpeg", ".dib", 0};
63    
64    
65  char *  char *
66  fm_quote_file (const char * name)  fm_quote_file (const char * name)
67  {  {
# Line 80  overwrite_file (const char * fname) Line 85  overwrite_file (const char * fname)
85      int id;      int id;
86    
87      if (file_exist_check (fname))      if (file_exist_check (fname))
88          return 1;          return -1;
89      id = log_box (_("File Manager"), MB_YESNO,      id = log_box (_("File Manager"), MB_YESNO,
90                    _("\"%s\" already exists.\n"                    _("\"%s\" already exists.\n"
91                      "Replace existing file?"), fname);                      "Replace existing file?"), fname);
92      return id == IDNO ? 0 : 1;      return id == IDNO ? 0 : -1;
93  } /* overwrite_file */  } /* overwrite_file */
94    
95    
# Line 163  is_multi_media (const char * name) Line 168  is_multi_media (const char * name)
168    
169    
170  const char*  const char*
171  file_get_extension (gpgme_ctx_t ctx, gpgme_sigmode_t sigmode)  file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode)
172  {  {
173      int use_armor = (int)gpgme_control (ctx, GPGME_CTRL_ARMOR, -1);      int use_armor = gpgme_get_armor (ctx);
174    
175      if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR)      if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR)
176          return ".asc";          return ".asc";
# Line 221  fm_state_new (fm_state_t * ctx) Line 226  fm_state_new (fm_state_t * ctx)
226          BUG (0);          BUG (0);
227      memset (c, 0, sizeof * c);      memset (c, 0, sizeof * c);
228      rc = gpgme_new (&c->ctx);      rc = gpgme_new (&c->ctx);
     if (!rc)      
         rc = gpgme_recipients_new (&c->recp);  
229      if (rc)      if (rc)
230          BUG (0);          BUG (0);
231      gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION);      /* XXX rc = gpgme_recipients_new (&c->recp);*/
232        /* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */
233      *ctx = c;      *ctx = c;
234      return 0;      return 0;
235  } /* fm_state_new */  } /* fm_state_new */
236    
237    
238    /* Release the FM state handle @c. */
239  void  void
240  fm_state_release (fm_state_t c)  fm_state_release (fm_state_t c)
241  {  {
242      if (c)      if (!c)
243      {          return;
244          if (c->recp)      if (c->recp)
245          {          free (c->recp);
246              gpgme_recipients_release (c->recp);      if (c->ctx) {
247              c->recp = NULL;          gpgme_release (c->ctx);
248          }          c->ctx = NULL;  
         if (c->ctx)  
         {  
             gpgme_release (c->ctx);  
             c->ctx = NULL;  
         }  
         free_if_alloc (c->opaque);  
         free_if_alloc (c->output);  
         delete c; c = NULL;  
249      }      }
250  } /* fm_state_release */      free_if_alloc (c->opaque);
251        free_if_alloc (c->output);
252        delete c; c = NULL;    
253    }
254    
255  static int  static int
256  fm_check_for_entry( listview_ctrl_t lv, const char *file )  fm_check_for_entry( listview_ctrl_t lv, const char *file )
# Line 318  fm_add_dir_files (listview_ctrl_t lv, ch Line 317  fm_add_dir_files (listview_ctrl_t lv, ch
317  }  }
318    
319    
320    /* Add the drag & drop files from @dd_files to the
321       list view control @lv. */
322  int  int
323  fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files)  fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files)
324  {  {
325      char name[384+4];      char name[384+4];
326      int nfiles, rc, i;      int nfiles, rc, i;
327            
328      memset( name, 0, sizeof (name) );      memset (name, 0, sizeof (name));
329      nfiles = DragQueryFile( dd_files, 0xFFFFFFFF, NULL, 0 );      nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0);
330      for (i = 0;  i < nfiles; i++) {      for (i = 0;  i < nfiles; i++) {
331          DragQueryFile (dd_files, i, name, sizeof (name) -1);          DragQueryFile (dd_files, i, name, sizeof (name) -1);
332          if (is_directory (name))          if (is_directory (name))
# Line 334  fm_add_dropped_files (listview_ctrl_t lv Line 335  fm_add_dropped_files (listview_ctrl_t lv
335              rc = fm_set_ftype (lv, name);              rc = fm_set_ftype (lv, name);
336          if (rc == -1)          if (rc == -1)
337              break;              break;
           
338      }      }
339      return rc;      return rc;
340  } /* fm_add_dropped_files */  }
341    
342    
343  int  int
# Line 352  fm_add_opened_files (listview_ctrl_t lv, Line 352  fm_add_opened_files (listview_ctrl_t lv,
352      open.lStructSize = sizeof (OPENFILENAME);      open.lStructSize = sizeof (OPENFILENAME);
353      open.hInstance = glob_hinst;      open.hInstance = glob_hinst;
354      open.lpstrTitle = _("File Open");      open.lpstrTitle = _("File Open");
355      open.lpstrFilter = _("All Files (*.*)\0*.*");      open.lpstrFilter = _("All Files (*.*)\0*.*\0\0");
356      open.hwndOwner = dlg;      open.hwndOwner = dlg;
357      open.lpstrFile = file;      open.lpstrFile = file;
358      open.nMaxFile = sizeof (file) - 1;      open.nMaxFile = sizeof (file) - 1;
# Line 372  fm_add_opened_files (listview_ctrl_t lv, Line 372  fm_add_opened_files (listview_ctrl_t lv,
372      }      }
373            
374      return rc;      return rc;
375  } /* fm_add_opened_files */  }
376    
377    
378    /* Check the armor type of the file @fname and return
379       a string representation of it. */
380  static const char *  static const char *
381  fm_check_armor_type (const char * fname)  fm_check_armor_type (const char *fname)
382  {  {
383      FILE * fp;      FILE * fp;
384      char header[768], * p;      char header[768], * p;
# Line 406  fm_check_armor_type (const char * fname) Line 408  fm_check_armor_type (const char * fname)
408    
409  leave:  leave:
410      return "UNKNOWN";      return "UNKNOWN";
411  } /* fm_check_armor_type */  }
412    
413    
414  int  int
# Line 420  fm_assume_onepass_sig (const char * fnam Line 422  fm_assume_onepass_sig (const char * fnam
422    
423      if (!fname)      if (!fname)
424      {      {
425          gpgme_data_new_from_clipboard (&dat);          gpg_data_new_from_clipboard (&dat, 0);
426          gpgme_data_release_and_set_file (dat, "gpgme.tmp");          gpg_data_release_and_set_file (dat, "gpgme.tmp");
427    
428          fp = gpg_iobuf_open ("gpgme.tmp");          fp = gpg_iobuf_open ("gpgme.tmp");
429          if (!fp)          if (!fp)
# Line 461  is_floppy_disc (const char * fname) Line 463  is_floppy_disc (const char * fname)
463      drv[i++] = '\0';      drv[i++] = '\0';
464      i = GetDriveType (drv);      i = GetDriveType (drv);
465      if (i == DRIVE_REMOVABLE)      if (i == DRIVE_REMOVABLE)
466          return 1;          return -1;
467      return 0;      return 0;
468  }  }
469    
# Line 774  fm_parse_files (listview_ctrl_t lv, HWND Line 776  fm_parse_files (listview_ctrl_t lv, HWND
776              n++;              n++;
777          if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN)          if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN)
778              n++;              n++;
779          if (fm_cmd == FM_WIPE) {          if (fm_cmd == FM_WIPE)
             if (!confirm.rset)  
                 gpgme_recipients_new (&confirm.rset);  
             listview_get_item_text (lv, i, 1, fname, sizeof (fname)-1);  
             gpgme_recipients_add_name (confirm.rset, fname);  
780              ndel++;              ndel++;
         }  
781      }      }
782            
783      if (n > 1 && fm_cmd != FM_SYMENC)      if (n > 1 && fm_cmd != FM_SYMENC)
784          ctx->cache_cb = 1;          ctx->cache_cb = 1;
785    
786      if (fm_cmd == FM_WIPE && ndel > 0) {      if (fm_cmd == FM_WIPE && ndel > 0) {
787            memset (&confirm, 0, sizeof confirm);
788            confirm.lv_files = lv;
789          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg,          DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg,
790                              file_secdel_confirm_dlg_proc, (LPARAM)&confirm);                              file_secdel_confirm_dlg_proc, (LPARAM)&confirm);
791          if (!confirm.yes)          if (!confirm.yes)
# Line 853  fm_parse_files (listview_ctrl_t lv, HWND Line 852  fm_parse_files (listview_ctrl_t lv, HWND
852  leave:  leave:
853      if (!rc)      if (!rc)
854          fm_state_release (ctx);          fm_state_release (ctx);
     if (confirm.rset)  
         gpgme_recipients_release (confirm.rset);  
855      progress_cleanup (&pfx);      progress_cleanup (&pfx);
856      return rc;      return rc;
857  } /* fm_parse_files */  } /* fm_parse_files */
858    
859    
860    /* Wipe the given file @name with the delete mode
861       from the configuration.
862       Return value: 0 on success. */
863  int  int
864  fm_wipe (const char * name)  fm_wipe (const char *name)
865  {  {
866      int rc;      int rc;
867    
# Line 870  fm_wipe (const char * name) Line 870  fm_wipe (const char * name)
870      rc = secure_unlink (name, reg_prefs.wipe_mode);      rc = secure_unlink (name, reg_prefs.wipe_mode);
871      SetCursor (LoadCursor (NULL, IDC_ARROW));      SetCursor (LoadCursor (NULL, IDC_ARROW));
872      return rc;      return rc;
873  } /* fm_wipe */  }
874    
875    
876    /* Dump out the given PGP packets from file @name in a dialog. */
877  int  int
878  fm_list( const char * name, HWND dlg )  fm_list (const char *name, HWND dlg)
879  {  {
880      dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg,      dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg,
881                        file_stat_dlg_proc, (LPARAM)name, _("File Status"),                        file_stat_dlg_proc, (LPARAM)name, _("File Status"),
882                        IDS_WINPT_FILE_STAT );                        IDS_WINPT_FILE_STAT );
883      return 0;      return 0;
884  } /* fm_list */  }
885    
886    
887  static int  static int
888  ask_filename (fm_state_t c, const char * msg, char ** dst)  ask_filename (fm_state_t c, const char *msg, char **dst)
889  {  {
890      const char * s;      const char * s;
891    
# Line 897  ask_filename (fm_state_t c, const char * Line 898  ask_filename (fm_state_t c, const char *
898      c->output = m_strdup (s);      c->output = m_strdup (s);
899      if (!c->output)      if (!c->output)
900          BUG (0);          BUG (0);
901      *dst = fm_quote_file (s);      if (dst)
902            *dst = fm_quote_file (s);
903      return 0;      return 0;
904  }  }
905    
# Line 908  fm_encrypt (fm_state_t c, const char * n Line 910  fm_encrypt (fm_state_t c, const char * n
910      gpgme_error_t err;      gpgme_error_t err;
911      gpgme_key_t key = NULL;      gpgme_key_t key = NULL;
912      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
913      char * src = NULL, * dst = NULL;      file_data_t in=NULL, out=NULL;
914      char * keyid = NULL, ext[5];          char * keyid = NULL, ext[5];    
915      int no_compr = 0;      int no_compr = 0;
916      int rc = 0;      int rc = 0;
917            
918      src = fm_quote_file (name);    
919      c->output = new char[strlen (name) + 5 + 1];      c->output = new char[strlen (name) + 5 + 1];
920      if (!c->output)      if (!c->output)
921          BUG (0);          BUG (0);
922      strcpy (ext, file_get_extension (ctx, c->sigmode));      strcpy (ext, file_get_extension (ctx, c->sigmode));
923      strcpy (c->output, name );      strcpy (c->output, name);
924      strcat (c->output, ext );          strcat (c->output, ext);        
     dst = fm_quote_file (c->output);  
925            
926      if (!overwrite_file (c->output))      if (!overwrite_file (c->output)) {
927      {          rc = ask_filename (c, _("Enter filename for encrypted file"), NULL);
         rc = ask_filename (c, _("Enter filename for encrypted file"), &dst);  
928          if (rc)          if (rc)
929              goto leave;              goto leave;
930      }      }
931    
932        err = gpg_file_data_new (name, &in, 1);
933        if (err)
934            goto leave;
935        err = gpg_file_data_new (c->output, &out, 0);
936        if (err)
937            goto leave;
938            
939        /* XXX
940      no_compr = is_multi_media (name);      no_compr = is_multi_media (name);
941      gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr);      gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr);
942        */
943    
944      if (sign) {      if (sign) {
945          if (gpgme_signers_enum (ctx, 0) == NULL) {          if (gpgme_signers_enum (ctx, 0) == NULL) {
946              keyid = get_gnupg_default_key ();              keyid = get_gnupg_default_key ();
947              if (!keyid) {              if (!keyid) {
948                  msg_box (c->dlg, _("Could not get default secret key."),                  msg_box (c->dlg, _("Could not get default secret key."),
949                           _("Signing"), MB_ERR);                           _("Signing"), MB_ERR);
950                  rc = WPTERR_GENERAL;                  rc = WPTERR_GENERAL;
951                  goto leave;                  goto leave;
# Line 946  fm_encrypt (fm_state_t c, const char * n Line 955  fm_encrypt (fm_state_t c, const char * n
955              gpgme_signers_add (ctx, key);              gpgme_signers_add (ctx, key);
956          }          }
957          else {          else {
958              const char * s;              gpgme_key_t key = gpgme_signers_enum (ctx, 0);
959              s = (char *)gpgme_key_get_string_attr (gpgme_signers_enum (ctx, 0),              if (key && key->subkeys) {
960                                                      GPGME_ATTR_KEYID, NULL, 0);                  keyid = m_strdup (key->subkeys->keyid);
961              keyid = m_strdup (s);                  if (!keyid)
962                        BUG (NULL);
963                }
964          }          }
965          if (!c->init_cb || !c->cache_cb) {          if (!c->init_cb || !c->cache_cb) {
966              set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_SIGN,              set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN,
967                                     c->dlg, _("Signing"));                                     c->dlg, _("Signing"));
968              c->init_cb = 1;              c->init_cb = 1;
969          }          }
970          err = gpgme_op_file_sign_encrypt (ctx, c->recp, src, dst);          err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
971                                         in->dat, out->dat);
972          if (!c->cache_cb)          if (!c->cache_cb)
973              memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd));              memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd));
974          if (c->pass_cb.cancel) {          if (c->pass_cb.cancel) {
# Line 965  fm_encrypt (fm_state_t c, const char * n Line 977  fm_encrypt (fm_state_t c, const char * n
977          }          }
978          if (err) {          if (err) {
979              msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);              msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR);
980              if (err == GPGME_Bad_Passphrase)              if (err == gpg_error (GPG_ERR_BAD_PASSPHRASE))
981                  agent_del_cache (keyid);                  agent_del_cache (keyid);
982              rc = WPTERR_GENERAL;              rc = WPTERR_GENERAL;
983              goto leave;              goto leave;
# Line 973  fm_encrypt (fm_state_t c, const char * n Line 985  fm_encrypt (fm_state_t c, const char * n
985          gpgme_key_release (key);          gpgme_key_release (key);
986      }      }
987      else {      else {
988          err = gpgme_op_file_encrypt (ctx, c->recp, src, dst);          err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST,
989                                    in->dat, out->dat);
990          if (err) {          if (err) {
991              msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);              msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR);
992              rc = WPTERR_GENERAL;              rc = WPTERR_GENERAL;
# Line 984  fm_encrypt (fm_state_t c, const char * n Line 997  fm_encrypt (fm_state_t c, const char * n
997          secure_unlink (name, WIPE_MODE_SIMPLE);          secure_unlink (name, WIPE_MODE_SIMPLE);
998            
999  leave:  leave:
1000        if (in)
1001            gpg_file_data_release (in);
1002        if (out)
1003            gpg_file_data_release (out);
1004      free_if_alloc (keyid);      free_if_alloc (keyid);
     free_if_alloc (dst);  
     free_if_alloc (src);  
1005      return rc;      return rc;
1006  } /* fm_encrypt */  }
1007    
1008    
1009  int  int
1010  fm_sym_encrypt (fm_state_t c, const char * name)  fm_sym_encrypt (fm_state_t c, const char * name)
1011  {  {
     int rc = 0, cancel = 0;  
     char * src = NULL, * dst = NULL;  
     char ext[5], * pass;  
1012      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
1013      gpgme_error_t err;          gpgme_error_t err;    
1014        file_data_t in=NULL, out=NULL;
1015        int rc = 0, cancel = 0;
1016        char * src = NULL, * dst = NULL;
1017        char ext[5], * pass;    
1018            
1019      pass = request_passphrase2 (_("Symmetric"), 0, &cancel);      pass = request_passphrase2 (_("Symmetric"), 0, &cancel);
1020      if (cancel)      if (cancel)
1021          return 0;          return 0;
1022            
1023      gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);      /* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/
     src = fm_quote_file (name);  
1024      c->output = new char[strlen (name) + 5 + 1];      c->output = new char[strlen (name) + 5 + 1];
1025      if (!c->output)      if (!c->output)
1026          BUG (0);          BUG (0);
1027      strcpy (ext, file_get_extension (ctx, c->sigmode));      strcpy (ext, file_get_extension (ctx, c->sigmode));
1028      strcpy (c->output, name);      strcpy (c->output, name);
1029      strcat (c->output, ext);          strcat (c->output, ext);
     dst = fm_quote_file (c->output);  
1030    
1031      if (overwrite_file (c->output) == 0) {      if (overwrite_file (c->output) == 0) {
1032          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1033          goto leave;              goto leave;    
1034      }      }
1035      gpgme_set_passphrase (ctx, pass);  
1036      err = gpgme_op_file_encrypt (ctx, NULL, src, dst);      gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass);    
1037    
1038        err = gpg_file_data_new (name, &in, 1);
1039        if (err)
1040            goto leave;
1041        err = gpg_file_data_new (c->output, &out, 0);
1042        if (err)
1043            goto leave;
1044    
1045        err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in->dat, out->dat);
1046      if (err) {      if (err) {
1047          msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR);          msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR);
1048          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
# Line 1031  fm_sym_encrypt (fm_state_t c, const char Line 1054  fm_sym_encrypt (fm_state_t c, const char
1054      }      }
1055            
1056  leave:  leave:
1057      free_if_alloc (src);      if (in)
1058      free_if_alloc (dst);          gpg_file_data_release (in);
1059        if (out)
1060            gpg_file_data_release (out);
1061      sfree_if_alloc (pass);      sfree_if_alloc (pass);
1062      return rc;      return rc;
1063  } /* fm_sym_encrypt */  } /* fm_sym_encrypt */
1064    
1065    
1066  static gpgme_error_t  /* Decrypt the file @name. */
 fm_list_keys( const char * name, gpgme_recipients_t *r_keys )  
 {  
     return gpgme_op_list_keys( NULL, name, r_keys );  
 } /* fm_list_keys */  
   
   
1067  int  int
1068  fm_decrypt (fm_state_t c, const char * name)  fm_decrypt (fm_state_t c, const char *name)
1069  {  {
1070      gpgme_error_t err;      gpgme_error_t err;
1071      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
1072      gpgme_recipients_t keys = NULL;      gpgme_signature_t sig = NULL;
1073      gpgme_sig_t sig = NULL;      gpgme_decrypt_result_t res;
1074      gpgme_op_flags_t flags;      gpgme_verify_result_t sigres;
1075      char * src = NULL, * dst = NULL, keyid[17];      file_data_t in =NULL, out=NULL;
1076        char keyid[17];
1077      int is_signed = 0, sigok = 0;      int is_signed = 0, sigok = 0;
1078      int rc = 0;      int rc = 0;
1079            
1080      if (!c->init_cb || !c->cache_cb) {      if (!c->init_cb || !c->cache_cb) {
1081          set_gpg_passphrase_cb (c->ctx, &c->pass_cb, GPG_CMD_DECRYPT,          set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT,
1082                                 c->dlg, _("Decryption"));                                 c->dlg, _("Decryption"));
1083          c->init_cb = 1;          c->init_cb = 1;
1084      }          }    
1085            
     src = fm_quote_file (name);  
1086      c->output = m_strdup (name);      c->output = m_strdup (name);
1087      if (!c->output)      if (!c->output)
1088          BUG (0);          BUG (0);
1089      if (is_openpgp_ext (c->output))      if (is_openpgp_ext (c->output))
1090          c->output[strlen (c->output)-4] = '\0';          c->output[strlen (c->output)-4] = '\0';
1091      else {      else {
1092          const char *s = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Filename for Output"),          const char *s = get_filename_dlg (c->dlg, FILE_SAVE,
1093                                              _("Choose Filename for Output"),
1094                                            NULL, NULL);                                            NULL, NULL);
1095          if( s ) {          if( s ) {
1096              free_if_alloc( c->output );              free_if_alloc (c->output);
1097              c->output = m_strdup( s );              c->output = m_strdup (s);
1098              if( !c->output )              if (!c->output)
1099                  BUG( NULL );                  BUG (NULL);
1100          }          }
1101      }      }
     dst = fm_quote_file( c->output );  
   
     err = fm_list_keys( src, &keys );  
     if( err )  
         goto leave;  
     c->pass_cb.enc_to = keys;  
1102    
1103      if (overwrite_file (c->output) == 0) {      if (overwrite_file (c->output) == 0) {
1104          rc = ask_filename (c, _("Please enter filename for plaintext file"), &dst);          rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL);
1105          if (rc)          if (rc)
1106              goto leave;              goto leave;
1107      }          }    
1108      remove_crit_file_attrs( c->output, 0 );  
1109      err = gpgme_op_file_decrypt( ctx, src, dst );      remove_crit_file_attrs (c->output, 0);
1110      if( !c->cache_cb )  
1111          memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) );      err = gpg_file_data_new (name, &in, 1);
1112      if( c->pass_cb.cancel ) {      if (err)
1113            goto leave;
1114        err = gpg_file_data_new (c->output, &out, 0);
1115        if (err)
1116            goto leave;
1117    
1118        err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat);
1119        if (!c->cache_cb)
1120            memset (c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd));
1121        if (c->pass_cb.cancel) {
1122          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1123          goto leave;          goto leave;
1124      }      }
1125      gpgme_decrypt_get_status( ctx, keyid, &flags );  
1126      if( err == GPGME_No_Seckey && (flags & GPGME_OPFLAG_NOSECKEY) ) {      res = gpgme_op_decrypt_result (ctx);
1127          char * p = get_key_userid( keyid+8 );      if (res && err == gpg_error (GPG_ERR_NO_SECKEY)) {
1128          int pkalgo = algo_from_list( keys, keyid );          char *p = get_key_userid (keyid+8);
1129            gpgme_pubkey_algo_t pkalgo = (gpgme_pubkey_algo_t)0;
1130    
1131            if (res->recipients)
1132                pkalgo = res->recipients->pubkey_algo;
1133          log_box( _("Decryption"), MB_ERR,          log_box( _("Decryption"), MB_ERR,
1134                   _("Encrypted with %s key, ID %s.%s\n"                   _("Encrypted with %s key, ID %s.%s\n"
1135                     "Decryption failed: secret key not available."),                     "Decryption failed: secret key not available."),
1136                     gpgme_key_expand_attr( GPGME_ATTR_ALGO, pkalgo ),                     get_key_pubalgo (pkalgo), keyid+8, p);
                    keyid+8, p );  
1137          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1138          free_if_alloc( p );          free_if_alloc (p);
1139          goto leave;          goto leave;
1140      }      }
1141      else if( err ) {      else if (err) {
1142          msg_box( c->dlg, gpgme_strerror( err ), _("Decrypt"), MB_ERR );          msg_box( c->dlg, gpgme_strerror( err ), _("Decrypt"), MB_ERR );
1143          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1144          goto leave;          goto leave;
# Line 1122  fm_decrypt (fm_state_t c, const char * n Line 1148  fm_decrypt (fm_state_t c, const char * n
1148          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1149      }      }
1150            
1151      gpgme_decrypt_get_sig_ctx( ctx, &sig );      sigres = gpgme_op_verify_result (ctx);
1152            if (sigres) {
1153      sigok = gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_VALIDITY ) == GPGME_SIG_STAT_GOOD;          sig = sigres->signatures;
1154      if( sig ) {          sigok = sig->summary & GPGME_SIGSUM_GREEN;      
1155          const char *id, *s = sigok? _("Good signature") : _("BAD signature");          const char *s = sigok? _("Good signature") : _("BAD signature");
1156          int type = sigok? MB_OK: MB_ICONWARNING|MB_OK;          int type = sigok? MB_OK: MB_ICONWARNING|MB_OK;
1157          gpgme_key_t key;          gpgme_key_t key;
1158          const char * keyid = gpgme_sig_get_string_attr( sig, GPGME_ATTR_KEYID );          const char *keyid = sig->fpr;
1159          if( !keyid )          if (!keyid)
1160              keyid = "DEADBEEFDEADBEEF";              keyid = "????????????????";
         if( get_pubkey( keyid+8, &key ) )  
             log_box( _("Verify"), type, _("%s using keyID 0x%s"), s, keyid+8 );  
1161          else {          else {
1162              id = gpgme_sig_get_string_attr( sig, GPGME_ATTR_USERID );              keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24;
1163              log_box( _("Verify"), type, "%s using keyID 0x%08X from %s",  
1164                                  s, keyid, id? id : _("Invalid User ID") );          if (get_pubkey (keyid+8, &key))
1165                log_box (_("Verify"), type, _("%s using keyID 0x%s"), s, keyid+8);
1166            else
1167                log_box (_("Verify"), type, "%s using keyID 0x%08X from %s",
1168                                    s, keyid, _("no user ID"));
1169          }          }
1170      }      }
1171            
1172  leave:  leave:
1173      free_if_alloc( dst );      if (in)
1174      free_if_alloc( src );          gpg_file_data_release (in);
1175      gpgme_sig_release( sig );      if (out)
1176      gpgme_recipients_release( keys );          gpg_file_data_release (out);
1177      return rc;      return rc;
1178  } /* fm_decrypt */  }
1179    
1180    
1181  int  int
# Line 1156  fm_sign (fm_state_t c, const char * name Line 1184  fm_sign (fm_state_t c, const char * name
1184      int rc = 0;      int rc = 0;
1185      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
1186      gpgme_error_t err;      gpgme_error_t err;
1187      char *src = NULL, *dst = NULL;      file_data_t in=NULL, out=NULL;
1188      char ext[5];      char ext[5];
1189    
1190      if( !c->init_cb || !c->cache_cb ) {      if( !c->init_cb || !c->cache_cb ) {
1191          set_gpg_passphrase_cb( c->ctx, &c->pass_cb, GPG_CMD_SIGN, c->dlg, _("Signing") );          set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") );
1192          c->init_cb = 1;          c->init_cb = 1;
1193      }      }
1194            
1195      src = fm_quote_file( name );      free_if_alloc (c->output);
     free_if_alloc( c->output );  
1196      c->output = new char[strlen( name ) + 5 + 1];      c->output = new char[strlen( name ) + 5 + 1];
1197      if( !c->output )      if( !c->output)
1198          BUG( NULL );          BUG( NULL );
1199      strcpy( ext, file_get_extension( ctx, c->sigmode ) );      strcpy( ext, file_get_extension( ctx, c->sigmode ) );
1200      strcpy( c->output, name );      strcpy( c->output, name );
1201      strcat( c->output, ext );      strcat( c->output, ext );
     dst = fm_quote_file( c->output );  
1202            
1203      if (!overwrite_file (c->output)) {      if (!overwrite_file (c->output)) {
1204          rc = ask_filename (c, _("Enter filename for signed file"), &dst);          rc = ask_filename (c, _("Enter filename for signed file"), NULL);
1205          if (rc)          if (rc)
1206              goto leave;              goto leave;
1207      }      }
1208      remove_crit_file_attrs( c->output, 0 );      remove_crit_file_attrs( c->output, 0 );
1209      err = gpgme_op_file_sign( ctx, c->sigmode, src, dst );  
1210        err = gpg_file_data_new (name, &in, 1);
1211        if (err)
1212            goto leave;
1213        err = gpg_file_data_new (c->output, &out, 0);
1214        if (err)
1215            goto leave;
1216    
1217        err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode);
1218      if( !c->cache_cb )      if( !c->cache_cb )
1219          memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) );          memset( c->pass_cb.pwd, 0, sizeof (c->pass_cb.pwd) );
1220      if( c->pass_cb.cancel ) {      if( c->pass_cb.cancel ) {
# Line 1194  fm_sign (fm_state_t c, const char * name Line 1228  fm_sign (fm_state_t c, const char * name
1228      }      }
1229    
1230  leave:  leave:
1231      free_if_alloc( src );      if (in)
1232      free_if_alloc( dst );          gpg_file_data_release (in);
1233        if (out)
1234            gpg_file_data_release (out);
1235      return rc;      return rc;
1236  } /* fm_sign */  } /* fm_sign */
1237    
# Line 1204  static int Line 1240  static int
1240  fm_add_sig_stat( siglog_context_t log )  fm_add_sig_stat( siglog_context_t log )
1241  {  {
1242      gpgme_key_t key;          gpgme_key_t key;    
1243      const char *uid, *keyid, * kid;      const char *kid;
1244      int not_found = 0;      int not_found = 0;
1245    
1246      kid = gpgme_sig_get_string_attr( log->sig, GPGME_ATTR_KEYID );      kid = log->sig->fpr;
1247      if( !kid )      if (!kid)
1248          kid = "DEADBEEFDEADBEEF";          kid = "????????????????";
1249      if( strlen( kid ) == 16 )      if (strlen (kid) == 40)
1250          keyid = kid + 8;          kid += 32;
1251      else if( strlen( kid ) == 32 )      else
1252          keyid = kid;          kid += 24;
1253      else if( strlen( kid ) == 40 )      if (get_pubkey (kid, &key))
         keyid = kid + 24;  
     if( get_pubkey( keyid, &key ) )  
1254          not_found = 1;          not_found = 1;
1255      log->use_uid = 0;      log->use_uid = 0;
1256      if( !not_found ) {      if (!not_found) {
1257          uid = gpgme_key_get_string_attr( key, GPGME_ATTR_USERID, NULL, 0 );          log->user_id = key->uids->uid;
         log->user_id = uid;  
1258          log->use_uid = 1;                log->use_uid = 1;      
1259      }      }
1260      file_verify_add_state( log );      file_verify_add_state (log);
1261    
1262      return 0;      return 0;
1263  } /* fm_add_sig_stat */  } /* fm_add_sig_stat */
# Line 1299  fm_verify_pasted_detsig (listview_ctrl_t Line 1332  fm_verify_pasted_detsig (listview_ctrl_t
1332    
1333    
1334  int  int
1335  fm_verify( fm_state_t c, int detached, const char *name )  fm_verify (fm_state_t c, int detached, const char *name)
1336  {  {
1337      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
1338      gpgme_error_t err;      gpgme_error_t err;
1339      gpgme_sig_t sig;      gpgme_signature_t s;
1340      struct siglog_context_s log;          gpgme_verify_result_t res;
1341      char * src = NULL;      struct siglog_context_s log;
1342        file_data_t in=NULL, out=NULL;
1343      int rc = 0;      int rc = 0;
     size_t i;  
1344                    
1345      if( detached ) {      if (detached) {
1346          const char *file = NULL;          const char *file = NULL;
1347          if( strstr( name, ".sig" ) || strstr( name, ".asc" ) ) {          if (strstr (name, ".sig") || strstr (name, ".asc")) {
1348              char fname[512];              char fname[512];
1349              _snprintf( fname, sizeof (fname) - 1, "%s", name );              _snprintf (fname, sizeof (fname) - 1, "%s", name);
1350              fname[strlen( fname ) - 4] = '\0';              fname[strlen (fname) - 4] = '\0';
1351              if( file_exist_check( fname ) == 0 )              if (file_exist_check (fname) == 0)
1352                  file = fname;                  file = fname;
1353          }                }      
1354          if( !file )          if (!file)
1355              file = get_filename_dlg( c->dlg, FILE_OPEN, _("Select Data File"), NULL, NULL );              file = get_filename_dlg (c->dlg, FILE_OPEN, _("Select Data File"), NULL, NULL);
1356          if( file ) {          if (file) {
1357              free_if_alloc( c->output );              free_if_alloc (c->output);
1358              c->output = m_strdup( file );              c->output = m_strdup (file);
1359          }          }
1360          else {          else {
1361              msg_box( c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR );              msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR);
1362              return WPTERR_GENERAL;              return WPTERR_GENERAL;
1363          }          }
1364          c->sigmode = GPGME_SIG_MODE_DETACH;          c->sigmode = GPGME_SIG_MODE_DETACH;
1365      }      }
1366      else {      else {
1367          if( strstr( name, ".asc" ) )              if (strstr (name, ".asc"))
1368              c->sigmode = GPGME_SIG_MODE_CLEAR;              c->sigmode = GPGME_SIG_MODE_CLEAR;
1369          else          else
1370              c->sigmode = GPGME_SIG_MODE_NORMAL;              c->sigmode = GPGME_SIG_MODE_NORMAL;
1371      }      }
1372    
1373      memset( &log, 0, sizeof (log) );      memset (&log, 0, sizeof (log));
1374      strcpy( log.file, name );      strcpy (log.file, name); /* XXX: check bounds */
1375      file_verify_create_dlg();      file_verify_create_dlg ();
1376      src = fm_quote_file( name );  
1377            err = gpg_file_data_new (name, &in, 1);
1378      err = gpgme_op_file_verify( ctx, c->sigmode, &sig, src, c->output );      if (err)
1379      if( err == GPGME_Bad_Signature ) {          goto leave;
1380          log.sig = sig;      err = gpg_file_data_new (c->output, &out, 0);
1381          fm_add_sig_stat( &log );      if (err)
1382            goto leave;
1383    
1384        if (c->sigmode == GPGME_SIG_MODE_DETACH)
1385            err = gpgme_op_verify (ctx, in->dat, NULL, out->dat);
1386        else
1387            err = gpgme_op_verify (ctx, in->dat, out->dat, NULL);
1388        
1389        res = gpgme_op_verify_result (ctx);
1390        if (err == gpg_error (GPG_ERR_BAD_SIGNATURE)) {
1391            log.sig = res->signatures;
1392            fm_add_sig_stat (&log);
1393          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1394          goto leave;          goto leave;
1395      }      }
1396      if( err ) {      if (err) {
1397          msg_box( c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR );          msg_box (c->dlg, gpgme_strerror( err ), _("Verify"), MB_ERR);
1398          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1399          goto leave;          goto leave;
1400        }    
1401    
1402        
1403        for (s=res->signatures; s; s=s->next) {
1404            log.sig = s;
1405            fm_add_sig_stat (&log);
1406      }      }
1407      for( i = 0; i < gpgme_sig_get_ulong_attr( sig, 0, GPGME_ATTR_LEVEL ); i++ ) {      if (!c->output)
         gpgme_sig_t _sig;  
         _sig = (gpgme_sig_t)gpgme_sig_get_ulong_attr( sig, i, GPGME_ATTR_OPAQUE );  
         log.sig = _sig;  
         fm_add_sig_stat( &log );  
     }  
     if( !c->output )  
1408          c->output = m_strdup( name ); /* for later use */          c->output = m_strdup( name ); /* for later use */
1409    
1410  leave:  leave:
1411      free_if_alloc( src );      if (in)
1412            gpg_file_data_release (in);
1413        if (out)
1414            gpg_file_data_release (out);
1415      return rc;      return rc;
1416  } /* fm_verify */  } /* fm_verify */
1417    
1418    
1419  int  int
1420  fm_import( fm_state_t c, const char *name )  fm_import (fm_state_t c, const char *name)
1421  {  {
1422      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
1423      gpgme_error_t err;      gpgme_error_t err;
1424      char *src = NULL;      gpgme_import_result_t res;
1425      int import_res[14] = {0};      file_data_t keydata = NULL;
1426      int rc = 0;      int rc = 0;
1427    
1428      free_if_alloc( c->output );      free_if_alloc (c->output);
1429      c->output = m_strdup( name );      c->output = m_strdup (name);
1430      if( !c->output )      if (!c->output)
1431          BUG( NULL );          BUG (NULL);
1432      src = fm_quote_file( name );  
1433      err = gpgme_op_file_import( ctx, NULL, src );      err = gpg_file_data_new (name, &keydata, 1);
1434      if( err ) {      if (err)
1435          msg_box( c->dlg, gpgme_strerror( err ), _("Import"), MB_ERR );          goto leave;
1436          rc = WPTERR_GENERAL;  
1437        err = gpgme_op_import (ctx, keydata->dat);
1438        if (err) {
1439            msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR);
1440            rc = WPTERR_GENERAL;
1441          goto leave;          goto leave;
1442      }      }
1443      gpgme_get_import_status( ctx, import_res, NULL );  
1444      print_import_status( import_res, c->implist_revcert );      res = gpgme_op_import_result (ctx);
1445      if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0  ) {      print_import_status (res);
1446          msg_box( c->dlg, _("Key without a self signature was dectected!\n"            if (res->no_user_id > 0) {
1447            msg_box (c->dlg, _("Key without a self signature was dectected!\n"      
1448                             "(This key is NOT usable for encryption, etc)\n"                             "(This key is NOT usable for encryption, etc)\n"
1449                             "\n"                             "\n"
1450                             "Cannot import these key(s)!"), _("Import"), MB_INFO );                             "Cannot import these key(s)!"), _("Import"), MB_INFO);
1451      }      }
1452    
1453  leave:  leave:
1454      free_if_alloc( src );      if (keydata)
1455            gpg_file_data_release (keydata);
1456      return rc;      return rc;
1457  } /* fm_import */  } /* fm_import */
1458    
1459    
1460    /* Export the selected keys from the File Manager to a file. */
1461  int  int
1462  fm_export( fm_state_t c )  fm_export (fm_state_t c)
1463  {  {
1464      int rc = 0, id = 0;      int rc = 0, id = 0;
1465      gpgme_ctx_t ctx = c->ctx;      gpgme_ctx_t ctx = c->ctx;
1466      gpgme_error_t err;      gpgme_error_t err;
1467      gpgme_recipients_t rset = c->recp;      gpgme_key_t *rset = c->recp;
1468        file_data_t keydata = NULL;
1469      const char *name, *s = NULL;      const char *name, *s = NULL;
1470      char *p = NULL, *dst = NULL;      char *p = NULL, *patt = NULL;
     void *recp;  
1471    
1472      if( !gpgme_recipients_count( rset ) ) {      if (!rset || !rset[0]) {
1473          msg_box( c->dlg, _("No key was selected for export."), _("Export"), MB_ERR );          msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR);
1474          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
1475          goto leave;          goto leave;
1476      }      }
1477    
1478      if( gpgme_recipients_count( rset ) == 1 ) {      if (rset[1] == NULL) { /* count == 1*/
1479          err = gpgme_recipients_enum_open( rset, &recp );          gpgme_key_t k = rset[0];
1480          if( err )          const char *s = k->uids->name;
1481              BUG( NULL );          p = new char[strlen (s)+1+8];
1482          s = gpgme_recipients_enum_read( rset, &recp );          if (!p)
1483          gpgme_recipients_enum_close( rset, &recp );              BUG (NULL);
1484          p = new char[strlen( s )+1+8];          strcpy (p, s );
1485          if( !p )          strcat (p, ".asc");
             BUG( NULL );  
         strcpy( p, s );  
         strcat( p, ".asc" );  
1486      }      }
1487    
1488      name = get_filename_dlg( c->dlg, FILE_SAVE, _("Choose Name for Key File"), NULL, p? p : NULL );      name = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Name for Key File"),
1489                                 NULL, p? p : NULL);
1490                                                            
1491      if( !name )      if (!name)
1492          name = "keys.gpg";          name = "keys.gpg";
1493    
1494      dst = fm_quote_file( name );      patt = gpg_keylist_to_pattern (rset);
1495      err = gpgme_op_file_export( ctx, rset, dst );  
1496      if( err ) {      err = gpg_file_data_new (name, &keydata, 0);
1497          msg_box( c->dlg, gpgme_strerror( err ), _("Export"), MB_ERR );      if (err)
1498          rc = WPTERR_GENERAL;          goto leave;
1499    
1500        err = gpgme_op_export (ctx, patt, 0, keydata->dat);
1501        if (err) {
1502            msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR);
1503            rc = WPTERR_GENERAL;
1504          goto leave;              goto leave;    
1505      }      }
1506      log_box( _("GnuPG status"), MB_OK, _("Finished (Output: %s)"),  name );      log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"),  name);
1507    
1508  leave:  leave:
1509      free_if_alloc( dst );      if (keydata)
1510      free_if_alloc( p );          gpg_file_data_release (keydata);
1511        if (patt)
1512            free (patt);
1513        free_if_alloc (p);
1514                    
1515      return rc;      return rc;
1516  } /* fm_export */  }
1517    
1518    
1519  int  int
# Line 1472  fm_parse_command_line (char *cmdl) Line 1532  fm_parse_command_line (char *cmdl)
1532      ctx->cache_cb = 1;              ctx->cache_cb = 1;        
1533            
1534      p = cmdl;      p = cmdl;
1535      if( p && *p > 32 && !memistr( p, strlen( p ), "winpt.exe" )      if( p && *p > 32 && !stristr( p, "winpt.exe" )
1536                       && !strstr( p, "--" ) ) {                       && !strstr( p, "--" ) ) {
1537          count++;          count++;
1538          if (*p == '"') { /* need to remove quotes */          if (*p == '"') { /* need to remove quotes */
# Line 1569  fm_encrypt_directory( fm_state_t c, cons Line 1629  fm_encrypt_directory( fm_state_t c, cons
1629          rc = -1;          rc = -1;
1630          goto leave;          goto leave;
1631      }      }
     if( !overwrite_file( s ) ) {  
         rc = -1;  
         goto leave;  
     }  
1632    
1633      rc = pk_archiv_create( list, s );      rc = pk_archiv_create( list, s );
1634      if( rc )      if( rc )

Legend:
Removed from v.22  
changed lines
  Added in v.23

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26