2 Unix SMB/CIFS implementation.
3 Transparent registry backend handling
4 Copyright (C) Jelmer Vernooij 2003-2007.
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/>.
21 #include "lib/registry/registry.h"
22 #include "librpc/gen_ndr/winreg.h"
23 #include "param/param.h"
27 * @brief Registry utility functions
33 } reg_value_types[] = {
35 { REG_DWORD, "REG_DWORD" },
36 { REG_BINARY, "REG_BINARY" },
37 { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
38 { REG_NONE, "REG_NONE" },
42 /** Return string description of registry value type */
43 _PUBLIC_ const char *str_regtype(int type)
46 for (i = 0; reg_value_types[i].name; i++) {
47 if (reg_value_types[i].id == type)
48 return reg_value_types[i].name;
54 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx,
55 struct smb_iconv_convenience *iconv_convenience,
62 return talloc_strdup(mem_ctx, "");
67 convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF16, CH_UNIX,
68 data.data, data.length,
72 ret = data_blob_hex_string(mem_ctx, &data);
75 if (*(int *)data.data == 0)
76 return talloc_strdup(mem_ctx, "0");
77 return talloc_asprintf(mem_ctx, "0x%x",
89 /** Generate a string that describes a registry value */
90 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
91 struct smb_iconv_convenience *iconv_convenience,
96 return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
97 str_regtype(data_type),
98 reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
101 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx,
102 struct smb_iconv_convenience *iconv_convenience,
103 const char *type_str,
104 const char *data_str, uint32_t *type,
110 /* Find the correct type */
111 for (i = 0; reg_value_types[i].name; i++) {
112 if (!strcmp(reg_value_types[i].name, type_str)) {
113 *type = reg_value_types[i].id;
121 /* Convert data appropriately */
127 data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UNIX, CH_UTF16,
128 data_str, strlen(data_str),
129 (void **)&data->data);
133 uint32_t tmp = strtol(data_str, NULL, 0);
134 *data = data_blob_talloc(mem_ctx, &tmp, 4);
143 *data = strhex_to_data_blob(data_str);
144 talloc_steal(mem_ctx, data->data);
154 /** Open a key by name (including the predefined key name!) */
155 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
156 const char *name, struct registry_key **result)
158 struct registry_key *predef;
163 if (strchr(name, '\\') != NULL)
164 predeflength = strchr(name, '\\')-name;
166 predeflength = strlen(name);
168 predefname = talloc_strndup(mem_ctx, name, predeflength);
169 error = reg_get_predefined_key_by_name(handle, predefname, &predef);
170 talloc_free(predefname);
172 if (!W_ERROR_IS_OK(error)) {
176 if (strchr(name, '\\')) {
177 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
185 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
186 const char *path, struct registry_key **parent,
192 if (strchr(path, '\\') == NULL) {
196 parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
198 error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
199 if (!W_ERROR_IS_OK(error)) {
203 *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
208 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
210 struct registry_key *parent;
212 TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
215 if (!strchr(path, '\\')) {
219 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
220 if (W_ERROR_IS_OK(error)) {
221 error = reg_key_del(parent, n);
224 talloc_free(mem_ctx);
229 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
230 const char *path, uint32_t access_mask,
231 struct security_descriptor *sec_desc,
232 struct registry_key **result)
234 struct registry_key *parent;
238 if (!strchr(path, '\\')) {
239 return WERR_ALREADY_EXISTS;
242 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
243 if (!W_ERROR_IS_OK(error)) {
244 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
249 error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);