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