Fix registry parsing of strings to also support the windows method.
[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    Copyright (C) Wilco Baan Hofman                      2010.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/registry.h"
23 #include "librpc/gen_ndr/winreg.h"
24
25 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type,
26                                    const DATA_BLOB data)
27 {
28         char *ret = NULL;
29
30         if (data.length == 0)
31                 return talloc_strdup(mem_ctx, "");
32
33         switch (type) {
34                 case REG_EXPAND_SZ:
35                 case REG_SZ:
36                         convert_string_talloc(mem_ctx,
37                                                           CH_UTF16, CH_UNIX, data.data, data.length,
38                                                           (void **)&ret, NULL, false);
39                         break;
40                 case REG_DWORD:
41                 case REG_DWORD_BIG_ENDIAN:
42                         SMB_ASSERT(data.length == sizeof(uint32_t));
43                         ret = talloc_asprintf(mem_ctx, "0x%8.8x",
44                                               IVAL(data.data, 0));
45                         break;
46                 case REG_QWORD:
47                         SMB_ASSERT(data.length == sizeof(uint64_t));
48                         ret = talloc_asprintf(mem_ctx, "0x%16.16llx",
49                                               (long long)BVAL(data.data, 0));
50                         break;
51                 case REG_BINARY:
52                         ret = data_blob_hex_string_upper(mem_ctx, &data);
53                         break;
54                 case REG_NONE:
55                         /* "NULL" is the right return value */
56                         break;
57                 case REG_MULTI_SZ:
58                         /* FIXME: We don't support this yet */
59                         break;
60                 default:
61                         /* FIXME */
62                         /* Other datatypes aren't supported -> return "NULL" */
63                         break;
64         }
65
66         return ret;
67 }
68
69 /** Generate a string that describes a registry value */
70 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, 
71                                    const char *name,
72                                    uint32_t data_type,
73                                    const DATA_BLOB data)
74 {
75         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
76                                str_regtype(data_type),
77                                reg_val_data_string(mem_ctx, data_type, data));
78 }
79
80 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str,
81                                 const char *data_str, uint32_t *type, DATA_BLOB *data)
82 {
83         char *tmp_type_str, *p, *q;
84         int result;
85
86         *type = regtype_by_string(type_str);
87
88         if (*type == -1) {
89                 /* Normal windows format is hex, hex(type int as string),
90                    dword or just a string. */
91                 if (strncmp(type_str, "hex(", 4) == 0) {
92                         /* there is a hex string with the value type between
93                            the braces */
94                         tmp_type_str = talloc_strdup(mem_ctx, type_str);
95                         q = p = tmp_type_str + strlen("hex(");
96
97                         /* Go to the closing brace or end of the string */
98                         while (*q != ')' && *q != '\0') q++;
99                         *q = '\0';
100
101                         /* Convert hex string to int, store it in type */
102                         result = sscanf(p, "%x", type);
103                         if (!result) {
104                                 DEBUG(0, ("Could not convert hex to int\n"));
105                                 return false;
106                         }
107                         DEBUG(10, ("Found string type: %s: %d\n", p, *type));
108                         talloc_free(tmp_type_str);
109                 } else if (strcmp(type_str, "hex") == 0) {
110                         *type = REG_BINARY;
111                 } else if (strcmp(type_str, "dword") == 0) {
112                         *type = REG_DWORD;
113                 }
114         }
115
116         if (*type == -1)
117                 return false;
118
119         /* Convert data appropriately */
120
121         switch (*type) {
122                 case REG_SZ:
123                 case REG_EXPAND_SZ:
124                         return convert_string_talloc(mem_ctx,
125                                                                  CH_UNIX, CH_UTF16, data_str,
126                                                                  strlen(data_str)+1,
127                                                                  (void **)&data->data,
128                                                                  &data->length, false);
129                         break;
130                 case REG_BINARY:
131                         *data = strhex_to_data_blob(mem_ctx, data_str);
132                         break;
133                 case REG_DWORD:
134                 case REG_DWORD_BIG_ENDIAN: {
135                         uint32_t tmp = strtol(data_str, NULL, 0);
136                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
137                         if (data->data == NULL) return false;
138                         SIVAL(data->data, 0, tmp);
139                         }
140                         break;
141                 case REG_QWORD: {
142                         uint64_t tmp = strtoll(data_str, NULL, 0);
143                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
144                         if (data->data == NULL) return false;
145                         SBVAL(data->data, 0, tmp);
146                         }
147                         break;
148                 case REG_NONE:
149                         ZERO_STRUCTP(data);
150                         break;
151                 case REG_MULTI_SZ:
152                         /* FIXME: We don't support this yet */
153                         return false;
154                 default:
155                         /* FIXME */
156                         /* Other datatypes aren't supported -> return no success */
157                         return false;
158         }
159         return true;
160 }
161
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,
164                         const char *name, struct registry_key **result)
165 {
166         struct registry_key *predef;
167         WERROR error;
168         size_t predeflength;
169         char *predefname;
170
171         if (strchr(name, '\\') != NULL)
172                 predeflength = strchr(name, '\\')-name;
173         else
174                 predeflength = strlen(name);
175
176         predefname = talloc_strndup(mem_ctx, name, predeflength);
177         W_ERROR_HAVE_NO_MEMORY(predefname);
178         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
179         talloc_free(predefname);
180
181         if (!W_ERROR_IS_OK(error)) {
182                 return error;
183         }
184
185         if (strchr(name, '\\')) {
186                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
187                                     result);
188         } else {
189                 *result = predef;
190                 return WERR_OK;
191         }
192 }
193
194 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
195                              const char *path, struct registry_key **parent,
196                              const char **name)
197 {
198         char *parent_name;
199         WERROR error;
200
201         if (strchr(path, '\\') == NULL) {
202                 return WERR_FOOBAR;
203         }
204
205         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
206         W_ERROR_HAVE_NO_MEMORY(parent_name);
207         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
208         talloc_free(parent_name);
209         if (!W_ERROR_IS_OK(error)) {
210                 return error;
211         }
212
213         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
214         W_ERROR_HAVE_NO_MEMORY(*name);
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(mem_ctx, 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         *result = NULL;
250
251         if (!strchr(path, '\\')) {
252                 return WERR_ALREADY_EXISTS;
253         }
254
255         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
256         if (!W_ERROR_IS_OK(error)) {
257                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
258                                   win_errstr(error)));
259                 return error;
260         }
261
262         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
263
264         return error;
265 }