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

Annotation of /trunk/Src/wptKeyManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations)
Wed Oct 26 11:20:09 2005 UTC (19 years, 4 months ago) by twoaday
File size: 30618 byte(s)
2005-10-25  Timo Schulz  <twoaday@g10code.com>
                                                                                
        * wptGPGUtil.cpp (create_process): Hide window.
        * wptKeyPropsDlg.cpp (get_photo_tmpname): New.
        * wptClipSignEncDlg.cpp (clip_signenc_dlg_proc): Remove
        static var 'enable'.
        * wptKeygenDlg.cpp (keygen_dlg_proc): Likewise.
        (gpg_genkey_params): Make sure all primary keys are capable
        for signing and certification.
        * wptKeySigDlg.cpp (is_sig): If no item is selected, return 0.
        * wptGPG.cpp (gnupg_access_keyring): Check return value for
        NULL. Noted by Ralf.
        (get_gnupg_prog): Simplified.
        (check_homedir): Fixed. Return 0 when the dir is successfully created.
        * wptKeyManagerDlg.cpp (km_file_import): Use the hourglass to
        indicate a pending GPG process.
        * wptFileManager.cpp (op_begin, op_end): New. Indicate an start
        and and of an operation. For now just the cursor changes.
        (fm_parse_command_line): Remove debug output. Thanks to Ralf again.
        * WinPT.cpp (WinMain): Check if there is already an instance and
        set a variable early as possible.
        (load_gettext): If a previous instance was found, do not output
        any errors. Kudos to Ralf.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26