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