2000-02-18 03:38:33 +08:00
|
|
|
/* registry.cc: registry interface
|
|
|
|
|
2002-06-05 12:01:43 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 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 00:23:51 +08:00
|
|
|
#include "shared_info.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "registry.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include <cygwin/version.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-09-06 13:17:22 +08:00
|
|
|
static char NO_COPY cygnus_class[] = "cygnus";
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
reg_key::reg_key (HKEY top, REGSAM access, ...)
|
|
|
|
{
|
|
|
|
va_list av;
|
|
|
|
va_start (av, access);
|
|
|
|
build_reg (top, access, av);
|
|
|
|
va_end (av);
|
|
|
|
}
|
|
|
|
|
|
|
|
reg_key::reg_key (REGSAM access, ...)
|
|
|
|
{
|
|
|
|
va_list av;
|
|
|
|
|
|
|
|
new (this) reg_key (HKEY_CURRENT_USER, access, "SOFTWARE",
|
|
|
|
CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
|
|
|
|
CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
|
|
|
|
|
|
|
|
HKEY top = key;
|
|
|
|
va_start (av, access);
|
|
|
|
build_reg (top, KEY_READ, av);
|
|
|
|
va_end (av);
|
|
|
|
if (top != key)
|
|
|
|
RegCloseKey (top);
|
|
|
|
}
|
|
|
|
|
|
|
|
reg_key::reg_key (REGSAM access)
|
|
|
|
{
|
|
|
|
new (this) reg_key (HKEY_CURRENT_USER, access, "SOFTWARE",
|
|
|
|
CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
|
|
|
|
CYGWIN_INFO_CYGWIN_REGISTRY_NAME,
|
|
|
|
CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
DWORD disp;
|
|
|
|
int res = RegCreateKeyExA (r,
|
|
|
|
name,
|
|
|
|
0,
|
|
|
|
cygnus_class,
|
|
|
|
REG_OPTION_NON_VOLATILE,
|
|
|
|
access,
|
|
|
|
&sec_none_nih,
|
|
|
|
&key,
|
|
|
|
&disp);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're considering the mounts key, check if it had to
|
|
|
|
be created and set had_to_create appropriately. */
|
|
|
|
if (strcmp (name, CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME) == 0)
|
|
|
|
if (disp == REG_CREATED_NEW_KEY)
|
2001-01-28 13:51:15 +08:00
|
|
|
mount_table->had_to_create_mount_areas++;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
LONG res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) &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,
|
|
|
|
(unsigned char *) &value, sizeof (value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the current registry key, return the specific string value
|
|
|
|
requested. Return zero on success, non-zero on failure. */
|
|
|
|
|
|
|
|
int
|
|
|
|
reg_key::get_string (const char *name, char *dst, size_t max, const char * def)
|
|
|
|
{
|
|
|
|
DWORD size = max;
|
|
|
|
DWORD type;
|
2000-05-23 22:08:52 +08:00
|
|
|
LONG res;
|
|
|
|
|
|
|
|
if (key_is_invalid)
|
|
|
|
res = key_is_invalid;
|
|
|
|
else
|
|
|
|
res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2000-02-18 03:38:33 +08:00
|
|
|
return (int) RegSetValueExA (key, name, 0, REG_SZ, (unsigned char*) src,
|
|
|
|
strlen (src) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
char *
|
|
|
|
get_registry_hive_path (const PSID psid, char *path)
|
|
|
|
{
|
|
|
|
char sid[256];
|
|
|
|
char key[256];
|
|
|
|
HKEY hkey;
|
|
|
|
|
|
|
|
if (!psid || !path)
|
|
|
|
return NULL;
|
2003-09-11 05:01:40 +08:00
|
|
|
cygpsid csid (psid);
|
* fork.cc (fork): Eliminate superfluous call to getuid().
* security.h: New define `NO_SID'. Remove declarations of functions
moved to methods into class cygsid.
(class cygsid): Declare new methods `getfromstr', `get_sid',
`getfrompw', `getfromgr', `get_rid', `get_uid', `get_gid', `string'
and new constructors and operators =, == and !=.
Declare new global cygsids `well_known_XXX_sid' substituting the
corresponding `get_XXX_sid' functions. Remove declarations of
these functions.
* sec_helper.cc (well_known_admin_sid): New global variable.
(well_known_system_sid): Ditto
(well_known_creator_owner_sid): Ditto
(well_known_world_sid): Ditto
(cygsid::string): New method, substituting `convert_sid_to_string_sid'.
(cygsid::get_sid): New method, substituting `get_sid'.
(cygsid::getfromstr): New method, substituting
`convert_string_sid_to_sid'.
(cygsid::getfrompw): New method, substituting `get_pw_sid'.
(cygsid::getfromgr): New method, substituting `get_gr_sid'.
(cygsid::get_id): New method, substituting `get_id_from_sid'.
(get_admin_sid): Eliminated.
(get_system_sid): Ditto.
(get_creator_owner_sid): Ditto.
(get_world_sid): Ditto.
* grp.cc: Use new cygsid methods and well known sids throughout.
* registry.cc: Ditto.
* sec_acl.cc: Ditto.
* security.cc: Ditto.
* shared.cc: Ditto.
* syscalls.cc (seteuid): Ditto. Eliminate redundant conditional.
* uinfo.cc (internal_getlogin): Ditto.
* spawn.cc (spawn_guts) Revert previous patch.
2001-05-16 03:23:31 +08:00
|
|
|
csid.string (sid);
|
2000-06-20 01:36:30 +08:00
|
|
|
strcpy (key,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
|
|
|
|
strcat (key, sid);
|
|
|
|
if (!RegOpenKeyExA (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
DWORD type, siz;
|
|
|
|
|
|
|
|
key[0] = '\0';
|
|
|
|
if (!RegQueryValueExA (hkey, "ProfileImagePath", 0, &type,
|
2000-09-03 12:16:35 +08:00
|
|
|
(BYTE *)buf, (siz = 256, &siz)))
|
|
|
|
ExpandEnvironmentStringsA (buf, key, 256);
|
2000-06-20 01:36:30 +08:00
|
|
|
RegCloseKey (hkey);
|
|
|
|
if (key[0])
|
2000-09-03 12:16:35 +08:00
|
|
|
return strcpy (path, key);
|
2000-06-20 01:36:30 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
load_registry_hive (PSID psid)
|
|
|
|
{
|
|
|
|
char sid[256];
|
2003-11-15 07:40:06 +08:00
|
|
|
char path[CYG_MAX_PATH + 1];
|
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
|
|
|
|
|
|
|
if (!psid)
|
|
|
|
return;
|
2000-06-23 02:54:26 +08:00
|
|
|
/* Check if user hive is already loaded. */
|
2003-09-11 05:01:40 +08:00
|
|
|
cygpsid csid (psid);
|
* fork.cc (fork): Eliminate superfluous call to getuid().
* security.h: New define `NO_SID'. Remove declarations of functions
moved to methods into class cygsid.
(class cygsid): Declare new methods `getfromstr', `get_sid',
`getfrompw', `getfromgr', `get_rid', `get_uid', `get_gid', `string'
and new constructors and operators =, == and !=.
Declare new global cygsids `well_known_XXX_sid' substituting the
corresponding `get_XXX_sid' functions. Remove declarations of
these functions.
* sec_helper.cc (well_known_admin_sid): New global variable.
(well_known_system_sid): Ditto
(well_known_creator_owner_sid): Ditto
(well_known_world_sid): Ditto
(cygsid::string): New method, substituting `convert_sid_to_string_sid'.
(cygsid::get_sid): New method, substituting `get_sid'.
(cygsid::getfromstr): New method, substituting
`convert_string_sid_to_sid'.
(cygsid::getfrompw): New method, substituting `get_pw_sid'.
(cygsid::getfromgr): New method, substituting `get_gr_sid'.
(cygsid::get_id): New method, substituting `get_id_from_sid'.
(get_admin_sid): Eliminated.
(get_system_sid): Ditto.
(get_creator_owner_sid): Ditto.
(get_world_sid): Ditto.
* grp.cc: Use new cygsid methods and well known sids throughout.
* registry.cc: Ditto.
* sec_acl.cc: Ditto.
* security.cc: Ditto.
* shared.cc: Ditto.
* syscalls.cc (seteuid): Ditto. Eliminate redundant conditional.
* uinfo.cc (internal_getlogin): Ditto.
* spawn.cc (spawn_guts) Revert previous patch.
2001-05-16 03:23:31 +08:00
|
|
|
csid.string (sid);
|
2002-05-06 18:05:46 +08:00
|
|
|
if (!RegOpenKeyExA (HKEY_USERS, sid, 0, KEY_READ, &hkey))
|
2000-06-20 01:36:30 +08:00
|
|
|
{
|
|
|
|
debug_printf ("User registry hive for %s already exists", sid);
|
|
|
|
RegCloseKey (hkey);
|
|
|
|
return;
|
|
|
|
}
|
2002-05-06 18:05:46 +08:00
|
|
|
set_process_privilege (SE_RESTORE_NAME);
|
2000-06-20 01:36:30 +08:00
|
|
|
if (get_registry_hive_path (psid, path))
|
|
|
|
{
|
|
|
|
strcat (path, "\\NTUSER.DAT");
|
2000-06-23 02:54:26 +08:00
|
|
|
if ((ret = RegLoadKeyA (HKEY_USERS, sid, path)) != ERROR_SUCCESS)
|
2000-09-03 12:16:35 +08:00
|
|
|
debug_printf ("Loading user registry hive for %s failed: %d", sid, ret);
|
2000-06-20 01:36:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|