/[winpt]/trunk/Src/wptKeysigDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptKeysigDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (show annotations)
Fri Apr 28 10:28:24 2006 UTC (18 years, 10 months ago) by twoaday
File size: 14481 byte(s)
Bug fixes and cleanups.


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26