aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/registry.cc
blob: 1fdf20a79d08566a6dca3b718d068ac1dada231f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* registry.cc: registry interface

   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
   2005, 2006, 2007, 2008, 2009 Red Hat, Inc.

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"
#include "registry.h"
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "tls_pbuf.h"
#include <wchar.h>
static const char cygnus_class[] = "cygnus";

reg_key::reg_key (HKEY top, REGSAM access, ...): _disposition (0)
{
  va_list av;
  va_start (av, access);
  build_reg (top, access, av);
  va_end (av);
}

/* Opens a key under the appropriate Cygwin key.
   Do not use HKCU per MS KB 199190  */

reg_key::reg_key (bool isHKLM, REGSAM access, ...): _disposition (0)
{
  va_list av;
  HKEY top;

  if (isHKLM)
    top = HKEY_LOCAL_MACHINE;
  else
    {
      char name[128];
      const char *names[2] = {cygheap->user.get_windows_id (name), ".DEFAULT"};
      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:
  new (this) reg_key (top, access, "SOFTWARE",
		      CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
  if (top != HKEY_LOCAL_MACHINE)
    RegCloseKey (top);
  if (key_is_invalid)
    return;

  top = key;
  va_start (av, access);
  build_reg (top, access, av);
  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;
  key_is_invalid = 0;

  /* 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,
				 (char *) cygnus_class,
				 REG_OPTION_NON_VOLATILE,
				 access,
				 &sec_none_nih,
				 &key,
				 &_disposition);
      if (r != top)
	RegCloseKey (r);
      r = key;
      if (res != ERROR_SUCCESS)
	{
	  key_is_invalid = res;
	  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);

  if (key_is_invalid)
    return def;

  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);

  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;
  if (key_is_invalid)
    return key_is_invalid;

  return (int) RegSetValueExA (key, name, 0, REG_DWORD,
			       (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));
}

/* 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;
  LONG res;

  if (key_is_invalid)
    res = key_is_invalid;
  else
    res = RegQueryValueExA (key, name, 0, &type, (LPBYTE) dst, &size);

  if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
    strcpy (dst, def);
  return (int) res;
}

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;
}

/* Given the current registry key, set a specific string value. */

int
reg_key::set_string (const char *name, const char *src)
{
  if (key_is_invalid)
    return key_is_invalid;
  return (int) RegSetValueExA (key, name, 0, REG_SZ, (const BYTE*) src,
			       strlen (src) + 1);
}

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));
}

/* 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)
{
  if (key_is_invalid)
    return key_is_invalid;
  return RegDeleteKeyA (key, name);
}

/* 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);
}

reg_key::~reg_key ()
{
  if (!key_is_invalid)
    RegCloseKey (key);
  key_is_invalid = 1;
}

PWCHAR
get_registry_hive_path (const PWCHAR name, PWCHAR path)
{
  WCHAR key[256], *kend;
  HKEY hkey;

  if (!name || !path)
    return NULL;
  kend = wcpcpy (key, L"SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion\\ProfileList\\");
  wcpcpy (kend, name);
  if (!RegOpenKeyExW (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
    {
      tmp_pathbuf tp;
      PWCHAR buf = tp.w_get ();
      DWORD type, siz;

      path[0] = L'\0';
      if (!RegQueryValueExW (hkey, L"ProfileImagePath", 0, &type,
			     (BYTE *)buf, (siz = NT_MAX_PATH, &siz)))
	ExpandEnvironmentStringsW (buf, path, NT_MAX_PATH);
      RegCloseKey (hkey);
      if (path[0])
	return path;
    }
  debug_printf ("HKLM\\%W not found", key);
  return NULL;
}

void
load_registry_hive (const PWCHAR name)
{
  tmp_pathbuf tp;
  PWCHAR path = tp.w_get ();
  HKEY hkey;
  LONG ret;

  if (!name)
    return;
  /* Check if user hive is already loaded. */
  if (!RegOpenKeyExW (HKEY_USERS, name, 0, KEY_READ, &hkey))
    {
      debug_printf ("User registry hive for %W already exists", name);
      RegCloseKey (hkey);
      return;
    }
  if (get_registry_hive_path (name, path))
    {
      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);
    }
}