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 assign 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;
457 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
459 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
464 while ((end = strchr(path, '\\')) != NULL) {
465 struct registry_key *tmp;
466 enum winreg_CreateAction action;
470 err = reg_createkey(mem_ctx, key, path,
471 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
472 if (!W_ERROR_IS_OK(err)) {
485 * At this point, "path" contains the one-element subkey of "key". We
486 * can try to open it.
489 err = reg_openkey(ctx, key, path, desired_access, pkey);
490 if (W_ERROR_IS_OK(err)) {
491 if (paction != NULL) {
492 *paction = REG_OPENED_EXISTING_KEY;
497 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
499 * Something but "notfound" has happened, so bail out
505 * We have to make a copy of the current key, as we opened it only
506 * with ENUM_SUBKEY access.
509 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
511 if (!W_ERROR_IS_OK(err)) {
516 * Actually create the subkey
519 err = fill_subkey_cache(create_parent);
520 if (!W_ERROR_IS_OK(err)) goto done;
522 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
523 if (!W_ERROR_IS_OK(err)) goto done;
525 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
526 TALLOC_FREE(create_parent->subkeys);
527 err = WERR_REG_IO_FAILURE;
532 * Now open the newly created key
535 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
536 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
537 *paction = REG_CREATED_NEW_KEY;
541 TALLOC_FREE(mem_ctx);
545 WERROR reg_deletekey(struct registry_key *parent, const char *path)
551 struct registry_key *tmp_key, *key;
553 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
555 if (!(name = talloc_strdup(mem_ctx, path))) {
560 /* check if the key has subkeys */
561 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
562 if (!W_ERROR_IS_OK(err)) {
565 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
568 if (key->subkeys->num_subkeys > 0) {
569 err = WERR_ACCESS_DENIED;
573 /* no subkeys - proceed with delete */
574 if ((end = strrchr(name, '\\')) != NULL) {
577 err = reg_openkey(mem_ctx, parent, name,
578 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
579 if (!W_ERROR_IS_OK(err)) {
587 if (name[0] == '\0') {
588 err = WERR_INVALID_PARAM;
592 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
596 num_subkeys = parent->subkeys->num_subkeys;
598 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
603 if (!store_reg_keys(parent->key, parent->subkeys)) {
604 TALLOC_FREE(parent->subkeys);
605 err = WERR_REG_IO_FAILURE;
609 regkey_set_secdesc(key->key, NULL);
614 TALLOC_FREE(mem_ctx);
618 WERROR reg_setvalue(struct registry_key *key, const char *name,
619 const struct registry_value *val)
622 DATA_BLOB value_data;
625 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
626 return WERR_ACCESS_DENIED;
629 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
633 err = registry_push_value(key, val, &value_data);
634 if (!W_ERROR_IS_OK(err)) {
638 res = regval_ctr_addvalue(key->values, name, val->type,
639 (char *)value_data.data, value_data.length);
640 TALLOC_FREE(value_data.data);
643 TALLOC_FREE(key->values);
647 if (!store_reg_values(key->key, key->values)) {
648 TALLOC_FREE(key->values);
649 return WERR_REG_IO_FAILURE;
655 WERROR reg_deletevalue(struct registry_key *key, const char *name)
659 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
660 return WERR_ACCESS_DENIED;
663 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
667 regval_ctr_delvalue(key->values, name);
669 if (!store_reg_values(key->key, key->values)) {
670 TALLOC_FREE(key->values);
671 return WERR_REG_IO_FAILURE;
677 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
678 struct security_descriptor **psecdesc)
680 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
683 WERROR reg_setkeysecurity(struct registry_key *key,
684 struct security_descriptor *psecdesc)
686 return regkey_set_secdesc(key->key, psecdesc);
689 WERROR reg_getversion(uint32_t *version)
691 if (version == NULL) {
692 return WERR_INVALID_PARAM;
695 *version = 0x00000005; /* Windows 2000 registry API version */
699 /**********************************************************************
700 * Higher level utility functions
701 **********************************************************************/
703 WERROR reg_deleteallvalues(struct registry_key *key)
708 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
709 return WERR_ACCESS_DENIED;
712 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
716 for (i=0; i<key->values->num_values; i++) {
717 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
720 if (!store_reg_values(key->key, key->values)) {
721 TALLOC_FREE(key->values);
722 return WERR_REG_IO_FAILURE;
729 * Utility function to open a complete registry path including the hive
730 * prefix. This should become the replacement function for
731 * regkey_open_internal.
734 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
735 uint32 desired_access, const struct nt_user_token *token,
736 struct registry_key **pkey)
738 struct registry_key *hive, *key;
742 if (!(path = SMB_STRDUP(orig_path))) {
746 p = strchr(path, '\\');
748 if ((p == NULL) || (p[1] == '\0')) {
750 * No key behind the hive, just return the hive
753 err = reg_openhive(mem_ctx, path, desired_access, token,
755 if (!W_ERROR_IS_OK(err)) {
766 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
768 if (!W_ERROR_IS_OK(err)) {
773 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
778 if (!W_ERROR_IS_OK(err)) {
787 * Utility function to delete a registry key with all its subkeys.
788 * Note that reg_deletekey returns ACCESS_DENIED when called on a
789 * key that has subkeys.
791 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
792 struct registry_key *parent,
796 TALLOC_CTX *mem_ctx = NULL;
797 WERROR werr = WERR_OK;
798 struct registry_key *key;
799 char *subkey_name = NULL;
801 mem_ctx = talloc_new(ctx);
802 if (mem_ctx == NULL) {
807 /* recurse through subkeys first */
808 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
809 if (!W_ERROR_IS_OK(werr)) {
813 while (W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, 0,
814 &subkey_name, NULL)))
816 werr = reg_deletekey_recursive_internal(mem_ctx, key,
819 if (!W_ERROR_IS_OK(werr)) {
823 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
824 DEBUG(1, ("reg_deletekey_recursive_internal: "
825 "Error enumerating subkeys: %s\n",
833 /* now delete the actual key */
834 werr = reg_deletekey(parent, path);
838 TALLOC_FREE(mem_ctx);
842 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
843 struct registry_key *parent,
846 return reg_deletekey_recursive_internal(ctx, parent, path, true);
849 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
850 struct registry_key *parent,
853 return reg_deletekey_recursive_internal(ctx, parent, path, false);
857 /* these two functions are unused. */
860 * Utility function to create a registry key without opening the hive
861 * before. Assumes the hive already exists.
864 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
865 uint32 desired_access,
866 const struct nt_user_token *token,
867 enum winreg_CreateAction *paction,
868 struct registry_key **pkey)
870 struct registry_key *hive;
874 if (!(path = SMB_STRDUP(orig_path))) {
878 p = strchr(path, '\\');
880 if ((p == NULL) || (p[1] == '\0')) {
882 * No key behind the hive, just return the hive
885 err = reg_openhive(mem_ctx, path, desired_access, token,
887 if (!W_ERROR_IS_OK(err)) {
893 *paction = REG_OPENED_EXISTING_KEY;
899 err = reg_openhive(mem_ctx, path,
900 (strchr(p+1, '\\') != NULL) ?
901 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
903 if (!W_ERROR_IS_OK(err)) {
908 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
915 * Utility function to create a registry key without opening the hive
916 * before. Will not delete a hive.
919 WERROR reg_delete_path(const struct nt_user_token *token,
920 const char *orig_path)
922 struct registry_key *hive;
926 if (!(path = SMB_STRDUP(orig_path))) {
930 p = strchr(path, '\\');
932 if ((p == NULL) || (p[1] == '\0')) {
934 return WERR_INVALID_PARAM;
939 err = reg_openhive(NULL, path,
940 (strchr(p+1, '\\') != NULL) ?
941 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
943 if (!W_ERROR_IS_OK(err)) {
948 err = reg_deletekey(hive, p+1);