summaryrefslogtreecommitdiffstats
path: root/source4/lib/registry/interface.c
blob: 2900c10419a641b950be7e35a0066b0bb0d2d95a (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
/*
   Unix SMB/CIFS implementation.
   Transparent registry backend handling
   Copyright (C) Jelmer Vernooij			2003-2007.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "../lib/util/dlinklist.h"
#include "lib/registry/registry.h"
#include "system/filesys.h"

#undef strcasecmp

/**
 * @file
 * @brief Main registry functions
 */

const struct reg_predefined_key reg_predefined_keys[] = {
	{HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
	{HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
	{HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
	{HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
	{HKEY_USERS, "HKEY_USERS" },
	{HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
	{HKEY_DYN_DATA, "HKEY_DYN_DATA" },
	{HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
	{HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
	{ 0, NULL }
};

/** Obtain name of specific hkey. */
_PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
{
	unsigned int i;
	for (i = 0; reg_predefined_keys[i].name; i++) {
		if (reg_predefined_keys[i].handle == hkey)
			return reg_predefined_keys[i].name;
	}

	return NULL;
}

/** Get predefined key by name. */
_PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx,
					       const char *name,
					       struct registry_key **key)
{
	unsigned int i;

	for (i = 0; reg_predefined_keys[i].name; i++) {
		if (!strcasecmp(reg_predefined_keys[i].name, name))
			return reg_get_predefined_key(ctx,
						      reg_predefined_keys[i].handle,
						      key);
	}

	DEBUG(1, ("No predefined key with name '%s'\n", name));

	return WERR_FILE_NOT_FOUND;
}

/** Get predefined key by id. */
_PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx,
				       uint32_t hkey, struct registry_key **key)
{
	return ctx->ops->get_predefined_key(ctx, hkey, key);
}

/**
 * Open a key
 * First tries to use the open_key function from the backend
 * then falls back to get_subkey_by_name and later get_subkey_by_index
 */
_PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent,
			     const char *name, struct registry_key **result)
{
	if (parent == NULL) {
		DEBUG(0, ("Invalid parent key specified for open of '%s'\n",
			name));
		return WERR_INVALID_PARAMETER;
	}

	if (parent->context->ops->open_key == NULL) {
		DEBUG(0, ("Registry backend doesn't have open_key!\n"));
		return WERR_NOT_SUPPORTED;
	}

	return parent->context->ops->open_key(mem_ctx, parent, name, result);
}

/**
 * Get value by index
 */
_PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx,
					   const struct registry_key *key,
					   uint32_t idx, const char **name,
					   uint32_t *type, DATA_BLOB *data)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->enum_value == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->enum_value(mem_ctx, key, idx, name,
					     type, data);
}

/**
 * Get the number of subkeys.
 */
_PUBLIC_ WERROR reg_key_get_info(TALLOC_CTX *mem_ctx,
				 const struct registry_key *key,
				 const char **classname,
				 uint32_t *num_subkeys,
				 uint32_t *num_values,
				 NTTIME *last_change_time,
				 uint32_t *max_subkeynamelen,
				 uint32_t *max_valnamelen,
				 uint32_t *max_valbufsize)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->get_key_info == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->get_key_info(mem_ctx,
					       key, classname, num_subkeys,
					       num_values, last_change_time,
					       max_subkeynamelen,
					       max_valnamelen, max_valbufsize);
}

/**
 * Get subkey by index.
 */
_PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx,
					    const struct registry_key *key,
					    uint32_t idx, const char **name,
					    const char **keyclass,
					    NTTIME *last_changed_time)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->enum_key == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->enum_key(mem_ctx, key, idx, name,
					   keyclass, last_changed_time);
}

/**
 * Get value by name.
 */
_PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx,
					  const struct registry_key *key,
					  const char *name,
					  uint32_t *type,
					  DATA_BLOB *data)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->get_value == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->get_value(mem_ctx, key, name, type, data);
}

/**
 * Delete a key.
 */
_PUBLIC_ WERROR reg_key_del(TALLOC_CTX *mem_ctx, struct registry_key *parent,
			    const char *name)
{
	if (parent == NULL)
		return WERR_INVALID_PARAMETER;

	if (parent->context->ops->delete_key == NULL)
		return WERR_NOT_SUPPORTED;

	return parent->context->ops->delete_key(mem_ctx, parent, name);
}

/**
 * Add a key.
 */
_PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx,
				 struct registry_key *parent,
				 const char *path, const char *key_class,
				 struct security_descriptor *desc,
				 struct registry_key **newkey)
{
	if (parent == NULL)
		return WERR_INVALID_PARAMETER;

	if (parent->context->ops->create_key == NULL) {
		DEBUG(1, ("Backend '%s' doesn't support method add_key\n",
				  parent->context->ops->name));
		return WERR_NOT_SUPPORTED;
	}

	return parent->context->ops->create_key(mem_ctx, parent, path,
						key_class, desc, newkey);
}

/**
 * Set a value.
 */
_PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value,
			    uint32_t type, const DATA_BLOB data)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	/* A 'real' set function has preference */
	if (key->context->ops->set_value == NULL) {
		DEBUG(1, ("Backend '%s' doesn't support method set_value\n",
				  key->context->ops->name));
		return WERR_NOT_SUPPORTED;
	}

	return key->context->ops->set_value(key, value, type, data);
}

/**
 * Get the security descriptor on a key.
 */
_PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx,
				 const struct registry_key *key,
				 struct security_descriptor **secdesc)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	/* A 'real' set function has preference */
	if (key->context->ops->get_sec_desc == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->get_sec_desc(ctx, key, secdesc);
}

/**
 * Delete a value.
 */
_PUBLIC_ WERROR reg_del_value(TALLOC_CTX *mem_ctx, struct registry_key *key,
			      const char *valname)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->delete_value == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->delete_value(mem_ctx, key, valname);
}

/**
 * Flush a key to disk.
 */
_PUBLIC_ WERROR reg_key_flush(struct registry_key *key)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->flush_key == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->flush_key(key);
}

_PUBLIC_ WERROR reg_set_sec_desc(struct registry_key *key,
				 const struct security_descriptor *security)
{
	if (key == NULL)
		return WERR_INVALID_PARAMETER;

	if (key->context->ops->set_sec_desc == NULL)
		return WERR_NOT_SUPPORTED;

	return key->context->ops->set_sec_desc(key, security);
}