2 Unix SMB/CIFS implementation.
3 Transparent registry backend handling
4 Copyright (C) Jelmer Vernooij 2003-2007.
5 Copyright (C) Wilco Baan Hofman 2010.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/registry/registry.h"
23 #include "librpc/gen_ndr/winreg.h"
24 #include "lib/util/data_blob.h"
26 _PUBLIC_
char *reg_val_data_string(TALLOC_CTX
*mem_ctx
, uint32_t type
,
32 return talloc_strdup(mem_ctx
, "");
37 convert_string_talloc(mem_ctx
,
39 data
.data
, data
.length
,
40 (void **)&ret
, NULL
, false);
43 case REG_DWORD_BIG_ENDIAN
:
44 SMB_ASSERT(data
.length
== sizeof(uint32_t));
45 ret
= talloc_asprintf(mem_ctx
, "0x%8.8x",
49 SMB_ASSERT(data
.length
== sizeof(uint64_t));
50 ret
= talloc_asprintf(mem_ctx
, "0x%16.16llx",
51 (long long)BVAL(data
.data
, 0));
54 ret
= data_blob_hex_string_upper(mem_ctx
, &data
);
57 /* "NULL" is the right return value */
60 /* FIXME: We don't support this yet */
64 /* Other datatypes aren't supported -> return "NULL" */
71 /** Generate a string that describes a registry value */
72 _PUBLIC_
char *reg_val_description(TALLOC_CTX
*mem_ctx
,
77 return talloc_asprintf(mem_ctx
, "%s = %s : %s", name
?name
:"<No Name>",
78 str_regtype(data_type
),
79 reg_val_data_string(mem_ctx
, data_type
, data
));
83 * This implements reading hex bytes that include comma's.
84 * It was previously handled by strhex_to_data_blob, but that did not cover
85 * the format used by windows.
87 static DATA_BLOB
reg_strhex_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *str
)
90 const char *HEXCHARS
= "0123456789ABCDEF";
94 ret
= data_blob_talloc_zero(mem_ctx
, (strlen(str
)+(strlen(str
) % 3))/3);
96 for (i
= 0; i
< strlen(str
); i
++) {
97 hi
= strchr(HEXCHARS
, toupper(str
[i
]));
102 lo
= strchr(HEXCHARS
, toupper(str
[i
]));
106 ret
.data
[j
] = PTR_DIFF(hi
, HEXCHARS
) << 4;
107 ret
.data
[j
] += PTR_DIFF(lo
, HEXCHARS
);
110 if (j
> ret
.length
) {
111 DEBUG(0, ("Trouble converting hex string to bin\n"));
119 _PUBLIC_
bool reg_string_to_val(TALLOC_CTX
*mem_ctx
, const char *type_str
,
120 const char *data_str
, uint32_t *type
, DATA_BLOB
*data
)
122 char *tmp_type_str
, *p
, *q
;
125 *type
= regtype_by_string(type_str
);
128 /* Normal windows format is hex, hex(type int as string),
129 dword or just a string. */
130 if (strncmp(type_str
, "hex(", 4) == 0) {
131 /* there is a hex string with the value type between
133 tmp_type_str
= talloc_strdup(mem_ctx
, type_str
);
134 q
= p
= tmp_type_str
+ strlen("hex(");
136 /* Go to the closing brace or end of the string */
137 while (*q
!= ')' && *q
!= '\0') q
++;
140 /* Convert hex string to int, store it in type */
141 result
= sscanf(p
, "%x", type
);
143 DEBUG(0, ("Could not convert hex to int\n"));
146 talloc_free(tmp_type_str
);
147 } else if (strcmp(type_str
, "hex") == 0) {
149 } else if (strcmp(type_str
, "dword") == 0) {
157 /* Convert data appropriately */
161 return convert_string_talloc(mem_ctx
,
163 data_str
, strlen(data_str
)+1,
164 (void **)&data
->data
,
165 &data
->length
, false);
170 *data
= reg_strhex_to_data_blob(mem_ctx
, data_str
);
173 case REG_DWORD_BIG_ENDIAN
: {
174 uint32_t tmp
= strtol(data_str
, NULL
, 16);
175 *data
= data_blob_talloc(mem_ctx
, NULL
, sizeof(uint32_t));
176 if (data
->data
== NULL
) return false;
177 SIVAL(data
->data
, 0, tmp
);
181 uint64_t tmp
= strtoll(data_str
, NULL
, 16);
182 *data
= data_blob_talloc(mem_ctx
, NULL
, sizeof(uint64_t));
183 if (data
->data
== NULL
) return false;
184 SBVAL(data
->data
, 0, tmp
);
192 /* Other datatypes aren't supported -> return no success */
198 /** Open a key by name (including the predefined key name!) */
199 WERROR
reg_open_key_abs(TALLOC_CTX
*mem_ctx
, struct registry_context
*handle
,
200 const char *name
, struct registry_key
**result
)
202 struct registry_key
*predef
;
207 if (strchr(name
, '\\') != NULL
)
208 predeflength
= strchr(name
, '\\')-name
;
210 predeflength
= strlen(name
);
212 predefname
= talloc_strndup(mem_ctx
, name
, predeflength
);
213 W_ERROR_HAVE_NO_MEMORY(predefname
);
214 error
= reg_get_predefined_key_by_name(handle
, predefname
, &predef
);
215 talloc_free(predefname
);
217 if (!W_ERROR_IS_OK(error
)) {
221 if (strchr(name
, '\\')) {
222 return reg_open_key(mem_ctx
, predef
, strchr(name
, '\\')+1,
230 static WERROR
get_abs_parent(TALLOC_CTX
*mem_ctx
, struct registry_context
*ctx
,
231 const char *path
, struct registry_key
**parent
,
237 if (strchr(path
, '\\') == NULL
) {
241 parent_name
= talloc_strndup(mem_ctx
, path
, strrchr(path
, '\\')-path
);
242 W_ERROR_HAVE_NO_MEMORY(parent_name
);
243 error
= reg_open_key_abs(mem_ctx
, ctx
, parent_name
, parent
);
244 talloc_free(parent_name
);
245 if (!W_ERROR_IS_OK(error
)) {
249 *name
= talloc_strdup(mem_ctx
, strrchr(path
, '\\')+1);
250 W_ERROR_HAVE_NO_MEMORY(*name
);
255 WERROR
reg_key_del_abs(struct registry_context
*ctx
, const char *path
)
257 struct registry_key
*parent
;
259 TALLOC_CTX
*mem_ctx
= talloc_init("reg_key_del_abs");
262 if (!strchr(path
, '\\')) {
266 error
= get_abs_parent(mem_ctx
, ctx
, path
, &parent
, &n
);
267 if (W_ERROR_IS_OK(error
)) {
268 error
= reg_key_del(mem_ctx
, parent
, n
);
271 talloc_free(mem_ctx
);
276 WERROR
reg_key_add_abs(TALLOC_CTX
*mem_ctx
, struct registry_context
*ctx
,
277 const char *path
, uint32_t access_mask
,
278 struct security_descriptor
*sec_desc
,
279 struct registry_key
**result
)
281 struct registry_key
*parent
;
287 if (!strchr(path
, '\\')) {
288 return WERR_ALREADY_EXISTS
;
291 error
= get_abs_parent(mem_ctx
, ctx
, path
, &parent
, &n
);
292 if (!W_ERROR_IS_OK(error
)) {
293 DEBUG(2, ("Opening parent of %s failed with %s\n", path
,
298 error
= reg_key_add_name(mem_ctx
, parent
, n
, NULL
, sec_desc
, result
);