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

Diff of /trunk/Src/wptKeyManager.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 23  Line 23 
23  #include <io.h>  #include <io.h>
24    
25  #include "../resource.h"  #include "../resource.h"
26    #include "w32gpgme.h"
27  #include "wptTypes.h"  #include "wptTypes.h"
28  #include "wptW32API.h"  #include "wptW32API.h"
29  #include "wptVersion.h"  #include "wptVersion.h"
30  #include "wptCommonCtl.h"  #include "wptCommonCtl.h"
31  #include "wptNLS.h"  #include "wptNLS.h"
32  #include "wptErrors.h"  #include "wptErrors.h"
 #include "wptGPG.h"  
33  #include "wptContext.h"  #include "wptContext.h"
34    #include "wptGPG.h"
35  #include "wptKeylist.h"  #include "wptKeylist.h"
36  #include "wptFileManager.h"  #include "wptFileManager.h"
37  #include "wptDlgs.h"  #include "wptDlgs.h"
# Line 38  Line 39 
39  #include "wptKeyManager.h"  #include "wptKeyManager.h"
40  #include "wptKeylist.h"  #include "wptKeylist.h"
41  #include "wptHTTP.h"  #include "wptHTTP.h"
42    #include "wptKeyEdit.h"
43    #include "wptImport.h"
44    
45    
46    /* Return a user friendly key representation in @buf from
47       the key given by @keyid. */
48  static void  static void
49  km_get_clip_info (const char *uid, char *buf, size_t buflen)  key_get_clip_info (const char *keyid, char *buf, size_t buflen)
50  {  {
51      gpgme_key_t pk;      gpgme_key_t pk;
     unsigned long a, algo;  
52    
53      if (get_pubkey (uid, &pk))      if (get_pubkey (keyid, &pk))
54          BUG (0);          BUG (0);  
       
     a = gpgme_key_get_ulong_attr (pk, GPGME_ATTR_CREATED, NULL, 0);  
     algo = gpgme_key_get_ulong_attr (pk, GPGME_ATTR_ALGO, NULL, 0);  
55      _snprintf (buf, buflen-1,      _snprintf (buf, buflen-1,
56                 "pub %04d%s/%s %s %s\r\n"                 "pub %04d%s/%s %s %s\r\n"
57                 "    Primary key fingerprint: %s\r\n",                   "    Primary key fingerprint: %s\r\n",
58          gpgme_key_get_ulong_attr (pk, GPGME_ATTR_LEN, NULL, 0),                 pk->subkeys->length,
59          gpgme_key_expand_attr (GPGME_ATTR_ALGO_SHORT, algo),                 get_key_pubalgo (pk->subkeys->pubkey_algo),
60          gpgme_key_get_string_attr (pk, GPGME_ATTR_KEYID, NULL, 0) + 8,                   pk->subkeys->keyid+8,
61          gpgme_key_expand_attr (GPGME_ATTR_CREATED, a ),                 get_key_created (pk->subkeys->timestamp),
62          gpgme_key_get_string_attr (pk, GPGME_ATTR_USERID, NULL, 0),                 pk->uids->uid,
63          get_key_fpr (pk));                 get_key_fpr (pk));
64  }  }
65    
66    
67    /* Quoted the user-id given by @uid. If @uid is already
68       quoted @uid is returned without any modifications.
69       Return value: quoted @uid. */
70  char*  char*
71  km_quote_uid (const char *uid)  km_quote_uid (const char *uid)
72  {      {    
73      char * q = new char[strlen (uid) + 4];      char *q;
74    
75        if (*uid == '"' && uid[strlen (uid)-1] == '"')
76            return m_strdup (uid);
77        q = new char[strlen (uid) + 4];
78      if (!q)      if (!q)
79          BUG (NULL);          BUG (NULL);
80      _snprintf (q, strlen (uid) + 3, "\"%s\"", uid);      _snprintf (q, strlen (uid) + 3, "\"%s\"", uid);
81      return q;      return q;
82  } /* km_quote_uid */  }
83    
84    
85  int  int
# Line 100  int Line 108  int
108  km_check_if_protected (listview_ctrl_t lv, int pos)  km_check_if_protected (listview_ctrl_t lv, int pos)
109  {  {
110      gpgme_key_t key;      gpgme_key_t key;
111        winpt_key_s k;
112    
113      key = (gpgme_key_t)listview_get_item2 (lv, pos);      key = (gpgme_key_t)listview_get_item2 (lv, pos);
114      if (key == NULL)      if (!key)
115          return 1; /* assume yes */          return 1; /* assume yes */
116      return gpgme_key_get_ulong_attr (key, GPGME_ATTR_IS_PROTECTED, NULL, 0);      winpt_get_pubkey (key->subkeys->keyid, &k);
117        return k.is_protected;
118  } /* km_check_if_protected */  } /* km_check_if_protected */
119    
120    
# Line 139  km_get_key_status (listview_ctrl_t lv, i Line 149  km_get_key_status (listview_ctrl_t lv, i
149      key = (gpgme_key_t)listview_get_item2 (lv, pos);      key = (gpgme_key_t)listview_get_item2 (lv, pos);
150      if (key == NULL)      if (key == NULL)
151          return 0;          return 0;
152      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_EXPIRED, NULL, 0))  
153        if (key->expired)
154          flags |= KM_FLAG_EXPIRED;          flags |= KM_FLAG_EXPIRED;
155      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_REVOKED, NULL, 0))      if (key->revoked)
156          flags |= KM_FLAG_REVOKED;          flags |= KM_FLAG_REVOKED;
157      if (gpgme_key_get_ulong_attr (key, GPGME_ATTR_KEY_DISABLED, NULL, 0))      if (key->disabled)
158          flags |= KM_FLAG_DISABLED;          flags |= KM_FLAG_DISABLED;
159      return flags;      return flags;
160  } /* km_get_key_status */  } /* km_get_key_status */
161    
162    
163    /* Interface to enable or disable a key (@enable = 1 then enable).
164       The key is retrieved from a list control @lv at the pos @pos. */
165  int  int
166  km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable)  km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable)
167  {  {
168      gpgme_ctx_t ctx;      GpgKeyEdit *ke;
     gpgme_editkey_t ek;  
169      gpgme_error_t err;      gpgme_error_t err;
     int edit_id;  
170      char keyid[32];      char keyid[32];
171    
172      listview_get_item_text (lv, pos, 1, keyid, DIM (keyid)-1);      listview_get_item_text (lv, pos, 1, keyid, DIM (keyid)-1);
173      err = gpgme_editkey_new (&ek);  
174      if (err)      ke = new GpgKeyEdit (keyid);
175        if (!ke)
176          BUG (NULL);          BUG (NULL);
177      if (enable) {  
178          gpgme_editkey_enable_set (ek);      if (enable)
179          edit_id = GPGME_EDITKEY_ENABLE;          err = ke->enable ();
180        else
181            err = ke->disable ();  
182        if (!err) {
183            show_msg (dlg, 1500, _("Key status changed."));
184            keycache_set_reload (1); /* XXX: set update flag */
185      }      }
186      else {      else
187          gpgme_editkey_disable_set (ek);          msg_box (dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR);
188          edit_id = GPGME_EDITKEY_DISABLE;      delete ke;
189        return err? WPTERR_GENERAL : 0;
190    }
191    
192    
193    
194    /* Create a string that contain all keyids from
195       the key list @rset separated by a space. */
196    char*
197    gpg_keylist_to_pattern (gpgme_key_t *rset)
198    {
199        int i, n=0;
200        char *p;
201    
202        for (i=0; rset[i] != NULL; i++)
203            n++;
204        p = (char *)calloc (1, n*(8+1)+n);
205        if (!p)
206            return NULL;
207        for (i=0; rset[i] != NULL; i++) {
208            strcat (p, rset[i]->subkeys->keyid);
209            strcat (p, " ");
210      }      }
211        return p;
212    }
213    
214    
215    /* Export the keys given in @rset to the clipboard.
216       Return value: 0 on success. */
217    gpgme_error_t
218    gpg_clip_export (gpgme_key_t *rset)
219    {
220        gpgme_error_t err = 0;
221        gpgme_ctx_t ctx = NULL;
222        gpgme_data_t keydata = NULL;
223        char *patt=NULL;
224        
225      err = gpgme_new (&ctx);      err = gpgme_new (&ctx);
226      if( err )      if (err)
227          BUG( NULL );          return err;
228      gpgme_set_edit_ctx (ctx, ek, edit_id);      gpgme_set_armor (ctx, 1);          
229      err = gpgme_op_editkey (ctx, keyid);      err = gpgme_data_new (&keydata);
230      if( !err ) {      if (err)
231          show_msg( dlg, 1500, _("Key status changed.") );          goto leave;
232          keycache_set_reload( 1 );  
233        patt = gpg_keylist_to_pattern (rset);
234        if (!patt) {
235            err = gpg_error (GPG_ERR_ENOMEM);
236            goto leave;
237      }      }
     else  
         msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );  
238    
239        err = gpgme_op_export (ctx, patt, 0, keydata);
240        if (err)
241            goto leave;
242    
243        gpg_data_release_and_set_clipboard (keydata, 1);
244    
245    leave:
246        if (patt)
247            free (patt);
248      gpgme_release (ctx);      gpgme_release (ctx);
249      gpgme_editkey_release (ek);      return err;
250      return err? WPTERR_GENERAL : 0;  }
 } /* km_enable_disable_key */  
251    
252    
253    /* Export the selected keys in @lv to the clipboard. */
254  int  int
255  km_clip_export (HWND dlg, listview_ctrl_t lv)  km_clip_export (HWND dlg, listview_ctrl_t lv)
256  {  {
257      gpgme_error_t err;      gpgme_error_t err;
258      gpgme_recipients_t rset;      gpgme_key_t *rset;
259      int rc, id = 0, n = 0;      int rc, id = 0, n = 0;
260            
261      rset = keylist_enum_recipients (lv, KEYLIST_LIST);      rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n);
     n = gpgme_recipients_count (rset);  
262      if (!n) {      if (!n) {
263          msg_box (dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR);          msg_box (dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR);
264          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
265          goto leave;          goto leave;
266      }      }
267            
268      err = gpgme_op_clip_export (rset);      err = gpg_clip_export (rset);
269      if (err) {      if (err) {
270          msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);          msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
271          rc = WPTERR_GENERAL;          rc = WPTERR_GENERAL;
272          goto leave;          goto leave;
273      }      }
274      if (n == 1) {      if (n == 1) {
         const char *s;  
275          char buf[256];          char buf[256];
276          s = gpgme_recipients_get_name (rset, 0);          key_get_clip_info (rset[0]->subkeys->keyid, buf, 255);
         km_get_clip_info (s, buf, 255);  
277          set_clip_text2 (NULL, buf, strlen (buf), 0);          set_clip_text2 (NULL, buf, strlen (buf), 0);
278      }      }
279    
280      show_msg (dlg, 1500, _("GnuPG Status: Finished"));      show_msg (dlg, 1500, _("GnuPG Status: Finished"));
281            
282  leave:  leave:
283      gpgme_recipients_release (rset);      free (rset);
284      return rc;      return rc;
285  } /* km_clip_export */  } /* km_clip_export */
286    
287    
288    /* Export the selected secret key from @lv into @fname.
289       It is only allowed to export a single secret key. */
290  int  int
291  km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname)  km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname)
292  {  {
293      gpgme_recipients_t rset;      gpgme_key_t *rset;
     gpgme_data_t keydata;  
294      gpgme_error_t err;      gpgme_error_t err;
295      gpgme_ctx_t ctx;      int n = 0;
     size_t n = 0;  
296    
297      rset = keylist_enum_recipients (lv, KEYLIST_LIST);      rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n);
     n = gpgme_recipients_count (rset);  
298      if (!n) {      if (!n) {
299          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );
300          return WPTERR_GENERAL;          return WPTERR_GENERAL;
301      }      }
302      if (n > 1) {      if (n > 1) {
303          msg_box( dlg, _("Only one secret key can be exported."), _("Key Manager"), MB_ERR );          msg_box (dlg, _("Only one secret key can be exported."), _("Key Manager"), MB_ERR);
304            free (rset);
305          return 0; /* we checked this before, so we just quit */          return 0; /* we checked this before, so we just quit */
306      }      }
     err = gpgme_data_new( &keydata );  
     if( err )  
         BUG( dlg );  
     err = gpgme_new( &ctx );  
     if( err )  
         BUG( dlg );  
     gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);  
     gpgme_control (ctx, GPGME_CTRL_WITH_SECRET_KEY, 1);  
     gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);  
307    
308      err = gpgme_op_export( ctx, rset, keydata );      err = gpg_export_seckey (rset[0]->subkeys->keyid, fname);
309      if( err ) {      if (err)
310          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
311          goto leave;      else
312      }          log_box (_("Key Manager"), MB_OK,
313                     _("Secret key successfully saved in '%s'."), fname);
314    
     log_box( _("Key Manager"), MB_OK,  
              _("Secret key successfully saved in '%s'."), fname );  
       
 leave:  
     err = gpgme_data_release_and_set_file( keydata, fname );  
     if( err )  
         log_box( _("Key Manager"), MB_OK,  
                  _("Could not save data to '%s'."), fname );  
     gpgme_release( ctx );  
315    
316      return (int)err;      free (rset);
317  } /* km_privkey_export */      return err? WPTERR_GENERAL : 0;
318    }
319    
320    
321  int  int
322  km_file_export (HWND dlg, listview_ctrl_t lv, const char * fname)  km_file_export (HWND dlg, listview_ctrl_t lv, const char * fname)
323  {  {
324      gpgme_recipients_t rset;      gpgme_key_t *rset;
325      gpgme_data_t keydata;            gpgme_data_t keydata;      
326      gpgme_error_t err;      gpgme_error_t err;
327      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
328        char *patt;
329        int n;
330    
331      rset = keylist_enum_recipients( lv, KEYLIST_LIST );      rset = keylist_enum_recipients( lv, KEYLIST_LIST, &n );
332      if( !gpgme_recipients_count( rset ) ) {      if (!n) {
333          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );
334          return WPTERR_GENERAL;          return WPTERR_GENERAL;
335      }      }
336            
337      err = gpgme_data_new( &keydata );      err = gpgme_data_new (&keydata);
338      if( err )      if( err )
339          BUG( dlg );          BUG( dlg );
340      err = gpgme_new( &ctx );      err = gpgme_new (&ctx);
341      if( err )      if( err )
342          BUG( dlg );          BUG( dlg );
343      gpgme_control (ctx, GPGME_CTRL_ARMOR, 1);      gpgme_set_armor (ctx, 1);
344      gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);  
345        /*gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);*/
346        patt = gpg_keylist_to_pattern (rset);
347            
348      err = gpgme_op_export( ctx, rset, keydata );      err = gpgme_op_export( ctx, patt, 0, keydata);
349      if( err ) {      if( err ) {
350          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );
351          goto leave;          goto leave;
# Line 307  km_file_export (HWND dlg, listview_ctrl_ Line 355  km_file_export (HWND dlg, listview_ctrl_
355               _("Key(s) successfully saved in '%s'."), fname );               _("Key(s) successfully saved in '%s'."), fname );
356            
357  leave:  leave:
358      err = gpgme_data_release_and_set_file( keydata, fname );      err = gpg_data_release_and_set_file( keydata, fname );
359      if( err )      if (err)
360          log_box( _("Key Manager"), MB_OK,          log_box (_("Key Manager"), MB_OK,
361                   _("Could not save data to '%s'."), fname );                       _("Could not save data to '%s'."), fname);
362      gpgme_release( ctx );      gpgme_release (ctx);
363        free (patt);
364      return (int)err;      return (int)err;
365  } /* km_file_export */  } /* km_file_export */
366    
# Line 323  extract_dash_escaped_key (void) Line 371  extract_dash_escaped_key (void)
371      gpgme_data_t inp, plain;      gpgme_data_t inp, plain;
372      gpgme_error_t err;      gpgme_error_t err;
373    
374      err = gpgme_data_new_from_clipboard (&inp);      err = gpg_data_new_from_clipboard (&inp, 0);
375      if (err) {      if (err) {
376          msg_box (NULL, gpgme_strerror( err ), _("Key Manager"), MB_ERR);          msg_box (NULL, gpgme_strerror( err ), _("Key Manager"), MB_ERR);
377          return -1;          return -1;
378      }      }
379      gpgme_data_extract_plaintext (inp, &plain);      gpg_data_extract_plaintext (inp, &plain);
380        gpg_data_release_and_set_clipboard (plain, 0);
381      gpgme_data_release (inp);      gpgme_data_release (inp);
     gpgme_data_release_and_set_clipboard (plain);  
382    
383      return 0;      return 0;
384  } /* extract_dash_escaped_key */  } /* extract_dash_escaped_key */
# Line 355  km_clip_import( HWND dlg ) Line 403  km_clip_import( HWND dlg )
403          msg_box( dlg, _("No valid OpenPGP data found."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("No valid OpenPGP data found."), _("Key Manager"), MB_ERR );
404          return WPTERR_GENERAL;          return WPTERR_GENERAL;
405      }      }
406      if( !(pgptype & GPGME_PGP_PUBKEY) && !(pgptype & GPGME_PGP_SECKEY) ) {      if( !(pgptype & PGP_PUBKEY) && !(pgptype & PGP_SECKEY) ) {
407          msg_box( dlg, _("No valid OpenPGP keys found."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("No valid OpenPGP keys found."), _("Key Manager"), MB_ERR );
408          return WPTERR_GENERAL;          return WPTERR_GENERAL;
409      }      }
410      if( pgptype & GPGME_PGP_DASH_ESCAPED ) {      if( pgptype & PGP_DASH_ESCAPED ) {
411          id = msg_box( dlg, _("The key you want to import is dash escacped.\n"          id = msg_box( dlg, _("The key you want to import is dash escacped.\n"
412                               "Do you want to extract the key?"),                               "Do you want to extract the key?"),
413                        _("Key Manager"), MB_YESNO );                        _("Key Manager"), MB_YESNO );
# Line 422  km_http_import (HWND dlg, const char * u Line 470  km_http_import (HWND dlg, const char * u
470  }  }
471    
472    
473    /* Import a key from the given file @fname.
474       On success an import statistics dialog is shown. */
475  int  int
476  km_file_import (HWND dlg, const char * fname)  km_file_import (HWND dlg, const char *fname)
477  {  {
478      gpgme_data_t keydata = NULL;      gpgme_data_t keydata = NULL;
479      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
480      gpgme_error_t err;          gpgme_error_t err;    
481      gpgme_recipients_t keys = NULL;      gpgme_key_t *keys = NULL;
482      fm_state_s fm_stat;      fm_state_s fm_stat;
483      int import_res[14];      gpgme_import_result_t res;
484            
485      memset (&fm_stat, 0, sizeof (fm_stat));      memset (&fm_stat, 0, sizeof (fm_stat));
486      fm_stat.opaque = m_strdup (fname);      fm_stat.opaque = m_strdup (fname);
# Line 446  km_file_import (HWND dlg, const char * f Line 496  km_file_import (HWND dlg, const char * f
496      err = gpgme_new( &ctx );      err = gpgme_new( &ctx );
497      if( err )      if( err )
498          BUG( dlg );          BUG( dlg );
499      gpgme_control( ctx, GPGME_CTRL_FORCETRUST, 1 );      /*XXX gpgme_control( ctx, GPGME_CTRL_FORCETRUST, 1 );*/
500      err = gpgme_data_new_from_file (&keydata, fname);      err = gpgme_data_new_from_file (&keydata, fname, 1);
501      if( err ) {      if( err ) {
502          msg_box( dlg, _("Could not read key-data from file."), _("Key Manager"), MB_ERR );          msg_box( dlg, _("Could not read key-data from file."), _("Key Manager"), MB_ERR );
503          goto leave;          goto leave;
504      }      }
505            
506      err = gpgme_op_import( ctx, NULL, keydata );      err = gpgme_op_import (ctx, keydata);
507      if( err ) {      if (err) {
508          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );
509          goto leave;          goto leave;
510      }      }
511    
512      gpgme_get_import_status (ctx, import_res, &keys);      res = gpgme_op_import_result (ctx);
513      print_import_status (import_res, fm_stat.implist_revcert);      
514      if (import_res[GPGME_IMPSTAT_NOSELFSIG] > 0) {      print_import_status (res);
515        if (res->no_user_id > 0) {
516          msg_box (dlg, _("Key without a self signature was dectected!\n"          msg_box (dlg, _("Key without a self signature was dectected!\n"
517                          "(This key is NOT usable for encryption, etc)\n"                          "(This key is NOT usable for encryption, etc)\n"
518                          "\n"                              "\n"    
# Line 469  km_file_import (HWND dlg, const char * f Line 520  km_file_import (HWND dlg, const char * f
520      }      }
521    
522  leave:  leave:
523      gpgme_recipients_release (keys);      free (keys);
524      gpgme_data_release (keydata);      gpgme_data_release (keydata);
525      gpgme_release (ctx);      gpgme_release (ctx);
526      free_if_alloc (fm_stat.opaque);      free_if_alloc (fm_stat.opaque);
527      return (int)err;      return (int)err;
528  } /* km_file_import */  }
529    
530    
531    /* Mark the keys in @rset as deleted in the keycache. */
532  static void  static void
533  delete_keys_from_cache (gpgme_recipients_t rset)  delete_keys_from_cache (gpgme_key_t *rset)
534  {  {
535      gpgme_keycache_t pub = keycache_get_ctx (1);      gpgme_keycache_t pub = keycache_get_ctx (1);
536      void *ctx =NULL;      int i=0;
     const char *s;  
537    
538      gpgme_recipients_enum_open (rset, &ctx);      while (rset[i++] != NULL)
539      while ((s = gpgme_recipients_enum_read (rset, &ctx)))          gpgme_keycache_delete_key (pub, rset[i]->subkeys->keyid);
540          gpgme_keycache_delete_key (pub, s);  }
     gpgme_recipients_enum_close (rset, &ctx);  
 } /* delete_keys_from_cache */  
541    
542    
543  int  int
544  km_delete_keys (listview_ctrl_t lv, HWND dlg)  km_delete_keys (listview_ctrl_t lv, HWND dlg)
545  {  {
546      gpgme_error_t err;      gpgme_error_t err;
547      gpgme_recipients_t rset;      gpgme_ctx_t ctx;
548        gpgme_key_t *rset, k;
549      char keyid[32], uid[256], date[64], keylen[64];          char keyid[32], uid[256], date[64], keylen[64];    
550      int with_seckey=0, seckey_type=0, confirm=0;      int with_seckey=0, seckey_type=0, confirm=0;
551      int i, rc, n;      int i, rc, n, k_pos=0;
552            
553      if (listview_get_curr_pos (lv) == -1) {      if (listview_get_curr_pos (lv) == -1) {
554          msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);          msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
# Line 514  km_delete_keys (listview_ctrl_t lv, HWND Line 564  km_delete_keys (listview_ctrl_t lv, HWND
564      }      }
565      else      else
566          confirm = 1;          confirm = 1;
567      err = gpgme_recipients_new (&rset);  
     if (err)  
         BUG (0);  
568      n = listview_count_items (lv, 0);      n = listview_count_items (lv, 0);
569        rset = (gpgme_key_t *)calloc (n+1, sizeof (gpgme_key_t));
570        if (!rset)
571            BUG (NULL);
572      for( i = 0; i < n; i++ ) {      for( i = 0; i < n; i++ ) {
573          if( listview_get_item_state( lv, i ) ) {          if( listview_get_item_state( lv, i ) ) {
574              listview_get_item_text( lv, i, 0, uid, sizeof uid - 1 );              listview_get_item_text( lv, i, 0, uid, sizeof uid - 1 );
# Line 530  km_delete_keys (listview_ctrl_t lv, HWND Line 581  km_delete_keys (listview_ctrl_t lv, HWND
581                                _("Do you really want to delete this key?\n\n"                                _("Do you really want to delete this key?\n\n"
582                                  "pub %s %s %s\n"                                  "pub %s %s %s\n"
583                                  "  \"%s\""), keylen, keyid, date, uid );                                  "  \"%s\""), keylen, keyid, date, uid );
584                  if (rc == IDYES)                  if (rc == IDYES) {
585                      gpgme_recipients_add_name(rset, keyid);                      get_pubkey (keyid, &k);
586                        rset[k_pos++] = k;
587                    }
588                  with_seckey = 0;                  with_seckey = 0;
589              }              }
590              else if (confirm) {              else if (confirm) {
# Line 547  km_delete_keys (listview_ctrl_t lv, HWND Line 600  km_delete_keys (listview_ctrl_t lv, HWND
600                          msg_box( dlg, _("The actual secret key is stored on a smartcard.\n"                          msg_box( dlg, _("The actual secret key is stored on a smartcard.\n"
601                                          "Only the public key and the secret key \n"                                          "Only the public key and the secret key \n"
602                                          "placeholder will be deleted.\n"), _("Key Manager"), MB_OK );                                          "placeholder will be deleted.\n"), _("Key Manager"), MB_OK );
603                      gpgme_recipients_add_name( rset, keyid );                      get_pubkey (keyid, &k);
604                        rset[k_pos++] = k;
605                  }                  }
606                  with_seckey = 1;                  with_seckey = 1;
607              }              }
608              else {              else {
609                  with_seckey = 1;                  with_seckey = 1;
610                  gpgme_recipients_add_name (rset, keyid);                  get_pubkey (keyid, &k);
611                    rset[k_pos++] = k;
612              }              }
613          }          }
614      }      }
615            
616      if (!gpgme_recipients_count (rset)) {      if (k_pos == 0) {
617          gpgme_recipients_release (rset);          free (rset);
618          return 0;          return 0;
619      }      }
620        
621      err = gpgme_op_delete_keys (rset, with_seckey);      err = gpgme_new (&ctx);
622      if (err) {      if (err)
623          if (err == GPGME_Invalid_Key)          BUG (NULL);
624              msg_box (dlg, _("No such key."), _("Key Manager"), MB_INFO);      for (i=0; i < n; i++) {    
625          else if (err == GPGME_Conflict)          err = gpgme_op_delete (ctx, rset[i], with_seckey);
626              msg_box (dlg, _("Must delete secret key first."), _("Key Manager"), MB_INFO);          if (err)
627          else              msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
628              msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);          else
629          return FALSE;              show_msg (dlg, 1500, _("GnuPG Status: Finished"));
630      }      }
631      show_msg (dlg, 1500, _("GnuPG Status: Finished"));      gpgme_release (ctx);
632      listview_del_items (lv);      listview_del_items (lv);
633      delete_keys_from_cache (rset);      delete_keys_from_cache (rset);
634      gpgme_recipients_release (rset);      free (rset);
635    
636      return (int)err;      return (int)err;
637  } /* km_delete_keys */  } /* km_delete_keys */
# Line 613  km_send_to_keyserver (listview_ctrl_t lv Line 668  km_send_to_keyserver (listview_ctrl_t lv
668  int  int
669  km_send_to_mail_recipient( listview_ctrl_t lv, HWND dlg )  km_send_to_mail_recipient( listview_ctrl_t lv, HWND dlg )
670  {  {
671    #if 0 /*FIXME*/
672      gpgme_key_t key;      gpgme_key_t key;
673      gpgme_ctx_t ctx=NULL;      gpgme_ctx_t ctx=NULL;
674      gpgme_recipients_t rset=NULL;      gpgme_recipients_t rset=NULL;
# Line 633  km_send_to_mail_recipient( listview_ctrl Line 689  km_send_to_mail_recipient( listview_ctrl
689      listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 );      listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 );
690      if( get_pubkey( keyid, &key ) )      if( get_pubkey( keyid, &key ) )
691          BUG( NULL );          BUG( NULL );
692      s = gpgme_key_get_string_attr( key, GPGME_ATTR_NAME, NULL, 0 );      s = key->uids->name;
693      GetTempPath (sizeof tmp-1, tmp);      GetTempPath (sizeof tmp-1, tmp);
694      strncat (tmp, s, sizeof tmp-200);      strncat (tmp, s, sizeof tmp-200);
695      strncat (tmp, ".asc", sizeof tmp-200);      strncat (tmp, ".asc", sizeof tmp-200);
# Line 645  km_send_to_mail_recipient( listview_ctrl Line 701  km_send_to_mail_recipient( listview_ctrl
701      if( !rc )      if( !rc )
702          rc = gpgme_new( &ctx );          rc = gpgme_new( &ctx );
703      if( !rc ) {      if( !rc ) {
704          gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );          gpgme_set_armor (ctx, 1);
705          rc = gpgme_op_file_export( ctx, rset, p );          rc = gpgme_op_file_export( ctx, rset, p );
706      }      }
707      if( rc )      if( rc )
# Line 656  km_send_to_mail_recipient( listview_ctrl Line 712  km_send_to_mail_recipient( listview_ctrl
712      gpgme_recipients_release( rset );      gpgme_recipients_release( rset );
713      gpgme_release( ctx );          gpgme_release( ctx );    
714      return rc;      return rc;
715    #endif
716        return 0;
717  }  }
718    
719    
# Line 712  km_refresh_from_keyserver (listview_ctrl Line 770  km_refresh_from_keyserver (listview_ctrl
770    
771    
772  void  void
773  km_set_clip_info( const char *uid )  km_set_clip_info (const char *uid)
774  {      {    
775      char buf[256];      char buf[256];
776            
777      km_get_clip_info (uid, buf, 255);          key_get_clip_info (uid, buf, 255);    
778      set_clip_text( NULL, buf, strlen( buf ) );      set_clip_text (NULL, buf, strlen (buf));
779  } /* km_set_clip_info */  } /* km_set_clip_info */
780    
781    
782    
783    /* Return TRUE if the key in the list @lv at pos @pos is an
784       old version 3 key. */
785  int  int
786  km_key_is_v3 (listview_ctrl_t lv, int pos)  km_key_is_v3 (listview_ctrl_t lv, int pos)
787  {  {
# Line 731  km_key_is_v3 (listview_ctrl_t lv, int po Line 791  km_key_is_v3 (listview_ctrl_t lv, int po
791      listview_get_item_text (lv, pos, 1, keyid, sizeof keyid-1);      listview_get_item_text (lv, pos, 1, keyid, sizeof keyid-1);
792      if (get_pubkey (keyid, &pk))      if (get_pubkey (keyid, &pk))
793          BUG (NULL);          BUG (NULL);
794      return gpgme_key_get_ulong_attr (pk, GPGME_ATTR_VERSION, NULL, 0);      if (strlen (pk->subkeys->fpr) == 32 &&
795  } /* km_key_is_v3 */          pk->subkeys->pubkey_algo == GPGME_PK_RSA)
796            return -1;
797        return 0;
798    }
799    
800    
801  void  void
802  km_update_default_key_str (HWND dlg, int * ret_len)  km_update_default_key_str (HWND dlg)
803  {  {
804      char * keyid, defkeyinf[512];      char * keyid, defkeyinf[512];
805      const char * fmt;      const char * fmt;
# Line 749  km_update_default_key_str (HWND dlg, int Line 812  km_update_default_key_str (HWND dlg, int
812          fmt = _("Default Key: %s");          fmt = _("Default Key: %s");
813      else      else
814          fmt = _("Default Key: 0x%s");          fmt = _("Default Key: 0x%s");
815      _snprintf( defkeyinf, sizeof defkeyinf - 1, fmt, keyid );      _snprintf (defkeyinf, sizeof defkeyinf - 1, fmt, keyid);
816      SetWindowText( dlg, defkeyinf );      SendMessage (dlg, SB_SETTEXT, 0, (LPARAM)defkeyinf);
817      *ret_len = strlen( defkeyinf );      free_if_alloc (keyid);
     free_if_alloc( keyid );  
818  } /* km_return_default_key_str */  } /* km_return_default_key_str */
819    
820    
821  void  void
822  km_complete_status_bar( HWND sb, listview_ctrl_t lv, int startpos )  km_complete_status_bar (HWND sb, listview_ctrl_t lv)
823  {  {
824      char text[384];      char txt_sec[128], txt_pub[128];
825      int nkeys = 0, nsec = 0, i;      int nkeys = 0, nsec = 0, i;
826    
827      GetWindowText( sb, text, sizeof text -1 );      nkeys = listview_count_items (lv, 0);
828      nkeys = listview_count_items( lv, 0 );      for (i = 0; i < nkeys; i++) {
829      for( i = 0; i < nkeys; i++ ) {          if (km_check_for_seckey (lv, i, NULL))
         if( km_check_for_seckey( lv, i, NULL ) )  
830              nsec++;              nsec++;
831      }      }
832      _snprintf( text+startpos, sizeof text-1, "                %d keys (%d secret keys)", nkeys, nsec );      _snprintf (txt_sec, sizeof (txt_sec)-1, "%d secret keys", nsec);
833      SetWindowText( sb, text );      _snprintf (txt_pub, sizeof (txt_pub)-1, "%d keys", nkeys);
834        SendMessage (sb, SB_SETTEXT, 1, (LPARAM)txt_sec);
835        SendMessage (sb, SB_SETTEXT, 2, (LPARAM)txt_pub);
836  } /* km_complete_status_bar */  } /* km_complete_status_bar */
837    
838    
839    
840  int  int
841  km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos)  km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos)
842  {  {
843      gpgme_error_t err;      GpgKeyEdit *ke;
844      gpgme_ctx_t ctx;      gpgme_error_t err;    
     gpgme_editkey_t ek;  
845      char keyid[32];      char keyid[32];
846    
847      listview_get_item_text (lv, pos, 1, keyid, 31);      listview_get_item_text (lv, pos, 1, keyid, 31);
     err = gpgme_new (&ctx);  
     if (err)  
         BUG (0);  
     err = gpgme_editkey_new (&ek);  
     if (err)  
         BUG (0);  
     gpgme_set_edit_ctx (ctx, ek, GPGME_EDITKEY_TRUST);  
     gpgme_editkey_trust_set (ek, 5);  
848    
849      err = gpgme_op_editkey (ctx, keyid);      ke = new GpgKeyEdit (keyid);
850        if (!ke)
851            BUG (0);
852    
853        err = ke->setTrust (GPGME_VALIDITY_ULTIMATE);
854      if (err)      if (err)
855          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
856      else      else
857          show_msg (dlg, 1500, _("GnuPG Status: Finished"));          show_msg (dlg, 1500, _("GnuPG Status: Finished"));
858    
859      gpgme_release (ctx);      delete ke;
     gpgme_editkey_release (ek);  
860      return (int)err;      return (int)err;
861  }  }
862    
# Line 838  km_dump_key (gpgme_key_t key) Line 895  km_dump_key (gpgme_key_t key)
895  {  {
896  #if _DEBUG  #if _DEBUG
897      log_box ("DEBUG", MB_OK,      log_box ("DEBUG", MB_OK,
898               "%d %d %s %d\n%s",               "%d %d %s %d\n%s", key->subkeys->length,
899               gpgme_key_get_ulong_attr (key, GPGME_ATTR_LEN, NULL, 0),               key->subkeys->pubkey_algo,
900               gpgme_key_get_ulong_attr (key, GPGME_ATTR_ALGO, NULL, 0),               key->subkeys->keyid,
901               gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0),               key->subkeys->timestamp,
902               gpgme_key_get_ulong_attr (key, GPGME_ATTR_CREATED, NULL, 0),               key->uids->uid);
              gpgme_key_get_string_attr (key, GPGME_ATTR_USERID, NULL, 0));  
903  #endif  #endif
904  }  }
905    
906    #if 0
907  gpg_optfile_t  gpg_optfile_t
908  km_groupdb_open( void )  km_groupdb_open( void )
909  {        {      
# Line 893  km_groupdb_expand_recipients( const char Line 949  km_groupdb_expand_recipients( const char
949      for( mbr = grp->list; mbr; mbr = mbr->next ) {      for( mbr = grp->list; mbr; mbr = mbr->next ) {
950          if( gpgme_keycache_find_key( kc, mbr->name, 0, &pk ) )          if( gpgme_keycache_find_key( kc, mbr->name, 0, &pk ) )
951              BUG( NULL );              BUG( NULL );
952          n = gpgme_key_count_items( pk, GPGME_ATTR_USERID );          n = count_userids (pk);
953          while( n-- ) {          while( n-- ) {
954              const char * s = gpgme_key_get_string_attr( pk, GPGME_ATTR_USERID, NULL, n );              gpgme_user_id_t u = get_nth_userid (pk, n);
955                const char * s =  u->uid;
956              if( s && stristr( s, mbr->name )              if( s && stristr( s, mbr->name )
957                  && gpgme_key_get_ulong_attr( pk, GPGME_ATTR_VALIDITY, NULL, n ) < 3 )                  && u->validity < 3 )
958                  no_trust++;                  no_trust++;
959          }          }
960      }      }
# Line 1104  km_groups_del( km_group_t gc ) Line 1161  km_groups_del( km_group_t gc )
1161      else      else
1162          return km_groups_del_main( gc );          return km_groups_del_main( gc );
1163  } /* km_groups_del */  } /* km_groups_del */
1164    #endif

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26