1 |
/* wptFileManager.cpp - File Manager routines |
/* wptFileManager.cpp - File Manager routines |
2 |
* Copyright (C) 2001-2005 Timo Schulz |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
* of the License, or (at your option) any later version. |
11 |
* |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
* General Public License for more details. |
16 |
* |
* |
17 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
/* TODO: |
|
22 |
* check_armor_type: we should check the whole file and not only the first line! |
#ifdef HAVE_CONFIG_H |
23 |
*/ |
#include <config.h> |
24 |
|
#endif |
25 |
#include <sys/types.h> |
|
26 |
#include <windows.h> |
#include <sys/types.h> |
27 |
#include <commdlg.h> |
#include <windows.h> |
28 |
#include <io.h> |
#include <commdlg.h> |
29 |
|
#include <io.h> |
30 |
#include "../resource.h" |
#include <stdio.h> |
31 |
#include "wptTypes.h" |
|
32 |
#include "wptGPG.h" |
#include "resource.h" |
33 |
#include "wptAgent.h" |
#include "wptTypes.h" |
34 |
#include "wptCommonCtl.h" |
#include "wptGPG.h" |
35 |
#include "wptContext.h" |
#include "wptAgent.h" |
36 |
#include "wptErrors.h" |
#include "wptCommonCtl.h" |
37 |
#include "wptKeylist.h" |
#include "wptContext.h" |
38 |
#include "wptFileManager.h" |
#include "wptErrors.h" |
39 |
#include "wptNLS.h" |
#include "wptKeylist.h" |
40 |
#include "wptW32API.h" |
#include "wptFileManager.h" |
41 |
#include "wptVersion.h" |
#include "wptNLS.h" |
42 |
#include "wptDlgs.h" |
#include "wptW32API.h" |
43 |
#include "wptGPGZIP.h" |
#include "wptVersion.h" |
44 |
#include "wptUTF8.h" |
#include "wptDlgs.h" |
45 |
#include "wptRegistry.h" |
#include "wptZIP.h" |
46 |
#include "wptImport.h" |
#include "wptUTF8.h" |
47 |
|
#include "wptRegistry.h" |
48 |
#include "openpgp.h" |
#include "wptImport.h" |
49 |
|
#include "wptCrypto.h" |
50 |
void progress_cleanup (progress_filter_s *pfx); |
#include "wptKeyManager.h" |
51 |
|
#include "wptKeyserver.h" |
52 |
char* gpg_keylist_to_pattern (gpgme_key_t *rset, int n); |
#include "openpgp.h" |
53 |
gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
|
54 |
int prev_was_bad, int fd); |
|
55 |
|
void progress_cleanup (progress_filter_s *pfx); |
56 |
/*-- wptFileVerifyDlg.cpp --*/ |
BOOL CALLBACK file_secdel_confirm_dlg_proc (HWND dlg, UINT msg, |
57 |
int file_verify_add_state (file_sig_ctx_t c); |
WPARAM wparam, LPARAM lparam); |
58 |
void file_verify_use_event (void); |
char* gpg_keylist_to_pattern (gpgme_key_t *rset, size_t n); |
59 |
void file_verify_wait (void); |
gpgme_error_t sym_passphrase_cb (void *hook, const char *hint, const char *pass_inf, |
60 |
|
int prev_was_bad, int fd); |
61 |
static const char * mm_files[] = {".mov", ".avi", ".mpg", ".mpeg", |
void verify_show_signature_state (gpgme_signature_t sig); |
62 |
".mp3", ".wav", ".mid", ".wma", |
|
63 |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
|
64 |
|
static const char *mm_files[] = {".mov", ".avi", ".mpg", ".mpeg", |
65 |
|
".mp3", ".wav", ".mid", ".wma", |
66 |
char * |
".gif", ".jpg", ".png", ".jpeg", ".dib", 0}; |
67 |
fm_quote_file (const char * name) |
|
68 |
{ |
|
69 |
char * p; |
/* Add a new file to the model @fm. */ |
70 |
size_t len = strlen (name) + 8; |
static void |
71 |
|
fm_model_add_file (fm_model_t *fm, fm_model_t file) |
72 |
if (*name == '"') |
{ |
73 |
return m_strdup (name); /* avoid double quotes */ |
fm_model_t m; |
74 |
p = new char[len + 1]; |
|
75 |
if (!p) |
if (!*fm) { |
76 |
BUG (0); |
*fm = file; |
77 |
_snprintf (p, len, "\"%s\"", name); |
return; |
78 |
|
} |
79 |
return p; |
for (m = *fm; m->next; m = m->next) |
80 |
} /* fm_quote_file */ |
; |
81 |
|
m->next = file; |
82 |
|
} |
83 |
int |
|
84 |
overwrite_file (const char * fname) |
|
85 |
{ |
/* Search for a file model based on the name @name. */ |
86 |
int id; |
static fm_model_t |
87 |
|
fm_model_find_file (fm_model_t fm, const char *name) |
88 |
if (file_exist_check (fname)) |
{ |
89 |
return -1; |
fm_model_t m; |
90 |
id = log_box (_("File Manager"), MB_YESNO, |
|
91 |
_("\"%s\" already exists.\n" |
for (m=fm; m; m=m->next) { |
92 |
"Replace existing file?"), fname); |
if (!stricmp (m->name, name)) |
93 |
return id == IDNO ? 0 : -1; |
return m; |
94 |
} /* overwrite_file */ |
} |
95 |
|
return NULL; |
96 |
|
} |
97 |
static void |
|
98 |
remove_crit_file_attrs (const char * fname, int force) |
|
99 |
{ |
/* Check if the drive given by @fname is a floppy disc. |
100 |
u32 f_attr; |
Return value: -1 for success. */ |
101 |
int id; |
static int |
102 |
|
is_floppy_disc (const char *fname) |
103 |
if (file_exist_check (fname)) |
{ |
104 |
return; /* Does not exist */ |
char drv[32] = {0}; |
105 |
|
int max = DIM (drv)-1; |
106 |
f_attr = GetFileAttributes (fname); |
int i=0; |
107 |
if ((f_attr & FILE_ATTRIBUTE_READONLY) && force) |
|
108 |
SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL); |
if (!strstr (fname, ":\\")) |
109 |
else if (f_attr & FILE_ATTRIBUTE_READONLY) { |
return 0; |
110 |
id = log_box (_("File Manager"), MB_YESNO, |
|
111 |
_("\"%s\" has read-only attribute.\n" |
while (fname && *fname && *fname != '\\' && i < max) |
112 |
"Set attribute to normal?"), fname); |
drv[i++] = *fname++; |
113 |
if (id == IDYES) |
drv[i++] = '\\'; |
114 |
SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL); |
drv[i++] = '\0'; |
115 |
} |
i = GetDriveType (drv); |
116 |
} /* remove_crit_file_attrs */ |
if (i == DRIVE_REMOVABLE) |
117 |
|
return -1; |
118 |
|
return 0; |
119 |
static int inline |
} |
120 |
is_directory (const char * fname) |
|
121 |
{ |
|
122 |
return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0; |
/* Ask the user to overwrite file @fname. |
123 |
} /* is_directory */ |
Return value: 0 for cancel. */ |
124 |
|
static int |
125 |
|
overwrite_file (const char *fname) |
126 |
static int inline |
{ |
127 |
is_openpgp_ext (const char * name) |
int id; |
128 |
{ |
|
129 |
if (strstr (name, ".gpg") || strstr (name, ".asc") |
if (file_exist_check (fname)) |
130 |
|| strstr (name, ".sig") || strstr (name, ".pgp")) |
return -1; |
131 |
return -1; |
id = log_box (_("File Manager"), MB_YESNO, |
132 |
return 0; |
_("\"%s\" already exists.\n" |
133 |
} |
"Replace existing file?"), fname); |
134 |
|
return id == IDNO ? 0 : -1; |
135 |
|
} |
136 |
static int |
|
137 |
is_multi_media (const char * name) |
|
138 |
{ |
/* Removes 'critical' attributes from the file @fname. |
139 |
const char * val; |
If @force is 1, the user is not asked for permission. */ |
140 |
char * p; |
void |
141 |
int i; |
remove_crit_file_attrs (const char *fname, int force) |
142 |
int ans=0; |
{ |
143 |
|
DWORD fattr; |
144 |
i = get_reg_winpt_single (CFG_NOZIP_MMEDIA); |
int id = 0; |
145 |
if (i == -1) |
|
146 |
{ |
if (file_exist_check (fname)) |
147 |
ans = msg_box (NULL, _("Multi-Media files are already compressed, GPG would compress\n" |
return; /* Does not exist */ |
148 |
"them anyway and this costs a lot of time.\n" |
|
149 |
"It is possible to disable compression for these files.\n" |
fattr = GetFileAttributes (fname); |
150 |
"Do you want to disable it?"), |
if ((fattr & FILE_ATTRIBUTE_READONLY) && force) |
151 |
_("File Manager"), MB_YESNO|MB_INFO); |
id = IDYES; |
152 |
set_reg_winpt_single (CFG_NOZIP_MMEDIA, ans == IDYES? 1 : 0); |
else if (fattr & FILE_ATTRIBUTE_READONLY) |
153 |
if (ans == IDNO) |
id = log_box (_("File Manager"), MB_YESNO, |
154 |
return 0; |
_("\"%s\" has read-only attribute.\n" |
155 |
} |
"Set attribute to normal?"), fname); |
156 |
else if (i == 0) |
if (id == IDYES) { |
157 |
return 0; |
if (!SetFileAttributes (fname, FILE_ATTRIBUTE_NORMAL)) |
158 |
|
msg_box (NULL, _("Could not reset file attribute to normal."), |
159 |
p = strrchr (name, '.'); |
_("File Manager"), MB_ERR); |
160 |
if (!p) |
} |
161 |
return 0; |
} |
162 |
for (i=0; (val = mm_files[i]); i++) |
|
163 |
{ |
|
164 |
if (!stricmp (p, val)) |
/* Return 1 if the given path @fname is a directory, 0 otherwise. */ |
165 |
return -1; |
static int |
166 |
} |
is_directory (const char *fname) |
167 |
return 0; |
{ |
168 |
} |
return GetFileAttributes (fname) & FILE_ATTRIBUTE_DIRECTORY? 1 : 0; |
169 |
|
} |
170 |
|
|
171 |
const char* |
|
172 |
file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode) |
/* Return -1 if the given name @name is a valid GPG extension. */ |
173 |
{ |
int |
174 |
int use_armor = gpgme_get_armor (ctx); |
is_openpgp_ext (const char *name) |
175 |
|
{ |
176 |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
if (stristr (name, ".gpg") || |
177 |
return ".asc"; |
stristr (name, ".asc") || |
178 |
if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH) |
stristr (name, ".sig") || |
179 |
return ".sig"; |
stristr (name, ".pgp")) |
180 |
return ".gpg"; |
return -1; |
181 |
} /* file_get_extension */ |
return 0; |
182 |
|
} |
183 |
|
|
184 |
int |
|
185 |
fm_build( listview_ctrl_t *lv, HWND ctrl ) |
static int |
186 |
{ |
is_multi_media (const char * name) |
187 |
int i, rc = 0; |
{ |
188 |
listview_ctrl_t c; |
const char * val; |
189 |
struct listview_column_s col[] = |
char * p; |
190 |
{ |
int i; |
191 |
{0, 80, (char *)_("Status") }, |
int ans=0; |
192 |
{1, 256, (char *)_("Name") }, |
|
193 |
{2, 128, (char *)_("Operation") }, |
i = get_reg_winpt_single (CFG_NOZIP_MMEDIA); |
194 |
{0, 0, NULL } |
if (i == -1) { |
195 |
}; |
ans = msg_box (NULL, _("Multi-Media files are already compressed, GPG would compress\n" |
196 |
|
"them anyway and this takes a lot of time.\n" |
197 |
rc = listview_new( &c ); |
"It is possible to disable compression for these files.\n" |
198 |
if( rc ) |
"Do you want to disable it?"), |
199 |
BUG( NULL ); |
_("File Manager"), MB_YESNO|MB_INFO); |
200 |
c->ctrl = ctrl; |
set_reg_winpt_single (CFG_NOZIP_MMEDIA, ans == IDYES? 1 : 0); |
201 |
for ( i = 0; col[i].width; i++ ) |
if (ans == IDNO) |
202 |
listview_add_column( c, &col[i] ); |
return 0; |
203 |
listview_set_ext_style( c ); |
} |
204 |
if( lv ) |
else if (i == 0) |
205 |
*lv = c; |
return 0; |
206 |
return 0; |
|
207 |
} /* fm_build */ |
p = strrchr (name, '.'); |
208 |
|
if (!p) |
209 |
|
return 0; |
210 |
void |
for (i=0; (val = mm_files[i]); i++) { |
211 |
fm_delete( listview_ctrl_t lv ) |
if (!stricmp (p, val)) |
212 |
{ |
return -1; |
213 |
if( lv ) { |
} |
214 |
listview_release( lv ); |
return 0; |
215 |
} |
} |
216 |
} /* fm_delete */ |
|
217 |
|
|
218 |
|
/* Return a GPG file extension which depends on the operation |
219 |
int |
mode in @ctx and the sig mode @sigmode. */ |
220 |
fm_state_new (fm_state_t * ctx) |
const char* |
221 |
{ |
file_get_extension (gpgme_ctx_t ctx, gpgme_sig_mode_t sigmode) |
222 |
gpgme_error_t rc; |
{ |
223 |
fm_state_s * c; |
int use_armor = gpgme_get_armor (ctx); |
224 |
|
|
225 |
c = new fm_state_s; |
if (use_armor || sigmode == GPGME_SIG_MODE_CLEAR) |
226 |
if (!c) |
return ".asc"; |
227 |
BUG (0); |
if (!use_armor && sigmode == GPGME_SIG_MODE_DETACH) |
228 |
memset (c, 0, sizeof * c); |
return ".sig"; |
229 |
rc = gpgme_new (&c->ctx); |
return reg_prefs.default_ext == 1? ".pgp" : ".gpg"; |
230 |
if (rc) |
} |
231 |
BUG (0); |
|
232 |
/* XXX rc = gpgme_recipients_new (&c->recp);*/ |
|
233 |
/* XXX gpgme_set_comment (c->ctx, "Generated by WinPT "PGM_VERSION); */ |
/* Quote a file to avoid shell problems with spaces in the files. */ |
234 |
*ctx = c; |
char* |
235 |
return 0; |
fm_quote_file (const char * name) |
236 |
} /* fm_state_new */ |
{ |
237 |
|
char * p; |
238 |
|
size_t len = strlen (name) + 8; |
239 |
/* Release the FM state handle @c. */ |
|
240 |
void |
if (*name == '"') |
241 |
fm_state_release (fm_state_t c) |
return m_strdup (name); /* avoid double quotes */ |
242 |
{ |
p = new char[len + 1]; |
243 |
if (!c) |
if (!p) |
244 |
return; |
BUG (0); |
245 |
if (c->recp) |
_snprintf (p, len, "\"%s\"", name); |
246 |
free (c->recp); |
|
247 |
if (c->ctx) { |
return p; |
248 |
gpgme_release (c->ctx); |
} |
249 |
c->ctx = NULL; |
|
250 |
} |
|
251 |
free_if_alloc (c->opaque); |
|
252 |
free_if_alloc (c->output); |
/* Check the armor type of the file @fname and return |
253 |
delete c; c = NULL; |
a string representation of it. */ |
254 |
} |
static const char* |
255 |
|
fm_check_armor_type (const char *fname, int *r_type) |
256 |
static int |
{ |
257 |
fm_check_for_entry( listview_ctrl_t lv, const char *file ) |
FILE *fp; |
258 |
{ |
char header[512], *p; |
259 |
char name[512]; |
|
260 |
int i; |
if (r_type) |
261 |
|
*r_type = PGP_NONE; |
262 |
memset (name, 0, sizeof (name)); |
fp = fopen (fname, "rb"); |
263 |
for( i = 0; i < listview_count_items( lv, 0 ); i++ ) |
if (!fp) |
264 |
{ |
return "UNKNOWN"; |
265 |
listview_get_item_text( lv, i, 1, name, sizeof (name) - 1 ); |
p = fgets (header, DIM (header) - 1, fp); |
266 |
if( !strcmp( name, file ) ) |
fclose (fp); |
267 |
return 1; /* found */ |
if (!p) |
268 |
} |
return "UNKNOWN"; |
269 |
|
|
270 |
return 0; |
if (strncmp (header, "-----", 5)) |
271 |
} /* fm_check_for_entry */ |
goto leave; |
272 |
|
if (strstr (header, "BEGIN PGP PUBLIC KEY" )) { |
273 |
|
if (r_type) *r_type = PGP_PUBKEY; |
274 |
static int |
return "PUBKEY"; |
275 |
fm_set_ftype (listview_ctrl_t lv, const char * name) |
} |
276 |
{ |
else if (strstr (header, "BEGIN PGP PRIVATE KEY") || |
277 |
const char *type; |
strstr (header, "BEGIN PGP SECRET KEY")) { |
278 |
int rc; |
if (r_type) *r_type = PGP_SECKEY; |
279 |
|
return "SECKEY"; |
280 |
rc = fm_check_for_entry (lv, name); |
} |
281 |
if (rc) |
else if (strstr (header, "BEGIN PGP MESSAGE")) { |
282 |
return 0; |
if (r_type) *r_type = PGP_MESSAGE; |
283 |
type = fm_get_file_type (name); |
return "ENCRYPTED"; |
284 |
if (!type || !strcmp (type, "UNKNOWN")) |
} |
285 |
type = gnupg_check_file_ext (name); |
else if (strstr( header, "BEGIN PGP SIGNED MESSAGE")) { |
286 |
rc = listview_add_item (lv, " "); |
if (r_type) *r_type = PGP_CLEARSIG; |
287 |
if (rc) |
return "SIGNED-CLEAR"; |
288 |
return -1; |
} |
289 |
listview_add_sub_item (lv, 0, 0, type); |
else if (strstr(header, "BEGIN PGP SIGNATURE")) { |
290 |
listview_add_sub_item (lv, 0, 1, name); |
if (r_type) *r_type = PGP_SIG; |
291 |
return 0; |
return "SIGNED-DETACH"; |
292 |
} |
} |
293 |
|
|
294 |
|
leave: |
295 |
static int |
return "UNKNOWN"; |
296 |
fm_add_dir_files (listview_ctrl_t lv, char *path) |
} |
297 |
{ |
|
298 |
struct _finddata_t fd; |
|
299 |
char * p; |
/* Extract file type from @fname. If @r_type is valid, |
300 |
long hd; |
it contains the PGP type on success. */ |
301 |
|
static const char* |
302 |
strcat (path, "\\*"); |
fm_get_file_type (const char *fname, int *r_type) |
303 |
hd = _findfirst (path, &fd); |
{ |
304 |
do { |
gpg_iobuf_t inp; |
305 |
p = new char [(strlen (path) + strlen (fd.name))+1]; |
armor_filter_context_t afx; |
306 |
if (!p) |
PACKET *pkt; |
307 |
BUG (0); |
const char *s = NULL; |
308 |
memcpy (p, path, strlen (path)-1); |
size_t count = 0, compr = 0; |
309 |
p[strlen (path)-1] = 0; |
int rc = 0; |
310 |
strcat (p, fd.name); |
|
311 |
if (!is_directory (p)) |
if (r_type) |
312 |
fm_set_ftype (lv, p); |
*r_type = PGP_NONE; |
313 |
free_if_alloc (p); |
if (!fname) { |
314 |
|
log_debug ("fm_get_file_type: !fname\r\n"); |
315 |
} while (_findnext (hd, &fd) == 0); |
return NULL; |
316 |
_findclose (hd); |
} |
317 |
return 0; |
|
318 |
} |
/* to avoid lengthy operations on floppy disks, we use a quick check. */ |
319 |
|
if (is_floppy_disc (fname)) |
320 |
|
return fm_check_armor_type (fname, r_type); |
321 |
/* Add the drag & drop files from @dd_files to the |
|
322 |
list view control @lv. */ |
inp = gpg_iobuf_open (fname); |
323 |
int |
if (!inp) { |
324 |
fm_add_dropped_files (listview_ctrl_t lv, HDROP dd_files) |
const char *err = winpt_strerror (WPTERR_FILE_OPEN); |
325 |
{ |
log_box (_("File Manager"), MB_ERR, "\"%s\": %s", fname, err); |
326 |
char name[384+4]; |
return NULL; |
327 |
int nfiles, rc, i; |
} |
328 |
|
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
329 |
memset (name, 0, sizeof (name)); |
if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */ |
330 |
nfiles = DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0); |
&& !is_openpgp_ext (fname)) { |
331 |
for (i = 0; i < nfiles; i++) { |
gpg_iobuf_close (inp); |
332 |
DragQueryFile (dd_files, i, name, sizeof (name) -1); |
return "UNKNOWN"; |
333 |
if (is_directory (name)) |
} |
334 |
rc = fm_add_dir_files (lv, name); |
|
335 |
else |
if (gpg_use_armor_filter(inp)) { |
336 |
rc = fm_set_ftype (lv, name); |
memset (&afx, 0, sizeof (afx)); |
337 |
if (rc == -1) |
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
338 |
break; |
} |
339 |
} |
pkt = (PACKET *)calloc (1, sizeof *pkt); |
340 |
return rc; |
if (!pkt) |
341 |
} |
BUG (NULL); |
342 |
|
gpg_init_packet (pkt); |
343 |
|
/* XXX: for larger files, we could just look for the first ~3 packets |
344 |
int |
and stop if we found a packet which were sufficient to idenitfy |
345 |
fm_add_opened_files (listview_ctrl_t lv, HWND dlg) |
the file contents. */ |
346 |
{ |
while (!(rc = gpg_parse_packet (inp, pkt))) { |
347 |
OPENFILENAME open; |
switch (pkt->pkttype) { |
348 |
const char *type; |
case PKT_PUBKEY_ENC: |
349 |
char file[1024] = ""; |
s = "ENCRYPTED";rc = -2; |
350 |
int rc; |
if (r_type) *r_type = PGP_MESSAGE; |
351 |
|
break; |
352 |
memset( &open, 0, sizeof (open) ); |
|
353 |
open.lStructSize = sizeof (OPENFILENAME); |
case PKT_SYMKEY_ENC: |
354 |
open.hInstance = glob_hinst; |
case PKT_ENCRYPTED: |
355 |
open.lpstrTitle = _("File Open"); |
s = "SYMKEYENC";rc = -2; |
356 |
open.lpstrFilter = _("All Files (*.*)\0*.*\0\0"); |
if (r_type) *r_type = PGP_MESSAGE; |
357 |
open.hwndOwner = dlg; |
break; |
358 |
open.lpstrFile = file; |
|
359 |
open.nMaxFile = sizeof (file) - 1; |
case PKT_SIGNATURE: |
360 |
open.Flags = 0; |
case PKT_ONEPASS_SIG: |
361 |
|
s = "SIGNED"; rc = -2; |
362 |
if (GetOpenFileName (&open)) { |
if (r_type) *r_type = PGP_SIG; |
363 |
type = fm_get_file_type (open.lpstrFile); |
break; |
364 |
if (!type) |
|
365 |
return WPTERR_FILE_OPEN; |
case PKT_PUBLIC_KEY: |
366 |
if (!strcmp (type, "UNKNOWN")) |
s = "PUBKEY"; rc = -2; |
367 |
type = gnupg_check_file_ext (open.lpstrFile); |
if (r_type) *r_type = PGP_PUBKEY; |
368 |
rc = listview_add_item (lv, ""); |
break; |
369 |
if( !rc ) { |
|
370 |
listview_add_sub_item (lv, 0, 0, type); |
case PKT_SECRET_KEY: |
371 |
listview_add_sub_item (lv, 0, 1, open.lpstrFile); |
s = "SECKEY"; rc = -2; |
372 |
} |
if (r_type) *r_type = PGP_SECKEY; |
373 |
} |
break; |
374 |
|
|
375 |
return rc; |
case PKT_COMPRESSED: |
376 |
} |
/* If we only find 1 packet and it is compressed, |
377 |
|
we assume a compressed one-pass signature. */ |
378 |
|
if (count != 0) |
379 |
/* Check the armor type of the file @fname and return |
break; |
380 |
a string representation of it. */ |
s = "SIGNED"; rc = -2; |
381 |
static const char * |
compr = 1; |
382 |
fm_check_armor_type (const char *fname) |
break; |
383 |
{ |
|
384 |
FILE * fp; |
default: |
385 |
char header[768], * p; |
break; |
386 |
|
} |
387 |
fp = fopen (fname, "rb"); |
count++; |
388 |
if (!fp) |
gpg_free_packet (pkt); |
389 |
return "UNKNOWN"; |
gpg_init_packet (pkt); |
390 |
p = fgets (header, sizeof (header) - 1, fp); |
if (rc == -2) |
391 |
fclose (fp); |
break; /* found */ |
392 |
if (!p) |
} |
393 |
return "UNKNOWN"; |
safe_free (pkt); |
394 |
|
gpg_iobuf_close (inp); |
395 |
if( strncmp( header, "-----", 5 ) ) |
if (!s) |
396 |
goto leave; |
s = fm_check_armor_type (fname, r_type); |
397 |
if( strstr( header, "BEGIN PGP PUBLIC KEY" ) ) |
if (!s) |
398 |
return "PUBKEY"; |
s = "UNKNOWN"; |
399 |
else if( strstr( header, "BEGIN PGP PRIVATE KEY" ) ) |
if (!strcmp (s, "SIGNED") && !compr |
400 |
return "SECKEY"; |
&& strcmp (fm_check_armor_type (fname, r_type), "SIGNED-CLEAR")) { |
401 |
else if( strstr( header, "BEGIN PGP SECRET KEY" ) ) |
if (r_type) *r_type = PGP_SIG; |
402 |
return "SECKEY"; |
s = "SIGNED-DETACH"; |
403 |
else if( strstr( header, "BEGIN PGP MESSAGE" ) ) |
} |
404 |
return "ENCRYPTED"; |
return s; |
405 |
else if( strstr( header, "BEGIN PGP SIGNED MESSAGE" ) ) |
} |
406 |
return "SIGNED-CLEAR"; |
|
407 |
else if( strstr(header, "BEGIN PGP SIGNATURE" ) ) |
|
408 |
return "SIGNED-DETACH"; |
/* Build the File Manager dialog context. */ |
409 |
|
void |
410 |
leave: |
fm_build (fm_info_t *r_fm, HWND ctrl) |
411 |
return "UNKNOWN"; |
{ |
412 |
} |
|
413 |
|
fm_info_t fm; |
414 |
|
struct listview_column_s col[] = { |
415 |
int |
{0, 80, (char *)_("Status") }, |
416 |
fm_assume_onepass_sig (const char * fname) |
{1, 256, (char *)_("Name") }, |
417 |
{ |
{2, 128, (char *)_("Operation") }, |
418 |
gpgme_data_t dat; |
{0, 0, NULL} |
419 |
armor_filter_context_t afx; |
}; |
420 |
gpg_iobuf_t fp; |
int i; |
421 |
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
|
422 |
int check = 0; |
fm = new fm_info_s; |
423 |
|
memset (fm, 0, sizeof *fm); |
424 |
if (!fname) |
listview_new (&fm->lv, ctrl); |
425 |
{ |
for (i = 0; col[i].width; i++) |
426 |
gpg_data_new_from_clipboard (&dat, 0); |
listview_add_column (fm->lv, &col[i]); |
427 |
gpg_data_release_and_set_file (dat, "gpgme.tmp"); |
listview_set_ext_style (fm->lv); |
428 |
|
fm->model = NULL; /*init*/ |
429 |
fp = gpg_iobuf_open ("gpgme.tmp"); |
*r_fm = fm; |
430 |
if (!fp) |
} |
431 |
return 0; |
|
432 |
gpg_iobuf_ioctl (fp, 3, 1, NULL); |
|
433 |
if (gpg_use_armor_filter(fp)) |
/* Release the file model in @mod. */ |
434 |
{ |
static void |
435 |
memset (&afx, 0, sizeof (afx)); |
fm_model_release (fm_model_t mod) |
436 |
gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx); |
{ |
437 |
} |
fm_model_t m; |
438 |
gpg_init_packet (pkt); |
|
439 |
if (!gpg_parse_packet (fp, pkt) |
while (mod) { |
440 |
&& pkt->pkttype == PKT_COMPRESSED) |
m = mod->next; |
441 |
check = 1; |
free_if_alloc (mod->name); |
442 |
gpg_free_packet (pkt); |
free_if_alloc (mod->op); |
443 |
safe_free (pkt); |
free_if_alloc (mod->status); |
444 |
gpg_iobuf_close (fp); |
free_if_alloc (mod); |
445 |
unlink ("gpgme.tmp"); |
mod = m; |
446 |
} |
} |
447 |
/* XXX: implement it for real files */ |
} |
448 |
return check; |
|
449 |
} |
|
450 |
|
/* Reset the File Manager info context @fm. */ |
451 |
|
void |
452 |
static int |
fm_reset (fm_info_t fm) |
453 |
is_floppy_disc (const char * fname) |
{ |
454 |
{ |
listview_del_all_items (fm->lv); |
455 |
char drv[32] = {0}; |
fm_model_release (fm->model); |
456 |
int i=0; |
fm->model = NULL; |
457 |
|
} |
458 |
if (!strstr (fname, ":\\")) |
|
459 |
return 0; |
|
460 |
|
/* Release the File Manager dialog context @fm. */ |
461 |
while (fname && *fname && *fname != '\\') |
void |
462 |
drv[i++] = *fname++; |
fm_delete (fm_info_t fm) |
463 |
drv[i++] = '\\'; |
{ |
464 |
drv[i++] = '\0'; |
if (!fm) |
465 |
i = GetDriveType (drv); |
return; |
466 |
if (i == DRIVE_REMOVABLE) |
if (fm->menu) |
467 |
return -1; |
DestroyMenu (fm->menu); |
468 |
return 0; |
listview_release (fm->lv); |
469 |
} |
fm_model_release (fm->model); |
470 |
|
free_if_alloc (fm); |
471 |
|
} |
472 |
const char * |
|
473 |
fm_get_file_type (const char * fname) |
|
474 |
{ |
int |
475 |
gpg_iobuf_t inp; |
fm_state_new (fm_state_t * ctx) |
476 |
armor_filter_context_t afx; |
{ |
477 |
PACKET * pkt = (PACKET *)calloc (1, sizeof *pkt); |
fm_state_s *c; |
478 |
int i = 0, rc = 0; |
|
479 |
const char * s = NULL; |
c = new fm_state_s; |
480 |
|
if (!c) |
481 |
if (!fname) { |
BUG (0); |
482 |
safe_free (pkt); |
memset (c, 0, sizeof *c); |
483 |
return NULL; |
if (gpgme_new (&c->ctx)) |
484 |
} |
BUG (0); |
485 |
|
*ctx = c; |
486 |
if (is_floppy_disc (fname)) |
return 0; |
487 |
return fm_check_armor_type (fname); |
} |
488 |
|
|
489 |
inp = gpg_iobuf_open (fname); |
|
490 |
if (!inp) { |
/* Release the FM state handle @c. */ |
491 |
const char *s = winpt_strerror (WPTERR_FILE_OPEN); |
void |
492 |
log_box( _("File Manager"), MB_ERR, "\"%s\": %s", fname, s ); |
fm_state_release (fm_state_t c) |
493 |
safe_free( pkt ); |
{ |
494 |
return NULL; |
if (!c) |
495 |
} |
return; |
496 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
if (c->ctx) { |
497 |
if (gpg_iobuf_get_filelength (inp) > 32000000 /* 32MB */ |
gpgme_release (c->ctx); |
498 |
&& !is_openpgp_ext (fname)) { |
c->ctx = NULL; |
499 |
gpg_iobuf_close (inp); |
} |
500 |
return "UNKNOWN"; |
safe_free (c->recp); |
501 |
} |
free_if_alloc (c->opaque); |
502 |
|
free_if_alloc (c->output); |
503 |
if (gpg_use_armor_filter(inp)) { |
delete c; |
504 |
memset (&afx, 0, sizeof (afx)); |
} |
505 |
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
|
506 |
} |
|
507 |
|
/* Check if file @file is already in the list view. */ |
508 |
gpg_init_packet (pkt); |
static int |
509 |
while (!(rc = gpg_parse_packet (inp, pkt))) { |
fm_check_for_entry (listview_ctrl_t lv, const char *file) |
510 |
switch (pkt->pkttype) { |
{ |
511 |
case PKT_PUBKEY_ENC: s = "ENCRYPTED";rc = -2; break; |
char name[MAX_PATH+128]; |
512 |
case PKT_SYMKEY_ENC: |
int i; |
513 |
case PKT_ENCRYPTED: s = "SYMKEYENC";rc = -2; break; |
|
514 |
case PKT_SIGNATURE: |
memset (name, 0, sizeof (name)); |
515 |
case PKT_ONEPASS_SIG: s = "SIGNED"; rc = -2; break; |
for (i = 0; i < listview_count_items (lv, 0); i++) { |
516 |
case PKT_PUBLIC_KEY: s = "PUBKEY"; rc = -2; break; |
listview_get_item_text (lv, i, FM_COL_NAME, name, DIM (name) - 1); |
517 |
case PKT_SECRET_KEY: s = "SECKEY"; rc = -2; break; |
if (!stricmp (name, file)) |
518 |
} |
return 1; /* found */ |
519 |
gpg_free_packet (pkt); |
} |
520 |
gpg_init_packet (pkt); |
|
521 |
if (rc == -2) |
return 0; |
522 |
break; /* found */ |
} |
523 |
} |
|
524 |
safe_free (pkt); |
|
525 |
gpg_iobuf_close (inp); |
static int |
526 |
if (!s) |
fm_set_ftype (listview_ctrl_t lv, fm_model_t *fm, const char *name) |
527 |
s = fm_check_armor_type (fname); |
{ |
528 |
if (!s) |
fm_model_t m; |
529 |
s = "UNKNOWN"; |
const char *type; |
530 |
if (!strcmp( s, "SIGNED") |
int rc; |
531 |
&& strcmp (fm_check_armor_type (fname), "SIGNED-CLEAR ")) |
|
532 |
s = "SIGNED-DETACH"; |
rc = fm_check_for_entry (lv, name); |
533 |
return s; |
if (rc) |
534 |
} /* fm_get_file_type */ |
return 0; /* already present. */ |
535 |
|
m = new fm_model_s; |
536 |
|
memset (m, 0, sizeof *m); |
537 |
int |
type = fm_get_file_type (name, NULL); |
538 |
fm_get_current_pos (listview_ctrl_t lv) |
if (!type || !strcmp (type, "UNKNOWN")) |
539 |
{ |
type = gnupg_check_file_ext (name, NULL); |
540 |
int i = 0, items; |
listview_add_item2 (lv, " ", (void*)m); |
541 |
|
listview_add_sub_item (lv, 0, FM_COL_STAT, type); |
542 |
items = listview_count_items (lv, 0); |
listview_add_sub_item (lv, 0, FM_COL_NAME, name); |
543 |
if (!items) |
m->name = m_strdup (name); |
544 |
return -1; |
m->status = m_strdup (type); |
545 |
else if (items == 1) |
m->op = NULL; |
546 |
{ |
fm_model_add_file (fm, m); |
547 |
listview_select_one (lv, 0); |
return 0; |
548 |
return 0; |
} |
549 |
} |
|
550 |
else if (items > 1) |
|
551 |
{ |
/* Add all files from the directory @path to the list view @lv. */ |
552 |
i = listview_get_curr_pos (lv); |
static int |
553 |
if (i == -1) |
fm_add_dir_files (listview_ctrl_t lv, fm_model_t *fm, char *path) |
554 |
{ |
{ |
555 |
msg_box (lv->ctrl, _("Please select a file."), _("File Manager"), MB_ERR); |
WIN32_FIND_DATA fd; |
556 |
return -1; |
HANDLE hd; |
557 |
} |
char *p; |
558 |
return i; |
|
559 |
} |
strcat (path, "\\*"); |
560 |
|
hd = FindFirstFile (path, &fd); |
561 |
return -1; |
if (hd == INVALID_HANDLE_VALUE) |
562 |
} /* fm_get_current_pos */ |
return -1; |
563 |
|
do { |
564 |
|
if (fd.cFileName[0] == '.' && strlen (fd.cFileName) < 3) |
565 |
static int |
continue; |
566 |
fm_check_detached_sig( listview_ctrl_t lv, int pos ) |
p = new char [strlen (path) + strlen (fd.cFileName)+1]; |
567 |
{ |
if (!p) |
568 |
char type[128]; |
BUG (0); |
569 |
|
memcpy (p, path, strlen (path)-1); |
570 |
listview_get_item_text( lv, pos, 0, type, 127 ); |
p[strlen (path)-1] = 0; |
571 |
return !strcmp( type, "SIGNED-DETACH" )? 1 : 0; |
strcat (p, fd.cFileName); |
572 |
} /* fm_check_detached_sig */ |
if (!is_directory (p)) |
573 |
|
fm_set_ftype (lv, fm, p); |
574 |
|
free_if_alloc (p); |
575 |
int |
} while (FindNextFile (hd, &fd)); |
576 |
fm_check_file_type (listview_ctrl_t lv, int pos, int fm_cmd) |
FindClose (hd); |
577 |
{ |
return 0; |
578 |
char status[128]; |
} |
579 |
int rc = 0; |
|
580 |
|
|
581 |
listview_get_item_text (lv, pos, 0, status, sizeof (status) - 1); |
/* Add the drag & drop files from @dd_files to the |
582 |
|
list view control @lv. */ |
583 |
switch (fm_cmd) { |
int |
584 |
case FM_ENCRYPT: |
fm_add_dropped_files (fm_info_t fm, HDROP dd_files) |
585 |
case FM_ENCRYPT_DIR: |
{ |
586 |
case FM_SIGNENCRYPT: |
char name[MAX_PATH+32+4]; |
587 |
if (strcmp (status, "ENCRYPTED") |
int rc = 0; |
588 |
&& strcmp (status, "SYMKEYENC")) |
UINT i; |
589 |
rc = 1; |
|
590 |
break; |
for (i = 0; i < DragQueryFile (dd_files, 0xFFFFFFFF, NULL, 0); i++) { |
591 |
|
memset (name, 0, sizeof (name)); |
592 |
case FM_DECRYPT: |
DragQueryFile (dd_files, i, name, DIM (name) -1); |
593 |
if (!strcmp (status, "DATA") |
if (is_directory (name)) |
594 |
|| !strcmp (status, "ENCRYPTED") |
rc = fm_add_dir_files (fm->lv, &fm->model, name); |
595 |
|| !strcmp (status, "SYMKEYENC") |
else |
596 |
|| !strcmp (status, "ARMORED")) |
rc = fm_set_ftype (fm->lv, &fm->model, name); |
597 |
rc = 1; |
if (rc == -1) |
598 |
break; |
break; /* XXX: fixme? */ |
599 |
|
} |
600 |
case FM_SIGN: |
DragFinish (dd_files); |
601 |
if( strncmp( status, "SIGNED", 6 ) ) |
return rc; |
602 |
rc = 1; |
} |
603 |
break; |
|
604 |
|
|
605 |
case FM_VERIFY: |
/* Add a single file @name to the list view and before |
606 |
if( !strncmp( status, "SIGNED", 6 ) |
figure out the type of it. |
607 |
|| !strcmp( status, "COMPRESSED" ) ) |
Return value: 0 on success. */ |
608 |
rc = 1; |
static int |
609 |
break; |
add_single_file (listview_ctrl_t lv, fm_model_t *fm, const char *name) |
610 |
|
{ |
611 |
case FM_SYMENC: |
fm_model_t m; |
612 |
if( strcmp( status, "SYMKEYENC" ) ) |
const char *type; |
613 |
rc = 1; |
int rc; |
614 |
break; |
|
615 |
|
type = fm_get_file_type (name, NULL); |
616 |
case FM_IMPORT: |
if (!type) |
617 |
if( !strcmp( status, "PUBKEY" ) |
return WPTERR_FILE_OPEN; |
618 |
|| !strcmp( status, "SECKEY" ) ) |
m = new fm_model_s; |
619 |
rc = 1; |
memset (m, 0, sizeof *m); |
620 |
break; |
if (!strcmp (type, "UNKNOWN")) |
621 |
|
type = gnupg_check_file_ext (name, NULL); |
622 |
case FM_WIPE: |
rc = listview_add_item2 (lv, "", (void*)m); |
623 |
case FM_LIST: |
if (!rc) { |
624 |
rc = 1; |
listview_add_sub_item (lv, 0, FM_COL_STAT, type); |
625 |
break; |
listview_add_sub_item (lv, 0, FM_COL_NAME, name); |
626 |
} |
} |
627 |
|
m->status = m_strdup (type); |
628 |
return rc; |
m->name = m_strdup (name); |
629 |
} /* fm_check_file_type */ |
m->op = NULL; |
630 |
|
fm_model_add_file (fm, m); |
631 |
|
return rc; |
632 |
static void |
} |
633 |
fm_set_status (listview_ctrl_t lv, int pos, int fm_cmd, int success, |
|
634 |
const char * output) |
|
635 |
{ |
/* Use the common Open-File-Dialog to allow the user to |
636 |
char status[128], operat[128]; |
add one ore more selected files to the listview @lv. */ |
637 |
int update = 1; |
int |
638 |
const char *s; |
fm_add_opened_files (fm_info_t fm, HWND dlg) |
639 |
|
{ |
640 |
if ( fm_cmd == FM_LIST ) |
OPENFILENAME open; |
641 |
return; |
char file[512], name[MAX_PATH+1]; |
642 |
success ? s = "SUCCESS" : s = "FAILED"; |
char *path = NULL; |
643 |
strcpy( operat, s ); |
const char *s; |
644 |
|
int i, len=0, n=0; |
645 |
switch (fm_cmd) { |
int rc=0; |
646 |
case FM_ENCRYPT: |
|
647 |
case FM_ENCRYPT_DIR: |
memset (&open, 0, sizeof (open)); |
648 |
case FM_SIGNENCRYPT: strcpy( status, "ENCRYPTED" ); break; |
open.lStructSize = sizeof (OPENFILENAME); |
649 |
case FM_DECRYPT: strcpy( status, "UNKNOWN" ); break; |
open.hInstance = glob_hinst; |
650 |
case FM_SIGN: strcpy( status, "SIGNED" ); break; |
open.lpstrTitle = _("File Open"); |
651 |
case FM_VERIFY: update = 0; break; |
open.lpstrFilter = "All Files (*.*)\0*.*\0\0"; |
652 |
case FM_SYMENC: strcpy( status, "SYMKEYENC" ); break; |
open.hwndOwner = dlg; |
653 |
case FM_IMPORT: update = 0; break; |
open.lpstrFile = file; |
654 |
case FM_WIPE: strcpy( status, "WIPED" ); break; |
open.nMaxFile = DIM (file) - 1; |
655 |
default: strcpy( status, "UNKNOWN"); break; |
open.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER ; |
656 |
} |
|
657 |
|
memset (file, 0, sizeof file); |
658 |
if (success) { |
if (!GetOpenFileName (&open)) |
659 |
if (update) { |
return 0; |
660 |
listview_add_sub_item (lv, pos, 0, status); |
|
661 |
listview_add_sub_item (lv, pos, 1, output); |
/* It is possible that multiple files are returned |
662 |
} |
and then they are separated by \0 chars. */ |
663 |
} |
s = file; |
664 |
listview_add_sub_item( lv, pos, 2, operat ); |
len = DIM (file)-1; |
665 |
} /* fm_set_status */ |
for (;;) { |
666 |
|
if (len < 2 || (*s == '\0' && *(s+1) == '\0')) |
667 |
|
break; |
668 |
int |
memset (name, 0, sizeof (name)); |
669 |
fm_clearsign_8bit (listview_ctrl_t lv, fm_state_s *ctx) |
for (i=0; len > 0; len--, i++) { |
670 |
{ |
if (*s == '\0') { |
671 |
FILE *f; |
name[i] = *s++; |
672 |
byte buf[32]; |
break; |
673 |
char name[256]; |
} |
674 |
int i, n, cnt=0; |
name[i] = *s++; |
675 |
|
} |
676 |
if (ctx->sigmode != GPGME_SIG_MODE_CLEAR) |
if (n == 0) |
677 |
return 0; |
path = m_strdup (name); |
678 |
listview_get_item_text (lv, -1, 1, name, sizeof (name)-1); |
else { |
679 |
if (stristr (name, ".TXT")) |
char *p = make_filename (path, name, NULL); |
680 |
return 0; |
rc = add_single_file (fm->lv, &fm->model, p); |
681 |
f = fopen (name, "rb"); |
free_if_alloc (p); |
682 |
if (!f) |
} |
683 |
return -1; /* should never happen */ |
n++; |
684 |
n = fread (buf, 1, 32, f); |
} |
685 |
for (i = 0; i < n; i++) { |
if (n == 1) /* single file selected. */ |
686 |
if (buf[i] == 0x00 || buf[i] > 170) |
rc = add_single_file (fm->lv, &fm->model, path); |
687 |
cnt++; |
free_if_alloc (path); |
688 |
} |
return rc; |
689 |
fclose (f); |
} |
690 |
if (!cnt) |
|
691 |
return 0; |
|
692 |
n = -1; |
int |
693 |
i = log_box (_("File Manager"), MB_WARN|MB_YESNO, |
fm_assume_onepass_sig (const char *fname) |
694 |
_("\"%s\" does not seems to be a text file.\n" |
{ |
695 |
"Do you really want to clearsign it?"), name); |
armor_filter_context_t afx; |
696 |
if (i == IDYES) |
gpg_iobuf_t fp; |
697 |
n = 0; |
gpgme_data_t dat; |
698 |
return n; |
PACKET *pkt; |
699 |
} |
char tmpfile[MAX_PATH+1]; |
700 |
|
int check = 0; |
701 |
int |
|
702 |
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
pkt = (PACKET *)calloc (1, sizeof *pkt); |
703 |
{ |
if (!fname) { |
704 |
struct secdel_confirm_s confirm = {0}; |
get_temp_name (tmpfile, DIM (tmpfile)-1, "gpgme.tmp"); |
705 |
struct progress_filter_s pfx, pfx2; |
gpg_data_new_from_clipboard (&dat, 0); |
706 |
fm_state_s * ctx; |
gpg_data_release_and_set_file (dat, tmpfile); |
707 |
int fm_cmd, sig_detached = 0; |
|
708 |
int rc = 0, i, n, ndel = 0; |
fp = gpg_iobuf_open (tmpfile); |
709 |
char fname[512], status[128]; |
if (!fp) |
710 |
|
return 0; |
711 |
switch (cmd) { |
gpg_iobuf_ioctl (fp, 3, 1, NULL); /* disable cache. */ |
712 |
case ID_FILEMISC_ENCRYPT: fm_cmd = FM_ENCRYPT; break; |
if (gpg_use_armor_filter(fp)) { |
713 |
case ID_FILEMISC_DECRYPT: fm_cmd = FM_DECRYPT; break; |
memset (&afx, 0, sizeof (afx)); |
714 |
case ID_FILEMISC_SYMENC: fm_cmd = FM_SYMENC; break; |
gpg_iobuf_push_filter (fp, gpg_armor_filter, &afx); |
715 |
case ID_FILEMISC_SIGN: fm_cmd = FM_SIGN; break; |
} |
716 |
case ID_FILEMISC_VERIFY: fm_cmd = FM_VERIFY; break; |
gpg_init_packet (pkt); |
717 |
case ID_FILEMISC_IMPORT: fm_cmd = FM_IMPORT; break; |
if (!gpg_parse_packet (fp, pkt) |
718 |
case ID_FILEMISC_WIPE: fm_cmd = FM_WIPE; break; |
&& pkt->pkttype == PKT_COMPRESSED) |
719 |
case ID_FILEMISC_LIST: fm_cmd = FM_LIST; break; |
check = 1; |
720 |
case ID_FILEMISC_SIGNENC: fm_cmd = FM_SIGNENCRYPT; break; |
gpg_free_packet (pkt); |
721 |
default: return 1; /* unknown command */ |
gpg_iobuf_close (fp); |
722 |
} |
DeleteFile (tmpfile); |
723 |
|
} |
724 |
if (fm_get_current_pos (lv) == -1) |
/* XXX: implement it for real files */ |
725 |
return WPTERR_GENERAL; |
safe_free (pkt); |
726 |
rc = fm_state_new (&ctx); |
return check; |
727 |
if (rc) |
} |
728 |
BUG (0); |
|
729 |
ctx->dlg = dlg; |
|
730 |
|
int |
731 |
// XXX: for file operations the progress dialog will be |
fm_get_current_pos (listview_ctrl_t lv) |
732 |
// reloaded somewhere and thus a 'dummy' dialog remains |
{ |
733 |
|
int i = 0; |
734 |
/* we use it here to make sure that pfx_cleanup will not use |
int items; |
735 |
any weird values. */ |
|
736 |
memset (&pfx, 0, sizeof (pfx)); |
items = listview_count_items (lv, 0); |
737 |
ctx->prog_cb = NULL; |
if (!items) |
738 |
if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) { |
return -1; |
739 |
pfx.hwnd = dlg; |
else if (items == 1) { |
740 |
/*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/ |
listview_select_one (lv, 0); |
741 |
/*ctx->prog_cb = &pfx;*/ |
return 0; |
742 |
} |
} |
743 |
|
else if (items > 1) { |
744 |
/* Commands we need before we can perform the main command */ |
i = listview_get_curr_pos (lv); |
745 |
switch (fm_cmd) { |
if (i == -1) { |
746 |
case FM_ENCRYPT: |
msg_box (lv->ctrl, _("Please select a file."), |
747 |
case FM_SIGNENCRYPT: |
_("File Manager"), MB_ERR); |
748 |
if (fm_cmd == FM_SIGNENCRYPT) |
return -1; |
749 |
ctx->req_signer = 1; |
} |
750 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_ENCRYPT, ctx->dlg, |
return i; |
751 |
file_encrypt_dlg_proc, (LPARAM)ctx); |
} |
752 |
if (ctx->cancel == 1) { |
|
753 |
rc = WPTERR_GENERAL; |
return -1; |
754 |
goto leave; |
} |
755 |
} |
|
756 |
break; |
|
757 |
|
static int |
758 |
case FM_SIGN: |
fm_check_detached_sig (listview_ctrl_t lv, int pos) |
759 |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FILE_SIGN, dlg, |
{ |
760 |
file_sign_dlg_proc, (LPARAM) ctx); |
char type[64]; |
761 |
if (ctx->cancel == 1 || fm_clearsign_8bit (lv, ctx)) { |
|
762 |
rc = WPTERR_GENERAL; |
listview_get_item_text (lv, pos, 0, type, DIM (type)-1); |
763 |
goto leave; |
return !strcmp (type, "SIGNED-DETACH")? 1 : 0; |
764 |
} |
} |
765 |
break; |
|
766 |
|
|
767 |
case FM_WIPE: |
/* Check if the given file on position @pos is useful for |
768 |
memset (&pfx2, 0, sizeof (pfx2)); |
command @fmd_cmd. For example sign can be used for all files |
769 |
secure_unlink_set_cb (progress_callback, &pfx2); |
which are not already signed. */ |
770 |
break; |
int |
771 |
} |
fm_check_file_type (listview_ctrl_t lv, int pos, int fm_cmd) |
772 |
|
{ |
773 |
for( i = 0, n = 0; i < listview_count_items( lv, 0 ); i++ ) { |
char status[64]; |
774 |
if( !listview_get_item_state( lv, i ) ) |
int rc = 0; |
775 |
continue; |
|
776 |
listview_get_item_text( lv, i, 0, status, sizeof (status) -1 ); |
listview_get_item_text (lv, pos, 0, status, DIM (status) - 1); |
777 |
if (!strcmp( status, "ENCRYPTED" ) && fm_cmd == FM_DECRYPT) |
|
778 |
n++; |
switch (fm_cmd) { |
779 |
if (!strcmp( status, "UNKNOWN" ) && fm_cmd == FM_SIGN) |
case FM_ENCRYPT: |
780 |
n++; |
case FM_ENCRYPT_DIR: |
781 |
if (fm_cmd == FM_WIPE) |
case FM_SIGNENCRYPT: |
782 |
ndel++; |
if (strcmp (status, "ENCRYPTED") |
783 |
} |
&& strcmp (status, "SYMKEYENC")) |
784 |
|
rc = 1; |
785 |
if (n > 1 && fm_cmd != FM_SYMENC) |
break; |
786 |
ctx->cache_cb = 1; |
|
787 |
|
case FM_DECRYPT: |
788 |
if (fm_cmd == FM_WIPE && ndel > 0) { |
if (!strcmp (status, "DATA") || |
789 |
memset (&confirm, 0, sizeof confirm); |
!strcmp (status, "ENCRYPTED") || |
790 |
confirm.lv_files = lv; |
!strcmp (status, "SYMKEYENC") || |
791 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
!strcmp (status, "ARMORED")) |
792 |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
rc = 1; |
793 |
if (!confirm.yes) |
break; |
794 |
goto leave; |
|
795 |
} |
case FM_SIGN: |
796 |
|
if (strncmp( status, "SIGNED", 6)) |
797 |
for( i = 0; i < listview_count_items( lv, 0 ); i++ ) { |
rc = 1; |
798 |
if( !listview_get_item_state( lv, i ) ) |
break; |
799 |
continue; |
|
800 |
listview_get_item_text( lv, i, 1, fname, sizeof (fname) - 1 ); |
case FM_VERIFY: |
801 |
if( file_exist_check( fname ) && !is_directory( fname ) ) { |
if(!strncmp (status, "SIGNED", 6) || |
802 |
log_box( _("File Manager"), MB_ERR, _("\"%s\" does not exist"), fname ); |
!strcmp (status, "COMPRESSED")) |
803 |
continue; |
rc = 1; |
804 |
} |
break; |
805 |
if( is_directory( fname ) ) |
|
806 |
fm_cmd = FM_ENCRYPT_DIR; |
case FM_SYMENC: |
807 |
if( !fm_check_file_type( lv, i, fm_cmd ) ) |
if (strcmp (status, "SYMKEYENC")) |
808 |
continue; |
rc = 1; |
809 |
sig_detached = fm_check_detached_sig( lv, i ); |
break; |
810 |
switch( fm_cmd ) { |
|
811 |
case FM_LIST: rc = fm_list( fname, dlg ); break; |
case FM_IMPORT: |
812 |
case FM_WIPE: rc = fm_wipe( fname ); break; |
if (!strcmp (status, "PUBKEY") || |
813 |
case FM_ENCRYPT: rc = fm_encrypt( ctx, fname, 0 ); break; |
!strcmp (status, "SECKEY")) |
814 |
case FM_ENCRYPT_DIR: rc = fm_encrypt_directory( ctx, fname ); break; |
rc = 1; |
815 |
case FM_SIGNENCRYPT: rc = fm_encrypt( ctx, fname, 1 ); break; |
break; |
816 |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
|
817 |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
case FM_WIPE: |
818 |
case FM_SYMENC: rc = fm_sym_encrypt( ctx, fname );break; |
case FM_LIST: |
819 |
case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break; |
rc = 1; |
820 |
case FM_IMPORT: |
break; |
821 |
free_if_alloc (ctx->opaque); |
} |
822 |
ctx->opaque = m_strdup (fname); |
|
823 |
if (!ctx->opaque) |
return rc; |
824 |
BUG (0); |
} |
825 |
DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
|
826 |
file_import_dlg_proc, (LPARAM)ctx ); |
|
827 |
if (ctx->cancel == 1) |
/* Set the file status of the given command @fm_cmd. |
828 |
continue; |
@success is 0 on success. */ |
829 |
rc = fm_import (ctx, fname); |
static void |
830 |
break; |
fm_set_status (listview_ctrl_t lv, int pos, int fm_cmd, |
831 |
} |
gpgme_sig_mode_t sigmode, int success, const char *output) |
832 |
fm_set_status (lv, i, fm_cmd, !rc, ctx->output); |
{ |
833 |
free_if_alloc (ctx->output); |
char status[128], operat[128]; |
834 |
progress_cleanup (&pfx); |
int update = 1; |
835 |
} |
const char *s; |
836 |
if (fm_cmd == FM_WIPE) { |
|
837 |
secure_unlink_set_cb (NULL, NULL); |
if (fm_cmd == FM_LIST) |
838 |
progress_cleanup (&pfx2); |
return; |
839 |
} |
success ? s = "SUCCESS" : s = "FAILED"; |
840 |
if (ctx->cache_cb) { |
strcpy (operat, s); |
841 |
release_gpg_passphrase_cb (&ctx->pass_cb); |
|
842 |
ctx->cache_cb = 0; /* make sure it's only used for this session! */ |
switch (fm_cmd) { |
843 |
} |
case FM_ENCRYPT: |
844 |
|
case FM_ENCRYPT_DIR: |
845 |
/* remove wipe files from the list */ |
case FM_SIGNENCRYPT: |
846 |
n = listview_count_items (lv, 0); |
strcpy (status, "ENCRYPTED"); |
847 |
while (n--) { |
break; |
848 |
char status[128]; |
|
849 |
listview_get_item_text (lv, n, 0, status, sizeof (status) - 1); |
case FM_DECRYPT: |
850 |
if( !strcmp (status, "WIPED")) |
strcpy (status, "UNKNOWN"); |
851 |
listview_del_item (lv, n); |
break; |
852 |
} |
|
853 |
|
case FM_SIGN: |
854 |
leave: |
if (sigmode == GPGME_SIG_MODE_DETACH) |
855 |
if (!rc) |
strcpy (status, "SIGNED-DETACH"); |
856 |
fm_state_release (ctx); |
else if (sigmode == GPGME_SIG_MODE_CLEAR) |
857 |
progress_cleanup (&pfx); |
strcpy (status, "SIGNED-CLEAR"); |
858 |
return rc; |
else |
859 |
} /* fm_parse_files */ |
strcpy (status, "SIGNED"); |
860 |
|
break; |
861 |
|
case FM_VERIFY: |
862 |
/* Wipe the given file @name with the delete mode |
update = 0; |
863 |
from the configuration. |
break; |
864 |
Return value: 0 on success. */ |
|
865 |
int |
case FM_SYMENC: |
866 |
fm_wipe (const char *name) |
strcpy (status, "SYMKEYENC"); |
867 |
{ |
break; |
868 |
int rc; |
|
869 |
|
case FM_IMPORT: |
870 |
SetCursor (LoadCursor (NULL, IDC_WAIT)); |
update = 0; |
871 |
remove_crit_file_attrs (name, 1); |
break; |
872 |
rc = secure_unlink (name, reg_prefs.wipe_mode); |
|
873 |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
case FM_WIPE: |
874 |
return rc; |
strcpy (status, "WIPED"); |
875 |
} |
break; |
876 |
|
|
877 |
|
default: |
878 |
/* Dump out the given PGP packets from file @name in a dialog. */ |
strcpy (status, "UNKNOWN"); |
879 |
int |
break; |
880 |
fm_list (const char *name, HWND dlg) |
} |
881 |
{ |
|
882 |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
if (success && update) { |
883 |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
listview_add_sub_item (lv, pos, FM_COL_STAT, status); |
884 |
IDS_WINPT_FILE_STAT ); |
listview_add_sub_item (lv, pos, FM_COL_NAME, output); |
885 |
return 0; |
} |
886 |
} |
listview_add_sub_item (lv, pos, FM_COL_OP, operat); |
887 |
|
} |
888 |
|
|
889 |
static int |
|
890 |
ask_filename (fm_state_t c, const char *msg, char **dst) |
int |
891 |
{ |
fm_clearsign_8bit (listview_ctrl_t lv, fm_state_s *ctx) |
892 |
const char * s; |
{ |
893 |
|
FILE *f; |
894 |
s = get_filename_dlg (c->dlg, FILE_SAVE, msg, NULL, NULL); |
BYTE buf[32]; |
895 |
if (!s) |
char name[MAX_PATH+1]; |
896 |
return WPTERR_GENERAL; |
int i, n, cnt=0; |
897 |
|
|
898 |
if (dst != NULL) |
if (ctx->sigmode != GPGME_SIG_MODE_CLEAR) |
899 |
free_if_alloc (*dst); |
return 0; |
900 |
free_if_alloc (c->output); |
listview_get_item_text (lv, -1, FM_COL_NAME, name, DIM (name)-1); |
901 |
c->output = m_strdup (s); |
if (stristr (name, ".txt")) |
902 |
if (!c->output) |
return 0; |
903 |
BUG (0); |
f = fopen (name, "rb"); |
904 |
if (dst) |
if (!f) |
905 |
*dst = fm_quote_file (s); |
return -1; /* should never happen */ |
906 |
return 0; |
n = fread (buf, 1, 32, f); |
907 |
} |
fclose (f); |
908 |
|
for (i = 0; i < n; i++) { |
909 |
|
if (buf[i] == 0x00 || buf[i] > 170) |
910 |
int |
cnt++; |
911 |
fm_encrypt (fm_state_t c, const char *name, int sign) |
} |
912 |
{ |
if (!cnt) |
913 |
gpgme_error_t err; |
return 0; |
914 |
gpgme_key_t key = NULL; |
n = -1; |
915 |
gpgme_ctx_t ctx = c->ctx; |
i = log_box (_("File Manager"), MB_WARN|MB_YESNO, |
916 |
file_data_t in=NULL, out=NULL; |
_("\"%s\" does not seems to be a text file.\n" |
917 |
char *keyid = NULL, ext[5]; |
"Do you really want to clearsign it?"), name); |
918 |
int no_compr = 0; |
if (i == IDYES) |
919 |
int rc = 0; |
n = 0; |
920 |
|
return n; |
921 |
c->output = new char[strlen (name) + 5 + 1]; |
} |
922 |
if (!c->output) |
|
923 |
BUG (0); |
|
924 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
/* Wipe the given file @name with the delete mode |
925 |
strcpy (c->output, name); |
from the configuration. |
926 |
strcat (c->output, ext); |
Return value: 0 on success. */ |
927 |
|
static int |
928 |
if (!overwrite_file (c->output)) { |
fm_wipe (const char *name, int mode, int passes) |
929 |
rc = ask_filename (c, _("Enter filename for encrypted file"), NULL); |
{ |
930 |
if (rc) |
int rc; |
931 |
goto leave; |
|
932 |
} |
SetCursor (LoadCursor (NULL, IDC_WAIT)); |
933 |
|
remove_crit_file_attrs (name, 1); |
934 |
err = gpg_file_data_new (name, 1, &in); |
rc = secure_unlink (name, mode, passes); |
935 |
if (err) |
SetCursor (LoadCursor (NULL, IDC_ARROW)); |
936 |
goto leave; |
return rc; |
937 |
err = gpg_file_data_new (c->output, 0, &out); |
} |
938 |
if (err) |
|
939 |
goto leave; |
int |
940 |
|
fm_parse_files (listview_ctrl_t lv, HWND dlg, int cmd) |
941 |
/* |
{ |
942 |
if (c->prog_cb) { |
struct secdel_confirm_s confirm = {0}; |
943 |
c->prog_cb->what = name; |
struct progress_filter_s pfx, pfx2; |
944 |
gpg_file_data_set_cb (in, c->prog_cb); |
fm_state_s * ctx; |
945 |
} |
int fm_cmd, sig_detached = 0; |
946 |
*/ |
int rc = 0, i, n, ndel = 0; |
947 |
|
char fname[2*MAX_PATH+1], status[128]; |
948 |
/* XXX |
|
949 |
no_compr = is_multi_media (name); |
switch (cmd) { |
950 |
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
case ID_FILEMISC_ENCRYPT: fm_cmd = FM_ENCRYPT; break; |
951 |
*/ |
case ID_FILEMISC_ENCRYPT_ZIP:fm_cmd = FM_ENCRYPT_ZIP; break; |
952 |
|
case ID_FILEMISC_DECRYPT: fm_cmd = FM_DECRYPT; break; |
953 |
if (sign) { |
case ID_FILEMISC_SYMENC: fm_cmd = FM_SYMENC; break; |
954 |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
case ID_FILEMISC_SIGN: fm_cmd = FM_SIGN; break; |
955 |
keyid = get_gnupg_default_key (); |
case ID_FILEMISC_VERIFY: fm_cmd = FM_VERIFY; break; |
956 |
if (!keyid) { |
case ID_FILEMISC_IMPORT: fm_cmd = FM_IMPORT; break; |
957 |
msg_box (c->dlg, _("Could not get default secret key."), |
case ID_FILEMISC_WIPE: fm_cmd = FM_WIPE; break; |
958 |
_("Signing"), MB_ERR); |
case ID_FILEMISC_LIST: fm_cmd = FM_LIST; break; |
959 |
rc = WPTERR_GENERAL; |
case ID_FILEMISC_SIGNENC: fm_cmd = FM_SIGNENCRYPT; break; |
960 |
goto leave; |
default: return 1; /* unknown command */ |
961 |
} |
} |
962 |
if (get_seckey (keyid, &key)) |
|
963 |
BUG (0); |
if (fm_get_current_pos (lv) == -1) |
964 |
gpgme_signers_add (ctx, key); |
return WPTERR_GENERAL; |
965 |
} |
rc = fm_state_new (&ctx); |
966 |
else { |
if (rc) |
967 |
gpgme_key_t key = gpgme_signers_enum (ctx, 0); |
BUG (0); |
968 |
if (key && key->subkeys) { |
ctx->dlg = dlg; |
969 |
keyid = m_strdup (key->subkeys->keyid); |
|
970 |
if (!keyid) |
// XXX: for file operations the progress dialog will be |
971 |
BUG (NULL); |
// reloaded somewhere and thus a 'dummy' dialog remains |
972 |
} |
|
973 |
} |
/* we use it here to make sure that pfx_cleanup will not use |
974 |
if (!c->init_cb || !c->cache_cb) { |
any weird values. */ |
975 |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, |
memset (&pfx, 0, sizeof (pfx)); |
976 |
c->dlg, _("Signing")); |
ctx->prog_cb = NULL; |
977 |
c->init_cb = 1; |
if (cmd != FM_VERIFY && cmd != FM_SIGN /*&& reg_prefs.fm.progress > 0*/) { |
978 |
} |
pfx.hwnd = dlg; |
979 |
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
/*gpgme_set_progress_cb (ctx->ctx, progress_callback, &pfx);*/ |
980 |
in->dat, out->dat); |
/*ctx->prog_cb = &pfx;*/ |
981 |
if (!c->cache_cb) |
} |
982 |
release_gpg_passphrase_cb (&c->pass_cb); |
|
983 |
if (c->pass_cb.cancel) { |
/* Commands we need before we can perform the main command */ |
984 |
rc = WPTERR_GENERAL; |
switch (fm_cmd) { |
985 |
goto leave; |
case FM_ENCRYPT_ZIP: |
986 |
} |
case FM_ENCRYPT: |
987 |
if (err) { |
case FM_SIGNENCRYPT: |
988 |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
if (fm_cmd == FM_SIGNENCRYPT) |
989 |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
ctx->req_signer = 1; |
990 |
agent_del_cache (keyid); |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_ENCRYPT, |
991 |
rc = WPTERR_GENERAL; |
ctx->dlg, file_encrypt_dlg_proc, (LPARAM)ctx); |
992 |
goto leave; |
if (ctx->cancel == 1) { |
993 |
} |
rc = WPTERR_GENERAL; |
994 |
} |
goto leave; |
995 |
else { |
} |
996 |
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
break; |
997 |
in->dat, out->dat); |
|
998 |
if (err) { |
case FM_SIGN: |
999 |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_FILE_SIGN, dlg, |
1000 |
rc = WPTERR_GENERAL; |
file_sign_dlg_proc, (LPARAM) ctx); |
1001 |
goto leave; |
if (ctx->cancel == 1 || fm_clearsign_8bit (lv, ctx)) { |
1002 |
} |
rc = WPTERR_GENERAL; |
1003 |
} |
goto leave; |
1004 |
if (c->wipe) |
} |
1005 |
secure_unlink (name, WIPE_MODE_SIMPLE); |
break; |
1006 |
|
|
1007 |
leave: |
case FM_WIPE: |
1008 |
if (in) |
memset (&pfx2, 0, sizeof (pfx2)); |
1009 |
gpg_file_data_release (in); |
secure_unlink_set_cb (progress_callback, &pfx2); |
1010 |
if (out) |
break; |
1011 |
gpg_file_data_release (out); |
} |
1012 |
free_if_alloc (keyid); |
|
1013 |
return rc; |
for( i = 0, n = 0; i < listview_count_items (lv, 0); i++ ) { |
1014 |
} |
if (!listview_get_item_state (lv, i)) |
1015 |
|
continue; |
1016 |
|
listview_get_item_text (lv, i, FM_COL_STAT, status, DIM (status) -1); |
1017 |
int |
if (!strcmp (status, "ENCRYPTED") && fm_cmd == FM_DECRYPT) |
1018 |
fm_sym_encrypt (fm_state_t c, const char * name) |
n++; |
1019 |
{ |
if (!strcmp (status, "UNKNOWN") && fm_cmd == FM_SIGN) |
1020 |
gpgme_ctx_t ctx = c->ctx; |
n++; |
1021 |
gpgme_error_t err; |
if (fm_cmd == FM_WIPE) |
1022 |
file_data_t in=NULL, out=NULL; |
ndel++; |
1023 |
int rc = 0, cancel = 0; |
} |
1024 |
char * src = NULL, * dst = NULL; |
|
1025 |
char ext[5], * pass; |
if (n > 1 && fm_cmd != FM_SYMENC) |
1026 |
|
ctx->cache_cb = 1; |
1027 |
pass = request_passphrase2 (_("Symmetric"), 0, &cancel); |
|
1028 |
if (cancel) |
if (fm_cmd == FM_WIPE && ndel > 0) { |
1029 |
return 0; |
memset (&confirm, 0, sizeof confirm); |
1030 |
|
confirm.lv_files = lv; |
1031 |
/* XXX gpgme_control (ctx, GPGME_CTRL_CIPHER, -1);*/ |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILES_SECDEL, ctx->dlg, |
1032 |
c->output = new char[strlen (name) + 5 + 1]; |
file_secdel_confirm_dlg_proc, (LPARAM)&confirm); |
1033 |
if (!c->output) |
if (!confirm.yes) |
1034 |
BUG (0); |
goto leave; |
1035 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
} |
1036 |
strcpy (c->output, name); |
|
1037 |
strcat (c->output, ext); |
if (fm_cmd == FM_ENCRYPT_ZIP) |
1038 |
|
fm_encrypt_into_zip (ctx, lv); |
1039 |
if (overwrite_file (c->output) == 0) { |
|
1040 |
rc = WPTERR_GENERAL; |
for (i = 0; i < listview_count_items (lv, 0); i++) { |
1041 |
goto leave; |
if (!listview_get_item_state (lv, i)) |
1042 |
} |
continue; |
1043 |
|
listview_get_item_text (lv, i, FM_COL_NAME, fname, DIM (fname) - 1); |
1044 |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
if (file_exist_check (fname) && !is_directory (fname)) { |
1045 |
|
log_box (_("File Manager"), MB_ERR, |
1046 |
err = gpg_file_data_new (name, 1, &in); |
_("\"%s\" does not exist"), fname); |
1047 |
if (err) |
continue; |
1048 |
goto leave; |
} |
1049 |
err = gpg_file_data_new (c->output, 0, &out); |
if (is_directory (fname)) |
1050 |
if (err) |
fm_cmd = FM_ENCRYPT_DIR; |
1051 |
goto leave; |
if (!fm_check_file_type (lv, i, fm_cmd)) |
1052 |
|
continue; |
1053 |
err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in->dat, out->dat); |
sig_detached = fm_check_detached_sig (lv, i); |
1054 |
if (err) { |
switch (fm_cmd) { |
1055 |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
case FM_LIST: rc = fm_list( fname, dlg ); break; |
1056 |
rc = WPTERR_GENERAL; |
case FM_WIPE: rc = fm_wipe (fname, confirm.mode, confirm.passes);break; |
1057 |
goto leave; |
case FM_ENCRYPT: rc = fm_encrypt( ctx, fname, 0 ); break; |
1058 |
} |
case FM_ENCRYPT_DIR: rc = fm_encrypt_directory( ctx, fname ); break; |
1059 |
if (file_exist_check (c->output)) { |
case FM_SIGNENCRYPT: rc = fm_encrypt( ctx, fname, 1 ); break; |
1060 |
msg_box (c->dlg, _("Encryption failed."), _("Symmetric"), MB_ERR); |
case FM_DECRYPT: rc = fm_decrypt( ctx, fname ); break; |
1061 |
rc = WPTERR_GENERAL; |
case FM_SIGN: rc = fm_sign( ctx, fname ); break; |
1062 |
} |
case FM_SYMENC: rc = fm_sym_encrypt (ctx, fname); break; |
1063 |
|
case FM_VERIFY: rc = fm_verify (ctx, sig_detached, fname);break; |
1064 |
leave: |
case FM_IMPORT: |
1065 |
if (in) |
free_if_alloc (ctx->opaque); |
1066 |
gpg_file_data_release (in); |
ctx->opaque = m_strdup (fname); |
1067 |
if (out) |
DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_IMPORT, dlg, |
1068 |
gpg_file_data_release (out); |
file_import_dlg_proc, (LPARAM)ctx); |
1069 |
sfree_if_alloc (pass); |
if (ctx->cancel == 1) |
1070 |
return rc; |
continue; |
1071 |
} /* fm_sym_encrypt */ |
rc = fm_import (ctx, fname); |
1072 |
|
break; |
1073 |
|
} |
1074 |
/* Show the human readable verify result from @sigres. */ |
if (ctx->cancel == 1) { |
1075 |
static void |
ctx->cancel = 0; |
1076 |
show_verify_result (gpgme_verify_result_t sigres) |
continue; |
1077 |
{ |
} |
1078 |
gpgme_key_t key=NULL; |
fm_set_status (lv, i, fm_cmd, ctx->sigmode, !rc, ctx->output); |
1079 |
gpgme_signature_t sig=sigres->signatures; |
free_if_alloc (ctx->output); |
1080 |
const char *s, *keyid; |
progress_cleanup (&pfx); |
1081 |
int sigok = 0; |
} |
1082 |
int type; |
|
1083 |
char buf[384]; |
if (fm_cmd == FM_WIPE) { |
1084 |
|
secure_unlink_set_cb (NULL, NULL); |
1085 |
sig = sigres->signatures; |
progress_cleanup (&pfx2); |
1086 |
sigok = sig->summary & GPGME_SIGSUM_GREEN; |
} |
1087 |
s = sigok? _("Good signature") : _("BAD signature"); |
if (ctx->cache_cb) { |
1088 |
type = sigok? MB_OK: MB_ICONWARNING|MB_OK; |
release_gpg_passphrase_cb (&ctx->pass_cb); |
1089 |
keyid = sig->fpr; |
ctx->cache_cb = 0; /* make sure it's only used for this session! */ |
1090 |
if (!keyid) |
} |
1091 |
return; |
|
1092 |
|
/* remove wipe files from the list */ |
1093 |
keyid = strlen (sig->fpr) == 40? sig->fpr+32 : sig->fpr + 24; |
n = listview_count_items (lv, 0); |
1094 |
get_pubkey (sig->fpr, &key); |
while (n--) { |
1095 |
_snprintf (buf, sizeof (buf)-1, "Signature made %s using %s key ID %s\n" |
listview_get_item_text (lv, n, FM_COL_STAT, status, DIM (status) - 1); |
1096 |
"%s from \"%s\"", |
if (!strcmp (status, "WIPED")) |
1097 |
strtimestamp (sig->timestamp), get_key_pubalgo (sig->pubkey_algo), |
listview_del_item (lv, n); |
1098 |
keyid, s, key? key->uids->uid : _("user ID not found")); |
} |
1099 |
msg_box (NULL, buf, _("Decrypt Verify"), type); |
|
1100 |
} |
leave: |
1101 |
|
if (!rc) |
1102 |
|
fm_state_release (ctx); |
1103 |
/* Check the recipients if we have at least one secret key. */ |
progress_cleanup (&pfx); |
1104 |
bool |
return rc; |
1105 |
secret_key_available (gpgme_recipient_t rset) |
} |
1106 |
{ |
|
1107 |
gpgme_recipient_t r; |
|
1108 |
gpgme_key_t key; |
/* Dump out the given PGP packets from file @name in a dialog. */ |
1109 |
|
int |
1110 |
for (r=rset; r; r = r->next) { |
fm_list (const char *name, HWND dlg) |
1111 |
if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY) |
{ |
1112 |
continue; |
dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_STAT, dlg, |
1113 |
else { |
file_stat_dlg_proc, (LPARAM)name, _("File Status"), |
1114 |
/* extra check to make sure the key is available right now. */ |
IDS_WINPT_FILE_STAT ); |
1115 |
if (!get_seckey (r->keyid, &key)) |
return 0; |
1116 |
return true; |
} |
1117 |
} |
|
1118 |
} |
|
1119 |
return false; |
static int |
1120 |
} |
ask_filename (fm_state_t c, const char *msg, char **dst) |
1121 |
|
{ |
1122 |
|
const char *s; |
1123 |
/* Decrypt the file @name. */ |
|
1124 |
int |
s = get_filesave_dlg (c->dlg, msg, NULL, NULL); |
1125 |
fm_decrypt (fm_state_t c, const char *name) |
if (!s) |
1126 |
{ |
return WPTERR_GENERAL; |
1127 |
gpgme_error_t err; |
|
1128 |
gpgme_ctx_t ctx = c->ctx; |
if (dst != NULL) |
1129 |
gpgme_decrypt_result_t res; |
free_if_alloc (*dst); |
1130 |
gpgme_verify_result_t sigres; |
free_if_alloc (c->output); |
1131 |
file_data_t in =NULL, out=NULL; |
c->output = m_strdup (s); |
1132 |
int is_signed = 0; |
if (dst) |
1133 |
int rc = 0; |
*dst = fm_quote_file (s); |
1134 |
|
return 0; |
1135 |
if (!c->init_cb || !c->cache_cb) { |
} |
1136 |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT, |
|
1137 |
c->dlg, _("Decryption")); |
|
1138 |
c->init_cb = 1; |
int |
1139 |
} |
fm_encrypt (fm_state_t c, const char *name, int sign) |
1140 |
|
{ |
1141 |
c->output = m_strdup (name); |
gpgme_error_t err; |
1142 |
if (!c->output) |
gpgme_key_t key = NULL; |
1143 |
BUG (0); |
gpgme_ctx_t ctx = c->ctx; |
1144 |
if (is_openpgp_ext (c->output)) |
file_data_t in=NULL, out=NULL; |
1145 |
c->output[strlen (c->output)-4] = '\0'; |
char *keyid = NULL, ext[5]; |
1146 |
else { |
int rc = 0; |
1147 |
const char *s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"), |
|
1148 |
NULL, NULL); |
c->output = new char[strlen (name) + 5 + 1]; |
1149 |
if (s) { |
if (!c->output) |
1150 |
free_if_alloc (c->output); |
BUG (0); |
1151 |
c->output = m_strdup (s); |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1152 |
if (!c->output) |
strcpy (c->output, name); |
1153 |
BUG (NULL); |
strcat (c->output, ext); |
1154 |
} |
|
1155 |
} |
if (!overwrite_file (c->output)) { |
1156 |
|
rc = ask_filename (c, _("Enter Filename for Encrypted File"), NULL); |
1157 |
if (overwrite_file (c->output) == 0) { |
if (rc) |
1158 |
rc = ask_filename (c, _("Please enter filename for plaintext file"), NULL); |
goto leave; |
1159 |
if (rc) |
} |
1160 |
goto leave; |
|
1161 |
} |
err = gpg_file_data_new (name, F_DATA_READ, &in); |
1162 |
|
if (err) |
1163 |
remove_crit_file_attrs (c->output, 0); |
goto leave; |
1164 |
|
remove_crit_file_attrs (c->output, 0); |
1165 |
err = gpg_file_data_new (name, 1, &in); |
err = gpg_file_data_new (c->output, F_DATA_WRITE, &out); |
1166 |
if (err) |
if (err) |
1167 |
goto leave; |
goto leave; |
1168 |
err = gpg_file_data_new (c->output, 0, &out); |
|
1169 |
if (err) |
/* |
1170 |
goto leave; |
int no_compr = 0; |
1171 |
|
if (c->prog_cb) { |
1172 |
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
c->prog_cb->what = name; |
1173 |
if (!c->cache_cb) |
gpg_file_data_set_cb (in, c->prog_cb); |
1174 |
release_gpg_passphrase_cb (&c->pass_cb); |
} |
1175 |
if (c->pass_cb.cancel) { |
*/ |
1176 |
rc = WPTERR_GENERAL; |
|
1177 |
goto leave; |
/* XXX: disable compression for multi-media files. |
1178 |
} |
no_compr = is_multi_media (name); |
1179 |
|
gpgme_control (ctx, GPGME_CTRL_NO_COMPR, no_compr); |
1180 |
res = gpgme_op_decrypt_result (ctx); |
*/ |
1181 |
if (res && res->recipients && !secret_key_available (res->recipients)) { |
|
1182 |
const char *keyid = res->recipients->keyid; |
if (sign) { |
1183 |
char *p = get_key_userid (keyid+8); |
if (gpgme_signers_enum (ctx, 0) == NULL) { |
1184 |
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
keyid = get_gnupg_default_key (); |
1185 |
|
if (!keyid) { |
1186 |
log_box( _("Decryption"), MB_ERR, |
msg_box (c->dlg, _("Could not get default secret key."), |
1187 |
_("Encrypted with %s key, ID %s.%s\n" |
_("Signing"), MB_ERR); |
1188 |
"Decryption failed: secret key not available."), |
rc = WPTERR_GENERAL; |
1189 |
get_key_pubalgo (pkalgo), keyid+8, p); |
goto leave; |
1190 |
rc = WPTERR_GENERAL; |
} |
1191 |
free_if_alloc (p); |
if (get_seckey (keyid, &key)) |
1192 |
goto leave; |
BUG (0); |
1193 |
} |
gpgme_signers_add (ctx, key); |
1194 |
else if (err) { |
} |
1195 |
msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR); |
else { |
1196 |
rc = WPTERR_GENERAL; |
gpgme_key_t sigkey = gpgme_signers_enum (ctx, 0); |
1197 |
goto leave; |
if (sigkey && sigkey->subkeys) { |
1198 |
} |
keyid = m_strdup (sigkey->subkeys->keyid); |
1199 |
if (file_exist_check (c->output)) { |
} |
1200 |
log_box ("Decrypt", MB_ERR, _("Decryption failed.\n%s: does not exist."), c->output); |
} |
1201 |
rc = WPTERR_GENERAL; |
if (!c->init_cb || !c->cache_cb) { |
1202 |
} |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, |
1203 |
|
c->dlg, _("Signing")); |
1204 |
sigres = gpgme_op_verify_result (ctx); |
c->init_cb = 1; |
1205 |
if (sigres && sigres->signatures) |
} |
1206 |
show_verify_result (sigres); |
op_begin (); |
1207 |
|
err = gpgme_op_encrypt_sign (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1208 |
|
in->dat, out->dat); |
1209 |
leave: |
op_end (); |
1210 |
if (in) |
if (!c->cache_cb) |
1211 |
gpg_file_data_release (in); |
release_gpg_passphrase_cb (&c->pass_cb); |
1212 |
if (out) |
if (c->pass_cb.cancel) { |
1213 |
gpg_file_data_release (out); |
rc = WPTERR_GENERAL; |
1214 |
return rc; |
goto leave; |
1215 |
} |
} |
1216 |
|
if (err) { |
1217 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
1218 |
int |
if (gpgme_err_code (err) == GPG_ERR_BAD_PASSPHRASE) |
1219 |
fm_sign (fm_state_t c, const char * name) |
agent_del_cache (keyid); |
1220 |
{ |
rc = WPTERR_GENERAL; |
1221 |
int rc = 0; |
goto leave; |
1222 |
gpgme_ctx_t ctx = c->ctx; |
} |
1223 |
gpgme_error_t err; |
} |
1224 |
file_data_t in=NULL, out=NULL; |
else { |
1225 |
char ext[5]; |
op_begin (); |
1226 |
|
err = gpgme_op_encrypt (ctx, c->recp, GPGME_ENCRYPT_ALWAYS_TRUST, |
1227 |
if (!c->init_cb || !c->cache_cb) { |
in->dat, out->dat); |
1228 |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_SIGN, c->dlg, _("Signing") ); |
op_end (); |
1229 |
c->init_cb = 1; |
if (err) { |
1230 |
} |
msg_box (c->dlg, gpgme_strerror (err), _("Encrypt"), MB_ERR); |
1231 |
|
rc = WPTERR_GENERAL; |
1232 |
free_if_alloc (c->output); |
goto leave; |
1233 |
c->output = new char[strlen (name) + 5 + 1]; |
} |
1234 |
if( !c->output) |
} |
1235 |
BUG( NULL ); |
|
1236 |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
leave: |
1237 |
strcpy (c->output, name); |
if (in) |
1238 |
strcat (c->output, ext); |
gpg_file_data_release (in); |
1239 |
|
if (out) |
1240 |
if (!overwrite_file (c->output)) { |
gpg_file_data_release (out); |
1241 |
rc = ask_filename (c, _("Enter filename for signed file"), NULL); |
free_if_alloc (keyid); |
1242 |
if (rc) |
if (!rc && c->wipe) |
1243 |
goto leave; |
secure_unlink (name, WIPE_MODE_SIMPLE, 0); |
1244 |
} |
return rc; |
1245 |
remove_crit_file_attrs (c->output, 0); |
} |
1246 |
|
|
1247 |
err = gpg_file_data_new (name, 1, &in); |
|
1248 |
if (err) |
int |
1249 |
goto leave; |
fm_sym_encrypt (fm_state_t c, const char * name) |
1250 |
err = gpg_file_data_new (c->output, 0, &out); |
{ |
1251 |
if (err) |
gpgme_ctx_t ctx = c->ctx; |
1252 |
goto leave; |
gpgme_error_t err; |
1253 |
|
file_data_t in=NULL, out=NULL; |
1254 |
err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode); |
int rc = 0, cancel = 0; |
1255 |
if( !c->cache_cb ) |
char ext[5], *pass; |
1256 |
release_gpg_passphrase_cb (&c->pass_cb); |
|
1257 |
if( c->pass_cb.cancel ) { |
pass = request_passphrase2 (_("Symmetric Encryption"), 0, &cancel); |
1258 |
rc = WPTERR_GENERAL; |
if (cancel) { |
1259 |
goto leave; |
c->cancel = 1; |
1260 |
} |
return 0; |
1261 |
if( err ) { |
} |
1262 |
msg_box( c->dlg, gpgme_strerror( err ), _("Sign"), MB_ERR ); |
|
1263 |
rc = WPTERR_GENERAL; |
/* XXX: a convenient feature could be to select the preferred |
1264 |
goto leave; |
symmetric algorithm. */ |
1265 |
} |
c->output = new char[strlen (name) + 5 + 1]; |
1266 |
|
if (!c->output) |
1267 |
leave: |
BUG (0); |
1268 |
if (in) |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1269 |
gpg_file_data_release (in); |
strcpy (c->output, name); |
1270 |
if (out) |
strcat (c->output, ext); |
1271 |
gpg_file_data_release (out); |
|
1272 |
return rc; |
if (overwrite_file (c->output) == 0) { |
1273 |
} |
rc = WPTERR_GENERAL; |
1274 |
|
goto leave; |
1275 |
|
} |
1276 |
static int |
|
1277 |
fm_add_sig_stat (file_sig_ctx_t log) |
gpgme_set_passphrase_cb (ctx, sym_passphrase_cb, pass); |
1278 |
{ |
err = gpg_file_data_new (name, 1, &in); |
1279 |
gpgme_key_t key; |
if (err) |
1280 |
const char *kid; |
goto leave; |
1281 |
int not_found = 0; |
err = gpg_file_data_new (c->output, 0, &out); |
1282 |
|
if (err) |
1283 |
kid = log->sig->fpr; |
goto leave; |
1284 |
if (!kid) |
|
1285 |
BUG (NULL); |
op_begin (); |
1286 |
if (strlen (kid) == 40) |
err = gpgme_op_encrypt (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, |
1287 |
kid += 32; |
in->dat, out->dat); |
1288 |
else if (strlen (kid) == 32) |
op_end (); |
1289 |
kid += 24; |
if (err) { |
1290 |
if (get_pubkey (kid, &key)) |
msg_box (c->dlg, gpgme_strerror (err), _("Symmetric"), MB_ERR); |
1291 |
log->use_uid = 0; |
rc = WPTERR_GENERAL; |
1292 |
else { |
goto leave; |
1293 |
log->user_id = key->uids->uid; |
} |
1294 |
log->use_uid = 1; |
if (file_exist_check (c->output)) { |
1295 |
} |
msg_box (c->dlg, _("Encryption failed."), _("Symmetric"), MB_ERR); |
1296 |
file_verify_add_state (log); |
rc = WPTERR_GENERAL; |
1297 |
return 0; |
} |
1298 |
} |
|
1299 |
|
leave: |
1300 |
|
if (in) |
1301 |
static int |
gpg_file_data_release (in); |
1302 |
verify_pasted (listview_ctrl_t lv, fm_state_t ctx, const char * dat, |
if (out) |
1303 |
int i, HWND dlg) |
gpg_file_data_release (out); |
1304 |
{ |
sfree_if_alloc (pass); |
1305 |
FILE * fp; |
return rc; |
1306 |
char stat[32]; |
} |
1307 |
char file[256], * fname = NULL; |
|
1308 |
int del_end=0; |
|
1309 |
|
/* Check the recipients if we have at least one secret key. */ |
1310 |
listview_get_item_text (lv, i, 0, stat, sizeof (stat)-1); |
bool |
1311 |
listview_get_item_text (lv, i, 1, file, sizeof (file)-1); |
is_seckey_available (gpgme_recipient_t rset) |
1312 |
if (strcmp (stat, "UNKNOWN")) |
{ |
1313 |
return 0; |
gpgme_recipient_t r; |
1314 |
fname = make_filename (NULL, file, "asc"); |
winpt_key_s key; |
1315 |
if (file_exist_check (fname) != 0) { |
|
1316 |
fp = fopen (fname, "wb"); |
for (r=rset; r; r = r->next) { |
1317 |
if (fp == NULL) { |
if (gpgme_err_code (r->status) == GPG_ERR_NO_SECKEY) |
1318 |
log_box (_("File Manager"), MB_ERR, "could not create '%s'", fname); |
continue; |
1319 |
free_if_alloc (fname); |
else { |
1320 |
return WPTERR_GENERAL; |
memset (&key, 0, sizeof (key)); |
1321 |
} |
/* extra check to make sure the key is available right now. */ |
1322 |
fwrite (dat, 1, strlen (dat), fp); |
if (!winpt_get_seckey (r->keyid, &key)) { |
1323 |
fclose (fp); |
winpt_release_pubkey (&key); |
1324 |
del_end = 1; |
return true; |
1325 |
} |
} |
1326 |
fm_verify (ctx, 1, fname); |
winpt_release_pubkey (&key); |
1327 |
if (del_end) |
} |
1328 |
unlink (fname); |
} |
1329 |
free_if_alloc (fname); |
return false; |
1330 |
return 0; |
} |
1331 |
} |
|
1332 |
|
|
1333 |
|
/* If the decrypt result contains the original file name, |
1334 |
int |
we use it instead of the artificial "output - .gpg" string. */ |
1335 |
fm_verify_pasted_detsig (listview_ctrl_t lv, HWND dlg) |
static int |
1336 |
{ |
restore_original_name (const char *output, const char *file_name) |
1337 |
fm_state_t ctx = NULL; |
{ |
1338 |
char * dat=NULL; |
char *dir; |
1339 |
int i, fnd = 0; |
char *orig; |
1340 |
|
int rc = 0; |
1341 |
dat = get_clip_text (NULL); |
|
1342 |
if (!dat || !strstr (dat, "BEGIN PGP SIGNATURE")) { |
dir = strrchr (output, '\\'); |
1343 |
msg_box (dlg, _("Could not find detached signature in the clipboard."), |
if (!dir) |
1344 |
_("File Manager"), MB_ERR); |
orig = strdup (file_name); |
1345 |
free_if_alloc (dat); |
else { |
1346 |
return WPTERR_GENERAL; |
orig = (char*)calloc (1, strlen (file_name)+ 1 + |
1347 |
} |
strlen (output)+1); |
1348 |
/* XXX find a way to filter out bad signatures or just ignore all in |
if (!orig) |
1349 |
this case */ |
BUG (0); |
1350 |
fm_state_new (&ctx); |
memcpy (orig, output, (dir-output)+1); |
1351 |
if ((i=listview_get_curr_pos (lv)) != -1) { |
strcat (orig, file_name); |
1352 |
verify_pasted (lv, ctx, dat, i, dlg); |
} |
1353 |
fnd = 1; |
/* XXX: we need to find out if the string needs to be utf8 decoded. */ |
1354 |
} |
if (overwrite_file (orig)) { |
1355 |
else { |
DeleteFile (orig); |
1356 |
for (i=0; i < listview_count_items (lv, 0); i++) { |
if (!MoveFile (output, orig)) |
1357 |
verify_pasted (lv, ctx, dat, i, dlg); |
rc = -1; |
1358 |
fnd = 1; |
} |
1359 |
} |
safe_free (orig); |
1360 |
} |
return rc; |
1361 |
if (!fnd) |
} |
1362 |
msg_box (dlg, _("No files to check."), _("File Manager"), MB_INFO); |
|
1363 |
free_if_alloc (dat); |
|
1364 |
fm_state_release (ctx); |
/* Decrypt the file @name. */ |
1365 |
return 0; |
int |
1366 |
} |
fm_decrypt (fm_state_t c, const char *name) |
1367 |
|
{ |
1368 |
|
gpgme_error_t err; |
1369 |
/* Extract automatically the output file name from @name. |
gpgme_ctx_t ctx = c->ctx; |
1370 |
If @detached is 1, a detached sig is assumed. */ |
gpgme_decrypt_result_t res; |
1371 |
static int |
gpgme_verify_result_t sigres; |
1372 |
get_output_file (fm_state_t c, const char *name, int detached) |
file_data_t in = NULL, out = NULL; |
1373 |
{ |
int rc = 0; |
1374 |
const char *file = NULL; |
|
1375 |
const char *title; |
if (!c->init_cb || !c->cache_cb) { |
1376 |
char fname[384]; |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, GPG_CMD_DECRYPT, |
1377 |
|
c->dlg, _("Decryption")); |
1378 |
if (detached) |
c->init_cb = 1; |
1379 |
title = _("Select Data File"); |
} |
1380 |
else |
|
1381 |
title = _("Selected Output File"); |
c->output = m_strdup (name); |
1382 |
|
if (is_openpgp_ext (c->output)) |
1383 |
if (strstr (name, ".sig") || strstr (name, ".asc") || strstr (name, ".gpg")) { |
c->output[strlen (c->output)-4] = '\0'; |
1384 |
_snprintf (fname, sizeof (fname) - 1, "%s", name); |
else { |
1385 |
fname[strlen (fname) - 4] = '\0'; |
const char *s; |
1386 |
if (file_exist_check (fname) == 0 && detached) |
s = get_filesave_dlg (c->dlg, _("Choose Filename for Output"), |
1387 |
file = fname; |
NULL, NULL); |
1388 |
else if (!detached) { |
if (s) { |
1389 |
/* If the signature is clear or normal, make sure we do not |
free_if_alloc (c->output); |
1390 |
overwrite the original file if it exists. */ |
c->output = m_strdup (s); |
1391 |
if (file_exist_check (fname) == 0 && !overwrite_file (fname)) { |
} |
1392 |
file = get_filesave_dlg (c->dlg, title, NULL, NULL); |
} |
1393 |
if (!file) |
|
1394 |
return WPTERR_GENERAL; |
if (overwrite_file (c->output) == 0) { |
1395 |
} |
rc = ask_filename (c, _("Enter Filename for Plaintext File"), NULL); |
1396 |
else |
if (rc) |
1397 |
file = fname; |
goto leave; |
1398 |
} |
} |
1399 |
} |
|
1400 |
if (!file) |
/* we fetch all recipients here to make sure they list is complete. */ |
1401 |
file = get_fileopen_dlg (c->dlg, title, NULL, NULL); |
release_gpg_recipients (&c->pass_cb.recipients); |
1402 |
if (file) { |
gpg_get_recipients (name, &c->pass_cb.recipients); |
1403 |
free_if_alloc (c->output); |
|
1404 |
c->output = m_strdup (file); |
err = gpg_file_data_new (name, F_DATA_READ, &in); |
1405 |
if (!c->output) |
if (err) |
1406 |
BUG (NULL); |
goto leave; |
1407 |
} |
remove_crit_file_attrs (c->output, 0); |
1408 |
else { |
err = gpg_file_data_new (c->output, F_DATA_WRITE, &out); |
1409 |
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
if (err) |
1410 |
return WPTERR_GENERAL; |
goto leave; |
1411 |
} |
|
1412 |
if (detached) |
op_begin (); |
1413 |
c->sigmode = GPGME_SIG_MODE_DETACH; |
err = gpgme_op_decrypt_verify (ctx, in->dat, out->dat); |
1414 |
else { |
op_end (); |
1415 |
if (strstr (name, ".asc")) |
if (!c->cache_cb) |
1416 |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
release_gpg_passphrase_cb (&c->pass_cb); |
1417 |
else |
if (c->pass_cb.cancel) { |
1418 |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
rc = WPTERR_GENERAL; |
1419 |
} |
goto leave; |
1420 |
return 0; |
} |
1421 |
} |
|
1422 |
|
res = gpgme_op_decrypt_result (ctx); |
1423 |
|
if (res && res->recipients && !is_seckey_available (res->recipients)) { |
1424 |
/* Verify the signature from the file @name. If @detached 1, |
const char *keyid = get_keyid_from_fpr (res->recipients->keyid); |
1425 |
it is assumed that a detached signature should be checked. */ |
char *p = get_key_userid (keyid); |
1426 |
int |
gpgme_pubkey_algo_t pkalgo = res->recipients->pubkey_algo; |
1427 |
fm_verify (fm_state_t c, int detached, const char *name) |
|
1428 |
{ |
log_box (_("Decryption"), MB_ERR, |
1429 |
gpgme_ctx_t ctx = c->ctx; |
_("Encrypted with %s key, ID 0x%s.%s\n" |
1430 |
gpgme_error_t err; |
"Decryption failed: secret key not available."), |
1431 |
gpgme_signature_t s; |
get_key_pubalgo (pkalgo), keyid, p); |
1432 |
gpgme_verify_result_t res; |
rc = WPTERR_GENERAL; |
1433 |
struct file_sig_ctx_s log; |
free_if_alloc (p); |
1434 |
file_data_t in=NULL, out=NULL; |
goto leave; |
1435 |
int rc = 0; |
} |
1436 |
|
else if (err) { |
1437 |
if (strstr (name, ".sig")) |
msg_box (c->dlg, gpgme_strerror (err), _("Decrypt"), MB_ERR); |
1438 |
detached = 1; |
rc = WPTERR_GENERAL; |
1439 |
|
goto leave; |
1440 |
if (get_output_file (c, name, detached)) |
} |
1441 |
return WPTERR_GENERAL; |
if (file_exist_check (c->output)) { |
1442 |
|
log_box ("Decrypt", MB_ERR, |
1443 |
memset (&log, 0, sizeof (log)); |
_("Decryption failed.\n%s: does not exist."), c->output); |
1444 |
log.file = m_strdup (name); |
rc = WPTERR_GENERAL; |
1445 |
if (!log.file) |
goto leave; |
1446 |
BUG (NULL); |
} |
1447 |
file_verify_create_dlg (); |
else if (res && res->file_name) { |
1448 |
|
char *file; |
1449 |
err = gpg_file_data_new (name, 1, &in); |
int id = IDNO; |
1450 |
if (err) |
|
1451 |
goto leave; |
file = strrchr (c->output, '\\'); |
1452 |
err = gpg_file_data_new (c->output, detached? 1 : 0, &out); |
if (!file) |
1453 |
if (err) |
file = c->output; |
1454 |
goto leave; |
else |
1455 |
|
file++; |
1456 |
if (c->sigmode == GPGME_SIG_MODE_DETACH) |
if (strcmp (res->file_name, file)) |
1457 |
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
id = log_box (_("Decrypt"), MB_QUEST_ASK, |
1458 |
else |
_("The original file name is '%s'.\n\n" |
1459 |
err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat); |
"Do you want to use this instead of '%s'?"), |
1460 |
if (err) { |
res->file_name, file); |
1461 |
msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
if (id == IDYES) { |
1462 |
rc = WPTERR_GENERAL; |
/* before we can move the file, it needs to be closed first. */ |
1463 |
goto leave; |
gpg_file_data_release (out); |
1464 |
} |
out = NULL; |
1465 |
|
restore_original_name (c->output, res->file_name); |
1466 |
res = gpgme_op_verify_result (ctx); |
} |
1467 |
for (s=res->signatures; s; s=s->next) { |
} |
1468 |
log.sig = s; |
sigres = gpgme_op_verify_result (ctx); |
1469 |
fm_add_sig_stat (&log); |
if (sigres && sigres->signatures) |
1470 |
} |
verify_show_signature_state (sigres->signatures); |
1471 |
if (!c->output) |
|
1472 |
c->output = m_strdup (name); /* for later use */ |
leave: |
1473 |
|
if (in) |
1474 |
leave: |
gpg_file_data_release (in); |
1475 |
if (in) |
if (out) |
1476 |
gpg_file_data_release (in); |
gpg_file_data_release (out); |
1477 |
if (out) |
|
1478 |
gpg_file_data_release (out); |
return rc; |
1479 |
if (log.file) |
} |
1480 |
delete []log.file; |
|
1481 |
return rc; |
|
1482 |
} |
int |
1483 |
|
fm_sign (fm_state_t c, const char * name) |
1484 |
|
{ |
1485 |
int |
gpgme_ctx_t ctx = c->ctx; |
1486 |
fm_import (fm_state_t c, const char *name) |
gpgme_error_t err; |
1487 |
{ |
file_data_t in=NULL, out=NULL; |
1488 |
gpgme_ctx_t ctx = c->ctx; |
char ext[5]; |
1489 |
gpgme_error_t err; |
int rc = 0; |
1490 |
gpgme_import_result_t res; |
|
1491 |
file_data_t keydata = NULL; |
if (!c->init_cb || !c->cache_cb) { |
1492 |
int rc = 0; |
set_gpg_passphrase_cb (&c->pass_cb, c->ctx, |
1493 |
|
GPG_CMD_SIGN, c->dlg, _("Signing")); |
1494 |
free_if_alloc (c->output); |
c->init_cb = 1; |
1495 |
c->output = m_strdup (name); |
} |
1496 |
if (!c->output) |
|
1497 |
BUG (NULL); |
free_if_alloc (c->output); |
1498 |
|
c->output = new char[strlen (name) + 5 + 1]; |
1499 |
err = gpg_file_data_new (name, 1, &keydata); |
if (!c->output) |
1500 |
if (err) |
BUG (NULL); |
1501 |
goto leave; |
strcpy (ext, file_get_extension (ctx, c->sigmode)); |
1502 |
|
strcpy (c->output, name); |
1503 |
err = gpgme_op_import (ctx, keydata->dat); |
strcat (c->output, ext); |
1504 |
if (err) { |
|
1505 |
msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
if (!overwrite_file (c->output)) { |
1506 |
rc = WPTERR_GENERAL; |
rc = ask_filename (c, _("Enter Filename for Signed File"), NULL); |
1507 |
goto leave; |
if (rc) |
1508 |
} |
goto leave; |
1509 |
|
} |
1510 |
res = gpgme_op_import_result (ctx); |
|
1511 |
print_import_status (res); |
err = gpg_file_data_new (name, F_DATA_READ, &in); |
1512 |
if (res->no_user_id > 0) { |
if (err) |
1513 |
msg_box (c->dlg, _("Key without a self signature was dectected!\n" |
goto leave; |
1514 |
"(This key is NOT usable for encryption, etc)\n" |
remove_crit_file_attrs (c->output, 0); |
1515 |
"\n" |
err = gpg_file_data_new (c->output, F_DATA_WRITE, &out); |
1516 |
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
if (err) |
1517 |
} |
goto leave; |
1518 |
|
|
1519 |
leave: |
op_begin (); |
1520 |
if (keydata) |
err = gpgme_op_sign (ctx, in->dat, out->dat, c->sigmode); |
1521 |
gpg_file_data_release (keydata); |
op_end (); |
1522 |
return rc; |
if (!c->cache_cb) |
1523 |
} /* fm_import */ |
release_gpg_passphrase_cb (&c->pass_cb); |
1524 |
|
if (c->pass_cb.cancel) { |
1525 |
|
rc = WPTERR_GENERAL; |
1526 |
/* Export the selected keys from the File Manager to a file. */ |
goto leave; |
1527 |
int |
} |
1528 |
fm_export (fm_state_t c) |
if (err) { |
1529 |
{ |
msg_box (c->dlg, gpgme_strerror (err), _("Sign"), MB_ERR); |
1530 |
int rc = 0, id = 0; |
rc = WPTERR_GENERAL; |
1531 |
gpgme_ctx_t ctx = c->ctx; |
goto leave; |
1532 |
gpgme_error_t err; |
} |
1533 |
gpgme_key_t *rset = c->recp; |
|
1534 |
file_data_t keydata = NULL; |
leave: |
1535 |
const char *name, *s = NULL; |
if (in) |
1536 |
char *p = NULL, *patt = NULL; |
gpg_file_data_release (in); |
1537 |
|
if (out) |
1538 |
if (!rset || !rset[0]) { |
gpg_file_data_release (out); |
1539 |
msg_box (c->dlg, _("No key was selected for export."), _("Export"), MB_ERR); |
return rc; |
1540 |
rc = WPTERR_GENERAL; |
} |
1541 |
goto leave; |
|
1542 |
} |
|
1543 |
|
static void |
1544 |
if (rset[1] == NULL) { /* count == 1*/ |
fm_add_sig_stat (file_sig_ctx_t log) |
1545 |
gpgme_key_t k = rset[0]; |
{ |
1546 |
const char *s = k->uids->name; |
struct winpt_key_s key; |
1547 |
p = new char[strlen (s)+1+8]; |
const char *kid; |
1548 |
if (!p) |
|
1549 |
BUG (NULL); |
memset (&key, 0, sizeof (key)); |
1550 |
strcpy (p, s ); |
kid = get_keyid_from_fpr (log->sig->fpr); |
1551 |
strcat (p, ".asc"); |
log->use_uid = 0; |
1552 |
} |
if (!winpt_get_pubkey (kid, &key)) { |
1553 |
|
log->user_id = key.ext->uids->uid; |
1554 |
name = get_filename_dlg (c->dlg, FILE_SAVE, _("Choose Name for Key File"), |
log->use_uid = 1; |
1555 |
NULL, p? p : NULL); |
} |
1556 |
|
file_verify_add_state (log); |
1557 |
if (!name) |
winpt_release_pubkey (&key); |
1558 |
name = "keys.gpg"; |
} |
1559 |
|
|
1560 |
patt = gpg_keylist_to_pattern (rset, c->n_recp); |
|
1561 |
|
/* Verify a detached signature from the clipboard. */ |
1562 |
err = gpg_file_data_new (name, 0, &keydata); |
static int |
1563 |
if (err) |
verify_pasted (listview_ctrl_t lv, fm_state_t ctx, |
1564 |
goto leave; |
const char *dat, int pos, HWND dlg) |
1565 |
|
{ |
1566 |
err = gpgme_op_export (ctx, patt, 0, keydata->dat); |
FILE *fp; |
1567 |
if (err) { |
char stat[32]; |
1568 |
msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR); |
char file[256], *fname = NULL; |
1569 |
rc = WPTERR_GENERAL; |
int del_end = 0; |
1570 |
goto leave; |
|
1571 |
} |
listview_get_item_text (lv, pos, FM_COL_STAT, stat, DIM (stat)-1); |
1572 |
log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name); |
listview_get_item_text (lv, pos, FM_COL_NAME, file, DIM (file)-1); |
1573 |
|
if (strcmp (stat, "UNKNOWN")) |
1574 |
leave: |
return 0; |
1575 |
if (keydata) |
fname = make_filename (NULL, file, "asc"); |
1576 |
gpg_file_data_release (keydata); |
if (file_exist_check (fname) != 0) { |
1577 |
if (patt) |
fp = fopen (fname, "wb"); |
1578 |
free (patt); |
if (fp == NULL) { |
1579 |
free_if_alloc (p); |
log_box (_("File Manager"), MB_ERR, "Could not create '%s'", fname); |
1580 |
|
free_if_alloc (fname); |
1581 |
return rc; |
return WPTERR_GENERAL; |
1582 |
} |
} |
1583 |
|
fwrite (dat, 1, strlen (dat), fp); |
1584 |
|
fclose (fp); |
1585 |
int |
del_end = 1; |
1586 |
fm_parse_command_line (char *cmdl) |
} |
1587 |
{ |
fm_verify (ctx, 1, fname); |
1588 |
fm_state_t ctx; |
if (del_end) |
1589 |
const char *s; |
DeleteFile (fname); |
1590 |
char *p, *fn = NULL; |
free_if_alloc (fname); |
1591 |
int count = 0, detached = 0; |
return 0; |
1592 |
|
} |
1593 |
if( !cmdl || !*cmdl ) |
|
1594 |
return 0; |
|
1595 |
|
/* Figure out if the clipboard contains a detached signature. */ |
1596 |
fm_state_new( &ctx ); |
int |
1597 |
ctx->dlg = GetActiveWindow( ); |
fm_verify_pasted_detsig (listview_ctrl_t lv, HWND dlg) |
1598 |
ctx->cache_cb = 1; |
{ |
1599 |
|
fm_state_t ctx = NULL; |
1600 |
p = cmdl; |
char * dat=NULL; |
1601 |
if( p && *p > 32 && !stristr( p, "winpt.exe" ) |
int i, fnd = 0; |
1602 |
&& !strstr( p, "--" ) ) { |
|
1603 |
count++; |
dat = get_clip_text (NULL); |
1604 |
if (*p == '"') { /* need to remove quotes */ |
if (!dat || !strstr (dat, "BEGIN PGP SIGNATURE")) { |
1605 |
fn = new char[strlen( p )]; |
msg_box (dlg, _("Could not find detached signature in the clipboard."), |
1606 |
if (!fn) |
_("File Manager"), MB_ERR); |
1607 |
BUG( NULL ); |
free_if_alloc (dat); |
1608 |
memcpy( fn, p+1, strlen( p ) - 2 ); |
return WPTERR_GENERAL; |
1609 |
fn[strlen( p ) -2] = '\0'; |
} |
1610 |
} |
/* XXX find a way to filter out bad signatures or just ignore all in |
1611 |
else |
this case */ |
1612 |
fn = m_strdup (p); |
fm_state_new (&ctx); |
1613 |
s = fm_get_file_type (fn); |
i = listview_get_curr_pos (lv); |
1614 |
if (!s || !strcmp (s, "UNKNOWN")) |
if (i == -1) { |
1615 |
s = gnupg_check_file_ext (fn); |
verify_pasted (lv, ctx, dat, i, dlg); |
1616 |
if (*s == 'U') { |
fnd = 1; |
1617 |
log_box( _("File Manager"), MB_ERR, _("%s: no valid OpenPGP data found."), p ); |
} |
1618 |
return count; |
else { |
1619 |
} |
for (i=0; i < listview_count_items (lv, 0); i++) { |
1620 |
|
verify_pasted (lv, ctx, dat, i, dlg); |
1621 |
switch( *s ) { |
fnd = 1; |
1622 |
case 'E': fm_decrypt (ctx, fn); break; |
} |
1623 |
case 'P': fm_import (ctx, fn); break; |
} |
1624 |
case 'S': |
if (!fnd) |
1625 |
if (s[1] == 'Y') { |
msg_box (dlg, _("No files to check."), _("File Manager"), MB_INFO); |
1626 |
fm_decrypt (ctx, fn); |
free_if_alloc (dat); |
1627 |
break; |
fm_state_release (ctx); |
1628 |
} |
return 0; |
1629 |
file_verify_use_event (); |
} |
1630 |
if (s[1] == 'I') { |
|
1631 |
if (strlen (s) == 13 && s[7] == 'D') |
|
1632 |
detached = 1; |
/* Extract automatically the output file name from @name. |
1633 |
fm_verify( ctx, detached, fn ); |
If @detached is 1, a detached sig is assumed. */ |
1634 |
} |
static int |
1635 |
file_verify_wait( ); |
get_output_file (fm_state_t c, const char *name, int detached) |
1636 |
break; |
{ |
1637 |
} |
const char *file = NULL; |
1638 |
} |
const char *title; |
1639 |
|
char fname[384]; |
1640 |
memset( &ctx->pass_cb, 0, sizeof (ctx->pass_cb) ); |
|
1641 |
safe_free( fn ); |
if (detached) |
1642 |
fm_state_release( ctx ); |
title = _("Select Data File"); |
1643 |
return count; |
else |
1644 |
} /* fm_parse_command_line */ |
title = _("Selected Output File"); |
1645 |
|
|
1646 |
|
if (is_openpgp_ext (name)) { |
1647 |
const char * |
_snprintf (fname, DIM (fname) - 1, "%s", name); |
1648 |
default_dirname( const char * name ) |
fname[strlen (fname) - 4] = '\0'; |
1649 |
{ |
if (file_exist_check (fname) == 0 && detached) |
1650 |
char * p = strrchr( name, '\\' ); |
file = fname; |
1651 |
if( !p ) |
else if (!detached) { |
1652 |
return NULL; |
/* If the signature is clear or normal, make sure we do not |
1653 |
return p+1; |
overwrite the original file if it exists. */ |
1654 |
} /* default_dirname */ |
if (file_exist_check (fname) == 0 && !overwrite_file (fname)) { |
1655 |
|
file = get_filesave_dlg (c->dlg, title, NULL, NULL); |
1656 |
|
if (!file) |
1657 |
int |
return WPTERR_GENERAL; |
1658 |
fm_encrypt_directory( fm_state_t c, const char * name ) |
} |
1659 |
{ |
else |
1660 |
PK_FILE_LIST list = NULL; |
file = fname; |
1661 |
WIN32_FIND_DATA findbuf; |
} |
1662 |
HANDLE hd; |
} |
1663 |
const char * s; |
if (!file) |
1664 |
char * patt = NULL, * p; |
file = get_fileopen_dlg (c->dlg, title, NULL, NULL); |
1665 |
int rc = 0; |
if (file) { |
1666 |
|
free_if_alloc (c->output); |
1667 |
if( !is_directory( name ) ) |
c->output = m_strdup (file); |
1668 |
return -1; |
} |
1669 |
patt = new char[strlen( name ) + 4]; |
else { |
1670 |
if( !patt ) |
msg_box (c->dlg, _("Invalid file name. Exit"), _("Verify"), MB_ERR); |
1671 |
BUG( NULL ); |
return WPTERR_GENERAL; |
1672 |
strcpy( patt, name ); |
} |
1673 |
strcat( patt, "\\*" ); |
if (detached) |
1674 |
hd = FindFirstFile( patt, &findbuf ); |
c->sigmode = GPGME_SIG_MODE_DETACH; |
1675 |
if( !hd ) { |
else { |
1676 |
free_if_alloc( patt ); |
if (stristr (name, ".asc")) |
1677 |
return -1; |
c->sigmode = GPGME_SIG_MODE_CLEAR; |
1678 |
} |
else |
1679 |
if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) { |
c->sigmode = GPGME_SIG_MODE_NORMAL; |
1680 |
p = make_filename( name, findbuf.cFileName, NULL ); |
} |
1681 |
pk_list_add( &list, p ); |
return 0; |
1682 |
free_if_alloc( p ); |
} |
1683 |
} |
|
1684 |
while( FindNextFile( hd, &findbuf ) ) { |
|
1685 |
if( strcmp( findbuf.cFileName, "." ) && strcmp( findbuf.cFileName, ".." ) ) { |
/* Handy function to kick of the gpg verify process. */ |
1686 |
p = make_filename( name, findbuf.cFileName, NULL ); |
static gpgme_error_t |
1687 |
pk_list_add( &list, p ); |
fm_gpg_verify (gpgme_sig_mode_t sigmode, gpgme_ctx_t ctx, |
1688 |
free_if_alloc( p ); |
file_data_t in, file_data_t out) |
1689 |
} |
{ |
1690 |
} |
gpgme_error_t err; |
1691 |
s = get_filename_dlg( c->dlg, FILE_SAVE, _("Choose a Name for the Archive"), |
|
1692 |
NULL, default_dirname( name ) ); |
op_begin (); |
1693 |
if( !s ) { |
if (sigmode == GPGME_SIG_MODE_DETACH) |
1694 |
msg_box( c->dlg, _("Invalid archive name. Exit."), _("Encrypt Directory"), MB_ERR ); |
err = gpgme_op_verify (ctx, in->dat, out->dat, NULL); |
1695 |
rc = -1; |
else |
1696 |
goto leave; |
err = gpgme_op_verify (ctx, in->dat, in->dat, out->dat); |
1697 |
} |
op_end (); |
1698 |
|
return err; |
1699 |
rc = pk_archiv_create( list, s ); |
} |
1700 |
if( rc ) |
|
1701 |
msg_box( c->dlg, _("Could not create zip archive."), _("Encrypt Directory"), MB_ERR ); |
|
1702 |
else { |
/* Verify the signature from the file @name. If @detached 1, |
1703 |
fm_encrypt( c, s, 0 ); |
it is assumed that a detached signature should be checked. */ |
1704 |
unlink( s ); |
int |
1705 |
} |
fm_verify (fm_state_t c, int detached, const char *name) |
1706 |
leave: |
{ |
1707 |
pk_list_free( list ); |
gpgme_error_t err; |
1708 |
free_if_alloc( patt ); |
gpgme_signature_t s; |
1709 |
return rc; |
gpgme_verify_result_t res; |
1710 |
} /* fm_encrypt_directory */ |
struct file_sig_ctx_s log; |
1711 |
|
file_data_t in=NULL, out=NULL; |
1712 |
|
int rc = 0; |
1713 |
static int CALLBACK |
|
1714 |
fm_cmp_cb( LPARAM first, LPARAM second, LPARAM sortby ) |
if (stristr (name, ".sig")) |
1715 |
{ |
detached = 1; |
1716 |
const char * a = 0, * b = 0; |
|
1717 |
|
if (get_output_file (c, name, detached)) |
1718 |
switch( (int)sortby ) { |
return WPTERR_GENERAL; |
1719 |
case FM_SORT_STAT: |
|
1720 |
break; |
file_verify_create_dlg (); |
1721 |
case FM_SORT_NAME: |
|
1722 |
break; |
memset (&log, 0, sizeof (log)); |
1723 |
case FM_SORT_OP: |
log.file = m_strdup (name); |
1724 |
break; |
|
1725 |
} |
err = gpg_file_data_new (name, F_DATA_READ, &in); |
1726 |
return stricmp( a, b ); |
if (err) |
1727 |
} /* fm_cmp_cb */ |
goto leave; |
1728 |
|
err = gpg_file_data_new (c->output, |
1729 |
|
detached? F_DATA_READ : F_DATA_WRITE, &out); |
1730 |
int |
if (err) |
1731 |
fm_sort( listview_ctrl_t lv, int sortby ) |
goto leave; |
1732 |
{ |
|
1733 |
return listview_sort_items( lv, sortby, fm_cmp_cb ); |
err = fm_gpg_verify (c->sigmode, c->ctx, in, out); |
1734 |
} /* fm_sort */ |
if (err) { |
1735 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
1736 |
|
rc = WPTERR_GENERAL; |
1737 |
void |
goto leave; |
1738 |
fm_print_md( listview_ctrl_t lv, HWND dlg, int mdalgo ) |
} |
1739 |
{ |
|
1740 |
struct md_file_s mdctx; |
res = gpgme_op_verify_result (c->ctx); |
1741 |
|
if (!res) |
1742 |
if( listview_count_items( lv, 0 ) == 0 ) |
goto leave; |
1743 |
return; |
if (res->signatures != NULL && |
1744 |
memset( &mdctx, 0, sizeof (mdctx) ); |
gpgme_err_code (res->signatures->status) == GPG_ERR_NO_PUBKEY) { |
1745 |
mdctx.lv = lv; |
/* We need to call gpg again to get the actual sig state |
1746 |
mdctx.mdalgo = mdalgo; |
and the data files must be rewinded also. */ |
1747 |
DialogBoxParam( glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg, |
if (fetch_key_from_keyserver (c->dlg, res->signatures)) { |
1748 |
mdsum_dlg_proc, (LPARAM)&mdctx ); |
rc = WPTERR_GENERAL; |
1749 |
} /* fm_print_md */ |
goto leave; |
1750 |
|
} |
1751 |
|
gpg_file_data_rewind (in); |
1752 |
int |
gpg_file_data_rewind (out); |
1753 |
fm_send_file (listview_ctrl_t lv) |
err = fm_gpg_verify (c->sigmode, c->ctx, in, out); |
1754 |
{ |
if (err) { |
1755 |
char buf[128]; |
msg_box (c->dlg, gpgme_strerror (err), _("Verify"), MB_ERR); |
1756 |
int rc; |
rc = WPTERR_GENERAL; |
1757 |
|
goto leave; |
1758 |
rc = listview_get_item_text (lv, -1, 1, buf, sizeof (buf)-1); |
} |
1759 |
if (rc == -1) |
/* Refresh the pointer because a new GPG instance has been |
1760 |
return 0; |
created for the new verify operation. */ |
1761 |
/*mapi_send_ascfile (buf); XXX */ |
res = gpgme_op_verify_result (c->ctx); |
1762 |
return 0; |
} |
1763 |
} |
|
1764 |
|
for (s=res->signatures; s; s=s->next) { |
1765 |
|
log.sig = s; |
1766 |
|
fm_add_sig_stat (&log); |
1767 |
|
} |
1768 |
|
if (!c->output) |
1769 |
|
c->output = m_strdup (name); /* for later use */ |
1770 |
|
|
1771 |
|
leave: |
1772 |
|
if (in) |
1773 |
|
gpg_file_data_release (in); |
1774 |
|
if (out) |
1775 |
|
gpg_file_data_release (out); |
1776 |
|
free_if_alloc (log.file); |
1777 |
|
return rc; |
1778 |
|
} |
1779 |
|
|
1780 |
|
|
1781 |
|
/* Import the keys from the file @name. |
1782 |
|
Return value: 0 on success. */ |
1783 |
|
int |
1784 |
|
fm_import (fm_state_t c, const char *name) |
1785 |
|
{ |
1786 |
|
gpgme_ctx_t ctx = c->ctx; |
1787 |
|
gpgme_error_t err; |
1788 |
|
gpgme_import_result_t res; |
1789 |
|
file_data_t keydata = NULL; |
1790 |
|
int rc = 0; |
1791 |
|
|
1792 |
|
free_if_alloc (c->output); |
1793 |
|
c->output = m_strdup (name); |
1794 |
|
|
1795 |
|
err = gpg_file_data_new (name, F_DATA_READ, &keydata); |
1796 |
|
if (err) |
1797 |
|
goto leave; |
1798 |
|
|
1799 |
|
op_begin (); |
1800 |
|
err = gpgme_op_import (ctx, keydata->dat); |
1801 |
|
op_end (); |
1802 |
|
if (err) { |
1803 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Import"), MB_ERR); |
1804 |
|
rc = WPTERR_GENERAL; |
1805 |
|
goto leave; |
1806 |
|
} |
1807 |
|
|
1808 |
|
res = gpgme_op_import_result (ctx); |
1809 |
|
print_import_status (res); |
1810 |
|
if (res->no_user_id > 0) { |
1811 |
|
msg_box (c->dlg, _("Key without a self signature was dectected!\n" |
1812 |
|
"(This key is NOT usable for encryption, etc)\n" |
1813 |
|
"\n" |
1814 |
|
"Cannot import these key(s)!"), _("Import"), MB_INFO); |
1815 |
|
} |
1816 |
|
|
1817 |
|
leave: |
1818 |
|
if (keydata) |
1819 |
|
gpg_file_data_release (keydata); |
1820 |
|
return rc; |
1821 |
|
} |
1822 |
|
|
1823 |
|
|
1824 |
|
/* Export the selected keys from the File Manager to a file. */ |
1825 |
|
int |
1826 |
|
fm_export (fm_state_t c) |
1827 |
|
{ |
1828 |
|
gpgme_ctx_t ctx = c->ctx; |
1829 |
|
gpgme_error_t err; |
1830 |
|
gpgme_key_t *rset = c->recp; |
1831 |
|
file_data_t keydata = NULL; |
1832 |
|
const char *name; |
1833 |
|
char *p = NULL, *patt = NULL; |
1834 |
|
int rc = 0; |
1835 |
|
|
1836 |
|
if (!rset || !rset[0]) { |
1837 |
|
msg_box (c->dlg, _("No key was selected for export."), |
1838 |
|
_("Export"), MB_ERR); |
1839 |
|
rc = WPTERR_GENERAL; |
1840 |
|
goto leave; |
1841 |
|
} |
1842 |
|
|
1843 |
|
if (rset[1] == NULL) /* count == 1*/ |
1844 |
|
p = km_gen_export_filename (rset[0]->subkeys->keyid+8, 0); |
1845 |
|
|
1846 |
|
name = get_filesave_dlg (c->dlg, _("Choose Name for Key File"), |
1847 |
|
NULL, p? p : NULL); |
1848 |
|
if (!name) |
1849 |
|
name = reg_prefs.default_ext? "keys.pgp" : "keys.gpg"; |
1850 |
|
|
1851 |
|
patt = gpg_keylist_to_pattern (rset, c->n_recp); |
1852 |
|
|
1853 |
|
err = gpg_file_data_new (name, F_DATA_WRITE, &keydata); |
1854 |
|
if (err) |
1855 |
|
goto leave; |
1856 |
|
|
1857 |
|
op_begin (); |
1858 |
|
err = gpgme_op_export (ctx, patt, 0, keydata->dat); |
1859 |
|
op_end (); |
1860 |
|
if (err) { |
1861 |
|
msg_box (c->dlg, gpgme_strerror (err), _("Export"), MB_ERR); |
1862 |
|
rc = WPTERR_GENERAL; |
1863 |
|
goto leave; |
1864 |
|
} |
1865 |
|
log_box (_("GnuPG status"), MB_OK, _("Finished (Output: %s)"), name); |
1866 |
|
|
1867 |
|
leave: |
1868 |
|
if (keydata) |
1869 |
|
gpg_file_data_release (keydata); |
1870 |
|
safe_free (patt); |
1871 |
|
free_if_alloc (p); |
1872 |
|
return rc; |
1873 |
|
} |
1874 |
|
|
1875 |
|
|
1876 |
|
/* Parse the command line and process the given file. */ |
1877 |
|
int |
1878 |
|
fm_parse_command_line (char *cmdl) |
1879 |
|
{ |
1880 |
|
fm_state_t ctx; |
1881 |
|
const char *s; |
1882 |
|
char *p, *fn = NULL; |
1883 |
|
int count = 0, detached = 0; |
1884 |
|
int type; |
1885 |
|
|
1886 |
|
if (!cmdl || !*cmdl) |
1887 |
|
return 0; |
1888 |
|
|
1889 |
|
fm_state_new (&ctx); |
1890 |
|
ctx->dlg = GetActiveWindow (); |
1891 |
|
ctx->cache_cb = 1; |
1892 |
|
|
1893 |
|
p = cmdl; |
1894 |
|
if (p && *p > 32 && !stristr (p, "winpt.exe") |
1895 |
|
&& !strstr (p, "--" )) { |
1896 |
|
count++; |
1897 |
|
if (*p == '"') { /* need to remove quotes */ |
1898 |
|
fn = new char[strlen (p)]; |
1899 |
|
if (!fn) |
1900 |
|
BUG (NULL); |
1901 |
|
memcpy (fn, p+1, strlen (p) - 2); |
1902 |
|
fn[strlen (p) -2] = '\0'; |
1903 |
|
} |
1904 |
|
else |
1905 |
|
fn = m_strdup (p); |
1906 |
|
s = fm_get_file_type (fn, &type); |
1907 |
|
if (!s || !strcmp (s, "UNKNOWN")) |
1908 |
|
s = gnupg_check_file_ext (fn, &type); |
1909 |
|
if (type == PGP_NONE) { |
1910 |
|
log_box (_("File Manager"), MB_ERR, |
1911 |
|
_("%s: no valid OpenPGP data found."), p); |
1912 |
|
free_if_alloc (fn); |
1913 |
|
return count; |
1914 |
|
} |
1915 |
|
switch (type) { |
1916 |
|
case PGP_MESSAGE: |
1917 |
|
fm_decrypt (ctx, fn); |
1918 |
|
break; |
1919 |
|
|
1920 |
|
case PGP_PUBKEY: |
1921 |
|
case PGP_SECKEY: |
1922 |
|
fm_import (ctx, fn); |
1923 |
|
break; |
1924 |
|
|
1925 |
|
case PGP_SIG: |
1926 |
|
case PGP_CLEARSIG: |
1927 |
|
if (type == PGP_SIG) |
1928 |
|
detached = 1; |
1929 |
|
fm_verify (ctx, detached, fn); |
1930 |
|
file_verify_wait (); |
1931 |
|
break; |
1932 |
|
|
1933 |
|
default: |
1934 |
|
break; |
1935 |
|
} |
1936 |
|
} |
1937 |
|
|
1938 |
|
wipememory (&ctx->pass_cb, sizeof (ctx->pass_cb)); |
1939 |
|
free_if_alloc (fn); |
1940 |
|
fm_state_release (ctx); |
1941 |
|
return count; |
1942 |
|
} |
1943 |
|
|
1944 |
|
|
1945 |
|
/* Extract the last folder name from @name. */ |
1946 |
|
const char* |
1947 |
|
default_dirname (const char *name) |
1948 |
|
{ |
1949 |
|
char *p = strrchr (name, '\\'); |
1950 |
|
if (!p) |
1951 |
|
return NULL; |
1952 |
|
return p+1; |
1953 |
|
} |
1954 |
|
|
1955 |
|
|
1956 |
|
/* Store all selected files from @lv in a zip archive |
1957 |
|
and encrypt the zip archive then. |
1958 |
|
Return value: 0 on success. */ |
1959 |
|
int |
1960 |
|
fm_encrypt_into_zip (fm_state_t ctx, listview_ctrl_t lv) |
1961 |
|
{ |
1962 |
|
PK_FILE_LIST list=NULL; |
1963 |
|
const char *outfile, *ext; |
1964 |
|
char *out_enc; |
1965 |
|
int nitems; |
1966 |
|
int i, idx = -1; |
1967 |
|
int rc; |
1968 |
|
|
1969 |
|
nitems = listview_count_items (lv, 0); |
1970 |
|
if (!nitems) { |
1971 |
|
msg_box (NULL, _("Encrypting into a ZIP archive makes sense with multiple files"), |
1972 |
|
_("File Manager"), MB_ERR); |
1973 |
|
return WPTERR_GENERAL; |
1974 |
|
} |
1975 |
|
|
1976 |
|
outfile = get_filesave_dlg (ctx->dlg, _("Choose File Name for Output"), |
1977 |
|
NULL, "Encrypted_Files.zip"); |
1978 |
|
if (!outfile) |
1979 |
|
return WPTERR_GENERAL; |
1980 |
|
|
1981 |
|
for (i=0; i < nitems; i++) { |
1982 |
|
char name[MAX_PATH+32]; |
1983 |
|
|
1984 |
|
if (!listview_get_item_state (lv, i)) |
1985 |
|
continue; |
1986 |
|
if (idx == -1) |
1987 |
|
idx = i; |
1988 |
|
listview_get_item_text (lv, i, 1, name, DIM (name)-1); |
1989 |
|
pk_list_add (&list, name); |
1990 |
|
} |
1991 |
|
|
1992 |
|
pk_archiv_create (list, outfile); |
1993 |
|
pk_list_free (list); |
1994 |
|
|
1995 |
|
rc = fm_encrypt (ctx, outfile, 0); |
1996 |
|
DeleteFile (outfile); |
1997 |
|
if (rc) |
1998 |
|
return rc; |
1999 |
|
|
2000 |
|
ext = file_get_extension (ctx->ctx, ctx->sigmode)+1; |
2001 |
|
out_enc = make_filename (NULL, outfile, ext); |
2002 |
|
fm_set_status (lv, idx, FM_ENCRYPT, (gpgme_sig_mode_t)0, 1, out_enc); |
2003 |
|
free_if_alloc (out_enc); |
2004 |
|
|
2005 |
|
for (i=nitems; i > -1; i--) { |
2006 |
|
if (i != idx && listview_get_item_state (lv, i)) |
2007 |
|
listview_del_item (lv, i); |
2008 |
|
} |
2009 |
|
return 0; |
2010 |
|
} |
2011 |
|
|
2012 |
|
|
2013 |
|
int |
2014 |
|
fm_encrypt_directory (fm_state_t c, const char *name) |
2015 |
|
{ |
2016 |
|
WIN32_FIND_DATA findbuf; |
2017 |
|
HANDLE hd; |
2018 |
|
PK_FILE_LIST list = NULL; |
2019 |
|
const char *s; |
2020 |
|
char *patt = NULL, *p; |
2021 |
|
int rc = 0; |
2022 |
|
|
2023 |
|
if (!is_directory (name)) |
2024 |
|
return -1; |
2025 |
|
patt = new char[strlen (name) + 4]; |
2026 |
|
if (!patt) |
2027 |
|
BUG (NULL); |
2028 |
|
strcpy (patt, name); |
2029 |
|
strcat (patt, "\\*"); |
2030 |
|
hd = FindFirstFile (patt, &findbuf); |
2031 |
|
if (!hd) { |
2032 |
|
free_if_alloc (patt); |
2033 |
|
return WPTERR_GENERAL; |
2034 |
|
} |
2035 |
|
if (strcmp (findbuf.cFileName, ".") && strcmp (findbuf.cFileName, "..")) { |
2036 |
|
p = make_filename (name, findbuf.cFileName, NULL); |
2037 |
|
pk_list_add (&list, p); |
2038 |
|
free_if_alloc (p); |
2039 |
|
} |
2040 |
|
/* XXX: support to scan sub directories also. */ |
2041 |
|
while (FindNextFile (hd, &findbuf)) { |
2042 |
|
if (strcmp (findbuf.cFileName, ".") && strcmp (findbuf.cFileName, "..")) { |
2043 |
|
p = make_filename (name, findbuf.cFileName, NULL); |
2044 |
|
pk_list_add (&list, p); |
2045 |
|
free_if_alloc (p); |
2046 |
|
} |
2047 |
|
} |
2048 |
|
s = get_filesave_dlg (c->dlg, _("Choose a Name for the Archive"), |
2049 |
|
NULL, default_dirname (name)); |
2050 |
|
if (!s) { |
2051 |
|
msg_box (c->dlg, _("Invalid archive name. Exit."), |
2052 |
|
_("Encrypt Directory"), MB_ERR); |
2053 |
|
rc = -1; |
2054 |
|
goto leave; |
2055 |
|
} |
2056 |
|
|
2057 |
|
rc = pk_archiv_create (list, s); |
2058 |
|
if (rc) |
2059 |
|
msg_box (c->dlg, _("Could not create zip archive."), |
2060 |
|
_("Encrypt Directory"), MB_ERR); |
2061 |
|
else { |
2062 |
|
fm_encrypt (c, s, 0); |
2063 |
|
DeleteFile (s); |
2064 |
|
} |
2065 |
|
leave: |
2066 |
|
FindClose (hd); |
2067 |
|
pk_list_free (list); |
2068 |
|
free_if_alloc (patt); |
2069 |
|
return rc; |
2070 |
|
} |
2071 |
|
|
2072 |
|
|
2073 |
|
static int CALLBACK |
2074 |
|
fm_cmp_cb (LPARAM first, LPARAM second, LPARAM sortby) |
2075 |
|
{ |
2076 |
|
fm_model_t a, b; |
2077 |
|
int cmpres = 0; |
2078 |
|
|
2079 |
|
a = (fm_model_t)first; |
2080 |
|
b = (fm_model_t)second; |
2081 |
|
if (!a || !b) |
2082 |
|
return 0; |
2083 |
|
|
2084 |
|
switch ((int)sortby) { |
2085 |
|
case FM_COL_STAT: |
2086 |
|
cmpres = stricmp (a->status, b->status); |
2087 |
|
break; |
2088 |
|
|
2089 |
|
case FM_COL_NAME: |
2090 |
|
cmpres = stricmp (a->name, b->name); |
2091 |
|
break; |
2092 |
|
|
2093 |
|
case FM_COL_OP: |
2094 |
|
if (a->op && b->op) |
2095 |
|
cmpres = stricmp (a->op, b->op); |
2096 |
|
break; |
2097 |
|
} |
2098 |
|
return cmpres; |
2099 |
|
} |
2100 |
|
|
2101 |
|
|
2102 |
|
/* Sort the list items from @lv with the mode given by @sortby. */ |
2103 |
|
int |
2104 |
|
fm_sort (listview_ctrl_t lv, int sortby) |
2105 |
|
{ |
2106 |
|
return ListView_SortItems (lv->ctrl, fm_cmp_cb, (LPARAM)sortby); |
2107 |
|
} |
2108 |
|
|
2109 |
|
|
2110 |
|
/* Start the 'print md' dialog. Pass over the listview control |
2111 |
|
@lv and the digest algo @mdalgo. */ |
2112 |
|
void |
2113 |
|
fm_print_md (listview_ctrl_t lv, HWND dlg, int mdalgo) |
2114 |
|
{ |
2115 |
|
struct md_file_s mdctx; |
2116 |
|
|
2117 |
|
if (listview_count_items (lv, 0) == 0) |
2118 |
|
return; |
2119 |
|
memset (&mdctx, 0, sizeof (mdctx)); |
2120 |
|
mdctx.lv = lv; |
2121 |
|
mdctx.mdalgo = mdalgo; |
2122 |
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_FILE_MDSUM, dlg, |
2123 |
|
mdsum_dlg_proc, (LPARAM)&mdctx); |
2124 |
|
} |
2125 |
|
|
2126 |
|
|
2127 |
|
/* Send the selected file in @lv via MAPI to a mail recipient. */ |
2128 |
|
int |
2129 |
|
fm_send_file (listview_ctrl_t lv) |
2130 |
|
{ |
2131 |
|
char name[MAX_PATH+1]; |
2132 |
|
int rc; |
2133 |
|
|
2134 |
|
rc = listview_get_item_text (lv, -1, FM_COL_NAME, name, DIM (name)-1); |
2135 |
|
if (rc != -1) |
2136 |
|
mapi_send_ascfile (name); |
2137 |
|
return 0; |
2138 |
|
} |