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, KS_COL_DEFAULT }; |
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 |
/* Format the proxy host and display it in the dialog. */ |
320 |
static void |
321 |
set_proxy (HWND dlg) |
322 |
{ |
323 |
char buf[384]; |
324 |
|
325 |
if (proxy.host) |
326 |
_snprintf (buf, sizeof (buf)-1, "%s proxy: \"%s:%d\"", |
327 |
name_from_proto (proxy.proto), proxy.host, proxy.port); |
328 |
else |
329 |
strcpy (buf, _("Proxy: none")); |
330 |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXY, buf); |
331 |
} |
332 |
|
333 |
|
334 |
static int inline |
335 |
kserver_get_pos (listview_ctrl_t lv) |
336 |
{ |
337 |
if (listview_count_items (lv, 0) == 1) |
338 |
return 0; |
339 |
return listview_get_curr_pos (lv); |
340 |
} |
341 |
|
342 |
|
343 |
static WORD inline |
344 |
kserver_get_port (listview_ctrl_t lv) |
345 |
{ |
346 |
char buf[16]; |
347 |
|
348 |
listview_get_item_text (lv, kserver_get_pos (lv), KS_COL_PORT, buf, 15); |
349 |
return (WORD)strtoul (buf, NULL, 10); |
350 |
} |
351 |
|
352 |
|
353 |
/* Load the default keyserver and mark it in @lv. */ |
354 |
static void |
355 |
load_default_ks (listview_ctrl_t lv) |
356 |
{ |
357 |
char * p, buf[192]; |
358 |
int i; |
359 |
|
360 |
p = get_reg_entry_keyserver ("Default"); |
361 |
if (!p) |
362 |
return; |
363 |
for (i = 0; i < listview_count_items (lv, 0); i++) { |
364 |
listview_get_item_text (lv, i, KS_COL_NAME, buf, sizeof (buf)-1); |
365 |
if (!strncmp (p, buf, strlen (p))) { |
366 |
listview_add_sub_item (lv, i, KS_COL_DEFAULT, "x"); |
367 |
break; |
368 |
} |
369 |
} |
370 |
free_if_alloc (p); |
371 |
} |
372 |
|
373 |
/* Save the selected keyserver from @lv as the default server. */ |
374 |
static int |
375 |
save_default_ks (listview_ctrl_t lv) |
376 |
{ |
377 |
char buf[192], port[32]; |
378 |
int idx, i; |
379 |
|
380 |
idx = listview_get_curr_pos (lv); |
381 |
if (idx == -1) { |
382 |
msg_box (NULL, _("Please select one of the servers."), _("Keyserver"), MB_ERR); |
383 |
return -1; |
384 |
} |
385 |
listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1); |
386 |
if (!strncmp (buf, "http", 4) && !strncmp (buf, "hkp", 3)) { |
387 |
msg_box (NULL, _("Only HTTP keyserver can be used."), |
388 |
_("Keyserver"), MB_ERR); |
389 |
return -1; |
390 |
} |
391 |
for (i = 0; i < listview_count_items (lv, 0); i++) |
392 |
listview_add_sub_item (lv, i, KS_COL_DEFAULT, ""); |
393 |
listview_add_sub_item (lv, idx, KS_COL_DEFAULT, "x"); |
394 |
listview_get_item_text (lv, idx, KS_COL_NAME, buf, sizeof (buf)-1); |
395 |
set_reg_entry_keyserver ("Default", buf); |
396 |
i = kserver_get_port (lv); |
397 |
sprintf (port, "%d", i); |
398 |
set_reg_entry_keyserver ("Default_Port", port); |
399 |
keyserver_set_default (buf, (WORD)i); |
400 |
return 0; |
401 |
} |
402 |
|
403 |
|
404 |
int |
405 |
keyserver_list_build (listview_ctrl_t *r_lv, HWND hwnd) |
406 |
{ |
407 |
struct listview_column_s keyserver[] = { |
408 |
{0, 160, (char *)_("DNS Name")}, |
409 |
{1, 46, (char *)_("Port")}, |
410 |
{2, 60, (char *)_("Default")}, |
411 |
{0, 0, NULL} |
412 |
}; |
413 |
HICON ico[1]; |
414 |
listview_ctrl_t lv; |
415 |
char buf[32]; |
416 |
int j; |
417 |
|
418 |
ico[0] = LoadIcon (glob_hinst, (LPCTSTR)IDI_COMPUTER); |
419 |
listview_new (&lv); |
420 |
lv->ctrl = hwnd; |
421 |
for (j=0; keyserver[j].fieldname; j++) |
422 |
listview_add_column (lv, &keyserver[j]); |
423 |
listview_set_image_list (lv, 16, 16, ico, 1); |
424 |
for (j = 0; j<MAX_KEYSERVERS; j++) { |
425 |
if (!server[j].used) |
426 |
continue; |
427 |
listview_add_item_image (lv, " ", 0); |
428 |
listview_add_sub_item (lv, 0, KS_COL_NAME, server[j].name); |
429 |
sprintf (buf, "%d", server[j].port); |
430 |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
431 |
} |
432 |
load_default_ks (lv); |
433 |
if (listview_count_items (lv, 0) == 0) { |
434 |
listview_add_item (lv, ""); |
435 |
listview_add_sub_item (lv, 0, KS_COL_NAME, DEF_HKP_KEYSERVER); |
436 |
} |
437 |
listview_set_ext_style (lv); |
438 |
*r_lv = lv; |
439 |
return 0; |
440 |
} |
441 |
|
442 |
|
443 |
static void |
444 |
keyserver_remove (listview_ctrl_t lv) |
445 |
{ |
446 |
char name[100]; |
447 |
int pos = listview_get_curr_pos (lv); |
448 |
int i; |
449 |
|
450 |
listview_get_item_text (lv, pos, KS_COL_NAME, name, sizeof (name)-1); |
451 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
452 |
if (server[i].name && strcmp (server[i].name, name) == 0) |
453 |
server[i].used = 0; |
454 |
} |
455 |
listview_del_item (lv, pos); |
456 |
} |
457 |
|
458 |
|
459 |
static void |
460 |
keyserver_edit (listview_ctrl_t lv, keyserver_rec_t ctx) |
461 |
{ |
462 |
char buf[16]; |
463 |
bool fnd = false; |
464 |
int i; |
465 |
|
466 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
467 |
if (server[i].name && strcmp (server[i].name, ctx->name) == 0) { |
468 |
fnd = true; |
469 |
break; |
470 |
} |
471 |
} |
472 |
if (!fnd) |
473 |
return; |
474 |
server[i].port = ctx->port; |
475 |
|
476 |
i = listview_get_curr_pos (lv); |
477 |
sprintf (buf, "%d", ctx->port); |
478 |
listview_add_sub_item (lv, i, KS_COL_NAME, ctx->name); |
479 |
listview_add_sub_item (lv, i, KS_COL_PORT, buf); |
480 |
} |
481 |
|
482 |
static void |
483 |
keyserver_add (listview_ctrl_t lv, keyserver_rec_t ctx) |
484 |
{ |
485 |
char buf[16]; |
486 |
bool fnd = false; |
487 |
int i; |
488 |
|
489 |
for (i=0; i < MAX_KEYSERVERS; i++) { |
490 |
if (server[i].used == 0) { |
491 |
fnd = true; |
492 |
break; |
493 |
} |
494 |
} |
495 |
if (!fnd) { |
496 |
msg_box (NULL, _("No space for new keyserver entry"), |
497 |
_("Keyserver"), MB_ERR); |
498 |
return; |
499 |
} |
500 |
free_if_alloc (server[i].name); |
501 |
server[i].name = m_strdup (ctx->name); |
502 |
server[i].port = ctx->port; |
503 |
server[i].proto = ctx->proto; |
504 |
server[i].used = 1; |
505 |
|
506 |
sprintf (buf, "%d", ctx->port); |
507 |
listview_add_item (lv, " "); |
508 |
listview_add_sub_item (lv, 0, KS_COL_NAME, ctx->name); |
509 |
listview_add_sub_item (lv, 0, KS_COL_PORT, buf); |
510 |
} |
511 |
|
512 |
|
513 |
static void |
514 |
fill_keyserv_types (HWND dlg) |
515 |
{ |
516 |
HWND h = GetDlgItem (dlg, IDC_KSERVADD_TYPES); |
517 |
combox_add_string (h, _("HKP Keyserver")); |
518 |
combox_add_string (h, _("LDAP Keyserver")); |
519 |
combox_add_string (h, _("Finger Keyserver")); |
520 |
SendMessage (h, CB_SETCURSEL, 0, 0); |
521 |
} |
522 |
|
523 |
|
524 |
/* Dialog box procedure for modify keyservers. */ |
525 |
BOOL CALLBACK |
526 |
keyserver_modify_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
527 |
{ |
528 |
static keyserver_rec_t ctx; |
529 |
char tmp[128]; |
530 |
|
531 |
switch (msg) { |
532 |
case WM_INITDIALOG: |
533 |
fill_keyserv_types (dlg); |
534 |
ctx = (keyserver_rec_t)lparam; |
535 |
if (ctx->mode) { |
536 |
SendDlgItemMessage (dlg, IDC_KSERVADD_TYPES, CB_SETCURSEL, ctx->proto, 0); |
537 |
SetDlgItemText (dlg, IDC_KSERVADD_HOST, ctx->name); |
538 |
SetDlgItemInt (dlg, IDC_KSERVADD_PORT, ctx->port, FALSE); |
539 |
EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_HOST), FALSE); |
540 |
EnableWindow (GetDlgItem (dlg, IDC_KSERVADD_TYPES), FALSE); |
541 |
} |
542 |
else |
543 |
SetDlgItemInt (dlg, IDC_KSERVADD_PORT, 11371, FALSE); |
544 |
SetWindowText (dlg, _("Edit Keyserver")); |
545 |
SetDlgItemText (dlg, IDOK, _("&Add")); |
546 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
547 |
SetDlgItemText (dlg, IDC_KSERVADD_TYPEINF, _("Type:")); |
548 |
SetDlgItemText (dlg, IDC_KSERVADD_PORTINF, _("Port:")); |
549 |
SetDlgItemText (dlg, IDC_KSERVADD_HOSTINF, _("Host name:")); |
550 |
SetForegroundWindow (dlg); |
551 |
break; |
552 |
|
553 |
case WM_COMMAND: |
554 |
switch (LOWORD (wparam)) { |
555 |
case IDOK: |
556 |
if (!GetDlgItemText (dlg, IDC_KSERVADD_HOST, tmp, sizeof (tmp)-1)) { |
557 |
msg_box (dlg, _("Please enter a host name"), _("Keyserver"), MB_ERR); |
558 |
return FALSE; |
559 |
} |
560 |
ctx->port = GetDlgItemInt (dlg, IDC_KSERVADD_PORT, NULL, FALSE); |
561 |
if (ctx->port > 65535) { |
562 |
msg_box (dlg, _("Invalid port, valid numbers are < 65535"), _("Keyserver"), MB_ERR); |
563 |
return FALSE; |
564 |
} |
565 |
ctx->proto = SendDlgItemMessage (dlg, IDC_KSERVADD_TYPES, CB_GETCURSEL, 0, 0); |
566 |
ctx->cancel = false; |
567 |
ctx->name[0] = '\0'; |
568 |
if (!strstr (tmp, "://")) { |
569 |
switch (ctx->proto) { |
570 |
case KSPROTO_HTTP: strcpy (ctx->name, "http://"); break; |
571 |
case KSPROTO_LDAP: strcpy (ctx->name, "ldap://"); break; |
572 |
case KSPROTO_FINGER:strcpy (ctx->name, "finger://"); break; |
573 |
} |
574 |
} |
575 |
strcat (ctx->name, tmp); |
576 |
ctx->cancel = false; |
577 |
EndDialog (dlg, TRUE); |
578 |
break; |
579 |
|
580 |
case IDCANCEL: |
581 |
ctx->cancel = true; |
582 |
EndDialog (dlg, FALSE); |
583 |
break; |
584 |
} |
585 |
break; |
586 |
} |
587 |
|
588 |
return FALSE; |
589 |
} |
590 |
|
591 |
/* Dialog box procedure to access keyservers. */ |
592 |
BOOL CALLBACK |
593 |
keyserver_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
594 |
{ |
595 |
static listview_ctrl_t lv = NULL; |
596 |
static int lv_idx = 0; |
597 |
keyserver_ctx ksc; |
598 |
struct keyserver_rec_s edit; |
599 |
char kserver[128], pattern[128]; |
600 |
char proto[16], *fpr = NULL; |
601 |
int rc, proto_nr = 0; |
602 |
|
603 |
switch (msg) { |
604 |
case WM_INITDIALOG: |
605 |
SetWindowText (dlg, _("Keyserver Access")); |
606 |
SetDlgItemText (dlg, IDC_KEYSERVER_RECV, _("&Receive")); |
607 |
SetDlgItemText (dlg, IDC_KEYSERVER_SEND, |
608 |
_("Send key (default is receiving)")); |
609 |
SetDlgItemText (dlg, IDC_KEYSERVER_INFO, |
610 |
_("Please enter the key ID or email address you search for")); |
611 |
SetDlgItemText (dlg, IDC_KEYSERVER_INDEX, _("&Search")); |
612 |
SetDlgItemText (dlg, IDC_KEYSERVER_PROXSETT, _("C&hange")); |
613 |
SetDlgItemText (dlg, IDC_KEYSERVER_DEFAULT, _("Set &default")); |
614 |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
615 |
|
616 |
set_proxy (dlg); |
617 |
keyserver_list_build (&lv, GetDlgItem (dlg, IDC_KEYSERVER_LIST)); |
618 |
center_window (dlg, NULL); |
619 |
SetForegroundWindow (dlg); |
620 |
return TRUE; |
621 |
|
622 |
case WM_NOTIFY: |
623 |
NMHDR *notify; |
624 |
notify = (NMHDR *)lparam; |
625 |
if (!notify) |
626 |
break; |
627 |
if (notify->code == NM_CLICK |
628 |
&& notify->idFrom == IDC_KEYSERVER_LIST) |
629 |
lv_idx = listview_get_curr_pos (lv); |
630 |
else if (notify->code == NM_RCLICK && |
631 |
notify->idFrom == IDC_KEYSERVER_LIST) { |
632 |
POINT p; |
633 |
GetCursorPos (&p); |
634 |
HMENU hm, pop; |
635 |
hm = LoadMenu (glob_hinst, (LPCTSTR)IDR_WINPT_KEYSERVER_CTX); |
636 |
pop = GetSubMenu (hm, 0); |
637 |
set_menu_text (pop, ID_KSERVCTX_ADD, _("&Add")); |
638 |
set_menu_text (pop, ID_KSERVCTX_DEL, _("&Remove")); |
639 |
set_menu_text (pop, ID_KSERVCTX_EDIT, _("&Edit")); |
640 |
TrackPopupMenu (pop, 0, p.x, p.y, 0, dlg, NULL); |
641 |
DestroyMenu (hm); |
642 |
DestroyMenu (pop); |
643 |
} |
644 |
|
645 |
return TRUE; |
646 |
|
647 |
case WM_DESTROY: |
648 |
if (lv) { |
649 |
listview_release (lv); |
650 |
lv = NULL; |
651 |
} |
652 |
lv_idx = 0; |
653 |
return FALSE; |
654 |
|
655 |
case WM_SYSCOMMAND: |
656 |
if (LOWORD (wparam) == SC_CLOSE) |
657 |
EndDialog (dlg, TRUE); |
658 |
return FALSE; |
659 |
|
660 |
case WM_COMMAND: |
661 |
switch (LOWORD (wparam)) { |
662 |
case IDC_KEYSERVER_PROXSETT: |
663 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_PROXY, |
664 |
dlg, keyserver_proxy_dlg_proc, NULL, |
665 |
_("Proxy Settings"), IDS_WINPT_KEYSERVER_PROXY); |
666 |
set_proxy (dlg); |
667 |
return TRUE; |
668 |
|
669 |
case IDC_KEYSERVER_INDEX: |
670 |
if (!lv_idx) { |
671 |
lv_idx = kserver_get_pos (lv); |
672 |
if (lv_idx == -1) { |
673 |
msg_box (dlg, _("Please select one of the keyservers."), |
674 |
_("Keyserver"), MB_INFO); |
675 |
return FALSE; |
676 |
} |
677 |
} |
678 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
679 |
proto, sizeof (proto)-1); |
680 |
if (!strncmp (proto, "ldap", 4)) { |
681 |
msg_box (dlg, _("This is not implemented yet!"), |
682 |
_("Keyserver"), MB_ERR); |
683 |
return FALSE; |
684 |
} |
685 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
686 |
kserver, sizeof (kserver)-1); |
687 |
if (!GetDlgItemText (dlg, IDC_KEYSERVER_SEARCH, pattern, sizeof (pattern)-1)) { |
688 |
msg_box (dlg, _("Please enter the search pattern."), |
689 |
_("Keyserver"), MB_INFO); |
690 |
return FALSE; |
691 |
} |
692 |
ksc.name = kserver; |
693 |
ksc.pattern = pattern; |
694 |
ksc.port = kserver_get_port (lv); |
695 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_HKPSEARCH, dlg, |
696 |
hkpsearch_dlg_proc, (LPARAM) &ksc); |
697 |
return TRUE; |
698 |
|
699 |
case IDC_KEYSERVER_RECV: |
700 |
memset (&kserver, 0, sizeof (kserver)); |
701 |
if (!lv_idx) { |
702 |
lv_idx = kserver_get_pos (lv); |
703 |
if (lv_idx == -1) { |
704 |
msg_box (dlg, _("Please select one of the keyservers."), |
705 |
_("Keyserver"), MB_INFO); |
706 |
return FALSE; |
707 |
} |
708 |
} |
709 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
710 |
proto, sizeof (proto)-1); |
711 |
proto_nr = KSPROTO_HTTP; |
712 |
if (!strncmp (proto, "ldap", 4)) |
713 |
proto_nr = KSPROTO_LDAP; |
714 |
else if (!strncmp (proto, "finger", 6)) |
715 |
proto_nr = KSPROTO_FINGER; |
716 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
717 |
kserver, sizeof (kserver)-1); |
718 |
if (!GetDlgItemText(dlg, IDC_KEYSERVER_SEARCH, |
719 |
pattern, sizeof (pattern)-1)) { |
720 |
msg_box (dlg, _("Please enter the search pattern."), |
721 |
_("Keyserver"), MB_INFO); |
722 |
return FALSE; |
723 |
} |
724 |
if (proto_nr == KSPROTO_LDAP && strchr (pattern, '@')) { |
725 |
msg_box (dlg, _("Only keyids are allowed."), |
726 |
_("Keyserver"), MB_INFO); |
727 |
return FALSE; |
728 |
} |
729 |
else if (proto_nr == KSPROTO_FINGER) { |
730 |
if (strchr (pattern, '@') || strchr (pattern, ' ')) { |
731 |
msg_box (dlg, _("Only enter the name of the user."), |
732 |
_("Keyserver"), MB_INFO); |
733 |
return FALSE; |
734 |
} |
735 |
} |
736 |
else if (check_pattern (pattern)) { |
737 |
msg_box (dlg, _("Only email addresses or keyids are allowed."), |
738 |
_("Keyserver"), MB_INFO); |
739 |
return FALSE; |
740 |
} |
741 |
rc = keyserver_recv_key (dlg, kserver, kserver_get_port (lv), |
742 |
pattern, proto_nr, 0, |
743 |
&fpr); |
744 |
if (!rc && fpr != NULL) { |
745 |
keycache_update (0, fpr); |
746 |
free_if_alloc (fpr); |
747 |
} |
748 |
return TRUE; |
749 |
|
750 |
case IDC_KEYSERVER_DEFAULT: |
751 |
save_default_ks (lv); |
752 |
break; |
753 |
|
754 |
case ID_KSERVCTX_ADD: |
755 |
memset (&edit, 0, sizeof (edit)); |
756 |
edit.mode = false; |
757 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_ADD, dlg, |
758 |
keyserver_modify_dlg_proc, |
759 |
(LPARAM)&edit, _("Edit Keyserver"), 0); |
760 |
if (!edit.cancel) |
761 |
keyserver_add (lv, &edit); |
762 |
return TRUE; |
763 |
|
764 |
case ID_KSERVCTX_EDIT: |
765 |
memset (&edit, 0, sizeof (edit)); |
766 |
edit.mode = true; |
767 |
listview_get_item_text (lv, lv_idx, KS_COL_NAME, |
768 |
edit.name, sizeof (edit.name)-1); |
769 |
listview_get_item_text (lv, lv_idx, KS_COL_PORT, pattern, 32); |
770 |
edit.port = atoi (pattern); |
771 |
if (!strncmp (edit.name, "hkp", 3) || !strncmp (edit.name, "http", 4)) |
772 |
edit.proto = KSPROTO_HTTP; |
773 |
else if (!strncmp (edit.name, "ldap", 4)) |
774 |
edit.proto = KSPROTO_LDAP; |
775 |
else |
776 |
edit.proto = KSPROTO_FINGER; |
777 |
dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYSERVER_ADD, dlg, |
778 |
keyserver_modify_dlg_proc, |
779 |
(LPARAM)&edit, _("Edit Keyserver"), 0); |
780 |
if (!edit.cancel) |
781 |
keyserver_edit (lv, &edit); |
782 |
break; |
783 |
|
784 |
case ID_KSERVCTX_DEL: |
785 |
keyserver_remove (lv); |
786 |
return TRUE; |
787 |
|
788 |
case IDCANCEL: |
789 |
EndDialog (dlg, FALSE); |
790 |
return FALSE; |
791 |
} |
792 |
break; |
793 |
} |
794 |
|
795 |
return FALSE; |
796 |
} |