1 |
werner |
46 |
#if 0 |
2 |
|
|
filedisk_init_info @600 |
3 |
|
|
filedisk_free_info @601 |
4 |
|
|
filedisk_mount @602 |
5 |
|
|
filedisk_unmount @603 |
6 |
|
|
filedisk_status @604 |
7 |
|
|
|
8 |
|
|
/* wptFileDisk.c - Control program for a virtual disk driver for WinNT/2000/XP. |
9 |
|
|
|
10 |
|
|
Copyright (C) 1999, 2000, 2001, 2002 Bo Brantén. |
11 |
|
|
|
12 |
|
|
Heavily modified for the use with Cryptdisk by Timo Schulz |
13 |
|
|
(C) 2004 Timo Schulz |
14 |
|
|
|
15 |
|
|
This program is free software; you can redistribute it and/or modify |
16 |
|
|
it under the terms of the GNU General Public License as published by |
17 |
|
|
the Free Software Foundation; either version 2 of the License, or |
18 |
|
|
(at your option) any later version. |
19 |
|
|
This program is distributed in the hope that it will be useful, |
20 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 |
|
|
GNU General Public License for more details. |
23 |
|
|
You should have received a copy of the GNU General Public License |
24 |
|
|
along with this program; if not, write to the Free Software |
25 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#ifdef HAVE_CONFIG_H |
29 |
|
|
#include <config.h> |
30 |
|
|
#endif |
31 |
|
|
|
32 |
|
|
#include <windows.h> |
33 |
|
|
#include <windows.h> |
34 |
|
|
#include <winioctl.h> |
35 |
|
|
#include <stdio.h> |
36 |
|
|
#include <sys/types.h> |
37 |
|
|
|
38 |
|
|
#include "openpgp.h" |
39 |
|
|
#include "../Include/wptCryptdisk.h" |
40 |
|
|
|
41 |
|
|
/* remember to change the values in wptErrors.h also */ |
42 |
|
|
#define CDISK_ERR_LOCK 201 |
43 |
|
|
#define CDISK_ERR_MOUNT 202 |
44 |
|
|
#define CDISK_ERR_UMOUNT 203 |
45 |
|
|
#define CDISK_ERR_OPEN 204 |
46 |
|
|
#define CDISK_ERR_BUSY 205 |
47 |
|
|
#define CDISK_ERR_QUERY 206 |
48 |
|
|
|
49 |
|
|
static void |
50 |
|
|
print_lasterr (char * prefix) |
51 |
|
|
{ |
52 |
|
|
LPVOID msg; |
53 |
|
|
|
54 |
|
|
FormatMessage( |
55 |
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
56 |
|
|
FORMAT_MESSAGE_FROM_SYSTEM | |
57 |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, |
58 |
|
|
NULL, |
59 |
|
|
GetLastError(), |
60 |
|
|
0, |
61 |
|
|
(LPTSTR) &msg, |
62 |
|
|
0, |
63 |
|
|
NULL |
64 |
|
|
); |
65 |
|
|
MessageBox (NULL, (LPTSTR)msg, prefix, MB_ICONERROR|MB_OK); |
66 |
|
|
LocalFree (msg); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
#if 0 |
70 |
|
|
static int |
71 |
|
|
add_key (OPEN_FILE_INFORMATION *ka, const char * key, size_t len) |
72 |
|
|
{ |
73 |
|
|
gpg_md_t md=NULL; |
74 |
|
|
|
75 |
|
|
if (ka->KeyNum >= MAX_KEYS) |
76 |
|
|
return -1; |
77 |
|
|
|
78 |
|
|
switch (ka->KeyType) { |
79 |
|
|
case 1: /* 2fish */ |
80 |
|
|
md = gpg_md_open (MD_RMD160); |
81 |
|
|
ka->KeyLength=20; |
82 |
|
|
break; |
83 |
|
|
case 2: /* AES256 */ |
84 |
|
|
md = gpg_md_open (MD_SHA512); |
85 |
|
|
ka->KeyLength=32; |
86 |
|
|
break; |
87 |
|
|
case 3: /* AES128 */ |
88 |
|
|
md = gpg_md_open (MD_SHA256); |
89 |
|
|
ka->KeyLength=16; |
90 |
|
|
break; |
91 |
|
|
case 4: /* AES192 */ |
92 |
|
|
ka->KeyLength=24; |
93 |
|
|
md = gpg_md_open (MD_SHA384); |
94 |
|
|
break; |
95 |
|
|
} |
96 |
|
|
if (!md) |
97 |
|
|
return -1; |
98 |
|
|
gpg_md_write (md, (char *)key, len); |
99 |
|
|
gpg_md_final (md); |
100 |
|
|
memcpy (ka->Key[ka->KeyNum++], gpg_md_read (md), ka->KeyLength); |
101 |
|
|
gpg_md_close (md); |
102 |
|
|
return 0; |
103 |
|
|
} |
104 |
|
|
#endif |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
int |
108 |
|
|
filedisk_mount (int DeviceNumber, |
109 |
|
|
OPEN_FILE_INFORMATION *ofi, |
110 |
|
|
char drivelet, |
111 |
|
|
BOOLEAN CdImage) |
112 |
|
|
{ |
113 |
|
|
char VolumeName[] = "\\\\.\\ :"; |
114 |
|
|
char DeviceName[255]; |
115 |
|
|
HANDLE Device; |
116 |
|
|
DWORD BytesReturned; |
117 |
|
|
|
118 |
|
|
VolumeName[4] = drivelet; |
119 |
|
|
|
120 |
|
|
Device = CreateFile (VolumeName, |
121 |
|
|
GENERIC_READ | GENERIC_WRITE, |
122 |
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
123 |
|
|
NULL, |
124 |
|
|
OPEN_EXISTING, |
125 |
|
|
FILE_FLAG_NO_BUFFERING, |
126 |
|
|
NULL); |
127 |
|
|
if (Device != INVALID_HANDLE_VALUE) { |
128 |
|
|
SetLastError (ERROR_BUSY); |
129 |
|
|
print_lasterr (&VolumeName[4]); |
130 |
|
|
return CDISK_ERR_BUSY; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
if (CdImage) |
134 |
|
|
sprintf (DeviceName, DEVICE_NAME_PREFIX "Cd" "%u", DeviceNumber); |
135 |
|
|
else |
136 |
|
|
sprintf (DeviceName, DEVICE_NAME_PREFIX "%u", DeviceNumber); |
137 |
|
|
|
138 |
|
|
if (!DefineDosDevice (DDD_RAW_TARGET_PATH, |
139 |
|
|
&VolumeName[4], |
140 |
|
|
DeviceName)) { |
141 |
|
|
print_lasterr (&VolumeName[4]); |
142 |
|
|
return CDISK_ERR_OPEN; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
Device = CreateFile (VolumeName, |
146 |
|
|
GENERIC_READ | GENERIC_WRITE, |
147 |
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
148 |
|
|
NULL, |
149 |
|
|
OPEN_EXISTING, |
150 |
|
|
FILE_FLAG_NO_BUFFERING, |
151 |
|
|
NULL); |
152 |
|
|
|
153 |
|
|
if (Device == INVALID_HANDLE_VALUE) { |
154 |
|
|
print_lasterr (&VolumeName[4]); |
155 |
|
|
DefineDosDevice (DDD_REMOVE_DEFINITION, &VolumeName[4], NULL); |
156 |
|
|
return CDISK_ERR_OPEN; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
if (!DeviceIoControl (Device, |
160 |
|
|
IOCTL_FILE_DISK_OPEN_FILE, |
161 |
|
|
ofi, |
162 |
|
|
sizeof(OPEN_FILE_INFORMATION) + ofi->FileNameLength - 1, |
163 |
|
|
NULL, 0, &BytesReturned, NULL)) { |
164 |
|
|
print_lasterr ("Cryptdisk Error"); |
165 |
|
|
DefineDosDevice (DDD_REMOVE_DEFINITION, &VolumeName[4], NULL); |
166 |
|
|
return CDISK_ERR_MOUNT; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
return 0; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
|
173 |
|
|
static int |
174 |
|
|
filedisk_unmount2 (char drivelet, int flags, int forced) |
175 |
|
|
{ |
176 |
|
|
char VolumeName[60] = "\\\\.\\ :"; |
177 |
|
|
HANDLE Device; |
178 |
|
|
DWORD BytesReturned; |
179 |
|
|
|
180 |
|
|
if (!flags) |
181 |
|
|
VolumeName[4] = drivelet; |
182 |
|
|
else if (flags & 1) |
183 |
|
|
sprintf(VolumeName,"\\\\.\\FileDisk%d", drivelet); |
184 |
|
|
else if (flags & 2) |
185 |
|
|
sprintf(VolumeName,"\\\\.\\FileDiskCd%d",drivelet); |
186 |
|
|
|
187 |
|
|
Device = CreateFile (VolumeName, |
188 |
|
|
GENERIC_READ | GENERIC_WRITE, |
189 |
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
190 |
|
|
NULL, |
191 |
|
|
OPEN_EXISTING, |
192 |
|
|
FILE_FLAG_NO_BUFFERING, |
193 |
|
|
NULL); |
194 |
|
|
if (Device == INVALID_HANDLE_VALUE) { |
195 |
|
|
if (forced) |
196 |
|
|
print_lasterr(&VolumeName[4]); |
197 |
|
|
return CDISK_ERR_OPEN; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
if (!DeviceIoControl (Device, |
201 |
|
|
FSCTL_LOCK_VOLUME, |
202 |
|
|
NULL, 0, NULL, 0, |
203 |
|
|
&BytesReturned, NULL)) { |
204 |
|
|
if (forced) { |
205 |
|
|
print_lasterr(&VolumeName[4]); |
206 |
|
|
return CDISK_ERR_LOCK; |
207 |
|
|
} |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
if (!DeviceIoControl (Device, |
211 |
|
|
IOCTL_FILE_DISK_CLOSE_FILE, |
212 |
|
|
NULL, 0, NULL, 0, |
213 |
|
|
&BytesReturned, NULL )) { |
214 |
|
|
if (forced) |
215 |
|
|
print_lasterr ("Cryptdisk Error"); |
216 |
|
|
return CDISK_ERR_UMOUNT; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
if (!DeviceIoControl ( |
220 |
|
|
Device, FSCTL_DISMOUNT_VOLUME, |
221 |
|
|
NULL, 0, NULL, 0, |
222 |
|
|
&BytesReturned, NULL)) { |
223 |
|
|
if (forced) |
224 |
|
|
print_lasterr (&VolumeName[4]); |
225 |
|
|
return CDISK_ERR_UMOUNT; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
if (!DeviceIoControl (Device, |
229 |
|
|
FSCTL_UNLOCK_VOLUME, |
230 |
|
|
NULL, 0, NULL, 0, |
231 |
|
|
&BytesReturned, NULL)) { |
232 |
|
|
if (forced) { |
233 |
|
|
print_lasterr (&VolumeName[4]); |
234 |
|
|
return CDISK_ERR_LOCK; |
235 |
|
|
} |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
CloseHandle (Device); |
239 |
|
|
|
240 |
|
|
if (!DefineDosDevice ( |
241 |
|
|
DDD_REMOVE_DEFINITION, |
242 |
|
|
&VolumeName[4], NULL)) { |
243 |
|
|
if (forced) |
244 |
|
|
print_lasterr (&VolumeName[4]); |
245 |
|
|
return CDISK_ERR_UMOUNT; |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
return 0; |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
int |
252 |
|
|
filedisk_unmount (char drivelet, int forced) |
253 |
|
|
{ |
254 |
|
|
char i; |
255 |
|
|
|
256 |
|
|
if (drivelet) { |
257 |
|
|
int rc = filedisk_unmount2 (drivelet, 0, forced); |
258 |
|
|
if (rc) |
259 |
|
|
print_lasterr ("Unmount´"); |
260 |
|
|
return rc; |
261 |
|
|
} |
262 |
|
|
for (i=0; i<20;i++) { |
263 |
|
|
if (filedisk_unmount2 (i, 1, 0) == CDISK_ERR_OPEN) |
264 |
|
|
break; |
265 |
|
|
} |
266 |
|
|
for (i=0; i<20;i++) { |
267 |
|
|
if (filedisk_unmount2 (i, 2, 0) == CDISK_ERR_OPEN) |
268 |
|
|
break; |
269 |
|
|
} |
270 |
|
|
return 0; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
|
274 |
|
|
int |
275 |
|
|
filedisk_status (char drivelet, LARGE_INTEGER * size, int * rdonly) |
276 |
|
|
{ |
277 |
|
|
char VolumeName[] = "\\\\.\\ :"; |
278 |
|
|
HANDLE Device; |
279 |
|
|
POPEN_FILE_INFORMATION ofi; |
280 |
|
|
DWORD BytesReturned; |
281 |
|
|
|
282 |
|
|
VolumeName[4] = drivelet; |
283 |
|
|
|
284 |
|
|
Device = CreateFile (VolumeName, |
285 |
|
|
GENERIC_READ, |
286 |
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
287 |
|
|
NULL, |
288 |
|
|
OPEN_EXISTING, |
289 |
|
|
FILE_FLAG_NO_BUFFERING, |
290 |
|
|
NULL); |
291 |
|
|
|
292 |
|
|
if (Device == INVALID_HANDLE_VALUE) { |
293 |
|
|
print_lasterr(&VolumeName[4]); |
294 |
|
|
return CDISK_ERR_OPEN; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
ofi = malloc (sizeof(OPEN_FILE_INFORMATION) + MAX_PATH); |
298 |
|
|
|
299 |
|
|
if (!DeviceIoControl (Device, |
300 |
|
|
IOCTL_FILE_DISK_QUERY_FILE, |
301 |
|
|
NULL, |
302 |
|
|
0, |
303 |
|
|
ofi, |
304 |
|
|
sizeof(OPEN_FILE_INFORMATION) + MAX_PATH, |
305 |
|
|
&BytesReturned, NULL)) { |
306 |
|
|
print_lasterr (&VolumeName[4]); |
307 |
|
|
return CDISK_ERR_QUERY; |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
if (BytesReturned < sizeof(OPEN_FILE_INFORMATION)) { |
311 |
|
|
SetLastError (ERROR_INSUFFICIENT_BUFFER); |
312 |
|
|
print_lasterr (&VolumeName[4]); |
313 |
|
|
return CDISK_ERR_QUERY; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
if (size) |
317 |
|
|
*size = ofi->FileSize; |
318 |
|
|
if (rdonly) |
319 |
|
|
*rdonly = ofi->ReadOnly; |
320 |
|
|
|
321 |
|
|
return 0; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
|
325 |
|
|
POPEN_FILE_INFORMATION |
326 |
|
|
filedisk_init_info (int devnum, char drivelet, const char * file, |
327 |
|
|
int ro, int cd_img, unsigned long size, const char * key, |
328 |
|
|
int keyalgo) |
329 |
|
|
{ |
330 |
|
|
POPEN_FILE_INFORMATION pfi; |
331 |
|
|
|
332 |
|
|
pfi = calloc (1, sizeof(OPEN_FILE_INFORMATION) + strlen (file) + 7); |
333 |
|
|
if (!pfi) |
334 |
|
|
return NULL; |
335 |
|
|
|
336 |
|
|
/*pfi->version = CCVERSION;*/ |
337 |
|
|
if (file[0] == '\\') { |
338 |
|
|
if (file[1] == '\\' && file[2] != '.') { |
339 |
|
|
/* \\server\share\path\filedisk.img */ |
340 |
|
|
strcpy(pfi->FileName, "\\??\\UNC"); |
341 |
|
|
strcat(pfi->FileName, file + 1); |
342 |
|
|
} |
343 |
|
|
else /* \Device\Harddisk0\Partition1\path\filedisk.img */ |
344 |
|
|
strcpy(pfi->FileName, file); |
345 |
|
|
} |
346 |
|
|
else { // c:\path\filedisk.img |
347 |
|
|
strcpy (pfi->FileName, "\\??\\"); |
348 |
|
|
strcat (pfi->FileName, file); |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
pfi->FileNameLength = (USHORT) strlen (pfi->FileName); |
352 |
|
|
pfi->ReadOnly = ro? TRUE : FALSE; |
353 |
|
|
if (cd_img) |
354 |
|
|
pfi->ReadOnly = TRUE; |
355 |
|
|
if (size) |
356 |
|
|
pfi->FileSize.QuadPart = size * 1024; |
357 |
|
|
if (key) { |
358 |
|
|
/* |
359 |
|
|
if (keyalgo < 1 || keyalgo > 4) |
360 |
|
|
pfi->KeyType = 1; |
361 |
|
|
else |
362 |
|
|
pfi->KeyType = keyalgo; |
363 |
|
|
add_key (pfi, key, strlen (key)); |
364 |
|
|
*/ |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
return pfi; |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
|
371 |
|
|
void |
372 |
|
|
filedisk_free_info (POPEN_FILE_INFORMATION pfi) |
373 |
|
|
{ |
374 |
|
|
if (!pfi) |
375 |
|
|
return; |
376 |
|
|
/*memset (pfi->Key, 0, sizeof pfi->Key);*/ |
377 |
|
|
free (pfi); |
378 |
|
|
} |
379 |
|
|
#endif |
380 |
|
|
|