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