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

Contents of /trunk/Src/wptKeyserverDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 15760 byte(s)


1 /* wptKeyserverDlg.cpp - Keyserver dialog
2 * Copyright (C) 2000-2007, 2009 Timo Schulz
3 * Copyright (C) 2005, 2006 g10 Code GmbH
4 *
5 * This file is part of WinPT.
6 *
7 * WinPT is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <windows.h>
22 #include <commctrl.h>
23 #include <ctype.h>
24
25 #include "resource.h"
26 #include "wptErrors.h"
27 #include "wptTypes.h"
28 #include "wptCommonCtl.h"
29 #include "wptNLS.h"
30 #include "wptW32API.h"
31 #include "wptVersion.h"
32 #include "wptGPG.h"
33 #include "wptGPGME.h"
34 #include "wptKeyserver.h"
35 #include "wptContext.h" /* for passphrase_s */
36 #include "wptKeylist.h"
37 #include "wptKeyManager.h"
38 #include "wptDlgs.h"
39 #include "wptUTF8.h"
40 #include "wptRegistry.h"
41
42
43 /* Symbolic column IDs */
44 enum { KS_COL_NAME = 0, KS_COL_PORT };
45
46
47 /* Print out keyserver error and a possible Winsock error. */
48 static void
49 hkp_err_box (HWND dlg, const char *host, WORD port, int rc)
50 {
51 const char *err = kserver_strerror ();
52 const char *ws_err = wsock_strerror ();
53
54 if (ws_err && strlen (ws_err) > 0)
55 log_box (_("Keyserver"), MB_ERR, "%s:%d: %s", host, port, ws_err);
56 else
57 log_box (_("Keyserver"), MB_ERR, "%s:%d: %s\n%s",
58 host, port, winpt_strerror (rc), err? err : "");
59 }
60
61
62 /* Send the extracted key with keyid @pattern to the
63 keyserver @kserver (port @port).
64 Return value: 0 on success. */
65 int
66 hkp_send_key (HWND dlg, const char *kserver, WORD port, const char *pattern)
67 {
68 GPGME gpg;
69 gpgme_error_t ec;
70 char *rawkey = NULL;
71 char msg[256];
72
73 gpg.setArmor (true);
74 ec = gpg.exportToBuffer (pattern, &rawkey);
75 if (ec) {
76 msg_box (dlg, gpgme_strerror (ec), _("Export"), MB_ERR);
77 goto leave;
78 }
79 op_begin ();
80 ec = kserver_sendkey (kserver, port, rawkey, strlen (rawkey));
81 op_end ();
82 if (ec) {
83 hkp_err_box (dlg, kserver, port, ec);
84 goto leave;
85 }
86
87 _snprintf (msg, DIM (msg) -1, _("Key '%s' successfully sent"), pattern);
88 status_box (dlg, msg, _("GnuPG Status"));
89
90 leave:
91 free_if_alloc (rawkey);
92 return ec? -1 : 0;
93 }
94
95
96
97 /* Show all received keys from the keyserver. */
98 static void
99 show_imported_keys (HWND dlg, gpgme_import_result_t res)
100 {
101 gpgme_import_status_t t;
102 gpgme_key_t key;
103 gpgme_data_t msg;
104 gpgme_ctx_t ctx;
105 gpgme_error_t err;
106 const char *s;
107 char *p, *uid;
108 size_t n=0;
109
110 if (!res)
111 return;
112
113 err = gpgme_data_new (&msg);
114 if (err)
115 BUG (NULL);
116 err = gpgme_new (&ctx);
117 if (err)
118 BUG (NULL);
119 if (res->considered > 1) {
120 s = _("WARNING: multiple keys matched request.\n\n");
121 gpgme_data_write (msg, s, strlen (s));
122 }
123
124 if (res->unchanged == res->considered)
125 s = _("Key(s) successfully received but nothing was changed.");
126 else
127 s = _("Key(s) sucessfully received and imported.");
128 // nice to have the amount of new signatures...
129 gpgme_data_write (msg, s, strlen (s));
130 gpgme_data_write (msg, "\n\n", 2);
131
132 for (t=res->imports; t; t = t->next) {
133 if (!gpgme_get_key (ctx, t->fpr, &key, 0)) {
134 s = uid = utf8_to_native (key->uids->uid);
135 gpgme_data_write (msg, s, strlen (s));
136 gpgme_data_write (msg, "\n", 1);
137 gpgme_key_release (key);
138 safe_free (uid);
139 }
140 }
141 gpgme_data_write (msg, "\0", 1);
142 p = gpgme_data_release_and_get_mem (msg, &n);
143 if (p != NULL) {
144 msg_box (dlg, p, _("Imported Keys"), MB_INFO);
145 gpgme_free (p);
146 }
147 gpgme_release (ctx);
148 }
149
150
151 /* Receive a key from the keyserver @kserver (port @port)
152 with the pattern @pattern.
153 Return value: 0 on success. */
154 static int
155 keyserver_recv_key (HWND dlg, const char *kserver, WORD port,
156 const char *pattern, int proto, int flags,
157 char **r_fpr)
158 {
159 gpgme_import_result_t import_res = NULL;
160 gpgme_error_t err;
161 GPGME gpg;
162 char *rawkey = NULL;
163 size_t keylen = 0;
164 int rc;
165
166 if ((rc = kserver_recvkey (kserver, port,
167 kserver_check_keyid (pattern),
168 &rawkey, &keylen))) {
169 hkp_err_box (dlg, kserver, port, rc);
170 free_if_alloc (rawkey);
171 return rc;
172 }
173
174 if (!strstr (rawkey, "BEGIN PGP PUBLIC KEY BLOCK")) {
175 msg_box (dlg, _("This is not a valid OpenPGP key."),
176 _("Keyserver"), MB_ERR);
177 goto leave;
178 }
179 err = gpg.importFromBuffer (rawkey);
180 if (err) {
181 msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR);
182 goto leave;
183 }
184 import_res = gpg.importGetResult ();
185 if (import_res && r_fpr)
186 *r_fpr = m_strdup (import_res->imports->fpr);
187
188 /* if we use the refresh mode, a lot of keys will be fetched and thus only
189 a summarize at the end is presented and not for each key. */
190 if (import_res && !(flags & KM_KS_REFRESH)) {
191 show_imported_keys (dlg, import_res);
192 if (import_res->unchanged == import_res->considered) {
193 rc = WPTERR_GENERAL; /* no keys updated. */
194 goto leave;
195 }
196 }
197
198 leave:
199 free_if_alloc (rawkey);
200 return rc;
201 }
202
203
204 /* Alias for keyserver_recv_key but without the ability to return
205 the fingerprint of the key. */
206 int
207 hkp_recv_key (HWND dlg, const char *kserver, WORD port,
208 const char *pattern, int proto, int flags)
209 {
210 return keyserver_recv_key (dlg, kserver, port, pattern,
211 proto, flags, NULL);
212 }
213
214 int
215 hkp_recv_key2 (HWND dlg, const char *kserver, WORD port,
216 const char *pattern, int proto, char **r_fpr)
217 {
218 return keyserver_recv_key (dlg, kserver, port, pattern,
219 proto, 0, r_fpr);
220 }
221
222
223 /* Utility function to fetch a key from the keyserver
224 based on a given signature (@sig).
225 Return 0 on success. */
226 int
227 fetch_key_from_keyserver (HWND dlg, gpgme_signature_t sig)
228 {
229 const char *keyid;
230 char timebuf[128];
231 int id;
232
233 // XXX: we have a problem with v3 keys here
234 if (!sig->fpr)
235 return FALSE;
236 if (!get_locale_timedate (sig->timestamp, timebuf, DIM (timebuf)-1))
237 _snprintf (timebuf, DIM (timebuf)-1, "'unknown time'");
238 keyid = get_keyid_from_fpr (sig->fpr);
239 id = log_box (_("Verify"), MB_INFO|MB_YESNO,
240 _("Signature made %s using %s key ID 0x%s\n"
241 "Cannot check signature: public key not found\n\n"
242 "Do you want to try to retrieve the key from the keyserver?"),
243 timebuf, get_key_pubalgo (sig->pubkey_algo), keyid);
244 if (id == IDNO) {
245 msg_box (dlg, get_gpg_sigstat (GPGME_SIGSUM_KEY_MISSING),
246 _("Verify"), MB_WARN);
247 return -1;
248 }
249
250 if (!hkp_recv_key (dlg, default_keyserver, default_keyserver_port,
251 keyid, 0, 0)) {
252 keycache_update (0, keyid);
253 return 0;
254 }
255 return -1;
256 }
257
258
259 /* Check if the given pattern are either a valid
260 email address, a {long, short} keyid or a fingerprint.
261 Return 0 on success. */
262 static int
263 check_pattern (const char *pattern)
264 {
265 size_t i;
266
267 if (strchr (pattern, ' '))
268 return WPTERR_GENERAL; /* do not allow white spaces. */
269
270 if (strchr (pattern, '@') && strlen (pattern) >= 3 &&
271 !check_email_address (pattern))
272 return 0;
273
274 if (strstr (pattern, "0x"))
275 pattern += 2;
276 if (strlen (pattern) != 8 &&
277 strlen (pattern) != 16 &&
278 strlen (pattern) != 40)
279 return WPTERR_GENERAL;
280
281 for (i=0; i < strlen (pattern); i++) {
282 if (!isxdigit (pattern[i]))
283 return WPTERR_GENERAL;
284 }
285 return 0;
286 }
287
288
289 /* Return human readable name for the proxy protocol. */
290 #if 0
291 static const char*
292 name_from_proto (int proto)
293 {
294 const char *s;
295
296 switch (proto) {
297 case PROXY_PROTO_NONE: s = ""; break;
298 case PROXY_PROTO_HTTP: s = "HTTP"; break;
299 case PROXY_PROTO_SOCKS5: s = "SOCKS5"; break;
300 default: s = "HTTP"; break;
301 }
302 return s;
303 }
304 #endif
305
306
307 static int inline
308 kserver_get_pos (listview_ctrl_t lv)
309 {
310 if (listview_count_items (lv, 0) == 1)
311 return 0;
312 return listview_get_curr_pos (lv);
313 }
314
315
316 static WORD inline
317 kserver_get_port (listview_ctrl_t lv)
318 {
319 char buf[16];
320
321 listview_get_item_text (lv, kserver_get_pos (lv),
322 KS_COL_PORT, buf, DIM (buf)-1);
323 return (WORD)strtoul (buf, NULL, 10);
324 }
325
326
327 int
328 keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd)
329 {
330 struct listview_column_s keyserver[] = {
331 {0, 180, (char *)_("DNS Name")},
332 {1, 55, (char *)_("Port")},
333 {0, 0, NULL}
334 };
335 HICON ico[1];
336 listview_ctrl_t lv;
337 char buf[32];
338 int j;
339
340 ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER);
341 listview_new (&lv, hwnd);
342 for (j=0; keyserver[j].fieldname; j++)
343 listview_add_column (lv, &keyserver[j]);
344 listview_set_image_list (lv, 16, 16, ico, 1);
345 for (j = 0; j < MAX_KEYSERVERS; j++) {
346 if (!server[j].used)
347 continue;
348 listview_add_item_image (lv, " ", 0);
349 listview_add_sub_item (lv, 0, KS_COL_NAME, server[j].name);
350 sprintf (buf, "%d", server[j].port);
351 listview_add_sub_item (lv, 0, KS_COL_PORT, buf);
352 }
353 listview_set_ext_style (lv);
354 *r_lv = lv;
355 return 0;
356 }
357
358
359 #if 0
360 static void
361 fill_keyserv_types (HWND dlg)
362 {
363 HWND h = GetDlgItem (dlg, IDC_KSERVADD_TYPES);
364 combox_add_string (h, _("HKP Keyserver"));
365 SendMessage (h, CB_SETCURSEL, 0, 0);
366 }
367 #endif
368
369
370 /* XXX: factor out the pattern code for generic use. */
371
372 /* Add search pattern @patt to the combo box if it is
373 not already available in the list. */
374 static void
375 add_pattern_to_combox (HWND dlg, int ctlid, const char *patt)
376 {
377 int err;
378
379 err = SendDlgItemMessage (dlg, ctlid, CB_FINDSTRINGEXACT,
380 0, (LPARAM)(LPCSTR)patt);
381 if (err != CB_ERR)
382 return;
383 SendDlgItemMessage (dlg, IDC_KEYSERVER_SEARCH, CB_ADDSTRING,
384 0, (LPARAM)(LPCSTR)patt);
385 }
386
387
388 /* Restore saved pattern from the registry back to the combo box.
389 If no pattern were saved, do nothing and just return. */
390 static void
391 load_pattern_to_combox (HWND dlg, int ctlid, const char *rkey_name)
392 {
393 char *p, *tok;
394
395 p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name);
396 if (!p || strlen (p) < 2) {
397 free_if_alloc (p);
398 return;
399 }
400 tok = strtok (p, "$");
401 while (tok != NULL) {
402 SendDlgItemMessage (dlg, ctlid, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)tok);
403 tok = strtok (NULL, "$");
404 }
405 free_if_alloc (p);
406 }
407
408
409 /* Save all entries from the combo box given in @ctlid to the
410 registry. Concat all values and use '$' as a separator. */
411 static void
412 save_pattern_from_combox (HWND dlg, int ctlid, const char *rkey_name)
413 {
414 char *p, tmp[64];
415 int i;
416 int n, len=0;
417
418 n = SendDlgItemMessage (dlg, ctlid, CB_GETCOUNT, 0, 0);
419 if (n == 0 || n == CB_ERR)
420 return;
421 /* it is very unlikely that the combox contain hundred of pattern
422 but even so we limit the value to 16 which should be sufficient. */
423 if (n > 16) n = 16;
424 for (i=0; i < n; i++)
425 len += SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXTLEN,
426 (WPARAM)i, 0) + 1 + 1;
427 p = new char[len+1];
428 memset (p, 0, len+1);
429 for (i=0; i < n; i++) {
430 memset (tmp, 0, sizeof (tmp));
431 SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXT,
432 (WPARAM)i, (LPARAM)tmp);
433 strcat (p, tmp);
434 strcat (p, "$");
435 }
436 set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name, p);
437 free_if_alloc (p);
438 }
439
440
441 /* Dialog box procedure to access keyservers. */
442 BOOL CALLBACK
443 keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
444 {
445 static listview_ctrl_t lv = NULL;
446 static int lv_idx = 0;
447 keyserver_ctx ksc;
448 char kserver[128], pattern[128];
449 char *fpr = NULL;
450 int rc, proto_nr = 0;
451
452 switch (msg) {
453 case WM_INITDIALOG:
454 SetWindowText (dlg, _("Keyserver Access"));
455 SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive"));
456 SetDlgItemText (dlg, IDC_KEYSERVER_SEND,
457 _("Send key (default is receiving)"));
458 SetDlgItemText (dlg, IDC_KEYSERVER_INFO,
459 _("Key ID or email address you want to search for"));
460 SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search"));
461 SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange proxy"));
462 SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default"));
463 SetDlgItemText (dlg, IDCANCEL, _("&Close"));
464
465 load_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch");
466 keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST));
467 center_window (dlg, NULL);
468 SetForegroundWindow (dlg);
469 return TRUE;
470
471 case WM_NOTIFY:
472 NMHDR *notify;
473 notify = (NMHDR *)lparam;
474 if (!notify)
475 break;
476 if (notify->code == (UINT)NM_CLICK
477 && notify->idFrom == (UINT)IDC_KEYSERVER_LIST)
478 lv_idx = listview_get_curr_pos (lv);
479 break;
480
481 case WM_DESTROY:
482 balloon_msg_disable ();
483 if (lv) {
484 listview_release (lv);
485 lv = NULL;
486 }
487 lv_idx = 0;
488 save_pattern_from_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch");
489 break;
490
491 case WM_COMMAND:
492 switch (LOWORD (wparam)) {
493 case IDC_KEYSERVER_PROXSETT:
494 dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY,
495 dlg, keyserver_proxy_dlg_proc, 0,
496 _("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY);
497 return TRUE;
498
499 case IDC_KEYSERVER_INDEX:
500 if (!lv_idx)
501 lv_idx = kserver_get_pos (lv);
502
503 if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH,
504 pattern, DIM (pattern)-1)) {
505 show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_SEARCH),
506 _("Please enter the search pattern."),
507 IDI_WARNING);
508 return FALSE;
509 }
510
511 if (lv_idx != -1) {
512 listview_get_item_text (lv, lv_idx, KS_COL_NAME,
513 kserver, DIM (kserver)-1);
514 ksc.name = kserver;
515 ksc.port = kserver_get_port (lv);
516 }
517 else {
518 ksc.name = DEF_HKP_KEYSERVER;
519 ksc.port = HKP_PORT;
520 }
521 ksc.pattern = pattern;
522 DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg,
523 hkpsearch_dlg_proc, (LPARAM) &ksc);
524 return TRUE;
525
526 case IDC_KEYSERVER_RECV:
527 memset (&kserver, 0, sizeof (kserver));
528 if (!lv_idx) {
529 lv_idx = kserver_get_pos (lv);
530 if (lv_idx == -1) {
531 show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_LIST),
532 _("Please select one of the keyservers."),
533 IDI_WARNING);
534 return FALSE;
535 }
536 }
537 proto_nr = KSPROTO_HTTP;
538 listview_get_item_text (lv, lv_idx, KS_COL_NAME,
539 kserver, DIM (kserver)-1);
540 if (!GetDlgItemText(dlg, IDC_KEYSERVER_SEARCH,
541 pattern, DIM (pattern)-1)) {
542 show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_SEARCH),
543 _("Please enter the search pattern."),
544 IDI_WARNING);
545 return FALSE;
546 }
547 if (check_pattern (pattern)) {
548 show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_SEARCH),
549 _("Only email addresses or keyids are allowed."),
550 IDI_ERROR);
551 return FALSE;
552 }
553 rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv),
554 pattern, proto_nr, 0, &fpr);
555 if (!rc && fpr != NULL) {
556 keycache_update (0, fpr);
557 free_if_alloc (fpr);
558 }
559 if (!rc)
560 add_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, pattern);
561 return TRUE;
562
563 case IDCANCEL:
564 EndDialog (dlg, FALSE);
565 return TRUE;
566 }
567 break;
568 }
569
570 return FALSE;
571 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26