Eliminate forward declarations, make functions static.
[wine] / misc / registry.c
1 /*
2  *      Registry Functions
3  *
4  * Copyright 1996 Marcus Meissner
5  * Copyright 1998 Matthew Becker
6  * Copyright 1999 Sylvain St-Germain
7  *
8  * December 21, 1997 - Kevin Cozens
9  * Fixed bugs in the _w95_loadreg() function. Added extra information
10  * regarding the format of the Windows '95 registry files.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  *
26  * TODO
27  *    Security access
28  *    Option handling
29  *    Time for RegEnumKey*, RegQueryInfoKey*
30  */
31
32 #include "config.h"
33 #include "wine/port.h"
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #ifdef HAVE_SYS_IOCTL_H
47 #include <sys/ioctl.h>
48 #endif
49 #ifdef HAVE_LINUX_HDREG_H
50 # include <linux/hdreg.h>
51 #endif
52
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
55 #include "ntstatus.h"
56 #include "windef.h"
57 #include "winbase.h"
58 #include "winerror.h"
59 #include "winioctl.h"
60 #include "ntddscsi.h"
61
62 #include "wine/library.h"
63 #include "wine/server.h"
64 #include "wine/unicode.h"
65
66 #include "wine/debug.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(reg);
69
70
71 /******************************************************************************
72  * allocate_default_keys [Internal]
73  * Registry initialisation, allocates some default keys.
74  */
75 static ULONG allocate_default_keys(void)
76 {
77     static const WCHAR StatDataW[] = {'D','y','n','D','a','t','a','\\',
78                                       'P','e','r','f','S','t','a','t','s','\\',
79                                       'S','t','a','t','D','a','t','a',0};
80     static const WCHAR ConfigManagerW[] = {'D','y','n','D','a','t','a','\\',
81                                            'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
82                                             'E','n','u','m',0};
83     static const WCHAR Clone[] = {'M','a','c','h','i','n','e','\\',
84                                   'S','y','s','t','e','m','\\',
85                                   'C','l','o','n','e',0};
86     HKEY hkey;
87     ULONG dispos;
88     OBJECT_ATTRIBUTES attr;
89     UNICODE_STRING nameW;
90
91     attr.Length = sizeof(attr);
92     attr.RootDirectory = 0;
93     attr.ObjectName = &nameW;
94     attr.Attributes = 0;
95     attr.SecurityDescriptor = NULL;
96     attr.SecurityQualityOfService = NULL;
97
98     RtlInitUnicodeString( &nameW, StatDataW );
99     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &dispos )) NtClose( hkey );
100     if (dispos == REG_OPENED_EXISTING_KEY)
101         return dispos; /* someone else already loaded the registry */
102
103     RtlInitUnicodeString( &nameW, ConfigManagerW );
104     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
105
106     /* this key is generated when the nt-core booted successfully */
107     RtlInitUnicodeString( &nameW, Clone );
108     if (!NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) NtClose( hkey );
109
110     return dispos;
111 }
112
113
114 /******************************************************************
115  *              init_cdrom_registry
116  *
117  * Initializes registry to contain scsi info about the cdrom in NT.
118  * All devices (even not real scsi ones) have this info in NT.
119  * TODO: for now it only works for non scsi devices
120  * NOTE: programs usually read these registry entries after sending the
121  *       IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
122  */
123 static void init_cdrom_registry( HANDLE handle )
124 {
125     OBJECT_ATTRIBUTES attr;
126     UNICODE_STRING nameW;
127     WCHAR dataW[50];
128     DWORD lenW;
129     char buffer[40];
130     DWORD value;
131     const char *data;
132     HKEY scsiKey;
133     HKEY portKey;
134     HKEY busKey;
135     HKEY targetKey;
136     DWORD disp;
137     IO_STATUS_BLOCK io;
138     SCSI_ADDRESS scsi_addr;
139
140     if (NtDeviceIoControlFile( handle, 0, NULL, NULL, &io, IOCTL_SCSI_GET_ADDRESS,
141                                NULL, 0, &scsi_addr, sizeof(scsi_addr) ))
142         return;
143
144     attr.Length = sizeof(attr);
145     attr.RootDirectory = 0;
146     attr.ObjectName = &nameW;
147     attr.Attributes = 0;
148     attr.SecurityDescriptor = NULL;
149     attr.SecurityQualityOfService = NULL;
150
151     /* Ensure there is Scsi key */
152     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Machine\\HARDWARE\\DEVICEMAP\\Scsi" ) ||
153         NtCreateKey( &scsiKey, KEY_ALL_ACCESS, &attr, 0,
154                      NULL, REG_OPTION_VOLATILE, &disp ))
155     {
156         ERR("Cannot create DEVICEMAP\\Scsi registry key\n" );
157         return;
158     }
159     RtlFreeUnicodeString( &nameW );
160
161     snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr.PortNumber);
162     attr.RootDirectory = scsiKey;
163     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
164         NtCreateKey( &portKey, KEY_ALL_ACCESS, &attr, 0,
165                      NULL, REG_OPTION_VOLATILE, &disp ))
166     {
167         ERR("Cannot create DEVICEMAP\\Scsi Port registry key\n" );
168         return;
169     }
170     RtlFreeUnicodeString( &nameW );
171
172     RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
173     data = "atapi";
174     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
175     NtSetValueKey( portKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
176     RtlFreeUnicodeString( &nameW );
177     value = 10;
178     RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
179     NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
180     RtlFreeUnicodeString( &nameW );
181     value = 0;
182 #ifdef HDIO_GET_DMA
183     {
184         int fd, dma;
185         if (!wine_server_handle_to_fd( handle, 0, &fd, NULL ))
186         {
187             if (ioctl(fd,HDIO_GET_DMA, &dma) != -1) value = dma;
188             wine_server_release_fd( handle, fd );
189         }
190     }
191 #endif
192     RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
193     NtSetValueKey( portKey,&nameW, 0, REG_DWORD, (BYTE *)&value, sizeof(DWORD));
194     RtlFreeUnicodeString( &nameW );
195
196     snprintf(buffer,40,"Scsi Bus %d", scsi_addr.PathId);
197     attr.RootDirectory = portKey;
198     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
199         NtCreateKey( &busKey, KEY_ALL_ACCESS, &attr, 0,
200                      NULL, REG_OPTION_VOLATILE, &disp ))
201     {
202         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus registry key\n" );
203         return;
204     }
205     RtlFreeUnicodeString( &nameW );
206
207     attr.RootDirectory = busKey;
208     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, "Initiator Id 255" ) ||
209         NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
210                      NULL, REG_OPTION_VOLATILE, &disp ))
211     {
212         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus\\Initiator Id 255 registry key\n" );
213         return;
214     }
215     RtlFreeUnicodeString( &nameW );
216     NtClose( targetKey );
217
218     snprintf(buffer,40,"Target Id %d", scsi_addr.TargetId);
219     attr.RootDirectory = busKey;
220     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, buffer ) ||
221         NtCreateKey( &targetKey, KEY_ALL_ACCESS, &attr, 0,
222                      NULL, REG_OPTION_VOLATILE, &disp ))
223     {
224         ERR("Cannot create DEVICEMAP\\Scsi Port\\Scsi Bus 0\\Target Id registry key\n" );
225         return;
226     }
227     RtlFreeUnicodeString( &nameW );
228
229     RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
230     data = "CdRomPeripheral";
231     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
232     NtSetValueKey( targetKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
233     RtlFreeUnicodeString( &nameW );
234     /* FIXME - maybe read the real identifier?? */
235     RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
236     data = "Wine CDROM";
237     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
238     NtSetValueKey( targetKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
239     RtlFreeUnicodeString( &nameW );
240     /* FIXME - we always use Cdrom0 - do not know about the nt behaviour */
241     RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
242     data = "Cdrom0";
243     RtlMultiByteToUnicodeN( dataW, 50, &lenW, data, strlen(data));
244     NtSetValueKey( targetKey, &nameW, 0, REG_SZ, (BYTE*)dataW, lenW );
245     RtlFreeUnicodeString( &nameW );
246
247     NtClose( targetKey );
248     NtClose( busKey );
249     NtClose( portKey );
250     NtClose( scsiKey );
251 }
252
253
254 /* create the hardware registry branch */
255 static void create_hardware_branch(void)
256 {
257     int i;
258     HANDLE handle;
259     char drive[] = "\\\\.\\A:";
260
261     /* create entries for cdroms */
262     for (i = 0; i < 26; i++)
263     {
264         drive[4] = 'A' + i;
265         handle = CreateFileA( drive, 0, 0, NULL, OPEN_EXISTING, 0, 0 );
266         if (handle == INVALID_HANDLE_VALUE) continue;
267         init_cdrom_registry( handle );
268         CloseHandle( handle );
269     }
270 }
271
272
273 /* convert the drive type entries from the old format to the new one */
274 static void convert_drive_types(void)
275 {
276     static const WCHAR TypeW[] = {'T','y','p','e',0};
277     static const WCHAR drive_types_keyW[] = {'M','a','c','h','i','n','e','\\',
278                                              'S','o','f','t','w','a','r','e','\\',
279                                              'W','i','n','e','\\',
280                                              'D','r','i','v','e','s',0 };
281     WCHAR driveW[] = {'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
282                       'W','i','n','e','\\','W','i','n','e','\\',
283                       'C','o','n','f','i','g','\\','D','r','i','v','e',' ','A',0};
284     char tmp[32*sizeof(WCHAR) + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
285     OBJECT_ATTRIBUTES attr;
286     UNICODE_STRING nameW;
287     DWORD dummy;
288     ULONG disp;
289     HKEY hkey_old, hkey_new;
290     int i;
291
292     attr.Length = sizeof(attr);
293     attr.RootDirectory = 0;
294     attr.ObjectName = &nameW;
295     attr.Attributes = 0;
296     attr.SecurityDescriptor = NULL;
297     attr.SecurityQualityOfService = NULL;
298     RtlInitUnicodeString( &nameW, drive_types_keyW );
299
300     if (NtCreateKey( &hkey_new, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &disp )) return;
301     if (disp != REG_CREATED_NEW_KEY)
302     {
303         NtClose( hkey_new );
304         return;
305     }
306
307     for (i = 0; i < 26; i++)
308     {
309         RtlInitUnicodeString( &nameW, driveW );
310         nameW.Buffer[(nameW.Length / sizeof(WCHAR)) - 1] = 'A' + i;
311         if (NtOpenKey( &hkey_old, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) continue;
312         RtlInitUnicodeString( &nameW, TypeW );
313         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
314         {
315             WCHAR valueW[] = {'A',':',0};
316             WCHAR *type = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
317
318             valueW[0] = 'A' + i;
319             RtlInitUnicodeString( &nameW, valueW );
320             NtSetValueKey( hkey_new, &nameW, 0, REG_SZ, type, (strlenW(type) + 1) * sizeof(WCHAR) );
321             MESSAGE( "Converted drive type to new entry HKLM\\Software\\Wine\\Drives \"%c:\" = %s\n",
322                      'A' + i, debugstr_w(type) );
323         }
324         NtClose( hkey_old );
325     }
326     NtClose( hkey_new );
327 }
328
329
330 /* convert the environment variable entries from the old format to the new one */
331 static void convert_environment( HKEY hkey_current_user )
332 {
333     static const WCHAR wineW[] = {'M','a','c','h','i','n','e','\\',
334                                   'S','o','f','t','w','a','r','e','\\',
335                                   'W','i','n','e','\\','W','i','n','e','\\',
336                                   'C','o','n','f','i','g','\\','W','i','n','e',0};
337     static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
338     static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
339     static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
340     static const WCHAR systemrootW[] = {'S','y','s','t','e','m','r','o','o','t',0};
341     static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
342     static const WCHAR envW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
343     static const WCHAR tempW[] = {'T','E','M','P',0};
344     static const WCHAR tmpW[] = {'T','M','P',0};
345     static const WCHAR pathW[] = {'P','A','T','H',0};
346     static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
347     static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
348
349     char buffer[1024*sizeof(WCHAR) + sizeof(KEY_VALUE_PARTIAL_INFORMATION)];
350     KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
351     OBJECT_ATTRIBUTES attr;
352     UNICODE_STRING nameW;
353     DWORD dummy;
354     ULONG disp;
355     HKEY hkey_old, hkey_env;
356
357     attr.Length = sizeof(attr);
358     attr.RootDirectory = 0;
359     attr.ObjectName = &nameW;
360     attr.Attributes = 0;
361     attr.SecurityDescriptor = NULL;
362     attr.SecurityQualityOfService = NULL;
363     RtlInitUnicodeString( &nameW, wineW );
364
365     if (NtOpenKey( &hkey_old, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) return;
366
367     attr.RootDirectory = hkey_current_user;
368     RtlInitUnicodeString( &nameW, envW );
369     if (NtCreateKey( &hkey_env, KEY_ALL_ACCESS, &attr, 0, NULL, 0, &disp ))
370     {
371         NtClose( hkey_old );
372         return;
373     }
374
375     /* Test for existence of TEMP */
376     RtlInitUnicodeString( &nameW, tempW );
377     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
378     {
379         /* convert TEMP */
380         RtlInitUnicodeString( &nameW, tempW );
381         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
382         {
383             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
384             RtlInitUnicodeString( &nameW, tmpW );
385             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
386             MESSAGE( "Converted temp dir to new entry HKCU\\Environment \"TEMP\" = %s\n",
387                     debugstr_w( (WCHAR*)info->Data ) );
388         }
389     }
390
391     /* Test for existence of PATH */
392     RtlInitUnicodeString( &nameW, pathW );
393     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
394     {
395         /* convert PATH */
396         RtlInitUnicodeString( &nameW, pathW );
397         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
398         {
399             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
400             MESSAGE( "Converted path dir to new entry HKCU\\Environment \"PATH\" = %s\n",
401                     debugstr_w( (WCHAR*)info->Data ) );
402         }
403     }
404
405     /* Test for existence of USERPROFILE */
406     RtlInitUnicodeString( &nameW, userprofileW );
407     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
408     {
409         /* convert USERPROFILE */
410         RtlInitUnicodeString( &nameW, profileW );
411         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
412         {
413             RtlInitUnicodeString( &nameW, userprofileW );
414             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
415             MESSAGE( "Converted profile dir to new entry HKCU\\Environment \"USERPROFILE\" = %s\n",
416                     debugstr_w( (WCHAR*)info->Data ) );
417         }
418     }
419
420     /* Test for existence of windir */
421     RtlInitUnicodeString( &nameW, windirW );
422     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
423     {
424         /* convert windir */
425         RtlInitUnicodeString( &nameW, windowsW );
426         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
427         {
428             RtlInitUnicodeString( &nameW, windirW );
429             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
430             RtlInitUnicodeString( &nameW, systemrootW );
431             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
432             MESSAGE( "Converted windows dir to new entry HKCU\\Environment \"windir\" = %s\n",
433                     debugstr_w( (WCHAR*)info->Data ) );
434         }
435     }
436         
437     /* Test for existence of winsysdir */
438     RtlInitUnicodeString( &nameW, winsysdirW );
439     if (NtQueryValueKey(hkey_env, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
440     {
441         /* convert winsysdir */
442         RtlInitUnicodeString( &nameW, systemW );
443         if (!NtQueryValueKey( hkey_old, &nameW, KeyValuePartialInformation, buffer, sizeof(buffer), &dummy ))
444         {
445             RtlInitUnicodeString( &nameW, winsysdirW );
446             NtSetValueKey( hkey_env, &nameW, 0, info->Type, info->Data, info->DataLength );
447             MESSAGE( "Converted system dir to new entry HKCU\\Environment \"winsysdir\" = %s\n",
448                     debugstr_w( (WCHAR*)info->Data ) );
449         }
450     }
451     NtClose( hkey_old );
452     NtClose( hkey_env );
453 }
454
455
456 /* load all registry (native and global and home) */
457 void SHELL_LoadRegistry( void )
458 {
459     HKEY hkey_current_user;
460     OBJECT_ATTRIBUTES attr;
461     UNICODE_STRING nameW;
462     ULONG dispos;
463
464     TRACE("(void)\n");
465
466     attr.Length = sizeof(attr);
467     attr.RootDirectory = 0;
468     attr.ObjectName = &nameW;
469     attr.Attributes = 0;
470     attr.SecurityDescriptor = NULL;
471     attr.SecurityQualityOfService = NULL;
472
473     dispos = allocate_default_keys();
474     if (dispos == REG_OPENED_EXISTING_KEY)
475         return; /* someone else already loaded the registry */
476
477     RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey_current_user );
478
479     /* load home registries */
480
481     SERVER_START_REQ( load_user_registries )
482     {
483         req->hkey = hkey_current_user;
484         wine_server_call( req );
485     }
486     SERVER_END_REQ;
487
488     /* create hardware registry branch */
489
490     create_hardware_branch();
491
492     /* convert keys from config file to new registry format */
493
494     convert_drive_types();
495     convert_environment( hkey_current_user );
496
497     NtClose(hkey_current_user);
498 }