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,
|
2009-01-03 13:12:22 +08:00
|
|
|
2005, 2006, 2007, 2008, 2009 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"
|
2008-02-15 00:47:11 +08:00
|
|
|
#include <wchar.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-01-04 01:44:26 +08:00
|
|
|
reg_key::reg_key (HKEY top, REGSAM access, ...): _disposition (0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
va_list av;
|
|
|
|
va_start (av, access);
|
|
|
|
build_reg (top, access, av);
|
|
|
|
va_end (av);
|
|
|
|
}
|
|
|
|
|
2004-12-03 10:00:37 +08:00
|
|
|
/* Opens a key under the appropriate Cygwin key.
|
|
|
|
Do not use HKCU per MS KB 199190 */
|
2005-02-14 02:17:29 +08:00
|
|
|
|
2006-01-04 01:44:26 +08:00
|
|
|
reg_key::reg_key (bool isHKLM, REGSAM access, ...): _disposition (0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
va_list av;
|
2004-12-03 10:00:37 +08:00
|
|
|
HKEY top;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-12-03 10:00:37 +08:00
|
|
|
if (isHKLM)
|
|
|
|
top = HKEY_LOCAL_MACHINE;
|
|
|
|
else
|
|
|
|
{
|
2005-02-14 02:17:29 +08:00
|
|
|
char name[128];
|
|
|
|
const char *names[2] = {cygheap->user.get_windows_id (name), ".DEFAULT"};
|
2004-12-03 10:00:37 +08:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
key_is_invalid = RegOpenKeyEx (HKEY_USERS, names[i], 0, access, &top);
|
|
|
|
if (key_is_invalid == ERROR_SUCCESS)
|
|
|
|
goto OK;
|
|
|
|
debug_printf ("HKU\\%s failed, Win32 error %ld", names[i], key_is_invalid);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OK:
|
2005-02-14 02:17:29 +08:00
|
|
|
new (this) reg_key (top, access, "SOFTWARE",
|
2004-12-03 10:00:37 +08:00
|
|
|
CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
|
|
|
|
if (top != HKEY_LOCAL_MACHINE)
|
|
|
|
RegCloseKey (top);
|
|
|
|
if (key_is_invalid)
|
|
|
|
return;
|
2005-02-14 02:17:29 +08:00
|
|
|
|
2004-12-03 10:00:37 +08:00
|
|
|
top = key;
|
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);
|
|
|
|
if (top != key)
|
|
|
|
RegCloseKey (top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
reg_key::build_reg (HKEY top, REGSAM access, va_list av)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
HKEY r = top;
|
2000-05-23 22:08:52 +08:00
|
|
|
key_is_invalid = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* FIXME: Most of the time a valid mount area should exist. Perhaps
|
|
|
|
we should just try an open of the correct key first and only resort
|
|
|
|
to this method in the unlikely situation that it's the first time
|
|
|
|
the current mount areas are being used. */
|
|
|
|
|
|
|
|
while ((name = va_arg (av, char *)) != NULL)
|
|
|
|
{
|
|
|
|
int res = RegCreateKeyExA (r,
|
|
|
|
name,
|
|
|
|
0,
|
2009-12-16 21:09:46 +08:00
|
|
|
NULL,
|
2000-02-18 03:38:33 +08:00
|
|
|
REG_OPTION_NON_VOLATILE,
|
|
|
|
access,
|
|
|
|
&sec_none_nih,
|
|
|
|
&key,
|
2006-01-04 01:44:26 +08:00
|
|
|
&_disposition);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (r != top)
|
|
|
|
RegCloseKey (r);
|
|
|
|
r = key;
|
|
|
|
if (res != ERROR_SUCCESS)
|
|
|
|
{
|
2000-05-23 22:08:52 +08:00
|
|
|
key_is_invalid = res;
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("failed to create key %s in the registry", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the current registry key, return the specific int value
|
|
|
|
requested. Return def on failure. */
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::get_int (const char *name, int def)
|
|
|
|
{
|
|
|
|
DWORD type;
|
|
|
|
DWORD dst;
|
|
|
|
DWORD size = sizeof (dst);
|
|
|
|
|
2000-05-23 22:08:52 +08:00
|
|
|
if (key_is_invalid)
|
|
|
|
return def;
|
|
|
|
|
2009-10-20 22:54:47 +08:00
|
|
|
LONG res = RegQueryValueExA (key, name, 0, &type, (LPBYTE) &dst, &size);
|
|
|
|
|
|
|
|
if (type != REG_DWORD || res != ERROR_SUCCESS)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::get_int (const PWCHAR name, int def)
|
|
|
|
{
|
|
|
|
DWORD type;
|
|
|
|
DWORD dst;
|
|
|
|
DWORD size = sizeof (dst);
|
|
|
|
|
|
|
|
if (key_is_invalid)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
LONG res = RegQueryValueExW (key, name, 0, &type, (LPBYTE) &dst, &size);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (type != REG_DWORD || res != ERROR_SUCCESS)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the current registry key, set a specific int value. */
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::set_int (const char *name, int val)
|
|
|
|
{
|
|
|
|
DWORD value = val;
|
2000-05-23 22:08:52 +08:00
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
return (int) RegSetValueExA (key, name, 0, REG_DWORD,
|
2009-10-20 22:54:47 +08:00
|
|
|
(const BYTE *) &value, sizeof (value));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::set_int (const PWCHAR name, int val)
|
|
|
|
{
|
|
|
|
DWORD value = val;
|
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
|
|
|
|
|
|
|
return (int) RegSetValueExW (key, name, 0, REG_DWORD,
|
|
|
|
(const BYTE *) &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. */
|
|
|
|
|
|
|
|
int
|
2009-10-20 22:54:47 +08:00
|
|
|
reg_key::get_string (const char *name, char *dst, size_t max, const char *def)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
DWORD size = max;
|
|
|
|
DWORD type;
|
2000-05-23 22:08:52 +08:00
|
|
|
LONG res;
|
|
|
|
|
|
|
|
if (key_is_invalid)
|
|
|
|
res = key_is_invalid;
|
|
|
|
else
|
2009-10-20 22:54:47 +08:00
|
|
|
res = RegQueryValueExA (key, name, 0, &type, (LPBYTE) dst, &size);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
|
2000-05-23 22:08:52 +08:00
|
|
|
strcpy (dst, def);
|
2000-02-18 03:38:33 +08:00
|
|
|
return (int) res;
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:54:47 +08:00
|
|
|
int
|
|
|
|
reg_key::get_string (const PWCHAR name, PWCHAR dst, size_t max, const PWCHAR def)
|
|
|
|
{
|
|
|
|
DWORD size = max;
|
|
|
|
DWORD type;
|
|
|
|
LONG res;
|
|
|
|
|
|
|
|
if (key_is_invalid)
|
|
|
|
res = key_is_invalid;
|
|
|
|
else
|
|
|
|
res = RegQueryValueExW (key, name, 0, &type, (LPBYTE) dst, &size);
|
|
|
|
|
|
|
|
if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
|
|
|
|
wcscpy (dst, def);
|
|
|
|
return (int) res;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Given the current registry key, set a specific string value. */
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::set_string (const char *name, const char *src)
|
|
|
|
{
|
2000-05-23 22:08:52 +08:00
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
2009-10-20 22:54:47 +08:00
|
|
|
return (int) RegSetValueExA (key, name, 0, REG_SZ, (const BYTE*) src,
|
2000-02-18 03:38:33 +08:00
|
|
|
strlen (src) + 1);
|
|
|
|
}
|
|
|
|
|
2009-10-20 22:54:47 +08:00
|
|
|
int
|
|
|
|
reg_key::set_string (const PWCHAR name, const PWCHAR src)
|
|
|
|
{
|
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
|
|
|
return (int) RegSetValueExW (key, name, 0, REG_SZ, (const BYTE*) src,
|
|
|
|
(wcslen (src) + 1) * sizeof (WCHAR));
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Return the handle to key. */
|
|
|
|
|
|
|
|
HKEY
|
|
|
|
reg_key::get_key ()
|
|
|
|
{
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete subkey of current key. Returns the error code from the
|
|
|
|
RegDeleteKeyA invocation. */
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::kill (const char *name)
|
|
|
|
{
|
2000-05-23 22:08:52 +08:00
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
2000-02-18 03:38:33 +08:00
|
|
|
return RegDeleteKeyA (key, name);
|
|
|
|
}
|
|
|
|
|
2000-07-29 06:33:43 +08:00
|
|
|
/* Delete the value specified by name of current key. Returns the error code
|
|
|
|
from the RegDeleteValueA invocation. */
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::killvalue (const char *name)
|
|
|
|
{
|
|
|
|
if (key_is_invalid)
|
|
|
|
return key_is_invalid;
|
|
|
|
return RegDeleteValueA (key, name);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
reg_key::~reg_key ()
|
|
|
|
{
|
2000-05-23 22:08:52 +08:00
|
|
|
if (!key_is_invalid)
|
2000-02-18 03:38:33 +08:00
|
|
|
RegCloseKey (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
|
|
|
|
get_registry_hive_path (const PWCHAR name, PWCHAR path)
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2008-02-15 00:47:11 +08:00
|
|
|
WCHAR key[256], *kend;
|
2000-06-20 01:36:30 +08:00
|
|
|
HKEY hkey;
|
|
|
|
|
2004-11-21 03:09:19 +08:00
|
|
|
if (!name || !path)
|
2000-06-20 01:36:30 +08:00
|
|
|
return NULL;
|
2010-02-12 09:04:52 +08:00
|
|
|
kend = wcpcpy (key, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
|
2008-02-15 00:47:11 +08:00
|
|
|
wcpcpy (kend, name);
|
|
|
|
if (!RegOpenKeyExW (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2008-03-07 19:24:51 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR buf = tp.w_get ();
|
2000-06-20 01:36:30 +08:00
|
|
|
DWORD type, siz;
|
|
|
|
|
2008-02-15 00:47:11 +08:00
|
|
|
path[0] = L'\0';
|
|
|
|
if (!RegQueryValueExW (hkey, L"ProfileImagePath", 0, &type,
|
2008-03-07 19:24:51 +08:00
|
|
|
(BYTE *)buf, (siz = NT_MAX_PATH, &siz)))
|
2008-02-15 00:47:11 +08:00
|
|
|
ExpandEnvironmentStringsW (buf, path, NT_MAX_PATH);
|
2000-06-20 01:36:30 +08:00
|
|
|
RegCloseKey (hkey);
|
2004-10-26 09:53:27 +08:00
|
|
|
if (path[0])
|
|
|
|
return path;
|
2000-06-20 01:36:30 +08:00
|
|
|
}
|
2008-02-15 00:47:11 +08:00
|
|
|
debug_printf ("HKLM\\%W not found", key);
|
2000-06-20 01:36:30 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-15 00:47:11 +08:00
|
|
|
load_registry_hive (const PWCHAR name)
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2008-03-07 19:24:51 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
PWCHAR path = tp.w_get ();
|
2000-06-20 01:36:30 +08:00
|
|
|
HKEY hkey;
|
2000-06-23 02:54:26 +08:00
|
|
|
LONG ret;
|
2000-06-20 01:36:30 +08:00
|
|
|
|
2004-11-21 03:09:19 +08:00
|
|
|
if (!name)
|
2000-06-20 01:36:30 +08:00
|
|
|
return;
|
2000-06-23 02:54:26 +08:00
|
|
|
/* Check if user hive is already loaded. */
|
2008-02-15 00:47:11 +08:00
|
|
|
if (!RegOpenKeyExW (HKEY_USERS, name, 0, KEY_READ, &hkey))
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2008-02-15 00:47:11 +08:00
|
|
|
debug_printf ("User registry hive for %W already exists", name);
|
2000-06-20 01:36:30 +08:00
|
|
|
RegCloseKey (hkey);
|
|
|
|
return;
|
|
|
|
}
|
2004-11-21 03:09:19 +08:00
|
|
|
if (get_registry_hive_path (name, path))
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
2008-02-15 00:47:11 +08:00
|
|
|
wcscat (path, L"\\NTUSER.DAT");
|
|
|
|
if ((ret = RegLoadKeyW (HKEY_USERS, name, path)) != ERROR_SUCCESS)
|
|
|
|
debug_printf ("Loading user registry hive for %W failed: %d", name, ret);
|
2000-06-20 01:36:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|