1 |
/* wptKeysigDlg.cpp - Key signature listing |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
3 |
* |
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 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
#include <windows.h> |
27 |
#include <commctrl.h> |
28 |
#include <time.h> |
29 |
|
30 |
#include "../resource.h" |
31 |
#include "wptGPG.h" |
32 |
#include "wptCommonCtl.h" |
33 |
#include "wptContext.h" /* for passphrase_s */ |
34 |
#include "wptDlgs.h" |
35 |
#include "wptW32API.h" |
36 |
#include "wptNLS.h" |
37 |
#include "wptKeyList.h" |
38 |
#include "wptKeyserver.h" |
39 |
#include "wptUTF8.h" |
40 |
#include "wptTypes.h" |
41 |
#include "wptVersion.h" |
42 |
#include "wptErrors.h" |
43 |
#include "wptKeyEdit.h" |
44 |
|
45 |
static subclass_s siglist_proc; |
46 |
|
47 |
|
48 |
static int |
49 |
is_sig (listview_ctrl_t lv, int pos) |
50 |
{ |
51 |
char tmpbuf[256]; |
52 |
|
53 |
if (pos == -1) |
54 |
pos = listview_get_curr_pos (lv); |
55 |
if (pos == -1) |
56 |
return 0; |
57 |
listview_get_item_text (lv, pos, 0, tmpbuf, 200); |
58 |
if (*tmpbuf == ' ') |
59 |
return -1; |
60 |
return 0; |
61 |
} |
62 |
|
63 |
|
64 |
/* Delete the selected signature from list view @lv. The |
65 |
key is given in @key. */ |
66 |
static int |
67 |
do_delsig (HWND dlg, listview_ctrl_t lv, winpt_key_t key) |
68 |
{ |
69 |
int pos, npos, id; |
70 |
int signo=0, uidno=0; |
71 |
GpgKeyEdit *ke; |
72 |
gpgme_error_t err; |
73 |
|
74 |
npos = pos = listview_get_curr_pos (lv); |
75 |
if (!is_sig (lv, -1)) |
76 |
return -1; |
77 |
while (pos > 0 && is_sig (lv, pos)) { |
78 |
signo++; |
79 |
pos--; |
80 |
} |
81 |
pos = npos; |
82 |
while (npos > 0) { |
83 |
if (!is_sig (lv, npos)) |
84 |
uidno++; |
85 |
npos--; |
86 |
} |
87 |
uidno++; |
88 |
|
89 |
/* XXX: do not allow to delete the self signature */ |
90 |
id = log_box (_("Key Manager"), MB_YESNO, |
91 |
_("Are you really sure you want to delete this signature from\n" |
92 |
" \"%s\""), key->uid); |
93 |
if (id == IDNO) |
94 |
return 0; |
95 |
|
96 |
ke = new GpgKeyEdit (key->keyid); |
97 |
if (!ke) |
98 |
BUG (NULL); |
99 |
err = ke->delUseridSignature (uidno, signo); |
100 |
if (err) |
101 |
msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR); |
102 |
else |
103 |
listview_del_item (lv, pos); |
104 |
delete ke; |
105 |
key->update = 1; /*update*/ |
106 |
return err? -1 : 0; |
107 |
} |
108 |
|
109 |
|
110 |
/* Dialog box procedure to display the signature properties. */ |
111 |
static BOOL CALLBACK |
112 |
sigprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
113 |
{ |
114 |
static listview_ctrl_t lv; |
115 |
char tmpbuf[256]; |
116 |
int n; |
117 |
struct { |
118 |
unsigned int exportable:1; |
119 |
unsigned int expired:1; |
120 |
unsigned int nrev:1; |
121 |
unsigned int rev:1; |
122 |
const char * alg; |
123 |
int _class; |
124 |
} ctx; |
125 |
const char *fmt_templ = _("%s %s signature"); |
126 |
|
127 |
switch (msg) { |
128 |
case WM_SYSCOMMAND: |
129 |
if (LOWORD (wparam) == SC_CLOSE) |
130 |
EndDialog (dlg, TRUE); |
131 |
return FALSE; |
132 |
|
133 |
case WM_INITDIALOG: |
134 |
lv = (listview_ctrl_t)lparam; |
135 |
if (!lv) |
136 |
dlg_fatal_error (dlg, "could not get dialog param"); |
137 |
memset (&ctx, 0, sizeof ctx); |
138 |
n = listview_get_curr_pos (lv); |
139 |
listview_get_item_text (lv, n, 2, tmpbuf, DIM (tmpbuf)-1); |
140 |
if (!strstr (tmpbuf, "L")) |
141 |
ctx.exportable = 1; |
142 |
ctx._class = atoi (tmpbuf); |
143 |
if (ctx._class == 0) |
144 |
ctx._class = 10; |
145 |
else if (ctx._class < 10) |
146 |
ctx._class += 10; |
147 |
listview_get_item_text (lv, n, 6, tmpbuf, DIM (tmpbuf)-1); |
148 |
if (strstr (tmpbuf, "DSA")) |
149 |
ctx.alg = "DSA"; |
150 |
else if (strstr (tmpbuf, "RSA")) |
151 |
ctx.alg = "RSA"; |
152 |
else |
153 |
ctx.alg = "ELG"; |
154 |
_snprintf (tmpbuf, DIM (tmpbuf)-1, fmt_templ, |
155 |
ctx.exportable? _("Exportable") : _("Non-exportable"), ctx.alg); |
156 |
SetDlgItemText (dlg, IDC_SIGPROPS_INFO, tmpbuf); |
157 |
listview_get_item_text (lv, n, 4, tmpbuf, DIM (tmpbuf)-1); |
158 |
SetDlgItemText (dlg, IDC_SIGPROPS_KEYID, tmpbuf); |
159 |
SetDlgItemInt (dlg, IDC_SIGPROPS_CLASS, ctx._class, FALSE); |
160 |
if (ctx.exportable) |
161 |
CheckDlgButton (dlg, IDC_SIGPROPS_EXP, BST_CHECKED); |
162 |
listview_get_item_text (lv, n, 0, tmpbuf, DIM (tmpbuf)-1); |
163 |
SetDlgItemText (dlg, IDC_SIGPROPS_ISSUER, tmpbuf+1); |
164 |
tmpbuf[0] = 0; |
165 |
listview_get_item_text (lv, n, 5, tmpbuf, DIM (tmpbuf)-1); |
166 |
if (strlen (tmpbuf) == 0) { |
167 |
ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPSTR), SW_HIDE); |
168 |
ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), SW_HIDE); |
169 |
} |
170 |
else { |
171 |
SYSTEMTIME st; |
172 |
struct tm * tm; |
173 |
time_t t = time (NULL); |
174 |
|
175 |
memset (&st, 0, sizeof st); |
176 |
st.wYear = atoi (tmpbuf); |
177 |
st.wMonth = atoi (tmpbuf+5); |
178 |
st.wDay = atoi (tmpbuf+8); |
179 |
DateTime_SetSystemtime (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), |
180 |
GDT_VALID, &st); |
181 |
|
182 |
tm = localtime (&t); |
183 |
tm->tm_mon++; |
184 |
tm->tm_year += 1900; |
185 |
if (tm->tm_year > st.wYear) |
186 |
ctx.expired = 1; |
187 |
else if (tm->tm_mon > st.wMonth) |
188 |
ctx.expired = 1; |
189 |
if (ctx.expired) |
190 |
CheckDlgButton (dlg, IDC_SIGPROPS_EXPIRED, BST_CHECKED); |
191 |
} |
192 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable")); |
193 |
SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably")); |
194 |
SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked")); |
195 |
SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired")); |
196 |
SetWindowText (dlg, _("Signature Properties")); |
197 |
SetForegroundWindow (dlg); |
198 |
center_window (dlg, NULL); |
199 |
return TRUE; |
200 |
|
201 |
case WM_COMMAND: |
202 |
switch (LOWORD (wparam)) { |
203 |
case IDOK: |
204 |
EndDialog (dlg, TRUE); |
205 |
break; |
206 |
} |
207 |
} |
208 |
|
209 |
return FALSE; |
210 |
} |
211 |
|
212 |
|
213 |
static BOOL CALLBACK |
214 |
subclass_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
215 |
{ |
216 |
listview_ctrl_t lv; |
217 |
winpt_key_t key; |
218 |
|
219 |
switch (msg) { |
220 |
case WM_KEYUP: |
221 |
int virt_key = (int)wparam; |
222 |
key = (winpt_key_t)siglist_proc.opaque; |
223 |
lv = key->callback.ctl; |
224 |
if (virt_key == VK_SPACE) { |
225 |
if (is_sig (lv, -1)) |
226 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
227 |
sigprops_dlg_proc, (LPARAM)lv); |
228 |
} |
229 |
else if (virt_key == VK_DELETE) |
230 |
do_delsig (dlg, lv, key); |
231 |
break; |
232 |
} |
233 |
return CallWindowProc (siglist_proc.old, dlg, msg, wparam, lparam); |
234 |
} |
235 |
|
236 |
|
237 |
/* Return 1 if the list view @lv contains non-available keys. */ |
238 |
static int |
239 |
check_for_missing_keys (listview_ctrl_t lv) |
240 |
{ |
241 |
int i, n; |
242 |
char id[128]; |
243 |
|
244 |
n = listview_count_items( lv, 0 ); |
245 |
for( i = 0; i < n; i++ ) { |
246 |
listview_get_item_text( lv, i, 1, id, sizeof (id) - 1 ); |
247 |
if( !strncmp( id, "NOKEY", 5 ) ) |
248 |
return 1; |
249 |
} |
250 |
|
251 |
return 0; |
252 |
} |
253 |
|
254 |
|
255 |
/* Receive all missing keys marked in the list @lv. */ |
256 |
static int |
257 |
recv_missing_keys (HWND dlg, listview_ctrl_t lv) |
258 |
{ |
259 |
int i, n, rc; |
260 |
char id[128], keyid[18+1]; |
261 |
|
262 |
n = listview_count_items (lv, 0); |
263 |
for( i = 0; i < n; i++ ) { |
264 |
listview_get_item_text( lv, i, 1, id, sizeof id - 1 ); |
265 |
if( !strncmp( id, "NOKEY", 5 ) ) { |
266 |
listview_get_item_text( lv, i, 4, keyid, sizeof keyid -1 ); |
267 |
rc = hkp_recv_key( dlg, default_keyserver, default_keyserver_port, keyid, 0, 0 ); |
268 |
if( rc ) |
269 |
break; |
270 |
} |
271 |
} |
272 |
|
273 |
return rc; |
274 |
} |
275 |
|
276 |
|
277 |
/* Create a mini popup with available choices. */ |
278 |
static void |
279 |
do_create_popup (HWND dlg) |
280 |
{ |
281 |
HMENU hm, sm; |
282 |
POINT p; |
283 |
|
284 |
GetCursorPos (&p); |
285 |
hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYSIG_CTX)); |
286 |
sm = GetSubMenu (hm, 0); |
287 |
|
288 |
set_menu_text (sm, ID_SIGCTX_PROPS, _("Signature &Properties")); |
289 |
set_menu_text (sm, ID_SIGCTX_KEYPROPS, _("Signing &Key Properties")); |
290 |
|
291 |
TrackPopupMenu (sm, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL); |
292 |
|
293 |
DestroyMenu (hm); |
294 |
DestroyMenu (sm); |
295 |
} |
296 |
|
297 |
|
298 |
/* Load the key property dialog with the selected key from @lv. */ |
299 |
static void |
300 |
do_load_keyprops (HWND dlg, listview_ctrl_t lv) |
301 |
{ |
302 |
winpt_key_s k; |
303 |
gpgme_key_t key; |
304 |
char keyid[32] = {0}; |
305 |
char status[64] = {0}, creation[64] = {0}; |
306 |
int n = listview_get_curr_pos (lv); |
307 |
|
308 |
listview_get_item_text (lv, n, 1, status, DIM (status)-1); |
309 |
listview_get_item_text (lv, n, 3, creation, DIM (creation)-1); |
310 |
listview_get_item_text (lv, n, 4, keyid, DIM (keyid)-1); |
311 |
if (!strcmp (status, "NOKEY")) { |
312 |
msg_box (dlg, _("Key not found in keyring, please get it from the keyserver first."), |
313 |
_("Key Manager"), MB_INFO); |
314 |
return; |
315 |
} |
316 |
|
317 |
if ((strlen (keyid) < 3 ||get_pubkey (keyid, &key))) { |
318 |
if (strlen (creation) > 0) |
319 |
msg_box (dlg, _("Key not found in keyring."), _("Key Manager"), MB_INFO); |
320 |
return; |
321 |
} |
322 |
memset (&k, 0, sizeof k); |
323 |
k.keyid = keyid; |
324 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYPROPS, dlg, |
325 |
keyprops_dlg_proc, (LPARAM)&k); |
326 |
} |
327 |
|
328 |
|
329 |
/* Dialog box procedure to list signatures. */ |
330 |
BOOL CALLBACK |
331 |
keysig_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
332 |
{ |
333 |
static listview_ctrl_t lv = NULL; |
334 |
static struct winpt_key_s *k; |
335 |
char inf[384], keyid[18+1]; |
336 |
int idx = 0, id, rc; |
337 |
HWND sl; |
338 |
|
339 |
switch( msg ) { |
340 |
case WM_INITDIALOG: |
341 |
k = (winpt_key_t) lparam; |
342 |
if (!k) |
343 |
BUG (0); |
344 |
if (k->uid) |
345 |
_snprintf (inf, DIM (inf)-1, _("Signature List for \"%s\""), k->uid); |
346 |
SetWindowText (dlg, inf); |
347 |
#ifndef LANG_DE |
348 |
SetDlgItemText (dlg, IDC_KEYSIG_RECVKEY, _("&Receive Key")); |
349 |
SetDlgItemText (dlg, IDC_KEYSIG_SIGPROPS, _("&Properties")); |
350 |
#endif |
351 |
lv = siglist_load (GetDlgItem (dlg, IDC_KEYSIG_LIST), k->keyid); |
352 |
if (!check_for_missing_keys (lv)) |
353 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY), FALSE); |
354 |
k->callback.ctl = lv; |
355 |
sl = GetDlgItem (dlg, IDC_KEYSIG_LIST); |
356 |
siglist_proc.dlg = dlg; |
357 |
siglist_proc.opaque = k; |
358 |
siglist_proc.current = (WNDPROC)subclass_dlg_proc; |
359 |
siglist_proc.old = (WNDPROC)GetWindowLong (sl, GWL_WNDPROC); |
360 |
if (siglist_proc.old) { |
361 |
if (!SetWindowLong (sl, GWL_WNDPROC, (LONG)siglist_proc.current)) { |
362 |
msg_box (dlg, _("Could not set keylist window procedure."), |
363 |
_("Key Manager"), MB_ERR); |
364 |
BUG (0); |
365 |
} |
366 |
} |
367 |
SetForegroundWindow (dlg); |
368 |
center_window (dlg, NULL); |
369 |
return TRUE; |
370 |
|
371 |
case WM_DESTROY: |
372 |
if ( lv ) { |
373 |
siglist_delete( lv ); |
374 |
lv = NULL; |
375 |
} |
376 |
return FALSE; |
377 |
|
378 |
case WM_SYSCOMMAND: |
379 |
if (LOWORD (wparam) == SC_CLOSE) { |
380 |
if (k->update) |
381 |
keycache_update (0, k->keyid); |
382 |
EndDialog (dlg, TRUE); |
383 |
} |
384 |
return FALSE; |
385 |
|
386 |
case WM_NOTIFY: |
387 |
NMHDR *notify; |
388 |
|
389 |
notify = (NMHDR *)lparam; |
390 |
if (notify && notify->code == NM_DBLCLK |
391 |
&& notify->idFrom == IDC_KEYSIG_LIST) |
392 |
do_load_keyprops (dlg, lv); |
393 |
if (notify && notify->code == NM_RCLICK && |
394 |
notify->idFrom == IDC_KEYSIG_LIST) |
395 |
do_create_popup (dlg); |
396 |
if (notify && notify->code == LVN_ITEMCHANGED && |
397 |
((LPNMLISTVIEW)lparam)->uNewState) { |
398 |
idx = listview_get_curr_pos (lv); |
399 |
listview_get_item_text (lv, idx, 1, inf, DIM (inf)-1); |
400 |
EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY), |
401 |
strcmp (inf, "NOKEY") == 0? TRUE: FALSE); |
402 |
} |
403 |
break; |
404 |
|
405 |
case WM_COMMAND: |
406 |
switch ( LOWORD( wparam ) ) { |
407 |
|
408 |
case ID_SIGCTX_KEYPROPS: |
409 |
do_load_keyprops (dlg, lv); |
410 |
break; |
411 |
|
412 |
case ID_SIGCTX_PROPS: |
413 |
if (is_sig (lv, -1)) |
414 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
415 |
sigprops_dlg_proc, (LPARAM)lv); |
416 |
break; |
417 |
|
418 |
case IDC_KEYSIG_RECVKEY: |
419 |
idx = listview_get_curr_pos (lv); |
420 |
if (idx == -1) { |
421 |
id = msg_box( dlg, _("Really receive all missing keys?"), |
422 |
_("Key Manager"), MB_YESNO|MB_INFO ); |
423 |
if (id == IDYES) { |
424 |
rc = recv_missing_keys (dlg, lv); |
425 |
if (!rc) |
426 |
keycache_set_reload (1); |
427 |
return TRUE; |
428 |
} |
429 |
return TRUE; |
430 |
} |
431 |
listview_get_item_text (lv, idx, 4, keyid, DIM (keyid)-1); |
432 |
rc = hkp_recv_key (dlg, default_keyserver, default_keyserver_port, keyid, 0, 0); |
433 |
if (!rc) |
434 |
keycache_update (0, keyid); |
435 |
return TRUE; |
436 |
|
437 |
case IDC_KEYSIG_SIGPROPS: |
438 |
if (is_sig (lv, -1)) |
439 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg, |
440 |
sigprops_dlg_proc, (LPARAM)lv); |
441 |
return TRUE; |
442 |
|
443 |
case IDOK: |
444 |
if (k->update) |
445 |
keycache_update (0, k->keyid); |
446 |
EndDialog (dlg, TRUE); |
447 |
return TRUE; |
448 |
} |
449 |
break; |
450 |
} |
451 |
return FALSE; |
452 |
} |