winex11: add owned windows to taskbar if owner is not mapped
[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 "ntddstor.h"
35 #include "ntddcdrm.h"
36 #include "ddk/wdm.h"
37 #include "ddk/mountmgr.h"
38 #include "wine/library.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
43
44 #define MAX_DOS_DRIVES 26
45 #define MAX_MOUNT_POINTS (2 * MAX_DOS_DRIVES)
46
47 /* extra info for disk devices, stored in DeviceExtension */
48 struct disk_device_info
49 {
50     UNICODE_STRING        name;     /* device name */
51     STORAGE_DEVICE_NUMBER devnum;   /* device number info */
52 };
53
54 struct mount_point
55 {
56     DEVICE_OBJECT *device;
57     UNICODE_STRING link;    /* DOS device symlink */
58     void          *id;      /* device unique id */
59     unsigned int   id_len;
60 };
61
62 static struct mount_point mount_points[MAX_MOUNT_POINTS];
63 static HKEY mount_key;
64
65 static inline UNICODE_STRING *get_device_name( DEVICE_OBJECT *dev )
66 {
67     return &((struct disk_device_info *)dev->DeviceExtension)->name;
68 }
69
70 /* read a Unix symlink; returned buffer must be freed by caller */
71 static char *read_symlink( const char *path )
72 {
73     char *buffer;
74     int ret, size = 128;
75
76     for (;;)
77     {
78         if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, size )))
79         {
80             SetLastError( ERROR_NOT_ENOUGH_MEMORY );
81             return 0;
82         }
83         ret = readlink( path, buffer, size );
84         if (ret == -1)
85         {
86             RtlFreeHeap( GetProcessHeap(), 0, buffer );
87             return 0;
88         }
89         if (ret != size)
90         {
91             buffer[ret] = 0;
92             return buffer;
93         }
94         RtlFreeHeap( GetProcessHeap(), 0, buffer );
95         size *= 2;
96     }
97 }
98
99 static NTSTATUS create_disk_device( DRIVER_OBJECT *driver, DWORD type, DEVICE_OBJECT **dev_obj )
100 {
101     static const WCHAR harddiskW[] = {'\\','D','e','v','i','c','e',
102                                       '\\','H','a','r','d','d','i','s','k','V','o','l','u','m','e','%','u',0};
103     static const WCHAR cdromW[] = {'\\','D','e','v','i','c','e','\\','C','d','R','o','m','%','u',0};
104     static const WCHAR floppyW[] = {'\\','D','e','v','i','c','e','\\','F','l','o','p','p','y','%','u',0};
105
106     UINT i, first = 0;
107     NTSTATUS status = 0;
108     const WCHAR *format;
109     UNICODE_STRING name;
110     struct disk_device_info *info;
111
112     switch(type)
113     {
114     case DRIVE_REMOVABLE:
115         format = floppyW;
116         break;
117     case DRIVE_CDROM:
118         format = cdromW;
119         break;
120     case DRIVE_FIXED:
121     default:  /* FIXME */
122         format = harddiskW;
123         first = 1;  /* harddisk volumes start counting from 1 */
124         break;
125     }
126
127     name.MaximumLength = (strlenW(format) + 10) * sizeof(WCHAR);
128     name.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, name.MaximumLength );
129     for (i = first; i < 32; i++)
130     {
131         sprintfW( name.Buffer, format, i );
132         name.Length = strlenW(name.Buffer) * sizeof(WCHAR);
133         status = IoCreateDevice( driver, sizeof(*info), &name, 0, 0, FALSE, dev_obj );
134         if (status != STATUS_OBJECT_NAME_COLLISION) break;
135     }
136     if (!status)
137     {
138         info = (*dev_obj)->DeviceExtension;
139         info->name = name;
140         switch(type)
141         {
142         case DRIVE_REMOVABLE:
143             info->devnum.DeviceType = FILE_DEVICE_DISK;
144             info->devnum.DeviceNumber = i;
145             info->devnum.PartitionNumber = ~0u;
146             break;
147         case DRIVE_CDROM:
148             info->devnum.DeviceType = FILE_DEVICE_CD_ROM;
149             info->devnum.DeviceNumber = i;
150             info->devnum.PartitionNumber = ~0u;
151             break;
152         case DRIVE_FIXED:
153         default:  /* FIXME */
154             info->devnum.DeviceType = FILE_DEVICE_DISK;
155             info->devnum.DeviceNumber = 0;
156             info->devnum.PartitionNumber = i;
157             break;
158         }
159     }
160     else
161     {
162         FIXME( "IoCreateDevice %s got %x\n", debugstr_w(name.Buffer), status );
163         RtlFreeUnicodeString( &name );
164     }
165     return status;
166 }
167
168
169 static NTSTATUS add_mount_point( DRIVER_OBJECT *driver, DWORD type, int drive,
170                                  const void *id, unsigned int id_len )
171 {
172     static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
173     static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
174                                     '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
175                                     '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
176                                     '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
177     WCHAR *drive_link, *volume_link;
178     NTSTATUS status;
179     GUID guid;
180     UINT i;
181     struct mount_point *mount_drive = NULL, *mount_volume = NULL;
182
183     /* find two free mount points */
184
185     for (i = 0; i < MAX_MOUNT_POINTS; i++)
186     {
187         if (mount_points[i].device) continue;
188         if (!mount_drive)
189         {
190             mount_drive = &mount_points[i];
191             continue;
192         }
193         mount_volume = &mount_points[i];
194         break;
195     }
196     if (!mount_volume) return STATUS_NO_MEMORY;
197
198     /* create the volume */
199
200     memset( &guid, 0, sizeof(guid) );  /* FIXME */
201     guid.Data4[7] = 'A' + drive;
202
203     drive_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(driveW) );
204     volume_link = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(volumeW) );
205     sprintfW( drive_link, driveW, 'A' + drive );
206     sprintfW( volume_link, volumeW, guid.Data1, guid.Data2, guid.Data3,
207               guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
208               guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
209
210     RtlInitUnicodeString( &mount_drive->link, drive_link );
211     RtlInitUnicodeString( &mount_volume->link, volume_link );
212     status = create_disk_device( driver, type, &mount_drive->device );
213     if (status)
214     {
215         RtlFreeUnicodeString( &mount_drive->link );
216         RtlFreeUnicodeString( &mount_volume->link );
217         return status;
218     }
219
220     mount_volume->device = mount_drive->device;  /* FIXME: incr ref count */
221     mount_drive->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
222     mount_drive->id_len = id_len;
223     memcpy( mount_drive->id, id, id_len );
224     mount_volume->id = RtlAllocateHeap( GetProcessHeap(), 0, id_len );
225     mount_volume->id_len = id_len;
226     memcpy( mount_volume->id, id, id_len );
227
228     IoCreateSymbolicLink( &mount_drive->link, get_device_name(mount_drive->device) );
229     IoCreateSymbolicLink( &mount_volume->link, get_device_name(mount_volume->device) );
230
231     TRACE( "created device %s symlinks %s %s\n", debugstr_w(get_device_name(mount_drive->device)->Buffer),
232            debugstr_w(mount_drive->link.Buffer), debugstr_w(mount_volume->link.Buffer) );
233
234     RegSetValueExW( mount_key, mount_drive->link.Buffer, 0, REG_BINARY,
235                     mount_drive->id, mount_drive->id_len );
236     RegSetValueExW( mount_key, mount_volume->link.Buffer, 0, REG_BINARY,
237                     mount_volume->id, mount_volume->id_len );
238
239     return STATUS_SUCCESS;
240 }
241
242 /* check if a given mount point matches the requested specs */
243 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
244 {
245     if (spec->SymbolicLinkNameOffset)
246     {
247         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
248         if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
249         if (memicmpW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
250             return FALSE;
251     }
252     if (spec->DeviceNameOffset)
253     {
254         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
255         const UNICODE_STRING *dev_name = get_device_name( mount->device );
256         if (spec->DeviceNameLength != dev_name->Length) return FALSE;
257         if (memicmpW( name, dev_name->Buffer, dev_name->Length/sizeof(WCHAR)))
258             return FALSE;
259     }
260     if (spec->UniqueIdOffset)
261     {
262         const void *id = ((const char *)spec + spec->UniqueIdOffset);
263         if (spec->UniqueIdLength != mount->id_len) return FALSE;
264         if (memcmp( id, mount->id, mount->id_len )) return FALSE;
265     }
266     return TRUE;
267 }
268
269 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
270 static NTSTATUS query_mount_points( const void *in_buff, SIZE_T insize,
271                                     void *out_buff, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
272 {
273     UINT i, j, pos, size;
274     const MOUNTMGR_MOUNT_POINT *input = in_buff;
275     MOUNTMGR_MOUNT_POINTS *info = out_buff;
276     UNICODE_STRING *dev_name;
277
278     /* sanity checks */
279     if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
280         input->UniqueIdOffset + input->UniqueIdLength > insize ||
281         input->DeviceNameOffset + input->DeviceNameLength > insize ||
282         input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
283         input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
284         input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
285         return STATUS_INVALID_PARAMETER;
286
287     for (i = j = size = 0; i < MAX_MOUNT_POINTS; i++)
288     {
289         if (!mount_points[i].device) continue;
290         if (!matching_mount_point( &mount_points[i], input )) continue;
291         size += get_device_name(mount_points[i].device)->Length;
292         size += mount_points[i].link.Length;
293         j++;
294     }
295     pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[j] );
296     size += pos;
297
298     if (size > outsize)
299     {
300         if (size >= sizeof(info->Size)) info->Size = size;
301         iosb->Information = sizeof(info->Size);
302         return STATUS_MORE_ENTRIES;
303     }
304
305     info->NumberOfMountPoints = j;
306     for (i = j = 0; i < MAX_MOUNT_POINTS; i++)
307     {
308         if (!mount_points[i].device) continue;
309         if (!matching_mount_point( &mount_points[i], input )) continue;
310         info->MountPoints[j].UniqueIdOffset = 0;  /* FIXME */
311         info->MountPoints[j].UniqueIdLength = 0;
312
313         dev_name = get_device_name( mount_points[i].device );
314         info->MountPoints[j].DeviceNameOffset = pos;
315         info->MountPoints[j].DeviceNameLength = dev_name->Length;
316         memcpy( (char *)out_buff + pos, dev_name->Buffer, dev_name->Length );
317         pos += dev_name->Length;
318
319         info->MountPoints[j].SymbolicLinkNameOffset = pos;
320         info->MountPoints[j].SymbolicLinkNameLength = mount_points[i].link.Length;
321         memcpy( (char *)out_buff + pos, mount_points[i].link.Buffer, mount_points[i].link.Length );
322         pos += mount_points[i].link.Length;
323         j++;
324     }
325     info->Size = pos;
326     iosb->Information = pos;
327     return STATUS_SUCCESS;
328 }
329
330 /* handler for ioctls on the mount manager device */
331 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
332 {
333     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
334
335     TRACE( "ioctl %x insize %u outsize %u\n",
336            irpsp->Parameters.DeviceIoControl.IoControlCode,
337            irpsp->Parameters.DeviceIoControl.InputBufferLength,
338            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
339
340     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
341     {
342     case IOCTL_MOUNTMGR_QUERY_POINTS:
343         if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
344             return STATUS_INVALID_PARAMETER;
345         irp->IoStatus.u.Status = query_mount_points( irpsp->Parameters.DeviceIoControl.Type3InputBuffer,
346                                                      irpsp->Parameters.DeviceIoControl.InputBufferLength,
347                                                      irp->MdlAddress->StartVa,
348                                                      irpsp->Parameters.DeviceIoControl.OutputBufferLength,
349                                                      &irp->IoStatus );
350         break;
351     default:
352         FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
353         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
354         break;
355     }
356     return irp->IoStatus.u.Status;
357 }
358
359 /* handler for ioctls on the harddisk device */
360 static NTSTATUS WINAPI harddisk_ioctl( DEVICE_OBJECT *device, IRP *irp )
361 {
362     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
363     struct disk_device_info *disk_info = device->DeviceExtension;
364
365     TRACE( "ioctl %x insize %u outsize %u\n",
366            irpsp->Parameters.DeviceIoControl.IoControlCode,
367            irpsp->Parameters.DeviceIoControl.InputBufferLength,
368            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
369
370     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
371     {
372     case IOCTL_DISK_GET_DRIVE_GEOMETRY:
373     {
374         DISK_GEOMETRY info;
375         DWORD len = min( sizeof(info), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
376
377         info.Cylinders.QuadPart = 10000;
378         info.MediaType = (disk_info->devnum.DeviceType == FILE_DEVICE_DISK) ? FixedMedia : RemovableMedia;
379         info.TracksPerCylinder = 255;
380         info.SectorsPerTrack = 63;
381         info.BytesPerSector = 512;
382         memcpy( irp->MdlAddress->StartVa, &info, len );
383         irp->IoStatus.Information = len;
384         irp->IoStatus.u.Status = STATUS_SUCCESS;
385         break;
386     }
387     case IOCTL_STORAGE_GET_DEVICE_NUMBER:
388     {
389         DWORD len = min( sizeof(disk_info->devnum), irpsp->Parameters.DeviceIoControl.OutputBufferLength );
390
391         memcpy( irp->MdlAddress->StartVa, &disk_info->devnum, len );
392         irp->IoStatus.Information = len;
393         irp->IoStatus.u.Status = STATUS_SUCCESS;
394         break;
395     }
396     case IOCTL_CDROM_READ_TOC:
397         irp->IoStatus.u.Status = STATUS_INVALID_DEVICE_REQUEST;
398         break;
399     default:
400         FIXME( "unsupported ioctl %x\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
401         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
402         break;
403     }
404     return irp->IoStatus.u.Status;
405 }
406
407 /* create mount points for mapped drives */
408 static void create_drive_mount_points( DRIVER_OBJECT *driver )
409 {
410     const char *config_dir = wine_get_config_dir();
411     char *buffer, *p, *link;
412     unsigned int i;
413
414     if ((buffer = RtlAllocateHeap( GetProcessHeap(), 0,
415                                    strlen(config_dir) + sizeof("/dosdevices/a:") )))
416     {
417         strcpy( buffer, config_dir );
418         strcat( buffer, "/dosdevices/a:" );
419         p = buffer + strlen(buffer) - 2;
420
421         for (i = 0; i < MAX_DOS_DRIVES; i++)
422         {
423             *p = 'a' + i;
424             if (!(link = read_symlink( buffer ))) continue;
425             add_mount_point( driver, DRIVE_FIXED, i, link, strlen(link) );
426             RtlFreeHeap( GetProcessHeap(), 0, link );
427         }
428         RtlFreeHeap( GetProcessHeap(), 0, buffer );
429     }
430 }
431
432 /* driver entry point for the harddisk driver */
433 static NTSTATUS WINAPI harddisk_driver_entry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
434 {
435     static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\',
436                                              'M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
437     static const WCHAR harddisk0W[] = {'\\','D','e','v','i','c','e',
438                                        '\\','H','a','r','d','d','i','s','k','0',0};
439     static const WCHAR physdrive0W[] = {'\\','?','?','\\','P','h','y','s','i','c','a','l','D','r','i','v','e','0',0};
440
441     UNICODE_STRING nameW, linkW;
442     DEVICE_OBJECT *device;
443     NTSTATUS status;
444     struct disk_device_info *info;
445
446     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = harddisk_ioctl;
447
448     RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
449                      REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
450
451     RtlInitUnicodeString( &nameW, harddisk0W );
452     RtlInitUnicodeString( &linkW, physdrive0W );
453     if (!(status = IoCreateDevice( driver, sizeof(*info), &nameW, 0, 0, FALSE, &device )))
454         status = IoCreateSymbolicLink( &linkW, &nameW );
455     if (status)
456     {
457         FIXME( "failed to create device error %x\n", status );
458         return status;
459     }
460     info = device->DeviceExtension;
461     info->name = nameW;
462     info->devnum.DeviceType = FILE_DEVICE_DISK;
463     info->devnum.DeviceNumber = 0;
464     info->devnum.PartitionNumber = 0;
465
466     create_drive_mount_points( driver );
467
468     return status;
469 }
470
471 /* main entry point for the mount point manager driver */
472 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
473 {
474     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};
475     static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
476     static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
477
478     UNICODE_STRING nameW, linkW;
479     DEVICE_OBJECT *device;
480     NTSTATUS status;
481
482     TRACE( "%s\n", debugstr_w(path->Buffer) );
483
484     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
485
486     RtlInitUnicodeString( &nameW, device_mountmgrW );
487     RtlInitUnicodeString( &linkW, link_mountmgrW );
488     if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
489         status = IoCreateSymbolicLink( &linkW, &nameW );
490     if (status)
491     {
492         FIXME( "failed to create device error %x\n", status );
493         return status;
494     }
495
496     RtlInitUnicodeString( &nameW, harddiskW );
497     status = IoCreateDriver( &nameW, harddisk_driver_entry );
498
499     return status;
500 }