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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Attempt to wrap the existing API in a more winreg.idl-like way */
25 static WERROR fill_value_cache(struct registry_key *key)
27 if (key->values != NULL) {
31 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
34 if (fetch_reg_values(key->key, key->values) == -1) {
35 TALLOC_FREE(key->values);
42 static WERROR fill_subkey_cache(struct registry_key *key)
44 if (key->subkeys != NULL) {
48 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
52 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
53 TALLOC_FREE(key->subkeys);
54 return WERR_NO_MORE_ITEMS;
60 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
61 uint32 desired_access,
62 const struct nt_user_token *token,
63 struct registry_key **pkey)
65 struct registry_key *key;
68 SMB_ASSERT(hive[0] != '\0');
69 SMB_ASSERT(strchr(hive, '\\') == NULL);
71 if (!(key = TALLOC_ZERO_P(mem_ctx, struct registry_key))) {
75 if (!(key->token = dup_nt_token(key, token))) {
80 err = regkey_open_internal(key, &key->key, hive, token,
83 if (!W_ERROR_IS_OK(err)) {
93 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
94 const char *name, uint32 desired_access,
95 struct registry_key **pkey)
97 struct registry_key *key;
101 if (!(key = TALLOC_ZERO_P(mem_ctx, struct registry_key))) {
105 if (!(key->token = dup_nt_token(key, parent->token))) {
110 if (name[0] == '\0') {
112 * Make a copy of the parent
114 path = talloc_strdup(key, parent->key->name);
118 * Normal subpath open
120 path = talloc_asprintf(key, "%s\\%s", parent->key->name,
129 err = regkey_open_internal(key, &key->key, path, parent->token,
133 if (!W_ERROR_IS_OK(err)) {
142 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
143 uint32 idx, char **name, NTTIME *last_write_time)
147 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
148 return WERR_ACCESS_DENIED;
151 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
155 if (idx >= key->subkeys->num_subkeys) {
156 return WERR_NO_MORE_ITEMS;
159 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
163 if (last_write_time) {
164 *last_write_time = 0;
170 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
171 uint32 idx, const char **pname,
172 struct registry_value **pval)
174 struct registry_value *val;
177 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
178 return WERR_ACCESS_DENIED;
181 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
185 if (idx >= key->values->num_values) {
189 err = registry_pull_value(mem_ctx, &val,
190 key->values->values[idx]->type,
191 key->values->values[idx]->data_p,
192 key->values->values[idx]->size,
193 key->values->values[idx]->size);
194 if (!W_ERROR_IS_OK(err)) {
199 && !(*pname = talloc_strdup(
200 mem_ctx, key->values->values[idx]->valuename))) {
209 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
210 const char *name, struct registry_value **pval)
215 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
216 return WERR_ACCESS_DENIED;
219 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
223 for (i=0; i<key->values->num_values; i++) {
224 if (strequal(key->values->values[i]->valuename, name)) {
225 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
232 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
233 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
234 uint32_t *num_values, uint32_t *max_valnamelen,
235 uint32_t *max_valbufsize, uint32_t *secdescsize,
236 NTTIME *last_changed_time)
242 struct security_descriptor *secdesc;
244 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
245 return WERR_ACCESS_DENIED;
248 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
249 !W_ERROR_IS_OK(fill_value_cache(key))) {
254 for (i=0; i<key->subkeys->num_subkeys; i++) {
255 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
258 *num_subkeys = key->subkeys->num_subkeys;
259 *max_subkeylen = max_len;
260 *max_subkeysize = 0; /* Class length? */
264 for (i=0; i<key->values->num_values; i++) {
265 max_len = MAX(max_len,
266 strlen(key->values->values[i]->valuename));
267 max_size = MAX(max_size, key->values->values[i]->size);
270 *num_values = key->values->num_values;
271 *max_valnamelen = max_len;
272 *max_valbufsize = max_size;
274 if (!(mem_ctx = talloc_new(key))) {
278 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
279 if (!W_ERROR_IS_OK(err)) {
280 TALLOC_FREE(mem_ctx);
284 *secdescsize = sec_desc_size(secdesc);
285 TALLOC_FREE(mem_ctx);
287 *last_changed_time = 0;
292 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
293 const char *subkeypath, uint32 desired_access,
294 struct registry_key **pkey,
295 enum winreg_CreateAction *paction)
297 struct registry_key *key = parent;
298 struct registry_key *create_parent;
302 REGSUBKEY_CTR *subkeys;
304 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
306 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
311 while ((end = strchr(path, '\\')) != NULL) {
312 struct registry_key *tmp;
313 enum winreg_CreateAction action;
317 err = reg_createkey(mem_ctx, key, path,
318 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
319 if (!W_ERROR_IS_OK(err)) {
332 * At this point, "path" contains the one-element subkey of "key". We
333 * can try to open it.
336 err = reg_openkey(ctx, key, path, desired_access, pkey);
337 if (W_ERROR_IS_OK(err)) {
338 *paction = REG_OPENED_EXISTING_KEY;
342 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
344 * Something but "notfound" has happened, so bail out
350 * We have to make a copy of the current key, as we opened it only
351 * with ENUM_SUBKEY access.
354 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
356 if (!W_ERROR_IS_OK(err)) {
361 * Actually create the subkey
364 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
369 err = fill_subkey_cache(create_parent);
370 if (!W_ERROR_IS_OK(err)) goto done;
372 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
373 if (!W_ERROR_IS_OK(err)) goto done;
375 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
376 TALLOC_FREE(create_parent->subkeys);
377 err = WERR_REG_IO_FAILURE;
382 * Now open the newly created key
385 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
386 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
387 *paction = REG_CREATED_NEW_KEY;
391 TALLOC_FREE(mem_ctx);
396 WERROR reg_deletekey(struct registry_key *parent, const char *path)
403 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
405 if (!(name = talloc_strdup(mem_ctx, path))) {
410 if ((end = strrchr(name, '\\')) != NULL) {
411 struct registry_key *tmp;
415 err = reg_openkey(mem_ctx, parent, name,
416 SEC_RIGHTS_CREATE_SUBKEY, &tmp);
417 if (!W_ERROR_IS_OK(err)) {
425 if (name[0] == '\0') {
426 err = WERR_INVALID_PARAM;
430 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
434 num_subkeys = parent->subkeys->num_subkeys;
436 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
441 if (!store_reg_keys(parent->key, parent->subkeys)) {
442 TALLOC_FREE(parent->subkeys);
443 err = WERR_REG_IO_FAILURE;
449 TALLOC_FREE(mem_ctx);
453 WERROR reg_setvalue(struct registry_key *key, const char *name,
454 const struct registry_value *val)
457 DATA_BLOB value_data;
460 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
461 return WERR_ACCESS_DENIED;
464 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
468 err = registry_push_value(key, val, &value_data);
469 if (!W_ERROR_IS_OK(err)) {
473 res = regval_ctr_addvalue(key->values, name, val->type,
474 (char *)value_data.data, value_data.length);
475 TALLOC_FREE(value_data.data);
478 TALLOC_FREE(key->values);
482 if (!store_reg_values(key->key, key->values)) {
483 TALLOC_FREE(key->values);
484 return WERR_REG_IO_FAILURE;
490 WERROR reg_deletevalue(struct registry_key *key, const char *name)
494 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
495 return WERR_ACCESS_DENIED;
498 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
502 regval_ctr_delvalue(key->values, name);
504 if (!store_reg_values(key->key, key->values)) {
505 TALLOC_FREE(key->values);
506 return WERR_REG_IO_FAILURE;