dinput: Silence incorrect warning and move it to a valid place.
[wine] / dlls / mountmgr.sys / mountmgr.c
1 /*
2  * Mount manager service implementation
3  *
4  * Copyright 2008 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <unistd.h>
23
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "winioctl.h"
33 #include "winreg.h"
34 #include "ddk/wdm.h"
35 #include "ddk/mountmgr.h"
36 #include "wine/library.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
41
42 #define MAX_DOS_DRIVES 26
43 #define MAX_MOUNT_POINTS (2 * MAX_DOS_DRIVES)
44
45 /* extra info for disk devices, stored in DeviceExtension */
46 struct disk_device_info
47 {
48     UNICODE_STRING name;    /* device name */
49 };
50
51 struct mount_point
52 {
53     DEVICE_OBJECT *device;
54     UNICODE_STRING link;    /* DOS device symlink */
55     void          *id;      /* device unique id */
56     unsigned int   id_len;
57 };
58
59 static struct mount_point mount_points[MAX_MOUNT_POINTS];
60 static HKEY mount_key;
61
62 static inline UNICODE_STRING *get_device_name( DEVICE_OBJECT *dev )
63 {
64     return &((struct disk_device_info *)dev->DeviceExtension)->name;
65 }
66
67 /* read a Unix symlink; returned buffer must be freed by caller */
68 static char *read_symlink( const char *path )
69 {
70     char *buffer;
71     int ret, size = 128;
72
73     for (;;)
74     {
75         if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
76         {
77             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
78             return 0;
79         }
80         ret = readlink( path, buffer, size );
81         if (ret == -1)
82         {
83             RtlFreeHeap( GetProcessHeap(), 0, buffer );
84             return 0;
85         }
86         if (ret != size)
87         {
88             buffer[ret] = 0;
89             return buffer;
90         }
91         RtlFreeHeap( GetProcessHeap(), 0, buffer );
92         size *= 2;
93     }
94 }
95
96 static NTSTATUS create_disk_device( DRIVER_OBJECT *driver, DWORD type, DEVICE_OBJECT **dev_obj )
97 {
98     static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e',
99                                       '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
100     static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
101     static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
102
103     UINT i, first = 0;
104     NTSTATUS status = 0;
105     const WCHAR *format;
106     UNICODE_STRING name;
107     struct disk_device_info *info;
108
109     switch(type)
110     {
111     case DRIVE_REMOVABLE:
112         format = floppyW;
113         break;
114     case DRIVE_CDROM:
115         format = cdromW;
116         break;
117     case DRIVE_FIXED:
118     default:  /* FIXME */
119         format = harddiskW;
120         first = 1;  /* harddisk volumes start counting from 1 */
121         break;
122     }
123
124     name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
125     name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
126     for (i = first; i < 32; i++)
127     {
128         sprintfW( name.Buffer, format, i );
129         name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
130         status = IoCreateDevice( driver, sizeof(*info), &name, 0, 0, FALSE, dev_obj );
131         if (status != STATUS_OBJECT_NAME_COLLISION) break;
132     }
133     if (!status)
134     {
135         info = (*dev_obj)->DeviceExtension;
136         info->name = name;
137     }
138     else
139     {
140         FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
141         RtlFreeUnicodeString( &name );
142     }
143     return status;
144 }
145
146
147 static NTSTATUS add_mount_point( DRIVER_OBJECT *driver, DWORD type, int drive,
148                                  const void *id, unsigned int id_len )
149 {
150     static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
151     static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
152                                     '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
153                                     '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
154                                     '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
155     WCHAR *drive_link, *volume_link;
156     NTSTATUS status;
157     GUID guid;
158     UINT i;
159     struct mount_point *mount_drive = NULL, *mount_volume = NULL;
160
161     /* find two free mount points */
162
163     for (i = 0; i < MAX_MOUNT_POINTS; i++)
164     {
165         if (mount_points[i].device) continue;
166         if (!mount_drive)
167         {
168             mount_drive = &mount_points[i];
169             continue;
170         }
171         mount_volume = &mount_points[i];
172         break;
173     }
174     if (!mount_volume) return STATUS_NO_MEMORY;
175
176     /* create the volume */
177
178     memset( &guid, 0, sizeof(guid) );  /* FIXME */
179     guid.Data4[7] = 'A' + drive;
180
181     drive_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(driveW) );
182     volume_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(volumeW) );
183     sprintfW( drive_link, driveW, 'A' + drive );
184     sprintfW( volume_link, volumeW, guid.Data1, guid.Data2, guid.Data3,
185               guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
186               guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
187
188     RtlInitUnicodeString( &mount_drive->link, drive_link );
189     RtlInitUnicodeString( &mount_volume->link, volume_link );
190     status = create_disk_device( driver, type, &mount_drive->device );
191     if (status)
192     {
193         RtlFreeUnicodeString( &mount_drive->link );
194         RtlFreeUnicodeString( &mount_volume->link );
195         return status;
196     }
197
198     mount_volume->device = mount_drive->device;  /* FIXME: incr ref count */
199     mount_drive->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
200     mount_drive->id_len = id_len;
201     memcpy( mount_drive->id, id, id_len );
202     mount_volume->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
203     mount_volume->id_len = id_len;
204     memcpy( mount_volume->id, id, id_len );
205
206     IoCreateSymbolicLink( &mount_drive->link, get_device_name(mount_drive->device) );
207     IoCreateSymbolicLink( &mount_volume->link, get_device_name(mount_volume->device) );
208
209     TRACE( "created device %s symlinks %s %s\n", debugstr_w(get_device_name(mount_drive->device)->Buffer),
210            debugstr_w(mount_drive->link.Buffer), debugstr_w(mount_volume->link.Buffer) );
211
212     RegSetValueExW( mount_key, mount_drive->link.Buffer, 0, REG_BINARY,
213                     mount_drive->id, mount_drive->id_len );
214     RegSetValueExW( mount_key, mount_volume->link.Buffer, 0, REG_BINARY,
215                     mount_volume->id, mount_volume->id_len );
216
217     return STATUS_SUCCESS;
218 }
219
220 /* check if a given mount point matches the requested specs */
221 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
222 {
223     if (spec->SymbolicLinkNameOffset)
224     {
225         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
226         if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
227         if (memicmpW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
228             return FALSE;
229     }
230     if (spec->DeviceNameOffset)
231     {
232         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
233         const UNICODE_STRING *dev_name = get_device_name( mount->device );
234         if (spec->DeviceNameLength != dev_name->Length) return FALSE;
235         if (memicmpW( name, dev_name->Buffer, dev_name->Length/sizeof(WCHAR)))
236             return FALSE;
237     }
238     if (spec->UniqueIdOffset)
239     {
240         const void *id = ((const char *)spec + spec->UniqueIdOffset);
241         if (spec->UniqueIdLength != mount->id_len) return FALSE;
242         if (memcmp( id, mount->id, mount->id_len )) return FALSE;
243     }
244     return TRUE;
245 }
246
247 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
248 static NTSTATUS query_mount_points( const void *in_buff, SIZE_T insize,
249                                     void *out_buff, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
250 {
251     UINT i, j, pos, size;
252     const MOUNTMGR_MOUNT_POINT *input = in_buff;
253     MOUNTMGR_MOUNT_POINTS *info = out_buff;
254     UNICODE_STRING *dev_name;
255
256     /* sanity checks */
257     if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
258         input->UniqueIdOffset + input->UniqueIdLength > insize ||
259         input->DeviceNameOffset + input->DeviceNameLength > insize ||
260         input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
261         input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
262         input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
263         return STATUS_INVALID_PARAMETER;
264
265     for (i = j = size = 0; i < MAX_MOUNT_POINTS; i++)
266     {
267         if (!mount_points[i].device) continue;
268         if (!matching_mount_point( &mount_points[i], input )) continue;
269         size += get_device_name(mount_points[i].device)->Length;
270         size += mount_points[i].link.Length;
271         j++;
272     }
273     pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[j] );
274     size += pos;
275
276     if (size > outsize)
277     {
278         if (size >= sizeof(info->Size)) info->Size = size;
279         iosb->Information = sizeof(info->Size);
280         return STATUS_MORE_ENTRIES;
281     }
282
283     info->NumberOfMountPoints = j;
284     for (i = j = 0; i < MAX_MOUNT_POINTS; i++)
285     {
286         if (!mount_points[i].device) continue;
287         if (!matching_mount_point( &mount_points[i], input )) continue;
288         info->MountPoints[j].UniqueIdOffset = 0;  /* FIXME */
289         info->MountPoints[j].UniqueIdLength = 0;
290
291         dev_name = get_device_name( mount_points[i].device );
292         info->MountPoints[j].DeviceNameOffset = pos;
293         info->MountPoints[j].DeviceNameLength = dev_name->Length;
294         memcpy( (char *)out_buff + pos, dev_name->Buffer, dev_name->Length );
295         pos += dev_name->Length;
296
297         info->MountPoints[j].SymbolicLinkNameOffset = pos;
298         info->MountPoints[j].SymbolicLinkNameLength = mount_points[i].link.Length;
299         memcpy( (char *)out_buff + pos, mount_points[i].link.Buffer, mount_points[i].link.Length );
300         pos += mount_points[i].link.Length;
301         j++;
302     }
303     info->Size = pos;
304     iosb->Information = pos;
305     return STATUS_SUCCESS;
306 }
307
308 /* handler for ioctls on the mount manager device */
309 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
310 {
311     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
312
313     TRACE( "ioctl %x insize %u outsize %u\n",
314            irpsp->Parameters.DeviceIoControl.IoControlCode,
315            irpsp->Parameters.DeviceIoControl.InputBufferLength,
316            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
317
318     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
319     {
320     case IOCTL_MOUNTMGR_QUERY_POINTS:
321         if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
322             return STATUS_INVALID_PARAMETER;
323         irp->IoStatus.u.Status = query_mount_points( irpsp->Parameters.DeviceIoControl.Type3InputBuffer,
324                                                      irpsp->Parameters.DeviceIoControl.InputBufferLength,
325                                                      irp->MdlAddress->StartVa,
326                                                      irpsp->Parameters.DeviceIoControl.OutputBufferLength,
327                                                      &irp->IoStatus );
328         break;
329     default:
330         FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
331         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
332         break;
333     }
334     return irp->IoStatus.u.Status;
335 }
336
337 /* handler for ioctls on the harddisk device */
338 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
339 {
340     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
341
342     TRACE( "ioctl %x insize %u outsize %u\n",
343            irpsp->Parameters.DeviceIoControl.IoControlCode,
344            irpsp->Parameters.DeviceIoControl.InputBufferLength,
345            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
346
347     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
348     {
349     default:
350         FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
351         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
352         break;
353     }
354     return irp->IoStatus.u.Status;
355 }
356
357 /* create mount points for mapped drives */
358 static void create_drive_mount_points( DRIVER_OBJECT *driver )
359 {
360     const char *config_dir = wine_get_config_dir();
361     char *buffer, *p, *link;
362     unsigned int i;
363
364     if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
365                                    strlen(config_dir) + sizeof("/dosdevices/a:") )))
366     {
367         strcpy( buffer, config_dir );
368         strcat( buffer, "/dosdevices/a:" );
369         p = buffer + strlen(buffer) - 2;
370
371         for (i = 0; i < MAX_DOS_DRIVES; i++)
372         {
373             *p = 'a' + i;
374             if (!(link = read_symlink( buffer ))) continue;
375             add_mount_point( driver, DRIVE_FIXED, i, link, strlen(link) );
376             RtlFreeHeap( GetProcessHeap(), 0, link );
377         }
378         RtlFreeHeap( GetProcessHeap(), 0, buffer );
379     }
380 }
381
382 /* driver entry point for the harddisk driver */
383 static NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
384 {
385     static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\',
386                                              'M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
387     static const WCHAR harddisk0W[] = {'\\','D','e','v','i','c','e',
388                                        '\\','H','a','r','d','d','i','s','k','0',0};
389     static const WCHAR physdrive0W[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
390
391     UNICODE_STRING nameW, linkW;
392     DEVICE_OBJECT *device;
393     NTSTATUS status;
394
395     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
396
397     RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
398                      REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
399
400     RtlInitUnicodeString( &nameW, harddisk0W );
401     RtlInitUnicodeString( &linkW, physdrive0W );
402     if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
403         status = IoCreateSymbolicLink( &linkW, &nameW );
404     if (status)
405     {
406         FIXME( "failed to create device error %x\n", status );
407         return status;
408     }
409
410     create_drive_mount_points( driver );
411
412     return status;
413 }
414
415 /* main entry point for the mount point manager driver */
416 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
417 {
418     static const WCHAR device_mountmgrW[] = {'\\','D','e','v','i','c','e','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
419     static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
420     static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
421
422     UNICODE_STRING nameW, linkW;
423     DEVICE_OBJECT *device;
424     NTSTATUS status;
425
426     TRACE( "%s\n", debugstr_w(path->Buffer) );
427
428     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
429
430     RtlInitUnicodeString( &nameW, device_mountmgrW );
431     RtlInitUnicodeString( &linkW, link_mountmgrW );
432     if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
433         status = IoCreateSymbolicLink( &linkW, &nameW );
434     if (status)
435     {
436         FIXME( "failed to create device error %x\n", status );
437         return status;
438     }
439
440     RtlInitUnicodeString( &nameW, harddiskW );
441     status = IoCreateDriver( &nameW, harddisk_driver_entry );
442
443     return status;
444 }