1 |
/* wptKeyserverSearchDlg.cpp - Keyserver Searching |
2 |
* Copyright (C) 2001-2007 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* WinPT is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
*/ |
16 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#include <windows.h> |
21 |
#include <stdio.h> |
22 |
|
23 |
#include "resource.h" |
24 |
#include "wptCommonCtl.h" |
25 |
#include "wptGPG.h" |
26 |
#include "wptKeyserver.h" |
27 |
#include "wptErrors.h" |
28 |
#include "wptTypes.h" |
29 |
#include "wptDlgs.h" |
30 |
#include "wptNLS.h" |
31 |
#include "wptKeylist.h" |
32 |
|
33 |
|
34 |
/* Default threshold for key items. */ |
35 |
#define MAX_N_KEYS 64 |
36 |
|
37 |
/* Symbolic column IDs. */ |
38 |
enum { KSS_COL_BITS = 0, KSS_COL_ALGO, |
39 |
KSS_COL_KEYID, KSS_COL_DATE, KSS_COL_UID }; |
40 |
|
41 |
int hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
42 |
const char *pattern, int proto, char **r_fpr); |
43 |
|
44 |
/* Initialize dialog. */ |
45 |
static listview_ctrl_t |
46 |
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
47 |
{ |
48 |
struct listview_column_s keysearch[] = { |
49 |
{0, 48, (char *)_("Size")}, |
50 |
{1, 48, (char*)_("Algorithm")}, |
51 |
{2, 80, (char *)_("Key ID")}, |
52 |
{3, 72, (char *)_("Creation")}, |
53 |
{4, 256, (char *)_("User ID")}, |
54 |
{0, 0, NULL} |
55 |
}; |
56 |
listview_ctrl_t lv = NULL; |
57 |
char info[300]; |
58 |
int i; |
59 |
|
60 |
SetDlgItemText (dlg, IDC_HKPSEARCH_RECV, _("&Receive")); |
61 |
SetDlgItemText (dlg, IDOK, _("&Cancel")); |
62 |
_snprintf (info, DIM (info) - 1, |
63 |
_("Connect to '%s' to search for \"%s\""), |
64 |
((keyserver_ctx *)lparam)->name, |
65 |
((keyserver_ctx *)lparam)->pattern); |
66 |
SetDlgItemText (dlg, IDC_HKPSEARCH_INFO, info); |
67 |
SetWindowText (dlg, _("Keyserver Searching")); |
68 |
SetForegroundWindow (dlg); |
69 |
listview_new (&lv, GetDlgItem (dlg, IDC_HKPSEARCH_LIST)); |
70 |
for (i = 0; keysearch[i].width; i++) |
71 |
listview_add_column (lv, &keysearch[i]); |
72 |
listview_set_ext_style (lv); |
73 |
return lv; |
74 |
} |
75 |
|
76 |
|
77 |
/* Begin the key serach and add all results to the list view @lv. |
78 |
Return value: 0 on success. */ |
79 |
static int |
80 |
search_hkp_keys (HWND dlg, keyserver_ctx *ksc, listview_ctrl_t lv) |
81 |
{ |
82 |
keyserver_key_s *key; |
83 |
const char *uid; |
84 |
char bits[32], algo[32], keyid[32]; |
85 |
size_t nkeys=0; |
86 |
int conn_fd = 0; |
87 |
int rc; |
88 |
|
89 |
rc = kserver_search_begin (ksc->name, ksc->port, ksc->pattern, |
90 |
&conn_fd, &nkeys); |
91 |
if (rc) { |
92 |
msg_box (dlg, winpt_strerror (rc), _("Keyserver Search"), MB_ERR); |
93 |
return rc; |
94 |
} |
95 |
|
96 |
if (nkeys > MAX_N_KEYS) { |
97 |
rc = log_box (_("Keyserver Search"), MB_INFO_ASK, |
98 |
_("The search result contains a lot of keys: %d\n\n" |
99 |
"Do you really want to continue?"), nkeys); |
100 |
if (rc == IDNO) { |
101 |
kserver_search_end (conn_fd); |
102 |
return -1; |
103 |
} |
104 |
} |
105 |
|
106 |
while (nkeys-- > 0) { |
107 |
if (kserver_search_next (conn_fd, &key)) |
108 |
break; |
109 |
if (!key) |
110 |
continue; |
111 |
|
112 |
_snprintf (bits, DIM (bits) - 1, "%d", key->bits); |
113 |
_snprintf (algo, DIM (algo)-1, "%s", |
114 |
get_key_pubalgo ((gpgme_pubkey_algo_t)key->algo)); |
115 |
_snprintf (keyid, DIM (keyid)-1, "0x%s", key->keyid); |
116 |
uid = key->main_uid? key->main_uid->uid : ""; |
117 |
listview_add_item (lv, " "); |
118 |
listview_add_sub_item (lv, 0, KSS_COL_BITS, bits); |
119 |
listview_add_sub_item (lv, 0, KSS_COL_ALGO, algo); |
120 |
listview_add_sub_item (lv, 0, KSS_COL_KEYID, keyid); |
121 |
listview_add_sub_item (lv, 0, KSS_COL_DATE, get_key_created (key->creation)); |
122 |
listview_add_sub_item (lv, 0, KSS_COL_UID, uid); |
123 |
kserver_release_key (key); key=NULL; |
124 |
} |
125 |
|
126 |
kserver_search_end (conn_fd); |
127 |
return 0; |
128 |
} |
129 |
|
130 |
|
131 |
/* Fetch all selected keys from the keyserver in @ksc. */ |
132 |
static void |
133 |
fetch_keys (HWND dlg, listview_ctrl_t lv, keyserver_ctx *ksc) |
134 |
{ |
135 |
char keyid[32]; |
136 |
char *fpr; |
137 |
int n, rc = 0; |
138 |
|
139 |
n = listview_count_items (lv, 1); |
140 |
if (!n) { |
141 |
msg_box (dlg, _("Please select a key."), _("Keyserver Search"), MB_ERR); |
142 |
return; |
143 |
} |
144 |
|
145 |
for (n=0; n < listview_count_items (lv, 0); n++) { |
146 |
if (!listview_get_item_state (lv, n)) |
147 |
continue; |
148 |
|
149 |
listview_get_item_text (lv, n, KSS_COL_KEYID, keyid, DIM (keyid)-1); |
150 |
rc = hkp_recv_key2 (dlg, ksc->name, ksc->port, keyid, 0, &fpr); |
151 |
if (!rc && fpr != NULL) |
152 |
keycache_update (0, fpr); |
153 |
free_if_alloc (fpr); |
154 |
} |
155 |
} |
156 |
|
157 |
|
158 |
/* Dialog box procedure for keyserver searching. */ |
159 |
BOOL CALLBACK |
160 |
hkpsearch_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
161 |
{ |
162 |
static listview_ctrl_t lv = NULL; |
163 |
static keyserver_ctx *ksc; |
164 |
|
165 |
switch (msg) { |
166 |
case WM_INITDIALOG: |
167 |
ksc = (keyserver_ctx *)lparam; |
168 |
if (!ksc) |
169 |
BUG (0); |
170 |
lv = on_init_dialog (dlg, msg, wparam, lparam); |
171 |
if (search_hkp_keys (dlg, ksc, lv)) { |
172 |
EndDialog (dlg, FALSE); |
173 |
return TRUE; |
174 |
} |
175 |
return TRUE; |
176 |
|
177 |
case WM_DESTROY: |
178 |
if (lv) { |
179 |
listview_release (lv); |
180 |
lv = NULL; |
181 |
} |
182 |
return FALSE; |
183 |
|
184 |
case WM_COMMAND: |
185 |
switch (LOWORD (wparam)) { |
186 |
case IDOK: |
187 |
EndDialog (dlg, TRUE); |
188 |
return TRUE; |
189 |
|
190 |
case IDCANCEL: |
191 |
EndDialog (dlg, FALSE); |
192 |
return TRUE; |
193 |
|
194 |
case IDC_HKPSEARCH_RECV: |
195 |
fetch_keys (dlg, lv, ksc); |
196 |
return TRUE; |
197 |
} |
198 |
break; |
199 |
} |
200 |
|
201 |
return FALSE; |
202 |
} |