comctl32: Add implementation of LVS_EX_ONECLICKACTIVATE.
[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 "mountmgr.h"
28 #include "winreg.h"
29 #include "wine/library.h"
30 #include "wine/list.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
35
36 #define MIN_ID_LEN     4
37
38 struct mount_point
39 {
40     struct list    entry;   /* entry in mount points list */
41     DEVICE_OBJECT *device;  /* disk device */
42     UNICODE_STRING name;    /* device name */
43     UNICODE_STRING link;    /* DOS device symlink */
44     void          *id;      /* device unique id */
45     unsigned int   id_len;
46 };
47
48 static struct list mount_points_list = LIST_INIT(mount_points_list);
49 static HKEY mount_key;
50
51 void set_mount_point_id( struct mount_point *mount, const void *id, unsigned int id_len )
52 {
53     RtlFreeHeap( GetProcessHeap(), 0, mount->id );
54     mount->id_len = max( MIN_ID_LEN, id_len );
55     if ((mount->id = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, mount->id_len )))
56     {
57         memcpy( mount->id, id, id_len );
58         RegSetValueExW( mount_key, mount->link.Buffer, 0, REG_BINARY, mount->id, mount->id_len );
59     }
60     else mount->id_len = 0;
61 }
62
63 static struct mount_point *add_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
64                                             const WCHAR *link, const void *id, unsigned int id_len )
65 {
66     struct mount_point *mount;
67     WCHAR *str;
68     UINT len = (strlenW(link) + 1) * sizeof(WCHAR) + device_name->Length + sizeof(WCHAR);
69
70     if (!(mount = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mount) + len ))) return NULL;
71
72     str = (WCHAR *)(mount + 1);
73     strcpyW( str, link );
74     RtlInitUnicodeString( &mount->link, str );
75     str += strlenW(str) + 1;
76     memcpy( str, device_name->Buffer, device_name->Length );
77     str[device_name->Length / sizeof(WCHAR)] = 0;
78     mount->name.Buffer = str;
79     mount->name.Length = device_name->Length;
80     mount->name.MaximumLength = device_name->Length + sizeof(WCHAR);
81     mount->device = device;
82     mount->id = NULL;
83     list_add_tail( &mount_points_list, &mount->entry );
84
85     IoCreateSymbolicLink( &mount->link, device_name );
86     set_mount_point_id( mount, id, id_len );
87
88     TRACE( "created %s id %s for %s\n", debugstr_w(mount->link.Buffer),
89            debugstr_a(mount->id), debugstr_w(mount->name.Buffer) );
90     return mount;
91 }
92
93 /* create the DosDevices mount point symlink for a new device */
94 struct mount_point *add_dosdev_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
95                                             int drive, const void *id, unsigned int id_len )
96 {
97     static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
98     WCHAR link[sizeof(driveW)];
99
100     sprintfW( link, driveW, 'A' + drive );
101     return add_mount_point( device, device_name, link, id, id_len );
102 }
103
104 /* create the Volume mount point symlink for a new device */
105 struct mount_point *add_volume_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
106                                             int drive, const void *id, unsigned int id_len )
107 {
108     static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
109                                     '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
110                                     '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
111                                     '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
112     WCHAR link[sizeof(volumeW)];
113     GUID guid;
114
115     memset( &guid, 0, sizeof(guid) );  /* FIXME */
116     guid.Data4[7] = 'A' + drive;
117     sprintfW( link, volumeW, guid.Data1, guid.Data2, guid.Data3,
118               guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
119               guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
120     return add_mount_point( device, device_name, link, id, id_len );
121 }
122
123 /* delete the mount point symlinks when a device goes away */
124 void delete_mount_point( struct mount_point *mount )
125 {
126     TRACE( "deleting %s\n", debugstr_w(mount->link.Buffer) );
127     list_remove( &mount->entry );
128     RegDeleteValueW( mount_key, mount->link.Buffer );
129     IoDeleteSymbolicLink( &mount->link );
130     RtlFreeHeap( GetProcessHeap(), 0, mount->id );
131     RtlFreeHeap( GetProcessHeap(), 0, mount );
132 }
133
134 /* check if a given mount point matches the requested specs */
135 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
136 {
137     if (spec->SymbolicLinkNameOffset)
138     {
139         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
140         if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
141         if (memicmpW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
142             return FALSE;
143     }
144     if (spec->DeviceNameOffset)
145     {
146         const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
147         if (spec->DeviceNameLength != mount->name.Length) return FALSE;
148         if (memicmpW( name, mount->name.Buffer, mount->name.Length/sizeof(WCHAR)))
149             return FALSE;
150     }
151     if (spec->UniqueIdOffset)
152     {
153         const void *id = ((const char *)spec + spec->UniqueIdOffset);
154         if (spec->UniqueIdLength != mount->id_len) return FALSE;
155         if (memcmp( id, mount->id, mount->id_len )) return FALSE;
156     }
157     return TRUE;
158 }
159
160 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
161 static NTSTATUS query_mount_points( const void *in_buff, SIZE_T insize,
162                                     void *out_buff, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
163 {
164     UINT count, pos, size;
165     const MOUNTMGR_MOUNT_POINT *input = in_buff;
166     MOUNTMGR_MOUNT_POINTS *info = out_buff;
167     struct mount_point *mount;
168
169     /* sanity checks */
170     if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
171         input->UniqueIdOffset + input->UniqueIdLength > insize ||
172         input->DeviceNameOffset + input->DeviceNameLength > insize ||
173         input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
174         input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
175         input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
176         return STATUS_INVALID_PARAMETER;
177
178     count = size = 0;
179     LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
180     {
181         if (!matching_mount_point( mount, input )) continue;
182         size += mount->name.Length;
183         size += mount->link.Length;
184         size += mount->id_len;
185         size = (size + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
186         count++;
187     }
188     pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[count] );
189     size += pos;
190
191     if (size > outsize)
192     {
193         if (size >= sizeof(info->Size)) info->Size = size;
194         iosb->Information = sizeof(info->Size);
195         return STATUS_MORE_ENTRIES;
196     }
197
198     info->NumberOfMountPoints = count;
199     count = 0;
200     LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
201     {
202         if (!matching_mount_point( mount, input )) continue;
203
204         info->MountPoints[count].DeviceNameOffset = pos;
205         info->MountPoints[count].DeviceNameLength = mount->name.Length;
206         memcpy( (char *)out_buff + pos, mount->name.Buffer, mount->name.Length );
207         pos += mount->name.Length;
208
209         info->MountPoints[count].SymbolicLinkNameOffset = pos;
210         info->MountPoints[count].SymbolicLinkNameLength = mount->link.Length;
211         memcpy( (char *)out_buff + pos, mount->link.Buffer, mount->link.Length );
212         pos += mount->link.Length;
213
214         info->MountPoints[count].UniqueIdOffset = pos;
215         info->MountPoints[count].UniqueIdLength = mount->id_len;
216         memcpy( (char *)out_buff + pos, mount->id, mount->id_len );
217         pos += mount->id_len;
218         pos = (pos + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
219         count++;
220     }
221     info->Size = pos;
222     iosb->Information = pos;
223     return STATUS_SUCCESS;
224 }
225
226 /* implementation of IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE */
227 static NTSTATUS define_unix_drive( const void *in_buff, SIZE_T insize )
228 {
229     const struct mountmgr_unix_drive *input = in_buff;
230     const char *mount_point = NULL, *device = NULL;
231     unsigned int i;
232     WCHAR letter = tolowerW( input->letter );
233
234     if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
235     if (input->type > DRIVE_RAMDISK) return STATUS_INVALID_PARAMETER;
236     if (input->mount_point_offset > insize || input->device_offset > insize)
237         return STATUS_INVALID_PARAMETER;
238
239     /* make sure string are null-terminated */
240     if (input->mount_point_offset)
241     {
242         mount_point = (const char *)in_buff + input->mount_point_offset;
243         for (i = input->mount_point_offset; i < insize; i++)
244             if (!*((const char *)in_buff + i)) break;
245         if (i >= insize) return STATUS_INVALID_PARAMETER;
246     }
247     if (input->device_offset)
248     {
249         device = (const char *)in_buff + input->device_offset;
250         for (i = input->device_offset; i < insize; i++)
251             if (!*((const char *)in_buff + i)) break;
252         if (i >= insize) return STATUS_INVALID_PARAMETER;
253     }
254
255     if (input->type != DRIVE_NO_ROOT_DIR)
256     {
257         TRACE( "defining %c: dev %s mount %s type %u\n",
258                letter, debugstr_a(device), debugstr_a(mount_point), input->type );
259         return add_dos_device( letter - 'a', NULL, device, mount_point, input->type );
260     }
261     else
262     {
263         TRACE( "removing %c:\n", letter );
264         return remove_dos_device( letter - 'a', NULL );
265     }
266 }
267
268 /* implementation of IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE */
269 static NTSTATUS query_unix_drive( const void *in_buff, SIZE_T insize,
270                                   void *out_buff, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
271 {
272     const struct mountmgr_unix_drive *input = in_buff;
273     struct mountmgr_unix_drive *output = out_buff;
274     const char *device, *mount_point;
275     int letter = tolowerW( input->letter );
276     NTSTATUS status;
277     DWORD size, type;
278     char *ptr;
279
280     if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
281
282     if ((status = query_dos_device( letter - 'a', &type, &device, &mount_point ))) return status;
283
284     size = sizeof(*output);
285     if (device) size += strlen(device) + 1;
286     if (mount_point) size += strlen(mount_point) + 1;
287
288     if (size > outsize)
289     {
290         if (size >= FIELD_OFFSET( struct mountmgr_unix_drive, size ) + sizeof(output->size))
291             output->size = size;
292         iosb->Information = FIELD_OFFSET( struct mountmgr_unix_drive, size ) + sizeof(output->size);
293         return STATUS_MORE_ENTRIES;
294     }
295     output->size = size;
296     output->letter = letter;
297     output->type = type;
298     ptr = (char *)(output + 1);
299
300     if (mount_point)
301     {
302         output->mount_point_offset = ptr - (char *)output;
303         strcpy( ptr, mount_point );
304         ptr += strlen(ptr) + 1;
305     }
306     else output->mount_point_offset = 0;
307
308     if (device)
309     {
310         output->device_offset = ptr - (char *)output;
311         strcpy( ptr, device );
312         ptr += strlen(ptr) + 1;
313     }
314     else output->device_offset = 0;
315
316     TRACE( "returning %c: dev %s mount %s type %u\n",
317            letter, debugstr_a(device), debugstr_a(mount_point), type );
318
319     iosb->Information = ptr - (char *)output;
320     return STATUS_SUCCESS;
321 }
322
323 /* handler for ioctls on the mount manager device */
324 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
325 {
326     IO_STACK_LOCATION *irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
327
328     TRACE( "ioctl %x insize %u outsize %u\n",
329            irpsp->Parameters.DeviceIoControl.IoControlCode,
330            irpsp->Parameters.DeviceIoControl.InputBufferLength,
331            irpsp->Parameters.DeviceIoControl.OutputBufferLength );
332
333     switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
334     {
335     case IOCTL_MOUNTMGR_QUERY_POINTS:
336         if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
337             return STATUS_INVALID_PARAMETER;
338         irp->IoStatus.u.Status = query_mount_points( irpsp->Parameters.DeviceIoControl.Type3InputBuffer,
339                                                      irpsp->Parameters.DeviceIoControl.InputBufferLength,
340                                                      irp->MdlAddress->StartVa,
341                                                      irpsp->Parameters.DeviceIoControl.OutputBufferLength,
342                                                      &irp->IoStatus );
343         break;
344     case IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE:
345         if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
346             return STATUS_INVALID_PARAMETER;
347         irp->IoStatus.Information = 0;
348         irp->IoStatus.u.Status = define_unix_drive( irpsp->Parameters.DeviceIoControl.Type3InputBuffer,
349                                                     irpsp->Parameters.DeviceIoControl.InputBufferLength );
350         break;
351     case IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE:
352         if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
353             return STATUS_INVALID_PARAMETER;
354         irp->IoStatus.u.Status = query_unix_drive( irpsp->Parameters.DeviceIoControl.Type3InputBuffer,
355                                                    irpsp->Parameters.DeviceIoControl.InputBufferLength,
356                                                    irp->MdlAddress->StartVa,
357                                                    irpsp->Parameters.DeviceIoControl.OutputBufferLength,
358                                                    &irp->IoStatus );
359         break;
360     default:
361         FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
362         irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
363         break;
364     }
365     return irp->IoStatus.u.Status;
366 }
367
368 /* main entry point for the mount point manager driver */
369 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
370 {
371     static const WCHAR mounted_devicesW[] = {'S','y','s','t','e','m','\\','M','o','u','n','t','e','d','D','e','v','i','c','e','s',0};
372     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};
373     static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
374     static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
375
376     UNICODE_STRING nameW, linkW;
377     DEVICE_OBJECT *device;
378     NTSTATUS status;
379
380     TRACE( "%s\n", debugstr_w(path->Buffer) );
381
382     driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
383
384     RtlInitUnicodeString( &nameW, device_mountmgrW );
385     RtlInitUnicodeString( &linkW, link_mountmgrW );
386     if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
387         status = IoCreateSymbolicLink( &linkW, &nameW );
388     if (status)
389     {
390         FIXME( "failed to create device error %x\n", status );
391         return status;
392     }
393
394     RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
395                      REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
396
397     RtlInitUnicodeString( &nameW, harddiskW );
398     status = IoCreateDriver( &nameW, harddisk_driver_entry );
399
400     initialize_hal();
401     initialize_diskarbitration();
402
403     return status;
404 }