Bug 6488: acl_group_override() call in posix acls references an uninitialized variable.
[Samba/bb.git] / source / registry / reg_api.c
blob0f12c786c3e7b9240bf29aa2c13e6d83b3e3293d
1 /*
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
25 * provided here:
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
31 * 0x04 winreg_OpenHKU
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
40 * 0x0d winreg_LoadKey
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 reg_restorekey
47 * 0x14 winreg_SaveKey reg_savekey
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
65 #include "includes.h"
66 #include "regfio.h"
68 #undef DBGC_CLASS
69 #define DBGC_CLASS DBGC_REGISTRY
72 /**********************************************************************
73 * Helper functions
74 **********************************************************************/
76 static WERROR fill_value_cache(struct registry_key *key)
78 if (key->values != NULL) {
79 if (!reg_values_need_update(key->key, key->values)) {
80 return WERR_OK;
84 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
85 return WERR_NOMEM;
87 if (fetch_reg_values(key->key, key->values) == -1) {
88 TALLOC_FREE(key->values);
89 return WERR_BADFILE;
92 return WERR_OK;
95 static WERROR fill_subkey_cache(struct registry_key *key)
97 WERROR werr;
99 if (key->subkeys != NULL) {
100 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
101 return WERR_OK;
105 werr = regsubkey_ctr_init(key, &(key->subkeys));
106 W_ERROR_NOT_OK_RETURN(werr);
108 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
109 TALLOC_FREE(key->subkeys);
110 return WERR_NO_MORE_ITEMS;
113 return WERR_OK;
116 static int regkey_destructor(REGISTRY_KEY *key)
118 return regdb_close();
121 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
122 struct registry_key *parent,
123 const char *name,
124 const struct nt_user_token *token,
125 uint32 access_desired,
126 struct registry_key **pregkey)
128 WERROR result = WERR_OK;
129 struct registry_key *regkey;
130 REGISTRY_KEY *key;
131 struct regsubkey_ctr *subkeys = NULL;
133 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
135 SMB_ASSERT(strchr(name, '\\') == NULL);
137 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
138 !(regkey->token = dup_nt_token(regkey, token)) ||
139 !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
140 result = WERR_NOMEM;
141 goto done;
144 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
145 goto done;
148 key = regkey->key;
149 talloc_set_destructor(key, regkey_destructor);
151 /* initialization */
153 key->type = REG_KEY_GENERIC;
155 if (name[0] == '\0') {
157 * Open a copy of the parent key
159 if (!parent) {
160 result = WERR_BADFILE;
161 goto done;
163 key->name = talloc_strdup(key, parent->key->name);
165 else {
167 * Normal subkey open
169 key->name = talloc_asprintf(key, "%s%s%s",
170 parent ? parent->key->name : "",
171 parent ? "\\": "",
172 name);
175 if (key->name == NULL) {
176 result = WERR_NOMEM;
177 goto done;
180 /* Tag this as a Performance Counter Key */
182 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
183 key->type = REG_KEY_HKPD;
185 /* Look up the table of registry I/O operations */
187 if ( !(key->ops = reghook_cache_find( key->name )) ) {
188 DEBUG(0,("reg_open_onelevel: Failed to assign "
189 "REGISTRY_OPS to [%s]\n", key->name ));
190 result = WERR_BADFILE;
191 goto done;
194 /* check if the path really exists; failed is indicated by -1 */
195 /* if the subkey count failed, bail out */
197 result = regsubkey_ctr_init(key, &subkeys);
198 if (!W_ERROR_IS_OK(result)) {
199 goto done;
202 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
203 result = WERR_BADFILE;
204 goto done;
207 TALLOC_FREE( subkeys );
209 if ( !regkey_access_check( key, access_desired, &key->access_granted,
210 token ) ) {
211 result = WERR_ACCESS_DENIED;
212 goto done;
215 *pregkey = regkey;
216 result = WERR_OK;
218 done:
219 if ( !W_ERROR_IS_OK(result) ) {
220 TALLOC_FREE(regkey);
223 return result;
226 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
227 uint32 desired_access,
228 const struct nt_user_token *token,
229 struct registry_key **pkey)
231 SMB_ASSERT(hive != NULL);
232 SMB_ASSERT(hive[0] != '\0');
233 SMB_ASSERT(strchr(hive, '\\') == NULL);
235 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
236 pkey);
240 /**********************************************************************
241 * The API functions
242 **********************************************************************/
244 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
245 const char *name, uint32 desired_access,
246 struct registry_key **pkey)
248 struct registry_key *direct_parent = parent;
249 WERROR err;
250 char *p, *path, *to_free;
251 size_t len;
253 if (!(path = SMB_STRDUP(name))) {
254 return WERR_NOMEM;
256 to_free = path;
258 len = strlen(path);
260 if ((len > 0) && (path[len-1] == '\\')) {
261 path[len-1] = '\0';
264 while ((p = strchr(path, '\\')) != NULL) {
265 char *name_component;
266 struct registry_key *tmp;
268 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
269 err = WERR_NOMEM;
270 goto error;
273 err = regkey_open_onelevel(mem_ctx, direct_parent,
274 name_component, parent->token,
275 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
276 SAFE_FREE(name_component);
278 if (!W_ERROR_IS_OK(err)) {
279 goto error;
281 if (direct_parent != parent) {
282 TALLOC_FREE(direct_parent);
285 direct_parent = tmp;
286 path = p+1;
289 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
290 desired_access, pkey);
291 error:
292 if (direct_parent != parent) {
293 TALLOC_FREE(direct_parent);
295 SAFE_FREE(to_free);
296 return err;
299 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
300 uint32 idx, char **name, NTTIME *last_write_time)
302 WERROR err;
304 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
305 return WERR_ACCESS_DENIED;
308 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
309 return err;
312 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
313 return WERR_NO_MORE_ITEMS;
316 if (!(*name = talloc_strdup(mem_ctx,
317 regsubkey_ctr_specific_key(key->subkeys, idx))))
319 return WERR_NOMEM;
322 if (last_write_time) {
323 *last_write_time = 0;
326 return WERR_OK;
329 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
330 uint32 idx, char **pname, struct registry_value **pval)
332 struct registry_value *val;
333 WERROR err;
335 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
336 return WERR_ACCESS_DENIED;
339 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
340 return err;
343 if (idx >= key->values->num_values) {
344 return WERR_NO_MORE_ITEMS;
347 err = registry_pull_value(mem_ctx, &val,
348 key->values->values[idx]->type,
349 key->values->values[idx]->data_p,
350 key->values->values[idx]->size,
351 key->values->values[idx]->size);
352 if (!W_ERROR_IS_OK(err)) {
353 return err;
356 if (pname
357 && !(*pname = talloc_strdup(
358 mem_ctx, key->values->values[idx]->valuename))) {
359 SAFE_FREE(val);
360 return WERR_NOMEM;
363 *pval = val;
364 return WERR_OK;
367 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
368 const char *name, struct registry_value **pval)
370 WERROR err;
371 uint32 i;
373 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
374 return WERR_ACCESS_DENIED;
377 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
378 return err;
381 for (i=0; i<key->values->num_values; i++) {
382 if (strequal(key->values->values[i]->valuename, name)) {
383 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
387 return WERR_BADFILE;
390 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
391 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
392 uint32_t *num_values, uint32_t *max_valnamelen,
393 uint32_t *max_valbufsize, uint32_t *secdescsize,
394 NTTIME *last_changed_time)
396 uint32 i, max_size;
397 size_t max_len;
398 TALLOC_CTX *mem_ctx;
399 WERROR err;
400 struct security_descriptor *secdesc;
402 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
403 return WERR_ACCESS_DENIED;
406 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
407 !W_ERROR_IS_OK(fill_value_cache(key))) {
408 return WERR_BADFILE;
411 max_len = 0;
412 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
413 max_len = MAX(max_len,
414 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
417 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
418 *max_subkeylen = max_len;
419 *max_subkeysize = 0; /* Class length? */
421 max_len = 0;
422 max_size = 0;
423 for (i=0; i<key->values->num_values; i++) {
424 max_len = MAX(max_len,
425 strlen(key->values->values[i]->valuename));
426 max_size = MAX(max_size, key->values->values[i]->size);
429 *num_values = key->values->num_values;
430 *max_valnamelen = max_len;
431 *max_valbufsize = max_size;
433 if (!(mem_ctx = talloc_new(key))) {
434 return WERR_NOMEM;
437 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
438 if (!W_ERROR_IS_OK(err)) {
439 TALLOC_FREE(mem_ctx);
440 return err;
443 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
444 TALLOC_FREE(mem_ctx);
446 *last_changed_time = 0;
448 return WERR_OK;
451 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
452 const char *subkeypath, uint32 desired_access,
453 struct registry_key **pkey,
454 enum winreg_CreateAction *paction)
456 struct registry_key *key = parent;
457 struct registry_key *create_parent;
458 TALLOC_CTX *mem_ctx;
459 char *path, *end;
460 WERROR err;
463 * We must refuse to handle subkey-paths containing
464 * a '/' character because at a lower level, after
465 * normalization, '/' is treated as a key separator
466 * just like '\\'.
468 if (strchr(subkeypath, '/') != NULL) {
469 return WERR_INVALID_PARAM;
472 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
474 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
475 err = WERR_NOMEM;
476 goto done;
479 while ((end = strchr(path, '\\')) != NULL) {
480 struct registry_key *tmp;
481 enum winreg_CreateAction action;
483 *end = '\0';
485 err = reg_createkey(mem_ctx, key, path,
486 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
487 if (!W_ERROR_IS_OK(err)) {
488 goto done;
491 if (key != parent) {
492 TALLOC_FREE(key);
495 key = tmp;
496 path = end+1;
500 * At this point, "path" contains the one-element subkey of "key". We
501 * can try to open it.
504 err = reg_openkey(ctx, key, path, desired_access, pkey);
505 if (W_ERROR_IS_OK(err)) {
506 if (paction != NULL) {
507 *paction = REG_OPENED_EXISTING_KEY;
509 goto done;
512 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
514 * Something but "notfound" has happened, so bail out
516 goto done;
520 * We have to make a copy of the current key, as we opened it only
521 * with ENUM_SUBKEY access.
524 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
525 &create_parent);
526 if (!W_ERROR_IS_OK(err)) {
527 goto done;
531 * Actually create the subkey
534 err = fill_subkey_cache(create_parent);
535 if (!W_ERROR_IS_OK(err)) goto done;
537 err = create_reg_subkey(key->key, path);
538 W_ERROR_NOT_OK_GOTO_DONE(err);
541 * Now open the newly created key
544 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
545 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
546 *paction = REG_CREATED_NEW_KEY;
549 done:
550 TALLOC_FREE(mem_ctx);
551 return err;
554 WERROR reg_deletekey(struct registry_key *parent, const char *path)
556 WERROR err;
557 char *name, *end;
558 struct registry_key *tmp_key, *key;
559 TALLOC_CTX *mem_ctx = talloc_stackframe();
561 name = talloc_strdup(mem_ctx, path);
562 if (name == NULL) {
563 err = WERR_NOMEM;
564 goto done;
567 /* check if the key has subkeys */
568 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
569 W_ERROR_NOT_OK_GOTO_DONE(err);
571 err = fill_subkey_cache(key);
572 W_ERROR_NOT_OK_GOTO_DONE(err);
574 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
575 err = WERR_ACCESS_DENIED;
576 goto done;
579 /* no subkeys - proceed with delete */
580 end = strrchr(name, '\\');
581 if (end != NULL) {
582 *end = '\0';
584 err = reg_openkey(mem_ctx, parent, name,
585 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
586 W_ERROR_NOT_OK_GOTO_DONE(err);
588 parent = tmp_key;
589 name = end+1;
592 if (name[0] == '\0') {
593 err = WERR_INVALID_PARAM;
594 goto done;
597 err = delete_reg_subkey(parent->key, name);
599 done:
600 TALLOC_FREE(mem_ctx);
601 return err;
604 WERROR reg_setvalue(struct registry_key *key, const char *name,
605 const struct registry_value *val)
607 WERROR err;
608 DATA_BLOB value_data;
609 int res;
611 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
612 return WERR_ACCESS_DENIED;
615 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
616 return err;
619 err = registry_push_value(key, val, &value_data);
620 if (!W_ERROR_IS_OK(err)) {
621 return err;
624 res = regval_ctr_addvalue(key->values, name, val->type,
625 (char *)value_data.data, value_data.length);
626 TALLOC_FREE(value_data.data);
628 if (res == 0) {
629 TALLOC_FREE(key->values);
630 return WERR_NOMEM;
633 if (!store_reg_values(key->key, key->values)) {
634 TALLOC_FREE(key->values);
635 return WERR_REG_IO_FAILURE;
638 return WERR_OK;
641 static WERROR reg_value_exists(struct registry_key *key, const char *name)
643 int i;
645 for (i=0; i<key->values->num_values; i++) {
646 if (strequal(key->values->values[i]->valuename, name)) {
647 return WERR_OK;
651 return WERR_BADFILE;
654 WERROR reg_deletevalue(struct registry_key *key, const char *name)
656 WERROR err;
658 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
659 return WERR_ACCESS_DENIED;
662 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
663 return err;
666 err = reg_value_exists(key, name);
667 if (!W_ERROR_IS_OK(err)) {
668 return err;
671 regval_ctr_delvalue(key->values, name);
673 if (!store_reg_values(key->key, key->values)) {
674 TALLOC_FREE(key->values);
675 return WERR_REG_IO_FAILURE;
678 return WERR_OK;
681 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
682 struct security_descriptor **psecdesc)
684 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
687 WERROR reg_setkeysecurity(struct registry_key *key,
688 struct security_descriptor *psecdesc)
690 return regkey_set_secdesc(key->key, psecdesc);
693 WERROR reg_getversion(uint32_t *version)
695 if (version == NULL) {
696 return WERR_INVALID_PARAM;
699 *version = 0x00000005; /* Windows 2000 registry API version */
700 return WERR_OK;
703 /*******************************************************************
704 Note: topkeypat is the *full* path that this *key will be
705 loaded into (including the name of the key)
706 ********************************************************************/
708 static WERROR reg_load_tree(REGF_FILE *regfile, const char *topkeypath,
709 REGF_NK_REC *key)
711 REGF_NK_REC *subkey;
712 REGISTRY_KEY registry_key;
713 REGVAL_CTR *values;
714 struct regsubkey_ctr *subkeys;
715 int i;
716 char *path = NULL;
717 WERROR result = WERR_OK;
719 /* initialize the REGISTRY_KEY structure */
721 registry_key.ops = reghook_cache_find(topkeypath);
722 if (!registry_key.ops) {
723 DEBUG(0, ("reg_load_tree: Failed to assign REGISTRY_OPS "
724 "to [%s]\n", topkeypath));
725 return WERR_BADFILE;
728 registry_key.name = talloc_strdup(regfile->mem_ctx, topkeypath);
729 if (!registry_key.name) {
730 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
731 return WERR_NOMEM;
734 /* now start parsing the values and subkeys */
736 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
737 W_ERROR_NOT_OK_RETURN(result);
739 values = TALLOC_ZERO_P(subkeys, REGVAL_CTR);
740 if (values == NULL) {
741 return WERR_NOMEM;
744 /* copy values into the REGVAL_CTR */
746 for (i=0; i<key->num_values; i++) {
747 regval_ctr_addvalue(values, key->values[i].valuename,
748 key->values[i].type,
749 (char*)key->values[i].data,
750 (key->values[i].data_size & ~VK_DATA_IN_OFFSET));
753 /* copy subkeys into the struct regsubkey_ctr */
755 key->subkey_index = 0;
756 while ((subkey = regfio_fetch_subkey( regfile, key ))) {
757 result = regsubkey_ctr_addkey(subkeys, subkey->keyname);
758 if (!W_ERROR_IS_OK(result)) {
759 TALLOC_FREE(subkeys);
760 return result;
764 /* write this key and values out */
766 if (!store_reg_values(&registry_key, values)
767 || !store_reg_keys(&registry_key, subkeys))
769 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath));
770 result = WERR_REG_IO_FAILURE;
773 TALLOC_FREE(subkeys);
775 if (!W_ERROR_IS_OK(result)) {
776 return result;
779 /* now continue to load each subkey registry tree */
781 key->subkey_index = 0;
782 while ((subkey = regfio_fetch_subkey(regfile, key))) {
783 path = talloc_asprintf(regfile->mem_ctx,
784 "%s\\%s",
785 topkeypath,
786 subkey->keyname);
787 if (path == NULL) {
788 return WERR_NOMEM;
790 result = reg_load_tree(regfile, path, subkey);
791 if (!W_ERROR_IS_OK(result)) {
792 break;
796 return result;
799 /*******************************************************************
800 ********************************************************************/
802 static WERROR restore_registry_key(REGISTRY_KEY *krecord, const char *fname)
804 REGF_FILE *regfile;
805 REGF_NK_REC *rootkey;
806 WERROR result;
808 /* open the registry file....fail if the file already exists */
810 regfile = regfio_open(fname, (O_RDONLY), 0);
811 if (regfile == NULL) {
812 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
813 fname, strerror(errno)));
814 return ntstatus_to_werror(map_nt_error_from_unix(errno));
817 /* get the rootkey from the regf file and then load the tree
818 via recursive calls */
820 if (!(rootkey = regfio_rootkey(regfile))) {
821 regfio_close(regfile);
822 return WERR_REG_FILE_INVALID;
825 result = reg_load_tree(regfile, krecord->name, rootkey);
827 /* cleanup */
829 regfio_close(regfile);
831 return result;
834 WERROR reg_restorekey(struct registry_key *key, const char *fname)
836 return restore_registry_key(key->key, fname);
839 /********************************************************************
840 ********************************************************************/
842 static WERROR reg_write_tree(REGF_FILE *regfile, const char *keypath,
843 REGF_NK_REC *parent)
845 REGF_NK_REC *key;
846 REGVAL_CTR *values;
847 struct regsubkey_ctr *subkeys;
848 int i, num_subkeys;
849 char *key_tmp = NULL;
850 char *keyname, *parentpath;
851 char *subkeypath = NULL;
852 char *subkeyname;
853 REGISTRY_KEY registry_key;
854 WERROR result = WERR_OK;
855 SEC_DESC *sec_desc = NULL;
857 if (!regfile) {
858 return WERR_GENERAL_FAILURE;
861 if (!keypath) {
862 return WERR_OBJECT_PATH_INVALID;
865 /* split up the registry key path */
867 key_tmp = talloc_strdup(regfile->mem_ctx, keypath);
868 if (!key_tmp) {
869 return WERR_NOMEM;
871 if (!reg_split_key(key_tmp, &parentpath, &keyname)) {
872 return WERR_OBJECT_PATH_INVALID;
875 if (!keyname) {
876 keyname = parentpath;
879 /* we need a REGISTRY_KEY object here to enumerate subkeys and values */
881 ZERO_STRUCT(registry_key);
883 registry_key.name = talloc_strdup(regfile->mem_ctx, keypath);
884 if (registry_key.name == NULL) {
885 return WERR_NOMEM;
888 registry_key.ops = reghook_cache_find(registry_key.name);
889 if (registry_key.ops == NULL) {
890 return WERR_BADFILE;
893 /* lookup the values and subkeys */
895 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
896 W_ERROR_NOT_OK_RETURN(result);
898 values = TALLOC_ZERO_P(subkeys, REGVAL_CTR);
899 if (values == NULL) {
900 return WERR_NOMEM;
903 fetch_reg_keys(&registry_key, subkeys);
904 fetch_reg_values(&registry_key, values);
906 result = regkey_get_secdesc(regfile->mem_ctx, &registry_key, &sec_desc);
907 if (!W_ERROR_IS_OK(result)) {
908 goto done;
911 /* write out this key */
913 key = regfio_write_key(regfile, keyname, values, subkeys, sec_desc,
914 parent);
915 if (key == NULL) {
916 result = WERR_CAN_NOT_COMPLETE;
917 goto done;
920 /* write each one of the subkeys out */
922 num_subkeys = regsubkey_ctr_numkeys(subkeys);
923 for (i=0; i<num_subkeys; i++) {
924 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
925 subkeypath = talloc_asprintf(regfile->mem_ctx, "%s\\%s",
926 keypath, subkeyname);
927 if (subkeypath == NULL) {
928 result = WERR_NOMEM;
929 goto done;
931 result = reg_write_tree(regfile, subkeypath, key);
932 if (!W_ERROR_IS_OK(result))
933 goto done;
936 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath));
938 done:
939 TALLOC_FREE(subkeys);
940 TALLOC_FREE(registry_key.name);
942 return result;
945 static WERROR backup_registry_key(REGISTRY_KEY *krecord, const char *fname)
947 REGF_FILE *regfile;
948 WERROR result;
950 /* open the registry file....fail if the file already exists */
952 regfile = regfio_open(fname, (O_RDWR|O_CREAT|O_EXCL),
953 (S_IREAD|S_IWRITE));
954 if (regfile == NULL) {
955 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
956 fname, strerror(errno) ));
957 return ntstatus_to_werror(map_nt_error_from_unix(errno));
960 /* write the registry tree to the file */
962 result = reg_write_tree(regfile, krecord->name, NULL);
964 /* cleanup */
966 regfio_close(regfile);
968 return result;
971 WERROR reg_savekey(struct registry_key *key, const char *fname)
973 return backup_registry_key(key->key, fname);
976 /**********************************************************************
977 * Higher level utility functions
978 **********************************************************************/
980 WERROR reg_deleteallvalues(struct registry_key *key)
982 WERROR err;
983 int i;
985 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
986 return WERR_ACCESS_DENIED;
989 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
990 return err;
993 for (i=0; i<key->values->num_values; i++) {
994 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
997 if (!store_reg_values(key->key, key->values)) {
998 TALLOC_FREE(key->values);
999 return WERR_REG_IO_FAILURE;
1002 return WERR_OK;
1006 * Utility function to open a complete registry path including the hive prefix.
1009 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1010 uint32 desired_access, const struct nt_user_token *token,
1011 struct registry_key **pkey)
1013 struct registry_key *hive, *key;
1014 char *path, *p;
1015 WERROR err;
1017 if (!(path = SMB_STRDUP(orig_path))) {
1018 return WERR_NOMEM;
1021 p = strchr(path, '\\');
1023 if ((p == NULL) || (p[1] == '\0')) {
1025 * No key behind the hive, just return the hive
1028 err = reg_openhive(mem_ctx, path, desired_access, token,
1029 &hive);
1030 if (!W_ERROR_IS_OK(err)) {
1031 SAFE_FREE(path);
1032 return err;
1034 SAFE_FREE(path);
1035 *pkey = hive;
1036 return WERR_OK;
1039 *p = '\0';
1041 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
1042 &hive);
1043 if (!W_ERROR_IS_OK(err)) {
1044 SAFE_FREE(path);
1045 return err;
1048 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
1050 TALLOC_FREE(hive);
1051 SAFE_FREE(path);
1053 if (!W_ERROR_IS_OK(err)) {
1054 return err;
1057 *pkey = key;
1058 return WERR_OK;
1062 * Utility function to delete a registry key with all its subkeys.
1063 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1064 * key that has subkeys.
1066 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
1067 struct registry_key *parent,
1068 const char *path,
1069 bool del_key)
1071 TALLOC_CTX *mem_ctx = NULL;
1072 WERROR werr = WERR_OK;
1073 struct registry_key *key;
1074 char *subkey_name = NULL;
1075 uint32 i;
1077 mem_ctx = talloc_new(ctx);
1078 if (mem_ctx == NULL) {
1079 werr = WERR_NOMEM;
1080 goto done;
1083 /* recurse through subkeys first */
1084 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
1085 if (!W_ERROR_IS_OK(werr)) {
1086 goto done;
1089 werr = fill_subkey_cache(key);
1090 W_ERROR_NOT_OK_GOTO_DONE(werr);
1093 * loop from top to bottom for perfomance:
1094 * this way, we need to rehash the regsubkey containers less
1096 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
1097 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
1098 werr = reg_deletekey_recursive_internal(mem_ctx, key,
1099 subkey_name,
1100 true);
1101 W_ERROR_NOT_OK_GOTO_DONE(werr);
1104 if (del_key) {
1105 /* now delete the actual key */
1106 werr = reg_deletekey(parent, path);
1109 done:
1110 TALLOC_FREE(mem_ctx);
1111 return werr;
1114 static WERROR reg_deletekey_recursive_trans(TALLOC_CTX *ctx,
1115 struct registry_key *parent,
1116 const char *path,
1117 bool del_key)
1119 WERROR werr;
1121 werr = regdb_transaction_start();
1122 if (!W_ERROR_IS_OK(werr)) {
1123 DEBUG(0, ("reg_deletekey_recursive_trans: "
1124 "error starting transaction: %s\n",
1125 dos_errstr(werr)));
1126 return werr;
1129 werr = reg_deletekey_recursive_internal(ctx, parent, path, del_key);
1131 if (!W_ERROR_IS_OK(werr)) {
1132 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1133 "'%s': %s\n", path, parent->key->name,
1134 dos_errstr(werr)));
1135 werr = regdb_transaction_cancel();
1136 if (!W_ERROR_IS_OK(werr)) {
1137 DEBUG(0, ("reg_deletekey_recursive_trans: "
1138 "error cancelling transaction: %s\n",
1139 dos_errstr(werr)));
1141 } else {
1142 werr = regdb_transaction_commit();
1143 if (!W_ERROR_IS_OK(werr)) {
1144 DEBUG(0, ("reg_deletekey_recursive_trans: "
1145 "error committing transaction: %s\n",
1146 dos_errstr(werr)));
1150 return werr;
1153 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
1154 struct registry_key *parent,
1155 const char *path)
1157 return reg_deletekey_recursive_trans(ctx, parent, path, true);
1160 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
1161 struct registry_key *parent,
1162 const char *path)
1164 return reg_deletekey_recursive_trans(ctx, parent, path, false);
1167 #if 0
1168 /* these two functions are unused. */
1171 * Utility function to create a registry key without opening the hive
1172 * before. Assumes the hive already exists.
1175 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1176 uint32 desired_access,
1177 const struct nt_user_token *token,
1178 enum winreg_CreateAction *paction,
1179 struct registry_key **pkey)
1181 struct registry_key *hive;
1182 char *path, *p;
1183 WERROR err;
1185 if (!(path = SMB_STRDUP(orig_path))) {
1186 return WERR_NOMEM;
1189 p = strchr(path, '\\');
1191 if ((p == NULL) || (p[1] == '\0')) {
1193 * No key behind the hive, just return the hive
1196 err = reg_openhive(mem_ctx, path, desired_access, token,
1197 &hive);
1198 if (!W_ERROR_IS_OK(err)) {
1199 SAFE_FREE(path);
1200 return err;
1202 SAFE_FREE(path);
1203 *pkey = hive;
1204 *paction = REG_OPENED_EXISTING_KEY;
1205 return WERR_OK;
1208 *p = '\0';
1210 err = reg_openhive(mem_ctx, path,
1211 (strchr(p+1, '\\') != NULL) ?
1212 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
1213 token, &hive);
1214 if (!W_ERROR_IS_OK(err)) {
1215 SAFE_FREE(path);
1216 return err;
1219 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
1220 SAFE_FREE(path);
1221 TALLOC_FREE(hive);
1222 return err;
1226 * Utility function to create a registry key without opening the hive
1227 * before. Will not delete a hive.
1230 WERROR reg_delete_path(const struct nt_user_token *token,
1231 const char *orig_path)
1233 struct registry_key *hive;
1234 char *path, *p;
1235 WERROR err;
1237 if (!(path = SMB_STRDUP(orig_path))) {
1238 return WERR_NOMEM;
1241 p = strchr(path, '\\');
1243 if ((p == NULL) || (p[1] == '\0')) {
1244 SAFE_FREE(path);
1245 return WERR_INVALID_PARAM;
1248 *p = '\0';
1250 err = reg_openhive(NULL, path,
1251 (strchr(p+1, '\\') != NULL) ?
1252 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
1253 token, &hive);
1254 if (!W_ERROR_IS_OK(err)) {
1255 SAFE_FREE(path);
1256 return err;
1259 err = reg_deletekey(hive, p+1);
1260 SAFE_FREE(path);
1261 TALLOC_FREE(hive);
1262 return err;
1264 #endif /* #if 0 */