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