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,
38 CH_UTF16, CH_UNIX, data.data, data.length,
39 (void **)&ret, NULL, false);
42 case REG_DWORD_BIG_ENDIAN:
43 SMB_ASSERT(data.length == sizeof(uint32_t));
44 ret = talloc_asprintf(mem_ctx, "0x%8.8x",
48 SMB_ASSERT(data.length == sizeof(uint64_t));
49 ret = talloc_asprintf(mem_ctx, "0x%16.16llx",
50 (long long)BVAL(data.data, 0));
53 ret = data_blob_hex_string_upper(mem_ctx, &data);
56 /* "NULL" is the right return value */
59 /* FIXME: We don't support this yet */
63 /* Other datatypes aren't supported -> return "NULL" */
70 /** Generate a string that describes a registry value */
71 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
76 return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
77 str_regtype(data_type),
78 reg_val_data_string(mem_ctx, data_type, data));
82 * This implements reading hex bytes that include comma's.
83 * It was previously handled by strhex_to_data_blob, but that did not cover
84 * the format used by windows.
86 static DATA_BLOB reg_strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *str)
89 const char *HEXCHARS = "0123456789ABCDEF";
93 ret = data_blob_talloc_zero(mem_ctx, (strlen(str)+(strlen(str) % 3))/3);
95 for (i = 0; i < strlen(str); i++) {
96 hi = strchr(HEXCHARS, toupper(str[i]));
101 lo = strchr(HEXCHARS, toupper(str[i]));
105 ret.data[j] = PTR_DIFF(hi, HEXCHARS) << 4;
106 ret.data[j] += PTR_DIFF(lo, HEXCHARS);
109 if (j > ret.length) {
110 DEBUG(0, ("Trouble converting hex string to bin\n"));
118 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str,
119 const char *data_str, uint32_t *type, DATA_BLOB *data)
121 char *tmp_type_str, *p, *q;
124 *type = regtype_by_string(type_str);
127 /* Normal windows format is hex, hex(type int as string),
128 dword or just a string. */
129 if (strncmp(type_str, "hex(", 4) == 0) {
130 /* there is a hex string with the value type between
132 tmp_type_str = talloc_strdup(mem_ctx, type_str);
133 q = p = tmp_type_str + strlen("hex(");
135 /* Go to the closing brace or end of the string */
136 while (*q != ')' && *q != '\0') q++;
139 /* Convert hex string to int, store it in type */
140 result = sscanf(p, "%x", type);
142 DEBUG(0, ("Could not convert hex to int\n"));
145 talloc_free(tmp_type_str);
146 } else if (strcmp(type_str, "hex") == 0) {
148 } else if (strcmp(type_str, "dword") == 0) {
156 /* Convert data appropriately */
160 return convert_string_talloc(mem_ctx,
161 CH_UNIX, CH_UTF16, data_str,
163 (void **)&data->data,
164 &data->length, false);
169 *data = reg_strhex_to_data_blob(mem_ctx, data_str);
172 case REG_DWORD_BIG_ENDIAN: {
173 uint32_t tmp = strtol(data_str, NULL, 16);
174 *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
175 if (data->data == NULL) return false;
176 SIVAL(data->data, 0, tmp);
180 uint64_t tmp = strtoll(data_str, NULL, 0);
181 *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
182 if (data->data == NULL) return false;
183 SBVAL(data->data, 0, tmp);
191 /* Other datatypes aren't supported -> return no success */
197 /** Open a key by name (including the predefined key name!) */
198 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
199 const char *name, struct registry_key **result)
201 struct registry_key *predef;
206 if (strchr(name, '\\') != NULL)
207 predeflength = strchr(name, '\\')-name;
209 predeflength = strlen(name);
211 predefname = talloc_strndup(mem_ctx, name, predeflength);
212 W_ERROR_HAVE_NO_MEMORY(predefname);
213 error = reg_get_predefined_key_by_name(handle, predefname, &predef);
214 talloc_free(predefname);
216 if (!W_ERROR_IS_OK(error)) {
220 if (strchr(name, '\\')) {
221 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
229 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
230 const char *path, struct registry_key **parent,
236 if (strchr(path, '\\') == NULL) {
240 parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
241 W_ERROR_HAVE_NO_MEMORY(parent_name);
242 error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
243 talloc_free(parent_name);
244 if (!W_ERROR_IS_OK(error)) {
248 *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
249 W_ERROR_HAVE_NO_MEMORY(*name);
254 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
256 struct registry_key *parent;
258 TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
261 if (!strchr(path, '\\')) {
265 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
266 if (W_ERROR_IS_OK(error)) {
267 error = reg_key_del(mem_ctx, parent, n);
270 talloc_free(mem_ctx);
275 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
276 const char *path, uint32_t access_mask,
277 struct security_descriptor *sec_desc,
278 struct registry_key **result)
280 struct registry_key *parent;
286 if (!strchr(path, '\\')) {
287 return WERR_ALREADY_EXISTS;
290 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
291 if (!W_ERROR_IS_OK(error)) {
292 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
297 error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);