f9ea2a193435a7dbe8100bae1e19e520d33800fc
[samba.git] / source4 / lib / registry / util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Transparent registry backend handling
4    Copyright (C) Jelmer Vernooij                        2003-2007.
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "lib/registry/registry.h"
22 #include "librpc/gen_ndr/winreg.h"
23
24 /**
25  * @file
26  * @brief Registry utility functions
27  */
28
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 }
39 };
40
41 /** Return string description of registry value type */
42 _PUBLIC_ const char *str_regtype(int type)
43 {
44         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;
48         }
49
50         return "Unknown";
51 }
52
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)
57 {
58         char *ret = NULL;
59
60         if (data.length == 0)
61                 return talloc_strdup(mem_ctx, "");
62
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 (*(int *)data.data == 0) {
79                                 ret = talloc_strdup(mem_ctx, "0");
80                         } else {
81                                 ret = talloc_asprintf(mem_ctx, "0x%x",
82                                                       *(int *)data.data);
83                         }
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                         /* Other datatypes aren't supported -> return "NULL" */
93                         break;
94         }
95
96         return ret;
97 }
98
99 /** Generate a string that describes a registry value */
100 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, 
101                                    struct smb_iconv_convenience *iconv_convenience, 
102                                    const char *name,
103                                    uint32_t data_type,
104                                    const DATA_BLOB data)
105 {
106         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
107                                str_regtype(data_type),
108                                reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
109 }
110
111 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, 
112                                 struct smb_iconv_convenience *iconv_convenience,
113                                 const char *type_str,
114                                 const char *data_str, uint32_t *type,
115                                 DATA_BLOB *data)
116 {
117         int i;
118         *type = -1;
119
120         /* Find the correct type */
121         for (i = 0; reg_value_types[i].name; i++) {
122                 if (!strcmp(reg_value_types[i].name, type_str)) {
123                         *type = reg_value_types[i].id;
124                         break;
125                 }
126         }
127
128         if (*type == -1)
129                 return false;
130
131         /* Convert data appropriately */
132
133         switch (*type) {
134                 case REG_SZ:
135                 case REG_EXPAND_SZ:
136                         convert_string_talloc_convenience(mem_ctx,
137                                                           iconv_convenience,
138                                                           CH_UNIX, CH_UTF16,
139                                                           data_str,
140                                                           strlen(data_str)+1,
141                                                           (void **)&data->data,
142                                                           &data->length, false);
143                         break;
144                 case REG_BINARY:
145                         *data = strhex_to_data_blob(mem_ctx, data_str);
146                         break;
147                 case REG_DWORD: {
148                         uint32_t tmp = strtol(data_str, NULL, 0);
149                         *data = data_blob_talloc(mem_ctx, &tmp, 4);
150                         }
151                         break;
152                 case REG_NONE:
153                         ZERO_STRUCTP(data);
154                         break;
155                 case REG_MULTI_SZ:
156                         /* FIXME: We don't support this yet */
157                         return false;
158                 default:
159                         /* Other datatypes aren't supported -> return no success */
160                         return false;
161         }
162         return true;
163 }
164
165 /** Open a key by name (including the predefined key name!) */
166 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
167                         const char *name, struct registry_key **result)
168 {
169         struct registry_key *predef;
170         WERROR error;
171         int predeflength;
172         char *predefname;
173
174         if (strchr(name, '\\') != NULL)
175                 predeflength = strchr(name, '\\')-name;
176         else
177                 predeflength = strlen(name);
178
179         predefname = talloc_strndup(mem_ctx, name, predeflength);
180         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
181         talloc_free(predefname);
182
183         if (!W_ERROR_IS_OK(error)) {
184                 return error;
185         }
186
187         if (strchr(name, '\\')) {
188                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
189                                     result);
190         } else {
191                 *result = predef;
192                 return WERR_OK;
193         }
194 }
195
196 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
197                              const char *path, struct registry_key **parent,
198                              const char **name)
199 {
200         char *parent_name;
201         WERROR error;
202
203         if (strchr(path, '\\') == NULL) {
204                 return WERR_FOOBAR;
205         }
206
207         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
208
209         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
210         if (!W_ERROR_IS_OK(error)) {
211                 return error;
212         }
213
214         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
215
216         return WERR_OK;
217 }
218
219 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
220 {
221         struct registry_key *parent;
222         const char *n;
223         TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
224         WERROR error;
225
226         if (!strchr(path, '\\')) {
227                 return WERR_FOOBAR;
228         }
229
230         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
231         if (W_ERROR_IS_OK(error)) {
232                 error = reg_key_del(parent, n);
233         }
234
235         talloc_free(mem_ctx);
236
237         return error;
238 }
239
240 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
241                        const char *path, uint32_t access_mask,
242                        struct security_descriptor *sec_desc,
243                        struct registry_key **result)
244 {
245         struct registry_key *parent;
246         const char *n;
247         WERROR error;
248
249         if (!strchr(path, '\\')) {
250                 return WERR_ALREADY_EXISTS;
251         }
252
253         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
254         if (!W_ERROR_IS_OK(error)) {
255                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
256                                   win_errstr(error)));
257                 return error;
258         }
259
260         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
261
262         return error;
263 }