2000-02-18 03:38:33 +08:00
|
|
|
/* registry.cc: registry interface
|
|
|
|
|
2005-02-20 12:25:33 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
2011-04-19 18:02:06 +08:00
|
|
|
2005, 2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "registry.h"
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2004-12-03 10:00:37 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2008-03-07 19:24:51 +08:00
|
|
|
#include "tls_pbuf.h"
|
2011-04-19 18:02:06 +08:00
|
|
|
#include "ntdll.h"
|
2008-02-15 00:47:11 +08:00
|
|
|
#include <wchar.h>
|
2011-04-19 18:02:06 +08:00
|
|
|
#include <alloca.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-12-03 10:00:37 +08:00
|
|
|
/* Opens a key under the appropriate Cygwin key.
|
|
|
|
Do not use HKCU per MS KB 199190 */
|
2011-04-19 18:02:06 +08:00
|
|
|
static NTSTATUS
|
|
|
|
top_key (bool isHKLM, REGSAM access, PHANDLE top)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
WCHAR rbuf[PATH_MAX], *p;
|
|
|
|
UNICODE_STRING rpath;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-04-19 18:02:06 +08:00
|
|
|
InitializeObjectAttributes (&attr, &rpath, OBJ_CASE_INSENSITIVE, NULL, NULL);
|
2004-12-03 10:00:37 +08:00
|
|
|
if (isHKLM)
|
2011-04-19 18:02:06 +08:00
|
|
|
{
|
|
|
|
wcpcpy (rbuf, L"\\Registry\\Machine");
|
|
|
|
RtlInitUnicodeString (&rpath, rbuf);
|
|
|
|
status = NtOpenKey (top, access, &attr);
|
|
|
|
}
|
2004-12-03 10:00:37 +08:00
|
|
|
else
|
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
WCHAR name[128];
|
|
|
|
PCWSTR names[2] = {cygheap->user.get_windows_id (name),
|
|
|
|
L".DEFAULT"};
|
|
|
|
|
|
|
|
p = wcpcpy (rbuf, L"\\Registry\\User\\");
|
2004-12-03 10:00:37 +08:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
wcpcpy (p, names[i]);
|
|
|
|
RtlInitUnicodeString (&rpath, rbuf);
|
|
|
|
status = NtOpenKey (top, access, &attr);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
break;
|
2004-12-03 10:00:37 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-19 18:02:06 +08:00
|
|
|
return status;
|
|
|
|
}
|
2005-02-14 02:17:29 +08:00
|
|
|
|
2011-04-19 18:02:06 +08:00
|
|
|
reg_key::reg_key (HKEY top, REGSAM access, ...): _disposition (0)
|
|
|
|
{
|
|
|
|
va_list av;
|
2000-02-18 03:38:33 +08:00
|
|
|
va_start (av, access);
|
2004-12-03 10:00:37 +08:00
|
|
|
build_reg (top, access, av);
|
2000-02-18 03:38:33 +08:00
|
|
|
va_end (av);
|
2011-04-19 18:02:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
reg_key::reg_key (bool isHKLM, REGSAM access, ...): _disposition (0)
|
|
|
|
{
|
|
|
|
va_list av;
|
|
|
|
HANDLE top;
|
|
|
|
|
|
|
|
key_is_invalid = top_key (isHKLM, access, &top);
|
|
|
|
if (NT_SUCCESS (key_is_invalid))
|
|
|
|
{
|
|
|
|
new (this) reg_key ((HKEY) top, access, L"SOFTWARE",
|
|
|
|
_WIDE (CYGWIN_INFO_CYGWIN_REGISTRY_NAME), NULL);
|
|
|
|
NtClose (top);
|
|
|
|
if (key_is_invalid)
|
|
|
|
return;
|
|
|
|
top = key;
|
|
|
|
va_start (av, access);
|
|
|
|
build_reg ((HKEY) top, access, av);
|
|
|
|
va_end (av);
|
|
|
|
if (top != key)
|
|
|
|
NtClose (top);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
reg_key::build_reg (HKEY top, REGSAM access, va_list av)
|
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
PWCHAR name;
|
|
|
|
HANDLE r;
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
if (top != HKEY_LOCAL_MACHINE && top != HKEY_CURRENT_USER)
|
|
|
|
r = (HANDLE) top;
|
|
|
|
else if (!NT_SUCCESS (top_key (top == HKEY_LOCAL_MACHINE, access, &r)))
|
|
|
|
return;
|
2000-05-23 22:08:52 +08:00
|
|
|
key_is_invalid = 0;
|
2011-04-19 18:02:06 +08:00
|
|
|
while ((name = va_arg (av, PWCHAR)) != NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
RtlInitUnicodeString (&uname, name);
|
|
|
|
InitializeObjectAttributes (&attr, &uname,
|
|
|
|
OBJ_CASE_INSENSITIVE | OBJ_OPENIF, r, NULL);
|
|
|
|
|
|
|
|
status = NtCreateKey (&key, access, &attr, 0, NULL,
|
|
|
|
REG_OPTION_NON_VOLATILE, &_disposition);
|
|
|
|
if (r != (HANDLE) top)
|
|
|
|
NtClose (r);
|
2000-02-18 03:38:33 +08:00
|
|
|
r = key;
|
2011-04-19 18:02:06 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
key_is_invalid = status;
|
|
|
|
debug_printf ("failed to create key %S in the registry", uname);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 21:15:46 +08:00
|
|
|
/* Given the current registry key, return the specific DWORD value
|
2000-02-18 03:38:33 +08:00
|
|
|
requested. Return def on failure. */
|
|
|
|
|
2011-04-23 21:15:46 +08:00
|
|
|
DWORD
|
|
|
|
reg_key::get_dword (PCWSTR name, DWORD def)
|
2009-10-20 22:54:47 +08:00
|
|
|
{
|
|
|
|
if (key_is_invalid)
|
|
|
|
return def;
|
|
|
|
|
2011-04-19 18:02:06 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
ULONG size = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + sizeof (DWORD);
|
|
|
|
ULONG rsize;
|
|
|
|
PKEY_VALUE_PARTIAL_INFORMATION vbuf = (PKEY_VALUE_PARTIAL_INFORMATION)
|
|
|
|
alloca (size);
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&uname, name);
|
|
|
|
status = NtQueryValueKey (key, &uname, KeyValuePartialInformation, vbuf,
|
|
|
|
size, &rsize);
|
|
|
|
if (status != STATUS_SUCCESS || vbuf->Type != REG_DWORD)
|
2000-02-18 03:38:33 +08:00
|
|
|
return def;
|
2011-04-23 21:15:46 +08:00
|
|
|
DWORD *dst = (DWORD *) vbuf->Data;
|
|
|
|
return *dst;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2011-04-23 21:15:46 +08:00
|
|
|
/* Given the current registry key, set a specific DWORD value. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-04-23 21:15:46 +08:00
|
|
|
NTSTATUS
|
|
|
|
reg_key::set_dword (PCWSTR name, DWORD val)
|
2009-10-20 22:54:47 +08:00
|
|
|
{
|
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
|
|
|
|
2011-04-19 18:02:06 +08:00
|
|
|
DWORD value = (DWORD) val;
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
RtlInitUnicodeString (&uname, name);
|
2011-04-23 21:15:46 +08:00
|
|
|
return NtSetValueKey (key, &uname, 0, REG_DWORD, &value, sizeof (value));
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the current registry key, return the specific string value
|
|
|
|
requested. Return zero on success, non-zero on failure. */
|
|
|
|
|
2011-04-23 21:15:46 +08:00
|
|
|
NTSTATUS
|
2011-04-19 18:02:06 +08:00
|
|
|
reg_key::get_string (PCWSTR name, PWCHAR dst, size_t max, PCWSTR def)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
NTSTATUS status;
|
2000-05-23 22:08:52 +08:00
|
|
|
|
|
|
|
if (key_is_invalid)
|
2011-04-19 18:02:06 +08:00
|
|
|
{
|
|
|
|
status = key_is_invalid;
|
|
|
|
if (def != NULL)
|
|
|
|
wcpncpy (dst, def, max);
|
|
|
|
}
|
2009-10-20 22:54:47 +08:00
|
|
|
else
|
2011-04-19 18:02:06 +08:00
|
|
|
{
|
|
|
|
UNICODE_STRING uname;
|
|
|
|
ULONG size = sizeof (KEY_VALUE_PARTIAL_INFORMATION) + max * sizeof (WCHAR);
|
|
|
|
ULONG rsize;
|
|
|
|
PKEY_VALUE_PARTIAL_INFORMATION vbuf = (PKEY_VALUE_PARTIAL_INFORMATION)
|
|
|
|
alloca (size);
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&uname, name);
|
|
|
|
status = NtQueryValueKey (key, &uname, KeyValuePartialInformation, vbuf,
|
|
|
|
size, &rsize);
|
|
|
|
if (status != STATUS_SUCCESS || vbuf->Type != REG_SZ)
|
|
|
|
wcpncpy (dst, def, max);
|
|
|
|
else
|
|
|
|
wcpncpy (dst, (PWCHAR) vbuf->Data, max);
|
|
|
|
}
|
2011-04-23 21:15:46 +08:00
|
|
|
return status;
|
2009-10-20 22:54:47 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Given the current registry key, set a specific string value. */
|
|
|
|
|
2011-04-23 21:15:46 +08:00
|
|
|
NTSTATUS
|
2011-04-19 18:02:06 +08:00
|
|
|
reg_key::set_string (PCWSTR name, PCWSTR src)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-05-23 22:08:52 +08:00
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-04-19 18:02:06 +08:00
|
|
|
UNICODE_STRING uname;
|
|
|
|
RtlInitUnicodeString (&uname, name);
|
2011-04-23 21:15:46 +08:00
|
|
|
return NtSetValueKey (key, &uname, 0, REG_SZ, (PVOID) src,
|
|
|
|
(wcslen (src) + 1) * sizeof (WCHAR));
|
2000-07-29 06:33:43 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
reg_key::~reg_key ()
|
|
|
|
{
|
2000-05-23 22:08:52 +08:00
|
|
|
if (!key_is_invalid)
|
2011-04-19 18:02:06 +08:00
|
|
|
NtClose (key);
|
2000-05-23 22:08:52 +08:00
|
|
|
key_is_invalid = 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-06-20 01:36:30 +08:00
|
|
|
|
2008-02-15 00:47:11 +08:00
|
|
|
PWCHAR
|
2011-04-19 18:02:06 +08:00
|
|
|
get_registry_hive_path (PCWSTR name, PWCHAR path)
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2004-11-21 03:09:19 +08:00
|
|
|
if (!name || !path)
|
2000-06-20 01:36:30 +08:00
|
|
|
return NULL;
|
2011-04-19 18:02:06 +08:00
|
|
|
|
|
|
|
WCHAR key[256];
|
|
|
|
UNICODE_STRING buf;
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
tp.u_get (&buf);
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
RTL_QUERY_REGISTRY_TABLE tab[2] = {
|
|
|
|
{ NULL, RTL_QUERY_REGISTRY_NOEXPAND | RTL_QUERY_REGISTRY_DIRECT
|
|
|
|
| RTL_QUERY_REGISTRY_REQUIRED,
|
|
|
|
L"ProfileImagePath", &buf, REG_NONE, NULL, 0 },
|
|
|
|
{ NULL, 0, NULL, NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
wcpcpy (wcpcpy (key, L"ProfileList\\"), name);
|
|
|
|
status = RtlQueryRegistryValues (RTL_REGISTRY_WINDOWS_NT, key, tab,
|
|
|
|
NULL, NULL);
|
|
|
|
if (!NT_SUCCESS (status) || buf.Length == 0)
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2011-05-08 02:56:39 +08:00
|
|
|
debug_printf ("ProfileImagePath for %W not found, status %p", name,
|
2011-04-19 18:02:06 +08:00
|
|
|
status);
|
|
|
|
return NULL;
|
2000-06-20 01:36:30 +08:00
|
|
|
}
|
2011-04-19 18:02:06 +08:00
|
|
|
wcpcpy (path, L"\\??\\");
|
|
|
|
ExpandEnvironmentStringsW (buf.Buffer, path + 4, NT_MAX_PATH - 4);
|
2011-05-08 02:56:39 +08:00
|
|
|
debug_printf ("ProfileImagePath for %W: %W", name, path);
|
2011-04-19 18:02:06 +08:00
|
|
|
return path;
|
2000-06-20 01:36:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-04-19 18:02:06 +08:00
|
|
|
load_registry_hive (PCWSTR name)
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2011-04-19 18:02:06 +08:00
|
|
|
if (!name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Fetch the path. */
|
2008-03-07 19:24:51 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR path = tp.w_get ();
|
2011-04-19 18:02:06 +08:00
|
|
|
if (!get_registry_hive_path (name, path))
|
2000-06-20 01:36:30 +08:00
|
|
|
return;
|
|
|
|
|
2011-04-19 18:02:06 +08:00
|
|
|
WCHAR key[256];
|
|
|
|
UNICODE_STRING ukey, upath;
|
|
|
|
OBJECT_ATTRIBUTES key_attr, path_attr;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
/* Create the object attributes for key and path. */
|
|
|
|
wcpcpy (wcpcpy (key, L"\\Registry\\User\\"), name);
|
|
|
|
RtlInitUnicodeString (&ukey, key);
|
|
|
|
InitializeObjectAttributes (&key_attr, &ukey, OBJ_CASE_INSENSITIVE,
|
|
|
|
NULL, NULL);
|
|
|
|
wcscat (path, L"\\NTUSER.DAT");
|
|
|
|
RtlInitUnicodeString (&upath, path);
|
|
|
|
InitializeObjectAttributes (&path_attr, &upath, OBJ_CASE_INSENSITIVE,
|
|
|
|
NULL, NULL);
|
|
|
|
/* Load file into key. */
|
|
|
|
status = NtLoadKey (&key_attr, &path_attr);
|
|
|
|
if (!NT_SUCCESS (status))
|
2011-05-08 02:56:39 +08:00
|
|
|
debug_printf ("Loading user registry hive %S into %S failed: %p",
|
2011-04-19 18:02:06 +08:00
|
|
|
&upath, &ukey, status);
|
|
|
|
else
|
2011-05-08 02:56:39 +08:00
|
|
|
debug_printf ("Loading user registry hive %S into %S SUCCEEDED: %p",
|
2011-04-19 18:02:06 +08:00
|
|
|
&upath, &ukey, status);
|
|
|
|
}
|