/[winpt]/trunk/PTD/wptFileDisk.cpp
ViewVC logotype

Annotation of /trunk/PTD/wptFileDisk.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations)
Tue Oct 25 07:46:20 2005 UTC (19 years, 4 months ago) by twoaday
File size: 9203 byte(s)
More bug fixes and cleanups.
See ChangeLog for details.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26