2 Unix SMB/CIFS implementation.
3 Transparent registry backend handling
4 Copyright (C) Jelmer Vernooij 2003-2004.
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/>.
21 #include "lib/registry/registry.h"
22 #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, uint32_t type, DATA_BLOB *data)
57 if(data->length == 0) return talloc_strdup(mem_ctx, "");
62 convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX, data->data, data->length, (void **)&ret);
66 ret = data_blob_hex_string(mem_ctx, data);
70 if (*(int *)data->data == 0)
71 return talloc_strdup(mem_ctx, "0");
73 return talloc_asprintf(mem_ctx, "0x%x", *(int *)data->data);
86 /** Generate a string that describes a registry value */
87 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, struct registry_value *val)
89 return talloc_asprintf(mem_ctx, "%s = %s : %s", val->name?val->name:"<No Name>", str_regtype(val->data_type), reg_val_data_string(mem_ctx, val->data_type, &val->data));
92 _PUBLIC_ BOOL reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str, const char *data_str, uint32_t *type, DATA_BLOB *data)
97 /* Find the correct type */
98 for (i = 0; reg_value_types[i].name; i++) {
99 if (!strcmp(reg_value_types[i].name, type_str)) {
100 *type = reg_value_types[i].id;
108 /* Convert data appropriately */
114 data->length = convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16, data_str, strlen(data_str), (void **)&data->data);
118 uint32_t tmp = strtol(data_str, NULL, 0);
119 *data = data_blob_talloc(mem_ctx, &tmp, 4);
128 *data = strhex_to_data_blob(data_str);
129 talloc_steal(mem_ctx, data->data);
140 * Replace all \'s with /'s
142 char *reg_path_win2unix(char *path)
145 for(i = 0; path[i]; i++) {
146 if(path[i] == '\\') path[i] = '/';
151 * Replace all /'s with \'s
153 char *reg_path_unix2win(char *path)
156 for(i = 0; path[i]; i++) {
157 if(path[i] == '/') path[i] = '\\';
162 /** Open a key by name (including the predefined key name!) */
163 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle, const char *name, struct registry_key **result)
165 struct registry_key *predef;
170 if(strchr(name, '\\')) predeflength = strchr(name, '\\')-name;
171 else predeflength = strlen(name);
173 predefname = talloc_strndup(mem_ctx, name, predeflength);
174 error = reg_get_predefined_key_by_name(handle, predefname, &predef);
175 talloc_free(predefname);
177 if(!W_ERROR_IS_OK(error)) {
181 if (strchr(name, '\\')) {
182 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1, result);
189 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx, const char *path, struct registry_key **parent, const char **name)
194 if (strchr(path, '\\') == NULL) {
198 parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-1-path);
200 error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
201 if (!W_ERROR_IS_OK(error)) {
205 *name = talloc_strdup(mem_ctx, strchr(path, '\\')+1);
210 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
212 struct registry_key *parent;
214 TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
217 if (!strchr(path, '\\')) {
221 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
222 if (W_ERROR_IS_OK(error)) {
223 error = reg_key_del(parent, n);
226 talloc_free(mem_ctx);
231 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx, const char *path, uint32_t access_mask, struct security_descriptor *sec_desc, struct registry_key **result)
233 struct registry_key *parent;
237 if (!strchr(path, '\\')) {
241 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
242 if (W_ERROR_IS_OK(error)) {
243 error = reg_key_add_name(mem_ctx, parent, n, access_mask, sec_desc, result);