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 */
24 static WERROR fill_value_cache(struct registry_key *key)
26 if (key->values != NULL) {
30 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
33 if (fetch_reg_values(key->key, key->values) == -1) {
34 TALLOC_FREE(key->values);
41 static WERROR fill_subkey_cache(struct registry_key *key)
43 if (key->subkeys != NULL) {
47 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
51 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
52 TALLOC_FREE(key->subkeys);
53 return WERR_NO_MORE_ITEMS;
59 static int regkey_destructor(REGISTRY_KEY *key)
64 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
65 struct registry_key *parent,
67 const struct nt_user_token *token,
68 uint32 access_desired,
69 struct registry_key **pregkey)
71 WERROR result = WERR_OK;
72 struct registry_key *regkey;
74 REGSUBKEY_CTR *subkeys = NULL;
76 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
78 SMB_ASSERT(strchr(name, '\\') == NULL);
80 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
81 !(regkey->token = dup_nt_token(regkey, token)) ||
82 !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
87 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
92 talloc_set_destructor(key, regkey_destructor);
96 key->type = REG_KEY_GENERIC;
98 if (name[0] == '\0') {
100 * Open a copy of the parent key
103 result = WERR_BADFILE;
106 key->name = talloc_strdup(key, parent->key->name);
112 key->name = talloc_asprintf(key, "%s%s%s",
113 parent ? parent->key->name : "",
118 if (key->name == NULL) {
123 /* Tag this as a Performance Counter Key */
125 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
126 key->type = REG_KEY_HKPD;
128 /* Look up the table of registry I/O operations */
130 if ( !(key->hook = reghook_cache_find( key->name )) ) {
131 DEBUG(0,("reg_open_onelevel: Failed to assigned a "
132 "REGISTRY_HOOK to [%s]\n", key->name ));
133 result = WERR_BADFILE;
137 /* check if the path really exists; failed is indicated by -1 */
138 /* if the subkey count failed, bail out */
140 if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
145 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
146 result = WERR_BADFILE;
150 TALLOC_FREE( subkeys );
152 if ( !regkey_access_check( key, access_desired, &key->access_granted,
154 result = WERR_ACCESS_DENIED;
162 if ( !W_ERROR_IS_OK(result) ) {
169 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
170 uint32 desired_access,
171 const struct nt_user_token *token,
172 struct registry_key **pkey)
174 SMB_ASSERT(hive != NULL);
175 SMB_ASSERT(hive[0] != '\0');
176 SMB_ASSERT(strchr(hive, '\\') == NULL);
178 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
182 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
183 const char *name, uint32 desired_access,
184 struct registry_key **pkey)
186 struct registry_key *direct_parent = parent;
188 char *p, *path, *to_free;
191 if (!(path = SMB_STRDUP(name))) {
198 if ((len > 0) && (path[len-1] == '\\')) {
202 while ((p = strchr(path, '\\')) != NULL) {
203 char *name_component;
204 struct registry_key *tmp;
206 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
211 err = regkey_open_onelevel(mem_ctx, direct_parent,
212 name_component, parent->token,
213 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
214 SAFE_FREE(name_component);
216 if (!W_ERROR_IS_OK(err)) {
219 if (direct_parent != parent) {
220 TALLOC_FREE(direct_parent);
227 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
228 desired_access, pkey);
230 if (direct_parent != parent) {
231 TALLOC_FREE(direct_parent);
237 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
238 uint32 idx, char **name, NTTIME *last_write_time)
242 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
243 return WERR_ACCESS_DENIED;
246 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
250 if (idx >= key->subkeys->num_subkeys) {
251 return WERR_NO_MORE_ITEMS;
254 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
258 if (last_write_time) {
259 *last_write_time = 0;
265 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
266 uint32 idx, char **pname, struct registry_value **pval)
268 struct registry_value *val;
271 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
272 return WERR_ACCESS_DENIED;
275 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
279 if (idx >= key->values->num_values) {
280 return WERR_NO_MORE_ITEMS;
283 err = registry_pull_value(mem_ctx, &val,
284 key->values->values[idx]->type,
285 key->values->values[idx]->data_p,
286 key->values->values[idx]->size,
287 key->values->values[idx]->size);
288 if (!W_ERROR_IS_OK(err)) {
293 && !(*pname = talloc_strdup(
294 mem_ctx, key->values->values[idx]->valuename))) {
303 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
304 const char *name, struct registry_value **pval)
309 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
310 return WERR_ACCESS_DENIED;
313 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
317 for (i=0; i<key->values->num_values; i++) {
318 if (strequal(key->values->values[i]->valuename, name)) {
319 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
326 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
327 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
328 uint32_t *num_values, uint32_t *max_valnamelen,
329 uint32_t *max_valbufsize, uint32_t *secdescsize,
330 NTTIME *last_changed_time)
336 struct security_descriptor *secdesc;
338 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
339 return WERR_ACCESS_DENIED;
342 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
343 !W_ERROR_IS_OK(fill_value_cache(key))) {
348 for (i=0; i<key->subkeys->num_subkeys; i++) {
349 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
352 *num_subkeys = key->subkeys->num_subkeys;
353 *max_subkeylen = max_len;
354 *max_subkeysize = 0; /* Class length? */
358 for (i=0; i<key->values->num_values; i++) {
359 max_len = MAX(max_len,
360 strlen(key->values->values[i]->valuename));
361 max_size = MAX(max_size, key->values->values[i]->size);
364 *num_values = key->values->num_values;
365 *max_valnamelen = max_len;
366 *max_valbufsize = max_size;
368 if (!(mem_ctx = talloc_new(key))) {
372 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
373 if (!W_ERROR_IS_OK(err)) {
374 TALLOC_FREE(mem_ctx);
378 *secdescsize = sec_desc_size(secdesc);
379 TALLOC_FREE(mem_ctx);
381 *last_changed_time = 0;
386 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
387 const char *subkeypath, uint32 desired_access,
388 struct registry_key **pkey,
389 enum winreg_CreateAction *paction)
391 struct registry_key *key = parent;
392 struct registry_key *create_parent;
396 REGSUBKEY_CTR *subkeys;
398 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
400 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
405 while ((end = strchr(path, '\\')) != NULL) {
406 struct registry_key *tmp;
407 enum winreg_CreateAction action;
411 err = reg_createkey(mem_ctx, key, path,
412 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
413 if (!W_ERROR_IS_OK(err)) {
426 * At this point, "path" contains the one-element subkey of "key". We
427 * can try to open it.
430 err = reg_openkey(ctx, key, path, desired_access, pkey);
431 if (W_ERROR_IS_OK(err)) {
432 if (paction != NULL) {
433 *paction = REG_OPENED_EXISTING_KEY;
438 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
440 * Something but "notfound" has happened, so bail out
446 * We have to make a copy of the current key, as we opened it only
447 * with ENUM_SUBKEY access.
450 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
452 if (!W_ERROR_IS_OK(err)) {
457 * Actually create the subkey
460 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
465 err = fill_subkey_cache(create_parent);
466 if (!W_ERROR_IS_OK(err)) goto done;
468 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
469 if (!W_ERROR_IS_OK(err)) goto done;
471 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
472 TALLOC_FREE(create_parent->subkeys);
473 err = WERR_REG_IO_FAILURE;
478 * Now open the newly created key
481 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
482 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
483 *paction = REG_CREATED_NEW_KEY;
487 TALLOC_FREE(mem_ctx);
492 WERROR reg_deletekey(struct registry_key *parent, const char *path)
498 struct registry_key *tmp_key;
500 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
502 if (!(name = talloc_strdup(mem_ctx, path))) {
507 /* check if the key has subkeys */
508 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &tmp_key);
509 if (!W_ERROR_IS_OK(err)) {
512 if (!W_ERROR_IS_OK(err = fill_subkey_cache(tmp_key))) {
515 if (tmp_key->subkeys->num_subkeys > 0) {
516 err = WERR_ACCESS_DENIED;
520 /* no subkeys - proceed with delete */
521 if ((end = strrchr(name, '\\')) != NULL) {
524 err = reg_openkey(mem_ctx, parent, name,
525 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
526 if (!W_ERROR_IS_OK(err)) {
534 if (name[0] == '\0') {
535 err = WERR_INVALID_PARAM;
539 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
543 num_subkeys = parent->subkeys->num_subkeys;
545 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
550 if (!store_reg_keys(parent->key, parent->subkeys)) {
551 TALLOC_FREE(parent->subkeys);
552 err = WERR_REG_IO_FAILURE;
558 TALLOC_FREE(mem_ctx);
562 WERROR reg_setvalue(struct registry_key *key, const char *name,
563 const struct registry_value *val)
566 DATA_BLOB value_data;
569 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
570 return WERR_ACCESS_DENIED;
573 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
577 err = registry_push_value(key, val, &value_data);
578 if (!W_ERROR_IS_OK(err)) {
582 res = regval_ctr_addvalue(key->values, name, val->type,
583 (char *)value_data.data, value_data.length);
584 TALLOC_FREE(value_data.data);
587 TALLOC_FREE(key->values);
591 if (!store_reg_values(key->key, key->values)) {
592 TALLOC_FREE(key->values);
593 return WERR_REG_IO_FAILURE;
599 WERROR reg_deletevalue(struct registry_key *key, const char *name)
603 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
604 return WERR_ACCESS_DENIED;
607 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
611 regval_ctr_delvalue(key->values, name);
613 if (!store_reg_values(key->key, key->values)) {
614 TALLOC_FREE(key->values);
615 return WERR_REG_IO_FAILURE;
621 WERROR reg_deleteallvalues(struct registry_key *key)
626 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
627 return WERR_ACCESS_DENIED;
630 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
634 for (i=0; i<key->values->num_values; i++) {
635 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
638 if (!store_reg_values(key->key, key->values)) {
639 TALLOC_FREE(key->values);
640 return WERR_REG_IO_FAILURE;
647 * Utility function to open a complete registry path including the hive
648 * prefix. This should become the replacement function for
649 * regkey_open_internal.
652 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
653 uint32 desired_access, const struct nt_user_token *token,
654 struct registry_key **pkey)
656 struct registry_key *hive, *key;
660 if (!(path = SMB_STRDUP(orig_path))) {
664 p = strchr(path, '\\');
666 if ((p == NULL) || (p[1] == '\0')) {
668 * No key behind the hive, just return the hive
671 err = reg_openhive(mem_ctx, path, desired_access, token,
673 if (!W_ERROR_IS_OK(err)) {
684 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
686 if (!W_ERROR_IS_OK(err)) {
691 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
696 if (!W_ERROR_IS_OK(err)) {
705 * Utility function to delete a registry key with all its subkeys.
706 * Note that reg_deletekey returns ACCESS_DENIED when called on a
707 * key that has subkeys.
709 WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
710 struct registry_key *parent,
714 TALLOC_CTX *mem_ctx = NULL;
715 WERROR werr = WERR_OK;
716 struct registry_key *key;
717 char *subkey_name = NULL;
719 mem_ctx = talloc_new(ctx);
720 if (mem_ctx == NULL) {
725 /* recurse through subkeys first */
726 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_WRITE, &key);
727 if (!W_ERROR_IS_OK(werr)) {
731 while (W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, 0,
732 &subkey_name, NULL)))
734 werr = reg_deletekey_recursive_internal(mem_ctx, key,
737 if (!W_ERROR_IS_OK(werr)) {
741 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
742 DEBUG(1, ("reg_deletekey_recursive_internal: "
743 "Error enumerating subkeys: %s\n",
751 /* now delete the actual key */
752 werr = reg_deletekey(parent, path);
756 TALLOC_FREE(mem_ctx);
760 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
761 struct registry_key *parent,
764 return reg_deletekey_recursive_internal(ctx, parent, path, True);
767 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
768 struct registry_key *parent,
771 return reg_deletekey_recursive_internal(ctx, parent, path, False);