s4:registry - util.c - move the "REG_NONE" case in the conversion functions on top...
[Samba/nascimento.git] / source4 / lib / registry / util.c
blobba284de8db7531bd587f3e803d01ec3f78bbbf65
1 /*
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/>.
20 #include "includes.h"
21 #include "lib/registry/registry.h"
22 #include "librpc/gen_ndr/winreg.h"
24 /**
25 * @file
26 * @brief Registry utility functions
29 static const struct {
30 uint32_t id;
31 const char *name;
32 } reg_value_types[] = {
33 { REG_NONE, "REG_NONE" },
34 { REG_SZ, "REG_SZ" },
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" },
46 { 0, NULL }
49 /** Return string description of registry value type */
50 _PUBLIC_ const char *str_regtype(int type)
52 unsigned int i;
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;
58 return "Unknown";
61 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx,
62 struct smb_iconv_convenience *iconv_convenience,
63 uint32_t type,
64 const DATA_BLOB data)
66 char *ret = NULL;
68 if (data.length == 0)
69 return talloc_strdup(mem_ctx, "");
71 switch (type) {
72 case REG_NONE:
73 /* "NULL" is the right return value */
74 break;
75 case REG_EXPAND_SZ:
76 case REG_SZ:
77 if (data.length % 2 == 0) {
78 convert_string_talloc_convenience(mem_ctx,
79 iconv_convenience,
80 CH_UTF16, CH_UNIX,
81 data.data,
82 data.length,
83 (void **)&ret,
84 NULL, false);
86 break;
87 case REG_BINARY:
88 ret = data_blob_hex_string_upper(mem_ctx, &data);
89 break;
90 case REG_DWORD:
91 if (data.length == sizeof(uint32_t)) {
92 ret = talloc_asprintf(mem_ctx, "0x%8.8x",
93 IVAL(data.data, 0));
95 break;
96 case REG_QWORD:
97 if (data.length == sizeof(uint64_t)) {
98 ret = talloc_asprintf(mem_ctx, "0x%16.16llx",
99 BVAL(data.data, 0));
101 break;
102 case REG_MULTI_SZ:
103 /* FIXME: We don't support this yet */
104 break;
105 default:
106 /* FIXME */
107 /* Other datatypes aren't supported -> return "NULL" */
108 break;
111 return ret;
114 /** Generate a string that describes a registry value */
115 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
116 struct smb_iconv_convenience *iconv_convenience,
117 const char *name,
118 uint32_t data_type,
119 const DATA_BLOB data)
121 return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
122 str_regtype(data_type),
123 reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
126 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx,
127 struct smb_iconv_convenience *iconv_convenience,
128 const char *type_str,
129 const char *data_str, uint32_t *type,
130 DATA_BLOB *data)
132 unsigned int i;
133 *type = -1;
135 /* Find the correct type */
136 for (i = 0; reg_value_types[i].name; i++) {
137 if (!strcmp(reg_value_types[i].name, type_str)) {
138 *type = reg_value_types[i].id;
139 break;
143 if (*type == -1)
144 return false;
146 /* Convert data appropriately */
148 switch (*type) {
149 case REG_NONE:
150 ZERO_STRUCTP(data);
151 break;
152 case REG_SZ:
153 case REG_EXPAND_SZ:
154 convert_string_talloc_convenience(mem_ctx,
155 iconv_convenience,
156 CH_UNIX, CH_UTF16,
157 data_str,
158 strlen(data_str)+1,
159 (void **)&data->data,
160 &data->length, false);
161 break;
162 case REG_BINARY:
163 *data = strhex_to_data_blob(mem_ctx, data_str);
164 break;
165 case REG_DWORD: {
166 uint32_t tmp = strtol(data_str, NULL, 0);
167 *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
168 SIVAL(data->data, 0, tmp);
170 break;
171 case REG_QWORD: {
172 uint64_t tmp = strtoll(data_str, NULL, 0);
173 *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
174 SBVAL(data->data, 0, tmp);
176 break;
177 case REG_MULTI_SZ:
178 /* FIXME: We don't support this yet */
179 return false;
180 default:
181 /* FIXME */
182 /* Other datatypes aren't supported -> return no success */
183 return false;
185 return true;
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;
193 WERROR error;
194 size_t predeflength;
195 char *predefname;
197 if (strchr(name, '\\') != NULL)
198 predeflength = strchr(name, '\\')-name;
199 else
200 predeflength = strlen(name);
202 predefname = talloc_strndup(mem_ctx, name, predeflength);
203 error = reg_get_predefined_key_by_name(handle, predefname, &predef);
204 talloc_free(predefname);
206 if (!W_ERROR_IS_OK(error)) {
207 return error;
210 if (strchr(name, '\\')) {
211 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
212 result);
213 } else {
214 *result = predef;
215 return WERR_OK;
219 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
220 const char *path, struct registry_key **parent,
221 const char **name)
223 char *parent_name;
224 WERROR error;
226 if (strchr(path, '\\') == NULL) {
227 return WERR_FOOBAR;
230 parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
232 error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
233 if (!W_ERROR_IS_OK(error)) {
234 return error;
237 *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
239 return WERR_OK;
242 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
244 struct registry_key *parent;
245 const char *n;
246 TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
247 WERROR error;
249 if (!strchr(path, '\\')) {
250 return WERR_FOOBAR;
253 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
254 if (W_ERROR_IS_OK(error)) {
255 error = reg_key_del(parent, n);
258 talloc_free(mem_ctx);
260 return error;
263 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
264 const char *path, uint32_t access_mask,
265 struct security_descriptor *sec_desc,
266 struct registry_key **result)
268 struct registry_key *parent;
269 const char *n;
270 WERROR error;
272 if (!strchr(path, '\\')) {
273 return WERR_ALREADY_EXISTS;
276 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
277 if (!W_ERROR_IS_OK(error)) {
278 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
279 win_errstr(error)));
280 return error;
283 error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
285 return error;