1 |
/* wptKeyserverDlg.cpp - Keyserver dialog |
/* wptKeyserverDlg.cpp - Keyserver dialog |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2007, 2009 Timo Schulz |
3 |
|
* Copyright (C) 2005, 2006 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
16 |
*/ |
*/ |
|
|
|
17 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
#include <config.h> |
19 |
#endif |
#endif |
20 |
|
|
21 |
#include <windows.h> |
#include <windows.h> |
22 |
#include <commctrl.h> |
#include <commctrl.h> |
23 |
#include <malloc.h> |
#include <ctype.h> |
24 |
|
|
25 |
#include "resource.h" |
#include "resource.h" |
|
#include "wptKeyserver.h" |
|
26 |
#include "wptErrors.h" |
#include "wptErrors.h" |
27 |
#include "wptTypes.h" |
#include "wptTypes.h" |
28 |
#include "wptCommonCtl.h" |
#include "wptCommonCtl.h" |
30 |
#include "wptW32API.h" |
#include "wptW32API.h" |
31 |
#include "wptVersion.h" |
#include "wptVersion.h" |
32 |
#include "wptGPG.h" |
#include "wptGPG.h" |
33 |
#include "wptKeyManager.h" |
#include "wptGPGME.h" |
34 |
|
#include "wptKeyserver.h" |
35 |
#include "wptContext.h" /* for passphrase_s */ |
#include "wptContext.h" /* for passphrase_s */ |
36 |
|
#include "wptKeylist.h" |
37 |
|
#include "wptKeyManager.h" |
38 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
39 |
|
#include "wptUTF8.h" |
40 |
|
#include "wptRegistry.h" |
41 |
|
|
42 |
|
|
43 |
#define MAX_KEYSIZE 70000 |
/* Symbolic column IDs */ |
44 |
|
enum { KS_COL_NAME = 0, KS_COL_PORT }; |
|
char * get_reg_entry_keyserver (const char *); |
|
|
int set_reg_entry_keyserver (const char *, const char *); |
|
45 |
|
|
46 |
|
|
47 |
|
/* Print out keyserver error and a possible Winsock error. */ |
48 |
static void |
static void |
49 |
hkp_err_box (HWND dlg, const char * host, u16 port, int rc) |
hkp_err_box (HWND dlg, const char *host, WORD port, int rc) |
50 |
{ |
{ |
51 |
const char *err = kserver_strerror(); |
const char *err = kserver_strerror (); |
52 |
|
const char *ws_err = wsock_strerror (); |
53 |
log_box( _("Keyserver"), MB_ERR, "%s:%d: %s", host, port, winpt_strerror (rc)); |
|
54 |
if (err) |
if (ws_err && strlen (ws_err) > 0) |
55 |
msg_box (dlg, err, wsock_strerror (), MB_ERR); |
log_box (_("Keyserver"), MB_ERR, "%s:%d: %s", host, port, ws_err); |
56 |
} /* hkp_err_box */ |
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 |
int |
66 |
hkp_send_key (HWND dlg, const char *kserver, u16 port, const char *pattern) |
hkp_send_key (HWND dlg, const char *kserver, WORD port, const char *pattern) |
67 |
{ |
{ |
68 |
gpgme_ctx_t c; |
GPGME gpg; |
|
gpgme_data_t keydata; |
|
69 |
gpgme_error_t ec; |
gpgme_error_t ec; |
70 |
char *rawkey = NULL, msg[1024]; |
char *rawkey = NULL; |
71 |
size_t n; |
char msg[256]; |
|
int rc; |
|
72 |
|
|
73 |
ec = gpgme_new( &c ); |
gpg.setArmor (true); |
74 |
if( ec ) |
ec = gpg.exportToBuffer (pattern, &rawkey); |
75 |
BUG( NULL ); |
if (ec) { |
76 |
gpgme_set_armor (c, 1); |
msg_box (dlg, gpgme_strerror (ec), _("Export"), MB_ERR); |
|
ec = gpgme_data_new( &keydata ); |
|
|
if( ec ) |
|
|
BUG( NULL ); |
|
|
rc = (int) gpgme_op_export( c, pattern, 0, keydata ); |
|
|
if( rc ) { |
|
|
msg_box( dlg, gpgme_strerror( (gpgme_error_t)rc ), _("Export"), MB_ERR ); |
|
77 |
goto leave; |
goto leave; |
78 |
} |
} |
79 |
rawkey = gpgme_data_release_and_get_mem (keydata, &n); |
op_begin (); |
80 |
rc = kserver_sendkey (kserver, port, rawkey, n); |
ec = kserver_sendkey (kserver, port, rawkey, strlen (rawkey)); |
81 |
if (rc) { |
op_end (); |
82 |
hkp_err_box (dlg, kserver, port, rc); |
if (ec) { |
83 |
|
hkp_err_box (dlg, kserver, port, ec); |
84 |
goto leave; |
goto leave; |
85 |
} |
} |
86 |
|
|
87 |
_snprintf (msg, sizeof (msg) -1, _("Key '%s' successfully sent"), pattern); |
_snprintf (msg, DIM (msg) -1, _("Key '%s' successfully sent"), pattern); |
88 |
status_box (dlg, msg, _("GnuPG status")); |
status_box (dlg, msg, _("GnuPG Status")); |
89 |
|
|
90 |
leave: |
leave: |
91 |
gpgme_release (c); |
free_if_alloc (rawkey); |
92 |
if (rawkey) |
return ec? -1 : 0; |
93 |
gpgme_free (rawkey); |
} |
|
return rc; |
|
|
} /* hkp_send_key */ |
|
94 |
|
|
95 |
|
|
96 |
int |
|
97 |
hkp_recv_key (HWND dlg, const char *kserver, u16 port, |
/* Show all received keys from the keyserver. */ |
98 |
const char *pattern, int proto, int flags) |
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; |
gpgme_ctx_t ctx; |
105 |
gpgme_data_t keydata; |
gpgme_error_t err; |
106 |
gpgme_error_t ec; |
const char *s; |
107 |
gpgme_import_result_t import_res = NULL; |
char *p, *uid; |
108 |
int rc; |
size_t n=0; |
109 |
char *rawkey = NULL, msg[384]; |
|
110 |
|
if (!res) |
111 |
rawkey = new char[MAX_KEYSIZE]; |
return; |
112 |
if (!rawkey) |
|
113 |
BUG (0); |
err = gpgme_data_new (&msg); |
114 |
memset (rawkey, 0, MAX_KEYSIZE); |
if (err) |
115 |
if( proto == KSPROTO_LDAP ) { |
BUG (NULL); |
116 |
rc = ldap_recvkey( kserver, pattern, rawkey, MAX_KEYSIZE-1 ); |
err = gpgme_new (&ctx); |
117 |
if( rc ) { |
if (err) |
118 |
msg_box( dlg, _("LDAP key import failed.\n" |
BUG (NULL); |
119 |
"Please make sure you have an online connection" |
if (res->considered > 1) { |
120 |
" and gpgkeys_ldap.exe is installed"), |
s = _("WARNING: multiple keys matched request.\n\n"); |
121 |
_("Keyserver"), MB_ERR ); |
gpgme_data_write (msg, s, strlen (s)); |
|
free_if_alloc( rawkey ); |
|
|
return rc; |
|
|
} |
|
122 |
} |
} |
123 |
else if (proto == KSPROTO_FINGER) { |
|
124 |
rc = finger_recvkey (kserver, pattern, rawkey, MAX_KEYSIZE-1); |
if (res->unchanged == res->considered) |
125 |
if (rc) { |
s = _("Key(s) successfully received but nothing was changed."); |
126 |
log_box (_("Keyserver"), MB_ERR, _("Finger key import failed: %s\n"), |
else |
127 |
winpt_strerror (rc)); |
s = _("Key(s) sucessfully received and imported."); |
128 |
free_if_alloc (rawkey); |
// nice to have the amount of new signatures... |
129 |
return rc; |
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 |
else if( ( rc = kserver_recvkey( kserver, port, kserver_check_keyid( pattern ), |
gpgme_data_write (msg, "\0", 1); |
142 |
rawkey, MAX_KEYSIZE-1 ) ) ) { |
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); |
hkp_err_box (dlg, kserver, port, rc); |
170 |
free_if_alloc (rawkey); |
free_if_alloc (rawkey); |
171 |
return rc; |
return rc; |
172 |
} |
} |
173 |
else { |
|
174 |
if( !strstr( rawkey, "BEGIN PGP PUBLIC KEY BLOCK" ) ) { |
if (!strstr (rawkey, "BEGIN PGP PUBLIC KEY BLOCK")) { |
175 |
msg_box( dlg, _("This is not a valid OpenPGP key."), _("Keyserver"), MB_ERR ); |
msg_box (dlg, _("This is not a valid OpenPGP key."), |
176 |
goto leave; |
_("Keyserver"), MB_ERR); |
177 |
} |
goto leave; |
178 |
ec = gpgme_new( &ctx ); |
} |
179 |
if( ec ) |
err = gpg.importFromBuffer (rawkey); |
180 |
BUG( NULL ); |
if (err) { |
181 |
gpgme_data_new_from_mem( &keydata, rawkey, strlen( rawkey ), 1 ); |
msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
182 |
rc = gpgme_op_import( ctx, keydata ); |
goto leave; |
183 |
if( rc ) { |
} |
184 |
msg_box( dlg, gpgme_strerror( (gpgme_error_t)rc ), _("Import"), MB_ERR ); |
import_res = gpg.importGetResult (); |
185 |
goto leave; |
if (import_res && r_fpr) |
186 |
} |
*r_fpr = m_strdup (import_res->imports->fpr); |
|
import_res = gpgme_op_import_result (ctx); |
|
|
} |
|
187 |
|
|
188 |
/* if we use the refresh mode, a lot of keys will be fetched and thus only |
/* 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. */ |
a summarize at the end is presented and not for each key. */ |
190 |
if (!(flags & KM_KS_REFRESH)) { |
if (import_res && !(flags & KM_KS_REFRESH)) { |
191 |
if (import_res && import_res->new_user_ids == 0) { |
show_imported_keys (dlg, import_res); |
192 |
_snprintf (msg, DIM (msg)-1, |
if (import_res->unchanged == import_res->considered) { |
193 |
_("Key '%s' successfully received but nothing was changed."), pattern ); |
rc = WPTERR_GENERAL; /* no keys updated. */ |
|
status_box (dlg, msg, _("GnuPG Status")); |
|
|
rc = WPTERR_GENERAL; |
|
194 |
goto leave; |
goto leave; |
195 |
} |
} |
|
_snprintf (msg, DIM (msg)-1, _("Key '%s' sucessfully received and imported."), pattern); |
|
|
status_box (dlg, msg, _("GnuPG Status")); |
|
196 |
} |
} |
197 |
|
|
198 |
leave: |
leave: |
199 |
free_if_alloc (rawkey); |
free_if_alloc (rawkey); |
|
gpgme_release (ctx); |
|
|
gpgme_data_release (keydata); |
|
|
|
|
200 |
return rc; |
return rc; |
201 |
} /* hkp_recv_key */ |
} |
202 |
|
|
203 |
|
|
204 |
#define my_iskeychar(a) ( ( (a) >='0' && (a) <= '9' ) || ( (a) >= 'A' && (a) <= 'F' ) ) |
/* 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 |
static int |
int |
215 |
check_pattern( const char *pattern ) |
hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
216 |
|
const char *pattern, int proto, char **r_fpr) |
217 |
{ |
{ |
218 |
int rc = 1; |
return keyserver_recv_key (dlg, kserver, port, pattern, |
219 |
|
proto, 0, r_fpr); |
220 |
/* Whitespace are not allowed! */ |
} |
221 |
if( strchr( pattern, ' ') ) { |
|
222 |
rc = WPTERR_GENERAL; |
|
223 |
goto leave; |
/* Utility function to fetch a key from the keyserver |
224 |
} |
based on a given signature (@sig). |
225 |
|
Return 0 on success. */ |
226 |
if( (( strstr( pattern, "0x" ) ) && ( strlen( pattern ) == 10 ) ) |
int |
227 |
|| (strstr(pattern, "0x")) && ( strlen( pattern ) == 18 ) ) { |
fetch_key_from_keyserver (HWND dlg, gpgme_signature_t sig) |
228 |
rc = 0; |
{ |
229 |
goto leave; |
const char *keyid; |
230 |
} |
char timebuf[128]; |
231 |
|
int id; |
232 |
if( (( my_iskeychar( pattern[0] )) && ( strlen( pattern ) == 8 ) ) |
|
233 |
|| (my_iskeychar(pattern[0])) && ( strlen( pattern ) == 16) ) { |
// XXX: we have a problem with v3 keys here |
234 |
rc = 0; |
if (!sig->fpr) |
235 |
goto leave; |
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 |
if( ( strchr( pattern, '@' ) ) && ( strlen( pattern ) >= 3 ) ) { |
} |
257 |
rc = 0; |
|
258 |
goto leave; |
|
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 |
leave: |
} |
|
return rc; |
|
|
} /* check_pattern */ |
|
287 |
|
|
288 |
|
|
289 |
static void |
/* Return human readable name for the proxy protocol. */ |
290 |
set_proxy (HWND dlg) |
#if 0 |
291 |
{ |
static const char* |
292 |
char proxy[384]; |
name_from_proto (int proto) |
293 |
int port = 0; |
{ |
294 |
|
const char *s; |
295 |
strcpy (proxy, "HTTP proxy: "); |
|
296 |
if (kserver_get_proxy (&port)) { |
switch (proto) { |
297 |
char t[256]; |
case PROXY_PROTO_NONE: s = ""; break; |
298 |
const char *http = kserver_get_proxy (&port); |
case PROXY_PROTO_HTTP: s = "HTTP"; break; |
299 |
_snprintf (t, sizeof (t) - 1, "\"%s:%d\"", http, port); |
case PROXY_PROTO_SOCKS5: s = "SOCKS5"; break; |
300 |
strcat (proxy, t); |
default: s = "HTTP"; break; |
301 |
} |
} |
302 |
else |
return s; |
303 |
strcat( proxy, "none" ); |
} |
304 |
SetDlgItemText( dlg, IDC_KEYSERVER_PROXY, proxy ); |
#endif |
|
} /* set_proxy */ |
|
305 |
|
|
306 |
|
|
307 |
static int inline |
static int inline |
313 |
} |
} |
314 |
|
|
315 |
|
|
316 |
static u16 inline |
static WORD inline |
317 |
kserver_get_port (listview_ctrl_t lv) |
kserver_get_port (listview_ctrl_t lv) |
318 |
{ |
{ |
319 |
char buf[16]; |
char buf[16]; |
320 |
|
|
321 |
listview_get_item_text (lv, kserver_get_pos (lv), 3, buf, 15); |
listview_get_item_text (lv, kserver_get_pos (lv), |
322 |
return (u16)strtoul (buf, NULL, 10); |
KS_COL_PORT, buf, DIM (buf)-1); |
323 |
|
return (WORD)strtoul (buf, NULL, 10); |
324 |
} |
} |
325 |
|
|
|
static void |
|
|
load_default_ks (listview_ctrl_t lv) |
|
|
{ |
|
|
char * p, buf[192]; |
|
|
int i; |
|
|
|
|
|
p = get_reg_entry_keyserver ("Default"); |
|
|
if (!p) |
|
|
return; |
|
|
for( i = 0; i < listview_count_items( lv, 0 ); i++ ) { |
|
|
listview_get_item_text( lv, i, 0, buf, sizeof (buf)-1 ); |
|
|
if( !strncmp( p, buf, strlen( p ) ) ) { |
|
|
listview_add_sub_item( lv, i, 2, "x" ); |
|
|
break; |
|
|
} |
|
|
} |
|
|
free_if_alloc (p); |
|
|
} /* load_default_ks */ |
|
|
|
|
|
|
|
|
static int |
|
|
save_default_ks (listview_ctrl_t lv) |
|
|
{ |
|
|
char buf[192], port[32]; |
|
|
int idx, i; |
|
|
|
|
|
idx = listview_get_curr_pos( lv ); |
|
|
if( idx == -1 ) { |
|
|
msg_box( NULL, _("Please select one of the servers."), _("Keyserver"), MB_ERR ); |
|
|
return -1; |
|
|
} |
|
|
listview_get_item_text( lv, idx, 1, buf, sizeof (buf)-1 ); |
|
|
if( *buf != 'H' ) { |
|
|
msg_box( NULL, _("Only HTTP keyserver can be used."), _("Keyserver"), MB_ERR ); |
|
|
return -1; |
|
|
} |
|
|
for (i = 0; i < listview_count_items( lv, 0 ); i++) |
|
|
listview_add_sub_item (lv, i, 2, ""); |
|
|
listview_add_sub_item (lv, idx, 2, "x"); |
|
|
listview_get_item_text (lv, idx, 0, buf, sizeof (buf)-1); |
|
|
set_reg_entry_keyserver ("Default", buf); |
|
|
i = kserver_get_port (lv); |
|
|
sprintf (port, "%d", i); |
|
|
set_reg_entry_keyserver ("Default_Port", port); |
|
|
keyserver_set_default (buf, (u16)i); |
|
|
return 0; |
|
|
} /* save_default_ks */ |
|
|
|
|
326 |
|
|
327 |
int |
int |
328 |
keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd) |
keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd) |
329 |
{ |
{ |
330 |
struct listview_column_s keyserver[] = { |
struct listview_column_s keyserver[] = { |
331 |
{0, 160, (char *)_("DNS Name")}, |
{0, 180, (char *)_("DNS Name")}, |
332 |
{1, 52, (char *)_("Protocol")}, |
{1, 55, (char *)_("Port")}, |
|
{2, 46, (char *)_("Default")}, |
|
|
{3, 46, (char *)_("Port")}, |
|
333 |
{0, 0, NULL} |
{0, 0, NULL} |
334 |
}; |
}; |
335 |
|
HICON ico[1]; |
336 |
listview_ctrl_t lv; |
listview_ctrl_t lv; |
337 |
char buf[32]; |
char buf[32]; |
338 |
int j; |
int j; |
339 |
|
|
340 |
listview_new (&lv); |
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER); |
341 |
lv->ctrl = hwnd; |
listview_new (&lv, hwnd); |
342 |
for (j=0; keyserver[j].fieldname; j++) |
for (j=0; keyserver[j].fieldname; j++) |
343 |
listview_add_column (lv, &keyserver[j]); |
listview_add_column (lv, &keyserver[j]); |
344 |
for (j = 0; j<MAX_KEYSERVERS; j++) { |
listview_set_image_list (lv, 16, 16, ico, 1); |
345 |
|
for (j = 0; j < MAX_KEYSERVERS; j++) { |
346 |
if (!server[j].used) |
if (!server[j].used) |
347 |
continue; |
continue; |
348 |
listview_add_item (lv, " "); |
listview_add_item_image (lv, " ", 0); |
349 |
listview_add_sub_item (lv, 0, 0, server[j].name); |
listview_add_sub_item (lv, 0, KS_COL_NAME, server[j].name); |
|
switch (server[j].proto) { |
|
|
case KSPROTO_HTTP: |
|
|
listview_add_sub_item( lv, 0, 1, "H" ); break; |
|
|
case KSPROTO_LDAP: |
|
|
listview_add_sub_item( lv, 0, 1, "L" ); break; |
|
|
case KSPROTO_FINGER: |
|
|
listview_add_sub_item( lv, 0, 1, "F" ); break; |
|
|
} |
|
350 |
sprintf (buf, "%d", server[j].port); |
sprintf (buf, "%d", server[j].port); |
351 |
listview_add_sub_item (lv, 0, 3, buf); |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
|
} |
|
|
load_default_ks (lv); |
|
|
if (listview_count_items (lv, 0) == 0) { |
|
|
listview_add_item (lv, ""); |
|
|
listview_add_sub_item (lv, 0, 0, DEF_HKP_KEYSERVER); |
|
|
listview_add_sub_item (lv, 0, 1, "H"); |
|
352 |
} |
} |
353 |
listview_set_ext_style (lv); |
listview_set_ext_style (lv); |
354 |
*r_lv = lv; |
*r_lv = lv; |
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 |
BOOL CALLBACK |
443 |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
444 |
{ |
{ |
445 |
static listview_ctrl_t lv = NULL; |
static listview_ctrl_t lv = NULL; |
446 |
static int lv_idx = 0; |
static int lv_idx = 0; |
|
int rc, proto_nr = 0; |
|
|
char kserver[128], pattern[128]; |
|
|
char proto[16]; |
|
447 |
keyserver_ctx ksc; |
keyserver_ctx ksc; |
448 |
|
char kserver[128], pattern[128]; |
449 |
|
char *fpr = NULL; |
450 |
|
int rc, proto_nr = 0; |
451 |
|
|
452 |
switch ( msg ) { |
switch (msg) { |
453 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
454 |
SetWindowText (dlg, _("Keyserver Access")); |
SetWindowText (dlg, _("Keyserver Access")); |
455 |
SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive")); |
SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive")); |
456 |
SetDlgItemText (dlg, IDC_KEYSERVER_SEND, |
SetDlgItemText (dlg, IDC_KEYSERVER_SEND, |
457 |
_("Send key (default is receiving)")); |
_("Send key (default is receiving)")); |
458 |
SetDlgItemText (dlg, IDC_KEYSERVER_INFO, |
SetDlgItemText (dlg, IDC_KEYSERVER_INFO, |
459 |
_("Please enter the key ID or email address you search for")); |
_("Key ID or email address you want to search for")); |
460 |
SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search")); |
SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search")); |
461 |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange")); |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange proxy")); |
462 |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
463 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
464 |
|
|
465 |
set_proxy (dlg); |
load_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
466 |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
467 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
468 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
471 |
case WM_NOTIFY: |
case WM_NOTIFY: |
472 |
NMHDR *notify; |
NMHDR *notify; |
473 |
notify = (NMHDR *)lparam; |
notify = (NMHDR *)lparam; |
474 |
if( notify && notify->code == NM_CLICK |
if (!notify) |
475 |
&& notify->idFrom == IDC_KEYSERVER_LIST ) |
break; |
476 |
lv_idx = listview_get_curr_pos( lv ); |
if (notify->code == (UINT)NM_CLICK |
477 |
return TRUE; |
&& notify->idFrom == (UINT)IDC_KEYSERVER_LIST) |
478 |
|
lv_idx = listview_get_curr_pos (lv); |
479 |
|
break; |
480 |
|
|
481 |
case WM_DESTROY: |
case WM_DESTROY: |
482 |
if( lv ) { |
balloon_msg_disable (); |
483 |
listview_release( lv ); |
if (lv) { |
484 |
|
listview_release (lv); |
485 |
lv = NULL; |
lv = NULL; |
486 |
} |
} |
487 |
lv_idx = 0; |
lv_idx = 0; |
488 |
return FALSE; |
save_pattern_from_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
489 |
|
break; |
490 |
case WM_SYSCOMMAND: |
|
|
if( LOWORD( wparam ) == SC_CLOSE ) |
|
|
EndDialog( dlg, TRUE ); |
|
|
return FALSE; |
|
|
|
|
491 |
case WM_COMMAND: |
case WM_COMMAND: |
492 |
switch( LOWORD( wparam ) ) { |
switch (LOWORD (wparam)) { |
493 |
case IDC_KEYSERVER_PROXSETT: |
case IDC_KEYSERVER_PROXSETT: |
494 |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, glob_hwnd, |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, |
495 |
keyserver_proxy_dlg_proc, 0, |
dlg, keyserver_proxy_dlg_proc, 0, |
496 |
_("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY ); |
_("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY); |
|
set_proxy( dlg ); |
|
497 |
return TRUE; |
return TRUE; |
498 |
|
|
499 |
case IDC_KEYSERVER_INDEX: |
case IDC_KEYSERVER_INDEX: |
500 |
if (!lv_idx) { |
if (!lv_idx) |
501 |
lv_idx = kserver_get_pos (lv); |
lv_idx = kserver_get_pos (lv); |
502 |
if (lv_idx == -1) { |
|
503 |
msg_box (dlg, _("Please select one of the keyservers."), _("Keyserver"), MB_INFO); |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, |
504 |
return FALSE; |
pattern, DIM (pattern)-1)) { |
505 |
} |
show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_SEARCH), |
506 |
} |
_("Please enter the search pattern."), |
507 |
listview_get_item_text (lv, lv_idx, 1, proto, sizeof (proto)-1); |
IDI_WARNING); |
|
if (*proto == 'L') { |
|
|
msg_box( dlg, _("This is not implemented yet!"), _("Keyserver"), MB_ERR ); |
|
|
return FALSE; |
|
|
} |
|
|
listview_get_item_text (lv, lv_idx, 0, kserver, sizeof (kserver)-1); |
|
|
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
|
|
msg_box (dlg, _("Please enter the search pattern."), _("Keyserver"), MB_INFO); |
|
508 |
return FALSE; |
return FALSE; |
509 |
} |
} |
510 |
ksc.name = kserver; |
|
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; |
ksc.pattern = pattern; |
522 |
ksc.port = kserver_get_port (lv); |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg, |
523 |
DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg, |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
|
hkpsearch_dlg_proc, (LPARAM) &ksc ); |
|
524 |
return TRUE; |
return TRUE; |
525 |
|
|
526 |
case IDC_KEYSERVER_RECV: |
case IDC_KEYSERVER_RECV: |
527 |
memset (&kserver, 0, sizeof (kserver)); |
memset (&kserver, 0, sizeof (kserver)); |
528 |
if (!lv_idx) { |
if (!lv_idx) { |
529 |
lv_idx = kserver_get_pos (lv); |
lv_idx = kserver_get_pos (lv); |
530 |
if (lv_idx == -1) { |
if (lv_idx == -1) { |
531 |
msg_box( dlg, _("Please select one of the keyservers."), _("Keyserver"), MB_INFO ); |
show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_LIST), |
532 |
|
_("Please select one of the keyservers."), |
533 |
|
IDI_WARNING); |
534 |
return FALSE; |
return FALSE; |
535 |
} |
} |
536 |
} |
} |
|
listview_get_item_text( lv, lv_idx, 1, proto, sizeof (proto)-1 ); |
|
537 |
proto_nr = KSPROTO_HTTP; |
proto_nr = KSPROTO_HTTP; |
538 |
if( *proto == 'L' ) |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
539 |
proto_nr = KSPROTO_LDAP; |
kserver, DIM (kserver)-1); |
540 |
else if( *proto == 'F' ) |
if (!GetDlgItemText(dlg, IDC_KEYSERVER_SEARCH, |
541 |
proto_nr = KSPROTO_FINGER; |
pattern, DIM (pattern)-1)) { |
542 |
listview_get_item_text( lv, lv_idx, 0, kserver, sizeof (kserver)-1 ); |
show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_SEARCH), |
543 |
if( !GetDlgItemText( dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1 ) ) { |
_("Please enter the search pattern."), |
544 |
msg_box( dlg, _("Please enter the search pattern."), _("Keyserver"), MB_INFO ); |
IDI_WARNING); |
545 |
return FALSE; |
return FALSE; |
546 |
} |
} |
547 |
if( proto_nr == KSPROTO_LDAP && strchr( pattern, '@' ) ) { |
if (check_pattern (pattern)) { |
548 |
msg_box( dlg, _("Only keyids are allowed."), _("LDAP Keyserver"), MB_INFO ); |
show_balloon_msg (GetDlgItem (dlg, IDC_KEYSERVER_SEARCH), |
549 |
|
_("Only email addresses or keyids are allowed."), |
550 |
|
IDI_ERROR); |
551 |
return FALSE; |
return FALSE; |
552 |
} |
} |
553 |
else if (proto_nr == KSPROTO_FINGER) { |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
554 |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
pattern, proto_nr, 0, &fpr); |
555 |
msg_box (dlg, _("Only enter the name of the user."), _("FINGER Keyserver"), MB_INFO); |
if (!rc && fpr != NULL) { |
556 |
return FALSE; |
keycache_update (0, fpr); |
557 |
} |
free_if_alloc (fpr); |
558 |
} |
} |
|
else if( check_pattern( pattern ) ) { |
|
|
msg_box( dlg, _("Only email addresses or keyids are allowed."), _("HKP Keyserver"), MB_INFO ); |
|
|
return FALSE; |
|
|
} |
|
|
rc = hkp_recv_key (dlg, kserver, kserver_get_port (lv), pattern, proto_nr, 0); |
|
559 |
if (!rc) |
if (!rc) |
560 |
keycache_set_reload (1); |
add_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, pattern); |
561 |
return TRUE; |
return TRUE; |
562 |
|
|
|
case IDC_KEYSERVER_DEFAULT: |
|
|
save_default_ks( lv ); |
|
|
break; |
|
|
|
|
563 |
case IDCANCEL: |
case IDCANCEL: |
564 |
EndDialog( dlg, FALSE ); |
EndDialog (dlg, FALSE); |
565 |
return FALSE; |
return TRUE; |
566 |
} |
} |
567 |
break; |
break; |
568 |
} |
} |
569 |
|
|
570 |
return FALSE; |
return FALSE; |
571 |
} /* keyserver_dlg_proc */ |
} |