1 |
werner |
36 |
/* wptKeyserverSearchDlg.cpp - Keyserver Searching |
2 |
twoaday |
147 |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
werner |
36 |
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
|
|
*/ |
20 |
|
|
#ifdef HAVE_CONFIG_H |
21 |
|
|
#include <config.h> |
22 |
|
|
#endif |
23 |
|
|
|
24 |
|
|
#include <windows.h> |
25 |
|
|
#include <stdio.h> |
26 |
|
|
|
27 |
werner |
47 |
#include "resource.h" |
28 |
werner |
36 |
#include "wptCommonCtl.h" |
29 |
|
|
#include "wptKeyserver.h" |
30 |
|
|
#include "wptErrors.h" |
31 |
|
|
#include "wptTypes.h" |
32 |
|
|
#include "wptGPG.h" |
33 |
|
|
#include "wptContext.h" /* for passphrase_s */ |
34 |
|
|
#include "wptDlgs.h" |
35 |
|
|
#include "wptNLS.h" |
36 |
twoaday |
214 |
#include "wptKeylist.h" |
37 |
werner |
36 |
|
38 |
|
|
|
39 |
twoaday |
219 |
#define MAX_N_KEYS 64 |
40 |
|
|
|
41 |
|
|
|
42 |
|
|
/* Symbolic column IDs. */ |
43 |
twoaday |
214 |
enum { KSS_COL_BITS = 0, KSS_COL_ALGO, |
44 |
|
|
KSS_COL_KEYID, KSS_COL_DATE, KSS_COL_UID }; |
45 |
twoaday |
185 |
|
46 |
twoaday |
214 |
int hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
47 |
|
|
const char *pattern, int proto, char **r_fpr); |
48 |
twoaday |
147 |
|
49 |
twoaday |
185 |
/* Initialize dialog. */ |
50 |
|
|
static listview_ctrl_t |
51 |
|
|
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
52 |
werner |
36 |
{ |
53 |
|
|
struct listview_column_s keysearch[] = { |
54 |
|
|
{0, 48, (char *)_("Size")}, |
55 |
twoaday |
214 |
{1, 48, (char*)_("Algorithm")}, |
56 |
twoaday |
219 |
{2, 80, (char *)_("Key ID")}, |
57 |
twoaday |
214 |
{3, 72, (char *)_("Creation")}, |
58 |
|
|
{4, 256, (char *)_("User ID")}, |
59 |
werner |
36 |
{0, 0, NULL} |
60 |
|
|
}; |
61 |
twoaday |
185 |
char info[384]; |
62 |
|
|
int i; |
63 |
|
|
listview_ctrl_t lv = NULL; |
64 |
|
|
|
65 |
|
|
SetDlgItemText (dlg, IDC_HKPSEARCH_RECV, _("&Receive")); |
66 |
|
|
SetDlgItemText (dlg, IDOK, _("&Cancel")); |
67 |
|
|
_snprintf (info, sizeof (info) - 1, |
68 |
|
|
_("Connect to '%s' to search for \"%s\""), |
69 |
|
|
((keyserver_ctx *)lparam)->name, ((keyserver_ctx *)lparam)->pattern); |
70 |
|
|
SetDlgItemText (dlg, IDC_HKPSEARCH_INFO, info); |
71 |
|
|
SetWindowText (dlg, _("Keyserver Searching")); |
72 |
|
|
SetForegroundWindow (dlg); |
73 |
twoaday |
208 |
listview_new (&lv, GetDlgItem (dlg, IDC_HKPSEARCH_LIST)); |
74 |
twoaday |
185 |
for (i = 0; keysearch[i].width; i++) |
75 |
|
|
listview_add_column (lv, &keysearch[i]); |
76 |
|
|
listview_set_ext_style (lv); |
77 |
|
|
return lv; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
twoaday |
256 |
/* Begin the key serach and add all results to the list view @lv. |
82 |
|
|
Return value: 0 on success. */ |
83 |
twoaday |
185 |
static int |
84 |
|
|
search_hkp_keys (HWND dlg, keyserver_ctx *ksc, listview_ctrl_t lv) |
85 |
|
|
{ |
86 |
twoaday |
214 |
keyserver_key_s *key; |
87 |
|
|
const char *uid; |
88 |
twoaday |
219 |
char bits[32], algo[32], keyid[32]; |
89 |
twoaday |
214 |
int conn_fd = 0; |
90 |
|
|
int rc, nkeys = 0; |
91 |
twoaday |
185 |
|
92 |
twoaday |
214 |
rc = kserver_search_begin (ksc->name, ksc->port, ksc->pattern, |
93 |
|
|
&conn_fd, &nkeys); |
94 |
twoaday |
185 |
if (rc) { |
95 |
|
|
msg_box (dlg, winpt_strerror (rc), _("Keyserver Search"), MB_ERR); |
96 |
|
|
return rc; |
97 |
|
|
} |
98 |
|
|
|
99 |
twoaday |
219 |
if (nkeys > MAX_N_KEYS) { |
100 |
|
|
rc = log_box (_("Keyserver Search"), MB_INFO_ASK, |
101 |
|
|
_("The search result contains a lot of keys: %d\n\n" |
102 |
|
|
"Do you really want to continue?"), nkeys); |
103 |
|
|
if (rc == IDNO) { |
104 |
|
|
kserver_search_end (conn_fd); |
105 |
|
|
return -1; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
twoaday |
214 |
while (nkeys-- > 0) { |
110 |
|
|
if (kserver_search_next (conn_fd, &key)) |
111 |
|
|
break; |
112 |
|
|
if (!key) |
113 |
twoaday |
185 |
continue; |
114 |
twoaday |
219 |
|
115 |
twoaday |
214 |
_snprintf (bits, sizeof (bits) - 1, "%d", key->bits); |
116 |
|
|
_snprintf (algo, sizeof (algo)-1, "%s", |
117 |
|
|
get_key_pubalgo ((gpgme_pubkey_algo_t)key->algo)); |
118 |
twoaday |
219 |
_snprintf (keyid, sizeof (keyid)-1, "0x%s", key->keyid); |
119 |
twoaday |
214 |
uid = key->main_uid? key->main_uid->uid : ""; |
120 |
twoaday |
185 |
listview_add_item (lv, " "); |
121 |
twoaday |
214 |
listview_add_sub_item (lv, 0, KSS_COL_BITS, bits); |
122 |
|
|
listview_add_sub_item (lv, 0, KSS_COL_ALGO, algo); |
123 |
twoaday |
219 |
listview_add_sub_item (lv, 0, KSS_COL_KEYID, keyid); |
124 |
twoaday |
214 |
listview_add_sub_item (lv, 0, KSS_COL_DATE, get_key_created (key->creation)); |
125 |
|
|
listview_add_sub_item (lv, 0, KSS_COL_UID, uid); |
126 |
|
|
kserver_release_key (key); key=NULL; |
127 |
twoaday |
185 |
} |
128 |
|
|
|
129 |
|
|
kserver_search_end (conn_fd); |
130 |
|
|
return 0; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
twoaday |
197 |
/* Fetch all selected keys from the keyserver in @ksc. */ |
135 |
|
|
static void |
136 |
|
|
fetch_keys (HWND dlg, listview_ctrl_t lv, keyserver_ctx *ksc) |
137 |
|
|
{ |
138 |
|
|
char keyid[32]; |
139 |
|
|
char *fpr; |
140 |
|
|
int n, rc = 0; |
141 |
|
|
|
142 |
|
|
n = listview_count_items (lv, 1); |
143 |
|
|
if (!n) { |
144 |
|
|
msg_box (dlg, _("Please select a key."), _("Keyserver Search"), MB_ERR); |
145 |
|
|
return; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
for (n=0; n < listview_count_items (lv, 0); n++) { |
149 |
|
|
if (!listview_get_item_state (lv, n)) |
150 |
|
|
continue; |
151 |
|
|
|
152 |
|
|
listview_get_item_text (lv, n, KSS_COL_KEYID, keyid, sizeof (keyid)-1); |
153 |
|
|
rc = hkp_recv_key2 (dlg, ksc->name, ksc->port, keyid, 0, &fpr); |
154 |
|
|
if (!rc && fpr != NULL) { |
155 |
|
|
keycache_update (0, fpr); |
156 |
|
|
free_if_alloc (fpr); |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
|
162 |
twoaday |
185 |
/* Dialog box procedure for keyserver searching. */ |
163 |
|
|
BOOL CALLBACK |
164 |
|
|
hkpsearch_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
165 |
|
|
{ |
166 |
|
|
static listview_ctrl_t lv = NULL; |
167 |
|
|
static keyserver_ctx *ksc; |
168 |
werner |
36 |
|
169 |
twoaday |
147 |
switch (msg) { |
170 |
werner |
36 |
case WM_INITDIALOG: |
171 |
|
|
ksc = (keyserver_ctx *)lparam; |
172 |
twoaday |
256 |
if (!ksc) |
173 |
|
|
BUG (0); |
174 |
twoaday |
185 |
lv = on_init_dialog (dlg, msg, wparam, lparam); |
175 |
twoaday |
219 |
if (search_hkp_keys (dlg, ksc, lv)) { |
176 |
werner |
36 |
EndDialog (dlg, FALSE); |
177 |
|
|
return FALSE; |
178 |
|
|
} |
179 |
|
|
return TRUE; |
180 |
|
|
|
181 |
|
|
case WM_DESTROY: |
182 |
|
|
if (lv) { |
183 |
|
|
listview_release (lv); |
184 |
|
|
lv = NULL; |
185 |
|
|
} |
186 |
|
|
return FALSE; |
187 |
|
|
|
188 |
|
|
case WM_SYSCOMMAND: |
189 |
|
|
if (LOWORD (wparam) == SC_CLOSE) |
190 |
|
|
EndDialog (dlg, TRUE); |
191 |
|
|
return FALSE; |
192 |
|
|
|
193 |
|
|
case WM_COMMAND: |
194 |
|
|
switch (LOWORD (wparam)) { |
195 |
|
|
case IDOK: |
196 |
|
|
EndDialog (dlg, TRUE); |
197 |
|
|
return TRUE; |
198 |
|
|
|
199 |
|
|
case IDCANCEL: |
200 |
|
|
EndDialog (dlg, FALSE); |
201 |
twoaday |
256 |
return TRUE; |
202 |
werner |
36 |
|
203 |
|
|
case IDC_HKPSEARCH_RECV: |
204 |
twoaday |
197 |
fetch_keys (dlg, lv, ksc); |
205 |
werner |
36 |
return TRUE; |
206 |
|
|
} |
207 |
|
|
break; |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
return FALSE; |
211 |
twoaday |
147 |
} |