1 |
/* wptKeyserverDlg.cpp - Keyserver dialog |
2 |
* Copyright (C) 2000-2006 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 |
* 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 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
#include <commctrl.h> |
27 |
#include <ctype.h> |
28 |
|
29 |
#include "resource.h" |
30 |
#include "wptErrors.h" |
31 |
#include "wptTypes.h" |
32 |
#include "wptCommonCtl.h" |
33 |
#include "wptNLS.h" |
34 |
#include "wptW32API.h" |
35 |
#include "wptVersion.h" |
36 |
#include "wptGPG.h" |
37 |
#include "wptGPGME.h" |
38 |
#include "wptKeyserver.h" |
39 |
#include "wptContext.h" /* for passphrase_s */ |
40 |
#include "wptKeyManager.h" |
41 |
#include "wptDlgs.h" |
42 |
#include "wptUTF8.h" |
43 |
#include "wptRegistry.h" |
44 |
#include "wptKeylist.h" |
45 |
|
46 |
char* get_reg_entry_keyserver (const char *); |
47 |
int set_reg_entry_keyserver (const char *, const char *); |
48 |
|
49 |
/* Symbolic column IDs */ |
50 |
enum { KS_COL_NAME = 0, KS_COL_PORT }; |
51 |
|
52 |
/* Callback context to modify (add, edit) keyserver items. */ |
53 |
struct keyserver_rec_s { |
54 |
char name[200]; /* hostname */ |
55 |
int proto; /* protocol */ |
56 |
int port; /* used port */ |
57 |
bool mode; /* true = edit */ |
58 |
bool cancel; |
59 |
}; |
60 |
typedef struct keyserver_rec_s *keyserver_rec_t; |
61 |
|
62 |
|
63 |
/* Print out keyserver error and a possible Winsock error. */ |
64 |
static void |
65 |
hkp_err_box (HWND dlg, const char *host, WORD port, int rc) |
66 |
{ |
67 |
const char *err = kserver_strerror (); |
68 |
const char *ws_err = wsock_strerror (); |
69 |
|
70 |
if (ws_err && strlen (ws_err) > 0) |
71 |
log_box (_("Keyserver"), MB_ERR, "%s:%d: %s", host, port, ws_err); |
72 |
else |
73 |
log_box (_("Keyserver"), MB_ERR, "%s:%d: %s\n%s", |
74 |
host, port, winpt_strerror (rc), err? err : ""); |
75 |
} |
76 |
|
77 |
|
78 |
/* Send the extracted key with keyid @pattern to the |
79 |
keyserver @kserver (port @port). |
80 |
Return value: 0 on success. */ |
81 |
int |
82 |
hkp_send_key (HWND dlg, const char *kserver, WORD port, const char *pattern) |
83 |
{ |
84 |
GPGME *gpg; |
85 |
gpgme_error_t ec; |
86 |
char *rawkey = NULL; |
87 |
char msg[256]; |
88 |
|
89 |
gpg = new GPGME (); |
90 |
gpg->setArmor (true); |
91 |
ec = gpg->exportToBuffer (pattern, &rawkey); |
92 |
if (ec) { |
93 |
msg_box (dlg, gpgme_strerror (ec), _("Export"), MB_ERR); |
94 |
goto leave; |
95 |
} |
96 |
op_begin (); |
97 |
ec = kserver_sendkey (kserver, port, rawkey, strlen (rawkey)); |
98 |
op_end (); |
99 |
if (ec) { |
100 |
hkp_err_box (dlg, kserver, port, ec); |
101 |
goto leave; |
102 |
} |
103 |
|
104 |
_snprintf (msg, sizeof (msg) -1, _("Key '%s' successfully sent"), pattern); |
105 |
status_box (dlg, msg, _("GnuPG status")); |
106 |
|
107 |
leave: |
108 |
delete gpg; |
109 |
free_if_alloc (rawkey); |
110 |
return ec? -1 : 0; |
111 |
} |
112 |
|
113 |
|
114 |
|
115 |
/* Show all received keys from the keyserver. */ |
116 |
static void |
117 |
show_imported_keys (gpgme_import_result_t res) |
118 |
{ |
119 |
gpgme_import_status_t t; |
120 |
gpgme_key_t key; |
121 |
gpgme_data_t msg; |
122 |
gpgme_ctx_t ctx; |
123 |
gpgme_error_t err; |
124 |
const char *s; |
125 |
char *p, *uid; |
126 |
size_t n=0; |
127 |
|
128 |
if (!res) |
129 |
return; |
130 |
|
131 |
err = gpgme_data_new (&msg); |
132 |
if (err) |
133 |
BUG (NULL); |
134 |
err = gpgme_new (&ctx); |
135 |
if (err) |
136 |
BUG (NULL); |
137 |
if (res->considered > 1) { |
138 |
s = _("WARNING: multiple keys matched request.\n\n"); |
139 |
gpgme_data_write (msg, s, strlen (s)); |
140 |
} |
141 |
|
142 |
if (res->unchanged == res->considered) |
143 |
s = _("Key(s) successfully received but nothing was changed."); |
144 |
else |
145 |
s = _("Key(s) sucessfully received and imported."); |
146 |
// nice to have the amount of new signatures... |
147 |
gpgme_data_write (msg, s, strlen (s)); |
148 |
gpgme_data_write (msg, "\n\n", 2); |
149 |
|
150 |
for (t=res->imports; t; t = t->next) { |
151 |
if (!gpgme_get_key (ctx, t->fpr, &key, 0)) { |
152 |
s = uid = utf8_to_native (key->uids->uid); |
153 |
gpgme_data_write (msg, s, strlen (s)); |
154 |
gpgme_data_write (msg, "\n", 1); |
155 |
gpgme_key_release (key); |
156 |
safe_free (uid); |
157 |
} |
158 |
} |
159 |
gpgme_data_write (msg, "\0", 1); |
160 |
p = gpgme_data_release_and_get_mem (msg, &n); |
161 |
if (p != NULL) { |
162 |
msg_box (NULL, p, _("Imported Keys"), MB_INFO); |
163 |
gpgme_free (p); |
164 |
} |
165 |
gpgme_release (ctx); |
166 |
} |
167 |
|
168 |
|
169 |
/* Receive a key from the keyserver @kserver (port @port) |
170 |
with the pattern @pattern. |
171 |
Return value: 0 on success. */ |
172 |
static int |
173 |
keyserver_recv_key (HWND dlg, const char *kserver, WORD port, |
174 |
const char *pattern, int proto, int flags, |
175 |
char **r_fpr) |
176 |
{ |
177 |
gpgme_import_result_t import_res = NULL; |
178 |
gpgme_error_t err; |
179 |
GPGME *gpg = NULL; |
180 |
char *rawkey = NULL; |
181 |
size_t keylen = 0; |
182 |
int rc; |
183 |
|
184 |
if (proto == KSPROTO_LDAP) { |
185 |
rc = ldap_recvkey (kserver, pattern, &rawkey, &keylen); |
186 |
if (rc) { |
187 |
msg_box (dlg, _("LDAP key import failed.\n" |
188 |
"Please make sure you have an online connection" |
189 |
" and gpgkeys_ldap.exe is installed"), |
190 |
_("Keyserver"), MB_ERR); |
191 |
free_if_alloc (rawkey); |
192 |
return rc; |
193 |
} |
194 |
} |
195 |
else if (proto == KSPROTO_FINGER) { |
196 |
rc = finger_recvkey (kserver, pattern, &rawkey, &keylen); |
197 |
if (rc) { |
198 |
log_box (_("Keyserver"), MB_ERR, |
199 |
_("Finger key import failed: %s\n"), winpt_strerror (rc)); |
200 |
free_if_alloc (rawkey); |
201 |
return rc; |
202 |
} |
203 |
} |
204 |
else if ((rc = kserver_recvkey (kserver, port, |
205 |
kserver_check_keyid (pattern), |
206 |
&rawkey, &keylen))) { |
207 |
hkp_err_box (dlg, kserver, port, rc); |
208 |
free_if_alloc (rawkey); |
209 |
return rc; |
210 |
} |
211 |
|
212 |
if (!strstr (rawkey, "BEGIN PGP PUBLIC KEY BLOCK")) { |
213 |
msg_box (dlg, _("This is not a valid OpenPGP key."), |
214 |
_("Keyserver"), MB_ERR); |
215 |
goto leave; |
216 |
} |
217 |
gpg = new GPGME (); |
218 |
err = gpg->importFromBuffer (rawkey); |
219 |
if (err) { |
220 |
msg_box (dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
221 |
goto leave; |
222 |
} |
223 |
import_res = gpg->importGetResult (); |
224 |
if (import_res && r_fpr) |
225 |
*r_fpr = m_strdup (import_res->imports->fpr); |
226 |
|
227 |
/* if we use the refresh mode, a lot of keys will be fetched and thus only |
228 |
a summarize at the end is presented and not for each key. */ |
229 |
if (import_res && !(flags & KM_KS_REFRESH)) { |
230 |
show_imported_keys (import_res); |
231 |
if (import_res->unchanged == import_res->considered) { |
232 |
rc = WPTERR_GENERAL; /* no keys updated. */ |
233 |
goto leave; |
234 |
} |
235 |
} |
236 |
|
237 |
leave: |
238 |
free_if_alloc (rawkey); |
239 |
if (gpg != NULL) |
240 |
delete gpg; |
241 |
return rc; |
242 |
} |
243 |
|
244 |
|
245 |
/* Alias for keyserver_recv_key but without the ability to return |
246 |
the fingerprint of the key. */ |
247 |
int |
248 |
hkp_recv_key (HWND dlg, const char *kserver, WORD port, |
249 |
const char *pattern, int proto, int flags) |
250 |
{ |
251 |
return keyserver_recv_key (dlg, kserver, port, pattern, |
252 |
proto, flags, NULL); |
253 |
} |
254 |
|
255 |
int |
256 |
hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
257 |
const char *pattern, int proto, char **r_fpr) |
258 |
{ |
259 |
return keyserver_recv_key (dlg, kserver, port, pattern, |
260 |
proto, 0, r_fpr); |
261 |
} |
262 |
|
263 |
|
264 |
/* Utility function to fetch a key from the keyserver |
265 |
based on a given signature (@sig). |
266 |
Return 0 on success. */ |
267 |
int |
268 |
fetch_key_from_keyserver (HWND dlg, gpgme_signature_t sig) |
269 |
{ |
270 |
const char *keyid; |
271 |
char timebuf[128]; |
272 |
int id; |
273 |
|
274 |
if (!sig->fpr) |
275 |
return FALSE; |
276 |
if (!get_locale_timedate (sig->timestamp, timebuf, DIM (timebuf)-1)) |
277 |
_snprintf (timebuf, DIM (timebuf)-1, "'unknown time'"); |
278 |
keyid = get_keyid_from_fpr (sig->fpr); |
279 |
id = log_box (_("Verify"), MB_INFO|MB_YESNO, |
280 |
_("Signature made %s using %s key ID 0x%s\n" |
281 |
"Cannot check signature: public key not found\n\n" |
282 |
"Do you want to try to retrieve the key from the keyserver?"), |
283 |
timebuf, get_key_pubalgo (sig->pubkey_algo), keyid); |
284 |
if (id == IDNO) { |
285 |
msg_box (dlg, get_gpg_sigstat (GPGME_SIGSUM_KEY_MISSING), |
286 |
_("Verify"), MB_WARN); |
287 |
return -1; |
288 |
} |
289 |
|
290 |
if (!hkp_recv_key (dlg, default_keyserver, default_keyserver_port, |
291 |
keyid, 0, 0)) { |
292 |
keycache_update (0, keyid); |
293 |
return 0; |
294 |
} |
295 |
return -1; |
296 |
} |
297 |
|
298 |
/* Check if the given pattern are either a valid |
299 |
email address, a {long, short} keyid or a fingerprint. |
300 |
Return 0 on success. */ |
301 |
static int |
302 |
check_pattern (const char *pattern) |
303 |
{ |
304 |
size_t i; |
305 |
|
306 |
if (strchr (pattern, ' ')) |
307 |
return WPTERR_GENERAL; /* do not allow white spaces. */ |
308 |
|
309 |
if (strchr (pattern, '@') && strlen (pattern) >= 3 && |
310 |
!check_email_address (pattern)) |
311 |
return 0; |
312 |
|
313 |
if (strstr (pattern, "0x")) |
314 |
pattern += 2; |
315 |
if (strlen (pattern) != 8 && |
316 |
strlen (pattern) != 16 && |
317 |
strlen (pattern) != 40) |
318 |
return WPTERR_GENERAL; |
319 |
|
320 |
for (i=0; i < strlen (pattern); i++) { |
321 |
if (!isxdigit (pattern[i])) |
322 |
return WPTERR_GENERAL; |
323 |
} |
324 |
return 0; |
325 |
} |
326 |
|
327 |
|
328 |
/* Return human readable name for the proxy protocol. */ |
329 |
static const char* |
330 |
name_from_proto (int proto) |
331 |
{ |
332 |
const char *s; |
333 |
|
334 |
switch (proto) { |
335 |
case PROXY_PROTO_NONE: s = ""; break; |
336 |
case PROXY_PROTO_HTTP: s = "HTTP"; break; |
337 |
case PROXY_PROTO_SOCKS5: s = "SOCKS5"; break; |
338 |
default: s = "HTTP"; break; |
339 |
} |
340 |
return s; |
341 |
} |
342 |
|
343 |
|
344 |
static int inline |
345 |
kserver_get_pos (listview_ctrl_t lv) |
346 |
{ |
347 |
if (listview_count_items (lv, 0) == 1) |
348 |
return 0; |
349 |
return listview_get_curr_pos (lv); |
350 |
} |
351 |
|
352 |
|
353 |
static WORD inline |
354 |
kserver_get_port (listview_ctrl_t lv) |
355 |
{ |
356 |
char buf[16]; |
357 |
|
358 |
listview_get_item_text (lv, kserver_get_pos (lv), |
359 |
KS_COL_PORT, buf, sizeof (buf)-1); |
360 |
return (WORD)strtoul (buf, NULL, 10); |
361 |
} |
362 |
|
363 |
|
364 |
static void |
365 |
set_default_ks (listview_ctrl_t lv) |
366 |
{ |
367 |
char *p, buf[128]; |
368 |
int i; |
369 |
|
370 |
p = get_reg_entry_keyserver ("Default"); |
371 |
if (!p) |
372 |
return; |
373 |
for (i=0; i < listview_count_items (lv, 0); i++) { |
374 |
listview_get_item_text (lv, i, KS_COL_NAME, buf, sizeof (buf)-1); |
375 |
if (!stricmp (buf, p)) { |
376 |
listview_select_one (lv, i); |
377 |
break; |
378 |
} |
379 |
} |
380 |
free_if_alloc (p); |
381 |
} |
382 |
|
383 |
|
384 |
/* Save the selected keyserver from @lv as the default server. */ |
385 |
static int |
386 |
save_default_ks (listview_ctrl_t lv) |
387 |
{ |
388 |
char buf[192], port[32]; |
389 |
int idx, i; |
390 |
|
391 |
idx = listview_get_curr_pos (lv); |
392 |
if (idx == -1) { |
393 |
msg_box (NULL, _("Please select one of the servers."), |
394 |
_("Keyserver"), MB_ERR); |
395 |
return -1; |
396 |
} |
397 |
listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1); |
398 |
if (strnicmp (buf, "http", 4) && strnicmp (buf, "hkp", 3)) { |
399 |
msg_box (NULL, _("Only HKP keyserver can be used."), |
400 |
_("Keyserver"), MB_ERR); |
401 |
return -1; |
402 |
} |
403 |
set_reg_entry_keyserver ("Default", buf); |
404 |
i = kserver_get_port (lv); |
405 |
sprintf (port, "%d", i); |
406 |
set_reg_entry_keyserver ("Default_Port", port); |
407 |
keyserver_set_default (buf, (WORD)i); |
408 |
return 0; |
409 |
} |
410 |
|
411 |
|
412 |
int |
413 |
keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd) |
414 |
{ |
415 |
struct listview_column_s keyserver[] = { |
416 |
{0, 180, (char *)_("DNS Name")}, |
417 |
{1, 55, (char *)_("Port")}, |
418 |
{0, 0, NULL} |
419 |
}; |
420 |
HICON ico[1]; |
421 |
listview_ctrl_t lv; |
422 |
char buf[32]; |
423 |
int j; |
424 |
|
425 |
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER); |
426 |
listview_new (&lv, hwnd); |
427 |
for (j=0; keyserver[j].fieldname; j++) |
428 |
listview_add_column (lv, &keyserver[j]); |
429 |
listview_set_image_list (lv, 16, 16, ico, 1); |
430 |
for (j = 0; j < MAX_KEYSERVERS; j++) { |
431 |
if (!server[j].used) |
432 |
continue; |
433 |
listview_add_item_image (lv, " ", 0); |
434 |
listview_add_sub_item (lv, 0, KS_COL_NAME, server[j].name); |
435 |
sprintf (buf, "%d", server[j].port); |
436 |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
437 |
} |
438 |
if (listview_count_items (lv, 0) == 0) { |
439 |
listview_add_item (lv, ""); |
440 |
listview_add_sub_item (lv, 0, KS_COL_NAME, DEF_HKP_KEYSERVER); |
441 |
} |
442 |
listview_set_ext_style (lv); |
443 |
*r_lv = lv; |
444 |
return 0; |
445 |
} |
446 |
|
447 |
|
448 |
static void |
449 |
keyserver_remove (listview_ctrl_t lv) |
450 |
{ |
451 |
char name[100]; |
452 |
int pos = listview_get_curr_pos (lv); |
453 |
int i; |
454 |
|
455 |
listview_get_item_text (lv, pos, KS_COL_NAME, name, sizeof (name)-1); |
456 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
457 |
if (server[i].name && stricmp (server[i].name, name) == 0) |
458 |
server[i].used = 0; |
459 |
} |
460 |
listview_del_item (lv, pos); |
461 |
} |
462 |
|
463 |
|
464 |
static void |
465 |
keyserver_edit (listview_ctrl_t lv, keyserver_rec_t ctx) |
466 |
{ |
467 |
char buf[16]; |
468 |
bool fnd = false; |
469 |
int i; |
470 |
|
471 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
472 |
if (server[i].name && stricmp (server[i].name, ctx->name) == 0) { |
473 |
fnd = true; |
474 |
break; |
475 |
} |
476 |
} |
477 |
if (!fnd) |
478 |
return; |
479 |
server[i].port = ctx->port; |
480 |
|
481 |
i = listview_get_curr_pos (lv); |
482 |
sprintf (buf, "%d", ctx->port); |
483 |
listview_add_sub_item (lv, i, KS_COL_NAME, ctx->name); |
484 |
listview_add_sub_item (lv, i, KS_COL_PORT, buf); |
485 |
} |
486 |
|
487 |
static void |
488 |
keyserver_add (listview_ctrl_t lv, keyserver_rec_t ctx) |
489 |
{ |
490 |
char buf[16]; |
491 |
bool fnd = false; |
492 |
int i; |
493 |
|
494 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
495 |
if (server[i].used == 0) { |
496 |
fnd = true; |
497 |
break; |
498 |
} |
499 |
} |
500 |
if (!fnd) { |
501 |
msg_box (NULL, _("No space for new keyserver entry"), |
502 |
_("Keyserver"), MB_ERR); |
503 |
return; |
504 |
} |
505 |
free_if_alloc (server[i].name); |
506 |
server[i].name = m_strdup (ctx->name); |
507 |
server[i].port = ctx->port; |
508 |
server[i].proto = ctx->proto; |
509 |
server[i].used = 1; |
510 |
|
511 |
sprintf (buf, "%d", ctx->port); |
512 |
listview_add_item (lv, " "); |
513 |
listview_add_sub_item (lv, 0, KS_COL_NAME, ctx->name); |
514 |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
515 |
} |
516 |
|
517 |
|
518 |
static void |
519 |
fill_keyserv_types (HWND dlg) |
520 |
{ |
521 |
HWND h = GetDlgItem (dlg, IDC_KSERVADD_TYPES); |
522 |
combox_add_string (h, _("HKP Keyserver")); |
523 |
combox_add_string (h, _("LDAP Keyserver")); |
524 |
combox_add_string (h, _("Finger Keyserver")); |
525 |
SendMessage (h, CB_SETCURSEL, 0, 0); |
526 |
} |
527 |
|
528 |
|
529 |
/* Dialog box procedure for modify keyservers. */ |
530 |
BOOL CALLBACK |
531 |
keyserver_modify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
532 |
{ |
533 |
static keyserver_rec_t ctx; |
534 |
char tmp[128]; |
535 |
|
536 |
switch (msg) { |
537 |
case WM_INITDIALOG: |
538 |
fill_keyserv_types (dlg); |
539 |
ctx = (keyserver_rec_t)lparam; |
540 |
if (ctx->mode) { |
541 |
SendDlgItemMessage (dlg, IDC_KSERVADD_TYPES, CB_SETCURSEL, ctx->proto, 0); |
542 |
SetDlgItemText (dlg, IDC_KSERVADD_HOST, ctx->name); |
543 |
SetDlgItemInt (dlg, IDC_KSERVADD_PORT, ctx->port, FALSE); |
544 |
EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_HOST), FALSE); |
545 |
EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_TYPES), FALSE); |
546 |
} |
547 |
else |
548 |
SetDlgItemInt (dlg, IDC_KSERVADD_PORT, 11371, FALSE); |
549 |
SetWindowText (dlg, _("Edit Keyserver")); |
550 |
SetDlgItemText (dlg, IDOK, _("&Add")); |
551 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
552 |
SetDlgItemText (dlg, IDC_KSERVADD_TYPEINF, _("Type:")); |
553 |
SetDlgItemText (dlg, IDC_KSERVADD_PORTINF, _("Port:")); |
554 |
SetDlgItemText (dlg, IDC_KSERVADD_HOSTINF, _("Host name:")); |
555 |
SetForegroundWindow (dlg); |
556 |
break; |
557 |
|
558 |
case WM_COMMAND: |
559 |
switch (LOWORD (wparam)) { |
560 |
case IDOK: |
561 |
if (!GetDlgItemText (dlg, IDC_KSERVADD_HOST, tmp, sizeof (tmp)-1)) { |
562 |
msg_box (dlg, _("Please enter a host name"), _("Keyserver"), MB_ERR); |
563 |
return FALSE; |
564 |
} |
565 |
ctx->port = GetDlgItemInt (dlg, IDC_KSERVADD_PORT, NULL, FALSE); |
566 |
if (ctx->port > 65535) { |
567 |
msg_box (dlg, _("Invalid port, valid numbers are < 65535"), _("Keyserver"), MB_ERR); |
568 |
return FALSE; |
569 |
} |
570 |
ctx->proto = SendDlgItemMessage (dlg, IDC_KSERVADD_TYPES, CB_GETCURSEL, 0, 0); |
571 |
ctx->cancel = false; |
572 |
ctx->name[0] = '\0'; |
573 |
if (!strstr (tmp, "://")) { |
574 |
switch (ctx->proto) { |
575 |
case KSPROTO_HTTP: strcpy (ctx->name, "http://"); break; |
576 |
case KSPROTO_LDAP: strcpy (ctx->name, "ldap://"); break; |
577 |
case KSPROTO_FINGER:strcpy (ctx->name, "finger://"); break; |
578 |
} |
579 |
} |
580 |
strcat (ctx->name, tmp); |
581 |
ctx->cancel = false; |
582 |
EndDialog (dlg, TRUE); |
583 |
break; |
584 |
|
585 |
case IDCANCEL: |
586 |
ctx->cancel = true; |
587 |
EndDialog (dlg, FALSE); |
588 |
break; |
589 |
} |
590 |
break; |
591 |
} |
592 |
|
593 |
return FALSE; |
594 |
} |
595 |
|
596 |
|
597 |
/* XXX: factor out the pattern code for generic use. */ |
598 |
|
599 |
/* Add search pattern @patt to the combo box if it is |
600 |
not already available in the list. */ |
601 |
static void |
602 |
add_pattern_to_combox (HWND dlg, int ctlid, const char *patt) |
603 |
{ |
604 |
int err; |
605 |
|
606 |
err = SendDlgItemMessage (dlg, ctlid, CB_FINDSTRINGEXACT, |
607 |
0, (LPARAM)(LPCSTR)patt); |
608 |
if (err != CB_ERR) |
609 |
return; |
610 |
SendDlgItemMessage (dlg, IDC_KEYSERVER_SEARCH, CB_ADDSTRING, |
611 |
0, (LPARAM)(LPCSTR)patt); |
612 |
} |
613 |
|
614 |
|
615 |
/* Restore saved pattern from the registry back to the combo box. |
616 |
If no pattern were saved, do nothing and just return. */ |
617 |
static void |
618 |
load_pattern_to_combox (HWND dlg, int ctlid, const char *rkey_name) |
619 |
{ |
620 |
char *p, *tok; |
621 |
|
622 |
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name); |
623 |
if (!p || strlen (p) < 2) { |
624 |
free_if_alloc (p); |
625 |
return; |
626 |
} |
627 |
tok = strtok (p, "$"); |
628 |
while (tok != NULL) { |
629 |
SendDlgItemMessage (dlg, ctlid, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)tok); |
630 |
tok = strtok (NULL, "$"); |
631 |
} |
632 |
free_if_alloc (p); |
633 |
} |
634 |
|
635 |
|
636 |
/* Save all entries from the combo box given in @ctlid to the |
637 |
registry. Concat all values and use '$' as a separator. */ |
638 |
static void |
639 |
save_pattern_from_combox (HWND dlg, int ctlid, const char *rkey_name) |
640 |
{ |
641 |
char *p, tmp[64]; |
642 |
int i; |
643 |
int n, len=0; |
644 |
|
645 |
n = SendDlgItemMessage (dlg, ctlid, CB_GETCOUNT, 0, 0); |
646 |
if (n == 0 || n == CB_ERR) |
647 |
return; |
648 |
/* it is very unlikely that the combox contain hundred of pattern |
649 |
but even so we limit the value to 16 which should be sufficient. */ |
650 |
if (n > 16) n = 16; |
651 |
for (i=0; i < n; i++) |
652 |
len += SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXTLEN, |
653 |
(WPARAM)i, 0) + 1 + 1; |
654 |
p = new char[len+1]; |
655 |
memset (p, 0, len+1); |
656 |
for (i=0; i < n; i++) { |
657 |
memset (tmp, 0, sizeof (tmp)); |
658 |
SendDlgItemMessage (dlg, ctlid, CB_GETLBTEXT, |
659 |
(WPARAM)i, (LPARAM)tmp); |
660 |
strcat (p, tmp); |
661 |
strcat (p, "$"); |
662 |
} |
663 |
set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", rkey_name, p); |
664 |
free_if_alloc (p); |
665 |
} |
666 |
|
667 |
|
668 |
/* Dialog box procedure to access keyservers. */ |
669 |
BOOL CALLBACK |
670 |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
671 |
{ |
672 |
static listview_ctrl_t lv = NULL; |
673 |
static int lv_idx = 0; |
674 |
keyserver_ctx ksc; |
675 |
struct keyserver_rec_s edit; |
676 |
char kserver[128], pattern[128]; |
677 |
char proto[16], *fpr = NULL; |
678 |
int rc, proto_nr = 0; |
679 |
|
680 |
switch (msg) { |
681 |
case WM_INITDIALOG: |
682 |
SetWindowText (dlg, _("Keyserver Access")); |
683 |
SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive")); |
684 |
SetDlgItemText (dlg, IDC_KEYSERVER_SEND, |
685 |
_("Send key (default is receiving)")); |
686 |
SetDlgItemText (dlg, IDC_KEYSERVER_INFO, |
687 |
_("Key ID or email address you want to search for")); |
688 |
SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search")); |
689 |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange proxy")); |
690 |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
691 |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
692 |
|
693 |
load_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
694 |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
695 |
set_default_ks (lv); |
696 |
center_window (dlg, NULL); |
697 |
SetForegroundWindow (dlg); |
698 |
return TRUE; |
699 |
|
700 |
case WM_NOTIFY: |
701 |
NMHDR *notify; |
702 |
notify = (NMHDR *)lparam; |
703 |
if (!notify) |
704 |
break; |
705 |
if (notify->code == NM_CLICK |
706 |
&& notify->idFrom == IDC_KEYSERVER_LIST) |
707 |
lv_idx = listview_get_curr_pos (lv); |
708 |
else if (notify->code == NM_RCLICK && |
709 |
notify->idFrom == IDC_KEYSERVER_LIST) { |
710 |
POINT p; |
711 |
GetCursorPos (&p); |
712 |
HMENU hm, pop; |
713 |
hm = LoadMenu (glob_hinst, (LPCTSTR)IDR_WINPT_KEYSERVER_CTX); |
714 |
pop = GetSubMenu (hm, 0); |
715 |
set_menu_text (pop, ID_KSERVCTX_ADD, _("&Add")); |
716 |
set_menu_text (pop, ID_KSERVCTX_DEL, _("&Remove")); |
717 |
set_menu_text (pop, ID_KSERVCTX_EDIT, _("&Edit")); |
718 |
TrackPopupMenu (pop, 0, p.x, p.y, 0, dlg, NULL); |
719 |
DestroyMenu (hm); |
720 |
DestroyMenu (pop); |
721 |
} |
722 |
else if (notify->code == LVN_ITEMCHANGED && |
723 |
((LPNMLISTVIEW)lparam)->uNewState) { |
724 |
listview_get_item_text (lv, listview_get_curr_pos (lv), |
725 |
KS_COL_NAME, kserver, sizeof (kserver)-1); |
726 |
if (!strnicmp (kserver, "ldap://", 7)) |
727 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSERVER_INDEX), FALSE); |
728 |
else |
729 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSERVER_INDEX), TRUE); |
730 |
} |
731 |
return TRUE; |
732 |
|
733 |
case WM_DESTROY: |
734 |
if (lv) { |
735 |
listview_release (lv); |
736 |
lv = NULL; |
737 |
} |
738 |
lv_idx = 0; |
739 |
save_pattern_from_combox (dlg, IDC_KEYSERVER_SEARCH, "KSsearch"); |
740 |
return FALSE; |
741 |
|
742 |
case WM_SYSCOMMAND: |
743 |
if (LOWORD (wparam) == SC_CLOSE) |
744 |
EndDialog (dlg, TRUE); |
745 |
return FALSE; |
746 |
|
747 |
case WM_COMMAND: |
748 |
switch (LOWORD (wparam)) { |
749 |
case IDC_KEYSERVER_PROXSETT: |
750 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, |
751 |
dlg, keyserver_proxy_dlg_proc, 0, |
752 |
_("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY); |
753 |
return TRUE; |
754 |
|
755 |
case IDC_KEYSERVER_INDEX: |
756 |
if (!lv_idx) |
757 |
lv_idx = kserver_get_pos (lv); |
758 |
|
759 |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
760 |
msg_box (dlg, _("Please enter the search pattern."), |
761 |
_("Keyserver"), MB_INFO); |
762 |
return TRUE; |
763 |
} |
764 |
|
765 |
if (lv_idx != -1) { |
766 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
767 |
kserver, sizeof (kserver)-1); |
768 |
ksc.name = kserver; |
769 |
ksc.port = kserver_get_port (lv); |
770 |
} |
771 |
else { |
772 |
ksc.name = DEF_HKP_KEYSERVER; |
773 |
ksc.port = HKP_PORT; |
774 |
} |
775 |
ksc.pattern = pattern; |
776 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg, |
777 |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
778 |
return TRUE; |
779 |
|
780 |
case IDC_KEYSERVER_RECV: |
781 |
memset (&kserver, 0, sizeof (kserver)); |
782 |
if (!lv_idx) { |
783 |
lv_idx = kserver_get_pos (lv); |
784 |
if (lv_idx == -1) { |
785 |
msg_box (dlg, _("Please select one of the keyservers."), |
786 |
_("Keyserver"), MB_INFO); |
787 |
return TRUE; |
788 |
} |
789 |
} |
790 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
791 |
proto, sizeof (proto)-1); |
792 |
proto_nr = KSPROTO_HTTP; |
793 |
if (!strnicmp (proto, "ldap", 4)) |
794 |
proto_nr = KSPROTO_LDAP; |
795 |
else if (!strnicmp (proto, "finger", 6)) |
796 |
proto_nr = KSPROTO_FINGER; |
797 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
798 |
kserver, sizeof (kserver)-1); |
799 |
if (!GetDlgItemText(dlg, IDC_KEYSERVER_SEARCH, |
800 |
pattern, sizeof (pattern)-1)) { |
801 |
msg_box (dlg, _("Please enter the search pattern."), |
802 |
_("Keyserver"), MB_INFO); |
803 |
return FALSE; |
804 |
} |
805 |
if (proto_nr == KSPROTO_LDAP && strchr (pattern, '@')) { |
806 |
msg_box (dlg, _("Only keyids are allowed."), |
807 |
_("Keyserver"), MB_INFO); |
808 |
return FALSE; |
809 |
} |
810 |
else if (proto_nr == KSPROTO_FINGER) { |
811 |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
812 |
msg_box (dlg, _("Only enter the name of the user."), |
813 |
_("Keyserver"), MB_INFO); |
814 |
return TRUE; |
815 |
} |
816 |
} |
817 |
else if (check_pattern (pattern)) { |
818 |
msg_box (dlg, _("Only email addresses or keyids are allowed."), |
819 |
_("Keyserver"), MB_INFO); |
820 |
return TRUE; |
821 |
} |
822 |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
823 |
pattern, proto_nr, 0, |
824 |
&fpr); |
825 |
if (!rc && fpr != NULL) { |
826 |
keycache_update (0, fpr); |
827 |
free_if_alloc (fpr); |
828 |
} |
829 |
if (!rc) |
830 |
add_pattern_to_combox (dlg, IDC_KEYSERVER_SEARCH, pattern); |
831 |
return TRUE; |
832 |
|
833 |
case IDC_KEYSERVER_DEFAULT: |
834 |
save_default_ks (lv); |
835 |
return TRUE; |
836 |
|
837 |
case ID_KSERVCTX_ADD: |
838 |
memset (&edit, 0, sizeof (edit)); |
839 |
edit.mode = false; |
840 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_ADD, dlg, |
841 |
keyserver_modify_dlg_proc, |
842 |
(LPARAM)&edit, _("Edit Keyserver"), 0); |
843 |
if (!edit.cancel) |
844 |
keyserver_add (lv, &edit); |
845 |
return TRUE; |
846 |
|
847 |
case ID_KSERVCTX_EDIT: |
848 |
memset (&edit, 0, sizeof (edit)); |
849 |
edit.mode = true; |
850 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
851 |
edit.name, sizeof (edit.name)-1); |
852 |
listview_get_item_text (lv, lv_idx, KS_COL_PORT, pattern, 32); |
853 |
edit.port = atoi (pattern); |
854 |
if (!strnicmp (edit.name, "hkp", 3) || !strnicmp (edit.name, "http", 4)) |
855 |
edit.proto = KSPROTO_HTTP; |
856 |
else if (!strnicmp (edit.name, "ldap", 4)) |
857 |
edit.proto = KSPROTO_LDAP; |
858 |
else |
859 |
edit.proto = KSPROTO_FINGER; |
860 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_ADD, dlg, |
861 |
keyserver_modify_dlg_proc, |
862 |
(LPARAM)&edit, _("Edit Keyserver"), 0); |
863 |
if (!edit.cancel) |
864 |
keyserver_edit (lv, &edit); |
865 |
break; |
866 |
|
867 |
case ID_KSERVCTX_DEL: |
868 |
keyserver_remove (lv); |
869 |
return TRUE; |
870 |
|
871 |
case IDCANCEL: |
872 |
EndDialog (dlg, FALSE); |
873 |
return TRUE; |
874 |
} |
875 |
break; |
876 |
} |
877 |
|
878 |
return FALSE; |
879 |
} |