2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Attempt to wrap the existing API in a more winreg.idl-like way */
24 * Here is a list of winreg.idl functions and corresponding implementations
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
32 * 0x05 winreg_CloseKey
33 * 0x06 winreg_CreateKey reg_createkey
34 * 0x07 winreg_DeleteKey reg_deletekey
35 * 0x08 winreg_DeleteValue reg_deletevalue
36 * 0x09 winreg_EnumKey reg_enumkey
37 * 0x0a winreg_EnumValue reg_enumvalue
38 * 0x0b winreg_FlushKey
39 * 0x0c winreg_GetKeySecurity reg_getkeysecurity
41 * 0x0e winreg_NotifyChangeKeyValue
42 * 0x0f winreg_OpenKey reg_openkey
43 * 0x10 winreg_QueryInfoKey reg_queryinfokey
44 * 0x11 winreg_QueryValue reg_queryvalue
45 * 0x12 winreg_ReplaceKey
46 * 0x13 winreg_RestoreKey
48 * 0x15 winreg_SetKeySecurity reg_setkeysecurity
49 * 0x16 winreg_SetValue reg_setvalue
50 * 0x17 winreg_UnLoadKey
51 * 0x18 winreg_InitiateSystemShutdown
52 * 0x19 winreg_AbortSystemShutdown
53 * 0x1a winreg_GetVersion reg_getversion
54 * 0x1b winreg_OpenHKCC
55 * 0x1c winreg_OpenHKDD
56 * 0x1d winreg_QueryMultipleValues
57 * 0x1e winreg_InitiateSystemShutdownEx
58 * 0x1f winreg_SaveKeyEx
59 * 0x20 winreg_OpenHKPT
60 * 0x21 winreg_OpenHKPN
61 * 0x22 winreg_QueryMultipleValues2
68 #define DBGC_CLASS DBGC_REGISTRY
71 /**********************************************************************
73 **********************************************************************/
75 static WERROR fill_value_cache(struct registry_key *key)
77 if (key->values != NULL) {
78 if (!reg_values_need_update(key->key, key->values)) {
83 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
86 if (fetch_reg_values(key->key, key->values) == -1) {
87 TALLOC_FREE(key->values);
94 static WERROR fill_subkey_cache(struct registry_key *key)
96 if (key->subkeys != NULL) {
97 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
102 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
106 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
107 TALLOC_FREE(key->subkeys);
108 return WERR_NO_MORE_ITEMS;
114 static int regkey_destructor(REGISTRY_KEY *key)
116 return regdb_close();
119 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
120 struct registry_key *parent,
122 const struct nt_user_token *token,
123 uint32 access_desired,
124 struct registry_key **pregkey)
126 WERROR result = WERR_OK;
127 struct registry_key *regkey;
129 REGSUBKEY_CTR *subkeys = NULL;
131 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
133 SMB_ASSERT(strchr(name, '\\') == NULL);
135 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
136 !(regkey->token = dup_nt_token(regkey, token)) ||
137 !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
142 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
147 talloc_set_destructor(key, regkey_destructor);
151 key->type = REG_KEY_GENERIC;
153 if (name[0] == '\0') {
155 * Open a copy of the parent key
158 result = WERR_BADFILE;
161 key->name = talloc_strdup(key, parent->key->name);
167 key->name = talloc_asprintf(key, "%s%s%s",
168 parent ? parent->key->name : "",
173 if (key->name == NULL) {
178 /* Tag this as a Performance Counter Key */
180 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
181 key->type = REG_KEY_HKPD;
183 /* Look up the table of registry I/O operations */
185 if ( !(key->hook = reghook_cache_find( key->name )) ) {
186 DEBUG(0,("reg_open_onelevel: Failed to assigned a "
187 "REGISTRY_HOOK to [%s]\n", key->name ));
188 result = WERR_BADFILE;
192 /* check if the path really exists; failed is indicated by -1 */
193 /* if the subkey count failed, bail out */
195 if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
200 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
201 result = WERR_BADFILE;
205 TALLOC_FREE( subkeys );
207 if ( !regkey_access_check( key, access_desired, &key->access_granted,
209 result = WERR_ACCESS_DENIED;
217 if ( !W_ERROR_IS_OK(result) ) {
224 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
225 uint32 desired_access,
226 const struct nt_user_token *token,
227 struct registry_key **pkey)
229 SMB_ASSERT(hive != NULL);
230 SMB_ASSERT(hive[0] != '\0');
231 SMB_ASSERT(strchr(hive, '\\') == NULL);
233 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
238 /**********************************************************************
240 **********************************************************************/
242 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
243 const char *name, uint32 desired_access,
244 struct registry_key **pkey)
246 struct registry_key *direct_parent = parent;
248 char *p, *path, *to_free;
251 if (!(path = SMB_STRDUP(name))) {
258 if ((len > 0) && (path[len-1] == '\\')) {
262 while ((p = strchr(path, '\\')) != NULL) {
263 char *name_component;
264 struct registry_key *tmp;
266 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
271 err = regkey_open_onelevel(mem_ctx, direct_parent,
272 name_component, parent->token,
273 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
274 SAFE_FREE(name_component);
276 if (!W_ERROR_IS_OK(err)) {
279 if (direct_parent != parent) {
280 TALLOC_FREE(direct_parent);
287 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
288 desired_access, pkey);
290 if (direct_parent != parent) {
291 TALLOC_FREE(direct_parent);
297 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
298 uint32 idx, char **name, NTTIME *last_write_time)
302 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
303 return WERR_ACCESS_DENIED;
306 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
310 if (idx >= key->subkeys->num_subkeys) {
311 return WERR_NO_MORE_ITEMS;
314 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
318 if (last_write_time) {
319 *last_write_time = 0;
325 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
326 uint32 idx, char **pname, struct registry_value **pval)
328 struct registry_value *val;
331 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
332 return WERR_ACCESS_DENIED;
335 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
339 if (idx >= key->values->num_values) {
340 return WERR_NO_MORE_ITEMS;
343 err = registry_pull_value(mem_ctx, &val,
344 key->values->values[idx]->type,
345 key->values->values[idx]->data_p,
346 key->values->values[idx]->size,
347 key->values->values[idx]->size);
348 if (!W_ERROR_IS_OK(err)) {
353 && !(*pname = talloc_strdup(
354 mem_ctx, key->values->values[idx]->valuename))) {
363 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
364 const char *name, struct registry_value **pval)
369 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
370 return WERR_ACCESS_DENIED;
373 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
377 for (i=0; i<key->values->num_values; i++) {
378 if (strequal(key->values->values[i]->valuename, name)) {
379 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
386 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
387 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
388 uint32_t *num_values, uint32_t *max_valnamelen,
389 uint32_t *max_valbufsize, uint32_t *secdescsize,
390 NTTIME *last_changed_time)
396 struct security_descriptor *secdesc;
398 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
399 return WERR_ACCESS_DENIED;
402 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
403 !W_ERROR_IS_OK(fill_value_cache(key))) {
408 for (i=0; i<key->subkeys->num_subkeys; i++) {
409 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
412 *num_subkeys = key->subkeys->num_subkeys;
413 *max_subkeylen = max_len;
414 *max_subkeysize = 0; /* Class length? */
418 for (i=0; i<key->values->num_values; i++) {
419 max_len = MAX(max_len,
420 strlen(key->values->values[i]->valuename));
421 max_size = MAX(max_size, key->values->values[i]->size);
424 *num_values = key->values->num_values;
425 *max_valnamelen = max_len;
426 *max_valbufsize = max_size;
428 if (!(mem_ctx = talloc_new(key))) {
432 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
433 if (!W_ERROR_IS_OK(err)) {
434 TALLOC_FREE(mem_ctx);
438 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
439 TALLOC_FREE(mem_ctx);
441 *last_changed_time = 0;
446 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
447 const char *subkeypath, uint32 desired_access,
448 struct registry_key **pkey,
449 enum winreg_CreateAction *paction)
451 struct registry_key *key = parent;
452 struct registry_key *create_parent;
456 REGSUBKEY_CTR *subkeys;
458 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
460 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
465 while ((end = strchr(path, '\\')) != NULL) {
466 struct registry_key *tmp;
467 enum winreg_CreateAction action;
471 err = reg_createkey(mem_ctx, key, path,
472 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
473 if (!W_ERROR_IS_OK(err)) {
486 * At this point, "path" contains the one-element subkey of "key". We
487 * can try to open it.
490 err = reg_openkey(ctx, key, path, desired_access, pkey);
491 if (W_ERROR_IS_OK(err)) {
492 if (paction != NULL) {
493 *paction = REG_OPENED_EXISTING_KEY;
498 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
500 * Something but "notfound" has happened, so bail out
506 * We have to make a copy of the current key, as we opened it only
507 * with ENUM_SUBKEY access.
510 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
512 if (!W_ERROR_IS_OK(err)) {
517 * Actually create the subkey
520 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
525 err = fill_subkey_cache(create_parent);
526 if (!W_ERROR_IS_OK(err)) goto done;
528 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
529 if (!W_ERROR_IS_OK(err)) goto done;
531 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
532 TALLOC_FREE(create_parent->subkeys);
533 err = WERR_REG_IO_FAILURE;
538 * Now open the newly created key
541 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
542 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
543 *paction = REG_CREATED_NEW_KEY;
547 TALLOC_FREE(mem_ctx);
551 WERROR reg_deletekey(struct registry_key *parent, const char *path)
557 struct registry_key *tmp_key, *key;
559 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
561 if (!(name = talloc_strdup(mem_ctx, path))) {
566 /* check if the key has subkeys */
567 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
568 if (!W_ERROR_IS_OK(err)) {
571 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
574 if (key->subkeys->num_subkeys > 0) {
575 err = WERR_ACCESS_DENIED;
579 /* no subkeys - proceed with delete */
580 if ((end = strrchr(name, '\\')) != NULL) {
583 err = reg_openkey(mem_ctx, parent, name,
584 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
585 if (!W_ERROR_IS_OK(err)) {
593 if (name[0] == '\0') {
594 err = WERR_INVALID_PARAM;
598 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
602 num_subkeys = parent->subkeys->num_subkeys;
604 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
609 if (!store_reg_keys(parent->key, parent->subkeys)) {
610 TALLOC_FREE(parent->subkeys);
611 err = WERR_REG_IO_FAILURE;
615 regkey_set_secdesc(key->key, NULL);
620 TALLOC_FREE(mem_ctx);
624 WERROR reg_setvalue(struct registry_key *key, const char *name,
625 const struct registry_value *val)
628 DATA_BLOB value_data;
631 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
632 return WERR_ACCESS_DENIED;
635 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
639 err = registry_push_value(key, val, &value_data);
640 if (!W_ERROR_IS_OK(err)) {
644 res = regval_ctr_addvalue(key->values, name, val->type,
645 (char *)value_data.data, value_data.length);
646 TALLOC_FREE(value_data.data);
649 TALLOC_FREE(key->values);
653 if (!store_reg_values(key->key, key->values)) {
654 TALLOC_FREE(key->values);
655 return WERR_REG_IO_FAILURE;
661 WERROR reg_deletevalue(struct registry_key *key, const char *name)
665 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
666 return WERR_ACCESS_DENIED;
669 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
673 regval_ctr_delvalue(key->values, name);
675 if (!store_reg_values(key->key, key->values)) {
676 TALLOC_FREE(key->values);
677 return WERR_REG_IO_FAILURE;
683 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
684 struct security_descriptor **psecdesc)
686 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
689 WERROR reg_setkeysecurity(struct registry_key *key,
690 struct security_descriptor *psecdesc)
692 return regkey_set_secdesc(key->key, psecdesc);
695 WERROR reg_getversion(uint32_t *version)
697 if (version == NULL) {
698 return WERR_INVALID_PARAM;
701 *version = 0x00000005; /* Windows 2000 registry API version */
705 /**********************************************************************
706 * Higher level utility functions
707 **********************************************************************/
709 WERROR reg_deleteallvalues(struct registry_key *key)
714 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
715 return WERR_ACCESS_DENIED;
718 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
722 for (i=0; i<key->values->num_values; i++) {
723 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
726 if (!store_reg_values(key->key, key->values)) {
727 TALLOC_FREE(key->values);
728 return WERR_REG_IO_FAILURE;
735 * Utility function to open a complete registry path including the hive
736 * prefix. This should become the replacement function for
737 * regkey_open_internal.
740 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
741 uint32 desired_access, const struct nt_user_token *token,
742 struct registry_key **pkey)
744 struct registry_key *hive, *key;
748 if (!(path = SMB_STRDUP(orig_path))) {
752 p = strchr(path, '\\');
754 if ((p == NULL) || (p[1] == '\0')) {
756 * No key behind the hive, just return the hive
759 err = reg_openhive(mem_ctx, path, desired_access, token,
761 if (!W_ERROR_IS_OK(err)) {
772 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
774 if (!W_ERROR_IS_OK(err)) {
779 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
784 if (!W_ERROR_IS_OK(err)) {
793 * Utility function to delete a registry key with all its subkeys.
794 * Note that reg_deletekey returns ACCESS_DENIED when called on a
795 * key that has subkeys.
797 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
798 struct registry_key *parent,
802 TALLOC_CTX *mem_ctx = NULL;
803 WERROR werr = WERR_OK;
804 struct registry_key *key;
805 char *subkey_name = NULL;
807 mem_ctx = talloc_new(ctx);
808 if (mem_ctx == NULL) {
813 /* recurse through subkeys first */
814 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
815 if (!W_ERROR_IS_OK(werr)) {
819 while (W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, 0,
820 &subkey_name, NULL)))
822 werr = reg_deletekey_recursive_internal(mem_ctx, key,
825 if (!W_ERROR_IS_OK(werr)) {
829 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
830 DEBUG(1, ("reg_deletekey_recursive_internal: "
831 "Error enumerating subkeys: %s\n",
839 /* now delete the actual key */
840 werr = reg_deletekey(parent, path);
844 TALLOC_FREE(mem_ctx);
848 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
849 struct registry_key *parent,
852 return reg_deletekey_recursive_internal(ctx, parent, path, true);
855 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
856 struct registry_key *parent,
859 return reg_deletekey_recursive_internal(ctx, parent, path, false);
863 /* these two functions are unused. */
866 * Utility function to create a registry key without opening the hive
867 * before. Assumes the hive already exists.
870 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
871 uint32 desired_access,
872 const struct nt_user_token *token,
873 enum winreg_CreateAction *paction,
874 struct registry_key **pkey)
876 struct registry_key *hive;
880 if (!(path = SMB_STRDUP(orig_path))) {
884 p = strchr(path, '\\');
886 if ((p == NULL) || (p[1] == '\0')) {
888 * No key behind the hive, just return the hive
891 err = reg_openhive(mem_ctx, path, desired_access, token,
893 if (!W_ERROR_IS_OK(err)) {
899 *paction = REG_OPENED_EXISTING_KEY;
905 err = reg_openhive(mem_ctx, path,
906 (strchr(p+1, '\\') != NULL) ?
907 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
909 if (!W_ERROR_IS_OK(err)) {
914 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
921 * Utility function to create a registry key without opening the hive
922 * before. Will not delete a hive.
925 WERROR reg_delete_path(const struct nt_user_token *token,
926 const char *orig_path)
928 struct registry_key *hive;
932 if (!(path = SMB_STRDUP(orig_path))) {
936 p = strchr(path, '\\');
938 if ((p == NULL) || (p[1] == '\0')) {
940 return WERR_INVALID_PARAM;
945 err = reg_openhive(NULL, path,
946 (strchr(p+1, '\\') != NULL) ?
947 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
949 if (!W_ERROR_IS_OK(err)) {
954 err = reg_deletekey(hive, p+1);