s4:libregistry - change counters to be "unsigned"
[Samba/nascimento.git] / source4 / lib / registry / util.c
blob8a4bc92fe1d8ffaeee620a410a0ebd1c8076e162
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_SZ, "REG_SZ" },
34 { REG_DWORD, "REG_DWORD" },
35 { REG_BINARY, "REG_BINARY" },
36 { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
37 { REG_NONE, "REG_NONE" },
38 { 0, NULL }
41 /** Return string description of registry value type */
42 _PUBLIC_ const char *str_regtype(int type)
44 unsigned int i;
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;
50 return "Unknown";
53 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx,
54 struct smb_iconv_convenience *iconv_convenience,
55 uint32_t type,
56 const DATA_BLOB data)
58 char *ret = NULL;
60 if (data.length == 0)
61 return talloc_strdup(mem_ctx, "");
63 switch (type) {
64 case REG_EXPAND_SZ:
65 case REG_SZ:
66 convert_string_talloc_convenience(mem_ctx,
67 iconv_convenience,
68 CH_UTF16, CH_UNIX,
69 data.data,
70 data.length,
71 (void **)&ret,
72 NULL, false);
73 break;
74 case REG_BINARY:
75 ret = data_blob_hex_string_upper(mem_ctx, &data);
76 break;
77 case REG_DWORD:
78 if (IVAL(data.data, 0) == 0) {
79 ret = talloc_strdup(mem_ctx, "0");
80 } else {
81 ret = talloc_asprintf(mem_ctx, "0x%x",
82 IVAL(data.data, 0));
84 break;
85 case REG_NONE:
86 /* "NULL" is the right return value */
87 break;
88 case REG_MULTI_SZ:
89 /* FIXME: We don't support this yet */
90 break;
91 default:
92 /* FIXME */
93 /* Other datatypes aren't supported -> return "NULL" */
94 break;
97 return ret;
100 /** Generate a string that describes a registry value */
101 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx,
102 struct smb_iconv_convenience *iconv_convenience,
103 const char *name,
104 uint32_t data_type,
105 const DATA_BLOB data)
107 return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
108 str_regtype(data_type),
109 reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
112 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx,
113 struct smb_iconv_convenience *iconv_convenience,
114 const char *type_str,
115 const char *data_str, uint32_t *type,
116 DATA_BLOB *data)
118 unsigned int i;
119 *type = -1;
121 /* Find the correct type */
122 for (i = 0; reg_value_types[i].name; i++) {
123 if (!strcmp(reg_value_types[i].name, type_str)) {
124 *type = reg_value_types[i].id;
125 break;
129 if (*type == -1)
130 return false;
132 /* Convert data appropriately */
134 switch (*type) {
135 case REG_SZ:
136 case REG_EXPAND_SZ:
137 convert_string_talloc_convenience(mem_ctx,
138 iconv_convenience,
139 CH_UNIX, CH_UTF16,
140 data_str,
141 strlen(data_str)+1,
142 (void **)&data->data,
143 &data->length, false);
144 break;
145 case REG_BINARY:
146 *data = strhex_to_data_blob(mem_ctx, data_str);
147 break;
148 case REG_DWORD: {
149 uint32_t tmp = strtol(data_str, NULL, 0);
150 *data = data_blob_talloc(mem_ctx, NULL, 4);
151 SIVAL(data->data, 0, tmp);
153 break;
154 case REG_NONE:
155 ZERO_STRUCTP(data);
156 break;
157 case REG_MULTI_SZ:
158 /* FIXME: We don't support this yet */
159 return false;
160 default:
161 /* FIXME */
162 /* Other datatypes aren't supported -> return no success */
163 return false;
165 return true;
168 /** Open a key by name (including the predefined key name!) */
169 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
170 const char *name, struct registry_key **result)
172 struct registry_key *predef;
173 WERROR error;
174 size_t predeflength;
175 char *predefname;
177 if (strchr(name, '\\') != NULL)
178 predeflength = strchr(name, '\\')-name;
179 else
180 predeflength = strlen(name);
182 predefname = talloc_strndup(mem_ctx, name, predeflength);
183 error = reg_get_predefined_key_by_name(handle, predefname, &predef);
184 talloc_free(predefname);
186 if (!W_ERROR_IS_OK(error)) {
187 return error;
190 if (strchr(name, '\\')) {
191 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
192 result);
193 } else {
194 *result = predef;
195 return WERR_OK;
199 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
200 const char *path, struct registry_key **parent,
201 const char **name)
203 char *parent_name;
204 WERROR error;
206 if (strchr(path, '\\') == NULL) {
207 return WERR_FOOBAR;
210 parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
212 error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
213 if (!W_ERROR_IS_OK(error)) {
214 return error;
217 *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
219 return WERR_OK;
222 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
224 struct registry_key *parent;
225 const char *n;
226 TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
227 WERROR error;
229 if (!strchr(path, '\\')) {
230 return WERR_FOOBAR;
233 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
234 if (W_ERROR_IS_OK(error)) {
235 error = reg_key_del(parent, n);
238 talloc_free(mem_ctx);
240 return error;
243 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
244 const char *path, uint32_t access_mask,
245 struct security_descriptor *sec_desc,
246 struct registry_key **result)
248 struct registry_key *parent;
249 const char *n;
250 WERROR error;
252 if (!strchr(path, '\\')) {
253 return WERR_ALREADY_EXISTS;
256 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
257 if (!W_ERROR_IS_OK(error)) {
258 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
259 win_errstr(error)));
260 return error;
263 error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
265 return error;