2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Attempt to wrap the existing API in a more winreg.idl-like way */
25 static WERROR fill_value_cache(struct registry_key *key)
27 if (key->values != NULL) {
31 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
34 if (fetch_reg_values(key->key, key->values) == -1) {
35 TALLOC_FREE(key->values);
42 static WERROR fill_subkey_cache(struct registry_key *key)
44 if (key->subkeys != NULL) {
48 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
52 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
53 TALLOC_FREE(key->subkeys);
54 return WERR_NO_MORE_ITEMS;
60 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
61 uint32 desired_access,
62 const struct nt_user_token *token,
63 struct registry_key **pkey)
65 struct registry_key *key;
68 SMB_ASSERT(hive[0] != '\0');
69 SMB_ASSERT(strchr(hive, '\\') == NULL);
71 if (!(key = TALLOC_ZERO_P(mem_ctx, struct registry_key))) {
75 if (!(key->token = dup_nt_token(key, token))) {
80 err = regkey_open_internal(key, &key->key, hive, token,
83 if (!W_ERROR_IS_OK(err)) {
93 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
94 const char *name, uint32 desired_access,
95 struct registry_key **pkey)
97 struct registry_key *key;
101 if (!(key = TALLOC_ZERO_P(mem_ctx, struct registry_key))) {
105 if (!(key->token = dup_nt_token(key, parent->token))) {
110 if (name[0] == '\0') {
112 * Make a copy of the parent
114 path = talloc_strdup(key, parent->key->name);
118 * Normal subpath open
120 path = talloc_asprintf(key, "%s\\%s", parent->key->name,
129 err = regkey_open_internal(key, &key->key, path, parent->token,
133 if (!W_ERROR_IS_OK(err)) {
142 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
143 uint32 idx, char **name, NTTIME *last_write_time)
147 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
148 return WERR_ACCESS_DENIED;
151 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
155 if (idx >= key->subkeys->num_subkeys) {
156 return WERR_NO_MORE_ITEMS;
159 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
163 if (last_write_time) {
164 *last_write_time = 0;
170 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
171 uint32 idx, char **pname, struct registry_value **pval)
173 struct registry_value *val;
176 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
177 return WERR_ACCESS_DENIED;
180 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
184 if (idx >= key->values->num_values) {
188 err = registry_pull_value(mem_ctx, &val,
189 key->values->values[idx]->type,
190 key->values->values[idx]->data_p,
191 key->values->values[idx]->size,
192 key->values->values[idx]->size);
193 if (!W_ERROR_IS_OK(err)) {
198 && !(*pname = talloc_strdup(
199 mem_ctx, key->values->values[idx]->valuename))) {
208 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
209 const char *name, struct registry_value **pval)
214 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
215 return WERR_ACCESS_DENIED;
218 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
222 for (i=0; i<key->values->num_values; i++) {
223 if (strequal(key->values->values[i]->valuename, name)) {
224 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
231 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
232 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
233 uint32_t *num_values, uint32_t *max_valnamelen,
234 uint32_t *max_valbufsize, uint32_t *secdescsize,
235 NTTIME *last_changed_time)
241 struct security_descriptor *secdesc;
243 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
244 return WERR_ACCESS_DENIED;
247 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
248 !W_ERROR_IS_OK(fill_value_cache(key))) {
253 for (i=0; i<key->subkeys->num_subkeys; i++) {
254 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
257 *num_subkeys = key->subkeys->num_subkeys;
258 *max_subkeylen = max_len;
259 *max_subkeysize = 0; /* Class length? */
263 for (i=0; i<key->values->num_values; i++) {
264 max_len = MAX(max_len,
265 strlen(key->values->values[i]->valuename));
266 max_size = MAX(max_size, key->values->values[i]->size);
269 *num_values = key->values->num_values;
270 *max_valnamelen = max_len;
271 *max_valbufsize = max_size;
273 if (!(mem_ctx = talloc_new(key))) {
277 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
278 if (!W_ERROR_IS_OK(err)) {
279 TALLOC_FREE(mem_ctx);
283 *secdescsize = sec_desc_size(secdesc);
284 TALLOC_FREE(mem_ctx);
286 *last_changed_time = 0;
291 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
292 const char *subkeypath, uint32 desired_access,
293 struct registry_key **pkey,
294 enum winreg_CreateAction *paction)
296 struct registry_key *key = parent;
297 struct registry_key *create_parent;
301 REGSUBKEY_CTR *subkeys;
303 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
305 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
310 while ((end = strchr(path, '\\')) != NULL) {
311 struct registry_key *tmp;
312 enum winreg_CreateAction action;
316 err = reg_createkey(mem_ctx, key, path,
317 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
318 if (!W_ERROR_IS_OK(err)) {
331 * At this point, "path" contains the one-element subkey of "key". We
332 * can try to open it.
335 err = reg_openkey(ctx, key, path, desired_access, pkey);
336 if (W_ERROR_IS_OK(err)) {
337 if (paction != NULL) {
338 *paction = REG_OPENED_EXISTING_KEY;
343 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
345 * Something but "notfound" has happened, so bail out
351 * We have to make a copy of the current key, as we opened it only
352 * with ENUM_SUBKEY access.
355 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
357 if (!W_ERROR_IS_OK(err)) {
362 * Actually create the subkey
365 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
370 err = fill_subkey_cache(create_parent);
371 if (!W_ERROR_IS_OK(err)) goto done;
373 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
374 if (!W_ERROR_IS_OK(err)) goto done;
376 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
377 TALLOC_FREE(create_parent->subkeys);
378 err = WERR_REG_IO_FAILURE;
383 * Now open the newly created key
386 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
387 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
388 *paction = REG_CREATED_NEW_KEY;
392 TALLOC_FREE(mem_ctx);
397 WERROR reg_deletekey(struct registry_key *parent, const char *path)
404 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
406 if (!(name = talloc_strdup(mem_ctx, path))) {
411 if ((end = strrchr(name, '\\')) != NULL) {
412 struct registry_key *tmp;
416 err = reg_openkey(mem_ctx, parent, name,
417 SEC_RIGHTS_CREATE_SUBKEY, &tmp);
418 if (!W_ERROR_IS_OK(err)) {
426 if (name[0] == '\0') {
427 err = WERR_INVALID_PARAM;
431 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
435 num_subkeys = parent->subkeys->num_subkeys;
437 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
442 if (!store_reg_keys(parent->key, parent->subkeys)) {
443 TALLOC_FREE(parent->subkeys);
444 err = WERR_REG_IO_FAILURE;
450 TALLOC_FREE(mem_ctx);
454 WERROR reg_setvalue(struct registry_key *key, const char *name,
455 const struct registry_value *val)
458 DATA_BLOB value_data;
461 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
462 return WERR_ACCESS_DENIED;
465 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
469 err = registry_push_value(key, val, &value_data);
470 if (!W_ERROR_IS_OK(err)) {
474 res = regval_ctr_addvalue(key->values, name, val->type,
475 (char *)value_data.data, value_data.length);
476 TALLOC_FREE(value_data.data);
479 TALLOC_FREE(key->values);
483 if (!store_reg_values(key->key, key->values)) {
484 TALLOC_FREE(key->values);
485 return WERR_REG_IO_FAILURE;
491 WERROR reg_deletevalue(struct registry_key *key, const char *name)
495 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
496 return WERR_ACCESS_DENIED;
499 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
503 regval_ctr_delvalue(key->values, name);
505 if (!store_reg_values(key->key, key->values)) {
506 TALLOC_FREE(key->values);
507 return WERR_REG_IO_FAILURE;