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

Annotation of /trunk/Src/wptKeyManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (hide annotations)
Sat Oct 8 10:43:08 2005 UTC (19 years, 4 months ago) by twoaday
File size: 30075 byte(s)
Bug fixes to correct some problems introduced by
the MyGPGME to GPGME port.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26