wined3d: Pass a wined3d_device_context to wined3d_texture_blt().
[wine/zf.git] / dlls / mountmgr.sys / mountmgr.c
blob61d51d8e61d6b53d63129c1ce9ad0ed6aed5b5a1
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 #ifdef __APPLE__
22 #include <CoreFoundation/CFString.h>
23 #define LoadResource mac_LoadResource
24 #define GetCurrentThread mac_GetCurrentThread
25 #include <CoreServices/CoreServices.h>
26 #undef LoadResource
27 #undef GetCurrentThread
28 #endif
30 #include <stdarg.h>
31 #include <unistd.h>
33 #define NONAMELESSUNION
35 #include "mountmgr.h"
36 #include "winreg.h"
37 #include "wine/list.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
43 #define MIN_ID_LEN 4
45 struct mount_point
47 struct list entry; /* entry in mount points list */
48 DEVICE_OBJECT *device; /* disk device */
49 UNICODE_STRING name; /* device name */
50 UNICODE_STRING link; /* DOS device symlink */
51 void *id; /* device unique id */
52 unsigned int id_len;
55 static struct list mount_points_list = LIST_INIT(mount_points_list);
56 static HKEY mount_key;
58 void set_mount_point_id( struct mount_point *mount, const void *id, unsigned int id_len )
60 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
61 mount->id_len = max( MIN_ID_LEN, id_len );
62 if ((mount->id = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, mount->id_len )))
64 memcpy( mount->id, id, id_len );
65 RegSetValueExW( mount_key, mount->link.Buffer, 0, REG_BINARY, mount->id, mount->id_len );
67 else mount->id_len = 0;
70 static struct mount_point *add_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
71 const WCHAR *link )
73 struct mount_point *mount;
74 WCHAR *str;
75 UINT len = (strlenW(link) + 1) * sizeof(WCHAR) + device_name->Length + sizeof(WCHAR);
77 if (!(mount = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*mount) + len ))) return NULL;
79 str = (WCHAR *)(mount + 1);
80 strcpyW( str, link );
81 RtlInitUnicodeString( &mount->link, str );
82 str += strlenW(str) + 1;
83 memcpy( str, device_name->Buffer, device_name->Length );
84 str[device_name->Length / sizeof(WCHAR)] = 0;
85 mount->name.Buffer = str;
86 mount->name.Length = device_name->Length;
87 mount->name.MaximumLength = device_name->Length + sizeof(WCHAR);
88 mount->device = device;
89 mount->id = NULL;
90 list_add_tail( &mount_points_list, &mount->entry );
92 IoCreateSymbolicLink( &mount->link, device_name );
94 TRACE( "created %s id %s for %s\n", debugstr_w(mount->link.Buffer),
95 debugstr_a(mount->id), debugstr_w(mount->name.Buffer) );
96 return mount;
99 /* create the DosDevices mount point symlink for a new device */
100 struct mount_point *add_dosdev_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name, int drive )
102 static const WCHAR driveW[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','%','c',':',0};
103 WCHAR link[sizeof(driveW)];
105 sprintfW( link, driveW, 'A' + drive );
106 return add_mount_point( device, device_name, link );
109 /* create the Volume mount point symlink for a new device */
110 struct mount_point *add_volume_mount_point( DEVICE_OBJECT *device, UNICODE_STRING *device_name,
111 const GUID *guid )
113 static const WCHAR volumeW[] = {'\\','?','?','\\','V','o','l','u','m','e','{',
114 '%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
115 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
116 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
117 WCHAR link[sizeof(volumeW)];
119 sprintfW( link, volumeW, guid->Data1, guid->Data2, guid->Data3,
120 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
121 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
122 return add_mount_point( device, device_name, link );
125 /* delete the mount point symlinks when a device goes away */
126 void delete_mount_point( struct mount_point *mount )
128 TRACE( "deleting %s\n", debugstr_w(mount->link.Buffer) );
129 list_remove( &mount->entry );
130 RegDeleteValueW( mount_key, mount->link.Buffer );
131 IoDeleteSymbolicLink( &mount->link );
132 RtlFreeHeap( GetProcessHeap(), 0, mount->id );
133 RtlFreeHeap( GetProcessHeap(), 0, mount );
136 /* check if a given mount point matches the requested specs */
137 static BOOL matching_mount_point( const struct mount_point *mount, const MOUNTMGR_MOUNT_POINT *spec )
139 if (spec->SymbolicLinkNameOffset)
141 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->SymbolicLinkNameOffset);
142 if (spec->SymbolicLinkNameLength != mount->link.Length) return FALSE;
143 if (strncmpiW( name, mount->link.Buffer, mount->link.Length/sizeof(WCHAR)))
144 return FALSE;
146 if (spec->DeviceNameOffset)
148 const WCHAR *name = (const WCHAR *)((const char *)spec + spec->DeviceNameOffset);
149 if (spec->DeviceNameLength != mount->name.Length) return FALSE;
150 if (strncmpiW( name, mount->name.Buffer, mount->name.Length/sizeof(WCHAR)))
151 return FALSE;
153 if (spec->UniqueIdOffset)
155 const void *id = ((const char *)spec + spec->UniqueIdOffset);
156 if (spec->UniqueIdLength != mount->id_len) return FALSE;
157 if (memcmp( id, mount->id, mount->id_len )) return FALSE;
159 return TRUE;
162 /* implementation of IOCTL_MOUNTMGR_QUERY_POINTS */
163 static NTSTATUS query_mount_points( void *buff, SIZE_T insize,
164 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
166 UINT count, pos, size;
167 MOUNTMGR_MOUNT_POINT *input = buff;
168 MOUNTMGR_MOUNT_POINTS *info;
169 struct mount_point *mount;
171 /* sanity checks */
172 if (input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength > insize ||
173 input->UniqueIdOffset + input->UniqueIdLength > insize ||
174 input->DeviceNameOffset + input->DeviceNameLength > insize ||
175 input->SymbolicLinkNameOffset + input->SymbolicLinkNameLength < input->SymbolicLinkNameOffset ||
176 input->UniqueIdOffset + input->UniqueIdLength < input->UniqueIdOffset ||
177 input->DeviceNameOffset + input->DeviceNameLength < input->DeviceNameOffset)
178 return STATUS_INVALID_PARAMETER;
180 count = size = 0;
181 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
183 if (!matching_mount_point( mount, input )) continue;
184 size += mount->name.Length;
185 size += mount->link.Length;
186 size += mount->id_len;
187 size = (size + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
188 count++;
190 pos = FIELD_OFFSET( MOUNTMGR_MOUNT_POINTS, MountPoints[count] );
191 size += pos;
193 if (size > outsize)
195 info = buff;
196 if (size >= sizeof(info->Size)) info->Size = size;
197 iosb->Information = sizeof(info->Size);
198 return STATUS_MORE_ENTRIES;
201 input = HeapAlloc( GetProcessHeap(), 0, insize );
202 if (!input)
203 return STATUS_NO_MEMORY;
204 memcpy( input, buff, insize );
205 info = buff;
207 info->NumberOfMountPoints = count;
208 count = 0;
209 LIST_FOR_EACH_ENTRY( mount, &mount_points_list, struct mount_point, entry )
211 if (!matching_mount_point( mount, input )) continue;
213 info->MountPoints[count].DeviceNameOffset = pos;
214 info->MountPoints[count].DeviceNameLength = mount->name.Length;
215 memcpy( (char *)buff + pos, mount->name.Buffer, mount->name.Length );
216 pos += mount->name.Length;
218 info->MountPoints[count].SymbolicLinkNameOffset = pos;
219 info->MountPoints[count].SymbolicLinkNameLength = mount->link.Length;
220 memcpy( (char *)buff + pos, mount->link.Buffer, mount->link.Length );
221 pos += mount->link.Length;
223 info->MountPoints[count].UniqueIdOffset = pos;
224 info->MountPoints[count].UniqueIdLength = mount->id_len;
225 memcpy( (char *)buff + pos, mount->id, mount->id_len );
226 pos += mount->id_len;
227 pos = (pos + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
228 count++;
230 info->Size = pos;
231 iosb->Information = pos;
232 HeapFree( GetProcessHeap(), 0, input );
233 return STATUS_SUCCESS;
236 /* implementation of IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE */
237 static NTSTATUS define_unix_drive( const void *in_buff, SIZE_T insize )
239 const struct mountmgr_unix_drive *input = in_buff;
240 const char *mount_point = NULL, *device = NULL;
241 unsigned int i;
242 WCHAR letter = tolowerW( input->letter );
244 if (letter < 'a' || letter > 'z') return STATUS_INVALID_PARAMETER;
245 if (input->type > DRIVE_RAMDISK) return STATUS_INVALID_PARAMETER;
246 if (input->mount_point_offset > insize || input->device_offset > insize)
247 return STATUS_INVALID_PARAMETER;
249 /* make sure string are null-terminated */
250 if (input->mount_point_offset)
252 mount_point = (const char *)in_buff + input->mount_point_offset;
253 for (i = input->mount_point_offset; i < insize; i++)
254 if (!*((const char *)in_buff + i)) break;
255 if (i >= insize) return STATUS_INVALID_PARAMETER;
257 if (input->device_offset)
259 device = (const char *)in_buff + input->device_offset;
260 for (i = input->device_offset; i < insize; i++)
261 if (!*((const char *)in_buff + i)) break;
262 if (i >= insize) return STATUS_INVALID_PARAMETER;
265 if (input->type != DRIVE_NO_ROOT_DIR)
267 enum device_type type = DEVICE_UNKNOWN;
269 TRACE( "defining %c: dev %s mount %s type %u\n",
270 letter, debugstr_a(device), debugstr_a(mount_point), input->type );
271 switch (input->type)
273 case DRIVE_REMOVABLE: type = (letter >= 'c') ? DEVICE_HARDDISK : DEVICE_FLOPPY; break;
274 case DRIVE_REMOTE: type = DEVICE_NETWORK; break;
275 case DRIVE_CDROM: type = DEVICE_CDROM; break;
276 case DRIVE_RAMDISK: type = DEVICE_RAMDISK; break;
277 case DRIVE_FIXED: type = DEVICE_HARDDISK_VOL; break;
279 return add_dos_device( letter - 'a', NULL, device, mount_point, type, NULL, NULL );
281 else
283 TRACE( "removing %c:\n", letter );
284 return remove_dos_device( letter - 'a', NULL );
288 /* implementation of IOCTL_MOUNTMGR_QUERY_DHCP_REQUEST_PARAMS */
289 static NTSTATUS query_dhcp_request_params( void *buff, SIZE_T insize,
290 SIZE_T outsize, IO_STATUS_BLOCK *iosb )
292 struct mountmgr_dhcp_request_params *query = buff;
293 ULONG i, offset;
295 /* sanity checks */
296 if (FIELD_OFFSET(struct mountmgr_dhcp_request_params, params[query->count]) > insize ||
297 !memchrW( query->adapter, 0, ARRAY_SIZE(query->adapter) )) return STATUS_INVALID_PARAMETER;
298 for (i = 0; i < query->count; i++)
299 if (query->params[i].offset + query->params[i].size > insize) return STATUS_INVALID_PARAMETER;
301 offset = FIELD_OFFSET(struct mountmgr_dhcp_request_params, params[query->count]);
302 for (i = 0; i < query->count; i++)
304 offset += get_dhcp_request_param( query->adapter, &query->params[i], buff, offset, outsize - offset );
305 if (offset > outsize)
307 if (offset >= sizeof(query->size)) query->size = offset;
308 iosb->Information = sizeof(query->size);
309 return STATUS_MORE_ENTRIES;
313 iosb->Information = offset;
314 return STATUS_SUCCESS;
317 /* implementation of Wine extension to use host APIs to find symbol file by GUID */
318 #ifdef __APPLE__
319 static void WINAPI query_symbol_file( TP_CALLBACK_INSTANCE *instance, void *context )
321 IRP *irp = context;
322 MOUNTMGR_TARGET_NAME *result;
323 CFStringRef query_cfstring;
324 WCHAR *filename, *unix_buf = NULL;
325 ANSI_STRING unix_path;
326 UNICODE_STRING path;
327 MDQueryRef mdquery;
328 const GUID *id;
329 size_t size;
330 NTSTATUS status = STATUS_NO_MEMORY;
332 static const WCHAR formatW[] = { 'c','o','m','_','a','p','p','l','e','_','x','c','o','d','e',
333 '_','d','s','y','m','_','u','u','i','d','s',' ','=','=',' ',
334 '"','%','0','8','X','-','%','0','4','X','-',
335 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
336 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
337 '%','0','2','X','%','0','2','X','"',0 };
338 WCHAR query_string[ARRAY_SIZE(formatW)];
340 id = irp->AssociatedIrp.SystemBuffer;
341 sprintfW( query_string, formatW, id->Data1, id->Data2, id->Data3,
342 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
343 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
344 if (!(query_cfstring = CFStringCreateWithCharacters(NULL, query_string, lstrlenW(query_string)))) goto done;
346 mdquery = MDQueryCreate(NULL, query_cfstring, NULL, NULL);
347 CFRelease(query_cfstring);
348 if (!mdquery) goto done;
350 MDQuerySetMaxCount(mdquery, 1);
351 TRACE("Executing %s\n", debugstr_w(query_string));
352 if (MDQueryExecute(mdquery, kMDQuerySynchronous))
354 if (MDQueryGetResultCount(mdquery) >= 1)
356 MDItemRef item = (MDItemRef)MDQueryGetResultAtIndex(mdquery, 0);
357 CFStringRef item_path = MDItemCopyAttribute(item, kMDItemPath);
359 if (item_path)
361 CFIndex item_path_len = CFStringGetLength(item_path);
362 if ((unix_buf = HeapAlloc(GetProcessHeap(), 0, (item_path_len + 1) * sizeof(WCHAR))))
364 CFStringGetCharacters(item_path, CFRangeMake(0, item_path_len), unix_buf);
365 unix_buf[item_path_len] = 0;
366 TRACE("found %s\n", debugstr_w(unix_buf));
368 CFRelease(item_path);
371 else status = STATUS_NO_MORE_ENTRIES;
373 CFRelease(mdquery);
374 if (!unix_buf) goto done;
376 RtlInitUnicodeString( &path, unix_buf );
377 status = RtlUnicodeStringToAnsiString( &unix_path, &path, TRUE );
378 HeapFree( GetProcessHeap(), 0, unix_buf );
379 if (status) goto done;
381 filename = wine_get_dos_file_name( unix_path.Buffer );
382 RtlFreeAnsiString( &unix_path );
383 if (!filename)
385 status = STATUS_NO_SUCH_FILE;
386 goto done;
388 result = irp->AssociatedIrp.SystemBuffer;
389 result->DeviceNameLength = lstrlenW(filename) * sizeof(WCHAR);
390 size = FIELD_OFFSET(MOUNTMGR_TARGET_NAME, DeviceName[lstrlenW(filename)]);
391 if (size <= IoGetCurrentIrpStackLocation(irp)->Parameters.DeviceIoControl.OutputBufferLength)
393 memcpy( result->DeviceName, filename, lstrlenW(filename) * sizeof(WCHAR) );
394 irp->IoStatus.Information = size;
395 status = STATUS_SUCCESS;
397 else
399 irp->IoStatus.Information = sizeof(*result);
400 status = STATUS_BUFFER_OVERFLOW;
402 RtlFreeHeap( GetProcessHeap(), 0, filename );
404 done:
405 irp->IoStatus.u.Status = status;
406 IoCompleteRequest( irp, IO_NO_INCREMENT );
409 static inline BOOL check_credential_string( const void *buf, SIZE_T buflen, ULONG size, ULONG offset )
411 const WCHAR *ptr = buf;
412 if (!size || size % sizeof(WCHAR) || offset + size > buflen || ptr[(offset + size) / sizeof(WCHAR) - 1]) return FALSE;
413 return TRUE;
416 static SecKeychainItemRef find_credential( const WCHAR *name )
418 int status;
419 SecKeychainSearchRef search;
420 SecKeychainItemRef item;
422 status = SecKeychainSearchCreateFromAttributes( NULL, kSecGenericPasswordItemClass, NULL, &search );
423 if (status != noErr) return NULL;
425 while (SecKeychainSearchCopyNext( search, &item ) == noErr)
427 SecKeychainAttributeInfo info;
428 SecKeychainAttributeList *attr_list;
429 UInt32 info_tags[] = { kSecServiceItemAttr };
430 WCHAR *itemname;
431 int len;
433 info.count = ARRAY_SIZE(info_tags);
434 info.tag = info_tags;
435 info.format = NULL;
436 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, NULL, NULL );
437 if (status != noErr)
439 WARN( "SecKeychainItemCopyAttributesAndData returned status %d\n", status );
440 continue;
442 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServiceItemAttr)
444 CFRelease( item );
445 continue;
447 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, NULL, 0 );
448 if (!(itemname = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
450 CFRelease( item );
451 CFRelease( search );
452 return NULL;
454 MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[0].data, attr_list->attr[0].length, itemname, len );
455 itemname[len] = 0;
456 if (strcmpiW( itemname, name ))
458 CFRelease( item );
459 RtlFreeHeap( GetProcessHeap(), 0, itemname );
460 continue;
462 RtlFreeHeap( GetProcessHeap(), 0, itemname );
463 SecKeychainItemFreeAttributesAndData( attr_list, NULL );
464 CFRelease( search );
465 return item;
468 CFRelease( search );
469 return NULL;
472 static NTSTATUS fill_credential( SecKeychainItemRef item, BOOL require_password, void *buf, SIZE_T data_offset,
473 SIZE_T buflen, SIZE_T *retlen )
475 struct mountmgr_credential *cred = buf;
476 int status, len;
477 SIZE_T size;
478 UInt32 i, cred_blob_len = 0;
479 void *cred_blob;
480 WCHAR *ptr;
481 BOOL user_name_present = FALSE;
482 SecKeychainAttributeInfo info;
483 SecKeychainAttributeList *attr_list = NULL;
484 UInt32 info_tags[] = { kSecServiceItemAttr, kSecAccountItemAttr, kSecCommentItemAttr, kSecCreationDateItemAttr };
486 info.count = ARRAY_SIZE(info_tags);
487 info.tag = info_tags;
488 info.format = NULL;
489 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, &cred_blob_len, &cred_blob );
490 if (status == errSecAuthFailed && !require_password)
492 cred_blob_len = 0;
493 cred_blob = NULL;
494 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, &cred_blob_len, NULL );
496 if (status != noErr)
498 WARN( "SecKeychainItemCopyAttributesAndData returned status %d\n", status );
499 return STATUS_NOT_FOUND;
501 for (i = 0; i < attr_list->count; i++)
503 if (attr_list->attr[i].tag == kSecAccountItemAttr && attr_list->attr[i].data)
505 user_name_present = TRUE;
506 break;
509 if (!user_name_present)
511 WARN( "no kSecAccountItemAttr for item\n" );
512 SecKeychainItemFreeAttributesAndData( attr_list, cred_blob );
513 return STATUS_NOT_FOUND;
516 *retlen = sizeof(*cred);
517 for (i = 0; i < attr_list->count; i++)
519 switch (attr_list->attr[i].tag)
521 case kSecServiceItemAttr:
522 TRACE( "kSecServiceItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
523 if (cred) cred->targetname_offset = cred->targetname_size = 0;
524 if (!attr_list->attr[i].data) continue;
526 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, NULL, 0 );
527 size = (len + 1) * sizeof(WCHAR);
528 if (cred && *retlen + size <= buflen)
530 cred->targetname_offset = data_offset;
531 cred->targetname_size = size;
532 ptr = (WCHAR *)((char *)cred + cred->targetname_offset);
533 MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, ptr, len );
534 ptr[len] = 0;
535 data_offset += size;
537 *retlen += size;
538 break;
539 case kSecAccountItemAttr:
541 TRACE( "kSecAccountItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
542 if (cred) cred->username_offset = cred->username_size = 0;
543 if (!attr_list->attr[i].data) continue;
545 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, NULL, 0 );
546 size = (len + 1) * sizeof(WCHAR);
547 if (cred && *retlen + size <= buflen)
549 cred->username_offset = data_offset;
550 cred->username_size = size;
551 ptr = (WCHAR *)((char *)cred + cred->username_offset);
552 MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, ptr, len );
553 ptr[len] = 0;
554 data_offset += size;
556 *retlen += size;
557 break;
559 case kSecCommentItemAttr:
560 TRACE( "kSecCommentItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
561 if (cred) cred->comment_offset = cred->comment_size = 0;
562 if (!attr_list->attr[i].data) continue;
564 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, NULL, 0 );
565 size = (len + 1) * sizeof(WCHAR);
566 if (cred && *retlen + size <= buflen)
568 cred->comment_offset = data_offset;
569 cred->comment_size = size;
570 ptr = (WCHAR *)((char *)cred + cred->comment_offset);
571 len = MultiByteToWideChar( CP_UTF8, 0, attr_list->attr[i].data, attr_list->attr[i].length, ptr, len );
572 ptr[len] = 0;
573 data_offset += size;
575 *retlen += size;
576 break;
577 case kSecCreationDateItemAttr:
579 LARGE_INTEGER wintime;
580 struct tm tm;
581 time_t time;
583 TRACE( "kSecCreationDateItemAttr: %.*s\n", (int)attr_list->attr[i].length, (char *)attr_list->attr[i].data );
584 if (cred) cred->last_written.dwLowDateTime = cred->last_written.dwHighDateTime = 0;
585 if (!attr_list->attr[i].data) continue;
587 if (cred)
589 memset( &tm, 0, sizeof(tm) );
590 strptime( attr_list->attr[i].data, "%Y%m%d%H%M%SZ", &tm );
591 time = mktime( &tm );
592 RtlSecondsSince1970ToTime( time, &wintime );
593 cred->last_written.dwLowDateTime = wintime.u.LowPart;
594 cred->last_written.dwHighDateTime = wintime.u.HighPart;
596 break;
598 default:
599 FIXME( "unhandled attribute %u\n", (unsigned)attr_list->attr[i].tag );
600 break;
604 if (cred)
606 if (*retlen + cred_blob_len <= buflen)
608 len = MultiByteToWideChar( CP_UTF8, 0, cred_blob, cred_blob_len, NULL, 0 );
609 cred->blob_offset = data_offset;
610 cred->blob_size = len * sizeof(WCHAR);
611 ptr = (WCHAR *)((char *)cred + cred->blob_offset);
612 MultiByteToWideChar( CP_UTF8, 0, cred_blob, cred_blob_len, ptr, len );
614 else cred->blob_offset = cred->blob_size = 0;
616 *retlen += cred_blob_len;
618 if (attr_list) SecKeychainItemFreeAttributesAndData( attr_list, cred_blob );
619 return STATUS_SUCCESS;
622 static NTSTATUS read_credential( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
624 struct mountmgr_credential *cred = buff;
625 const WCHAR *targetname;
626 SecKeychainItemRef item;
627 SIZE_T size;
628 NTSTATUS status;
630 if (!check_credential_string( buff, insize, cred->targetname_size, cred->targetname_offset ))
631 return STATUS_INVALID_PARAMETER;
632 targetname = (const WCHAR *)((const char *)cred + cred->targetname_offset);
634 if (!(item = find_credential( targetname ))) return STATUS_NOT_FOUND;
636 status = fill_credential( item, TRUE, cred, sizeof(*cred), outsize, &size );
637 CFRelease( item );
638 if (status != STATUS_SUCCESS) return status;
640 iosb->Information = size;
641 return (size > outsize) ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
644 static NTSTATUS write_credential( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
646 const struct mountmgr_credential *cred = buff;
647 int status, len, len_password = 0;
648 const WCHAR *ptr;
649 SecKeychainItemRef keychain_item;
650 char *targetname, *username = NULL, *password = NULL;
651 SecKeychainAttribute attrs[1];
652 SecKeychainAttributeList attr_list;
653 NTSTATUS ret = STATUS_NO_MEMORY;
655 if (!check_credential_string( buff, insize, cred->targetname_size, cred->targetname_offset ) ||
656 !check_credential_string( buff, insize, cred->username_size, cred->username_offset ) ||
657 ((cred->blob_size && cred->blob_size % sizeof(WCHAR)) || cred->blob_offset + cred->blob_size > insize) ||
658 (cred->comment_size && !check_credential_string( buff, insize, cred->comment_size, cred->comment_offset )) ||
659 sizeof(*cred) + cred->targetname_size + cred->username_size + cred->blob_size + cred->comment_size > insize)
661 return STATUS_INVALID_PARAMETER;
664 ptr = (const WCHAR *)((const char *)cred + cred->targetname_offset);
665 len = WideCharToMultiByte( CP_UTF8, 0, ptr, -1, NULL, 0, NULL, NULL );
666 if (!(targetname = RtlAllocateHeap( GetProcessHeap(), 0, len ))) goto error;
667 WideCharToMultiByte( CP_UTF8, 0, ptr, -1, targetname, len, NULL, NULL );
669 ptr = (const WCHAR *)((const char *)cred + cred->username_offset);
670 len = WideCharToMultiByte( CP_UTF8, 0, ptr, -1, NULL, 0, NULL, NULL );
671 if (!(username = RtlAllocateHeap( GetProcessHeap(), 0, len ))) goto error;
672 WideCharToMultiByte( CP_UTF8, 0, ptr, -1, username, len, NULL, NULL );
674 if (cred->blob_size)
676 ptr = (const WCHAR *)((const char *)cred + cred->blob_offset);
677 len_password = WideCharToMultiByte( CP_UTF8, 0, ptr, cred->blob_size / sizeof(WCHAR), NULL, 0, NULL, NULL );
678 if (!(password = RtlAllocateHeap( GetProcessHeap(), 0, len_password ))) goto error;
679 WideCharToMultiByte( CP_UTF8, 0, ptr, cred->blob_size / sizeof(WCHAR), password, len_password, NULL, NULL );
682 TRACE("adding target %s, username %s using Keychain\n", targetname, username );
683 status = SecKeychainAddGenericPassword( NULL, strlen(targetname), targetname, strlen(username), username,
684 len_password, password, &keychain_item );
685 if (status != noErr) ERR( "SecKeychainAddGenericPassword returned %d\n", status );
686 if (status == errSecDuplicateItem)
688 status = SecKeychainFindGenericPassword( NULL, strlen(targetname), targetname, strlen(username), username, NULL,
689 NULL, &keychain_item );
690 if (status != noErr) ERR( "SecKeychainFindGenericPassword returned %d\n", status );
692 RtlFreeHeap( GetProcessHeap(), 0, username );
693 RtlFreeHeap( GetProcessHeap(), 0, targetname );
694 if (status != noErr)
696 RtlFreeHeap( GetProcessHeap(), 0, password );
697 return STATUS_UNSUCCESSFUL;
699 if (cred->comment_size)
701 attr_list.count = 1;
702 attr_list.attr = attrs;
703 attrs[0].tag = kSecCommentItemAttr;
704 ptr = (const WCHAR *)((const char *)cred + cred->comment_offset);
705 attrs[0].length = WideCharToMultiByte( CP_UTF8, 0, ptr, -1, NULL, 0, NULL, NULL );
706 if (attrs[0].length) attrs[0].length--;
707 if (!(attrs[0].data = RtlAllocateHeap( GetProcessHeap(), 0, attrs[0].length ))) goto error;
708 WideCharToMultiByte( CP_UTF8, 0, ptr, -1, attrs[0].data, attrs[0].length, NULL, NULL );
710 else
712 attr_list.count = 0;
713 attr_list.attr = NULL;
715 status = SecKeychainItemModifyAttributesAndData( keychain_item, &attr_list, cred->blob_preserve ? 0 : len_password,
716 cred->blob_preserve ? NULL : password );
718 if (cred->comment_size) RtlFreeHeap( GetProcessHeap(), 0, attrs[0].data );
719 RtlFreeHeap( GetProcessHeap(), 0, password );
720 /* FIXME: set TargetAlias attribute */
721 CFRelease( keychain_item );
722 if (status != noErr) return STATUS_UNSUCCESSFUL;
723 return STATUS_SUCCESS;
725 error:
726 RtlFreeHeap( GetProcessHeap(), 0, username );
727 RtlFreeHeap( GetProcessHeap(), 0, targetname );
728 RtlFreeHeap( GetProcessHeap(), 0, password );
729 return ret;
732 static NTSTATUS delete_credential( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
734 const struct mountmgr_credential *cred = buff;
735 const WCHAR *targetname;
736 SecKeychainItemRef item;
738 if (!check_credential_string( buff, insize, cred->targetname_size, cred->targetname_offset ))
739 return STATUS_INVALID_PARAMETER;
740 targetname = (const WCHAR *)((const char *)cred + cred->targetname_offset);
742 if (!(item = find_credential( targetname ))) return STATUS_NOT_FOUND;
744 SecKeychainItemDelete( item );
745 CFRelease( item );
746 return STATUS_SUCCESS;
749 static BOOL match_credential( void *data, UInt32 data_len, const WCHAR *filter )
751 int len;
752 WCHAR *targetname;
753 const WCHAR *p;
754 BOOL ret;
756 if (!*filter) return TRUE;
758 len = MultiByteToWideChar( CP_UTF8, 0, data, data_len, NULL, 0 );
759 if (!(targetname = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return FALSE;
760 MultiByteToWideChar( CP_UTF8, 0, data, data_len, targetname, len );
761 targetname[len] = 0;
763 TRACE( "comparing filter %s to target name %s\n", debugstr_w(filter), debugstr_w(targetname) );
765 p = strchrW( filter, '*' );
766 ret = CompareStringW( GetThreadLocale(), NORM_IGNORECASE, filter,
767 (p && !p[1]) ? p - filter : -1, targetname, (p && !p[1]) ? p - filter : -1 ) == CSTR_EQUAL;
768 RtlFreeHeap( GetProcessHeap(), 0, targetname );
769 return ret;
772 static NTSTATUS search_credentials( const WCHAR *filter, struct mountmgr_credential_list *list, SIZE_T *ret_count, SIZE_T *ret_size )
774 SecKeychainSearchRef search;
775 SecKeychainItemRef item;
776 int status;
777 ULONG i = 0;
778 SIZE_T data_offset, data_size = 0, size;
779 NTSTATUS ret = STATUS_NOT_FOUND;
781 status = SecKeychainSearchCreateFromAttributes( NULL, kSecGenericPasswordItemClass, NULL, &search );
782 if (status != noErr)
784 ERR( "SecKeychainSearchCreateFromAttributes returned status %d\n", status );
785 return STATUS_INTERNAL_ERROR;
788 while (SecKeychainSearchCopyNext( search, &item ) == noErr)
790 SecKeychainAttributeInfo info;
791 SecKeychainAttributeList *attr_list;
792 UInt32 info_tags[] = { kSecServiceItemAttr };
793 BOOL match;
795 info.count = ARRAY_SIZE(info_tags);
796 info.tag = info_tags;
797 info.format = NULL;
798 status = SecKeychainItemCopyAttributesAndData( item, &info, NULL, &attr_list, NULL, NULL );
799 if (status != noErr)
801 WARN( "SecKeychainItemCopyAttributesAndData returned status %d\n", status );
802 CFRelease( item );
803 continue;
805 if (attr_list->count != 1 || attr_list->attr[0].tag != kSecServiceItemAttr)
807 SecKeychainItemFreeAttributesAndData( attr_list, NULL );
808 CFRelease( item );
809 continue;
811 TRACE( "service item: %.*s\n", (int)attr_list->attr[0].length, (char *)attr_list->attr[0].data );
813 match = match_credential( attr_list->attr[0].data, attr_list->attr[0].length, filter );
814 SecKeychainItemFreeAttributesAndData( attr_list, NULL );
815 if (!match)
817 CFRelease( item );
818 continue;
821 if (!list) ret = fill_credential( item, FALSE, NULL, 0, 0, &size );
822 else
824 data_offset = FIELD_OFFSET( struct mountmgr_credential_list, creds[list->count] ) -
825 FIELD_OFFSET( struct mountmgr_credential_list, creds[i] ) + data_size;
826 ret = fill_credential( item, FALSE, &list->creds[i], data_offset, list->size - data_offset, &size );
829 CFRelease( item );
830 if (ret == STATUS_NOT_FOUND) continue;
831 if (ret != STATUS_SUCCESS) break;
832 data_size += size - sizeof(struct mountmgr_credential);
833 i++;
836 if (ret_count) *ret_count = i;
837 if (ret_size) *ret_size = FIELD_OFFSET( struct mountmgr_credential_list, creds[i] ) + data_size;
839 CFRelease( search );
840 return ret;
843 static NTSTATUS enumerate_credentials( void *buff, SIZE_T insize, SIZE_T outsize, IO_STATUS_BLOCK *iosb )
845 struct mountmgr_credential_list *list = buff;
846 WCHAR *filter;
847 SIZE_T size, count;
848 Boolean saved_user_interaction_allowed;
849 NTSTATUS status;
851 if (!check_credential_string( buff, insize, list->filter_size, list->filter_offset )) return STATUS_INVALID_PARAMETER;
852 if (!(filter = strdupW( (const WCHAR *)((const char *)list + list->filter_offset) ))) return STATUS_NO_MEMORY;
854 SecKeychainGetUserInteractionAllowed( &saved_user_interaction_allowed );
855 SecKeychainSetUserInteractionAllowed( false );
857 if ((status = search_credentials( filter, NULL, &count, &size )) == STATUS_SUCCESS)
860 if (size > outsize)
862 if (size >= sizeof(list->size)) list->size = size;
863 iosb->Information = sizeof(list->size);
864 status = STATUS_MORE_ENTRIES;
866 else
868 list->size = size;
869 list->count = count;
870 iosb->Information = size;
871 status = search_credentials( filter, list, NULL, NULL );
875 SecKeychainSetUserInteractionAllowed( saved_user_interaction_allowed );
876 RtlFreeHeap( GetProcessHeap(), 0, filter );
877 return status;
879 #endif /* __APPLE__ */
881 /* handler for ioctls on the mount manager device */
882 static NTSTATUS WINAPI mountmgr_ioctl( DEVICE_OBJECT *device, IRP *irp )
884 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
885 NTSTATUS status;
887 TRACE( "ioctl %x insize %u outsize %u\n",
888 irpsp->Parameters.DeviceIoControl.IoControlCode,
889 irpsp->Parameters.DeviceIoControl.InputBufferLength,
890 irpsp->Parameters.DeviceIoControl.OutputBufferLength );
892 switch(irpsp->Parameters.DeviceIoControl.IoControlCode)
894 case IOCTL_MOUNTMGR_QUERY_POINTS:
895 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(MOUNTMGR_MOUNT_POINT))
897 status = STATUS_INVALID_PARAMETER;
898 break;
900 status = query_mount_points( irp->AssociatedIrp.SystemBuffer,
901 irpsp->Parameters.DeviceIoControl.InputBufferLength,
902 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
903 &irp->IoStatus );
904 break;
905 case IOCTL_MOUNTMGR_DEFINE_UNIX_DRIVE:
906 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
908 status = STATUS_INVALID_PARAMETER;
909 break;
911 irp->IoStatus.Information = 0;
912 status = define_unix_drive( irp->AssociatedIrp.SystemBuffer,
913 irpsp->Parameters.DeviceIoControl.InputBufferLength );
914 break;
915 case IOCTL_MOUNTMGR_QUERY_UNIX_DRIVE:
916 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_unix_drive))
918 status = STATUS_INVALID_PARAMETER;
919 break;
921 status = query_unix_drive( irp->AssociatedIrp.SystemBuffer,
922 irpsp->Parameters.DeviceIoControl.InputBufferLength,
923 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
924 &irp->IoStatus );
925 break;
926 case IOCTL_MOUNTMGR_QUERY_DHCP_REQUEST_PARAMS:
927 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_dhcp_request_params))
929 status = STATUS_INVALID_PARAMETER;
930 break;
932 status = query_dhcp_request_params( irp->AssociatedIrp.SystemBuffer,
933 irpsp->Parameters.DeviceIoControl.InputBufferLength,
934 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
935 &irp->IoStatus );
936 break;
937 #ifdef __APPLE__
938 case IOCTL_MOUNTMGR_QUERY_SYMBOL_FILE:
939 if (irpsp->Parameters.DeviceIoControl.InputBufferLength != sizeof(GUID)
940 || irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTMGR_TARGET_NAME))
942 status = STATUS_INVALID_PARAMETER;
943 break;
945 if (TrySubmitThreadpoolCallback( query_symbol_file, irp, NULL ))
946 return (irp->IoStatus.u.Status = STATUS_PENDING);
947 status = STATUS_NO_MEMORY;
948 break;
949 case IOCTL_MOUNTMGR_READ_CREDENTIAL:
950 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential))
952 status = STATUS_INVALID_PARAMETER;
953 break;
955 status = read_credential( irp->AssociatedIrp.SystemBuffer,
956 irpsp->Parameters.DeviceIoControl.InputBufferLength,
957 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
958 &irp->IoStatus );
959 break;
960 case IOCTL_MOUNTMGR_WRITE_CREDENTIAL:
961 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential))
963 status = STATUS_INVALID_PARAMETER;
964 break;
966 status = write_credential( irp->AssociatedIrp.SystemBuffer,
967 irpsp->Parameters.DeviceIoControl.InputBufferLength,
968 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
969 &irp->IoStatus );
970 break;
971 case IOCTL_MOUNTMGR_DELETE_CREDENTIAL:
972 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential))
974 status = STATUS_INVALID_PARAMETER;
975 break;
977 status = delete_credential( irp->AssociatedIrp.SystemBuffer,
978 irpsp->Parameters.DeviceIoControl.InputBufferLength,
979 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
980 &irp->IoStatus );
981 break;
982 case IOCTL_MOUNTMGR_ENUMERATE_CREDENTIALS:
983 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(struct mountmgr_credential_list))
985 status = STATUS_INVALID_PARAMETER;
986 break;
988 status = enumerate_credentials( irp->AssociatedIrp.SystemBuffer,
989 irpsp->Parameters.DeviceIoControl.InputBufferLength,
990 irpsp->Parameters.DeviceIoControl.OutputBufferLength,
991 &irp->IoStatus );
992 break;
993 #endif
994 default:
995 FIXME( "ioctl %x not supported\n", irpsp->Parameters.DeviceIoControl.IoControlCode );
996 status = STATUS_NOT_SUPPORTED;
997 break;
999 irp->IoStatus.u.Status = status;
1000 IoCompleteRequest( irp, IO_NO_INCREMENT );
1001 return status;
1004 /* main entry point for the mount point manager driver */
1005 NTSTATUS WINAPI DriverEntry( DRIVER_OBJECT *driver, UNICODE_STRING *path )
1007 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};
1008 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};
1009 static const WCHAR link_mountmgrW[] = {'\\','?','?','\\','M','o','u','n','t','P','o','i','n','t','M','a','n','a','g','e','r',0};
1010 static const WCHAR harddiskW[] = {'\\','D','r','i','v','e','r','\\','H','a','r','d','d','i','s','k',0};
1011 static const WCHAR driver_serialW[] = {'\\','D','r','i','v','e','r','\\','S','e','r','i','a','l',0};
1012 static const WCHAR driver_parallelW[] = {'\\','D','r','i','v','e','r','\\','P','a','r','a','l','l','e','l',0};
1013 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};
1015 #ifdef _WIN64
1016 static const WCHAR qualified_ports_keyW[] = {'\\','R','E','G','I','S','T','R','Y','\\',
1017 'M','A','C','H','I','N','E','\\','S','o','f','t','w','a','r','e','\\',
1018 'W','i','n','e','\\','P','o','r','t','s'}; /* no null terminator */
1019 static const WCHAR wow64_ports_keyW[] = {'S','o','f','t','w','a','r','e','\\',
1020 'W','o','w','6','4','3','2','N','o','d','e','\\','W','i','n','e','\\',
1021 'P','o','r','t','s',0};
1022 static const WCHAR symbolic_link_valueW[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e',0};
1023 HKEY wow64_ports_key = NULL;
1024 #endif
1026 UNICODE_STRING nameW, linkW;
1027 DEVICE_OBJECT *device;
1028 HKEY devicemap_key;
1029 NTSTATUS status;
1031 TRACE( "%s\n", debugstr_w(path->Buffer) );
1033 driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = mountmgr_ioctl;
1035 RtlInitUnicodeString( &nameW, device_mountmgrW );
1036 RtlInitUnicodeString( &linkW, link_mountmgrW );
1037 if (!(status = IoCreateDevice( driver, 0, &nameW, 0, 0, FALSE, &device )))
1038 status = IoCreateSymbolicLink( &linkW, &nameW );
1039 if (status)
1041 FIXME( "failed to create device error %x\n", status );
1042 return status;
1045 RegCreateKeyExW( HKEY_LOCAL_MACHINE, mounted_devicesW, 0, NULL,
1046 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &mount_key, NULL );
1048 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, devicemapW, 0, NULL, REG_OPTION_VOLATILE,
1049 KEY_ALL_ACCESS, NULL, &devicemap_key, NULL ))
1050 RegCloseKey( devicemap_key );
1052 RtlInitUnicodeString( &nameW, harddiskW );
1053 status = IoCreateDriver( &nameW, harddisk_driver_entry );
1055 initialize_dbus();
1056 initialize_diskarbitration();
1058 #ifdef _WIN64
1059 /* create a symlink so that the Wine port overrides key can be edited with 32-bit reg or regedit */
1060 RegCreateKeyExW( HKEY_LOCAL_MACHINE, wow64_ports_keyW, 0, NULL, REG_OPTION_CREATE_LINK,
1061 KEY_SET_VALUE, NULL, &wow64_ports_key, NULL );
1062 RegSetValueExW( wow64_ports_key, symbolic_link_valueW, 0, REG_LINK,
1063 (BYTE *)qualified_ports_keyW, sizeof(qualified_ports_keyW) );
1064 RegCloseKey( wow64_ports_key );
1065 #endif
1067 RtlInitUnicodeString( &nameW, driver_serialW );
1068 IoCreateDriver( &nameW, serial_driver_entry );
1070 RtlInitUnicodeString( &nameW, driver_parallelW );
1071 IoCreateDriver( &nameW, parallel_driver_entry );
1073 return status;