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 3 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, see <http://www.gnu.org/licenses/>.
20 /* Attempt to wrap the existing API in a more winreg.idl-like way */
25 #define DBGC_CLASS DBGC_REGISTRY
27 static WERROR fill_value_cache(struct registry_key *key)
29 if (key->values != NULL) {
30 if (!reg_values_need_update(key->key, key->values)) {
35 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
38 if (fetch_reg_values(key->key, key->values) == -1) {
39 TALLOC_FREE(key->values);
46 static WERROR fill_subkey_cache(struct registry_key *key)
48 if (key->subkeys != NULL) {
49 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
54 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
58 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
59 TALLOC_FREE(key->subkeys);
60 return WERR_NO_MORE_ITEMS;
66 static int regkey_destructor(REGISTRY_KEY *key)
71 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
72 struct registry_key *parent,
74 const struct nt_user_token *token,
75 uint32 access_desired,
76 struct registry_key **pregkey)
78 WERROR result = WERR_OK;
79 struct registry_key *regkey;
81 REGSUBKEY_CTR *subkeys = NULL;
83 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
85 SMB_ASSERT(strchr(name, '\\') == NULL);
87 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
88 !(regkey->token = dup_nt_token(regkey, token)) ||
89 !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
94 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
99 talloc_set_destructor(key, regkey_destructor);
103 key->type = REG_KEY_GENERIC;
105 if (name[0] == '\0') {
107 * Open a copy of the parent key
110 result = WERR_BADFILE;
113 key->name = talloc_strdup(key, parent->key->name);
119 key->name = talloc_asprintf(key, "%s%s%s",
120 parent ? parent->key->name : "",
125 if (key->name == NULL) {
130 /* Tag this as a Performance Counter Key */
132 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
133 key->type = REG_KEY_HKPD;
135 /* Look up the table of registry I/O operations */
137 if ( !(key->hook = reghook_cache_find( key->name )) ) {
138 DEBUG(0,("reg_open_onelevel: Failed to assigned a "
139 "REGISTRY_HOOK to [%s]\n", key->name ));
140 result = WERR_BADFILE;
144 /* check if the path really exists; failed is indicated by -1 */
145 /* if the subkey count failed, bail out */
147 if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
152 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
153 result = WERR_BADFILE;
157 TALLOC_FREE( subkeys );
159 if ( !regkey_access_check( key, access_desired, &key->access_granted,
161 result = WERR_ACCESS_DENIED;
169 if ( !W_ERROR_IS_OK(result) ) {
176 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
177 uint32 desired_access,
178 const struct nt_user_token *token,
179 struct registry_key **pkey)
181 SMB_ASSERT(hive != NULL);
182 SMB_ASSERT(hive[0] != '\0');
183 SMB_ASSERT(strchr(hive, '\\') == NULL);
185 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
189 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
190 const char *name, uint32 desired_access,
191 struct registry_key **pkey)
193 struct registry_key *direct_parent = parent;
195 char *p, *path, *to_free;
198 if (!(path = SMB_STRDUP(name))) {
205 if ((len > 0) && (path[len-1] == '\\')) {
209 while ((p = strchr(path, '\\')) != NULL) {
210 char *name_component;
211 struct registry_key *tmp;
213 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
218 err = regkey_open_onelevel(mem_ctx, direct_parent,
219 name_component, parent->token,
220 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
221 SAFE_FREE(name_component);
223 if (!W_ERROR_IS_OK(err)) {
226 if (direct_parent != parent) {
227 TALLOC_FREE(direct_parent);
234 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
235 desired_access, pkey);
237 if (direct_parent != parent) {
238 TALLOC_FREE(direct_parent);
244 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
245 uint32 idx, char **name, NTTIME *last_write_time)
249 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
250 return WERR_ACCESS_DENIED;
253 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
257 if (idx >= key->subkeys->num_subkeys) {
258 return WERR_NO_MORE_ITEMS;
261 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
265 if (last_write_time) {
266 *last_write_time = 0;
272 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
273 uint32 idx, char **pname, struct registry_value **pval)
275 struct registry_value *val;
278 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
279 return WERR_ACCESS_DENIED;
282 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
286 if (idx >= key->values->num_values) {
287 return WERR_NO_MORE_ITEMS;
290 err = registry_pull_value(mem_ctx, &val,
291 key->values->values[idx]->type,
292 key->values->values[idx]->data_p,
293 key->values->values[idx]->size,
294 key->values->values[idx]->size);
295 if (!W_ERROR_IS_OK(err)) {
300 && !(*pname = talloc_strdup(
301 mem_ctx, key->values->values[idx]->valuename))) {
310 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
311 const char *name, struct registry_value **pval)
316 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
317 return WERR_ACCESS_DENIED;
320 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
324 for (i=0; i<key->values->num_values; i++) {
325 if (strequal(key->values->values[i]->valuename, name)) {
326 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
333 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
334 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
335 uint32_t *num_values, uint32_t *max_valnamelen,
336 uint32_t *max_valbufsize, uint32_t *secdescsize,
337 NTTIME *last_changed_time)
343 struct security_descriptor *secdesc;
345 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
346 return WERR_ACCESS_DENIED;
349 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
350 !W_ERROR_IS_OK(fill_value_cache(key))) {
355 for (i=0; i<key->subkeys->num_subkeys; i++) {
356 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
359 *num_subkeys = key->subkeys->num_subkeys;
360 *max_subkeylen = max_len;
361 *max_subkeysize = 0; /* Class length? */
365 for (i=0; i<key->values->num_values; i++) {
366 max_len = MAX(max_len,
367 strlen(key->values->values[i]->valuename));
368 max_size = MAX(max_size, key->values->values[i]->size);
371 *num_values = key->values->num_values;
372 *max_valnamelen = max_len;
373 *max_valbufsize = max_size;
375 if (!(mem_ctx = talloc_new(key))) {
379 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
380 if (!W_ERROR_IS_OK(err)) {
381 TALLOC_FREE(mem_ctx);
385 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
386 TALLOC_FREE(mem_ctx);
388 *last_changed_time = 0;
393 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
394 const char *subkeypath, uint32 desired_access,
395 struct registry_key **pkey,
396 enum winreg_CreateAction *paction)
398 struct registry_key *key = parent;
399 struct registry_key *create_parent;
403 REGSUBKEY_CTR *subkeys;
405 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
407 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
412 while ((end = strchr(path, '\\')) != NULL) {
413 struct registry_key *tmp;
414 enum winreg_CreateAction action;
418 err = reg_createkey(mem_ctx, key, path,
419 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
420 if (!W_ERROR_IS_OK(err)) {
433 * At this point, "path" contains the one-element subkey of "key". We
434 * can try to open it.
437 err = reg_openkey(ctx, key, path, desired_access, pkey);
438 if (W_ERROR_IS_OK(err)) {
439 if (paction != NULL) {
440 *paction = REG_OPENED_EXISTING_KEY;
445 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
447 * Something but "notfound" has happened, so bail out
453 * We have to make a copy of the current key, as we opened it only
454 * with ENUM_SUBKEY access.
457 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
459 if (!W_ERROR_IS_OK(err)) {
464 * Actually create the subkey
467 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
472 err = fill_subkey_cache(create_parent);
473 if (!W_ERROR_IS_OK(err)) goto done;
475 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
476 if (!W_ERROR_IS_OK(err)) goto done;
478 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
479 TALLOC_FREE(create_parent->subkeys);
480 err = WERR_REG_IO_FAILURE;
485 * Now open the newly created key
488 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
489 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
490 *paction = REG_CREATED_NEW_KEY;
494 TALLOC_FREE(mem_ctx);
498 WERROR reg_deletekey(struct registry_key *parent, const char *path)
504 struct registry_key *tmp_key, *key;
506 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
508 if (!(name = talloc_strdup(mem_ctx, path))) {
513 /* check if the key has subkeys */
514 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
515 if (!W_ERROR_IS_OK(err)) {
518 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
521 if (key->subkeys->num_subkeys > 0) {
522 err = WERR_ACCESS_DENIED;
526 /* no subkeys - proceed with delete */
527 if ((end = strrchr(name, '\\')) != NULL) {
530 err = reg_openkey(mem_ctx, parent, name,
531 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
532 if (!W_ERROR_IS_OK(err)) {
540 if (name[0] == '\0') {
541 err = WERR_INVALID_PARAM;
545 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
549 num_subkeys = parent->subkeys->num_subkeys;
551 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
556 if (!store_reg_keys(parent->key, parent->subkeys)) {
557 TALLOC_FREE(parent->subkeys);
558 err = WERR_REG_IO_FAILURE;
562 regkey_set_secdesc(key->key, NULL);
567 TALLOC_FREE(mem_ctx);
571 WERROR reg_setvalue(struct registry_key *key, const char *name,
572 const struct registry_value *val)
575 DATA_BLOB value_data;
578 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
579 return WERR_ACCESS_DENIED;
582 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
586 err = registry_push_value(key, val, &value_data);
587 if (!W_ERROR_IS_OK(err)) {
591 res = regval_ctr_addvalue(key->values, name, val->type,
592 (char *)value_data.data, value_data.length);
593 TALLOC_FREE(value_data.data);
596 TALLOC_FREE(key->values);
600 if (!store_reg_values(key->key, key->values)) {
601 TALLOC_FREE(key->values);
602 return WERR_REG_IO_FAILURE;
608 WERROR reg_deletevalue(struct registry_key *key, const char *name)
612 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
613 return WERR_ACCESS_DENIED;
616 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
620 regval_ctr_delvalue(key->values, name);
622 if (!store_reg_values(key->key, key->values)) {
623 TALLOC_FREE(key->values);
624 return WERR_REG_IO_FAILURE;
630 WERROR reg_deleteallvalues(struct registry_key *key)
635 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
636 return WERR_ACCESS_DENIED;
639 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
643 for (i=0; i<key->values->num_values; i++) {
644 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
647 if (!store_reg_values(key->key, key->values)) {
648 TALLOC_FREE(key->values);
649 return WERR_REG_IO_FAILURE;
656 * Utility function to open a complete registry path including the hive
657 * prefix. This should become the replacement function for
658 * regkey_open_internal.
661 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
662 uint32 desired_access, const struct nt_user_token *token,
663 struct registry_key **pkey)
665 struct registry_key *hive, *key;
669 if (!(path = SMB_STRDUP(orig_path))) {
673 p = strchr(path, '\\');
675 if ((p == NULL) || (p[1] == '\0')) {
677 * No key behind the hive, just return the hive
680 err = reg_openhive(mem_ctx, path, desired_access, token,
682 if (!W_ERROR_IS_OK(err)) {
693 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
695 if (!W_ERROR_IS_OK(err)) {
700 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
705 if (!W_ERROR_IS_OK(err)) {
714 * Utility function to delete a registry key with all its subkeys.
715 * Note that reg_deletekey returns ACCESS_DENIED when called on a
716 * key that has subkeys.
718 WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
719 struct registry_key *parent,
723 TALLOC_CTX *mem_ctx = NULL;
724 WERROR werr = WERR_OK;
725 struct registry_key *key;
726 char *subkey_name = NULL;
728 mem_ctx = talloc_new(ctx);
729 if (mem_ctx == NULL) {
734 /* recurse through subkeys first */
735 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
736 if (!W_ERROR_IS_OK(werr)) {
740 while (W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, 0,
741 &subkey_name, NULL)))
743 werr = reg_deletekey_recursive_internal(mem_ctx, key,
746 if (!W_ERROR_IS_OK(werr)) {
750 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
751 DEBUG(1, ("reg_deletekey_recursive_internal: "
752 "Error enumerating subkeys: %s\n",
760 /* now delete the actual key */
761 werr = reg_deletekey(parent, path);
765 TALLOC_FREE(mem_ctx);
769 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
770 struct registry_key *parent,
773 return reg_deletekey_recursive_internal(ctx, parent, path, true);
776 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
777 struct registry_key *parent,
780 return reg_deletekey_recursive_internal(ctx, parent, path, false);