wined3d: Get rid of the "render_to_fbo" field from the wined3d_swapchain structure.
[wine.git] / dlls / mountmgr.sys / mountmgr.c
blob2f669fcc868ba1132c558692f8a8a588244e9ba7
1 /*
2 * Mount manager service implementation
4 * Copyright 2008 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #ifdef __APPLE__
25 #include <CoreFoundation/CFString.h>
26 #define LoadResource mac_LoadResource
27 #define GetCurrentThread mac_GetCurrentThread
28 #include <CoreServices/CoreServices.h>
29 #undef LoadResource
30 #undef GetCurrentThread
31 #endif
33 #include <stdarg.h>
34 #include <unistd.h>
35 #ifdef HAVE_SYS_STAT_H
36 #include <sys/stat.h>
37 #endif
39 #define NONAMELESSUNION
41 #include "mountmgr.h"
42 #include "winreg.h"
43 #include "wine/list.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
49 #define MIN_ID_LEN 4
51 struct mount_point
53 struct list entry; /* entry in mount points list */
54 DEVICE_OBJECT *device; /* disk device */
55 UNICODE_STRING name; /* device name */
56 UNICODE_STRING link; /* DOS device symlink */
57 void *id; /* device unique id */
58 unsigned int id_len;
61 static struct list mount_points_list = LIST_INIT(mount_points_list);
62 static HKEY mount_key;
64 void set_mount_point_id( struct mount_point *mount, const void *id, unsigned int id_len )
66 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
67 mount->id_len = max( MIN_ID_LEN, id_len );
68 if ((mount->id = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, mount->id_len )))
70 memcpy( mount->id, id, id_len );
71 RegSetValueExW( mount_key, mount->link.Buffer, 0, REG_BINARY, mount->id, mount->id_len );
73 else mount->id_len = 0;
76 static struct mount_point *add_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
77 const WCHAR *link )
79 struct mount_point *mount;
80 WCHAR *str;
81 UINT len = (strlenW(link) + 1) * sizeof(WCHAR) + device_name->Length + sizeof(WCHAR);
83 if (!(mount = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mount) + len ))) return NULL;
85 str = (WCHAR *)(mount + 1);
86 strcpyW( str, link );
87 RtlInitUnicodeString( &mount->link, str );
88 str += strlenW(str) + 1;
89 memcpy( str, device_name->Buffer, device_name->Length );
90 str[device_name->Length / sizeof(WCHAR)] = 0;
91 mount->name.Buffer = str;
92 mount->name.Length = device_name->Length;
93 mount->name.MaximumLength = device_name->Length + sizeof(WCHAR);
94 mount->device = device;
95 mount->id = NULL;
96 list_add_tail( &mount_points_list, &mount->entry );
98 IoCreateSymbolicLink( &mount->link, device_name );
100 TRACE( "created %s id %s for %s\n", debugstr_w(mount->link.Buffer),
101 debugstr_a(mount->id), debugstr_w(mount->name.Buffer) );
102 return mount;
105 /* create the DosDevices mount point symlink for a new device */
106 struct mount_point *add_dosdev_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name, int drive )
108 static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
109 WCHAR link[sizeof(driveW)];
111 sprintfW( link, driveW, 'A' + drive );
112 return add_mount_point( device, device_name, link );
115 /* create the Volume mount point symlink for a new device */
116 struct mount_point *add_volume_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
117 const GUID *guid )
119 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
120 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
121 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
122 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
123 WCHAR link[sizeof(volumeW)];
125 sprintfW( link, volumeW, guid->Data1, guid->Data2, guid->Data3,
126 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
127 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
128 return add_mount_point( device, device_name, link );
131 /* delete the mount point symlinks when a device goes away */
132 void delete_mount_point( struct mount_point *mount )
134 TRACE( "deleting %s\n", debugstr_w(mount->link.Buffer) );
135 list_remove( &mount->entry );
136 RegDeleteValueW( mount_key, mount->link.Buffer );
137 IoDeleteSymbolicLink( &mount->link );
138 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
139 RtlFreeHeap( GetProcessHeap(), 0, mount );
142 /* check if a given mount point matches the requested specs */
143 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
145 if (spec->SymbolicLinkNameOffset)
147 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
148 if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
149 if (strncmpiW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
150 return FALSE;
152 if (spec->DeviceNameOffset)
154 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
155 if (spec->DeviceNameLength != mount->name.Length) return FALSE;
156 if (strncmpiW( name, mount->name.Buffer, mount->name.Length/sizeof(WCHAR)))
157 return FALSE;
159 if (spec->UniqueIdOffset)
161 const void *id = ((const char *)spec + spec->UniqueIdOffset);
162 if (spec->UniqueIdLength != mount->id_len) return FALSE;
163 if (memcmp( id, mount->id, mount->id_len )) return FALSE;
165 return TRUE;
168 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
169 static NTSTATUS query_mount_points( void *buff, SIZE_T insize,
170 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
172 UINT count, pos, size;
173 MOUNTMGR_MOUNT_POINT *input = buff;
174 MOUNTMGR_MOUNT_POINTS *info;
175 struct mount_point *mount;
177 if (insize < sizeof(*input) ||
178 outsize < sizeof(*info) ||
179 input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
180 input->UniqueIdOffset + input->UniqueIdLength > insize ||
181 input->DeviceNameOffset + input->DeviceNameLength > insize ||
182 input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
183 input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
184 input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
185 return STATUS_INVALID_PARAMETER;
187 count = size = 0;
188 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
190 if (!matching_mount_point( mount, input )) continue;
191 size += mount->name.Length;
192 size += mount->link.Length;
193 size += mount->id_len;
194 size = (size + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
195 count++;
197 pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[count] );
198 size += pos;
200 if (size > outsize)
202 info = buff;
203 info->Size = size;
204 iosb->Information = sizeof(info->Size);
205 return STATUS_BUFFER_OVERFLOW;
208 input = HeapAlloc( GetProcessHeap(), 0, insize );
209 if (!input)
210 return STATUS_NO_MEMORY;
211 memcpy( input, buff, insize );
212 info = buff;
214 info->NumberOfMountPoints = count;
215 count = 0;
216 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
218 if (!matching_mount_point( mount, input )) continue;
220 info->MountPoints[count].DeviceNameOffset = pos;
221 info->MountPoints[count].DeviceNameLength = mount->name.Length;
222 memcpy( (char *)buff + pos, mount->name.Buffer, mount->name.Length );
223 pos += mount->name.Length;
225 info->MountPoints[count].SymbolicLinkNameOffset = pos;
226 info->MountPoints[count].SymbolicLinkNameLength = mount->link.Length;
227 memcpy( (char *)buff + pos, mount->link.Buffer, mount->link.Length );
228 pos += mount->link.Length;
230 info->MountPoints[count].UniqueIdOffset = pos;
231 info->MountPoints[count].UniqueIdLength = mount->id_len;
232 memcpy( (char *)buff + pos, mount->id, mount->id_len );
233 pos += mount->id_len;
234 pos = (pos + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
235 count++;
237 info->Size = pos;
238 iosb->Information = pos;
239 HeapFree( GetProcessHeap(), 0, input );
240 return STATUS_SUCCESS;
243 /* implementation of IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE */
244 static NTSTATUS define_unix_drive( const void *in_buff, SIZE_T insize )
246 const struct mountmgr_unix_drive *input = in_buff;
247 const char *mount_point = NULL, *device = NULL;
248 unsigned int i;
249 WCHAR letter = tolowerW( input->letter );
251 if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
252 if (input->type > DRIVE_RAMDISK) return STATUS_INVALID_PARAMETER;
253 if (input->mount_point_offset > insize || input->device_offset > insize)
254 return STATUS_INVALID_PARAMETER;
256 /* make sure string are null-terminated */
257 if (input->mount_point_offset)
259 mount_point = (const char *)in_buff + input->mount_point_offset;
260 for (i = input->mount_point_offset; i < insize; i++)
261 if (!*((const char *)in_buff + i)) break;
262 if (i >= insize) return STATUS_INVALID_PARAMETER;
264 if (input->device_offset)
266 device = (const char *)in_buff + input->device_offset;
267 for (i = input->device_offset; i < insize; i++)
268 if (!*((const char *)in_buff + i)) break;
269 if (i >= insize) return STATUS_INVALID_PARAMETER;
272 if (input->type != DRIVE_NO_ROOT_DIR)
274 enum device_type type = DEVICE_UNKNOWN;
276 TRACE( "defining %c: dev %s mount %s type %u\n",
277 letter, debugstr_a(device), debugstr_a(mount_point), input->type );
278 switch (input->type)
280 case DRIVE_REMOVABLE: type = (letter >= 'c') ? DEVICE_HARDDISK : DEVICE_FLOPPY; break;
281 case DRIVE_REMOTE: type = DEVICE_NETWORK; break;
282 case DRIVE_CDROM: type = DEVICE_CDROM; break;
283 case DRIVE_RAMDISK: type = DEVICE_RAMDISK; break;
284 case DRIVE_FIXED: type = DEVICE_HARDDISK_VOL; break;
286 return add_dos_device( letter - 'a', NULL, device, mount_point, type, NULL, NULL );
288 else
290 TRACE( "removing %c:\n", letter );
291 return remove_dos_device( letter - 'a', NULL );
295 /* implementation of IOCTL_MOUNTMGR_DEFINE_SHELL_FOLDER */
296 static NTSTATUS define_shell_folder( const void *in_buff, SIZE_T insize )
298 const struct mountmgr_shell_folder *input = in_buff;
299 const char *link = NULL;
300 OBJECT_ATTRIBUTES attr;
301 UNICODE_STRING name;
302 NTSTATUS status;
303 ULONG size = 256;
304 char *buffer, *backup = NULL;
305 struct stat st;
306 unsigned int i;
308 if (input->folder_offset >= insize || input->folder_size > insize - input->folder_offset ||
309 input->symlink_offset >= insize)
310 return STATUS_INVALID_PARAMETER;
312 /* make sure string is null-terminated */
313 if (input->symlink_offset)
315 link = (const char *)in_buff + input->symlink_offset;
316 for (i = input->symlink_offset; i < insize; i++)
317 if (!*((const char *)in_buff + i)) break;
318 if (i >= insize) return STATUS_INVALID_PARAMETER;
319 if (!link[0]) link = NULL;
322 /* ignore nonexistent link targets */
323 if (link && stat( link, &st )) return STATUS_OBJECT_NAME_NOT_FOUND;
325 name.Buffer = (WCHAR *)((char *)in_buff + input->folder_offset);
326 name.Length = input->folder_size;
327 InitializeObjectAttributes( &attr, &name, 0, 0, NULL );
329 for (;;)
331 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size ))) return STATUS_NO_MEMORY;
332 status = wine_nt_to_unix_file_name( &attr, buffer, &size, FILE_OPEN );
333 if (!status) break;
334 HeapFree( GetProcessHeap(), 0, buffer );
335 if (status != STATUS_BUFFER_TOO_SMALL) return status;
338 if (!(backup = HeapAlloc( GetProcessHeap(), 0, strlen(buffer) + sizeof(".backup" ) )))
340 status = STATUS_NO_MEMORY;
341 goto done;
343 strcpy( backup, buffer );
344 strcat( backup, ".backup" );
346 if (!lstat( buffer, &st )) /* move old folder/link out of the way */
348 if (S_ISLNK( st.st_mode ))
350 unlink( buffer );
352 else if (link && S_ISDIR( st.st_mode ))
354 if (rmdir( buffer )) /* non-empty dir, try to make a backup */
356 if (!backup || rename( buffer, backup ))
358 status = STATUS_OBJECT_NAME_COLLISION;
359 goto done;
363 else goto done; /* nothing to do, folder already exists */
366 if (link) symlink( link, buffer );
367 else
369 if (backup && !lstat( backup, &st ) && S_ISDIR( st.st_mode )) rename( backup, buffer );
370 else mkdir( buffer, 0777 );
373 done:
374 HeapFree( GetProcessHeap(), 0, buffer );
375 HeapFree( GetProcessHeap(), 0, backup );
376 return status;
379 /* implementation of IOCTL_MOUNTMGR_QUERY_SHELL_FOLDER */
380 static NTSTATUS query_shell_folder( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
382 char *output = buff;
383 OBJECT_ATTRIBUTES attr;
384 UNICODE_STRING name;
385 NTSTATUS status;
386 ULONG size = 256;
387 char *buffer;
388 int ret;
390 name.Buffer = buff;
391 name.Length = insize;
392 InitializeObjectAttributes( &attr, &name, 0, 0, NULL );
394 for (;;)
396 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size ))) return STATUS_NO_MEMORY;
397 status = wine_nt_to_unix_file_name( &attr, buffer, &size, FILE_OPEN );
398 if (!status) break;
399 HeapFree( GetProcessHeap(), 0, buffer );
400 if (status != STATUS_BUFFER_TOO_SMALL) return status;
402 ret = readlink( buffer, output, outsize - 1 );
403 if (ret >= 0)
405 output[ret] = 0;
406 iosb->Information = ret + 1;
408 else status = STATUS_OBJECT_NAME_NOT_FOUND;
410 HeapFree( GetProcessHeap(), 0, buffer );
411 return status;
414 /* implementation of IOCTL_MOUNTMGR_QUERY_DHCP_REQUEST_PARAMS */
415 static void WINAPI query_dhcp_request_params( TP_CALLBACK_INSTANCE *instance, void *context )
417 IRP *irp = context;
418 struct mountmgr_dhcp_request_params *query = irp->AssociatedIrp.SystemBuffer;
419 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
420 SIZE_T insize = irpsp->Parameters.DeviceIoControl.InputBufferLength;
421 SIZE_T outsize = irpsp->Parameters.DeviceIoControl.OutputBufferLength;
422 ULONG i, offset = 0;
424 /* sanity checks */
425 if (FIELD_OFFSET(struct mountmgr_dhcp_request_params, params[query->count]) > insize)
427 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
428 goto err;
431 for (i = 0; i < query->count; i++)
432 if (query->params[i].offset + query->params[i].size > insize)
434 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
435 goto err;
439 offset = FIELD_OFFSET(struct mountmgr_dhcp_request_params, params[query->count]);
440 for (i = 0; i < query->count; i++)
442 offset += get_dhcp_request_param( &query->adapter, &query->params[i], (char *)query, offset, outsize - offset );
443 if (offset > outsize)
445 if (offset >= sizeof(query->size)) query->size = offset;
446 offset = sizeof(query->size);
447 irp->IoStatus.u.Status = STATUS_BUFFER_OVERFLOW;
448 goto err;
451 irp->IoStatus.u.Status = STATUS_SUCCESS;
453 err:
454 irp->IoStatus.Information = offset;
455 IoCompleteRequest( irp, IO_NO_INCREMENT );
458 /* implementation of Wine extension to use host APIs to find symbol file by GUID */
459 #ifdef __APPLE__
460 static void WINAPI query_symbol_file( TP_CALLBACK_INSTANCE *instance, void *context )
462 IRP *irp = context;
463 MOUNTMGR_TARGET_NAME *result;
464 CFStringRef query_cfstring;
465 WCHAR *filename, *unix_buf = NULL;
466 ANSI_STRING unix_path;
467 UNICODE_STRING path;
468 MDQueryRef mdquery;
469 const GUID *id;
470 size_t size;
471 NTSTATUS status = STATUS_NO_MEMORY;
473 static const WCHAR formatW[] = { 'c','o','m','_','a','p','p','l','e','_','x','c','o','d','e',
474 '_','d','s','y','m','_','u','u','i','d','s',' ','=','=',' ',
475 '"','%','0','8','X','-','%','0','4','X','-',
476 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
477 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
478 '%','0','2','X','%','0','2','X','"',0 };
479 WCHAR query_string[ARRAY_SIZE(formatW)];
481 id = irp->AssociatedIrp.SystemBuffer;
482 sprintfW( query_string, formatW, id->Data1, id->Data2, id->Data3,
483 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
484 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
485 if (!(query_cfstring = CFStringCreateWithCharacters(NULL, query_string, lstrlenW(query_string)))) goto done;
487 mdquery = MDQueryCreate(NULL, query_cfstring, NULL, NULL);
488 CFRelease(query_cfstring);
489 if (!mdquery) goto done;
491 MDQuerySetMaxCount(mdquery, 1);
492 TRACE("Executing %s\n", debugstr_w(query_string));
493 if (MDQueryExecute(mdquery, kMDQuerySynchronous))
495 if (MDQueryGetResultCount(mdquery) >= 1)
497 MDItemRef item = (MDItemRef)MDQueryGetResultAtIndex(mdquery, 0);
498 CFStringRef item_path = MDItemCopyAttribute(item, kMDItemPath);
500 if (item_path)
502 CFIndex item_path_len = CFStringGetLength(item_path);
503 if ((unix_buf = HeapAlloc(GetProcessHeap(), 0, (item_path_len + 1) * sizeof(WCHAR))))
505 CFStringGetCharacters(item_path, CFRangeMake(0, item_path_len), unix_buf);
506 unix_buf[item_path_len] = 0;
507 TRACE("found %s\n", debugstr_w(unix_buf));
509 CFRelease(item_path);
512 else status = STATUS_NO_MORE_ENTRIES;
514 CFRelease(mdquery);
515 if (!unix_buf) goto done;
517 RtlInitUnicodeString( &path, unix_buf );
518 status = RtlUnicodeStringToAnsiString( &unix_path, &path, TRUE );
519 HeapFree( GetProcessHeap(), 0, unix_buf );
520 if (status) goto done;
522 filename = wine_get_dos_file_name( unix_path.Buffer );
523 RtlFreeAnsiString( &unix_path );
524 if (!filename)
526 status = STATUS_NO_SUCH_FILE;
527 goto done;
529 result = irp->AssociatedIrp.SystemBuffer;
530 result->DeviceNameLength = lstrlenW(filename) * sizeof(WCHAR);
531 size = FIELD_OFFSET(MOUNTMGR_TARGET_NAME, DeviceName[lstrlenW(filename)]);
532 if (size <= IoGetCurrentIrpStackLocation(irp)->Parameters.DeviceIoControl.OutputBufferLength)
534 memcpy( result->DeviceName, filename, lstrlenW(filename) * sizeof(WCHAR) );
535 irp->IoStatus.Information = size;
536 status = STATUS_SUCCESS;
538 else
540 irp->IoStatus.Information = sizeof(*result);
541 status = STATUS_BUFFER_OVERFLOW;
543 RtlFreeHeap( GetProcessHeap(), 0, filename );
545 done:
546 irp->IoStatus.u.Status = status;
547 IoCompleteRequest( irp, IO_NO_INCREMENT );
550 static inline BOOL check_credential_string( const void *buf, SIZE_T buflen, ULONG size, ULONG offset )
552 const WCHAR *ptr = buf;
553 if (!size || size % sizeof(WCHAR) || offset + size > buflen || ptr[(offset + size) / sizeof(WCHAR) - 1]) return FALSE;
554 return TRUE;
557 static SecKeychainItemRef find_credential( const WCHAR *name )
559 int status;
560 SecKeychainSearchRef search;
561 SecKeychainItemRef item;
563 status = SecKeychainSearchCreateFromAttributes( NULL, kSecGenericPasswordItemClass, NULL, &search );
564 if (status != noErr) return NULL;
566 while (SecKeychainSearchCopyNext( search, &item ) == noErr)
568 SecKeychainAttributeInfo info;
569 SecKeychainAttributeList *attr_list;
570 UInt32 info_tags[] = { kSecServiceItemAttr };
571 WCHAR *itemname;
572 int len;
574 info.count = ARRAY_SIZE(info_tags);
575 info.tag = info_tags;
576 info.format = NULL;
577 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, NULL, NULL );
578 if (status != noErr)
580 WARN( "SecKeychainItemCopyAttributesAndData returned status %d\n", status );
581 continue;
583 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServiceItemAttr)
585 CFRelease( item );
586 continue;
588 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, NULL, 0 );
589 if (!(itemname = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
591 CFRelease( item );
592 CFRelease( search );
593 return NULL;
595 MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, itemname, len );
596 itemname[len] = 0;
597 if (strcmpiW( itemname, name ))
599 CFRelease( item );
600 RtlFreeHeap( GetProcessHeap(), 0, itemname );
601 continue;
603 RtlFreeHeap( GetProcessHeap(), 0, itemname );
604 SecKeychainItemFreeAttributesAndData( attr_list, NULL );
605 CFRelease( search );
606 return item;
609 CFRelease( search );
610 return NULL;
613 static NTSTATUS fill_credential( SecKeychainItemRef item, BOOL require_password, void *buf, SIZE_T data_offset,
614 SIZE_T buflen, SIZE_T *retlen )
616 struct mountmgr_credential *cred = buf;
617 int status, len;
618 SIZE_T size;
619 UInt32 i, cred_blob_len = 0;
620 void *cred_blob;
621 WCHAR *ptr;
622 BOOL user_name_present = FALSE;
623 SecKeychainAttributeInfo info;
624 SecKeychainAttributeList *attr_list = NULL;
625 UInt32 info_tags[] = { kSecServiceItemAttr, kSecAccountItemAttr, kSecCommentItemAttr, kSecCreationDateItemAttr };
627 info.count = ARRAY_SIZE(info_tags);
628 info.tag = info_tags;
629 info.format = NULL;
630 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, &cred_blob_len, &cred_blob );
631 if (status == errSecAuthFailed && !require_password)
633 cred_blob_len = 0;
634 cred_blob = NULL;
635 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, &cred_blob_len, NULL );
637 if (status != noErr)
639 WARN( "SecKeychainItemCopyAttributesAndData returned status %d\n", status );
640 return STATUS_NOT_FOUND;
642 for (i = 0; i < attr_list->count; i++)
644 if (attr_list->attr[i].tag == kSecAccountItemAttr && attr_list->attr[i].data)
646 user_name_present = TRUE;
647 break;
650 if (!user_name_present)
652 WARN( "no kSecAccountItemAttr for item\n" );
653 SecKeychainItemFreeAttributesAndData( attr_list, cred_blob );
654 return STATUS_NOT_FOUND;
657 *retlen = sizeof(*cred);
658 for (i = 0; i < attr_list->count; i++)
660 switch (attr_list->attr[i].tag)
662 case kSecServiceItemAttr:
663 TRACE( "kSecServiceItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
664 if (cred) cred->targetname_offset = cred->targetname_size = 0;
665 if (!attr_list->attr[i].data) continue;
667 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, NULL, 0 );
668 size = (len + 1) * sizeof(WCHAR);
669 if (cred && *retlen + size <= buflen)
671 cred->targetname_offset = data_offset;
672 cred->targetname_size = size;
673 ptr = (WCHAR *)((char *)cred + cred->targetname_offset);
674 MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, ptr, len );
675 ptr[len] = 0;
676 data_offset += size;
678 *retlen += size;
679 break;
680 case kSecAccountItemAttr:
682 TRACE( "kSecAccountItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
683 if (cred) cred->username_offset = cred->username_size = 0;
684 if (!attr_list->attr[i].data) continue;
686 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, NULL, 0 );
687 size = (len + 1) * sizeof(WCHAR);
688 if (cred && *retlen + size <= buflen)
690 cred->username_offset = data_offset;
691 cred->username_size = size;
692 ptr = (WCHAR *)((char *)cred + cred->username_offset);
693 MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, ptr, len );
694 ptr[len] = 0;
695 data_offset += size;
697 *retlen += size;
698 break;
700 case kSecCommentItemAttr:
701 TRACE( "kSecCommentItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
702 if (cred) cred->comment_offset = cred->comment_size = 0;
703 if (!attr_list->attr[i].data) continue;
705 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, NULL, 0 );
706 size = (len + 1) * sizeof(WCHAR);
707 if (cred && *retlen + size <= buflen)
709 cred->comment_offset = data_offset;
710 cred->comment_size = size;
711 ptr = (WCHAR *)((char *)cred + cred->comment_offset);
712 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, ptr, len );
713 ptr[len] = 0;
714 data_offset += size;
716 *retlen += size;
717 break;
718 case kSecCreationDateItemAttr:
720 LARGE_INTEGER wintime;
721 struct tm tm;
722 time_t time;
724 TRACE( "kSecCreationDateItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
725 if (cred) cred->last_written.dwLowDateTime = cred->last_written.dwHighDateTime = 0;
726 if (!attr_list->attr[i].data) continue;
728 if (cred)
730 memset( &tm, 0, sizeof(tm) );
731 strptime( attr_list->attr[i].data, "%Y%m%d%H%M%SZ", &tm );
732 time = mktime( &tm );
733 RtlSecondsSince1970ToTime( time, &wintime );
734 cred->last_written.dwLowDateTime = wintime.u.LowPart;
735 cred->last_written.dwHighDateTime = wintime.u.HighPart;
737 break;
739 default:
740 FIXME( "unhandled attribute %u\n", (unsigned)attr_list->attr[i].tag );
741 break;
745 if (cred)
747 if (*retlen + cred_blob_len <= buflen)
749 len = MultiByteToWideChar( CP_UTF8, 0, cred_blob, cred_blob_len, NULL, 0 );
750 cred->blob_offset = data_offset;
751 cred->blob_size = len * sizeof(WCHAR);
752 ptr = (WCHAR *)((char *)cred + cred->blob_offset);
753 MultiByteToWideChar( CP_UTF8, 0, cred_blob, cred_blob_len, ptr, len );
755 else cred->blob_offset = cred->blob_size = 0;
757 *retlen += cred_blob_len;
759 if (attr_list) SecKeychainItemFreeAttributesAndData( attr_list, cred_blob );
760 return STATUS_SUCCESS;
763 static NTSTATUS read_credential( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
765 struct mountmgr_credential *cred = buff;
766 const WCHAR *targetname;
767 SecKeychainItemRef item;
768 SIZE_T size;
769 NTSTATUS status;
771 if (!check_credential_string( buff, insize, cred->targetname_size, cred->targetname_offset ))
772 return STATUS_INVALID_PARAMETER;
773 targetname = (const WCHAR *)((const char *)cred + cred->targetname_offset);
775 if (!(item = find_credential( targetname ))) return STATUS_NOT_FOUND;
777 status = fill_credential( item, TRUE, cred, sizeof(*cred), outsize, &size );
778 CFRelease( item );
779 if (status != STATUS_SUCCESS) return status;
781 iosb->Information = size;
782 return (size > outsize) ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
785 static NTSTATUS write_credential( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
787 const struct mountmgr_credential *cred = buff;
788 int status, len, len_password = 0;
789 const WCHAR *ptr;
790 SecKeychainItemRef keychain_item;
791 char *targetname, *username = NULL, *password = NULL;
792 SecKeychainAttribute attrs[1];
793 SecKeychainAttributeList attr_list;
794 NTSTATUS ret = STATUS_NO_MEMORY;
796 if (!check_credential_string( buff, insize, cred->targetname_size, cred->targetname_offset ) ||
797 !check_credential_string( buff, insize, cred->username_size, cred->username_offset ) ||
798 ((cred->blob_size && cred->blob_size % sizeof(WCHAR)) || cred->blob_offset + cred->blob_size > insize) ||
799 (cred->comment_size && !check_credential_string( buff, insize, cred->comment_size, cred->comment_offset )) ||
800 sizeof(*cred) + cred->targetname_size + cred->username_size + cred->blob_size + cred->comment_size > insize)
802 return STATUS_INVALID_PARAMETER;
805 ptr = (const WCHAR *)((const char *)cred + cred->targetname_offset);
806 len = WideCharToMultiByte( CP_UTF8, 0, ptr, -1, NULL, 0, NULL, NULL );
807 if (!(targetname = RtlAllocateHeap( GetProcessHeap(), 0, len ))) goto error;
808 WideCharToMultiByte( CP_UTF8, 0, ptr, -1, targetname, len, NULL, NULL );
810 ptr = (const WCHAR *)((const char *)cred + cred->username_offset);
811 len = WideCharToMultiByte( CP_UTF8, 0, ptr, -1, NULL, 0, NULL, NULL );
812 if (!(username = RtlAllocateHeap( GetProcessHeap(), 0, len ))) goto error;
813 WideCharToMultiByte( CP_UTF8, 0, ptr, -1, username, len, NULL, NULL );
815 if (cred->blob_size)
817 ptr = (const WCHAR *)((const char *)cred + cred->blob_offset);
818 len_password = WideCharToMultiByte( CP_UTF8, 0, ptr, cred->blob_size / sizeof(WCHAR), NULL, 0, NULL, NULL );
819 if (!(password = RtlAllocateHeap( GetProcessHeap(), 0, len_password ))) goto error;
820 WideCharToMultiByte( CP_UTF8, 0, ptr, cred->blob_size / sizeof(WCHAR), password, len_password, NULL, NULL );
823 TRACE("adding target %s, username %s using Keychain\n", targetname, username );
824 status = SecKeychainAddGenericPassword( NULL, strlen(targetname), targetname, strlen(username), username,
825 len_password, password, &keychain_item );
826 if (status != noErr) ERR( "SecKeychainAddGenericPassword returned %d\n", status );
827 if (status == errSecDuplicateItem)
829 status = SecKeychainFindGenericPassword( NULL, strlen(targetname), targetname, strlen(username), username, NULL,
830 NULL, &keychain_item );
831 if (status != noErr) ERR( "SecKeychainFindGenericPassword returned %d\n", status );
833 RtlFreeHeap( GetProcessHeap(), 0, username );
834 RtlFreeHeap( GetProcessHeap(), 0, targetname );
835 if (status != noErr)
837 RtlFreeHeap( GetProcessHeap(), 0, password );
838 return STATUS_UNSUCCESSFUL;
840 if (cred->comment_size)
842 attr_list.count = 1;
843 attr_list.attr = attrs;
844 attrs[0].tag = kSecCommentItemAttr;
845 ptr = (const WCHAR *)((const char *)cred + cred->comment_offset);
846 attrs[0].length = WideCharToMultiByte( CP_UTF8, 0, ptr, -1, NULL, 0, NULL, NULL );
847 if (attrs[0].length) attrs[0].length--;
848 if (!(attrs[0].data = RtlAllocateHeap( GetProcessHeap(), 0, attrs[0].length ))) goto error;
849 WideCharToMultiByte( CP_UTF8, 0, ptr, -1, attrs[0].data, attrs[0].length, NULL, NULL );
851 else
853 attr_list.count = 0;
854 attr_list.attr = NULL;
856 status = SecKeychainItemModifyAttributesAndData( keychain_item, &attr_list, cred->blob_preserve ? 0 : len_password,
857 cred->blob_preserve ? NULL : password );
859 if (cred->comment_size) RtlFreeHeap( GetProcessHeap(), 0, attrs[0].data );
860 RtlFreeHeap( GetProcessHeap(), 0, password );
861 /* FIXME: set TargetAlias attribute */
862 CFRelease( keychain_item );
863 if (status != noErr) return STATUS_UNSUCCESSFUL;
864 return STATUS_SUCCESS;
866 error:
867 RtlFreeHeap( GetProcessHeap(), 0, username );
868 RtlFreeHeap( GetProcessHeap(), 0, targetname );
869 RtlFreeHeap( GetProcessHeap(), 0, password );
870 return ret;
873 static NTSTATUS delete_credential( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
875 const struct mountmgr_credential *cred = buff;
876 const WCHAR *targetname;
877 SecKeychainItemRef item;
879 if (!check_credential_string( buff, insize, cred->targetname_size, cred->targetname_offset ))
880 return STATUS_INVALID_PARAMETER;
881 targetname = (const WCHAR *)((const char *)cred + cred->targetname_offset);
883 if (!(item = find_credential( targetname ))) return STATUS_NOT_FOUND;
885 SecKeychainItemDelete( item );
886 CFRelease( item );
887 return STATUS_SUCCESS;
890 static BOOL match_credential( void *data, UInt32 data_len, const WCHAR *filter )
892 int len;
893 WCHAR *targetname;
894 const WCHAR *p;
895 BOOL ret;
897 if (!*filter) return TRUE;
899 len = MultiByteToWideChar( CP_UTF8, 0, data, data_len, NULL, 0 );
900 if (!(targetname = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return FALSE;
901 MultiByteToWideChar( CP_UTF8, 0, data, data_len, targetname, len );
902 targetname[len] = 0;
904 TRACE( "comparing filter %s to target name %s\n", debugstr_w(filter), debugstr_w(targetname) );
906 p = strchrW( filter, '*' );
907 ret = CompareStringW( GetThreadLocale(), NORM_IGNORECASE, filter,
908 (p && !p[1]) ? p - filter : -1, targetname, (p && !p[1]) ? p - filter : -1 ) == CSTR_EQUAL;
909 RtlFreeHeap( GetProcessHeap(), 0, targetname );
910 return ret;
913 static NTSTATUS search_credentials( const WCHAR *filter, struct mountmgr_credential_list *list, SIZE_T *ret_count, SIZE_T *ret_size )
915 SecKeychainSearchRef search;
916 SecKeychainItemRef item;
917 int status;
918 ULONG i = 0;
919 SIZE_T data_offset, data_size = 0, size;
920 NTSTATUS ret = STATUS_NOT_FOUND;
922 status = SecKeychainSearchCreateFromAttributes( NULL, kSecGenericPasswordItemClass, NULL, &search );
923 if (status != noErr)
925 ERR( "SecKeychainSearchCreateFromAttributes returned status %d\n", status );
926 return STATUS_INTERNAL_ERROR;
929 while (SecKeychainSearchCopyNext( search, &item ) == noErr)
931 SecKeychainAttributeInfo info;
932 SecKeychainAttributeList *attr_list;
933 UInt32 info_tags[] = { kSecServiceItemAttr };
934 BOOL match;
936 info.count = ARRAY_SIZE(info_tags);
937 info.tag = info_tags;
938 info.format = NULL;
939 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, NULL, NULL );
940 if (status != noErr)
942 WARN( "SecKeychainItemCopyAttributesAndData returned status %d\n", status );
943 CFRelease( item );
944 continue;
946 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServiceItemAttr)
948 SecKeychainItemFreeAttributesAndData( attr_list, NULL );
949 CFRelease( item );
950 continue;
952 TRACE( "service item: %.*s\n", (int)attr_list->attr[0].length, (char *)attr_list->attr[0].data );
954 match = match_credential( attr_list->attr[0].data, attr_list->attr[0].length, filter );
955 SecKeychainItemFreeAttributesAndData( attr_list, NULL );
956 if (!match)
958 CFRelease( item );
959 continue;
962 if (!list) ret = fill_credential( item, FALSE, NULL, 0, 0, &size );
963 else
965 data_offset = FIELD_OFFSET( struct mountmgr_credential_list, creds[list->count] ) -
966 FIELD_OFFSET( struct mountmgr_credential_list, creds[i] ) + data_size;
967 ret = fill_credential( item, FALSE, &list->creds[i], data_offset, list->size - data_offset, &size );
970 CFRelease( item );
971 if (ret == STATUS_NOT_FOUND) continue;
972 if (ret != STATUS_SUCCESS) break;
973 data_size += size - sizeof(struct mountmgr_credential);
974 i++;
977 if (ret_count) *ret_count = i;
978 if (ret_size) *ret_size = FIELD_OFFSET( struct mountmgr_credential_list, creds[i] ) + data_size;
980 CFRelease( search );
981 return ret;
984 static NTSTATUS enumerate_credentials( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
986 struct mountmgr_credential_list *list = buff;
987 WCHAR *filter;
988 SIZE_T size, count;
989 Boolean saved_user_interaction_allowed;
990 NTSTATUS status;
992 if (!check_credential_string( buff, insize, list->filter_size, list->filter_offset )) return STATUS_INVALID_PARAMETER;
993 if (!(filter = strdupW( (const WCHAR *)((const char *)list + list->filter_offset) ))) return STATUS_NO_MEMORY;
995 SecKeychainGetUserInteractionAllowed( &saved_user_interaction_allowed );
996 SecKeychainSetUserInteractionAllowed( false );
998 if ((status = search_credentials( filter, NULL, &count, &size )) == STATUS_SUCCESS)
1001 if (size > outsize)
1003 if (size >= sizeof(list->size)) list->size = size;
1004 iosb->Information = sizeof(list->size);
1005 status = STATUS_BUFFER_OVERFLOW;
1007 else
1009 list->size = size;
1010 list->count = count;
1011 iosb->Information = size;
1012 status = search_credentials( filter, list, NULL, NULL );
1016 SecKeychainSetUserInteractionAllowed( saved_user_interaction_allowed );
1017 RtlFreeHeap( GetProcessHeap(), 0, filter );
1018 return status;
1020 #endif /* __APPLE__ */
1022 /* handler for ioctls on the mount manager device */
1023 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
1025 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
1026 NTSTATUS status;
1028 TRACE( "ioctl %x insize %u outsize %u\n",
1029 irpsp->Parameters.DeviceIoControl.IoControlCode,
1030 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1031 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
1033 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
1035 case IOCTL_MOUNTMGR_QUERY_POINTS:
1036 status = query_mount_points( irp->AssociatedIrp.SystemBuffer,
1037 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1038 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1039 &irp->IoStatus );
1040 break;
1041 case IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE:
1042 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
1044 status = STATUS_INVALID_PARAMETER;
1045 break;
1047 irp->IoStatus.Information = 0;
1048 status = define_unix_drive( irp->AssociatedIrp.SystemBuffer,
1049 irpsp->Parameters.DeviceIoControl.InputBufferLength );
1050 break;
1051 case IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE:
1052 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
1054 status = STATUS_INVALID_PARAMETER;
1055 break;
1057 status = query_unix_drive( irp->AssociatedIrp.SystemBuffer,
1058 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1059 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1060 &irp->IoStatus );
1061 break;
1062 case IOCTL_MOUNTMGR_DEFINE_SHELL_FOLDER:
1063 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_shell_folder))
1065 status = STATUS_INVALID_PARAMETER;
1066 break;
1068 irp->IoStatus.Information = 0;
1069 status = define_shell_folder( irp->AssociatedIrp.SystemBuffer,
1070 irpsp->Parameters.DeviceIoControl.InputBufferLength );
1071 break;
1072 case IOCTL_MOUNTMGR_QUERY_SHELL_FOLDER:
1073 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_shell_folder))
1075 status = STATUS_INVALID_PARAMETER;
1076 break;
1078 status = query_shell_folder( irp->AssociatedIrp.SystemBuffer,
1079 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1080 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1081 &irp->IoStatus );
1082 break;
1083 case IOCTL_MOUNTMGR_QUERY_DHCP_REQUEST_PARAMS:
1084 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_dhcp_request_params))
1086 status = STATUS_INVALID_PARAMETER;
1087 break;
1090 if (TrySubmitThreadpoolCallback( query_dhcp_request_params, irp, NULL ))
1091 return (irp->IoStatus.u.Status = STATUS_PENDING);
1092 status = STATUS_NO_MEMORY;
1093 break;
1094 #ifdef __APPLE__
1095 case IOCTL_MOUNTMGR_QUERY_SYMBOL_FILE:
1096 if (irpsp->Parameters.DeviceIoControl.InputBufferLength != sizeof(GUID)
1097 || irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTMGR_TARGET_NAME))
1099 status = STATUS_INVALID_PARAMETER;
1100 break;
1102 if (TrySubmitThreadpoolCallback( query_symbol_file, irp, NULL ))
1103 return (irp->IoStatus.u.Status = STATUS_PENDING);
1104 status = STATUS_NO_MEMORY;
1105 break;
1106 case IOCTL_MOUNTMGR_READ_CREDENTIAL:
1107 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential))
1109 status = STATUS_INVALID_PARAMETER;
1110 break;
1112 status = read_credential( irp->AssociatedIrp.SystemBuffer,
1113 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1114 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1115 &irp->IoStatus );
1116 break;
1117 case IOCTL_MOUNTMGR_WRITE_CREDENTIAL:
1118 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential))
1120 status = STATUS_INVALID_PARAMETER;
1121 break;
1123 status = write_credential( irp->AssociatedIrp.SystemBuffer,
1124 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1125 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1126 &irp->IoStatus );
1127 break;
1128 case IOCTL_MOUNTMGR_DELETE_CREDENTIAL:
1129 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential))
1131 status = STATUS_INVALID_PARAMETER;
1132 break;
1134 status = delete_credential( irp->AssociatedIrp.SystemBuffer,
1135 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1136 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1137 &irp->IoStatus );
1138 break;
1139 case IOCTL_MOUNTMGR_ENUMERATE_CREDENTIALS:
1140 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential_list))
1142 status = STATUS_INVALID_PARAMETER;
1143 break;
1145 status = enumerate_credentials( irp->AssociatedIrp.SystemBuffer,
1146 irpsp->Parameters.DeviceIoControl.InputBufferLength,
1147 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
1148 &irp->IoStatus );
1149 break;
1150 #endif
1151 default:
1152 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
1153 status = STATUS_NOT_SUPPORTED;
1154 break;
1156 irp->IoStatus.u.Status = status;
1157 IoCompleteRequest( irp, IO_NO_INCREMENT );
1158 return status;
1161 /* main entry point for the mount point manager driver */
1162 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
1164 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};
1165 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};
1166 static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
1167 static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
1168 static const WCHAR driver_serialW[] = {'\\','D','r','i','v','e','r','\\','S','e','r','i','a','l',0};
1169 static const WCHAR driver_parallelW[] = {'\\','D','r','i','v','e','r','\\','P','a','r','a','l','l','e','l',0};
1170 static const WCHAR devicemapW[] = {'H','A','R','D','W','A','R','E','\\','D','E','V','I','C','E','M','A','P','\\','S','c','s','i',0};
1172 #ifdef _WIN64
1173 static const WCHAR qualified_ports_keyW[] = {'\\','R','E','G','I','S','T','R','Y','\\',
1174 'M','A','C','H','I','N','E','\\','S','o','f','t','w','a','r','e','\\',
1175 'W','i','n','e','\\','P','o','r','t','s'}; /* no null terminator */
1176 static const WCHAR wow64_ports_keyW[] = {'S','o','f','t','w','a','r','e','\\',
1177 'W','o','w','6','4','3','2','N','o','d','e','\\','W','i','n','e','\\',
1178 'P','o','r','t','s',0};
1179 static const WCHAR symbolic_link_valueW[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e',0};
1180 HKEY wow64_ports_key = NULL;
1181 #endif
1183 UNICODE_STRING nameW, linkW;
1184 DEVICE_OBJECT *device;
1185 HKEY devicemap_key;
1186 NTSTATUS status;
1188 TRACE( "%s\n", debugstr_w(path->Buffer) );
1190 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
1192 RtlInitUnicodeString( &nameW, device_mountmgrW );
1193 RtlInitUnicodeString( &linkW, link_mountmgrW );
1194 if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
1195 status = IoCreateSymbolicLink( &linkW, &nameW );
1196 if (status)
1198 FIXME( "failed to create device error %x\n", status );
1199 return status;
1202 RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
1203 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
1205 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, devicemapW, 0, NULL, REG_OPTION_VOLATILE,
1206 KEY_ALL_ACCESS, NULL, &devicemap_key, NULL ))
1207 RegCloseKey( devicemap_key );
1209 RtlInitUnicodeString( &nameW, harddiskW );
1210 status = IoCreateDriver( &nameW, harddisk_driver_entry );
1212 initialize_dbus();
1213 initialize_diskarbitration();
1215 #ifdef _WIN64
1216 /* create a symlink so that the Wine port overrides key can be edited with 32-bit reg or regedit */
1217 RegCreateKeyExW( HKEY_LOCAL_MACHINE, wow64_ports_keyW, 0, NULL, REG_OPTION_CREATE_LINK,
1218 KEY_SET_VALUE, NULL, &wow64_ports_key, NULL );
1219 RegSetValueExW( wow64_ports_key, symbolic_link_valueW, 0, REG_LINK,
1220 (BYTE *)qualified_ports_keyW, sizeof(qualified_ports_keyW) );
1221 RegCloseKey( wow64_ports_key );
1222 #endif
1224 RtlInitUnicodeString( &nameW, driver_serialW );
1225 IoCreateDriver( &nameW, serial_driver_entry );
1227 RtlInitUnicodeString( &nameW, driver_parallelW );
1228 IoCreateDriver( &nameW, parallel_driver_entry );
1230 return status;