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