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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "lib/registry/registry.h"
23 #include "librpc/gen_ndr/winreg.h"
27 * @brief Registry utility functions
33 } reg_value_types[] = {
35 { REG_DWORD, "REG_DWORD" },
36 { REG_BINARY, "REG_BINARY" },
37 { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
38 { REG_NONE, "REG_NONE" },
42 /** Return string description of registry value type */
43 _PUBLIC_ const char *str_regtype(int type)
46 for (i = 0; reg_value_types[i].name; i++) {
47 if (reg_value_types[i].id == type)
48 return reg_value_types[i].name;
54 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type, DATA_BLOB *data)
58 if(data->length == 0) return talloc_strdup(mem_ctx, "");
63 convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX, data->data, data->length, (void **)&ret);
67 ret = data_blob_hex_string(mem_ctx, data);
71 if (*(int *)data->data == 0)
72 return talloc_strdup(mem_ctx, "0");
74 return talloc_asprintf(mem_ctx, "0x%x", *(int *)data->data);
87 /** Generate a string that describes a registry value */
88 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, struct registry_value *val)
90 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));
93 _PUBLIC_ BOOL reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str, const char *data_str, uint32_t *type, DATA_BLOB *data)
98 /* Find the correct type */
99 for (i = 0; reg_value_types[i].name; i++) {
100 if (!strcmp(reg_value_types[i].name, type_str)) {
101 *type = reg_value_types[i].id;
109 /* Convert data appropriately */
115 data->length = convert_string_talloc(mem_ctx, CH_UNIX, CH_UTF16, data_str, strlen(data_str), (void **)&data->data);
119 uint32_t tmp = strtol(data_str, NULL, 0);
120 *data = data_blob_talloc(mem_ctx, &tmp, 4);
129 *data = strhex_to_data_blob(data_str);
130 talloc_steal(mem_ctx, data->data);
141 * Replace all \'s with /'s
143 char *reg_path_win2unix(char *path)
146 for(i = 0; path[i]; i++) {
147 if(path[i] == '\\') path[i] = '/';
152 * Replace all /'s with \'s
154 char *reg_path_unix2win(char *path)
157 for(i = 0; path[i]; i++) {
158 if(path[i] == '/') path[i] = '\\';
163 /** Open a key by name (including the predefined key name!) */
164 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle, const char *name, struct registry_key **result)
166 struct registry_key *predef;
171 if(strchr(name, '\\')) predeflength = strchr(name, '\\')-name;
172 else predeflength = strlen(name);
174 predefname = strndup(name, predeflength);
175 error = reg_get_predefined_key_by_name(handle, predefname, &predef);
176 SAFE_FREE(predefname);
178 if(!W_ERROR_IS_OK(error)) {
182 if (strchr(name, '\\')) {
183 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1, result);
190 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx, const char *path, struct registry_key **parent, const char **name)
195 if (strchr(path, '\\') == NULL) {
199 parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-1-path);
201 error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
202 if (!W_ERROR_IS_OK(error)) {
206 *name = talloc_strdup(mem_ctx, strchr(path, '\\')+1);
211 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
213 struct registry_key *parent;
215 TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
218 if (!strchr(path, '\\')) {
222 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
223 if (W_ERROR_IS_OK(error)) {
224 error = reg_key_del(parent, n);
227 talloc_free(mem_ctx);
232 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)
234 struct registry_key *parent;
238 if (!strchr(path, '\\')) {
242 error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
243 if (W_ERROR_IS_OK(error)) {
244 error = reg_key_add_name(mem_ctx, parent, n, access_mask, sec_desc, result);