1 |
/* wptOwnertrust.cpp - Ownertrust im- and export |
2 |
* Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 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 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
|
21 |
#include <windows.h> |
22 |
#include <sys/stat.h> |
23 |
|
24 |
#include "resource.h" |
25 |
#include "wptTypes.h" |
26 |
#include "wptGPG.h" |
27 |
#include "wptErrors.h" |
28 |
#include "wptNLS.h" |
29 |
#include "wptW32API.h" |
30 |
|
31 |
|
32 |
/* Export the ownertrust to the file @fname. |
33 |
Return value: 0 on success. */ |
34 |
static gpgme_error_t |
35 |
gpg_export_ownertrust (const char *fname) |
36 |
{ |
37 |
gpgme_error_t err; |
38 |
char *ot_data = NULL; |
39 |
FILE *f; |
40 |
|
41 |
err = gpg_manage_ownertrust (&ot_data, 1); |
42 |
if (err) |
43 |
return err; |
44 |
f = fopen (fname, "wb"); |
45 |
if (f) { |
46 |
fwrite (ot_data, 1, strlen (ot_data), f); |
47 |
fclose (f); |
48 |
} |
49 |
else |
50 |
err = gpg_error (GPG_ERR_ENOENT); |
51 |
safe_free (ot_data); |
52 |
return 0; |
53 |
} |
54 |
|
55 |
|
56 |
/* Import the ownertrust from file @fname. |
57 |
Return value: 0 on success. */ |
58 |
static gpgme_error_t |
59 |
gpg_import_ownertrust (const char *fname) |
60 |
{ |
61 |
gpgme_error_t err; |
62 |
FILE *f; |
63 |
struct stat st; |
64 |
char *ot_data; |
65 |
|
66 |
if (stat (fname, &st)) |
67 |
return gpg_error (GPG_ERR_ENOENT); |
68 |
f = fopen (fname, "rb"); |
69 |
if (!f) |
70 |
return gpg_error (GPG_ERR_ENOENT); |
71 |
ot_data = (char*)calloc (1, st.st_size+1); |
72 |
if (!ot_data) |
73 |
BUG (NULL); |
74 |
fread (ot_data, 1, st.st_size, f); |
75 |
fclose (f); |
76 |
|
77 |
err = gpg_manage_ownertrust (&ot_data, 0); |
78 |
|
79 |
safe_free (ot_data); |
80 |
return err; |
81 |
} |
82 |
|
83 |
|
84 |
/* Dialog procedure to manage the ownertrust. */ |
85 |
BOOL CALLBACK |
86 |
ownertrust_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
87 |
{ |
88 |
gpgme_error_t err; |
89 |
const char *fname; |
90 |
|
91 |
switch (msg) { |
92 |
case WM_INITDIALOG: |
93 |
SetWindowText (dlg, _("Ownertrust")); |
94 |
SetDlgItemText (dlg, IDC_OWNERTRUST_INFO, _("File")); |
95 |
SetDlgItemText (dlg, IDC_OWNERTRUST_DESC, |
96 |
_("Here it is possible to save or restore the ownertrust from the " |
97 |
"'trustdb' file. This could be very useful because the values are NOT " |
98 |
"stored in the keyring.")); |
99 |
SetDlgItemText (dlg, IDCANCEL, _("&Close")); |
100 |
center_window (dlg, NULL); |
101 |
SetForegroundWindow (dlg); |
102 |
return TRUE; |
103 |
|
104 |
case WM_COMMAND: |
105 |
switch (LOWORD (wparam)) { |
106 |
case IDC_OWNERTRUST_EXPORT: |
107 |
fname = get_filesave_dlg (dlg, _("Select File Name for Output"), NULL, NULL); |
108 |
if (!fname) |
109 |
return TRUE; |
110 |
err = gpg_export_ownertrust (fname); |
111 |
if (err) { |
112 |
msg_box (dlg, gpgme_strerror( err ), _("Ownertrust"), MB_ERR ); |
113 |
return TRUE; |
114 |
} |
115 |
status_box (dlg, _("Ownertrust successfully exported."), |
116 |
_("GnuPG Status")); |
117 |
return TRUE; |
118 |
|
119 |
case IDC_OWNERTRUST_IMPORT: |
120 |
fname = get_fileopen_dlg (dlg, _("Select File Name for Input"), |
121 |
NULL, NULL); |
122 |
if (!fname) |
123 |
return TRUE; |
124 |
err = gpg_import_ownertrust (fname); |
125 |
if (err) { |
126 |
msg_box (dlg, gpgme_strerror (err), _("Ownertrust"), MB_ERR); |
127 |
return TRUE; |
128 |
} |
129 |
status_box (dlg, _("Ownertrust successfully imported."), |
130 |
_("GnuPG Status")); |
131 |
return TRUE; |
132 |
|
133 |
case IDCANCEL: |
134 |
EndDialog (dlg, FALSE); |
135 |
return TRUE; |
136 |
} |
137 |
break; |
138 |
} |
139 |
|
140 |
return FALSE; |
141 |
} |