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"
26 * @brief Registry utility functions
32 } reg_value_types
[] = {
33 { REG_NONE
, "REG_NONE" },
35 { REG_EXPAND_SZ
, "REG_EXPAND_SZ" },
36 { REG_BINARY
, "REG_BINARY" },
37 { REG_DWORD
, "REG_DWORD" },
38 { REG_DWORD_BIG_ENDIAN
, "REG_DWORD_BIG_ENDIAN" },
39 { REG_LINK
, "REG_LINK" },
40 { REG_MULTI_SZ
, "REG_MULTI_SZ" },
41 { REG_RESOURCE_LIST
, "REG_RESOURCE_LIST" },
42 { REG_FULL_RESOURCE_DESCRIPTOR
, "REG_FULL_RESOURCE_DESCRIPTOR" },
43 { REG_RESOURCE_REQUIREMENTS_LIST
, "REG_RESOURCE_REQUIREMENTS_LIST" },
44 { REG_QWORD
, "REG_QWORD" },
49 /** Return string description of registry value type */
50 _PUBLIC_
const char *str_regtype(int type
)
53 for (i
= 0; reg_value_types
[i
].name
; i
++) {
54 if (reg_value_types
[i
].id
== type
)
55 return reg_value_types
[i
].name
;
61 _PUBLIC_
char *reg_val_data_string(TALLOC_CTX
*mem_ctx
,
62 struct smb_iconv_convenience
*iconv_convenience
,
69 return talloc_strdup(mem_ctx
, "");
74 convert_string_talloc_convenience(mem_ctx
,
83 case REG_DWORD_BIG_ENDIAN
:
84 SMB_ASSERT(data
.length
== sizeof(uint32_t));
85 ret
= talloc_asprintf(mem_ctx
, "0x%8.8x",
89 SMB_ASSERT(data
.length
== sizeof(uint64_t));
90 ret
= talloc_asprintf(mem_ctx
, "0x%16.16llx",
94 ret
= data_blob_hex_string_upper(mem_ctx
, &data
);
97 /* "NULL" is the right return value */
100 /* FIXME: We don't support this yet */
104 /* Other datatypes aren't supported -> return "NULL" */
111 /** Generate a string that describes a registry value */
112 _PUBLIC_
char *reg_val_description(TALLOC_CTX
*mem_ctx
,
113 struct smb_iconv_convenience
*iconv_convenience
,
116 const DATA_BLOB data
)
118 return talloc_asprintf(mem_ctx
, "%s = %s : %s", name
?name
:"<No Name>",
119 str_regtype(data_type
),
120 reg_val_data_string(mem_ctx
, iconv_convenience
, data_type
, data
));
123 _PUBLIC_
bool reg_string_to_val(TALLOC_CTX
*mem_ctx
,
124 struct smb_iconv_convenience
*iconv_convenience
,
125 const char *type_str
,
126 const char *data_str
, uint32_t *type
,
132 /* Find the correct type */
133 for (i
= 0; reg_value_types
[i
].name
; i
++) {
134 if (!strcmp(reg_value_types
[i
].name
, type_str
)) {
135 *type
= reg_value_types
[i
].id
;
143 /* Convert data appropriately */
148 return convert_string_talloc_convenience(mem_ctx
,
153 (void **)&data
->data
,
154 &data
->length
, false);
157 *data
= strhex_to_data_blob(mem_ctx
, data_str
);
160 case REG_DWORD_BIG_ENDIAN
: {
161 uint32_t tmp
= strtol(data_str
, NULL
, 0);
162 *data
= data_blob_talloc(mem_ctx
, NULL
, sizeof(uint32_t));
163 if (data
->data
== NULL
) return false;
164 SIVAL(data
->data
, 0, tmp
);
168 uint64_t tmp
= strtoll(data_str
, NULL
, 0);
169 *data
= data_blob_talloc(mem_ctx
, NULL
, sizeof(uint64_t));
170 if (data
->data
== NULL
) return false;
171 SBVAL(data
->data
, 0, tmp
);
178 /* FIXME: We don't support this yet */
182 /* Other datatypes aren't supported -> return no success */
188 /** Open a key by name (including the predefined key name!) */
189 WERROR
reg_open_key_abs(TALLOC_CTX
*mem_ctx
, struct registry_context
*handle
,
190 const char *name
, struct registry_key
**result
)
192 struct registry_key
*predef
;
197 if (strchr(name
, '\\') != NULL
)
198 predeflength
= strchr(name
, '\\')-name
;
200 predeflength
= strlen(name
);
202 predefname
= talloc_strndup(mem_ctx
, name
, predeflength
);
203 W_ERROR_HAVE_NO_MEMORY(predefname
);
204 error
= reg_get_predefined_key_by_name(handle
, predefname
, &predef
);
205 talloc_free(predefname
);
207 if (!W_ERROR_IS_OK(error
)) {
211 if (strchr(name
, '\\')) {
212 return reg_open_key(mem_ctx
, predef
, strchr(name
, '\\')+1,
220 static WERROR
get_abs_parent(TALLOC_CTX
*mem_ctx
, struct registry_context
*ctx
,
221 const char *path
, struct registry_key
**parent
,
227 if (strchr(path
, '\\') == NULL
) {
231 parent_name
= talloc_strndup(mem_ctx
, path
, strrchr(path
, '\\')-path
);
232 W_ERROR_HAVE_NO_MEMORY(parent_name
);
233 error
= reg_open_key_abs(mem_ctx
, ctx
, parent_name
, parent
);
234 talloc_free(parent_name
);
235 if (!W_ERROR_IS_OK(error
)) {
239 *name
= talloc_strdup(mem_ctx
, strrchr(path
, '\\')+1);
240 W_ERROR_HAVE_NO_MEMORY(*name
);
245 WERROR
reg_key_del_abs(struct registry_context
*ctx
, const char *path
)
247 struct registry_key
*parent
;
249 TALLOC_CTX
*mem_ctx
= talloc_init("reg_key_del_abs");
252 if (!strchr(path
, '\\')) {
256 error
= get_abs_parent(mem_ctx
, ctx
, path
, &parent
, &n
);
257 if (W_ERROR_IS_OK(error
)) {
258 error
= reg_key_del(mem_ctx
, parent
, n
);
261 talloc_free(mem_ctx
);
266 WERROR
reg_key_add_abs(TALLOC_CTX
*mem_ctx
, struct registry_context
*ctx
,
267 const char *path
, uint32_t access_mask
,
268 struct security_descriptor
*sec_desc
,
269 struct registry_key
**result
)
271 struct registry_key
*parent
;
277 if (!strchr(path
, '\\')) {
278 return WERR_ALREADY_EXISTS
;
281 error
= get_abs_parent(mem_ctx
, ctx
, path
, &parent
, &n
);
282 if (!W_ERROR_IS_OK(error
)) {
283 DEBUG(2, ("Opening parent of %s failed with %s\n", path
,
288 error
= reg_key_add_name(mem_ctx
, parent
, n
, NULL
, sec_desc
, result
);