/[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 19 by twoaday, Fri May 20 08:39:15 2005 UTC revision 164 by twoaday, Fri Jan 20 09:19:15 2006 UTC
# Line 1  Line 1 
1  /* wptKeyManager.cpp - Handy functions for the Key Manager dialog  /* wptKeyManager.cpp - Handy functions for the Key Manager dialog
2   *      Copyright (C) 2001-2005 Timo Schulz   *      Copyright (C) 2001-2006 Timo Schulz
3   *   *      Copyright (C) 2005 g10 Code GmbH
4   * This file is part of WinPT.   *
5   *   * This file is part of WinPT.
6   * WinPT is free software; you can redistribute it and/or   *
7   * modify it under the terms of the GNU General Public License   * WinPT is free software; you can redistribute it and/or
8   * as published by the Free Software Foundation; either version 2   * modify it under the terms of the GNU General Public License
9   * of the License, or (at your option) any later version.   * as published by the Free Software Foundation; either version 2
10   *     * of the License, or (at your option) any later version.
11   * WinPT is distributed in the hope that it will be useful,   *  
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * WinPT is distributed in the hope that it will be useful,
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * General Public License for more details.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   *   * General Public License for more details.
16   * You should have received a copy of the GNU General Public License   *
17   * along with WinPT; if not, write to the Free Software Foundation,   * You should have received a copy of the GNU General Public License
18   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   * along with WinPT; if not, write to the Free Software Foundation,
19   */   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  #include <windows.h>   */
21  #include <commctrl.h>  
22  #include <stdio.h>  #ifdef HAVE_CONFIG_H
23  #include <io.h>  #include <config.h>
24    #endif
25  #include "../resource.h"  
26  #include "wptTypes.h"  #include <windows.h>
27  #include "wptW32API.h"  #include <commctrl.h>
28  #include "wptVersion.h"  #include <stdio.h>
29  #include "wptCommonCtl.h"  
30  #include "wptNLS.h"  #include "gpgme.h"
31  #include "wptErrors.h"  #include "resource.h"
32  #include "wptGPG.h"  #include "wptTypes.h"
33  #include "wptContext.h"  #include "wptW32API.h"
34  #include "wptKeylist.h"  #include "wptVersion.h"
35  #include "wptFileManager.h"  #include "wptCommonCtl.h"
36  #include "wptDlgs.h"  #include "wptNLS.h"
37  #include "wptKeyserver.h"  #include "wptErrors.h"
38  #include "wptKeyManager.h"  #include "wptContext.h"
39  #include "wptKeylist.h"  #include "wptGPG.h"
40  #include "wptHTTP.h"  #include "wptKeylist.h"
41    #include "wptFileManager.h"
42    #include "wptDlgs.h"
43  static void  #include "wptKeyserver.h"
44  km_get_clip_info (const char *uid, char *buf, size_t buflen)  #include "wptKeyManager.h"
45  {  #include "wptKeylist.h"
46      gpgme_key_t pk;  #include "wptHTTP.h"
47      unsigned long a, algo;  #include "wptKeyEdit.h"
48    #include "wptImport.h"
49      if (get_pubkey (uid, &pk))  #include "wptCrypto.h"
50          BUG (0);  #include "wptUTF8.h"
51        #include "wptGPGME.h"
52      a = gpgme_key_get_ulong_attr (pk, GPGME_ATTR_CREATED, NULL, 0);  
53      algo = gpgme_key_get_ulong_attr (pk, GPGME_ATTR_ALGO, NULL, 0);  
54      _snprintf (buf, buflen-1,  /* Macros to change the cursor */
55                 "pub %04d%s/%s %s %s\r\n"  #define op_begin()  SetCursor (LoadCursor (NULL, IDC_WAIT))
56                 "    Primary key fingerprint: %s\r\n",    #define op_end()    SetCursor (LoadCursor (NULL, IDC_ARROW))
57          gpgme_key_get_ulong_attr (pk, GPGME_ATTR_LEN, NULL, 0),  
58          gpgme_key_expand_attr (GPGME_ATTR_ALGO_SHORT, algo),  
59          gpgme_key_get_string_attr (pk, GPGME_ATTR_KEYID, NULL, 0) + 8,    /* Return a user friendly key representation in @buf of
60          gpgme_key_expand_attr (GPGME_ATTR_CREATED, a ),     the key given by @keyid. */
61          gpgme_key_get_string_attr (pk, GPGME_ATTR_USERID, NULL, 0),  static void
62          get_key_fpr (pk));  key_get_clip_info (const char *keyid, char *buf, size_t buflen)
63  }  {
64        gpgme_key_t pk;
65        char *uid;
66  char*  
67  km_quote_uid (const char * uid)      if (get_pubkey (keyid, &pk))
68  {              BUG (NULL);
69      char * q = new char[strlen (uid) + 4];      uid = utf8_to_wincp2 (pk->uids->uid);
70      if (!q)      _snprintf (buf, buflen-1,
71          BUG (NULL);                 "pub %04d%s/%s %s %s\r\n"
72      _snprintf (q, strlen (uid) + 3, "\"%s\"", uid);                 "    Primary key fingerprint: %s\r\n",
73      return q;                 pk->subkeys->length,
74  } /* km_quote_uid */                 get_key_pubalgo2 (pk->subkeys->pubkey_algo),
75                   pk->subkeys->keyid+8,
76                   get_key_created (pk->subkeys->timestamp),
77  int                 uid,
78  km_check_for_seckey (listview_ctrl_t lv, int pos, int * utrust)                 get_key_fpr (pk));
79  {      safe_free (uid);
80      char t[32], t2[64];  }
81      int type = 0;  
82        
83      listview_get_item_text (lv, pos, 5, t, sizeof (t) - 1);  /* Return a general description of the key @key. */
84      listview_get_item_text (lv, pos, 2, t2, sizeof (t2) - 1);  char*
85      if (!strcmp (t2, "pub/sec"))  km_key_get_info (gpgme_key_t pk, int is_sec)
86          type = 1;  {
87      else if (!strcmp (t2, "pub/crd"))      const char *fmt = "%s %04d%s/0x%s %s\n  \"%s\"";
88          type = 2;      char *p, *uid;
89      if (stristr (t, "ultimate") && utrust)      int n;
90          *utrust = 1;  
91      return type;      n = strlen (fmt) + 8 + 2 + 16 + 12 + strlen (pk->uids->uid) + 32;
92  } /* km_check_for_seckey */      p = new char[n+1];
93        if (!p)
94            BUG (NULL);
95  int      uid = utf8_to_wincp2 (pk->uids->uid);
96  km_check_if_protected( listview_ctrl_t lv, int pos )      _snprintf (p, n, fmt, is_sec? "sec" : "pub",
97  {                 pk->subkeys->length,
98      gpgme_key_t key;                 get_key_pubalgo2 (pk->subkeys->pubkey_algo),
99      char keyid[32];                     pk->subkeys->keyid+8, get_key_created (pk->subkeys->timestamp),
100                       uid);
101      listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 );      safe_free (uid);
102      if( get_pubkey( keyid, &key ) )      return p;
103          BUG( NULL );      }
104      return gpgme_key_get_ulong_attr( key, GPGME_ATTR_IS_PROTECTED, NULL, 0 );  
105  } /* km_check_if_protected */  
106    #if 0
107    /* Quoted the user-id given by @uid. If @uid is already
108  int     quoted @uid is returned without any modifications.
109  km_check_key_status (listview_ctrl_t lv, int pos)     Return value: quoted @uid. */
110  {  char*
111      char t[128];  km_quote_uid (const char *uid)
112      int i = 1;  {    
113            char *q;
114      listview_get_item_text( lv, pos, 5, t, sizeof t - 1 );  
115      if( t[0] == '[' && t[1] == ']' )      if (*uid == '"' && uid[strlen (uid)-1] == '"')
116          return 1;          return m_strdup (uid);
117      for( i = 0; t[i] != ']'; i++ ) {      q = new char[strlen (uid) + 4];
118          if( t[i] == 'E' )      if (!q)
119              msg_box(lv->ctrl, _("This key has expired!\n"          BUG (NULL);
120                                  "Key check failed."), _("Key Manager"), MB_ERR );      _snprintf (q, strlen (uid) + 3, "\"%s\"", uid);
121          else if( t[i] == 'R' )      return q;
122              msg_box(lv->ctrl, _("This key has been revoked by its owner!\n"  }
123                                  "Key check failed."), _("Key Manager"), MB_ERR );  #endif
124      }  
125    
126      return 0;  /* Check if list view @lv contains a secret key at position @pos.
127  } /* km_check_key_status */     If utrust is valid, set it to 1 if the key is valid -1 otherwise.
128       Return value: 1 normal key, 2 smart card key. */
129    int
130  int  km_check_for_seckey (listview_ctrl_t lv, int pos, int *utrust)
131  km_get_key_status( listview_ctrl_t lv, int pos )  {
132  {      gpgme_key_t key;
133      char t[128];      winpt_key_s sk;
134      int i, flags = 0;      int type = 0;
135        
136      if( pos == -1 )      key = (gpgme_key_t)listview_get_item2 (lv, pos);
137          return 0;      if (!key)
138      listview_get_item_text( lv, pos, 5, t, sizeof t-1 );          BUG (NULL);
139      for( i = 0; t[i] != ']'; i++ ) {      if (utrust)
140          if( t[i] == 'E' )          *utrust = 0;
141              flags |= KM_FLAG_EXPIRED;      memset (&sk, 0, sizeof (sk));
142          if( t[i] == 'R' )      if (!winpt_get_seckey (key->subkeys->keyid+8, &sk))
143              flags |= KM_FLAG_REVOKED;          type = 1;
144          if( t[i] == 'D' )      if (sk.ext && sk.ext->gloflags.divert_to_card)
145              flags |= KM_FLAG_DISABLED;          type = 2;
146      }      if (utrust && (key->expired || key->revoked))
147      return flags;          *utrust = -1;
148  } /* km_get_key_status */      else if (utrust && key->owner_trust == GPGME_VALIDITY_ULTIMATE)
149            *utrust = 1;
150        return type;
151  int  }
152  km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable)  
153  {  
154      gpgme_ctx_t ctx;  /* Check if the key at position @pos is protected with a passwd. */
155      gpgme_editkey_t ek;  int
156      gpgme_error_t err;  km_check_if_protected (listview_ctrl_t lv, int pos)
157      int edit_id;  {
158      char keyid[32];      gpgme_key_t key;
159        winpt_key_s k;
160      listview_get_item_text( lv, pos, 1, keyid, DIM (keyid)-1 );  
161      err = gpgme_editkey_new( &ek );      key = (gpgme_key_t)listview_get_item2 (lv, pos);
162      if( err )      if (!key)
163          BUG( NULL );          return 1; /* assume yes */
164      if( enable ) {      winpt_get_pubkey (key->subkeys->keyid, &k);
165          gpgme_editkey_enable_set (ek);      return k.is_protected;
166          edit_id = GPGME_EDITKEY_ENABLE;  }
167      }  
168      else {  
169          gpgme_editkey_disable_set (ek);  /* Check if the key has a good status.
170          edit_id = GPGME_EDITKEY_DISABLE;     Return value: 0 on success. */
171      }  int
172      err = gpgme_new (&ctx);  km_check_key_status (listview_ctrl_t lv, int pos)
173      if( err )  {
174          BUG( NULL );      int flags = km_get_key_status (lv, pos);
175      gpgme_set_edit_ctx (ctx, ek, edit_id);      
176      err = gpgme_op_editkey (ctx, keyid);      if (flags & KM_FLAG_EXPIRED) {
177      if( !err ) {          msg_box (lv->ctrl, _("This key has expired!\n"  
178          show_msg( dlg, 1500, _("Key status changed.") );                               "Key check failed."), _("Key Manager"), MB_ERR);
179          keycache_set_reload( 1 );          return -1;
180      }      }
181      else      else if (flags & KM_FLAG_REVOKED) {
182          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );          msg_box (lv->ctrl, _("This key has been revoked by its owner!\n"
183                                 "Key check failed."), _("Key Manager"), MB_ERR);
184      gpgme_release (ctx);          return -1;
185      gpgme_editkey_release (ek);      }
186      return err? WPTERR_GENERAL : 0;  
187  } /* km_enable_disable_key */      return 0;
188    }
189    
190  int  
191  km_clip_export (HWND dlg, listview_ctrl_t lv)  /* Return all key flags ORed. */
192  {  int
193      gpgme_error_t err;  km_get_key_status (listview_ctrl_t lv, int pos)
194      gpgme_recipients_t rset;  {
195      int rc, id = 0, n = 0;      gpgme_key_t key;
196            int flags = 0;
197      rset = keylist_enum_recipients (lv, KEYLIST_LIST);  
198      n = gpgme_recipients_count (rset);      if (pos == -1)
199      if (!n) {          return 0;
200          msg_box (dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR);      key = (gpgme_key_t)listview_get_item2 (lv, pos);
201          rc = WPTERR_GENERAL;      if (key == NULL)
202          goto leave;          return 0;
203      }  
204            if (key->expired)
205      err = gpgme_op_clip_export (rset);          flags |= KM_FLAG_EXPIRED;
206      if (err) {      if (key->revoked)
207          msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);          flags |= KM_FLAG_REVOKED;
208          rc = WPTERR_GENERAL;      if (key->disabled)
209          goto leave;          flags |= KM_FLAG_DISABLED;
210      }      return flags;
211      if (n == 1) {  }
212          const char *s;  
213          char buf[256];  
214          s = gpgme_recipients_get_name (rset, 0);  /* Interface to enable or disable a key (@enable = 1 then enable).
215          km_get_clip_info (s, buf, 255);     The key is retrieved from a list control @lv at the pos @pos. */
216          set_clip_text2 (NULL, buf, strlen (buf), 0);  int
217      }  km_enable_disable_key (listview_ctrl_t lv, HWND dlg, int pos, int enable)
218    {
219      show_msg (dlg, 1500, _("GnuPG Status: Finished"));      gpgme_error_t err;
220            gpgme_key_t key;
221  leave:      GpgKeyEdit *ke;
222      gpgme_recipients_release (rset);  
223      return rc;      key = (gpgme_key_t)listview_get_item2 (lv, pos);
224  } /* km_clip_export */      if (!key)
225            BUG (NULL);
226        ke = new GpgKeyEdit (key->subkeys->keyid);
227  int      if (!ke)
228  km_privkey_export( HWND dlg, listview_ctrl_t lv, const char *fname )          BUG (NULL);
229  {  
230      gpgme_recipients_t rset;      err = enable? ke->enable () : ke->disable ();
231      gpgme_data_t keydata;      if (!err)
232      gpgme_error_t err;          show_msg (dlg, 1500, _("Key status changed."));
233      gpgme_ctx_t ctx;      else
234      size_t n = 0;          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
235        delete ke;
236      rset = keylist_enum_recipients( lv, KEYLIST_LIST );      return err? WPTERR_GENERAL : 0;
237      n = gpgme_recipients_count( rset );  }
238      if( !n ) {  
239          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );  
240          return WPTERR_GENERAL;  
241      }  /* Create a string that contain all keyids from
242      if( n > 1 ) {     the key list @rset separated by a space. */
243          msg_box( dlg, _("Only one secret key can be exported."), _("Key Manager"), MB_ERR );  char*
244          return 0; /* we checked this before, so we just quit */  gpg_keylist_to_pattern (gpgme_key_t *rset, int n)
245      }  {
246      err = gpgme_data_new( &keydata );      char *p;
247      if( err )      int i;
248          BUG( dlg );  
249      err = gpgme_new( &ctx );      if (!n)
250      if( err )          return NULL;
251          BUG( dlg );      p = (char *)calloc (1, n*(16+1)+n+2);
252      gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );      if (!p)
253      gpgme_control( ctx, GPGME_CTRL_WITH_SECRET_KEY, 1 );          BUG (NULL);
254        for (i=0; i < n; i++) {
255      err = gpgme_op_export( ctx, rset, keydata );          strcat (p, rset[i]->subkeys->keyid);
256      if( err ) {          strcat (p, " ");
257          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );      }
258          goto leave;      return p;
259      }  }
260    
261      log_box( _("Key Manager"), MB_OK,  
262               _("Secret key successfully saved in '%s'."), fname );  /* Export the keys given in @rset to the clipboard.
263           Return value: 0 on success. */
264  leave:  static gpgme_error_t
265      err = gpgme_data_release_and_set_file( keydata, fname );  gpg_clip_export (gpgme_key_t *rset, int n)
266      if( err )  {
267          log_box( _("Key Manager"), MB_OK,      gpgme_error_t err = 0;
268                   _("Could not save data to '%s'."), fname );      gpgme_ctx_t ctx = NULL;
269      gpgme_release( ctx );      gpgme_data_t keydata = NULL;
270        char *patt=NULL;
271      return (int)err;      
272  } /* km_privkey_export */      err = gpgme_new (&ctx);
273        if (err)
274            return err;
275  int      gpgme_set_armor (ctx, 1);          
276  km_file_export (HWND dlg, listview_ctrl_t lv, const char * fname)      err = gpgme_data_new (&keydata);
277  {      if (err)
278      gpgme_recipients_t rset;          goto leave;
279      gpgme_data_t keydata;        
280      gpgme_error_t err;      patt = gpg_keylist_to_pattern (rset, n);
281      gpgme_ctx_t ctx;      if (!patt) {
282            err = gpg_error (GPG_ERR_ENOMEM);
283      rset = keylist_enum_recipients( lv, KEYLIST_LIST );          goto leave;
284      if( !gpgme_recipients_count( rset ) ) {      }
285          msg_box( dlg, _("No key was selected for export."), _("Key Manager"), MB_ERR );  
286          return WPTERR_GENERAL;      err = gpgme_op_export (ctx, patt, 0, keydata);
287      }      if (err)
288                goto leave;
289      err = gpgme_data_new( &keydata );  
290      if( err )      gpg_data_release_and_set_clipboard (keydata, 1);
291          BUG( dlg );  
292      err = gpgme_new( &ctx );  leave:
293      if( err )      if (patt)
294          BUG( dlg );          free (patt);
295      gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );      gpgme_release (ctx);
296      gpgme_set_comment (ctx, "Generated by WinPT "PGM_VERSION);      return err;
297        }
298      err = gpgme_op_export( ctx, rset, keydata );  
299      if( err ) {  
300          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );  /* Export the selected keys in @lv to the clipboard. */
301          goto leave;  int
302      }  km_clip_export (HWND dlg, listview_ctrl_t lv)
303            {
304      log_box( _("Key Manager"), MB_OK,      gpgme_error_t err;
305               _("Key(s) successfully saved in '%s'."), fname );      gpgme_key_t *rset;
306            char buf[256];
307  leave:      int n=0;
308      err = gpgme_data_release_and_set_file( keydata, fname );      int rc=0;
309      if( err )      
310          log_box( _("Key Manager"), MB_OK,      rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n);
311                   _("Could not save data to '%s'."), fname );          if (!n) {
312      gpgme_release( ctx );          msg_box (dlg, _("No key was selected for export."),
313                     _("Key Manager"), MB_ERR);
314      return (int)err;          rc = WPTERR_GENERAL;
315  } /* km_file_export */          goto leave;
316        }
317        
318  static int      err = gpg_clip_export (rset, n);
319  extract_dash_escaped_key (void)      if (err) {
320  {          msg_box( dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
321      gpgme_data_t inp, plain;          rc = WPTERR_GENERAL;
322      gpgme_error_t err;          goto leave;
323        }
324      err = gpgme_data_new_from_clipboard (&inp);      if (n == 1) {
325      if (err) {          key_get_clip_info (rset[0]->subkeys->keyid, buf, sizeof (buf)-1);
326          msg_box (NULL, gpgme_strerror( err ), _("Key Manager"), MB_ERR);          set_clip_text2 (NULL, buf, strlen (buf), 0);
327          return -1;      }
328      }  
329      gpgme_data_extract_plaintext (inp, &plain);      show_msg (dlg, 1500, _("GnuPG Status: Finished"));
330      gpgme_data_release (inp);      
331      gpgme_data_release_and_set_clipboard (plain);  leave:
332        free (rset);
333      return 0;      return rc;
334  } /* extract_dash_escaped_key */  }
335    
336    
337  int  /* Export the selected secret key from @lv into @fname.
338  km_clip_import( HWND dlg )     It is only allowed to export a single secret key. */
339  {  int
340      gpgme_error_t err;  km_privkey_export (HWND dlg, listview_ctrl_t lv, const char *fname)
341      gpgme_pgptype_t pgptype;  {
342      int id;      gpgme_key_t *rset;
343      int has_data = 0;      gpgme_error_t err;
344            int n = 0;
345      if( !gpgme_clip_istext_avail( &has_data ) && !has_data ) {  
346          msg_box( dlg, winpt_strerror( WPTERR_CLIP_ISEMPTY ), _("Key Manager"), MB_ERR );      rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n);
347          return WPTERR_CLIP_ISEMPTY;      if (!n) {
348      }          msg_box (dlg, _("No key was selected for export."),
349      err = gpgme_clip_is_secured( &pgptype, &has_data );                   _("Key Manager"), MB_ERR);
350      if( err )          return WPTERR_GENERAL;
351          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );      }
352      if( !has_data ) {      if (n > 1) {
353          msg_box( dlg, _("No valid OpenPGP data found."), _("Key Manager"), MB_ERR );          msg_box (dlg, _("Only one secret key can be exported."),
354          return WPTERR_GENERAL;                   _("Key Manager"), MB_ERR);
355      }          free (rset);
356      if( !(pgptype & GPGME_PGP_PUBKEY) && !(pgptype & GPGME_PGP_SECKEY) ) {          return 0; /* we checked this before, so we just quit */
357          msg_box( dlg, _("No valid OpenPGP keys found."), _("Key Manager"), MB_ERR );      }
358          return WPTERR_GENERAL;  
359      }      err = gpg_export_seckey (rset[0]->subkeys->keyid, fname);
360      if( pgptype & GPGME_PGP_DASH_ESCAPED ) {      if (err)
361          id = msg_box( dlg, _("The key you want to import is dash escacped.\n"          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
362                               "Do you want to extract the key?"),      else
363                        _("Key Manager"), MB_YESNO );          log_box (_("Key Manager"), MB_OK,
364          if( id == IDYES )                   _("Secret key successfully saved in '%s'."), fname);
365              extract_dash_escaped_key( );  
366          else      free (rset);
367              msg_box( dlg, _("Cannot import dash escaped OpenPGP keys."), _("Key Manager"), MB_INFO );      return err? WPTERR_GENERAL : 0;
368      }  }
369    
370      dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,  
371                        clip_import_dlg_proc, NULL,  /* Export the selected recipients from @lv into the file @fname. */
372                        _("Key Import"), IDS_WINPT_IMPORT );  int
373    km_file_export (HWND dlg, listview_ctrl_t lv, const char *fname)
374      return 0;  {
375  } /* km_clip_import */      GPGME *ctx;
376        gpgme_key_t *rset;
377        gpgme_error_t err;
378  int      char *patt;
379  km_http_import (HWND dlg, const char * url)      int n;
380  {  
381      FILE * fp;      rset = keylist_enum_recipients (lv, KEYLIST_LIST, &n);
382      char * p;      if (!n) {
383      char tmpdir[500];          msg_box (dlg, _("No key was selected for export."),
384      http_hd_t hd;                   _("Key Manager"), MB_ERR);
385      int statcode;          return WPTERR_GENERAL;
386      int rc = 0;      }
387        patt = gpg_keylist_to_pattern (rset, n);
388      if (strncmp (url, "http://", 7)) {  
389          log_box (_("Key Import HTTP"), MB_ERR, _("Invalid HTTP URL: %s"), url);      ctx = new GPGME ();
390          return WPTERR_GENERAL;      ctx->setArmor (true);
391      }      err = ctx->exportToFile (patt, fname);
392        if (err) {
393      GetTempPath (499, tmpdir);          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
394      p = make_filename (tmpdir, "file_http", "tmp");          goto leave;
395      if (!p)      }
396          BUG (0);      else
397      fp = fopen (p, "wb");          log_box (_("Key Manager"), MB_OK,
398      if (fp == NULL) {                   _("Key(s) successfully saved in '%s'."), fname);
399          free_if_alloc (p);      
400          log_box (_("Key Import HTTP"), MB_ERR, "%s: %s", p, winpt_strerror (WPTERR_FILE_CREAT));  leave:
401          return WPTERR_FILE_CREAT;      delete ctx;
402      }      safe_free (patt);
403      /* parse URL */      return (int)err;
404      rc = http_send_request2 (url, &hd);  }
405      if (!rc)  
406          rc = http_parse_response (hd, &statcode);  
407      if (!rc)  /* Read a dash escaped key from the clipboard
408          rc = http_parse_data (hd, fp);     unescape it and write it back. */
409      http_hd_free (hd);  static int
410      fclose (fp);  extract_dash_escaped_key (void)
411      if (rc) {  {
412          free_if_alloc (p);      gpgme_data_t inp, plain;
413          msg_box (dlg, http_strerror (rc), _("Key Import HTTP"), MB_ERR);      gpgme_error_t err;
414          return WPTERR_GENERAL;  
415      }      err = gpg_data_new_from_clipboard (&inp, 0);
416      km_file_import (dlg, p);      if (err) {
417      free_if_alloc (p);          msg_box (NULL, gpgme_strerror (err), _("Key Manager"), MB_ERR);
418      return 0;          return -1;
419  }      }
420        gpg_data_extract_plaintext (inp, &plain);
421  int      gpg_data_release_and_set_clipboard (plain, 0);
422  km_file_import( HWND dlg, const char * fname )      gpgme_data_release (inp);
423  {  
424      gpgme_data_t keydata = NULL;      return 0;
425      gpgme_ctx_t ctx;  }
426      gpgme_error_t err;  
427      fm_state_s fm_stat;  
428      int import_res[14];  /* Import keys from the clipboard. */
429        int
430      memset( &fm_stat, 0, sizeof fm_stat );  km_clip_import (HWND dlg, int *r_newkeys)
431      fm_stat.opaque = m_strdup( fname );  {
432            gpgme_error_t err;
433      dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,      int pgptype;
434                        file_import_dlg_proc, (LPARAM)&fm_stat,      int id;
435                        _("File Import"), IDS_WINPT_IMPORT );      int new_keys = 0, has_data = 0;
436      if( fm_stat.cancel == 1 )      
437          return WPTERR_GENERAL;      if (!gpg_clip_istext_avail (&has_data) && !has_data) {
438                msg_box (dlg, winpt_strerror (WPTERR_CLIP_ISEMPTY),
439      err = gpgme_new( &ctx );                   _("Key Manager"), MB_ERR);
440      if( err )          return WPTERR_CLIP_ISEMPTY;
441          BUG( dlg );      }
442      gpgme_control( ctx, GPGME_CTRL_FORCETRUST, 1 );      err = gpg_clip_is_secured (&pgptype, &has_data);
443      err = gpgme_data_new_from_file (&keydata, fname);      if (err)
444      if( err ) {          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
445          msg_box( dlg, _("Could not read key-data from file."), _("Key Manager"), MB_ERR );      if (!has_data) {
446          goto leave;          msg_box (dlg, _("No valid OpenPGP data found."),
447      }                   _("Key Manager"), MB_ERR);
448                return WPTERR_NODATA;
449      err = gpgme_op_import( ctx, NULL, keydata );      }
450      if( err ) {      if (!(pgptype & PGP_PUBKEY) && !(pgptype & PGP_SECKEY)) {
451          msg_box( dlg, gpgme_strerror( err ), _("Key Manager"), MB_ERR );          msg_box (dlg, _("No valid OpenPGP keys found."),
452          goto leave;                   _("Key Manager"), MB_ERR);
453      }          return WPTERR_NODATA;
454            }
455      gpgme_get_import_status( ctx, import_res, NULL );      if (pgptype & PGP_DASH_ESCAPED) {
456      print_import_status( import_res, fm_stat.implist_revcert );          id = msg_box (dlg, _("The key you want to import is dash escacped.\n"
457      if( import_res[GPGME_IMPSTAT_NOSELFSIG] > 0  ) {                               "Do you want to extract the key?"),
458          msg_box( dlg, _("Key without a self signature was dectected!\n"                        _("Key Manager"), MB_YESNO);
459                          "(This key is NOT usable for encryption, etc)\n"          if (id == IDYES)
460                          "\n"                  extract_dash_escaped_key ();
461                          "Cannot import these key(s)!"), _("Import"), MB_INFO );          else
462      }              msg_box (dlg, _("Cannot import dash escaped OpenPGP keys."),
463                             _("Key Manager"), MB_INFO);
464  leave:              }
465      gpgme_data_release( keydata );  
466      gpgme_release( ctx );      new_keys = dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,
467      free_if_alloc( fm_stat.opaque );                                   clip_import_dlg_proc, 0,
468      return (int)err;                                   _("Key Import"), IDS_WINPT_IMPORT);
469  } /* km_file_import */      if (r_newkeys)
470            *r_newkeys = new_keys;
471        if (!new_keys)
472  static void          return WPTERR_NODATA;
473  delete_keys_from_cache (gpgme_recipients_t rset)      return 0;
474  {  }
475      gpgme_keycache_t pub = keycache_get_ctx (1);  
476      void * ctx =NULL;  
477      const char * s;  /* Import a key from the http URL @url. */
478    int
479      gpgme_recipients_enum_open (rset, &ctx);  km_http_import (HWND dlg, const char *url)
480      while ((s = gpgme_recipients_enum_read (rset, &ctx)))  {
481          gpgme_keycache_delete_key (pub, s);      http_hd_t hd;
482      gpgme_recipients_enum_close (rset, &ctx);      FILE *fp;
483  } /* delete_keys_from_cache */      char *p;
484        char tmpfile[500];
485        int statcode;
486  int      int rc = 0;
487  km_delete_keys (listview_ctrl_t lv, HWND dlg)  
488  {      if (strncmp (url, "http://", 7)) {
489      gpgme_error_t err;          log_box (_("Key Import HTTP"), MB_ERR, _("Invalid HTTP URL: %s"), url);
490      gpgme_recipients_t rset;          return WPTERR_GENERAL;
491      char keyid[32], uid[256], date[64], keylen[64];          }
492      int with_seckey, seckey_type=0;  
493      int i, rc;      GetTempPath (sizeof (tmpfile)-128, tmpfile);
494            p = make_filename (tmpfile, "winpt_file_http", "tmp");
495      if( listview_get_curr_pos( lv ) == -1 ) {      if (!p)
496          msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );          BUG (0);
497          return WPTERR_GENERAL;      fp = fopen (p, "wb");
498      }      if (!fp) {
499                free_if_alloc (p);
500      err = gpgme_recipients_new (&rset);          log_box (_("Key Import HTTP"), MB_ERR, "%s: %s", p,
501      if (err)                   winpt_strerror (WPTERR_FILE_CREAT));
502          BUG (0);          return WPTERR_FILE_CREAT;
503            }
504      for( i = 0; i < listview_count_items( lv, 0 ); i++ ) {  
505          if( listview_get_item_state( lv, i ) ) {      /* parse URL */
506              listview_get_item_text( lv, i, 0, uid, sizeof uid - 1 );      rc = http_send_request2 (url, &hd);
507              listview_get_item_text( lv, i, 1, keyid, sizeof keyid - 1 );      if (!rc)
508              listview_get_item_text( lv, i, 3, keylen, sizeof keylen - 1 );          rc = http_parse_response (hd, &statcode);
509              listview_get_item_text( lv, i, 7, date, sizeof date - 1 );      if (!rc)
510              seckey_type = km_check_for_seckey( lv, i, NULL );          rc = http_parse_data (hd, fp);
511              if( !seckey_type ) {      http_hd_free (hd);
512                  rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING,      fclose (fp);
513                                _("Do you really want to delete this key?\n\n"      if (rc) {  
514                                  "pub %s %s %s\n"          msg_box (dlg, winpt_strerror (rc), _("Key Import HTTP"), MB_ERR);
515                                  "  \"%s\""), keylen, keyid, date, uid );          rc = WPTERR_GENERAL;
516                  if( rc == IDYES )      }
517                      gpgme_recipients_add_name( rset, keyid );      km_file_import (dlg, p, NULL);
518                  with_seckey = 0;      remove (p);
519              }      free_if_alloc (p);
520              else {      return rc;
521                  rc = log_box( _("Key Manager"), MB_YESNO|MB_ICONWARNING,                  }
522                                _("Do you really want to delete this KEY PAIR?\n\n"  
523                                  "Please remember that you are not able to decrypt\n"  
524                                  "messages you stored with this key any longer.\n"  /* Import a key from the given file @fname.
525                                  "\n"     On success an import statistics dialog is shown. */
526                                  "pub/sec %s %s %s\n"  int
527                                  "  \"%s\""), keylen, keyid, date, uid );  km_file_import (HWND dlg, const char *fname, int *r_newkeys)
528                  if( rc == IDYES ) {  {
529                      if( seckey_type == 2 )      gpgme_data_t keydata = NULL;
530                          msg_box( dlg, _("The actual secret key is stored on a smartcard.\n"      gpgme_ctx_t ctx;
531                                          "Only the public key and the secret key \n"      gpgme_error_t err;    
532                                          "placeholder will be deleted.\n"), _("Key Manager"), MB_OK );      fm_state_s fm_stat;
533                      gpgme_recipients_add_name( rset, keyid );      gpgme_import_result_t res;
534                  }      int no_data = 0;
535                  with_seckey = 1;      
536              }      memset (&fm_stat, 0, sizeof (fm_stat));
537          }      fm_stat.opaque = m_strdup (fname);
538      }      
539            dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg,
540      if (!gpgme_recipients_count (rset)) {                        file_import_dlg_proc, (LPARAM)&fm_stat,
541          gpgme_recipients_release (rset);                        _("File Import"), IDS_WINPT_IMPORT);
542          return 0;      if (fm_stat.cancel == 1) {
543      }          free_if_alloc (fm_stat.opaque);
544                return WPTERR_GENERAL;
545      err = gpgme_op_delete_keys (rset, with_seckey);      }
546      if (err) {      
547          if (err == GPGME_Invalid_Key)      err = gpgme_new (&ctx);
548              msg_box (dlg, _("No such key."), _("Key Manager"), MB_INFO);      if (err)
549          else if (err == GPGME_Conflict)          BUG (NULL);
550              msg_box (dlg, _("Must delete secret key first."), _("Key Manager"), MB_INFO);      err = gpgme_data_new_from_file (&keydata, fname, 1);
551          else      if (err) {
552              msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);          msg_box (dlg, _("Could not read key-data from file."),
553          return FALSE;                   _("Key Manager"), MB_ERR);
554      }          goto leave;
555      show_msg (dlg, 1500, _("GnuPG Status: Finished"));      }
556      listview_del_items (lv);      
557      if (keyring_check_last_access ())      op_begin ();
558          keycache_set_reload (1);      err = gpgme_op_import (ctx, keydata);
559      delete_keys_from_cache (rset);      op_end ();
560      gpgme_recipients_release (rset);      if (err) {
561            msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
562      return (int)err;          goto leave;
563  } /* km_delete_keys */      }    
564    
565        res = gpgme_op_import_result (ctx);
566  int      if (res->unchanged == res->considered)
567  km_send_to_keyserver (listview_ctrl_t lv, HWND dlg, const char * host, u16 port)          no_data = 1;
568  {      if (r_newkeys)
569      char keyid[32];          *r_newkeys = res->considered - res->unchanged;
570      const char *t;      if (res->new_revocations == 0 && fm_stat.import.revcert == 1)
571      int id;          res->new_revocations = 1;
572            if (res->secret_imported == 0 && fm_stat.import.has_seckey == 1)
573      id = listview_get_curr_pos( lv );          res->secret_imported = 1;
574      if( id == -1 ) {  
575          msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );      /* XXX: if we import a key pair but the secret key comes first,
576          return WPTERR_GENERAL;              no_{valid}_user_id is 1 even so the public key, which comes
577      }              later is valid and self-signed. */
578        print_import_status (res);
579      listview_get_item_text( lv, id, 1, keyid, sizeof keyid - 1 );      if (res->no_user_id > 0) {
580      id = log_box (_("Key Manager"), MB_YESNO,          msg_box (dlg, _("Key without a self signature was dectected!\n"
581                    _("Do you really want to send '%s' to keyserver %s?"),                          "(This key is NOT usable for encryption, etc)\n"
582                      keyid, host);                          "\n"    
583      if (id == IDYES) {                          "Cannot import these key(s)!"), _("Import"), MB_INFO);
584          t = keyid;      }
585          if (!strncmp (keyid, "0x", 2))  
586              t += 2;  leave:
587          hkp_send_key (dlg, host, port, t);      gpgme_data_release (keydata);
588      }      gpgme_release (ctx);
589        free_if_alloc (fm_stat.opaque);
590      return 0;      if (no_data)
591  } /* km_send_to_keyserver */          return WPTERR_NODATA;
592        return (int)err;
593    }
594  int  
595  km_send_to_mail_recipient( listview_ctrl_t lv, HWND dlg )  
596  {  /* Mark the keys in @rset as deleted in the keycache. */
597      gpgme_key_t key;  static void
598      gpgme_ctx_t ctx=NULL;  delete_keys_from_cache (gpgme_key_t *rset, size_t n)
599      gpgme_recipients_t rset=NULL;  {
600      gpgme_error_t rc;      gpg_keycache_t pub = keycache_get_ctx (1);
601      const char * s;      gpg_keycache_t sec = keycache_get_ctx (0);
602      char keyid[32], tmp[192+256], * p =NULL;      gpgme_key_t sk;
603      int pos;      int i=0;
604    
605      if( listview_count_items( lv, 1 ) > 1 ) {      while (n-- > 0) {
606          msg_box( dlg, _("Please only select one key."), _("Key Manager"), MB_INFO|MB_OK );          if (!get_seckey (rset[i]->subkeys->keyid, &sk))
607          return WPTERR_GENERAL;              gpg_keycache_delete_key (sec, sk->subkeys->keyid);
608      }          gpg_keycache_delete_key (pub, rset[i]->subkeys->keyid);
609      pos = listview_get_curr_pos( lv );          i++;
610      if( pos == -1 ) {      }
611          msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );  }
612          return WPTERR_GENERAL;  
613      }  
614      listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 );  /* Delete all selected keys from the list view @lv. */
615      if( get_pubkey( keyid, &key ) )  int
616          BUG( NULL );  km_delete_keys (listview_ctrl_t lv, HWND dlg)
617      s = gpgme_key_get_string_attr( key, GPGME_ATTR_NAME, NULL, 0 );  {
618      GetTempPath (sizeof tmp-1, tmp);      gpgme_error_t err;
619      strncat (tmp, s, sizeof tmp-200);      gpgme_ctx_t ctx;
620      strncat (tmp, ".asc", sizeof tmp-200);      gpgme_key_t *rset;
621      p = fm_quote_file (tmp);      gpgme_key_t key;
622        char *p;
623      rc = gpgme_recipients_new( &rset );      int with_seckey=0, seckey_type=0, confirm=0;
624      if( !rc )      int i, rc, n, k_pos=0;
625          rc = gpgme_recipients_add_name( rset, keyid );      
626      if( !rc )      if (listview_get_curr_pos (lv) == -1) {
627          rc = gpgme_new( &ctx );          msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
628      if( !rc ) {          return -1;
629          gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );      }
630          rc = gpgme_op_file_export( ctx, rset, p );          
631      }      if (listview_count_items (lv, 1) > 8) {
632      if( rc )          i = msg_box (NULL, _("Do you really want to confirm each key?"),
633          msg_box( dlg, gpgme_strerror( rc ), _("Key Manager"), MB_ERR );                       _("Delete Confirmation"), MB_YESNOCANCEL|MB_ICONQUESTION);
634      else          if (i == IDCANCEL)
635          mapi_send_pubkey (keyid, tmp);              return 0;
636      free_if_alloc( p );          if (i != IDNO)
637      gpgme_recipients_release( rset );              confirm = 1;
638      gpgme_release( ctx );          }
639      return rc;      else
640  }          confirm = 1;
641    
642        /* n = total amount of keys, i is the selected amount. */
643  static void      n = listview_count_items (lv, 0);
644  km_refresh_one_key (listview_ctrl_t lv, HWND dlg, int pos)      i = listview_count_items (lv, 1);
645  {      rset = (gpgme_key_t *)calloc (i+1, sizeof (gpgme_key_t));
646      int idx;      if (!rset)
647      char keyid[32];          BUG (NULL);
648      const char *t;      for (i = 0; i < n; i++) {
649            if (listview_get_item_state(lv, i)) {
650      if (pos != 0)              key = (gpgme_key_t)listview_get_item2 (lv, i);
651          idx = pos;              if (!key)
652      else                  BUG (NULL);
653          idx = listview_get_curr_pos (lv);              seckey_type = km_check_for_seckey (lv, i, NULL);
654      if (idx != -1)              if (confirm && !seckey_type) {
655      {                  p = km_key_get_info (key, 0);
656          listview_get_item_text (lv, idx, 1, keyid, sizeof keyid - 1);                  rc = log_box (_("Key Manager"), MB_YESNO|MB_ICONWARNING,
657          t = keyid;                                _("Do you really want to delete this key?\n\n"
658          if (!strncmp (keyid, "0x", 2))                                  "%s"), p);
659              t += 2;                  if (rc == IDYES)
660          hkp_recv_key (dlg, default_keyserver, default_keyserver_port, t, 0, KM_KS_REFRESH);                      rset[k_pos++] = key;
661      }                  with_seckey = 0;
662  }                  free_if_alloc (p);
663                }
664                else if (confirm) {
665  void                  p = km_key_get_info (key, 1);
666  km_refresh_from_keyserver (listview_ctrl_t lv, HWND dlg)                  rc = log_box (_("Key Manager"), MB_YESNO|MB_ICONWARNING,                
667  {                                _("Do you really want to delete this KEY PAIR?\n\n"
668      int idx, id, i;                                  "Please remember that you are not able to decrypt\n"
669                                        "messages you stored with this key any longer.\n"
670      if (kserver_check_inet_connection ())                                  "\n"
671      {                                  "%s"), p);
672          msg_box (dlg, _("Could not connect to keyserver, abort procedure."),                  if (rc == IDYES) {
673                   _("Key Manager"), MB_ERR);                      if (seckey_type == 2)
674          return;                          msg_box (dlg, _("The actual secret key is stored on a smartcard.\n"
675      }                                          "Only the public key and the secret key \n"
676      idx = listview_count_items (lv, 0);                                          "placeholder will be deleted.\n"),
677      if (listview_count_items (lv, 1) == idx) {                                          _("Key Manager"), MB_OK);
678          id = msg_box (dlg, _("Do you really want to refresh all keys in the keyring?"), _("Key Manager"), MB_YESNO);                      rset[k_pos++] = key;
679          if (id == IDNO)                  }
680              return;                  with_seckey = 1;
681          for (i = 0; i < idx; i++)                  free_if_alloc (p);
682              km_refresh_one_key (lv, dlg, i);              }
683      }              else {
684      else if (idx == 1)                  with_seckey = 1;
685          km_refresh_one_key (lv, dlg, 0);                  rset[k_pos++] = key;
686      else {              }
687          for (i=0; i < listview_count_items (lv, 0); i++) {          }
688              if (listview_get_item_state (lv, i))      }
689                  km_refresh_one_key (lv, dlg, i);      
690          }      if (k_pos == 0) {
691      }          free (rset);
692  } /* km_refresh_from_keyserver */          return 0;
693        }
694    
695  void      err = gpgme_new (&ctx);
696  km_set_clip_info( const char *uid )      if (err)
697  {              BUG (NULL);
698      char buf[256];      n = k_pos;
699            op_begin ();
700      km_get_clip_info (uid, buf, 255);          for (i=0; i < k_pos; i++) {
701      set_clip_text( NULL, buf, strlen( buf ) );          err = gpgme_op_delete (ctx, rset[i], with_seckey);
702  } /* km_set_clip_info */          if (err)
703                msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
704            else
705                n--;
706  int      }
707  km_key_is_v3( listview_ctrl_t lv, int pos )      op_end ();
708  {      if (n == 0)
709      gpgme_key_t pk;          show_msg (dlg, 1500, _("GnuPG Status: Finished"));      
710      const char * fpr;      gpgme_release (ctx);
711      unsigned long algo;      listview_del_sel_items (lv);
712      char keyid[32];      delete_keys_from_cache (rset, k_pos);
713        free (rset);
714      listview_get_item_text( lv, pos, 1, keyid, sizeof keyid-1 );  
715      if( get_pubkey( keyid, &pk ) )      return (int)err;
716          BUG( NULL );  }
717      algo = gpgme_key_get_ulong_attr( pk, GPGME_ATTR_ALGO, NULL, 0 );  
718      fpr = gpgme_key_get_string_attr( pk, GPGME_ATTR_FPR, NULL, 0 );  
719      return strlen( fpr ) == 32 && algo == GPGME_PK_RSA? 1 : 0;  /* Send the select key in @lv to the keyserver @host:@port. */
720  } /* km_key_is_v3 */  int
721    km_send_to_keyserver (listview_ctrl_t lv, HWND dlg, const char *host, u16 port)
722    {
723  void      gpgme_key_t key;
724  km_update_default_key_str (HWND dlg, int * ret_len)      int id;
725  {      
726      char * keyid, defkeyinf[512];      id = listview_get_curr_pos (lv);
727      const char * fmt;      if (id == -1) {
728                msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
729      keyid = get_gnupg_default_key ();          return WPTERR_GENERAL;
730      if (!keyid)      }
731          BUG (0);  
732      if( (keyid[0] >= 'A' && keyid[0] <= 'Z') || (keyid[0] >= 'a' && keyid[0] <= 'z')      key = (gpgme_key_t)listview_get_item2 (lv, id);
733          || (keyid[0] == '0' && keyid[1] == 'x') )      if (!key)
734          fmt = _("Default Key: %s");          BUG (NULL);
735      else      id = log_box (_("Key Manager"), MB_YESNO,
736          fmt = _("Default Key: 0x%s");                    _("Do you really want to send '%s' to keyserver %s?"),
737      _snprintf( defkeyinf, sizeof defkeyinf - 1, fmt, keyid );                      key->subkeys->keyid+8, host);
738      SetWindowText( dlg, defkeyinf );      if (id == IDYES)
739      *ret_len = strlen( defkeyinf );          hkp_send_key (dlg, host, port, key->subkeys->keyid+8);
740      free_if_alloc( keyid );  
741  } /* km_return_default_key_str */      return 0;
742    }
743    
744  void  
745  km_complete_status_bar( HWND sb, listview_ctrl_t lv, int startpos )  /* Send the selected key in @lv via MAPI to a mail recipient. */
746  {  int
747      char text[384];  km_send_to_mail_recipient (listview_ctrl_t lv, HWND dlg)
748      int nkeys = 0, nsec = 0, i;  {
749        gpgme_key_t key;
750      GetWindowText( sb, text, sizeof text -1 );      gpgme_ctx_t ctx=NULL;
751      nkeys = listview_count_items( lv, 0 );      gpgme_data_t out;
752      for( i = 0; i < nkeys; i++ ) {      gpgme_error_t rc;
753          if( km_check_for_seckey( lv, i, NULL ) )      char tmp[128];
754              nsec++;      char *fname;
755      }      char *name;
756      _snprintf( text+startpos, sizeof text-1, "                %d keys (%d secret keys)", nkeys, nsec );      int pos;
757      SetWindowText( sb, text );  
758  } /* km_complete_status_bar */      if (listview_count_items (lv, 1) > 1) {
759            msg_box (dlg, _("Please only select one key."),
760                     _("Key Manager"), MB_INFO|MB_OK);
761  void          return WPTERR_GENERAL;
762  km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos)      }
763  {      pos = listview_get_curr_pos (lv);
764      gpgme_error_t err;      if (pos == -1) {
765      gpgme_ctx_t ctx;          msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
766      gpgme_editkey_t ek;          return WPTERR_GENERAL;
767      char keyid[32];      }
768        key = (gpgme_key_t)listview_get_item2 (lv, pos);
769      listview_get_item_text (lv, pos, 1, keyid, 31);      if (!key)
770      err = gpgme_new (&ctx);          BUG (NULL);
771      if (err)  
772          BUG (0);      GetTempPath (sizeof (tmp)-1, tmp);
773      err = gpgme_editkey_new (&ek);      if (tmp[strlen (tmp)-1] == '\\')
774      if (err)          tmp[strlen (tmp)-1] = 0;
775          BUG (0);      name = utf8_to_wincp2 (key->uids->name);
776      gpgme_set_edit_ctx (ctx, ek, GPGME_EDITKEY_TRUST);      fname = make_filename (tmp, name, "asc");
777      gpgme_editkey_trust_set (ek, 5);      for (pos=0; pos < (int)strlen (fname); pos++) {
778            if (fname[pos] == ' ')
779      err = gpgme_op_editkey (ctx, keyid);              fname[pos] = '_';
780      if (err)      }
781          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);  
782      else {      rc = gpgme_new (&ctx);
783          show_msg (dlg, 1500, _("GnuPG Status: Finished"));      if (rc)
784          keycache_set_reload (1);          BUG (NULL);
785      }      rc = gpgme_data_new (&out);
786        if (rc)
787      gpgme_release (ctx);          BUG (NULL);
788      gpgme_editkey_release (ek);      
789  }      gpgme_set_armor (ctx, 1);
790        rc = gpgme_op_export (ctx, key->subkeys->keyid, 0, out);
791        if (rc) {
792  gpg_optfile_t          gpgme_data_release (out);
793  km_groupdb_open( void )          msg_box (dlg, gpgme_strerror (rc), _("Key Manager"), MB_ERR);
794  {            }
795      gpg_optfile_t opt;      else {
796      char * optfile;          gpg_data_release_and_set_file (out, fname);
797      int err = 0;          mapi_send_pubkey (key->subkeys->keyid+8, fname);
798            }
799      optfile = get_gnupg_cfgfile();  
800      if( !optfile )      gpgme_release (ctx);
801          BUG( NULL );      safe_free (name);
802      if( parse_gpg_options( optfile, &opt ) )      free_if_alloc (fname);
803          err = 1;      return rc;
804      free_if_alloc( optfile );  }
805      return err? NULL : opt;  
806  } /* km_groupdb_open */  
807    static void
808    km_refresh_one_key (listview_ctrl_t lv, HWND dlg, int pos, int flags)
809  int  {
810  km_groupdb_expand_recipients( const char *name, gpgme_recipients_t rset )      gpgme_key_t key;
811  {        int idx;
812      gpgme_keycache_t kc;  
813      gpgme_key_t pk;      if (pos != 0)
814      gpg_optfile_t opt;          idx = pos;
815      gpg_group_t grp;          else
816      gpg_member_t mbr;          idx = listview_get_curr_pos (lv);
817      int no_trust = 0, n;      if (idx != -1) {
818            key = (gpgme_key_t)listview_get_item2 (lv, idx);
819      kc = keycache_get_ctx( 1 );          if (!key)
820      if( !kc )              BUG (0);
821          BUG( NULL );          hkp_recv_key (dlg, default_keyserver, default_keyserver_port,
822                          key->subkeys->keyid+8, 0, flags);
823      opt = km_groupdb_open( );      }
824      if( !opt )  }
825          return WPTERR_FILE_OPEN;  
826        
827      grp = find_group( opt, name );  /* Refresh the selected keys from the default keyserver. */
828      if( !grp )  void
829          return WPTERR_GENERAL;  km_refresh_from_keyserver (listview_ctrl_t lv, HWND dlg)
830        {
831      /* we are paranoid and check that all group members exist in the      int cnt, id, i;
832         key cache. there is no need that it is really the real key, but      
833         an entry should be available. the rest is up to GPG. */      if (kserver_check_inet_connection ()) {
834      for( mbr = grp->list; mbr; mbr = mbr->next ) {          msg_box (dlg, _("Could not connect to keyserver, abort procedure."),
835          if( gpgme_keycache_find_key( kc, mbr->name, 0, &pk ) )                   _("Key Manager"), MB_ERR);
836              BUG( NULL );          return;
837          n = gpgme_key_count_items( pk, GPGME_ATTR_USERID );      }
838          while( n-- ) {  
839              const char * s = gpgme_key_get_string_attr( pk, GPGME_ATTR_USERID, NULL, n );      cnt = listview_count_items (lv, 0);
840              if( s && stristr( s, mbr->name )      if (listview_count_items (lv, 1) == cnt) {
841                  && gpgme_key_get_ulong_attr( pk, GPGME_ATTR_VALIDITY, NULL, n ) < 3 )          id = msg_box (dlg, _("Do you really want to refresh all keys in the keyring?"),
842                  no_trust++;                        _("Key Manager"), MB_YESNO);
843          }          if (id == IDNO)
844      }              return;
845        }
846      gpgme_recipients_add_name( rset, name );      if (listview_count_items (lv, 1) == 1)
847      release_gpg_options( opt );          km_refresh_one_key (lv, dlg, listview_get_curr_pos (lv), 0);
848            else {
849      return no_trust;          for (i=0; i < cnt; i++) {
850  } /* km_groupdb_expand_recipients */              if (listview_get_item_state (lv, i))
851                    km_refresh_one_key (lv, dlg, i, KM_KS_REFRESH);
852            }
853  static HTREEITEM      }
854  km_tv_insert_item( HWND tree, HTREEITEM parent, const char *text )  }
855  {  
856      TVINSERTSTRUCT tvis;  
857      HTREEITEM node;  void
858        km_set_clip_info (const char *uid)
859      memset( &tvis, 0, sizeof tvis );  {    
860      tvis.hParent = parent;      char buf[256];
861      tvis.hInsertAfter = TVI_LAST;      
862      tvis.item.mask = TVIF_TEXT;      key_get_clip_info (uid, buf, sizeof (buf)-1);
863      tvis.item.pszText = (char *)text;      set_clip_text (NULL, buf, strlen (buf));
864      node = TreeView_InsertItem( tree, &tvis );  }
865      return node;  
866  } /* km_tv_insert_item */  
867    
868    /* Return TRUE if the key in the list @lv at pos @pos is an
869  int     old version 3 key. */
870  km_groups_new( km_group_t *r_gc, HWND ctrl )  int
871  {  km_key_is_v3 (listview_ctrl_t lv, int pos)
872      km_group_t gc;  {
873            gpgme_key_t pk;
874      gc = new km_group_s;  
875      if (!gc)      pk = (gpgme_key_t)listview_get_item2 (lv, pos);
876          BUG (NULL);      if (!pk)
877      gc->tree = ctrl;          BUG (NULL);
878      gc->gh = km_groupdb_open ();      if (strlen (pk->subkeys->fpr) == 32 &&
879      *r_gc = gc;          pk->subkeys->pubkey_algo == GPGME_PK_RSA)
880      return 0;          return -1;
881  } /* km_groups_new */      return 0;
882    }
883    
884  void  
885  km_groups_sync( km_group_t gc )  /* Set trust of selected key in @lv (at @pos) to ultimate. */
886  {  int
887      char * optfile;  km_set_implicit_trust (HWND dlg, listview_ctrl_t lv, int pos)
888    {
889      optfile = get_gnupg_cfgfile ();      gpgme_error_t err;
890      if( !optfile )      gpgme_key_t key;
891          BUG( NULL );      GpgKeyEdit *ke;
892      commit_gpg_options( optfile, gc->gh );  
893      free_if_alloc( optfile );      key = (gpgme_key_t)listview_get_item2 (lv, pos);
894      gc->need_sync = 0;      if (!key)
895  } /* km_groups_sync */          BUG (NULL);
896        ke = new GpgKeyEdit (key->subkeys->keyid);
897        if (!ke)
898  void          BUG (0);
899  km_groups_release (km_group_t gc)  
900  {      err = ke->setTrust (GPGME_VALIDITY_ULTIMATE);
901      if( gc ) {      if (err)
902          /* xxx: this reset the default key (sync=1) */          msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
903          gc->need_sync=0;      else
904          if (gc->need_sync)          show_msg (dlg, 1500, _("GnuPG Status: Finished"));
905              km_groups_sync (gc);  
906          if (gc->gh)      delete ke;
907              release_gpg_options( gc->gh );      return (int)err;
908          gc->gh = NULL;  }
909          gc->tree = NULL;  
910          delete gc;  
911      }      void
912  } /* km_groups_release */  km_find_key (HWND dlg, listview_ctrl_t lv)
913    {
914        int oldpos = listview_get_curr_pos (lv);
915  int      int n;
916  km_groups_load( km_group_t gc )      char *name = get_input_dialog (dlg, _("Search"), _("Search for:"));
917  {          if (name == NULL)
918      HTREEITEM n;          return;
919      gpg_group_t grp, g;      if (oldpos < 0)
920      gpg_member_t mbr;          oldpos = 0;
921      u32 gid = 0;      n = listview_find (lv, name);
922            if (n != -1) {
923      if( !gc->gh )          listview_select_one (lv, n);
924          return 0;          listview_scroll (lv, oldpos, n);
925      grp = gc->gh->grp;      }
926      if( !grp )      else {
927          return 0; /* no groups */          const char *s = _("String pattern \"%s\" not found.");
928                    char *p = new char[strlen (s) + strlen (name) + 2];
929      for( g = grp; g; g = g->next ) {          if (!p)
930          n = km_tv_insert_item( gc->tree, NULL, g->name );              BUG (0);
931          for( mbr = g->list; mbr; mbr = mbr->next ) {          sprintf (p, s, name);
932              if( mbr->used && mbr->name )          msg_box (dlg, p, _("Key Manager"), MB_INFO);
933                  km_tv_insert_item( gc->tree, n, mbr->name );          free_if_alloc (p);
934          }      }
935      }      free_if_alloc (name);
936      DragAcceptFiles( gc->tree, TRUE );  }
937      gc->need_sync = 0;  
938      return 0;  
939  } /* km_groups_load */  /* Return a user-friendly name for a key derrived from
940       name. If @is_secret is 1, a secret key name will be generated. */
941    char*
942  int  km_gen_export_filename (const char *keyid, int is_secret)
943  km_groups_add( km_group_t gc, listview_ctrl_t lv, int km_index )  {
944  {      gpgme_key_t key;
945      TVITEM tvi;      char *p, *uid;
946      char uid[128], valid[64], text[128];  
947      int i_valid;      if (get_pubkey (keyid, &key))
948                return m_strdup (keyid);
949      memset( &tvi, 0, sizeof tvi );      uid = utf8_to_wincp2 (key->uids->name);
950      tvi.hItem = TreeView_GetSelection( gc->tree );      if (!uid)
951      tvi.pszText = text;          return m_strdup (keyid);
952      tvi.cchTextMax = sizeof text -1;      p = new char[strlen (uid) + 8 + 16];
953      tvi.mask = TVIF_TEXT;      if (!p)
954      TreeView_GetItem( gc->tree, &tvi );          BUG (0);
955            sprintf (p, "%s%s.asc", uid, is_secret? "_sec" : "");
956            for (size_t i=0; i < strlen (p); i++) {
957      listview_get_item_text( lv, km_index, 0, uid, sizeof uid -1 );          if (p[i] == ' ' || p[i] == ':' || p[i] == '?' || p[i] == '|')
958      listview_get_item_text( lv, km_index, 5, valid, sizeof valid -1 );              p[i] = '_';
959            }
960      if( strstr( valid, "Ultimate" ) )      safe_free (uid);
961          i_valid = 5;          return p;
962      else if( !strstr( valid, "Full" ) )  }
         i_valid = 4;  
     else if( !strstr( valid, "Marginal" ) )  
         i_valid = 3;  
     else  
         i_valid = 0;  
       
     /* we can't add the full name. one way would be to use the first  
        text until a space appears.  
     group_add_entry(&gc->gh, gid, i_valid, uid);  
     treeview_add_item(gc->tree, tvi.hItem, uid);  
     */  
     gc->need_sync = 1;  
       
     return 0;  
 } /* km_groups_add */  
   
   
 static int  
 km_groups_del_main( km_group_t gc )  
 {  
     TVITEM tvi;  
     char text[128];  
     int id;  
       
     memset( &tvi, 0, sizeof tvi );  
     tvi.hItem = TreeView_GetSelection( gc->tree );  
     tvi.pszText = text;  
     tvi.cchTextMax = sizeof text -1;  
     tvi.mask = TVIF_TEXT;  
     TreeView_GetItem( gc->tree, &tvi );  
                               
     id = log_box( _("Key Manager"), MB_INFO_ASK,  
                    _("Do you really want to delete this group?\n\n%s"), text);  
     if( id == IDNO )  
         return 0;  
     delete_group( gc->gh, text );      
     TreeView_DeleteItem( gc->tree, &tvi );  
     gc->need_sync = 1;  
       
     return 0;  
 } /* km_groups_del */  
   
   
 static int  
 km_groups_del_entry( km_group_t gc )  
 {  
     TVITEM tvi;  
     HTREEITEM root;  
     int id;  
     char text[128], info[256];  
     gpg_group_t  grp = NULL;  
       
     memset( &tvi, 0, sizeof tvi );  
     tvi.hItem = TreeView_GetSelection( gc->tree );  
     tvi.pszText = text;  
     tvi.cchTextMax = sizeof text-1;  
     tvi.mask = TVIF_TEXT;  
     TreeView_GetItem( gc->tree, &tvi );  
       
     _snprintf( info, sizeof info -1,  
               _("Do you really want to delete this entry?\n\n%s"), text );  
       
     id = msg_box( gc->tree, info, _("Key Manager"), MB_INFO_ASK );  
     if( id == IDNO )  
         return 0;  
   
     root = TreeView_GetParent( gc->tree, tvi.hItem );  
     if( root ) {  
     }  
       
     delete_member( gc->gh, /*fixme*/NULL, text );  
     TreeView_DeleteItem( gc->tree, &tvi );  
     gc->need_sync = 1;  
     return 0;  
 } /* km_groups_del_entry */  
   
   
 int  
 km_groups_del( km_group_t gc )  
 {  
     if ( TreeView_GetParent( gc->tree, TreeView_GetSelection( gc->tree ) ) )  
         return km_groups_del_entry( gc );  
     else  
         return km_groups_del_main( gc );  
 } /* km_groups_del */  

Legend:
Removed from v.19  
changed lines
  Added in v.164

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26