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 SMB_ASSERT(hive != NULL);
66 SMB_ASSERT(hive[0] != '\0');
67 SMB_ASSERT(strchr(hive, '\\') == NULL);
69 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
73 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
74 const char *name, uint32 desired_access,
75 struct registry_key **pkey)
77 struct registry_key *direct_parent = parent;
79 char *p, *path, *to_free;
82 if (!(path = SMB_STRDUP(name))) {
89 if ((len > 0) && (path[len-1] == '\\')) {
93 while ((p = strchr(path, '\\')) != NULL) {
95 struct registry_key *tmp;
97 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
102 err = regkey_open_onelevel(mem_ctx, direct_parent,
103 name_component, parent->token,
104 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
105 SAFE_FREE(name_component);
107 if (!W_ERROR_IS_OK(err)) {
110 if (direct_parent != parent) {
111 TALLOC_FREE(direct_parent);
118 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
119 desired_access, pkey);
121 if (direct_parent != parent) {
122 TALLOC_FREE(direct_parent);
128 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
129 uint32 idx, char **name, NTTIME *last_write_time)
133 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
134 return WERR_ACCESS_DENIED;
137 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
141 if (idx >= key->subkeys->num_subkeys) {
142 return WERR_NO_MORE_ITEMS;
145 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
149 if (last_write_time) {
150 *last_write_time = 0;
156 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
157 uint32 idx, char **pname, struct registry_value **pval)
159 struct registry_value *val;
162 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
163 return WERR_ACCESS_DENIED;
166 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
170 if (idx >= key->values->num_values) {
174 err = registry_pull_value(mem_ctx, &val,
175 key->values->values[idx]->type,
176 key->values->values[idx]->data_p,
177 key->values->values[idx]->size,
178 key->values->values[idx]->size);
179 if (!W_ERROR_IS_OK(err)) {
184 && !(*pname = talloc_strdup(
185 mem_ctx, key->values->values[idx]->valuename))) {
194 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
195 const char *name, struct registry_value **pval)
200 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
201 return WERR_ACCESS_DENIED;
204 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
208 for (i=0; i<key->values->num_values; i++) {
209 if (strequal(key->values->values[i]->valuename, name)) {
210 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
217 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
218 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
219 uint32_t *num_values, uint32_t *max_valnamelen,
220 uint32_t *max_valbufsize, uint32_t *secdescsize,
221 NTTIME *last_changed_time)
227 struct security_descriptor *secdesc;
229 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
230 return WERR_ACCESS_DENIED;
233 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
234 !W_ERROR_IS_OK(fill_value_cache(key))) {
239 for (i=0; i<key->subkeys->num_subkeys; i++) {
240 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
243 *num_subkeys = key->subkeys->num_subkeys;
244 *max_subkeylen = max_len;
245 *max_subkeysize = 0; /* Class length? */
249 for (i=0; i<key->values->num_values; i++) {
250 max_len = MAX(max_len,
251 strlen(key->values->values[i]->valuename));
252 max_size = MAX(max_size, key->values->values[i]->size);
255 *num_values = key->values->num_values;
256 *max_valnamelen = max_len;
257 *max_valbufsize = max_size;
259 if (!(mem_ctx = talloc_new(key))) {
263 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
264 if (!W_ERROR_IS_OK(err)) {
265 TALLOC_FREE(mem_ctx);
269 *secdescsize = sec_desc_size(secdesc);
270 TALLOC_FREE(mem_ctx);
272 *last_changed_time = 0;
277 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
278 const char *subkeypath, uint32 desired_access,
279 struct registry_key **pkey,
280 enum winreg_CreateAction *paction)
282 struct registry_key *key = parent;
283 struct registry_key *create_parent;
287 REGSUBKEY_CTR *subkeys;
289 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
291 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
296 while ((end = strchr(path, '\\')) != NULL) {
297 struct registry_key *tmp;
298 enum winreg_CreateAction action;
302 err = reg_createkey(mem_ctx, key, path,
303 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
304 if (!W_ERROR_IS_OK(err)) {
317 * At this point, "path" contains the one-element subkey of "key". We
318 * can try to open it.
321 err = reg_openkey(ctx, key, path, desired_access, pkey);
322 if (W_ERROR_IS_OK(err)) {
323 if (paction != NULL) {
324 *paction = REG_OPENED_EXISTING_KEY;
329 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
331 * Something but "notfound" has happened, so bail out
337 * We have to make a copy of the current key, as we opened it only
338 * with ENUM_SUBKEY access.
341 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
343 if (!W_ERROR_IS_OK(err)) {
348 * Actually create the subkey
351 if (!(subkeys = TALLOC_ZERO_P(mem_ctx, REGSUBKEY_CTR))) {
356 err = fill_subkey_cache(create_parent);
357 if (!W_ERROR_IS_OK(err)) goto done;
359 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
360 if (!W_ERROR_IS_OK(err)) goto done;
362 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
363 TALLOC_FREE(create_parent->subkeys);
364 err = WERR_REG_IO_FAILURE;
369 * Now open the newly created key
372 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
373 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
374 *paction = REG_CREATED_NEW_KEY;
378 TALLOC_FREE(mem_ctx);
383 WERROR reg_deletekey(struct registry_key *parent, const char *path)
390 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
392 if (!(name = talloc_strdup(mem_ctx, path))) {
397 if ((end = strrchr(name, '\\')) != NULL) {
398 struct registry_key *tmp;
402 err = reg_openkey(mem_ctx, parent, name,
403 SEC_RIGHTS_CREATE_SUBKEY, &tmp);
404 if (!W_ERROR_IS_OK(err)) {
412 if (name[0] == '\0') {
413 err = WERR_INVALID_PARAM;
417 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
421 num_subkeys = parent->subkeys->num_subkeys;
423 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
428 if (!store_reg_keys(parent->key, parent->subkeys)) {
429 TALLOC_FREE(parent->subkeys);
430 err = WERR_REG_IO_FAILURE;
436 TALLOC_FREE(mem_ctx);
440 WERROR reg_setvalue(struct registry_key *key, const char *name,
441 const struct registry_value *val)
444 DATA_BLOB value_data;
447 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
448 return WERR_ACCESS_DENIED;
451 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
455 err = registry_push_value(key, val, &value_data);
456 if (!W_ERROR_IS_OK(err)) {
460 res = regval_ctr_addvalue(key->values, name, val->type,
461 (char *)value_data.data, value_data.length);
462 TALLOC_FREE(value_data.data);
465 TALLOC_FREE(key->values);
469 if (!store_reg_values(key->key, key->values)) {
470 TALLOC_FREE(key->values);
471 return WERR_REG_IO_FAILURE;
477 WERROR reg_deletevalue(struct registry_key *key, const char *name)
481 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
482 return WERR_ACCESS_DENIED;
485 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
489 regval_ctr_delvalue(key->values, name);
491 if (!store_reg_values(key->key, key->values)) {
492 TALLOC_FREE(key->values);
493 return WERR_REG_IO_FAILURE;