Fix build for pam_smbpass
[Samba.git] / source / registry / reg_api.c
blobd1657c8cf6075fb260234b2afd2f46942566e8be
1 /*
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 */
22 #include "includes.h"
24 #undef DBGC_CLASS
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)) {
31 return WERR_OK;
35 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
36 return WERR_NOMEM;
38 if (fetch_reg_values(key->key, key->values) == -1) {
39 TALLOC_FREE(key->values);
40 return WERR_BADFILE;
43 return WERR_OK;
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)) {
50 return WERR_OK;
54 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
55 return WERR_NOMEM;
58 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
59 TALLOC_FREE(key->subkeys);
60 return WERR_NO_MORE_ITEMS;
63 return WERR_OK;
66 static int regkey_destructor(REGISTRY_KEY *key)
68 return regdb_close();
71 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
72 struct registry_key *parent,
73 const char *name,
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;
80 REGISTRY_KEY *key;
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))) {
90 result = WERR_NOMEM;
91 goto done;
94 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
95 goto done;
98 key = regkey->key;
99 talloc_set_destructor(key, regkey_destructor);
101 /* initialization */
103 key->type = REG_KEY_GENERIC;
105 if (name[0] == '\0') {
107 * Open a copy of the parent key
109 if (!parent) {
110 result = WERR_BADFILE;
111 goto done;
113 key->name = talloc_strdup(key, parent->key->name);
115 else {
117 * Normal subkey open
119 key->name = talloc_asprintf(key, "%s%s%s",
120 parent ? parent->key->name : "",
121 parent ? "\\": "",
122 name);
125 if (key->name == NULL) {
126 result = WERR_NOMEM;
127 goto done;
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;
141 goto done;
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 )) ) {
148 result = WERR_NOMEM;
149 goto done;
152 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
153 result = WERR_BADFILE;
154 goto done;
157 TALLOC_FREE( subkeys );
159 if ( !regkey_access_check( key, access_desired, &key->access_granted,
160 token ) ) {
161 result = WERR_ACCESS_DENIED;
162 goto done;
165 *pregkey = regkey;
166 result = WERR_OK;
168 done:
169 if ( !W_ERROR_IS_OK(result) ) {
170 TALLOC_FREE(regkey);
173 return 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,
186 pkey);
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;
194 WERROR err;
195 char *p, *path, *to_free;
196 size_t len;
198 if (!(path = SMB_STRDUP(name))) {
199 return WERR_NOMEM;
201 to_free = path;
203 len = strlen(path);
205 if ((len > 0) && (path[len-1] == '\\')) {
206 path[len-1] = '\0';
209 while ((p = strchr(path, '\\')) != NULL) {
210 char *name_component;
211 struct registry_key *tmp;
213 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
214 err = WERR_NOMEM;
215 goto error;
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)) {
224 goto error;
226 if (direct_parent != parent) {
227 TALLOC_FREE(direct_parent);
230 direct_parent = tmp;
231 path = p+1;
234 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
235 desired_access, pkey);
236 error:
237 if (direct_parent != parent) {
238 TALLOC_FREE(direct_parent);
240 SAFE_FREE(to_free);
241 return err;
244 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
245 uint32 idx, char **name, NTTIME *last_write_time)
247 WERROR err;
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))) {
254 return err;
257 if (idx >= key->subkeys->num_subkeys) {
258 return WERR_NO_MORE_ITEMS;
261 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
262 return WERR_NOMEM;
265 if (last_write_time) {
266 *last_write_time = 0;
269 return WERR_OK;
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;
276 WERROR err;
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)))) {
283 return err;
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)) {
296 return err;
299 if (pname
300 && !(*pname = talloc_strdup(
301 mem_ctx, key->values->values[idx]->valuename))) {
302 SAFE_FREE(val);
303 return WERR_NOMEM;
306 *pval = val;
307 return WERR_OK;
310 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
311 const char *name, struct registry_value **pval)
313 WERROR err;
314 uint32 i;
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)))) {
321 return err;
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);
330 return WERR_BADFILE;
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)
339 uint32 i, max_size;
340 size_t max_len;
341 TALLOC_CTX *mem_ctx;
342 WERROR err;
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))) {
351 return WERR_BADFILE;
354 max_len = 0;
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? */
363 max_len = 0;
364 max_size = 0;
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))) {
376 return WERR_NOMEM;
379 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
380 if (!W_ERROR_IS_OK(err)) {
381 TALLOC_FREE(mem_ctx);
382 return err;
385 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
386 TALLOC_FREE(mem_ctx);
388 *last_changed_time = 0;
390 return WERR_OK;
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;
400 TALLOC_CTX *mem_ctx;
401 char *path, *end;
402 WERROR err;
403 REGSUBKEY_CTR *subkeys;
405 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
407 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
408 err = WERR_NOMEM;
409 goto done;
412 while ((end = strchr(path, '\\')) != NULL) {
413 struct registry_key *tmp;
414 enum winreg_CreateAction action;
416 *end = '\0';
418 err = reg_createkey(mem_ctx, key, path,
419 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
420 if (!W_ERROR_IS_OK(err)) {
421 goto done;
424 if (key != parent) {
425 TALLOC_FREE(key);
428 key = tmp;
429 path = end+1;
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;
442 goto done;
445 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
447 * Something but "notfound" has happened, so bail out
449 goto done;
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,
458 &create_parent);
459 if (!W_ERROR_IS_OK(err)) {
460 goto done;
464 * Actually create the subkey
467 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
468 err = WERR_NOMEM;
469 goto done;
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;
481 goto done;
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;
493 done:
494 TALLOC_FREE(mem_ctx);
495 return err;
498 WERROR reg_deletekey(struct registry_key *parent, const char *path)
500 WERROR err;
501 TALLOC_CTX *mem_ctx;
502 char *name, *end;
503 int num_subkeys;
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))) {
509 err = WERR_NOMEM;
510 goto error;
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)) {
516 goto error;
518 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
519 goto error;
521 if (key->subkeys->num_subkeys > 0) {
522 err = WERR_ACCESS_DENIED;
523 goto error;
526 /* no subkeys - proceed with delete */
527 if ((end = strrchr(name, '\\')) != NULL) {
528 *end = '\0';
530 err = reg_openkey(mem_ctx, parent, name,
531 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
532 if (!W_ERROR_IS_OK(err)) {
533 goto error;
536 parent = tmp_key;
537 name = end+1;
540 if (name[0] == '\0') {
541 err = WERR_INVALID_PARAM;
542 goto error;
545 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
546 goto error;
549 num_subkeys = parent->subkeys->num_subkeys;
551 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
552 err = WERR_BADFILE;
553 goto error;
556 if (!store_reg_keys(parent->key, parent->subkeys)) {
557 TALLOC_FREE(parent->subkeys);
558 err = WERR_REG_IO_FAILURE;
559 goto error;
562 regkey_set_secdesc(key->key, NULL);
564 err = WERR_OK;
566 error:
567 TALLOC_FREE(mem_ctx);
568 return err;
571 WERROR reg_setvalue(struct registry_key *key, const char *name,
572 const struct registry_value *val)
574 WERROR err;
575 DATA_BLOB value_data;
576 int res;
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))) {
583 return err;
586 err = registry_push_value(key, val, &value_data);
587 if (!W_ERROR_IS_OK(err)) {
588 return 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);
595 if (res == 0) {
596 TALLOC_FREE(key->values);
597 return WERR_NOMEM;
600 if (!store_reg_values(key->key, key->values)) {
601 TALLOC_FREE(key->values);
602 return WERR_REG_IO_FAILURE;
605 return WERR_OK;
608 WERROR reg_deletevalue(struct registry_key *key, const char *name)
610 WERROR err;
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))) {
617 return err;
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;
627 return WERR_OK;
630 WERROR reg_deleteallvalues(struct registry_key *key)
632 WERROR err;
633 int i;
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))) {
640 return err;
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;
652 return WERR_OK;
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;
666 char *path, *p;
667 WERROR err;
669 if (!(path = SMB_STRDUP(orig_path))) {
670 return WERR_NOMEM;
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,
681 &hive);
682 if (!W_ERROR_IS_OK(err)) {
683 SAFE_FREE(path);
684 return err;
686 SAFE_FREE(path);
687 *pkey = hive;
688 return WERR_OK;
691 *p = '\0';
693 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
694 &hive);
695 if (!W_ERROR_IS_OK(err)) {
696 SAFE_FREE(path);
697 return err;
700 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
702 TALLOC_FREE(hive);
703 SAFE_FREE(path);
705 if (!W_ERROR_IS_OK(err)) {
706 return err;
709 *pkey = key;
710 return WERR_OK;
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,
720 const char *path,
721 bool del_key)
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) {
730 werr = WERR_NOMEM;
731 goto done;
734 /* recurse through subkeys first */
735 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
736 if (!W_ERROR_IS_OK(werr)) {
737 goto done;
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,
744 subkey_name,
745 true);
746 if (!W_ERROR_IS_OK(werr)) {
747 goto done;
750 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
751 DEBUG(1, ("reg_deletekey_recursive_internal: "
752 "Error enumerating subkeys: %s\n",
753 dos_errstr(werr)));
754 goto done;
757 werr = WERR_OK;
759 if (del_key) {
760 /* now delete the actual key */
761 werr = reg_deletekey(parent, path);
764 done:
765 TALLOC_FREE(mem_ctx);
766 return werr;
769 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
770 struct registry_key *parent,
771 const char *path)
773 return reg_deletekey_recursive_internal(ctx, parent, path, true);
776 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
777 struct registry_key *parent,
778 const char *path)
780 return reg_deletekey_recursive_internal(ctx, parent, path, false);