1 |
/* wptKeyserverDlg.cpp - Keyserver dialog |
/* wptKeyserverDlg.cpp - Keyserver dialog |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2007 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* 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> |
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 "wptKeyManager.h" |
37 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
38 |
#include "wptUTF8.h" |
#include "wptUTF8.h" |
39 |
|
#include "wptRegistry.h" |
40 |
#define MAX_KEYSIZE 70000 |
#include "wptKeylist.h" |
41 |
|
|
42 |
char* get_reg_entry_keyserver (const char *); |
char* get_reg_entry_keyserver (const char *); |
43 |
int set_reg_entry_keyserver (const char *, const char *); |
int set_reg_entry_keyserver (const char *, const char *); |
44 |
|
|
45 |
enum { KS_COL_NAME = 0, KS_COL_PORT, KS_COL_DEFAULT }; |
/* Symbolic column IDs */ |
46 |
|
enum { KS_COL_NAME = 0, KS_COL_PORT }; |
47 |
|
|
48 |
/* Callback context to modify (add, edit) keyserver items. */ |
/* Callback context to modify (add, edit) keyserver items. */ |
49 |
struct keyserver_rec_s { |
struct keyserver_rec_s { |
55 |
}; |
}; |
56 |
typedef struct keyserver_rec_s *keyserver_rec_t; |
typedef struct keyserver_rec_s *keyserver_rec_t; |
57 |
|
|
58 |
|
|
59 |
/* Print out keyserver error and a possible Winsock error. */ |
/* Print out keyserver error and a possible Winsock error. */ |
60 |
static void |
static void |
61 |
hkp_err_box (HWND dlg, const char *host, WORD port, int rc) |
hkp_err_box (HWND dlg, const char *host, WORD port, int rc) |
62 |
{ |
{ |
63 |
const char *err = kserver_strerror (); |
const char *err = kserver_strerror (); |
64 |
|
const char *ws_err = wsock_strerror (); |
65 |
log_box (_("Keyserver"), MB_ERR, "%s:%d: %s", |
|
66 |
host, port, winpt_strerror (rc)); |
if (ws_err && strlen (ws_err) > 0) |
67 |
if (err) |
log_box (_("Keyserver"), MB_ERR, "%s:%d: %s", host, port, ws_err); |
68 |
msg_box (dlg, err, wsock_strerror (), MB_ERR); |
else |
69 |
|
log_box (_("Keyserver"), MB_ERR, "%s:%d: %s\n%s", |
70 |
|
host, port, winpt_strerror (rc), err? err : ""); |
71 |
} |
} |
72 |
|
|
73 |
|
|
77 |
int |
int |
78 |
hkp_send_key (HWND dlg, const char *kserver, WORD port, const char *pattern) |
hkp_send_key (HWND dlg, const char *kserver, WORD port, const char *pattern) |
79 |
{ |
{ |
80 |
gpgme_ctx_t ctx; |
GPGME gpg; |
|
gpgme_data_t keydata; |
|
81 |
gpgme_error_t ec; |
gpgme_error_t ec; |
82 |
char *rawkey = NULL; |
char *rawkey = NULL; |
83 |
char msg[384]; |
char msg[256]; |
|
size_t n; |
|
84 |
|
|
85 |
ec = gpgme_new (&ctx); |
gpg.setArmor (true); |
86 |
if (ec) |
ec = gpg.exportToBuffer (pattern, &rawkey); |
|
BUG (NULL); |
|
|
gpgme_set_armor (ctx, 1); |
|
|
ec = gpgme_data_new (&keydata); |
|
|
if (ec) |
|
|
BUG (NULL); |
|
|
ec = gpgme_op_export (ctx, pattern, 0, keydata); |
|
87 |
if (ec) { |
if (ec) { |
88 |
msg_box (dlg, gpgme_strerror (ec), _("Export"), MB_ERR); |
msg_box (dlg, gpgme_strerror (ec), _("Export"), MB_ERR); |
89 |
goto leave; |
goto leave; |
90 |
} |
} |
91 |
rawkey = gpgme_data_release_and_get_mem (keydata, &n); |
op_begin (); |
92 |
ec = kserver_sendkey (kserver, port, rawkey, n); |
ec = kserver_sendkey (kserver, port, rawkey, strlen (rawkey)); |
93 |
|
op_end (); |
94 |
if (ec) { |
if (ec) { |
95 |
hkp_err_box (dlg, kserver, port, ec); |
hkp_err_box (dlg, kserver, port, ec); |
96 |
goto leave; |
goto leave; |
97 |
} |
} |
98 |
|
|
99 |
_snprintf (msg, sizeof (msg) -1, _("Key '%s' successfully sent"), pattern); |
_snprintf (msg, DIM (msg) -1, _("Key '%s' successfully sent"), pattern); |
100 |
status_box (dlg, msg, _("GnuPG status")); |
status_box (dlg, msg, _("GnuPG status")); |
101 |
|
|
102 |
leave: |
leave: |
103 |
gpgme_release (ctx); |
free_if_alloc (rawkey); |
|
if (rawkey) |
|
|
gpgme_free (rawkey); |
|
104 |
return ec? -1 : 0; |
return ec? -1 : 0; |
105 |
} |
} |
106 |
|
|
137 |
s = _("Key(s) successfully received but nothing was changed."); |
s = _("Key(s) successfully received but nothing was changed."); |
138 |
else |
else |
139 |
s = _("Key(s) sucessfully received and imported."); |
s = _("Key(s) sucessfully received and imported."); |
140 |
|
// nice to have the amount of new signatures... |
141 |
gpgme_data_write (msg, s, strlen (s)); |
gpgme_data_write (msg, s, strlen (s)); |
142 |
gpgme_data_write (msg, "\n\n", 2); |
gpgme_data_write (msg, "\n\n", 2); |
143 |
|
|
144 |
for (t=res->imports; t; t = t->next) { |
for (t=res->imports; t; t = t->next) { |
145 |
if (!gpgme_get_key (ctx, t->fpr, &key, 0)) { |
if (!gpgme_get_key (ctx, t->fpr, &key, 0)) { |
146 |
s = uid = utf8_to_wincp2 (key->uids->uid); |
s = uid = utf8_to_native (key->uids->uid); |
147 |
gpgme_data_write (msg, s, strlen (s)); |
gpgme_data_write (msg, s, strlen (s)); |
148 |
gpgme_data_write (msg, "\n", 1); |
gpgme_data_write (msg, "\n", 1); |
149 |
gpgme_key_release (key); |
gpgme_key_release (key); |
150 |
free (uid); |
safe_free (uid); |
151 |
} |
} |
152 |
} |
} |
153 |
gpgme_data_write (msg, "\0", 1); |
gpgme_data_write (msg, "\0", 1); |
167 |
keyserver_recv_key (HWND dlg, const char *kserver, WORD port, |
keyserver_recv_key (HWND dlg, const char *kserver, WORD port, |
168 |
const char *pattern, int proto, int flags, |
const char *pattern, int proto, int flags, |
169 |
char **r_fpr) |
char **r_fpr) |
170 |
{ |
{ |
171 |
gpgme_ctx_t ctx; |
gpgme_import_result_t import_res = NULL; |
172 |
gpgme_data_t keydata; |
gpgme_error_t err; |
173 |
gpgme_error_t ec; |
GPGME gpg; |
|
gpgme_import_result_t import_res = NULL; |
|
174 |
char *rawkey = NULL; |
char *rawkey = NULL; |
175 |
|
size_t keylen = 0; |
176 |
int rc; |
int rc; |
177 |
|
|
|
/* XXX: implement dynamic buffers. */ |
|
|
rawkey = new char[MAX_KEYSIZE]; |
|
|
if (!rawkey) |
|
|
BUG (0); |
|
|
memset (rawkey, 0, MAX_KEYSIZE); |
|
178 |
if (proto == KSPROTO_LDAP) { |
if (proto == KSPROTO_LDAP) { |
179 |
rc = ldap_recvkey (kserver, pattern, rawkey, MAX_KEYSIZE-1); |
rc = ldap_recvkey (kserver, pattern, &rawkey, &keylen); |
180 |
if (rc) { |
if (rc) { |
181 |
msg_box (dlg, _("LDAP key import failed.\n" |
msg_box (dlg, _("LDAP key import failed.\n" |
182 |
"Please make sure you have an online connection" |
"Please make sure you have an online connection" |
187 |
} |
} |
188 |
} |
} |
189 |
else if (proto == KSPROTO_FINGER) { |
else if (proto == KSPROTO_FINGER) { |
190 |
rc = finger_recvkey (kserver, pattern, rawkey, MAX_KEYSIZE-1); |
rc = finger_recvkey (kserver, pattern, &rawkey, &keylen); |
191 |
if (rc) { |
if (rc) { |
192 |
log_box (_("Keyserver"), MB_ERR, |
log_box (_("Keyserver"), MB_ERR, |
193 |
_("Finger key import failed: %s\n"), winpt_strerror (rc)); |
_("Finger key import failed: %s\n"), winpt_strerror (rc)); |
197 |
} |
} |
198 |
else if ((rc = kserver_recvkey (kserver, port, |
else if ((rc = kserver_recvkey (kserver, port, |
199 |
kserver_check_keyid (pattern), |
kserver_check_keyid (pattern), |
200 |
rawkey, MAX_KEYSIZE-1))) { |
&rawkey, &keylen))) { |
201 |
hkp_err_box (dlg, kserver, port, rc); |
hkp_err_box (dlg, kserver, port, rc); |
202 |
free_if_alloc (rawkey); |
free_if_alloc (rawkey); |
203 |
return rc; |
return rc; |
204 |
} |
} |
205 |
else { |
|
206 |
if (!strstr (rawkey, "BEGIN PGP PUBLIC KEY BLOCK")) { |
if (!strstr (rawkey, "BEGIN PGP PUBLIC KEY BLOCK")) { |
207 |
msg_box (dlg, _("This is not a valid OpenPGP key."), |
msg_box (dlg, _("This is not a valid OpenPGP key."), |
208 |
_("Keyserver"), MB_ERR); |
_("Keyserver"), MB_ERR); |
209 |
goto leave; |
goto leave; |
210 |
} |
} |
211 |
ec = gpgme_new (&ctx); |
err = gpg.importFromBuffer (rawkey); |
212 |
if (ec) |
if (err) { |
213 |
BUG (NULL); |
msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
214 |
gpgme_data_new_from_mem (&keydata, rawkey, strlen (rawkey), 1); |
goto leave; |
215 |
rc = gpgme_op_import (ctx, keydata); |
} |
216 |
if (rc) { |
import_res = gpg.importGetResult (); |
217 |
msg_box (dlg, gpgme_strerror ((gpgme_error_t)rc), _("Import"), MB_ERR); |
if (import_res && r_fpr) |
218 |
goto leave; |
*r_fpr = m_strdup (import_res->imports->fpr); |
|
} |
|
|
import_res = gpgme_op_import_result (ctx); |
|
|
if (import_res && r_fpr) |
|
|
*r_fpr = m_strdup (import_res->imports->fpr); |
|
|
} |
|
219 |
|
|
220 |
/* 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 |
221 |
a summarize at the end is presented and not for each key. */ |
a summarize at the end is presented and not for each key. */ |
222 |
if (!(flags & KM_KS_REFRESH)) { |
if (import_res && !(flags & KM_KS_REFRESH)) { |
223 |
show_imported_keys (import_res); |
show_imported_keys (import_res); |
224 |
if (import_res && import_res->unchanged == import_res->considered) { |
if (import_res->unchanged == import_res->considered) { |
225 |
rc = WPTERR_GENERAL; /* no keys updated. */ |
rc = WPTERR_GENERAL; /* no keys updated. */ |
226 |
goto leave; |
goto leave; |
227 |
} |
} |
229 |
|
|
230 |
leave: |
leave: |
231 |
free_if_alloc (rawkey); |
free_if_alloc (rawkey); |
|
gpgme_release (ctx); |
|
|
gpgme_data_release (keydata); |
|
|
|
|
232 |
return rc; |
return rc; |
233 |
} |
} |
234 |
|
|
247 |
hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
248 |
const char *pattern, int proto, char **r_fpr) |
const char *pattern, int proto, char **r_fpr) |
249 |
{ |
{ |
250 |
return keyserver_recv_key (dlg, kserver, port, pattern, proto, 0, |
return keyserver_recv_key (dlg, kserver, port, pattern, |
251 |
r_fpr); |
proto, 0, r_fpr); |
252 |
} |
} |
253 |
|
|
254 |
|
|
255 |
#define my_iskeychar(a) (((a) >='0' && (a) <= '9' ) || ((a) >= 'A' && (a) <= 'F')) |
/* Utility function to fetch a key from the keyserver |
256 |
|
based on a given signature (@sig). |
257 |
|
Return 0 on success. */ |
258 |
|
int |
259 |
|
fetch_key_from_keyserver (HWND dlg, gpgme_signature_t sig) |
260 |
|
{ |
261 |
|
const char *keyid; |
262 |
|
char timebuf[128]; |
263 |
|
int id; |
264 |
|
|
265 |
|
// XXX: we have a problem with v3 keys here |
266 |
|
if (!sig->fpr) |
267 |
|
return FALSE; |
268 |
|
if (!get_locale_timedate (sig->timestamp, timebuf, DIM (timebuf)-1)) |
269 |
|
_snprintf (timebuf, DIM (timebuf)-1, "'unknown time'"); |
270 |
|
keyid = get_keyid_from_fpr (sig->fpr); |
271 |
|
id = log_box (_("Verify"), MB_INFO|MB_YESNO, |
272 |
|
_("Signature made %s using %s key ID 0x%s\n" |
273 |
|
"Cannot check signature: public key not found\n\n" |
274 |
|
"Do you want to try to retrieve the key from the keyserver?"), |
275 |
|
timebuf, get_key_pubalgo (sig->pubkey_algo), keyid); |
276 |
|
if (id == IDNO) { |
277 |
|
msg_box (dlg, get_gpg_sigstat (GPGME_SIGSUM_KEY_MISSING), |
278 |
|
_("Verify"), MB_WARN); |
279 |
|
return -1; |
280 |
|
} |
281 |
|
|
282 |
|
if (!hkp_recv_key (dlg, default_keyserver, default_keyserver_port, |
283 |
|
keyid, 0, 0)) { |
284 |
|
keycache_update (0, keyid); |
285 |
|
return 0; |
286 |
|
} |
287 |
|
return -1; |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
|
/* Check if the given pattern are either a valid |
292 |
|
email address, a {long, short} keyid or a fingerprint. |
293 |
|
Return 0 on success. */ |
294 |
static int |
static int |
295 |
check_pattern (const char *pattern) |
check_pattern (const char *pattern) |
296 |
{ |
{ |
297 |
int rc = 1; |
size_t i; |
298 |
|
|
299 |
/* Whitespace are not allowed! */ |
if (strchr (pattern, ' ')) |
300 |
if (strchr (pattern, ' ')) { |
return WPTERR_GENERAL; /* do not allow white spaces. */ |
|
rc = WPTERR_GENERAL; |
|
|
goto leave; |
|
|
} |
|
|
|
|
|
if (((strstr (pattern, "0x")) && (strlen (pattern) == 10)) || |
|
|
(strstr(pattern, "0x")) && (strlen (pattern) == 18)) { |
|
|
rc = 0; /* Either long or short keyid */ |
|
|
goto leave; |
|
|
} |
|
|
|
|
|
if( (( my_iskeychar( pattern[0] )) && ( strlen( pattern ) == 8 ) ) |
|
|
|| (my_iskeychar(pattern[0])) && ( strlen( pattern ) == 16) ) { |
|
|
rc = 0; |
|
|
goto leave; |
|
|
} |
|
301 |
|
|
302 |
if ((strchr (pattern, '@')) && |
if (strchr (pattern, '@') && strlen (pattern) >= 3 && |
303 |
(strlen (pattern) >= 3)) { |
!check_email_address (pattern)) |
304 |
rc = 0; |
return 0; |
305 |
goto leave; |
|
306 |
|
if (strstr (pattern, "0x")) |
307 |
|
pattern += 2; |
308 |
|
if (strlen (pattern) != 8 && |
309 |
|
strlen (pattern) != 16 && |
310 |
|
strlen (pattern) != 40) |
311 |
|
return WPTERR_GENERAL; |
312 |
|
|
313 |
|
for (i=0; i < strlen (pattern); i++) { |
314 |
|
if (!isxdigit (pattern[i])) |
315 |
|
return WPTERR_GENERAL; |
316 |
} |
} |
317 |
|
return 0; |
318 |
leave: |
} |
|
return rc; |
|
|
} /* check_pattern */ |
|
319 |
|
|
320 |
|
|
321 |
static void |
/* Return human readable name for the proxy protocol. */ |
322 |
set_proxy (HWND dlg) |
static const char* |
323 |
|
name_from_proto (int proto) |
324 |
{ |
{ |
325 |
char proxy[256]; |
const char *s; |
326 |
int port = 0; |
|
327 |
|
switch (proto) { |
328 |
strcpy (proxy, "HTTP proxy: "); |
case PROXY_PROTO_NONE: s = ""; break; |
329 |
if (kserver_get_proxy (&port)) { |
case PROXY_PROTO_HTTP: s = "HTTP"; break; |
330 |
char t[128]; |
case PROXY_PROTO_SOCKS5: s = "SOCKS5"; break; |
331 |
const char *http = kserver_get_proxy (&port); |
default: s = "HTTP"; break; |
|
_snprintf (t, sizeof (t) - 1, "\"%s:%d\"", http, port); |
|
|
strcat (proxy, t); |
|
332 |
} |
} |
333 |
else |
return s; |
|
strcat (proxy, "none"); |
|
|
SetDlgItemText (dlg, IDC_KEYSERVER_PROXY, proxy); |
|
334 |
} |
} |
335 |
|
|
336 |
|
|
343 |
} |
} |
344 |
|
|
345 |
|
|
346 |
static u16 inline |
static WORD inline |
347 |
kserver_get_port (listview_ctrl_t lv) |
kserver_get_port (listview_ctrl_t lv) |
348 |
{ |
{ |
349 |
char buf[16]; |
char buf[16]; |
350 |
|
|
351 |
listview_get_item_text (lv, kserver_get_pos (lv), KS_COL_PORT, buf, 15); |
listview_get_item_text (lv, kserver_get_pos (lv), |
352 |
return (u16)strtoul (buf, NULL, 10); |
KS_COL_PORT, buf, sizeof (buf)-1); |
353 |
|
return (WORD)strtoul (buf, NULL, 10); |
354 |
} |
} |
355 |
|
|
356 |
|
|
|
/* Load the default keyserver and mark it in @lv. */ |
|
357 |
static void |
static void |
358 |
load_default_ks (listview_ctrl_t lv) |
set_default_ks (listview_ctrl_t lv) |
359 |
{ |
{ |
360 |
char * p, buf[192]; |
char *p, buf[128]; |
361 |
int i; |
int i; |
362 |
|
|
363 |
p = get_reg_entry_keyserver ("Default"); |
p = get_reg_entry_keyserver ("Default"); |
364 |
if (!p) |
if (!p) |
365 |
return; |
return; |
366 |
for (i = 0; i < listview_count_items( lv, 0); i++ ) { |
for (i=0; i < listview_count_items (lv, 0); i++) { |
367 |
listview_get_item_text (lv, i, KS_COL_NAME, buf, sizeof (buf)-1); |
listview_get_item_text (lv, i, KS_COL_NAME, buf, sizeof (buf)-1); |
368 |
if (!strncmp (p, buf, strlen (p))) { |
if (!stricmp (buf, p)) { |
369 |
listview_add_sub_item (lv, i, KS_COL_DEFAULT, "x"); |
listview_select_one (lv, i); |
370 |
break; |
break; |
371 |
} |
} |
372 |
} |
} |
373 |
free_if_alloc (p); |
free_if_alloc (p); |
374 |
} |
} |
375 |
|
|
376 |
|
|
377 |
/* Save the selected keyserver from @lv as the default server. */ |
/* Save the selected keyserver from @lv as the default server. */ |
378 |
static int |
static int |
379 |
save_default_ks (listview_ctrl_t lv) |
save_default_ks (listview_ctrl_t lv) |
383 |
|
|
384 |
idx = listview_get_curr_pos (lv); |
idx = listview_get_curr_pos (lv); |
385 |
if (idx == -1) { |
if (idx == -1) { |
386 |
msg_box (NULL, _("Please select one of the servers."), _("Keyserver"), MB_ERR); |
msg_box (NULL, _("Please select one of the servers."), |
387 |
|
_("Keyserver"), MB_ERR); |
388 |
return -1; |
return -1; |
389 |
} |
} |
390 |
listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1); |
listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1); |
391 |
if (!strncmp (buf, "http", 4) && !strncmp (buf, "hkp", 3)) { |
if (strnicmp (buf, "http", 4) && strnicmp (buf, "hkp", 3)) { |
392 |
msg_box (NULL, _("Only HTTP keyserver can be used."), |
msg_box (NULL, _("Only HKP keyserver can be used."), |
393 |
_("Keyserver"), MB_ERR); |
_("Keyserver"), MB_ERR); |
394 |
return -1; |
return -1; |
395 |
} |
} |
|
for (i = 0; i < listview_count_items (lv, 0); i++) |
|
|
listview_add_sub_item (lv, i, KS_COL_DEFAULT, ""); |
|
|
listview_add_sub_item (lv, idx, KS_COL_DEFAULT, "x"); |
|
|
listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1); |
|
396 |
set_reg_entry_keyserver ("Default", buf); |
set_reg_entry_keyserver ("Default", buf); |
397 |
i = kserver_get_port (lv); |
i = kserver_get_port (lv); |
398 |
sprintf (port, "%d", i); |
sprintf (port, "%d", i); |
399 |
set_reg_entry_keyserver ("Default_Port", port); |
set_reg_entry_keyserver ("Default_Port", port); |
400 |
keyserver_set_default (buf, (u16)i); |
keyserver_set_default (buf, (WORD)i); |
401 |
return 0; |
return 0; |
402 |
} |
} |
403 |
|
|
406 |
keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd) |
keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd) |
407 |
{ |
{ |
408 |
struct listview_column_s keyserver[] = { |
struct listview_column_s keyserver[] = { |
409 |
{0, 160, (char *)_("DNS Name")}, |
{0, 180, (char *)_("DNS Name")}, |
410 |
{1, 46, (char *)_("Port")}, |
{1, 55, (char *)_("Port")}, |
|
{2, 60, (char *)_("Default")}, |
|
411 |
{0, 0, NULL} |
{0, 0, NULL} |
412 |
}; |
}; |
413 |
HICON ico[1]; |
HICON ico[1]; |
416 |
int j; |
int j; |
417 |
|
|
418 |
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER); |
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER); |
419 |
listview_new (&lv); |
listview_new (&lv, hwnd); |
|
lv->ctrl = hwnd; |
|
420 |
for (j=0; keyserver[j].fieldname; j++) |
for (j=0; keyserver[j].fieldname; j++) |
421 |
listview_add_column (lv, &keyserver[j]); |
listview_add_column (lv, &keyserver[j]); |
422 |
listview_set_image_list (lv, ico, 1); |
listview_set_image_list (lv, 16, 16, ico, 1); |
423 |
for (j = 0; j<MAX_KEYSERVERS; j++) { |
for (j = 0; j < MAX_KEYSERVERS; j++) { |
424 |
if (!server[j].used) |
if (!server[j].used) |
425 |
continue; |
continue; |
426 |
listview_add_item_image (lv, " ", 0); |
listview_add_item_image (lv, " ", 0); |
428 |
sprintf (buf, "%d", server[j].port); |
sprintf (buf, "%d", server[j].port); |
429 |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
430 |
} |
} |
|
load_default_ks (lv); |
|
431 |
if (listview_count_items (lv, 0) == 0) { |
if (listview_count_items (lv, 0) == 0) { |
432 |
listview_add_item (lv, ""); |
listview_add_item (lv, ""); |
433 |
listview_add_sub_item (lv, 0, KS_COL_NAME, DEF_HKP_KEYSERVER); |
listview_add_sub_item (lv, 0, KS_COL_NAME, DEF_HKP_KEYSERVER); |
447 |
|
|
448 |
listview_get_item_text (lv, pos, KS_COL_NAME, name, sizeof (name)-1); |
listview_get_item_text (lv, pos, KS_COL_NAME, name, sizeof (name)-1); |
449 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
450 |
if (server[i].name && strcmp (server[i].name, name) == 0) |
if (server[i].name && stricmp (server[i].name, name) == 0) |
451 |
server[i].used = 0; |
server[i].used = 0; |
452 |
} |
} |
453 |
listview_del_item (lv, pos); |
listview_del_item (lv, pos); |
462 |
int i; |
int i; |
463 |
|
|
464 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
for (i=0; i < MAX_KEYSERVERS; i++) { |
465 |
if (server[i].name && strcmp (server[i].name, ctx->name) == 0) { |
if (server[i].name && stricmp (server[i].name, ctx->name) == 0) { |
466 |
fnd = true; |
fnd = true; |
467 |
break; |
break; |
468 |
} |
} |
538 |
EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_TYPES), FALSE); |
EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_TYPES), FALSE); |
539 |
} |
} |
540 |
else |
else |
541 |
SetDlgItemInt (dlg, IDC_KSERVADD_PORT, 11371, FALSE); |
SetDlgItemInt (dlg, IDC_KSERVADD_PORT, 11371, FALSE); |
542 |
SetWindowText (dlg, _("Edit Keyserver")); |
SetWindowText (dlg, _("Edit Keyserver")); |
543 |
SetDlgItemText (dlg, IDOK, _("&Add")); |
SetDlgItemText (dlg, IDOK, _("&Add")); |
544 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
586 |
return FALSE; |
return FALSE; |
587 |
} |
} |
588 |
|
|
589 |
|
|
590 |
|
/* XXX: factor out the pattern code for generic use. */ |
591 |
|
|
592 |
|
/* Add search pattern @patt to the combo box if it is |
593 |
|
not already available in the list. */ |
594 |
|
static void |
595 |
|
add_pattern_to_combox (HWND dlg, int ctlid, const char *patt) |
596 |
|
{ |
597 |
|
int err; |
598 |
|
|
599 |
|
err = SendDlgItemMessage (dlg, ctlid, CB_FINDSTRINGEXACT, |
600 |
|
0, (LPARAM)(LPCSTR)patt); |
601 |
|
if (err != CB_ERR) |
602 |
|
return; |
603 |
|
SendDlgItemMessage (dlg, IDC_KEYSERVER_SEARCH, CB_ADDSTRING, |
604 |
|
0, (LPARAM)(LPCSTR)patt); |
605 |
|
} |
606 |
|
|
607 |
|
|
608 |
|
/* Restore saved pattern from the registry back to the combo box. |
609 |
|
If no pattern were saved, do nothing and just return. */ |
610 |
|
static void |
611 |
|
load_pattern_to_combox (HWND dlg, int ctlid, const char *rkey_name) |
612 |
|
{ |
613 |
|
char *p, *tok; |
614 |
|
|
615 |
|
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name); |
616 |
|
if (!p || strlen (p) < 2) { |
617 |
|
free_if_alloc (p); |
618 |
|
return; |
619 |
|
} |
620 |
|
tok = strtok (p, "$"); |
621 |
|
while (tok != NULL) { |
622 |
|
SendDlgItemMessage (dlg, ctlid, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)tok); |
623 |
|
tok = strtok (NULL, "$"); |
624 |
|
} |
625 |
|
free_if_alloc (p); |
626 |
|
} |
627 |
|
|
628 |
|
|
629 |
|
/* Save all entries from the combo box given in @ctlid to the |
630 |
|
registry. Concat all values and use '$' as a separator. */ |
631 |
|
static void |
632 |
|
save_pattern_from_combox (HWND dlg, int ctlid, const char *rkey_name) |
633 |
|
{ |
634 |
|
char *p, tmp[64]; |
635 |
|
int i; |
636 |
|
int n, len=0; |
637 |
|
|
638 |
|
n = SendDlgItemMessage (dlg, ctlid, CB_GETCOUNT, 0, 0); |
639 |
|
if (n == 0 || n == CB_ERR) |
640 |
|
return; |
641 |
|
/* it is very unlikely that the combox contain hundred of pattern |
642 |
|
but even so we limit the value to 16 which should be sufficient. */ |
643 |
|
if (n > 16) n = 16; |
644 |
|
for (i=0; i < n; i++) |
645 |
|
len += SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXTLEN, |
646 |
|
(WPARAM)i, 0) + 1 + 1; |
647 |
|
p = new char[len+1]; |
648 |
|
memset (p, 0, len+1); |
649 |
|
for (i=0; i < n; i++) { |
650 |
|
memset (tmp, 0, sizeof (tmp)); |
651 |
|
SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXT, |
652 |
|
(WPARAM)i, (LPARAM)tmp); |
653 |
|
strcat (p, tmp); |
654 |
|
strcat (p, "$"); |
655 |
|
} |
656 |
|
set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name, p); |
657 |
|
free_if_alloc (p); |
658 |
|
} |
659 |
|
|
660 |
|
|
661 |
/* Dialog box procedure to access keyservers. */ |
/* Dialog box procedure to access keyservers. */ |
662 |
BOOL CALLBACK |
BOOL CALLBACK |
663 |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
677 |
SetDlgItemText (dlg, IDC_KEYSERVER_SEND, |
SetDlgItemText (dlg, IDC_KEYSERVER_SEND, |
678 |
_("Send key (default is receiving)")); |
_("Send key (default is receiving)")); |
679 |
SetDlgItemText (dlg, IDC_KEYSERVER_INFO, |
SetDlgItemText (dlg, IDC_KEYSERVER_INFO, |
680 |
_("Please enter the key ID or email address you search for")); |
_("Key ID or email address you want to search for")); |
681 |
SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search")); |
SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search")); |
682 |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange")); |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange proxy")); |
683 |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
684 |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
685 |
|
|
686 |
set_proxy (dlg); |
load_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
687 |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
688 |
|
set_default_ks (lv); |
689 |
center_window (dlg, NULL); |
center_window (dlg, NULL); |
690 |
SetForegroundWindow (dlg); |
SetForegroundWindow (dlg); |
691 |
return TRUE; |
return TRUE; |
695 |
notify = (NMHDR *)lparam; |
notify = (NMHDR *)lparam; |
696 |
if (!notify) |
if (!notify) |
697 |
break; |
break; |
698 |
if (notify->code == NM_CLICK |
if (notify->code == (UINT)NM_CLICK |
699 |
&& notify->idFrom == IDC_KEYSERVER_LIST) |
&& notify->idFrom == (UINT)IDC_KEYSERVER_LIST) |
700 |
lv_idx = listview_get_curr_pos (lv); |
lv_idx = listview_get_curr_pos (lv); |
701 |
else if (notify->code == NM_RCLICK && |
else if (notify->code == (UINT)NM_RCLICK && |
702 |
notify->idFrom == IDC_KEYSERVER_LIST) { |
notify->idFrom == (UINT)IDC_KEYSERVER_LIST) { |
703 |
POINT p; |
POINT p; |
704 |
GetCursorPos (&p); |
GetCursorPos (&p); |
705 |
HMENU hm, pop; |
HMENU hm, pop; |
712 |
DestroyMenu (hm); |
DestroyMenu (hm); |
713 |
DestroyMenu (pop); |
DestroyMenu (pop); |
714 |
} |
} |
715 |
|
else if (notify->code == LVN_ITEMCHANGED && |
716 |
|
((LPNMLISTVIEW)lparam)->uNewState) { |
717 |
|
listview_get_item_text (lv, listview_get_curr_pos (lv), |
718 |
|
KS_COL_NAME, kserver, sizeof (kserver)-1); |
719 |
|
if (!strnicmp (kserver, "ldap://", 7)) |
720 |
|
EnableWindow (GetDlgItem (dlg, IDC_KEYSERVER_INDEX), FALSE); |
721 |
|
else |
722 |
|
EnableWindow (GetDlgItem (dlg, IDC_KEYSERVER_INDEX), TRUE); |
723 |
|
} |
724 |
return TRUE; |
return TRUE; |
725 |
|
|
726 |
case WM_DESTROY: |
case WM_DESTROY: |
729 |
lv = NULL; |
lv = NULL; |
730 |
} |
} |
731 |
lv_idx = 0; |
lv_idx = 0; |
732 |
|
save_pattern_from_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
733 |
return FALSE; |
return FALSE; |
734 |
|
|
735 |
case WM_SYSCOMMAND: |
case WM_SYSCOMMAND: |
736 |
if (LOWORD (wparam) == SC_CLOSE) |
if (LOWORD (wparam) == SC_CLOSE) |
737 |
EndDialog (dlg, TRUE); |
EndDialog (dlg, TRUE); |
738 |
return FALSE; |
return FALSE; |
739 |
|
|
740 |
case WM_COMMAND: |
case WM_COMMAND: |
741 |
switch (LOWORD (wparam)) { |
switch (LOWORD (wparam)) { |
742 |
case IDC_KEYSERVER_PROXSETT: |
case IDC_KEYSERVER_PROXSETT: |
743 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, |
744 |
dlg, keyserver_proxy_dlg_proc, NULL, |
dlg, keyserver_proxy_dlg_proc, 0, |
745 |
_("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY); |
_("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY); |
|
set_proxy (dlg); |
|
746 |
return TRUE; |
return TRUE; |
747 |
|
|
748 |
case IDC_KEYSERVER_INDEX: |
case IDC_KEYSERVER_INDEX: |
749 |
if (!lv_idx) { |
if (!lv_idx) |
750 |
lv_idx = kserver_get_pos (lv); |
lv_idx = kserver_get_pos (lv); |
751 |
if (lv_idx == -1) { |
|
|
msg_box (dlg, _("Please select one of the keyservers."), |
|
|
_("Keyserver"), MB_INFO); |
|
|
return FALSE; |
|
|
} |
|
|
} |
|
|
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
|
|
proto, sizeof (proto)-1); |
|
|
if (!strncmp (proto, "ldap", 4)) { |
|
|
msg_box (dlg, _("This is not implemented yet!"), |
|
|
_("Keyserver"), MB_ERR); |
|
|
return FALSE; |
|
|
} |
|
|
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
|
|
kserver, sizeof (kserver)-1); |
|
752 |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
753 |
msg_box (dlg, _("Please enter the search pattern."), |
msg_box (dlg, _("Please enter the search pattern."), |
754 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
755 |
return FALSE; |
return TRUE; |
756 |
} |
} |
757 |
ksc.name = kserver; |
|
758 |
|
if (lv_idx != -1) { |
759 |
|
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
760 |
|
kserver, sizeof (kserver)-1); |
761 |
|
ksc.name = kserver; |
762 |
|
ksc.port = kserver_get_port (lv); |
763 |
|
} |
764 |
|
else { |
765 |
|
ksc.name = DEF_HKP_KEYSERVER; |
766 |
|
ksc.port = HKP_PORT; |
767 |
|
} |
768 |
ksc.pattern = pattern; |
ksc.pattern = pattern; |
|
ksc.port = kserver_get_port (lv); |
|
769 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg, |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg, |
770 |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
771 |
return TRUE; |
return TRUE; |
772 |
|
|
773 |
case IDC_KEYSERVER_RECV: |
case IDC_KEYSERVER_RECV: |
774 |
memset (&kserver, 0, sizeof (kserver)); |
memset (&kserver, 0, sizeof (kserver)); |
775 |
if (!lv_idx) { |
if (!lv_idx) { |
776 |
lv_idx = kserver_get_pos (lv); |
lv_idx = kserver_get_pos (lv); |
777 |
if (lv_idx == -1) { |
if (lv_idx == -1) { |
778 |
msg_box (dlg, _("Please select one of the keyservers."), |
msg_box (dlg, _("Please select one of the keyservers."), |
779 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
780 |
return FALSE; |
return TRUE; |
781 |
} |
} |
782 |
} |
} |
783 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
784 |
proto, sizeof (proto)-1); |
proto, sizeof (proto)-1); |
785 |
proto_nr = KSPROTO_HTTP; |
proto_nr = KSPROTO_HTTP; |
786 |
if (!strncmp (proto, "ldap", 4)) |
if (!strnicmp (proto, "ldap", 4)) |
787 |
proto_nr = KSPROTO_LDAP; |
proto_nr = KSPROTO_LDAP; |
788 |
else if (!strncmp (proto, "finger", 6)) |
else if (!strnicmp (proto, "finger", 6)) |
789 |
proto_nr = KSPROTO_FINGER; |
proto_nr = KSPROTO_FINGER; |
790 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
791 |
kserver, sizeof (kserver)-1); |
kserver, sizeof (kserver)-1); |
796 |
return FALSE; |
return FALSE; |
797 |
} |
} |
798 |
if (proto_nr == KSPROTO_LDAP && strchr (pattern, '@')) { |
if (proto_nr == KSPROTO_LDAP && strchr (pattern, '@')) { |
799 |
msg_box (dlg, _("Only keyids are allowed."), |
msg_box (dlg, _("Only keyids are allowed."), |
800 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
801 |
return FALSE; |
return FALSE; |
802 |
} |
} |
804 |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
805 |
msg_box (dlg, _("Only enter the name of the user."), |
msg_box (dlg, _("Only enter the name of the user."), |
806 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
807 |
return FALSE; |
return TRUE; |
808 |
} |
} |
809 |
} |
} |
810 |
else if (check_pattern (pattern)) { |
else if (check_pattern (pattern)) { |
811 |
msg_box (dlg, _("Only email addresses or keyids are allowed."), |
msg_box (dlg, _("Only email addresses or keyids are allowed."), |
812 |
_("Keyserver"), MB_INFO); |
_("Keyserver"), MB_INFO); |
813 |
return FALSE; |
return TRUE; |
814 |
} |
} |
815 |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
816 |
pattern, proto_nr, 0, |
pattern, proto_nr, 0, |
819 |
keycache_update (0, fpr); |
keycache_update (0, fpr); |
820 |
free_if_alloc (fpr); |
free_if_alloc (fpr); |
821 |
} |
} |
822 |
|
if (!rc) |
823 |
|
add_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, pattern); |
824 |
return TRUE; |
return TRUE; |
825 |
|
|
826 |
case IDC_KEYSERVER_DEFAULT: |
case IDC_KEYSERVER_DEFAULT: |
827 |
save_default_ks (lv); |
save_default_ks (lv); |
828 |
break; |
return TRUE; |
829 |
|
|
830 |
case ID_KSERVCTX_ADD: |
case ID_KSERVCTX_ADD: |
831 |
memset (&edit, 0, sizeof (edit)); |
memset (&edit, 0, sizeof (edit)); |
844 |
edit.name, sizeof (edit.name)-1); |
edit.name, sizeof (edit.name)-1); |
845 |
listview_get_item_text (lv, lv_idx, KS_COL_PORT, pattern, 32); |
listview_get_item_text (lv, lv_idx, KS_COL_PORT, pattern, 32); |
846 |
edit.port = atoi (pattern); |
edit.port = atoi (pattern); |
847 |
if (!strncmp (edit.name, "hkp", 3) || !strncmp (edit.name, "http", 4)) |
if (!strnicmp (edit.name, "hkp", 3) || !strnicmp (edit.name, "http", 4)) |
848 |
edit.proto = KSPROTO_HTTP; |
edit.proto = KSPROTO_HTTP; |
849 |
else if (!strncmp (edit.name, "ldap", 4)) |
else if (!strnicmp (edit.name, "ldap", 4)) |
850 |
edit.proto = KSPROTO_LDAP; |
edit.proto = KSPROTO_LDAP; |
851 |
else |
else |
852 |
edit.proto = KSPROTO_FINGER; |
edit.proto = KSPROTO_FINGER; |
863 |
|
|
864 |
case IDCANCEL: |
case IDCANCEL: |
865 |
EndDialog (dlg, FALSE); |
EndDialog (dlg, FALSE); |
866 |
return FALSE; |
return TRUE; |
867 |
} |
} |
868 |
break; |
break; |
869 |
} |
} |