20
21
#include "includes.h"
21
22
#include "lib/registry/registry.h"
22
23
#include "librpc/gen_ndr/winreg.h"
26
* @brief Registry utility functions
32
} reg_value_types[] = {
34
{ REG_DWORD, "REG_DWORD" },
35
{ REG_BINARY, "REG_BINARY" },
36
{ REG_EXPAND_SZ, "REG_EXPAND_SZ" },
37
{ REG_NONE, "REG_NONE" },
41
/** Return string description of registry value type */
42
_PUBLIC_ const char *str_regtype(int type)
45
for (i = 0; reg_value_types[i].name; i++) {
46
if (reg_value_types[i].id == type)
47
return reg_value_types[i].name;
53
_PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx,
54
struct smb_iconv_convenience *iconv_convenience,
24
#include "lib/util/data_blob.h"
26
_PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type,
56
27
const DATA_BLOB data)
64
35
case REG_EXPAND_SZ:
66
convert_string_talloc_convenience(mem_ctx, iconv_convenience, CH_UTF16, CH_UNIX,
37
convert_string_talloc(mem_ctx,
67
39
data.data, data.length,
68
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));
71
ret = data_blob_hex_string(mem_ctx, &data);
74
if (*(int *)data.data == 0)
75
return talloc_strdup(mem_ctx, "0");
76
return talloc_asprintf(mem_ctx, "0x%x",
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" */
88
71
/** Generate a string that describes a registry value */
89
_PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
90
struct smb_iconv_convenience *iconv_convenience,
72
_PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
92
74
uint32_t data_type,
93
75
const DATA_BLOB data)
95
77
return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
96
78
str_regtype(data_type),
97
reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
100
_PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx,
101
struct smb_iconv_convenience *iconv_convenience,
102
const char *type_str,
103
const char *data_str, uint32_t *type,
109
/* Find the correct type */
110
for (i = 0; reg_value_types[i].name; i++) {
111
if (!strcmp(reg_value_types[i].name, type_str)) {
112
*type = reg_value_types[i].id;
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) {
120
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);
125
168
case REG_EXPAND_SZ:
126
convert_string_talloc_convenience(mem_ctx, iconv_convenience, CH_UNIX, CH_UTF16,
127
data_str, strlen(data_str),
128
(void **)&data->data, &data->length, false);
132
uint32_t tmp = strtol(data_str, NULL, 0);
133
*data = data_blob_talloc(mem_ctx, &tmp, 4);
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);
138
188
ZERO_STRUCTP(data);
142
*data = strhex_to_data_blob(mem_ctx, data_str);
192
/* Other datatypes aren't supported -> return no success */
194
241
parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
242
W_ERROR_HAVE_NO_MEMORY(parent_name);
196
243
error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
244
talloc_free(parent_name);
197
245
if (!W_ERROR_IS_OK(error)) {
201
249
*name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
250
W_ERROR_HAVE_NO_MEMORY(*name);